Parse out medications from discharge summaries in MIMIC


In [ ]:
import os
import pandas as pd
import psycopg2
import getpass
import re

# Import the finddrugs module
import finddrugs

In [ ]:
# Create a database connection
user = 'postgres'
host = 'localhost'
dbname = 'mimic'
schema = 'mimiciii'

In [ ]:
# Connect to the database
con = psycopg2.connect(dbname=dbname, user=user, host=host, 
                       password=getpass.getpass(prompt='Password:'.format(user)))
cur = con.cursor()
cur.execute('SET search_path to {}'.format(schema))

In [ ]:
# Get the notes to be analysed
# NOTE: we are limiting the query to 100 rows here
query = \
"""
SELECT n.row_id, n.subject_id, n.hadm_id, n.category, n.description, n.text
FROM noteevents n
WHERE n.category like 'Discharge summary'
LIMIT 100;
"""

data = pd.read_sql_query(query,con)

In [ ]:
data.head()

In [ ]:
# Search the notes
finddrugs.search(data)

In [9]:
# load the output to a dataframe
medications = pd.read_csv('output.csv')
medications.head()


Out[9]:
ROW_ID SUBJECT_ID HADM_ID HIST_FOUND DEPRESSION ADMIT_FOUND DIS_FOUND GEN_DEPRESS_MEDS_FOUND GROUP SSRI MISC citalopram escitalopram fluoxetine fluvoxamine paroxetine sertraline
0 174 22532 167853 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 175 13702 107527 1 0 1 1 0 3 1 0 0 1 0 0 0 0
2 176 13702 167118 1 1 1 1 0 3 1 0 0 1 0 0 0 0
3 177 13702 196489 1 1 1 1 0 3 1 0 0 1 0 0 0 0
4 178 26880 135453 1 0 1 1 0 3 1 0 1 0 0 0 0 0