def deserialize_item(data: dict) -> Any: # Figures out what type of manifest is being decoded with some heuristics # and returns a Lhotse manifest object rather than a raw dict. from lhotse import Features, MonoCut, Recording, SupervisionSegment from lhotse.array import deserialize_array from lhotse.cut import MixedCut if "shape" in data or "array" in data: return deserialize_array(data) if "sources" in data: return Recording.from_dict(data) if "num_features" in data: return Features.from_dict(data) if "type" not in data: return SupervisionSegment.from_dict(data) cut_type = data.pop("type") if cut_type == "MonoCut": return MonoCut.from_dict(data) if cut_type == "Cut": warnings.warn( "Your manifest was created with Lhotse version earlier than v0.8, when MonoCut was called Cut. " "Please re-generate it with Lhotse v0.8 as it might stop working in a future version " "(using manifest.from_file() and then manifest.to_file() should be sufficient)." ) return MonoCut.from_dict(data) if cut_type == "MixedCut": return MixedCut.from_dict(data) raise ValueError( f"Unexpected cut type during deserialization: '{cut_type}'")
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())
def deserialize_item(data: dict) -> Any: # Figures out what type of manifest is being decoded with some heuristics # and returns a Lhotse manifest object rather than a raw dict. from lhotse import MonoCut, Features, Recording, SupervisionSegment from lhotse.cut import MixedCut data = arr2list_recursive(data) if 'sources' in data: return Recording.from_dict(data) if 'num_features' in data: return Features.from_dict(data) if 'type' not in data: return SupervisionSegment.from_dict(data) cut_type = data.pop('type') if cut_type == 'MonoCut': return MonoCut.from_dict(data) if cut_type == 'Cut': warnings.warn('Your manifest was created with Lhotse version earlier than v0.8, when MonoCut was called Cut. ' 'Please re-generate it with Lhotse v0.8 as it might stop working in a future version ' '(using manifest.from_file() and then manifest.to_file() should be sufficient).') return MonoCut.from_dict(data) if cut_type == 'MixedCut': return MixedCut.from_dict(data) raise ValueError(f"Unexpected cut type during deserialization: '{cut_type}'")