TEA record shows the zones most affected by each EEG alteration
This commit is contained in:
@@ -186,9 +186,22 @@ def study(eeg_id: str) -> dict | None:
|
||||
join = ("left join bronze.eeg_scores as s on s.edf = m.edf and s.metric = m.code "
|
||||
"and s.part = 'value'") if scored else "" # fmt: skip
|
||||
metrics = con.execute(text(f"""
|
||||
select m.code, m.value, {score} from bronze.eeg_metrics as m {join}
|
||||
select m.code, m.value, m.electrodes, m.zones, {score}
|
||||
from bronze.eeg_metrics as m {join}
|
||||
where m.edf = :e and m.value is not null order by m.code
|
||||
"""), {"e": head["edf"]}).mappings() # fmt: skip
|
||||
found = [dict(m) for m in metrics]
|
||||
sites: dict[tuple[str, str, str], float | None] = {}
|
||||
if scored:
|
||||
for r in con.execute(text("""
|
||||
select metric, part, site, z from bronze.eeg_scores
|
||||
where edf = :e and part in ('zones', 'electrodes')
|
||||
"""), {"e": head["edf"]}).mappings(): # fmt: skip
|
||||
sites[(r["metric"], r["part"], r["site"])] = r["z"]
|
||||
for m in found:
|
||||
for part in ("zones", "electrodes"):
|
||||
values = json.loads(m[part] or "{}")
|
||||
m[part] = [{"site": k, "value": v, "z": sites.get((m["code"], part, k))}
|
||||
for k, v in values.items() if v is not None and v == v] # fmt: skip
|
||||
return {"id": eeg_id, "valid": head["valid"], "level": head["level"],
|
||||
"age_months": head["age_months"], "metrics": found} # fmt: skip
|
||||
|
||||
+1
-1
Submodule WebApi updated: 7222cbdb49...f6e8b09dd0
+1
-1
Submodule WebApp updated: 20e26b847a...fd19fdaefa
@@ -0,0 +1,44 @@
|
||||
"""One EEG as the patient record reads it (persistance.eeg.study): each metric with its age
|
||||
curve's score, and its zones and electrodes with theirs, so the record can say which zones
|
||||
are most affected."""
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
|
||||
from persistance import destination, eeg
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def one_eeg(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("PLATFORM_DATABASE_URL", f"sqlite:///{tmp_path / 'platform.db'}")
|
||||
monkeypatch.setattr(destination, "DATA_DIR", tmp_path) # never the deployed data folder
|
||||
destination.engine.cache_clear()
|
||||
with destination.engine().begin() as con:
|
||||
con.execute(text("create table silver.eeg (person_id text, study_date text, edf text)"))
|
||||
con.execute(text("create table bronze.eeg_analysis (edf text, valid boolean, "
|
||||
"level text, age_months real)")) # fmt: skip
|
||||
con.execute(text("create table bronze.eeg_metrics (edf text, code text, value real, "
|
||||
"electrodes text, zones text)")) # fmt: skip
|
||||
con.execute(text("create table bronze.eeg_scores (edf text, metric text, part text, "
|
||||
"site text, z real, outside real, p5 real, median real, p95 real, "
|
||||
"model text)")) # fmt: skip
|
||||
con.execute(text("insert into silver.eeg values ('p1', '2026-10-01', 'h1')"))
|
||||
con.execute(text("insert into bronze.eeg_analysis values ('h1', 1, 'good', 30)"))
|
||||
con.execute(text("insert into bronze.eeg_metrics values ('h1', 'spindles_n2', 2.0, "
|
||||
":e, :z)"), {"e": json.dumps({"C3": 1.5}), "z": json.dumps(
|
||||
{"central": 1.9, "occipital": float("nan")})}) # fmt: skip
|
||||
con.execute(text("insert into bronze.eeg_scores values "
|
||||
"('h1', 'spindles_n2', 'value', null, -2.4, -1, 3, 4, 5, 'm/1'), "
|
||||
"('h1', 'spindles_n2', 'zones', 'central', -2.6, -1, 3, 4, 5, 'm/1')"))
|
||||
yield
|
||||
destination.engine.cache_clear()
|
||||
|
||||
|
||||
def test_a_metric_carries_its_zones_and_electrodes_with_their_scores(one_eeg):
|
||||
found = eeg.study("p1|2026-10-01")
|
||||
(metric,) = found["metrics"]
|
||||
assert (metric["z"], metric["p5"], metric["p95"]) == (-2.4, 3, 5)
|
||||
assert metric["zones"] == [{"site": "central", "value": 1.9, "z": -2.6}] # NaN left out
|
||||
assert metric["electrodes"] == [{"site": "C3", "value": 1.5, "z": None}] # no curve
|
||||
Reference in New Issue
Block a user