T3: TEA age curves train from one training source: the site or a folder
PLAN-feature-parity §2.3 and user decisions 2 and 6. - jobs._cohort(source, as_of) replaces _reference: kind "site" (default) takes the valid EEGs of the children without the diagnosis by the cut-off (persistance.eeg .without_diagnosis, rebuild's ruled-out group in tea/parity's design); kind "folder" takes a folder under TEA_REFERENCE_ROOT with its EDFs and edades.csv (path, age in months), measured at the folder's mains and cached by content as before. - The legacy TEA_REFERENCE_DIR / TEA_REFERENCE_AGES path, the hk1k name and the years-to-months ages table are gone: Helsinki is one such folder. - Reports and params name the cohort (site or the folder), its digest, and the cut-off (site) or mains (folder). - definitions: Training.source (TrainingSourceConfig) replaces reference. - tests: test_tea_reference -> test_tea_training_source (folder listing and errors, the site cohort's cut-off rules).
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
import pytest
|
||||
from edf_files import small_edf
|
||||
from pydantic import ValidationError
|
||||
|
||||
from models.tea import jobs
|
||||
from persistance.cluster import Reference
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cohort(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("TEA_REFERENCE_ROOT", str(tmp_path))
|
||||
monkeypatch.delenv("TEA_REFERENCE_CACHE", raising=False)
|
||||
folder = tmp_path / "hel"
|
||||
folder.mkdir()
|
||||
for i in range(3):
|
||||
small_edf(folder / f"{i}.edf")
|
||||
ages = folder / "edades.csv"
|
||||
ages.write_text("name,age\n0.edf,1.5\n1.edf,2.0\n2.edf,2.5\n")
|
||||
return folder, ages
|
||||
|
||||
|
||||
def test_bad_references_fail_with_a_reason(cohort):
|
||||
folder, ages = cohort
|
||||
for name in ("..", ".", "nope"):
|
||||
with pytest.raises(ValueError) as exc:
|
||||
jobs._reference(Reference(name=name))
|
||||
assert str(exc.value) == f"no reference cohort {name!r}"
|
||||
|
||||
ages.write_text("name,age\n0.edf,1\n1.edf,4\n2.edf,5\n")
|
||||
with pytest.raises(
|
||||
ValueError, match="reference cohort 'hel' has no EDF with an age from 16 to 36 months"
|
||||
):
|
||||
jobs._reference(Reference(name="hel"))
|
||||
|
||||
ages.unlink()
|
||||
with pytest.raises(ValueError, match=r"reference cohort 'hel' has no edades\.csv"):
|
||||
jobs._reference(Reference(name="hel"))
|
||||
|
||||
with pytest.raises(ValidationError, match="name"):
|
||||
Reference(name="../x")
|
||||
with pytest.raises(ValidationError, match="line_hz"):
|
||||
Reference(name="hel", line_hz=55)
|
||||
@@ -0,0 +1,101 @@
|
||||
import json
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
from edf_files import small_edf
|
||||
from pydantic import ValidationError
|
||||
|
||||
from models.jobs import EmptyCohort
|
||||
from models.tea import jobs
|
||||
from models.tea.eeg import reference
|
||||
from persistance import eeg
|
||||
from persistance.cluster import TrainingSource
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def folder(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("TEA_REFERENCE_ROOT", str(tmp_path))
|
||||
monkeypatch.delenv("TEA_REFERENCE_CACHE", raising=False)
|
||||
cohort = tmp_path / "hel"
|
||||
(cohort / "night").mkdir(parents=True)
|
||||
for name in ("0.edf", "1.edf", "night/2.edf"):
|
||||
small_edf(cohort / name)
|
||||
ages = cohort / "edades.csv"
|
||||
ages.write_text("path,age\n0.edf,18\n1.edf,24.04\nnight/2.edf,30\n")
|
||||
return cohort, ages
|
||||
|
||||
|
||||
def test_a_folder_lists_each_edf_with_its_age_in_months(folder):
|
||||
cohort, ages = folder
|
||||
assert reference.listing(cohort, ages) == [
|
||||
("hel-0001", cohort / "0.edf", 18.0),
|
||||
("hel-0002", cohort / "1.edf", 24.0),
|
||||
("hel-0003", cohort / "night" / "2.edf", 30.0),
|
||||
]
|
||||
ages.write_text("path,age\n0.edf,12\n1.edf,24\nnight/2.edf,40\n")
|
||||
assert [age for _, _, age in reference.listing(cohort, ages)] == [24.0]
|
||||
|
||||
|
||||
def test_bad_folders_fail_with_a_reason(folder):
|
||||
cohort, ages = folder
|
||||
for name in ("..", ".", "nope"):
|
||||
with pytest.raises(ValueError) as exc:
|
||||
jobs._cohort(TrainingSource(kind="folder", folder=name), date(2026, 1, 1))
|
||||
assert str(exc.value) == f"no reference cohort {name!r}"
|
||||
|
||||
ages.write_text("path,age\n0.edf,12\n1.edf,48\nnight/2.edf,60\n")
|
||||
with pytest.raises(
|
||||
ValueError, match="folder 'hel' has no EDF with an age from 16 to 36 months"
|
||||
):
|
||||
jobs._cohort(TrainingSource(kind="folder", folder="hel"), date(2026, 1, 1))
|
||||
|
||||
ages.unlink()
|
||||
with pytest.raises(ValueError, match=r"folder 'hel' has no edades\.csv"):
|
||||
jobs._cohort(TrainingSource(kind="folder", folder="hel"), date(2026, 1, 1))
|
||||
|
||||
with pytest.raises(ValidationError, match="folder"):
|
||||
TrainingSource(kind="folder")
|
||||
with pytest.raises(ValidationError, match="line_hz"):
|
||||
TrainingSource(kind="folder", folder="hel", line_hz=55)
|
||||
|
||||
|
||||
def test_the_site_trains_on_children_without_the_diagnosis_by_the_cut_off(fresh_platform):
|
||||
with fresh_platform.begin() as con:
|
||||
con.exec_driver_sql("create table silver.eeg (person_id text, study_date text, edf text)")
|
||||
con.exec_driver_sql(
|
||||
"insert into silver.eeg values (?, ?, ?)",
|
||||
[("ruled_out", "2025-03-01", "a"), ("confirmed", "2025-03-01", "b"),
|
||||
("later", "2025-03-01", "c"), ("ruled_out", "2025-04-01", "d"),
|
||||
("ruled_out", "2025-09-01", "e")],
|
||||
) # fmt: skip
|
||||
con.exec_driver_sql(
|
||||
"create table bronze.eeg_analysis "
|
||||
"(edf text, age_months real, features text, valid boolean, error text)"
|
||||
)
|
||||
features = json.dumps({"eeg-gradient": {"value": 1.0}})
|
||||
con.exec_driver_sql(
|
||||
"insert into bronze.eeg_analysis values (?, ?, ?, ?, ?)",
|
||||
[("a", 20.0, features, True, None), ("b", 22.0, features, True, None),
|
||||
("c", 24.0, features, True, None), ("d", 21.0, features, False, None),
|
||||
("e", 26.0, features, True, None)],
|
||||
) # fmt: skip
|
||||
con.exec_driver_sql(
|
||||
"create table gold.tea_screening "
|
||||
"(person_id text, not_confirmed_date text, confirmed_date text)"
|
||||
)
|
||||
con.exec_driver_sql(
|
||||
"insert into gold.tea_screening values (?, ?, ?)",
|
||||
[("ruled_out", "2025-05-01", None), ("confirmed", "2025-02-01", "2025-05-01"),
|
||||
("later", "2025-07-01", None)],
|
||||
) # fmt: skip
|
||||
|
||||
cut = date(2025, 6, 1)
|
||||
assert eeg.without_diagnosis(cut) == [
|
||||
{"key": "a", "age_months": 20.0, "valid": True, "error": None,
|
||||
"features": {"eeg-gradient": {"value": 1.0}}},
|
||||
] # fmt: skip
|
||||
digest, measured, cohort, hz = jobs._cohort(TrainingSource(), cut)
|
||||
assert (cohort, hz, [m["key"] for m in measured]) == ("site", None, ["a"])
|
||||
assert len(digest) == 64
|
||||
with pytest.raises(EmptyCohort, match="without the diagnosis by 2025-01-01"):
|
||||
jobs._cohort(TrainingSource(), date(2025, 1, 1))
|
||||
Reference in New Issue
Block a user