<jupyter_start><jupyter_text>Looking at ICD-9 "Other Bacterial Diseases" http://www.icd9data.com/2015/Volume1/001-139/030-041/default.htm* 030 Leprosy* 031 Diseases due to other mycobacteria* 032 Diphtheria* 033 Whooping cough* 034 Streptococcal sore throat and scarlet fever* 035 Erysipelas* 036 Meningococcal infection* 037 Tetanus* 038 Septicemia* 039 Actinomycotic infections* 040 Other bacterial diseases* 041 Bacterial infection in conditions classified elsewhere and of unspecified site<jupyter_code># Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import psycopg2
import getpass
import pdvega

# for configuring connection 
from configobj import ConfigObj
import os

%matplotlib inline
# display all rows of pandas data frame
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', -1)

# Create a database connection using settings from config file
config='../db/config.ini'

# connection info
conn_info = dict()
if os.path.isfile(config):
    config = ConfigObj(config)
    conn_info["sqluser"] = config['username']
    conn_info["sqlpass"] = config['password']
    conn_info["sqlhost"] = config['host']
    conn_info["sqlport"] = config['port']
    conn_info["dbname"] = config['dbname']
    conn_info["schema_name"] = config['schema_name']
else:
    conn_info["sqluser"] = 'postgres'
    conn_info["sqlpass"] = ''
    conn_info["sqlhost"] = '127.0.0.1'
    conn_info["sqlport"] = 5432
    conn_info["dbname"] = 'eicu'
    conn_info["schema_name"] = 'eicu'
    
# Connect to the eICU database
print('Database: {}'.format(conn_info['dbname']))
print('Username: {}'.format(conn_info["sqluser"]))
if conn_info["sqlpass"] == '':
    # try connecting without password, i.e. peer or OS authentication
    try:
        if (conn_info["sqlhost"] == 'localhost') & (conn_info["sqlport"]=='5432'):
            con = psycopg2.connect(dbname=conn_info["dbname"],
                                   user=conn_info["sqluser"])            
        else:
            con = psycopg2.connect(dbname=conn_info["dbname"],
                                   host=conn_info["sqlhost"],
                                   port=conn_info["sqlport"],
                                   user=conn_info["sqluser"])
    except:
        conn_info["sqlpass"] = getpass.getpass('Password: ')

        con = psycopg2.connect(dbname=conn_info["dbname"],
                               host=conn_info["sqlhost"],
                               port=conn_info["sqlport"],
                               user=conn_info["sqluser"],
                               password=conn_info["sqlpass"])
query_schema = 'set search_path to ' + conn_info['schema_name'] + ';'<jupyter_output>Database: eicu
Username: postgres
Password: ········<jupyter_text>038 Septicemiahttp://www.icd9data.com/2015/Volume1/001-139/030-041/038/default.htm  Number of patients with each subtype<jupyter_code>icd9 = '038.'
query = query_schema + """
select diagnosisstring, icd9code
, count(distinct patientunitstayid) as n
from diagnosis
where icd9code like '%{}%'
group by diagnosisstring, icd9code
order by n desc
""".format(icd9)

df = pd.read_sql_query(query, con)
df
icd9 = '038.'
query = query_schema + """
SELECT *
FROM diagnosis
""".format(icd9)

diagnosis_df = pd.read_sql_query(query, con)
# df
pd.DataFrame.head(diagnosis_df)<jupyter_output><empty_output><jupyter_text>=============================================<jupyter_code>icd9 = '410.'
query = query_schema + """
select diagnosisstring, icd9code
, count(distinct patientunitstayid) as n
from diagnosis
where icd9code like '%{}%'
group by diagnosisstring, icd9code
order by n desc
""".format(icd9)

df = pd.read_sql_query(query, con)
df
icd9 = '038.9'
query = query_schema + """
select icd9code
, count(distinct patientunitstayid) as n
from diagnosis
where icd9code like '%{}%'
group by icd9code
order by n desc
""".format(icd9)

df = pd.read_sql_query(query, con)
df<jupyter_output><empty_output><jupyter_text>Stratify by hospital<jupyter_code>icd9 = '038.9, A41.9'
query = query_schema + """
select icd9code
, count(distinct patientunitstayid) as n
from diagnosis
where icd9code like '%{}%'
group by icd9code
left join diagnosis dx
  on pt.patientunitstayid = dx.patientunitstayid
group by pt.hospitalid
order by n desc
""".format(icd9)

df = pd.read_sql_query(query, con)
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13378995433789953, 'fraction_numerical': 0.0319634703196347, 'mean_word_length': 3.9224719101123595, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '409647', 'license': 'MIT', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1424, 'n_words': 334, 'path': 'notebooks/my_scripts/explore_diagnoses.ipynb', 'repo_name': 'JimmyJHickey/eicu-code', 'size': '4380'}