161 lines
7.9 KiB
Python
161 lines
7.9 KiB
Python
"""TEA's synthetic staging, written by hand. Never real data.
|
|
|
|
Seven children screened in 2025, each one exercising a rule of the screening's chain (M-CHAT, psychology note
|
|
with the ADOS-2, neuropediatrics note, good-quality EEG). Ages are at the first M-CHAT, in completed months:
|
|
|
|
- 1, H-T01, 19 months (16 to 23): the whole chain. M-CHAT 10 Jan, ADOS-2 5 Feb, F84.0 note 12 Mar, a bad EEG
|
|
2 Apr, a good EEG 20 May with a slow posterior rhythm, then a second good EEG 1 Sep that changes nothing. Leaves
|
|
after May. Its EEG has an order.
|
|
- 2, H-T02, 26 months (24 to 29): M-CHAT 20 Jan, ADOS-2 1 Mar, a note with F84 without subcode 10 Apr (not a
|
|
confirmation), no EEG: stays at neuropediatrics.
|
|
- 3, H-T03, 35 months (30 to 36): M-CHAT 14 Feb (a second one 10 Jun doesn't count), ADOS-2 28 Feb, nothing else:
|
|
stays at psychology.
|
|
- 4, H-T04, 39 months: out of the cohort, never counted.
|
|
- 5, H-T05, 21 months: M-CHAT 15 Jun, a F84.0 note 1 Jul and a good EEG 20 Jul without the ADOS-2: steps go in
|
|
order, so it stays at M-CHAT and in process.
|
|
- 6, H-T06, 26 months: M-CHAT 5 Apr, ADOS-2 20 Apr, a good EEG 1 Jun before its F84.0 note of 2 Jun: the EEG
|
|
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 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.
|
|
"""
|
|
|
|
from datetime import date
|
|
|
|
from core.tea.models import (
|
|
Ados2,
|
|
Eeg,
|
|
EegCriterio,
|
|
EegMetrica,
|
|
EegMetricaElectrodo,
|
|
EvaluacionNeuropediatrica,
|
|
Mchat,
|
|
OrdenesEstudio,
|
|
Pacientes,
|
|
)
|
|
from sqlmodel import Session
|
|
|
|
# (pac_id, pac_id_hospital, birth date)
|
|
PACIENTES = (
|
|
(1, "H-T01", date(2023, 6, 1)),
|
|
(2, "H-T02", date(2022, 11, 15)),
|
|
(3, "H-T03", date(2022, 3, 1)),
|
|
(4, "H-T04", date(2021, 12, 1)),
|
|
(5, "H-T05", date(2023, 9, 1)),
|
|
(6, "H-T06", date(2023, 1, 10)),
|
|
(7, "H-T07", None),
|
|
)
|
|
|
|
# (pac_id, date, score)
|
|
MCHATS = (
|
|
(1, date(2025, 1, 10), 12),
|
|
(2, date(2025, 1, 20), 9),
|
|
(3, date(2025, 2, 14), 15),
|
|
(3, date(2025, 6, 10), 4),
|
|
(4, date(2025, 3, 3), 11),
|
|
(5, date(2025, 6, 15), 8),
|
|
(6, date(2025, 4, 5), 14),
|
|
(7, date(2025, 5, 1), 7),
|
|
)
|
|
|
|
# (pac_id, date, module, CSS, classification)
|
|
ADOS2 = (
|
|
(1, date(2025, 2, 5), "MODULO_1", 7, "AUTISMO"),
|
|
(2, date(2025, 3, 1), "Toddler", 4, "espectro autista"),
|
|
(3, date(2025, 2, 28), "MODULO_2", 2, "NO_TEA"),
|
|
(6, date(2025, 4, 20), "MODULO_1", 8, "AUTISMO"),
|
|
)
|
|
|
|
# (pac_id, date, ICD-10 as the hospital writes it)
|
|
NOTAS = (
|
|
(1, date(2025, 3, 12), "F84.0"),
|
|
(2, date(2025, 4, 10), "F84"),
|
|
(5, date(2025, 7, 1), "f84.0"),
|
|
(6, date(2025, 6, 2), "F84.0"),
|
|
)
|
|
|
|
# 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, 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),
|
|
("eeg-1c", 1, date(2025, 9, 1), "LAB-1C", GOOD, 1),
|
|
("eeg-5", 5, date(2025, 7, 20), "LAB-5", GOOD, 0),
|
|
("eeg-6", 6, date(2025, 6, 1), "LAB-6", GOOD, 1),
|
|
)
|
|
|
|
# (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, (-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, value, z-score)
|
|
ELECTRODOS = (
|
|
("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`): 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:
|
|
"""Writes the children, their orders and their studies."""
|
|
sesion.add_all(Pacientes(pac_id=p, pac_id_hospital=h, pac_fecha_nacimiento=n) for p, h, n in PACIENTES)
|
|
sesion.add_all(OrdenesEstudio(ord_id=i, ord_pac_id=p, ord_id_hospital=h, ord_tipo_estudio=t, ord_fecha=f)
|
|
for i, p, h, t, f in ORDENES)
|
|
sesion.add_all(Mchat(mch_id=f"mch-{k}", mch_pac_id=p, mch_fecha=f, mch_puntaje=s)
|
|
for k, (p, f, s) in enumerate(MCHATS, 1))
|
|
for k, (p, f, modulo, css, clasificacion) in enumerate(ADOS2, 1):
|
|
items = {f"ado_sa_b{n}": n % 3 for n in range(1, 6)} | {"ado_rrb_d1": 2, "ado_rrb_d2": 1}
|
|
sesion.add(Ados2(ado_id=f"ado-{k}", ado_pac_id=p, ado_fecha=f, ado_modulo=modulo, ado_css=css,
|
|
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, 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_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(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)
|