118 lines
5.6 KiB
Python
118 lines
5.6 KiB
Python
"""What this module covers: the EEG analysis's settings and its unit of work, `measure`, run on the real `tea`
|
|
library.
|
|
|
|
Fixtures: `measured`, three synthetic studies measured once for the module (see `synth_edf.py`): a clean four-minute
|
|
study, half awake with a 7 Hz posterior rhythm and half asleep with spindles; the same with eight central and
|
|
posterior channels flat; and bytes that are not an EDF.
|
|
"""
|
|
import pytest
|
|
from synth_edf import edf_bytes
|
|
|
|
from core.tea.enums import CriterioEeg, ELECTRODOS_10_20, MetricaEeg
|
|
from models.eeg_analysis import checked_settings
|
|
from models.tea.eeg.analyze import criterion_code, measure, pipeline
|
|
from models.tea.eeg.settings import scenario_of, settings
|
|
|
|
FLAT = ["C3", "C4", "Cz", "P3", "P4", "Pz", "O1", "O2"]
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def measured():
|
|
"""The three studies' measurements, keyed `clean`, `flat` and `broken`; the clean child is 27.5 months old."""
|
|
s = settings()
|
|
return {
|
|
"clean": measure("clean", edf_bytes(minutes=4), 27.5, s),
|
|
"flat": measure("flat", edf_bytes(minutes=4, bad={"flat": FLAT}, seed=1), 27.5, s),
|
|
"broken": measure("broken", b"not an edf", None, s),
|
|
}
|
|
|
|
|
|
def test_every_metric_is_a_feature_of_its_biomarker():
|
|
"""Pins down: each `MetricaEeg` code maps to a feature the library's biomarker actually measures.
|
|
|
|
Fails if `model.toml` names a feature, biomarker or electrode prefix the library doesn't have (a renamed feature
|
|
would otherwise leave the metric null on every EEG), or leaves a `MetricaEeg` code unmapped.
|
|
"""
|
|
s = checked_settings()
|
|
pipe = pipeline(s)
|
|
assert sorted(m.code for m in s.metrics) == sorted(c.value for c in MetricaEeg)
|
|
for m in s.metrics:
|
|
assert m.feature in pipe.module(m.biomarker).features, m.code
|
|
if m.electrodes:
|
|
electrodes = [f.removeprefix(m.electrode_prefix) for f in pipe.module(m.electrodes).features
|
|
if f.startswith(m.electrode_prefix)]
|
|
assert electrodes and set(electrodes) <= set(ELECTRODOS_10_20), m.code
|
|
|
|
|
|
def test_each_baseline_has_its_own_scenario_per_sleep_stage():
|
|
"""Pins down: a biomarker's baseline is registered per sleep stage, as `eeg-<biomarker>[-<stage>]`.
|
|
|
|
Fails if the two stages' spindles share one registered model, or the naming changes without the trained models.
|
|
"""
|
|
assert scenario_of("spindle_topography", "N2") == "eeg-spindle-topography-n2"
|
|
assert scenario_of("posterior_rhythm", None) == "eeg-posterior-rhythm"
|
|
assert len({m.scenario for m in settings().metrics}) == len(settings().metrics)
|
|
|
|
|
|
def test_a_clean_study_is_valid_and_has_every_metric(measured):
|
|
"""Pins down: a clean study gets the verdict, the study-wide checks and one value per metric, with the
|
|
spindles' value per electrode, the sleep it contains and the age it was measured at.
|
|
|
|
Fails if `measure` stops running the processes the biomarkers need, drops a metric, or loses the electrodes.
|
|
"""
|
|
m = measured["clean"]
|
|
assert (m.valid, m.level, m.error, m.asleep, m.age_months) == (1, "completo", None, 1, 27.5)
|
|
assert {c.code for c in m.checks} >= {"clean-windows", "internal-coverage", "usable-scalp", "triage-level"}
|
|
assert not any(c.abnormal for c in m.checks)
|
|
values = {v.code: v for v in m.metrics}
|
|
assert set(values) == {c.value for c in MetricaEeg}
|
|
assert values["RITMO_POSTERIOR"].value == pytest.approx(7.0, abs=0.5)
|
|
assert values["HUSOS_SUENO_N2"].value > 0
|
|
assert set(values["HUSOS_SUENO_N2"].electrodes) <= set(ELECTRODOS_10_20)
|
|
assert m.plan_hash and "spindle_detection=" in m.versions
|
|
|
|
|
|
def test_flat_channels_make_the_study_not_valid_with_a_check_per_electrode(measured):
|
|
"""Pins down: a study the validator rejects keeps its checks, one per bad channel with its electrode, and has
|
|
no metrics.
|
|
|
|
Fails if a rejected study is measured anyway, or the channel checks lose their electrode or normalised code.
|
|
"""
|
|
m = measured["flat"]
|
|
assert (m.valid, m.error, m.metrics) == (0, None, ())
|
|
dead = {c.electrode for c in m.checks if c.code == CriterioEeg.CANAL_MUERTO.value}
|
|
assert dead == set(FLAT)
|
|
assert any(c.decisive and c.abnormal and c.electrode is None for c in m.checks)
|
|
|
|
|
|
def test_a_file_that_is_not_an_edf_is_not_valid_rather_than_an_error(measured):
|
|
"""Pins down: unreadable bytes are the validator's `file` check, not an analysis error.
|
|
|
|
Fails if `measure` raises, or reports the file as an error (which would retry it on every run).
|
|
"""
|
|
m = measured["broken"]
|
|
assert (m.valid, m.error) == (0, None)
|
|
assert [c.code for c in m.checks] == [CriterioEeg.ARCHIVO.value]
|
|
|
|
|
|
def test_every_check_code_is_in_the_catalog(measured):
|
|
"""Pins down: the codes `measure` stores are `CriterioEeg` values.
|
|
|
|
Fails if the library adds or rewords a check and `criterion_code` stops normalising it.
|
|
"""
|
|
codes = {c.code for m in measured.values() for c in m.checks}
|
|
assert codes <= {c.value for c in CriterioEeg}
|
|
|
|
|
|
def test_channel_codes_lose_their_numbers():
|
|
"""Pins down: the library's channel codes, which embed the measured numbers, become fixed codes.
|
|
|
|
Fails if `criterion_code` stops mapping amplitude or burst reasons, or touches the study-wide codes.
|
|
"""
|
|
assert criterion_code("amplitude-4.5-x", "amplitude 4.5 x the scalp median", "C3") == "amplitude"
|
|
assert criterion_code("amplitude-25000-uv,", "amplitude 25000 uV, out of range even for ekg", "EKG") == \
|
|
"aux-amplitude"
|
|
assert criterion_code("intermittent-contact-by", "intermittent contact by bursts (30% of blocks)", "F3") == \
|
|
"intermittent-bursts"
|
|
assert criterion_code("clean-windows", "clean 4 s windows", None) == "clean-windows"
|