In [1]:
# Import libraries
import pandas as pd
import matplotlib.pyplot as plt
import psycopg2
import os

# Plot settings
%matplotlib inline
plt.style.use('ggplot')
fontsize = 20 # size for x and y ticks
plt.rcParams['legend.fontsize'] = fontsize
plt.rcParams.update({'font.size': fontsize})

In [2]:
# create a database connection
sqluser = 'postgres'
dbname = 'eicu'
schema_name = 'eicu_crd'
sqlhost = 'localhost'
sqlport = 5432
# Connect to the database
con = psycopg2.connect(dbname=dbname, user=sqluser, host=sqlhost, port=sqlport)

In [3]:
query_schema = 'set search_path to public,eicu_crd_phi;'
query = query_schema + """
with t1 as
(
select
    patientunitstayid
    , treatmentoffset
    , case
        when treatmentstring like 'pulmonary|ventilation and oxygenation|mechanical ventilation%' then 1
        when treatmentstring like 'surgery|pulmonary therapies|mechanical ventilation%' then 1
        when treatmentstring like 'toxicology|drug overdose|mechanical ventilation%' then 1
    else 0 end as mechvent
from treatment
)
select 
    patientunitstayid
    , min(treatmentoffset) as mvstart
    , max(treatmentoffset) as mvend
from t1
where mechvent = 1
group by patientunitstayid
order by patientunitstayid
"""

tr = pd.read_sql_query(query, con)

In [8]:
query_schema = 'set search_path to public,eicu_crd_phi;'
query = query_schema + """
select
    apv.patientunitstayid
    , oOBIntubDay1 as mv_apache
from apachepredvar apv
inner join apachepatientresult apr
on apv.patientunitstayid = apr.patientunitstayid
and apacheversion = 'IVa'
where apr.predictedhospitalmortality != '-1'
"""

ap = pd.read_sql_query(query, con)

In [16]:
# cross-reference
df = ap.merge(tr, how='left', on='patientunitstayid').copy()
df['mv_treatment'] = (df['mvstart'] < 1440).astype(int)

pd.crosstab(df['mv_apache'],df['mv_treatment'], margins=True, normalize=True)


Out[16]:
mv_treatment 0 1 All
mv_apache
0 0.726071 0.012853 0.738924
1 0.055536 0.205540 0.261076
All 0.781607 0.218393 1.000000

In [15]:
df.loc[~df['mvstart'].isnull(),:]


Out[15]:
patientunitstayid mv_apache mvstart mvend mv_treatment
4709 242154 1 204.0 4683.0 1
4732 243283 1 33.0 503.0 1
4735 243494 1 392.0 3851.0 1
4737 243643 1 77.0 142.0 1
4745 243990 1 103.0 125.0 1
4751 244255 0 -389.0 -306.0 1
4757 244445 1 184.0 3162.0 1
4763 244718 1 27.0 1134.0 1
4765 244763 0 993.0 1125.0 1
4767 244975 1 74.0 1601.0 1
4775 245542 1 151.0 151.0 1
4784 245956 1 118.0 1257.0 1
4785 246128 1 87.0 87.0 1
4787 246393 1 104.0 1609.0 1
4790 246449 1 78.0 78.0 1
4792 246531 1 131.0 131.0 1
4798 246953 1 25.0 1374.0 1
4800 247030 1 50.0 50.0 1
4801 247048 1 45.0 45.0 1
4802 247164 1 851.0 2255.0 1
4803 247206 1 213.0 1471.0 1
4804 247249 0 2088.0 2088.0 1
4807 247296 1 149.0 4272.0 1
4808 247363 1 142.0 540.0 1
4809 247408 1 1259.0 8363.0 1
4814 247748 1 32.0 32.0 1
4820 247872 1 48.0 1038.0 1
4821 247888 1 77.0 4168.0 1
4828 248438 1 375.0 375.0 1
4830 248581 1 11.0 26621.0 1
... ... ... ... ... ...
136147 3352097 1 47.0 47.0 1
136148 3352103 1 134.0 134.0 1
136149 3352114 1 75.0 75.0 1
136150 3352119 1 109.0 129.0 1
136151 3352125 1 84.0 1304.0 1
136153 3352178 1 145.0 145.0 1
136155 3352203 1 137.0 4370.0 1
136163 3352336 1 59.0 59.0 1
136164 3352340 1 203.0 203.0 1
136165 3352344 1 99.0 99.0 1
136167 3352402 1 44.0 44.0 1
136168 3352437 1 96.0 7382.0 1
136170 3352445 1 451.0 7922.0 1
136176 3352526 1 321.0 7224.0 1
136181 3352546 1 67.0 714.0 1
136182 3352569 1 11.0 2256.0 1
136187 3352618 1 84.0 9412.0 1
136188 3352620 0 2051.0 2051.0 1
136191 3352711 1 80.0 6212.0 1
136192 3352721 1 308.0 20292.0 1
136201 3352801 1 309.0 310.0 1
136202 3352827 0 1192.0 21916.0 1
136204 3352861 1 140.0 598.0 1
136217 3352986 1 453.0 51720.0 1
136221 3353031 1 95.0 95.0 1
136225 3353092 1 46.0 46.0 1
136229 3353144 1 103.0 11217.0 1
136231 3353194 1 52.0 2212.0 1
136232 3353226 1 81.0 10522.0 1
136234 3353251 1 108.0 11304.0 1

32365 rows × 5 columns