-
Notifications
You must be signed in to change notification settings - Fork 0
/
STU3Templates.py
179 lines (141 loc) · 6.12 KB
/
STU3Templates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
cql_header = '''library {} version '1.0'
using FHIR version '3.0.0'
include FHIRHelpers version '3.0.0' called FHIRHelpers
'''
codesystems_map = {'http://loinc.org': 'LOINC' ,
'http://snomed.info/sct': 'SNOMED',
'http://www.nlm.nih.gov/research/umls/rxnorm': 'RxNorm',
'http://www.ama-assn.org/go/cpt': 'CPT',
'http://hl7.org/fhir/sid/icd-9-cm': 'ICD9',
'http://hl7.org/fhir/sid/icd-10': 'ICD10'}
resource_temporal_map = {'Encounter': 'period.start',
'Condition': 'onset',
'Procedure': 'performed',
'Observation': 'effective',
'MedicationRequest': 'authoredOn',
'MedicationStatement': 'effective'
}
fhir_choice_fields_map = {
'abatement': ['DateTime','Age'],
'deceased': ['Boolean','DateTime'],
'effective': ['DateTime','Period','Timing','Instant'],
'item': ['CodeableConcept',"Reference"],
'medication': ['CodeableConcept','Reference'],
'multipleBirth': ['Boolean','Reference'],
'occurrance': ['DateTime','String'],
'onset': ['DateTime','Age','Period','Range','String'],
'protocolApplied': ['PositiveInt','String'],
'reported': ['Boolean','Reference'],
'statusReason': ['CodeableConcept','Reference'],
'value': ['Quantity','CodeableConcept','String','Boolean','Integer','Ratio','SampledData','Time','DateTime','Period']
}
fhir_choice_fields_typing_default_map = {
'abatement': 'ToString(FHIRHelpers.ToDateTime(target.abatement as FHIR.dateTime))',
'effectiveDateTime': 'ToString(FHIRHelpers.ToDateTime(target.effective as FHIR.dateTime))',
'effectivePeriod': 'ToString(FHIRHelpers.ToDateTime(target.effective.start as FHIR.dateTime))',
'effective': 'ToString(FHIRHelpers.ToDateTime(msConvertEffective(target)))',
'onset': 'ToString(FHIRHelpers.ToDateTime(target.onset as FHIR.dateTime))',
'dosage[0].dose': 'ToString(FHIRHelpers.ToQuantity((target.dosage[0].dose as FHIR.Quantity)))'
}
msConvertEffectiveFunction = '''\n// Helper Functions\ndefine function msConvertEffective(medStatments List<FHIR.MedicationStatement>):\n\tmedStatments S\n\t\treturn\n\t\t\tcase\n\t\t\t\twhen (S.effective as FHIR.dateTime) is not null then (S.effective as FHIR.dateTime)\n\t\t\t\twhen (S.effective.start as FHIR.dateTime) is not null then (S.effective.start as FHIR.dateTime)\n\t\t\t\telse null\n\t\t\tend'''
cql_codesystem = '''codesystem "{}": '{}'
'''
cql_index_event = '''define "{}": [{}: Code in "{}"]'''
cql_temporal_start_suffix = '''where (target.{} as FHIR.{}) after "IndexEvent" {}'''
cql_temporal_end_suffix = '''where (target.{} as FHIR.{}) before "IndexEvent" {}'''
cql_temporal_both_suffix = '''with "IndexEvent" e\n\t\tsuch that (target.{} as FHIR.{}) after e {} and (target.{} as FHIR.{}) before e {}\n\t\tor FHIRHelpers.ToInterval((target.{})) overlaps Interval[e {}, e {}]'''
cql_temporal_datetime_start_suffix = '''where target.{} after {}'''
cql_temporal_datetime_end_suffix = '''where target.{} before {}'''
cql_temporal_interval_suffix = '''where target.{} during Interval[@{}, @{}]'''
cql_filter = '''define "{}": {}({})'''
cql_shaping = '''define "{}Tuple": from {} target\n\treturn all Tuple {{\n\t\tquestionConcept: '{}',\n\t\tsourceValue: {}, \n\t\tanswerValue: '{}',\n\t\tresultType: '{}'\n\t}}'''
cql_shaping_derived = '''define "{}Tuple": from "{}" target\n\treturn all Tuple {{\n\t\tfhirResourceId: target.id,\n\t\tfhirField: '{}',\n\t\tquestionConcept: '{}',\n\t\tsourceNote: {}, \n\t\tanswerValue: {},\n\t\tvalueType: '{}'\n\t}}'''
cql_aggregator_prefix = '''define "{}":\n\t"{}" '''
cql_aggregator_suffix = '''{} "{}" '''
cql_index_event_return_with_choice_cast = '''define "IndexEvent": "{}" E return (E.{} as FHIR.{})'''
basic_data_entity_template = '''
define final {}:
{}
'''
cql_valueset_template = '''
valueset "{}_valueset": '{}'
'''
pt_define = '''
define "Pt": [Patient]
'''
cql_simple_define_template = '''
{}
{}
'''
cql_template = '''
library Retrieve2 version '1.0'
using FHIR version '3.0.0'
include FHIRHelpers version '3.0.0' called FHIRHelpers
codesystem "LOINC": 'http://loinc.org'
codesystem "SNOMED": 'urn:oid:2.16.840.1.113883.6.96'
codesystem "RxNorm": 'http://www.nlm.nih.gov/research/umls/rxnorm'
codesystem "CPT": 'http://www.ama-assn.org/go/cpt'
codesystem "ICD9": 'urn:oid:2.16.840.1.113883.6.42'
codesystem "ICD10": 'urn:oid:2.16.840.1.113883.6.3'
codesystem USCoreEthnicitySystem: 'urn:oid:2.16.840.1.113883.6.238'
codesystem RelationshipType: 'urn:oid:2.16.840.1.113883.4.642.3.449'
{}
context Patient
{}
{}
{}
'''
# +
cql_concept_code_template = '''Code '{}' from "{}"'''
cql_concept_template = '''define "{}": Concept {{\n\t{}\n}}'''
cql_filter_where_in_clause_template = '''
target
where target.{} in {}
'''
cql_filter_where_during_clause_template = '''
target
where target.{} during Interval[{},{}]
'''
cql_filter_where_before_clause_template = '''
target
where target.{} before {}
'''
cql_filter_where_after_clause_template = '''
target
where target.{} after {}
'''
cql_with_relationship_template = """
target1
with {} target2
such that {}
"""
cql_without_relationship_template = """
target1
without {} target2
such that target1.{} {} target2.{}
"""
# -
# Code '26464-8' from "LOINC",
# Code '804-5' from "LOINC",
# Code '6690-2' from "LOINC",
# Code '49498-9' from "LOINC"
# +
cql_retrieval = '''\n define "{}": [{}: Code in "{}"]'''
cql_result_template = '''
define "{}":
{}
'''
cql_result_template_res = ''' [{}]'''
cql_result_template_cs = ''' [{}: Code in "{}_concepts"]'''
cql_result_template_vs = '''[{}:"{}_valueset"]'''
cql_task_template = '''
define final %s:
Clarity.CQLExecutionTask({
"task_index": %s,
cql: \"\"\"
%s
\"\"\"
});
'''