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

core: add some missing Encounter.class codes #334

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ SELECT DISTINCT
concat('Encounter/', e.id) AS encounter_ref
FROM temp_encounter AS e
LEFT JOIN core__fhir_mapping_expected_act_encounter_code_v3 AS eac
ON e.class_code = eac.found
ON e.class_code = eac.found AND e.class_system = eac.found_system
LEFT JOIN core__fhir_act_encounter_code_v3 AS ac ON eac.expected = ac.code
INNER JOIN core__patient AS p ON e.subject_ref = p.subject_ref;
38 changes: 23 additions & 15 deletions cumulus_library/studies/core/fhir_mapping_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,31 @@ SELECT * FROM

-- ############################################################
-- FHIR mapping of as found Encounter codes to the expected encounter code from
-- http://hl7.org/fhir/STU3/v3/ActEncounterCode/vs.html
-- http://hl7.org/fhir/R4/v3/ActEncounterCode/vs.html

CREATE TABLE core__fhir_mapping_expected_act_encounter_code_v3 AS
SELECT * FROM
(
VALUES
('AMB', 'AMB'),
('AMB', 'R'),
('AMB', 'O'),
('EMER', 'EMER'),
('EMER', 'E'),
('FLD', 'FLD'),
('HH', 'HH'),
('IMP', 'IMP'),
('ACUTE', 'ACUTE'),
('NONAC', 'NONAC'),
('PRENC', 'PRENC'),
('SS', 'SS'),
('VR', 'VR')
) AS t (expected, found);
-- http://hl7.org/fhir/R4/v3/ActEncounterCode/vs.html
-- This is the modern, expected class system.
('AMB', 'AMB', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('EMER', 'EMER', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('FLD', 'FLD', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('HH', 'HH', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('IMP', 'IMP', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('ACUTE', 'ACUTE', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('NONAC', 'NONAC', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('OBSENC', 'OBSENC', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('PRENC', 'PRENC', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('SS', 'SS', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('VR', 'VR', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
-- http://terminology.hl7.org/CodeSystem/v2-0004
-- This is an older class system, still in active use apparently (at BCH Cerner).
-- Unmapped codes: B (obstetrics), C (commercial), N (not applicable), U (unknown)
('EMER', 'E', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('IMP', 'I', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('AMB', 'O', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('PRENC', 'P', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('AMB', 'R', 'http://terminology.hl7.org/CodeSystem/v2-0004') -- recurring patient
Comment on lines +129 to +147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have we checked this against a recent-ish epic? just to make sure these codes are actually present in the form we'd expect as opposed to the proprietary format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the export I have from the Epic sandbox, it's always urn:oid:1.2.840.114350.1.72.1.7.7.10.696784.13260 and codes like 13 or 17

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this field is just a raw coding - so only room for one code & system pair 😦

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use multiple entries in this table? like core/fhir_mapping_tables.sql?

) AS t (expected, found, found_system);
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ temp_encounter_completion AS (
SELECT
ece.encounter_id,
(
-- Every time you add a resource here, update core-study-details.md
BOOL_OR(ec.table_name = 'allergyintolerance')
AND BOOL_OR(ec.table_name = 'condition')
AND BOOL_OR(ec.table_name = 'diagnosticreport')
Expand Down Expand Up @@ -705,7 +706,7 @@ SELECT DISTINCT
concat('Encounter/', e.id) AS encounter_ref
FROM temp_encounter AS e
LEFT JOIN core__fhir_mapping_expected_act_encounter_code_v3 AS eac
ON e.class_code = eac.found
ON e.class_code = eac.found AND e.class_system = eac.found_system
LEFT JOIN core__fhir_act_encounter_code_v3 AS ac ON eac.expected = ac.code
INNER JOIN core__patient AS p ON e.subject_ref = p.subject_ref;

Expand Down Expand Up @@ -735,6 +736,7 @@ temp_encounter_completion AS (
SELECT
ece.encounter_id,
(
-- Every time you add a resource here, update core-study-details.md
BOOL_OR(ec.table_name = 'allergyintolerance')
AND BOOL_OR(ec.table_name = 'condition')
AND BOOL_OR(ec.table_name = 'diagnosticreport')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,31 @@ SELECT * FROM

-- ############################################################
-- FHIR mapping of as found Encounter codes to the expected encounter code from
-- http://hl7.org/fhir/STU3/v3/ActEncounterCode/vs.html
-- http://hl7.org/fhir/R4/v3/ActEncounterCode/vs.html

CREATE TABLE core__fhir_mapping_expected_act_encounter_code_v3 AS
SELECT * FROM
(
VALUES
('AMB', 'AMB'),
('AMB', 'R'),
('AMB', 'O'),
('EMER', 'EMER'),
('EMER', 'E'),
('FLD', 'FLD'),
('HH', 'HH'),
('IMP', 'IMP'),
('ACUTE', 'ACUTE'),
('NONAC', 'NONAC'),
('PRENC', 'PRENC'),
('SS', 'SS'),
('VR', 'VR')
) AS t (expected, found);
-- http://hl7.org/fhir/R4/v3/ActEncounterCode/vs.html
-- This is the modern, expected class system.
('AMB', 'AMB', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('EMER', 'EMER', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('FLD', 'FLD', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('HH', 'HH', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('IMP', 'IMP', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('ACUTE', 'ACUTE', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('NONAC', 'NONAC', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('OBSENC', 'OBSENC', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('PRENC', 'PRENC', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('SS', 'SS', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
('VR', 'VR', 'http://terminology.hl7.org/CodeSystem/v3-ActCode'),
-- http://terminology.hl7.org/CodeSystem/v2-0004
-- This is an older class system, still in active use apparently (at BCH Cerner).
-- Unmapped codes: B (obstetrics), C (commercial), N (not applicable), U (unknown)
('EMER', 'E', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('IMP', 'I', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('AMB', 'O', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('PRENC', 'P', 'http://terminology.hl7.org/CodeSystem/v2-0004'),
('AMB', 'R', 'http://terminology.hl7.org/CodeSystem/v2-0004') -- recurring patient
) AS t (expected, found, found_system);
25 changes: 25 additions & 0 deletions tests/core/test_core_encounters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Tests for core__encounter"""

import json

from tests import testbed_utils


def test_core_enc_class(tmp_path):
"""Verify that we handle multiply rows as needed when multiple options appear"""
v2_sys = "http://terminology.hl7.org/CodeSystem/v2-0004"
v3_sys = "http://terminology.hl7.org/CodeSystem/v3-ActCode"

testbed = testbed_utils.LocalTestbed(tmp_path)
testbed.add_encounter("o", **{"class": {"code": "O", "system": v2_sys}})
testbed.add_encounter("obsenc", **{"class": {"code": "OBSENC", "system": v3_sys}})
testbed.add_encounter("unsupported", **{"class": {"code": "?", "system": v3_sys}})

con = testbed.build()
df = con.sql("SELECT id, class_code, class_display FROM core__encounter ORDER BY id").df()
rows = json.loads(df.to_json(orient="records"))
assert rows == [
{"id": "o", "class_code": "AMB", "class_display": "ambulatory"},
{"id": "obsenc", "class_code": "OBSENC", "class_display": "observation encounter"},
{"id": "unsupported", "class_code": None, "class_display": None},
]
Loading