Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly find reportable conditions #2338

Merged
merged 11 commits into from
Aug 14, 2024
42 changes: 19 additions & 23 deletions containers/trigger-code-reference/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import List
from typing import Union

import fhirpathpy


def convert_inputs_to_list(value: Union[list, str, int, float]) -> list:
"""
Expand Down Expand Up @@ -200,31 +202,25 @@ def read_json_from_assets(filename: str) -> dict:

def find_conditions(bundle: dict) -> set[str]:
"""
Finds conditions in a bundle of resources.
Extracts the SNOMED codes of reportable conditions from a FHIR bundle.

:param bundle: The bundle of resources to search.
:return: A set of SNOMED codes representing the conditions found.
:param bundle: A FHIR bundle
:return: A set of SNOMED codes for reportable conditions
"""
CONDITION_CODE = "64572001"
SNOMED_URL = "http://snomed.info/sct"

# Get all resources
resources = [resource["resource"] for resource in bundle["entry"]]

# Filter observations that have the SNOMED code for "Condition".
resources_with_conditions = [
obs
for obs in resources
if "code" in obs
and any(coding["code"] == CONDITION_CODE for coding in obs["code"]["coding"])
]
path_to_reportability_response_info_section = fhirpathpy.compile(
"Bundle.entry.resource.where(resourceType='Composition').section.where(title = 'Reportability Response Information Section').entry"
)
trigger_entries = path_to_reportability_response_info_section(bundle)
triggering_IDs = [x["reference"].split("/") for x in trigger_entries]
codes = set()
for type, id in triggering_IDs:
result = fhirpathpy.evaluate(
bundle,
f"Bundle.entry.resource.ofType({type}).where(id='{id}').valueCodeableConcept.coding.where(system = 'http://snomed.info/sct').code",
)

# Extract unique SNOMED codes from the observations
snomed_codes = {
coding["code"]
for obs in resources_with_conditions
for coding in obs.get("valueCodeableConcept", {}).get("coding", [])
if coding["system"] == SNOMED_URL
}
if result:
codes.add(result[0])

return snomed_codes
return codes
3 changes: 2 additions & 1 deletion containers/trigger-code-reference/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
pathlib
httpx
requests
python-multipart
python-multipart
fhirpathpy
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def test_stamp_condition_extensions(patched_get_services_list):
# We'll just try stamping one of each resource type, no need
# to see 47 observations
message = json.load(open(Path(__file__).parent / "assets" / "sample_ecr.json"))
composition = [
e
for e in message["entry"]
if e.get("resource").get("resourceType") == "Composition"
][0]
obs_e = [
e
for e in message["entry"]
Expand All @@ -123,7 +128,7 @@ def test_stamp_condition_extensions(patched_get_services_list):
for e in message["entry"]
if e.get("resource").get("resourceType") == "Immunization"
][3]
message["entry"] = [obs_e, cond_e, imm_e]
message["entry"] = [composition, obs_e, cond_e, imm_e]

# Note: obviously not real conditions, we're just simulating stamping different
# resource types according to different condition criteria
Expand All @@ -142,15 +147,3 @@ def test_stamp_condition_extensions(patched_get_services_list):
stamped_message, "840539006", "Observation"
)
assert found_matching_extension

# Check condition: no value set cross-referenced for this bundle, no stamp
found_matching_extension = _check_for_stamped_resource_in_bundle(
stamped_message, "840539006", "Condition"
)
assert not found_matching_extension

# Check immunization: we did find a referenced immunization code, so it should be there
found_matching_extension = _check_for_stamped_resource_in_bundle(
stamped_message, "840539006", "Immunization"
)
assert found_matching_extension
Loading