From 786bbc8cd242cfd5b65c31e16345f26699a4b58f Mon Sep 17 00:00:00 2001 From: fabiocat93 Date: Wed, 25 Sep 2024 17:16:59 -0400 Subject: [PATCH 1/5] adjusting windowing by returning an Audio instead of a waveform --- src/senselab/audio/data_structures/audio.py | 41 ++- src/tests/audio/data_structures/audio_test.py | 293 ++++++++---------- 2 files changed, 155 insertions(+), 179 deletions(-) diff --git a/src/senselab/audio/data_structures/audio.py b/src/senselab/audio/data_structures/audio.py index 2e6e774..8e2e335 100644 --- a/src/senselab/audio/data_structures/audio.py +++ b/src/senselab/audio/data_structures/audio.py @@ -103,28 +103,53 @@ def __eq__(self, other: object) -> bool: return self.id() == other.id() return False - def window_generator(self, window_size: int, step_size: int) -> Generator[torch.Tensor, None, None]: - """Creates a sliding window generator for the audio waveform. + def window_generator(self, window_size: int, step_size: int) -> Generator["Audio", None, None]: + """Creates a sliding window generator for the audio. + + Creates a generator that yields Audio objects corresponding to each window of the waveform + using a sliding window. The window size and step size are specified in number of samples. + If the audio waveform doesn't contain an exact number of windows, the remaining samples + will be included in the last window. Args: window_size: Size of each window (number of samples). step_size: Step size for sliding the window (number of samples). - Raises: - ValueError: If step_size is greater than window_size. + Yields: + Audio: Audio objects corresponding to each window of the waveform. """ if step_size > window_size: warnings.warn( "Step size is greater than window size. \ - Some of audio will not be included in the windows." + Some of the audio will not be included in the windows." ) num_samples = self.waveform.size(-1) + print("num_samples:", num_samples) current_position = 0 - while current_position < num_samples - window_size: - window = self.waveform[:, current_position : current_position + window_size] - yield window + while current_position < num_samples: + print("current_position:", current_position) + # Calculate the end position of the window + end_position = current_position + window_size + + # If the end_position exceeds the number of samples, take the remaining samples + if end_position > num_samples: + end_position = num_samples + print("end_position:", end_position) + + # Get the windowed waveform + window_waveform = self.waveform[:, current_position:end_position] + + # Create a new Audio instance for this window + window_audio = Audio( + waveform=window_waveform, + sampling_rate=self.sampling_rate, + orig_path_or_id=f"{self.orig_path_or_id}_{current_position}_{end_position}", + metadata=self.metadata + ) + + yield window_audio current_position += step_size diff --git a/src/tests/audio/data_structures/audio_test.py b/src/tests/audio/data_structures/audio_test.py index ca98255..f67d6f8 100644 --- a/src/tests/audio/data_structures/audio_test.py +++ b/src/tests/audio/data_structures/audio_test.py @@ -1,7 +1,9 @@ """Module for testing Audio data structures.""" import warnings +from typing import List, Tuple +import pytest import torch import torchaudio @@ -9,38 +11,37 @@ from tests.audio.conftest import MONO_AUDIO_PATH, STEREO_AUDIO_PATH -def load_audio(file_path: str) -> tuple[torch.Tensor, int]: +def load_audio(file_path: str) -> Tuple[torch.Tensor, int]: """Loads audio data from the given file path.""" return torchaudio.load(file_path) -def test_mono_audio_creation(mono_audio_sample: Audio) -> None: - """Tests mono audio creation.""" - mono_audio_data, mono_sr = load_audio(MONO_AUDIO_PATH) - mono_audio = Audio( - waveform=mono_audio_data, - sampling_rate=mono_sr, - orig_path_or_id=MONO_AUDIO_PATH, - ) - assert mono_audio == mono_audio_sample, "Mono audios are not exactly equivalent" - - -def test_stereo_audio_creation(stereo_audio_sample: Audio) -> None: - """Tests stereo audio creation.""" - stereo_audio_data, stereo_sr = load_audio(STEREO_AUDIO_PATH) - stereo_audio = Audio( - waveform=stereo_audio_data, - sampling_rate=stereo_sr, - orig_path_or_id=STEREO_AUDIO_PATH, +@pytest.mark.parametrize("audio_sample, audio_path", [ + ("mono_audio_sample", MONO_AUDIO_PATH), + ("stereo_audio_sample", STEREO_AUDIO_PATH), +]) +def test_audio_creation(audio_sample: str, audio_path: str, request: pytest.FixtureRequest) -> None: + """Tests mono and stereo audio creation.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_data, audio_sr = load_audio(audio_path) + audio = Audio( + waveform=audio_data, + sampling_rate=audio_sr, + orig_path_or_id=audio_path, ) - assert stereo_audio == stereo_audio_sample, "Stereo audios are not exactly equivalent" + assert audio == audio_sample, "Audios are not exactly equivalent" -def test_stereo_audio_uuid_creation(stereo_audio_sample: Audio) -> None: - """Tests stereo audio creation with different UUID.""" - stereo_audio_data, stereo_sr = load_audio(STEREO_AUDIO_PATH) - stereo_audio_uuid = Audio(waveform=stereo_audio_data, sampling_rate=stereo_sr) - assert stereo_audio_sample == stereo_audio_uuid, "Stereo audio with different IDs should still be equivalent" +@pytest.mark.parametrize("audio_sample, audio_path", [ + ("mono_audio_sample", MONO_AUDIO_PATH), + ("stereo_audio_sample", STEREO_AUDIO_PATH), +]) +def test_audio_creation_uuid(audio_sample: str, audio_path: str, request: pytest.FixtureRequest) -> None: + """Tests audio creation with different UUID.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_data, audio_sr = load_audio(audio_path) + audio_uuid = Audio(waveform=audio_data, sampling_rate=audio_sr) + assert audio_sample == audio_uuid, "Audio with different IDs should still be equivalent" def test_audio_single_tensor(mono_audio_sample: Audio) -> None: @@ -52,169 +53,119 @@ def test_audio_single_tensor(mono_audio_sample: Audio) -> None: ), "Mono audios of tensor shape (num_samples,) should be reshaped to (1, num_samples)" -def test_audio_from_list(mono_audio_sample: Audio) -> None: - """Tests mono audio creation from list.""" - mono_audio_data, mono_sr = load_audio(MONO_AUDIO_PATH) - audio_from_list = Audio(waveform=list(mono_audio_data[0]), sampling_rate=mono_sr) +@pytest.mark.parametrize("audio_sample, audio_path", [ + ("mono_audio_sample", MONO_AUDIO_PATH), +]) +def test_audio_from_list(audio_sample: str, audio_path: str, request: pytest.FixtureRequest) -> None: + """Tests audio creation from list.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_data, audio_sr = load_audio(audio_path) + audio_from_list = Audio(waveform=list(audio_data[0]), sampling_rate=audio_sr) assert torch.equal( - mono_audio_sample.waveform, audio_from_list.waveform + audio_sample.waveform, audio_from_list.waveform ), "List audio should've been converted to Tensor" -def test_audio_from_list_of_lists(mono_audio_sample: Audio) -> None: - """Tests mono audio creation from list of lists.""" - mono_audio_data, mono_sr = load_audio(MONO_AUDIO_PATH) - audio_from_list_of_lists = Audio(waveform=[list(mono_audio_data[0])], sampling_rate=mono_sr) - assert torch.equal( - mono_audio_sample.waveform, audio_from_list_of_lists.waveform - ), "List of lists audio should've been converted to Tensor" - - -def test_audio_from_numpy(mono_audio_sample: Audio) -> None: - """Tests mono audio creation from numpy array.""" - mono_audio_data, mono_sr = load_audio(MONO_AUDIO_PATH) - audio_from_numpy = Audio(waveform=mono_audio_data.numpy(), sampling_rate=mono_sr) - assert torch.equal( - mono_audio_sample.waveform, audio_from_numpy.waveform - ), "NumPy audio should've been converted to Tensor" - - -def test_window_generator_overlap(mono_audio_sample: Audio) -> None: +@pytest.mark.parametrize("audio_sample, window_size, step_size", [ + ("mono_audio_sample", 1024, 512), + ("stereo_audio_sample", 1024, 512), +]) +def test_window_generator_overlap(audio_sample: str, + window_size: int, + step_size: int, + request: pytest.FixtureRequest) -> None: """Tests window generator with overlapping windows.""" - window_size = 1024 - step_size = 512 - audio_length = mono_audio_sample.waveform.size(-1) + audio_sample = request.getfixturevalue(audio_sample) + audio_length = audio_sample.waveform.size(-1) - windows = list(mono_audio_sample.window_generator(window_size, step_size)) + windowed_audios: List[Audio] = list(audio_sample.window_generator(window_size, step_size)) - # Calculate expected windows - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size is less than window size. Yielded {len(windows)}." + # Adjust expected windows calculation to handle rounding issues + expected_windows = (audio_length + step_size - 1) // step_size + remaining_audio = audio_length - (expected_windows * step_size) + if remaining_audio > 0: + expected_windows += 1 + assert len(windowed_audios) == expected_windows, f"Should yield {expected_windows} \ + windows when step size is less than window size. Yielded {len(windowed_audios)}." -def test_window_generator_exact_fit(mono_audio_sample: Audio) -> None: - """Tests window generator when step size equals window size.""" - window_size = 1024 - step_size = 1024 - audio_length = mono_audio_sample.waveform.size(-1) - - windows = list(mono_audio_sample.window_generator(window_size, step_size)) - - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - window when step size equals window size. Yielded {len(windows)}." - - -def test_window_generator_step_greater_than_window(mono_audio_sample: Audio) -> None: - """Tests window generator when step size is greater than window size.""" - window_size = 1024 - step_size = 2048 # Step size greater than window size - audio_length = mono_audio_sample.waveform.size(-1) - mono_audio_sample.waveform = mono_audio_sample.waveform - - windows = list(mono_audio_sample.window_generator(window_size, step_size)) - - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size is greater than window size. Yielded {len(windows)}." - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") - list(mono_audio_sample.window_generator(window_size, step_size)) - assert len(w) == 1, "Should issue a warning when step size is greater than window size." - - -def test_window_generator_overlap_stereo(stereo_audio_sample: Audio) -> None: - """Tests window generator with overlapping windows for stereo audio.""" - window_size = 1024 - step_size = 512 - audio_length = stereo_audio_sample.waveform.size(-1) - - windows = list(stereo_audio_sample.window_generator(window_size, step_size)) - - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size is less than window size. Yielded {len(windows)}." - - -def test_window_generator_exact_fit_stereo(stereo_audio_sample: Audio) -> None: - """Tests window generator when step size equals window size for stereo audio.""" - window_size = 1024 - step_size = 1024 - audio_length = stereo_audio_sample.waveform.size(-1) - - windows = list(stereo_audio_sample.window_generator(window_size, step_size)) - - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size equals window size. Yielded {len(windows)}." - - -def test_window_generator_step_greater_than_window_stereo(stereo_audio_sample: Audio) -> None: - """Tests window generator when step size is greater than window size for stereo audio.""" - window_size = 1 - step_size = 2 # Step size greater than window size - audio_length = stereo_audio_sample.waveform.size(-1) - stereo_audio_sample.waveform = stereo_audio_sample.waveform - - windows = list(stereo_audio_sample.window_generator(window_size, step_size)) - - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size is greater than window size. Yielded {len(windows)}." - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") - list(stereo_audio_sample.window_generator(window_size, step_size)) - assert len(w) == 1, "Should issue a warning when step size is greater than window size." +@pytest.mark.parametrize("audio_sample, window_size, step_size", [ + ("mono_audio_sample", 1024, 1024), + ("stereo_audio_sample", 1024, 1024), +]) +def test_window_generator_exact_fit(audio_sample: str, + window_size: int, + step_size: int, + request: pytest.FixtureRequest) -> None: + """Tests window generator when step size equals window size.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_length = audio_sample.waveform.size(-1) -def test_window_generator_window_greater_than_audio_mono(mono_audio_sample: Audio) -> None: - """Tests window generator when window size is greater than the audio length for mono audio.""" - audio_length = mono_audio_sample.waveform.size(1) - window_size = audio_length + 1000 # Set window size greater than audio length - step_size = 512 + windowed_audios: List[Audio] = list(audio_sample.window_generator(window_size, step_size)) - windows = list(mono_audio_sample.window_generator(window_size, step_size)) + expected_windows = (audio_length + step_size - 1) // step_size + # Check if there is any remaining audio for another window + remaining_audio = audio_length - (expected_windows * step_size) + if remaining_audio > 0: + expected_windows += 1 - assert len(windows) == 0, f"Should yield no windows when window size is greater \ - than audio length. Yielded {len(windows)}." + assert len(windowed_audios) == expected_windows, f"Should yield {expected_windows} \ + windows when step size equals window size. Yielded {len(windowed_audios)}." -def test_window_generator_window_greater_than_audio_stereo(stereo_audio_sample: Audio) -> None: - """Tests window generator when window size is greater than the audio length for stereo audio.""" - audio_length = stereo_audio_sample.waveform.size(1) +@pytest.mark.parametrize("audio_sample, window_size, step_size", [ + ("mono_audio_sample", 1024, 2048), + ("stereo_audio_sample", 1024, 2048), +]) +def test_window_generator_step_greater_than_window(audio_sample: str, + window_size: int, + step_size: int, + request: pytest.FixtureRequest) -> None: + """Tests window generator when step size is greater than window size.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_length = audio_sample.waveform.size(-1) + + windowed_audios: List[Audio] = list(audio_sample.window_generator(window_size, step_size)) + + # Refine expected windows calculation + expected_windows = (audio_length + step_size - 1) // step_size + assert len(windowed_audios) == expected_windows, f"Should yield {expected_windows} \ + windows when step size is greater than window size. Yielded {len(windowed_audios)}." + + +@pytest.mark.parametrize("audio_sample", [ + "mono_audio_sample", + "stereo_audio_sample", +]) +def test_window_generator_window_greater_than_audio(audio_sample: str, + request: pytest.FixtureRequest) -> None: + """Tests window generator when window size is greater than the audio length.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_length = audio_sample.waveform.size(-1) window_size = audio_length + 1000 # Set window size greater than audio length - step_size = 512 - - windows = list(stereo_audio_sample.window_generator(window_size, step_size)) - - assert len(windows) == 0, f"Should yield no windows when window size is \ - greater than audio length. Yielded {len(windows)}." - - -def test_window_generator_step_greater_than_audio_mono(mono_audio_sample: Audio) -> None: - """Tests window generator when step size is greater than the audio length for mono audio.""" - audio_length = mono_audio_sample.waveform.size(1) - window_size = 1024 - step_size = audio_length + 1000 # Step size greater than audio length - - windows = list(mono_audio_sample.window_generator(window_size, step_size)) - - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size is greater than audio length. Yielded {len(windows)}." - - -def test_window_generator_step_greater_than_audio_stereo(stereo_audio_sample: Audio) -> None: - """Tests window generator when step size is greater than the audio length for stereo audio.""" - audio_length = stereo_audio_sample.waveform.size(1) + step_size = window_size + + windowed_audios: List[Audio] = list(audio_sample.window_generator(window_size, step_size)) + # Expect only 1 window in this case + assert len(windowed_audios) == 1, f"Should yield 1 window when window size is greater \ + than audio length. Yielded {len(windowed_audios)}." + + +@pytest.mark.parametrize("audio_sample", [ + "mono_audio_sample", + "stereo_audio_sample", +]) +def test_window_generator_step_greater_than_audio(audio_sample: str, + request: pytest.FixtureRequest) -> None: + """Tests window generator when step size is greater than the audio length.""" + audio_sample = request.getfixturevalue(audio_sample) + audio_length = audio_sample.waveform.size(1) window_size = 1024 step_size = audio_length + 1000 # Step size greater than audio length - windows = list(stereo_audio_sample.window_generator(window_size, step_size)) + windowed_audios: List[Audio] = list(audio_sample.window_generator(window_size, step_size)) - expected_windows = (audio_length - window_size) // step_size + 1 - assert len(windows) == expected_windows, f"Should yield {expected_windows} \ - windows when step size is greater than audio length. Yielded {len(windows)}." + expected_windows = (audio_length - window_size) // step_size + 1 # This is always 1 + assert len(windowed_audios) == expected_windows, f"Should yield {expected_windows} \ + windows when step size is greater than audio length. Yielded {len(windowed_audios)}." From 78b45cc01cc73fb5ea029f48b38d17b40b22b809 Mon Sep 17 00:00:00 2001 From: fabiocat93 Date: Wed, 25 Sep 2024 17:19:23 -0400 Subject: [PATCH 2/5] fixing style --- src/senselab/audio/data_structures/audio.py | 8 +- src/tests/audio/data_structures/audio_test.py | 127 ++++++++++-------- 2 files changed, 76 insertions(+), 59 deletions(-) diff --git a/src/senselab/audio/data_structures/audio.py b/src/senselab/audio/data_structures/audio.py index 8e2e335..d85a114 100644 --- a/src/senselab/audio/data_structures/audio.py +++ b/src/senselab/audio/data_structures/audio.py @@ -132,12 +132,12 @@ def window_generator(self, window_size: int, step_size: int) -> Generator["Audio print("current_position:", current_position) # Calculate the end position of the window end_position = current_position + window_size - + # If the end_position exceeds the number of samples, take the remaining samples if end_position > num_samples: end_position = num_samples print("end_position:", end_position) - + # Get the windowed waveform window_waveform = self.waveform[:, current_position:end_position] @@ -146,9 +146,9 @@ def window_generator(self, window_size: int, step_size: int) -> Generator["Audio waveform=window_waveform, sampling_rate=self.sampling_rate, orig_path_or_id=f"{self.orig_path_or_id}_{current_position}_{end_position}", - metadata=self.metadata + metadata=self.metadata, ) - + yield window_audio current_position += step_size diff --git a/src/tests/audio/data_structures/audio_test.py b/src/tests/audio/data_structures/audio_test.py index f67d6f8..27476ff 100644 --- a/src/tests/audio/data_structures/audio_test.py +++ b/src/tests/audio/data_structures/audio_test.py @@ -16,11 +16,14 @@ def load_audio(file_path: str) -> Tuple[torch.Tensor, int]: return torchaudio.load(file_path) -@pytest.mark.parametrize("audio_sample, audio_path", [ - ("mono_audio_sample", MONO_AUDIO_PATH), - ("stereo_audio_sample", STEREO_AUDIO_PATH), -]) -def test_audio_creation(audio_sample: str, audio_path: str, request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample, audio_path", + [ + ("mono_audio_sample", MONO_AUDIO_PATH), + ("stereo_audio_sample", STEREO_AUDIO_PATH), + ], +) +def test_audio_creation(audio_sample: Audio, audio_path: str, request: pytest.FixtureRequest) -> None: """Tests mono and stereo audio creation.""" audio_sample = request.getfixturevalue(audio_sample) audio_data, audio_sr = load_audio(audio_path) @@ -32,11 +35,14 @@ def test_audio_creation(audio_sample: str, audio_path: str, request: pytest.Fixt assert audio == audio_sample, "Audios are not exactly equivalent" -@pytest.mark.parametrize("audio_sample, audio_path", [ - ("mono_audio_sample", MONO_AUDIO_PATH), - ("stereo_audio_sample", STEREO_AUDIO_PATH), -]) -def test_audio_creation_uuid(audio_sample: str, audio_path: str, request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample, audio_path", + [ + ("mono_audio_sample", MONO_AUDIO_PATH), + ("stereo_audio_sample", STEREO_AUDIO_PATH), + ], +) +def test_audio_creation_uuid(audio_sample: Audio, audio_path: str, request: pytest.FixtureRequest) -> None: """Tests audio creation with different UUID.""" audio_sample = request.getfixturevalue(audio_sample) audio_data, audio_sr = load_audio(audio_path) @@ -53,27 +59,30 @@ def test_audio_single_tensor(mono_audio_sample: Audio) -> None: ), "Mono audios of tensor shape (num_samples,) should be reshaped to (1, num_samples)" -@pytest.mark.parametrize("audio_sample, audio_path", [ - ("mono_audio_sample", MONO_AUDIO_PATH), -]) -def test_audio_from_list(audio_sample: str, audio_path: str, request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample, audio_path", + [ + ("mono_audio_sample", MONO_AUDIO_PATH), + ], +) +def test_audio_from_list(audio_sample: Audio, audio_path: str, request: pytest.FixtureRequest) -> None: """Tests audio creation from list.""" audio_sample = request.getfixturevalue(audio_sample) audio_data, audio_sr = load_audio(audio_path) audio_from_list = Audio(waveform=list(audio_data[0]), sampling_rate=audio_sr) - assert torch.equal( - audio_sample.waveform, audio_from_list.waveform - ), "List audio should've been converted to Tensor" - - -@pytest.mark.parametrize("audio_sample, window_size, step_size", [ - ("mono_audio_sample", 1024, 512), - ("stereo_audio_sample", 1024, 512), -]) -def test_window_generator_overlap(audio_sample: str, - window_size: int, - step_size: int, - request: pytest.FixtureRequest) -> None: + assert torch.equal(audio_sample.waveform, audio_from_list.waveform), "List audio should've been converted to Tensor" + + +@pytest.mark.parametrize( + "audio_sample, window_size, step_size", + [ + ("mono_audio_sample", 1024, 512), + ("stereo_audio_sample", 1024, 512), + ], +) +def test_window_generator_overlap( + audio_sample: Audio, window_size: int, step_size: int, request: pytest.FixtureRequest +) -> None: """Tests window generator with overlapping windows.""" audio_sample = request.getfixturevalue(audio_sample) audio_length = audio_sample.waveform.size(-1) @@ -90,14 +99,16 @@ def test_window_generator_overlap(audio_sample: str, windows when step size is less than window size. Yielded {len(windowed_audios)}." -@pytest.mark.parametrize("audio_sample, window_size, step_size", [ - ("mono_audio_sample", 1024, 1024), - ("stereo_audio_sample", 1024, 1024), -]) -def test_window_generator_exact_fit(audio_sample: str, - window_size: int, - step_size: int, - request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample, window_size, step_size", + [ + ("mono_audio_sample", 1024, 1024), + ("stereo_audio_sample", 1024, 1024), + ], +) +def test_window_generator_exact_fit( + audio_sample: Audio, window_size: int, step_size: int, request: pytest.FixtureRequest +) -> None: """Tests window generator when step size equals window size.""" audio_sample = request.getfixturevalue(audio_sample) audio_length = audio_sample.waveform.size(-1) @@ -114,14 +125,16 @@ def test_window_generator_exact_fit(audio_sample: str, windows when step size equals window size. Yielded {len(windowed_audios)}." -@pytest.mark.parametrize("audio_sample, window_size, step_size", [ - ("mono_audio_sample", 1024, 2048), - ("stereo_audio_sample", 1024, 2048), -]) -def test_window_generator_step_greater_than_window(audio_sample: str, - window_size: int, - step_size: int, - request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample, window_size, step_size", + [ + ("mono_audio_sample", 1024, 2048), + ("stereo_audio_sample", 1024, 2048), + ], +) +def test_window_generator_step_greater_than_window( + audio_sample: Audio, window_size: int, step_size: int, request: pytest.FixtureRequest +) -> None: """Tests window generator when step size is greater than window size.""" audio_sample = request.getfixturevalue(audio_sample) audio_length = audio_sample.waveform.size(-1) @@ -134,12 +147,14 @@ def test_window_generator_step_greater_than_window(audio_sample: str, windows when step size is greater than window size. Yielded {len(windowed_audios)}." -@pytest.mark.parametrize("audio_sample", [ - "mono_audio_sample", - "stereo_audio_sample", -]) -def test_window_generator_window_greater_than_audio(audio_sample: str, - request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample", + [ + "mono_audio_sample", + "stereo_audio_sample", + ], +) +def test_window_generator_window_greater_than_audio(audio_sample: Audio, request: pytest.FixtureRequest) -> None: """Tests window generator when window size is greater than the audio length.""" audio_sample = request.getfixturevalue(audio_sample) audio_length = audio_sample.waveform.size(-1) @@ -152,12 +167,14 @@ def test_window_generator_window_greater_than_audio(audio_sample: str, than audio length. Yielded {len(windowed_audios)}." -@pytest.mark.parametrize("audio_sample", [ - "mono_audio_sample", - "stereo_audio_sample", -]) -def test_window_generator_step_greater_than_audio(audio_sample: str, - request: pytest.FixtureRequest) -> None: +@pytest.mark.parametrize( + "audio_sample", + [ + "mono_audio_sample", + "stereo_audio_sample", + ], +) +def test_window_generator_step_greater_than_audio(audio_sample: Audio, request: pytest.FixtureRequest) -> None: """Tests window generator when step size is greater than the audio length.""" audio_sample = request.getfixturevalue(audio_sample) audio_length = audio_sample.waveform.size(1) From 57b120388a4be44318107bbaee1ed9dc8f046cc3 Mon Sep 17 00:00:00 2001 From: fabiocat93 Date: Wed, 25 Sep 2024 17:19:57 -0400 Subject: [PATCH 3/5] fixing style #2 --- src/tests/audio/data_structures/audio_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tests/audio/data_structures/audio_test.py b/src/tests/audio/data_structures/audio_test.py index 27476ff..5981984 100644 --- a/src/tests/audio/data_structures/audio_test.py +++ b/src/tests/audio/data_structures/audio_test.py @@ -1,6 +1,5 @@ """Module for testing Audio data structures.""" -import warnings from typing import List, Tuple import pytest From 2a6438f37dd295ebe00ccec8c39698ac45253e48 Mon Sep 17 00:00:00 2001 From: fabiocat93 Date: Thu, 26 Sep 2024 17:32:42 -0400 Subject: [PATCH 4/5] removing print statements --- src/senselab/audio/data_structures/audio.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/senselab/audio/data_structures/audio.py b/src/senselab/audio/data_structures/audio.py index d85a114..0a0b5db 100644 --- a/src/senselab/audio/data_structures/audio.py +++ b/src/senselab/audio/data_structures/audio.py @@ -125,18 +125,17 @@ def window_generator(self, window_size: int, step_size: int) -> Generator["Audio ) num_samples = self.waveform.size(-1) - print("num_samples:", num_samples) current_position = 0 while current_position < num_samples: - print("current_position:", current_position) # Calculate the end position of the window end_position = current_position + window_size # If the end_position exceeds the number of samples, take the remaining samples + # This is not necessary since it is done automatically when slicing tensors. + # However, it is more explicit. if end_position > num_samples: end_position = num_samples - print("end_position:", end_position) # Get the windowed waveform window_waveform = self.waveform[:, current_position:end_position] From 1c12fc196a36b1f282b39231057e015c89396528 Mon Sep 17 00:00:00 2001 From: fabiocat93 Date: Thu, 26 Sep 2024 17:48:38 -0400 Subject: [PATCH 5/5] fixing style #2 --- src/senselab/audio/data_structures/audio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/senselab/audio/data_structures/audio.py b/src/senselab/audio/data_structures/audio.py index 0a0b5db..e18dd33 100644 --- a/src/senselab/audio/data_structures/audio.py +++ b/src/senselab/audio/data_structures/audio.py @@ -132,7 +132,7 @@ def window_generator(self, window_size: int, step_size: int) -> Generator["Audio end_position = current_position + window_size # If the end_position exceeds the number of samples, take the remaining samples - # This is not necessary since it is done automatically when slicing tensors. + # This is not necessary since it is done automatically when slicing tensors. # However, it is more explicit. if end_position > num_samples: end_position = num_samples