Skip to content

compute_frac_peak_latency onset is the first crossing in the window, so an earlier bump captures it #14556

Open

Description

@evadiva3

Description

compute_frac_peak_latency onset is the first crossing in the window, so an earlier bump captures it; now searched back from the peak like the offset

Reproduction

import numpy as np
from numpy.testing import assert_allclose
from mne import EvokedArray, create_info
from mne.stats.erp import compute_frac_peak_latency

def _triangle_evoked(sfreq=1000.0):
    """Return an Evoked with triangular peaks of exactly known area and latency.

    Each triangle has its apex at 0.3 s and a base spanning 0.2-0.4 s, so the
    area is 0.5 * 0.2 * height, and the half-amplitude points are at 0.25 and
    0.35 s.
    """
    times = np.arange(-0.1, 0.6, 1 / sfreq)
    triangle = np.clip(1 - np.abs(times - 0.3) / 0.1, 0, None)
    data = np.array([triangle * 1e-6, triangle * 2e-6, triangle * -1e-6])
    info = create_info(["ch0", "ch1", "ch2"], sfreq, "eeg")
    return EvokedArray(data, info, tmin=times[0])

def test_compute_frac_peak_latency():
    """Test fractional peak latency against known half-amplitude crossings."""
    evoked = _triangle_evoked()
    latencies = compute_frac_peak_latency(
        evoked, frac=0.5, start=0.2, stop=0.4, mode="pos"
    )
    assert_allclose(latencies["fractional_peak_onset"][0], 0.25, atol=2e-3)
    assert_allclose(latencies["fractional_peak_offset"][0], 0.35, atol=2e-3)

    # an earlier, smaller deflection that exceeds the threshold must not be taken
    # as the onset of the peak: onset and offset are both searched from the peak
    times = evoked.times
    data = evoked.data.copy()
    data[0] += 0.7e-6 * np.clip(1 - np.abs(times - 0.1) / 0.05, 0, None)
    bumpy = EvokedArray(data, evoked.info, tmin=times[0])
    latencies = compute_frac_peak_latency(
        bumpy, frac=0.5, start=0.0, stop=0.4, mode="pos", picks=["ch0"]
    )
    assert_allclose(latencies["fractional_peak_onset"][0], 0.25, atol=2e-3)
    assert_allclose(latencies["fractional_peak_offset"][0], 0.35, atol=2e-3)

test_compute_frac_peak_latency()

Environment

Mac; Python 3.13.9; development checkout 18bdc974cbc20252920d82ac86b1f52861fe1443.

Proposed patch: evadiva3@7aa9875

Discussion before PR: numerical behavior change.

Tests

Regression: fails before the fix, passes after.

5 passed in 0.88s

AI assistance

OpenAI Codex (GPT-6.1 Sol): code review, regression-test preparation, test execution, and submission preparation. The description is copied from my original tracker notes.

Activity

  1. scott-huberty commented on Nov 17, 2026

    @scott-huberty
    Contributor

    compute_frac_peak_latency onset is the first crossing in the window, so an earlier bump captures it; now searched back from the peak like the offset

    @evadiva3 can you remind us of what the current behavior is on main, describe why you think this behavior is a problem, and then explain your proposed fix?

  2. evadiva3 commented on Nov 17, 2026

    @evadiva3
    ContributorAuthor

    @scott-huberty On main, the function uses the largest peak in the given window to calculate the fractional threshold, but reports the first crossing of that threshold anywhere in the window as the onset (the offset is searched forward from the selected peak). So my concern is that when an earlier, smaller bump crosses the threshold, drops below it, and then the signal rises to the main peak, the onset would then come from the earlier bump, while the offset would come from the main peak instead. My proposed fix is to search backward from the selected peak for the onset crossing--that way, both onset and offset would refer to the same peak.

    Is the current first-crossing behavior intentional, or would you expect the onset to be tied to the selected peak too?

  3. divya-git03 commented on Nov 19, 2026

    @divya-git03

    Thanks for raising this issue. I'd be interested in investigating the current implementation and its expected numerical behavior.

    Before proceeding, could you clarify whether the onset is intentionally defined as the first threshold crossing anywhere within the selected window, or whether it should correspond specifically to the selected peak, as the offset does?

    If the latter is the intended behavior, I'd be happy to work on the implementation and add regression tests covering earlier threshold-crossing deflections.

    I'd appreciate your guidance before starting the changes.

  4. evadiva3 commented on Nov 20, 2026

    @evadiva3
    ContributorAuthor

    @divya-git03 The current implementation takes the first onset threshold crossing anywhere in the window; my proposed change is to search backward from the selected peak, so onset and offset refer to that same peak. The earlier comment above explains the two-bump example.

    I have a candidate patch and regression prepared, but whether the current definition is intentional still needs a decision from an MNE maintainer. I'm happy to coordinate once that behavior is clarified.

  5. divya-git03 commented on Nov 20, 2026

    @divya-git03

    Thank you for clarifying! I understand the issue now. Searching backward from the selected peak seems like a reasonable approach to ensure that onset and offset correspond to the same peak. I'll wait for the maintainer's guidance on the intended behavior before proceeding with implementation.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions