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 mimic-iii/concepts/severityscores/apsiii.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ with pa as
(
select bg.icustay_id, bg.charttime
, po2 as PaO2
, ROW_NUMBER() over (partition by bg.icustay_id ORDER BY bg.po2 DESC) as rn
, ROW_NUMBER() over (partition by bg.icustay_id ORDER BY bg.po2 ASC) as rn
from `physionet-data.mimiciii_derived.blood_gas_first_day_arterial` bg
left join `physionet-data.mimiciii_derived.ventilation_durations` vd
on bg.icustay_id = vd.icustay_id
Expand Down
2 changes: 1 addition & 1 deletion mimic-iii/concepts_duckdb/severityscores/apsiii.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WITH pa AS (
bg.icustay_id,
bg.charttime,
po2 AS PaO2,
ROW_NUMBER() OVER (PARTITION BY bg.icustay_id ORDER BY bg.po2 DESC) AS rn
ROW_NUMBER() OVER (PARTITION BY bg.icustay_id ORDER BY bg.po2 ASC NULLS FIRST) AS rn
FROM mimiciii_derived.blood_gas_first_day_arterial AS bg
LEFT JOIN mimiciii_derived.ventilation_durations AS vd
ON bg.icustay_id = vd.icustay_id
Expand Down
40 changes: 37 additions & 3 deletions mimic-iii/concepts_postgres/severityscores/apsiii.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
DROP TABLE IF EXISTS mimiciii_derived.apsiii; CREATE TABLE mimiciii_derived.apsiii AS
/* ------------------------------------------------------------------ */ /* Title: Acute Physiology Score III (APS III) */ /* This query extracts the acute physiology score III. */ /* This score is a measure of patient severity of illness. */ /* The score is calculated on the first day of each ICU patients' stay. */ /* ------------------------------------------------------------------ */ /* Reference for APS III: */ /* Knaus WA, Wagner DP, Draper EA, Zimmerman JE, Bergner M, Bastos PG, Sirio CA, Murphy DJ, Lotring T, Damiano A. */ /* The APACHE III prognostic system. Risk prediction of hospital mortality for critically ill hospitalized adults. */ /* Chest Journal. 1991 Dec 1;100(6):1619-36. */ /* Reference for the equation for calibrating APS III to hospital mortality: */ /* Johnson, A. E. W. (2015). Mortality prediction and acuity assessment in critical care. */ /* University of Oxford, Oxford, UK. */ /* Variables used in APS III: */ /* GCS */ /* VITALS: Heart rate, mean blood pressure, temperature, respiration rate */ /* FLAGS: ventilation/cpap, chronic dialysis */ /* IO: urine output */ /* LABS: pao2, A-aDO2, hematocrit, WBC, creatinine */ /* , blood urea nitrogen, sodium, albumin, bilirubin, glucose, pH, pCO2 */ /* The following views are required to run this query: */ /* 1) urine_output_first_day - generated by urine-output-first-day.sql */ /* 2) ventilation_first_day - generated by ventilated-first-day.sql */ /* 3) vitals_first_day - generated by vitals-first-day.sql */ /* 4) gcs_first_day - generated by gcs-first-day.sql */ /* 5) labs_first_day - generated by labs-first-day.sql */ /* Note: */ /* The score is calculated for *all* ICU patients, with the assumption that the user will subselect appropriate icustay_ids. */ /* For example, the score is calculated for neonates, but it is likely inappropriate to actually use the score values for these patients. */ /* List of TODO: */ /* The site of temperature is not incorporated. Axillary measurements should be increased by 1 degree. */ /* Unfortunately the data for metavision is not available at the moment. */ /* 674 | Temp. Site */ /* 224642 | Temperature Site */
/* ------------------------------------------------------------------ */
/* Title: Acute Physiology Score III (APS III) */
/* This query extracts the acute physiology score III. */
/* This score is a measure of patient severity of illness. */
/* The score is calculated on the first day of each ICU patients' stay. */
/* ------------------------------------------------------------------ */
/* Reference for APS III: */
/* Knaus WA, Wagner DP, Draper EA, Zimmerman JE, Bergner M, Bastos PG, Sirio CA, Murphy DJ, Lotring T, Damiano A. */
/* The APACHE III prognostic system. Risk prediction of hospital mortality for critically ill hospitalized adults. */
/* Chest Journal. 1991 Dec 1;100(6):1619-36. */
/* Reference for the equation for calibrating APS III to hospital mortality: */
/* Johnson, A. E. W. (2015). Mortality prediction and acuity assessment in critical care. */
/* University of Oxford, Oxford, UK. */
/* Variables used in APS III: */
/* GCS */
/* VITALS: Heart rate, mean blood pressure, temperature, respiration rate */
/* FLAGS: ventilation/cpap, chronic dialysis */
/* IO: urine output */
/* LABS: pao2, A-aDO2, hematocrit, WBC, creatinine */
/* , blood urea nitrogen, sodium, albumin, bilirubin, glucose, pH, pCO2 */
/* The following views are required to run this query: */
/* 1) urine_output_first_day - generated by urine-output-first-day.sql */
/* 2) ventilation_first_day - generated by ventilated-first-day.sql */
/* 3) vitals_first_day - generated by vitals-first-day.sql */
/* 4) gcs_first_day - generated by gcs-first-day.sql */
/* 5) labs_first_day - generated by labs-first-day.sql */
/* Note: */
/* The score is calculated for *all* ICU patients, with the assumption that the user will subselect appropriate icustay_ids. */
/* For example, the score is calculated for neonates, but it is likely inappropriate to actually use the score values for these patients. */
/* List of TODO: */
/* The site of temperature is not incorporated. Axillary measurements should be increased by 1 degree. */
/* Unfortunately the data for metavision is not available at the moment. */
/* 674 | Temp. Site */
/* 224642 | Temperature Site */
WITH pa AS (
SELECT
bg.icustay_id,
bg.charttime,
po2 AS PaO2,
ROW_NUMBER() OVER (PARTITION BY bg.icustay_id ORDER BY bg.po2 DESC NULLS LAST) AS rn
ROW_NUMBER() OVER (PARTITION BY bg.icustay_id ORDER BY bg.po2 ASC NULLS FIRST) AS rn
FROM mimiciii_derived.blood_gas_first_day_arterial AS bg
LEFT JOIN mimiciii_derived.ventilation_durations AS vd
ON bg.icustay_id = vd.icustay_id
Expand All @@ -17,7 +50,8 @@ WITH pa AS (
AND /* and fio2 < 50, or if no fio2, assume room air */ COALESCE(fio2, fio2_chartevents, 21) < 50
AND NOT bg.po2 IS NULL
), aa AS (
/* join blood gas to ventilation durations to determine if patient was vent */ /* also join to cpap table for the same purpose */
/* join blood gas to ventilation durations to determine if patient was vent */
/* also join to cpap table for the same purpose */
SELECT
bg.icustay_id,
bg.charttime,
Expand Down
2 changes: 1 addition & 1 deletion mimic-iv/concepts/score/apsiii.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
WITH pa AS (
SELECT ie.stay_id, bg.charttime
, po2 AS pao2
, ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 DESC) AS rn
, ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 ASC) AS rn
FROM `physionet-data.mimiciv_derived.bg` bg
INNER JOIN `physionet-data.mimiciv_icu.icustays` ie
ON bg.hadm_id = ie.hadm_id
Expand Down
2 changes: 1 addition & 1 deletion mimic-iv/concepts_duckdb/score/apsiii.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WITH pa AS (
ie.stay_id,
bg.charttime,
po2 AS pao2,
ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 DESC) AS rn
ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 ASC NULLS FIRST) AS rn
FROM mimiciv_derived.bg AS bg
INNER JOIN mimiciv_icu.icustays AS ie
ON bg.hadm_id = ie.hadm_id
Expand Down
34 changes: 31 additions & 3 deletions mimic-iv/concepts_postgres/score/apsiii.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
DROP TABLE IF EXISTS mimiciv_derived.apsiii; CREATE TABLE mimiciv_derived.apsiii AS
/* ------------------------------------------------------------------ */ /* Title: Acute Physiology Score III (APS III) */ /* This query extracts the acute physiology score III. */ /* This score is a measure of patient severity of illness. */ /* The score is calculated on the first day of each ICU patients' stay. */ /* ------------------------------------------------------------------ */ /* Reference for APS III: */ /* Knaus WA, Wagner DP, Draper EA, Zimmerman JE, Bergner M, */ /* Bastos PG, Sirio CA, Murphy DJ, Lotring T, Damiano A. */ /* The APACHE III prognostic system. Risk prediction of hospital */ /* mortality for critically ill hospitalized adults. Chest Journal. */ /* 1991 Dec 1;100(6):1619-36. */ /* Reference for the equation for calibrating APS III: */ /* Johnson, A. E. W. (2015). Mortality prediction and acuity assessment */ /* in critical care. University of Oxford, Oxford, UK. */ /* Variables used in APS III: */ /* GCS */ /* VITALS: Heart rate, mean blood pressure, temperature, respiration rate */ /* FLAGS: ventilation/cpap, chronic dialysis */ /* IO: urine output */ /* LABS: pao2, A-aDO2, hematocrit, WBC, creatinine */ /* , blood urea nitrogen, sodium, albumin, bilirubin, glucose, pH, pCO2 */ /* Note: */ /* The score is calculated for *all* ICU patients, with the assumption that */ /* the user will subselect appropriate stay_ids. */ /* List of TODO: */ /* The site of temperature is not incorporated. Axillary measurements */ /* should be increased by 1 degree. */
/* ------------------------------------------------------------------ */
/* Title: Acute Physiology Score III (APS III) */
/* This query extracts the acute physiology score III. */
/* This score is a measure of patient severity of illness. */
/* The score is calculated on the first day of each ICU patients' stay. */
/* ------------------------------------------------------------------ */
/* Reference for APS III: */
/* Knaus WA, Wagner DP, Draper EA, Zimmerman JE, Bergner M, */
/* Bastos PG, Sirio CA, Murphy DJ, Lotring T, Damiano A. */
/* The APACHE III prognostic system. Risk prediction of hospital */
/* mortality for critically ill hospitalized adults. Chest Journal. */
/* 1991 Dec 1;100(6):1619-36. */
/* Reference for the equation for calibrating APS III: */
/* Johnson, A. E. W. (2015). Mortality prediction and acuity assessment */
/* in critical care. University of Oxford, Oxford, UK. */
/* Variables used in APS III: */
/* GCS */
/* VITALS: Heart rate, mean blood pressure, temperature, respiration rate */
/* FLAGS: ventilation/cpap, chronic dialysis */
/* IO: urine output */
/* LABS: pao2, A-aDO2, hematocrit, WBC, creatinine */
/* , blood urea nitrogen, sodium, albumin, bilirubin, glucose, pH, pCO2 */
/* Note: */
/* The score is calculated for *all* ICU patients, with the assumption that */
/* the user will subselect appropriate stay_ids. */
/* List of TODO: */
/* The site of temperature is not incorporated. Axillary measurements */
/* should be increased by 1 degree. */
WITH pa AS (
SELECT
ie.stay_id,
bg.charttime,
po2 AS pao2,
ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 DESC NULLS LAST) AS rn
ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 ASC NULLS FIRST) AS rn
FROM mimiciv_derived.bg AS bg
INNER JOIN mimiciv_icu.icustays AS ie
ON bg.hadm_id = ie.hadm_id
Expand All @@ -23,7 +50,8 @@ WITH pa AS (
AND NOT bg.po2 IS NULL
AND bg.specimen = 'ART.'
), aa AS (
/* join blood gas to ventilation durations to determine if patient was vent */ /* also join to cpap table for the same purpose */
/* join blood gas to ventilation durations to determine if patient was vent */
/* also join to cpap table for the same purpose */
SELECT
ie.stay_id,
bg.charttime,
Expand Down
134 changes: 134 additions & 0 deletions tests/test_apsiii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"""Regression tests for APS III worst-PaO2 selection in MIMIC-IV and MIMIC-III."""
from pathlib import Path

import pytest

from mimic_utils.transpile import transpile_query

REPO_ROOT = Path(__file__).resolve().parent.parent
MIMIC_III_SCHEMA_MAP = {"mimiciii_clinical": "mimiciii", "mimiciii_notes": "mimiciii"}


def test_mimic_iv_apsiii_uses_lowest_pao2():
duckdb = pytest.importorskip("duckdb")
con = duckdb.connect()
con.execute(
"CREATE SCHEMA mimiciv_icu;"
"CREATE SCHEMA mimiciv_hosp;"
"CREATE SCHEMA mimiciv_derived;"
)
con.execute(
"""
CREATE TABLE mimiciv_icu.icustays AS
SELECT 1 AS subject_id, 10 AS hadm_id, 100 AS stay_id,
TIMESTAMP '2150-01-01 00:00:00' AS intime,
TIMESTAMP '2150-01-03 00:00:00' AS outtime;
CREATE TABLE mimiciv_hosp.admissions AS SELECT 10 AS hadm_id;
CREATE TABLE mimiciv_hosp.patients AS SELECT 1 AS subject_id;
CREATE TABLE mimiciv_derived.bg AS
SELECT 10 AS hadm_id, TIMESTAMP '2150-01-01 02:00:00' AS charttime,
45.0 AS po2, CAST(NULL AS DOUBLE) AS aado2,
CAST(NULL AS DOUBLE) AS ph, CAST(NULL AS DOUBLE) AS pco2,
21.0 AS fio2, CAST(NULL AS DOUBLE) AS fio2_chartevents,
'ART.' AS specimen
UNION ALL
SELECT 10, TIMESTAMP '2150-01-01 06:00:00',
95.0, NULL, NULL, NULL, 21.0, NULL, 'ART.';
CREATE TABLE mimiciv_derived.ventilation (
stay_id INT, starttime TIMESTAMP, endtime TIMESTAMP,
ventilation_status VARCHAR
);
CREATE TABLE mimiciv_derived.first_day_urine_output (
stay_id INT, urineoutput DOUBLE
);
CREATE TABLE mimiciv_derived.first_day_lab (
stay_id INT, hematocrit_min DOUBLE, hematocrit_max DOUBLE,
wbc_min DOUBLE, wbc_max DOUBLE, creatinine_min DOUBLE,
creatinine_max DOUBLE, bun_min DOUBLE, bun_max DOUBLE,
sodium_min DOUBLE, sodium_max DOUBLE, albumin_min DOUBLE,
albumin_max DOUBLE, bilirubin_total_min DOUBLE,
bilirubin_total_max DOUBLE, glucose_min DOUBLE, glucose_max DOUBLE
);
CREATE TABLE mimiciv_hosp.diagnoses_icd (
hadm_id INT, icd_version INT, icd_code VARCHAR
);
CREATE TABLE mimiciv_derived.first_day_gcs (
stay_id INT, gcs_min INT, gcs_motor INT, gcs_verbal INT,
gcs_eyes INT, gcs_unable INT
);
CREATE TABLE mimiciv_derived.first_day_vitalsign (
stay_id INT, heart_rate_min DOUBLE, heart_rate_max DOUBLE,
mbp_min DOUBLE, mbp_max DOUBLE, temperature_min DOUBLE,
temperature_max DOUBLE, resp_rate_min DOUBLE, resp_rate_max DOUBLE,
glucose_min DOUBLE, glucose_max DOUBLE
);
"""
)

bq = (REPO_ROOT / "mimic-iv" / "concepts" / "score" / "apsiii.sql").read_text()
sql = transpile_query(bq, "bigquery", "duckdb")
row = con.execute(sql).fetchdf().iloc[0]
assert row["pao2_aado2_score"] == 15
assert row["apsiii"] == 15


def test_mimic_iii_apsiii_uses_lowest_pao2():
duckdb = pytest.importorskip("duckdb")
con = duckdb.connect()
con.execute("CREATE SCHEMA mimiciii; CREATE SCHEMA mimiciii_derived;")
con.execute(
"""
CREATE TABLE mimiciii.icustays AS
SELECT 1 AS subject_id, 10 AS hadm_id, 100 AS icustay_id,
TIMESTAMP '2150-01-01 00:00:00' AS intime,
TIMESTAMP '2150-01-03 00:00:00' AS outtime;
CREATE TABLE mimiciii.admissions AS SELECT 10 AS hadm_id;
CREATE TABLE mimiciii.patients AS SELECT 1 AS subject_id;
CREATE TABLE mimiciii_derived.blood_gas_first_day_arterial AS
SELECT 100 AS icustay_id, TIMESTAMP '2150-01-01 02:00:00' AS charttime,
45.0 AS po2, CAST(NULL AS DOUBLE) AS aado2,
CAST(NULL AS DOUBLE) AS ph, CAST(NULL AS DOUBLE) AS pco2,
21.0 AS fio2, CAST(NULL AS DOUBLE) AS fio2_chartevents
UNION ALL
SELECT 100, TIMESTAMP '2150-01-01 06:00:00',
95.0, NULL, NULL, NULL, 21.0, NULL;
CREATE TABLE mimiciii_derived.ventilation_durations (
icustay_id INT, starttime TIMESTAMP, endtime TIMESTAMP
);
CREATE TABLE mimiciii_derived.ventilation_first_day (
icustay_id INT, vent INT
);
CREATE TABLE mimiciii_derived.urine_output_first_day (
icustay_id INT, urineoutput DOUBLE
);
CREATE TABLE mimiciii_derived.labs_first_day (
icustay_id INT, hematocrit_min DOUBLE, hematocrit_max DOUBLE,
wbc_min DOUBLE, wbc_max DOUBLE, creatinine_min DOUBLE,
creatinine_max DOUBLE, bun_min DOUBLE, bun_max DOUBLE,
sodium_min DOUBLE, sodium_max DOUBLE, albumin_min DOUBLE,
albumin_max DOUBLE, bilirubin_min DOUBLE, bilirubin_max DOUBLE,
glucose_min DOUBLE, glucose_max DOUBLE
);
CREATE TABLE mimiciii.diagnoses_icd (
hadm_id INT, icd9_code VARCHAR
);
CREATE TABLE mimiciii_derived.gcs_first_day (
icustay_id INT, mingcs INT, gcsmotor INT, gcsverbal INT,
gcseyes INT, endotrachflag INT
);
CREATE TABLE mimiciii_derived.vitals_first_day (
icustay_id INT, heartrate_min DOUBLE, heartrate_max DOUBLE,
meanbp_min DOUBLE, meanbp_max DOUBLE, tempc_min DOUBLE,
tempc_max DOUBLE, resprate_min DOUBLE, resprate_max DOUBLE,
glucose_min DOUBLE, glucose_max DOUBLE
);
"""
)

bq = (
REPO_ROOT / "mimic-iii" / "concepts" / "severityscores" / "apsiii.sql"
).read_text(encoding="utf-8-sig")
sql = transpile_query(bq, "bigquery", "duckdb", MIMIC_III_SCHEMA_MAP)
row = con.execute(sql).fetchdf().iloc[0]
assert row["pao2_aado2_score"] == 15
assert row["apsiii"] == 15