Skip to content

[FIX] ASR: round half away from zero for window/bad-chan counts#116

Open
sappelhoff wants to merge 1 commit into
nbara:masterfrom
sappelhoff:fix/asr-round-half-away
Open

[FIX] ASR: round half away from zero for window/bad-chan counts#116
sappelhoff wants to merge 1 commit into
nbara:masterfrom
sappelhoff:fix/asr-round-half-away

Conversation

@sappelhoff

Copy link
Copy Markdown
Collaborator

numpy's np.round (banker's rounding, round-half-to-even) and Python's 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.

Reachability is narrow (ties need specific channel counts or odd-integer sfreq with win_len=0.5; common rates never trigger), so this mainly hardens the edge cases.

Testing

Added a helper unit test covering ties (2.5→3, 128.5→129, -2.5→-3), divergence from np.round, array input, and int() exactness. Full tests/test_asr.py passes; ruff clean.

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.12%. Comparing base (107cd1b) to head (f496f9d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #116      +/-   ##
==========================================
+ Coverage   83.11%   83.12%   +0.01%     
==========================================
  Files          25       25              
  Lines        2825     2827       +2     
==========================================
+ Hits         2348     2350       +2     
  Misses        477      477              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sappelhoff

Copy link
Copy Markdown
Collaborator Author

Note: includes the nonlinear_eigenspace reproducibility fix from #118. test_asr_class[*-riemann] compares incremental-vs-bulk output with a loose tolerance and is flaky on CI because that path used an unseeded RNG; this can trip on any PR regardless of what it changes. #118 is the root fix — once it merges first, this commit drops out on rebase.

@sappelhoff sappelhoff mentioned this pull request Jul 14, 2026
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.
@sappelhoff
sappelhoff force-pushed the fix/asr-round-half-away branch from b89951f to f496f9d Compare July 19, 2026 09:10
@sappelhoff

Copy link
Copy Markdown
Collaborator Author

This one is probably a bit unnecessary, unless "matlab parity" is a goal, which I don't see why it should be.

@nbara

nbara commented Jul 20, 2026

Copy link
Copy Markdown
Owner

It was at the beginning as I tried to get identical numerical results vs the reference implementation (Matlab). But I agree now it's not such a problem anymore

@sappelhoff

Copy link
Copy Markdown
Collaborator Author

I will leave it up to you to merge or close :) I think both would be fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants