Skip to content

Commit

Permalink
feat (synthea): new notebook version
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Apr 26, 2022
1 parent fd4e731 commit aa72c4a
Showing 1 changed file with 137 additions and 68 deletions.
205 changes: 137 additions & 68 deletions sql/synthea/synthea-prognostics.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Synthea Case Study\n",
"*Lab 26 April 2022*\n",
"\n",
"* https://synthea.mitre.org/\n",
"* https://github.com/synthetichealth/synthea"
]
},
{
"cell_type": "code",
"execution_count": 1,
Expand Down Expand Up @@ -99,7 +110,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "95101f66-d9bd-4276-b438-00e57bc08fe7",
"model_id": "8183b964-50ff-4e96-9e2b-8d3fcbcf4534",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -158,7 +169,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "18f35571-b727-45df-9f35-92d226bfddab",
"model_id": "e6fca0f2-5850-4deb-b147-793d5f801015",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -174,6 +185,13 @@
"LIMIT 5;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Encounters in the Emergency"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -268,7 +286,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7f2351bc-5553-4bc0-9fed-0e1abeaf7039",
"model_id": "dc5d8534-8c9e-48a3-8aa8-e421ba1760a5",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -279,12 +297,19 @@
}
],
"source": [
"SELECT e.patient, c.code, c.description_condition\n",
"SELECT e.patient, c.description_condition\n",
" FROM Emergency e, Conditions c\n",
" WHERE e.patient = c.patient\n",
" LIMIT 10;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Patients that had a Stroke in the Last Encounter"
]
},
{
"cell_type": "code",
"execution_count": 10,
Expand All @@ -293,7 +318,7 @@
{
"data": {
"text/plain": [
"202"
"74"
]
},
"execution_count": 10,
Expand All @@ -304,34 +329,27 @@
"source": [
"SELECT COUNT(DISTINCT e.patient)\n",
" FROM Emergency e, Conditions c\n",
" WHERE e.patient = c.patient AND c.code='59621000';"
" WHERE e.patient = c.patient AND\n",
" (c.description_condition='Cardiac Arrest' OR\n",
" c.description_condition='Myocardial Infarction');"
]
},
{
"cell_type": "code",
"execution_count": 11,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"DROP VIEW IF EXISTS Stroke;\n",
"\n",
"CREATE VIEW Stroke AS\n",
"SELECT patient, MAX(start) last_encounter FROM Encounters\n",
" WHERE encounterclass = 'emergency' AND (description = 'Cardiac Arrest' OR description='Stroke' OR description='Myocardial Infarction')\n",
" GROUP BY patient;"
"## Patients with a Stroke that Died 30 days after the Last Encounter"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "43b9309f-538b-4dda-9e37-01f34e7c4f77",
"model_id": "2fce665e-3e0d-4efb-ab43-76b1636460c9",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -343,7 +361,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b3c98bf7-5225-4a2c-aec5-ecbb7b5097f0",
"model_id": "c8fd19cc-abac-4521-8fcc-79ce40b4547e",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -355,60 +373,90 @@
],
"source": [
"SELECT COUNT(*)\n",
" FROM Patients p, Emergency s\n",
" WHERE p.id=s.patient AND p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 60;\n",
" FROM Patients p, Emergency s, Conditions c\n",
" WHERE p.id=s.patient AND s.patient=c.patient AND\n",
" p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 30 AND\n",
" (c.description_condition='Cardiac Arrest' OR\n",
" c.description_condition='Myocardial Infarction');\n",
"\n",
"SELECT DISTINCT p.first, p.last,\n",
" to_char(p.birthdate,'dd-mm-yyyy') birth, to_char(p.deathdate,'dd-mm-yyyy') death,\n",
" (p.deathdate-p.birthdate)/365 age,\n",
" to_char(s.last_encounter,'dd-mm-yyyy') last_encounter,\n",
" p.deathdate-s.last_encounter days\n",
" FROM Patients p, Emergency s\n",
" WHERE p.id=s.patient AND p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 60;"
" FROM Patients p, Emergency s, Conditions c\n",
" WHERE p.id=s.patient AND s.patient=c.patient AND\n",
" p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 30 AND\n",
" (c.description_condition='Cardiac Arrest' OR\n",
" c.description_condition='Myocardial Infarction');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conditions in the Last Encounter with Code"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "95eb37e6-9614-4ef5-b231-a29c83ca107d",
"model_id": "d4bc32b6-ab46-43d6-b72f-21c57d0ecfbd",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
},
}
],
"source": [
"SELECT e.patient, c.code, c.description_condition\n",
" FROM Emergency e, Conditions c\n",
" WHERE e.patient = c.patient\n",
" LIMIT 10;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Patients that have Hypertension (code 59621000) in the Last Encounter"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "14ff9136-7aac-44a6-83ab-3455bafda8a4",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
"text/plain": [
"202"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "display_data"
"output_type": "execute_result"
}
],
"source": [
"SELECT COUNT(*)\n",
" FROM Patients p, Stroke s\n",
" WHERE p.id=s.patient AND p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 60;\n",
"\n",
"SELECT DISTINCT p.first, p.last,\n",
" to_char(p.birthdate,'dd-mm-yyyy') birth, to_char(p.deathdate,'dd-mm-yyyy') death,\n",
" (p.deathdate-p.birthdate)/365 age,\n",
" to_char(s.last_encounter,'dd-mm-yyyy') last_encounter,\n",
" p.deathdate-s.last_encounter days\n",
" FROM Patients p, Stroke s\n",
" WHERE p.id=s.patient AND p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 60;"
"SELECT COUNT(DISTINCT e.patient)\n",
" FROM Emergency e, Conditions c\n",
" WHERE e.patient = c.patient AND c.code='59621000';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Patients with Hypertension that died 30 days after the Last Encounter"
]
},
{
Expand All @@ -419,29 +467,19 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d6b5e06a-3c86-49e8-81b7-f5b1b2bf9e16",
"model_id": "4c24bff9-9ba0-492a-9719-631857a8781f",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"SELECT DISTINCT description_condition FROM conditions ORDER BY description_condition;"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "28df3337-3959-448e-ac40-494d60d69018",
"model_id": "a09b0f5c-6b1e-45e2-bbf2-b6daf1a73349",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -452,20 +490,39 @@
}
],
"source": [
"SELECT DISTINCT c.description_condition, e.description\n",
" FROM conditions c, encounters e\n",
" WHERE c.encounter = e.id AND encounterclass = 'emergency';"
"SELECT COUNT(*)\n",
" FROM Patients p, Emergency s, Conditions c\n",
" WHERE p.id=s.patient AND s.patient=c.patient AND\n",
" p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 30 AND\n",
" c.code='59621000';\n",
"\n",
"SELECT DISTINCT p.first, p.last,\n",
" to_char(p.birthdate,'dd-mm-yyyy') birth, to_char(p.deathdate,'dd-mm-yyyy') death,\n",
" (p.deathdate-p.birthdate)/365 age,\n",
" to_char(s.last_encounter,'dd-mm-yyyy') last_encounter,\n",
" p.deathdate-s.last_encounter days\n",
" FROM Patients p, Emergency s, Conditions c\n",
" WHERE p.id=s.patient AND s.patient=c.patient AND\n",
" p.deathdate IS NOT NULL AND p.deathdate-s.last_encounter <= 30 AND\n",
" c.code='59621000';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Description of Conditions versus Encounters"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "515d3f65-9b2a-4b6e-a17a-ae41f2bceaaf",
"model_id": "b143e098-e699-456b-9a3d-444c83e9a7c8",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -476,18 +533,27 @@
}
],
"source": [
"SELECT DISTINCT description FROM encounters ORDER BY description;"
"SELECT DISTINCT c.description_condition, e.description\n",
" FROM conditions c, encounters e\n",
" WHERE c.encounter = e.id AND encounterclass = 'emergency';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Description of Conditions"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "08715a04-c816-4105-9599-2b820016b4b8",
"model_id": "fe4574a7-5d9f-4e08-b948-0be7acd08e5d",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -498,7 +564,10 @@
}
],
"source": [
"SELECT description_condition FROM conditions;"
"SELECT DISTINCT c.description_condition\n",
" FROM conditions c, encounters e\n",
" WHERE c.encounter = e.id AND encounterclass = 'emergency'\n",
" ORDER BY c.description_condition;"
]
}
],
Expand Down

0 comments on commit aa72c4a

Please sign in to comment.