From 71c4dc90752b89f729e53694d6da0eaddcb4fa9d Mon Sep 17 00:00:00 2001 From: Fabi Date: Thu, 8 Aug 2024 20:26:41 +0200 Subject: [PATCH] temporally changed testing strategy --- pyrasa/utils/irasa_utils.py | 25 ---------------- .../notebooks}/test_td.py | 0 tests/conftest.py | 30 ++++++++++++------- tests/settings.py | 19 ++++++------ tests/test_irasa_knee.py | 12 ++++---- 5 files changed, 36 insertions(+), 50 deletions(-) rename {examples => simulations/notebooks}/test_td.py (100%) diff --git a/pyrasa/utils/irasa_utils.py b/pyrasa/utils/irasa_utils.py index f0fdee4..b700735 100644 --- a/pyrasa/utils/irasa_utils.py +++ b/pyrasa/utils/irasa_utils.py @@ -10,7 +10,6 @@ from pyrasa.utils.types import IrasaFun -# TODO: Port to Cython def _gen_irasa( data: np.ndarray, orig_spectrum: np.ndarray, @@ -73,30 +72,6 @@ def _crop_data( return freqs, psd_aperiodic, psd_periodic, psd -def _gen_time_from_sft(SFT: type[dsp.ShortTimeFFT], sgramm: np.ndarray) -> np.ndarray: # noqa N803 - """Generates time from SFT object""" - - tmin, tmax = SFT.extent(sgramm.shape[-1])[:2] - delta_t = SFT.delta_t - - time = np.arange(tmin, tmax, delta_t) - return time - - -def _find_nearest(sgramm_ud: np.ndarray, time_array: np.ndarray, time_value: float) -> np.ndarray: - """Find the nearest time point in an up/downsampled spectrogram""" - - idx = (np.abs(time_array - time_value)).argmin() - - if idx < sgramm_ud.shape[2]: - sgramm_sel = sgramm_ud[:, :, idx] - - elif idx == sgramm_ud.shape[2]: - sgramm_sel = sgramm_ud[:, :, idx - 1] - - return sgramm_sel - - def _get_windows( nperseg: int, dpss_settings: dict, win_func: Callable, win_func_kwargs: dict ) -> tuple[np.ndarray, np.ndarray]: diff --git a/examples/test_td.py b/simulations/notebooks/test_td.py similarity index 100% rename from examples/test_td.py rename to simulations/notebooks/test_td.py diff --git a/tests/conftest.py b/tests/conftest.py index 80a1df9..d1f4924 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,19 +28,29 @@ def knee_aperiodic_signal(exponent, fs, knee_freq): @pytest.fixture(scope='session') -def load_knee_aperiodic_signal(exponent, fs, knee): - base_dir = 'tests/test_data/knee_data/' - yield np.load( - base_dir + f'knee_sim__fs_{fs}__exp1_0__exp2_{exponent}_knee_{knee}_.npy', - ) # allow_pickle=True) +def load_knee_aperiodic_signal(exponent, fs, knee_freq): + # % generate and save knee + knee_sim = sim_knee(n_seconds=N_SECONDS, fs=fs, exponent1=0, exponent2=exponent, knee=knee_freq) + yield knee_sim + # base_dir = 'tests/test_data/knee_data/' + # yield np.load( + # base_dir + f'knee_sim__fs_{fs}__exp1_0__exp2_{exponent}_knee_{knee}_.npy', + # ) # allow_pickle=True) @pytest.fixture(scope='session') -def load_knee_cmb_signal(exponent, fs, knee, osc_freq): - base_dir = 'tests/test_data/knee_osc_data/' - yield np.load( - base_dir + f'cmb_sim__fs_{fs}__exp1_0__exp2_{exponent}_knee_{knee}__osc_freq_{osc_freq}_.npy', - ) # allow_pickle=True) +def load_knee_cmb_signal(exponent, fs, knee_freq, osc_freq): + knee = knee_freq ** np.abs(exponent) + components = { + 'sim_knee': {'exponent1': 0, 'exponent2': exponent, 'knee': knee}, + 'sim_oscillation': {'freq': osc_freq}, + } + cmb_sim = sim_combined(n_seconds=N_SECONDS, fs=fs, components=components) + yield cmb_sim + # base_dir = 'tests/test_data/knee_osc_data/' + # yield np.load( + # base_dir + f'cmb_sim__fs_{fs}__exp1_0__exp2_{exponent}_knee_{knee}__osc_freq_{osc_freq}_.npy', + # ) # allow_pickle=True) # noqa: E501 diff --git a/tests/settings.py b/tests/settings.py index 2554378..caed52f 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -13,19 +13,20 @@ # If you want to play around with this check: simulations/notebooks/sim_peak_tests.py EXPONENT = [-1, -1.5, -2.0] KNEE_FREQ = 15 +KNEE_FREQS = [10, 15, 25] # There seems to be a higher error in knee fits for knee and exponent estimates when # the difference in pre and post knee exponent is low. This kinda makes sense # TODO: Test this systematically -> see whether this is an issue with irasa or slope fitting in general EXP_KNEE_COMBO = [ - (1.0, 10.0), - (1.0, 15.0), - (1.0, 25.0), - (1.5, 125.0), - (1.5, 32.0), - (1.5, 58.0), - (2.0, 100.0), - (2.0, 225.0), - (2.0, 625.0), + (-1.0, 10.0), + (-1.0, 15.0), + (-1.0, 25.0), + (-1.5, 125.0), + (-1.5, 32.0), + (-1.5, 58.0), + (-2.0, 100.0), + (-2.0, 225.0), + (-2.0, 625.0), ] # we test exp + knee combined as both relate to each other TOLERANCE = 0.3 # 0.15 HIGH_TOLERANCE = 0.5 diff --git a/tests/test_irasa_knee.py b/tests/test_irasa_knee.py index c858c87..ea83734 100644 --- a/tests/test_irasa_knee.py +++ b/tests/test_irasa_knee.py @@ -11,9 +11,9 @@ # knee model -@pytest.mark.parametrize('exponent, knee', EXP_KNEE_COMBO, scope='session') +@pytest.mark.parametrize('exponent, knee_freq', EXP_KNEE_COMBO, scope='session') @pytest.mark.parametrize('fs', FS, scope='session') -def test_irasa_knee_peakless(load_knee_aperiodic_signal, fs, exponent, knee): +def test_irasa_knee_peakless(load_knee_aperiodic_signal, fs, exponent, knee_freq): f_range = [0.1, 100] irasa_out = irasa(load_knee_aperiodic_signal, fs, f_range, psd_kwargs={'nperseg': 4 * fs}) # test the shape of the output @@ -33,7 +33,7 @@ def test_irasa_knee_peakless(load_knee_aperiodic_signal, fs, exponent, knee): knee_hat = slope_fit_k.aperiodic_params['Knee'][0] ** ( 1 / (2 * slope_fit_k.aperiodic_params['Exponent_1'][0] + slope_fit_k.aperiodic_params['Exponent_2'][0]) ) - knee_real = knee ** (1 / np.abs(exponent)) + knee_real = knee_freq ** (1 / np.abs(exponent)) assert bool(np.isclose(knee_hat, knee_real, atol=KNEE_TOLERANCE)) # test bic/aic -> should be better for knee assert slope_fit_k.gof['AIC'][0] < slope_fit_f.gof['AIC'][0] @@ -41,10 +41,10 @@ def test_irasa_knee_peakless(load_knee_aperiodic_signal, fs, exponent, knee): # knee model -@pytest.mark.parametrize('exponent, knee', EXP_KNEE_COMBO, scope='session') +@pytest.mark.parametrize('exponent, knee_freq', EXP_KNEE_COMBO, scope='session') @pytest.mark.parametrize('fs', FS, scope='session') @pytest.mark.parametrize('osc_freq', OSC_FREQ, scope='session') -def test_irasa_knee_cmb(load_knee_cmb_signal, fs, exponent, knee, osc_freq): +def test_irasa_knee_cmb(load_knee_cmb_signal, fs, exponent, knee_freq, osc_freq): f_range = [0.1, 100] irasa_out = irasa(load_knee_cmb_signal, fs, f_range, psd_kwargs={'nperseg': 4 * fs}) # test the shape of the output @@ -64,7 +64,7 @@ def test_irasa_knee_cmb(load_knee_cmb_signal, fs, exponent, knee, osc_freq): knee_hat = slope_fit_k.aperiodic_params['Knee'][0] ** ( 1 / (2 * slope_fit_k.aperiodic_params['Exponent_1'][0] + slope_fit_k.aperiodic_params['Exponent_2'][0]) ) - knee_real = knee ** (1 / np.abs(exponent)) + knee_real = knee_freq ** (1 / np.abs(exponent)) assert bool(np.isclose(knee_hat, knee_real, atol=KNEE_TOLERANCE)) # test bic/aic -> should be better for knee assert slope_fit_k.gof['AIC'][0] < slope_fit_f.gof['AIC'][0]