Skip to content

Commit

Permalink
temporally changed testing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabi committed Aug 8, 2024
1 parent ecc24ca commit 71c4dc9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 50 deletions.
25 changes: 0 additions & 25 deletions pyrasa/utils/irasa_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pyrasa.utils.types import IrasaFun


# TODO: Port to Cython
def _gen_irasa(
data: np.ndarray,
orig_spectrum: np.ndarray,
Expand Down Expand Up @@ -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]:
Expand Down
File renamed without changes.
30 changes: 20 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
19 changes: 10 additions & 9 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/test_irasa_knee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,18 +33,18 @@ 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]
assert slope_fit_k.gof['BIC'][0] < slope_fit_f.gof['BIC'][0]


# 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
Expand All @@ -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]
Expand Down

0 comments on commit 71c4dc9

Please sign in to comment.