Module redvox.tests.common.test_gap_and_pad_utils
Expand source code
import unittest
import numpy as np
import pyarrow as pa
import redvox.common.date_time_utils as dt
import redvox.common.gap_and_pad_utils as gpu
class CalcTimestampsTest(unittest.TestCase):
def test_calc_timestamps(self):
timestamps = gpu.calc_evenly_sampled_timestamps(1000., 100, 1000)
self.assertEqual(len(timestamps), 100)
self.assertEqual(timestamps[0], 1000)
self.assertEqual(timestamps[1], 2000)
self.assertEqual(timestamps[99], 100000)
class InterpolateGapsTest(unittest.TestCase):
def test_create_simple_df(self):
my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]})
gaps = [(1000., 8000.), (9000., 15000.)]
filled_df = gpu.fill_gaps(my_df, gaps, 1000.)
self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_after_end(self):
my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]})
gaps = [(1000., 8000.), (9000., 19000.)]
filled_df = gpu.fill_gaps(my_df, gaps, 1000.)
self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_before_begin(self):
my_df = pa.Table.from_pydict({"timestamps": [11000., 18000., 19000., 25000.], "data": [50., 400., 450., 750.]})
gaps = [(1000., 18000.), (19000., 29000.)]
filled_df = gpu.fill_gaps(my_df, gaps, 1000.)
self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_intersect_end(self):
my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]})
gaps = [(1000., 7000.), (6000., 8000.), (9000., 15000.)]
filled_df = gpu.fill_gaps(my_df, gaps, 1000.)
self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_intersect_begin(self):
my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]})
gaps = [(5000., 8000.), (1000., 7000.), (9000., 15000.)]
filled_df = gpu.fill_gaps(my_df, gaps, 1000.)
self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_overlap(self):
my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]})
gaps = [(4000., 6000.), (1000., 8000.), (9000., 15000.)]
filled_df = gpu.fill_gaps(my_df, gaps, 1000.)
self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
class AudioGapFillTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.sample_interval = 250
def test_audio_gap_df(self):
my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})),
(2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})),
(5000., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]}))
]
result = gpu.fill_audio_gaps(my_data, self.sample_interval)
filled_df = result.create_timestamps()
self.assertEqual(len(filled_df["timestamps"]), 20)
self.assertEqual(len(result.gaps), 1)
def test_misshapen_audio_gap_df(self):
my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})),
(3400., pa.Table.from_pydict({"microphone": [40, 30]})),
(4400., pa.Table.from_pydict({"microphone": [5, 15, 25, 35, 65]}))
]
result = gpu.fill_audio_gaps(my_data, self.sample_interval)
filled_df = result.create_timestamps()
self.assertEqual(len(filled_df["timestamps"]), 19)
self.assertEqual(len(result.gaps), 2)
def test_tiny_audio_gap_df(self):
my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})),
(2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})),
(3005., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]}))
]
result = gpu.fill_audio_gaps(my_data, self.sample_interval)
filled_df = result.create_timestamps()
self.assertEqual(len(filled_df["timestamps"]), 12)
self.assertEqual(len(result.gaps), 0)
def test_undersized_audio_gap_df(self):
my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})),
(2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})),
(3100., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]}))
]
result = gpu.fill_audio_gaps(my_data, self.sample_interval)
filled_df = result.create_timestamps()
self.assertEqual(len(filled_df["timestamps"]), 13)
self.assertEqual(len(result.gaps), 1)
def test_failure_audio_gap_df(self):
my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})),
(1500., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]}))
]
result = gpu.fill_audio_gaps(my_data, self.sample_interval)
self.assertEqual(len(result.errors.get()), 1)
Classes
class AudioGapFillTest (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class AudioGapFillTest(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.sample_interval = 250 def test_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})), (5000., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 20) self.assertEqual(len(result.gaps), 1) def test_misshapen_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (3400., pa.Table.from_pydict({"microphone": [40, 30]})), (4400., pa.Table.from_pydict({"microphone": [5, 15, 25, 35, 65]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 19) self.assertEqual(len(result.gaps), 2) def test_tiny_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})), (3005., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 12) self.assertEqual(len(result.gaps), 0) def test_undersized_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})), (3100., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 13) self.assertEqual(len(result.gaps), 1) def test_failure_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (1500., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) self.assertEqual(len(result.errors.get()), 1)
Ancestors
- unittest.case.TestCase
Static methods
def setUpClass() ‑> None
-
Hook method for setting up class fixture before running tests in the class.
Expand source code
@classmethod def setUpClass(cls) -> None: cls.sample_interval = 250
Methods
def test_audio_gap_df(self)
-
Expand source code
def test_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})), (5000., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 20) self.assertEqual(len(result.gaps), 1)
def test_failure_audio_gap_df(self)
-
Expand source code
def test_failure_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (1500., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) self.assertEqual(len(result.errors.get()), 1)
def test_misshapen_audio_gap_df(self)
-
Expand source code
def test_misshapen_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (3400., pa.Table.from_pydict({"microphone": [40, 30]})), (4400., pa.Table.from_pydict({"microphone": [5, 15, 25, 35, 65]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 19) self.assertEqual(len(result.gaps), 2)
def test_tiny_audio_gap_df(self)
-
Expand source code
def test_tiny_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})), (3005., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 12) self.assertEqual(len(result.gaps), 0)
def test_undersized_audio_gap_df(self)
-
Expand source code
def test_undersized_audio_gap_df(self): my_data = [(1000., pa.Table.from_pydict({"microphone": [10, 20, 30, 40]})), (2000., pa.Table.from_pydict({"microphone": [40, 30, 20, 10]})), (3100., pa.Table.from_pydict({"microphone": [5, 15, 25, 35]})) ] result = gpu.fill_audio_gaps(my_data, self.sample_interval) filled_df = result.create_timestamps() self.assertEqual(len(filled_df["timestamps"]), 13) self.assertEqual(len(result.gaps), 1)
class CalcTimestampsTest (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class CalcTimestampsTest(unittest.TestCase): def test_calc_timestamps(self): timestamps = gpu.calc_evenly_sampled_timestamps(1000., 100, 1000) self.assertEqual(len(timestamps), 100) self.assertEqual(timestamps[0], 1000) self.assertEqual(timestamps[1], 2000) self.assertEqual(timestamps[99], 100000)
Ancestors
- unittest.case.TestCase
Methods
def test_calc_timestamps(self)
-
Expand source code
def test_calc_timestamps(self): timestamps = gpu.calc_evenly_sampled_timestamps(1000., 100, 1000) self.assertEqual(len(timestamps), 100) self.assertEqual(timestamps[0], 1000) self.assertEqual(timestamps[1], 2000) self.assertEqual(timestamps[99], 100000)
class InterpolateGapsTest (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class InterpolateGapsTest(unittest.TestCase): def test_create_simple_df(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 8000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15) def test_create_gap_after_end(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 8000.), (9000., 19000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15) def test_create_gap_before_begin(self): my_df = pa.Table.from_pydict({"timestamps": [11000., 18000., 19000., 25000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 18000.), (19000., 29000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15) def test_create_gap_intersect_end(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 7000.), (6000., 8000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15) def test_create_gap_intersect_begin(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(5000., 8000.), (1000., 7000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15) def test_create_gap_overlap(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(4000., 6000.), (1000., 8000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
Ancestors
- unittest.case.TestCase
Methods
def test_create_gap_after_end(self)
-
Expand source code
def test_create_gap_after_end(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 8000.), (9000., 19000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_before_begin(self)
-
Expand source code
def test_create_gap_before_begin(self): my_df = pa.Table.from_pydict({"timestamps": [11000., 18000., 19000., 25000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 18000.), (19000., 29000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_intersect_begin(self)
-
Expand source code
def test_create_gap_intersect_begin(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(5000., 8000.), (1000., 7000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_intersect_end(self)
-
Expand source code
def test_create_gap_intersect_end(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 7000.), (6000., 8000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_gap_overlap(self)
-
Expand source code
def test_create_gap_overlap(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(4000., 6000.), (1000., 8000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)
def test_create_simple_df(self)
-
Expand source code
def test_create_simple_df(self): my_df = pa.Table.from_pydict({"timestamps": [1000., 8000., 9000., 15000.], "data": [50., 400., 450., 750.]}) gaps = [(1000., 8000.), (9000., 15000.)] filled_df = gpu.fill_gaps(my_df, gaps, 1000.) self.assertEqual(len(filled_df[0]["timestamps"].to_numpy()), 15)