Training config: reference name and mains frequency: train takes a named reference cohort and its mains frequency; legacy TEA_REFERENCE_DIR stays the default
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import hashlib
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from edf_files import small_edf
|
||||
from pydantic import ValidationError
|
||||
|
||||
from models import definitions
|
||||
from models.tea import jobs
|
||||
from models.tea.eeg import curves, reference
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def measurements(monkeypatch):
|
||||
calls = []
|
||||
|
||||
def measure(studies, *, mains_hz):
|
||||
calls.append((studies, mains_hz))
|
||||
return [
|
||||
{"key": key, "age_months": age, "valid": True, "error": None, "features": {}}
|
||||
for key, _, age in studies
|
||||
]
|
||||
|
||||
monkeypatch.setattr(reference, "measure", measure)
|
||||
return calls
|
||||
|
||||
|
||||
def test_a_named_reference_resolves_under_the_root(cohort, measurements):
|
||||
_, measured, name, hz = jobs._reference(Reference(name="hel"))
|
||||
[(studies, mains_hz)] = measurements
|
||||
assert len(studies) == len(measured) == 3
|
||||
assert all(path.parent == cohort[0] for _, path, _ in studies)
|
||||
assert mains_hz == 50.0
|
||||
assert (name, hz) == ("hel", 50)
|
||||
|
||||
|
||||
def test_60_hz_changes_the_digest_and_is_passed_on(cohort, measurements):
|
||||
digest_50, *_ = jobs._reference(Reference(name="hel"))
|
||||
digest_60, _, _, hz = jobs._reference(Reference(name="hel", line_hz=60))
|
||||
assert digest_50 == hashlib.sha256(b"0.edf|18.0\n1.edf|24.0\n2.edf|30.0").hexdigest()
|
||||
assert digest_60 != digest_50
|
||||
assert hz == 60
|
||||
assert measurements[-1][1] == 60.0
|
||||
|
||||
|
||||
def test_the_cache_is_named_by_cohort_and_digest_and_reused(cohort, measurements, monkeypatch):
|
||||
cache = cohort[0].parent / "c"
|
||||
monkeypatch.setenv("TEA_REFERENCE_CACHE", str(cache))
|
||||
result = jobs._reference(Reference(name="hel"))
|
||||
assert [p.name for p in cache.iterdir()] == [f"hel-{result[0][:16]}.json"]
|
||||
assert jobs._reference(Reference(name="hel")) == result
|
||||
assert len(measurements) == 1
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def test_legacy_environment_still_works(cohort, measurements, monkeypatch):
|
||||
folder, ages = cohort
|
||||
monkeypatch.setenv("TEA_REFERENCE_DIR", str(folder))
|
||||
monkeypatch.setenv("TEA_REFERENCE_AGES", str(ages))
|
||||
_, measured, name, hz = jobs._reference(None)
|
||||
assert len(measured) == 3
|
||||
assert (name, hz) == ("hk1k", 50)
|
||||
assert measurements[0][1] == 50.0
|
||||
|
||||
|
||||
def test_train_records_cohort_and_line_hz(cohort, measurements, monkeypatch):
|
||||
monkeypatch.setattr(reference, "tables", lambda measured: {"curve": measured})
|
||||
monkeypatch.setattr(curves, "train", lambda table, name: {"value": {}})
|
||||
registrations = []
|
||||
|
||||
def register(*args):
|
||||
registrations.append(args)
|
||||
return 1
|
||||
|
||||
monkeypatch.setattr(jobs.registry, "register", register)
|
||||
config = Reference(name="hel", line_hz=60)
|
||||
assert jobs.train("curve", reference=config) == 1
|
||||
[(_, _, params, _, reports)] = registrations
|
||||
digest, *_ = jobs._reference(config)
|
||||
assert params == {"cohort": "hel", "cohort_sha256": digest, "line_hz": "60"}
|
||||
technical = reports["technical_validation"]
|
||||
assert technical["cohort"] == "hel"
|
||||
assert technical["line_hz"] == 60
|
||||
|
||||
|
||||
def test_the_train_op_passes_the_reference_or_refuses_it(monkeypatch):
|
||||
calls = []
|
||||
|
||||
def train(name, as_of, reference=None):
|
||||
calls.append((name, as_of, reference))
|
||||
return 1
|
||||
|
||||
fake = SimpleNamespace(train=train)
|
||||
monkeypatch.setattr(definitions, "_jobs", lambda domain: fake)
|
||||
config = {"ops": {"train_models": {"config": {
|
||||
"domain": "tea", "models": ["curve"], "execution": {"mode": "sync"},
|
||||
"reference": {"name": "a", "line_hz": 60},
|
||||
}}}} # fmt: skip
|
||||
assert definitions.train.execute_in_process(run_config=config).success
|
||||
assert calls == [("curve", None, Reference(name="a", line_hz=60))]
|
||||
|
||||
fake.train = lambda name, as_of: 1
|
||||
with pytest.raises(ValueError, match="tea models take no reference cohort"):
|
||||
definitions.train.execute_in_process(run_config=config)
|
||||
|
||||
|
||||
def test_reference_config_mirrors_the_shared_model():
|
||||
assert definitions.ReferenceConfig.model_fields.keys() == Reference.model_fields.keys()
|
||||
Reference in New Issue
Block a user