{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Minimal Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pretty printing has been turned OFF\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%pprint off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pretty printing has been turned ON\n"
     ]
    }
   ],
   "source": [
    "%pprint"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preamble"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Installing FHIRPACK from PIP (recommended)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install fhirpack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using FHIRPACK without Installation by Modifying PYTHONPATH\n",
    "\n",
    "You can use FHIRPACK without installing it, this is mostly used for testing purposes during development, but it's listed here for reference.\n",
    "\n",
    "Simply clone the FHIRPACK repository using `git clone git@github.com:fhirpack/main.git` or `git clone git@gitlab.com:fhirpack/main.git`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys,os\n",
    "#sys.path.append(os.getcwd()+'/../src/')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Installing FHIRPACK from Source\n",
    "\n",
    "Alternatively, you can installed the latest version of this repository in edit mode if you want to play around and modify FHIRPACK."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install -e ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fhirpack\tas fp\n",
    "import pandas\tas pd\n",
    "import numpy\tas np\n",
    "import json\n",
    "\n",
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.width', None)\n",
    "pd.set_option('display.max_colwidth', None)\n",
    "\n",
    "RESPATH=f\"./data/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "### FHIR Server Connection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configure PACK with Environment File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pack = fp.PACK(envFile='.env.example')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Playground"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                       \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(102, 1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions = pack\\\n",
    "\t.getConditions(\n",
    "\t\tsearchParams={\n",
    "\t\t\t\"_content\":\"Coronary Heart Disease\",\n",
    "\t\t\t})\n",
    "\n",
    "conditions.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '617158', 'meta': {'versionId': '1', 'lastUpdated': '2020-02-07T11:51:46.361+00:00', 'source': '#msg5Ia42NNbn9SQ0'}, 'clinicalStatus': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-clinical', 'code': 'active'}]}, 'verificationStatus': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-ver-status', 'code': 'confirmed'}]}, 'code': {'coding': [{'system': 'http://snomed.info/sct', 'code': '53741008', 'display': 'Coronary Heart Disease'}], 'text': 'Coronary Heart Disease'}, 'subject': {'reference': 'Patient/617077'}, 'encounter': {'reference': 'Encounter/617157'}, 'onsetDateTime': '2003-12-16T02:47:35-05:00', 'recordedDate': '2003-12-16T02:47:35-05:00'}</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  data\n",
       "0  {'resourceType': 'Condition', 'id': '617158', 'meta': {'versionId': '1', 'lastUpdated': '2020-02-07T11:51:46.361+00:00', 'source': '#msg5Ia42NNbn9SQ0'}, 'clinicalStatus': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-clinical', 'code': 'active'}]}, 'verificationStatus': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-ver-status', 'code': 'confirmed'}]}, 'code': {'coding': [{'system': 'http://snomed.info/sct', 'code': '53741008', 'display': 'Coronary Heart Disease'}], 'text': 'Coronary Heart Disease'}, 'subject': {'reference': 'Patient/617077'}, 'encounter': {'reference': 'Encounter/617157'}, 'onsetDateTime': '2003-12-16T02:47:35-05:00', 'recordedDate': '2003-12-16T02:47:35-05:00'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"clinicalStatus\": {\n",
      "        \"coding\": [\n",
      "            {\n",
      "                \"code\": \"active\",\n",
      "                \"system\": \"http://terminology.hl7.org/CodeSystem/condition-clinical\"\n",
      "            }\n",
      "        ]\n",
      "    },\n",
      "    \"code\": {\n",
      "        \"coding\": [\n",
      "            {\n",
      "                \"code\": \"53741008\",\n",
      "                \"display\": \"Coronary Heart Disease\",\n",
      "                \"system\": \"http://snomed.info/sct\"\n",
      "            }\n",
      "        ],\n",
      "        \"text\": \"Coronary Heart Disease\"\n",
      "    },\n",
      "    \"encounter\": {\n",
      "        \"reference\": \"Encounter/617157\"\n",
      "    },\n",
      "    \"id\": \"617158\",\n",
      "    \"meta\": {\n",
      "        \"lastUpdated\": \"2020-02-07T11:51:46.361+00:00\",\n",
      "        \"source\": \"#msg5Ia42NNbn9SQ0\",\n",
      "        \"versionId\": \"1\"\n",
      "    },\n",
      "    \"onsetDateTime\": \"2003-12-16T02:47:35-05:00\",\n",
      "    \"recordedDate\": \"2003-12-16T02:47:35-05:00\",\n",
      "    \"resourceType\": \"Condition\",\n",
      "    \"subject\": {\n",
      "        \"reference\": \"Patient/617077\"\n",
      "    },\n",
      "    \"verificationStatus\": {\n",
      "        \"coding\": [\n",
      "            {\n",
      "                \"code\": \"confirmed\",\n",
      "                \"system\": \"http://terminology.hl7.org/CodeSystem/condition-ver-status\"\n",
      "            }\n",
      "        ]\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "conditions[0:1].pretty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                \r"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name.given</th>\n",
       "      <th>name.family</th>\n",
       "      <th>birthDate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[617077]</td>\n",
       "      <td>[[[Andres25]]]</td>\n",
       "      <td>[[Konopelski743]]</td>\n",
       "      <td>[1975-10-07]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[ddbf5561-4bfd-4434-89fb-344387ca945b]</td>\n",
       "      <td>[[[Adam631]]]</td>\n",
       "      <td>[[Keebler762]]</td>\n",
       "      <td>[1948-07-09]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[4cee554c-9681-47b7-a3c4-06864ec294a0]</td>\n",
       "      <td>[[[Agustín529]]]</td>\n",
       "      <td>[[Cintrón695]]</td>\n",
       "      <td>[1974-08-25]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[1b8a7c69-e0d0-4832-a9a1-b487bbc8af90]</td>\n",
       "      <td>[[[Aja848], [Aja848]]]</td>\n",
       "      <td>[[Herman763, Hansen121]]</td>\n",
       "      <td>[1959-10-02]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       id              name.given  \\\n",
       "0                                [617077]          [[[Andres25]]]   \n",
       "1  [ddbf5561-4bfd-4434-89fb-344387ca945b]           [[[Adam631]]]   \n",
       "2                                    None                    None   \n",
       "3  [4cee554c-9681-47b7-a3c4-06864ec294a0]        [[[Agustín529]]]   \n",
       "4  [1b8a7c69-e0d0-4832-a9a1-b487bbc8af90]  [[[Aja848], [Aja848]]]   \n",
       "\n",
       "                name.family     birthDate  \n",
       "0         [[Konopelski743]]  [1975-10-07]  \n",
       "1            [[Keebler762]]  [1948-07-09]  \n",
       "2                      None          None  \n",
       "3            [[Cintrón695]]  [1974-08-25]  \n",
       "4  [[Herman763, Hansen121]]  [1959-10-02]  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions[:5]\\\n",
    "\t.getPatients()\\\n",
    "\t\t.gatherSimplePaths([\n",
    "\t\t\t\"id\",\n",
    "\t\t\t\"name.given\",\n",
    "\t\t\t\"name.family\",\n",
    "\t\t\t\"birthDate\"\n",
    "\t\t])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.6 ('fhirpack-I5oSLEhk')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "vscode": {
   "interpreter": {
    "hash": "22fa324736d3c21f3e94ad75ccc035126135b5718e73e83d80ae7053c0444244"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
