Skip to content

Commit 81deb22

Browse files
authored
Merge pull request #1482 from MIT-LCP/postgres_shell_script_update
Postgres shell script update
2 parents 5c8a9a2 + d51b3b5 commit 81deb22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+691
-554
lines changed

mimic-iii/concepts/convert_mimiciii_concepts_bq_to_psql.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ do
150150
if ! [[ "$DIR_AND_TABLES_ALREADY_IN_PSQL" =~ "$d.$tbl" ]]; then
151151
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "${TARGET_PATH}/${d}/${tbl}.sql"
152152
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS " >> "${TARGET_PATH}/${d}/${tbl}.sql"
153-
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}"
153+
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}/${tbl}.sql"
154154
fi
155155
# write out a call to this script in the make concepts file
156156
echo "\i ${d}/${tbl}.sql" >> $TARGET_PATH/postgres-make-concepts.sql

mimic-iv/concepts/convert_bigquery_to_postgres.sh

Lines changed: 74 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ TARGET_PATH='../concepts_postgres'
66
mkdir -p $TARGET_PATH
77

88
# String replacements are necessary for some queries.
9-
export REGEX_SCHEMA='s/`physionet-data.(mimiciv_hosp|mimiciv_icu|mimiciv_derived).([A-Za-z0-9_-]+)`/\1.\2/g'
109
# Note that these queries are very senstive to changes, e.g. adding whitespaces after comma can already change the behavior.
10+
11+
# Schema replacement: change `physionet-data.<dataset>.<table>` to just <table> (with no backticks)
12+
export REGEX_SCHEMA='s/`physionet-data.(mimiciv_hosp|mimiciv_icu|mimiciv_derived).([A-Za-z0-9_-]+)`/\1.\2/g'
13+
# Add single quotes around the date part
1114
export REGEX_DATETIME_DIFF="s/DATETIME_DIFF\(([^,]+), ?(.*), ?(DAY|MINUTE|SECOND|HOUR|YEAR)\)/DATETIME_DIFF(\1, \2, '\3')/g"
1215
export REGEX_DATETIME_TRUNC="s/DATETIME_TRUNC\(([^,]+), ?(DAY|MINUTE|SECOND|HOUR|YEAR)\)/DATE_TRUNC('\2', \1)/g"
1316
# Add necessary quotes to INTERVAL, e.g. "INTERVAL 5 hour" to "INTERVAL '5' hour"
@@ -18,6 +21,24 @@ export REGEX_ARRAY="s/GENERATE_ARRAY\(-24, CEIL\(DATETIME\_DIFF\(it\.outtime_hr,
1821
export REGEX_HOUR_INTERVAL="s/INTERVAL CAST\(hr AS INT64\) HOUR/interval \'1\' hour * CAST\(hr AS bigint\)/g"
1922
export REGEX_SECONDS="s/SECOND\)/\'SECOND\'\)/g"
2023

24+
#=== Decide on the order queries are executed
25+
# Normally, queries will be executed in alphabetical order.
26+
# However, some tables depend on each other. We specify
27+
# which queries to run first, and in what order.
28+
29+
# tables we want to run before all other concepts
30+
# usually because they are used as dependencies
31+
DIR_AND_TABLES_TO_PREBUILD='demographics.icustay_times demographics.icustay_hourly demographics.weight_durations measurement.urine_output organfailure.kdigo_uo'
32+
33+
# tables which are written directly in postgresql and source code controlled
34+
# this is usually because there is no trivial conversion between bq/psql syntax
35+
DIR_AND_TABLES_ALREADY_IN_PSQL=''
36+
37+
# tables which we want to run after all other concepts
38+
# usually because they depend on one or more other queries
39+
# these will be generated in the order specified
40+
DIR_AND_TABLES_TO_SKIP='organfailure.kdigo_stages firstday.first_day_sofa sepsis.sepsis3 medication.vasoactive_agent medication.norepinephrine_equivalent_dose'
41+
2142
# First, we re-create the postgres-make-concepts.sql file.
2243
echo "\echo ''" > $TARGET_PATH/postgres-make-concepts.sql
2344

@@ -37,52 +58,58 @@ echo "-- NOTE: many scripts *require* you to use mimiciv_derived as the schema f
3758
echo "-- change the search path at your peril!" >> $TARGET_PATH/postgres-make-concepts.sql
3859
echo "SET search_path TO mimiciv_derived, mimiciv_hosp, mimiciv_icu, mimiciv_ed;" >> $TARGET_PATH/postgres-make-concepts.sql
3960

40-
# reporting to stdout the folder being run
61+
# ======================================== #
62+
# === CONCEPTS WHICH WE MUST RUN FIRST === #
63+
# ======================================== #
4164
echo -n "Dependencies:"
4265

4366
# output table creation calls to the make-concepts script
4467
echo "" >> $TARGET_PATH/postgres-make-concepts.sql
4568
echo "-- dependencies" >> $TARGET_PATH/postgres-make-concepts.sql
4669

47-
for dir_and_table in demographics.icustay_times demographics.weight_durations measurement.urine_output organfailure.kdigo_uo;
70+
for dir_and_table in $DIR_AND_TABLES_TO_PREBUILD;
4871
do
4972
d=`echo ${dir_and_table} | cut -d. -f1`
5073
tbl=`echo ${dir_and_table} | cut -d. -f2`
5174

75+
# catch special case when file is in current directory
76+
if [[ $d == '' ]]; then
77+
d='.'
78+
fi
79+
5280
# make the sub-folder for postgres if it does not exist
5381
mkdir -p "$TARGET_PATH/${d}"
5482

5583
# convert the bigquery script to psql and output it to the appropriate subfolder
5684
echo -n " ${d}.${tbl} .."
57-
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "$TARGET_PATH/${d}/${tbl}.sql"
58-
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS " >> "$TARGET_PATH/${d}/${tbl}.sql"
5985

60-
# apply regex to map bigquery syntax to postgres syntax
61-
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}" | sed -r -e "${REGEX_SECONDS}" >> "$TARGET_PATH/${d}/${tbl}.sql"
86+
# re-write the script into psql using regex
87+
# the if statement ensures we do not overwrite tables
88+
# for which we already have psql code
89+
if ! [[ "$DIR_AND_TABLES_ALREADY_IN_PSQL" =~ "$d.$tbl" ]]; then
90+
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "$TARGET_PATH/${d}/${tbl}.sql"
91+
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS" >> "${TARGET_PATH}/${d}/${tbl}.sql"
92+
93+
# apply regex to map bigquery syntax to postgres syntax
94+
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}" | sed -r -e "${REGEX_SECONDS}" >> "$TARGET_PATH/${d}/${tbl}.sql"
95+
else
96+
echo -n "(psql!) .."
97+
fi
6298

6399
# write out a call to this script in the make concepts file
64100
echo "\i ${d}/${tbl}.sql" >> $TARGET_PATH/postgres-make-concepts.sql
65101
done
66102
echo " done!"
67103

104+
# ================================== #
105+
# === MAIN LOOP FOR ALL CONCEPTS === #
106+
# ================================== #
107+
68108
# Iterate through each concept subfolder, and:
69109
# (1) apply the above regular expressions to update the script
70110
# (2) output to the postgres subfolder
71111
# (3) add a line to the postgres-make-concepts.sql script to generate this table
72112

73-
# we control the order by skipping tables listed in the below var
74-
DIR_AND_TABLES_TO_SKIP='demographics.icustay_times demographics.weight_durations measurement.urine_output organfailure.kdigo_uo organfailure.kdigo_stages firstday.first_day_sofa sepsis.sepsis3 medication.vasoactive_agent medication.norepinephrine_equivalent_dose'
75-
76-
# create an array to store tables for which the order of generation matters
77-
# i.e. these tables cannot be generated in alphabetical order, as done in the later loop
78-
TABLES_TO_SKIP=()
79-
for dir_and_table in $DIR_AND_TABLES_TO_SKIP;
80-
do
81-
tbl=`echo ${dir_and_table} | cut -d. -f2`
82-
TABLES_TO_SKIP+=($tbl)
83-
done
84-
85-
echo $TABLES_TO_SKIP
86113
# the order *only* matters during the conversion step because our loop is
87114
# inserting table build commands into the postgres-make-concepts.sql file
88115
for d in demographics measurement comorbidity medication treatment firstday organfailure score sepsis;
@@ -97,28 +124,35 @@ do
97124
if [[ "${fn: -4}" == ".sql" ]]; then
98125
# table name is file name minus extension
99126
tbl="${fn%????}"
100-
101-
# skip first_day_sofa as it depends on other firstday queries, we'll generate it later
102-
# we also skipped tables generated in the "Dependencies" loop above.
103-
if [[ "${tbl}" == "first_day_sofa" ]] || [[ "${tbl}" == "icustay_times" ]] || [[ "${tbl}" == "weight_durations" ]] || [[ "${tbl}" == "urine_output" ]] || [[ "${tbl}" == "kdigo_uo" ]] || [[ "${tbl}" == "sepsis3" ]]; then
104-
continue
127+
echo -n " ${tbl} "
128+
129+
if [[ "$DIR_AND_TABLES_TO_PREBUILD" =~ "$d.$tbl" ]]; then
130+
echo -n "(prebuilt!) .."
131+
continue
132+
elif [[ "$DIR_AND_TABLES_TO_SKIP" =~ "$d.$tbl" ]]; then
133+
echo -n "(skipping!) .."
134+
continue
135+
else
136+
echo -n ".."
105137
fi
106-
echo -n " ${tbl} .."
107-
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "${TARGET_PATH}/${d}/${tbl}.sql"
108-
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS " >> "${TARGET_PATH}/${d}/${tbl}.sql"
109-
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}"
110-
111-
if [[ ! " ${TABLES_TO_SKIP[*]} " =~ " ${tbl} " ]]; then
112-
# this table is *not* in our skip array
113-
# therefore, we print it out to the make concepts script
114-
echo "\i ${d}/${fn}" >> ${TARGET_PATH}/postgres-make-concepts.sql
138+
139+
# re-write the script into psql using regex
140+
# the if statement ensures we do not overwrite tables which are already written in psql
141+
if ! [[ "$DIR_AND_TABLES_ALREADY_IN_PSQL" =~ "$d.$tbl" ]]; then
142+
echo "-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY." > "${TARGET_PATH}/${d}/${tbl}.sql"
143+
echo "DROP TABLE IF EXISTS ${tbl}; CREATE TABLE ${tbl} AS" >> "${TARGET_PATH}/${d}/${tbl}.sql"
144+
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}"
115145
fi
146+
147+
# add statement to generate this table
148+
# in the make concepts script
149+
echo "\i ${d}/${fn}" >> ${TARGET_PATH}/postgres-make-concepts.sql
116150
fi
117151
done
118152
echo " done!"
119153
done
120154

121-
# finally generate first_day_sofa which depends on concepts in firstday folder
155+
# generate remaining concepts
122156
echo "" >> ${TARGET_PATH}/postgres-make-concepts.sql
123157
echo "-- final tables which were dependent on one or more prior tables" >> ${TARGET_PATH}/postgres-make-concepts.sql
124158

@@ -133,10 +167,12 @@ do
133167

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

139-
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}" | sed -r -e "${REGEX_SECONDS}" >> "$TARGET_PATH/${d}/${tbl}.sql"
174+
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}" | sed -r -e "${REGEX_SECONDS}" >> "$TARGET_PATH/${d}/${tbl}.sql"
175+
fi
140176

141177
# write out a call to this script in the make concepts file
142178
echo "\i ${d}/${tbl}.sql" >> $TARGET_PATH/postgres-make-concepts.sql

mimic-iv/concepts_postgres/comorbidity/charlson.sql

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2-
DROP TABLE IF EXISTS charlson; CREATE TABLE charlson AS
2+
DROP TABLE IF EXISTS charlson; CREATE TABLE charlson AS
33
-- ------------------------------------------------------------------
4-
-- This query extracts Charlson Comorbidity Index (CCI) based on the recorded ICD-9 and ICD-10 codes.
4+
-- This query extracts Charlson Comorbidity Index (CCI) based on the
5+
-- recorded ICD-9 and ICD-10 codes.
56
--
67
-- Reference for CCI:
7-
-- (1) Charlson ME, Pompei P, Ales KL, MacKenzie CR. (1987) A new method of classifying prognostic
8-
-- comorbidity in longitudinal studies: development and validation.J Chronic Dis; 40(5):373-83.
8+
-- (1) Charlson ME, Pompei P, Ales KL, MacKenzie CR. (1987) A new method
9+
-- of classifying prognostic comorbidity in longitudinal studies:
10+
-- development and validation.J Chronic Dis; 40(5):373-83.
911
--
10-
-- (2) Charlson M, Szatrowski TP, Peterson J, Gold J. (1994) Validation of a combined comorbidity
11-
-- index. J Clin Epidemiol; 47(11):1245-51.
12+
-- (2) Charlson M, Szatrowski TP, Peterson J, Gold J. (1994) Validation
13+
-- of a combined comorbidity index. J Clin Epidemiol; 47(11):1245-51.
1214
--
13-
-- Reference for ICD-9-CM and ICD-10 Coding Algorithms for Charlson Comorbidities:
14-
-- (3) Quan H, Sundararajan V, Halfon P, et al. Coding algorithms for defining Comorbidities in ICD-9-CM
15-
-- and ICD-10 administrative data. Med Care. 2005 Nov; 43(11): 1130-9.
15+
-- Reference for ICD-9-CM and ICD-10 Coding Algorithms for Charlson
16+
-- Comorbidities:
17+
-- (3) Quan H, Sundararajan V, Halfon P, et al. Coding algorithms for
18+
-- defining Comorbidities in ICD-9-CM and ICD-10 administrative data.
19+
-- Med Care. 2005 Nov; 43(11): 1130-9.
1620
-- ------------------------------------------------------------------
1721

1822
WITH diag AS (
@@ -281,7 +285,8 @@ WITH diag AS (
281285
THEN 1
282286
ELSE 0 END) AS renal_disease
283287

284-
-- Any malignancy, including lymphoma and leukemia, except malignant neoplasm of skin
288+
-- Any malignancy, including lymphoma and leukemia,
289+
-- except malignant neoplasm of skin.
285290
, MAX(CASE WHEN
286291
SUBSTR(icd9_code, 1, 3) BETWEEN '140' AND '172'
287292
OR
@@ -379,8 +384,9 @@ SELECT
379384
-- Calculate the Charlson Comorbidity Score using the original
380385
-- weights from Charlson, 1987.
381386
, age_score
382-
+ myocardial_infarct + congestive_heart_failure + peripheral_vascular_disease
383-
+ cerebrovascular_disease + dementia + chronic_pulmonary_disease
387+
+ myocardial_infarct + congestive_heart_failure
388+
+ peripheral_vascular_disease + cerebrovascular_disease
389+
+ dementia + chronic_pulmonary_disease
384390
+ rheumatic_disease + peptic_ulcer_disease
385391
+ GREATEST(mild_liver_disease, 3 * severe_liver_disease)
386392
+ GREATEST(2 * diabetes_with_cc, diabetes_without_cc)

mimic-iv/concepts_postgres/demographics/age.sql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2-
DROP TABLE IF EXISTS age; CREATE TABLE age AS
2+
DROP TABLE IF EXISTS age; CREATE TABLE age AS
33
-- This query calculates the age of a patient on admission to the hospital.
44

55
-- The columns of the table patients: anchor_age, anchor_year, anchor_year_group
6-
-- provide information regarding the actual patient year for the patient admission,
7-
-- and the patient's age at that time.
6+
-- provide information regarding the actual patient year for the patient
7+
-- admission, and the patient's age at that time.
88

99
-- anchor_year is a shifted year for the patient.
10-
-- anchor_year_group is a range of years - the patient's anchor_year occurred during this range.
10+
-- anchor_year_group is a range of years - the patient's anchor_year occurred
11+
-- during this range.
1112
-- anchor_age is the patient's age in the anchor_year.
1213
-- Example: a patient has an anchor_year of 2153,
1314
-- anchor_year_group of 2008 - 2010, and an anchor_age of 60.
1415
-- The year 2153 for the patient corresponds to 2008, 2009, or 2010.
15-
-- The patient was 60 in the shifted year of 2153, i.e. they were 60 in 2008, 2009, or 2010.
16+
-- The patient was 60 in the shifted year of 2153,
17+
-- i.e. they were 60 in 2008, 2009, or 2010.
1618
-- A patient admission in 2154 will occur in 2009-2011,
1719
-- an admission in 2155 will occur in 2010-2012, and so on.
1820

19-
-- Therefore, the age of a patient = hospital admission time - anchor_year + anchor_age
21+
-- Therefore, the age of a patient = admission time - anchor_year + anchor_age
2022
SELECT
2123
ad.subject_id
2224
, ad.hadm_id

mimic-iv/concepts_postgres/demographics/icustay_detail.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2-
DROP TABLE IF EXISTS icustay_detail; CREATE TABLE icustay_detail AS
2+
DROP TABLE IF EXISTS icustay_detail; CREATE TABLE icustay_detail AS
33
SELECT ie.subject_id, ie.hadm_id, ie.stay_id
44

55
-- patient level factors

mimic-iv/concepts_postgres/demographics/icustay_hourly.sql

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2-
DROP TABLE IF EXISTS icustay_hourly; CREATE TABLE icustay_hourly AS
2+
DROP TABLE IF EXISTS icustay_hourly; CREATE TABLE icustay_hourly AS
33
-- This query generates a row for every hour the patient is in the ICU.
44
-- The hours are based on clock-hours (i.e. 02:00, 03:00).
55
-- The hour clock starts 24 hours before the first heart rate measurement.
6-
-- Note that the time of the first heart rate measurement is ceilinged to the hour.
6+
-- Note that the time of the first heart rate measurement is ceilinged to
7+
-- the hour.
78

89
-- this query extracts the cohort and every possible hour they were in the ICU
9-
-- this table can be to other tables on ICUSTAY_ID and (ENDTIME - 1 hour,ENDTIME]
10+
-- this table can be to other tables on stay_id and (ENDTIME - 1 hour,ENDTIME]
1011

1112
-- get first/last measurement time
1213
WITH all_hours AS (
1314
SELECT
1415
it.stay_id
1516

16-
-- ceiling the intime to the nearest hour by adding 59 minutes then truncating
17-
-- note thart we truncate by parsing as string, rather than using DATETIME_TRUNC
18-
-- this is done to enable compatibility with psql
17+
-- ceiling the intime to the nearest hour by adding 59 minutes,
18+
-- then applying truncate by parsing as string
19+
-- string truncate is done to enable compatibility with psql
1920
, PARSE_DATETIME(
2021
'%Y-%m-%d %H:00:00'
2122
, FORMAT_DATETIME(
@@ -24,7 +25,8 @@ WITH all_hours AS (
2425
)) AS endtime
2526

2627
-- create integers for each charttime in hours from admission
27-
-- so 0 is admission time, 1 is one hour after admission, etc, up to ICU disch
28+
-- so 0 is admission time, 1 is one hour after admission, etc,
29+
-- up to ICU disch
2830
-- we allow 24 hours before ICU admission (to grab labs before admit)
2931
, ARRAY(SELECT * FROM generate_series(-24, CEIL(DATETIME_DIFF(it.outtime_hr, it.intime_hr, 'HOUR')))) AS hrs -- noqa: L016
3032
FROM mimiciv_derived.icustay_times it

mimic-iv/concepts_postgres/demographics/icustay_times.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2-
DROP TABLE IF EXISTS icustay_times; CREATE TABLE icustay_times AS
2+
DROP TABLE IF EXISTS icustay_times; CREATE TABLE icustay_times AS
33
-- create a table which has fuzzy boundaries on hospital admission
44
-- involves first creating a lag/lead version of disch/admit time
55
-- get first/last heart rate measurement during hospitalization for each stay_id

mimic-iv/concepts_postgres/demographics/weight_durations.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
2-
DROP TABLE IF EXISTS weight_durations; CREATE TABLE weight_durations AS
2+
DROP TABLE IF EXISTS weight_durations; CREATE TABLE weight_durations AS
33
-- This query extracts weights for adult ICU patients with start/stop times
44
-- if an admission weight is given, then this is assigned from intime to outtime
55
WITH wt_stg AS (
@@ -85,7 +85,8 @@ WITH wt_stg AS (
8585
-- if the intime for the patient is < the first charted daily weight
8686
-- then we will have a "gap" at the start of their stay
8787
-- to prevent this, we look for these gaps and backfill the first weight
88-
-- this adds (153255-149657)=3598 rows, meaning this fix helps for up to 3598 stay_id
88+
-- this adds (153255-149657)=3598 rows, meaning this fix helps for up
89+
-- to 3598 stay_id
8990
, wt_fix AS (
9091
SELECT ie.stay_id
9192
-- we add a 2 hour "fuzziness" window

0 commit comments

Comments
 (0)