Skip to content

Commit d3e2b1f

Browse files
committed
[FIX] ASR: init clean_windows rejection masks before one-sided zthresholds
mask1 and mask2 in clean_windows were only assigned inside their respective `if np.max(zthresholds) > 0` / `if np.min(zthresholds) < 0` branches, but combined unconditionally via `np.logical_or.reduce((mask1, mask2, mask3))`. Since zthresholds is a documented, user-facing parameter, passing a one-sided value (e.g. [1, 2] or [-5, -1]) skips one branch and raises UnboundLocalError. The default [-3.5, 5] straddles 0 so both branches run, keeping the bug latent. Fix: initialize both masks to all-False (matching the EEGLAB/BCILAB reference behavior of flagging no windows by default) before the conditionals, so each criterion that isn't evaluated simply rejects no windows instead of being undefined. Added a regression test exercising clean_windows with both one-sided directions of zthresholds on the eeg_raw.npy fixture.
1 parent 3721224 commit d3e2b1f

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎meegkit/asr.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def clean_windows(X, sfreq, max_bad_chans=0.2, zthresholds=[-3.5, 5],
353353
swz = np.sort(wz, axis=0)
354354

355355
# determine which windows to remove
356+
mask1 = np.zeros(len(offsets), dtype=bool)
357+
mask2 = np.zeros(len(offsets), dtype=bool)
356358
if np.max(zthresholds) > 0:
357359
mask1 = swz[-(int(max_bad_chans) + 1), :] > np.max(zthresholds)
358360
if np.min(zthresholds) < 0:

‎tests/test_asr.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,29 @@ def test_asr_functions(show=False, method="riemann"):
171171
plt.show()
172172

173173

174+
@pytest.mark.parametrize(argnames="zthresholds", argvalues=([1, 2], [-5, -1]))
175+
def test_clean_windows_one_sided_zthresholds(zthresholds):
176+
"""Test clean_windows with a one-sided zthresholds.
177+
178+
Regression test: when zthresholds does not straddle 0 (i.e. both bounds
179+
are positive, or both are negative), only one of the two rejection-mask
180+
branches in clean_windows runs. The other mask must still be defined
181+
(as an all-False "reject nothing" default) so that combining the masks
182+
does not raise UnboundLocalError.
183+
"""
184+
raw = np.load(os.path.join(THIS_FOLDER, "data", "eeg_raw.npy"))
185+
sfreq = 250
186+
# Use a short slice for speed; still exercises the mask logic.
187+
X = raw[:, :10 * sfreq]
188+
189+
clean, sample_mask = clean_windows(X, sfreq, zthresholds=zthresholds)
190+
191+
assert clean.shape[0] == X.shape[0]
192+
assert clean.shape[1] <= X.shape[1]
193+
assert sample_mask.shape == (1, X.shape[1])
194+
assert sample_mask.dtype == bool
195+
196+
174197
@pytest.mark.parametrize(argnames="method", argvalues=("riemann", "euclid"))
175198
@pytest.mark.parametrize(argnames="reref", argvalues=(False, True))
176199
def test_asr_class(method, reref, show=False):

0 commit comments

Comments
 (0)