def test_cut_load_temporal_array(): """Check that we can read a TemporalArray from a cut when their durations match.""" alignment = np.random.randint(500, size=131) with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer: manifest = writer.store_array( key="utt1", value=alignment, frame_shift=0.4, temporal_dim=0 ) expected_duration = 52.4 # 131 frames x 0.4s frame shift == 52.4s cut = MonoCut(id="x", start=0, duration=expected_duration, channel=0) # Note: MonoCut doesn't normally have an "alignment" attribute, # and a "load_alignment()" method. # We are dynamically extending it. cut.alignment = manifest restored_alignment = cut.load_alignment() np.testing.assert_equal(alignment, restored_alignment)
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 = MonoCut(id="x", start=0, duration=expected_duration, channel=0) 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)
def test_cut_load_temporal_array_pad(pad_value): """Check the array loaded via TemporalArray is padded along with the cut.""" with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer(f.name) as writer: cut = MonoCut( id="x", start=0, duration=52.4, # 131 frames x 0.4s frame shift == 52.4s channel=0, recording=dummy_recording(1), ) alignment = np.random.randint(500, size=131) cut.alignment = writer.store_array( key="utt1", value=alignment, frame_shift=0.4, temporal_dim=0 ) cut_pad = cut.pad(duration=60.0, pad_value_dict={"alignment": pad_value}) alignment_pad = cut_pad.load_alignment() assert alignment_pad.shape == (150,) # 60.0 / 0.4 == 150 np.testing.assert_equal(alignment_pad[:131], alignment) np.testing.assert_equal(alignment_pad[131:], pad_value)
def test_validate_cut_with_temporal_array(caplog): # Note: "caplog" is a special variable in pytest that captures logs. caplog.set_level(logging.WARNING) with NamedTemporaryFile(suffix=".h5") as f, NumpyHdf5Writer( f.name) as writer: cut = MonoCut( id="cut1", start=0, duration=4.9, channel=0, recording=dummy_recording(1), ) alignment = np.random.randint(500, size=131) cut.alignment = writer.store_array(key="utt1", value=alignment, frame_shift=0.4, temporal_dim=0) validate(cut) assert ("MonoCut cut1: possibly mismatched duration between cut (4.9s) " "and temporal array in custom field 'alignment' (num_frames=131 " "* frame_shift=0.4 == duration=52.400000000000006)" in caplog.text)