Examples

Welcome! THis collection of examples helps to highlight FHIR Parser’s core features; for further details, see the getting started guide, or the documentation index which has links to the API doc sections.

Average Patient Age

Calculate the average age of all patients in the FHIR database

from fhir_parser import FHIR

fhir = FHIR()
patients = fhir.get_all_patients()

ages = []
for patient in patients:
    ages.append(patient.age())

print(sum(ages)/len(ages))

Most Common Patient Observations

Calculate the most common patient observations, a single observation comprises of multiple observation components which is normally the actual investigation (the observation might be vital-signs which consists of two observation components for blood pressure tests).

from fhir_parser import FHIR

fhir = FHIR()
patients = fhir.get_all_patients()
observations = []

for patient in patients:
    observations.extend(fhir.get_patient_observations(patient.uuid))

print("Total of {} observations".format(len(observations)))

observation_types = [observation.type for observation in observations]
most_frequent_observation_type = max(set(observation_types), key=observation_types.count)
print("Most common observation type: {}".format(most_frequent_observation_type))

observation_components = []
for observation in observations:
    observation_components.extend(observation.components)

print("Total of {} observation components".format(len(observation_components)))

observation_component_types = [observation_component.display for observation_component in observation_components]
most_frequent_observation_component_type = max(set(observation_types), key=observation_types.count)
print("Most common observation component type: {}".format(most_frequent_observation_component_type))

Graphing

The following examples use MatPlotLib to graph FHIR data.

Marital Status

A bar chart of the marital status of the dataset.

import matplotlib.pyplot as plt
from fhir_parser import FHIR

fhir = FHIR()
patients = fhir.get_all_patients()

marital_status = {}
for patient in patients:
    if str(patient.marital_status) in marital_status:
        marital_status[str(patient.marital_status)] += 1
    else:
        marital_status[str(patient.marital_status)] = 1


plt.bar(range(len(marital_status)), list(marital_status.values()), align='center')
plt.xticks(range(len(marital_status)), list(marital_status.keys()))
plt.show()

Languages Spoken

A bar chart of all languges spoken in the dataset.

import matplotlib.pyplot as plt
from fhir_parser import FHIR

fhir = FHIR()
patients = fhir.get_all_patients()

languages = {}
for patient in patients:
    for language in patient.communications.languages:
        languages.update({language: languages.get(language, 0) + 1})


plt.bar(range(len(languages)), list(languages.values()), align='center')
plt.xticks(range(len(languages)), list(languages.keys()), rotation='vertical')
plt.show()