Skip to content

[Timefreq] Add time-warped newtimef helpers - #155

Merged
suraj-ranganath merged 1 commit into
feature/eeglab-core-parity-completionfrom
followup/146-timewarped-newtimef-marker-cycle-gui
Jun 7, 2026
Merged

suraj-ranganath merged 1 commit into
feature/eeglab-core-parity-completionfrom
followup/146-timewarped-newtimef-marker-cycle-gui

Conversation

@suraj-ranganath

Copy link
Copy Markdown
Member

Add standalone timewarp, angtimewarp, and tf_cycle_calc helpers, and thread time-warped marker options through newtimef and pop_newtimef. Wire the pop_newtimef cycle calculator dialog while keeping this stacked under PR #152 as part of epic #131.

Closes #146

@claude

claude Bot commented Jun 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 6m 9s —— View job


Code review

  • Overall assessment: Solid PR. The standalone helpers are well-built and, importantly, guarded by direct MATLAB parity tests. The main gap is that the end-to-end time-warp path through newtimef/timefreq is only shape-checked, not numerically verified.
  • Highest-risk area: _time_stretch_tfdata in timefreq.py (output-frame snapping + magnitude/phase-split warp) — the most intricate new logic and the only part with no numerical parity coverage.
  • Merge recommendation: Needs more context — add an end-to-end timewarp parity assertion before merge.

Blocking

None.

Important

1. End-to-end timewarp/timestretch path is numerically unverified.
src/eegprep/functions/timefreqfunc/timefreq.py — _time_stretch_tfdata / _nearest_output_positions.
The new tests for the timewarp option (test_pop_newtimef_timewarp_options_are_replayable, and the console test) only assert result.tfdata.shape and the vertical timewarp_markers positions — never the warped tfdata/ERSP/ITC values. Yet _time_stretch_tfdata is the riskiest new code: it snaps markers onto the downsampled output grid via _nearest_output_positions, then warps magnitude (M @ |tf|) and phase (angtimewarp) separately and recombines. The building blocks (timewarp, angtimewarp) have 1e-12 MATLAB parity tests, but their composition here does not.
Why it matters: a subtle error in the output-frame mapping or the magnitude/phase recombination would silently produce wrong warped ERSP/ITC and no test would catch it.
Suggested fix: extend test_timefreq_helpers_match_eeglab_deterministic_outputs (or add a matlab-marked test) to compare newtimef(..., 'timewarp', ...) warped output (e.g. ERSP or the warped tfdata) against EEGLAB at the same tolerance you already use for the building blocks.

Nits

2. _nearest_output_positions can yield duplicate positions and crash timewarp().
src/eegprep/functions/timefreqfunc/timefreq.py — _time_stretch_tfdata.
After snapping markers to the nearest output column, the code appends [1, output_frames.size]:

marks_pos = np.sort(np.concatenate([marks_pos, np.asarray([1, output_frames.size], dtype=int)]))
warp_matrix = timewarp(marks_pos, refs_pos)

With coarse timesout or markers near epoch edges, two distinct markers can snap to the same column, or a marker can snap onto the appended boundary 1/size. timewarp() then raises "...must be unique after adding the synchronized first frame". Consider deduping/clamping the snapped positions (or asserting a minimum output resolution) so a legitimate timewarp call can't fail on the grid-snapping step.

3. Dead branch / unused parameter in angtimewarp._wrap_to_pi.
src/eegprep/functions/timefreqfunc/angtimewarp.py:

def _wrap_to_pi(angles: np.ndarray, center: float = 0.0) -> np.ndarray:
    wrapped = np.mod(angles, 2.0 * np.pi)   # always in [0, 2*pi)
    high = wrapped > np.pi - center
    wrapped[high] -= 2.0 * np.pi
    low = wrapped < center - np.pi          # never true for the only caller (center=0)
    wrapped[low] += 2.0 * np.pi
    return wrapped

After np.mod, values are in [0, 2*pi); the only caller uses center=0, so wrapped < center - pi (i.e. < -pi) is never true and the low branch is dead. The center parameter is never passed. Per AGENTS.md (no speculative configurability; remove code your change makes unused), drop center and the dead branch.

Test gaps

Covered above in Important #1 — the only material gap is end-to-end numerical timewarp parity. The standalone-helper parity tests and the replayability/console tests are good.

EEGLAB parity notes

  • timewarp, angtimewarp, and tf_cycle_calc each have direct MATLAB parity assertions (1e-12) — good. I could not independently diff against the vendored EEGLAB sources because src/eegprep/eeglab/ is empty in this checkout, so I'm relying on those tests for the building blocks.
  • The marker→frame conversion round((ms - tlimits(1))/1000*srate)+1 and the in-epoch guard max > frames-2 || min < 3 match EEGLAB's newtimef timewarp handling. Drawing warped reference latencies as magenta vertical lines also matches EEGLAB behavior.
  • Please confirm (via the parity test in refactor for packaging and test distribution #1) that the magnitude/phase-split warp in _time_stretch_tfdata reproduces EEGLAB's timefreq timestretch output — this is the one parity claim currently resting only on the helpers rather than on the composed path.
    · branch followup/146-timewarped-newtimef-marker-cycle-gui

@suraj-ranganath

suraj-ranganath commented Jun 6, 2026 •

Copy link
Copy Markdown
Member Author

🤖 pop_newtimef GUI visual parity

Channels dialog Components dialog
pop_newtimef_channels_side_by_side.png pop_newtimef_components_side_by_side.png

@suraj-ranganath
suraj-ranganath merged commit c529930 into feature/eeglab-core-parity-completion Jun 7, 2026
1 check passed
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