Skip to content

Commit

Permalink
Add get seeds notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Nov 16, 2023
1 parent ff7a641 commit 47f241d
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions client/Get Seeds.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1b76234d-f32b-499e-a0df-f64d085e7a0a",
"metadata": {},
"source": [
"# Get seeds from FHIR server"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43e12dca-2544-4674-ad69-6789c2b6bdcc",
"metadata": {},
"outputs": [],
"source": [
"from fhirpy import SyncFHIRClient\n",
"from fhirpy.base.exceptions import OperationOutcome\n",
"from collections import defaultdict\n",
"import json\n",
"import gzip"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f79f578-4bb2-4bdc-94f6-4839ae45b51a",
"metadata": {},
"outputs": [],
"source": [
"SOURCE_BASE_URL = \"https://au-core.beda.software/fhir/\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4cee225-2e53-4956-aab6-db477524cb3f",
"metadata": {},
"outputs": [],
"source": [
"source = SyncFHIRClient(SOURCE_BASE_URL)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b983fa5e-f4d5-4b98-beaf-fbd8faa61086",
"metadata": {},
"outputs": [],
"source": [
"resources = defaultdict(list)\n",
"patients = source.resources(\"Patient\").fetch_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c29483b-11bb-433a-b1a8-191326301c6f",
"metadata": {},
"outputs": [],
"source": [
"for patient in patients:\n",
" for entry in patient.execute(\"$everything\", method=\"GET\")[\"entry\"]:\n",
" resource = entry[\"resource\"]\n",
" resources[resource[\"resourceType\"]].append(resource)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44b8144f-b3f1-4129-9942-69055db3d350",
"metadata": {},
"outputs": [],
"source": [
"with gzip.open('seeds.ndjson.gz', 'wt') as file:\n",
" for item in [item for sublist in resources.values() for item in sublist]:\n",
" json.dump(item, file)\n",
" file.write('\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d97cbcaf-60d5-4731-a68e-798553915e22",
"metadata": {},
"outputs": [],
"source": [
"len([item for sublist in resources.values() for item in sublist])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 47f241d

Please sign in to comment.