Skip to content

Commit

Permalink
fix audio and video starting times and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyCBakerPhD committed Jun 7, 2023
1 parent cf02df5 commit 3942d24
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,10 @@ def set_aligned_starting_time(self, aligned_starting_time: float, stub_test: boo
To limit that scan to a small number of frames, set `stub_test=True`.
"""
if self._timestamps is not None:
self.set_aligned_timestamps(
aligned_timestamps=[
timestamps + aligned_starting_time for timestamps in self.get_timestamps(stub_test=stub_test)
]
)
aligned_timestamps = [
timestamps + aligned_starting_time for timestamps in self.get_timestamps(stub_test=stub_test)
]
self.set_aligned_timestamps(aligned_timestamps=aligned_timestamps)
elif self._segment_starting_times is not None:
self._segment_starting_times = [
segment_starting_time + aligned_starting_time for segment_starting_time in self._segment_starting_times
Expand Down
38 changes: 22 additions & 16 deletions tests/test_behavior/test_audio_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setUpClass(cls):
cls.num_frames = 10000
cls.num_audio_files = 3
cls.sampling_rate = 500
cls.segment_starting_times = [0.0, 20.0, 40.0]
cls.aligned_segment_starting_times = [0.0, 20.0, 40.0]

cls.test_dir = Path(mkdtemp())
cls.file_paths = create_audio_files(
Expand Down Expand Up @@ -77,7 +77,9 @@ class AudioTestNWBConverter(NWBConverter):
source_data = dict(Audio=dict(file_paths=self.file_paths))
self.nwb_converter = AudioTestNWBConverter(source_data)
self.interface = self.nwb_converter.data_interface_objects["Audio"]
self.interface.align_segment_starting_times(segment_starting_times=self.segment_starting_times)
self.interface.set_aligned_segment_starting_times(
aligned_segment_starting_times=self.aligned_segment_starting_times
)

def test_unsupported_format(self):
exc_msg = "The currently supported file format for audio is WAV file. Some of the provided files does not match this format: ['.test']."
Expand Down Expand Up @@ -158,35 +160,37 @@ def test_not_all_metadata_are_unique(self):

def test_segment_starting_times_are_floats(self):
with self.assertRaisesWith(
exc_type=AssertionError, exc_msg="Argument 'segment_starting_times' must be a list of floats."
exc_type=AssertionError, exc_msg="Argument 'aligned_segment_starting_times' must be a list of floats."
):
self.interface.align_segment_starting_times(segment_starting_times=[0, 1, 2])
self.interface.set_aligned_segment_starting_times(aligned_segment_starting_times=[0, 1, 2])

def test_segment_starting_times_length_mismatch(self):
with self.assertRaisesWith(
exc_type=AssertionError,
exc_msg="The number of entries in 'segment_starting_times' (4) must be equal to the number of audio file paths (3).",
exc_msg="The number of entries in 'aligned_segment_starting_times' (4) must be equal to the number of audio file paths (3).",
):
self.interface.align_segment_starting_times(segment_starting_times=[0.0, 1.0, 2.0, 4.0])
self.interface.set_aligned_segment_starting_times(aligned_segment_starting_times=[0.0, 1.0, 2.0, 4.0])

def test_align_segment_starting_times(self):
def test_set_aligned_segment_starting_times(self):
fresh_interface = AudioInterface(file_paths=self.file_paths[:2])

segment_starting_times = [0.0, 1.0]
fresh_interface.align_segment_starting_times(segment_starting_times=segment_starting_times)
aligned_segment_starting_times = [0.0, 1.0]
fresh_interface.set_aligned_segment_starting_times(
aligned_segment_starting_times=aligned_segment_starting_times
)

assert_array_equal(x=self.interface._segment_starting_times, y=self.segment_starting_times)
assert_array_equal(x=self.interface._segment_starting_times, y=self.aligned_segment_starting_times)

def test_align_starting_time(self):
def test_set_aligned_starting_time(self):
fresh_interface = AudioInterface(file_paths=self.file_paths[:2])

starting_time = 1.23
aligned_starting_time = 1.23
relative_starting_times = [0.0, 1.0]
fresh_interface.align_segment_starting_times(segment_starting_times=relative_starting_times)
fresh_interface.align_starting_time(starting_time=starting_time)
fresh_interface.set_aligned_segment_starting_times(aligned_segment_starting_times=relative_starting_times)
fresh_interface.set_aligned_starting_time(aligned_starting_time=aligned_starting_time)

expecting_starting_times = [
relative_starting_time + starting_time for relative_starting_time in relative_starting_times
relative_starting_time + aligned_starting_time for relative_starting_time in relative_starting_times
]
assert_array_equal(x=fresh_interface._segment_starting_times, y=expecting_starting_times)

Expand All @@ -205,6 +209,8 @@ def test_run_conversion(self):
for audio_ind, audio_metadata in enumerate(metadata["Behavior"]["Audio"]):
audio_interface_name = audio_metadata["name"]
assert audio_interface_name in container
self.assertEqual(self.segment_starting_times[audio_ind], container[audio_interface_name].starting_time)
self.assertEqual(
self.aligned_segment_starting_times[audio_ind], container[audio_interface_name].starting_time
)
self.assertEqual(self.sampling_rate, container[audio_interface_name].rate)
assert_array_equal(audio_test_data[audio_ind], container[audio_interface_name].data)
18 changes: 10 additions & 8 deletions tests/test_behavior/test_video_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setUp(self) -> None:
self.metadata = self.nwb_converter.get_metadata()
self.metadata["NWBFile"].update(session_start_time=datetime.now(tz=gettz(name="US/Pacific")))
self.nwbfile_path = self.test_dir / "video_test.nwb"
self.segment_starting_times = [0.0, 50.0]
self.aligned_segment_starting_times = [0.0, 50.0]

def tearDown(self) -> None:
shutil.rmtree(self.test_dir)
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_video_external_mode(self):
timestamps = [np.array([2.2, 2.4, 2.6]), np.array([3.2, 3.4, 3.6])]
interface = self.nwb_converter.data_interface_objects["Video"]
interface.set_aligned_timestamps(aligned_timestamps=timestamps)
interface.align_segment_starting_times(segment_starting_times=self.segment_starting_times)
interface.set_aligned_segment_starting_times(aligned_segment_starting_times=self.aligned_segment_starting_times)

conversion_options = dict(Video=dict(external_mode=True, starting_frames=[0, 4]))
self.nwb_converter.run_conversion(
Expand All @@ -116,10 +116,10 @@ def test_video_external_mode(self):
self.assertListEqual(list1=list(module["Video: test1"].external_file[:]), list2=self.video_files)

def test_video_irregular_timestamps(self):
timestamps = [np.array([1.0, 2.0, 4.0]), np.array([5.0, 6.0, 7.0])]
aligned_timestamps = [np.array([1.0, 2.0, 4.0]), np.array([5.0, 6.0, 7.0])]
interface = self.nwb_converter.data_interface_objects["Video"]
interface.set_aligned_timestamps(aligned_timestamps=timestamps)
interface.align_segment_starting_times(segment_starting_times=self.segment_starting_times)
interface.set_aligned_timestamps(aligned_timestamps=aligned_timestamps)
interface.set_aligned_segment_starting_times(aligned_segment_starting_times=self.aligned_segment_starting_times)

conversion_options = dict(Video=dict(external_mode=True, starting_frames=[0, 4]))
self.nwb_converter.run_conversion(
Expand Down Expand Up @@ -221,10 +221,12 @@ def test_video_chunking(self):
assert mod[video_interface_name].data.chunks is not None # TODO retrieve storage_layout of hdf5 dataset

def test_video_stub(self):
timestamps = [np.array([1, 2, 4, 5, 6, 7, 8, 9, 10, 11])]
aligned_timestamps = [np.array([1, 2, 4, 5, 6, 7, 8, 9, 10, 11])]
interface = self.nwb_converter.data_interface_objects["Video"]
interface.set_aligned_timestamps(aligned_timestamps=timestamps)
interface.align_segment_starting_times(segment_starting_times=[self.segment_starting_times[0]])
interface.set_aligned_timestamps(aligned_timestamps=aligned_timestamps)
interface.set_aligned_segment_starting_times(
aligned_segment_starting_times=[self.aligned_segment_starting_times[0]]
)

conversion_options = dict(Video=dict(external_mode=False, stub_test=True))
self.nwb_converter.run_conversion(
Expand Down

0 comments on commit 3942d24

Please sign in to comment.