Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Bug: SHHS sleep staging can raise IndexError when signal epochs exceed available labels Copy title

已关闭
#1,283 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@cpruijsen 已经在做这个了。

开始于 2026年10月9日。

  • #1288 来自 @cpruijsen —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
data

调研方向

Start in pyhealth/tasks/sleep_staging.py at the epoch loop in sleep_staging_shhs_fn(), and compare it with sleep_staging_sleepedf_fn(), which already caps the range by label count. Pull request #1288 is already open against this issue, so check it before starting. Done means the loop no longer raises IndexError when there are fewer labels than complete epochs, and a regression test covers that case.

由索引模型根据 Issue 内容生成。

描述

Description

In pyhealth/tasks/sleep_staging.py, the sleep_staging_shhs_fn() function iterates over all complete signal epochs without checking whether a corresponding annotation exists.

The current implementation is:

for slice_index in range(X.shape[1] // sample_length):
    epoch_signal = X[
        :, slice_index * sample_length : (slice_index + 1) * sample_length
    ]
    epoch_label = Y[slice_index]

Here, the loop count depends only on the signal length, while Y is populated independently from the XML annotation file.

If the number of complete signal epochs exceeds the number of available labels, accessing Y[slice_index] raises IndexError.

Steps to Reproduce

The following minimal example reproduces the indexing behavior without requiring the SHHS dataset:

import numpy as np

sample_length = 125 * 30

# Three complete signal epochs
X = np.zeros((2, sample_length * 3))

# Only two annotation labels
Y = ["0", "1"]

for slice_index in range(X.shape[1] // sample_length):
    epoch_signal = X[
        :, slice_index * sample_length : (slice_index + 1) * sample_length
    ]
    epoch_label = Y[slice_index]

Observed Behavior

The third iteration attempts to access Y[2] and raises:

IndexError: list index out of range

This reproduces the indexing logic used in sleep_staging_shhs_fn(). An end-to-end failure with a real SHHS recording has not been verified.

Expected Behavior

The function should handle differences between signal duration and annotation coverage without unexpectedly raising an indexing error.

If annotations are shorter than the signal, the function should either:

  • Process only epochs with corresponding labels, or
  • Raise a clear validation error explaining the mismatch.

Suggested Fix

If processing only labeled epochs is the intended behavior, limit the loop to the number of available labels:

num_signal_epochs = X.shape[1] // sample_length
num_labeled_epochs = len(Y)

for slice_index in range(min(num_signal_epochs, num_labeled_epochs)):
    ...

Alternatively, explicitly validate the signal and annotation lengths before processing.

A regression test should cover recordings with fewer annotations than complete signal epochs.

Environment

  • Repository: sunlabuiuc/PyHealth
  • File: pyhealth/tasks/sleep_staging.py
  • Function: sleep_staging_shhs_fn()
  • Affected behavior: Signal epoch and annotation length mismatch
  • Verification: Minimal indexing reproduction; source inspection
  • Full SHHS dataset test: Not performed

Additional Context

The neighboring sleep_staging_sleepedf_fn() already limits its iteration using:

range(min(X.shape[1] // sample_length, len(labels)))

Applying a similar safeguard to the SHHS implementation would provide consistent handling of annotation length mismatches.

主要语言
Python
星标
1.7k
派生
803
平均合并
5 天 20 小时
30 天内合并 PR
56

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

sunlabuiuc/PyHealth 的其他 Issue

查看 sunlabuiuc/PyHealth 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。