TEA EEG analysis at the age the EDF header gives, the patient's rows as fallback; tea library 0.2.0

This commit is contained in:
AlbertoPMDM
2026-11-19 19:20:29 -06:00
parent 25c460f80e
commit 3669aa9eae
4 changed files with 181 additions and 11 deletions
+34 -7
View File
@@ -1,11 +1,16 @@
"""One EEG through the `tea` library: validation, then preprocessing, the processes and the metrics' biomarkers.
`measure` is the unit of work of a batch and may run on a Dask worker: it takes the EDF's bytes and the age, touches
no database and returns a `Measurement` with the verdict, every quality check and each metric's raw value. The
deviation from the age baselines is added afterwards (`baselines.py`), where the registered models are.
`measure` is the unit of work of a batch and may run on a Dask worker: it takes the EDF's bytes and the age the
patient's rows give, touches no database and returns a `Measurement` with the verdict, every quality check and each
metric's raw value. The deviation from the age baselines is added afterwards (`baselines.py`), where the registered
models are.
The age is the EDF header's (recording start minus birth date) when the header has both, and the rows' otherwise:
the file is written when the study is recorded, the rows' birth date may be missing or mistyped.
The library reads EDFs from paths only, so the bytes go to a temporary file for the duration of the call.
"""
import logging
import re
import tempfile
import time
@@ -15,8 +20,12 @@ from types import SimpleNamespace
from models.tea.eeg.settings import Settings
log = logging.getLogger(__name__)
# Channel codes the library builds from the reason's first words, and the fixed code each is stored as.
CHANNEL_CODES = {"intermittent-contact-by": "intermittent-bursts"}
# Header and rows further apart than this, in months, are logged.
AGE_DISAGREEMENT_MONTHS = 1.0
@dataclass(frozen=True)
@@ -42,7 +51,7 @@ class MetricValue:
@dataclass(frozen=True)
class Measurement:
key: str
age_months: float | None
age_months: float | None # the age the study was analysed at
plan_hash: str
versions: str
valid: int | None = None
@@ -190,9 +199,26 @@ def _study(pipe, path, settings: Settings, the_plan: dict, age_months: float | N
return run, summary, valid, study
def study_age(key: str, path: Path, row_age: float | None, days_per_month: float) -> float | None:
"""The header's age if it has one, else `row_age`. An unreadable header counts as none: validation reports the
file."""
from tea.contracts import header_age_months
try:
header = header_age_months(path, days_per_month=days_per_month)
except Exception: # noqa: BLE001
header = None
if header is None:
return row_age
if row_age is not None and abs(header - row_age) > AGE_DISAGREEMENT_MONTHS:
log.warning("EEG %s: the EDF header gives %.1f months and the patient's rows %.1f; using the header",
key, header, row_age)
return header
def measure(key: str, edf: bytes, age_months: float | None, settings: Settings) -> Measurement:
"""Never raises: a failure is a `Measurement` with `error` set, keeping the verdict and checks if validation
finished before it."""
"""`age_months` is the rows' age, used when the EDF header has none (`study_age`). Never raises: a failure is a
`Measurement` with `error` set, keeping the verdict and checks if validation finished before it."""
started = time.time()
pipe = pipeline(settings)
the_plan = plan(settings)
@@ -201,8 +227,9 @@ def measure(key: str, edf: bytes, age_months: float | None, settings: Settings)
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / "study.edf"
path.write_bytes(edf)
result["age_months"] = age = study_age(key, path, age_months, settings.days_per_month)
validation = pipe.module("validation")
run, summary, valid, study = _study(pipe, path, settings, the_plan, age_months, settings.line_hz)
run, summary, valid, study = _study(pipe, path, settings, the_plan, age, settings.line_hz)
result.update(
valid=valid, level=summary.level, clean_fraction=_number(summary.clean_fraction),
bad_channels=len(summary.bad_channels), line_db=_number(summary.line_db),