def t_stop_ds(ds: Dataset, t: float):
    "Dummy-event for the end of the last step"
    t_stop = ds.info['tstop'] + t
    out = {}
    for k, v in ds.items():
        if k == 'time':
            out['time'] = Var([t_stop])
        elif isinstance(v, Var):
            out[k] = Var(numpy.asarray([0], v.x.dtype))
        elif isinstance(v, Factor):
            out[k] = Factor([''])
        else:
            raise ValueError(f"{k!r} in predictor: {v!r}")
    return Dataset(out)
Exemple #2
0
    def align_word_dataset(
        self,
        ds: Dataset,
        words: FactorArg = 'word',
    ) -> Dataset:
        """Align ``ds`` to the TextGrid

        Parameters
        ----------
        ds
            Dataset with data to align.
        words
            Words in ``ds`` to use to align to the TextGrid words.

        Returns
        -------
        aligned_ds
            Dataset with the variables in ``ds`` aligned to the TextGrid,
            including time stamps and TextGrid words.
        """
        words_ = asfactor(words, ds=ds)
        index = self._align_index(words_, silence=-1, missing=-2)
        out = Dataset(
            {
                'time': Var([r.times[0] for r in self.realizations]),
                'grid_word': Factor([r.graphs for r in self.realizations]),
            },
            info={'tstop': self.realizations[-1].tstop})
        for key, variable in ds.items():
            if isinstance(variable, (Var, Factor)):
                values = dict(enumerate(variable))
                if isinstance(variable, Var):
                    values[-1] = values[
                        -2] = False  # coerced to 0 unless all values are boolean
                    out[key] = Var([values[i] for i in index])
                else:
                    values[-1] = values[-2] = ''
                    out[key] = Factor([values[i] for i in index],
                                      random=variable.random)
        return out