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))