From ae2afe6cc713fa83a6ae2efaede99bf05226ea5b Mon Sep 17 00:00:00 2001 From: Alistair Johnson Date: Wed, 21 Jun 2017 20:48:34 -0400 Subject: [PATCH] remove cursors and use schema name in queries --- notebooks/crrt-notebook.ipynb | 46 ++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/notebooks/crrt-notebook.ipynb b/notebooks/crrt-notebook.ipynb index 9d4b0f8..4a959d5 100644 --- a/notebooks/crrt-notebook.ipynb +++ b/notebooks/crrt-notebook.ipynb @@ -109,7 +109,6 @@ "and lower(label) like '%crrt%'\n", "\"\"\"\n", "df = pd.read_sql_query(query,con)\n", - "cur.close()\n", "\n", "df" ] @@ -141,11 +140,7 @@ }, "outputs": [], "source": [ - "cur = con.cursor()\n", - "cur.execute('SET search_path to ' + schema_name)\n", - "\n", - "\n", - "query = \"\"\"\n", + "query = query_schema + \"\"\"\n", "select itemid, label, category, linksto\n", "from d_items di\n", "where dbsource = 'metavision'\n", @@ -156,7 +151,6 @@ "order by linksto, category, label\n", "\"\"\"\n", "df = pd.read_sql_query(query,con)\n", - "cur.close()\n", "\n", "HTML(df.head().to_html().replace('NaN', ''))" ] @@ -315,11 +309,7 @@ }, "outputs": [], "source": [ - "cur = con.cursor()\n", - "cur.execute('SET search_path to ' + schema_name)\n", - "\n", - "\n", - "query = \"\"\"\n", + "query = query_schema + \"\"\"\n", "select\n", " ce.icustay_id, di.label, ce.charttime\n", " , ce.value\n", @@ -350,7 +340,6 @@ "order by ce.icustay_id, ce.charttime, di.label;\n", "\"\"\"\n", "df = pd.read_sql_query(query,con)\n", - "cur.close()\n", "\n", "HTML(df.head().to_html().replace('NaN', ''))" ] @@ -404,7 +393,7 @@ "source": [ "def print_itemid_info(con, itemid):\n", " # get name of itemid\n", - " query = 'SET search_path to ' + schema_name + \"\"\";\n", + " query = query_schema + \"\"\"\n", " select label\n", " from d_items\n", " where itemid = \"\"\" + str(itemid)\n", @@ -413,7 +402,7 @@ " print('Values for {} - {}...'.format(itemid, df['label'][0]))\n", " \n", " \n", - " query = 'SET search_path to ' + schema_name + \"\"\";\n", + " query = query_schema + \"\"\"\n", " select value\n", " , count(distinct icustay_id) as number_of_patients\n", " , count(icustay_id) as number_of_observations\n", @@ -634,7 +623,6 @@ "from t1\n", "\"\"\"\n", "df = pd.read_sql_query(query,con)\n", - "cur.close()\n", "\n", "HTML(df.to_html().replace('NaN', ''))" ] @@ -654,7 +642,7 @@ }, "outputs": [], "source": [ - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as \n", "(\n", "select icustay_id, charttime\n", @@ -874,7 +862,7 @@ "outputs": [], "source": [ "print(\"Durations from INPUTEVENTS for one patient with KCl...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "select \n", " linkorderid\n", " , orderid\n", @@ -967,7 +955,7 @@ "outputs": [], "source": [ "print(\"Durations from INPUTEVENTS_MV, new events noted with time_partition...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select \n", @@ -1036,7 +1024,7 @@ "outputs": [], "source": [ "print(\"Durations from INPUTEVENTS for one patient with KCl...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select icustay_id\n", @@ -1106,7 +1094,7 @@ "outputs": [], "source": [ "print(\"Durations from INPUTEVENTS for one patient with KCl...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select icustay_id\n", @@ -1199,7 +1187,7 @@ "outputs": [], "source": [ "print(\"Durations from INPUTEVENTS for one patient with KCl...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select icustay_id\n", @@ -1276,7 +1264,7 @@ }, "outputs": [], "source": [ - "query_inputevents = 'SET search_path to ' + schema_name + \"\"\";\n", + "query_inputevents = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select icustay_id\n", @@ -1353,7 +1341,7 @@ "outputs": [], "source": [ "print(\"Durations from INPUTEVENTS for one patient given propofol...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select icustay_id\n", @@ -1404,7 +1392,7 @@ "outputs": [], "source": [ "print(\"Grouped durations from INPUTEVENTS for one patient given propofol...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with t1 as\n", "(\n", "select icustay_id\n", @@ -1466,7 +1454,7 @@ "# convert CHARTEVENTS into durations\n", "# NOTE: we only look at a single patient as an exemplar\n", "print(\"Durations from CHARTEVENTS...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "with crrt_settings as\n", "(\n", "select ce.icustay_id, ce.charttime\n", @@ -1642,7 +1630,7 @@ "outputs": [], "source": [ "# happy with the above query - repeat it without the isolation to a single ICUSTAY_ID\n", - "query_chartevents = 'SET search_path to ' + schema_name + \"\"\";\n", + "query_chartevents = query_schema + \"\"\"\n", "with crrt_settings as\n", "(\n", "select ce.icustay_id, ce.charttime\n", @@ -1833,7 +1821,7 @@ "# extract the durations from PROCEDUREEVENTS_MV\n", "# NOTE: we only look at a single patient as an exemplar\n", "print(\"Durations from PROCEDUREEVENTS_MV...\")\n", - "query = 'SET search_path to ' + schema_name + \"\"\";\n", + "query = query_schema + \"\"\"\n", "select icustay_id\n", " , ROW_NUMBER() over (partition by icustay_id order by starttime, endtime) as num\n", " , starttime, endtime\n", @@ -1869,7 +1857,7 @@ "outputs": [], "source": [ "# happy with above query\n", - "query_procedureevents = 'SET search_path to ' + schema_name + \"\"\";\n", + "query_procedureevents = query_schema + \"\"\"\n", "select icustay_id\n", " , ROW_NUMBER() over (partition by icustay_id order by starttime, endtime) as num\n", " , starttime, endtime\n",