Skip to content

Commit 60e1614

Browse files
committed
[FIX] ASR: round half away from zero for window/bad-chan counts
numpy's np.round (banker's rounding) and int() truncation diverge from MATLAB's round-half-away-from-zero at exact-half inputs, giving +-1 counts for the bad-channel count and window length N in clean_windows and asr_calibrate. Add a _round_half_away helper and apply it at those sites.
1 parent 3721224 commit 60e1614

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎meegkit/asr.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ def transform(self, X, y=None, **kwargs):
259259
return out
260260

261261

262+
def _round_half_away(x):
263+
"""Round half away from zero (MATLAB `round` semantics).
264+
265+
numpy's ``np.round`` rounds half to even (banker's rounding); this
266+
matches MATLAB/Octave which round halves away from zero.
267+
"""
268+
return np.floor(np.abs(x) + 0.5) * np.sign(x)
269+
270+
262271
def clean_windows(X, sfreq, max_bad_chans=0.2, zthresholds=[-3.5, 5],
263272
win_len=.5, win_overlap=0.66, min_clean_fraction=0.25,
264273
max_dropout_fraction=0.1, show=False):
@@ -323,11 +332,11 @@ def clean_windows(X, sfreq, max_bad_chans=0.2, zthresholds=[-3.5, 5],
323332
truncate_quant = [0.0220, 0.6000]
324333
step_sizes = [0.01, 0.01]
325334
shape_range = np.arange(1.7, 3.5, 0.15)
326-
max_bad_chans = np.round(X.shape[0] * max_bad_chans)
335+
max_bad_chans = _round_half_away(X.shape[0] * max_bad_chans)
327336

328337
# set data indices
329338
[nc, ns] = X.shape
330-
N = int(win_len * sfreq)
339+
N = int(_round_half_away(win_len * sfreq))
331340
offsets = np.round(np.arange(0, ns - N, (N * (1 - win_overlap))))
332341
offsets = offsets.astype(int)
333342
logging.debug("[ASR] Determining channel-wise rejection thresholds")
@@ -481,7 +490,7 @@ def asr_calibrate(X, sfreq, cutoff=5, blocksize=100, win_len=0.5,
481490
X, _zf = yulewalk_filter(X, sfreq, ab=None)
482491

483492
# window length for calculating thresholds
484-
N = int(np.round(win_len * sfreq))
493+
N = int(_round_half_away(win_len * sfreq))
485494

486495
U = block_covariance(X, window=blocksize, overlap=win_overlap,
487496
estimator=estimator)

‎tests/test_asr.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
from scipy import signal
88

9-
from meegkit.asr import ASR, asr_calibrate, asr_process, clean_windows
9+
from meegkit.asr import ASR, _round_half_away, asr_calibrate, asr_process, clean_windows
1010
from meegkit.utils.asr import yulewalk, yulewalk_filter
1111
from meegkit.utils.matrix import sliding_window
1212

@@ -122,6 +122,34 @@ def test_yulewalk_filter(n_chans, show=False):
122122
plt.show()
123123

124124

125+
def test_round_half_away():
126+
"""_round_half_away rounds ties away from zero, unlike numpy's banker's rounding."""
127+
# exact-half (tie) behavior: rounds away from zero
128+
assert _round_half_away(2.5) == 3
129+
assert _round_half_away(128.5) == 129
130+
assert _round_half_away(-2.5) == -3
131+
assert _round_half_away(0.5) == 1
132+
assert _round_half_away(1.5) == 2 # np.round gives 2 here too (even)
133+
134+
# divergence from banker's rounding on ties
135+
assert _round_half_away(2.5) != np.round(2.5) # 3 != 2
136+
assert _round_half_away(128.5) != np.round(128.5) # 129 != 128
137+
138+
# non-tie values round normally
139+
assert _round_half_away(2.4) == 2
140+
assert _round_half_away(2.6) == 3
141+
assert _round_half_away(-2.6) == -3
142+
143+
# int() of the result is exact for scalars
144+
assert int(_round_half_away(2.5)) == 3
145+
assert int(_round_half_away(-2.5)) == -3
146+
147+
# array input
148+
ties = np.array([2.5, 128.5, -2.5, 0.5, 1.5])
149+
expected = np.array([3, 129, -3, 1, 2])
150+
np.testing.assert_array_equal(_round_half_away(ties), expected)
151+
152+
125153
def test_asr_functions(show=False, method="riemann"):
126154
"""Test ASR functions (offline use).
127155

0 commit comments

Comments
 (0)