Skip to content

Commit de51231

Browse files
committed
[FIX] ASR: reuse full SHAPE_RANGE grid in clean_windows (include beta=3.5)
clean_windows hardcoded np.arange(1.7, 3.5, 0.15), a 12-point grid that excludes the beta=3.5 endpoint, overriding the correct 13-point SHAPE_RANGE that fit_eeg_distribution defaults to. Reuse SHAPE_RANGE so the KL grid search can select beta=3.5.
1 parent 3721224 commit de51231

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎meegkit/asr.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
from statsmodels.robust.scale import mad
99

1010
from .utils import block_covariance, nonlinear_eigenspace
11-
from .utils.asr import fit_eeg_distribution, geometric_median, yulewalk, yulewalk_filter
11+
from .utils.asr import (
12+
SHAPE_RANGE,
13+
fit_eeg_distribution,
14+
geometric_median,
15+
yulewalk,
16+
yulewalk_filter,
17+
)
1218

1319

1420
class ASR:
@@ -322,7 +328,7 @@ def clean_windows(X, sfreq, max_bad_chans=0.2, zthresholds=[-3.5, 5],
322328
# set internal variables
323329
truncate_quant = [0.0220, 0.6000]
324330
step_sizes = [0.01, 0.01]
325-
shape_range = np.arange(1.7, 3.5, 0.15)
331+
shape_range = SHAPE_RANGE
326332
max_bad_chans = np.round(X.shape[0] * max_bad_chans)
327333

328334
# set data indices

‎tests/test_asr.py‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""ASR test."""
22
import os
3+
from unittest.mock import patch
34

45
import matplotlib.pyplot as plt
56
import numpy as np
67
import pytest
78
from scipy import signal
89

910
from meegkit.asr import ASR, asr_calibrate, asr_process, clean_windows
10-
from meegkit.utils.asr import yulewalk, yulewalk_filter
11+
from meegkit.utils.asr import SHAPE_RANGE, fit_eeg_distribution, yulewalk, yulewalk_filter
1112
from meegkit.utils.matrix import sliding_window
1213

1314
# Data files
@@ -258,6 +259,44 @@ def test_asr_class(method, reref, show=False):
258259
ASR(sfreq=125)
259260
ASR(Sfreq=150)
260261

262+
def test_clean_windows_uses_full_shape_range():
263+
"""clean_windows passes the full 13-point SHAPE_RANGE (incl beta=3.5) down.
264+
265+
Spies on the shape_range argument to fit_eeg_distribution (6th positional).
266+
"""
267+
raw = np.load(os.path.join(THIS_FOLDER, "data", "eeg_raw.npy"))
268+
sfreq = 250
269+
270+
with patch(
271+
"meegkit.asr.fit_eeg_distribution", side_effect=fit_eeg_distribution
272+
) as mock_fit:
273+
clean_windows(raw, sfreq)
274+
275+
assert mock_fit.called
276+
captured = mock_fit.call_args.args[5]
277+
assert len(captured) == 13
278+
assert np.isclose(captured[-1], 3.5)
279+
assert np.allclose(captured, SHAPE_RANGE)
280+
281+
282+
def test_fit_eeg_distribution_shape_range_materiality():
283+
"""Dropping the beta=3.5 endpoint changes the selected shape parameter.
284+
285+
A bounded, near-uniform sample drives the grid search to the largest beta.
286+
"""
287+
# Bounded, near-uniform sample: favors the largest available beta.
288+
Y = np.linspace(-1, 1, 2000)
289+
290+
shape_range_trunc = np.arange(1.7, 3.5, 0.15)
291+
292+
beta_full = fit_eeg_distribution(Y, shape_range=SHAPE_RANGE)[3]
293+
beta_trunc = fit_eeg_distribution(Y, shape_range=shape_range_trunc)[3]
294+
295+
assert np.isclose(beta_full, 3.5)
296+
assert beta_trunc <= np.max(shape_range_trunc) + 1e-9
297+
assert beta_full > beta_trunc
298+
299+
261300
if __name__ == "__main__":
262301
pytest.main([__file__])
263302
# test_yulewalk(250, True)

0 commit comments

Comments
 (0)