{ "cells": [ { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/Users/ehaas/Documents/FHIR/davinci-ecdx/CDEX-Signatures/in_files/test_bundle.json')" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from json import loads, dumps, dump\n", "from pathlib import Path\n", "from jcs import canonicalize\n", "from yaml import dump as dumpy\n", "from IPython.display import display, Markdown\n", "\n", "out_file = 'canonical-fhir-document.json'\n", "in_file = '/Users/ehaas/Documents/FHIR/davinci-ecdx/CDEX-Signatures/in_files/test_bundle.json'\n", "path = Path() / in_file\n", "path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get Document" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'resourceType': 'Bundle',\n", " 'id': '12521',\n", " 'meta': {'versionId': '1', 'lastUpdated': '2022-02-15T15:53:19.296Z'},\n", " 'identifier': {'system': 'urn:ietf:rfc:3986',\n", " 'value': 'urn:uuid:0c3151bd-1cbf-4d64-b04d-cd9187a4c6e0'},\n", " 'type': 'document',\n", " 'timestamp': '2013-05-28T22:12:21Z',\n", " 'entry': [{'fullUrl': 'urn:uuid:541a72a8-df75-4484-ac89-ac4923f03b81',\n", " 'resource': {'resourceType': 'Observation',\n", " 'referenceRange': [{'text': 'this is my markdown file line 1 \\nstill line 1 \\nline2\\n\\nline3\\n- bullet 1 \\n- bullet 1.\\n\\n code \"block\"\\n'}],\n", " 'meta': {'lastUpdated': '2013-05-05T16:13:03Z'},\n", " 'text': {'status': 'additional',\n", " 'div': '
Acute Asthmatic attack. Was wheezing\\n\\n\\n\\n for days prior to admission.
'},\n", " 'status': 'final',\n", " 'code': {'coding': [{'system': 'http://loinc.org', 'code': '46241-6'}],\n", " 'text': 'Reason for admission'},\n", " 'subject': {'reference': 'urn:uuid:d546d85-2487-4ae5-a7b2-f7fa79811120',\n", " 'display': 'Eve Everywoman'},\n", " 'encounter': {'reference': 'urn:uuid:a16719d5-5267-4f69-913a-f29e4b800a14'}}}]}" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict = loads(path.read_text())\n", "my_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pop sig, id, meta if there" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'resourceType': 'Bundle',\n", " 'identifier': {'system': 'urn:ietf:rfc:3986',\n", " 'value': 'urn:uuid:0c3151bd-1cbf-4d64-b04d-cd9187a4c6e0'},\n", " 'type': 'document',\n", " 'timestamp': '2013-05-28T22:12:21Z',\n", " 'entry': [{'fullUrl': 'urn:uuid:541a72a8-df75-4484-ac89-ac4923f03b81',\n", " 'resource': {'resourceType': 'Observation',\n", " 'referenceRange': [{'text': 'this is my markdown file line 1 \\nstill line 1 \\nline2\\n\\nline3\\n- bullet 1 \\n- bullet 1.\\n\\n code \"block\"\\n'}],\n", " 'meta': {'lastUpdated': '2013-05-05T16:13:03Z'},\n", " 'text': {'status': 'additional',\n", " 'div': '
Acute Asthmatic attack. Was wheezing\\n\\n\\n\\n for days prior to admission.
'},\n", " 'status': 'final',\n", " 'code': {'coding': [{'system': 'http://loinc.org', 'code': '46241-6'}],\n", " 'text': 'Reason for admission'},\n", " 'subject': {'reference': 'urn:uuid:d546d85-2487-4ae5-a7b2-f7fa79811120',\n", " 'display': 'Eve Everywoman'},\n", " 'encounter': {'reference': 'urn:uuid:a16719d5-5267-4f69-913a-f29e4b800a14'}}}]}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "try:\n", " my_dict.pop(\"signature\")\n", "except KeyError:\n", " pass\n", "\n", "# http://hl7.org/fhir/canonicalization/json keep all\n", "# for http://hl7.org/fhir/canonicalization/json#static pop id meta and text elements first\n", "# my_dict.pop(\"text\") recursively\n", "\n", "# for http://hl7.org/fhir/canonicalization/json#document pop Bundle.id Bundle.meta elements first\n", "try:\n", " my_dict.pop(\"id\")\n", "except KeyError:\n", " pass\n", "try:\n", " my_dict.pop(\"meta\")\n", "except KeyError:\n", " pass\n", "\n", "my_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sort and remove white space" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "this is my markdown file line 1 \n", "still line 1 \n", "line2\n", "\n", "line3\n", "- bullet 1 \n", "- bullet 1.\n", "\n", " code \"block\"\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(Markdown(my_dict['entry'][0]['resource']['referenceRange'][0]['text']))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'{\"entry\":[{\"fullUrl\":\"urn:uuid:541a72a8-df75-4484-ac89-ac4923f03b81\",\"resource\":{\"code\":{\"coding\":[{\"code\":\"46241-6\",\"system\":\"http://loinc.org\"}],\"text\":\"Reason for admission\"},\"encounter\":{\"reference\":\"urn:uuid:a16719d5-5267-4f69-913a-f29e4b800a14\"},\"meta\":{\"lastUpdated\":\"2013-05-05T16:13:03Z\"},\"referenceRange\":[{\"text\":\"this is my markdown file line 1 \\\\nstill line 1 \\\\nline2\\\\n\\\\nline3\\\\n- bullet 1 \\\\n- bullet 1.\\\\n\\\\n code \\\\\"block\\\\\"\\\\n\"}],\"resourceType\":\"Observation\",\"status\":\"final\",\"subject\":{\"display\":\"Eve Everywoman\",\"reference\":\"urn:uuid:d546d85-2487-4ae5-a7b2-f7fa79811120\"},\"text\":{\"div\":\"
Acute Asthmatic attack. Was wheezing\\\\n\\\\n\\\\n\\\\n for days prior to admission.
\",\"status\":\"additional\"}}}],\"identifier\":{\"system\":\"urn:ietf:rfc:3986\",\"value\":\"urn:uuid:0c3151bd-1cbf-4d64-b04d-cd9187a4c6e0\"},\"resourceType\":\"Bundle\",\"timestamp\":\"2013-05-28T22:12:21Z\",\"type\":\"document\"}'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "canon_json = canonicalize(my_dict)\n", "canon_json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Write to file" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "955" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "out_path = Path() / out_file\n", "out_path.write_bytes(canon_json)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'entry': [{'fullUrl': 'urn:uuid:541a72a8-df75-4484-ac89-ac4923f03b81',\n", " 'resource': {'code': {'coding': [{'code': '46241-6',\n", " 'system': 'http://loinc.org'}],\n", " 'text': 'Reason for admission'},\n", " 'encounter': {'reference': 'urn:uuid:a16719d5-5267-4f69-913a-f29e4b800a14'},\n", " 'meta': {'lastUpdated': '2013-05-05T16:13:03Z'},\n", " 'referenceRange': [{'text': 'this is my markdown file line 1 \\nstill line 1 \\nline2\\n\\nline3\\n- bullet 1 \\n- bullet 1.\\n\\n code \"block\"\\n'}],\n", " 'resourceType': 'Observation',\n", " 'status': 'final',\n", " 'subject': {'display': 'Eve Everywoman',\n", " 'reference': 'urn:uuid:d546d85-2487-4ae5-a7b2-f7fa79811120'},\n", " 'text': {'div': '
Acute Asthmatic attack. Was wheezing\\n\\n\\n\\n for days prior to admission.
',\n", " 'status': 'additional'}}}],\n", " 'identifier': {'system': 'urn:ietf:rfc:3986',\n", " 'value': 'urn:uuid:0c3151bd-1cbf-4d64-b04d-cd9187a4c6e0'},\n", " 'resourceType': 'Bundle',\n", " 'timestamp': '2013-05-28T22:12:21Z',\n", " 'type': 'document'}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json_string = canon_json.decode('utf-8')\n", "data = loads(json_string)\n", "data\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"
Acute Asthmatic attack. Was wheezing\\n\\n\\n\\n for days prior to admission.
\"\n" ] } ], "source": [ "print(dumps(data['entry'][0]['resource']['text']['div'],indent=2))\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "this is my markdown file line 1 \n", "still line 1 \n", "line2\n", "\n", "line3\n", "- bullet 1 \n", "- bullet 1.\n", "\n", " code \"block\"\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(Markdown(data['entry'][0]['resource']['referenceRange'][0]['text']))" ] } ], "metadata": { "kernelspec": { "display_name": "fhir_builds", "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.7.12" } }, "nbformat": 4, "nbformat_minor": 4 }