Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

`find_local_peaks` ignores the sample at `i + radius`, so points on a rising slope are returned as peaks

オープン 初心者向け
#572 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
84/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
numpy, python
領域
data

調査の方向性

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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MIT-LCP/wfdb-python のほかの issue

MIT-LCP/wfdb-python の issue をすべて見る

似ている issue

Data Engineering の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。