|
1
|
|
|
import os |
|
|
|
|
|
|
2
|
|
|
import sys |
|
3
|
|
|
import logging |
|
4
|
|
|
import unittest |
|
5
|
|
|
|
|
6
|
|
|
from DICOM.decompress import DICOM_decompress |
|
|
|
|
|
|
7
|
|
|
from DICOM.anonymize import DICOM_anonymize |
|
|
|
|
|
|
8
|
|
|
from DICOM.elements import DICOM_elements |
|
|
|
|
|
|
9
|
|
|
from pydicom.data import get_testdata_files |
|
10
|
|
|
|
|
11
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def get_test_DICOM_path(): |
|
|
|
|
|
|
15
|
|
|
filename = get_testdata_files("rtplan.dcm")[0] |
|
16
|
|
|
return filename |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class UT_DICOMManipulation(unittest.TestCase): |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
@staticmethod |
|
22
|
|
|
def test_DICOM_decompress(): |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
logger = logging.getLogger(__name__) |
|
25
|
|
|
|
|
26
|
|
|
# Get all test files with "JPEG" in them. |
|
27
|
|
|
file_names = get_testdata_files("[Jj][Pp][Ee][Gg]") |
|
28
|
|
|
|
|
29
|
|
|
logger.info("List of files to try to decompress:") |
|
30
|
|
|
logger.info(file_names) |
|
31
|
|
|
|
|
32
|
|
|
success_counter = 0 |
|
33
|
|
|
fail_counter = 0 |
|
34
|
|
|
|
|
35
|
|
|
for file in file_names: |
|
36
|
|
|
logger.info("Processing: " + file) |
|
37
|
|
|
|
|
38
|
|
|
decompressed_file = file+"DECOM" |
|
39
|
|
|
|
|
40
|
|
|
success, reason = DICOM_decompress.save_as(file, decompressed_file) |
|
41
|
|
|
|
|
42
|
|
|
logger.info(reason) |
|
43
|
|
|
|
|
44
|
|
|
if success: |
|
45
|
|
|
success_counter += 1 |
|
46
|
|
|
os.remove(decompressed_file) # restore the test environment to its former state. |
|
47
|
|
|
logger.info("Successful.") |
|
48
|
|
|
else: |
|
49
|
|
|
fail_counter += 1 |
|
50
|
|
|
logger.info("Failed. Reason:" + reason) |
|
51
|
|
|
assert success_counter == 7 # within the test data folder, there should be SEVEN files with JPEG in their name that CAN be successfully loaded. |
|
|
|
|
|
|
52
|
|
|
assert fail_counter == 4 # within the test data folder, there should be FOUR files with JPEG in their name that CANNOT be successfully loaded. |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
@staticmethod |
|
55
|
|
|
def test_DICOM_RequireDecompression(): |
|
|
|
|
|
|
56
|
|
|
assert not (DICOM_decompress.check_decompression('1.2.840.10008.1.2')) |
|
|
|
|
|
|
57
|
|
|
assert not (DICOM_decompress.check_decompression('1.2.840.10008.1.2.1')) |
|
|
|
|
|
|
58
|
|
|
assert not (DICOM_decompress.check_decompression('1.2.840.10008.1.2.2')) |
|
|
|
|
|
|
59
|
|
|
assert (DICOM_decompress.check_decompression('1.2.840.10008.1.2.4')) |
|
|
|
|
|
|
60
|
|
|
assert (DICOM_decompress.check_decompression('1.2.840.10008.1.2.4.57')) |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def test_wrong_syntax(self): |
|
|
|
|
|
|
65
|
|
|
self.assertRaises(ValueError, DICOM_decompress.check_decompression, 'FakeTest') |
|
66
|
|
|
|
|
67
|
|
|
@staticmethod |
|
68
|
|
|
def test_DICOM_anonymizer(): |
|
|
|
|
|
|
69
|
|
|
file_names = get_testdata_files("emri") |
|
70
|
|
|
for file_name in file_names: |
|
71
|
|
|
success = DICOM_anonymize.save(file_name, "CNBP0010001") |
|
72
|
|
|
assert success |
|
73
|
|
|
for file_name in file_names: |
|
74
|
|
|
success, value = DICOM_elements.retrieve(file_name, "PatientID") |
|
75
|
|
|
assert success |
|
76
|
|
|
assert (value == "CNBP0010001") |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
@staticmethod |
|
79
|
|
|
def test_DICOM_retrieveMRN(): |
|
|
|
|
|
|
80
|
|
|
path = get_testdata_files("emri_small_big_endian")[0] |
|
81
|
|
|
success, MRN = DICOM_elements.retrieveMRN(path) |
|
|
|
|
|
|
82
|
|
|
assert success |
|
83
|
|
|
assert (MRN == 'CNBP0010001') |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
@staticmethod |
|
86
|
|
|
def test_DICOM_update(): |
|
|
|
|
|
|
87
|
|
|
path = get_testdata_files("emri") |
|
88
|
|
|
for file in path: |
|
89
|
|
|
success, _ = DICOM_elements.update(file, "PatientBirthDate", "19950101", file) |
|
90
|
|
|
assert success |
|
91
|
|
|
|
|
92
|
|
|
for file in path: |
|
93
|
|
|
success, value = DICOM_elements.retrieve(file, "PatientBirthDate") |
|
94
|
|
|
assert success |
|
95
|
|
|
assert(value == "19950101") |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
@staticmethod |
|
98
|
|
|
def test_DICOM_computerScanAge(): |
|
|
|
|
|
|
99
|
|
|
logger = logging.getLogger("DICOM compute age") |
|
100
|
|
|
path = get_testdata_files("emri_small_RLE")[0] |
|
101
|
|
|
success, Age = DICOM_elements.computeScanAge(path) |
|
|
|
|
|
|
102
|
|
|
assert success |
|
103
|
|
|
logger.info(Age.day) |
|
104
|
|
|
|
|
105
|
|
|
@staticmethod |
|
106
|
|
|
def test_DICOM_check_dependency(): |
|
|
|
|
|
|
107
|
|
|
logger = logging.getLogger("Checking vital system path dependencies:") |
|
108
|
|
|
import subprocess |
|
109
|
|
|
|
|
110
|
|
|
try: |
|
111
|
|
|
# SUPER IMPORTANT! MAKE SURE DCMDJPEG is in the system path! |
|
112
|
|
|
subprocess.check_output(['dcmdjpeg']) |
|
113
|
|
|
# When dcmdjpeg has errors |
|
114
|
|
|
except Exception as e: |
|
|
|
|
|
|
115
|
|
|
logger.info(e) |
|
116
|
|
|
ErrorMessage = "DCMDJPEG decompression call failed! Make sure DCMDJPEG is in your SYSTEMOS PATH. " |
|
|
|
|
|
|
117
|
|
|
logger.info(ErrorMessage) |
|
118
|
|
|
raise ImportError |
|
119
|
|
|
|
|
120
|
|
|
try: |
|
121
|
|
|
# SUPER IMPORTANT! MAKE SURE DCMDJPEG is in the system path! |
|
122
|
|
|
subprocess.check_output(['dcm2niix']) |
|
123
|
|
|
# When dcmdjpeg has errors |
|
124
|
|
|
except Exception as e: |
|
|
|
|
|
|
125
|
|
|
logger.info(e) |
|
126
|
|
|
ErrorMessage = "DCMDJPEG decompression call failed! Make sure DCMDJPEG is in your SYSTEMOS PATH. " |
|
|
|
|
|
|
127
|
|
|
logger.info(ErrorMessage) |
|
128
|
|
|
raise ImportError |
|
129
|
|
|
|
|
130
|
|
|
assert(True) |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
if __name__ == '__main__': |
|
133
|
|
|
UT_DICOMManipulation.test_DICOM_check_dependency() |
|
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.