`find_local_peaks` ignores the sample at `i + radius`, so points on a rising slope are returned as peaks
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 84/100
Hướng nghiên cứu
Bắt đầu trong wfdb/processing/peaks.py tại find_local_peaks và kiểm tra các lát cắt cửa sổ trong các vòng lặp của nó. Sử dụng bản tái hiện được cung cấp để xác minh rằng biên bên phải được xét đến với mỗi bán kính, sau đó kiểm tra tính tương thích của các vị trí gọi XQRS trong qrs.py. Được coi là hoàn tất khi đầu ra khớp với các trường hợp tham chiếu mà không thay đổi TODO flat-top riêng biệt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Jupyter Notebook
- Star
- 853
- Fork
- 322
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của MIT-LCP/wfdb-python
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
MIT-LCP/wfdb-python#568 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
MIT-LCP/wfdb-python#557 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 58/100
MIT-LCP/wfdb-python#554 ·
-
WFDB path ignoredĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
MIT-LCP/wfdb-python#545 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
MIT-LCP/wfdb-python#540 ·
Tất cả issue của MIT-LCP/wfdb-python
Issue tương tự
-
Fix archive paths on UrsaĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
NOAA-EMC/EMC_verif-global#296 ·
-
New Submission: TropWATERĐang mởmetadata submission
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
check:failed feeds:add
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
iptv-org/database#36228 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[BUG]: Excel files created by the agent turn IP addresses and version numbers into wrong numbersĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Mintplex-Labs/anything-llm#6533 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Add: TVNĐang mởcheck:passed streams:add
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày