Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/transpile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion mimic-iv/concepts/medication/phenylephrine.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion mimic-iv/concepts_duckdb/medication/phenylephrine.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ SELECT
endtime
FROM mimiciv_icu.inputevents
WHERE
itemid = 221749
itemid IN (221749, 229630, 229631, 229632)
10 changes: 8 additions & 2 deletions mimic-iv/concepts_postgres/medication/phenylephrine.sql
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -14,4 +15,9 @@ SELECT
endtime
FROM mimiciv_icu.inputevents
WHERE
itemid = 221749 /* phenylephrine */
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. */
71 changes: 71 additions & 0 deletions tests/test_phenylephrine.py
Original file line number Diff line number Diff line change
@@ -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])
Loading