Skip to content

Commit

Permalink
Reformat code with updated black
Browse files Browse the repository at this point in the history
  • Loading branch information
iver56 committed Sep 2, 2024
1 parent 8969984 commit 02aaf0b
Show file tree
Hide file tree
Showing 28 changed files with 5 additions and 65 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
- seaborn>=0.9,<1.0
- pip:
- audioread==2.1.8
- black
- black==23.12.1
- coverage==5.3
- julius>=0.2.3,<0.3
- librosa==0.9.1
Expand Down
2 changes: 1 addition & 1 deletion scripts/measure_convolve_execution_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
execution_times[(t.description, num_examples)].append(t.execution_time)

normalized_execution_times = {}
for (description, batch_size) in execution_times:
for description, batch_size in execution_times:
times = execution_times[(description, batch_size)]
if len(times) == 0:
continue
Expand Down
1 change: 0 additions & 1 deletion tests/test_background_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def test_background_noise_no_guarantee_with_empty_tensor(self):
self.assertEqual(mixed_input.size(0), self.empty_input_audio.size(0))

def test_background_noise_guaranteed_with_zero_length_samples(self):

with self.assertWarns(UserWarning) as warning_context_manager:
mixed_input = self.bg_noise_transform_guaranteed(
self.empty_input_audio, self.sample_rate
Expand Down
4 changes: 3 additions & 1 deletion tests/test_colored_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def test_invalid_params():

def test_various_lengths_and_sample_rates():
random.seed(42)
transform = AddColoredNoise(min_snr_in_db=10, max_snr_in_db=12, p=1.0, output_type="dict")
transform = AddColoredNoise(
min_snr_in_db=10, max_snr_in_db=12, p=1.0, output_type="dict"
)

for _ in range(100):
length = random.randint(1000, 100_000)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_gain.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ def test_reset_distribution(self):
actual_gains_in_db = []
for i in range(processed_samples.shape[0]):
if not np.allclose(processed_samples[i], samples_batch[i]):

estimated_gain_factor = np.mean(processed_samples[i] / samples_batch[i])
estimated_gain_factor_in_db = convert_amplitude_ratio_to_decibels(
torch.tensor(estimated_gain_factor)
Expand Down Expand Up @@ -526,7 +525,6 @@ def test_cuda_reset_distribution(self):
actual_gains_in_db = []
for i in range(processed_samples.shape[0]):
if not np.allclose(processed_samples[i], samples_batch[i]):

estimated_gain_factor = np.mean(processed_samples[i] / samples_batch[i])
estimated_gain_factor_in_db = convert_amplitude_ratio_to_decibels(
torch.tensor(estimated_gain_factor)
Expand Down
1 change: 0 additions & 1 deletion tests/test_impulse_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ def test_impulse_response_guaranteed_with_zero_length_samples(ir_transform):


def test_impulse_response_access_file_paths(ir_path, sample_rate, input_audios):

augment = ApplyImpulseResponse(
ir_path, p=1.0, sample_rate=sample_rate, output_type="dict"
)
Expand Down
7 changes: 0 additions & 7 deletions tests/test_padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class TestPadding(unittest.TestCase):
def test_padding_end(self):

audio_samples = torch.rand(size=(2, 2, 32000), dtype=torch.float32)
augment = Padding(
min_fraction=0.2,
Expand All @@ -24,7 +23,6 @@ def test_padding_end(self):
assert_almost_equal(padded_samples[..., -6400:].numpy(), np.zeros((2, 2, 6400)))

def test_padding_start(self):

audio_samples = torch.rand(size=(2, 2, 32000), dtype=torch.float32)
augment = Padding(
min_fraction=0.2,
Expand All @@ -39,7 +37,6 @@ def test_padding_start(self):
assert_almost_equal(padded_samples[..., :6400].numpy(), np.zeros((2, 2, 6400)))

def test_padding_zero(self):

audio_samples = torch.rand(size=(2, 2, 32000), dtype=torch.float32)
augment = Padding(min_fraction=0.2, max_fraction=0.5, p=0.0, output_type="dict")
padded_samples = augment(audio_samples).samples
Expand All @@ -48,7 +45,6 @@ def test_padding_zero(self):
assert_almost_equal(audio_samples.numpy(), padded_samples.numpy())

def test_padding_perexample(self):

audio_samples = torch.rand(size=(10, 2, 32000), dtype=torch.float32)
augment = Padding(
min_fraction=0.2,
Expand All @@ -72,7 +68,6 @@ def test_padding_perexample(self):
self.assertLess(padded_samples.sum(), audio_samples.numpy().sum())

def test_padding_perchannel(self):

audio_samples = torch.rand(size=(10, 2, 32000), dtype=torch.float32)
augment = Padding(
min_fraction=0.2,
Expand All @@ -96,7 +91,6 @@ def test_padding_perchannel(self):
self.assertLess(padded_samples.sum(), audio_samples.numpy().sum())

def test_padding_variability_perexample(self):

audio_samples = torch.rand(size=(10, 2, 32000), dtype=torch.float32)
augment = Padding(
min_fraction=0.2,
Expand All @@ -123,7 +117,6 @@ def test_padding_variability_perexample(self):

@pytest.mark.skipif(not torch.cuda.is_available(), reason="Requires CUDA")
def test_padding_cuda(self):

audio_samples = torch.rand(
size=(2, 2, 32000), dtype=torch.float32, device=torch.device("cuda")
)
Expand Down
1 change: 0 additions & 1 deletion tests/test_random_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def test_crop_larger_cropto(self):
@pytest.mark.skip(reason="output_type is not implemented yet")
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Requires CUDA")
def test_crop_on_device_cuda(self):

samples = torch.rand(
size=(8, 2, 32000), dtype=torch.float32, device=torch.device("cuda")
)
Expand Down
5 changes: 0 additions & 5 deletions tests/test_spliceout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class TestSpliceout(unittest.TestCase):
def test_splice_out(self):

audio_samples = torch.rand(size=(8, 1, 32000), dtype=torch.float32)
augment = Compose(
[
Expand All @@ -24,7 +23,6 @@ def test_splice_out(self):
assert splice_out_samples.dtype == np.float32

def test_splice_out_odd_hann(self):

audio_samples = torch.rand(size=(8, 1, 32000), dtype=torch.float32)
augment = Compose(
[
Expand All @@ -39,7 +37,6 @@ def test_splice_out_odd_hann(self):
assert splice_out_samples.dtype == np.float32

def test_splice_out_per_batch(self):

audio_samples = torch.rand(size=(8, 1, 32000), dtype=torch.float32)
augment = Compose(
[
Expand All @@ -62,7 +59,6 @@ def test_splice_out_per_batch(self):
self.assertEqual(splice_out_samples.shape, audio_samples.shape)

def test_splice_out_multichannel(self):

audio_samples = torch.rand(size=(8, 2, 32000), dtype=torch.float32)
augment = Compose(
[
Expand All @@ -81,7 +77,6 @@ def test_splice_out_multichannel(self):
@pytest.mark.skip(reason="This test fails and SpliceOut is not released yet")
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Requires CUDA")
def test_splice_out_cuda(self):

audio_samples = (
torch.rand(
size=(8, 1, 32000), dtype=torch.float32, device=torch.device("cuda")
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/band_stop_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

perturbed = super().apply_transform(
samples.clone(),
sample_rate,
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/colored_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

batch_size, num_channels, num_samples = samples.shape

# (batch_size, num_samples)
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/gain.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

return ObjectDict(
samples=samples * self.transform_parameters["gain_factors"],
sample_rate=sample_rate,
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/high_pass_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

perturbed = super().apply_transform(
samples=samples.clone(),
sample_rate=sample_rate,
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

return ObjectDict(
samples=samples,
sample_rate=sample_rate,
Expand Down
2 changes: 0 additions & 2 deletions torch_audiomentations/augmentations/impulse_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def randomize_parameters(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
):

batch_size, _, _ = samples.shape

audio = self.audio if hasattr(self, "audio") else Audio(sample_rate, mono=True)
Expand All @@ -105,7 +104,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

batch_size, num_channels, num_samples = samples.shape

# (batch_size, 1, max_ir_length)
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/low_pass_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

batch_size, num_channels, num_samples = samples.shape

if self.cached_lpf is None:
Expand Down
2 changes: 0 additions & 2 deletions torch_audiomentations/augmentations/mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def randomize_parameters(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
):

batch_size, num_channels, num_samples = samples.shape
snr_distribution = torch.distributions.Uniform(
low=torch.tensor(
Expand Down Expand Up @@ -109,7 +108,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

snr = self.transform_parameters["snr_in_db"]
idx = self.transform_parameters["sample_idx"]

Expand Down
2 changes: 0 additions & 2 deletions torch_audiomentations/augmentations/padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Padding(BaseWaveformTransform):

supported_modes = {"per_batch", "per_example", "per_channel"}
supports_multichannel = True
requires_sample_rate = False
Expand Down Expand Up @@ -70,7 +69,6 @@ def apply_transform(
targets: Optional[int] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

for i, index in enumerate(self.transform_parameters["pad_length"]):
if self.pad_section == "start":
samples[i, :, :index] = 0.0
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/peak_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

if "divisors" in self.transform_parameters:
samples[self.transform_parameters["selector"]] /= self.transform_parameters[
"divisors"
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/polarity_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

return ObjectDict(
samples=-samples,
sample_rate=sample_rate,
Expand Down
2 changes: 0 additions & 2 deletions torch_audiomentations/augmentations/random_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __init__(
raise ValueError('max_length_unit must be "samples" or "seconds"')

def forward(self, samples, sampling_rate: typing.Optional[int] = None):

sample_rate = sampling_rate or self.sampling_rate
if sample_rate is None:
raise RuntimeError("sample_rate is required")
Expand Down Expand Up @@ -74,7 +73,6 @@ def forward(self, samples, sampling_rate: typing.Optional[int] = None):
(samples.shape[0], samples.shape[1], self.num_samples), device=samples.device
)
for i, sample in enumerate(samples):

samples_cropped[i] = sample.unsqueeze(0)[
:, :, start_indices[i] : start_indices[i] + self.num_samples
]
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

num_samples_to_shift = self.transform_parameters["num_samples_to_shift"]

# Select fastest implementation based on device
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/shuffle_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

if samples.shape[1] == 1:
warnings.warn(
"Mono audio was passed to ShuffleChannels - there are no channels to shuffle."
Expand Down
3 changes: 0 additions & 3 deletions torch_audiomentations/augmentations/splice_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def randomize_parameters(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
):

self.transform_parameters["splice_lengths"] = torch.randint(
low=0,
high=int(sample_rate * self.max_width * 1e-3),
Expand All @@ -68,11 +67,9 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

spliceout_samples = []

for i in range(samples.shape[0]):

random_lengths = self.transform_parameters["splice_lengths"][i]
sample = samples[i][:, :]
for j in range(self.num_time_intervals):
Expand Down
1 change: 0 additions & 1 deletion torch_audiomentations/augmentations/time_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def apply_transform(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

# torch.flip() is supposed to be slower than np.flip()
# An alternative is to use advanced indexing: https://github.com/pytorch/pytorch/issues/16424
# reverse_index = torch.arange(selected_samples.size(-1) - 1, -1, -1).to(selected_samples.device)
Expand Down
3 changes: 0 additions & 3 deletions torch_audiomentations/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def forward(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

inputs = ObjectDict(
samples=samples,
sample_rate=sample_rate,
Expand Down Expand Up @@ -191,7 +190,6 @@ def forward(
targets: Optional[Tensor] = None,
target_rate: Optional[int] = None,
) -> ObjectDict:

inputs = ObjectDict(
samples=samples,
sample_rate=sample_rate,
Expand All @@ -200,7 +198,6 @@ def forward(
)

if random.random() < self.p:

if not self.are_parameters_frozen:
self.randomize_parameters()

Expand Down
Loading

0 comments on commit 02aaf0b

Please sign in to comment.