Skip to content

Commit d24300b

Browse files
committed
[FIX] ASR: asr_calibrate must pass step_sizes to fit_eeg_distribution
fit_eeg_distribution's step_sizes default was [0.0220, 0.6000], an accidental copy of the fit_quantiles default. That value collapses the grid search over dropout/scale to essentially a single point, so asr_calibrate (which called fit_eeg_distribution without step_sizes) inherited a degenerate grid, biasing the fitted mu/sig and hence the threshold matrix T. fit_eeg_distribution's own docstring already documents default=[0.01, 0.01] for step_sizes, matching the value clean_windows already passes explicitly. Correct the signature default to match the docstring, and have asr_calibrate pass step_sizes=[0.01, 0.01] explicitly (mirroring clean_windows) so its behavior no longer depends on the buggy default. Added a regression test that builds a windowed-RMS vector from the eeg_raw.npy fixture (mirroring asr_calibrate's own windowing) and asserts that fit_eeg_distribution's default step_sizes now matches an explicit [0.01, 0.01] call, while the old [0.022, 0.6] default diverges materially.
1 parent 107cd1b commit d24300b

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎meegkit/asr.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ def asr_calibrate(X, sfreq, cutoff=5, blocksize=100, win_len=0.5,
541541
sig = np.zeros(nc)
542542
for ichan in reversed(range(nc)):
543543
mu[ichan], sig[ichan], alpha, beta = fit_eeg_distribution(
544-
rms[ichan], min_clean_fraction, max_dropout_fraction)
544+
rms[ichan], min_clean_fraction, max_dropout_fraction,
545+
step_sizes=[0.01, 0.01])
545546

546547
T = np.dot(np.diag(mu + cutoff * sig), V.T)
547548
logging.debug("[ASR] Calibration done.")

‎meegkit/utils/asr.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
def fit_eeg_distribution(X, min_clean_fraction=0.25, max_dropout_fraction=0.1,
13-
fit_quantiles=[0.022, 0.6], step_sizes=[0.0220, 0.6000],
13+
fit_quantiles=[0.022, 0.6], step_sizes=[0.01, 0.01],
1414
shape_range=SHAPE_RANGE):
1515
"""Estimate the mean and SD of clean EEG from contaminated data.
1616

‎tests/test_asr.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,39 @@ def test_clean_windows_offset_phase_drift():
418418
assert abs(int(offsets_raw[-1]) - int(offsets_truncated[-1])) >= 10
419419

420420

421+
def test_fit_eeg_distribution_default_step_sizes():
422+
"""Default step_sizes matches the documented [0.01, 0.01].
423+
424+
The old default [0.022, 0.6] (copied from fit_quantiles) collapses the
425+
grid search, so callers that omit step_sizes get a degenerate fit.
426+
"""
427+
raw = np.load(os.path.join(THIS_FOLDER, "data", "eeg_raw.npy"))
428+
sfreq = 250
429+
430+
# windowed-RMS vector for one channel, as asr_calibrate builds it
431+
# internally (full recording so there are enough windows to matter)
432+
N = int(np.round(0.5 * sfreq))
433+
step = int(np.round(N * (1 - 0.66)))
434+
x = raw[0]
435+
offsets = np.arange(0, len(x) - N, step).astype(int)
436+
csum = np.zeros(len(x) + 1)
437+
np.cumsum(x ** 2, out=csum[1:])
438+
rms = np.sqrt((csum[offsets + N] - csum[offsets]) / N)
439+
440+
mu_default, sig_default, _, _ = fit_eeg_distribution(rms)
441+
mu_explicit, sig_explicit, _, _ = fit_eeg_distribution(
442+
rms, step_sizes=[0.01, 0.01])
443+
444+
# default must match the documented [0.01, 0.01] grid
445+
np.testing.assert_allclose(mu_default, mu_explicit, rtol=1e-9)
446+
np.testing.assert_allclose(sig_default, sig_explicit, rtol=1e-9)
447+
448+
# the old [0.022, 0.6] default is degenerate -> materially different fit
449+
mu_old, sig_old, _, _ = fit_eeg_distribution(rms, step_sizes=[0.022, 0.6])
450+
assert not np.allclose(mu_old, mu_explicit, rtol=1e-9)
451+
assert not np.allclose(sig_old, sig_explicit, rtol=1e-9)
452+
453+
421454
if __name__ == "__main__":
422455
pytest.main([__file__])
423456
# test_yulewalk(250, True)

0 commit comments

Comments
 (0)