Skip to content

Commit

Permalink
Update seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Nov 17, 2023
1 parent a6bbaf3 commit b8965ec
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 28 deletions.
155 changes: 155 additions & 0 deletions client/Prepare Seeds.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7858d66a-fb63-4aa0-a3bc-09a84af48ba4",
"metadata": {},
"source": [
"# Prepare seeds"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f061eb7c-a615-44e0-bf6b-97215386df0a",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import gzip\n",
"import shutil\n",
"import requests\n",
"import zipfile\n",
"from pathlib import Path\n",
"from fhirpy import SyncFHIRClient"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c097a082-37bd-487f-ac78-6a5b5fc5ae02",
"metadata": {},
"outputs": [],
"source": [
"SOURCE = \"http://localhost:8080\"\n",
"client = SyncFHIRClient(SOURCE)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20a511c3-5867-41ec-beff-3212c7881f44",
"metadata": {},
"outputs": [],
"source": [
"def download_and_unzip(url, headers, extract_to):\n",
" response = requests.get(url, headers=headers)\n",
" if response.status_code == 200:\n",
" with open(\"temp.zip\", \"wb\") as f:\n",
" f.write(response.content)\n",
"\n",
" with zipfile.ZipFile(\"temp.zip\", 'r') as zip_ref:\n",
" zip_ref.extractall(extract_to)\n",
" os.remove(\"temp.zip\")\n",
" else:\n",
" print(\"Failed to download the file\")\n",
" return False\n",
" return True\n",
"\n",
"def filter_files(directory, exclude_strings):\n",
" for root, dirs, files in os.walk(directory):\n",
" for file in files:\n",
" if any(s in file for s in exclude_strings):\n",
" os.remove(os.path.join(root, file))\n",
"\n",
"def create_ndjson(input_dir, output_file):\n",
" data = []\n",
" for file_name in os.listdir(input_dir):\n",
" with open(os.path.join(input_dir, file_name), 'r') as file:\n",
" data.append(json.load(file))\n",
"\n",
" with open(output_file, 'w') as f:\n",
" for entry in data:\n",
" json.dump(entry, f)\n",
" f.write('\\n')\n",
"\n",
"def gzip_file(input_file, output_file):\n",
" with open(input_file, 'rb') as f_in:\n",
" with gzip.open(output_file, 'wb') as f_out:\n",
" shutil.copyfileobj(f_in, f_out)\n",
"\n",
"def move_file(src, dest):\n",
" shutil.move(src, dest)\n",
"\n",
"def clean_up(directory):\n",
" shutil.rmtree(directory)\n",
"\n",
"def to_aidbox_format(directory):\n",
" for filename in os.listdir(directory):\n",
" if filename.endswith('.json'):\n",
" file_path = os.path.join(directory, filename)\n",
" \n",
" with open(file_path, 'r') as file:\n",
" data = json.load(file)\n",
" result = client.execute(\"/$to-format/aidbox\", method='post', data=data, params=None) \n",
" with open(file_path, 'w') as file:\n",
" json.dump(result[\"resource\"], file, indent=4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bfeee29-3754-4eac-954f-b366ac3e3d7b",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://hl7.org.au/fhir/core/0.2.2-preview/examples.json.zip\"\n",
"headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}\n",
"include_strings = [\"StructureDefinition\", \"ValueSet\", \"CapabilityStatement\"]\n",
"extract_to = \"extracted\"\n",
"ndjson_file = \"seeds.ndjson\"\n",
"gzipped_file = \"seeds.ndjson.gz\"\n",
"destination_folder = \"../zenproject/\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "754cc3c2-8568-4ffd-9b6a-d37c2537b1c4",
"metadata": {},
"outputs": [],
"source": [
"if download_and_unzip(url, headers, extract_to):\n",
" filter_files(extract_to, include_strings)\n",
" to_aidbox_format(extract_to)\n",
" create_ndjson(extract_to, ndjson_file)\n",
" gzip_file(ndjson_file, gzipped_file)\n",
" move_file(gzipped_file, Path(destination_folder) / gzipped_file)\n",
" clean_up(extract_to)\n",
" os.remove(ndjson_file)"
]
}
],
"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
}
38 changes: 10 additions & 28 deletions client/Read & Search Test Scenarios.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
"metadata": {},
"outputs": [],
"source": [
"SOURCE_BASE_URL = \"https://au-core.beda.software/fhir/\""
"LOCAL = \"http://localhost:8080/\"\n",
"AU_CORE_BEDA_SOFTWARE = \"https://au-core.beda.software/fhir/\"\n",
"HL7_AU_CONNECTATHON = \"https://hl7auconnectathon.salessbx.smiledigitalhealth.com/fhir-request\"\n",
"\n",
"SOURCE_BASE_URL = AU_CORE_BEDA_SOFTWARE"
]
},
{
Expand Down Expand Up @@ -337,28 +341,7 @@
"\n",
"**Searches you can try**\n",
"\n",
"1. Find patient records using combination of birthdate parameter '1989-05-07' and name parameter 'Ms' \n",
"2. Find patient records using combination of birthdate parameter '1939-08-2' and name parameter 'Dan' "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "552c6649-0e04-4f9e-a096-2396dce3febe",
"metadata": {},
"outputs": [],
"source": [
"patient_1_5_1 = client.resources(\"Patient\").search(birthdate='1989-05-07', name=\"Ms\").fetch_all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36ea97a5-0c58-4d90-8285-4363a4781c06",
"metadata": {},
"outputs": [],
"source": [
"pprint(patient_1_5_1[0].serialize())"
"1. Find patient records using combination of birthdate parameter '1939-08-2' and name parameter 'Dan' "
]
},
{
Expand All @@ -368,7 +351,7 @@
"metadata": {},
"outputs": [],
"source": [
"patient_1_5_2 = client.resources(\"Patient\").search(birthdate='1939-08-25', name=\"Dan\").fetch_all()"
"patient_1_5_1 = client.resources(\"Patient\").search(birthdate='1939-08-25', name=\"Dan\").fetch_all()"
]
},
{
Expand All @@ -378,7 +361,7 @@
"metadata": {},
"outputs": [],
"source": [
"pprint(patient_1_5_2[0].serialize())"
"pprint(patient_1_5_1[0].serialize())"
]
},
{
Expand Down Expand Up @@ -1133,9 +1116,8 @@
"metadata": {},
"outputs": [],
"source": [
"ident = '2338189'\n",
"date = \"01-01-2014\"\n",
"condition_2_9_1 = client.resources(\"Condition\").search(patient__Patient__identifier=ident, onset_date__ge=date).fetch_all()"
"ident = 'http://ns.electronichealth.net.au/id/hi/ihi/1.0|8003608833357361'\n",
"condition_2_9_1 = client.resources(\"Condition\").search(patient__Patient__identifier=ident).fetch_all()"
]
},
{
Expand Down
Binary file modified zenproject/seeds.ndjson.gz
Binary file not shown.

0 comments on commit b8965ec

Please sign in to comment.