-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-raw-fluids.R
executable file
·131 lines (118 loc) · 2.63 KB
/
get-raw-fluids.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
library(RPostgres)
library(dplyr)
library(readr)
library(lubridate)
source("scripts/common/setup-argparse.R")
source("scripts/common/logger-setup.R")
parser$add_argument("--inputs-cv-query")
parser$add_argument("--inputs-mv-query")
parser$add_argument("--outputs-query")
args <- parser$parse_args()
mimic <- dbConnect(
RPostgres::Postgres(),
dbname = "mimic",
host = "localhost",
port = 5432,
user = "amanderson",
password = "postgres",
timezone = "US/Eastern"
)
icustays <- dbGetQuery(
mimic,
statement =
"SELECT
subject_id,
hadm_id,
icustay_id,
intime
FROM
mimiciii.icustays;"
) %>%
as_tibble()
flog.info(
"mimic-example: querying carevue fluid data",
name = base_filename
)
inputs_cv <- dbGetQuery(
mimic,
statement = read_file(args$inputs_cv_query)
) %>%
select(
-c(subject_id, hadm_id, itemid),
charttime = charttime_equiv,
value = amount,
origin = orig_table
) %>%
as_tibble()
flog.info(
"mimic-example: querying metavision fluid data",
name = base_filename
)
inputs_mv <- dbGetQuery(
mimic,
statement = read_file(args$inputs_mv_query)
) %>%
select(
-c(subject_id, hadm_id, itemid),
charttime = charttime_equiv,
value = amount,
origin = orig_table
) %>%
as_tibble()
flog.info(
"mimic-example: querying outputs fluid data",
name = base_filename
)
outputs <- dbGetQuery(
mimic,
statement = read_file(args$outputs_query)
) %>%
mutate(
value = -1 * value # make the outputs negative, so that we can average them all.
) %>%
filter(value != 0) %>%
rename(origin = orig_table) %>%
as_tibble()
# Set time origin as admission time, then we can do a window from there.
# filter to complete cases as, as there are a handful of NA values
# due to bag/bottle changes with non-zero entries
# and missing admission identifiers.
relevant_fluid_events <- bind_rows(
inputs_cv,
inputs_mv,
outputs
) %>%
left_join(
icustays %>%
select(-subject_id, hadm_id),
by = 'icustay_id'
) %>%
mutate(
time_since_icu_adm = time_length(
charttime - intime,
unit = 'day'
)
) %>%
filter(complete.cases(.)) %>%
filter(
value > 0,
time_since_icu_adm > 0,
label != 'OR Crystalloid Intake',
label != 'OR Crystalloid',
label != 'Pre-Admission Intake',
label != 'OR Colloid',
label != 'OR Colloid Intake',
label != 'OR Packed RBC Intake',
label != "OR Packed RBC's",
label != 'OR FFP Intake',
label != 'OR FFP'
) %>%
as_tibble()
flog.info(
"mimic-example: writing raw fluid data",
name = base_filename
)
saveRDS(
file = args$output,
object = relevant_fluid_events
)