Skip to content

Commit ba7aa78

Browse files
sappelhoffnbara
andauthored
[FIX] ASR: asr_calibrate must pass step_sizes to fit_eeg_distribution (#109)
`fit_eeg_distribution`'s `step_sizes` default was `[0.0220, 0.6000]`, an accidental copy of the `fit_quantiles` default. That value collapses the scale grid search 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`. The function's own docstring already documents `default=[0.01, 0.01]`, matching the value `clean_windows` passes explicitly. Correct the signature default to match the docstring, and have `asr_calibrate` pass `step_sizes=[0.01, 0.01]` explicitly so its behavior no longer depends on the default. ### Testing Added a regression test that builds a windowed-RMS vector from the `eeg_raw.npy` fixture and asserts the default fit now matches an explicit `[0.01, 0.01]` call while diverging materially from the old `[0.022, 0.6]` default. Full `tests/test_asr.py` passes; `ruff` clean. --------- Co-authored-by: nbara <10333715+nbara@users.noreply.github.com>
1 parent 107cd1b commit ba7aa78

5 files changed

Lines changed: 46 additions & 11 deletions

File tree

‎Makefile‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# simple makefile to simplify repetetive build env management tasks under posix
22
# caution: testing won't work on windows, see README
33
PYTHON ?= python
4-
PYTESTS ?= py.test
4+
PYTESTS ?= pytest
55
CTAGS ?= ctags
66
CODESPELL_SKIPS ?= "*.html,*.fif,*.eve,*.gz,*.tgz,*.zip,*.mat,*.stc,*.label,*.w,*.bz2,*.annot,*.sulc,*.log,*.local-copy,*.orig_avg,*.inflated_avg,*.gii,*.pyc,*.doctree,*.pickle,*.inv,*.png,*.edf,*.touch,*.thickness,*.nofix,*.volume,*.defect_borders,*.mgh,lh.*,rh.*,COR-*,FreeSurferColorLUT.txt,*.examples,.xdebug_mris_calc,bad.segments,BadChannels,*.hist,empty_file,*.orig,*.js,*.map,*.ipynb,searchindex.dat"
77
CODESPELL_DIRS ?= meegkit/ examples/
@@ -85,21 +85,21 @@ install-dev:
8585

8686
# Tests
8787
# =============================================================================
88-
test: in
88+
test:
8989
rm -f .coverage
90-
$(PYTESTS) -m 'not ultraslowtest' meegkit
90+
$(PYTESTS) -m 'not ultraslowtest'
9191

92-
test-verbose: in
92+
test-verbose:
9393
rm -f .coverage
94-
$(PYTESTS) -m 'not ultraslowtest' meegkit --verbose
94+
$(PYTESTS) -m 'not ultraslowtest' --verbose
9595

96-
test-fast: in
96+
test-fast:
9797
rm -f .coverage
98-
$(PYTESTS) -m 'not slowtest' meegkit
98+
$(PYTESTS) -m 'not slowtest'
9999

100-
test-full: in
100+
test-full:
101101
rm -f .coverage
102-
$(PYTESTS) meegkit
102+
$(PYTESTS)
103103

104104
.PHONY: init test
105105

‎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

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ filterwarnings = [
9898
addopts = """
9999
--color=yes
100100
--durations 10
101+
--noplots
101102
"""
102103

103104
[tool.coverage.run]

‎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)