CaMa survival cohort: no exclusions, the surgery as its procedure of interest and only the patients in post-treatment

This commit is contained in:
rockdrigo
2026-11-20 18:40:54 -06:00
parent 1937834b25
commit ddeabab640
5 changed files with 33 additions and 76 deletions
+2 -6
View File
@@ -130,17 +130,13 @@ vars:
- ["gradiente_anteroposterior", "16_23", 2.0, 3.5]
- ["gradiente_anteroposterior", "24_29", 1.75, 3.25]
- ["gradiente_anteroposterior", "30_36", 1.5, 3.0]
# CaMa's survival cohort (models/cama/cohorts/survival): the surgeries that count as a first treatment (ESCISIONAL
# includes a lumpectomy; a diagnostic excision falls out by `no_cancer`), what proves an invasive cancer and how
# long after the surgery its report may come. Provisional, to be decided with the domain expert.
# CaMa's survival cohort (models/cama/cohorts/survival): the surgeries that are its procedure of interest
# (ESCISIONAL includes a lumpectomy). Provisional, to be decided with the domain expert.
survival_cohort:
recruitment_start: "2018-01-01"
recruitment_end: "2027-01-01"
cutoff: "2026-12-31"
surgeries: ["ESCISIONAL", "SEGMENTECTOMIA", "MASTECTOMIA_TOTAL", "MASTECTOMIA_RADICAL_MODIFICADA"]
cancer_grades: ["CARCINOMA_INVASOR"]
cancer_icd10: ["C50", "C500", "C501", "C502", "C503", "C504", "C505", "C506", "C508", "C509"]
report_window_days: 90
# The surveillance cohort's rules (models/cacu/cohorts/surveillance).
surveillance_cohort:
recruitment_start: "2024-01-01"
@@ -1,10 +1,10 @@
version: 2
# The survival cohort, one query per rule, compiled and chained by `models cohorts build` like CaCu's surveillance
# cohort. The index is the first surgery, the first treatment: its report is where the catalog's covariates are
# (histology, receptors, size, nodes, margins), while the needle biopsy before it only says whether there is a
# carcinoma. Each exclusion reads only the index date and the data. Parameters: the `survival_cohort` var in
# dbt_project.yml.
# cohort. Whoever has the procedure of interest enters, a surgery of a kind in `surgeries`, its date the index: its
# report is where the catalog's covariates are (histology, receptors, size, nodes, margins), while the needle biopsy
# before it only says whether there is a carcinoma. The one filter keeps the patients in post-treatment. Parameters:
# the `survival_cohort` var in dbt_project.yml.
models:
- name: cama_survival_all_patients
description: Every patient of the domain.
@@ -18,12 +18,9 @@ models:
- name: cama_survival_index_date
description: First breast surgery of a kind in `surgeries`, if it falls in the recruitment window. Sets the index date.
config: {materialized: ephemeral, meta: {cohort: cama/cohorte_supervivencia, kind: index}}
- name: cama_survival_excl_prior_treatment
description: Radiotherapy or chemotherapy before the index surgery, so the surgery wasn't the first treatment.
config: {materialized: ephemeral, meta: {cohort: cama/cohorte_supervivencia, kind: exclusion, name: prior_treatment, order: 1}}
- name: cama_survival_excl_no_cancer
description: No invasive carcinoma by biopsy nor a malignant ICD-10 by the time the surgical report is due.
config: {materialized: ephemeral, meta: {cohort: cama/cohorte_supervivencia, kind: exclusion, name: no_cancer, order: 2}}
- name: cama_survival_excl_not_post_treatment
description: Not in post-treatment by the cut-off (no `escenario_postratamiento` in the timeline).
config: {materialized: ephemeral, meta: {cohort: cama/cohorte_supervivencia, kind: exclusion, name: not_post_treatment, order: 1}}
unit_tests:
- name: survival_index_is_the_first_therapeutic_surgery_in_the_window
@@ -35,7 +32,7 @@ unit_tests:
model: cama_survival_index_date
overrides:
vars:
survival_cohort: {recruitment_start: "2020-01-01", recruitment_end: "2027-01-01", cutoff: "2026-12-31", surgeries: ["SEGMENTECTOMIA", "MASTECTOMIA_TOTAL"], cancer_grades: ["CARCINOMA_INVASOR"], cancer_icd10: ["C504"], report_window_days: 90}
survival_cohort: {recruitment_start: "2020-01-01", recruitment_end: "2027-01-01", cutoff: "2026-12-31", surgeries: ["SEGMENTECTOMIA", "MASTECTOMIA_TOTAL"]}
given:
- input: ref('cama_linea_de_tiempo')
format: sql
@@ -52,12 +49,15 @@ unit_tests:
rows: |
select 1 as pac_id, '2023-03-01' as fecha_indice, 'cx' as indice_fuente_tabla, 'b' as indice_fuente_id
union all select 2, '2024-05-05', 'cx', 'c'
- name: survival_prior_treatment_flags_a_neoadjuvant_course
- name: survival_keeps_only_patients_in_post_treatment
description: >
Pins down: patient 1 had chemotherapy and patient 2 radiotherapy before the index surgery, so both are flagged;
patient 3's radiotherapy is on the index date and patient 4's after it, so neither is. Fails if the rule stops
reading both treatments or counts a course on the day of surgery.
model: cama_survival_excl_prior_treatment
Pins down: patient 1 entered post-treatment before the cut-off, so she stays; patient 2 is only in control,
patient 3's post-treatment starts after the cut-off and patient 4 has no scenario, so the three are flagged.
Fails if the filter reads another scenario or ignores the cut-off.
model: cama_survival_excl_not_post_treatment
overrides:
vars:
survival_cohort: {recruitment_start: "2020-01-01", recruitment_end: "2027-01-01", cutoff: "2026-12-31", surgeries: ["SEGMENTECTOMIA"]}
given:
- input: ref('cama_survival_index_date')
format: sql
@@ -67,39 +67,11 @@ unit_tests:
- input: ref('cama_linea_de_tiempo')
format: sql
rows: |
select 1 as pac_id, '2023-11-01' as fecha, 'quimioterapia' as tipo
union all select 2, '2024-01-15', 'radioterapia'
union all select 3, '2024-03-01', 'radioterapia'
union all select 4, '2024-05-01', 'quimioterapia'
select 1 as pac_id, '2024-03-10' as fecha, 'escenario_postratamiento' as tipo
union all select 2, '2024-02-01', 'escenario_control'
union all select 3, '2027-02-01', 'escenario_postratamiento'
expect:
rows:
- {pac_id: 1}
- {pac_id: 2}
- name: survival_no_cancer_waits_for_the_surgical_report
description: >
Pins down: patient 1's needle biopsy before surgery is invasive and patient 2's surgical report, 30 days after,
is too, so neither is flagged; patient 3's invasive report comes after the 90-day window and patient 4 only has a
benign biopsy, so both are; patient 5 has a malignant ICD-10 note, which is enough. Fails if the window, the
grades or the ICD-10 list stop applying.
model: cama_survival_excl_no_cancer
overrides:
vars:
survival_cohort: {recruitment_start: "2020-01-01", recruitment_end: "2027-01-01", cutoff: "2026-12-31", surgeries: ["SEGMENTECTOMIA"], cancer_grades: ["CARCINOMA_INVASOR"], cancer_icd10: ["C504"], report_window_days: 90}
given:
- input: ref('cama_survival_index_date')
format: sql
rows: |
select 1 as pac_id, '2024-03-01' as fecha_indice union all select 2, '2024-03-01'
union all select 3, '2024-03-01' union all select 4, '2024-03-01' union all select 5, '2024-03-01'
- input: ref('cama_linea_de_tiempo')
format: sql
rows: |
select 1 as pac_id, '2024-02-01' as fecha, 'biopsia' as tipo, 'CARCINOMA_INVASOR' as valor_codigo
union all select 2, '2024-03-31', 'biopsia', 'CARCINOMA_INVASOR'
union all select 3, '2024-07-15', 'biopsia', 'CARCINOMA_INVASOR'
union all select 4, '2024-03-20', 'biopsia', 'BENIGNO'
union all select 5, '2024-02-20', 'nota_medica', 'C504'
expect:
rows:
- {pac_id: 3}
- {pac_id: 4}
@@ -1,15 +0,0 @@
{# Candidates with no invasive breast cancer by the time the surgical report is due (`report_window_days` after the
index): neither a biopsy graded in `cancer_grades` nor a note with an ICD-10 in `cancer_icd10`. Their surgery
wasn't a cancer's treatment. #}
{% set p = var('survival_cohort') %}
select c.pac_id
from {{ ref('cama_survival_index_date') }} as c
where not exists (
select 1
from {{ ref('cama_linea_de_tiempo') }} as e
where e.pac_id = c.pac_id
and e.fecha <= '{{ p.cutoff }}'
and {{ dbt.datediff('c.fecha_indice', 'e.fecha', 'day') }} <= {{ p.report_window_days }}
and ((e.tipo = 'biopsia' and e.valor_codigo in {{ sql_list(p.cancer_grades) }})
or (e.tipo = 'nota_medica' and e.valor_codigo in {{ sql_list(p.cancer_icd10) }}))
)
@@ -0,0 +1,12 @@
{# Candidates not in post-treatment by the cut-off: no `escenario_postratamiento` fact in their timeline
(`cama_procedimientos_inferidos`, rules in `cama.escenarios`). #}
{% set p = var('survival_cohort') %}
select c.pac_id
from {{ ref('cama_survival_index_date') }} as c
where not exists (
select 1
from {{ ref('cama_linea_de_tiempo') }} as e
where e.pac_id = c.pac_id
and e.tipo = 'escenario_postratamiento'
and e.fecha <= '{{ p.cutoff }}'
)
@@ -1,8 +0,0 @@
{# Candidates with radiotherapy or chemotherapy before their index surgery: the surgery wasn't the first treatment,
and a neoadjuvant course changes what the surgical report finds. #}
{% set p = var('survival_cohort') %}
select distinct c.pac_id
from {{ ref('cama_survival_index_date') }} as c
join {{ ref('cama_linea_de_tiempo') }} as e on e.pac_id = c.pac_id
where e.tipo in ('radioterapia', 'quimioterapia')
and e.fecha < c.fecha_indice