spindle phases (N2/N3), README, registry test for the full module set

- spindle_detection (legacy and modern): phase='N2'|'N3' searches only the
  sleep windows of that stage (husos --fase); requires slow_wave_staging then.
- README: modules, requirements, use, tests and cluster verification.
- test_pipeline: both implementations register the same 5 processes and 9
  biomarkers.
This commit is contained in:
AlbertoPMDM
2026-11-18 22:12:02 -06:00
parent ef2f3dbf39
commit 5da1921c69
5 changed files with 125 additions and 10 deletions
@@ -54,8 +54,20 @@ class LegacySpindleDetection:
requires = ("sleep_state",)
impl = "legacy"
def __init__(self, variant: str = "hp05_prom"):
def __init__(self, variant: str = "hp05_prom", phase: str | None = None):
"""`phase` 'N2' or 'N3': search only the sleep windows of that stage (husos --fase,
the CLI route with N2/N3 on); the thresholds then come from that stage alone."""
if phase not in (None, "N2", "N3"):
raise ValueError(f"phase must be None, 'N2' or 'N3', not {phase!r}")
self.variant = variant
self.phase = phase
self.requires = ("sleep_state", "slow_wave_staging") if phase else ("sleep_state",)
def _sleep(self, study, lab):
if not self.phase:
return [e == "sueno" for e in lab]
st = study.label("slow_wave_staging").per_window
return [e == "sueno" and f == self.phase for e, f in zip(lab, st)]
def train(self, studies):
return self
@@ -65,7 +77,7 @@ class LegacySpindleDetection:
from ._director import ritmo as r
lab, w = study.label("sleep_state").per_window, study.prep.windows
sueno = [float(s) for s, e in zip(w.start_s, lab) if e == "sueno"]
sueno = [float(s) for s, ok in zip(w.start_s, self._sleep(study, lab)) if ok]
vigilia = [float(s) for s, e in zip(w.start_s, lab) if e == "vigilia"]
with _adapter.estudio(study.prep, self.variant, eeg_io) as est:
rh = wake_rhythm(est, vigilia, r, eeg_io)
@@ -80,7 +92,7 @@ class LegacySpindleDetection:
return Labels(self.name, events=out.reset_index(drop=True))
def save(self, path):
Path(path, "params.json").write_text(json.dumps({"variant": self.variant}))
Path(path, "params.json").write_text(json.dumps({"variant": self.variant, "phase": self.phase}))
@classmethod
def load(cls, path):
@@ -140,8 +140,20 @@ class SpindleDetection:
requires = ("sleep_state",)
impl = "modern"
def __init__(self, variant: str = "hp05_prom"):
def __init__(self, variant: str = "hp05_prom", phase: str | None = None):
"""`phase` 'N2' or 'N3': search only the sleep windows of that stage (husos --fase,
the CLI route with N2/N3 on); the thresholds then come from that stage alone."""
if phase not in (None, "N2", "N3"):
raise ValueError(f"phase must be None, 'N2' or 'N3', not {phase!r}")
self.variant = variant
self.phase = phase
self.requires = ("sleep_state", "slow_wave_staging") if phase else ("sleep_state",)
def _sleep(self, study, lab):
if not self.phase:
return [e == "sueno" for e in lab]
st = study.label("slow_wave_staging").per_window
return [e == "sueno" and f == self.phase for e, f in zip(lab, st)]
def train(self, studies):
return self
@@ -154,8 +166,8 @@ class SpindleDetection:
x = raw.get_data(picks=names).T * 1e6
asleep = np.zeros(len(x), bool)
n = int(4 * fs)
for s, e in zip(w.start_s, lab):
if e == "sueno":
for s, ok in zip(w.start_s, self._sleep(study, lab)):
if ok:
a = int(round(float(s) * fs))
asleep[a:a + n] = True
minutes = float(asleep.sum() / fs / 60)
@@ -166,7 +178,7 @@ class SpindleDetection:
return Labels(self.name, events=ev)
def save(self, path):
Path(path, "params.json").write_text(json.dumps({"variant": self.variant}))
Path(path, "params.json").write_text(json.dumps({"variant": self.variant, "phase": self.phase}))
@classmethod
def load(cls, path):