Skip to content

[Parity] Add time-frequency internals - #143

Merged
suraj-ranganath merged 3 commits into
feature/eeglab-core-parity-completionfrom
phase/4-timefreq-internals
Jun 5, 2026
Merged

suraj-ranganath merged 3 commits into
feature/eeglab-core-parity-completionfrom
phase/4-timefreq-internals

Conversation

@suraj-ranganath

@suraj-ranganath suraj-ranganath commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Implement Phase 4 time-frequency internals, legacy timef/crossf wrappers, pop_timef/pop_crossf entry points, and supported pop_newtimef/pop_newcrossf option paths. Adds focused compute, history, helper, docs, matrix, and visual parity evidence while deferring timewarp and exact rsfit parity to Phase 7.

Closes #135
Part of #131

@claude

claude Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 7m 0s —— View job


Code review

  • Overall assessment: Needs changes — core time-frequency math is solid, but there's a blocking packaging regression and a checked-in artifact to remove.
  • Highest-risk area: src/eegprep/__init__.py lazy exports.
  • Merge recommendation: Needs changes.

I reviewed the core compute paths (timefreq, dftfilt/dftfilt2/dftfilt3, newtimef, newcrossf), the stats/baseline helpers (bootstat, correct_mc, newtimefbaseln, newtimeftrialbaseln, newtimefitc, newtimefpowerunit), the pop_*/legacy wrappers and exports, and the tests/docs. The numerically load-bearing helpers (ITC formulas, divisive vs basenorm baseline, baseline indexing, dB conversion, bootstrap tail thresholds) are correct and several are pinned by the MATLAB parity test. The issues below are around packaging, a committed artifact, and test/doc coverage — not the DSP math.

Blocking

  • src/eegprep/__init__.py:59 (and ~13 more) — lazy exports to modules that don't exist on this branch. Alongside the time-frequency exports, this PR adds chancenter, convertlocs, floatread, floatwrite, readlocs/readelp/readeetraklocs, readegilocs, snapread, writelocs, pop_chancenter, pop_chancoresp, pop_loadbci, pop_readlocs, pop_snapread, pop_writelocs. I verified none of these target files exist under functions/sigprocfunc/ or functions/popfunc/. They're lazy so import eegprep succeeds, but any access — including from eegprep import * — raises ModuleNotFoundError at __init__.py:306, and they're in __all__. tests/test_package_exports.py only spot-checks names, so CI won't catch it. These are also out of scope for a time-frequency PR (likely an accidental paste). Fix: remove them, keeping only the time-frequency exports this PR actually lands. Fix this →

  • .notes/implementation-notes.html — committed against the repo's .gitignore policy. .gitignore ignores .notes/* and allow-lists only .gitkeep and eeglab-migration-gap-audit.md; this internal implementation log was force-added. It belongs in the PR description, not the tree. Fix: git rm it from the PR.

Important

  • Public exports with no caller and no test — bootstat / bootstrap_threshold, correct_mc, legacy dftfilt. newtimef.py:11 and newcrossf.py:12 import only exact_p_values from bootstat; the bootstat() resampling and bootstrap_threshold() tail-averaging are never exercised. correct_mc and the legacy dftfilt likewise have no src/ caller and no test. The parity matrix's claim that bootstat is "Covered by seeded newtimef/newcrossf bootstrap tests" is therefore inaccurate. Add direct tests (see Test gaps) or trim the unused surface.

Nits

  • tests/test_phase4_timefreq_statistics.py:105-107 — the rewritten test_newcrossf_single_trial_switches_to_cross_spectrum replaced a real numerical assertion with assert_allclose(coherence, abs(allcoher[:,:,0])), an identity true by construction. The test no longer verifies the "switch to cross-spectrum" behavior it's named for. (Inline comment posted.)
  • dftfilt/dftfilt2/dftfilt3 are exported but absent from docs/source/api/signal_processing.rst, which only added the timefreq/newtimef* family. Minor doc-completeness gap.
  • newtimefpowerunit.py:19,25 uses "uV^{2}/Hz" where EEGLAB uses "\muV^{2}/Hz". The test pins the u spelling, so it's a deliberate cosmetic divergence — flagging only for awareness.
  • dftfilt2.py:33 (low confidence) — np.linspace(winlenint/2, -winlenint/2, winlenint + 1) yields a wavelet of length winlenint+1; EEGLAB dftfilt2.m uses length winlength. dftfilt2 isn't the default (dftfilt3 is) and isn't in the parity test, so this only matters for exact wletmethod='dftfilt2' parity. Worth a check against the reference.

Test gaps

  • bootstat / bootstrap_threshold: call bootstat([a, b], statistic=..., naccu=50, boottype="shuffle", rng=0) on a small deterministic array; assert surrogate count and that bootside="upper" returns a single tail vs "both" returning stacked [lower, upper]; add a boottype="rand" complex-input case asserting magnitude is preserved.
  • correct_mc: on a tiny epoched EEG dict (2 ch, ~64 pnts, 4 trials, srate 128), assert pvalues.shape == (ceil(log2(freqrange[1])), len(timesout)) and ncorrect is an int.
  • legacy dftfilt: assert output shape/dtype for a normal call and that the no-column branch returns an empty (length, 0) array.

EEGLAB parity notes

  • ITC (phasecoher/phasecoher2/coher), divisive vs basenorm baseline (ddof=1 matches MATLAB std), trial-then-average baseline ordering, baseline index selection (times < base scalar; >=/<= range), 10*log10 dB, and bootstrap tail-mean thresholds all match EEGLAB and, for newtimefbaseln/timefreq/dftfilt3, are pinned by the MATLAB parity test. No parity defects found in the math.
  • correct_mc is an acknowledged standalone neighbor-correlation approximation (not EEGLAB's rsfit/curve-fit), documented as Phase 7 follow-up — fine as scoped, just untested.
    · branch phase/4-timefreq-internals

@suraj-ranganath

Copy link
Copy Markdown
Member Author

🤖 Phase 4 GUI visual parity captures for the time-frequency dialogs.

These were captured on macOS with MATLAB R2026a and the EEGPrep Qt renderer. The comparison reports show size mismatches from native MATLAB/Qt window sizing, so the side-by-sides were inspected for control labels, ordering, defaults, and enabled states. pop_newtimef was corrected so tf cycle calc is enabled like EEGLAB.

One caveat: the pop_newcrossf channel EEGLAB capture shows n m in the first channel field even though EEGLAB source defaults that field via getkeyval(..., '1') when invoked as pop_newcrossf(EEG, 1). EEGPrep keeps the documented numeric default.

pop_newtimef channels

pop_newtimef channels

pop_newtimef components

pop_newtimef components

pop_newcrossf channels

pop_newcrossf channels

pop_newcrossf components

pop_newcrossf components

Comment thread src/eegprep/__init__.py Outdated
"build_safe_update_commands": ("eegprep.extension_catalog", "build_safe_update_commands"),
"bundled_plugins": ("eegprep.functions.adminfunc.plugin_menu", "bundled_plugins"),
"cart2topo": ("eegprep.functions.sigprocfunc.cart2topo", "cart2topo"),
"chancenter": ("eegprep.functions.sigprocfunc.chancenter", "chancenter"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking — orphaned lazy exports point to modules that don't exist on this branch.

This PR adds ~14 exports unrelated to time-frequency whose target modules/attributes are absent on HEAD: chancenter, convertlocs, floatread, floatwrite, readlocs/readelp/readeetraklocs, readegilocs, snapread, writelocs, and pop_chancenter, pop_chancoresp, pop_loadbci, pop_readlocs, pop_snapread, pop_writelocs. I verified none of these files exist under functions/sigprocfunc/ or functions/popfunc/.

Because they're lazy, import eegprep still succeeds, but any access (eegprep.readlocs, from eegprep import *, etc.) hits importlib.import_module(...) at __init__.py:306 and raises ModuleNotFoundError. They're also in __all__. tests/test_package_exports.py only checks representative names, so CI won't catch this. These also appear out of scope for a "time-frequency internals" PR — likely an accidental paste.

Fix: drop these entries, keeping only the time-frequency exports whose modules this PR actually adds (bootstat, correct_mc, crossf, dftfilt, dftfilt2, dftfilt3, newtimefbaseln, newtimefitc, newtimefpowerunit, newtimeftrialbaseln, timef, timefreq).

Comment on lines +105 to +107
assert result.coherence.shape == result.phase.shape
assert result.allcoher.shape[2] == 1
np.testing.assert_allclose(result.coherence, np.abs(result.allcoher[:, :, 0]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit — this rewrite makes the test near-tautological and drops the behavior it claims to check.

The old assertion np.nanmean(result.coherence) < 0.5 made a real numerical claim about single-trial cross-spectrum behavior. The replacement's assert_allclose(result.coherence, np.abs(result.allcoher[:, :, 0])) is an identity that holds by construction regardless of correctness, so the test named ..._switches_to_cross_spectrum no longer verifies the switch. Keep the shape checks, but restore a numerical assertion that distinguishes single-trial cross-spectrum output from the multi-trial coherence path.

@suraj-ranganath suraj-ranganath changed the title Phase 4: Time-frequency internals [Parity] Add time-frequency internals Jun 5, 2026
@suraj-ranganath
suraj-ranganath merged commit e01fcc0 into feature/eeglab-core-parity-completion Jun 5, 2026
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.

1 participant