Skip to content

Commit

Permalink
Merge pull request #1505 from MIT-LCP/tidy_ned_sql
Browse files Browse the repository at this point in the history
Fix norepinephrine in PSQL, tidy vasoactive code
  • Loading branch information
alistairewj authored Mar 23, 2023
2 parents 176be33 + 572233c commit 04a11ba
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 25 deletions.
14 changes: 8 additions & 6 deletions mimic-iv/concepts/convert_bigquery_to_postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,24 @@ do
tbl="${fn%????}"
echo -n " ${tbl} "

if [[ "$DIR_AND_TABLES_TO_PREBUILD" =~ "$d.$tbl" ]]; then
# check if var2 is in var1
if echo "$DIR_AND_TABLES_TO_PREBUILD" | grep -wq "$d.$tbl"; then
echo -n "(prebuilt!) .."
continue
elif [[ "$DIR_AND_TABLES_TO_SKIP" =~ "$d.$tbl" ]]; then
elif echo "$DIR_AND_TABLES_TO_SKIP" | grep -wq "$d.$tbl"; then
echo -n "(skipping!) .."
continue
else
echo -n ".."
fi

# re-write the script into psql using regex
# the if statement ensures we do not overwrite tables which are already written in psql
if ! [[ "$DIR_AND_TABLES_ALREADY_IN_PSQL" =~ "$d.$tbl" ]]; then
if ! echo "$DIR_AND_TABLES_ALREADY_IN_PSQL" | grep -wq "$d.$tbl"; then
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "${TARGET_PATH}/${d}/${tbl}.sql"
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS" >> "${TARGET_PATH}/${d}/${tbl}.sql"
cat "${d}/${tbl}.sql" | sed -r -e "${REGEX_ARRAY}" | sed -r -e "${REGEX_HOUR_INTERVAL}" | sed -r -e "${REGEX_INT}" | sed -r -e "${REGEX_DATETIME_DIFF}" | sed -r -e "${REGEX_DATETIME_TRUNC}" | sed -r -e "${REGEX_SCHEMA}" | sed -r -e "${REGEX_INTERVAL}" >> "${TARGET_PATH}/${d}/${fn}"
echo -n ".."
else
echo -n "(psql!) .."
fi

# add statement to generate this table
Expand All @@ -167,7 +169,7 @@ do

# convert the bigquery script to psql and output it to the appropriate subfolder
echo -n " ${d}.${tbl} .."
if ! [[ "$DIR_AND_TABLES_ALREADY_IN_PSQL" =~ "$d.$tbl" ]]; then
if ! echo "$DIR_AND_TABLES_ALREADY_IN_PSQL" | grep -wq "$d.$tbl"; then
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "$TARGET_PATH/${d}/${tbl}.sql"
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS" >> "$TARGET_PATH/${d}/${tbl}.sql"

Expand Down
3 changes: 2 additions & 1 deletion mimic-iv/concepts/medication/dobutamine.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- This query extracts dose+durations of dopamine administration
-- This query extracts dose+durations of dobutamine administration
-- Local hospital dosage guidance: 2 mcg/kg/min (low) - 40 mcg/kg/min (max)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts/medication/dopamine.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- This query extracts dose+durations of dopamine administration
-- Local hospital dosage guidance: 2 mcg/kg/min (low) - 10 mcg/kg/min (high)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts/medication/epinephrine.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- This query extracts dose+durations of epinephrine administration
-- Local hospital dosage guidance: 0.2 mcg/kg/min (low) - 2 mcg/kg/min (high)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts/medication/milrinone.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- This query extracts dose+durations of milrinone administration
-- Local hospital dosage guidance: 0.5 mcg/kg/min (usual)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
2 changes: 2 additions & 0 deletions mimic-iv/concepts/medication/norepinephrine.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
-- This query extracts dose+durations of norepinephrine administration
-- Local hospital dosage guidance: 0.03 mcg/kg/min (low), 0.5 mcg/kg/min (high)
SELECT
stay_id, linkorderid
-- two rows in mg/kg/min... rest in mcg/kg/min
-- the rows in mg/kg/min are documented incorrectly
-- all rows converted into mcg/kg/min (equiv to ug/kg/min)
, CASE WHEN rateuom = 'mg/kg/min' AND patientweight = 1 THEN rate
-- below row is written for completion, but doesn't impact rows
WHEN rateuom = 'mg/kg/min' THEN rate * 1000.0
Expand Down
14 changes: 13 additions & 1 deletion mimic-iv/concepts/medication/norepinephrine_equivalent_dose.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
-- This query calculates norepinephrine equivalent dose for vasopressors.
-- Based on "Vasopressor dose equivalence: A scoping review and
-- suggested formula" by Goradia et al. 2020.

-- The relevant table makes the following equivalences:
-- Norepinephrine - 1:1 - comparison dose of 0.1 ug/kg/min
-- Epinephrine - 1:1 [0.7, 1.4] - 0.1 ug/kg/min
-- Dopamine - 1:100 [75.2, 144.4] - 10 ug/kg/min
-- Metaraminol - 1:8 [8.3] - 0.8 ug/kg/min
-- Phenylephrine - 1:10 [1.1, 16.3] - 1 ug/kg/min
-- Vasopressin - 1:0.4 [0.3, 0.4] - 0.04 units/min
-- Angiotensin II - 1:0.1 [0.07, 0.13] - 0.01 ug/kg/min

SELECT stay_id, starttime, endtime
-- calculate the dose
-- all sources are in mcg/kg/min,
-- except vasopressin which is in units/hour
, ROUND(CAST(
COALESCE(norepinephrine, 0)
+ COALESCE(epinephrine, 0)
+ COALESCE(phenylephrine / 10, 0)
+ COALESCE(dopamine / 100, 0)
-- + metaraminol/8 -- metaraminol not used in BIDMC
+ COALESCE(vasopressin * 2.5/60, 0)
+ COALESCE(vasopressin * 2.5 / 60, 0)
-- angiotensin_ii*10 -- angiotensin ii rarely used, though
-- it could be included due to norepinephrine sparing effects
AS NUMERIC), 4) AS norepinephrine_equivalent_dose
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts/medication/phenylephrine.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- 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 Down
14 changes: 7 additions & 7 deletions mimic-iv/concepts/medication/vasoactive_agent.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ WITH tm AS (
-- left join to raw data tables to combine doses
SELECT t.stay_id, t.starttime, t.endtime
-- inopressors/vasopressors
, dop.vaso_rate AS dopamine
, epi.vaso_rate AS epinephrine
, nor.vaso_rate AS norepinephrine
, phe.vaso_rate AS phenylephrine
, vas.vaso_rate AS vasopressin
, dop.vaso_rate AS dopamine -- mcg/kg/min
, epi.vaso_rate AS epinephrine -- mcg/kg/min
, nor.vaso_rate AS norepinephrine -- mcg/kg/min
, phe.vaso_rate AS phenylephrine -- mcg/kg/min
, vas.vaso_rate AS vasopressin -- units/hour
-- inodialators
, dob.vaso_rate AS dobutamine
, mil.vaso_rate AS milrinone
, dob.vaso_rate AS dobutamine -- mcg/kg/min
, mil.vaso_rate AS milrinone -- mcg/kg/min
-- isoproterenol is used in CCU/CVICU but not in metavision
-- other drugs not included here but (rarely) used in the BIDMC:
-- angiotensin II, methylene blue
Expand Down
3 changes: 3 additions & 0 deletions mimic-iv/concepts/medication/vasopressin.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
-- This query extracts dose+durations of vasopressin administration
-- Local hospital dosage guidance: 1.2 units/hour (low) - 2.4 units/hour (high)
SELECT
stay_id, linkorderid
-- three rows in units/min, rest in units/hour
-- the three rows in units/min look reasonable and
-- fit with the patient course

-- convert all rows to units/hour
, CASE WHEN rateuom = 'units/min' THEN rate * 60.0
ELSE rate END AS vaso_rate
, amount AS vaso_amount
Expand Down
3 changes: 2 additions & 1 deletion mimic-iv/concepts_postgres/medication/dobutamine.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 dobutamine; CREATE TABLE dobutamine AS
-- This query extracts dose+durations of dopamine administration
-- This query extracts dose+durations of dobutamine administration
-- Local hospital dosage guidance: 2 mcg/kg/min (low) - 40 mcg/kg/min (max)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts_postgres/medication/dopamine.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 dopamine; CREATE TABLE dopamine AS
-- This query extracts dose+durations of dopamine administration
-- Local hospital dosage guidance: 2 mcg/kg/min (low) - 10 mcg/kg/min (high)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts_postgres/medication/epinephrine.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 epinephrine; CREATE TABLE epinephrine AS
-- This query extracts dose+durations of epinephrine administration
-- Local hospital dosage guidance: 0.2 mcg/kg/min (low) - 2 mcg/kg/min (high)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts_postgres/medication/milrinone.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 milrinone; CREATE TABLE milrinone AS
-- This query extracts dose+durations of milrinone administration
-- Local hospital dosage guidance: 0.5 mcg/kg/min (usual)
SELECT
stay_id, linkorderid
-- all rows in mcg/kg/min
Expand Down
4 changes: 3 additions & 1 deletion mimic-iv/concepts_postgres/medication/norepinephrine.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
DROP TABLE IF EXISTS norepinephrine; CREATE TABLE norepinephrine AS
DROP TABLE IF EXISTS norepinephrine; CREATE TABLE norepinephrine AS
-- This query extracts dose+durations of norepinephrine administration
-- Local hospital dosage guidance: 0.03 mcg/kg/min (low), 0.5 mcg/kg/min (high)
SELECT
stay_id, linkorderid
-- two rows in mg/kg/min... rest in mcg/kg/min
-- the rows in mg/kg/min are documented incorrectly
-- all rows converted into mcg/kg/min (equiv to ug/kg/min)
, CASE WHEN rateuom = 'mg/kg/min' AND patientweight = 1 THEN rate
-- below row is written for completion, but doesn't impact rows
WHEN rateuom = 'mg/kg/min' THEN rate * 1000.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@ DROP TABLE IF EXISTS norepinephrine_equivalent_dose; CREATE TABLE norepinephrine
-- This query calculates norepinephrine equivalent dose for vasopressors.
-- Based on "Vasopressor dose equivalence: A scoping review and
-- suggested formula" by Goradia et al. 2020.

-- The relevant table makes the following equivalences:
-- Norepinephrine - 1:1 - comparison dose of 0.1 ug/kg/min
-- Epinephrine - 1:1 [0.7, 1.4] - 0.1 ug/kg/min
-- Dopamine - 1:100 [75.2, 144.4] - 10 ug/kg/min
-- Metaraminol - 1:8 [8.3] - 0.8 ug/kg/min
-- Phenylephrine - 1:10 [1.1, 16.3] - 1 ug/kg/min
-- Vasopressin - 1:0.4 [0.3, 0.4] - 0.04 units/min
-- Angiotensin II - 1:0.1 [0.07, 0.13] - 0.01 ug/kg/min

SELECT stay_id, starttime, endtime
-- calculate the dose
-- all sources are in mcg/kg/min,
-- except vasopressin which is in units/hour
, ROUND(CAST(
COALESCE(norepinephrine, 0)
+ COALESCE(epinephrine, 0)
+ COALESCE(phenylephrine / 10, 0)
+ COALESCE(dopamine / 100, 0)
-- + metaraminol/8 -- metaraminol not used in BIDMC
+ COALESCE(vasopressin * 2.5, 0)
+ COALESCE(vasopressin * 2.5 / 60, 0)
-- angiotensin_ii*10 -- angiotensin ii rarely used, though
-- it could be included due to norepinephrine sparing effects
AS NUMERIC), 4) AS norepinephrine_equivalent_dose
Expand Down
1 change: 1 addition & 0 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 phenylephrine; CREATE TABLE 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)
SELECT
stay_id, linkorderid
-- one row in mcg/min, the rest in mcg/kg/min
Expand Down
14 changes: 7 additions & 7 deletions mimic-iv/concepts_postgres/medication/vasoactive_agent.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ WITH tm AS (
-- left join to raw data tables to combine doses
SELECT t.stay_id, t.starttime, t.endtime
-- inopressors/vasopressors
, dop.vaso_rate AS dopamine
, epi.vaso_rate AS epinephrine
, nor.vaso_rate AS norepinephrine
, phe.vaso_rate AS phenylephrine
, vas.vaso_rate AS vasopressin
, dop.vaso_rate AS dopamine -- mcg/kg/min
, epi.vaso_rate AS epinephrine -- mcg/kg/min
, nor.vaso_rate AS norepinephrine -- mcg/kg/min
, phe.vaso_rate AS phenylephrine -- mcg/kg/min
, vas.vaso_rate AS vasopressin -- units/hour
-- inodialators
, dob.vaso_rate AS dobutamine
, mil.vaso_rate AS milrinone
, dob.vaso_rate AS dobutamine -- mcg/kg/min
, mil.vaso_rate AS milrinone -- mcg/kg/min
-- isoproterenol is used in CCU/CVICU but not in metavision
-- other drugs not included here but (rarely) used in the BIDMC:
-- angiotensin II, methylene blue
Expand Down
3 changes: 3 additions & 0 deletions mimic-iv/concepts_postgres/medication/vasopressin.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
DROP TABLE IF EXISTS vasopressin; CREATE TABLE vasopressin AS
-- This query extracts dose+durations of vasopressin administration
-- Local hospital dosage guidance: 1.2 units/hour (low) - 2.4 units/hour (high)
SELECT
stay_id, linkorderid
-- three rows in units/min, rest in units/hour
-- the three rows in units/min look reasonable and
-- fit with the patient course

-- convert all rows to units/hour
, CASE WHEN rateuom = 'units/min' THEN rate * 60.0
ELSE rate END AS vaso_rate
, amount AS vaso_amount
Expand Down
1 change: 1 addition & 0 deletions mimic-iv/concepts_postgres/postgres-make-concepts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ SET search_path TO mimiciv_derived, mimiciv_hosp, mimiciv_icu, mimiciv_ed;
\i medication/epinephrine.sql
\i medication/milrinone.sql
\i medication/neuroblock.sql
\i medication/norepinephrine.sql
\i medication/phenylephrine.sql
\i medication/vasopressin.sql

Expand Down

0 comments on commit 04a11ba

Please sign in to comment.