{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# General Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n",
      "Pretty printing has been turned OFF\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%pprint off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "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": [
    "[!] use only if you have **NOT** installed FHIR PACK via `pip`, `pipenv` or similar in your environment but you have instead cloned the project and are now in the `/examples` directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys,os\n",
    "sys.path.append(os.getcwd()+'/../src/')\n",
    "# sys.path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fhirpack\t\t\tas fp\n",
    "import pandas\t\t\tas pd\n",
    "import fhirpy\n",
    "import numpy\t\t\tas np\n",
    "import fhir.resources\tas fr\n",
    "import json\n",
    "from tqdm import tqdm\n",
    "from fhirpack.constants import CONFIG"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Useful Paths for Data Storage "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATAPATH=CONFIG.get('DATAPATH')\n",
    "RESPATH=f\"{DATAPATH}/fhir/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FHIR Server Connection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CLI Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                data  Patient                   \n",
      "0  {'resourceType': 'Patient', 'id': '1567099', '...  1567099\n"
     ]
    }
   ],
   "source": [
    "!python -m fhirpack.cli -e .env.example \"-o\" \"getPatients\" -p \"given = Chalmers\" #-v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                data Patient                    \n",
      "0  {'resourceType': 'Patient', 'id': '146053', 'm...  146053\n",
      "1  {'resourceType': 'Patient', 'id': '146255', 'm...  146255\n",
      "2  {'resourceType': 'Patient', 'id': '146496', 'm...  146496\n"
     ]
    }
   ],
   "source": [
    "# public FHIR test servers may be unavailable sometimes\n",
    "# if you prefer to test FHIRPACK using a locally running FHIR server\n",
    "# you may use our provided Docker environment by changing into the\n",
    "# cloned repository and running `docker-compose up``\n",
    "\n",
    "# !fp -e .env.docker -o \"getPatients\" -p \"given = Max\" #-v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### From Custom Environment File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                     "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"address\": [\n",
      "        {\n",
      "            \"city\": \"Walpole\",\n",
      "            \"country\": \"US\",\n",
      "            \"extension\": [\n",
      "                {\n",
      "                    \"extension\": [\n",
      "                        {\n",
      "      \n",
      "...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r"
     ]
    }
   ],
   "source": [
    "# pack = fp.PACK(envFile='.env.docker')\n",
    "\n",
    "# for i,e in pack.getPatients(['146053']).data.items():\n",
    "# \tprint(\n",
    "# \t\tjson.dumps(e.serialize(), indent=4, sort_keys=True)[:200],\n",
    "# \t\t'\\n...'\n",
    "# \t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "SCHEMA=https\n",
      "PORT=80\n",
      "\n",
      "#DOMAIN=${SCHEMA}://hapi.fhir.org\n",
      "#DOMAIN=${SCHEMA}://test.hapifhir.io\n",
      "#might be unstable or unavailable https://github.com/hapifhir/hapi-fhir/issues/1171#issuecomment-1179728061\n",
      "\n",
      "DOMAIN=${SCHEMA}://hapi.fhir.org\n",
      "\n",
      "#DOMAIN=${SCHEMA}://example.com:${PORT}\n",
      "# only use port if the server is running on a non-standard port \n",
      "# and this is reflected in the FHIR pagination links\n",
      "# see https://gitlab.com/fhirpack/main/-/issues/75 for more information\n",
      "\n",
      "APIBASE=${DOMAIN}/baseR4\n",
      "\n",
      "#AUTH_METHOD=\n",
      "#AUTH_PARAMS_PRESET=\n",
      "#LOGINURL=${DOMAIN}/oauth/token\n",
      "#OAUTH_TOKEN_ENDPOINT=${DOMAIN}/oauth/token\n",
      "#OAUTH_USERNAME=username\n",
      "#OAUTH_PASSWORD=password\n",
      "#OAUTH_TOKEN=token\n",
      "\n",
      "LOGSPATH=./logs\n",
      "DATAPATH=./data\n",
      "\n",
      "#EXTRACTION_BASE_TOKEN_DICOM=${OAUTH_TOKEN}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!cat .env.example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### From Default .env Environment File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pack = fp.PACK()\n",
    "# pack.client.authorization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Manual Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2918556', '...</td>\n",
       "      <td>2918556</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  Patient\n",
       "0  {'resourceType': 'Patient', 'id': '2918556', '...  2918556"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# pack  = fp.PACK(envFile=\".env.docker\")\n",
    "\n",
    "pack = fp.PACK(\"https://hapi.fhir.org/baseR4\")\n",
    "pack.getPatients(['2918556'])\n",
    "\n",
    "# pack  = fp.PACK(\"http://127.0.0.1:42112/hapi-fhir-jpaserver/fhir\")\n",
    "# pack.getPatients(['146053'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#testing with another less reliable FHIR server\n",
    "# pack  = fp.PACK(\"http://test.fhir.org/r4/\")\n",
    "# pack.getPatients(['11']).data.iloc[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Testing with A Docker FHIR Server and Synthetic Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# alternatively, if you're using our Dockerfile to run your FHIR server\n",
    "# remember to run \"docker-compose up\" in your local clone of FHIRPACK first  \n",
    "\n",
    "# pack  = fp.PACK(envFile=\".env.docker\")\n",
    "# pack  = fp.PACK(\"http://127.0.0.1:42112/hapi-fhir-jpaserver/fhir\")\n",
    "# pack.getPatients(['1']).data.iloc[0].serialize()\n",
    "# pack.getPatients(['146053'])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introductory Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "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>birthDate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7ca61bcb-80a6-43fb-ad49-7334df273d39</td>\n",
       "      <td>2007-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7ca61bcb-80a6-43fb-ad49-7334df273d39</td>\n",
       "      <td>2007-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7ca61bcb-80a6-43fb-ad49-7334df273d39</td>\n",
       "      <td>2007-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7ca61bcb-80a6-43fb-ad49-7334df273d39</td>\n",
       "      <td>2007-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7ca61bcb-80a6-43fb-ad49-7334df273d39</td>\n",
       "      <td>2007-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>2744581</td>\n",
       "      <td>1956-08-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>2744581</td>\n",
       "      <td>1956-08-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>2744581</td>\n",
       "      <td>1956-08-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>2744581</td>\n",
       "      <td>1956-08-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>2744581</td>\n",
       "      <td>1956-08-03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>69 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      id   birthDate\n",
       "0   7ca61bcb-80a6-43fb-ad49-7334df273d39  2007-11-18\n",
       "1   7ca61bcb-80a6-43fb-ad49-7334df273d39  2007-11-18\n",
       "2   7ca61bcb-80a6-43fb-ad49-7334df273d39  2007-11-18\n",
       "3   7ca61bcb-80a6-43fb-ad49-7334df273d39  2007-11-18\n",
       "4   7ca61bcb-80a6-43fb-ad49-7334df273d39  2007-11-18\n",
       "..                                   ...         ...\n",
       "64                               2744581  1956-08-03\n",
       "65                               2744581  1956-08-03\n",
       "66                               2744581  1956-08-03\n",
       "67                               2744581  1956-08-03\n",
       "68                               2744581  1956-08-03\n",
       "\n",
       "[69 rows x 2 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# for patients whose lastname is Koepp, find the root identity,\n",
    "# get their associated diagnostic reports and report their ID and status\n",
    "\n",
    "pack.getPatients(searchParams={\"family\":\"koepp\"})\\\n",
    "\t.getDiagnosticReports()\\\n",
    "\t\t.getPatients()\\\n",
    "\t\t\t.getRootPatients()\\\n",
    "\t\t\t\t.gatherSimplePaths(['id','birthDate'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FHIR PACK Usage "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'resourceType': 'Patient',\n",
       " 'id': '647487',\n",
       " 'meta': {'versionId': '1',\n",
       "  'lastUpdated': '2020-03-20T21:38:58.974+00:00',\n",
       "  'source': '#bE19PhJ3nQMhB1xy'},\n",
       " 'text': {'status': 'generated',\n",
       "  'div': '<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\">Series_Status: Not complete Evaluation_Status_1.0: Valid Evaluation_Status_2.0: Valid Evaluation_Status_3.0: Valid Evaluation_Status_4.0: Valid Evaluation_Status_5.0: Valid <b># 5 PENTACEL AT ≥ 4 YRS </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Date of birth</td><td><span>20 January 2016</span></td></tr></tbody></table></div>'},\n",
       " 'name': [{'family': '# 5 Pentacel at ≥ 4 yrs',\n",
       "   'given': ['Series_Status: Not complete',\n",
       "    'Evaluation_Status_1.0: Valid',\n",
       "    'Evaluation_Status_2.0: Valid',\n",
       "    'Evaluation_Status_3.0: Valid',\n",
       "    'Evaluation_Status_4.0: Valid',\n",
       "    'Evaluation_Status_5.0: Valid']}],\n",
       " 'gender': 'female',\n",
       " 'birthDate': '2016-01-20'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patientReference = pack.getReferences(['Patient/647487'])\n",
    "patientReference.data.iloc[0].to_resource().serialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '647487', 'm...</td>\n",
       "      <td>647487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data Patient\n",
       "0  {'resourceType': 'Patient', 'id': '647487', 'm...  647487"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# getResources applied to References simplifies the conversion\n",
    "# of FHIR/FHIRpy References to Resources\n",
    "\n",
    "patientReference.getResources()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Resources"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reference to Resource"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1849165\n",
       "1     647487\n",
       "Name: data, dtype: object"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multiPatientReference = pack.getReferences(\n",
    "    [\n",
    "        'Patient/1849165',\n",
    "        'Patient/647487'\n",
    "    ])\n",
    "\n",
    "multiPatientReference.data.apply( lambda x:x.id )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1849165', '...</td>\n",
       "      <td>1849165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '647487', 'm...</td>\n",
       "      <td>647487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  Patient\n",
       "0  {'resourceType': 'Patient', 'id': '1849165', '...  1849165\n",
       "1  {'resourceType': 'Patient', 'id': '647487', 'm...   647487"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multiPatientReference.getResources()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Direct Resource"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2583214', '...</td>\n",
       "      <td>2583214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2583215', '...</td>\n",
       "      <td>2583215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6844384', '...</td>\n",
       "      <td>6844384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6844385', '...</td>\n",
       "      <td>6844385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6844388', '...</td>\n",
       "      <td>6844388</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>555</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6841240', '...</td>\n",
       "      <td>6841240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>556</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6841241', '...</td>\n",
       "      <td>6841241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>557</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6842052', '...</td>\n",
       "      <td>6842052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>558</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6842064', '...</td>\n",
       "      <td>6842064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>559</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '6842065', '...</td>\n",
       "      <td>6842065</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>560 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  data  Patient\n",
       "0    {'resourceType': 'Patient', 'id': '2583214', '...  2583214\n",
       "1    {'resourceType': 'Patient', 'id': '2583215', '...  2583215\n",
       "2    {'resourceType': 'Patient', 'id': '6844384', '...  6844384\n",
       "3    {'resourceType': 'Patient', 'id': '6844385', '...  6844385\n",
       "4    {'resourceType': 'Patient', 'id': '6844388', '...  6844388\n",
       "..                                                 ...      ...\n",
       "555  {'resourceType': 'Patient', 'id': '6841240', '...  6841240\n",
       "556  {'resourceType': 'Patient', 'id': '6841241', '...  6841241\n",
       "557  {'resourceType': 'Patient', 'id': '6842052', '...  6842052\n",
       "558  {'resourceType': 'Patient', 'id': '6842064', '...  6842064\n",
       "559  {'resourceType': 'Patient', 'id': '6842065', '...  6842065\n",
       "\n",
       "[560 rows x 2 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "\tsearchParams={\"given\":\"max\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                 \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(12, 2)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "\tsearchParams={\"family\":\"Betterhalf\"}\n",
    ").shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1849165', '...</td>\n",
       "      <td>1849165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '647487', 'm...</td>\n",
       "      <td>647487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  Patient\n",
       "0  {'resourceType': 'Patient', 'id': '1849165', '...  1849165\n",
       "1  {'resourceType': 'Patient', 'id': '647487', 'm...   647487"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multiPatientResource = pack.getResources(\n",
    "    ['Patient/1849165',\n",
    "     'Patient/647487']\n",
    ")\n",
    "\n",
    "multiPatientResource"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Serialization and Element Access"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Series_Status: Not complete',\n",
       " 'Evaluation_Status_1.0: Valid',\n",
       " 'Evaluation_Status_2.0: Valid',\n",
       " 'Evaluation_Status_3.0: Valid',\n",
       " 'Evaluation_Status_4.0: Valid',\n",
       " 'Evaluation_Status_5.0: Valid']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'2016-01-20'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patientResource = patientReference.data.iloc[0].to_resource()\n",
    "\n",
    "patientResource['name'][0]['given']\n",
    "\n",
    "patientResource.serialize().get('birthDate')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "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>id</th>\n",
       "      <th>resourceType</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1849165</td>\n",
       "      <td>Patient</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>647487</td>\n",
       "      <td>Patient</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id resourceType\n",
       "0  1849165      Patient\n",
       "1   647487      Patient"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "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",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "      <th>country</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1849165</td>\n",
       "      <td>[[Peter, James], [Jim], [Peter, James]]</td>\n",
       "      <td>[Chalmers, Windsor]</td>\n",
       "      <td>1974-12-25</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>647487</td>\n",
       "      <td>[[Series_Status: Not complete, Evaluation_Stat...</td>\n",
       "      <td>[# 5 Pentacel at ≥ 4 yrs]</td>\n",
       "      <td>2016-01-20</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id                                         name.given  \\\n",
       "0  1849165            [[Peter, James], [Jim], [Peter, James]]   \n",
       "1   647487  [[Series_Status: Not complete, Evaluation_Stat...   \n",
       "\n",
       "                 name.family   birthDate  city state country  \n",
       "0        [Chalmers, Windsor]  1974-12-25  None  None    None  \n",
       "1  [# 5 Pentacel at ≥ 4 yrs]  2016-01-20  None  None    None  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multiPatientReference.summary\n",
    "multiPatientReference.getResources().summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    {\n",
      "        \"_birthDate\": {\n",
      "            \"extension\": [\n",
      "                {\n",
      "                    \"url\": \"http://hl7.org/fhir/StructureDefinition/patient-birthTime\",\n",
      "                    \"valueDateTime\": \"1974-12-25T14:35:45-05:00\"\n",
      "                }\n",
      "            ]\n",
      "        },\n",
      "        \"active\": true,\n",
      "        \"address\": [\n",
      "            {\n",
      "                \"city\": \"PleasantVille\",\n",
      "                \"district\": \"Rainbow\",\n",
      "                \"line\": [\n",
      "                    \"534 Erewhon St\"\n",
      "                ],\n",
      "                \"period\": {\n",
      "                    \"start\": \"1974-12-25\"\n",
      "                },\n",
      "                \"postalCode\": \"3999\",\n",
      "                \"state\": \"Vic\",\n",
      "                \"text\": \"534 Erewhon St PeasantVille, Rainbow, Vic  3999\",\n",
      "                \"type\": \"both\",\n",
      "                \"use\": \"home\"\n",
      "            }\n",
      "        ],\n",
      "        \"birthDate\": \"1974-12-25\",\n",
      "        \"contact\": [\n",
      "            {\n",
      "                \"address\": {\n",
      "                    \"city\": \"PleasantVille\",\n",
      "                    \"district\": \"Rainbow\",\n",
      "                    \"line\": [\n",
      "                        \"534 Erewhon St\"\n",
      "                    ],\n",
      "                    \"period\": {\n",
      "                        \"start\": \"1974-12-25\"\n",
      "                    },\n",
      "                    \"postalCode\": \"3999\",\n",
      "                    \"state\": \"Vic\",\n",
      "                    \"type\": \"both\",\n",
      "                    \"use\": \"home\"\n",
      "                },\n",
      "                \"gender\": \"female\",\n",
      "                \"name\": {\n",
      "                    \"_family\": {\n",
      "                        \"extension\": [\n",
      "                            {\n",
      "                                \"url\": \"http://hl7.org/fhir/StructureDefinition/humanname-own-prefix\",\n",
      "                                \"valueString\": \"VV\"\n",
      "                            }\n",
      "                        ]\n",
      "                    },\n",
      "                    \"family\": \"du March\\u00e9\",\n",
      "                    \"given\": [\n",
      "                        \"B\\u00e9n\\u00e9dicte\"\n",
      "                    ]\n",
      "                },\n",
      "                \"period\": {\n",
      "                    \"start\": \"2012\"\n",
      "                },\n",
      "                \"relationship\": [\n",
      "                    {\n",
      "                        \"coding\": [\n",
      "                            {\n",
      "                                \"code\": \"N\",\n",
      "                                \"system\": \"http://terminology.hl7.org/CodeSystem/v2-0131\"\n",
      "                            }\n",
      "                        ]\n",
      "                    }\n",
      "                ],\n",
      "                \"telecom\": [\n",
      "                    {\n",
      "                        \"system\": \"phone\",\n",
      "                        \"value\": \"+33 (237) 998327\"\n",
      "                    }\n",
      "                ]\n",
      "            }\n",
      "        ],\n",
      "        \"deceasedBoolean\": false,\n",
      "        \"gender\": \"male\",\n",
      "        \"id\": \"1849165\",\n",
      "        \"identifier\": [\n",
      "            {\n",
      "                \"assigner\": {\n",
      "                    \"display\": \"Acme Healthcare\"\n",
      "                },\n",
      "                \"period\": {\n",
      "                    \"start\": \"2001-05-06\"\n",
      "                },\n",
      "                \"system\": \"urn:oid:1.2.36.146.595.217.0.1\",\n",
      "                \"type\": {\n",
      "                    \"coding\": [\n",
      "                        {\n",
      "                            \"code\": \"MR\",\n",
      "                            \"system\": \"http://terminology.hl7.org/CodeSystem/v2-0203\"\n",
      "                        }\n",
      "                    ]\n",
      "                },\n",
      "                \"use\": \"usual\",\n",
      "                \"value\": \"12345\"\n",
      "            }\n",
      "        ],\n",
      "        \"managingOrganization\": {\n",
      "            \"reference\": \"Organization/1845391\"\n",
      "        },\n",
      "        \"meta\": {\n",
      "            \"lastUpdated\": \"2021-02-08T21:42:25.829+00:00\",\n",
      "            \"source\": \"#PBPO58xnJNERsS1A\",\n",
      "            \"versionId\": \"1\"\n",
      "        },\n",
      "        \"name\": [\n",
      "            {\n",
      "                \"family\": \"Chalmers\",\n",
      "                \"given\": [\n",
      "                    \"Peter\",\n",
      "                    \"James\"\n",
      "                ],\n",
      "                \"use\": \"official\"\n",
      "            },\n",
      "            {\n",
      "                \"given\": [\n",
      "                    \"Jim\"\n",
      "                ],\n",
      "                \"use\": \"usual\"\n",
      "            },\n",
      "            {\n",
      "                \"family\": \"Windsor\",\n",
      "                \"given\": [\n",
      "                    \"Peter\",\n",
      "                    \"James\"\n",
      "                ],\n",
      "                \"period\": {\n",
      "                    \"end\": \"2002\"\n",
      "                },\n",
      "                \"use\": \"maiden\"\n",
      "            }\n",
      "        ],\n",
      "        \"resourceType\": \"Patient\",\n",
      "        \"telecom\": [\n",
      "            {\n",
      "                \"use\": \"home\"\n",
      "            },\n",
      "            {\n",
      "                \"rank\": 1,\n",
      "                \"system\": \"phone\",\n",
      "                \"use\": \"work\",\n",
      "                \"value\": \"(03) 5555 6473\"\n",
      "            },\n",
      "            {\n",
      "                \"rank\": 2,\n",
      "                \"system\": \"phone\",\n",
      "                \"use\": \"mobile\",\n",
      "                \"value\": \"(03) 3410 5613\"\n",
      "            },\n",
      "            {\n",
      "                \"period\": {\n",
      "                    \"end\": \"2014\"\n",
      "                },\n",
      "                \"system\": \"phone\",\n",
      "                \"use\": \"old\",\n",
      "                \"value\": \"(03) 5555 8834\"\n",
      "            }\n",
      "        ],\n",
      "        \"text\": {\n",
      "            \"div\": \"<div xmlns=\\\"http://www.w3.org/1999/xhtml\\\">\\n\\t\\t\\t<table>\\n\\t\\t\\t\\t<tbody>\\n\\t\\t\\t\\t\\t<tr>\\n\\t\\t\\t\\t\\t\\t<td>Name</td>\\n\\t\\t\\t\\t\\t\\t<td>Peter James \\n              <b>Chalmers</b> (&quot;Jim&quot;)\\n            </td>\\n\\t\\t\\t\\t\\t</tr>\\n\\t\\t\\t\\t\\t<tr>\\n\\t\\t\\t\\t\\t\\t<td>Address</td>\\n\\t\\t\\t\\t\\t\\t<td>534 Erewhon, Pleasantville, Vic, 3999</td>\\n\\t\\t\\t\\t\\t</tr>\\n\\t\\t\\t\\t\\t<tr>\\n\\t\\t\\t\\t\\t\\t<td>Contacts</td>\\n\\t\\t\\t\\t\\t\\t<td>Home: unknown. Work: (03) 5555 6473</td>\\n\\t\\t\\t\\t\\t</tr>\\n\\t\\t\\t\\t\\t<tr>\\n\\t\\t\\t\\t\\t\\t<td>Id</td>\\n\\t\\t\\t\\t\\t\\t<td>MRN: 12345 (Acme Healthcare)</td>\\n\\t\\t\\t\\t\\t</tr>\\n\\t\\t\\t\\t</tbody>\\n\\t\\t\\t</table>\\n\\t\\t</div>\",\n",
      "            \"status\": \"generated\"\n",
      "        }\n",
      "    }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "multiPatientResource[:1].pretty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                      "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "resourceType\n",
      "id\n",
      "meta\n",
      "meta.versionId\n",
      "meta.lastUpdated\n",
      "meta.source\n",
      "text\n",
      "text.status\n",
      "text.div\n",
      "name\n",
      "name.family\n",
      "name.given\n",
      "gender\n",
      "birthDate\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r"
     ]
    }
   ],
   "source": [
    "pack.getDiagnosticReports(\n",
    "    ['Patient/647487']\n",
    ")[:1].keys\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame Operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "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>{'reference': 'Patient/1849165'}</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               data\n",
       "0  {'reference': 'Patient/1849165'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "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>1</th>\n",
       "      <td>{'reference': 'Patient/647487'}</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              data\n",
       "1  {'reference': 'Patient/647487'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[<SyncFHIRReference Patient/1849165>],\n",
       "       [<SyncFHIRReference Patient/647487>]], dtype=object)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'fhirpack.base.Frame'>\n",
      "RangeIndex: 2 entries, 0 to 1\n",
      "Data columns (total 1 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   data    2 non-null      object\n",
      "dtypes: object(1)\n",
      "memory usage: 144.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "multiPatientReference[:1]\n",
    "\n",
    "multiPatientReference[-1:]\n",
    "\n",
    "multiPatientReference.values\n",
    "\n",
    "multiPatientReference.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pack.base.Frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "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': 'Patient', 'id': '647487', 'm...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'Patient', 'id': '647487', 'm..."
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp.base.Frame( [[patientResource]], columns=['data'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Extraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Patient Extraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getPatients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '647487', 'm...</td>\n",
       "      <td>647487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data Patient\n",
       "0  {'resourceType': 'Patient', 'id': '647487', 'm...  647487"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "\t[\n",
    "\t\t'647487'\n",
    "\t]\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                             \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'resourceType': 'Patient',\n",
       " 'id': '647487',\n",
       " 'meta': {'versionId': '1',\n",
       "  'lastUpdated': '2020-03-20T21:38:58.974+00:00',\n",
       "  'source': '#bE19PhJ3nQMhB1xy'},\n",
       " 'text': {'status': 'generated',\n",
       "  'div': '<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\">Series_Status: Not complete Evaluation_Status_1.0: Valid Evaluation_Status_2.0: Valid Evaluation_Status_3.0: Valid Evaluation_Status_4.0: Valid Evaluation_Status_5.0: Valid <b># 5 PENTACEL AT ≥ 4 YRS </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Date of birth</td><td><span>20 January 2016</span></td></tr></tbody></table></div>'},\n",
       " 'name': [{'family': '# 5 Pentacel at ≥ 4 yrs',\n",
       "   'given': ['Series_Status: Not complete',\n",
       "    'Evaluation_Status_1.0: Valid',\n",
       "    'Evaluation_Status_2.0: Valid',\n",
       "    'Evaluation_Status_3.0: Valid',\n",
       "    'Evaluation_Status_4.0: Valid',\n",
       "    'Evaluation_Status_5.0: Valid']}],\n",
       " 'gender': 'female',\n",
       " 'birthDate': '2016-01-20'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "\t[\n",
    "\t\t'647487'\n",
    "\t]\n",
    ").data.item().serialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                             "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Row 0 with Patient ID 647487\n",
      "Row 1 with Patient ID 1849165\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r"
     ]
    }
   ],
   "source": [
    "for i, r in pack.getPatients(['647487','1849165']).iterrows():\n",
    "    print (f\"Row {i} with Patient ID {r.data.id}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                             \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[<SyncFHIRResource Patient/647487>, '647487'],\n",
       " [<SyncFHIRResource Patient/1849165>, '1849165']]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getPatientsAsList=pack.getPatients(['647487','1849165']).cast('list')\n",
    "\n",
    "getPatientsAsList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[<SyncFHIRResource Patient/647487>, '647487'],\n",
       " [<SyncFHIRResource Patient/1849165>, '1849165']]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getPatientsAsList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['647487', '1849165']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['647487', '1849165']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[e[1] for e in getPatientsAsList]\n",
    "\n",
    "[e[0].id for e in getPatientsAsList]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Patients as Operand and Extracting other Resources "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                             \r"
     ]
    }
   ],
   "source": [
    "patients = pack.getPatients(\n",
    "    ['9ac622c4-61c8-40b4-b6d9-06cfeb3d6995',\n",
    "    '1849165',\n",
    "    '649231',\n",
    "    '650224']\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getRootPatient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "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",
       "      <th>RootPatient</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '9ac622c4-61...</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1849165', '...</td>\n",
       "      <td>1849165</td>\n",
       "      <td>1849165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '649231', 'm...</td>\n",
       "      <td>649231</td>\n",
       "      <td>649231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '650224', 'm...</td>\n",
       "      <td>650224</td>\n",
       "      <td>650224</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'Patient', 'id': '9ac622c4-61...   \n",
       "1  {'resourceType': 'Patient', 'id': '1849165', '...   \n",
       "2  {'resourceType': 'Patient', 'id': '649231', 'm...   \n",
       "3  {'resourceType': 'Patient', 'id': '650224', 'm...   \n",
       "\n",
       "                            RootPatient                               Patient  \n",
       "0  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  \n",
       "1                               1849165                               1849165  \n",
       "2                                649231                                649231  \n",
       "3                                650224                                650224  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patients.getRootPatients()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "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>data</th>\n",
       "      <th>LinkedPatient</th>\n",
       "      <th>RootPatient</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': 'b558da74-77...</td>\n",
       "      <td>b558da74-7756-4845-87d5-d1cca8b79a62</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>b558da74-7756-4845-87d5-d1cca8b79a62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'Patient', 'id': 'b558da74-77...   \n",
       "\n",
       "                          LinkedPatient                           RootPatient  \\\n",
       "0  b558da74-7756-4845-87d5-d1cca8b79a62  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995   \n",
       "\n",
       "                                Patient  \n",
       "0  b558da74-7756-4845-87d5-d1cca8b79a62  "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getRootPatients(\n",
    "    [\n",
    "        'b558da74-7756-4845-87d5-d1cca8b79a62',\n",
    "    ],\n",
    ").getLinkedPatients()\n",
    "\n",
    "# .gatherSimplePaths([\"id\"]).id.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "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>data</th>\n",
       "      <th>RootPatient</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '9ac622c4-61...</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>b558da74-7756-4845-87d5-d1cca8b79a62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'Patient', 'id': '9ac622c4-61...   \n",
       "\n",
       "                            RootPatient                               Patient  \n",
       "0  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  b558da74-7756-4845-87d5-d1cca8b79a62  "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getResources(\n",
    "\t[\n",
    "\t\t'Patient/b558da74-7756-4845-87d5-d1cca8b79a62'\n",
    "\t]\n",
    ").getRootPatients()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "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>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>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>[[TestGiven]]</td>\n",
       "      <td>[TestFamily]</td>\n",
       "      <td>1981-01-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1849165</td>\n",
       "      <td>[[Peter, James], [Jim], [Peter, James]]</td>\n",
       "      <td>[Chalmers, Windsor]</td>\n",
       "      <td>1974-12-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>649231</td>\n",
       "      <td>[[Series_Status: Complete, Evaluation_Status_1...</td>\n",
       "      <td>[Dose # 1 PCV 13 at age 12 mos # 2 at 1 yr 8 w...</td>\n",
       "      <td>2018-12-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>650224</td>\n",
       "      <td>[[Series_Status: Not complete, Evaluation_Stat...</td>\n",
       "      <td>[# 1 at age ≥ 13 years old]</td>\n",
       "      <td>2007-02-08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     id  \\\n",
       "0  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995   \n",
       "1                               1849165   \n",
       "2                                649231   \n",
       "3                                650224   \n",
       "\n",
       "                                          name.given  \\\n",
       "0                                      [[TestGiven]]   \n",
       "1            [[Peter, James], [Jim], [Peter, James]]   \n",
       "2  [[Series_Status: Complete, Evaluation_Status_1...   \n",
       "3  [[Series_Status: Not complete, Evaluation_Stat...   \n",
       "\n",
       "                                         name.family   birthDate  \n",
       "0                                       [TestFamily]  1981-01-01  \n",
       "1                                [Chalmers, Windsor]  1974-12-25  \n",
       "2  [Dose # 1 PCV 13 at age 12 mos # 2 at 1 yr 8 w...  2018-12-21  \n",
       "3                        [# 1 at age ≥ 13 years old]  2007-02-08  "
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rootPatients = patients.getRootPatients()\n",
    "\n",
    "rootPatients.gatherSimplePaths(['id','name.given','name.family','birthDate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'family': 'TestFamily', 'given': ['TestGiven']}]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rootPatients.data[0].name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b558da74-7756-4845-87d5-d1cca8b79a62\n"
     ]
    }
   ],
   "source": [
    "for link in rootPatients.data[0]['link']:\n",
    "\tlpat=link.other.to_resource()\n",
    "\tprint(lpat.id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getLinkedPatients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "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>data</th>\n",
       "      <th>LinkedPatient</th>\n",
       "      <th>RootPatient</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': 'b558da74-77...</td>\n",
       "      <td>b558da74-7756-4845-87d5-d1cca8b79a62</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'Patient', 'id': 'b558da74-77...   \n",
       "\n",
       "                          LinkedPatient                           RootPatient  \\\n",
       "0  b558da74-7756-4845-87d5-d1cca8b79a62  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995   \n",
       "\n",
       "                                Patient  \n",
       "0  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getLinkedPatients(\n",
    "\t[\n",
    "\t\t'9ac622c4-61c8-40b4-b6d9-06cfeb3d6995',\n",
    "\t]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getConditions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "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>data</th>\n",
       "      <th>Condition</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1584692',...</td>\n",
       "      <td>1584692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1591180',...</td>\n",
       "      <td>1591180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1402386',...</td>\n",
       "      <td>1402386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1403562',...</td>\n",
       "      <td>1403562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1495528',...</td>\n",
       "      <td>1495528</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data Condition\n",
       "0  {'resourceType': 'Condition', 'id': '1584692',...   1584692\n",
       "1  {'resourceType': 'Condition', 'id': '1591180',...   1591180\n",
       "2  {'resourceType': 'Condition', 'id': '1402386',...   1402386\n",
       "3  {'resourceType': 'Condition', 'id': '1403562',...   1403562\n",
       "4  {'resourceType': 'Condition', 'id': '1495528',...   1495528"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions = pack.getConditions(\n",
    "\tsearchParams={\n",
    "\t\t'code':'44465007',\n",
    "\t\t'_sort': '-onset-date',\n",
    "\t}\n",
    ")[:5]\n",
    "\n",
    "conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Patients for Conditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "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",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>70 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id name.given name.family\n",
       "0   None       None        None\n",
       "1   None       None        None\n",
       "2   None       None        None\n",
       "3   None       None        None\n",
       "4   None       None        None\n",
       "..   ...        ...         ...\n",
       "65  None       None        None\n",
       "66  None       None        None\n",
       "67  None       None        None\n",
       "68  None       None        None\n",
       "69  None       None        None\n",
       "\n",
       "[70 rows x 3 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions.getPatients().explode().gatherSimplePaths(['id','name.given','name.family'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                     \r"
     ]
    }
   ],
   "source": [
    "conditions\\\n",
    "\t.getPatients()\\\n",
    "\t\t\t.data.apply(lambda x:x.id)\\\n",
    "\t\t\t\t.to_csv(f\"data.ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ",data\n",
      "0,1402076\n",
      "1,1403421\n",
      "2,1495412\n",
      "3,1583909\n",
      "4,1590885\n"
     ]
    }
   ],
   "source": [
    "!cat data.ignore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 1)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "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>1402076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1403421</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1495412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1583909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1590885</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      data\n",
       "0  1402076\n",
       "1  1403421\n",
       "2  1495412\n",
       "3  1583909\n",
       "4  1590885"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions = pd.read_csv( f\"data.ignore\", index_col=0)\n",
    "conditions.shape\n",
    "conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conditions for Patient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "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>data</th>\n",
       "      <th>Condition</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '2b7a4d69-...</td>\n",
       "      <td>2b7a4d69-00fd-4574-86da-bdf46b21b72b</td>\n",
       "      <td>8cbf1128-3644-47a1-9cc8-05f1aac6071d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '0fc73e7c-...</td>\n",
       "      <td>0fc73e7c-18a8-47d8-bdc9-82df7f06e4a8</td>\n",
       "      <td>8cbf1128-3644-47a1-9cc8-05f1aac6071d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': 'cb1bab6c-...</td>\n",
       "      <td>cb1bab6c-581b-47f4-8036-429462224f55</td>\n",
       "      <td>8cbf1128-3644-47a1-9cc8-05f1aac6071d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': 'bc301bbf-...</td>\n",
       "      <td>bc301bbf-d354-4422-abe8-f7fe9c48d83c</td>\n",
       "      <td>8cbf1128-3644-47a1-9cc8-05f1aac6071d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '5db4866a-...</td>\n",
       "      <td>5db4866a-c48a-4b62-923b-96daa243226b</td>\n",
       "      <td>8cbf1128-3644-47a1-9cc8-05f1aac6071d</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'Condition', 'id': '2b7a4d69-...   \n",
       "1  {'resourceType': 'Condition', 'id': '0fc73e7c-...   \n",
       "2  {'resourceType': 'Condition', 'id': 'cb1bab6c-...   \n",
       "3  {'resourceType': 'Condition', 'id': 'bc301bbf-...   \n",
       "4  {'resourceType': 'Condition', 'id': '5db4866a-...   \n",
       "\n",
       "                              Condition                               Patient  \n",
       "0  2b7a4d69-00fd-4574-86da-bdf46b21b72b  8cbf1128-3644-47a1-9cc8-05f1aac6071d  \n",
       "1  0fc73e7c-18a8-47d8-bdc9-82df7f06e4a8  8cbf1128-3644-47a1-9cc8-05f1aac6071d  \n",
       "2  cb1bab6c-581b-47f4-8036-429462224f55  8cbf1128-3644-47a1-9cc8-05f1aac6071d  \n",
       "3  bc301bbf-d354-4422-abe8-f7fe9c48d83c  8cbf1128-3644-47a1-9cc8-05f1aac6071d  \n",
       "4  5db4866a-c48a-4b62-923b-96daa243226b  8cbf1128-3644-47a1-9cc8-05f1aac6071d  "
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['8cbf1128-3644-47a1-9cc8-05f1aac6071d']).getConditions()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getEpisodesOfCare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "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",
       "      <th>RootPatient</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '9ac622c4-61...</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1849165', '...</td>\n",
       "      <td>1849165</td>\n",
       "      <td>1849165</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'Patient', 'id': '9ac622c4-61...   \n",
       "1  {'resourceType': 'Patient', 'id': '1849165', '...   \n",
       "\n",
       "                            RootPatient                               Patient  \n",
       "0  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  \n",
       "1                               1849165                               1849165  "
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rootPatients[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "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>data</th>\n",
       "      <th>EpisodeOfCare</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'EpisodeOfCare', 'id': 'P0522...</td>\n",
       "      <td>P0522-eLTSS-episode-Initial</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data  \\\n",
       "0  {'resourceType': 'EpisodeOfCare', 'id': 'P0522...   \n",
       "\n",
       "                 EpisodeOfCare Patient  \n",
       "0  P0522-eLTSS-episode-Initial    None  "
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['P0522-patientBSJ1']).getEpisodesOfCare()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getFamilyMemberHistories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "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>data</th>\n",
       "      <th>FamilyMemberHistory</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty Frame\n",
       "Columns: [data, FamilyMemberHistory, Patient]\n",
       "Index: []"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/2866670']).getFamilyMemberHistories()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getMedicationAdministrations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "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>data</th>\n",
       "      <th>MedicationAdministration</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'MedicationAdministration', '...</td>\n",
       "      <td>31675</td>\n",
       "      <td>31678</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data MedicationAdministration  \\\n",
       "0  {'resourceType': 'MedicationAdministration', '...                    31675   \n",
       "\n",
       "  Patient  \n",
       "0   31678  "
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/31678']).getMedicationAdministrations()[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getMedicationRequests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "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>data</th>\n",
       "      <th>MedicationRequest</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty Frame\n",
       "Columns: [data, MedicationRequest, Patient]\n",
       "Index: []"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/202205uscore-patient-example-1']).getMedicationRequests().explode()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getObservations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "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>data</th>\n",
       "      <th>Patient</th>\n",
       "      <th>Condition</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '258974', 'm...</td>\n",
       "      <td>258974</td>\n",
       "      <td>259094</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data Patient Condition\n",
       "0  {'resourceType': 'Patient', 'id': '258974', 'm...  258974    259094"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack\\\n",
    "\t.getPatients(['Patient/258974'])\\\n",
    "\t\t.getConditions(searchParams={\"code\":\"44465007\"})\\\n",
    "\t\t\t\t.getPatients(searchParams={\"family\":\"Keebler762\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "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>issued</th>\n",
       "      <th>code.coding.code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2164039</td>\n",
       "      <td>2021-06-05 05:30:23.291</td>\n",
       "      <td>29463-7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id                  issued code.coding.code\n",
       "0  2164039 2021-06-05 05:30:23.291          29463-7"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "from datetime import datetime\n",
    "\n",
    "interesting = pack.getPatients(['Patient/2164033']).\\\n",
    "    getObservations(\n",
    "        searchParams={\n",
    "            \"code\":\"http ://loinc.org|29463-7\",\n",
    "        })\\\n",
    "        .gatherSimplePaths(['id','issued','code.coding.code'])\\\n",
    "        .explode(['code.coding.code'])\n",
    "\n",
    "interesting.issued = pd.to_datetime(interesting.issued).dropna().apply(lambda x:x.replace(tzinfo=None))\n",
    "interesting[interesting.issued> datetime.fromisoformat('2021-01-01T00:00:00')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getDiagnosticReports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "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>data</th>\n",
       "      <th>DiagnosticReport</th>\n",
       "      <th>Patient</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '12...</td>\n",
       "      <td>122da94f-d24b-47ef-8409-58eb023cfb78</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '5a...</td>\n",
       "      <td>5a1a396b-783c-495c-ace9-cedce2f0c335</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '2d...</td>\n",
       "      <td>2d363733-58cb-4aba-b73f-487181359418</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '01...</td>\n",
       "      <td>013d71cd-e38e-497c-9382-b21fb5eb5f53</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '47...</td>\n",
       "      <td>471713d6-69eb-4fcf-900d-2eccf9d10f69</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'b1...</td>\n",
       "      <td>b17c7848-f6c5-49a7-bd0a-f018ecc422de</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '58...</td>\n",
       "      <td>58c69ea7-b4be-40a5-a59e-459b8fed660a</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '87...</td>\n",
       "      <td>873433a5-68ed-42da-a6a0-d0ef3f7fd9ef</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '8c...</td>\n",
       "      <td>8c5adc06-2c8c-431f-9492-d7c9ef99635d</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'ac...</td>\n",
       "      <td>ac83cf63-9ea8-43ce-89a8-21a6b3d3319d</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'bc...</td>\n",
       "      <td>bcd144a0-7a88-4a7c-bcb1-acf8601d6f54</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'bf...</td>\n",
       "      <td>bf0754d5-26cd-46b3-a675-32e718d17a28</td>\n",
       "      <td>9b8c1901-62ee-48a7-8229-b771d59f1e5f</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 data  \\\n",
       "0   {'resourceType': 'DiagnosticReport', 'id': '12...   \n",
       "1   {'resourceType': 'DiagnosticReport', 'id': '5a...   \n",
       "2   {'resourceType': 'DiagnosticReport', 'id': '2d...   \n",
       "3   {'resourceType': 'DiagnosticReport', 'id': '01...   \n",
       "4   {'resourceType': 'DiagnosticReport', 'id': '47...   \n",
       "5   {'resourceType': 'DiagnosticReport', 'id': 'b1...   \n",
       "6   {'resourceType': 'DiagnosticReport', 'id': '58...   \n",
       "7   {'resourceType': 'DiagnosticReport', 'id': '87...   \n",
       "8   {'resourceType': 'DiagnosticReport', 'id': '8c...   \n",
       "9   {'resourceType': 'DiagnosticReport', 'id': 'ac...   \n",
       "10  {'resourceType': 'DiagnosticReport', 'id': 'bc...   \n",
       "11  {'resourceType': 'DiagnosticReport', 'id': 'bf...   \n",
       "\n",
       "                        DiagnosticReport                               Patient  \n",
       "0   122da94f-d24b-47ef-8409-58eb023cfb78  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "1   5a1a396b-783c-495c-ace9-cedce2f0c335  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "2   2d363733-58cb-4aba-b73f-487181359418  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "3   013d71cd-e38e-497c-9382-b21fb5eb5f53  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "4   471713d6-69eb-4fcf-900d-2eccf9d10f69  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "5   b17c7848-f6c5-49a7-bd0a-f018ecc422de  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "6   58c69ea7-b4be-40a5-a59e-459b8fed660a  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "7   873433a5-68ed-42da-a6a0-d0ef3f7fd9ef  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "8   8c5adc06-2c8c-431f-9492-d7c9ef99635d  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "9   ac83cf63-9ea8-43ce-89a8-21a6b3d3319d  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "10  bcd144a0-7a88-4a7c-bcb1-acf8601d6f54  9b8c1901-62ee-48a7-8229-b771d59f1e5f  \n",
       "11  bf0754d5-26cd-46b3-a675-32e718d17a28  9b8c1901-62ee-48a7-8229-b771d59f1e5f  "
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diagnosticReports=pack\\\n",
    "\t.getPatients([\"Patient/9b8c1901-62ee-48a7-8229-b771d59f1e5f\"])\\\n",
    "\t\t.getDiagnosticReports(\n",
    "\t\t\tsearchParams={}\n",
    "\t\t\t)\\\n",
    "\n",
    "diagnosticReports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "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>subject</th>\n",
       "      <th>presentedForm.contentType</th>\n",
       "      <th>presentedForm.data</th>\n",
       "      <th>presentedForm.url</th>\n",
       "      <th>presentedForm.title</th>\n",
       "      <th>presentedForm.creation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'reference': 'Patient/e92abcdc-a300-41e0-aa0f...</td>\n",
       "      <td>[text/plain]</td>\n",
       "      <td>[Q2pJd01UQXRNVEV0TVRNS0NpTWdRMmhwWldZZ1EyOXRjR...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             subject  \\\n",
       "0  {'reference': 'Patient/e92abcdc-a300-41e0-aa0f...   \n",
       "\n",
       "  presentedForm.contentType  \\\n",
       "0              [text/plain]   \n",
       "\n",
       "                                  presentedForm.data presentedForm.url  \\\n",
       "0  [Q2pJd01UQXRNVEV0TVRNS0NpTWdRMmhwWldZZ1EyOXRjR...              None   \n",
       "\n",
       "  presentedForm.title presentedForm.creation  \n",
       "0                None                   None  "
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "paths=[\n",
    "\t\"subject\",\n",
    "\t\"presentedForm.contentType\",\n",
    "\t\"presentedForm.data\",\n",
    "\t\"presentedForm.url\",\n",
    "\t\"presentedForm.title\",\n",
    "\t\"presentedForm.creation\"\n",
    "]\n",
    "\n",
    "\n",
    "\n",
    "diagnosticReports=pack.getDiagnosticReports(\n",
    "\tsearchParams={\n",
    "\t\t\"_id\":\"19bdf90a-8ca4-4921-8aeb-2bf3423aaf09\",\n",
    "\t\t# \"identifier\":\"|\",\n",
    "\t\t# \"_content\":\"covid19\",\n",
    "\t\t# \"code\":\"|\",\n",
    "\t\t# \"issued__gt\":\"2010-01-01\",\n",
    "\t\t# \"issued__lt\":\"2011-01-01\"\n",
    "\t}\n",
    ")\n",
    "\n",
    "diagnosticReports.gatherSimplePaths(paths)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getURLBytes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "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>subject.reference</th>\n",
       "      <th>presentedForm.url</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1ca4c411-116d-49d7-82e0-cde6bff90cfd</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>126df183-9908-4df3-80a3-128a10fd6b3a</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19bdf90a-8ca4-4921-8aeb-2bf3423aaf09</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>71701d6e-3bba-44c0-af75-271558aedb22</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>540317ac-5490-4186-8930-8bfbdaad1055</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>97f7f1b1-661f-4076-9352-e698cdf9ef6f</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>9995355f-1361-45de-aebb-a7446f8f13f7</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>34c775ba-b76d-4ef0-a69f-ab88e90ad151</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1ff86952-9480-45b5-953d-e6056f74659f</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5bd0f37e-7c1f-40ac-b654-97fa66875f70</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     id  \\\n",
       "0  1ca4c411-116d-49d7-82e0-cde6bff90cfd   \n",
       "1  126df183-9908-4df3-80a3-128a10fd6b3a   \n",
       "2  19bdf90a-8ca4-4921-8aeb-2bf3423aaf09   \n",
       "3  71701d6e-3bba-44c0-af75-271558aedb22   \n",
       "4  540317ac-5490-4186-8930-8bfbdaad1055   \n",
       "5  97f7f1b1-661f-4076-9352-e698cdf9ef6f   \n",
       "6  9995355f-1361-45de-aebb-a7446f8f13f7   \n",
       "7  34c775ba-b76d-4ef0-a69f-ab88e90ad151   \n",
       "8  1ff86952-9480-45b5-953d-e6056f74659f   \n",
       "9  5bd0f37e-7c1f-40ac-b654-97fa66875f70   \n",
       "\n",
       "                              subject.reference presentedForm.url  \n",
       "0  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "1  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "2  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "3  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "4  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "5  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "6  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "7  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "8  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "9  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "paths=[\n",
    "    'id',\n",
    "    'subject.reference',\n",
    "    'presentedForm.url'\n",
    "]\n",
    "\n",
    "diagnosticReports = pack.getPatients(\n",
    "    ['Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc']\n",
    "    )\\\n",
    "        .getDiagnosticReports()[:10]\n",
    "\n",
    "diagnosticReports=diagnosticReports.gatherSimplePaths(paths)\n",
    "diagnosticReports=diagnosticReports.explode('presentedForm.url')\n",
    "diagnosticReports\n",
    "\n",
    "# diagnosticReports['path']=diagnosticReports['id']+'_'+diagnosticReports['presentedForm.url'].str.split('/').str[-1:].str[0]\n",
    "# diagnosticReports['data']=diagnosticReports.getURLBytes(operateOnCol='presentedForm.url')\n",
    "# diagnosticReports.sendBytesToFile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "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>id</th>\n",
       "      <th>subject.reference</th>\n",
       "      <th>presentedForm.url</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1ca4c411-116d-49d7-82e0-cde6bff90cfd</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>126df183-9908-4df3-80a3-128a10fd6b3a</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19bdf90a-8ca4-4921-8aeb-2bf3423aaf09</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>71701d6e-3bba-44c0-af75-271558aedb22</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>540317ac-5490-4186-8930-8bfbdaad1055</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     id  \\\n",
       "0  1ca4c411-116d-49d7-82e0-cde6bff90cfd   \n",
       "1  126df183-9908-4df3-80a3-128a10fd6b3a   \n",
       "2  19bdf90a-8ca4-4921-8aeb-2bf3423aaf09   \n",
       "3  71701d6e-3bba-44c0-af75-271558aedb22   \n",
       "4  540317ac-5490-4186-8930-8bfbdaad1055   \n",
       "\n",
       "                              subject.reference presentedForm.url  \n",
       "0  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "1  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "2  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "3  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  \n",
       "4  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc              None  "
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diagnosticReports.loc[:, diagnosticReports.columns!='data'][:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.base.getAbsolutePaths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<SyncFHIRClient https://hapi.fhir.org/baseR4>"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                   \r"
     ]
    }
   ],
   "source": [
    "absolutePaths=[\n",
    "#    'Condition.code.coding.code',\n",
    "   'Condition.id',\n",
    "    'Condition.subject.reference',\n",
    "    'Encounter.id',\n",
    "    'Encounter.participant.individual.reference',\n",
    "#     'Procedure.code.coding.display',\n",
    "#     'Procedure.code.coding.code',\n",
    "    'Procedure.id',\n",
    "    'Procedure.status',\n",
    "    'Procedure.performedDateTime'\n",
    "]\n",
    "\n",
    "pack.client\n",
    "\n",
    "result=pack.getPatients(\n",
    "    ['Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc']\n",
    ").getAbsolutePaths(absolutePaths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "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>Procedure.id</th>\n",
       "      <th>Procedure.performedDateTime</th>\n",
       "      <th>Procedure.status</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f262bafb-789a-44a1-9453-c21065abc4d1</td>\n",
       "      <td>None</td>\n",
       "      <td>completed</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>e65c5378-5d1a-4978-9590-c6c8846dd6d0</td>\n",
       "      <td>None</td>\n",
       "      <td>completed</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Procedure.id Procedure.performedDateTime  \\\n",
       "0  f262bafb-789a-44a1-9453-c21065abc4d1                        None   \n",
       "1  e65c5378-5d1a-4978-9590-c6c8846dd6d0                        None   \n",
       "\n",
       "  Procedure.status  \n",
       "0        completed  \n",
       "1        completed  "
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['Procedure']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "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>Condition.id</th>\n",
       "      <th>Condition.subject.reference</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b8a1a1c4-f973-4b6b-a0ea-3d04bc4c8823</td>\n",
       "      <td>Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Condition.id  \\\n",
       "0  b8a1a1c4-f973-4b6b-a0ea-3d04bc4c8823   \n",
       "\n",
       "                    Condition.subject.reference  \n",
       "0  Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc  "
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"Condition\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "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>Encounter.id</th>\n",
       "      <th>Encounter.participant.individual.reference</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b9f19065-c8e6-40b9-b368-b09a8fb9a8d6</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>174d8c79-5517-4f80-967f-3939f943b2c8</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3f3a061a-5df2-4dc0-872c-e1e4b88955a1</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2b8cb0e7-5b1e-4769-a418-bf9e6dd86611</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>36fb900c-9ff7-4131-812c-536ab50a0a5c</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>9911ef15-1b9a-4fbd-b95d-05bd4f060269</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>f3b1e501-aac6-4480-bf35-38474563069a</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>969f9166-4f9a-4be6-9257-ed00b295a408</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>806264f4-4f45-4105-a5bc-8e9a9c49b119</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>40166ee5-0bc3-4819-96c9-e07dd26e938b</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>48f082f8-b46c-4127-a1b4-8e3c1d71a132</td>\n",
       "      <td>[Practitioner/00000171-0929-2892-0000-00000001...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            Encounter.id  \\\n",
       "0   b9f19065-c8e6-40b9-b368-b09a8fb9a8d6   \n",
       "1   174d8c79-5517-4f80-967f-3939f943b2c8   \n",
       "2   3f3a061a-5df2-4dc0-872c-e1e4b88955a1   \n",
       "3   2b8cb0e7-5b1e-4769-a418-bf9e6dd86611   \n",
       "4   36fb900c-9ff7-4131-812c-536ab50a0a5c   \n",
       "5   9911ef15-1b9a-4fbd-b95d-05bd4f060269   \n",
       "6   f3b1e501-aac6-4480-bf35-38474563069a   \n",
       "7   969f9166-4f9a-4be6-9257-ed00b295a408   \n",
       "8   806264f4-4f45-4105-a5bc-8e9a9c49b119   \n",
       "9   40166ee5-0bc3-4819-96c9-e07dd26e938b   \n",
       "10  48f082f8-b46c-4127-a1b4-8e3c1d71a132   \n",
       "\n",
       "           Encounter.participant.individual.reference  \n",
       "0   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "1   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "2   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "3   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "4   [Practitioner/00000171-0929-2892-0000-00000000...  \n",
       "5   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "6   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "7   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "8   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "9   [Practitioner/00000171-0929-2892-0000-00000001...  \n",
       "10  [Practitioner/00000171-0929-2892-0000-00000001...  "
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"Encounter\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "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>name.given</th>\n",
       "      <th>name.family</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[[Willy639]]</td>\n",
       "      <td>[Rutherford999]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[[Lilla884]]</td>\n",
       "      <td>[Mills423]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     name.given      name.family\n",
       "0  [[Willy639]]  [Rutherford999]\n",
       "1  [[Lilla884]]       [Mills423]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['Encounter'].explode('Encounter.participant.individual.reference')\\\n",
    "\t.rename(columns={\"Encounter.participant.individual.reference\":\"data\"})\\\n",
    "\t\t\t.dropna()\\\n",
    "\t\t\t\t.getResources()[:5]\\\n",
    "\t\t\t\t\t.gatherSimplePaths(['name.given','name.family'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "paths=[\n",
    "\t'valueQuantity.value',\n",
    "\t'valueQuantity.unit',\n",
    "\t'valueQuantity.system',\n",
    "\t'valueQuantity.code',\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "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>valueQuantity.value</th>\n",
       "      <th>valueQuantity.unit</th>\n",
       "      <th>valueQuantity.system</th>\n",
       "      <th>valueQuantity.code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>102.12000</td>\n",
       "      <td>U/L</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>U/L</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7.58040</td>\n",
       "      <td>g/dL</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>g/dL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6.58630</td>\n",
       "      <td>mL/min</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>mL/min</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>31.21100</td>\n",
       "      <td>pg</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>pg</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20.88000</td>\n",
       "      <td>mmol/L</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>mmol/L</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>94.10200</td>\n",
       "      <td>fL</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>fL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>32.47200</td>\n",
       "      <td>g/dL</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>g/dL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>87.16000</td>\n",
       "      <td>mg/dL</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>mg/dL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>78.92000</td>\n",
       "      <td>%</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.27443</td>\n",
       "      <td>10*3/uL</td>\n",
       "      <td>http://unitsofmeasure.org</td>\n",
       "      <td>10*3/uL</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   valueQuantity.value valueQuantity.unit       valueQuantity.system  \\\n",
       "0            102.12000                U/L  http://unitsofmeasure.org   \n",
       "1              7.58040               g/dL  http://unitsofmeasure.org   \n",
       "2              6.58630             mL/min  http://unitsofmeasure.org   \n",
       "3             31.21100                 pg  http://unitsofmeasure.org   \n",
       "4             20.88000             mmol/L  http://unitsofmeasure.org   \n",
       "5             94.10200                 fL  http://unitsofmeasure.org   \n",
       "6             32.47200               g/dL  http://unitsofmeasure.org   \n",
       "7             87.16000              mg/dL  http://unitsofmeasure.org   \n",
       "8             78.92000                  %  http://unitsofmeasure.org   \n",
       "9              0.27443            10*3/uL  http://unitsofmeasure.org   \n",
       "\n",
       "  valueQuantity.code  \n",
       "0                U/L  \n",
       "1               g/dL  \n",
       "2             mL/min  \n",
       "3                 pg  \n",
       "4             mmol/L  \n",
       "5                 fL  \n",
       "6               g/dL  \n",
       "7              mg/dL  \n",
       "8                  %  \n",
       "9            10*3/uL  "
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['1555106'])\\\n",
    "\t\t.getObservations()[:10]\\\n",
    "\t\t\t\t.gatherSimplePaths(paths)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getImagingStudies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "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>data</th>\n",
       "      <th>Condition</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1849657',...</td>\n",
       "      <td>1849657</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data Condition\n",
       "0  {'resourceType': 'Condition', 'id': '1849657',...   1849657"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conds=pack.getConditions(\n",
    "\tsearchParams={\n",
    "\t\t\"code\":\"C61\",\n",
    "\t\t# \"_content\": \"leber\"\n",
    "\t}\n",
    "\t)\n",
    "conds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "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>series.description</th>\n",
       "      <th>series.uid</th>\n",
       "      <th>identifier.value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>None</td>\n",
       "      <td>[IdType-2]</td>\n",
       "      <td>[Identifier-117]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>None</td>\n",
       "      <td>[IdType-5]</td>\n",
       "      <td>[Identifier-118]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>None</td>\n",
       "      <td>[1.2.276.0.7230010.3.1.3.8323329.10.1594989066...</td>\n",
       "      <td>[urn:oid:1.2.826.0.1.3680043.8.498.94636677472...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  series.description                                         series.uid  \\\n",
       "0               None                                         [IdType-2]   \n",
       "1               None                                         [IdType-5]   \n",
       "2               None                                               None   \n",
       "3               None  [1.2.276.0.7230010.3.1.3.8323329.10.1594989066...   \n",
       "\n",
       "                                    identifier.value  \n",
       "0                                   [Identifier-117]  \n",
       "1                                   [Identifier-118]  \n",
       "2                                               None  \n",
       "3  [urn:oid:1.2.826.0.1.3680043.8.498.94636677472...  "
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack\\\n",
    "\t.getImagingStudies(searchParams={\"endpoint:missing\":False})\\\n",
    "\t\t.gatherSimplePaths(['series.description','series.uid','identifier.value'])\n",
    "\n",
    "# use .getDICOMInstances().sendDICOMToFiles() to download DICOM files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.gatherKeys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[resourceType, id, meta, meta.versionId, meta.lastUpdated, meta.source, text, text.status, text.div, name, name.text, name.family, name.given, gender, birthDate]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                2\n",
       "[resourceType, id, meta, meta.versionId, meta.lastUpdated, meta.source, text, text.status, text.div, identifier, identifier.system, identifier.value, identifier.use, identifier.type, identifier.type.coding, identifier.type.coding.system, identifier.type.coding.code, identifier.type.coding.display, identifier.system, identifier.value, name, name.family, name.given, telecom, telecom.system, telecom.value, telecom.use, gender, birthDate, address, address.line, address.postalCode, link, link.other, link.other.reference, link.type]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             1\n",
       "[resourceType, id, meta, meta.versionId, meta.lastUpdated, meta.source, text, text.status, text.div, identifier, identifier.use, identifier.type, identifier.type.coding, identifier.type.coding.system, identifier.type.coding.code, identifier.system, identifier.value, identifier.period, identifier.period.start, identifier.assigner, identifier.assigner.display, active, name, name.use, name.family, name.given, name.use, name.given, name.use, name.family, name.given, name.period, name.period.end, telecom, telecom.use, telecom.system, telecom.value, telecom.use, telecom.rank, telecom.system, telecom.value, telecom.use, telecom.rank, telecom.system, telecom.value, telecom.use, telecom.period, telecom.period.end, gender, birthDate, _birthDate, _birthDate.extension, _birthDate.extension.url, _birthDate.extension.valueDateTime, deceasedBoolean, address, address.use, address.type, address.text, address.line, address.city, address.district, address.state, address.postalCode, address.period, address.period.start, contact, contact.relationship, contact.relationship.coding, contact.relationship.coding.system, contact.relationship.coding.code, contact.name, contact.name.family, contact.name._family, contact.name._family.extension, contact.name._family.extension.url, contact.name._family.extension.valueString, contact.name.given, contact.telecom, contact.telecom.system, contact.telecom.value, contact.address, contact.address.use, contact.address.type, contact.address.line, contact.address.city, contact.address.district, contact.address.state, contact.address.postalCode, contact.address.period, contact.address.period.start, contact.gender, contact.period, contact.period.start, managingOrganization, managingOrganization.reference]    1\n",
       "Name: data, dtype: int64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patients.gatherKeys().data.value_counts()[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "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>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[resourceType, id, meta, meta.versionId, meta....</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [resourceType, id, meta, meta.versionId, meta...."
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['e92abcdc-a300-41e0-aa0f-378daebe25dc'])\\\n",
    "        .gatherKeys(['valueString'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.valuesForKeys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "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>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[White, Not Hispanic or Latino, Nanci249 Gleic...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [White, Not Hispanic or Latino, Nanci249 Gleic..."
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['e92abcdc-a300-41e0-aa0f-378daebe25dc'])\\\n",
    "        .gatherValuesForKeys(['valueString'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.gatherReferences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "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>referencer</th>\n",
       "      <th>referencee</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'reference': 'Patient/9ac622c4-61c8-40b4-b6d9...</td>\n",
       "      <td>[Patient/b558da74-7756-4845-87d5-d1cca8b79a62]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'reference': 'Patient/b558da74-7756-4845-87d5...</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          referencer  \\\n",
       "0  {'reference': 'Patient/9ac622c4-61c8-40b4-b6d9...   \n",
       "1  {'reference': 'Patient/b558da74-7756-4845-87d5...   \n",
       "\n",
       "                                       referencee  \n",
       "0  [Patient/b558da74-7756-4845-87d5-d1cca8b79a62]  \n",
       "1                                              []  "
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.gatherReferences(\n",
    "    ['Patient/9ac622c4-61c8-40b4-b6d9-06cfeb3d6995']\n",
    "    , recursive=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.gatherText"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "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>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[laboratory, 102.12, Alkaline phosphatase [Enz...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[Protein [Mass/volume] in Serum or Plasma, lab...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[Glomerular filtration rate/1.73 sq M.predicte...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[MCH [Entitic mass] by Automated count, labora...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[20.88, Carbon dioxide, total [Moles/volume] i...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [laboratory, 102.12, Alkaline phosphatase [Enz...\n",
       "1  [Protein [Mass/volume] in Serum or Plasma, lab...\n",
       "2  [Glomerular filtration rate/1.73 sq M.predicte...\n",
       "3  [MCH [Entitic mass] by Automated count, labora...\n",
       "4  [20.88, Carbon dioxide, total [Moles/volume] i..."
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/1555106'],)\\\n",
    "        .getObservations()[:5]\\\n",
    "                .gatherText()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.gatherDates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "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>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[2020-03-18T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[2020-03-19T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[2020-03-19T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[2020-03-14T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[2020-03-19T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              data\n",
       "0  [2020-03-18T21:53:14.197-04:00]\n",
       "1  [2020-03-19T21:53:14.197-04:00]\n",
       "2  [2020-03-19T21:53:14.197-04:00]\n",
       "3  [2020-03-14T21:53:14.197-04:00]\n",
       "4  [2020-03-19T21:53:14.197-04:00]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/1555106'],)\\\n",
    "        .getObservations()[:5]\\\n",
    "                .gatherValuesForKeys(['issued'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "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>dates</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[2020-03-18T21:53:14-04:00, 2020-03-18T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[2020-03-19T21:53:14-04:00, 2020-03-19T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[2020-03-19T21:53:14-04:00, 2020-03-19T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[2020-03-14T21:53:14-04:00, 2020-03-14T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[2020-03-19T21:53:14-04:00, 2020-03-19T21:53:1...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               dates\n",
       "0  [2020-03-18T21:53:14-04:00, 2020-03-18T21:53:1...\n",
       "1  [2020-03-19T21:53:14-04:00, 2020-03-19T21:53:1...\n",
       "2  [2020-03-19T21:53:14-04:00, 2020-03-19T21:53:1...\n",
       "3  [2020-03-14T21:53:14-04:00, 2020-03-14T21:53:1...\n",
       "4  [2020-03-19T21:53:14-04:00, 2020-03-19T21:53:1..."
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/1555106'],)\\\n",
    "        .getObservations()[:5]\\\n",
    "                .gatherDates()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.validate(['Patient/1555106']).sendResourcesToFiles(['./data.ignore'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Utils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Server Profiling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>resourceType</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Account</td>\n",
       "      <td>510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ActivityDefinition</td>\n",
       "      <td>284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AdverseEvent</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AllergyIntolerance</td>\n",
       "      <td>9356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Appointment</td>\n",
       "      <td>29636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>TestReport</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>TestScript</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>ValueSet</td>\n",
       "      <td>3126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>VerificationResult</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>VisionPrescription</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>146 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           resourceType  count\n",
       "0               Account    510\n",
       "1    ActivityDefinition    284\n",
       "2          AdverseEvent    116\n",
       "3    AllergyIntolerance   9356\n",
       "4           Appointment  29636\n",
       "..                  ...    ...\n",
       "141          TestReport     11\n",
       "142          TestScript     33\n",
       "143            ValueSet   3126\n",
       "144  VerificationResult      9\n",
       "145  VisionPrescription     15\n",
       "\n",
       "[146 rows x 2 columns]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.countServerResources()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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': 'Patient', 'id': '1555106', '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'Patient', 'id': '1555106', '..."
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.validate(['Patient/1555106'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FHIR PACK Internals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### FHIRPy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### URL Parsing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('/app/FHIR/r4/Condition',\n",
       " {'code': ['C61'],\n",
       "  '_count': ['1'],\n",
       "  '_sort': ['-onset-date'],\n",
       "  'identifier': ['|'],\n",
       "  '_Pagination': ['eyJvZmZzZXQiOjIwfQ==']})"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import fhirpy.base.utils as fpu\n",
    "url=fpu.parse_pagination_url('/app/FHIR/r4/Condition?code=C61&_count=1&_sort=-onset-date&identifier=%7C&_Pagination=eyJvZmZzZXQiOjIwfQ%3D%3D')\n",
    "url"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Raw fetch_resource()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'resourceType': 'Condition',\n",
       " 'id': '1499041',\n",
       " 'meta': {'versionId': '1',\n",
       "  'lastUpdated': '2020-10-05T16:29:07.960+00:00',\n",
       "  'source': '#0sjLd8ZeFHHrRHaX'},\n",
       " 'clinicalStatus': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-clinical',\n",
       "    'code': 'active'}]},\n",
       " 'verificationStatus': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-ver-status',\n",
       "    'code': 'confirmed'}]},\n",
       " 'category': [{'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/condition-category',\n",
       "     'code': 'encounter-diagnosis',\n",
       "     'display': 'Encounter Diagnosis'}]}],\n",
       " 'code': {'coding': [{'system': 'http://snomed.info/sct',\n",
       "    'code': '22298006',\n",
       "    'display': 'Myocardial Infarction'}],\n",
       "  'text': 'Myocardial Infarction'},\n",
       " 'subject': {'reference': 'Patient/1465609'},\n",
       " 'onsetDateTime': '2020-04-01T09:52:49-04:00',\n",
       " 'recordedDate': '2020-04-01T09:52:49-04:00',\n",
       " 'asserter': {'reference': 'Practitioner/1498740'}}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.client._fetch_resource(\n",
    "\t\"Condition/1499041\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Raw client.execute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'resourceType': 'Bundle',\n",
       " 'id': 'af8b2454-29bd-4fe3-b350-733131e9383a',\n",
       " 'meta': {'lastUpdated': '2022-07-13T08:04:01.285+00:00'},\n",
       " 'type': 'searchset',\n",
       " 'link': [{'relation': 'self',\n",
       "   'url': 'https://hapi.fhir.org/baseR4/DiagnosticReport/_search'},\n",
       "  {'relation': 'next',\n",
       "   'url': 'https://hapi.fhir.org/baseR4?_getpages=af8b2454-29bd-4fe3-b350-733131e9383a&_getpagesoffset=1&_count=1&_pretty=true&_bundletype=searchset'}],\n",
       " 'entry': [{'fullUrl': 'https://hapi.fhir.org/baseR4/DiagnosticReport/6630582',\n",
       "   'resource': {'resourceType': 'DiagnosticReport',\n",
       "    'id': '6630582',\n",
       "    'meta': {'versionId': '1',\n",
       "     'lastUpdated': '2022-07-05T06:03:55.922+00:00',\n",
       "     'source': '#VSn1gQals3CWesRQ'},\n",
       "    'text': {'status': 'generated',\n",
       "     'div': '<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\"> Untitled Diagnostic Report </div><table class=\"hapiPropertyTable\"><tbody><tr><td>Status</td><td>REGISTERED</td></tr><tr><td>Issued</td><td> 15 May 2013 18:32:52 </td></tr><tr><td>Conclusion</td><td>Core lab</td></tr></tbody></table></div>'},\n",
       "    'identifier': [{'use': 'official',\n",
       "      'system': 'http://www.bmc.nl/zorgportal/identifiers/reports',\n",
       "      'value': 'dtyj'}],\n",
       "    'basedOn': [{'reference': '/fhir/NutritionOrder/srth'}],\n",
       "    'status': 'registered',\n",
       "    'category': [{'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/v2-0074',\n",
       "        'code': '未輸入'}]}],\n",
       "    'code': {'coding': [{'system': 'http://loinc.org', 'code': '未輸入'}]},\n",
       "    'subject': {'reference': '/fhir/Patient/f001'},\n",
       "    'issued': '2013-05-15T19:32:52+01:00',\n",
       "    'performer': [{'reference': '/fhir/Organization/f001'}],\n",
       "    'result': [{'reference': '/fhir/Observation/f001'}],\n",
       "    'conclusion': 'Core lab'},\n",
       "   'search': {'mode': 'match'}}]}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.client.execute('Condition',\n",
    "method='get',\n",
    "params={\n",
    "\t'code':'C61',\n",
    "\t'_sort':'-onset-date',\n",
    "\t'_count': 1,\n",
    "\t'_total':'accurate'\n",
    "}\n",
    ").total\n",
    "\n",
    "pack.client.execute(\n",
    "\t'DiagnosticReport/_search',\n",
    "\tmethod=\"post\",\n",
    "\tdata={\"_id\": \"9fa9ab4c-9e1c-46c9-9bec-ec01b3215716\"},\n",
    "\tparams={\"_count\": 1},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Customization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.custom.extraction.base"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the fhirpack.custom package is guaranteed to never be used by us\n",
    "# use it to customize fhirpack\n",
    "\n",
    "# pack.unimplementedPluginBaseExtractorMethod()\n",
    "# pack.unimplementedPluginBaseTransformerMethod()\n",
    "# pack.unimplementedPluginBaseLoaderMethod()\n",
    "\n",
    "# pack.unimplementedPluginSampleExtractorMethod()\n",
    "# pack.unimplementedPluginSampleTransformerMethod()\n",
    "# pack.unimplementedPluginSampleLoaderMethod()"
   ]
  }
 ],
 "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": 2
}
