This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StructuredQuery.py
139 lines (99 loc) · 3.85 KB
/
StructuredQuery.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
import json
class TermCode:
def __init__(self, system, code, display, version=None):
self.system = system
self.code = code
self.version = version
self.display = display
def __eq__(self, other):
return self.system == other.system and self.code == other.code
def __hash__(self):
return hash(self.system + self.code)
def __lt__(self, other):
return self.code < other.code
def __repr__(self):
return self.display + " " + self.code + " " + self.system
def del_none(dictionary):
"""
Delete keys with the value ``None`` in a dictionary, recursively.
This alters the input so you may wish to ``copy`` the dict first.
"""
for key, value in list(dictionary.items()):
if value is None:
del dictionary[key]
elif value is []:
del dictionary[key]
elif isinstance(value, dict):
del_none(value)
return dictionary
def del_keys(dictionary, keys):
for k in keys:
dictionary.pop(k, None)
return dictionary
class StructuredQuery:
DO_NOT_SERIALIZE = ["DO_NOT_SERIALIZE"]
def __init__(self):
self.version = ""
self.inclusionCriteria = []
self.exclusionCriteria = None
def to_json(self):
return json.dumps(self, default=lambda o: del_none(
del_keys(o.__dict__, self.DO_NOT_SERIALIZE)),
sort_keys=True, indent=4)
class Unit:
def __init__(self, code, display):
self.code = code
self.display = display
class ValueFilter:
def __init__(self, value_type):
self.type = value_type
class ConceptFilter(ValueFilter):
def __init__(self, selected_concepts=None):
super().__init__("concept")
if selected_concepts is None:
selected_concepts = []
self.selectedConcepts = selected_concepts
class QuantityComparatorFilter(ValueFilter):
def __init__(self, comparator, value, unit: Unit):
super().__init__("quantity-comparator")
self.comparator = comparator
self.value = value
self.unit = unit
class QuantityRangeFilter(ValueFilter):
def __init__(self, min_value, max_value, unit: Unit):
super().__init__("quantity-range")
self.minValue = min_value
self.maxValue = max_value
self.unit = unit
class TimeRestriction:
def __init__(self, before_date=None, after_date=None):
self.beforDate = before_date
self.afterDate = after_date
class AttributeFilter(ValueFilter):
def __init__(self, attribute_code: TermCode, value_type):
self.type = value_type
self.attribute_code = attribute_code
class AttributeConceptFilter(AttributeFilter):
def __init__(self, attribute_code, selected_concepts=None):
super().__init__(attribute_code, "concept")
if selected_concepts is None:
selected_concepts = []
self.selectedConcepts = selected_concepts
class AttributeQuantityComparatorFilter(AttributeFilter):
def __init__(self, attribute_code, comparator, value, unit: Unit):
super().__init__(attribute_code, "quantity-comparator")
self.comparator = comparator
self.value = value
self.unit = unit
class AttributeQuantityRangeFilter(AttributeFilter):
def __init__(self, attribute_code, min_value, max_value, unit: Unit):
super().__init__(attribute_code, "quantity-range")
self.minValue = min_value
self.maxValue = max_value
self.unit = unit
class Criterion:
def __init__(self, term_codes, value_filter: ValueFilter, time_restriction: TimeRestriction = None, attribute_filter: AttributeFilter = None):
self.termCodes = term_codes
self.valueFilter = value_filter
self.timeRestriction = time_restriction
self.attributeFilters = attribute_filter if attribute_filter else []