Skip to content

Add .copy_with(**kwargs) convenience method to manifests (#242)#1571

Open
deekshaNVIDIA wants to merge 1 commit into
lhotse-speech:masterfrom
deekshaNVIDIA:add-copy-with-method
Open

Add .copy_with(**kwargs) convenience method to manifests (#242)#1571
deekshaNVIDIA wants to merge 1 commit into
lhotse-speech:masterfrom
deekshaNVIDIA:add-copy-with-method

Conversation

@deekshaNVIDIA

Copy link
Copy Markdown

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 importing fastcopy, in a way that reads naturally and composes nicely with comprehensions:

sup2 = supervision.copy_with(text="new text")
cut2 = cut.copy_with(start=5.0)
rec2 = recording.copy_with(id="new-id")

What changed

copy_with(**kwargs) was added to the core manifest types:

  • CustomFieldMixin (lhotse/custom.py) — covers SupervisionSegment, DataCut/MonoCut/MultiCut, and the text examples.
  • Cut base class (lhotse/cut/base.py) — covers PaddingCut and MixedCut; delegates to the existing Cut.copy() so cuts expose the same name as every other manifest (this is exactly the cut.copy_with(start=5.0) example from the issue).
  • Recording, Features, Array, TemporalArray — thin wrappers around fastcopy.

All implementations delegate to fastcopy, so behavior/semantics are identical to the existing idiom.

Tests

test/test_copy_with.py parametrizes over every manifest type and verifies that copy_with:

  • overwrites the requested field,
  • returns a new object and does not mutate the original,
  • matches fastcopy(obj, **kwargs),
  • is a no-op-equivalent when called with no kwargs,
  • and can set custom fields on SupervisionSegment.

37 passed locally. I also ran test/test_supervision_set.py, test/cut/test_padding_cut.py, and test/features/test_temporal_array.py with no regressions (one unrelated failure, test_serialize_padded_cut_set, is a Windows-only NamedTemporaryFile reopen quirk). black, isort --profile black, and flake8 --select=E9,F63,F7,F82 are clean on the changed files.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor fastcopy() into a member method .copy_with()

1 participant