Files
tea/tests/test_validation.py
T
AlbertoPMDM fcd0f76d76 validation: modern validation on MNE/SciPy, agreeing with the director's
The director's rules (validacion.validar, preproceso.canales_malos,
lote.qc_estudio window QC, triaje.triar, cluster gates) rebuilt on MNE
(EDF reading, IIR filtering run by run, Welch PSD), SciPy and antropy (DFA).
All thresholds in rules.ValidationConfig with their sources; edfio header
checks (no silent resampling); R4B FHIR Bundle (private copy of the builder).

Agrees with legacy.validation on dropped channels, triage level and verdict
in every synthetic case; accepted differences (gaps as NaN, one notch, MNE
filter edges, no set-up detection) are documented and tested.
2026-11-18 20:47:07 -06:00

64 lines
2.3 KiB
Python

"""Modern validation: agrees with the legacy (director) validation on synthetic studies."""
from __future__ import annotations
import pytest
from synth_edf import SCALP, make_edf
from tea_modulo.contracts.testing import check_validation
from tea_modulo.legacy.validation import MODULE as LEGACY
from tea_modulo.validation import MODULE, Validation, ValidationConfig
CASES = {
"faults": dict(bad={"flat": ["P3"], "bursts": ["P4"], "bridge": [("F3", "C3")]}),
"too_few_scalp": dict(bad={"flat": SCALP[:8]}),
"loud_and_frozen": dict(bad={"loud": ["O1"], "frozen": ["T4"]}),
"clean": dict(),
}
@pytest.fixture(scope="module", params=list(CASES))
def case(request, tmp_path_factory):
p = make_edf(tmp_path_factory.mktemp(request.param) / "s.edf", minutes=5, **CASES[request.param])
return request.param, p
def test_agrees_with_legacy(case):
name, p = case
m, l = MODULE.run(p), LEGACY.run(p)
assert sorted(m["bad"]) == sorted(l["malos"]), name
assert m["valid"] == l["valid"], name
assert m["level"] == l["triaje"]["nivel"], name
assert abs(m["clean_pct"] - l["qc"]["pct_ventanas_limpias"]) <= 5.0, name
def test_contract_and_fhir(case):
_, p = case
bundle, valid = MODULE.validate(p)
check_validation(bundle, valid, MODULE.summarize(p))
from fhir.resources.R4B.bundle import Bundle
Bundle.model_validate(bundle)
def test_gaps_are_gaps_not_signal(tmp_path):
"""Accepted difference: a BAD_ACQ_SKIP span makes its windows HUECO (legacy reads zeros)."""
p = make_edf(tmp_path / "g.edf", minutes=5, gaps=[(60.0, 8.0)])
r = MODULE.run(p)
assert any(w["verdict"] == "HUECO" for w in r["windows"])
assert r["coverage"] < 1.0 and r["valid"] == 1
def test_broken_file_is_not_valid_not_an_exception(tmp_path):
p = tmp_path / "x.edf"
p.write_bytes(b"0" * 300)
bundle, valid = MODULE.validate(p)
assert valid == 0
check_validation(bundle, valid)
def test_config_changes_the_verdict(tmp_path):
p = make_edf(tmp_path / "s.edf", minutes=4, bad={"flat": SCALP[:2]}) # 17 usable, 2 bad
assert MODULE.run(p)["valid"] == 1
assert Validation(ValidationConfig(min_scalp=18)).run(p)["valid"] == 0
assert Validation(ValidationConfig(max_bad_scalp=1)).run(p)["level"] == "no_analizable"