`find_local_peaks` ignores the sample at `i + radius`, so points on a rising slope are returned as peaks
還沒有人認領這個 Issue。
評估
研究方向
從 wfdb/processing/peaks.py 中的 find_local_peaks 開始,檢查其迴圈中的視窗切片。使用提供的重現來驗證每個半徑都會考慮右側邊界,然後檢查 qrs.py 中 XQRS 呼叫點的相容性。在不更改獨立的 flat-top TODO 的情況下,輸出與參考案例相符即表示完成。
由索引模型根據 Issue 內容生成。
描述
The docstring of wfdb.processing.find_local_peaks says a sample is a local peak "if it is the largest value within the samples on its left and right". The window it actually checks leaves out the right side:
# wfdb/processing/peaks.py
while i < radius + 1:
if sig[i] == max(sig[: i + radius]): # right side stops at i + radius - 1
...
while i < len(sig):
if sig[i] == max(sig[i - radius : i + radius]): # sample i + radius is never looked at
Python slices exclude the end, so sig[i + radius] is never compared. With radius=1 the right neighbour is not checked at all, so every sample on a rising edge counts as a peak. With bigger radii a sample still wins if the only larger value is exactly radius samples to its right.
Reproduction
import numpy as np
from wfdb.processing import find_local_peaks
def reference(sig, radius):
# what the docstring describes
out = []
for i in range(len(sig)):
lo, hi = max(0, i - radius), min(len(sig), i + radius + 1)
if sig[i] == sig[lo:hi].max():
out.append(i)
return out
for sig, r in [
(np.array([0, 1, 2, 3, 2, 1, 0], dtype=float), 1),
(np.array([0, 1, 3, 2, 10, 5, 1, 0], dtype=float), 2),
]:
print(sig.astype(int).tolist(), "radius", r)
print(" find_local_peaks:", find_local_peaks(sig, r).tolist())
print(" expected :", reference(sig, r))
Output:
[0, 1, 2, 3, 2, 1, 0] radius 1
find_local_peaks: [0, 1, 2, 3]
expected : [3]
[0, 1, 3, 2, 10, 5, 1, 0] radius 2
find_local_peaks: [2, 4]
expected : [4]
In the first case indices 0, 1 and 2 are all on the way up to the real maximum at index 3. In the second case index 2 (value 3) is reported even though index 4 (value 10) is only 2 samples away.
Why it matters
XQRS uses this function to pick QRS candidates (qrs.py, lines 236 and 283), so a candidate can end up on the slope next to the real peak, not on the peak itself. With real ECG sampling rates the radius is large, so this probably shows up rarely there, but anyone calling find_local_peaks directly with a small radius gets wrong results.
Possible fix
Include the right edge in both slices (sig[: i + radius + 1] and sig[i - radius : i + radius + 1]). The third loop is unreachable (already reported in #474), so it can go away in the same change. I have not touched the flat-top case that the TODO comment mentions; that looks like a separate question.
Versions
- wfdb 4.3.1 from PyPI, and also current
main(f627b5f), same output - numpy 2.5.3, Python 3.12.13, macOS 26.4.1
- 主要語言
- Jupyter Notebook
- 星號
- 853
- 分支
- 322
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
MIT-LCP/wfdb-python 的其他 Issue
-
難度 4/5 3-5 天 新手友好度 45/100
MIT-LCP/wfdb-python#568 ·
-
難度 3/5 1-2 天 新手友好度 48/100
MIT-LCP/wfdb-python#557 ·
-
難度 2/5 1-3 小時 新手友好度 58/100
MIT-LCP/wfdb-python#554 ·
-
難度 4/5 3-5 天 新手友好度 35/100
MIT-LCP/wfdb-python#545 ·
-
難度 5/5 一週以上 新手友好度 30/100
MIT-LCP/wfdb-python#540 ·
查看 MIT-LCP/wfdb-python 的全部 Issue
相似的 Issue
-
daily-issues
難度 2/5 1-3 小時 新手友好度 68/100
opensanctions/opensanctions#5766 ·
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 90/100
NOAA-EMC/EMC_verif-global#296 ·
-
metadata submission
難度 2/5 1-3 小時 新手友好度 82/100
-
bug
難度 2/5 1-3 小時 新手友好度 90/100
recommenders-team/recommenders#2386 ·
維護者通常 2 天內回覆
-
check:failed feeds:add
難度 1/5 1 小時以內 新手友好度 85/100
iptv-org/database#36228 · 1 則留言 ·
維護者通常 2 天內回覆