From 3f4359faa91b33601531abd017b3b9e16b403a1b Mon Sep 17 00:00:00 2001 From: Anne Heimes Date: Thu, 12 Dec 2024 12:00:52 +0100 Subject: [PATCH 1/3] fix "NPY" --- pyproject.toml | 10 +++++----- pyrato/__init__.py | 2 +- pyrato/analytic/__init__.py | 4 ++-- pyrato/analytic/analytic.py | 4 ++-- pyrato/analytic/impedance.py | 6 +++--- tests/test_analytic_impedance.py | 12 ++++++------ tests/test_dsp.py | 16 +++++++++------- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bc59a49..1fe1849 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -105,11 +105,11 @@ lint.select = [ # "PT", # Pytest style # "A", # Avoid builtin function and type shadowing # "ERA", # No commented out code - # "NPY", # Check all numpy related deprecations - # "COM", # trailing comma rules - # "I002", # missing required import - # "TID252", # Use absolute over relative imports - # "FIX", # Code should not contain FIXME, TODO, etc + "NPY", # Check all numpy related deprecations + "COM", # trailing comma rules + "I002", # missing required import + "TID252", # Use absolute over relative imports + "FIX", # Code should not contain FIXME, TODO, etc ] # Ignore missing docstrings in tests diff --git a/pyrato/__init__.py b/pyrato/__init__.py index da183f1..16e27a0 100644 --- a/pyrato/__init__.py +++ b/pyrato/__init__.py @@ -8,7 +8,7 @@ from .rap import ( - reverberation_time_linear_regression + reverberation_time_linear_regression, ) from .roomacoustics import ( reverberation_time_energy_decay_curve, diff --git a/pyrato/analytic/__init__.py b/pyrato/analytic/__init__.py index e80c1a8..891529e 100644 --- a/pyrato/analytic/__init__.py +++ b/pyrato/analytic/__init__.py @@ -1,9 +1,9 @@ from .analytic import ( rectangular_room_rigid_walls, - eigenfrequencies_rectangular_room_rigid + eigenfrequencies_rectangular_room_rigid, ) __all__ = ( 'rectangular_room_rigid_walls', - 'eigenfrequencies_rectangular_room_rigid' + 'eigenfrequencies_rectangular_room_rigid', ) diff --git a/pyrato/analytic/analytic.py b/pyrato/analytic/analytic.py index c32b450..e4befe6 100644 --- a/pyrato/analytic/analytic.py +++ b/pyrato/analytic/analytic.py @@ -76,7 +76,7 @@ def eigenfrequencies_rectangular_room_rigid( for n_y in range(0, n_y_max): n_modes += int(np.floor(np.real( np.sqrt( - (2*f_max/c)**2 - (n_x/L_x)**2 - (n_y/L_y)**2 + (2*f_max/c)**2 - (n_x/L_x)**2 - (n_y/L_y)**2, ) * L_z))) + 1 n = np.zeros((3, n_modes), dtype=int) @@ -89,7 +89,7 @@ def eigenfrequencies_rectangular_room_rigid( for n_y in range(0, n_y_max): n_z_max = int(np.floor(np.real( np.sqrt( - (2*f_max/c)**2 - (n_x/L_x)**2 - (n_y/L_y)**2 + (2*f_max/c)**2 - (n_x/L_x)**2 - (n_y/L_y)**2, ) * L_z))) + 1 idx_end = idx + n_z_max diff --git a/pyrato/analytic/impedance.py b/pyrato/analytic/impedance.py index a631391..7b2053f 100644 --- a/pyrato/analytic/impedance.py +++ b/pyrato/analytic/impedance.py @@ -284,7 +284,7 @@ def eigenfrequencies_rectangular_room_impedance( mask = ks >= 0.02 ks_search = ks[mask] k_ns = normal_eigenfrequencies_rectangular_room_impedance( - L, ks_search, k_max, zeta + L, ks_search, k_max, zeta, ) for idx in range(0, len(L)): k_ns[idx] = np.hstack(( @@ -394,7 +394,7 @@ def pressure_modal_superposition( k_ns_xyz = np.array([ k_ns[0][mode_indices[:, 0]], k_ns[1][mode_indices[:, 1]], - k_ns[2][mode_indices[:, 2]] + k_ns[2][mode_indices[:, 2]], ]) phi = np.arctanh(ks/(zeta_0 * k_ns_xyz.T).T) @@ -491,7 +491,7 @@ def rectangular_room_impedance( k_ns_xyz = np.array([ k_ns[0][mode_indices[:, 0]], k_ns[1][mode_indices[:, 1]], - k_ns[2][mode_indices[:, 2]] + k_ns[2][mode_indices[:, 2]], ]) return rir, spectrum, k_ns_xyz diff --git a/tests/test_analytic_impedance.py b/tests/test_analytic_impedance.py index 8e625e7..f43ded7 100644 --- a/tests/test_analytic_impedance.py +++ b/tests/test_analytic_impedance.py @@ -12,7 +12,7 @@ def test_eigenfreq_impedance_1d_real(): k_max = 1e3*2*np.pi/c k_ns = analytic.eigenfrequencies_rectangular_room_1d( - L, k, k_max, zeta + L, k, k_max, zeta, ) truth = np.array([ @@ -35,7 +35,7 @@ def test_eigenfreq_impedance_1d_real_jac(): k_max = 1e3*2*np.pi/c k_ns = analytic.eigenfrequencies_rectangular_room_1d( - L, k, k_max, zeta, gradient=True + L, k, k_max, zeta, gradient=True, ) truth = np.array([ @@ -59,7 +59,7 @@ def test_analytic_shoebox_eigenfreqs_impedance_multi_k(): k = np.linspace(0, k_max*1.1, 2**10) k_ns, _ = analytic.eigenfrequencies_rectangular_room_impedance( - L, k, k_max, zetas + L, k, k_max, zetas, ) truth = np.array([ @@ -103,7 +103,7 @@ def test_analytic_shoebox_eigenfreqs_impedance(): k_max = 1e3*2*np.pi/c k_ns, _ = analytic.eigenfrequencies_rectangular_room_impedance( - L, k, k_max, zetas + L, k, k_max, zetas, ) truth = np.array([ @@ -148,7 +148,7 @@ def test_analytic_eigenfrequencies_impedance_cplx(): k = np.linspace(k_min, k_max*1.1, 2**10) k_ns, _ = analytic.eigenfrequencies_rectangular_room_impedance( - L, k, k_max, zetas, only_normal=True + L, k, k_max, zetas, only_normal=True, ) k_ns_x = np.loadtxt( @@ -180,7 +180,7 @@ def test_analytic_eigenfrequencies_impedance_zeta15(): k = np.linspace(k_min, k_max*1.1, 2**10) k_ns, _ = analytic.eigenfrequencies_rectangular_room_impedance( - L, k, k_max, zetas, only_normal=True + L, k, k_max, zetas, only_normal=True, ) k_ns_x = np.loadtxt( diff --git a/tests/test_dsp.py b/tests/test_dsp.py index 6d2b82a..f168cff 100644 --- a/tests/test_dsp.py +++ b/tests/test_dsp.py @@ -28,7 +28,8 @@ def test_start_ir_insufficient_snr(): snr = 15 - noise = np.random.randn(n_samples) + rng = np.random.default_rng() + noise = rng.standard_normal(n_samples) noise = noise / np.sqrt(np.mean(np.abs(noise**2))) * 10**(-snr/20) noise = pf.Signal(noise, 44100) @@ -42,8 +43,8 @@ def test_start_ir(): n_samples = 2**10 ir = np.zeros(n_samples) snr = 60 - - noise = pf.Signal(np.random.randn(n_samples) * 10**(-snr/20), 44100) + rng = np.random.default_rng() + noise = pf.Signal(rng.standard_normal(n_samples) * 10**(-snr/20), 44100) start_sample = 24 ir[start_sample] = 1 @@ -77,10 +78,11 @@ def test_start_ir_multidim(): n_channels = 3 ir = np.zeros((n_channels, n_samples)) + rng = np.random.default_rng() snr = 60 noise = pf.Signal( - np.random.randn(n_channels, n_samples) * 10**(-snr/20), 44100) + rng.standard_normal(n_channels, n_samples) * 10**(-snr/20), 44100) start_sample = [24, 5, 43] ir[[0, 1, 2], start_sample] = 1 @@ -94,7 +96,7 @@ def test_start_ir_multidim(): ir = np.zeros((2, n_channels, n_samples)) noise = pf.Signal( - np.random.randn(2, n_channels, n_samples) * 10**(-snr/20), 44100) + rng.standard_normal(2, n_channels, n_samples) * 10**(-snr/20), 44100) start_sample_1 = [24, 5, 43] ir[0, [0, 1, 2], start_sample_1] = 1 @@ -144,9 +146,9 @@ def test_max_ir(): ir = np.zeros(n_samples) snr = 60 - + rng = np.random.default_rng() noise = pf.Signal( - np.random.randn(n_samples) * 10**(-snr/20), 44100) + rng.standard_normal(n_samples) * 10**(-snr/20), 44100) start_sample = 24 ir[start_sample] = 1 From a199f46782cf0ff09dbdb5079bfc07df2ed3ae28 Mon Sep 17 00:00:00 2001 From: Anne Heimes Date: Thu, 12 Dec 2024 12:02:15 +0100 Subject: [PATCH 2/3] fix "ERA" --- pyproject.toml | 2 +- tests/test_data/generate_test_data.py | 36 --------------------------- 2 files changed, 1 insertion(+), 37 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1fe1849..2da9307 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -104,7 +104,7 @@ lint.select = [ # "D417", # Missing argument descriptions in the docstring # "PT", # Pytest style # "A", # Avoid builtin function and type shadowing - # "ERA", # No commented out code + "ERA", # No commented out code "NPY", # Check all numpy related deprecations "COM", # trailing comma rules "I002", # missing required import diff --git a/tests/test_data/generate_test_data.py b/tests/test_data/generate_test_data.py index 2d67743..45e4c1e 100644 --- a/tests/test_data/generate_test_data.py +++ b/tests/test_data/generate_test_data.py @@ -27,34 +27,6 @@ max_freq = 1000 n_samples = 2**13 -# rir_1 = analytic.rectangular_room_rigid_walls( -# dimensions=room_dimensions, source=src_pos, receiver=rec_pos, reverberation_time=t_60, max_freq=max_freq, -# samplingrate=sampling_rate, speed_of_sound=343.9, n_samples=n_samples*2)[0] - -# rir_2 = analytic.rectangular_room_rigid_walls( -# dimensions=room_dimensions, source=src_pos, receiver=rec_pos, reverberation_time=t_60*2, max_freq=max_freq, -# samplingrate=sampling_rate, speed_of_sound=343.9, n_samples=n_samples*2)[0] - -# # %% - -# rir_array = np.zeros(([2, rir_2.time.size])) - -# psnr = 50 - -# # rir_1 /= np.amax(np.abs(rir_1)) -# # rir_2 /= np.amax(np.abs(rir_2)) - -# rir_array[0] = rir_1.time -# rir_array[1] = rir_2.time - -# rir_array = pf.dsp.normalize(pf.Signal(rir_array, sampling_rate)) - -# rms = 10**(-(psnr-10) / 20) -# noise = pf.signals.noise( -# n_samples*2, rms=rms, sampling_rate=sampling_rate, seed=1) - -# rir_array += noise - # %% # Use existing RIR to avoid re-generating data for all tests @@ -130,11 +102,6 @@ rir_array, freq='broadband', is_energy=False, time_shift=True, channel_independent=False, plot=False) -# noise_energy_from_edc_1D = pyrato.edc.estimate_noise_energy_from_edc( -# edc_lundeby_chu_1D, intersection_time_1D[0], sampling_rate) -# noise_energy_from_edc_2D = pyrato.edc.estimate_noise_energy_from_edc( -# edc_lundeby_chu_2D, intersection_time_2D[0], sampling_rate) - # %% pf.plot.time(edc_lundeby_2D, dB=True, log_prefix=10) # %% @@ -205,7 +172,4 @@ np.savetxt("intersection_time_1D.csv", intersection_time_1D, delimiter=",") np.savetxt("intersection_time_2D.csv", intersection_time_2D, delimiter=",") -# np.savetxt("noise_energy_from_edc_1D.csv", noise_energy_from_edc_1D, delimiter=",") -# np.savetxt("noise_energy_from_edc_2D.csv", noise_energy_from_edc_2D, delimiter=",") - # %% From ba76fd8bde8b0b4943f4a7e0636a54242cac5464 Mon Sep 17 00:00:00 2001 From: Anne Heimes Date: Thu, 12 Dec 2024 12:02:56 +0100 Subject: [PATCH 3/3] fix "PT" --- pyproject.toml | 4 ++-- tests/test_rt.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2da9307..0d7f653 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,8 +102,8 @@ lint.select = [ # "D", # Docstring guidelines # "NPY", # Check all numpy related deprecations # "D417", # Missing argument descriptions in the docstring - # "PT", # Pytest style - # "A", # Avoid builtin function and type shadowing + "PT", # Pytest style + "A", # Avoid builtin function and type shadowing "ERA", # No commented out code "NPY", # Check all numpy related deprecations "COM", # trailing comma rules diff --git a/tests/test_rt.py b/tests/test_rt.py index bb2ca57..8513162 100644 --- a/tests/test_rt.py +++ b/tests/test_rt.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- """ Tests for reverberation time related things. """ -from pytest import raises - import numpy as np import numpy.testing as npt @@ -62,5 +60,5 @@ def test_rt_from_edc_error(): edc_exp = pf.TimeData(10**(edc/10), times) T = 'Bla' - with raises(ValueError, match='is not a valid interval.'): + with pytest.raises(ValueError, match='is not a valid interval.'): ra.reverberation_time_linear_regression(edc_exp, T=T)