From a0f83014955af7f6f9bfd70be37d4839736b7050 Mon Sep 17 00:00:00 2001 From: Taksh Date: Thu, 1 Oct 2026 15:04:07 +0530 Subject: [PATCH] fix(mimic-iv): include phenylephrine infusion variants --- .github/workflows/transpile.yml | 2 +- .../concepts/medication/phenylephrine.sql | 8 ++- .../medication/phenylephrine.sql | 2 +- .../medication/phenylephrine.sql | 10 ++- tests/test_phenylephrine.py | 71 +++++++++++++++++++ 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/test_phenylephrine.py diff --git a/.github/workflows/transpile.yml b/.github/workflows/transpile.yml index 4d75074ea..fed8ecf5a 100644 --- a/.github/workflows/transpile.yml +++ b/.github/workflows/transpile.yml @@ -18,7 +18,7 @@ jobs: - name: Install package run: pip install -e . --no-deps - name: Run transpiler unit tests - run: pytest tests/test_transpile.py -q + run: pytest tests/test_transpile.py tests/test_phenylephrine.py -q generated-up-to-date: name: Check cross-dialect concepts match BQ diff --git a/mimic-iv/concepts/medication/phenylephrine.sql b/mimic-iv/concepts/medication/phenylephrine.sql index d26c5336b..ea3e27abf 100644 --- a/mimic-iv/concepts/medication/phenylephrine.sql +++ b/mimic-iv/concepts/medication/phenylephrine.sql @@ -9,4 +9,10 @@ SELECT , starttime , endtime FROM `physionet-data.mimiciv_icu.inputevents` -WHERE itemid = 221749 -- phenylephrine +WHERE itemid IN ( + 221749 -- Phenylephrine + , 229630 -- Phenylephrine (50/250) + , 229631 -- Phenylephrine (200/250)_OLD_1 + , 229632 -- Phenylephrine (200/250) +) +-- Exclude 229789 (Phenylephrine (Intubation)), which is bolus dosing. diff --git a/mimic-iv/concepts_duckdb/medication/phenylephrine.sql b/mimic-iv/concepts_duckdb/medication/phenylephrine.sql index f2049a01c..94e08435a 100644 --- a/mimic-iv/concepts_duckdb/medication/phenylephrine.sql +++ b/mimic-iv/concepts_duckdb/medication/phenylephrine.sql @@ -9,4 +9,4 @@ SELECT endtime FROM mimiciv_icu.inputevents WHERE - itemid = 221749 \ No newline at end of file + itemid IN (221749, 229630, 229631, 229632) \ No newline at end of file diff --git a/mimic-iv/concepts_postgres/medication/phenylephrine.sql b/mimic-iv/concepts_postgres/medication/phenylephrine.sql index bd2666520..82468e918 100644 --- a/mimic-iv/concepts_postgres/medication/phenylephrine.sql +++ b/mimic-iv/concepts_postgres/medication/phenylephrine.sql @@ -1,6 +1,7 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciv_derived.phenylephrine; CREATE TABLE mimiciv_derived.phenylephrine AS -/* This query extracts dose+durations of phenylephrine administration */ /* Local hospital dosage guidance: 0.5 mcg/kg/min (low) - 5 mcg/kg/min (high) */ +/* This query extracts dose+durations of phenylephrine administration */ +/* Local hospital dosage guidance: 0.5 mcg/kg/min (low) - 5 mcg/kg/min (high) */ SELECT stay_id, linkorderid, /* one row in mcg/min, the rest in mcg/kg/min */ @@ -14,4 +15,9 @@ SELECT endtime FROM mimiciv_icu.inputevents WHERE - itemid = 221749 /* phenylephrine */ \ No newline at end of file + itemid IN ( + 221749, /* Phenylephrine */ + 229630, /* Phenylephrine (50/250) */ + 229631, /* Phenylephrine (200/250)_OLD_1 */ + 229632 /* Phenylephrine (200/250) */ + ) /* Exclude 229789 (Phenylephrine (Intubation)), which is bolus dosing. */ \ No newline at end of file diff --git a/tests/test_phenylephrine.py b/tests/test_phenylephrine.py new file mode 100644 index 000000000..6199d983d --- /dev/null +++ b/tests/test_phenylephrine.py @@ -0,0 +1,71 @@ +"""Synthetic regression coverage for phenylephrine concentration variants (#2159).""" +from pathlib import Path + +import pytest + +from mimic_utils.transpile import transpile_query + +duckdb = pytest.importorskip("duckdb") +ROOT = Path(__file__).resolve().parents[1] / "mimic-iv" + + +@pytest.mark.parametrize("sql_source", ["bigquery", "committed_duckdb"]) +def test_infusion_variants_reach_norepinephrine_equivalent_dose(sql_source): + with duckdb.connect() as con: + con.execute("CREATE SCHEMA mimiciv_icu; CREATE SCHEMA mimiciv_derived") + con.execute(""" + CREATE TABLE mimiciv_icu.inputevents ( + stay_id BIGINT, linkorderid BIGINT, itemid INTEGER, + rate DOUBLE, rateuom VARCHAR, patientweight DOUBLE, + amount DOUBLE, starttime TIMESTAMP, endtime TIMESTAMP + ) + """) + # All four infusion labels, the intubation bolus label, and an unrelated drug. + # Each infusion has a distinct stay so a missing item cannot be masked. + for stay_id, itemid, rate, unit in [ + (1, 221749, 1.0, "mcg/kg/min"), + (2, 229630, 2.0, "mcg/kg/min"), + (3, 229631, 3.0, "mcg/kg/min"), + (4, 229632, 200.0, "mcg/min"), + (5, 229789, None, None), + (6, 999999, 9.0, "mcg/kg/min"), + ]: + con.execute(""" + INSERT INTO mimiciv_icu.inputevents VALUES + (?, ?, ?, ?, ?, 50, 10, '2150-01-01 00:00:00', '2150-01-01 02:00:00') + """, [stay_id, stay_id, itemid, rate, unit]) + + def build(name): + if sql_source == "bigquery": + sql = (ROOT / "concepts" / "medication" / f"{name}.sql").read_text() + con.execute( + f"CREATE TABLE mimiciv_derived.{name} AS " + + transpile_query(sql, "bigquery", "duckdb") + ) + else: + con.execute((ROOT / "concepts_duckdb" / "medication" / f"{name}.sql").read_text()) + + build("phenylephrine") + assert con.execute(""" + SELECT stay_id, vaso_rate FROM mimiciv_derived.phenylephrine ORDER BY stay_id + """).fetchall() == [(1, 1.0), (2, 2.0), (3, 3.0), (4, 4.0)] + + for name in ("dobutamine", "dopamine", "epinephrine", "norepinephrine", "vasopressin", "milrinone"): + con.execute( + f"CREATE TABLE mimiciv_derived.{name} AS " + "SELECT * FROM mimiciv_derived.phenylephrine WHERE FALSE" + ) + # A variant also contributes when another vasopressor is already present. + con.execute(""" + INSERT INTO mimiciv_derived.norepinephrine + SELECT stay_id, linkorderid, 0.1, vaso_amount, starttime, endtime + FROM mimiciv_derived.phenylephrine WHERE stay_id = 2 + """) + build("vasoactive_agent") + build("norepinephrine_equivalent_dose") + rows = con.execute(""" + SELECT stay_id, norepinephrine_equivalent_dose + FROM mimiciv_derived.norepinephrine_equivalent_dose ORDER BY stay_id + """).fetchall() + assert [r[0] for r in rows] == [1, 2, 3, 4] + assert [float(r[1]) for r in rows] == pytest.approx([0.1, 0.3, 0.3, 0.4])