-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathextractor.py
More file actions
445 lines (359 loc) · 16.5 KB
/
extractor.py
File metadata and controls
445 lines (359 loc) · 16.5 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import decimal
import json
from datetime import UTC, datetime, timedelta
import exception_messages
from mappings import ConversionFieldName, Gender
class Extractor:
# This file holds the schema/base layout that maps FHIR fields to flat JSON fields
# Each entry tells the converter how to extract and transform a specific value
EXTENSION_URL_VACCINATION_PRODEDURE = (
"https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure"
)
EXTENSION_URL_SCT_DESC_DISPLAY = "https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-CodingSCTDescDisplay"
CODING_SYSTEM_URL_SNOMED = "http://snomed.info/sct"
ODS_ORG_CODE_SYSTEM_URL = "https://fhir.nhs.uk/Id/ods-organization-code"
DEFAULT_LOCATION = "X99999"
NHS_NUMBER_SYSTEM_URL = "https://fhir.nhs.uk/Id/nhs-number"
DATE_CONVERT_FORMAT = "%Y%m%d"
DEFAULT_POSTCODE = "ZZ99 3CZ"
def __init__(self, fhir_json_data):
self.fhir_json_data = (
json.loads(fhir_json_data, parse_float=decimal.Decimal)
if isinstance(fhir_json_data, str)
else fhir_json_data
)
self.error_records = []
def _get_patient(self):
contained = self.fhir_json_data.get("contained", [])
return next(
(c for c in contained if isinstance(c, dict) and c.get("resourceType") == "Patient"),
"",
)
def _get_valid_names(self, names, occurrence_time):
official_names = [n for n in names if n.get("use") == "official" and self._is_current_period(n, occurrence_time)]
if official_names:
return official_names[0]
valid_names = [n for n in names if self._is_current_period(n, occurrence_time) and n.get("use") != "old"]
if valid_names:
return valid_names[0]
return names[0]
def _get_person_names(self):
occurrence_time = self._get_occurrence_date_time()
patient = self._get_patient()
names = patient.get("name", [])
names = [n for n in names if "given" in n and "family" in n]
if not names:
return "", ""
selected_name = self._get_valid_names(names, occurrence_time)
person_forename = " ".join(selected_name.get("given", []))
person_surname = selected_name.get("family", "")
if person_forename and person_surname:
return person_forename, person_surname
return "", ""
def _get_practitioner_names(self):
contained = self.fhir_json_data.get("contained", [])
occurrence_time = self._get_occurrence_date_time()
practitioner = next(
(c for c in contained if isinstance(c, dict) and c.get("resourceType") == "Practitioner"),
None,
)
if not practitioner or "name" not in practitioner:
return "", ""
practitioner_names = practitioner.get("name", [])
valid_practitioner_names = [n for n in practitioner_names if "given" in n or "family" in n]
if not valid_practitioner_names:
return "", ""
selected_practitioner_name = self._get_valid_names(valid_practitioner_names, occurrence_time)
performing_professional_forename = " ".join(selected_practitioner_name.get("given", []))
performing_professional_surname = selected_practitioner_name.get("family", "")
return performing_professional_forename, performing_professional_surname
def _is_current_period(self, name, occurrence_time):
period = name.get("period")
if not isinstance(period, dict):
return True # If no period is specified, assume it's valid
start = datetime.fromisoformat(period.get("start")) if period.get("start") else None
end_str = period.get("end")
end = datetime.fromisoformat(period.get("end")) if end_str else None
# Ensure all datetime objects are timezone-aware
if start and start.tzinfo is None:
start = start.replace(tzinfo=UTC)
if end and "T" not in end_str:
# If end is a date-only string like "2025-06-12", upgrade to full end-of-day
end = end.replace(hour=23, minute=59, second=59, microsecond=999999)
if end and end.tzinfo is None:
# If end still has no timezone info, assign UTC
end = end.replace(tzinfo=UTC)
return (not start or start <= occurrence_time) and (not end or occurrence_time <= end)
def _get_occurrence_date_time(self) -> datetime:
occurrence_time = datetime.fromisoformat(self.fhir_json_data.get("occurrenceDateTime", ""))
if occurrence_time and occurrence_time.tzinfo is None:
occurrence_time = occurrence_time.replace(tzinfo=UTC)
return occurrence_time
def _get_first_snomed_code(self, coding_container: dict) -> str:
codings = coding_container.get("coding", [])
for coding in codings:
if coding.get("system") == self.CODING_SYSTEM_URL_SNOMED:
return coding.get("code", "")
return ""
def _get_codeable_term(self, concept: dict) -> str:
if concept.get("text"):
return concept["text"]
codings = concept.get("coding", [])
for coding in codings:
if coding.get("system") == self.CODING_SYSTEM_URL_SNOMED:
return self._get_snomed_display(coding)
return ""
def _get_snomed_display(self, coding: dict) -> str:
for ext in coding.get("extension", []):
if ext.get("url") == self.EXTENSION_URL_SCT_DESC_DISPLAY:
value_string = ext.get("valueString")
if value_string:
return value_string
return coding.get("display", "")
def _get_site_information(self):
performers = self.fhir_json_data.get("performer", [])
if not isinstance(performers, list) or not performers:
return "", ""
valid_performers = [p for p in performers if "actor" in p and "identifier" in p["actor"]]
if not valid_performers:
return "", ""
selected_performer = next(
(
p
for p in valid_performers
if p.get("actor", {}).get("type") == "Organization"
and p.get("actor", {}).get("identifier", {}).get("system") == self.ODS_ORG_CODE_SYSTEM_URL
),
next(
(
p
for p in valid_performers
if p.get("actor", {}).get("identifier", {}).get("system") == self.ODS_ORG_CODE_SYSTEM_URL
),
next(
(p for p in valid_performers if p.get("actor", {}).get("type") == "Organization"),
valid_performers[0] if valid_performers else "",
),
),
)
site_code = selected_performer["actor"].get("identifier", {}).get("value")
site_code_type_uri = selected_performer["actor"].get("identifier", {}).get("system")
return site_code, site_code_type_uri
def _log_error(self, field_name, field_value, e, code=exception_messages.RECORD_CHECK_FAILED):
message = (
exception_messages.MESSAGES[exception_messages.UNEXPECTED_EXCEPTION] % (e.__class__.__name__, str(e))
if isinstance(e, Exception)
else str(e)
)
self.error_records.append(
{
"code": code,
"field": field_name,
"value": field_value,
"message": message,
}
)
def _convert_date(self, field_name, date, format) -> str:
"""
Convert a date string according to match YYYYMMDD format.
"""
if not date:
return ""
try:
dt = datetime.fromisoformat(date)
return dt.strftime(format)
except ValueError as e:
self._log_error(field_name, date, e)
return ""
def _convert_date_to_safe_format(self, field_name, date) -> str:
try:
dt = datetime.fromisoformat(date)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=UTC)
except Exception as e:
self._log_error(field_name, date, e)
return ""
# Allow only +00:00 or +01:00 offsets (UTC and BST) and reject unsupported timezones
offset = dt.utcoffset()
allowed_offsets = [timedelta(hours=0), timedelta(hours=1)]
if offset is not None and offset not in allowed_offsets:
self._log_error(field_name, date, "Unsupported Format or offset")
return ""
# remove microseconds
dt_format = dt.replace(microsecond=0)
formatted = dt_format.strftime("%Y%m%dT%H%M%S%z")
return formatted.replace("+0000", "00").replace("+0100", "01")
def extract_nhs_number(self):
patient = self._get_patient()
if patient:
identifier_list = patient.get("identifier", [])
for identifier in identifier_list:
if identifier.get("system", "") == self.NHS_NUMBER_SYSTEM_URL:
return identifier.get("value", "")
return ""
def extract_person_forename(self):
return self._get_person_names()[0]
def extract_person_surname(self):
return self._get_person_names()[1]
def extract_person_dob(self):
patient = self._get_patient()
if patient:
dob = patient.get("birthDate", "")
return self._convert_date(ConversionFieldName.PERSON_DOB, dob, self.DATE_CONVERT_FORMAT)
return ""
def extract_person_gender(self):
patient = self._get_patient()
if patient:
gender = patient.get("gender", "").upper()
try:
return Gender[gender].value
except KeyError:
return ""
return ""
def normalize(self, value):
return value.lower() if isinstance(value, str) else value
def extract_valid_address(self):
occurrence_time = self._get_occurrence_date_time()
patient = self._get_patient()
addresses = patient.get("address", [])
if not isinstance(addresses, list) or not addresses:
return self.DEFAULT_POSTCODE
if len(addresses) == 1:
return addresses[0].get("postalCode") or self.DEFAULT_POSTCODE
if not (
valid_addresses := [
addr for addr in addresses if addr.get("postalCode") and self._is_current_period(addr, occurrence_time)
]
):
return self.DEFAULT_POSTCODE
selected_address = (
next(
(
a
for a in valid_addresses
if self.normalize(a.get("use")) == "home" and self.normalize(a.get("type")) != "postal"
),
None,
)
or next(
(
a
for a in valid_addresses
if self.normalize(a.get("use")) != "old" and self.normalize(a.get("type")) != "postal"
),
None,
)
or next(
(a for a in valid_addresses if self.normalize(a.get("use")) != "old"),
None,
)
or valid_addresses[0]
)
return selected_address.get("postalCode") or self.DEFAULT_POSTCODE
def extract_date_time(self) -> str:
date = self.fhir_json_data.get("occurrenceDateTime", "")
if date:
return self._convert_date_to_safe_format(ConversionFieldName.DATE_AND_TIME, date)
return ""
def extract_site_code(self):
return self._get_site_information()[0]
def extract_site_code_type_uri(self):
return self._get_site_information()[1]
def extract_unique_id(self):
identifier = self.fhir_json_data.get("identifier", [])
if identifier:
return identifier[0].get("value", "")
return ""
def extract_unique_id_uri(self):
identifier = self.fhir_json_data.get("identifier", [])
if identifier and len(identifier) == 1:
return identifier[0].get("system", "")
return ""
def extract_practitioner_forename(self):
return self._get_practitioner_names()[0]
def extract_practitioner_surname(self):
return self._get_practitioner_names()[1]
def extract_recorded_date(self) -> str:
date = self.fhir_json_data.get("recorded", "")
return self._convert_date(ConversionFieldName.RECORDED_DATE, date, self.DATE_CONVERT_FORMAT)
def extract_primary_source(self) -> bool | str:
primary_source = self.fhir_json_data.get("primarySource")
if isinstance(primary_source, bool):
return str(primary_source).upper()
return ""
def extract_vaccination_procedure_code(self) -> str:
extensions = self.fhir_json_data.get("extension", [])
for ext in extensions:
if ext.get("url") == self.EXTENSION_URL_VACCINATION_PRODEDURE:
value_cc = ext.get("valueCodeableConcept", {})
return self._get_first_snomed_code(value_cc)
return ""
def extract_vaccination_procedure_term(self) -> str:
extensions = self.fhir_json_data.get("extension", [])
for ext in extensions:
if ext.get("url") == self.EXTENSION_URL_VACCINATION_PRODEDURE:
return self._get_codeable_term(ext.get("valueCodeableConcept", {}))
return ""
def extract_dose_sequence(self) -> str:
protocol_applied = self.fhir_json_data.get("protocolApplied", [])
if protocol_applied:
dose = protocol_applied[0].get("doseNumberPositiveInt", None)
return str(dose) if dose else ""
return ""
def extract_vaccine_product_code(self) -> str:
vaccine_code = self.fhir_json_data.get("vaccineCode", {})
return self._get_first_snomed_code(vaccine_code)
def extract_vaccine_product_term(self) -> str:
return self._get_codeable_term(self.fhir_json_data.get("vaccineCode", {}))
def extract_vaccine_manufacturer(self) -> str:
manufacturer = self.fhir_json_data.get("manufacturer", {})
if manufacturer:
return manufacturer.get("display", "")
return ""
def extract_batch_number(self) -> str:
return self.fhir_json_data.get("lotNumber", "")
def extract_expiry_date(self) -> str:
date = self.fhir_json_data.get("expirationDate", "")
return self._convert_date(ConversionFieldName.EXPIRY_DATE, date, self.DATE_CONVERT_FORMAT)
def extract_site_of_vaccination_code(self) -> str:
site = self.fhir_json_data.get("site", {})
return self._get_first_snomed_code(site)
def extract_site_of_vaccination_term(self) -> str:
return self._get_codeable_term(self.fhir_json_data.get("site", {}))
def extract_route_of_vaccination_code(self) -> str:
route = self.fhir_json_data.get("route", {})
return self._get_first_snomed_code(route)
def extract_route_of_vaccination_term(self) -> str:
return self._get_codeable_term(self.fhir_json_data.get("route", {}))
def extract_dose_amount(self) -> str:
dose_quantity = self.fhir_json_data.get("doseQuantity", {})
return dose_quantity.get("value", "")
def extract_dose_unit_code(self) -> str:
dose_quantity = self.fhir_json_data.get("doseQuantity", {})
if dose_quantity.get("system") == self.CODING_SYSTEM_URL_SNOMED and dose_quantity.get("code"):
return dose_quantity.get("code")
return ""
def extract_dose_unit_term(self) -> str:
dose_quantity = self.fhir_json_data.get("doseQuantity", {})
return dose_quantity.get("unit", "")
def extract_indication_code(self) -> str:
for reason in self.fhir_json_data.get("reasonCode", []):
codings = reason.get("coding", [])
for coding in codings:
if coding.get("system") == self.CODING_SYSTEM_URL_SNOMED:
return coding.get("code", "")
return ""
def extract_location_code(self) -> str:
location = self.fhir_json_data.get("location", {})
if location:
identifier = location.get("identifier", {})
return identifier.get("value", self.DEFAULT_LOCATION)
return self.DEFAULT_LOCATION
def extract_location_code_type_uri(self) -> str:
location = self.fhir_json_data.get("location", {})
if location:
identifier = location.get("identifier", {})
return identifier.get("system", self.ODS_ORG_CODE_SYSTEM_URL)
return self.ODS_ORG_CODE_SYSTEM_URL
def get_error_records(self):
return self.error_records