{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Parse out medications from discharge summaries in MIMIC " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "import psycopg2\n", "import getpass\n", "import re\n", "\n", "# Import the finddrugs module\n", "import finddrugs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a database connection\n", "user = 'postgres'\n", "host = 'localhost'\n", "dbname = 'mimic'\n", "schema = 'mimiciii'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Connect to the database\n", "con = psycopg2.connect(dbname=dbname, user=user, host=host, \n", " password=getpass.getpass(prompt='Password:'.format(user)))\n", "cur = con.cursor()\n", "cur.execute('SET search_path to {}'.format(schema))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Get the notes to be analysed\n", "# NOTE: we are limiting the query to 100 rows here\n", "query = \\\n", "\"\"\"\n", "SELECT n.row_id, n.subject_id, n.hadm_id, n.category, n.description, n.text\n", "FROM noteevents n\n", "WHERE n.category like 'Discharge summary'\n", "LIMIT 100;\n", "\"\"\"\n", "\n", "data = pd.read_sql_query(query,con)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Search the notes\n", "finddrugs.search(data)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ROW_IDSUBJECT_IDHADM_IDHIST_FOUNDDEPRESSIONADMIT_FOUNDDIS_FOUNDGEN_DEPRESS_MEDS_FOUNDGROUPSSRIMISCcitalopramescitalopramfluoxetinefluvoxamineparoxetinesertraline
01742253216785300000000000000
11751370210752710110310010000
21761370216711811110310010000
31771370219648911110310010000
41782688013545310110310100000
\n", "
" ], "text/plain": [ " ROW_ID SUBJECT_ID HADM_ID HIST_FOUND DEPRESSION ADMIT_FOUND \\\n", "0 174 22532 167853 0 0 0 \n", "1 175 13702 107527 1 0 1 \n", "2 176 13702 167118 1 1 1 \n", "3 177 13702 196489 1 1 1 \n", "4 178 26880 135453 1 0 1 \n", "\n", " DIS_FOUND GEN_DEPRESS_MEDS_FOUND GROUP SSRI MISC citalopram \\\n", "0 0 0 0 0 0 0 \n", "1 1 0 3 1 0 0 \n", "2 1 0 3 1 0 0 \n", "3 1 0 3 1 0 0 \n", "4 1 0 3 1 0 1 \n", "\n", " escitalopram fluoxetine fluvoxamine paroxetine sertraline \n", "0 0 0 0 0 0 \n", "1 1 0 0 0 0 \n", "2 1 0 0 0 0 \n", "3 1 0 0 0 0 \n", "4 0 0 0 0 0 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load the output to a dataframe\n", "medications = pd.read_csv('output.csv')\n", "medications.head()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }