feat(tea): EEG analysis before the silver load, and its verdict, quality checks and baselines through silver and FHIR
This commit is contained in:
+47
-26
@@ -17,7 +17,8 @@ with the ADOS-2, neuropediatrics note, good-quality EEG). Ages are at the first
|
||||
step is dated by the note, so it reaches EEG on 2 Jun and leaves after June.
|
||||
- 7, H-T07, without birth date: in the cohort with age group `sin_dato`. M-CHAT 1 May, nothing else.
|
||||
|
||||
Every EEG's quality criteria are written as its analysis would; a bad EEG has one criterion out of its threshold.
|
||||
Every EEG's verdict and quality checks are written as its analysis would: a good EEG has its study-wide checks
|
||||
within their limits; a bad one has too few clean windows and a dead channel (T3).
|
||||
|
||||
Test support: writes in the session it's given, never in the domain's database.
|
||||
"""
|
||||
@@ -27,6 +28,7 @@ from datetime import date
|
||||
from core.tea.models import (
|
||||
Ados2,
|
||||
Eeg,
|
||||
EegCriterio,
|
||||
EegMetrica,
|
||||
EegMetricaElectrodo,
|
||||
EvaluacionNeuropediatrica,
|
||||
@@ -75,10 +77,22 @@ NOTAS = (
|
||||
(6, date(2025, 6, 2), "F84.0"),
|
||||
)
|
||||
|
||||
GOOD = {"amplitud": 0, "alta_frecuencia": 1, "correlacion": 0, "sin_senal": 0, "linea": 3.5}
|
||||
BAD = {**GOOD, "sin_senal": 5}
|
||||
# The analysis's verdict, triage level, clean fraction, bad channels and line noise, and its checks:
|
||||
# (code, electrode, value, text, unit, low, high, decisive, abnormal).
|
||||
GOOD = {"valido": 1, "nivel": "completo", "limpia": 0.95, "malos": 0, "linea": 3.5, "criterios": (
|
||||
("clean-windows", None, 95.0, None, "%", 60.0, None, 1, 0),
|
||||
("usable-scalp", None, 19.0, None, "electrodes", 12.0, None, 1, 0),
|
||||
("triage-level", None, None, "completo", None, None, None, 1, 0),
|
||||
("line-noise", None, 3.5, None, "dB", None, 45.0, 0, 0),
|
||||
)}
|
||||
BAD = {"valido": 0, "nivel": "parcial", "limpia": 0.4, "malos": 1, "linea": 3.5, "criterios": (
|
||||
("dead-channel", "T3", None, "dead channel", None, None, None, 0, 1),
|
||||
("clean-windows", None, 40.0, None, "%", 60.0, None, 1, 1),
|
||||
("usable-scalp", None, 18.0, None, "electrodes", 12.0, None, 1, 0),
|
||||
("line-noise", None, 3.5, None, "dB", None, 45.0, 0, 0),
|
||||
)}
|
||||
|
||||
# (eeg id, pac_id, date, laboratory id, criteria, with sleep)
|
||||
# (eeg id, pac_id, date, laboratory id, analysis, with sleep)
|
||||
EEGS = (
|
||||
("eeg-1a", 1, date(2025, 4, 2), "LAB-1A", BAD, 0),
|
||||
("eeg-1b", 1, date(2025, 5, 20), "LAB-1B", GOOD, 1),
|
||||
@@ -87,30 +101,32 @@ EEGS = (
|
||||
("eeg-6", 6, date(2025, 6, 1), "LAB-6", GOOD, 1),
|
||||
)
|
||||
|
||||
# (metric id, eeg id, code, value): a null value can't be evaluated.
|
||||
# (metric id, eeg id, code, value, deviation): a null value can't be evaluated. The deviation is the analysis's
|
||||
# against the metric's age baseline: (z, -1/0/+1, p5, median, p95). Child 1's slow posterior rhythm is below p5.
|
||||
METRICAS = (
|
||||
("met-1b-rp", "eeg-1b", "RITMO_POSTERIOR", 3.2),
|
||||
("met-1b-n2", "eeg-1b", "HUSOS_SUENO_N2", 4.0),
|
||||
("met-1b-pp", "eeg-1b", "PICOS_PAROXISTICOS", 0.4),
|
||||
("met-1a-rp", "eeg-1a", "RITMO_POSTERIOR", 2.0),
|
||||
("met-6-rp", "eeg-6", "RITMO_POSTERIOR", 6.5),
|
||||
("met-6-n2", "eeg-6", "HUSOS_SUENO_N2", None),
|
||||
("met-6-gr", "eeg-6", "GRADIENTE_ANTEROPOSTERIOR", 2.6),
|
||||
("met-1b-rp", "eeg-1b", "RITMO_POSTERIOR", 3.2, (-4.08, -1, 4.5, 5.75, 7.0)),
|
||||
("met-1b-n2", "eeg-1b", "HUSOS_SUENO_N2", 4.0, (-0.5, 0, 2.5, 4.5, 6.5)),
|
||||
("met-1b-pp", "eeg-1b", "PICOS_PAROXISTICOS", 0.4, (-0.4, 0, 0.0, 0.5, 1.0)),
|
||||
("met-1a-rp", "eeg-1a", "RITMO_POSTERIOR", 2.0, (-6.0, -1, 4.5, 5.75, 7.0)),
|
||||
("met-6-rp", "eeg-6", "RITMO_POSTERIOR", 6.5, (0.4, 0, 5.0, 6.25, 7.5)),
|
||||
("met-6-n2", "eeg-6", "HUSOS_SUENO_N2", None, (None, None, None, None, None)),
|
||||
("met-6-gr", "eeg-6", "GRADIENTE_ANTEROPOSTERIOR", 2.6, (0.27, 0, 1.75, 2.5, 3.25)),
|
||||
)
|
||||
|
||||
# (electrode value id, metric id, electrode, z-score)
|
||||
# (electrode value id, metric id, electrode, value, z-score)
|
||||
ELECTRODOS = (
|
||||
("mel-1", "met-1b-rp", "O1", -2.8),
|
||||
("mel-2", "met-1b-rp", "O2", -2.4),
|
||||
("mel-3", "met-1b-rp", "Pz", -1.1),
|
||||
("mel-1", "met-1b-rp", "O1", 3.0, -2.8),
|
||||
("mel-2", "met-1b-rp", "O2", 3.1, -2.4),
|
||||
("mel-3", "met-1b-rp", "Pz", 3.6, -1.1),
|
||||
)
|
||||
|
||||
# The EEG of child 1 that completes the chain hangs from an order.
|
||||
ORDENES = (("ord-1b", 1, "ORD-T01-EEG", "ELECTROENCEFALOGRAMA", date(2025, 5, 2)),)
|
||||
ORDEN_DE = {"eeg-1b": "ord-1b"}
|
||||
|
||||
# What the load gives (`test_orquestacion_plata`).
|
||||
PROCEDIMIENTOS = len(ORDENES) + len(MCHATS) + len(ADOS2) + len(NOTAS) + len(EEGS) + len(ELECTRODOS)
|
||||
# What the load gives (`test_orquestacion_plata`): a bad channel is a part of its EEG, as an electrode value is.
|
||||
CANALES_MALOS = sum(1 for *_, analisis, _ in EEGS for c in analisis["criterios"] if c[1] is not None)
|
||||
PROCEDIMIENTOS = len(ORDENES) + len(MCHATS) + len(ADOS2) + len(NOTAS) + len(EEGS) + len(ELECTRODOS) + CANALES_MALOS
|
||||
|
||||
|
||||
def insertar(sesion: Session) -> None:
|
||||
@@ -126,14 +142,19 @@ def insertar(sesion: Session) -> None:
|
||||
ado_clasificacion=clasificacion, ado_algoritmo="Pocas o ninguna palabra", **items))
|
||||
sesion.add_all(EvaluacionNeuropediatrica(neu_id=f"neu-{k}", neu_pac_id=p, neu_fecha=f, neu_cie_codigo=c)
|
||||
for k, (p, f, c) in enumerate(NOTAS, 1))
|
||||
for i, p, f, lab, criterios, sueno in EEGS:
|
||||
for i, p, f, lab, analisis, sueno in EEGS:
|
||||
sesion.add(Eeg(
|
||||
eeg_id=i, eeg_pac_id=p, eeg_fecha_estudio=f, eeg_id_registro_laboratorio=lab, eeg_ord_id=ORDEN_DE.get(i),
|
||||
eeg_con_sueno=sueno, eeg_canales=19, eeg_duracion_min=25.0,
|
||||
eeg_canales_amplitud=criterios["amplitud"], eeg_canales_alta_frecuencia=criterios["alta_frecuencia"],
|
||||
eeg_canales_correlacion=criterios["correlacion"], eeg_canales_sin_senal=criterios["sin_senal"],
|
||||
eeg_ruido_linea_db=criterios["linea"],
|
||||
eeg_con_sueno=sueno, eeg_canales=19, eeg_duracion_min=25.0, eeg_valido=analisis["valido"],
|
||||
eeg_nivel=analisis["nivel"], eeg_fraccion_limpia=analisis["limpia"], eeg_canales_malos=analisis["malos"],
|
||||
eeg_ruido_linea_db=analisis["linea"],
|
||||
))
|
||||
sesion.add_all(EegMetrica(met_id=i, met_eeg_id=e, met_codigo=c, met_valor=v) for i, e, c, v in METRICAS)
|
||||
sesion.add_all(EegMetricaElectrodo(mel_id=i, mel_met_id=m, mel_electrodo=e, mel_valor=v)
|
||||
for i, m, e, v in ELECTRODOS)
|
||||
sesion.add_all(EegCriterio(cri_id=f"cri-{i}-{k}", cri_eeg_id=i, cri_codigo=c, cri_electrodo=e, cri_valor=v,
|
||||
cri_valor_texto=t, cri_unidad=u, cri_bajo=lo, cri_alto=hi, cri_decisivo=d,
|
||||
cri_anormal=a)
|
||||
for k, (c, e, v, t, u, lo, hi, d, a) in enumerate(analisis["criterios"], 1))
|
||||
sesion.add_all(EegMetrica(met_id=i, met_eeg_id=e, met_codigo=c, met_valor=v, met_z=z, met_fuera=fuera, met_p5=p5,
|
||||
met_mediana=mediana, met_p95=p95)
|
||||
for i, e, c, v, (z, fuera, p5, mediana, p95) in METRICAS)
|
||||
sesion.add_all(EegMetricaElectrodo(mel_id=i, mel_met_id=m, mel_electrodo=e, mel_valor=v, mel_z=z)
|
||||
for i, m, e, v, z in ELECTRODOS)
|
||||
|
||||
Reference in New Issue
Block a user