diff --git a/mimic-iv/concepts/organfailure/kdigo_stages.sql b/mimic-iv/concepts/organfailure/kdigo_stages.sql index db7758cbb..c3f910a05 100644 --- a/mimic-iv/concepts/organfailure/kdigo_stages.sql +++ b/mimic-iv/concepts/organfailure/kdigo_stages.sql @@ -132,7 +132,7 @@ SELECT ) ) OVER ( - PARTITION BY ie.subject_id + PARTITION BY ie.stay_id ORDER BY DATETIME_DIFF(tm.charttime, ie.intime, SECOND) RANGE BETWEEN 21600 PRECEDING AND CURRENT ROW ) AS aki_stage_smoothed diff --git a/mimic-iv/concepts_duckdb/organfailure/kdigo_stages.sql b/mimic-iv/concepts_duckdb/organfailure/kdigo_stages.sql index 8b0b6369b..6f4e1b7b3 100644 --- a/mimic-iv/concepts_duckdb/organfailure/kdigo_stages.sql +++ b/mimic-iv/concepts_duckdb/organfailure/kdigo_stages.sql @@ -125,7 +125,7 @@ SELECT ) END ) OVER ( - PARTITION BY ie.subject_id + PARTITION BY ie.stay_id ORDER BY DATE_DIFF('SECOND', ie.intime, tm.charttime) NULLS FIRST RANGE BETWEEN 21600 PRECEDING AND CURRENT ROW ) AS aki_stage_smoothed diff --git a/mimic-iv/concepts_postgres/organfailure/kdigo_stages.sql b/mimic-iv/concepts_postgres/organfailure/kdigo_stages.sql index 3946bccfb..757e7a306 100644 --- a/mimic-iv/concepts_postgres/organfailure/kdigo_stages.sql +++ b/mimic-iv/concepts_postgres/organfailure/kdigo_stages.sql @@ -114,7 +114,7 @@ SELECT COALESCE(crrt.aki_stage_crrt, 0) ) ) OVER ( - PARTITION BY ie.subject_id + PARTITION BY ie.stay_id ORDER BY CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', tm.charttime) - DATE_TRUNC('second', ie.intime)) / 1 AS BIGINT) NULLS FIRST RANGE BETWEEN 21600 PRECEDING AND CURRENT ROW ) AS aki_stage_smoothed diff --git a/tests/test_kdigo_stages.py b/tests/test_kdigo_stages.py new file mode 100644 index 000000000..44a3a9bf5 --- /dev/null +++ b/tests/test_kdigo_stages.py @@ -0,0 +1,78 @@ +"""Run the MIMIC-IV kdigo_stages concept on a synthetic DuckDB fixture.""" +from pathlib import Path + +import pytest + +from mimic_utils.transpile import transpile_query + +duckdb = pytest.importorskip("duckdb") + +REPO_ROOT = Path(__file__).resolve().parent.parent +KDIGO_STAGES = REPO_ROOT / "mimic-iv" / "concepts" / "organfailure" / "kdigo_stages.sql" + + +def kdigo_stages(icustays, creatinine): + """Return {(stay_id, charttime): (aki_stage, aki_stage_smoothed)}.""" + con = duckdb.connect() + con.execute("CREATE SCHEMA mimiciv_icu") + con.execute("CREATE SCHEMA mimiciv_derived") + con.execute( + "CREATE TABLE mimiciv_icu.icustays " + "(subject_id INT, hadm_id INT, stay_id INT, intime TIMESTAMP)" + ) + con.execute( + "CREATE TABLE mimiciv_derived.kdigo_creatinine (stay_id INT, charttime TIMESTAMP, " + "creat_low_past_7day DOUBLE, creat_low_past_48hr DOUBLE, creat DOUBLE)" + ) + con.execute( + "CREATE TABLE mimiciv_derived.kdigo_uo (stay_id INT, charttime TIMESTAMP, " + "weight DOUBLE, uo_rt_6hr DOUBLE, uo_rt_12hr DOUBLE, uo_rt_24hr DOUBLE, " + "uo_tm_6hr DOUBLE, uo_tm_12hr DOUBLE, uo_tm_24hr DOUBLE)" + ) + con.execute( + "CREATE TABLE mimiciv_derived.crrt (stay_id INT, charttime TIMESTAMP, crrt_mode VARCHAR)" + ) + con.executemany("INSERT INTO mimiciv_icu.icustays VALUES (?, ?, ?, ?)", icustays) + # creatinine rows: (stay_id, charttime, creat); a baseline of 1.0 makes 4.0 stage 3 + con.executemany( + "INSERT INTO mimiciv_derived.kdigo_creatinine VALUES (?, ?, 1.0, 1.0, ?)", creatinine + ) + query = transpile_query(KDIGO_STAGES.read_text(), "bigquery", "duckdb") + rows = con.execute( + f"SELECT stay_id, charttime, aki_stage, aki_stage_smoothed FROM ({query}) AS k" + ).fetchall() + return {(stay_id, str(charttime)): (stage, smoothed) for stay_id, charttime, stage, smoothed in rows} + + +def test_smoothing_does_not_borrow_stage_from_a_later_stay(): + # stay 20 is six months after stay 10, but both are two to three hours + # after their own intime, so they collide on a per-patient "seconds since + # intime" axis (#2184) + result = kdigo_stages( + icustays=[ + (1, 100, 10, "2100-01-01 00:00:00"), + (1, 200, 20, "2100-07-01 00:00:00"), + ], + creatinine=[ + (10, "2100-01-01 03:00:00", 1.0), + (20, "2100-07-01 02:00:00", 4.0), + ], + ) + assert result[(10, "2100-01-01 03:00:00")] == (0, 0) + assert result[(20, "2100-07-01 02:00:00")] == (3, 3) + + +def test_smoothing_carries_highest_stage_forward_within_a_stay(): + result = kdigo_stages( + icustays=[(1, 100, 10, "2100-01-01 00:00:00")], + creatinine=[ + (10, "2100-01-01 02:00:00", 4.0), + (10, "2100-01-01 07:00:00", 1.0), + (10, "2100-01-01 08:30:00", 1.0), + ], + ) + assert result[(10, "2100-01-01 02:00:00")] == (3, 3) + # five hours later: still inside the six hour window + assert result[(10, "2100-01-01 07:00:00")] == (0, 3) + # six and a half hours later: the stage 3 row has left the window + assert result[(10, "2100-01-01 08:30:00")] == (0, 0)