Skip to content

Commit 6c3c1a9

Browse files
authored
Merge pull request #331 from seattleflu/process_tract_only
Add option to process tract without address in FHIR ETL
2 parents 5dc3023 + 968ee3d commit 6c3c1a9

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lib/id3c/cli/command/etl/fhir.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -798,15 +798,17 @@ def process_locations(db: DatabaseSession, encounter_id: int, encounter: Encount
798798
def process_location(db: DatabaseSession, encounter_id: int, location: Location):
799799
"""
800800
Process a FHIR *location* and attach it to an *encounter_id*.
801+
First looks for a parent location's tract hierarchy;
802+
if no parent location is available, assumes that the location entry itself
803+
is a tract and looks for a tract hierarchy.
801804
"""
802-
# XXX FIXME: This function assumes we always have an address, and never
803-
# just a tract. It also assumes the scales tract and address and their
805+
# XXX FIXME: This function assumes the scales tract and address and their
804806
# relationship in the hierarchy instead of being agnostic to the scale.
805807
# -trs, 19 Dec 2019
806808

807-
def get_tract_hierarchy(location: Location) -> Optional[str]:
809+
def get_tract(location: Location) -> Optional[Any]:
808810
"""
809-
Given a *location*, returns its tract hierarchy if it exists, else None.
811+
Given a *location*, returns its tract location entry if it exists, else None.
810812
"""
811813
scale = 'tract'
812814
tract_identifier = identifier(location, f"{INTERNAL_SYSTEM}/location/{scale}")
@@ -817,7 +819,7 @@ def get_tract_hierarchy(location: Location) -> Optional[str]:
817819
tract = find_location(db, scale, tract_identifier)
818820
assert tract, f"Tract «{tract_identifier}» is unknown"
819821

820-
return tract.hierarchy
822+
return tract
821823

822824
def process_address(location: Location, tract_hierarchy: str) -> Optional[Any]:
823825
"""
@@ -844,12 +846,19 @@ def process_address(location: Location, tract_hierarchy: str) -> Optional[Any]:
844846

845847
try:
846848
parent_location = location.partOf.resolved(Location)
847-
tract_hierarchy = get_tract_hierarchy(parent_location) # TODO do we only want parent hierarchy?
849+
tract_hierarchy = get_tract(parent_location).hierarchy # TODO do we only want parent hierarchy?
848850

849851
except AttributeError:
850852
LOG.info(f"No parent location found for Location with code «{code}», " + \
851-
f"relation «{relation}».")
852-
tract_hierarchy = None
853+
f"relation «{relation}». " + \
854+
"Assuming location is a tract.")
855+
try: # handle encounter locations that are only tracts
856+
tract = get_tract(location)
857+
tract_hierarchy = tract.hierarchy
858+
except (AttributeError, AssertionError):
859+
LOG.info(f"No tract found for Location with code «{code}», " + \
860+
f"relation «{relation}».")
861+
tract_hierarchy = None
853862

854863
address = process_address(location, tract_hierarchy)
855864

@@ -860,7 +869,8 @@ def process_address(location: Location, tract_hierarchy: str) -> Optional[Any]:
860869
upsert_encounter_location(db,
861870
encounter_id = encounter_id,
862871
relation = relation,
863-
location_id = address.id)
872+
location_id = address.id if address else tract.id)
873+
# if there is an address, link encounter location to address; if only tract, link encounter location to tract
864874

865875

866876
def location_code(location: Location) -> str:

0 commit comments

Comments
 (0)