Medir en CaMa el tiempo de diagnóstico tras biopsia a tratamiento (provisional)

This commit is contained in:
2026-12-04 01:23:41 -06:00
parent b8367dfc8b
commit 96b517d826
7 changed files with 111 additions and 2 deletions
+3 -1
View File
@@ -34,7 +34,9 @@ DIAGNOSIS_ICD10 = [
# Measures (core.measures): the horizons, in days; an interval that hasn't ended after its
# horizon counts as not ended. A diagnosis this soon after the mammogram resolves it.
HORIZONS = {"sample_to_report": 60, "to_diagnosis": 180}
# Provisional: both 60-day horizons require clinical validation.
HORIZONS = {"sample_to_report": 60, "to_diagnosis": 180,
"biopsy_to_diagnosis": 60, "diagnosis_to_treatment": 60}
AGE_GROUPS = [(40, 49), (50, 59), (60, 69)] # plus "<40" and "70+"
# The follow-up list: patients whose latest mammogram, taken within the window before the
+12
View File
@@ -137,6 +137,18 @@ MEASURES = {m.id: m for m in [
H["to_diagnosis"], units=NO_HISTORY, unit="mammogram_id",
date="report_date"),
"decrease", known="report_known_at"),
Measure("CAMA_DIAS_DIAGNOSTICO_TRATAMIENTO",
"Tiempo de diagnóstico tras biopsia a tratamiento", "diagnosis_treatment",
Duration("days_diagnosis_treatment", "diagnosis_treatment_date", "biopsy_diagnosis_date",
H["diagnosis_to_treatment"], units=lambda r: NO_HISTORY(r) and r["biopsy_diagnosis_date"] is not None,
unit="mammogram_id", date="exam_date"), "decrease",
facts={"biopsy_date_known_at": ("biopsy_diagnosis_date", "diagnosis_treatment_date", "days_diagnosis_treatment"),
"biopsy_diagnosis_known_at": ("biopsy_diagnosis_date", "diagnosis_treatment_date", "days_diagnosis_treatment"),
"diagnosis_treatment_known_at": ("diagnosis_treatment_date", "days_diagnosis_treatment")},
dated={"biopsy_date": ("biopsy_diagnosis_date", "diagnosis_treatment_date", "days_diagnosis_treatment"),
"biopsy_diagnosis_date": ("diagnosis_treatment_date", "days_diagnosis_treatment")},
description="PROVISIONAL: horizontes de 60 días pendientes de validación clínica; "
"diagnóstico posterior a biopsia vinculada y primer tratamiento, incluido el mismo día."),
# Pending the analysts' validation (LOTE-3-CAMA-REBUILD.md): the biopsy is the one whose
# request names the mammogram, the first reported if several; the horizon is the
# diagnosis's, as no other is set.
+14
View File
@@ -3,6 +3,20 @@ version: 2
models:
- name: cama_mammograms
columns:
- name: biopsy_date
description: Primer reporte válido de biopsia vinculada a la mastografía.
- name: biopsy_date_known_at
description: Disponibilidad del reporte seleccionado.
- name: biopsy_diagnosis_date
description: Primer diagnóstico posterior al reporte dentro de la ventana provisional.
- name: biopsy_diagnosis_known_at
description: Disponibilidad del diagnóstico seleccionado.
- name: diagnosis_treatment_date
description: Primer tratamiento desde el diagnóstico, incluido el mismo día, dentro de la ventana provisional.
- name: diagnosis_treatment_known_at
description: Disponibilidad del tratamiento seleccionado.
- name: days_diagnosis_treatment
description: Días entre diagnóstico y tratamiento; nulo si falta alguno.
- name: mammogram_id
data_tests: [unique, not_null]
- name: cama_biopsies
@@ -21,6 +21,29 @@ events as (select * from {{ ref('cama_events') }}),
{{ first_after('diagnosis', 's.exam_date', 'diagnosis', p.horizons.to_diagnosis, key='mammogram_id') }},
{{ first_after('report_diagnosis', 's.report_date', 'diagnosis', p.horizons.to_diagnosis, strict=false, key='mammogram_id') }},
biopsy_dates as (
select s.mammogram_id, min(b.report_date) as date
from sample as s join {{ ref('breast_biopsy') }} as b
on b.person_id = s.person_id and b.mammogram_exam_date = s.exam_date
where b.report_date >= s.exam_date group by s.mammogram_id
),
biopsy as (
select s.mammogram_id, s.person_id, b.date, min(x.report_known_at) as known_at
from biopsy_dates as b join sample as s on s.mammogram_id = b.mammogram_id
join {{ ref('breast_biopsy') }} as x on x.person_id = s.person_id
and x.mammogram_exam_date = s.exam_date and x.report_date = b.date
group by s.mammogram_id, s.person_id, b.date
),
{{ first_after('biopsy_diagnosis', 's.date', 'diagnosis', p.horizons.biopsy_to_diagnosis, units='biopsy', key='mammogram_id') }},
diagnosis_units as (
select s.person_id, d.mammogram_id, d.date
from biopsy_diagnosis as d join sample as s on s.mammogram_id = d.mammogram_id
),
{{ first_after('diagnosis_treatment', 's.date', 'treatment', p.horizons.diagnosis_to_treatment, strict=false, units='diagnosis_units', key='mammogram_id') }},
event_known as (
select person_id, type, date, min(known_at) as known_at
from events where type in ('diagnosis', 'treatment') group by person_id, type, date
),
known as (
select person_id, date, min(known_at) as known_at
from events where type = 'diagnosis' group by person_id, date
@@ -45,9 +68,19 @@ select
{{ days_between('s.exam_date', 'd.date') }} as days_sample_diagnosis,
r.date as report_diagnosis_date,
{{ days_between('s.report_date', 'r.date') }} as days_report_diagnosis,
b.date as biopsy_date, b.known_at as biopsy_date_known_at,
bd.date as biopsy_diagnosis_date, bdk.known_at as biopsy_diagnosis_known_at,
dt.date as diagnosis_treatment_date, dtk.known_at as diagnosis_treatment_known_at,
{{ days_between('bd.date', 'dt.date') }} as days_diagnosis_treatment,
case when h.first_history < s.exam_date then 1 else 0 end as history
from sample as s
left join diagnosis as d on d.mammogram_id = s.mammogram_id
left join known as kd on kd.person_id = s.person_id and kd.date = d.date
left join report_diagnosis as r on r.mammogram_id = s.mammogram_id
left join history as h on h.person_id = s.person_id
left join biopsy as b on b.mammogram_id = s.mammogram_id
left join biopsy_diagnosis as bd on bd.mammogram_id = s.mammogram_id
left join diagnosis_treatment as dt on dt.mammogram_id = s.mammogram_id
left join event_known as bdk on bdk.person_id = s.person_id and bdk.type = 'diagnosis' and bdk.date = bd.date
left join event_known as dtk on dtk.person_id = s.person_id and dtk.type = 'treatment' and dtk.date = dt.date
+1
View File
@@ -35,6 +35,7 @@ READS = {
},
"cama": {
"mammograms": "select * from gold.cama_mammograms",
"diagnosis_treatment": "select * from gold.cama_mammograms",
"biopsies": "select * from gold.cama_biopsies where report_date is not null",
# An event names a biopsy by person and request date. If several biopsies share that
# key, or several matching type events exist, the event cannot be assigned safely.
+1 -1
Submodule WebApi updated: 033c0678ef...37c6f242f9
+47
View File
@@ -0,0 +1,47 @@
from datetime import date, datetime, timezone
from core.cama.measures import MEASURES
from core.measures import Period, evaluate
from persistance import destination, silver
def test_gold_and_historical_measure(tmp_path, monkeypatch):
monkeypatch.setenv('ISSSTELEON_DATA', str(tmp_path))
monkeypatch.setenv('PLATFORM_DATABASE_URL', f'sqlite:///{tmp_path / "platform.db"}')
monkeypatch.setenv('DBT_TARGET', 'sqlite')
monkeypatch.setattr(destination, 'DATA_DIR', tmp_path)
destination.engine.cache_clear()
engine = destination.engine()
try:
with engine.begin() as con:
con.exec_driver_sql('create table silver.person (person_id text, birth_date date)')
con.exec_driver_sql('create table silver.mammography (person_id text, exam_date date, report_date date, birads text, adequacy text, sample_known_at text, report_known_at text)')
con.exec_driver_sql('create table silver.breast_biopsy (person_id text, mammogram_exam_date date, report_date date, report_known_at text)')
con.exec_driver_sql('create table gold.cama_events (person_id text, date date, type text, known_at text)')
for name in ('same', 'none', 'multiple', 'boundary', 'txboundary', 'earlier'):
con.exec_driver_sql('insert into silver.mammography values (?, ?, ?, ?, ?, ?, ?)', (name, '2026-01-01', '2026-01-02', '4', None, '2026-01-01', '2026-01-02'))
for name in ('same', 'multiple', 'boundary', 'txboundary', 'earlier'):
con.exec_driver_sql('insert into silver.breast_biopsy values (?, ?, ?, ?)', (name, '2026-01-01', '2026-01-10', '2026-01-12'))
events = [('same','2026-01-15','diagnosis','2026-01-20'), ('same','2026-01-14','treatment','2026-01-14'), ('same','2026-01-15','treatment','2026-01-22'), ('multiple','2026-01-20','diagnosis','2026-01-20'), ('multiple','2026-01-18','diagnosis','2026-01-18'), ('multiple','2026-01-21','treatment','2026-01-21'), ('multiple','2026-01-23','treatment','2026-01-23'), ('boundary','2026-03-11','diagnosis','2026-03-11'), ('txboundary','2026-01-11','diagnosis','2026-01-11'), ('txboundary','2026-03-12','treatment','2026-03-12'), ('earlier','2026-01-20','diagnosis','2026-01-20'), ('earlier','2026-01-19','treatment','2026-01-19')]
for event in events:
con.exec_driver_sql('insert into gold.cama_events values (?, ?, ?, ?)', event)
silver.build(['cama_mammograms'], as_of=date(2026, 12, 31))
with engine.connect() as con:
rows = {r['person_id']: dict(r) for r in con.exec_driver_sql('select * from gold.cama_mammograms').mappings()}
assert rows['none']['biopsy_diagnosis_date'] is None
assert rows['boundary']['biopsy_diagnosis_date'] is None # 60 days excluded
assert rows['txboundary']['diagnosis_treatment_date'] is None # 60 days excluded
assert rows['earlier']['diagnosis_treatment_date'] is None
assert rows['same']['days_diagnosis_treatment'] == 0
assert rows['multiple']['days_diagnosis_treatment'] == 3
measure = MEASURES['CAMA_DIAS_DIAGNOSTICO_TRATAMIENTO']
period = Period.parse('2026-S1')
result = evaluate(measure, list(rows.values()), period, event_cutoff=date(2026, 12, 31))
assert result.populations['measure-observation'] == 2
early = evaluate(measure, [rows['same']], period, as_of=datetime(2026, 1, 21, tzinfo=timezone.utc))
assert early.populations['measure-observation'] == 0
cutoff = evaluate(measure, [rows['same']], period, event_cutoff=date(2026, 1, 14))
assert cutoff.populations['measure-observation'] == 0
finally:
engine.dispose()
destination.engine.cache_clear()