Skip to content
Open
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
11 changes: 4 additions & 7 deletions batbot/batbot_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ def preprocess(
)
data['output_path'].extend(output_paths)
data['compressed_path'].extend(compressed_paths)
if process_metadata:
data['metadata_path'].append(metadata_path)
data['metadata_path'].append(metadata_path)
except Exception as e:
warnings.warn('WARNING: Pipeline failed for file {}'.format(file))
data['failed_files'].append((str(file), e))
Expand Down Expand Up @@ -356,18 +355,16 @@ def preprocess(
)
data['output_path'].extend(output_paths)
data['compressed_path'].extend(compressed_paths)
if process_metadata:
data['metadata_path'].extend(metadata_paths)
data['metadata_path'].extend(metadata_paths)
data['failed_files'].extend(failed_files)

if output_json is None:
print('\nFull spectrogram output paths:')
pprint.pp(sorted(data['output_path']))
print('\nCompressed spectrogram output paths:')
pprint.pp(sorted(data['compressed_path']))
if process_metadata:
print('\nProcessed metadata paths:')
pprint.pp(sorted(data['metadata_path']))
print('\nProcessed metadata paths:')
pprint.pp(sorted(data['metadata_path']))
print('\nFiles skipped due to failure, and corresponding exceptions:')
pprint.pp(sorted(data['failed_files']))
else:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_preprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json
import os

from click.testing import CliRunner

from batbot.batbot_cli import preprocess


def test_preprocess():
"""Test of batbot preprocess CLI ensuring the example files are processed without error.
Additionally, a regression test ensuring that the number of detected bat calls in the examples
does not decrease. The minumum numbers of bat call segments listed below correspond to the number
of bat call segments detected at the time of writing minus detected noise segments (noise counted by hand).
Note that this test uses "fast mode" processing, which is more permissive of low-amplitude calls and noise.
"""
runner = CliRunner()
data = runner.invoke(preprocess, ['examples', '-o', './output', '--force-overwrite'])
assert data.exit_code == 0
# parse stdout to ensure example files were processed properly
num_examples = 4
output_str = str(data.output).split('\n')
for ii in range(num_examples):
expected_file = './output/example{}.01of01.compressed.jpg'.format(ii + 1)
assert any(
[expected_file in x for x in output_str]
), 'Did not find file listed among outputs: {}'.format(expected_file)
assert os.path.exists(expected_file), 'Did not find file on filesystem: {}'.format(
expected_file
)
num_min_call_segments = [65, 18, 149, 47]
for ii in range(num_examples):
expected_file = './output/example{}.metadata.json'.format(ii + 1)
assert any(
[expected_file in x for x in output_str]
), 'Did not find file listed among outputs: {}'.format(expected_file)
assert os.path.exists(expected_file), 'Did not find file on filesystem: {}'.format(
expected_file
)
# load metadata file and ensure minimum number of call segments were detected
with open(expected_file) as hf:
data = json.load(hf)
n_segments = len(data['segments'])
err_str = (
'Expected at least {} bat call segments in file {}, found only {} segments'.format(
num_min_call_segments[ii], expected_file, n_segments
)
)
assert n_segments >= num_min_call_segments[ii], err_str
32 changes: 32 additions & 0 deletions tests/test_preprocess_parallel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

from click.testing import CliRunner

from batbot.batbot_cli import preprocess


def test_preprocess_parallel():
runner = CliRunner()
data = runner.invoke(
preprocess,
['examples', '-o', './output', '--process-metadata', '--force-overwrite', '-n', 4],
)
# parse stdout to ensure example files were processed properly
num_examples = 4
output_str = str(data.output).split('\n')
for ii in range(num_examples):
expected_file = './output/example{}.01of01.compressed.jpg'.format(ii + 1)
assert any(
[expected_file in x for x in output_str]
), 'Did not find file listed among outputs: {}'.format(expected_file)
assert os.path.exists(expected_file), 'Did not find file in filesystem: {}'.format(
expected_file
)
for ii in range(num_examples):
expected_file = './output/example{}.metadata.json'.format(ii + 1)
assert any(
[expected_file in x for x in output_str]
), 'Did not find file listed among outputs: {}'.format(expected_file)
assert os.path.exists(expected_file), 'Did not find file in filesystem: {}'.format(
expected_file
)
Loading