From 5e77827f3e77f65ce10ba66c9874308449b8989a Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sat, 19 Dec 2015 19:44:15 -0500 Subject: [PATCH 1/4] test age and los --- tests/test_build.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_build.py b/tests/test_build.py index 52d62c5..5fa0c0f 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -250,6 +250,42 @@ def test_row_counts_are_as_expected(self): queryresult = pd.read_sql_query(query,self.con) self.assertEqual(queryresult.values[0][0],expectedrows) + def check_age_and_los_is_expected(self): + query = \ + """ + WITH icuadmissions as ( + SELECT a.subject_id, a.hadm_id, i.icustay_id, + a.admittime as hosp_admittime, a.dischtime as hosp_dischtime, + i.first_careunit, + DENSE_RANK() over(PARTITION BY a.hadm_id ORDER BY i.intime ASC) as icu_seq, + p.dob, p.dod, i.intime as icu_intime, i.outtime as icu_outtime, + i.los as icu_los, + round((EXTRACT(EPOCH FROM (a.dischtime-a.admittime))/60/60/24) :: NUMERIC, 4) as hosp_los, + p.gender, + round((EXTRACT(EPOCH FROM (a.admittime-p.dob))/60/60/24/365.242) :: NUMERIC, 4) as age_hosp_in, + round((EXTRACT(EPOCH FROM (i.intime-p.dob))/60/60/24/365.242) :: NUMERIC, 4) as age_icu_in, + hospital_expire_flag, + CASE WHEN p.dod IS NOT NULL + AND p.dod >= i.intime - interval '6 hour' + AND p.dod <= i.outtime + interval '6 hour' THEN 1 + ELSE 0 END AS icu_expire_flag + FROM admissions a + INNER JOIN icustays i + ON a.hadm_id = i.hadm_id + INNER JOIN patients p + ON a.subject_id = p.subject_id + ORDER BY a.subject_id, i.intime) + SELECT round(avg(age_icu_in)) as avg_age_icu, + round(avg(hosp_los)) as avg_los_hosp, + round(avg(icu_los)) as avg_los_icu + FROM icuadmissions; + """ + queryresult = pd.read_sql_query(query,self.con) + self.assertEqual(queryresult['avg_age_icu'].values[0],65) + self.assertEqual(queryresult['avg_los_hosp'].values[0],11) + self.assertEqual(queryresult['avg_los_icu'].values[0],5) + + def main(): unittest.main() From 0f9421b85ee7bca39255d98fb6f4c38ebc63dae9 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sat, 19 Dec 2015 19:46:20 -0500 Subject: [PATCH 2/4] test --- tests/test_build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_build.py b/tests/test_build.py index 5fa0c0f..e75aa4b 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -250,7 +250,7 @@ def test_row_counts_are_as_expected(self): queryresult = pd.read_sql_query(query,self.con) self.assertEqual(queryresult.values[0][0],expectedrows) - def check_age_and_los_is_expected(self): + def test_age_and_los_is_expected(self): query = \ """ WITH icuadmissions as ( From 84b4485ac39fd02c17d95b5ea574e831a20f56f4 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sat, 19 Dec 2015 19:48:45 -0500 Subject: [PATCH 3/4] add 0 --- tests/test_build.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_build.py b/tests/test_build.py index e75aa4b..9dbee00 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -281,9 +281,9 @@ def test_age_and_los_is_expected(self): FROM icuadmissions; """ queryresult = pd.read_sql_query(query,self.con) - self.assertEqual(queryresult['avg_age_icu'].values[0],65) - self.assertEqual(queryresult['avg_los_hosp'].values[0],11) - self.assertEqual(queryresult['avg_los_icu'].values[0],5) + self.assertEqual(queryresult['avg_age_icu'].values[0][0],65) + self.assertEqual(queryresult['avg_los_hosp'].values[0][0],11) + self.assertEqual(queryresult['avg_los_icu'].values[0][0],5) def main(): From f796b6f977ab4cf88c9244610d0735b2709dce73 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Sat, 19 Dec 2015 20:09:34 -0500 Subject: [PATCH 4/4] prep for adding mysql. ref #45 --- tests/test_build.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/tests/test_build.py b/tests/test_build.py index 9dbee00..0a1c1fd 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -5,8 +5,13 @@ import os from subprocess import call +# Prep for Oracle and MySQL database connection +# http://stackoverflow.com/questions/10065051/python-pandas-and-databases-like-mysql +# import cx_Oracle +# import MySQLdb + # Config -sqluser = 'postgres' +psqluser = 'postgres' testdbname = 'mimic_test_db' hostname = 'localhost' datadir = 'testdata/v1_3/' @@ -78,7 +83,7 @@ def run_postgres_build_scripts(cur): mimic_data_dir = '/home/mimicadmin/data/mimiciii_1_3/' else: mimic_data_dir = curpath+datadir - call(['psql','-f',fn,'-d',testdbname,'-U',sqluser,'-v','mimic_data_dir='+mimic_data_dir]) + call(['psql','-f',fn,'-d',testdbname,'-U',psqluser,'-v','mimic_data_dir='+mimic_data_dir]) # Add constraints fn = curpath + '../buildmimic/postgres/postgres_add_constraints.sql' cur.execute(open(fn, "r").read()) @@ -86,13 +91,33 @@ def run_postgres_build_scripts(cur): fn = curpath + '../buildmimic/postgres/postgres_add_indexes.sql' cur.execute(open(fn, "r").read()) +# # Prep for adding MySQL build +# def run_mysql_build_scripts(cur): +# # Create tables +# fn = curpath + '../buildmimic/mysql/mysql_create_tables.sql' +# cur.execute(open(fn, "r").read()) +# # Loads data +# fn = curpath + '../buildmimic/mysql/mysql_load_data.sql' +# if os.environ.has_key('USER') and os.environ['USER'] == 'jenkins': +# # use full dataset +# mimic_data_dir = '/home/mimicadmin/data/mimiciii_1_3/' +# else: +# mimic_data_dir = curpath+datadir +# call(['psql','-f',fn,'-d',testdbname,'-U',psqluser,'-v','mimic_data_dir='+mimic_data_dir]) +# # Add constraints +# fn = curpath + '../buildmimic/mysql/mysql_add_constraints.sql' +# cur.execute(open(fn, "r").read()) +# # Add indexes +# fn = curpath + '../buildmimic/mysql/mysql_add_indexes.sql' +# cur.execute(open(fn, "r").read()) + # Class to run unit tests class test_postgres(unittest.TestCase): # setUpClass runs once for the class @classmethod def setUpClass(cls): # Connect to default postgres database - cls.con = psycopg2.connect(dbname='postgres', user=sqluser) + cls.con = psycopg2.connect(dbname='postgres', user=psqluser) cls.con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cls.cur = cls.con.cursor() # Create test database @@ -104,7 +129,7 @@ def setUpClass(cls): cls.cur.close() cls.con.close() # Connect to the test database - cls.con = psycopg2.connect(dbname=testdbname, user=sqluser) + cls.con = psycopg2.connect(dbname=testdbname, user=psqluser) cls.con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cls.cur = cls.con.cursor() # Build the test database @@ -116,7 +141,7 @@ def setUpClass(cls): @classmethod def tearDownClass(cls): # Connect to default postgres database - cls.con = psycopg2.connect(dbname='postgres', user=sqluser) + cls.con = psycopg2.connect(dbname='postgres', user=psqluser) cls.con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cls.cur = cls.con.cursor() # Drop test database @@ -127,7 +152,7 @@ def tearDownClass(cls): # setUp runs once for each test method def setUp(self): # Connect to the test database - self.con = psycopg2.connect(dbname=testdbname, user=sqluser) + self.con = psycopg2.connect(dbname=testdbname, user=psqluser) self.con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) self.cur = self.con.cursor()