Passed
Branch DICOMAnonimization (c59297)
by Yang
02:05
created

DICOM.DICOM_computeScanAge()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 1
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
import pydicom
2
import sys
3
import os
4
import argparse
5
import getpass
6
import logging
7
from pydicom.data import get_testdata_files
8
9
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
10
11
def DICOM_decompressor(path):
12
    """
13
    Decompress the dicoms in case they are compressed.
14
    :param path:
15
    :return:
16
    """
17
18
    ds = pydicom.dcmread(path)
19
20
    ds.decompress()
21
    ds.save_as("Test.dcm")
22
23
24
def DICOM_anonymizer(path, PSCID):
25
    """
26
    Anonymize the DICOMS to remove any identifiable information
27
    :param path:
28
    :param PSCID:
29
    :return:
30
    """
31
32
    return DICOM_anonymizer_save_as(path, PSCID, path)
33
34
35
def DICOM_anonymizer_save_as(path, PSCID, out_path):
36
    """
37
    Anonymize the DICOMS to remove any identifiable information
38
    :param path:
39
    :param PSCID:
40
    :return:
41
    """
42
43
    success, dataset = DICOM_validator(path)
44
    if not success:
45
        return False
46
47
    dataset.PatientID = PSCID
48
    dataset.PatientName = PSCID
49
    dataset.save_as(out_path)
50
51
    return True
52
53
54
def DICOM_recursive_loader(path, action):
55
    """
56
    load all the files, validate and then pass to decompress or anonimize.
57
    :param path:
58
    :return:
59
    """
60
61
62
def DICOM_validator(file_path):
63
    """
64
    validate to check if the DICOM file is an actual DICOM file.
65
    :param file_path:
66
    :return:
67
    """
68
    logger = logging.getLogger(__name__)
69
70
    global dicom
71
    dicom = None
72
73
    from pydicom.filereader import InvalidDicomError
74
    from pydicom.filereader import read_file
75
    try:
76
        dicom = read_file(file_path)
77
    except InvalidDicomError:
78
        logger.info(file_path + " is not a DICOM file. Skipping")
79
        return False, None
80
    return True, dicom
81
82
83
def DICOM_batchValidator(dir_path):
84
    """
85
    Some basic information of the participants must be consistent across the files, such as the SCAN DATE (assuming they are not scanning across MIDNIGHT POINT)
86
    Birthday date, subject name, etc MUST BE CONSISTENT across a SINGLE subject's folder, RIGHT!
87
88
    :param dir_path:
89
    :return:
90
    """
91
92
93
def DICOM_TransferSyntax(file_path):
94
    """
95
    Used to find if a file is compressed
96
    :param file_path:
97
    :return:
98
    """
99
    from pydicom.filereader import read_file_meta_info
100
101
    # Validity check:
102
    success, DICOM = DICOM_validator(file_path)
103
    if not success:
104
        raise IOError
105
106
    # Now read the meta information.
107
    dicom_file = read_file_meta_info(file_path)
108
    transfer_syntax = dicom_file.TransferSyntaxUID
109
110
    return transfer_syntax
111
112
113
def DICOM_computeScanAge(file_path):
114
    """
115
    Read the PatientID field which normally used as MRN number.
116
    :param file_path:
117
    :return: Age as a relative delta time object.
118
    """
119
120
    from dateutil.relativedelta import relativedelta
121
122
    success, DICOM = DICOM_validator(file_path)
123
    if not success:
124
        return False, None
125
    from datetime import datetime
126
    scan_date = datetime.strptime(DICOM.SeriesDate, "%Y%m%d")
127
    birthday = datetime.strptime(DICOM.PatientBirthDate, "%Y%m%d")
128
    age = relativedelta(scan_date, birthday)
129
    return True, age
130
131
132
def DICOM_retrieveMRN(file_path):
133
    """
134
    Read the PatientID field which normally used as MRN number.
135
    :param file_path:
136
    :return: MRN number, as a STRING
137
    """
138
    success, DICOM = DICOM_validator(file_path)
139
    if not success:
140
        return False, None
141
142
    MRN_number = DICOM.PatientID
143
    return True, MRN_number
144
145
146
def DICOM_TransferSyntaxCheck(transfer_syntax):
147
    """
148
    Determine if the transfer syntax symbolize LEE or JPEG compressed!
149
    :param transfer_syntax:
150
    :return: whether the DICOM files are compressed.
151
    """
152
153
    if not ("1.2.840.10008.1.2" in transfer_syntax):
154
        raise ValueError
155
    elif transfer_syntax == "1.2.840.10008.1.2" or transfer_syntax[18] == '1' or transfer_syntax[18] == '2':
156
        return True
157
    elif transfer_syntax[18] == '4' or transfer_syntax[18] == '5' or transfer_syntax[18] == '6':
158
        return False
159
    else:
160
        raise ValueError
161
162
163
if __name__ == '__main__':
164
    DICOM_decompressor("0000000A")
165
166
167