TEA EEG: training and analysis choose the cluster, EEG ids are text, run progress snapshots; tea library pointer

This commit is contained in:
ulises
2026-11-23 09:29:26 -06:00
parent f9d97f4d12
commit 2a04ee8c8e
9 changed files with 564 additions and 56 deletions
+78 -3
View File
@@ -1,9 +1,10 @@
"""What this module covers: the EEG analysis's settings and its unit of work, `measure`, run on the real `tea`
library.
library, plus EEG selection and the CLI summary. Fails if explicit selection skips current studies or the CLI
stops reporting its final counts.
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.
posterior channels flat; and bytes that are not an EDF. `selection_session` holds three EEG rows in SQLite.
"""
import pytest
from synth_edf import edf_bytes
@@ -18,7 +19,7 @@ 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."""
"""Measure clean, flat, and broken studies to expose changes in triage and metrics."""
s = settings()
return {
"clean": measure("clean", edf_bytes(minutes=4), 27.5, s),
@@ -115,3 +116,77 @@ def test_channel_codes_lose_their_numbers():
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"
@pytest.fixture
def selection_session():
"""Store current, pending, and EDF-less EEGs to expose scope regressions."""
from sqlmodel import Session, create_engine
from core.tea.models import Eeg
engine = create_engine("sqlite://")
Eeg.__table__.create(engine)
with Session(engine) as session:
session.add_all([
Eeg(eeg_id="1", eeg_estudio=b"edf", eeg_plan_hash="current", eeg_versiones="current", eeg_valido=0),
Eeg(eeg_id="2"),
Eeg(eeg_id="3", eeg_estudio=b"edf"),
])
session.commit()
yield session
engine.dispose()
def test_explicit_ids_select_current_edfs(selection_session):
"""Pin explicit reanalysis. Fail if current EEGs are skipped or EDF-less and unrequested rows are selected."""
from models.eeg_analysis import pending_ids
options = {"plan_hash": "current", "versions": "current", "models": dict}
assert pending_ids(selection_session, **options) == ["3"]
assert pending_ids(selection_session, ids=[2, 1, 1], **options) == ["1"]
with pytest.raises(LookupError, match=r"8, 9"):
pending_ids(selection_session, ids=[1, 9, 8], **options)
@pytest.mark.parametrize("failed", [0, 1])
def test_analyze_cli_emits_summary_and_passes_ids(monkeypatch, capsys, failed):
"""Pin the final JSON line and failure status. Fail if explicit IDs or the failed count are ignored."""
import json
from models import eeg_analysis
from models.__main__ import main
def analyze(**options):
assert options == {"everything": False, "limit": None, "ids": ["1", "2"], "cluster": "sync"}
return eeg_analysis.Summary(pending=2, valid=1, not_valid=1 - failed, failed=failed)
monkeypatch.setattr(eeg_analysis, "analyze_pending", analyze)
assert main(["tea", "analyze", "--ids", "1,2", "--cluster", "sync"]) == failed
line = capsys.readouterr().out.splitlines()[-1]
assert line.startswith("summary: ")
assert json.loads(line.removeprefix("summary: ")) == {
"pending": 2, "valid": 1, "not_valid": 1 - failed, "failed": failed,
}
def test_explicit_ids_may_be_uuids(selection_session):
"""Pin that ids are matched as strings, so the UUID ids real EEGs carry select their rows. Fail if ids go back
to being parsed as integers."""
from core.tea.models import Eeg
from models.eeg_analysis import selected_ids
uuid = "ed257fbd-61d5-4c9c-bb72-00c4ec550961"
selection_session.add(Eeg(eeg_id=uuid, eeg_estudio=b"EDF"))
selection_session.commit()
assert selected_ids(selection_session, [uuid]) == [uuid]
with pytest.raises(LookupError, match="no-such-id"):
selected_ids(selection_session, [uuid, "no-such-id"])
@pytest.mark.parametrize("other", [["--all"], ["--limit", "2"]])
def test_analyze_cli_rejects_conflicting_scope(other):
"""Pin exit 2 for ambiguous scopes. Fail if an explicit ID request can start with another scope."""
from models.__main__ import main
assert main(["tea", "analyze", "--ids", "1", *other]) == 2