Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle Empty Audio Files Gracefully (GH-20) #39

Merged
merged 2 commits into from
Nov 11, 2024
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
6 changes: 3 additions & 3 deletions batdetect2/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""BatDetect2 command line interface."""

import os

import click
Expand Down Expand Up @@ -129,10 +130,9 @@ def detect(
):
results_path = audio_file.replace(audio_dir, ann_dir)
save_results_to_file(results, results_path)
except (RuntimeError, ValueError, LookupError) as err:
except (RuntimeError, ValueError, LookupError, EOFError) as err:
error_files.append(audio_file)
click.secho(f"Error processing file!: {err}", fg="red")
raise err
click.secho(f"Error processing file {audio_file}: {err}", fg="red")

click.echo(f"\nResults saved to: {ann_dir}")

Expand Down
30 changes: 25 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

from batdetect2.cli import cli

runner = CliRunner()


def test_cli_base_command():
"""Test the base command."""
runner = CliRunner()
result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert (
Expand All @@ -20,7 +21,6 @@ def test_cli_base_command():

def test_cli_detect_command_help():
"""Test the detect command help."""
runner = CliRunner()
result = runner.invoke(cli, ["detect", "--help"])
assert result.exit_code == 0
assert "Detect bat calls in files in AUDIO_DIR" in result.output
Expand All @@ -34,7 +34,6 @@ def test_cli_detect_command_on_test_audio(tmp_path):
if results_dir.exists():
results_dir.rmdir()

runner = CliRunner()
result = runner.invoke(
cli,
[
Expand All @@ -58,7 +57,6 @@ def test_cli_detect_command_with_non_trivial_time_expansion(tmp_path):
if results_dir.exists():
results_dir.rmdir()

runner = CliRunner()
result = runner.invoke(
cli,
[
Expand All @@ -83,7 +81,6 @@ def test_cli_detect_command_with_the_spec_feature_flag(tmp_path: Path):
if results_dir.exists():
results_dir.rmdir()

runner = CliRunner()
result = runner.invoke(
cli,
[
Expand All @@ -110,3 +107,26 @@ def test_cli_detect_command_with_the_spec_feature_flag(tmp_path: Path):

df = pd.read_csv(results_dir / expected_file)
assert not (df.duration == -1).any()


def test_cli_detect_fails_gracefully_on_empty_file(tmp_path: Path):
results_dir = tmp_path / "results"
target = tmp_path / "audio"
target.mkdir()

# Create an empty file with the .wav extension
empty_file = target / "empty.wav"
empty_file.touch()

result = runner.invoke(
cli,
args=[
"detect",
str(target),
str(results_dir),
"0.3",
"--spec_features",
],
)
assert result.exit_code == 0
assert f"Error processing file {empty_file}" in result.output