|
1
|
|
|
import logging |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from DICOM.validate import DICOM_validator |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
def DICOM_retrieveElements(file_path, data_element): |
|
|
|
|
|
|
7
|
|
|
""" |
|
8
|
|
|
A low level function used to retrieve elements from DICOM and return a LIST of matching element. ACCEPT PARTIAL MATCH |
|
|
|
|
|
|
9
|
|
|
:param file_path: |
|
10
|
|
|
:param data_element: |
|
11
|
|
|
:return: LIST of all data elements that match the pattern provided in the data_element and their value. NO Regular EXPRESSION. |
|
|
|
|
|
|
12
|
|
|
""" |
|
13
|
|
|
success, DICOM = DICOM_validator(file_path) |
|
|
|
|
|
|
14
|
|
|
if not success: |
|
15
|
|
|
return False, None, None |
|
16
|
|
|
|
|
17
|
|
|
# Get a list of all data elements that can have element label. |
|
18
|
|
|
element_values = DICOM.data_element(data_element).value |
|
19
|
|
|
|
|
20
|
|
|
return True, element_values |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def DICOM_updateElement(file_path, data_element, element_value, out_path): |
|
|
|
|
|
|
24
|
|
|
""" |
|
25
|
|
|
Update a particular data_element to the desired value, then write back to the SOURCE FILE! |
|
26
|
|
|
:param file_path: |
|
27
|
|
|
:param data_element: |
|
28
|
|
|
:param element_value: |
|
29
|
|
|
:return: bool on operation success, and string on reason. |
|
30
|
|
|
""" |
|
31
|
|
|
|
|
32
|
|
|
"""BE AWARE that if the key does not exist, it will not be created currently!""" |
|
|
|
|
|
|
33
|
|
|
logger = logging.getLogger(__name__) |
|
34
|
|
|
success, DICOM = DICOM_validator(file_path) |
|
|
|
|
|
|
35
|
|
|
if not success: |
|
36
|
|
|
return False, "DICOM not valid." |
|
37
|
|
|
|
|
38
|
|
|
try: |
|
39
|
|
|
DICOM.data_element(data_element).value = element_value |
|
40
|
|
|
except KeyError: |
|
41
|
|
|
logger.info("Key " + data_element + " does not exist, creating the key.") |
|
42
|
|
|
return False, "DICOM key field does not exist. Not sure how to create one yet. " |
|
43
|
|
|
DICOM.save_as(out_path) |
|
44
|
|
|
return True, "Data element update completed." |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def DICOM_retrieveMRN(file_path): |
|
|
|
|
|
|
48
|
|
|
""" |
|
49
|
|
|
Read the PatientID field which normally used as MRN number. |
|
50
|
|
|
:param file_path: |
|
51
|
|
|
:return: MRN number, as a STRING |
|
52
|
|
|
""" |
|
53
|
|
|
success, value = DICOM_retrieveElements(file_path, "PatientID") |
|
54
|
|
|
|
|
55
|
|
|
if not success: |
|
|
|
|
|
|
56
|
|
|
return False, None |
|
57
|
|
|
else: |
|
58
|
|
|
return True, value |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
def DICOM_computeScanAge(file_path): |
|
|
|
|
|
|
62
|
|
|
""" |
|
63
|
|
|
Read the PatientID field which normally used as MRN number. |
|
64
|
|
|
:param file_path: |
|
65
|
|
|
:return: Age as a relative delta time object. |
|
66
|
|
|
""" |
|
67
|
|
|
|
|
68
|
|
|
from dateutil.relativedelta import relativedelta |
|
69
|
|
|
|
|
70
|
|
|
success, DICOM = DICOM_validator(file_path) |
|
|
|
|
|
|
71
|
|
|
if not success: |
|
72
|
|
|
return False, None |
|
73
|
|
|
from datetime import datetime |
|
74
|
|
|
scan_date = datetime.strptime(DICOM.SeriesDate, "%Y%m%d") |
|
75
|
|
|
birthday = datetime.strptime(DICOM.PatientBirthDate, "%Y%m%d") |
|
76
|
|
|
age = relativedelta(scan_date, birthday) |
|
77
|
|
|
return True, age |
|
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.