Skip to content

Commit

Permalink
Skip duration plots for single duration (#74)
Browse files Browse the repository at this point in the history
* Skip duration plots for single duration

* Simplify docstring

* Extend message for single duration
  • Loading branch information
hagenw authored Apr 3, 2024
1 parent 423b572 commit fef0b0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
8 changes: 8 additions & 0 deletions audbcards/core/datacard.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def file_duration_distribution(self) -> str:
under ``<dataset-name>/<dataset-name>-file-durations.png``
and displayed
between the minimum and maximum values.
If all duration values are the same,
no distribution plot is created.
"""
min_ = 0
Expand All @@ -149,6 +151,12 @@ def file_duration_distribution(self) -> str:
if len(durations) > 0:
min_ = np.min(durations)
max_ = np.max(durations)

# Skip creating a distribution plot,
# if all durations are the same
if min_ == max_:
return f"each file is {max_:.1f} {unit}"

distribution_str = f"{min_:.1f} {unit} .. {max_:.1f} {unit}"

# Save distribution plot
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data/rendered_templates/bare_db.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ channel
sampling rate
bit depth
duration 0 days 00:00:00
files 0, duration distribution: 0.0 s .. 0.0 s
files 0, duration distribution: each file is 0.0 s
repository `data-local <.../data-local/bare_db>`__
published 2023-04-05 by author
============= ======================
2 changes: 1 addition & 1 deletion tests/test_data/rendered_templates/minimal_db.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ channel 1
sampling rate 8000
bit depth 16
duration 0 days 00:00:00.100000
files 1, duration distribution: 0.1 s .. 0.1 s
files 1, duration distribution: each file is 0.1 s
repository `data-local <.../data-local/minimal_db>`__
published 2023-04-05 by author
============= ======================
Expand Down
14 changes: 9 additions & 5 deletions tests/test_datacard.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,21 @@ def test_datacard_file_duration_distribution(
f"{db.name}-file-durations.png",
)
assert not os.path.exists(image_file)
expected_distribution_str = f"{expected_min:.1f} s .. {expected_max:.1f} s"
if expected_min == expected_max:
expected_distribution_str = f"each file is {expected_max:.1f} s"
else:
expected_distribution_str = f"{expected_min:.1f} s .. {expected_max:.1f} s"
assert expected_distribution_str == distribution_str

# Set sphinx src and build dir and execute again
datacard.sphinx_build_dir = build_dir
datacard.sphinx_src_dir = src_dir
distribution_str = datacard.file_duration_distribution
assert os.path.exists(image_file)
expected_distribution_str = (
f"{expected_min:.1f} s |{db.name}-file-durations| {expected_max:.1f} s"
)
if expected_min != expected_max:
assert os.path.exists(image_file)
expected_distribution_str = (
f"{expected_min:.1f} s |{db.name}-file-durations| {expected_max:.1f} s"
)
assert expected_distribution_str == distribution_str


Expand Down

0 comments on commit fef0b0e

Please sign in to comment.