Skip to content

Commit a4487f2

Browse files
authored
[FIX] ASR: init clean_windows rejection masks before one-sided zthresholds (#113)
In `clean_windows`, `mask1` and `mask2` 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. Initialize both masks to all-`False` before the conditionals, so a criterion that isn't evaluated simply rejects no windows. ### Testing Added a regression test exercising `clean_windows` with both one-sided directions of `zthresholds`. Full `tests/test_asr.py` passes; `ruff` clean.
1 parent 6b107c9 commit a4487f2

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
@@ -361,6 +361,8 @@ def clean_windows(X, sfreq, max_bad_chans=0.2, zthresholds=[-3.5, 5],
361361
swz = np.sort(wz, axis=0)
362362

363363
# determine which windows to remove
364+
mask1 = np.zeros(len(offsets), dtype=bool)
365+
mask2 = np.zeros(len(offsets), dtype=bool)
364366
if np.max(zthresholds) > 0:
365367
mask1 = swz[-(int(max_bad_chans) + 1), :] > np.max(zthresholds)
366368
if np.min(zthresholds) < 0:

‎tests/test_asr.py‎

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

191191

192+
@pytest.mark.parametrize(argnames="zthresholds", argvalues=([1, 2], [-5, -1]))
193+
def test_clean_windows_one_sided_zthresholds(zthresholds):
194+
"""Test clean_windows with a one-sided zthresholds.
195+
196+
Regression test: when zthresholds does not straddle 0 (i.e. both bounds
197+
are positive, or both are negative), only one of the two rejection-mask
198+
branches in clean_windows runs. The other mask must still be defined
199+
(as an all-False "reject nothing" default) so that combining the masks
200+
does not raise UnboundLocalError.
201+
"""
202+
raw = np.load(os.path.join(THIS_FOLDER, "data", "eeg_raw.npy"))
203+
sfreq = 250
204+
# Use a short slice for speed; still exercises the mask logic.
205+
X = raw[:, :10 * sfreq]
206+
207+
clean, sample_mask = clean_windows(X, sfreq, zthresholds=zthresholds)
208+
209+
assert clean.shape[0] == X.shape[0]
210+
assert clean.shape[1] <= X.shape[1]
211+
assert sample_mask.shape == (1, X.shape[1])
212+
assert sample_mask.dtype == bool
213+
214+
192215
@pytest.mark.parametrize(argnames="method", argvalues=("riemann", "euclid"))
193216
@pytest.mark.parametrize(argnames="reref", argvalues=(False, True))
194217
def test_asr_class(method, reref, show=False):

0 commit comments

Comments
 (0)