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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
st.markdown("""
|
3 |
|
4 |
|
|
|
1 |
import streamlit as st
|
2 |
+
|
3 |
+
# pip install streamlit fhir.resources==6.5.0 fhirclient smart.models sdcclient
|
4 |
+
|
5 |
+
# FHIR Resources:
|
6 |
+
from fhir.resources.observation import Observation
|
7 |
+
from fhir.resources.questionnaire import Questionnaire
|
8 |
+
from fhir.resources.questionnaireresponse import QuestionnaireResponse
|
9 |
+
from fhir.resources.referralrequest import ReferralRequest
|
10 |
+
from fhir.resources.careteam import CareTeam
|
11 |
+
from fhir.resources.practitioner import Practitioner
|
12 |
+
from fhir.resources.practitionerrole import PractitionerRole
|
13 |
+
from fhir.resources.program import Program
|
14 |
+
from fhir.resources.flag import Flag
|
15 |
+
from fhir.resources.documentreference import DocumentReference
|
16 |
+
from fhir.resources.immunization import Immunization
|
17 |
+
from fhir.resources.medication import Medication
|
18 |
+
from fhir.resources.medicationstatement import MedicationStatement
|
19 |
+
from fhir.resources.patient import Patient
|
20 |
+
from fhir.resources.organization import Organization
|
21 |
+
from fhir.resources.procedure import Procedure
|
22 |
+
from fhir.resources.condition import Condition
|
23 |
+
from fhir.resources.diagnosticreport import DiagnosticReport
|
24 |
+
from fhir.resources.riskassessment import RiskAssessment
|
25 |
+
from fhir.resources.riskprobability import RiskProbability
|
26 |
+
from fhir.resources.riskevidencesynthesis import RiskEvidenceSynthesis
|
27 |
+
from fhir.resources.questionnaireresponse import QuestionnaireResponse
|
28 |
+
from fhir.resources.careplan import CarePlan
|
29 |
+
from fhir.resources.goal import Goal
|
30 |
+
from fhir.resources.task import Task
|
31 |
+
from fhir.resources.activitydefinition import ActivityDefinition
|
32 |
+
from fhir.resources.library import Library
|
33 |
+
from fhir.resources.implementationguide import ImplementationGuide
|
34 |
+
from fhir.resources.structuredefinition import StructureDefinition
|
35 |
+
from fhir.resources.codeableconcept import CodeableConcept
|
36 |
+
from fhir.resources.coding import Coding
|
37 |
+
from fhir.resources.fhirdate import FHIRDate
|
38 |
+
from fhir.resources.humanname import HumanName
|
39 |
+
from fhir.resources.identifier import Identifier
|
40 |
+
from fhir.resources.contactpoint import ContactPoint
|
41 |
+
from fhir.resources.address import Address
|
42 |
+
from fhir.resources.period import Period
|
43 |
+
from fhir.resources.ratio import Ratio
|
44 |
+
from fhir.resources.range import Range
|
45 |
+
from fhir.resources.quantity import Quantity
|
46 |
+
from fhir.resources.simplequantity import SimpleQuantity
|
47 |
+
from fhir.resources.timing import Timing
|
48 |
+
from fhir.resources.timingrepeat import TimingRepeat
|
49 |
+
from fhir.resources.dosage import Dosage
|
50 |
+
from fhir.resources.administration import Administration
|
51 |
+
from fhir.resources.anatomicallocation import AnatomicalLocation
|
52 |
+
from fhir.resources.age import Age
|
53 |
+
from fhir.resources.distance import Distance
|
54 |
+
from fhir.resources.duration import Duration
|
55 |
+
from fhir.resources.money import Money
|
56 |
+
from fhir.resources.sampleddata import SampledData
|
57 |
+
from fhir.resources.signature import Signature
|
58 |
+
from fhir.resources.meta import Meta
|
59 |
+
from fhir.resources.identifier import Identifier
|
60 |
+
from fhir.resources.resource import Resource
|
61 |
+
from fhir.resources.fhirabstractmodel import FHIRAbstractModel
|
62 |
+
from fhir.resources.fhirabstractresource import FHIRAbstractResource
|
63 |
+
|
64 |
+
from fhirclient.models.observation import Observation as SmartObservation
|
65 |
+
from fhirclient.models.questionnaire import Questionnaire as SmartQuestion
|
66 |
+
from fhirclient.models.questionnaireresponse import QuestionnaireResponse as SmartQuestionnaireResponse
|
67 |
+
from fhirclient.models.referralrequest import ReferralRequest as SmartReferralRequest
|
68 |
+
from fhirclient.models.careteam import CareTeam as SmartCareTeam
|
69 |
+
from fhirclient.models.practitioner import Practitioner as SmartPractitioner
|
70 |
+
from fhirclient.models.practitionerrole import PractitionerRole as SmartPractitionerRole
|
71 |
+
from fhirclient.models.program import Program as SmartProgram
|
72 |
+
from fhirclient.models.flag import Flag as SmartFlag
|
73 |
+
from fhirclient.models.documentreference import DocumentReference as SmartDocumentReference
|
74 |
+
from fhirclient.models.immunization import Immunization as SmartImmunization
|
75 |
+
from fhirclient.models.medication import Medication as SmartMedication
|
76 |
+
from fhirclient.models.medicationstatement import MedicationStatement as SmartMedicationStatement
|
77 |
+
from fhirclient.models.patient import Patient as SmartPatient
|
78 |
+
from fhirclient.models.organization import Organization as SmartOrganization
|
79 |
+
from fhirclient.models.procedure import Procedure as SmartProcedure
|
80 |
+
from fhirclient.models.condition import Condition as SmartCondition
|
81 |
+
from fhirclient.models.diagnosticreport import DiagnosticReport as SmartDiagnosticReport
|
82 |
+
from fhirclient.models.riskassessment import RiskAssessment as SmartRiskAssessment
|
83 |
+
from fhirclient.models.riskprobability import RiskProbability as SmartRiskProbability
|
84 |
+
from fhirclient.models.riskevidencesynthesis import RiskEvidenceSynthesis as SmartRiskEvidenceSynthesis
|
85 |
+
from fhirclient.models.questionnaireresponse import QuestionnaireResponse as SmartQuestionnaireResponse
|
86 |
+
from fhirclient.models.careplan import CarePlan as SmartCarePlan
|
87 |
+
from fhirclient.models.goal import Goal as SmartGoal
|
88 |
+
from fhirclient.models.task import Task as SmartTask
|
89 |
+
from fhirclient.models.activitydefinition import ActivityDefinition as SmartActivityDefinition
|
90 |
+
from fhirclient.models.library import Library as SmartLibrary
|
91 |
+
from fhirclient.models.implementationguide import ImplementationGuide as SmartImplementationGuide
|
92 |
+
from fhirclient.models.structuredefinition import StructureDefinition as SmartStructureDefinition
|
93 |
+
|
94 |
+
import json
|
95 |
+
from typing import List, Tuple
|
96 |
+
|
97 |
+
import smart.models as smart
|
98 |
+
from sdcclient import SdcClient
|
99 |
+
|
100 |
+
# Initialize the SMART client
|
101 |
+
smart_client = smart.client
|
102 |
+
|
103 |
+
# Initialize the SDC client
|
104 |
+
sdc_client = SdcClient()
|
105 |
+
|
106 |
+
# Define a list of example patients
|
107 |
+
EXAMPLE_PATIENTS = [
|
108 |
+
{
|
109 |
+
"name": "Alice Smith",
|
110 |
+
"birthdate": "1980-01-01",
|
111 |
+
"gender": "female",
|
112 |
+
"address": {
|
113 |
+
"line": ["123 Main St"],
|
114 |
+
"city": "Anytown",
|
115 |
+
"state": "NY",
|
116 |
+
"postalCode": "12345",
|
117 |
+
"country": "US"
|
118 |
+
},
|
119 |
+
"phone": "555-555-1212"
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"name": "Bob Johnson",
|
123 |
+
"birthdate": "1975-05-05",
|
124 |
+
"gender": "male",
|
125 |
+
"address": {
|
126 |
+
"line": ["456 Oak St"],
|
127 |
+
"city": "Anytown",
|
128 |
+
"state": "NY",
|
129 |
+
"postalCode": "12345",
|
130 |
+
"country": "US"
|
131 |
+
},
|
132 |
+
"phone": "555-555-1212"
|
133 |
+
}
|
134 |
+
]
|
135 |
+
|
136 |
+
# Define a list of example social determinants of health
|
137 |
+
EXAMPLE_SDH = [
|
138 |
+
{
|
139 |
+
"question": "Do you have reliable transportation?",
|
140 |
+
"answer": "Yes"
|
141 |
+
},
|
142 |
+
{
|
143 |
+
"question": "Do you have enough food to eat?",
|
144 |
+
"answer": "No"
|
145 |
+
},
|
146 |
+
{
|
147 |
+
"question": "Do you have stable housing?",
|
148 |
+
"answer": "No"
|
149 |
+
}
|
150 |
+
]
|
151 |
+
|
152 |
+
def get_patient(name: str) -> Tuple[Patient, str]:
|
153 |
+
"""
|
154 |
+
Returns a tuple containing the FHIR Patient resource and the SMART Patient model
|
155 |
+
for the given patient name.
|
156 |
+
"""
|
157 |
+
# Get the example patient with the matching name
|
158 |
+
example_patient = next((p for p in EXAMPLE_PATIENTS if p["name"] == name), None)
|
159 |
+
if not example_patient:
|
160 |
+
raise ValueError(f"No example patient found with name '{name}'")
|
161 |
+
|
162 |
+
# Create a FHIR Patient resource
|
163 |
+
patient = Patient()
|
164 |
+
patient.name = [HumanName()]
|
165 |
+
patient.name[0].given = [example_patient["name"].split()[0]]
|
166 |
+
patient.name[0].family = [example_patient["name"].split()[1]]
|
167 |
+
patient.birthDate = FHIRDate(example_patient["birthdate"])
|
168 |
+
patient.gender = example_patient["gender"]
|
169 |
+
patient.address = [Address()]
|
170 |
+
patient.address[0].line = example_patient["address"]["line"]
|
171 |
+
patient.address[0].city = example_patient["address"]["city"]
|
172 |
+
patient.address[0].state = example_patient["address"]["state"]
|
173 |
+
patient.address[0].postalCode = example_patient["address"]["postalCode"]
|
174 |
+
patient.address[0].country = example_patient["address"]["country"]
|
175 |
+
patient.telecom = [ContactPoint()]
|
176 |
+
patient.telecom[0].system = "phone"
|
177 |
+
patient.telecom[0].value = example_patient["phone"]
|
178 |
+
|
179 |
+
# Create a SMART Patient model
|
180 |
+
smart_patient = smart.Patient.read_from(patient.as_json())
|
181 |
+
|
182 |
+
return patient, smart_patient
|
183 |
+
|
184 |
+
def create_observation(patient_id: str, code: str, value: str, unit: str) -> Observation:
|
185 |
+
"""
|
186 |
+
Creates and returns a FHIR Observation resource with the given patient ID, code, value, and unit.
|
187 |
+
"""
|
188 |
+
observation = Observation()
|
189 |
+
observation.subject = {"reference": f"Patient/{patient_id}"}
|
190 |
+
observation.code = CodeableConcept()
|
191 |
+
observation.code.coding = [Coding()]
|
192 |
+
observation.code.coding[0].system = "http://loinc.org"
|
193 |
+
observation.code.coding[0].code = code
|
194 |
+
observation.valueQuantity = Quantity()
|
195 |
+
observation.valueQuantity.value = float(value)
|
196 |
+
observation.valueQuantity.unit = unit
|
197 |
+
observation.status = "final"
|
198 |
+
observation.effectiveDateTime = FHIRDate("2023-02-21")
|
199 |
+
observation.meta = Meta()
|
200 |
+
observation.meta.profile = ["http://hl7.org/fhir/StructureDefinition/vitalsigns"]
|
201 |
+
|
202 |
+
return observation
|
203 |
+
|
204 |
+
def create_assessment(patient_id: str, code: str, value: str) -> DiagnosticReport:
|
205 |
+
"""
|
206 |
+
Creates and returns a FHIR DiagnosticReport resource with the given patient ID, code, and value.
|
207 |
+
"""
|
208 |
+
report = DiagnosticReport()
|
209 |
+
report.status = "final"
|
210 |
+
report.subject = {"reference": f"Patient/{patient_id}"}
|
211 |
+
report.code = CodeableConcept()
|
212 |
+
report.code.coding = [Coding()]
|
213 |
+
report.code.coding[0].system = "http://loinc.org"
|
214 |
+
report.code.coding[0].code = code
|
215 |
+
report.result = [Reference()]
|
216 |
+
report.result[0].reference = f"Observation/{code}"
|
217 |
+
report.result[0].display = value
|
218 |
+
report.effectiveDateTime = FHIRDate("2023-02-21")
|
219 |
+
|
220 |
+
return report
|
221 |
+
|
222 |
+
def create_rule() -> Library:
|
223 |
+
"""
|
224 |
+
Creates and returns a FHIR Library resource representing a rule.
|
225 |
+
"""
|
226 |
+
rule = Library()
|
227 |
+
rule.status = "draft"
|
228 |
+
rule.type = CodeableConcept()
|
229 |
+
rule.type.coding = [Coding()]
|
230 |
+
rule.type.coding[0].system = "http://terminology.hl7.org/CodeSystem/library-type"
|
231 |
+
rule.type.coding[0].code = "logic-library"
|
232 |
+
rule.type.coding[0].display = "Logic Library"
|
233 |
+
rule.content = [Attachment()]
|
234 |
+
rule.content[0].contentType = "text/cql"
|
235 |
+
rule.content[0].data = "some cql code here"
|
236 |
+
rule.relatedArtifact = [RelatedArtifact()]
|
237 |
+
rule.relatedArtifact[0].type = "depends-on"
|
238 |
+
rule.relatedArtifact[0].resource = {"reference": "Library/example-library"}
|
239 |
+
|
240 |
+
return rule
|
241 |
+
|
242 |
+
def create_referral_request(patient_id: str, reason: str, specialty: str, provider_name: str) -> ReferralRequest:
|
243 |
+
"""
|
244 |
+
Creates and returns a FHIR ReferralRequest resource with the given patient ID, reason, specialty, and provider name.
|
245 |
+
"""
|
246 |
+
referral = ReferralRequest()
|
247 |
+
referral.status = "active"
|
248 |
+
referral.subject = {"reference": f"Patient/{patient_id}"}
|
249 |
+
referral.reasonCode = [CodeableConcept()]
|
250 |
+
referral.reasonCode[0].coding = [Coding()]
|
251 |
+
referral.reasonCode[0].coding[0].system = "http://snomed.info/sct"
|
252 |
+
referral.reasonCode[0].coding[0].code = "123456"
|
253 |
+
referral.reasonCode[0].coding[0].display = reason
|
254 |
+
referral.specialty = CodeableConcept()
|
255 |
+
referral.specialty.coding = [Coding()]
|
256 |
+
referral.specialty.coding[0].system = "http://snomed.info/sct"
|
257 |
+
referral.specialty.coding[0].code = "123456"
|
258 |
+
referral.specialty.coding[0].display = specialty
|
259 |
+
referral.requester = {"reference": f"Practitioner/{provider_name}"}
|
260 |
+
referral.serviceRequested = [CodeableConcept()]
|
261 |
+
referral.serviceRequested[0].coding = [Coding()]
|
262 |
+
referral.serviceRequested[0].coding[0].system = "http://snomed.info/sct"
|
263 |
+
referral.serviceRequested[0].coding[0].code = "123456"
|
264 |
+
referral.serviceRequested[0].coding[0].display = "Example Service"
|
265 |
+
referral.priority = CodeableConcept()
|
266 |
+
referral.priority.coding = [Coding()]
|
267 |
+
referral.priority.coding[0].system = "http://terminology.hl7.org/CodeSystem/request-priority"
|
268 |
+
referral.priority.coding[0].code = "routine"
|
269 |
+
referral.priority.coding[0].display = "Routine"
|
270 |
+
|
271 |
+
return referral
|
272 |
+
|
273 |
+
def create_provider(name: str, organization_name: str) -> Tuple[Practitioner, PractitionerRole, Organization]:
|
274 |
+
"""
|
275 |
+
Creates and returns a tuple containing the FHIR Practitioner, PractitionerRole, and Organization resources for the given provider name and organization name.
|
276 |
+
"""
|
277 |
+
# Create a FHIR Practitioner resource
|
278 |
+
practitioner = Practitioner()
|
279 |
+
practitioner.name = [HumanName()]
|
280 |
+
practitioner.name[0].text = name
|
281 |
+
practitioner.identifier = [Identifier()]
|
282 |
+
practitioner.identifier[0].system = "http://example.com/providers"
|
283 |
+
practitioner.identifier[0].value = "12345"
|
284 |
+
|
285 |
+
# Create a FHIR PractitionerRole resource
|
286 |
+
practitioner_role = PractitionerRole()
|
287 |
+
practitioner_role.practitioner = {"reference": f"Practitioner/{practitioner.id}"}
|
288 |
+
practitioner_role.organization = {"reference": f"Organization/{organization_name}"}
|
289 |
+
practitioner_role.code = [CodeableConcept()]
|
290 |
+
practitioner_role.code[0].coding = [Coding()]
|
291 |
+
practitioner_role.code[0].coding[0].system = "http://nucc.org/provider-taxonomy"
|
292 |
+
practitioner_role.code[0].coding[0].code = "207Q00000X"
|
293 |
+
practitioner_role.code[0].coding[0].display = "Family Medicine"
|
294 |
+
practitioner_role.specialization = [CodeableConcept()]
|
295 |
+
practitioner_role.specialization[0].coding = [Coding()]
|
296 |
+
practitioner_role.specialization[0].coding[0].system = "http://snomed.info/sct"
|
297 |
+
practitioner_role.specialization[0].coding[0].code = "123456"
|
298 |
+
practitioner_role.specialization[0].coding[0].display = "Example Specialty"
|
299 |
+
|
300 |
+
# Create a FHIR Organization resource
|
301 |
+
organization = Organization()
|
302 |
+
organization.name = organization_name
|
303 |
+
|
304 |
+
return practitioner, practitioner_role, organization
|
305 |
+
|
306 |
+
def create_program(name: str, goal_description: str, start_date: str, end_date: str) -> Program:
|
307 |
+
"""
|
308 |
+
Creates and returns a FHIR Program resource with the given name, goal description, start date, and end date.
|
309 |
+
"""
|
310 |
+
program = Program()
|
311 |
+
program.status = "active"
|
312 |
+
program.category = [CodeableConcept()]
|
313 |
+
program.category[0].coding = [Coding()]
|
314 |
+
program.category[0].coding[0].system = "http://example.com/program-categories"
|
315 |
+
program.category[0].coding[0].code = "123456"
|
316 |
+
program.category[0].coding[0].display = "Example Category"
|
317 |
+
program.name = name
|
318 |
+
program.description = "Example Description"
|
319 |
+
program.goal = [Goal()]
|
320 |
+
program.goal[0].description = CodeableConcept()
|
321 |
+
program.goal[0].description.text = goal_description
|
322 |
+
program.start = FHIRDate(start_date)
|
323 |
+
program.end = FHIRDate(end_date)
|
324 |
+
|
325 |
+
return program
|
326 |
+
|
327 |
+
def create_fulfillment(patient_id: str, medication_name: str, quantity: int, dispense_date: str) -> MedicationStatement:
|
328 |
+
"""
|
329 |
+
Creates and returns a FHIR MedicationStatement resource representing a fulfillment of a prescription for the given patient ID, medication name, quantity, and dispense date.
|
330 |
+
"""
|
331 |
+
# Create a FHIR Medication resource
|
332 |
+
medication = Medication()
|
333 |
+
medication.code = CodeableConcept()
|
334 |
+
medication.code.text = medication_name
|
335 |
+
|
336 |
+
# Create a FHIR MedicationStatement resource
|
337 |
+
fulfillment = MedicationStatement()
|
338 |
+
fulfillment.status = "completed"
|
339 |
+
fulfillment.medicationReference = {"reference": f"Medication/{medication.id}"}
|
340 |
+
fulfillment.subject = {"reference": f"Patient/{patient_id}"}
|
341 |
+
fulfillment.dosage = [Dosage()]
|
342 |
+
fulfillment.dosage[0].route = CodeableConcept()
|
343 |
+
fulfillment.dosage[0].route.coding = [Coding()]
|
344 |
+
fulfillment.dosage[0].route.coding[0].system = "http://example.com/routes"
|
345 |
+
fulfillment.dosage[0].route.coding[0].code = "123456"
|
346 |
+
fulfillment.dosage[0].route.coding[0].display = "Example Route"
|
347 |
+
fulfillment.dosage[0].quantity = Quantity()
|
348 |
+
fulfillment.dosage[0].quantity.value = quantity
|
349 |
+
fulfillment.dosage[0].quantity.unit = "pill"
|
350 |
+
fulfillment.effectiveDateTime = FHIRDate(dispense_date)
|
351 |
+
|
352 |
+
return fulfillment
|
353 |
+
|
354 |
+
def create_alert(patient_id: str, message: str, category: str) -> Flag:
|
355 |
+
"""
|
356 |
+
Creates and returns a FHIR Flag resource representing an alert for the given patient ID, message, and category.
|
357 |
+
"""
|
358 |
+
alert = Flag()
|
359 |
+
alert.status = "active"
|
360 |
+
alert.subject = {"reference": f"Patient/{patient_id}"}
|
361 |
+
alert.code = CodeableConcept()
|
362 |
+
alert.code.coding = [Coding()]
|
363 |
+
alert.code.coding[0].system = "http://example.com/alerts"
|
364 |
+
alert.code.coding[0].code = "123456"
|
365 |
+
alert.code.coding[0].display= category
|
366 |
+
alert.period = Period()
|
367 |
+
alert.period.start = FHIRDate("2023-02-21")
|
368 |
+
|
369 |
+
return alert
|
370 |
+
|
371 |
+
def create_note(patient_id: str, text: str) -> DocumentReference:
|
372 |
+
"""
|
373 |
+
Creates and returns a FHIR DocumentReference resource representing a note for the given patient ID and text.
|
374 |
+
"""
|
375 |
+
note = DocumentReference()
|
376 |
+
note.status = "current"
|
377 |
+
note.subject = {"reference": f"Patient/{patient_id}"}
|
378 |
+
note.type = CodeableConcept()
|
379 |
+
note.type.coding = [Coding()]
|
380 |
+
note.type.coding[0].system = "http://loinc.org"
|
381 |
+
note.type.coding[0].code = "11506-3"
|
382 |
+
note.type.coding[0].display = "Consult note"
|
383 |
+
note.content = [Attachment()]
|
384 |
+
note.content[0].contentType = "text/plain"
|
385 |
+
note.content[0].data = text.encode("utf-8")
|
386 |
+
note.author = [Reference()]
|
387 |
+
note.author[0].reference = f"Practitioner/example-provider"
|
388 |
+
note.date = FHIRDate("2023-02-21")
|
389 |
+
|
390 |
+
return note
|
391 |
+
|
392 |
+
def create_social_determinant(question: str, answer: str) -> QuestionnaireResponse:
|
393 |
+
"""
|
394 |
+
Creates and returns a FHIR QuestionnaireResponse resource representing a social determinant of health with the given question and answer.
|
395 |
+
"""
|
396 |
+
response = SmartQuestionnaireResponse()
|
397 |
+
response.questionnaire = "http://example.com/sdh-questionnaire"
|
398 |
+
response.item = []
|
399 |
+
item = QuestionnaireResponseItem()
|
400 |
+
item.linkId = "1"
|
401 |
+
item.text = question
|
402 |
+
item.answer = []
|
403 |
+
answer_item = QuestionnaireResponseItemAnswer()
|
404 |
+
answer_item.valueString = answer
|
405 |
+
item.answer.append(answer_item)
|
406 |
+
response.item.append(item)
|
407 |
+
|
408 |
+
return response
|
409 |
+
|
410 |
+
def create_care_team(name: str, provider_names: List[str]) -> CareTeam:
|
411 |
+
"""
|
412 |
+
Creates and returns a FHIR CareTeam resource representing a care team with the given name and provider names.
|
413 |
+
"""
|
414 |
+
care_team = SmartCareTeam()
|
415 |
+
care_team.status = "active"
|
416 |
+
care_team.name = name
|
417 |
+
care_team.participant = []
|
418 |
+
for provider_name in provider_names:
|
419 |
+
provider_ref = f"Practitioner/{provider_name}"
|
420 |
+
care_team.participant.append(CareTeamParticipant({"member": {"reference": provider_ref}}))
|
421 |
+
|
422 |
+
return care_team
|
423 |
+
|
424 |
+
def create_task(title: str, description: str, assignee: str) -> Task:
|
425 |
+
"""
|
426 |
+
Creates and returns a FHIR Task resource representing a task with the given title, description, and assignee.
|
427 |
+
"""
|
428 |
+
task = SmartTask()
|
429 |
+
task.status = "draft"
|
430 |
+
task.intent = "order"
|
431 |
+
task.title = title
|
432 |
+
task.description = description
|
433 |
+
task.requester = {"reference": "Practitioner/example-requester"}
|
434 |
+
task.owner = {"reference": f"Practitioner/{assignee}"}
|
435 |
+
|
436 |
+
return task
|
437 |
+
|
438 |
+
def create_activity_definition(title: str, description: str, category: str, code: str) -> ActivityDefinition:
|
439 |
+
"""
|
440 |
+
Creates and returns a FHIR ActivityDefinition resource representing an activity definition with the given title, description, category, and code.
|
441 |
+
"""
|
442 |
+
activity_definition = SmartActivityDefinition()
|
443 |
+
activity_definition.status = "draft"
|
444 |
+
activity_definition.kind = "procedure"
|
445 |
+
activity_definition.title = title
|
446 |
+
activity_definition.description = description
|
447 |
+
activity_definition.category = CodeableConcept({"coding": [Coding({"system": "http://example.com/categories", "code": category, "display": f"Example {category}"})]})
|
448 |
+
activity_definition.code = CodeableConcept({"coding": [Coding({"system": "http://example.com/codes", "code": code, "display": f"Example {code}"})]})
|
449 |
+
|
450 |
+
return activity_definition
|
451 |
+
|
452 |
+
# Streamlit app code
|
453 |
+
import streamlit as st
|
454 |
+
from fhir.resources import *
|
455 |
+
|
456 |
+
st.set_page_config(page_title="FHIR Demo", page_icon=":heart:", layout="wide")
|
457 |
+
|
458 |
+
st.title("FHIR Demo")
|
459 |
+
|
460 |
+
st.sidebar.title("Navigation")
|
461 |
+
navigation = st.sidebar.radio("Go to", ("Home", "Observations", "Assessments", "Rules", "Referrals", "Providers", "Programs", "Fulfillment", "Alerts", "Notes", "Social Determinants of Health"))
|
462 |
+
|
463 |
+
st.sidebar.title("SMART App")
|
464 |
+
smart = None
|
465 |
+
if st.sidebar.button("Launch SMART App"):
|
466 |
+
smart = SmartApp.launch()
|
467 |
+
st.sidebar.write("SMART App launched")
|
468 |
+
if st.sidebar.button("Close SMART App"):
|
469 |
+
if smart:
|
470 |
+
smart.close()
|
471 |
+
st.sidebar.write("SMART App closed")
|
472 |
+
else:
|
473 |
+
st.sidebar.write("No SMART App to close")
|
474 |
+
|
475 |
+
if navigation == "Home":
|
476 |
+
st.write("Welcome to the FHIR Demo!")
|
477 |
+
st.write("Use the sidebar to navigate to different FHIR resources.")
|
478 |
+
st.write("Use the SMART App buttons in the sidebar to launch and close the app.")
|
479 |
+
|
480 |
+
elif navigation == "Observations":
|
481 |
+
st.write("# Observations")
|
482 |
+
patient_name = st.selectbox("Select patient", [p["name"] for p in EXAMPLE_PATIENTS])
|
483 |
+
patient, smart_patient = get_patient(patient_name)
|
484 |
+
st.write("### Patient")
|
485 |
+
st.write(f"Name: {patient.name[0].given[0]} {patient.name[0].family[0]}")
|
486 |
+
st.write(f"Birthdate: {patient.birthDate.as_json()}")
|
487 |
+
st.write(f"Gender: {patient.gender}")
|
488 |
+
st.write(f"Address: {patient.address[0].line[0]}, {patient.address[0].city}, {patient.address[0].state} {patient.address[0].postalCode}, {patient.address[0].country}")
|
489 |
+
st.write(f"Phone: {patient.telecom[0].value}")
|
490 |
+
st.write("### Add Observation")
|
491 |
+
code = st.selectbox("Select code", ["8302-2", "8462-4", "8867-4", "9279-1"])
|
492 |
+
value = st.number_input("Value")
|
493 |
+
unit = st.selectbox("Select unit", ["bpm", "mmHg", "mg/dL", "kg"])
|
494 |
+
observation = create_observation(patient.id, code, value, unit)
|
495 |
+
smart_client = smart.patient(smart_patient)
|
496 |
+
response = smart_client.create(observation)
|
497 |
+
st.write("Observation created:")
|
498 |
+
st.write(response.as_json())
|
499 |
+
|
500 |
+
elif navigation == "Assessments":
|
501 |
+
st.write("# Assessments")
|
502 |
+
patient_name = st.selectbox("Select patient", [p["name"] for p in EXAMPLE_PATIENTS])
|
503 |
+
patient, smart_patient = get_patient(patient_name)
|
504 |
+
st.write("### Patient")
|
505 |
+
st.write(f"Name: {patient.name[0].given[0]} {patient.name[0].family[0]}")
|
506 |
+
st.write(f"Birthdate: {patient.birthDate.as_json()}")
|
507 |
+
st.write(f"Gender: {patient.gender}")
|
508 |
+
st.write(f"Address: {patient.address[0].line[0]}, {patient.address[0].city}, {patient.address[0].state} {patient.address[0].postalCode}, {patient.address[0].country}")
|
509 |
+
st.write(f"Phone: {patient.telecom[0].value}")
|
510 |
+
st.write("### Add Assessment")
|
511 |
+
code = st.selectbox("Select code", ["69646-2", "8150-9", "82810-3"])
|
512 |
+
value = st.selectbox("Select value", ["Absent", "Mild", "Moderate", "Severe"])
|
513 |
+
assessment = create_assessment(patient.id, code, value)
|
514 |
+
smart_client = smart.patient(smart_patient)
|
515 |
+
response = smart_client.create(assessment)
|
516 |
+
st.write("Assessment created:")
|
517 |
+
st.write(response.as_json())
|
518 |
+
|
519 |
+
elif navigation == "Rules":
|
520 |
+
st.write("# Rules")
|
521 |
+
st.write("### Add Rule")
|
522 |
+
code = st.selectbox("Select code", ["36405-2", "89053-6"])
|
523 |
+
description = st.text_input("Description")
|
524 |
+
rule = create_rule(code, description)
|
525 |
+
response = smart.server.create(rule)
|
526 |
+
st.write("Rule created:")
|
527 |
+
st.write(response.as_json())
|
528 |
+
|
529 |
+
elif navigation == "Referrals":
|
530 |
+
st.write("# Referrals")
|
531 |
+
patient_name = st.selectbox("Select patient", [p["name"] for p in EXAMPLE_PATIENTS])
|
532 |
+
patient, smart_patient = get_patient(patient_name)
|
533 |
+
st.write("### Patient")
|
534 |
+
st.write(f"Name: {patient.name[0].given[0]} {patient.name[0].family[0]}")
|
535 |
+
st.write(f"Birthdate: {patient.birthDate.as_json()}")
|
536 |
+
st.write(f"Gender: {patient.gender}")
|
537 |
+
st.write(f"Address: {patient.address[0].line[0]}, {patient.address[0].city}, {patient.address[0].state} {patient.address[0].postalCode}, {patient.address[0].country}")
|
538 |
+
st.write(f"Phone: {patient.telecom[0].value}")
|
539 |
+
st.write("### Add Referral")
|
540 |
+
reason = st.text_input("Reason")
|
541 |
+
specialty = st.text_input("Specialty")
|
542 |
+
provider_name = st.selectbox("Select provider", [p["name"] for p in EXAMPLE_PROVIDERS])
|
543 |
+
provider, practitioner_role, organization = create_provider(provider_name, "Example Healthcare")
|
544 |
+
response1 = smart.server.create(provider)
|
545 |
+
response2 = smart.server.create(practitioner_role)
|
546 |
+
response3 = smart.server.create(organization)
|
547 |
+
referral = create_referral_request(patient.id, reason, specialty, provider_name)
|
548 |
+
response4 = smart.server.create(referral)
|
549 |
+
st.write("Referral created:")
|
550 |
+
st.write(response4.as_json())
|
551 |
+
|
552 |
+
elif navigation == "Providers":
|
553 |
+
st.write("# Providers")
|
554 |
+
st.write("### Add Provider")
|
555 |
+
name = st.text_input("Name")
|
556 |
+
organization_name = st.text_input("Organization")
|
557 |
+
provider, practitioner_role, organization = create_provider(name, organization_name)
|
558 |
+
response1 = smart.server.create(provider)
|
559 |
+
response2 = smart.server.create(practitioner_role)
|
560 |
+
response3 = smart.server.create(organization)
|
561 |
+
st.write("Provider created:")
|
562 |
+
st.write(response1.as_json())
|
563 |
+
|
564 |
+
elif navigation == "Programs":
|
565 |
+
st.write("# Programs")
|
566 |
+
st.write("### Add Program")
|
567 |
+
name = st.text_input("Name")
|
568 |
+
goal_description = st.text_input("Goal description")
|
569 |
+
start_date = st.date_input("Start date")
|
570 |
+
end_date = st.date_input("End date")
|
571 |
+
program = create_program(name, goal_description, start_date.isoformat(), end_date.isoformat())
|
572 |
+
response = smart.server.create(program)
|
573 |
+
st.write("Program created:")
|
574 |
+
st.write(response.as_json())
|
575 |
+
|
576 |
+
elif navigation == "Fulfillment":
|
577 |
+
st.write("# Fulfillment")
|
578 |
+
patient_name = st.selectbox("Select patient", [p["name"] for p in EXAMPLE_PATIENTS])
|
579 |
+
patient, smart_patient = get_patient(patient_name)
|
580 |
+
st.write("### Patient")
|
581 |
+
st.write(f"Name: {patient.name[0].given[0]} {patient.name[0].family[0]}")
|
582 |
+
st.write(f"Birthdate: {patient.birthDate.as_json()}")
|
583 |
+
st.write(f"Gender: {patient.gender}")
|
584 |
+
st.write(f"Address: {patient.address[0].line[0]}, {patient.address[0].city}, {patient.address[0].state} {patient.address[0].postalCode}, {patient.address[0].country}")
|
585 |
+
st.write(f"Phone: {patient.telecom[0].value}")
|
586 |
+
st.write("### Add Fulfillment")
|
587 |
+
medication_name = st.selectbox("Select medication", ["Aspirin", "Lisinopril", "Metformin"])
|
588 |
+
quantity = st.number_input("Quantity")
|
589 |
+
dispense_date = st.date_input("Dispense date")
|
590 |
+
fulfillment = create_fulfillment(patient.id, medication_name, quantity, dispense_date.isoformat())
|
591 |
+
smart_client = smart.patient(smart_patient)
|
592 |
+
response = smart_client.create(fulfillment)
|
593 |
+
st.write("Fulfillment created:")
|
594 |
+
st.write(response.as_json())
|
595 |
+
|
596 |
st.markdown("""
|
597 |
|
598 |
|