Skip to content

fix(mimic-iv): restart KDIGO stage smoothing on each ICU stay - #2188

Open
eastagile-tracker wants to merge 1 commit into
MIT-LCP:mainfrom
eastagile-tracker:agile-board/kdigo-smoothing-per-stay
Open

eastagile-tracker wants to merge 1 commit into
MIT-LCP:mainfrom
eastagile-tracker:agile-board/kdigo-smoothing-per-stay

Conversation

@eastagile-tracker

Copy link
Copy Markdown

This PR proposes restarting the KDIGO aki_stage_smoothed window on each ICU stay (Fixes #2184). We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/1460. You can sign in with your GitHub ID to claim ownership of the project.

What changed and why

In mimic-iv/concepts/organfailure/kdigo_stages.sql, aki_stage_smoothed takes the max stage over the preceding six hours, ordered by DATETIME_DIFF(tm.charttime, ie.intime, SECOND) but partitioned by ie.subject_id. That clock restarts at every ICU admission, so rows from a different stay of the same patient fall inside the window: as #2184 reports, a stage 3 creatinine two hours into a July stay raises the smoothed stage of a January row three hours into its stay from 0 to 3. The fix partitions by ie.stay_id, which matches the per-stay example in the comment above the window. The DuckDB and Postgres copies get the same one-line change; I confirmed the DuckDB file is byte-identical to mimic_utils convert_folder output, and the Postgres diff is the same single line.

If the intent was to carry a stage across a short gap between stays, the alternative from the issue (keep subject_id and order by the absolute charttime) would also work; happy to switch if you prefer that.

Reproduction at current main (e3e0310)

On an in-memory DuckDB with synthetic icustays / kdigo_creatinine / kdigo_uo / crrt tables (one patient, stays 10 and 20 six months apart, creatinine 1.0 at 03:00 in stay 10 and 4.0 at 02:00 in stay 20, baseline 1.0), running both the transpiled BigQuery source and the checked-in concepts_duckdb file returns (stay_id, charttime, aki_stage, aki_stage_smoothed):

before: (10, 2100-01-01 03:00, 0, 3)   (20, 2100-07-01 02:00, 3, 3)
after:  (10, 2100-01-01 03:00, 0, 0)   (20, 2100-07-01 02:00, 3, 3)

Verification

  • New tests/test_kdigo_stages.py runs the concept (BigQuery source transpiled to DuckDB) on that fixture. test_smoothing_does_not_borrow_stage_from_a_later_stay fails on main (3 != 0) and passes with the fix; test_smoothing_carries_highest_stage_forward_within_a_stay passes on both, confirming the six-hour carry-forward within a stay is unchanged (stage 3 still carried at +5h, dropped at +6.5h).
  • Full pytest before: 402 passed, 1 failed; after: 404 passed, 1 failed. The one failure is the same pre-existing test_sqlfluff.py::test_concept_passes_sqlfluff[score/sirs.sql] on both trees.
  • The transpile workflow currently runs only tests/test_transpile.py, so the new test runs with pytest tests/test_kdigo_stages.py (or a plain pytest) until you add it there.

How this was managed

This work was tracked as https://eastagiletracker.com/projects/1460/stories/1568266 on https://eastagiletracker.com/projects/1460, a board imported from this repo's 1,807 issues and pull requests.

board

If you'd rather not receive contributions like this, reply no-more-prs on this pull request and we won't open any further ones on your repositories.


Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com

aki_stage_smoothed takes the max stage over the preceding six hours,
ordered by seconds since ie.intime, but partitioned by subject_id. The
clock restarts on every stay, so rows from a different stay of the same
patient land in the window: a stage 3 observation two hours into a later
stay raised an earlier stay's smoothed stage from 0 to 3.

Partition by stay_id so the window only sees the stay's own rows, and
add a DuckDB regression test that runs the concept on synthetic tables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KDIGO smoothing can use measurements from a later ICU stay

1 participant