def simple_mixed_cut():
    return MixedCut(id='simple-mixed-cut',
                    tracks=[
                        MixTrack(cut=dummy_cut('cut0', duration=10.0)),
                        MixTrack(cut=dummy_cut('cut1', duration=10.0),
                                 offset=5.0),
                    ])
Beispiel #2
0
def simple_mixed_cut():
    return MixedCut(
        id="simple-mixed-cut",
        tracks=[
            MixTrack(cut=dummy_cut(0, duration=10.0)),
            MixTrack(cut=dummy_cut(1, duration=10.0), offset=5.0),
        ],
    )
Beispiel #3
0
def test_cut_set_extend_by():
    cut1 = dummy_cut(int(uuid4()), start=0.0, duration=0.5)
    cut2 = dummy_cut(int(uuid4()), start=0.2, duration=0.4)
    cut_set = CutSet.from_cuts([cut1, cut2])
    extended_cut_set = cut_set.extend_by(duration=0.3,
                                         direction="both",
                                         preserve_id=True)
    assert isclose(extended_cut_set[cut1.id].start, 0.0)
    assert isclose(extended_cut_set[cut1.id].end, 0.8)
    assert isclose(extended_cut_set[cut2.id].start, 0.0)
    assert isclose(extended_cut_set[cut2.id].end, 0.9)
def test_mixed_cut_fill_supervision_shrink():
    cut = dummy_cut(0, supervisions=[dummy_supervision(0)])
    cut = cut.mix(dummy_cut(1)).truncate(
        duration=0.5)  # cuts are 100% overlapping
    fcut = cut.fill_supervision(shrink_ok=True)
    # Original is not modified
    assert cut.supervisions[0].start == 0
    assert cut.supervisions[0].duration == 1
    # Result is modified
    assert fcut.supervisions[0].start == 0
    assert fcut.supervisions[0].duration == 0.5
Beispiel #5
0
def test_cut_pairs_sampler_2():
    cut_set = CutSet.from_cuts([
        dummy_cut(0, duration=10),
        dummy_cut(1, duration=20),
    ])
    sampler = CutPairsSampler(
        source_cuts=cut_set,
        target_cuts=cut_set,
        max_source_duration=50,
        max_target_duration=50,
    )
    batch = next(iter(sampler))
    assert len(batch) == 2
def cutset():
    return CutSet.from_cuts([
        # MonoCut
        dummy_cut(0, supervisions=[dummy_supervision(0)]),
        # PaddingCut
        PaddingCut('pad', duration=1.0, sampling_rate=16000, feat_value=-100,
                   num_frames=100, frame_shift=0.01, num_features=80, num_samples=16000),
        # MixedCut
        dummy_cut(0, supervisions=[dummy_supervision(0)]).mix(
            dummy_cut(1, supervisions=[dummy_supervision(1)]),
            offset_other_by=0.5,
            snr=10
        )
    ])
Beispiel #7
0
def test_token_collater(add_bos, add_eos):
    test_sentences = [
        "Testing the first sentence.",
        "Let's add some more punctuation, shall we?",
        "How about number 42!",
    ]

    cuts = CutSet.from_cuts(
        dummy_cut(idx,
                  idx,
                  supervisions=[dummy_supervision(idx, idx, text=sentence)])
        for idx, sentence in enumerate(test_sentences))

    token_collater = TokenCollater(cuts, add_bos=add_bos, add_eos=add_eos)
    tokens_batch, tokens_lens = token_collater(cuts)

    assert isinstance(tokens_batch, torch.LongTensor)
    assert isinstance(tokens_lens, torch.IntTensor)

    extend = int(add_bos) + int(add_eos)
    expected_len = len(max(test_sentences, key=len)) + extend
    assert tokens_batch.shape == (len(test_sentences), expected_len)
    assert torch.all(tokens_lens == torch.IntTensor(
        [len(sentence) + extend for sentence in test_sentences]))

    reconstructed = token_collater.inverse(tokens_batch, tokens_lens)
    assert reconstructed == test_sentences
Beispiel #8
0
def test_cut_set_decompose():
    c = dummy_cut(
        0,
        start=5.0,
        duration=10.0,
        supervisions=[
            dummy_supervision(0, start=0.0),
            dummy_supervision(1, start=6.5)
        ],
    )
    assert c.start == 5.0
    assert c.end == 15.0
    cuts = CutSet.from_cuts([c])

    recs, sups, feats = cuts.decompose()

    assert isinstance(recs, RecordingSet)
    assert len(recs) == 1
    assert recs[0].id == "dummy-recording-0000"

    assert isinstance(sups, SupervisionSet)
    assert len(sups) == 2
    assert sups[0].id == "dummy-segment-0000"
    assert sups[0].start == 5.0
    assert sups[0].end == 6.0
    assert sups[1].id == "dummy-segment-0001"
    assert sups[1].start == 11.5
    assert sups[1].end == 12.5

    assert isinstance(feats, FeatureSet)
    assert len(feats) == 1
Beispiel #9
0
def dummy_cut_with_supervisions():
    return dummy_cut(
        unique_id=0,
        supervisions=[
            dummy_supervision(unique_id=i, duration=i) for i in range(1, 7)
        ],
    )
Beispiel #10
0
def test_cut_split():
    cut = dummy_cut(0, start=1.0, duration=4.0)
    left, right = cut.split(2.0)
    assert left.start == pytest.approx(1.0)
    assert left.end == pytest.approx(3.0)
    assert right.start == pytest.approx(3.0)
    assert right.end == pytest.approx(5.0)
Beispiel #11
0
def test_extend_by_cut_with_supervision(
    cut_start,
    cut_duration,
    extend_duration,
    extend_direction,
    supervision_start,
    supervision_duration,
    expected_start,
    expected_end,
):
    recording = dummy_recording(int(uuid4()), duration=1.0)
    supervisions = SupervisionSet.from_segments([
        SupervisionSegment(
            id=int(uuid4()),
            recording_id=recording.id,
            start=supervision_start,
            duration=supervision_duration,
        )
    ])
    cut = dummy_cut(int(uuid4()),
                    start=cut_start,
                    duration=cut_duration,
                    supervisions=supervisions)
    extended_cut = cut.extend_by(duration=extend_duration,
                                 direction=extend_direction)
    assert isclose(extended_cut.supervisions[0].start, expected_start)
    assert isclose(extended_cut.supervisions[0].end, expected_end)
Beispiel #12
0
def test_time_constraint_strictness():
    normal = TimeConstraint(max_duration=100, strict=False)
    strict = TimeConstraint(max_duration=100, strict=True)

    # create cuts with large variance of durations
    cut_durs = [30.0, 30.0, 10.0, 10.0, 20.0]
    assert sum(cut_durs) == pytest.approx(100.0)
    cuts = [dummy_cut(idx, duration=cd) for idx, cd in enumerate(cut_durs)]

    # accumulate 80s of duration
    for cut in cuts[:-1]:
        normal.add(cut)
        strict.add(cut)

    assert normal.current == pytest.approx(80)
    assert strict.current == pytest.approx(80)

    # non-strict constraint is not close to exceeding (will accept next cut in a batch)
    # strict constraint is close to exceeding (will not accept next cut in a batch)
    assert not normal.close_to_exceeding()
    assert strict.close_to_exceeding()

    normal.add(cuts[-1])
    strict.add(cuts[-1])

    assert not normal.exceeded()
    assert strict.exceeded()
def test_mixed_cut_fill_supervision_raises_on_two_supervisions():
    cut = dummy_cut(0,
                    supervisions=[dummy_supervision(0),
                                  dummy_supervision(1)])
    cut = cut.pad(duration=10)
    with pytest.raises(AssertionError):
        fcut = cut.fill_supervision()
def test_mono_cut_fill_supervision_add_empty_true():
    cut = dummy_cut(0)
    fcut = cut.fill_supervision()
    # Original is not modified
    assert len(cut.supervisions) == 0
    # Result is modified
    assert fcut.supervisions[0].start == 0
    assert fcut.supervisions[0].duration == 1
def test_cut_with_audio_move_to_memory():
    path = "test/fixtures/mono_c0.wav"
    cut = dummy_cut(0, duration=0.5).drop_recording()
    cut.recording = Recording.from_file(path)

    memory_cut = cut.move_to_memory()

    np.testing.assert_equal(memory_cut.load_audio(), cut.load_audio())
Beispiel #16
0
def test_cut_set_decompose_output_dir_doesnt_duplicate_recording():
    c = dummy_cut(0)
    c2 = dummy_cut(0)
    c2.id = "dummy-cut-0001"  # override cut ID, retain identical recording ID as `c`
    cuts = CutSet.from_cuts([c, c2])

    with TemporaryDirectory() as td:
        td = Path(td)
        cuts.decompose(output_dir=td)

        text = load_jsonl(td / "recordings.jsonl.gz")
        print(list(text))

        recs = load_manifest(td / "recordings.jsonl.gz")
        assert isinstance(recs, RecordingSet)
        # deduplicated recording
        assert len(recs) == 1
        assert recs[0].id == "dummy-recording-0000"
Beispiel #17
0
def test_extend_by_cut_preserve_id(preserve_id):
    cut = dummy_cut(int(uuid4()), start=0.0, duration=0.5)
    extended_cut = cut.extend_by(duration=0.3,
                                 direction="right",
                                 preserve_id=preserve_id)
    if preserve_id:
        assert extended_cut.id == cut.id
    else:
        assert extended_cut.id != cut.id
Beispiel #18
0
def test_cut_truncate_offset_with_nonzero_start():
    cut = dummy_cut(0, start=1.0, duration=4.0)
    left = cut.truncate(duration=2.0)
    assert left.start == pytest.approx(1.0)
    assert left.end == pytest.approx(3.0)

    right = cut.truncate(offset=2.0)
    assert right.start == pytest.approx(3.0)
    assert right.end == pytest.approx(5.0)
Beispiel #19
0
def test_cut_set_decompose_doesnt_duplicate_recording():
    c = dummy_cut(0)
    c2 = dummy_cut(0)
    c2.id = "dummy-cut-0001"  # override cut ID, retain identical recording ID as `c`
    cuts = CutSet.from_cuts([c, c2])

    recs, sups, feats = cuts.decompose()

    assert isinstance(recs, RecordingSet)
    # deduplicated recording
    assert len(recs) == 1
    assert recs[0].id == "dummy-recording-0000"

    assert sups is None

    assert isinstance(feats, FeatureSet)
    # not deduplicated features
    assert len(feats) == 2
def test_cut_with_audio_move_to_memory_large_offset():
    path = "test/fixtures/mono_c0.wav"
    cut = dummy_cut(0, duration=0.1).drop_recording()
    cut.recording = Recording.from_file(path)
    cut.start = 0.4
    assert isclose(cut.end, 0.5)

    memory_cut = cut.move_to_memory()

    np.testing.assert_equal(memory_cut.load_audio(), cut.load_audio())
def test_mono_cut_fill_supervision_shrink():
    cut = dummy_cut(0, supervisions=[dummy_supervision(0)])
    cut.duration = 0.5
    fcut = cut.fill_supervision(shrink_ok=True)
    # Original is not modified
    assert cut.supervisions[0].start == 0
    assert cut.supervisions[0].duration == 1
    # Result is modified
    assert fcut.supervisions[0].start == 0
    assert fcut.supervisions[0].duration == 0.5
def test_mixed_cut_fill_supervision_expand():
    cut = dummy_cut(0, supervisions=[dummy_supervision(0)])
    cut = cut.pad(duration=7.51)
    fcut = cut.fill_supervision()
    # Original is not modified
    assert cut.supervisions[0].start == 0
    assert cut.supervisions[0].duration == 1
    # Result is modified
    assert fcut.supervisions[0].start == 0
    assert fcut.supervisions[0].duration == 7.51
Beispiel #23
0
def test_single_cut_sampler_len():
    # total duration is 55 seconds
    # each second has 100 frames
    cuts = CutSet.from_cuts(dummy_cut(idx, duration=float(idx)) for idx in range(1, 11))
    sampler = SimpleCutSampler(cuts, shuffle=True, max_frames=10 * 100, max_cuts=6)

    for epoch in range(5):
        sampler.set_epoch(epoch)
        sampler_len = len(sampler)
        num_batches = len([batch for batch in sampler])
        assert sampler_len == num_batches
def test_extend_by_cut(
    cut_start,
    cut_duration,
    extend_duration,
    extend_direction,
    expected_start,
    expected_end,
):
    cut = dummy_cut(int(uuid4()), start=cut_start, duration=cut_duration)
    extended_cut = cut.extend_by(duration=extend_duration, direction=extend_direction)
    assert isclose(extended_cut.start, expected_start)
    assert isclose(extended_cut.end, expected_end)
Beispiel #25
0
def test_cut_load_array_truncate():
    """Check that loading a custom Array works after truncation."""
    ivector = np.arange(20).astype(np.float32)
    with NamedTemporaryFile(suffix=".h5") as f, LilcomHdf5Writer(
            f.name) as writer:
        cut = dummy_cut(0, duration=5.0)
        cut.ivector = writer.store_array(key="utt1", value=ivector)

        cut = cut.truncate(duration=3)

        restored_ivector = cut.load_ivector()
        np.testing.assert_equal(ivector, restored_ivector)
def test_cut_move_to_memory_audio_serialization():
    path = "test/fixtures/mono_c0.wav"
    cut = dummy_cut(0, duration=0.5).drop_recording()
    cut.recording = Recording.from_file(path)

    cut_with_audio = cut.move_to_memory()

    assert cut.custom is None  # original cut is unmodified

    data = cut_with_audio.to_dict()
    cut_deserialized = MonoCut.from_dict(data)

    np.testing.assert_equal(cut_deserialized.load_audio(),
                            cut_with_audio.load_audio())
Beispiel #27
0
def test_cut_pairs_sampler_len():
    # total duration is 55 seconds
    # each second has 100 frames
    cuts = CutSet.from_cuts(dummy_cut(idx, duration=float(idx)) for idx in range(1, 11))
    sampler = CutPairsSampler(
        source_cuts=cuts,
        target_cuts=cuts,
        shuffle=True,
        max_source_frames=10 * 100,
        max_target_frames=10 * 100,
    )

    for epoch in range(5):
        assert len(sampler) == len([batch for batch in sampler])
        sampler.set_epoch(epoch)
Beispiel #28
0
def test_bucketing_sampler_len():
    # total duration is 550 seconds
    # each second has 100 frames
    cuts = CutSet.from_cuts(
        dummy_cut(idx, duration=float(duration))
        for idx, duration in enumerate(list(range(1, 11)) * 10))

    sampler = BucketingSampler(cuts,
                               num_buckets=4,
                               shuffle=True,
                               max_frames=64 * 100,
                               max_cuts=6)

    for epoch in range(5):
        assert len(sampler) == len([item for item in sampler])
        sampler.set_epoch(epoch)
Beispiel #29
0
def test_cut_load_temporal_array_truncate():
    """Check the array loaded via TemporalArray is truncated along with the cut."""
    with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(
            f.name) as writer:
        expected_duration = 52.4  # 131 frames x 0.4s frame shift == 52.4s
        cut = dummy_cut(0, duration=expected_duration)

        alignment = np.random.randint(500, size=131)
        cut.alignment = writer.store_array(key="utt1",
                                           value=alignment,
                                           frame_shift=0.4,
                                           temporal_dim=0)
        cut_trunc = cut.truncate(duration=5.0)

        alignment_piece = cut_trunc.load_alignment()
        assert alignment_piece.shape == (13, )  # 5.0 / 0.4 == 12.5 ~= 13
        np.testing.assert_equal(alignment[:13], alignment_piece)
Beispiel #30
0
def test_concat_cuts():
    cuts = [
        dummy_cut(duration=30.0),
        dummy_cut(duration=20.0),
        dummy_cut(duration=10.0),
        dummy_cut(duration=5.0),
        dummy_cut(duration=4.0),
        dummy_cut(duration=3.0),
        dummy_cut(duration=2.0),
    ]
    concat = concat_cuts(cuts, gap=1.0)
    assert [c.duration for c in concat] == [
        30.0,
        20.0 + 1.0 + 2.0 + 1.0 + 3.0,  # == 27.0
        10.0 + 1.0 + 4.0 + 1.0 + 5.0,  # == 21.0
    ]