legacy: annotation_mask, sleep_state and posterior_rhythm on the director's code

- contracts: Preprocessed.quality_spans (QC-unclean stretches at the QC's
  own resolution), so the director's state mask can be rebuilt exactly.
- legacy.preprocessing: annotations straight from the EDF (exact onsets, as
  edf_convert), quality_spans from his 4 s QC.
- legacy processes annotation_mask (intervalos_estado, estado_de_ventana) and
  sleep_state (vigilancia.rasgos with senal._mascara_excluir's mask,
  clasificar with the HK1K cut-offs or cohort cut-offs after train()).
- legacy biomarker posterior_rhythm (medidas_director.ritmo on
  ritmo.psd_de_ventanas + pico_dominante) with a private _baseline.py holding
  curvas.py and perfil_alteraciones' age-block z, copied verbatim.
- Each legacy module owns its adapter, which writes the study in the
  director's preprocessed format and opens it with his eeg_io.Estudio.

Identical to the submodule's cluster route on a synthetic study (window
labels and rhythm); contract suites pass.
This commit is contained in:
AlbertoPMDM
2026-11-18 20:12:47 -06:00
parent 02a885dc10
commit 441113c7b7
36 changed files with 13530 additions and 14 deletions
+19 -13
View File
@@ -115,7 +115,7 @@ class LegacyPreprocessing:
fs = float(arrays["fs_hz"])
cuero = [str(c) for c in arrays["canales"]]
aux = [str(c) for c in arrays.get("canales_aux", np.array([]))]
anns = _annotations(proc.get("eventos", []), fs)
anns = _annotations(edf_path)
variants = {}
for v in plan["variants"]:
etq = f"{float(v['highpass_hz']):g}".replace(".", "")
@@ -142,7 +142,19 @@ class LegacyPreprocessing:
"args": {**args, "pasa_altas": list(args["pasa_altas"])}}}
return Preprocessed(variants=variants, windows=windows, age_months=age_months,
plan=plan_out, plan_hash=str(plan.get("hash", "")),
source_sha256=sha256_of(edf_path))
source_sha256=sha256_of(edf_path),
quality_spans=spans_from_qc(qc.get("_por_ventana", [])))
def spans_from_qc(qc_windows) -> pd.DataFrame:
"""The director's 4 s QC windows that are not clean: veredicto != 'limpia' or in the
set-up ('montaje'), as in biomarcadores senal._mascara_excluir."""
rows = [{"onset_s": float(v["t_ini"]), "duration_s": float(v["t_fin"]) - float(v["t_ini"]),
"reason": "montaje" if v.get("montaje", False) and v.get("veredicto") == "limpia"
else str(v.get("veredicto"))}
for v in qc_windows if v.get("veredicto") != "limpia" or v.get("montaje", False)]
return pd.DataFrame(rows, columns=["onset_s", "duration_s", "reason"]).astype(
{"onset_s": float, "duration_s": float, "reason": str})
def windows_from_qc(qc_windows, duration_s: float, window_s: float, step_s: float) -> pd.DataFrame:
@@ -167,18 +179,12 @@ def windows_from_qc(qc_windows, duration_s: float, window_s: float, step_s: floa
"clean": np.array(clean, bool), "gap": np.array(gap, bool)})
def _annotations(eventos, fs):
def _annotations(edf_path):
"""Every EDF+ annotation with its exact onset and duration, as edf_convert reads them
(the director keeps BAD_ACQ_SKIP among his events too)."""
import mne
on, du, de = [], [], []
for e in eventos:
t = e.get("t_seg")
if t is None:
continue
fin = e.get("stamp_fin")
on.append(float(t))
du.append(max(0.0, fin / fs - float(t)) if fin is not None else 0.0)
de.append(str(e.get("texto") or ""))
return mne.Annotations(on, du, de)
a = mne.read_annotations(edf_path)
return mne.Annotations(list(a.onset), list(a.duration), list(a.description))
MODULE = LegacyPreprocessing()