141 lines
5.9 KiB
Python
141 lines
5.9 KiB
Python
"""What this module covers: the age an EEG is analysed at. The `tea` library reads it from the EDF header (recording
|
|
start minus birth date, `header_age_months`) when no age is passed, and `measure` prefers it over the age the
|
|
patient's rows give.
|
|
|
|
Fixtures: `dated` and `undated`, paths to one-minute synthetic studies (`synth_edf.py`); `dated`'s header says the
|
|
child was born 2023-06-01 and recorded 2025-06-01 (24.0 months), `undated`'s has neither date. `measured_dated` is the
|
|
four-minute dated study measured once, with 27.5 months from the rows.
|
|
"""
|
|
import logging
|
|
from datetime import date
|
|
|
|
import pytest
|
|
from synth_edf import edf_bytes
|
|
|
|
from models.tea.eeg.analyze import measure, study_age
|
|
from models.tea.eeg.settings import settings
|
|
|
|
BORN, RECORDED = date(2023, 6, 1), date(2025, 6, 1)
|
|
HEADER_AGE = 24.0
|
|
ROW_AGE = 27.5
|
|
|
|
|
|
def _write(tmp_path, name: str, **header) -> str:
|
|
path = tmp_path / f"{name}.edf"
|
|
path.write_bytes(edf_bytes(minutes=1, **header))
|
|
return str(path)
|
|
|
|
|
|
@pytest.fixture
|
|
def dated(tmp_path):
|
|
"""A study whose header has the birth date and the recording date."""
|
|
return _write(tmp_path, "dated", birthdate=BORN, startdate=RECORDED)
|
|
|
|
|
|
@pytest.fixture
|
|
def undated(tmp_path):
|
|
"""A study whose header has neither date (`X` and the 01.01.85 placeholder)."""
|
|
return _write(tmp_path, "undated")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def measured_dated():
|
|
"""The dated study, four minutes long so it passes validation, measured with the rows' 27.5 months."""
|
|
return measure("dated", edf_bytes(minutes=4, birthdate=BORN, startdate=RECORDED), ROW_AGE, settings())
|
|
|
|
|
|
def test_header_age_is_start_minus_birth_in_months(dated):
|
|
"""Pins down: the header age is (start - birth) days / 30.4375, to one decimal, like `eeg_analysis.age_months`.
|
|
|
|
Fails if `header_age_months` reads another header field, uses another month length or stops rounding, which
|
|
would put the header age and the rows' age on different scales.
|
|
"""
|
|
from tea.contracts import header_age_months
|
|
|
|
assert header_age_months(dated) == HEADER_AGE
|
|
assert header_age_months(dated, days_per_month=30.0) == round(731 / 30.0, 1)
|
|
|
|
|
|
@pytest.mark.parametrize("header", [
|
|
{},
|
|
{"startdate": RECORDED},
|
|
{"birthdate": BORN},
|
|
{"birthdate": date(2025, 7, 1), "startdate": RECORDED},
|
|
{"birthdate": date(2000, 1, 1), "startdate": RECORDED},
|
|
], ids=["no-dates", "no-birth", "placeholder-start", "born-after-start", "over-18-years"])
|
|
def test_header_without_a_usable_age_gives_none(tmp_path, header):
|
|
"""Pins down: a missing birth date, the 01.01.85 placeholder start, a birth after the start and an age over 216
|
|
months all give no header age.
|
|
|
|
Fails if `header_age_months` stops checking any of them: anonymised or mis-clocked files would then be analysed
|
|
at a nonsense age instead of the rows' age.
|
|
"""
|
|
from tea.contracts import header_age_months
|
|
|
|
assert header_age_months(_write(tmp_path, "study", **header)) is None
|
|
|
|
|
|
def test_recording_takes_the_header_age_unless_one_is_passed(dated):
|
|
"""Pins down: `Recording.from_edf` without an age carries the header's, and an explicit age still wins.
|
|
|
|
Fails if `from_edf` stops falling back to the header, or lets the header override an age the caller passed
|
|
(reference cohorts pass their manifest ages).
|
|
"""
|
|
from tea.contracts import Recording
|
|
|
|
assert Recording.from_edf(dated).age_months == HEADER_AGE
|
|
assert Recording.from_edf(dated, age_months=ROW_AGE).age_months == ROW_AGE
|
|
|
|
|
|
def test_preprocessing_takes_the_header_age_unless_one_is_passed(dated, undated):
|
|
"""Pins down: modern preprocessing without an age carries the header's in `Preprocessed.age_months`, keeps an
|
|
explicit one, and has none for a header without dates.
|
|
|
|
Fails if `Preprocessing.run` stops falling back to the header, which would leave the biomarkers' age baselines
|
|
without an age for every caller that doesn't pass one.
|
|
"""
|
|
from tea.preprocessing import DIRECTOR_CLUSTER, Preprocessing
|
|
|
|
plan = DIRECTOR_CLUSTER.to_dict()
|
|
assert Preprocessing().run(dated, plan).age_months == HEADER_AGE
|
|
assert Preprocessing().run(dated, plan, age_months=ROW_AGE).age_months == ROW_AGE
|
|
assert Preprocessing().run(undated, plan).age_months is None
|
|
|
|
|
|
def test_study_age_prefers_the_header_and_logs_a_disagreement(dated, caplog):
|
|
"""Pins down: with both ages, `study_age` returns the header's and logs that the rows disagree by more than a
|
|
month.
|
|
|
|
Fails if the rows' age wins again, or if a disagreement stops being logged.
|
|
"""
|
|
with caplog.at_level(logging.WARNING, logger="models.tea.eeg.analyze"):
|
|
assert study_age("e1", dated, ROW_AGE, 30.4375) == HEADER_AGE
|
|
assert "e1" in caplog.text and "24.0" in caplog.text and "27.5" in caplog.text
|
|
caplog.clear()
|
|
with caplog.at_level(logging.WARNING, logger="models.tea.eeg.analyze"):
|
|
assert study_age("e1", dated, 24.3, 30.4375) == HEADER_AGE
|
|
assert caplog.text == ""
|
|
|
|
|
|
def test_study_age_falls_back_to_the_rows(undated, tmp_path):
|
|
"""Pins down: a header without dates, or a file that isn't an EDF, gives the rows' age, which may be None.
|
|
|
|
Fails if `study_age` returns None, or raises, when the header has no age: EEGs from hospitals that anonymise
|
|
their files would lose the age the platform has.
|
|
"""
|
|
broken = tmp_path / "broken.edf"
|
|
broken.write_bytes(b"not an edf")
|
|
assert study_age("e1", undated, ROW_AGE, 30.4375) == ROW_AGE
|
|
assert study_age("e1", undated, None, 30.4375) is None
|
|
assert study_age("e1", broken, ROW_AGE, 30.4375) == ROW_AGE
|
|
|
|
|
|
def test_measure_analyses_and_reports_the_header_age(measured_dated):
|
|
"""Pins down: `measure` on a study whose header has an age reports that age, not the rows', on a valid
|
|
analysis; that is what `eeg_edad_meses` and the baseline deviations are written from.
|
|
|
|
Fails if `measure` reports the argument instead of the age it analysed at, or stops reading the header.
|
|
"""
|
|
m = measured_dated
|
|
assert (m.valid, m.error, m.age_months) == (1, None, HEADER_AGE)
|