Many patients admitted to the intensive care unit (ICU) originally present in the emergency department (ED). Information on care provided is not directly a part of MIMIC-III, though there are some pieces of information about patient ED stays which are available. Information available includes: (1) admission information (2) (infrequently) fluids/treatment received.
In [1]:
# Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import psycopg2
from IPython.display import display, HTML # used to print out pretty pandas dataframes
import matplotlib.dates as dates
import matplotlib.lines as mlines
%matplotlib inline
plt.style.use('ggplot')
# specify user/password/where the database is
sqluser = 'postgres'
sqlpass = 'postgres'
dbname = 'mimic'
schema_name = 'mimiciii'
host = 'localhost'
query_schema = 'SET search_path to ' + schema_name + ';'
# connect to the database
con = psycopg2.connect(dbname=dbname, user=sqluser, password=sqlpass, host=host)
In [2]:
# load the admissions table from the local database
query = query_schema + 'select * from admissions'
df = pd.read_sql_query(query,con)
# define a function to only output human readable dates
def pretty_dates(x):
if 'NaTType' in str(type(x)):
return ''
else:
return x.strftime('%m-%d %H:%M')
df['ed-registered'] = df['edregtime'].apply(pretty_dates)
df['ed-out'] = df['edouttime'].apply(pretty_dates)
df['hosp-admit'] = df['admittime'].apply(pretty_dates)
df['hosp-disch'] = df['dischtime'].apply(pretty_dates)
# look at the top 5 rows for the table
df[['ed-registered','ed-out','admission_location','hosp-admit','hosp-disch']].head()
Out[2]:
Above we can see how the emergency department (ED) times in the admissions table work. Let's look at the top row (index 0): the patient is registered in the ED at 10:06, and is recorded as leaving the ED at 13:24. During this time they have been admitted to the hospital (admittime
of 12:26). While it seems odd to be in the ED and the hospital at the same time, keep in mind that the ED and the hospital are operating on different systems, and admittime
is an administrative database, not a real-time GPS tracker!
In [3]:
df.groupby('admission_location')[['edregtime','edouttime']].count().sort_values('edregtime', ascending=False)
Out[3]:
With the above group, we can see that most patients who go through the ED are direct admissions, but sometimes clinical referrals and transfers also go via the ED.
@msjoding asks:
I see that the PRESCRIPTIONS table includes meds that were ordered outside of ICU stays. Does this table also include any medications that were ordered in the emergency department just prior to the hospital admission?
We can investigate this by cross-referencing hospital admission times with prescriptions. However, since prescriptions are only available with a date, we need to isolate to admissions where the patient was in the ED for at least a calendar day.
In [4]:
# load entire prescriptions table into memory
query = query_schema + 'select * from prescriptions'
pr = pd.read_sql_query(query,con)
In [5]:
# first, isolate to patients in the ed
ed = df.loc[df['edregtime'].notnull(), :]
# now find patients in the ED but not in the hospital
ed_day = ed['edregtime'].dt.floor('d')
hosp_day = ed['admittime'].dt.floor('d')
hosp_hour = ed['admittime'].apply(lambda x: x.hour)
# add a gap
idx = (hosp_day > ed_day) & (hosp_hour > 6)
ed = ed.loc[idx, :]
# look at the top 5 rows for the table
ed[['ed-registered','ed-out','admission_location','hosp-admit','hosp-disch']].head()
Out[5]:
Above, row index 143 looks promising: they were registered in the ED at 15:27 and admitted to the hospital the next day at 11:06.
In [6]:
subject_id = ed.loc[143, 'subject_id']
# how many admissions does this patient have?
idx = df['subject_id']==subject_id
print('{} hospital admissions for subject.'.format(df.loc[idx, 'hadm_id'].count()))
print('Row {} is the first admission.'.format(
df.loc[idx, :].sort_values('admittime', ascending=True).index[0]))
query = query_schema + 'select * from prescriptions where subject_id = {}'.format(subject_id)
pr = pd.read_sql_query(query,con)
# truncate the admittime to day
admitdate = pd.Timestamp(ed.loc[143, 'admittime'].date())
# look for rows before this day
idx = (pr['subject_id'] == subject_id) & (pr['startdate'] < admitdate)
pr.loc[idx, :].sort_values('startdate')
Out[6]:
Given the above, it seems unlikely the prescriptions table captures ED medications.
In [7]:
n = 0
for i, row in ed.iterrows():
# get the subject ID
subject_id = row['subject_id']
# skip this row if it is a secondary admission to the hospital
idx = df['subject_id']==subject_id
if i != df.loc[idx, :].sort_values('admittime', ascending=True).index[0]:
continue
# truncate the admittime to day
admitdate = pd.Timestamp(row['admittime'].date())
# look for rows before this day but no more than 7 days before
idx = (pr['subject_id'] == subject_id) & (pr['startdate'] < admitdate)
print('subject_id {:5d} - {} rows.'.format(subject_id, idx.sum()))
n+=1
if n>20:
break
It would appear that no emergency department prescriptions exist in the prescriptions
table!