Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ docstring-code-line-length = 79
[tool.ruff.lint]
preview = true
select = ["E4", "E7", "E9", "F", "B", "Q", "I", "D", "DOC"]
ignore = ["DOC502"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"
Expand Down
38 changes: 32 additions & 6 deletions src/audioclass/batch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,48 @@ def from_dataframe(
)


def recordings_from_files(files: List[Path]) -> List[data.Recording]:
def recordings_from_files(
files: List[Path],
ignore_errors: bool = True,
) -> List[data.Recording]:
"""Create a list of `Recording` objects from a list of file paths.

This function iterates through a list of file paths, creating a
`soundevent.data.Recording` object for each file. It can optionally
ignore errors that occur during file processing.

Parameters
----------
files
A list of paths to audio files.
A list of `pathlib.Path` objects pointing to audio files.
ignore_errors
If True, any errors encountered while creating a `Recording` from a
file will be ignored, and the file will be skipped. If False, any
error will be raised. Defaults to True.

Returns
-------
List[data.Recording]
A list of `Recording` objects corresponding to the input files.
A list of `Recording` objects created from the provided files.

Raises
------
Exception
If `ignore_errors` is False and an error occurs while processing a
file.
"""
return [
data.Recording.from_file(file, compute_hash=False) for file in files
]
recordings = []

for path in files:
try:
recording = data.Recording.from_file(path, compute_hash=False)
recordings.append(recording)
except Exception as e:
if not ignore_errors:
raise e
continue

return recordings


def recordings_from_directory(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_batch/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def test_can_run_get_recordings_from_files(
assert len(recordings) == len(file_list)


def test_recordings_from_directory_skips_empty_files(
file_list: List[Path],
audio_dir: Path,
):
(audio_dir / "empty_audio.wav").touch()

recordings = recordings_from_directory(audio_dir)
assert len(recordings) == len(file_list)


def test_can_get_all_audio_files_in_dir(
random_wav_factory,
tmp_path: Path,
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.