You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
compute_frac_peak_latency onset is the first crossing in the window, so an earlier bump captures it #14556
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
importnumpyasnpfromnumpy.testingimportassert_allclosefrommneimportEvokedArray, create_infofrommne.stats.erpimportcompute_frac_peak_latencydef_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")
returnEvokedArray(data, info, tmin=times[0])
deftest_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 peaktimes=evoked.timesdata=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.
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.
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?
@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?
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.
@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.
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.
Activity
scott-huberty commentedon Nov 17, 2026
@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?evadiva3 commentedon Nov 17, 2026
@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?
divya-git03 commentedon Nov 19, 2026
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.
evadiva3 commentedon Nov 20, 2026
@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.
divya-git03 commentedon Nov 20, 2026
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.