def get_patient_detail(fhir, pat_id):
    pat = Patient.read(pat_id, fhir.server)

    # If set, use this to query on a fixed time point.
    if REF_DATE:
        ref_date = REF_DATE
    else:
        ref_date = datetime.now()

    # Only the last 24 hours are considered.
    past24 = ref_date - timedelta(days=1)

    # Vital signs for the patient. Includes SBP, HR, RR, satO2, temp
    search = Observation.where(struct={
        'subject': pat_id,
        'category': VITAL_SIGNS_CATEGORY,
        'date': {
            '$gte': past24.strftime('%Y-%m-%dT%H:%M:%SZ'),
        },
    })

    bp = []
    hr = []
    rr = []
    temp = []
    o2 = []

    # Implicitly fetches pages on demand.
    for r in search.perform_resources(fhir.server):
        # Resource code.
        system = r.code.coding[0].system
        code = r.code.coding[0].code

        if system != LOINC_SYSTEM:
            continue

        # Extract systolic component from blood pressure.
        if code == LOINC_BP:
            sbp = None
            dbp = None

            for c in r.component:
                if c.code.coding[0].code == LOINC_SBP:
                    sbp = {
                        'time': r.effectiveDateTime.isostring,
                        'value': c.valueQuantity.value,
                        'unit': c.valueQuantity.unit,
                    }
                elif c.code.coding[0].code == LOINC_DBP:
                    dbp = {
                        'time': r.effectiveDateTime.isostring,
                        'value': c.valueQuantity.value,
                        'unit': c.valueQuantity.unit,
                    }

            if sbp or dbp:
                bp.append({
                    'systolic': sbp,
                    'diastolic': dbp,
                })

        elif code == LOINC_HR:
            hr.append({
                'time': r.effectiveDateTime.isostring,
                'value': r.valueQuantity.value,
                'unit': r.valueQuantity.unit,
            })

        elif code == LOINC_RR:
            rr.append({
                'time': r.effectiveDateTime.isostring,
                'value': r.valueQuantity.value,
                'unit': r.valueQuantity.unit,
            })

        elif code == LOINC_TEMP:
            temp.append({
                'time': r.effectiveDateTime.isostring,
                'value': r.valueQuantity.value,
                'unit': r.valueQuantity.unit,
            })

        elif code == LOINC_O2:
            o2.append({
                'time': r.effectiveDateTime.isostring,
                'value': r.valueQuantity.value,
                'unit': r.valueQuantity.unit,
            })

    mrn = None
    if pat.identifier:
        mrn = pat.identifier[0].value

    return {
        'id': pat_id,
        'mrn': mrn,
        'name': parse_human_name(pat.name[0]),
        'birth_date': pat.birthDate.isostring,
        'gender': pat.gender,
        'measures': {
            'heart_rate': hr,
            'respiratory_rate': rr,
            'temperature': temp,
            'pulse_ox': o2,
            'blood_pressure': bp,
            'mental_status': None,
            'eating_status': None,
        },
    }
Beispiel #2
0
from fhirclient.client import FHIRServer
from fhirclient.models.patient import Patient

server = FHIRServer(None, "http://hapi.fhir.org/baseR4")
p = Patient.read("921009", server)
print("-" * 40)
print("Patient Information")
print("-" * 40)
print(f"NAME: {p.name[0].text}")
print(f"BIRTHDATE: {p.birthDate.date.isoformat()}")

from fhirclient.models.condition import Condition

# from fhirclient.models.bundle import Bundle

# conditionBundle = Condition.where( { "subject": "921009" } ).perform( server )
# if conditionBundle.entry is None:
conditions = Condition.where({"subject": "921009"}).perform_resources(server)
if not conditions:
    print("PROBLEM LIST: None")
else:
    print("PROBLEM LIST:")
    i = 1
    # for entry in conditionBundle.entry:
    for cond in conditions:
        # cond = entry.resource
        print(
            f"{i:>3d}. {cond.code.text:<40s} Severity: {cond.severity.text:<12s}"
        )
        i += 1
Beispiel #3
0
from fhirclient.client import FHIRServer
from fhirclient.models.patient import Patient

server = FHIRServer(None, "http://hapi.fhir.org/baseDstu3")
p = Patient.read("2716055", server)
print("-" * 40)
print("Patient Information")
print("-" * 40)
print(f"NAME: {p.name[0].text}")
print(f"BIRTHDATE: {p.birthDate.date.isoformat()}")

from fhirclient.models.condition import Condition

conditions = Condition.where({"subject": "2716055"}).perform_resources(server)
if not conditions:
    print("PROBLEM LIST: None")
else:
    print("PROBLEM LIST:")
    i = 1
    for cond in conditions:
        print(
            f"{i:>3d}. {cond.code.text:<40s} Severity: {cond.severity.text:<12s}"
        )
        i += 1

from fhirclient.models.encounter import Encounter

encounters = Encounter.where({"subject": "2716055"}).perform_resources(server)
if not encounters:
    print("ENCOUNTER LIST: None")
else: