-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_medication.py
More file actions
109 lines (97 loc) · 3.64 KB
/
test_medication.py
File metadata and controls
109 lines (97 loc) · 3.64 KB
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
import pandas as pd
from pandas.io import gbq
import logging
_LOGGER = logging.getLogger(__name__)
def test_vasopressor_units(dataset, project_id):
# verify vasopressors in expected units
units = {
'milrinone': 'mcg/kg/min',
'dobutamine': 'mcg/kg/min',
'dopamine': 'mcg/kg/min',
'epinephrine': 'mcg/kg/min',
'norepinephrine': 'mcg/kg/min',
'phenylephrine': 'mcg/kg/min',
'vasopressin': 'units/hour',
}
itemids = {
'milrinone': 221986,
'dobutamine': 221653,
'dopamine': 221662,
'epinephrine': 221289,
'norepinephrine': 221906,
'phenylephrine': 221749,
'vasopressin': 222315,
}
hadm_id = {
'norepinephrine': [21898267],
'phenylephrine': [26809360],
'vasopressin': [26272149]
}
# verify we always have a unit of measure for the rate
query = f"""
select itemid, COUNT(*) AS n
FROM mimic_icu.inputevents
WHERE itemid IN ({", ".join([str(x) for x in itemids.values()])})
AND rateuom IS NULL
GROUP BY itemid
"""
df = gbq.read_gbq(query, project_id=project_id, dialect="standard")
assert df.shape[0] == 0, 'found vasopressors with null units'
# norepinephrine has two rows in mg/kg/min
# these are actually supposed to be mcg/kg/min - and the patient weight has been set to 1 to make it work
# phenylephrine has one row in mcg/min - looks fine, within expected dose
# vasopressin three rows in units/min - these look OK
for drug, hadm_id_list in hadm_id.items():
query = f"""
select hadm_id, rate, rateuom
FROM mimic_icu.inputevents
WHERE itemid = {itemids[drug]}
AND rateuom != '{units[drug]}'
LIMIT 10
"""
df = gbq.read_gbq(query, project_id=project_id, dialect="standard")
# if we find new uninspected rows, raise a warning. this will only happen when mimic-iv is updated.
if (~df['hadm_id'].isin(hadm_id_list)).any():
_LOGGER.warn(f"""New data found with non-standard unit. Inspect the data with this query:
select *
from `physionet-data.mimic_icu.inputevents`
where itemid = {itemids['vasopressin']}
and stay_id in (
select stay_id from `physionet-data.mimic_icu.inputevents`
where itemid = {itemids['vasopressin']}
and rateuom != '{units['vasopressin']}'
)
order by starttime
""")
assert df.shape[0] != 10, f'many rows found with non-standard unit for {drug}'
def test_vasopressor_doses(dataset, project_id):
# verify vasopressors have reasonable doses
# based on uptodate graphic 99963 version 19.0
# double the maximum dose used in refractory shock is the upper limit used
itemids = {
'milrinone': 221986,
'dobutamine': 221653,
'dopamine': 221662,
'epinephrine': 221289,
'norepinephrine': 221906,
'phenylephrine': 221749,
'vasopressin': 222315,
}
max_dose = {
'milrinone': 1.5,
'dobutamine': 40,
'dopamine': 40,
'epinephrine': 4,
'norepinephrine': 6.6,
'phenylephrine': 18.2,
'vasopressin': 0.08,
}
for vaso, dose in max_dose.items():
query = f"""
select COUNT(vaso_rate) AS n_above_rate
FROM mimic_derived.{vaso}
WHERE vaso_rate >= {dose}
"""
df = gbq.read_gbq(query, project_id=project_id, dialect="standard")
n_above_rate = df.loc[0, 'n_above_rate']
assert n_above_rate == 0, f'found {vaso} rows with dose above {dose}, potentially incorrect'