diff --git a/CHANGELOG.md b/CHANGELOG.md index 80056f2..92f63a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -177,4 +177,7 @@ - added primary diagnosis condition mv, registry compile/drift checks, postgres CI coverage, and schema snapshot artifact export ## 0.4.7 -- fixed treatment envelope window issues \ No newline at end of file +- fixed treatment envelope window issues + +## 0.4.8 +- treatment envelope should left join to death (bug fix) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index b95d5c1..595600a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "omop-constructs" -version = "0.4.7" +version = "0.4.8" description = "For building complex constructs on top of the omop-alchemy library." readme = "README.md" authors = [ diff --git a/src/omop_constructs/alchemy/episodes/treatment_envelope_query.py b/src/omop_constructs/alchemy/episodes/treatment_envelope_query.py index ac8a36a..cd7c5aa 100644 --- a/src/omop_constructs/alchemy/episodes/treatment_envelope_query.py +++ b/src/omop_constructs/alchemy/episodes/treatment_envelope_query.py @@ -102,12 +102,18 @@ # --------------------------------------------------------------------------- -# treatment_envelope — person-level envelope joined to death +# treatment_envelope — condition-episode spine joined to death # --------------------------------------------------------------------------- +# The spine is treatment_window (rooted at ModifiedCondition / ConditionEpisodeMV) +# so that every condition episode appears regardless of mortality status. +# Death is outer-joined on person_id to attach death_datetime where it exists. +# Using Death as the spine instead would silently exclude all living patients — +# treatment timing scalars such as days_from_dx_to_treatment are meaningful for +# the full cohort, not only for those with a death record. treatment_envelope = ( sa.select( sa.func.row_number().over().label('mv_id'), - Death.person_id, + treatment_window.c.person_id, treatment_window.c.condition_episode, treatment_window.c.condition_start_date, treatment_window.c.concurrent_chemort, @@ -135,8 +141,8 @@ ).label('latest_treatment'), Death.death_datetime, ) - .select_from(Death) - .join(treatment_window, treatment_window.c.person_id == Death.person_id, isouter=True) + .select_from(treatment_window) + .join(Death, Death.person_id == treatment_window.c.person_id, isouter=True) .subquery() ) diff --git a/uv.lock b/uv.lock index 3d40a05..efd7362 100644 --- a/uv.lock +++ b/uv.lock @@ -1407,7 +1407,7 @@ postgres = [ [[package]] name = "omop-constructs" -version = "0.4.7" +version = "0.4.8" source = { editable = "." } dependencies = [ { name = "omop-alchemy", extra = ["postgres"] },