153 lines
6.6 KiB
Python
153 lines
6.6 KiB
Python
"""TEA in the two-table silver: what each staging row gives, as `DESDE_STAGING` (read by `carga_desde_staging`).
|
|
|
|
- an order is an `orden` of the study it asks for (`ord_tipo_estudio`, in Core's code or one of its aliases);
|
|
- the M-CHAT is a `toma`, the ADOS-2 and the neuropediatrics note are `encuentro`s, the EEG is a `toma`; each
|
|
hangs from its order when it has one;
|
|
- what the EEG analysis writes: the EEG's verdict and study-wide quality checks (`eeg_criterio` without an
|
|
electrode) and each metric with its deviation from the age baseline are results of the EEG's recording, not
|
|
procedures; a bad channel (`eeg_criterio` with an electrode) and a metric's value at an electrode are `parte`s of
|
|
the EEG, `codigo_fuente` the electrode. The metric rows find their EEG through the metric.
|
|
"""
|
|
|
|
from core.tea.enums import (
|
|
CLASIFICACION_ADOS2_ALIAS,
|
|
TIPO_ESTUDIO_ALIAS,
|
|
ClasificacionAdos2,
|
|
ModuloAdos2,
|
|
TipoEstudio,
|
|
)
|
|
from core.tea.models import (
|
|
ADOS2_ITEMS,
|
|
Ados2,
|
|
Eeg,
|
|
EegCriterio,
|
|
EegMetrica,
|
|
EegMetricaElectrodo,
|
|
EvaluacionNeuropediatrica,
|
|
Mchat,
|
|
OrdenesEstudio,
|
|
Pacientes,
|
|
)
|
|
|
|
from webapi.orchestration.silver.procedures import DeStaging, Junta, Padre
|
|
from webapi.orchestration.silver.values import INTENCION, cie, codigo_de_especialidad, texto_limpio
|
|
|
|
PACIENTES = Pacientes # where persons come from
|
|
ORDENES = OrdenesEstudio.__tablename__
|
|
EEG = Eeg.__tablename__
|
|
|
|
|
|
def _codigo(columna: str, enum, alias: dict | None = None):
|
|
codigos = {e.value for e in enum}
|
|
return lambda f: codigo_de_especialidad(getattr(f, columna), alias or {}, codigos)
|
|
|
|
|
|
def _su_orden(estudio_id: str, columna: str) -> Padre:
|
|
"""The order a study row names by its `*_ord_id`."""
|
|
return Padre(estudio_id=estudio_id, clase="orden", fuente_id=columna, tabla=ORDENES)
|
|
|
|
|
|
def _metrica(f) -> str | None:
|
|
"""A metric's result in the catalog: its code in lower case (`RITMO_POSTERIOR` -> `ritmo_posterior`)."""
|
|
codigo = texto_limpio(f.met_codigo)
|
|
return None if codigo is None else codigo.lower()
|
|
|
|
|
|
def _criterio(f) -> str | None:
|
|
"""A quality check's results in the catalog: `calidad_` and its code in snake case (`clean-windows` ->
|
|
`calidad_clean_windows`)."""
|
|
codigo = texto_limpio(f.cri_codigo)
|
|
return None if codigo is None else "calidad_" + codigo.lower().replace("-", "_")
|
|
|
|
|
|
def _valor_de_criterio(f):
|
|
return f.cri_valor if f.cri_valor is not None else texto_limpio(f.cri_valor_texto)
|
|
|
|
|
|
SU_EEG = Padre(estudio_id="eeg", clase="toma", fuente_id="met_eeg_id", tabla=EEG)
|
|
EEG_DEL_CRITERIO = Padre(estudio_id="eeg", clase="toma", fuente_id="cri_eeg_id", tabla=EEG)
|
|
DEVIATION = ("z", "fuera", "p5", "mediana", "p95")
|
|
|
|
|
|
DESDE_STAGING = (
|
|
DeStaging(
|
|
OrdenesEstudio, "ord_id", "orden", estudio_de="tipo_estudio", fecha="ord_fecha", paciente="ord_pac_id",
|
|
derivadas={
|
|
"tipo_estudio": _codigo("ord_tipo_estudio", TipoEstudio, TIPO_ESTUDIO_ALIAS),
|
|
"intencion": lambda f: INTENCION,
|
|
},
|
|
columnas={"id_hospital": "ord_id_hospital", "codigo_fuente": "ord_tipo_estudio"},
|
|
resultados={"intencion": "orden.intencion"},
|
|
),
|
|
DeStaging(
|
|
Mchat, "mch_id", "toma", estudio_id="mchat", fecha="mch_fecha", paciente="mch_pac_id",
|
|
padres=(_su_orden("mchat", "mch_ord_id"),),
|
|
resultados={"mch_puntaje": "mchat.puntaje"},
|
|
),
|
|
DeStaging(
|
|
Ados2, "ado_id", "encuentro", estudio_id="ados2", fecha="ado_fecha", paciente="ado_pac_id",
|
|
padres=(_su_orden("ados2", "ado_ord_id"),),
|
|
derivadas={
|
|
"modulo": _codigo("ado_modulo", ModuloAdos2),
|
|
"clasificacion": _codigo("ado_clasificacion", ClasificacionAdos2, CLASIFICACION_ADOS2_ALIAS),
|
|
"algoritmo": lambda f: texto_limpio(f.ado_algoritmo),
|
|
},
|
|
resultados={
|
|
"modulo": "ados2.modulo",
|
|
"algoritmo": "ados2.algoritmo",
|
|
"clasificacion": "ados2.clasificacion",
|
|
"ado_css": "ados2.css",
|
|
**{f"ado_{item}": f"ados2.{item}" for item in ADOS2_ITEMS},
|
|
},
|
|
fuentes={"modulo": "ado_modulo", "clasificacion": "ado_clasificacion"},
|
|
),
|
|
DeStaging(
|
|
EvaluacionNeuropediatrica, "neu_id", "encuentro", estudio_id="evaluacion_neuropediatrica",
|
|
fecha="neu_fecha", paciente="neu_pac_id", padres=(_su_orden("evaluacion_neuropediatrica", "neu_ord_id"),),
|
|
derivadas={"cie_codigo": lambda f: cie(f.neu_cie_codigo)},
|
|
resultados={"cie_codigo": "evaluacion_neuropediatrica.diagnostico"},
|
|
),
|
|
DeStaging(
|
|
Eeg, "eeg_id", "toma", estudio_id="eeg", fecha="eeg_fecha_estudio", paciente="eeg_pac_id",
|
|
padres=(_su_orden("eeg", "eeg_ord_id"),), columnas={"id_hospital": "eeg_id_registro_laboratorio"},
|
|
resultados={
|
|
"eeg_valido": "eeg.valido",
|
|
"eeg_nivel": "eeg.nivel",
|
|
"eeg_fraccion_limpia": "eeg.fraccion_limpia",
|
|
"eeg_canales_malos": "eeg.canales_malos",
|
|
"eeg_edad_meses": "eeg.edad_meses",
|
|
"eeg_con_sueno": "eeg.con_sueno",
|
|
"eeg_canales": "eeg.canales",
|
|
"eeg_duracion_min": "eeg.duracion_min",
|
|
"eeg_ruido_linea_db": "eeg.ruido_linea",
|
|
},
|
|
),
|
|
DeStaging(
|
|
EegCriterio, "cri_id", "toma", estudio_id="eeg", paciente=None, observa_en=EEG_DEL_CRITERIO,
|
|
solo_si=lambda f: f.cri_electrodo is None,
|
|
derivadas={"criterio": _criterio, "valor": _valor_de_criterio},
|
|
resultados={"valor": "eeg.{criterio}", "cri_anormal": "eeg.{criterio}_anormal",
|
|
"cri_decisivo": "eeg.{criterio}_decisivo", "cri_bajo": "eeg.{criterio}_bajo",
|
|
"cri_alto": "eeg.{criterio}_alto"},
|
|
),
|
|
DeStaging(
|
|
EegCriterio, "cri_id", "parte", estudio_id="eeg", paciente=None, padres=(EEG_DEL_CRITERIO,),
|
|
solo_si=lambda f: f.cri_electrodo is not None,
|
|
derivadas={"criterio": _criterio, "valor": _valor_de_criterio},
|
|
columnas={"codigo_fuente": "cri_electrodo"},
|
|
resultados={"valor": "eeg.{criterio}"},
|
|
),
|
|
DeStaging(
|
|
EegMetrica, "met_id", "toma", estudio_id="eeg", paciente=None, observa_en=SU_EEG,
|
|
derivadas={"metrica": _metrica},
|
|
resultados={"met_valor": "eeg.{metrica}", **{f"met_{d}": f"eeg.{{metrica}}_{d}" for d in DEVIATION}},
|
|
),
|
|
DeStaging(
|
|
EegMetricaElectrodo, "mel_id", "parte", estudio_id="eeg", paciente=None,
|
|
juntas=(Junta(EegMetrica, "met_id", "mel_met_id"),), padres=(SU_EEG,),
|
|
derivadas={"metrica": _metrica},
|
|
columnas={"codigo_fuente": "mel_electrodo"},
|
|
resultados={"mel_valor": "eeg.{metrica}", "mel_z": "eeg.{metrica}_z"},
|
|
),
|
|
)
|