Skip to content

Commit a41bf50

Browse files
committed
Refactoring and removed lookup functionality
1 parent 9c7e687 commit a41bf50

22 files changed

+178
-380
lines changed

delta_backend/src/conversion_checker.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import exception_messages
55
from datetime import datetime,timedelta, timezone
66
import re
7-
from utils import LookUpData
8-
97

108
# --------------------------------------------------------------------------------------------------------
119
# Custom error type to handle validation failures
@@ -31,11 +29,9 @@ class ConversionChecker:
3129
summarise = False
3230
report_unexpected_exception = True
3331
dataParser = any
34-
dataLookUp = any
3532

3633
def __init__(self, dataParser, summarise, report_unexpected_exception):
3734
self.dataParser = dataParser # FHIR data parser for additional functions
38-
self.dataLookUp = LookUpData() # used for generic look up
3935
self.summarise = summarise # instance attribute
4036
self.report_unexpected_exception = report_unexpected_exception # instance attribute
4137
self.errorRecords = [] # Store all errors here
@@ -75,10 +71,6 @@ def convertData(self, expressionType, expressionRule, fieldName, fieldValue):
7571
return self._convertToBoolean(
7672
expressionRule, fieldName, fieldValue, self.summarise, self.report_unexpected_exception
7773
)
78-
case "LOOKUP":
79-
return self._convertToLookUp(
80-
expressionRule, fieldName, fieldValue, self.summarise, self.report_unexpected_exception
81-
)
8274
case "SNOMED":
8375
return self._convertToSnomed(
8476
expressionRule, fieldName, fieldValue, self.summarise, self.report_unexpected_exception
@@ -241,21 +233,6 @@ def _convertToDose(self, expressionRule, fieldName, fieldValue, summarise, repor
241233
return fieldValue
242234
return ""
243235

244-
# Change to Lookup (loads expected data as is but if empty use lookup extraction to populate value)
245-
def _convertToLookUp(self, expressionRule, fieldName, fieldValue, summarise, report_unexpected_exception):
246-
if isinstance(fieldValue, str) and any(char.isalpha() for char in fieldValue) and not fieldValue.isdigit():
247-
return fieldValue
248-
try:
249-
lookUpValue = self.dataParser.getKeyValue(expressionRule)
250-
IdentifiedLookup = self.dataLookUp.findLookUp(lookUpValue[0])
251-
return IdentifiedLookup
252-
253-
except Exception as e:
254-
if report_unexpected_exception:
255-
message = exception_messages.MESSAGES[exception_messages.UNEXPECTED_EXCEPTION] % (e.__class__.__name__, e)
256-
self._log_error(fieldName, fieldValue, message)
257-
return ""
258-
259236
# Default to Validate
260237
def _convertToDefaultTo(self, expressionRule, fieldName, fieldValue, summarise, report_unexpected_exception):
261238
try:
@@ -273,7 +250,7 @@ def _convertToOnlyIfTo(self, expressionRule, fieldName, fieldValue, summarise, r
273250
conversionList = expressionRule.split("|")
274251
location = conversionList[0]
275252
valueCheck = conversionList[1]
276-
dataValue = self.dataParser.getKeyValue(location)
253+
dataValue = self.dataParser.get_key_value(location)
277254

278255
if dataValue[0] != valueCheck:
279256
return ""

delta_backend/src/delta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def handler(event, context):
7171
action_flag = "NEW" if operation == "CREATE" else operation
7272
resource_json = json.loads(new_image["Resource"]["S"])
7373
FHIRConverter = Converter(json.dumps(resource_json))
74-
flat_json = FHIRConverter.runConversion(resource_json) # Get the flat JSON
75-
error_records = FHIRConverter.getErrorRecords()
74+
flat_json = FHIRConverter.run_conversion() # Get the flat JSON
75+
error_records = FHIRConverter.get_error_records()
7676
flat_json["ACTION_FLAG"] = action_flag
7777
response = delta_table.put_item(
7878
Item={

delta_backend/src/delta_converter.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, fhir_data, summarise=False, report_unexpected_exception=True)
3131

3232
self.conversion_checker = ConversionChecker(self.data_parser, summarise, report_unexpected_exception)
3333
self.extractor = Extractor(self.data_parser)
34+
3435
except Exception as e:
3536
self._log_error(f"Initialization failed: [{e.__class__.__name__}] {e}")
3637

@@ -55,7 +56,10 @@ def _convertData(self, expression):
5556
expr_type = expression["expression"]["expressionType"]
5657
expr_rule = expression["expression"]["expressionRule"]
5758

58-
values = self.data_parser.getKeyValue(fhir_field, flat_field, expr_type, expr_rule)
59+
### TODO: Remove this after refactoring all fields to be extracted with the Extractor
60+
values = self.data_parser.get_key_value(fhir_field, flat_field, expr_type, expr_rule)
61+
###
62+
5963
for val in values:
6064
converted = self.conversion_checker.convertData(expr_type, expr_rule, fhir_field, val)
6165
if converted is not None:
@@ -66,11 +70,11 @@ def _convertData(self, expression):
6670

6771

6872
# run the conversion against the data
69-
def runConversion(self):
73+
def run_conversion(self):
7074
try:
71-
conversions = self.schema_parser.getConversions()
75+
conversions = self.schema_parser.get_conversions()
7276
except Exception as e:
73-
return self._log_error(f"Schema getConversions error [{e.__class__.__name__}]: {e}")
77+
return self._log_error(f"Schema get_conversions error [{e.__class__.__name__}]: {e}")
7478

7579
for conversion in conversions:
7680
self._convertData(conversion)
@@ -83,5 +87,5 @@ def runConversion(self):
8387
self.converted["CONVERSION_ERRORS"] = self.conversion_checker.get_error_records() + self.error_records
8488
return self.converted
8589

86-
def getErrorRecords(self):
90+
def get_error_records(self):
8791
return self.error_records

delta_backend/src/fhir_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def _scanForValue(self, FHIRFields, expression_type, expression_rule: str = ""):
9393
return rootfield
9494

9595
# get the value for a key
96-
def getKeyValue(self, fieldName, flatFieldName, expression_type: str = "", expression_rule = ""):
96+
def get_key_value(self, fieldName, flatFieldName, expression_type: str = "", expression_rule = ""):
9797
value = []
9898
try:
9999
# extract

delta_backend/src/json_field_extractor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _get_patient(self):
2121

2222
def _get_valid_names(self, names, occurrence_time):
2323

24-
official_names = [n for n in names if n.get("use") == "official" and self.is_current_period(n, occurrence_time)]
24+
official_names = [n for n in names if n.get("use") == "official" and self._is_current_period(n, occurrence_time)]
2525
if official_names:
2626
return official_names[0]
2727

@@ -239,10 +239,13 @@ def extract_dose_sequence(self) -> str:
239239

240240
def _get_occurance_date_time(self) -> str:
241241
try:
242+
#TODO: Double check if this logic is correct
242243
occurrence_time = datetime.fromisoformat(self.fhir_json_data.get("occurrenceDateTime", ""))
243244
if occurrence_time and occurrence_time.tzinfo is None:
244245
occurrence_time = occurrence_time.replace(tzinfo=timezone.utc)
245246
return occurrence_time
247+
return occurrence_time
248+
246249
except Exception as e:
247250
message = "DateTime conversion error [%s]: %s" % (e.__class__.__name__, e)
248251
error = self._log_error(message, code=exception_messages.UNEXPECTED_EXCEPTION)

delta_backend/src/schema_parser.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,5 @@ def parseSchema(self, schema_file): # changed to accept JSON better for cache
1111
self.schema_file = schema_file
1212
self.conversions = self.schema_file["conversions"]
1313

14-
def conversionCount(self):
15-
count = 0
16-
count = sum([1 for d in self.conversions if "conversion" in d])
17-
return count
18-
19-
def getConversions(self):
14+
def get_conversions(self):
2015
return self.conversions
21-
22-
def getConversion(self, conversion_number):
23-
conversion = self.conversions[conversion_number]
24-
return conversion

delta_backend/src/utils.py

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -18,113 +18,4 @@ def is_valid_simple_snomed(simple_snomed: str) -> bool:
1818
)
1919
except:
2020
return False
21-
22-
# ---------------------------------------------------------------------------------------------------------
23-
# main conversion lookup
24-
25-
26-
class LookUpData:
27-
# data settings
28-
allData = {
29-
"368208006": "Left upper arm structure",
30-
"279549004": "Nasal cavity structure",
31-
"74262004": "Oral cavity structure",
32-
"368209003": "Right upper arm structure",
33-
"723979003": "Structure of left buttock",
34-
"61396006": "Structure of left thigh",
35-
"723980000": "Structure of right buttock",
36-
"11207009": "Structure of right thigh",
37-
"420254004": "Body cavity",
38-
"54471007": "Buccal",
39-
"372449004": "Dental",
40-
"372450004": "Endocervical",
41-
"372451000": "Endosinusial",
42-
"372452007": "Endotracheopulmonary",
43-
"404820008": "Epidural",
44-
"18246711000001107": "Epilesional",
45-
"372453002": "Extraamniotic",
46-
"372454008": "Gastroenteral",
47-
"127490009": "Gastrostomy route",
48-
"372457001": "Gingival",
49-
"9191401000001100": "Haemodiafiltration",
50-
"18682911000001103": "Haemodialysis",
51-
"10334211000001103": "Haemofiltration",
52-
"718329006": "Infiltration",
53-
"18679011000001101": "Inhalation",
54-
"34777511000001106": "Intestinal use",
55-
"372458006": "Intraamniotic",
56-
"58100008": "Intraarterial",
57-
"12130007": "Intraarticular",
58-
"372459003": "Intrabursal",
59-
"418821007": "Intracameral use",
60-
"372460008": "Intracardiac",
61-
"372461007": "Intracavernous",
62-
"420719007": "Intracerebroventricular",
63-
"19537211000001108": "Intracervical route",
64-
"372463005": "Intracoronary",
65-
"372464004": "Intradermal",
66-
"372465003": "Intradiscal",
67-
"448077001": "Intraepidermal",
68-
"38233211000001106": "Intraglandular",
69-
"372466002": "Intralesional",
70-
"372467006": "Intralymphatic",
71-
"78421000": "Intramuscular",
72-
"255559005": "Intramuscular",
73-
"372468001": "Intraocular",
74-
"417255000": "Intraosseous",
75-
"38239002": "Intraperitoneal",
76-
"372469009": "Intrapleural",
77-
"372470005": "Intrasternal",
78-
"418586008": "Intratendinous route",
79-
"72607000": "Intrathecal",
80-
"447122006": "Intratumoral",
81-
"62226000": "Intrauterine",
82-
"47625008": "Intravenous",
83-
"420287000": "Intraventricular cardiac",
84-
"372471009": "Intravesical",
85-
"418401004": "Intravitreal",
86-
"21856811000001103": "Iontophoresis",
87-
"127491008": "Jejunostomy route",
88-
"9907001000001103": "Line lock",
89-
"46713006": "Nasal",
90-
"127492001": "Nasogastric route",
91-
"418730005": "Nasojejunal route",
92-
"54485002": "Ophthalmic route",
93-
"26643006": "Oral",
94-
"372473007": "Oromucosal",
95-
"10547007": "Otic",
96-
"225691000001105": "PEG tube route",
97-
"9191501000001101": "Percutaneous",
98-
"372474001": "Periarticular",
99-
"39338211000001108": "Peribulbar ocular",
100-
"3323001000001107": "Pericardial route",
101-
"372475000": "Perineural",
102-
"11478901000001102": "Periosseous",
103-
"419762003": "Peritendinous route",
104-
"39337511000001107": "Peritumoral",
105-
"37161004": "Rectal",
106-
"11564311000001109": "Regional perfusion",
107-
"418321004": "Retrobulbar route",
108-
"3594011000001102": "Route of administration not applicable",
109-
"372476004": "Subconjunctival",
110-
"34206005": "Subcutaneous",
111-
"37839007": "Sublingual",
112-
"419874009": "Submucosal",
113-
"11564211000001101": "Submucosal rectal",
114-
"33770711000001104": "Subretinal",
115-
"6064005": "Topical",
116-
"45890007": "Transdermal",
117-
"11479001000001107": "Translingual",
118-
"404815008": "Transmucosal",
119-
"90028008": "Urethral",
120-
"16857009": "Vaginal",
121-
}
122-
123-
# Look up the term for the code
124-
def findLookUp(self, fieldValue):
125-
try:
126-
lookUpValue = self.allData[fieldValue]
127-
except:
128-
lookUpValue = ""
129-
return lookUpValue
130-
21+

delta_backend/tests/check_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# Run the converter
1717
converter = Converter(json_data)
18-
result = converter.runConversion()
18+
result = converter.run_conversion()
1919

2020
# Absolute path to /delta_backend
2121
output_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

delta_backend/tests/sample_data/ConversionLayout.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
"fieldNameFlat": "SITE_OF_VACCINATION_TERM",
235235
"expression": {
236236
"expressionName": "Look Up",
237-
"expressionType": "LOOKUP",
237+
"expressionType": "NORMAL",
238238
"expressionRule": "site.coding.0.code"
239239
}
240240
},
@@ -252,7 +252,7 @@
252252
"fieldNameFlat": "ROUTE_OF_VACCINATION_TERM",
253253
"expression": {
254254
"expressionName": "Look Up",
255-
"expressionType": "LOOKUP",
255+
"expressionType": "NORMAL",
256256
"expressionRule": "route.coding.0.code"
257257
}
258258
},

delta_backend/tests/test_convert_dose_sequence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setUp(self):
1313
def _run_test(self, expected_result):
1414
"""Helper function to run the test"""
1515
self.converter = Converter(json.dumps(self.request_json_data))
16-
flat_json = self.converter.runConversion()
16+
flat_json = self.converter.run_conversion()
1717
self.assertEqual(flat_json["DOSE_SEQUENCE"], expected_result)
1818

1919
def test_dose_sequence_present_int(self):

0 commit comments

Comments
 (0)