Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Cerrado
#1,283 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

@cpruijsen ya está trabajando en esto.

Desde el 9/10/2026.

  • #1288 de @cpruijsen — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
data

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
1.7k
Forks
803
Merge medio
5 d 16 h
PR fusionados (30 d)
58

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de sunlabuiuc/PyHealth

Todos los issues de sunlabuiuc/PyHealth

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.