AmsterdamUMC / AmsterdamUMCdb

AmsterdamUMCdb - Freely Accessible ICU database. Please access our Open Access manuscript at https://doi.org/10.1097/CCM.0000000000004916
https://amsterdammedicaldatascience.nl/
MIT License
154 stars 43 forks source link

Is there a viggette to identify sepsis patients #81

Closed zh-zhang1984 closed 1 year ago

zh-zhang1984 commented 1 year ago

I have following code to extract sepsis patients, but it reported error; How can I solve this problem?

import amsterdamumcdb 
import configparser
import os
import psycopg2
import pandas as pd
import numpy as np
config = configparser.ConfigParser()

if os.path.isfile('/Users/zhangzhongheng/Documents/2023/Subtype_Sepsis/AmsterdamUMCdb/AmsterdamUMCdb/config.ini'):
    config.read('/Users/zhangzhongheng/Documents/2023/Subtype_Sepsis/AmsterdamUMCdb/AmsterdamUMCdb/config.ini')
else:
    config.read('/Users/zhangzhongheng/Documents/2023/Subtype_Sepsis/AmsterdamUMCdb/AmsterdamUMCdb/config.SAMPLE.ini')

#Open a connection to the postgres database:
con = psycopg2.connect(database=config['psycopg2']['database'], 
                       user=config['psycopg2']['username'],
                       password=config['psycopg2']['password'], 
                       host=config['psycopg2']['host'],
                       port=config['psycopg2']['port'])
con.set_client_encoding('WIN1252') #Uses code page for Dutch accented characters.
con.set_session(autocommit=True)
cursor = con.cursor()                       
sepsisDF = amsterdamumcdb.get_sepsis_patients(con)

/Users/zhangzhongheng/Documents/2022/ARDS_CT_radiomics/envs/lib/python3.9/site-packages/amsterdamumcdb/util.py:134: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
  result = pd.read_sql(sqlalchemy.text(sql), con)
Creating cohort Sepsis-3 at admission (infection and SOFA score ≥2)...
Querying reason for admission...
DatabaseError: Execution failed on sql 'WITH diagnosis_groups AS (
    SELECT admissionid,
        item,
        CASE
            WHEN itemid IN (
                18669, --NICE APACHEII diagnosen
                18671 --NICE APACHEIV diagnosen
            )
            THEN split_part(value, ' - ', 1)
            -- 'e.g. 'Non-operative cardiovascular - Anaphylaxis' -> Non-operative cardiovascular
            ELSE value
        END as diagnosis_group,
        valueid as diagnosis_group_id,
        ROW_NUMBER() OVER(PARTITION BY admissionid
        ORDER BY
            CASE --prefer NICE > APACHE IV > II > D
                WHEN itemid = 18671 THEN 6 --NICE APACHEIV diagnosen
                WHEN itemid = 18669 THEN 5 --NICE APACHEII diagnosen
patrickthoral commented 1 year ago

Since you are not displaying the full error message, I am not completely sure, but most likely the reason is that the amsterdamumcdb.get_sepsis_patients(con) requires a SQLAlchemy engine, since a recent pandas update:

#Modify config.ini in the root folder of the repository to change the settings to connect to your postgreSQL database
import configparser
import os
config = configparser.ConfigParser()

if os.path.isfile('../../config.ini'):
    config.read('../../config.ini')
else:
    config.read('../../config.SAMPLE.ini')

#Open a connection to the postgres database:
pg_con = psycopg2.connect(database=config['psycopg2']['database'], 
                       user=config['psycopg2']['username'], password=config['psycopg2']['password'], 
                       host=config['psycopg2']['host'], port=config['psycopg2']['port'])
pg_con.set_client_encoding('WIN1252') #Uses code page for Dutch accented characters.
pg_con.set_session(autocommit=True)

cursor = pg_con.cursor()
cursor.execute('SET SCHEMA \'amsterdamumcdb\''); #set search_path to amsterdamumcdb schema

# create SQLAlchemy engine for official pandas database support
engine = sqlalchemy.create_engine('postgresql+psycopg2://', creator=lambda: pg_con)
con = engine.connect()

If you make sure that con is a SQLAlchemy engine, it would most likely work.

zh-zhang1984 commented 1 year ago

yes, it works fine after adding the SQLAlchemy engine.