Implement centralized Parity Utility Library for MATLAB math logic - #243
Conversation
This commit creates a new parity math utility module at src/eegprep/functions/miscfunc/parity.py, moving round_mat, rand_sample, and rand_permutation from local script scopes, and providing a parity_accumulate_float32 function to replace manual matrix multiplication logic in reref.py. The resample_raw and upfirdn_raw functions from pop_resample.py are also extracted to the parity library for centralization.
|
🤖 Closing this PR. The new |
|
I have reverted the commit that introduced |
Context & Rationale
Currently, developers manually implement MATLAB-specific mathematical logic across multiple signal processing functions. This has led to code duplication, high maintenance overhead, and subtle numerical drift because Python's default behaviors (e.g., rounding and float accumulation) differ from MATLAB's.
This PR introduces a central "source of truth" for parity-critical operations. By centralizing these primitives, we eliminate the need for one-off memory fixes and custom rounding logic in individual modules, ensuring bit-identical results to legacy MATLAB outputs across the entire codebase.
Key Changes
1. Core Parity Module
Created
src/eegprep/functions/miscfunc/parity.pyto house all MATLAB-compatible mathematical functions. This keeps the logic isolated and easily testable.2. Centralized Math Utilities
round_mat(rounding away from zero),rand_sample, andrand_permutationfrom local modules (misc.pyandransac.py) to the parity library.5489and Fisher-Yates sampling to maintain deterministic alignment with MATLAB's generator.3. Signal Processing Consolidation
upfirdn_rawandresample_rawfrom the resampling module into the parity library.4. Precision Accumulation
parity_accumulate_float32to handle column-major accumulation.reref.py. It ensures that average reference calculations match MATLAB’s float32 precision limits and summation order without requiring boilerplate code in every signal module.Impact & Success Metrics
reref.pyandpop_resample.py.Testing Performed
uv run pytestto verify that the refactored imports and consolidated logic do not introduce regression.ruffformatting for linting compliance.