Mercurial > hg > auditok
changeset 215:e59b071b03cb
Fix _duration_to_nb_windows and add tests
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Fri, 05 Jul 2019 20:19:34 +0100 |
parents | a377e6c5ea40 |
children | 3708f2eb8a40 |
files | auditok/core.py tests/test_core.py |
diffstat | 2 files changed, 31 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/auditok/core.py Thu Jul 04 21:38:00 2019 +0100 +++ b/auditok/core.py Fri Jul 05 20:19:34 2019 +0100 @@ -146,9 +146,10 @@ def _duration_to_nb_windows(duration, analysis_window): """ - Converts a given duration into a positive integer on analysis windows. + Converts a given duration into a positive integer of analysis windows. if `duration / analysis_window` is not an integer, the result will be rounded to the closest bigger integer. If `duration == 0`, returns `0`. + If `duration < analysis_window`, returns 1. `duration` and `analysis_window` can be in seconds or milliseconds but must be in the same unit. @@ -165,13 +166,15 @@ minimum number of `analysis_window`'s to cover `durartion`. That means that `analysis_window * nb_windows >= duration`. """ + if duration < 0 or analysis_window <= 0: + err_msg = "'duration' ({}) must be >= 0 and 'analysis_window' ({}) > 0" + raise ValueError(err_msg.format(duration, analysis_window)) if duration == 0: return 0 - if duration > analysis_window: - nb_windows, rest = divmod(duration, analysis_window) - if rest > 0: - nb_windows += 1 - return int(nb_windows) + nb_windows, rest = divmod(duration, analysis_window) + if rest > 0: + nb_windows += 1 + return int(nb_windows) def _make_audio_region( @@ -447,11 +450,11 @@ ) def __getitem__(self, index): - err_message = "AudioRegion index must a slice object without a step" + err_msg = "AudioRegion index must a slice object without a step" if not isinstance(index, slice): - raise TypeError(err_message) + raise TypeError(err_msg) if index.step is not None: - raise ValueError(err_message) + raise ValueError(err_msg) start_ms = index.start if index.start is not None else 0 stop_ms = index.stop if index.stop is not None else len(self)
--- a/tests/test_core.py Thu Jul 04 21:38:00 2019 +0100 +++ b/tests/test_core.py Fri Jul 05 20:19:34 2019 +0100 @@ -4,6 +4,7 @@ from tempfile import TemporaryDirectory from genty import genty, genty_dataset from auditok import split, AudioRegion, AudioParameterError +from auditok.core import _duration_to_nb_windows from auditok.util import AudioDataSource from auditok.io import ( _normalize_use_channel, @@ -28,6 +29,24 @@ @genty +class TestFunctions(TestCase): + @genty_dataset( + zero_duration=(0, 1, 0), + multiple=(0.3, 0.1, 3), + not_multiple=(0.35, 0.1, 4), + small_duration=(0.05, 0.1, 1), + ) + def test_duration_to_nb_windows(self, duration, analysis_window, expected): + + if issubclass(expected.__class__, Exception): + with self.assertRaises(expected): + _duration_to_nb_windows(duration, analysis_window) + else: + result = _duration_to_nb_windows(duration, analysis_window) + self.assertEqual(result, expected) + + +@genty class TestSplit(TestCase): @genty_dataset( simple=(