Add .copy_with(**kwargs) convenience method to manifests (#242)#1571
Open
deekshaNVIDIA wants to merge 1 commit into
Open
Add .copy_with(**kwargs) convenience method to manifests (#242)#1571deekshaNVIDIA wants to merge 1 commit into
deekshaNVIDIA wants to merge 1 commit into
Conversation
…ch#242) Lhotse treats manifest objects as immutable and widely uses the `fastcopy(obj, field=...)` idiom to create modified copies. This adds a `.copy_with(**kwargs)` member method so the same can be done without importing fastcopy and in a way that reads naturally and composes with comprehensions, e.g. `supervision.copy_with(text=...)`. The method is added to CustomFieldMixin (covering SupervisionSegment, DataCut/MonoCut/MultiCut, and text examples), to the base Cut class (covering PaddingCut and MixedCut; it delegates to the existing Cut.copy), and to Recording, Features, Array, and TemporalArray. Tests in test/test_copy_with.py verify, for every manifest type, that copy_with overwrites the requested field, leaves the original object unmutated, and matches fastcopy semantics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #242.
Lhotse treats manifest objects as (effectively) immutable and uses the
fastcopy(obj, field=...)idiom all over the codebase to produce a modified copy. As suggested in #242, this adds a.copy_with(**kwargs)member method so users can do the same without importingfastcopy, in a way that reads naturally and composes nicely with comprehensions:What changed
copy_with(**kwargs)was added to the core manifest types:CustomFieldMixin(lhotse/custom.py) — coversSupervisionSegment,DataCut/MonoCut/MultiCut, and the text examples.Cutbase class (lhotse/cut/base.py) — coversPaddingCutandMixedCut; delegates to the existingCut.copy()so cuts expose the same name as every other manifest (this is exactly thecut.copy_with(start=5.0)example from the issue).Recording,Features,Array,TemporalArray— thin wrappers aroundfastcopy.All implementations delegate to
fastcopy, so behavior/semantics are identical to the existing idiom.Tests
test/test_copy_with.pyparametrizes over every manifest type and verifies thatcopy_with:fastcopy(obj, **kwargs),SupervisionSegment.37 passedlocally. I also rantest/test_supervision_set.py,test/cut/test_padding_cut.py, andtest/features/test_temporal_array.pywith no regressions (one unrelated failure,test_serialize_padded_cut_set, is a Windows-onlyNamedTemporaryFilereopen quirk).black,isort --profile black, andflake8 --select=E9,F63,F7,F82are clean on the changed files.