Hi Community, This article will demonstrate how to create Patient and Patient Observation Resources by using iris-fhir-client application. ![image](https://user-images.githubusercontent.com/18219467/170976217-70b63f48-981a-4b76-9f9a-282f5f86ff59.png) I recommend to read my first article about this app and watch Youtube Video before continue so let's start ## 1-Create Patient Resource below CreatePatient() function of dc.FhirClient can be use to Create Patient Resource ClassMethod CreatePatient(givenName As %String, familyName As %String, birthDate As %String,gender As %String) `function requires giveName,failyName,birthDate and gender to create Patient Resource<br> below command will create Patient` do ##class(dc.FhirClient).CreatePatient("PatientGN","PatientFN","2000-06-01","male") ![image](https://user-images.githubusercontent.com/18219467/171737063-423401ef-0d59-4ce9-ac1d-af9f5c75c9b7.png) Below is the python function in irisfhirclient.py file which will create patient
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">from</span> fhirpy <span class="hljs-keyword">import</span> SyncFHIRClient
<span class="hljs-keyword">from</span> tabulate <span class="hljs-keyword">import</span> tabulate
<span class="hljs-keyword">from</span> fhirpy.base.searchset <span class="hljs-keyword">import</span> Raw
<span class="hljs-keyword">import</span> requests
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">CreatePatient</span><span class="hljs-params">(givenName,familyName,birthDate,gender,url,api_key)</span>:</span>
headers = {<span class="hljs-string">"Content-Type"</span>:contentType,<span class="hljs-string">"x-api-key"</span>:api_key}
client = SyncFHIRClient(url = url, extra_headers=headers)
patient = client.resource(<span class="hljs-string">"Patient"</span>)
patient[<span class="hljs-string">'name'</span>] = [
{
<span class="hljs-string">'given'</span>: [givenName],
<span class="hljs-string">'family'</span>: familyName,
<span class="hljs-string">'use'</span>: <span class="hljs-string">'official'</span>
}
]
patient[<span class="hljs-string">'birthDate'</span>] = birthDate
patient[<span class="hljs-string">'gender'</span>] = gender
<span class="hljs-keyword">try</span>:
patient.save()
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
print(<span class="hljs-string">"Error while creating Patient:"</span> +str(e))
<span class="hljs-keyword">return</span>
print(<span class="hljs-string">"Patient Created Successfully"</span>)
## 2- Create Patient Observation Resource
#### Let us create Observation against our newly created Patient Resource
###### below CreateObservatoin() function of dc.FhirClient can be use to Create Patient Observatoins
ClassMethod CreateObservation(patientId As %String, loincCode As %String, ObrCategory As %String, ObrValue As %Integer, ObrUOM As %String, effectiveDate As %String)
Parametres
* patientId is the Id of Patient
* LioncCode is Lionc Code, Detail can be found here
* ObrCategory is Observation Category, Detail can be found here
* ObrValue is Observatoin Value
* ObrUOM is Observation Unit
* EffectiveDate
below command will create Patient Vital Sign Observation
do ##class(dc.FhirClient).CreateObservation("8111","8310-5","vital-signs",96.8,"degF","2022-01-22")
![image](https://user-images.githubusercontent.com/18219467/171738074-2a0dda54-6215-46b0-a3aa-6a2fcb27bb85.png)
Let's List down patient observations
do ##class(dc.FhirClient).GetPatientResources("Observation","8111")
![image](https://user-images.githubusercontent.com/18219467/172003634-3f8c9ef6-76c2-4af0-97ac-e4431750e2e1.png)
Below is the python function in irisfhirclient.py file which will create patient
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">from</span> fhirpy <span class="hljs-keyword">import</span> SyncFHIRClient
<span class="hljs-keyword">from</span> tabulate <span class="hljs-keyword">import</span> tabulate
<span class="hljs-keyword">from</span> fhirpy.base.searchset <span class="hljs-keyword">import</span> Raw
<span class="hljs-keyword">import</span> requests
<span class="hljs-comment">#Function to create Patient Observation</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">CreateObservation</span><span class="hljs-params">(patientId,loincCode,ObrCategory,ObrValue,ObrUOM,effectiveDate,url,api_key)</span>:</span>
headers = {<span class="hljs-string">"Content-Type"</span>:contentType,<span class="hljs-string">"x-api-key"</span>:api_key}
client = SyncFHIRClient(url = url, extra_headers=headers)
observation = client.resource(
<span class="hljs-string">'Observation'</span>,
status=<span class="hljs-string">'preliminary'</span>,
category=[{
<span class="hljs-string">'coding'</span>: [{
<span class="hljs-string">'system'</span>: <span class="hljs-string">'http://hl7.org/fhir/observation-category'</span>,
<span class="hljs-string">'code'</span>: ObrCategory
}]
}],
code={
<span class="hljs-string">'coding'</span>: [{
<span class="hljs-string">'system'</span>: <span class="hljs-string">'http://loinc.org'</span>,
<span class="hljs-string">'code'</span>: loincCode
}]
})
observation[<span class="hljs-string">'effectiveDateTime'</span>] = effectiveDate
observation[<span class="hljs-string">'valueQuantity'</span>] = {
<span class="hljs-string">'system'</span>: <span class="hljs-string">'http://unitsofmeasure.org'</span>,
<span class="hljs-string">'value'</span>: ObrValue,
<span class="hljs-string">'code'</span>: ObrUOM
}
<span class="hljs-comment">#find the patient</span>
patient = client.resources(<span class="hljs-string">'Patient'</span>).search(_id=patientId).first()
observation[<span class="hljs-string">'subject'</span>] = patient.to_reference()
<span class="hljs-keyword">try</span>:
observation.save()
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
print(<span class="hljs-string">"Error while creating observation :"</span>+ str(e))
<span class="hljs-keyword">return</span>
print(<span class="hljs-string">"Patient Observation Created Successfully"</span>)
That's it
If you found this app useful, consider voting for my app.
Thanks