Skip to content

Commit 37c955d

Browse files
committed
Improve coverage of fetchers, heart and staging files
1 parent 827ec2f commit 37c955d

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/yasa/fetchers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def fetch_sample(fname, version="v1", **kwargs):
156156
try:
157157
fetched = pup.fetch(fname, **kwargs)
158158
break
159-
except Exception:
159+
except Exception: # pragma: no cover
160160
if attempt < 2:
161161
time.sleep(2**attempt)
162162
else:

src/yasa/heart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def hrv_stage(
160160
# Detect R-peaks
161161
try:
162162
pks = detect_heartbeats(data[start:end], fs=sf)
163-
except Exception as e:
163+
except Exception as e: # pragma: no cover
164164
logger.info(f"Heartbeat detection failed for epoch {idx[1]} of stage {idx[0]}: {e}")
165165
continue
166166

tests/test_staging.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""Test the functions in yasa/staging.py."""
22

33
import unittest
4+
from unittest.mock import MagicMock
45

56
import matplotlib.pyplot as plt
67
import mne
78
import numpy as np
9+
import pandas as pd
810

911
from yasa.fetchers import fetch_sample
1012
from yasa.hypno import Hypnogram
@@ -31,6 +33,7 @@ def test_sleep_staging(self):
3133
)
3234
print(sls)
3335
print(str(sls))
36+
assert repr(sls)
3437
sls.get_features()
3538
y_pred = sls.predict()
3639
assert isinstance(y_pred, Hypnogram)
@@ -56,3 +59,40 @@ def test_sleep_staging(self):
5659
SleepStaging(raw, eeg_name="C4", eog_name="EOG1").fit()
5760
# .. just the EEG
5861
SleepStaging(raw, eeg_name="C4").fit()
62+
63+
def test_short_data_warning(self):
64+
"""Test that a warning is raised for recordings shorter than 5 minutes."""
65+
raw_short = raw.copy().crop(tmax=200)
66+
with self.assertLogs("yasa", level="WARNING"):
67+
SleepStaging(raw_short, eeg_name="C4")
68+
69+
def test_validate_predict_errors(self):
70+
"""Test _validate_predict raises ValueError for mismatched features."""
71+
sls = SleepStaging(raw, eeg_name="C4")
72+
sls.fit()
73+
74+
# Features in clf not present in current feature set
75+
clf_mock = MagicMock()
76+
clf_mock.feature_name_ = ["nonexistent_feature"]
77+
with self.assertRaises(ValueError):
78+
sls._validate_predict(clf_mock)
79+
80+
# Features in current set not present in clf
81+
clf_mock.feature_name_ = sls.feature_name_[:-1]
82+
with self.assertRaises(ValueError):
83+
sls._validate_predict(clf_mock)
84+
85+
def test_plot_predict_proba_no_predict(self):
86+
"""Test that plot_predict_proba raises ValueError before predict is called."""
87+
sls = SleepStaging(raw, eeg_name="C4")
88+
with self.assertRaises(ValueError):
89+
sls.plot_predict_proba()
90+
91+
def test_predict_proba_without_prior_predict(self):
92+
"""Test that predict_proba internally calls predict when _proba is not set."""
93+
sls = SleepStaging(raw, eeg_name="C4")
94+
sls.fit()
95+
with self.assertWarns(FutureWarning):
96+
proba = sls.predict_proba()
97+
assert isinstance(proba, pd.DataFrame)
98+
plt.close("all")

0 commit comments

Comments
 (0)