Bug: SHHS sleep staging can raise IndexError when signal epochs exceed available labels Copy title
Los mantenedores suelen responder en 3 días
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 25/100
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
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de sunlabuiuc/PyHealth
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
sunlabuiuc/PyHealth#1346 ·
Los mantenedores suelen responder en 3 días
-
Dificultad 4/5 3-5 días Aptitud para principiantes 30/100
sunlabuiuc/PyHealth#1297 ·
Los mantenedores suelen responder en 3 días
-
Dificultad 4/5 3-5 días Aptitud para principiantes 22/100
sunlabuiuc/PyHealth#1295 · 1 comentario ·
Los mantenedores suelen responder en 3 días
-
Dificultad 3/5 1-2 días Aptitud para principiantes 42/100
sunlabuiuc/PyHealth#1294 · 1 comentario ·
Los mantenedores suelen responder en 3 días
-
Internal transfers are counted as readmissionsPosiblemente ocupada @thebean27 la tomó hace 22 días. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
sunlabuiuc/PyHealth#1269 · 3 comentarios ·
Los mantenedores suelen responder en 3 días
Todos los issues de sunlabuiuc/PyHealth
Issues similares
-
app: scripts status: backlog type: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
netbox-community/netbox-scripts#68 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 91/100
marin-community/marin#10765 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 70/100
flathub/com.unity.UnityHub#230 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
Claiming namespace `lang2hong`Abiertonamespace operations
Dificultad 1/5 Menos de una hora Aptitud para principiantes 63/100
EclipseFdn/open-vsx.org#14910 ·
Los mantenedores suelen responder en 1 día