-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaki-postgres.py
1238 lines (1151 loc) · 52.3 KB
/
aki-postgres.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import psycopg2
from sqlalchemy import create_engine
import pandas as pd
def test_postgres(cursor):
cursor.execute("""
SELECT
*
FROM
pg_catalog.pg_tables
WHERE
schemaname != 'pg_catalog'
AND schemaname != 'information_schema';
""")
tables = cursor.fetchall()
print("List of tables:")
for table in tables:
print(table)
# construct an engine connection string
engine_string = "postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}".format(
user = "",
password = "",
host = "",
port = "5432",
database = mimicDB,
)
# create sqlalchemy engine
engine = create_engine(engine_string)
# read a table from database into pandas dataframe
for table in tables:
tn = table[1]
print("Table {}".format(tn))
df = pd.read_sql_table(tn, engine)
print("Head of extracted pandas DF:")
print(df.head())
def urine_output(cursor):
view ="DROP MATERIALIZED VIEW IF EXISTS urineoutput CASCADE; \
CREATE MATERIALIZED VIEW urineoutput as \
select oe.icustay_id, oe.charttime \
, SUM( \
case when oe.itemid = 227488 then -1*value \
else value end \
) as value \
from outputevents oe \
where oe.itemid in \
( \
40055, \
43175, \
40069, \
40094, \
40715, \
40473, \
40085, \
40057, \
40056, \
40405, \
40428, \
40086, \
40096, \
40651, \
226559, \
226560, \
226561, \
226584, \
226563, \
226564, \
226565, \
226567, \
226557, \
226558, \
227488, \
227489 \
) \
and oe.value < 5000 \
and oe.icustay_id is not null \
group by icustay_id, charttime;"
cursor.execute(view)
def creatinine(cursor):
view = "DROP MATERIALIZED VIEW IF EXISTS kdigo_creat CASCADE; \
CREATE MATERIALIZED VIEW kdigo_creat as \
with cr as \
( \
select \
ie.icustay_id \
, ie.intime, ie.outtime \
, le.valuenum as creat \
, le.charttime \
, ie.DBSOURCE \
from icustays ie \
left join labevents le \
on ie.subject_id = le.subject_id \
and le.ITEMID = 50912 \
and le.VALUENUM is not null \
and le.CHARTTIME between (ie.intime - interval '7' day) and (ie.intime + interval '7' day) \
) \
SELECT \
cr.icustay_id \
, cr.charttime \
, cr.creat \
, MIN(cr48.creat) AS creat_low_past_48hr \
, MIN(cr7.creat) AS creat_low_past_7day \
FROM cr \
LEFT JOIN cr cr48 \
ON cr.icustay_id = cr48.icustay_id \
AND cr48.charttime < cr.charttime \
AND cr48.charttime >= (cr.charttime - INTERVAL '48' HOUR) \
LEFT JOIN cr cr7 \
ON cr.icustay_id = cr7.icustay_id \
AND cr7.charttime < cr.charttime \
AND cr7.charttime >= (cr.charttime - INTERVAL '7' DAY) \
GROUP BY cr.icustay_id, cr.charttime, cr.creat \
ORDER BY cr.icustay_id, cr.charttime, cr.creat;"
cursor.execute(view)
def echo_data(cursor):
#-- This code extracts structured data from echocardiographies
#-- You can join it to the text notes using ROW_ID
#-- Just note that ROW_ID will differ across versions of MIMIC-III.
view = "DROP MATERIALIZED VIEW IF EXISTS ECHODATA CASCADE; \
CREATE MATERIALIZED VIEW ECHODATA AS \
select ROW_ID \
, subject_id, hadm_id \
, chartdate \
, cast(to_timestamp( (to_char( chartdate, 'DD-MM-YYYY' ) || substring(ne.text, 'Date/Time: [\[\]0-9*-]+ at ([0-9:]+)')), \
'DD-MM-YYYYHH24:MI') as timestamp without time zone) \
as charttime \
, substring(ne.text, 'Indication: (.*?)\n') as Indication \
, case \
when substring(ne.text, 'Height: \(in\) (.*?)\n') like '%*%' \
then null \
else cast(substring(ne.text, 'Height: \(in\) (.*?)\n') as numeric) \
end as Height \
, case \
when substring(ne.text, 'Weight \(lb\): (.*?)\n') like '%*%' \
then null \
else cast(substring(ne.text, 'Weight \(lb\): (.*?)\n') as numeric) \
end as Weight \
, case \
when substring(ne.text, 'BSA \(m2\): (.*?) m2\n') like '%*%' \
then null \
else cast(substring(ne.text, 'BSA \(m2\): (.*?) m2\n') as numeric) \
end as BSA \
, substring(ne.text, 'BP \(mm Hg\): (.*?)\n') as BP \
, case \
when substring(ne.text, 'BP \(mm Hg\): ([0-9]+)/[0-9]+?\n') like '%*%' \
then null \
else cast(substring(ne.text, 'BP \(mm Hg\): ([0-9]+)/[0-9]+?\n') as numeric) \
end as BPSys \
, case \
when substring(ne.text, 'BP \(mm Hg\): [0-9]+/([0-9]+?)\n') like '%*%' \
then null \
else cast(substring(ne.text, 'BP \(mm Hg\): [0-9]+/([0-9]+?)\n') as numeric) \
end as BPDias \
, case \
when substring(ne.text, 'HR \(bpm\): ([0-9]+?)\n') like '%*%' \
then null \
else cast(substring(ne.text, 'HR \(bpm\): ([0-9]+?)\n') as numeric) \
end as HR \
, substring(ne.text, 'Status: (.*?)\n') as Status \
, substring(ne.text, 'Test: (.*?)\n') as Test \
, substring(ne.text, 'Doppler: (.*?)\n') as Doppler \
, substring(ne.text, 'Contrast: (.*?)\n') as Contrast \
, substring(ne.text, 'Technical Quality: (.*?)\n') as TechnicalQuality \
from noteevents ne \
where category = 'Echo';"
cursor.execute(view)
def weight_duration(cursor):
#-- This query extracts weights for ICU patients with start/stop times
#-- if only an admission weight is given, then this is assigned from intime to outtime
view = " DROP MATERIALIZED VIEW IF EXISTS weightdurations CASCADE; \
CREATE MATERIALIZED VIEW weightdurations as \
WITH wt_neonate AS \
(\
SELECT c.icustay_id, c.charttime \
, MAX(CASE WHEN c.itemid = 3580 THEN c.valuenum END) as wt_kg \
, MAX(CASE WHEN c.itemid = 3581 THEN c.valuenum END) as wt_lb \
, MAX(CASE WHEN c.itemid = 3582 THEN c.valuenum END) as wt_oz \
FROM chartevents c \
WHERE c.itemid in (3580, 3581, 3582) \
AND c.icustay_id IS NOT NULL \
AND c.error IS DISTINCT FROM 1 \
AND c.valuenum > 0 \
GROUP BY c.icustay_id, c.charttime \
) \
, birth_wt AS \
( \
SELECT c.icustay_id, c.charttime \
, MAX( \
CASE \
WHEN c.itemid = 4183 THEN \
CASE \
WHEN c.value ~ '[^0-9\.]' THEN NULL \
WHEN CAST(c.value AS NUMERIC) > 100 THEN CAST(c.value AS NUMERIC)/1000 \
WHEN CAST(c.value AS NUMERIC) < 10 THEN CAST(c.value AS NUMERIC) \
ELSE NULL END \
WHEN c.itemid = 3723 AND c.valuenum < 10 THEN c.valuenum \
ELSE NULL END) as wt_kg \
FROM chartevents c \
WHERE c.itemid in (3723, 4183) \
AND c.icustay_id IS NOT NULL \
AND c.error IS DISTINCT FROM 1 \
GROUP BY c.icustay_id, c.charttime \
) \
, wt_stg as \
( \
SELECT \
c.icustay_id \
, c.charttime \
, case when c.itemid in (762,226512) then 'admit' \
else 'daily' end as weight_type \
, c.valuenum as weight \
FROM chartevents c \
WHERE c.valuenum IS NOT NULL \
AND c.itemid in \
( \
762,226512 \
, 763,224639 \
) \
AND c.icustay_id IS NOT NULL \
AND c.valuenum > 0 \
AND c.error IS DISTINCT FROM 1 \
UNION ALL \
SELECT \
n.icustay_id \
, n.charttime \
, 'daily' AS weight_type \
, CASE \
WHEN wt_kg IS NOT NULL THEN wt_kg \
WHEN wt_lb IS NOT NULL THEN wt_lb*0.45359237 + wt_oz*0.0283495231 \
ELSE NULL END AS weight \
FROM wt_neonate n \
UNION ALL \
SELECT \
b.icustay_id \
, b.charttime \
, 'admit' AS weight_type \
, wt_kg as weight \
FROM birth_wt b \
) \
, wt_stg1 as \
( \
select \
icustay_id \
, charttime \
, weight_type \
, weight \
, ROW_NUMBER() OVER (partition by icustay_id, weight_type order by charttime) as rn \
from wt_stg \
WHERE weight IS NOT NULL \
) \
, wt_stg2 AS \
( \
SELECT \
wt_stg1.icustay_id \
, ie.intime, ie.outtime \
, ie.DBSOURCE \
, case when wt_stg1.weight_type = 'admit' and wt_stg1.rn = 1 \
then ie.intime - interval '2' hour \
else wt_stg1.charttime end as starttime \
, wt_stg1.weight \
from wt_stg1 \
INNER JOIN icustays ie \
on ie.icustay_id = wt_stg1.icustay_id \
) \
, wt_stg3 as \
( \
select \
icustay_id \
, intime, outtime \
, starttime \
, DBSOURCE \
, coalesce( \
LEAD(starttime) OVER (PARTITION BY icustay_id ORDER BY starttime), \
outtime + interval '2' hour \
) as endtime \
, weight \
from wt_stg2 \
) \
, wt1 as \
( \
select \
icustay_id \
, starttime \
, DBSOURCE \
, coalesce(endtime, \
LEAD(starttime) OVER (partition by icustay_id order by starttime), \
outtime + interval '2' hour) \
as endtime \
, weight \
from wt_stg3 \
) \
, wt_fix as \
( \
select ie.icustay_id \
, ie.intime - interval '2' hour as starttime \
, wt.starttime as endtime \
, wt.weight \
from icustays ie \
inner join \
( \
SELECT wt1.icustay_id, wt1.starttime, wt1.weight , wt1.DBSOURCE \
, ROW_NUMBER() OVER (PARTITION BY wt1.icustay_id ORDER BY wt1.starttime) as rn \
FROM wt1 \
) wt \
ON ie.icustay_id = wt.icustay_id \
AND wt.rn = 1 \
and ie.intime < wt.starttime \
) \
, wt2 as \
( \
select \
wt1.icustay_id \
, wt1.starttime \
, wt1.endtime \
, wt1.weight \
from wt1 \
UNION \
SELECT \
wt_fix.icustay_id \
, wt_fix.starttime \
, wt_fix.endtime \
, wt_fix.weight \
from wt_fix \
) \
, echo_lag as \
( \
select \
ie.icustay_id \
, ie.intime, ie.outtime \
, 0.453592*ec.weight as weight_echo \
, ROW_NUMBER() OVER (PARTITION BY ie.icustay_id ORDER BY ec.charttime) as rn \
, ec.charttime as starttime \
, LEAD(ec.charttime) OVER (PARTITION BY ie.icustay_id ORDER BY ec.charttime) as endtime \
from icustays ie \
inner join echodata ec \
on ie.hadm_id = ec.hadm_id \
where ec.weight is not null \
) \
, echo_final as \
( \
select \
el.icustay_id \
, el.starttime \
, coalesce(el.endtime, el.outtime + interval '2' hour) as endtime \
, weight_echo \
from echo_lag el \
UNION \
select \
el.icustay_id \
, el.intime - interval '2' hour as starttime \
, el.starttime as endtime \
, el.weight_echo \
from echo_lag el \
where el.rn = 1 \
and el.starttime > el.intime - interval '2' hour \
) \
select \
wt2.icustay_id, wt2.starttime, wt2.endtime, wt2.weight\
from wt2 \
UNION \
select \
ef.icustay_id, ef.starttime, ef.endtime, ef.weight_echo as weight \
from echo_final ef \
where ef.icustay_id not in (select distinct icustay_id from wt2) \
order by icustay_id, starttime, endtime;"
cursor.execute(view)
def urine_kidigo(cursor):
# -- we have joined each row to all rows preceding within 24 hours \
# -- we can now sum these rows to get total UO over the last 24 hours \
# -- we can use case statements to restrict it to only the last 6/12 hours \
# -- therefore we have three sums: \
# -- 1) over a 6 hour period \
# -- 2) over a 12 hour period \
# -- 3) over a 24 hour period \
# -- note that we assume data charted at charttime corresponds to 1 hour of UO \
# -- therefore we use '5' and '11' to restrict the period, rather than 6/12 \
# -- this assumption may overestimate UO rate when documentation is done less than hourly \
# -- 6 hours \
view= " DROP MATERIALIZED VIEW IF EXISTS kdigo_uo CASCADE; \
CREATE MATERIALIZED VIEW kdigo_uo AS \
with ur_stg as \
( \
select io.icustay_id, io.charttime \
, sum(case when io.charttime <= iosum.charttime + interval '5' hour \
then iosum.VALUE \
else null end) as UrineOutput_6hr \
, sum(case when io.charttime <= iosum.charttime + interval '11' hour \
then iosum.VALUE \
else null end) as UrineOutput_12hr \
, sum(iosum.VALUE) as UrineOutput_24hr \
, ROUND(CAST(EXTRACT(EPOCH FROM \
io.charttime - \
MIN(case when io.charttime <= iosum.charttime + interval '5' hour \
then iosum.charttime \
else null end) \
)/3600.0 AS NUMERIC), 4) AS uo_tm_6hr \
, ROUND(CAST(EXTRACT(EPOCH FROM \
io.charttime - \
MIN(case when io.charttime <= iosum.charttime + interval '11' hour \
then iosum.charttime \
else null end) \
)/3600.0 AS NUMERIC), 4) AS uo_tm_12hr \
, ROUND(CAST(EXTRACT(EPOCH FROM \
io.charttime - MIN(iosum.charttime) \
)/3600.0 AS NUMERIC), 4) AS uo_tm_24hr \
from urineoutput io \
left join urineoutput iosum \
on io.icustay_id = iosum.icustay_id \
and io.charttime >= iosum.charttime \
and io.charttime <= (iosum.charttime + interval '23' hour) \
group by io.icustay_id, io.charttime \
) \
select \
ur.icustay_id \
, ur.charttime \
, wd.weight \
, ur.UrineOutput_6hr \
, ur.UrineOutput_12hr \
, ur.UrineOutput_24hr \
, ROUND((ur.UrineOutput_6hr/wd.weight/(uo_tm_6hr+1))::NUMERIC, 4) AS uo_rt_6hr \
, ROUND((ur.UrineOutput_12hr/wd.weight/(uo_tm_12hr+1))::NUMERIC, 4) AS uo_rt_12hr \
, ROUND((ur.UrineOutput_24hr/wd.weight/(uo_tm_24hr+1))::NUMERIC, 4) AS uo_rt_24hr \
, uo_tm_6hr \
, uo_tm_12hr \
, uo_tm_24hr \
from ur_stg ur \
left join weightdurations wd \
on ur.icustay_id = wd.icustay_id \
and ur.charttime >= wd.starttime \
and ur.charttime < wd.endtime \
order by icustay_id, charttime; "
cursor.execute(view)
def kidigo_48h(cursor):
#-- This query checks if the patient had AKI during the first 48 hours of their ICU
#-- stay according to the KDIGO guideline.
#-- https://kdigo.org/wp-content/uploads/2016/10/KDIGO-2012-AKI-Guideline-English.pdf
view = "DROP MATERIALIZED VIEW IF EXISTS kdigo_stages_48hr; \
CREATE MATERIALIZED VIEW kdigo_stages_48hr AS \
WITH cr_aki AS \
( \
SELECT \
k.icustay_id \
, k.charttime \
, k.creat \
, k.DBSOURCE \
, k.aki_stage_creat \
, ROW_NUMBER() OVER (PARTITION BY k.icustay_id ORDER BY k.aki_stage_creat DESC, k.creat DESC) AS rn \
FROM icustays ie \
INNER JOIN kdigo_stages k \
ON ie.icustay_id = k.icustay_id \
WHERE k.charttime > (ie.intime - interval '6' hour) \
AND k.charttime <= (ie.intime + interval '48' hour) \
AND k.aki_stage_creat IS NOT NULL \
) \
, uo_aki AS \
( \
SELECT \
k.icustay_id \
, k.DBSOURCE \
, k.charttime \
, k.uo_rt_6hr, k.uo_rt_12hr, k.uo_rt_24hr \
, k.aki_stage_uo \
, ROW_NUMBER() OVER \
( \
PARTITION BY k.icustay_id \
ORDER BY k.aki_stage_uo DESC, k.uo_rt_24hr DESC, k.uo_rt_12hr DESC, k.uo_rt_6hr DESC \
) AS rn \
FROM icustays ie \
INNER JOIN kdigo_stages k \
ON ie.icustay_id = k.icustay_id \
WHERE k.charttime > (ie.intime - interval '6' hour) \
AND k.charttime <= (ie.intime + interval '48' hour) \
AND k.aki_stage_uo IS NOT NULL \
) \
select \
ie.icustay_id \
, ie.DBSOURCE \
, cr.charttime as charttime_creat \
, cr.creat \
, cr.aki_stage_creat \
, uo.charttime as charttime_uo \
, uo.uo_rt_6hr \
, uo.uo_rt_12hr \
, uo.uo_rt_24hr \
, uo.aki_stage_uo \
, GREATEST(cr.aki_stage_creat,uo.aki_stage_uo) AS aki_stage_48hr \
, CASE WHEN GREATEST(cr.aki_stage_creat, uo.aki_stage_uo) > 0 THEN 1 ELSE 0 END AS aki_48hr \
FROM icustays ie \
LEFT JOIN cr_aki cr \
ON ie.icustay_id = cr.icustay_id \
AND cr.rn = 1 \
LEFT JOIN uo_aki uo \
ON ie.icustay_id = uo.icustay_id \
AND uo.rn = 1 \
order by ie.icustay_id; "
cursor.execute(view)
def kidigo_7_days_creatinine(cursor):
#-- This query checks if the patient had AKI during the first 7 days of their ICU
#-- stay according to the KDIGO guideline.
#-- https://kdigo.org/wp-content/uploads/2016/10/KDIGO-2012-AKI-Guideline-English.pdf
view = "DROP MATERIALIZED VIEW IF EXISTS kdigo_7_days_creatinine; \
CREATE MATERIALIZED VIEW kdigo_7_days_creatinine AS \
WITH cr_aki AS \
( \
SELECT \
k.icustay_id \
, k.DBSOURCE \
, k.charttime \
, k.creat \
, k.aki_stage_creat \
, ROW_NUMBER() OVER (PARTITION BY k.icustay_id ORDER BY k.aki_stage_creat DESC, k.creat DESC) AS rn \
FROM icustays ie \
INNER JOIN kdigo_stages_creatinine k \
ON ie.icustay_id = k.icustay_id \
WHERE k.charttime > (ie.intime - interval '6' hour) \
AND k.charttime <= (ie.intime + interval '7' day) \
AND k.aki_stage_creat IS NOT NULL \
) \
select \
ie.icustay_id \
, ie.DBSOURCE \
, cr.charttime as charttime_creat \
, cr.creat \
, cr.aki_stage_creat \
, cr.aki_stage_creat AS aki_stage_7day \
, CASE WHEN (cr.aki_stage_creat > 0) THEN 1 ELSE 0 END AS aki_7day \
FROM icustays ie \
LEFT JOIN cr_aki cr \
ON ie.icustay_id = cr.icustay_id \
AND cr.rn = 1 \
order by ie.icustay_id; "
cursor.execute(view)
def kidigo_stages_creatinine(cursor):
#-- This query checks if the patient had AKI according to KDIGO.
#-- AKI is calculated every time a creatinine or urine output measurement occurs.
#-- Baseline creatinine is defined as the lowest creatinine in the past 7 days.
view = " DROP MATERIALIZED VIEW IF EXISTS kdigo_stages_creatinine CASCADE; \
CREATE MATERIALIZED VIEW kdigo_stages_creatinine AS \
with cr_stg AS \
( \
SELECT \
cr.icustay_id \
, cr.charttime \
, cr.creat \
, case \
when cr.creat >= (cr.creat_low_past_7day*3.0) then 3 \
when cr.creat >= 4 \
and (cr.creat_low_past_48hr <= 3.7 OR cr.creat >= (1.5*cr.creat_low_past_7day)) \
then 3 \
when cr.creat >= (cr.creat_low_past_7day*2.0) then 2 \
when cr.creat >= (cr.creat_low_past_48hr+0.3) then 1 \
when cr.creat >= (cr.creat_low_past_7day*1.5) then 1 \
else 0 end as aki_stage_creat \
FROM kdigo_creat cr \
) \
, tm_stg AS \
( \
SELECT \
icustay_id, charttime \
FROM cr_stg \
) \
select \
ie.icustay_id \
, ie.DBSOURCE \
, tm.charttime \
, cr.creat \
, cr.aki_stage_creat \
, cr.aki_stage_creat AS aki_stage \
FROM icustays ie \
LEFT JOIN tm_stg tm \
ON ie.icustay_id = tm.icustay_id \
LEFT JOIN cr_stg cr \
ON ie.icustay_id = cr.icustay_id \
AND tm.charttime = cr.charttime \
order by ie.icustay_id, tm.charttime; "
cursor.execute(view)
def kidigo_7_days(cursor):
#-- This query checks if the patient had AKI during the first 7 days of their ICU
#-- stay according to the KDIGO guideline.
#-- https://kdigo.org/wp-content/uploads/2016/10/KDIGO-2012-AKI-Guideline-English.pdf
view = "DROP MATERIALIZED VIEW IF EXISTS kdigo_stages_7day; \
CREATE MATERIALIZED VIEW kdigo_stages_7day AS \
WITH cr_aki AS \
( \
SELECT \
k.icustay_id \
, k.DBSOURCE \
, k.charttime \
, k.creat \
, k.aki_stage_creat \
, ROW_NUMBER() OVER (PARTITION BY k.icustay_id ORDER BY k.aki_stage_creat DESC, k.creat DESC) AS rn \
FROM icustays ie \
INNER JOIN kdigo_stages k \
ON ie.icustay_id = k.icustay_id \
WHERE k.charttime > (ie.intime - interval '6' hour) \
AND k.charttime <= (ie.intime + interval '7' day) \
AND k.aki_stage_creat IS NOT NULL \
) \
, uo_aki AS \
( \
SELECT \
k.icustay_id \
, k.DBSOURCE \
, k.charttime \
, k.uo_rt_6hr, k.uo_rt_12hr, k.uo_rt_24hr \
, k.aki_stage_uo \
, ROW_NUMBER() OVER \
( \
PARTITION BY k.icustay_id \
ORDER BY k.aki_stage_uo DESC, k.uo_rt_24hr DESC, k.uo_rt_12hr DESC, k.uo_rt_6hr DESC \
) AS rn \
FROM icustays ie \
INNER JOIN kdigo_stages k \
ON ie.icustay_id = k.icustay_id \
WHERE k.charttime > (ie.intime - interval '6' hour) \
AND k.charttime <= (ie.intime + interval '7' day) \
AND k.aki_stage_uo IS NOT NULL \
) \
select \
ie.icustay_id \
, ie.DBSOURCE \
, cr.charttime as charttime_creat \
, cr.creat \
, cr.aki_stage_creat \
, uo.charttime as charttime_uo \
, uo.uo_rt_6hr \
, uo.uo_rt_12hr \
, uo.uo_rt_24hr \
, uo.aki_stage_uo \
, GREATEST(cr.aki_stage_creat,uo.aki_stage_uo) AS aki_stage_7day \
, CASE WHEN GREATEST(cr.aki_stage_creat, uo.aki_stage_uo) > 0 THEN 1 ELSE 0 END AS aki_7day \
FROM icustays ie \
LEFT JOIN cr_aki cr \
ON ie.icustay_id = cr.icustay_id \
AND cr.rn = 1 \
LEFT JOIN uo_aki uo \
ON ie.icustay_id = uo.icustay_id \
AND uo.rn = 1 \
order by ie.icustay_id; "
cursor.execute(view)
def kidigo_stages(cursor):
#-- This query checks if the patient had AKI according to KDIGO.
#-- AKI is calculated every time a creatinine or urine output measurement occurs.
#-- Baseline creatinine is defined as the lowest creatinine in the past 7 days.
view = " DROP MATERIALIZED VIEW IF EXISTS kdigo_stages CASCADE; \
CREATE MATERIALIZED VIEW kdigo_stages AS \
with cr_stg AS \
( \
SELECT \
cr.icustay_id \
, cr.charttime \
, cr.creat \
, case \
when cr.creat >= (cr.creat_low_past_7day*3.0) then 3 \
when cr.creat >= 4 \
and (cr.creat_low_past_48hr <= 3.7 OR cr.creat >= (1.5*cr.creat_low_past_7day)) \
then 3 \
when cr.creat >= (cr.creat_low_past_7day*2.0) then 2 \
when cr.creat >= (cr.creat_low_past_48hr+0.3) then 1 \
when cr.creat >= (cr.creat_low_past_7day*1.5) then 1 \
else 0 end as aki_stage_creat \
FROM kdigo_creat cr \
) \
, uo_stg as \
( \
select \
uo.icustay_id \
, ie.DBSOURCE \
, uo.charttime \
, uo.weight \
, uo.uo_rt_6hr \
, uo.uo_rt_12hr \
, uo.uo_rt_24hr \
, CASE \
WHEN uo.uo_rt_6hr IS NULL THEN NULL \
WHEN uo.charttime <= ie.intime + interval '6' hour THEN 0 \
WHEN uo.uo_tm_24hr >= 11 AND uo.uo_rt_24hr < 0.3 THEN 3 \
WHEN uo.uo_tm_12hr >= 5 AND uo.uo_rt_12hr = 0 THEN 3 \
WHEN uo.uo_tm_12hr >= 5 AND uo.uo_rt_12hr < 0.5 THEN 2 \
WHEN uo.uo_tm_6hr >= 2 AND uo.uo_rt_6hr < 0.5 THEN 1 \
ELSE 0 END AS aki_stage_uo \
from kdigo_uo uo \
INNER JOIN icustays ie \
ON uo.icustay_id = ie.icustay_id \
) \
, tm_stg AS \
( \
SELECT \
icustay_id, charttime \
FROM cr_stg \
UNION \
SELECT \
icustay_id, charttime \
FROM uo_stg \
) \
select \
ie.icustay_id \
, ie.DBSOURCE \
, tm.charttime \
, cr.creat \
, cr.aki_stage_creat \
, uo.uo_rt_6hr \
, uo.uo_rt_12hr \
, uo.uo_rt_24hr \
, uo.aki_stage_uo \
, GREATEST(cr.aki_stage_creat, uo.aki_stage_uo) AS aki_stage \
FROM icustays ie \
LEFT JOIN tm_stg tm \
ON ie.icustay_id = tm.icustay_id \
LEFT JOIN cr_stg cr \
ON ie.icustay_id = cr.icustay_id \
AND tm.charttime = cr.charttime \
LEFT JOIN uo_stg uo \
ON ie.icustay_id = uo.icustay_id \
AND tm.charttime = uo.charttime \
order by ie.icustay_id, tm.charttime; "
cursor.execute(view)
def get_labevents(cursor):
#-- This query pivots lab values taken during the 7 first days of a patient's stay
#-- Have already confirmed that the unit of measurement is always the same: null or the correct unit
#-- Extract all bicarbonate, blood urea nitrogen (BUN), calcium, chloride, creatinine,
#hemoglobin, international normalized ratio (INR), platelet, potassium, prothrombin time (PT),
#partial throm- boplastin time (PTT), and white blood count (WBC) values from labevents around patient's ICU stay
view = "DROP MATERIALIZED VIEW IF EXISTS labstay CASCADE; \
CREATE materialized VIEW labstay AS \
SELECT \
pvt.subject_id, pvt.hadm_id, pvt.icustay_id , pvt.DBSOURCE \
, min(CASE WHEN label = 'ANION GAP' THEN valuenum ELSE null END) as ANIONGAP_min \
, max(CASE WHEN label = 'ANION GAP' THEN valuenum ELSE null END) as ANIONGAP_max \
, min(CASE WHEN label = 'ALBUMIN' THEN valuenum ELSE null END) as ALBUMIN_min \
, max(CASE WHEN label = 'ALBUMIN' THEN valuenum ELSE null END) as ALBUMIN_max \
, min(CASE WHEN label = 'BANDS' THEN valuenum ELSE null END) as BANDS_min \
, max(CASE WHEN label = 'BANDS' THEN valuenum ELSE null END) as BANDS_max \
, min(CASE WHEN label = 'BICARBONATE' THEN valuenum ELSE null END) as BICARBONATE_min \
, max(CASE WHEN label = 'BICARBONATE' THEN valuenum ELSE null END) as BICARBONATE_max \
, min(CASE WHEN label = 'BILIRUBIN' THEN valuenum ELSE null END) as BILIRUBIN_min \
, max(CASE WHEN label = 'BILIRUBIN' THEN valuenum ELSE null END) as BILIRUBIN_max \
, min(CASE WHEN label = 'CREATININE' THEN valuenum ELSE null END) as CREATININE_min \
, max(CASE WHEN label = 'CREATININE' THEN valuenum ELSE null END) as CREATININE_max \
, min(CASE WHEN label = 'CHLORIDE' THEN valuenum ELSE null END) as CHLORIDE_min \
, max(CASE WHEN label = 'CHLORIDE' THEN valuenum ELSE null END) as CHLORIDE_max \
, min(CASE WHEN label = 'GLUCOSE' THEN valuenum ELSE null END) as GLUCOSE_min \
, max(CASE WHEN label = 'GLUCOSE' THEN valuenum ELSE null END) as GLUCOSE_max \
, min(CASE WHEN label = 'HEMATOCRIT' THEN valuenum ELSE null END) as HEMATOCRIT_min \
, max(CASE WHEN label = 'HEMATOCRIT' THEN valuenum ELSE null END) as HEMATOCRIT_max \
, min(CASE WHEN label = 'HEMOGLOBIN' THEN valuenum ELSE null END) as HEMOGLOBIN_min \
, max(CASE WHEN label = 'HEMOGLOBIN' THEN valuenum ELSE null END) as HEMOGLOBIN_max \
, min(CASE WHEN label = 'LACTATE' THEN valuenum ELSE null END) as LACTATE_min \
, max(CASE WHEN label = 'LACTATE' THEN valuenum ELSE null END) as LACTATE_max \
, min(CASE WHEN label = 'PLATELET' THEN valuenum ELSE null END) as PLATELET_min \
, max(CASE WHEN label = 'PLATELET' THEN valuenum ELSE null END) as PLATELET_max \
, min(CASE WHEN label = 'POTASSIUM' THEN valuenum ELSE null END) as POTASSIUM_min \
, max(CASE WHEN label = 'POTASSIUM' THEN valuenum ELSE null END) as POTASSIUM_max \
, min(CASE WHEN label = 'PTT' THEN valuenum ELSE null END) as PTT_min \
, max(CASE WHEN label = 'PTT' THEN valuenum ELSE null END) as PTT_max \
, min(CASE WHEN label = 'INR' THEN valuenum ELSE null END) as INR_min \
, max(CASE WHEN label = 'INR' THEN valuenum ELSE null END) as INR_max \
, min(CASE WHEN label = 'PT' THEN valuenum ELSE null END) as PT_min \
, max(CASE WHEN label = 'PT' THEN valuenum ELSE null END) as PT_max \
, min(CASE WHEN label = 'SODIUM' THEN valuenum ELSE null END) as SODIUM_min \
, max(CASE WHEN label = 'SODIUM' THEN valuenum ELSE null end) as SODIUM_max \
, min(CASE WHEN label = 'BUN' THEN valuenum ELSE null end) as BUN_min \
, max(CASE WHEN label = 'BUN' THEN valuenum ELSE null end) as BUN_max \
, min(CASE WHEN label = 'WBC' THEN valuenum ELSE null end) as WBC_min \
, max(CASE WHEN label = 'WBC' THEN valuenum ELSE null end) as WBC_max \
FROM \
( SELECT ie.subject_id, ie.hadm_id, ie.icustay_id ,ie.DBSOURCE \
, CASE \
WHEN itemid = 50868 THEN 'ANION GAP' \
WHEN itemid = 50862 THEN 'ALBUMIN' \
WHEN itemid = 51144 THEN 'BANDS' \
WHEN itemid = 50882 THEN 'BICARBONATE' \
WHEN itemid = 50885 THEN 'BILIRUBIN' \
WHEN itemid = 50912 THEN 'CREATININE' \
WHEN itemid = 50806 THEN 'CHLORIDE' \
WHEN itemid = 50902 THEN 'CHLORIDE' \
WHEN itemid = 50809 THEN 'GLUCOSE' \
WHEN itemid = 50931 THEN 'GLUCOSE' \
WHEN itemid = 50810 THEN 'HEMATOCRIT' \
WHEN itemid = 51221 THEN 'HEMATOCRIT' \
WHEN itemid = 50811 THEN 'HEMOGLOBIN' \
WHEN itemid = 51222 THEN 'HEMOGLOBIN' \
WHEN itemid = 50813 THEN 'LACTATE' \
WHEN itemid = 51265 THEN 'PLATELET' \
WHEN itemid = 50822 THEN 'POTASSIUM' \
WHEN itemid = 50971 THEN 'POTASSIUM' \
WHEN itemid = 51275 THEN 'PTT' \
WHEN itemid = 51237 THEN 'INR' \
WHEN itemid = 51274 THEN 'PT' \
WHEN itemid = 50824 THEN 'SODIUM' \
WHEN itemid = 50983 THEN 'SODIUM' \
WHEN itemid = 51006 THEN 'BUN' \
WHEN itemid = 51300 THEN 'WBC' \
WHEN itemid = 51301 THEN 'WBC' \
ELSE null \
END AS label \
, CASE \
WHEN itemid = 50862 and valuenum > 10 THEN null \
WHEN itemid = 50868 and valuenum > 10000 THEN null \
WHEN itemid = 51144 and valuenum < 0 THEN null \
WHEN itemid = 51144 and valuenum > 100 THEN null \
WHEN itemid = 50882 and valuenum > 10000 THEN null \
WHEN itemid = 50885 and valuenum > 150 THEN null \
WHEN itemid = 50806 and valuenum > 10000 THEN null \
WHEN itemid = 50902 and valuenum > 10000 THEN null \
WHEN itemid = 50912 and valuenum > 150 THEN null \
WHEN itemid = 50809 and valuenum > 10000 THEN null \
WHEN itemid = 50931 and valuenum > 10000 THEN null \
WHEN itemid = 50810 and valuenum > 100 THEN null \
WHEN itemid = 51221 and valuenum > 100 THEN null \
WHEN itemid = 50811 and valuenum > 50 THEN null \
WHEN itemid = 51222 and valuenum > 50 THEN null \
WHEN itemid = 50813 and valuenum > 50 THEN null \
WHEN itemid = 51265 and valuenum > 10000 THEN null \
WHEN itemid = 50822 and valuenum > 30 THEN null \
WHEN itemid = 50971 and valuenum > 30 THEN null \
WHEN itemid = 51275 and valuenum > 150 THEN null \
WHEN itemid = 51237 and valuenum > 50 THEN null \
WHEN itemid = 51274 and valuenum > 150 THEN null \
WHEN itemid = 50824 and valuenum > 200 THEN null \
WHEN itemid = 50983 and valuenum > 200 THEN null \
WHEN itemid = 51006 and valuenum > 300 THEN null \
WHEN itemid = 51300 and valuenum > 1000 THEN null \
WHEN itemid = 51301 and valuenum > 1000 THEN null \
ELSE le.valuenum \
END AS valuenum \
FROM icustays ie \
LEFT JOIN labevents le \
ON le.subject_id = ie.subject_id AND le.hadm_id = ie.hadm_id \
AND le.CHARTTIME between (ie.intime - interval '6' hour) and (ie.intime + interval '7' day)\
AND le.ITEMID in \
( \
50868, \
50862, \
51144, \
50882, \
50885, \
50912, \
50902, \
50806, \
50931, \
50809, \
51221, \
50810, \
51222, \
50811, \
50813, \
51265, \
50971, \
50822, \
51275, \
51237, \
51274, \
50983, \
50824, \
51006, \
51301, \
51300 \
) \
AND valuenum IS NOT null AND valuenum > 0 \
) pvt \
GROUP BY pvt.subject_id, pvt.hadm_id, pvt.icustay_id , pvt.DBSOURCE \
ORDER BY pvt.subject_id, pvt.hadm_id, pvt.icustay_id, pvt.DBSOURCE;"
cursor.execute(view)
def get_vitals_chart(cursor):
# -- This query pivots the vital signs during the first 7 days of a patient's stay
#-- Vital signs include heart rate, blood pressure, respiration rate, and temperature
view = "DROP MATERIALIZED VIEW IF EXISTS vitalsfirstday CASCADE; \
create materialized view vitalsfirstday as \
SELECT pvt.subject_id, pvt.hadm_id, pvt.icustay_id, pvt.DBSOURCE \
, min(case when VitalID = 1 then valuenum else null end) as HeartRate_Min \
, max(case when VitalID = 1 then valuenum else null end) as HeartRate_Max \
, avg(case when VitalID = 1 then valuenum else null end) as HeartRate_Mean \
, min(case when VitalID = 2 then valuenum else null end) as SysBP_Min \
, max(case when VitalID = 2 then valuenum else null end) as SysBP_Max \
, avg(case when VitalID = 2 then valuenum else null end) as SysBP_Mean \
, min(case when VitalID = 3 then valuenum else null end) as DiasBP_Min \
, max(case when VitalID = 3 then valuenum else null end) as DiasBP_Max \
, avg(case when VitalID = 3 then valuenum else null end) as DiasBP_Mean \
, min(case when VitalID = 4 then valuenum else null end) as MeanBP_Min \
, max(case when VitalID = 4 then valuenum else null end) as MeanBP_Max \
, avg(case when VitalID = 4 then valuenum else null end) as MeanBP_Mean \
, min(case when VitalID = 5 then valuenum else null end) as RespRate_Min \
, max(case when VitalID = 5 then valuenum else null end) as RespRate_Max \
, avg(case when VitalID = 5 then valuenum else null end) as RespRate_Mean \
, min(case when VitalID = 6 then valuenum else null end) as TempC_Min \
, max(case when VitalID = 6 then valuenum else null end) as TempC_Max \
, avg(case when VitalID = 6 then valuenum else null end) as TempC_Mean \
, min(case when VitalID = 7 then valuenum else null end) as SpO2_Min \
, max(case when VitalID = 7 then valuenum else null end) as SpO2_Max \
, avg(case when VitalID = 7 then valuenum else null end) as SpO2_Mean \
, min(case when VitalID = 8 then valuenum else null end) as Glucose_Min \
, max(case when VitalID = 8 then valuenum else null end) as Glucose_Max \
, avg(case when VitalID = 8 then valuenum else null end) as Glucose_Mean \
FROM ( \
select ie.subject_id, ie.hadm_id, ie.icustay_id, ie.DBSOURCE\
, case \
when itemid in (211,220045) and valuenum > 0 and valuenum < 300 then 1 \
when itemid in (51,442,455,6701,220179,220050) and valuenum > 0 and valuenum < 400 then 2 \
when itemid in (8368,8440,8441,8555,220180,220051) and valuenum > 0 and valuenum < 300 then 3 \
when itemid in (456,52,6702,443,220052,220181,225312) and valuenum > 0 and valuenum < 300 then 4 \
when itemid in (615,618,220210,224690) and valuenum > 0 and valuenum < 70 then 5 \
when itemid in (223761,678) and valuenum > 70 and valuenum < 120 then 6 \
when itemid in (223762,676) and valuenum > 10 and valuenum < 50 then 6 \
when itemid in (646,220277) and valuenum > 0 and valuenum <= 100 then 7 \
when itemid in (807,811,1529,3745,3744,225664,220621,226537) and valuenum > 0 then 8 \
else null end as VitalID \
, case when itemid in (223761,678) then (valuenum-32)/1.8 else valuenum end as valuenum \
from icustays ie \
left join chartevents ce \
on ie.subject_id = ce.subject_id and ie.hadm_id = ce.hadm_id and ie.icustay_id = ce.icustay_id \
and ce.charttime between ie.intime - interval '6' hour and ie.intime + interval '7' day \
and ce.error IS DISTINCT FROM 1 \
where ce.itemid in \
( \
211, \
220045, \
51, \
442, \
455, \
6701, \
220179, \
220050, \
8368, \
8440, \
8441, \
8555, \
220180, \
220051, \
456, \
52, \
6702, \
443, \
220052, \
220181, \
225312, \
618, \
615, \
220210, \
224690, \
646, 220277, \
807, \
811, \
1529, \
3745, \