105 lines
4.2 KiB
Python
105 lines
4.2 KiB
Python
"""TEA's follow-up list (pure): the children of the screening cohort active in the window, where
|
|
each stands on the route by the cut-off day, and their EEG.
|
|
|
|
- status (the route): mchat_only, psychology (ADOS-2), neuropediatrics (a neurology note), eeg
|
|
(a good EEG after it);
|
|
- case: confirmed (the neurologist confirmed TEA), not_confirmed, or in_process;
|
|
- EEG: the study EEG is her first valid one known by the cut-off, else her latest; its status
|
|
is no_valid_eeg without a valid one, abnormal when any of its metrics falls outside the age
|
|
curve's band (its alterations, each `<metric>:<above|below>`), else normal.
|
|
A child is on the list when any step of hers happened within the window."""
|
|
|
|
from datetime import date
|
|
|
|
from core import tea
|
|
from core.measures import as_day
|
|
from core.tea import ados2
|
|
|
|
WINDOWS = tea.FOLLOW_UP_WINDOWS
|
|
SORTS = ("-recent",)
|
|
DEFAULT_HORIZON = None
|
|
STATUS_SYSTEM = "tea-follow-up-status"
|
|
PROCEDURES: tuple[str, ...] = ()
|
|
REPORT_FACTS: dict[str, str] = {}
|
|
STEPS = (("psychology_date", "psychology"), ("neuro_date", "neuropediatrics"),
|
|
("eeg_date", "eeg")) # fmt: skip
|
|
DATES = ("mchat_date", "psychology_date", "neuro_date", "confirmed_date", "not_confirmed_date",
|
|
"eeg_date") # fmt: skip
|
|
|
|
|
|
def _by(day, cutoff: date) -> date | None:
|
|
found = as_day(day)
|
|
return found if found is not None and found <= cutoff else None
|
|
|
|
|
|
def stage(row: dict, cutoff: date) -> str:
|
|
reached = "mchat_only"
|
|
for column, name in STEPS:
|
|
if _by(row.get(column), cutoff):
|
|
reached = name
|
|
return reached
|
|
|
|
|
|
def case(row: dict, cutoff: date) -> str:
|
|
if _by(row.get("confirmed_date"), cutoff):
|
|
return "confirmed"
|
|
if _by(row.get("not_confirmed_date"), cutoff):
|
|
return "not_confirmed"
|
|
return "in_process"
|
|
|
|
|
|
def listed(rows: list[dict], start: date, cutoff: date) -> list[dict]:
|
|
"""From each child's screening row joined to her EEGs and their metrics outside the band
|
|
(one row per child, EEG and such metric), who is on the list, most recent step first."""
|
|
children: dict[str, dict] = {}
|
|
eegs: dict[str, dict[str, dict]] = {}
|
|
for row in rows:
|
|
person = row["person_id"]
|
|
if not _by(row.get("mchat_date"), cutoff):
|
|
continue
|
|
children.setdefault(person, row)
|
|
studied = _by(row.get("eeg_study_date"), cutoff)
|
|
if row.get("eeg_id") and studied:
|
|
eeg = eegs.setdefault(person, {}).setdefault(
|
|
row["eeg_id"], {"date": studied, "valid": row.get("valid"), "altered": set()}
|
|
)
|
|
if row.get("metric") and row.get("outside"):
|
|
side = "above" if row["outside"] > 0 else "below"
|
|
eeg["altered"].add(f"{row['metric']}:{side}")
|
|
out = []
|
|
for person, row in children.items():
|
|
steps = [d for c in DATES if (d := _by(row.get(c), cutoff))]
|
|
recent = max(steps)
|
|
if recent < start:
|
|
continue
|
|
mine = sorted(eegs.get(person, {}).values(), key=lambda e: e["date"])
|
|
valid = [e for e in mine if e["valid"]]
|
|
study = valid[0] if valid else (mine[-1] if mine else None)
|
|
if not valid:
|
|
eeg_status, altered = "no_valid_eeg", []
|
|
else:
|
|
altered = sorted(study["altered"])
|
|
eeg_status = "abnormal" if altered else "normal"
|
|
css = None
|
|
if row.get("module") and as_day(row.get("evaluation_date")):
|
|
evaluated = {**row, "evaluation_date": as_day(row["evaluation_date"])}
|
|
css, _ = ados2.score(evaluated, as_day(row.get("birth_date")))
|
|
out.append({
|
|
"person_id": person, "birth_date": row.get("birth_date"),
|
|
"status": stage(row, cutoff), "priority": None, "recent": recent,
|
|
"age_group": row.get("age_group"), "case": case(row, cutoff),
|
|
"eeg_status": eeg_status, "alterations": altered,
|
|
"last_eeg": mine[-1]["date"] if mine else None,
|
|
"mchat_score": row.get("score"), "ados2_css": css,
|
|
}) # fmt: skip
|
|
return sorted(out, key=lambda r: (-r["recent"].toordinal(), r["person_id"]))
|
|
|
|
|
|
def status(row: dict, cutoff: date) -> None:
|
|
"""A child's status exists only on the list."""
|
|
return None
|
|
|
|
|
|
def ordered(rows: list[dict], sort: str, risks: dict, horizon) -> list[dict]:
|
|
return rows
|