Skip to content

Commit 35a6d53

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 3721224 commit 35a6d53

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎meegkit/asr.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ def asr_calibrate(X, sfreq, cutoff=5, blocksize=100, win_len=0.5,
511511
sig = np.zeros(nc)
512512
for ichan in reversed(range(nc)):
513513
mu[ichan], sig[ichan], alpha, beta = fit_eeg_distribution(
514-
rms[ichan], min_clean_fraction, max_dropout_fraction)
514+
rms[ichan], min_clean_fraction, max_dropout_fraction,
515+
step_sizes=[0.01, 0.01])
515516

516517
T = np.dot(np.diag(mu + cutoff * sig), V.T)
517518
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: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from scipy import signal
88

99
from meegkit.asr import ASR, asr_calibrate, asr_process, clean_windows
10-
from meegkit.utils.asr import yulewalk, yulewalk_filter
10+
from meegkit.utils.asr import fit_eeg_distribution, yulewalk, yulewalk_filter
1111
from meegkit.utils.matrix import sliding_window
1212

1313
# Data files
@@ -258,6 +258,39 @@ def test_asr_class(method, reref, show=False):
258258
ASR(sfreq=125)
259259
ASR(Sfreq=150)
260260

261+
def test_fit_eeg_distribution_default_step_sizes():
262+
"""Default step_sizes matches the documented [0.01, 0.01].
263+
264+
The old default [0.022, 0.6] (copied from fit_quantiles) collapses the
265+
grid search, so callers that omit step_sizes get a degenerate fit.
266+
"""
267+
raw = np.load(os.path.join(THIS_FOLDER, "data", "eeg_raw.npy"))
268+
sfreq = 250
269+
270+
# windowed-RMS vector for one channel, as asr_calibrate builds it
271+
# internally (full recording so there are enough windows to matter)
272+
N = int(np.round(0.5 * sfreq))
273+
step = int(np.round(N * (1 - 0.66)))
274+
x = raw[0]
275+
offsets = np.arange(0, len(x) - N, step).astype(int)
276+
csum = np.zeros(len(x) + 1)
277+
np.cumsum(x ** 2, out=csum[1:])
278+
rms = np.sqrt((csum[offsets + N] - csum[offsets]) / N)
279+
280+
mu_default, sig_default, _, _ = fit_eeg_distribution(rms)
281+
mu_explicit, sig_explicit, _, _ = fit_eeg_distribution(
282+
rms, step_sizes=[0.01, 0.01])
283+
284+
# default must match the documented [0.01, 0.01] grid
285+
np.testing.assert_allclose(mu_default, mu_explicit, rtol=1e-9)
286+
np.testing.assert_allclose(sig_default, sig_explicit, rtol=1e-9)
287+
288+
# the old [0.022, 0.6] default is degenerate -> materially different fit
289+
mu_old, sig_old, _, _ = fit_eeg_distribution(rms, step_sizes=[0.022, 0.6])
290+
assert not np.allclose(mu_old, mu_explicit, rtol=1e-9)
291+
assert not np.allclose(sig_old, sig_explicit, rtol=1e-9)
292+
293+
261294
if __name__ == "__main__":
262295
pytest.main([__file__])
263296
# test_yulewalk(250, True)

0 commit comments

Comments
 (0)