CaMa survival cohort in dbt: indexed at the first surgery, excluding a prior radiotherapy or chemotherapy and no invasive cancer by the surgical report

This commit is contained in:
rockdrigo
2026-11-20 18:29:03 -06:00
parent 655067e47d
commit 1937834b25
6 changed files with 156 additions and 0 deletions
+11
View File
@@ -130,6 +130,17 @@ 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.
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"
@@ -0,0 +1,105 @@
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.
models:
- name: cama_survival_all_patients
description: Every patient of the domain.
config:
materialized: ephemeral
meta:
cohort: cama/cohorte_supervivencia
kind: population
cutoff: "{{ var('survival_cohort')['cutoff'] }}"
parameters: "{{ var('survival_cohort') | tojson }}"
- 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}}
unit_tests:
- name: survival_index_is_the_first_therapeutic_surgery_in_the_window
description: >
Pins down: patient 1's index is her first surgery of a listed kind, not the earlier one of another kind; patient
2's two surgeries on one day give one index, the lower `fuente_id`; patient 3's first surgery falls before the
window, so she has no index even with a later one; patient 4 has only a radiotherapy. Fails if the index stops
filtering by kind, ordering by date or applying the window to the first surgery only.
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}
given:
- input: ref('cama_linea_de_tiempo')
format: sql
rows: |
select 1 as pac_id, '2023-01-10' as fecha, 'cirugia' as tipo, 'ESCISIONAL' as valor_codigo, 'cx' as fuente_tabla, 'a' as fuente_id
union all select 1, '2023-03-01', 'cirugia', 'SEGMENTECTOMIA', 'cx', 'b'
union all select 2, '2024-05-05', 'cirugia', 'MASTECTOMIA_TOTAL', 'cx', 'd'
union all select 2, '2024-05-05', 'cirugia', 'MASTECTOMIA_TOTAL', 'cx', 'c'
union all select 3, '2019-06-01', 'cirugia', 'SEGMENTECTOMIA', 'cx', 'e'
union all select 3, '2021-06-01', 'cirugia', 'MASTECTOMIA_TOTAL', 'cx', 'f'
union all select 4, '2024-01-01', 'radioterapia', 'ADYUVANTE', 'rt', 'g'
expect:
format: sql
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
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
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'
- 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'
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}
@@ -0,0 +1,3 @@
{# Every patient of the domain: where the cohort's attrition starts. #}
select pac_id
from {{ source('silver', 'persona') }}
@@ -0,0 +1,15 @@
{# 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,8 @@
{# 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
@@ -0,0 +1,14 @@
{# The patient's first breast surgery of a kind in `surgeries` up to the cut-off, if it falls in the recruitment window:
where the surgical report gives the covariates. A surgery per breast on the same day is one index; the lower
`fuente_id` names it. #}
{% set p = var('survival_cohort') %}
with surgeries as (
select
pac_id, fecha, fuente_tabla, fuente_id,
row_number() over (partition by pac_id order by fecha, fuente_id) as position
from {{ ref('cama_linea_de_tiempo') }}
where tipo = 'cirugia' and valor_codigo in {{ sql_list(p.surgeries) }} and fecha <= '{{ p.cutoff }}'
)
select pac_id, fecha as fecha_indice, fuente_tabla as indice_fuente_tabla, fuente_id as indice_fuente_id
from surgeries
where position = 1 and fecha >= '{{ p.recruitment_start }}' and fecha < '{{ p.recruitment_end }}'