{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# General Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pretty printing has been turned OFF\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "%pprint off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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 haven't 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": 5,
   "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": 6,
   "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": 7,
   "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": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                data                            \n",
      "0  {'resourceType': 'Patient', 'id': '1567099', '...\n",
      "                                                data                            \n",
      "0  {'resourceType': 'Patient', 'id': '1567099', '...\n"
     ]
    }
   ],
   "source": [
    "!python -m fhirpack.cli -e .env.example \"-o\" \"getPatients\" -p \"given = Chalmers\" #-v\n",
    "\n",
    "!fp -e .env.example -o \"getPatients\" -p \"given = Chalmers\" #-v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### From Custom Environment File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                             "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"active\": false,\n",
      "    \"address\": [\n",
      "        {\n",
      "            \"city\": \"[Ljava.lang.String;@6486517a\",\n",
      "            \"country\": \"lJltcNpfGu\",\n",
      "            \"district\": \"kIATSJDTIN\",\n",
      "            \"line\": [\n",
      "  \n",
      "...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r"
     ]
    }
   ],
   "source": [
    "pack = fp.PACK(envFile='.env.example')\n",
    "\n",
    "for i,e in pack.getPatients(['2918556']).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": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "SCHEMA=http\n",
      "PORT=80\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": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pack = fp.PACK()\n",
    "# pack.client.authorization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Manual Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "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': 'Patient', 'id': '2918556', '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'Patient', 'id': '2918556', '..."
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack  = fp.PACK(\"http://hapi.fhir.org/baseR4\")\n",
    "pack.getPatients(['2918556'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#testing with another less reliable FHIR server\n",
    "#\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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#run \"docker-compose up\" in your local clone of FHIRPACK first  \n",
    "\n",
    "# pack  = fp.PACK(\"http://127.0.0.1:42112/hapi-fhir-jpaserver/fhir\")\n",
    "# pack.getPatients(['1']).data.iloc[0].serialize()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introductory Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "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>status</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6570895b-5943-430b-8f7b-e5df1ffc6d83</td>\n",
       "      <td>final</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>85d08e59-3e58-4518-be8b-3ccfc15cc9fa</td>\n",
       "      <td>final</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35f7efea-6557-467e-a693-34d73a097f64</td>\n",
       "      <td>final</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>1516127</td>\n",
       "      <td>final</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>104 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      id status\n",
       "0                                   None   None\n",
       "1                                   None   None\n",
       "2   6570895b-5943-430b-8f7b-e5df1ffc6d83  final\n",
       "2   85d08e59-3e58-4518-be8b-3ccfc15cc9fa  final\n",
       "2   35f7efea-6557-467e-a693-34d73a097f64  final\n",
       "..                                   ...    ...\n",
       "37                                  None   None\n",
       "38                                  None   None\n",
       "39                                  None   None\n",
       "40                                  None   None\n",
       "41                               1516127  final\n",
       "\n",
       "[104 rows x 2 columns]"
      ]
     },
     "execution_count": 123,
     "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.getRootPatients()\\\n",
    "\t\t.explode()\\\n",
    "\t\t\t.getDiagnosticReports()\\\n",
    "\t\t\t\t.dropna()\\\n",
    "\t\t\t\t\t.gatherSimplePaths(['id','status'])\\\n",
    "\t\t\t\t\t\t.explode(['id','status'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### FHIR PACK Usage "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "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": 44,
     "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>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": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patientReference = pack.getReferences(['Patient/647487'])\n",
    "\n",
    "patientReference.data.iloc[0].to_resource().serialize()\n",
    "\n",
    "patientReference.getResources()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Resources"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reference to Resource"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1849165\n",
       "1     647487\n",
       "Name: data, dtype: object"
      ]
     },
     "execution_count": 45,
     "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": "markdown",
   "metadata": {},
   "source": [
    "### Direct Resource"
   ]
  },
  {
   "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",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2080537', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2080822', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2153287', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '619088', 'm...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2548478', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2548794', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '2737336', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1376090', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1376092', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1377059', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1377093', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '1388633', '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 data\n",
       "0   {'resourceType': 'Patient', 'id': '2080537', '...\n",
       "1   {'resourceType': 'Patient', 'id': '2080822', '...\n",
       "2   {'resourceType': 'Patient', 'id': '2153287', '...\n",
       "3   {'resourceType': 'Patient', 'id': '619088', 'm...\n",
       "4   {'resourceType': 'Patient', 'id': '2548478', '...\n",
       "5   {'resourceType': 'Patient', 'id': '2548794', '...\n",
       "6   {'resourceType': 'Patient', 'id': '2737336', '...\n",
       "7   {'resourceType': 'Patient', 'id': '1376090', '...\n",
       "8   {'resourceType': 'Patient', 'id': '1376092', '...\n",
       "9   {'resourceType': 'Patient', 'id': '1377059', '...\n",
       "10  {'resourceType': 'Patient', 'id': '1377093', '...\n",
       "11  {'resourceType': 'Patient', 'id': '1388633', '..."
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(searchParams={\"family\":\"Betterhalf\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                 \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(searchParams={\"family\":\"Betterhalf\"}).size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "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': 'Patient', 'id': '1849165', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</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': '1849165', '...\n",
       "1  {'resourceType': 'Patient', 'id': '647487', 'm..."
      ]
     },
     "execution_count": 48,
     "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": 49,
   "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": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'2016-01-20'"
      ]
     },
     "execution_count": 49,
     "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": 50,
   "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': 'Patient', 'id': '1849165', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</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': '1849165', '...\n",
       "1  {'resourceType': 'Patient', 'id': '647487', 'm..."
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multiPatientReference.getResources()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\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"
     ]
    }
   ],
   "source": [
    "multiPatientResource[:1].pretty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "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": 59,
   "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": 59,
     "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": 59,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[<SyncFHIRReference Patient/1849165>],\n",
       "       [<SyncFHIRReference Patient/647487>]], dtype=object)"
      ]
     },
     "execution_count": 59,
     "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": 60,
   "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": 60,
     "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": 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",
       "    </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": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "\t[\n",
    "\t\t'647487'\n",
    "\t]\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "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": 62,
     "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": 63,
   "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,e in pack.getPatients(['647487','1849165']).itertuples():\n",
    "    print (f\"Row {i} with Patient ID {e.id}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                             \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[<SyncFHIRResource Patient/647487>], [<SyncFHIRResource Patient/1849165>]]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getPatientsAsList=pack.getPatients(['647487','1849165']).cast('list')\n",
    "\n",
    "getPatientsAsList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['647487', '1849165']"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[e.pop().id for e in getPatientsAsList]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Patients as Operand and Extracting other Resources "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "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": 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>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[{'resourceType': 'Patient', 'id': '9ac622c4-6...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[{'resourceType': 'Patient', 'id': '1849165', ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[{'resourceType': 'Patient', 'id': '649231', '...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[{'resourceType': 'Patient', 'id': '650224', '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'Patient', 'id': '9ac622c4-6...\n",
       "1  [{'resourceType': 'Patient', 'id': '1849165', ...\n",
       "2  [{'resourceType': 'Patient', 'id': '649231', '...\n",
       "3  [{'resourceType': 'Patient', 'id': '650224', '..."
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patients.getRootPatients()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array(['9ac622c4-61c8-40b4-b6d9-06cfeb3d6995'], dtype=object)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getRootPatients(\n",
    "    [\n",
    "        'b558da74-7756-4845-87d5-d1cca8b79a62',\n",
    "    ],\n",
    ").explode().gatherSimplePaths([\"id\"]).id.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "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': 'Patient', 'id': '9ac622c4-6...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'Patient', 'id': '9ac622c4-6..."
      ]
     },
     "execution_count": 69,
     "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": 70,
   "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>9ac622c4-61c8-40b4-b6d9-06cfeb3d6995</td>\n",
       "      <td>1981-01-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1849165</td>\n",
       "      <td>1974-12-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>649231</td>\n",
       "      <td>2018-12-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>650224</td>\n",
       "      <td>2007-02-08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     id   birthDate\n",
       "0  9ac622c4-61c8-40b4-b6d9-06cfeb3d6995  1981-01-01\n",
       "1                               1849165  1974-12-25\n",
       "2                                649231  2018-12-21\n",
       "3                                650224  2007-02-08"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rootPatients = patients.getResources().getRootPatients().explode()\n",
    "\n",
    "rootPatients.gatherSimplePaths(['id','birthDate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'family': 'TestFamily', 'given': ['TestGiven']}]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rootPatients.data[0].name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "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": 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>data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[{'resourceType': 'Patient', 'id': 'b558da74-7...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'Patient', 'id': 'b558da74-7..."
      ]
     },
     "execution_count": 73,
     "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": 74,
   "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': 'Condition', 'id': '1584692',...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1591180',...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1402386',...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1403562',...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '1495528',...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'Condition', 'id': '1584692',...\n",
       "1  {'resourceType': 'Condition', 'id': '1591180',...\n",
       "2  {'resourceType': 'Condition', 'id': '1402386',...\n",
       "3  {'resourceType': 'Condition', 'id': '1403562',...\n",
       "4  {'resourceType': 'Condition', 'id': '1495528',..."
      ]
     },
     "execution_count": 74,
     "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": 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>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>1583909</td>\n",
       "      <td>[[Shayne60]]</td>\n",
       "      <td>[Miller503]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1590885</td>\n",
       "      <td>[[Ty725]]</td>\n",
       "      <td>[Sporer811]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1402076</td>\n",
       "      <td>[[Phyliss]]</td>\n",
       "      <td>[Nyambura]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1403421</td>\n",
       "      <td>[[Stefania]]</td>\n",
       "      <td>[Bethwell]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1495412</td>\n",
       "      <td>[[Hallie315], [Hallie315]]</td>\n",
       "      <td>[Donnelly343, Osinski784]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        id                  name.given                name.family\n",
       "0  1583909                [[Shayne60]]                [Miller503]\n",
       "1  1590885                   [[Ty725]]                [Sporer811]\n",
       "2  1402076                 [[Phyliss]]                 [Nyambura]\n",
       "3  1403421                [[Stefania]]                 [Bethwell]\n",
       "4  1495412  [[Hallie315], [Hallie315]]  [Donnelly343, Osinski784]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conditions.getPatients().explode().gatherSimplePaths(['id','name.given','name.family'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                \r"
     ]
    }
   ],
   "source": [
    "conditions\\\n",
    "\t.getPatients()\\\n",
    "\t\t.explode()\\\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": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ",data\n",
      "0,1583909\n",
      "1,1590885\n",
      "2,1402076\n",
      "3,1403421\n",
      "4,1495412\n"
     ]
    }
   ],
   "source": [
    "!cat data.ignore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 1)"
      ]
     },
     "execution_count": 78,
     "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>1583909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1590885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1402076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1403421</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1495412</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      data\n",
       "0  1583909\n",
       "1  1590885\n",
       "2  1402076\n",
       "3  1403421\n",
       "4  1495412"
      ]
     },
     "execution_count": 78,
     "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": 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': 'Condition', 'id': '2b7a4d69-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '0fc73e7c-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': 'cb1bab6c-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': 'bc301bbf-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'Condition', 'id': '5db4866a-...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'Condition', 'id': '2b7a4d69-...\n",
       "0  {'resourceType': 'Condition', 'id': '0fc73e7c-...\n",
       "0  {'resourceType': 'Condition', 'id': 'cb1bab6c-...\n",
       "0  {'resourceType': 'Condition', 'id': 'bc301bbf-...\n",
       "0  {'resourceType': 'Condition', 'id': '5db4866a-..."
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['8cbf1128-3644-47a1-9cc8-05f1aac6071d']).getConditions().explode()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getEpisodesOfCare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "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': '9ac622c4-61...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>{'resourceType': 'Patient', 'id': '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', '..."
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rootPatients[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "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': 'EpisodeOfCare', 'id': 'P052...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'EpisodeOfCare', 'id': 'P052..."
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['P0522-patientBSJ1']).getEpisodesOfCare()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getFamilyMemberHistories"
   ]
  },
  {
   "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>[{'resourceType': 'FamilyMemberHistory', 'id':...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'FamilyMemberHistory', 'id':..."
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(['Patient/2866670']).getFamilyMemberHistories()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhipack.extraction.getMedicationAdministrations"
   ]
  },
  {
   "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>[{'resourceType': 'MedicationAdministration', ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'MedicationAdministration', ..."
      ]
     },
     "execution_count": 83,
     "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": 84,
   "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': 'MedicationRequest', 'id': '2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'MedicationRequest', 'id': '2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'MedicationRequest', 'id': '2...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'MedicationRequest', 'id': '2...\n",
       "0  {'resourceType': 'MedicationRequest', 'id': '2...\n",
       "0  {'resourceType': 'MedicationRequest', 'id': '2..."
      ]
     },
     "execution_count": 84,
     "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": 85,
   "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': 'Patient', 'id': '258974', '...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  [{'resourceType': 'Patient', 'id': '258974', '..."
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack\\\n",
    "\t.getPatients(['Patient/258974'])\\\n",
    "\t\t.getConditions(searchParams={\"code\":\"44465007\"})\\\n",
    "\t\t\t.explode()\\\n",
    "\t\t\t\t.getPatients(searchParams={\"family\":\"Keebler762\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "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": 86,
     "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",
    "    )\\\n",
    "        .gatherSimplePaths(['id','issued','code.coding.code'])\\\n",
    "            .explode(['id','issued','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": 87,
   "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': 'DiagnosticReport', 'id': '12...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '5a...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '2d...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '01...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '47...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'b1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '58...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '87...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': '8c...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'ac...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'bc...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>{'resourceType': 'DiagnosticReport', 'id': 'bf...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '12...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '5a...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '2d...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '01...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '47...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': 'b1...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '58...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '87...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': '8c...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': 'ac...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': 'bc...\n",
       "0  {'resourceType': 'DiagnosticReport', 'id': 'bf..."
      ]
     },
     "execution_count": 87,
     "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",
    "\t\t\t.explode()\n",
    "\n",
    "diagnosticReports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "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": 88,
     "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": 89,
   "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, 126df18...</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, 126df18...   \n",
       "\n",
       "                                   subject.reference presentedForm.url  \n",
       "0  [Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc,...              None  "
      ]
     },
     "execution_count": 89,
     "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": 93,
   "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, 126df18...</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, 126df18...   \n",
       "\n",
       "                                   subject.reference presentedForm.url  \n",
       "0  [Patient/e92abcdc-a300-41e0-aa0f-378daebe25dc,...              None  "
      ]
     },
     "execution_count": 93,
     "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": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<SyncFHIRClient http://hapi.fhir.org/baseR4>"
      ]
     },
     "execution_count": 98,
     "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": 99,
   "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": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['Procedure']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "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": 101,
   "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>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  valueQuantity.value valueQuantity.unit valueQuantity.system  \\\n",
       "0                None               None                 None   \n",
       "\n",
       "  valueQuantity.code  \n",
       "0               None  "
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.\\\n",
    "\tgetPatients(['e92abcdc-a300-41e0-aa0f-378daebe25dc'])\\\n",
    "\t\t.getObservations()\\\n",
    "\t\t\t.explode()\\\n",
    "\t\t\t\t.gatherSimplePaths(paths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "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": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"Condition\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "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": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"Encounter\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "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": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[\"Procedure\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "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>[[Willy639]]</td>\n",
       "      <td>[Rutherford999]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[[Willy639]]</td>\n",
       "      <td>[Rutherford999]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[[Willy639]]</td>\n",
       "      <td>[Rutherford999]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</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  [[Willy639]]  [Rutherford999]\n",
       "2  [[Willy639]]  [Rutherford999]\n",
       "3  [[Willy639]]  [Rutherford999]\n",
       "4  [[Lilla884]]       [Mills423]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result['Encounter'].explode(\n",
    "\t'Encounter.participant.individual.reference'\n",
    ")\\\n",
    "\t.rename(\n",
    "\t\tcolumns={\"Encounter.participant.individual.reference\":\"data\"}\n",
    "\t\t)\\\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": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.extraction.getImagingStudies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "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': 'Condition', 'id': '1849657',...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                data\n",
       "0  {'resourceType': 'Condition', 'id': '1849657',..."
      ]
     },
     "execution_count": 106,
     "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": 107,
   "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": 107,
     "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": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name.given        6\n",
       "name.family       5\n",
       "telecom.use       5\n",
       "resourceType      4\n",
       "id                4\n",
       "telecom.system    4\n",
       "gender            4\n",
       "name              4\n",
       "birthDate         4\n",
       "telecom.value     4\n",
       "Name: data, dtype: int64"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "patients.gatherKeys().data.explode().value_counts()[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "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</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>id</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>meta</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>meta.versionId</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>meta.lastUpdated</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>communication.language.coding</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>communication.language.coding.system</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>communication.language.coding.code</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>communication.language.coding.display</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>communication.language.text</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>118 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     data\n",
       "0                            resourceType\n",
       "0                                      id\n",
       "0                                    meta\n",
       "0                          meta.versionId\n",
       "0                        meta.lastUpdated\n",
       "..                                    ...\n",
       "0           communication.language.coding\n",
       "0    communication.language.coding.system\n",
       "0      communication.language.coding.code\n",
       "0   communication.language.coding.display\n",
       "0             communication.language.text\n",
       "\n",
       "[118 rows x 1 columns]"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "    ['e92abcdc-a300-41e0-aa0f-378daebe25dc']\n",
    "    )\\\n",
    "        .gatherKeys(['valueString'])\\\n",
    "            .explode()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.valuesForKeys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "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</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Not Hispanic or Latino</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Nanci249 Gleichner915</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     data\n",
       "0                   White\n",
       "0  Not Hispanic or Latino\n",
       "0   Nanci249 Gleichner915"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "    ['e92abcdc-a300-41e0-aa0f-378daebe25dc'],\n",
    "    # includeLinkedPatients=True\n",
    "    )\\\n",
    "        .gatherValuesForKeys(['valueString'])\\\n",
    "            .explode()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.gatherReferences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "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": 111,
     "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": 113,
   "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, Alkaline phosphatase [Enzymatic a...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[laboratory, Protein [Mass/volume] in Serum or...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[laboratory, Glomerular filtration rate/1.73 s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[laboratory, 31.211, MCH [Entitic mass] by Aut...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[laboratory, Carbon dioxide, total [Moles/volu...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>672</th>\n",
       "      <td>[2.7168, laboratory, Basophils/100 leukocytes ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>673</th>\n",
       "      <td>[Monocytes [#/volume] in Blood by Automated co...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>674</th>\n",
       "      <td>[1.1423, laboratory, Lymphocytes [#/volume] in...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>675</th>\n",
       "      <td>[Platelets [#/volume] in Blood by Automated co...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>676</th>\n",
       "      <td>[laboratory, 12.922, Erythrocyte distribution ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>677 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  data\n",
       "0    [laboratory, Alkaline phosphatase [Enzymatic a...\n",
       "1    [laboratory, Protein [Mass/volume] in Serum or...\n",
       "2    [laboratory, Glomerular filtration rate/1.73 s...\n",
       "3    [laboratory, 31.211, MCH [Entitic mass] by Aut...\n",
       "4    [laboratory, Carbon dioxide, total [Moles/volu...\n",
       "..                                                 ...\n",
       "672  [2.7168, laboratory, Basophils/100 leukocytes ...\n",
       "673  [Monocytes [#/volume] in Blood by Automated co...\n",
       "674  [1.1423, laboratory, Lymphocytes [#/volume] in...\n",
       "675  [Platelets [#/volume] in Blood by Automated co...\n",
       "676  [laboratory, 12.922, Erythrocyte distribution ...\n",
       "\n",
       "[677 rows x 1 columns]"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "    ['Patient/1555106'],\n",
    "    )\\\n",
    "        .getObservations()[:5]\\\n",
    "            .explode()\\\n",
    "                .gatherText()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### fhirpack.transformation.base.gatherDates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "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",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>672</th>\n",
       "      <td>[2020-03-20T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>673</th>\n",
       "      <td>[2020-03-20T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>674</th>\n",
       "      <td>[2020-03-20T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>675</th>\n",
       "      <td>[2020-03-20T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>676</th>\n",
       "      <td>[2020-03-20T21:53:14.197-04:00]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>677 rows × 1 columns</p>\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]\n",
       "..                               ...\n",
       "672  [2020-03-20T21:53:14.197-04:00]\n",
       "673  [2020-03-20T21:53:14.197-04:00]\n",
       "674  [2020-03-20T21:53:14.197-04:00]\n",
       "675  [2020-03-20T21:53:14.197-04:00]\n",
       "676  [2020-03-20T21:53:14.197-04:00]\n",
       "\n",
       "[677 rows x 1 columns]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "    ['Patient/1555106'],\n",
    "    )\\\n",
    "        .getObservations()[:5]\\\n",
    "            .explode()\\\n",
    "                .gatherValuesForKeys(['issued'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "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",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>672</th>\n",
       "      <td>[2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>673</th>\n",
       "      <td>[2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>674</th>\n",
       "      <td>[2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>675</th>\n",
       "      <td>[2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>676</th>\n",
       "      <td>[2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>677 rows × 1 columns</p>\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...\n",
       "..                                                 ...\n",
       "672  [2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...\n",
       "673  [2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...\n",
       "674  [2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...\n",
       "675  [2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...\n",
       "676  [2020-03-20T21:53:14-04:00, 2020-03-20T21:53:1...\n",
       "\n",
       "[677 rows x 1 columns]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.getPatients(\n",
    "    ['Patient/1555106'],\n",
    "    )\\\n",
    "        .getObservations()[:5]\\\n",
    "            .explode()\\\n",
    "                .gatherDates()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.validate(\n",
    "    ['Patient/1555106']\n",
    "    )\\\n",
    "        .sendResourcesToFiles(['./data.ignore'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Utils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Server Profiling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "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": 119,
   "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": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack.validate(\n",
    "    ['Patient/1555106']\n",
    ")\n"
   ]
  },
  {
   "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": 120,
   "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": 120,
     "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": 121,
   "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": 121,
     "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": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "{'resourceType': 'Bundle',\n",
       " 'id': 'ba7c0ad0-cb78-4909-8564-13fe58f5fec1',\n",
       " 'meta': {'lastUpdated': '2022-06-23T08:25:02.930+00:00'},\n",
       " 'type': 'searchset',\n",
       " 'link': [{'relation': 'self',\n",
       "   'url': 'http://hapi.fhir.org/baseR4/DiagnosticReport/_search'},\n",
       "  {'relation': 'next',\n",
       "   'url': 'http://hapi.fhir.org/baseR4?_getpages=ba7c0ad0-cb78-4909-8564-13fe58f5fec1&_getpagesoffset=1&_count=1&_pretty=true&_bundletype=searchset'}],\n",
       " 'entry': [{'fullUrl': 'http://hapi.fhir.org/baseR4/DiagnosticReport/3090108',\n",
       "   'resource': {'resourceType': 'DiagnosticReport',\n",
       "    'id': '3090108',\n",
       "    'meta': {'versionId': '4',\n",
       "     'lastUpdated': '2022-06-23T06:53:13.631+00:00',\n",
       "     'source': 'http://hl7.org/fhir/StructureDefinition/uri#uJONmcfcpMBdbD2s',\n",
       "     'profile': ['https://www.medizininformatik-initiative.de/fhir/core/modul-labor/StructureDefinition/DiagnosticReportLab']},\n",
       "    'text': {'status': 'generated',\n",
       "     'div': '<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\"> EKGR </div><table class=\"hapiPropertyTable\"><tbody><tr><td>Status</td><td>FINAL</td></tr></tbody></table></div>'},\n",
       "    'identifier': [{'type': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/v2-0203',\n",
       "         'code': 'FILL'}]},\n",
       "      'system': 'http://ukgm.de/fhir/NameSpace/akedis/DiagnosticReport',\n",
       "      'value': 'SEMA_250351055836748624'}],\n",
       "    'status': 'final',\n",
       "    'category': [{'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/v2-0074',\n",
       "        'code': 'EC'}]}],\n",
       "    'code': {'coding': [{'system': 'https://loinc.org/search/?t=1&s=11524-6+',\n",
       "       'code': '11524-6',\n",
       "       'display': 'EKGR'}]},\n",
       "    'subject': {'reference': 'Patient/3909181'},\n",
       "    'result': [{'reference': 'Observation/3909182'},\n",
       "     {'reference': 'Observation/3909183'},\n",
       "     {'reference': 'Observation/3909184'},\n",
       "     {'reference': 'Observation/3909185'},\n",
       "     {'reference': 'Observation/3909186'},\n",
       "     {'reference': 'Observation/3909187'},\n",
       "     {'reference': 'Observation/3909188'},\n",
       "     {'reference': 'Observation/3909189'},\n",
       "     {'reference': 'Observation/3909190'}]},\n",
       "   'search': {'mode': 'match'}}]}"
      ]
     },
     "execution_count": 133,
     "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
}
