141 lines
8.3 KiB
Python
141 lines
8.3 KiB
Python
"""CaMa en la plata de dos tablas (ADR-MD-007), por staging directo (T-20c, ADR-MD-009).
|
|
|
|
`DESDE_STAGING`: qué procedimientos salen de cada fila de staging, con la normalización como columnas
|
|
derivadas. La lee `plata_desde_staging` (`plata.carga.carga_desde_staging`).
|
|
"""
|
|
|
|
from core.cama.models import BiopsiaMama, Mastografia, NotaMedica, OrdenesEstudio, Paciente, SeguimientoMama, TratamientoMama
|
|
from core.cama.nota_medica.enums import ESPECIALIDAD_ALIAS, Especialidad
|
|
|
|
from webapi.orchestration.silver.extracted import ConEstructurado, DesdeTexto
|
|
from webapi.orchestration.silver.orders_and_notes import nota_medica, orden_de_estudio
|
|
from webapi.orchestration.silver.procedures import DeStaging, Hija, Padre
|
|
|
|
PACIENTES = Paciente # de donde salen las personas
|
|
ESPECIALIDADES = {e.value for e in Especialidad}
|
|
# La toma de un resultado: la mastografía de su fila (BI-RADS) o la que la originó (`mast_id` de la biopsia).
|
|
LA_MASTOGRAFIA_DE_SU_FILA = Padre(estudio_id="mastografia", clase="toma", fuente_id="mast_id", tabla=Mastografia.__tablename__)
|
|
LA_ORDEN_DEL_ESTUDIO = Padre(por_numero_de_orden="ord_numero_hospital")
|
|
|
|
# La biopsia (T-22h, I-MD23): solicitada, es una `orden` del estudio `biopsia` que cuelga de la toma que la
|
|
# originó; su reporte llega después como hija `reporte`, sin reescribir la orden. Pasa de staging solo lo
|
|
# realizado: la que trae fecha de solicitud o de reporte. Con reporte y sin solicitud, la orden queda sin fecha.
|
|
def _biopsia_que_pasa(fila) -> bool:
|
|
return fila.bx_fecha_solicitud is not None or fila.bx_fecha_reporte is not None
|
|
|
|
|
|
# Results read from text, with how the catalog says each is printed (`etiquetas_fuente`, `alias`): the
|
|
# radiologist's interpretation, the pathology report's text layer, and the note's diagnosis and prognosis.
|
|
DE_LA_INTERPRETACION = DesdeTexto(
|
|
"cama", ("mastografia.densidad", "mastografia.tamano_lesion_mm"), texto=lambda f: f.mast_interpretacion,
|
|
)
|
|
DEL_REPORTE_DE_PATOLOGIA = DesdeTexto(
|
|
"cama",
|
|
("biopsia.tipo_histologico", "biopsia.grado_nottingham", "biopsia.receptor_estrogeno",
|
|
"biopsia.receptor_estrogeno_pct", "biopsia.receptor_progesterona", "biopsia.receptor_progesterona_pct",
|
|
"biopsia.her2", "biopsia.ki67_pct", "biopsia.tamano_tumor_mm"),
|
|
texto=lambda f: f.bx_pdf_blob,
|
|
)
|
|
DE_LA_MASTOGRAFIA = ConEstructurado(DE_LA_INTERPRETACION, {"mastografia.densidad": "mast_densidad"})
|
|
# A mammogram's findings, each a part of its report.
|
|
LOS_HALLAZGOS = (
|
|
Hija("mastografia", "parte", solo_si=("mast_margen_masa",), fecha="mast_fecha_reporte",
|
|
resultados={"mast_margen_masa": "mastografia.margen_masa"}),
|
|
Hija("mastografia", "parte", solo_si=("mast_tipo_hallazgo",), fecha="mast_fecha_reporte",
|
|
resultados={"mast_tipo_hallazgo": "mastografia.tipo_hallazgo"}),
|
|
)
|
|
DE_LA_NOTA = DesdeTexto(
|
|
"cama", ("nota_medica.estadio_clinico",),
|
|
texto=lambda f: "\n".join(t for t in (f.nm_diagnostico_texto, f.nm_pronostico) if t),
|
|
)
|
|
|
|
# What a biopsy report and a note carry structured wins over the text's reading; the rest is read from the text.
|
|
DE_LA_PATOLOGIA = ConEstructurado(DEL_REPORTE_DE_PATOLOGIA, {
|
|
"biopsia.tipo_histologico": "bx_tipo_histologico", "biopsia.grado_nottingham": "bx_grado_nottingham",
|
|
"biopsia.receptor_estrogeno": "bx_receptor_estrogeno", "biopsia.receptor_estrogeno_pct": "bx_receptor_estrogeno_pct",
|
|
"biopsia.receptor_progesterona": "bx_receptor_progesterona",
|
|
"biopsia.receptor_progesterona_pct": "bx_receptor_progesterona_pct", "biopsia.her2": "bx_her2",
|
|
"biopsia.ki67_pct": "bx_ki67_pct", "biopsia.her2_fish": "bx_her2_fish",
|
|
"biopsia.tamano_tumor_mm": "bx_tamano_tumor_mm", "biopsia.ganglios_positivos": "bx_ganglios_positivos",
|
|
"biopsia.invasion_linfovascular": "bx_invasion_linfovascular", "biopsia.multifocalidad": "bx_multifocalidad",
|
|
"biopsia.margenes": "bx_margenes", "biopsia.pt": "bx_pt", "biopsia.pn": "bx_pn",
|
|
})
|
|
DE_LA_NOTA_ESTRUCTURADA = ConEstructurado(DE_LA_NOTA, {
|
|
"nota_medica.estadio_clinico": "nm_estadio_clinico", "nota_medica.m": "nm_metastasis",
|
|
})
|
|
# The biopsy's sample: the breast and the site it was taken from, dated by its request or, without one, its report.
|
|
LA_TOMA_DE_LA_BIOPSIA = Hija(
|
|
"biopsia", "toma", solo_si=("bx_lateralidad", "bx_localizacion"), fecha="bx_fecha_toma",
|
|
resultados={"bx_lateralidad": "biopsia.lateralidad", "bx_localizacion": "biopsia.localizacion"},
|
|
)
|
|
|
|
DESDE_STAGING = (
|
|
DeStaging(
|
|
Mastografia, "mast_id", "toma", estudio_id="mastografia", fecha="mast_fecha_toma",
|
|
padres=(LA_ORDEN_DEL_ESTUDIO,), columnas={"orden_numero": "ord_numero_hospital"},
|
|
resultados={"mast_calidad_imagen": "mastografia.calidad_imagen"},
|
|
),
|
|
# Un resultado sin fecha de reporte todavía no es un resultado.
|
|
DeStaging(
|
|
Mastografia, "mast_id", "reporte", estudio_id="mastografia", fecha="mast_fecha_reporte",
|
|
solo_si=lambda f: f.mast_fecha_reporte is not None, padres=(LA_MASTOGRAFIA_DE_SU_FILA,),
|
|
derivadas=DE_LA_MASTOGRAFIA.derivadas,
|
|
resultados={"mast_birads": "mastografia.birads", **DE_LA_MASTOGRAFIA.resultados},
|
|
fuentes=DE_LA_MASTOGRAFIA.fuentes, hijas=LOS_HALLAZGOS,
|
|
),
|
|
DeStaging(
|
|
BiopsiaMama, "bx_id", "orden", estudio_id="biopsia", fecha="bx_fecha_solicitud",
|
|
solo_si=_biopsia_que_pasa, padres=(LA_MASTOGRAFIA_DE_SU_FILA,), columnas={"orden_numero": "ord_numero_hospital"},
|
|
derivadas={**DE_LA_PATOLOGIA.derivadas,
|
|
"bx_fecha_toma": lambda f: f.bx_fecha_solicitud or f.bx_fecha_reporte},
|
|
fuentes=DE_LA_PATOLOGIA.fuentes,
|
|
hijas=(Hija("biopsia", "reporte", solo_si=("bx_fecha_reporte",), fecha="bx_fecha_reporte",
|
|
resultados={"bx_grado_histologico": "biopsia.grado", **DE_LA_PATOLOGIA.resultados}),
|
|
LA_TOMA_DE_LA_BIOPSIA),
|
|
),
|
|
orden_de_estudio(OrdenesEstudio, ESPECIALIDAD_ALIAS, ESPECIALIDADES),
|
|
nota_medica(NotaMedica, ESPECIALIDAD_ALIAS, ESPECIALIDADES, extraidos=DE_LA_NOTA_ESTRUCTURADA),
|
|
# Facts of the patient with no date of their own: dated at her birth, known as of the load.
|
|
DeStaging(
|
|
Paciente, "pac_id", "parte", estudio_id="sociodemografico", fecha="pac_fecha_nacimiento",
|
|
resultados={"pac_estado_civil": "sociodemografico.estado_civil", "pac_raza": "sociodemografico.raza",
|
|
"pac_estado_menopausico": "sociodemografico.estado_menopausico",
|
|
"pac_fuente": "sociodemografico.fuente"},
|
|
),
|
|
DeStaging(
|
|
Paciente, "pac_id", "reporte", estudio_id="prueba_genetica", fecha="pac_fecha_nacimiento",
|
|
solo_si=lambda f: f.pac_brca1 is not None or f.pac_brca2 is not None,
|
|
resultados={"pac_brca1": "prueba_genetica.brca1", "pac_brca2": "prueba_genetica.brca2"},
|
|
),
|
|
*(
|
|
DeStaging(
|
|
TratamientoMama, "trat_id", "encuentro", estudio_id=estudio, fecha="trat_fecha",
|
|
solo_si=lambda f, e=estudio: f.trat_tipo == e,
|
|
resultados={"trat_detalle": resultado, **({"trat_lateralidad": "cirugia.lateralidad"} if estudio == "cirugia" else {})},
|
|
)
|
|
for estudio, resultado in (
|
|
("cirugia", "cirugia.tipo_cirugia"), ("radioterapia", "radioterapia.intencion"),
|
|
("quimioterapia", "quimioterapia.intencion"), ("hormonoterapia", "hormonoterapia.intencion"),
|
|
("anti_her2", "anti_her2.intencion"),
|
|
)
|
|
),
|
|
*(
|
|
DeStaging(
|
|
SeguimientoMama, "seg_id", "encuentro", estudio_id=estudio, fecha="seg_fecha",
|
|
solo_si=lambda f, e=estudio: f.seg_tipo == e, resultados={"seg_detalle": resultado},
|
|
)
|
|
for estudio, resultado in (("recurrencia", "recurrencia.sitio"), ("ultimo_contacto", "ultimo_contacto.estado"))
|
|
),
|
|
DeStaging(
|
|
Paciente, "pac_id", "parte", estudio_id="perfil_molecular", fecha="pac_fecha_nacimiento",
|
|
solo_si=lambda f: any(getattr(f, c) is not None for c in (
|
|
"pac_pam50", "pac_subtipo_clinico", "pac_cluster_integrativo", "pac_celularidad", "pac_carga_mutacional",
|
|
"pac_ki67_categoria")),
|
|
resultados={"pac_pam50": "perfil_molecular.pam50", "pac_subtipo_clinico": "perfil_molecular.subtipo_clinico",
|
|
"pac_cluster_integrativo": "perfil_molecular.cluster_integrativo",
|
|
"pac_celularidad": "perfil_molecular.celularidad",
|
|
"pac_carga_mutacional": "perfil_molecular.carga_mutacional",
|
|
"pac_ki67_categoria": "perfil_molecular.ki67_categoria"},
|
|
),
|
|
)
|