|
1
|
|
|
import os |
|
|
|
|
|
|
2
|
|
|
import sys |
|
3
|
|
|
import logging |
|
4
|
|
|
import unittest |
|
5
|
|
|
|
|
6
|
|
|
from DICOM.validate import DICOM_validator |
|
|
|
|
|
|
7
|
|
|
from DICOM.decompress import DICOM_RequireDecompression, DICOM_decompress |
|
|
|
|
|
|
8
|
|
|
from DICOM.anonymize import DICOM_anonymizer |
|
|
|
|
|
|
9
|
|
|
from DICOM.elements import DICOM_retrieveElements, DICOM_updateElement, DICOM_retrieveMRN, DICOM_computeScanAge |
|
|
|
|
|
|
10
|
|
|
from pydicom.data import get_testdata_files |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
14
|
|
|
|
|
15
|
|
|
def get_test_DICOM_path(): |
|
|
|
|
|
|
16
|
|
|
filename = get_testdata_files("rtplan.dcm")[0] |
|
17
|
|
|
return filename |
|
18
|
|
|
|
|
19
|
|
|
def test_DICOM_decompress(): |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
logger = logging.getLogger(__name__) |
|
22
|
|
|
|
|
23
|
|
|
# Get all test files with "JPEG" in them. |
|
24
|
|
|
file_names = get_testdata_files("[Jj][Pp][Ee][Gg]") |
|
25
|
|
|
|
|
26
|
|
|
logger.info("List of files to try to decompress:") |
|
27
|
|
|
logger.info(file_names) |
|
28
|
|
|
|
|
29
|
|
|
success_counter = 0 |
|
30
|
|
|
fail_counter = 0 |
|
31
|
|
|
|
|
32
|
|
|
for file in file_names: |
|
33
|
|
|
logger.info("Processing: " + file) |
|
34
|
|
|
|
|
35
|
|
|
decompressed_file = file+"DECOM" |
|
36
|
|
|
|
|
37
|
|
|
success, reason = DICOM_decompress(file, decompressed_file) |
|
38
|
|
|
|
|
39
|
|
|
logger.info(reason) |
|
40
|
|
|
|
|
41
|
|
|
if success: |
|
42
|
|
|
success_counter += 1 |
|
43
|
|
|
os.remove(decompressed_file) # restore the test environment to its former state. |
|
44
|
|
|
logger.info("Successful.") |
|
45
|
|
|
else: |
|
46
|
|
|
fail_counter += 1 |
|
47
|
|
|
logger.info("Failed. Reason:" + reason) |
|
48
|
|
|
assert success_counter == 7 # within the test data folder, there should be SEVEN files with JPEG in their name that CAN be successfully loaded. |
|
|
|
|
|
|
49
|
|
|
assert fail_counter == 4 # within the test data folder, there should be FOUR files with JPEG in their name that CANNOT be successfully loaded. |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def test_DICOM_validator(): |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
file_name = get_test_DICOM_path() |
|
54
|
|
|
success, data = DICOM_validator(file_name) |
|
|
|
|
|
|
55
|
|
|
assert success |
|
56
|
|
|
|
|
57
|
|
|
file_name = get_testdata_files("README.txt")[0] |
|
58
|
|
|
success, data = DICOM_validator(file_name) |
|
59
|
|
|
assert not success |
|
60
|
|
|
|
|
61
|
|
|
def test_DICOM_RequireDecompression(): |
|
|
|
|
|
|
62
|
|
|
assert not (DICOM_RequireDecompression('1.2.840.10008.1.2')) |
|
|
|
|
|
|
63
|
|
|
assert not (DICOM_RequireDecompression('1.2.840.10008.1.2.1')) |
|
|
|
|
|
|
64
|
|
|
assert not (DICOM_RequireDecompression('1.2.840.10008.1.2.2')) |
|
|
|
|
|
|
65
|
|
|
assert (DICOM_RequireDecompression('1.2.840.10008.1.2.4')) |
|
|
|
|
|
|
66
|
|
|
assert (DICOM_RequireDecompression('1.2.840.10008.1.2.4.57')) |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
class MyTestCase(unittest.TestCase): |
|
|
|
|
|
|
70
|
|
|
def test_wrong_syntax(self): |
|
|
|
|
|
|
71
|
|
|
self.assertRaises(ValueError, DICOM_RequireDecompression, 'FakeTest') |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def test_DICOM_anonymizer(): |
|
|
|
|
|
|
75
|
|
|
file_names = get_testdata_files("emri") |
|
76
|
|
|
for file_name in file_names: |
|
77
|
|
|
success = DICOM_anonymizer(file_name, "CNBP0010001") |
|
78
|
|
|
assert success |
|
79
|
|
|
for file_name in file_names: |
|
80
|
|
|
success, value = DICOM_retrieveElements(file_name, "PatientID") |
|
81
|
|
|
assert success |
|
82
|
|
|
assert (value == "CNBP0010001") |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def test_DICOM_retrieveMRN(): |
|
|
|
|
|
|
86
|
|
|
path = get_testdata_files("emri_small_big_endian")[0] |
|
87
|
|
|
success, MRN = DICOM_retrieveMRN(path) |
|
|
|
|
|
|
88
|
|
|
assert success |
|
89
|
|
|
assert (MRN == 'CNBP0010001') |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def test_DICOM_update(): |
|
|
|
|
|
|
93
|
|
|
path = get_testdata_files("emri") |
|
94
|
|
|
for file in path: |
|
95
|
|
|
success, _ = DICOM_updateElement(file, "PatientBirthDate", "19950101", file) |
|
96
|
|
|
assert success |
|
97
|
|
|
|
|
98
|
|
|
for file in path: |
|
99
|
|
|
success, value = DICOM_retrieveElements(file, "PatientBirthDate") |
|
100
|
|
|
assert success |
|
101
|
|
|
assert(value == "19950101") |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def test_DICOM_computerScanAge(): |
|
|
|
|
|
|
104
|
|
|
logger = logging.getLogger("DICOM compute age") |
|
105
|
|
|
path = get_testdata_files("emri_small_RLE")[0] |
|
106
|
|
|
success, Age = DICOM_computeScanAge(path) |
|
|
|
|
|
|
107
|
|
|
assert success |
|
108
|
|
|
logger.info(Age.day) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
if __name__ == '__main__': |
|
112
|
|
|
test_DICOM_decompress() |
|
|
|
|
|
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.