Skip to content

Commit 4d0622a

Browse files
authored
Merge pull request #74 from smart-on-fhir/mikix/encounter-class
feat!: switch encounter categories from 'type' to 'class'
2 parents 5012592 + 8d2015d commit 4d0622a

File tree

14 files changed

+45
-43
lines changed

14 files changed

+45
-43
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A Cumulus-based implementation of the [qualifier metrics](https://github.com/syn
44

55
## Implemented Metrics
66

7-
The following qualifier metrics are implemented (per January 2025 qualifer definitions).
7+
The following qualifier metrics are implemented (per February 2025 qualifer definitions).
88

99
- [c_attachment_count](https://github.com/sync-for-science/qualifier/blob/master/metrics.md#c_attachment_count)
1010
- [c_content_type_use](https://github.com/sync-for-science/qualifier/blob/master/metrics.md#c_content_type_use)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Data Metrics study for Cumulus Library"""
22

3-
__version__ = "7.0.2"
3+
__version__ = "8.0.0"

cumulus_library_data_metrics/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def render_sql(self, template: str, **kwargs) -> str:
152152
kwargs["dates"] = self.date_fields.get(src)
153153
kwargs["patient_field"] = resource_info.PATIENTS.get(src)
154154
kwargs["schema"] = self.schemas.get(src)
155-
kwargs.update(resource_info.CATEGORIES.get(src, {}))
155+
kwargs["cat_info"] = resource_info.CATEGORIES.get(src, {})
156156

157157
# See how we should combine counts.
158158
kwargs["output_mode"] = self.output_mode

cumulus_library_data_metrics/c_resource_count/c_resource_count.jinja

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{% import 'utils.jinja' as utils %}
22

33
CREATE TABLE {{ study_prefix }}__count_c_resource_count_{{ src|lower }}_{{ period|lower }} AS (
4-
{%- set orig_cat_field = cat_field %}
54
WITH
65
src_status AS {{ utils.extract_status(src) }},
7-
src_category AS {{ utils.extract_category_codes(src, cat_field, cat_systems) }},
6+
src_category AS {{ utils.extract_category_codes(src, cat_info) }},
87

98
simplified AS (
109
SELECT
1110
src.id,
1211

13-
{%- if cat_field %}
14-
{{ utils.coalesce_missing('src_category.category') }} AS {{ cat_field }},
12+
{%- if cat_info %}
13+
{{ utils.coalesce_missing('src_category.category') }} AS {{ cat_info["cat_field"] }},
1514
{%- endif %}
1615

1716
-- Parse string datetime into a single period string
@@ -30,8 +29,8 @@ simplified AS (
3029
)
3130

3231
{% call utils.make_counts('simplified', output_mode) %}
33-
{%- if cat_field %}
34-
{{ cat_field }},
32+
{%- if cat_info %}
33+
{{ cat_info["cat_field"] }},
3534
{%- endif %}
3635

3736
{%- if dates %}

cumulus_library_data_metrics/c_resources_per_pt/c_resources_per_pt.jinja

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ patient_refs AS (
1212

1313
{% for resource, field_name in patient_fields.items() %}
1414
cat_slice_{{ resource|lower }}_codes AS
15-
{{ utils.extract_category_codes(
16-
resource,
17-
categories.get(resource, {}).get("cat_field"),
18-
categories.get(resource, {}).get("cat_systems"),
19-
) }},
15+
{{ utils.extract_category_codes(resource, categories.get(resource, {})) }},
2016
{% endfor %}
2117

2218
counts_by_pt AS (

cumulus_library_data_metrics/c_system_use/c_system_use.jinja

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ WITH
66
src_status AS {{ utils.extract_status(src) }},
77

88
{% if use_category %}
9-
src_categories AS {{ utils.extract_codes(src, cat_field, system=cat_systems, is_array=true) }},
9+
src_categories AS {{ utils.extract_category_codes(src, cat_info) }},
1010
{% endif %}
1111

1212
src_systems_flat AS (
@@ -58,7 +58,7 @@ simplified AS (
5858
{% endif %}
5959

6060
{% if use_category %}
61-
{{ utils.array_to_string('src_categories.codes') }} AS category,
61+
{{ utils.coalesce_missing('src_categories.category') }} AS category,
6262
{% endif %}
6363

6464
{{ utils.array_to_string('src_systems.systems') }} AS systems,
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
CREATE TABLE {{ study_prefix }}__meta_version AS
2-
SELECT 2 AS data_package_version;
2+
SELECT 3 AS data_package_version;

cumulus_library_data_metrics/resource_info.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
"cat_systems": [systems.USCORE_DOCREF_CATEGORY],
3232
},
3333
"Encounter": {
34-
"cat_field": "type",
35-
# https://hl7.org/fhir/us/core/STU4/ValueSet-us-core-encounter-type.html
36-
"cat_systems": [systems.CPT, systems.SNOMED],
34+
"cat_field": "class",
35+
# https://www.hl7.org/fhir/R4/v3/ActEncounterCode/vs.html
36+
"cat_systems": [systems.ENCOUNTER_CLASS],
37+
"is_coding": True,
3738
},
3839
"MedicationRequest": {
3940
"cat_field": "category",
@@ -50,7 +51,7 @@
5051
# Date fields in preference order.
5152
# This prefers "interaction with health system" dates, then administrative dates like "issued",
5253
# then best effort start dates like onsetDateTime.
53-
# See https://github.com/smart-on-fhir/cumulus-library-data-metrics/issues/16 for more.
54+
# See https://github.com/sync-for-science/qualifier/blob/master/metrics.md#by-date
5455
DATES = {
5556
"AllergyIntolerance": ["recordedDate", "onsetDateTime", "onsetPeriod.start", "onsetPeriod.end"],
5657
"Condition": [

cumulus_library_data_metrics/systems.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# Resource-specific systems
1515
CONDITION_CATEGORY = "http://terminology.hl7.org/CodeSystem/condition-category"
1616
DIAGNOSTIC_SECTION = "http://terminology.hl7.org/CodeSystem/v2-0074"
17+
ENCOUNTER_CLASS = "http://terminology.hl7.org/CodeSystem/v3-ActCode"
1718
MEDREQ_CATEGORY = "http://terminology.hl7.org/CodeSystem/medicationrequest-category"
1819
OBSERVATION_CATEGORY = "http://terminology.hl7.org/CodeSystem/observation-category"
1920
USCORE_CONDITION_CATEGORY = "http://hl7.org/fhir/us/core/CodeSystem/condition-category"

cumulus_library_data_metrics/utils.jinja

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
{% macro _compare_systems(system) -%}
1+
{% macro _compare_systems(coding_field, system) -%}
22
{#- Support both a list of systems or a single system #}
33
{%- if system is string %}
4-
c.coding.system = '{{ system }}'
4+
{{ coding_field }}.system = '{{ system }}'
55
{%- elif system %}
66
(
77
FALSE -- just here so rest of these can start with OR
88
{%- for sys_info in system %}
99
{#- sys_info can either by a string or a list of string then codes #}
1010
{%- if sys_info is string %}
11-
OR c.coding.system = '{{ sys_info }}'
11+
OR {{ coding_field }}.system = '{{ sys_info }}'
1212
{%- else %}
1313
OR (
14-
c.coding.system = '{{ sys_info[0] }}'
15-
AND c.coding.code in ('{{ sys_info[1]|join("', '") }}')
14+
{{ coding_field }}.system = '{{ sys_info[0] }}'
15+
AND {{ coding_field }}.code in ('{{ sys_info[1]|join("', '") }}')
1616
)
1717
{%- endif %}
1818
{%- endfor %}
@@ -48,7 +48,7 @@
4848
{%- endif %}
4949
WHERE
5050
c.coding.code IS NOT NULL
51-
AND {{ _compare_systems(system) }}
51+
AND {{ _compare_systems("c.coding", system) }}
5252

5353
GROUP BY id
5454
)
@@ -72,7 +72,7 @@
7272
{%- endif %}
7373
WHERE
7474
c.coding.code IS NOT NULL
75-
AND {{ _compare_systems(system) }}
75+
AND {{ _compare_systems("c.coding", system) }}
7676
)
7777
{%- endmacro %}
7878

@@ -117,9 +117,17 @@
117117

118118

119119
-- Extracts the category codes and returns a select with (id (string), category (string)).
120-
{% macro extract_category_codes(src, cat_field, cat_systems) -%}
120+
{% macro extract_category_codes(src, cat_info) -%}
121121
(
122-
{% if cat_systems %}
122+
{%- set cat_systems = cat_info.get("cat_systems") %}
123+
{%- set cat_field = cat_info.get("cat_field") %}
124+
{%- set is_coding = cat_info.get("is_coding", False) %}
125+
{% if is_coding %}
126+
{# TODO: handle array & no-systems cases. But this works for now. #}
127+
SELECT id, {{ cat_field }}.code AS category
128+
FROM {{ src }}
129+
WHERE {{ _compare_systems(cat_field, cat_systems) }}
130+
{% elif cat_systems %}
123131
WITH
124132
array_codes AS {{ extract_codes(src, cat_field, system=cat_systems, is_array=true) }}
125133
SELECT id, {{ array_to_string('codes') }} AS category
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
{"resourceType": "Encounter", "id": "with-date", "type": [{"coding": [{"code": "abcd", "system": "http://www.ama-assn.org/go/cpt"}]}], "period": {"start": "2023-10-04"}}
2-
{"resourceType": "Encounter", "id": "no-date", "type": [{"coding": [{"code": "1234", "system": "http://snomed.info/sct"}]}]}
3-
{"resourceType": "Encounter", "id": "multiple-codings", "type": [{"coding": [{"code": "abcd", "system": "http://www.ama-assn.org/go/cpt"}, {"code": "1234", "system": "http://snomed.info/sct"}]}], "period": {"start": "2023-10-04"}}
4-
{"resourceType": "Encounter", "id": "unrelated-category", "type": [{"coding": [{"code": "nope", "system": "nope"}]}]}
1+
{"resourceType": "Encounter", "id": "with-date", "class": {"code": "AMB", "system": "http://terminology.hl7.org/CodeSystem/v3-ActCode"}, "period": {"start": "2023-10-04"}}
2+
{"resourceType": "Encounter", "id": "no-date", "class": {"code": "EMER", "system": "http://terminology.hl7.org/CodeSystem/v3-ActCode"}}
3+
{"resourceType": "Encounter", "id": "unrelated-category", "class": {"code": "nope", "system": "nope"}}
54
{"resourceType": "Encounter", "id": "no-category", "period": {"start": "2022-06-04"}}
65
{"resourceType": "Encounter", "id": "nothing"}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
cnt,type,month,status
1+
cnt,class,month,status
22
2,cumulus__none,cumulus__none,cumulus__none
33
1,cumulus__none,2022-06,cumulus__none
4-
1,abcd,2023-10,cumulus__none
5-
1,1234; abcd,2023-10,cumulus__none
6-
1,1234,cumulus__none,cumulus__none
4+
1,EMER,cumulus__none,cumulus__none
5+
1,AMB,2023-10,cumulus__none
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
cnt,type,year,status
1+
cnt,class,year,status
22
2,cumulus__none,cumulus__none,cumulus__none
33
1,cumulus__none,2022,cumulus__none
4-
1,abcd,2023,cumulus__none
5-
1,1234; abcd,2023,cumulus__none
6-
1,1234,cumulus__none,cumulus__none
4+
1,EMER,cumulus__none,cumulus__none
5+
1,AMB,2023,cumulus__none
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
data_package_version
2-
2
2+
3

0 commit comments

Comments
 (0)