Skip to content

Commit

Permalink
BoxedAnnotations.concat should retain audio_file and annotation_file …
Browse files Browse the repository at this point in the history
…lists

concatenates the lists from joined files, handling cases where the value is None instead of list

updated tests

resolves #1100
  • Loading branch information
sammlapp committed Feb 6, 2025
1 parent 20fa94a commit e13f478
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 16 additions & 7 deletions opensoundscape/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,13 +1176,22 @@ def convert_labels(self, conversion_table):
def concat(cls, list_of_boxed_annotations):
"""concatenate a list of BoxedAnnotations objects into one"""
dfs = [ba.df for ba in list_of_boxed_annotations]
return cls(pd.concat(dfs).reset_index(drop=True))

@classmethod
def concat(cls, list_of_boxed_annotations):
"""concatenate a list of BoxedAnnotations objects into one"""
dfs = [ba.df for ba in list_of_boxed_annotations]
return cls(pd.concat(dfs).reset_index(drop=True))
audio_files = []
annotation_files = []
for ba in list_of_boxed_annotations:
if ba.audio_files is not None:
audio_files.extend(ba.audio_files)
if ba.annotation_files is not None:
annotation_files.extend(ba.annotation_files)
if len(audio_files) == 0:
audio_files = None
if len(annotation_files) == 0:
annotation_files = None
return cls(
pd.concat(dfs).reset_index(drop=True),
audio_files=audio_files,
annotation_files=annotation_files,
)


def diff(base_annotations, comparison_annotations):
Expand Down
14 changes: 12 additions & 2 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def boxed_annotations():
)
return BoxedAnnotations(
df,
audio_files=["audio_file.wav"] * 3,
annotation_files=["audio_file.annotations.txt"] * 3,
audio_files=["audio_file.wav"],
annotation_files=["audio_file.annotations.txt"],
)


Expand Down Expand Up @@ -186,6 +186,16 @@ def isnan(x):
def test_concat_boxed_annotations(boxed_annotations):
joined = BoxedAnnotations.concat([boxed_annotations] * 3)
assert len(joined.df) == 9
assert len(joined.audio_files) == 3
assert len(joined.annotation_files) == 3

# handles scenario where audio_files and/or annotation_files are None
boxed_annotations.annotation_files = None
boxed_annotations.audio_files = None
joined = BoxedAnnotations.concat([boxed_annotations] * 3)
assert len(joined.df) == 9
assert joined.audio_files is None
assert joined.annotation_files is None


def test_load_raven_annotations_w_audio(raven_file):
Expand Down

0 comments on commit e13f478

Please sign in to comment.