def subset(self, first: Optional[int] = None, last: Optional[int] = None) -> "FeatureSet": """ Return a new ``FeatureSet`` according to the selected subset criterion. Only a single argument to ``subset`` is supported at this time. :param first: int, the number of first supervisions to keep. :param last: int, the number of last supervisions to keep. :return: a new ``FeatureSet`` with the subset results. """ assert exactly_one_not_null( first, last), "subset() can handle only one non-None arg." if first is not None: assert first > 0 out = FeatureSet.from_items(islice(self, first)) if len(out) < first: logging.warning( f"FeatureSet has only {len(out)} items but first {first} were requested." ) return out if last is not None: assert last > 0 if last > len(self): logging.warning( f"FeatureSet has only {len(self)} items but last {last} required; " f"not doing anything.") return self return FeatureSet.from_features(self.features[-last:])
def subset(self, first: Optional[int] = None, last: Optional[int] = None) -> 'SupervisionSet': """ Return a new ``SupervisionSet`` according to the selected subset criterion. Only a single argument to ``subset`` is supported at this time. :param first: int, the number of first supervisions to keep. :param last: int, the number of last supervisions to keep. :return: a new ``SupervisionSet`` with the subset results. """ assert exactly_one_not_null( first, last), "subset() can handle only one non-None arg." if first is not None: assert first > 0 if first > len(self): logging.warning( f'SupervisionSet has only {len(self)} items but first {first} required; ' f'not doing anything.') return self return SupervisionSet.from_segments(islice(self, first)) if last is not None: assert last > 0 if last > len(self): logging.warning( f'SupervisionSet has only {len(self)} items but last {last} required; ' f'not doing anything.') return self return SupervisionSet.from_segments( islice(self, len(self) - last, len(self)))
def __init__(self, extra_frames: Optional[int] = None, extra_samples: Optional[int] = None, extra_seconds: Optional[Seconds] = None, pad_feat_value: float = LOG_EPSILON, randomized: bool = False) -> None: """ ExtraPadding's constructor. :param extra_frames: The total number of frames to add to each cut. We will add half that number on each side of the cut ("both" directions padding). :param extra_samples: The total number of samples to add to each cut. We will add half that number on each side of the cut ("both" directions padding). :param extra_seconds: The total duration in seconds to add to each cut. We will add half that number on each side of the cut ("both" directions padding). :param pad_feat_value: When padding a cut with precomputed features, what value should be used for padding (the default is a very low log-energy). :param randomized: When ``True``, we will sample a value from a uniform distribution of ``[0, extra_X]`` for each cut (for samples/frames -- sample an int, for duration -- sample a float). """ assert exactly_one_not_null(extra_frames, extra_samples, extra_seconds), \ "For ExtraPadding, you have to specify exactly one of: frames, samples, or duration." self.extra_frames = extra_frames self.extra_samples = extra_samples self.extra_seconds = extra_seconds self.pad_feat_value = pad_feat_value self.randomized = randomized
def __post_init__(self) -> None: assert exactly_one_not_null(*self._constraints) or all( x is None for x in self._constraints) for c in self._constraints: assert is_none_or_gt(c, 0)