121 lines
4.9 KiB
Python
121 lines
4.9 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;
|
|
- an EEG metric is a result of its EEG's recording, not a procedure; a metric's electrode value is a `parte` of the
|
|
EEG (`codigo_fuente` the electrode) with the metric's result. Both find their EEG through the metric's row.
|
|
"""
|
|
|
|
from core.tea.enums import (
|
|
CLASIFICACION_ADOS2_ALIAS,
|
|
TIPO_ESTUDIO_ALIAS,
|
|
ClasificacionAdos2,
|
|
ModuloAdos2,
|
|
TipoEstudio,
|
|
)
|
|
from core.tea.models import (
|
|
ADOS2_ITEMS,
|
|
Ados2,
|
|
Eeg,
|
|
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()
|
|
|
|
|
|
SU_EEG = Padre(estudio_id="eeg", clase="toma", fuente_id="met_eeg_id", tabla=EEG)
|
|
|
|
|
|
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_con_sueno": "eeg.con_sueno",
|
|
"eeg_canales": "eeg.canales",
|
|
"eeg_duracion_min": "eeg.duracion_min",
|
|
"eeg_canales_amplitud": "eeg.calidad_amplitud",
|
|
"eeg_canales_alta_frecuencia": "eeg.calidad_alta_frecuencia",
|
|
"eeg_canales_correlacion": "eeg.calidad_correlacion",
|
|
"eeg_canales_sin_senal": "eeg.calidad_sin_senal",
|
|
"eeg_ruido_linea_db": "eeg.calidad_linea",
|
|
},
|
|
),
|
|
DeStaging(
|
|
EegMetrica, "met_id", "toma", estudio_id="eeg", paciente=None, observa_en=SU_EEG,
|
|
derivadas={"metrica": _metrica},
|
|
resultados={"met_valor": "eeg.{metrica}"},
|
|
),
|
|
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}"},
|
|
),
|
|
)
|