Compare commits

...

1 Commits

Author SHA1 Message Date
edgar 78b3d28008 feat(cacu): add the times from sample and from diagnosis to treatment
A macro finds the first event of a kind after each start, for the same patient, within a horizon. The diagnosis
lookup of cacu_tomas_citologia uses it with the same result, and two more calls give the days from the sample to
the first treatment and from its diagnosis to it. Both horizons are 60 days, provisional.
2026-11-21 19:35:44 -06:00
4 changed files with 161 additions and 22 deletions
+4
View File
@@ -28,6 +28,10 @@ vars:
cie_antecedente: ["D06", "D060", "D061", "D067", "D069", "C53", "C530", "C531", "C538", "C539"]
horizonte_toma_reporte_dias: 60
horizonte_toma_diagnostico_dias: 180
# Provisional: how long after the sample, and after its diagnosis, a treatment still counts for the times to
# treatment (models/cacu/gold/eventos/cacu_tomas_citologia.sql).
horizonte_toma_tratamiento_dias: 60
horizonte_diagnostico_tratamiento_dias: 60
# ASCCP follow-up (the kit's cacu.seguimiento.*; provisional): what counts as a colposcopy and a treatment
# (models/cacu/silver/cacu_procedimientos_inferidos.sql).
seguimiento:
@@ -0,0 +1,30 @@
{#
The first event of a kind after each start, for the same patient: the building block of a time between two events.
`inicio`: a relation with one row per start, with columns `id`, `pac_id` and the date `fecha`.
`linea`: the patient's timeline (`pac_id`, `fecha`, `conocida_en`, `tipo`).
`tipo`: the timeline kind that ends the interval.
`horizonte_dias`: the event must fall fewer days after the start.
`estricto`: true leaves out an event on the start's own day, false counts it.
One row per start that has such an event: `inicio_id`, `fecha_fin` (its date) and `fin_conocido_en` (when it became
known: the earliest load among the patient's events of that day). The days between are the difference of
`dim_fecha.dia_num`, plain arithmetic that every engine runs. The caller joins on `inicio_id`; a start without a
row has no event in its horizon.
#}
{% macro primer_evento_posterior(inicio, id, fecha, linea, tipo, horizonte_dias, estricto=true) -%}
select p.inicio_id, p.fecha_fin, min(l.conocida_en) as fin_conocido_en
from (
select i.{{ id }} as inicio_id, min(l.fecha) as fecha_fin
from {{ inicio }} i
join {{ linea }} l
on l.pac_id = i.pac_id and l.tipo = '{{ tipo }}' and l.fecha {{ '>' if estricto else '>=' }} i.{{ fecha }}
join {{ source('silver', 'dim_fecha') }} dl on dl.fecha = l.fecha
join {{ source('silver', 'dim_fecha') }} di on di.fecha = i.{{ fecha }}
where dl.dia_num - di.dia_num < {{ horizonte_dias }}
group by i.{{ id }}
) p
join {{ inicio }} i on i.{{ id }} = p.inicio_id
join {{ linea }} l on l.pac_id = i.pac_id and l.tipo = '{{ tipo }}' and l.fecha = p.fecha_fin
group by p.inicio_id, p.fecha_fin
{%- endmacro %}
@@ -3,27 +3,29 @@
-- was known (`toma_conocida_en`) and when its result was (`resultado_conocido_en`, null without a result). The birth
-- date is the patient's, for strata by age. From her timeline: the first note that resolves the sample (a
-- `diagnostico` after it, within `horizonte_toma_diagnostico_dias`), with its days and when it was known, and whether
-- a cervical cancer note (`antecedente`) came before the sample.
-- a cervical cancer note (`antecedente`) came before the sample. The first `tratamiento` after the sample, within
-- `horizonte_toma_tratamiento_dias`, and the first one after that diagnosis, within
-- `horizonte_diagnostico_tratamiento_dias`, each with its days and when it was known.
with resuelve as (
select c.procedimiento_id, min(l.fecha) as fecha_diagnostico
from {{ ref('cacu_citologias') }} c
join {{ ref('cacu_linea_de_tiempo') }} l
on l.pac_id = c.pac_id and l.tipo = 'diagnostico' and l.fecha > c.fecha_toma
join {{ source('silver', 'dim_fecha') }} dl on dl.fecha = l.fecha
join {{ source('silver', 'dim_fecha') }} dt on dt.fecha = c.fecha_toma
where dl.dia_num - dt.dia_num < {{ var('horizonte_toma_diagnostico_dias') }}
group by c.procedimiento_id
with diagnostico as (
{{ primer_evento_posterior(ref('cacu_citologias'), 'procedimiento_id', 'fecha_toma', ref('cacu_linea_de_tiempo'),
'diagnostico', var('horizonte_toma_diagnostico_dias')) }}
),
-- When the first resolving note became known: the earliest load among her notes of that day.
diagnostico as (
select r.procedimiento_id, r.fecha_diagnostico, min(l.conocida_en) as diagnostico_conocido_en
from resuelve r
join {{ ref('cacu_citologias') }} c on c.procedimiento_id = r.procedimiento_id
join {{ ref('cacu_linea_de_tiempo') }} l
on l.pac_id = c.pac_id and l.tipo = 'diagnostico' and l.fecha = r.fecha_diagnostico
group by r.procedimiento_id, r.fecha_diagnostico
tratamiento as (
{{ primer_evento_posterior(ref('cacu_citologias'), 'procedimiento_id', 'fecha_toma', ref('cacu_linea_de_tiempo'),
'tratamiento', var('horizonte_toma_tratamiento_dias')) }}
),
desde_diagnostico as (
select c.procedimiento_id, c.pac_id, dx.fecha_fin as fecha
from {{ ref('cacu_citologias') }} c
join diagnostico dx on dx.inicio_id = c.procedimiento_id
),
tratamiento_del_diagnostico as (
{{ primer_evento_posterior('desde_diagnostico', 'procedimiento_id', 'fecha', ref('cacu_linea_de_tiempo'),
'tratamiento', var('horizonte_diagnostico_tratamiento_dias')) }}
),
primer_antecedente as (
@@ -47,9 +49,16 @@ select
persona.fecha_nacimiento,
dia_toma.dia_num as toma_dia,
dia_reporte.dia_num - dia_toma.dia_num as dias_toma_reporte,
dx.fecha_diagnostico,
dx.diagnostico_conocido_en,
dx.fecha_fin as fecha_diagnostico,
dx.fin_conocido_en as diagnostico_conocido_en,
dia_diagnostico.dia_num - dia_toma.dia_num as dias_toma_diagnostico,
tx.fecha_fin as fecha_tratamiento,
tx.fin_conocido_en as tratamiento_conocido_en,
dia_tratamiento.dia_num - dia_toma.dia_num as dias_toma_tratamiento,
dia_diagnostico.dia_num as diagnostico_dia,
td.fecha_fin as fecha_tratamiento_diagnostico,
td.fin_conocido_en as tratamiento_diagnostico_conocido_en,
dia_tratamiento_dx.dia_num - dia_diagnostico.dia_num as dias_diagnostico_tratamiento,
case when antecedente.fecha < c.fecha_toma then 1 else 0 end as con_antecedente
from {{ ref('cacu_citologias') }} c
join {{ source('silver', 'persona') }} persona
@@ -64,8 +73,16 @@ left join {{ source('silver', 'dim_fecha') }} dia_toma
left join {{ source('silver', 'dim_fecha') }} dia_reporte
on dia_reporte.fecha = c.fecha_reporte
left join diagnostico dx
on dx.procedimiento_id = c.procedimiento_id
on dx.inicio_id = c.procedimiento_id
left join {{ source('silver', 'dim_fecha') }} dia_diagnostico
on dia_diagnostico.fecha = dx.fecha_diagnostico
on dia_diagnostico.fecha = dx.fecha_fin
left join tratamiento tx
on tx.inicio_id = c.procedimiento_id
left join {{ source('silver', 'dim_fecha') }} dia_tratamiento
on dia_tratamiento.fecha = tx.fecha_fin
left join tratamiento_del_diagnostico td
on td.inicio_id = c.procedimiento_id
left join {{ source('silver', 'dim_fecha') }} dia_tratamiento_dx
on dia_tratamiento_dx.fecha = td.fecha_fin
left join primer_antecedente antecedente
on antecedente.pac_id = c.pac_id
@@ -60,6 +60,28 @@ semantic_models:
time_granularity: day
- name: dias_toma_diagnostico
type: categorical
- name: fecha_tratamiento
type: time
type_params:
time_granularity: day
- name: tratamiento_conocido_en
type: time
type_params:
time_granularity: day
- name: dias_toma_tratamiento
type: categorical
- name: diagnostico_dia
type: categorical
- name: fecha_tratamiento_diagnostico
type: time
type_params:
time_granularity: day
- name: tratamiento_diagnostico_conocido_en
type: time
type_params:
time_granularity: day
- name: dias_diagnostico_tratamiento
type: categorical
- name: con_antecedente
type: categorical
measures:
@@ -258,3 +280,69 @@ metrics:
horizonte:
dias: "{{ var('horizonte_toma_diagnostico_dias') }}"
dia_inicio: toma_dia
- name: cacu_dias_toma_tratamiento
label: "Tiempo de tamizaje a tratamiento"
description: >-
Días naturales de la toma de cada citología del periodo al primer tratamiento posterior: una nota médica con un
CIE-10 de tratamiento después de la primera colposcopia de la paciente, dentro del horizonte y a la fecha de
corte. Entran todas las citologías, sean de tamizaje o no; se excluyen las de pacientes con un diagnóstico de
cáncer cervicouterino anterior a la toma. El tratamiento se infiere de la nota: no distingue el indicado del
realizado.
type: simple
type_params:
measure: citologias
filter: "{{ Dimension('toma__con_antecedente') }} = 0"
config:
meta:
measure_url: cacu/CACU_DIAS_TOMA_TRATAMIENTO
version: 0.1.0
scoring: continuous-variable
basis: examen
grupo: TOTAL
contrato: C3r
improvement_notation: decrease
tipos_periodo: [semestre, mes]
figuras: [fig_c3r_caja]
poblaciones_fhir: [initial-population]
conocida_en: [toma_conocida_en]
# The first treatment's days, if it was written by the event cut-off; a sample without one whose horizon
# hasn't passed by then isn't measured yet. The horizon is the one the model used to find the treatment.
observacion:
valor: dias_toma_tratamiento
fechada_en: [fecha_tratamiento]
conocida_en: [tratamiento_conocido_en]
horizonte:
dias: "{{ var('horizonte_toma_tratamiento_dias') }}"
dia_inicio: toma_dia
- name: cacu_dias_diagnostico_tratamiento
label: "Tiempo de diagnóstico a tratamiento"
description: >-
Días naturales del diagnóstico que resuelve cada citología del periodo al primer tratamiento posterior: una nota
médica con un CIE-10 de tratamiento después de la primera colposcopia de la paciente, dentro del horizonte y a la
fecha de corte. Solo entran las citologías que un diagnóstico resolvió; se excluyen las de pacientes con un
diagnóstico de cáncer cervicouterino anterior a la toma. Dos citologías que resuelve el mismo diagnóstico cuentan
cada una. El tratamiento se infiere de la nota: no distingue el indicado del realizado.
type: simple
type_params:
measure: citologias
filter: "{{ Dimension('toma__con_antecedente') }} = 0 and {{ Dimension('toma__diagnostico_dia') }} is not null"
config:
meta:
measure_url: cacu/CACU_DIAS_DIAGNOSTICO_TRATAMIENTO
version: 0.1.0
scoring: continuous-variable
basis: examen
grupo: TOTAL
contrato: C3r
improvement_notation: decrease
tipos_periodo: [semestre, mes]
figuras: [fig_c3r_caja]
poblaciones_fhir: [initial-population]
conocida_en: [toma_conocida_en, diagnostico_conocido_en]
observacion:
valor: dias_diagnostico_tratamiento
fechada_en: [fecha_tratamiento_diagnostico]
conocida_en: [tratamiento_diagnostico_conocido_en]
horizonte:
dias: "{{ var('horizonte_diagnostico_tratamiento_dias') }}"
dia_inicio: diagnostico_dia