|
1
|
|
|
import logging |
|
|
|
|
|
|
2
|
|
|
import os |
|
3
|
|
|
import pydicom, subprocess |
|
|
|
|
|
|
4
|
|
|
from PythonUtils.folder import recursive_list |
|
|
|
|
|
|
5
|
|
|
from tqdm import tqdm |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
from pydicom.filereader import read_file_meta_info |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
from DICOM.validate import DICOM_validate |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class DICOM_decompress: |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
@staticmethod |
|
15
|
|
|
def save_as(input_file, out_put): |
|
16
|
|
|
""" |
|
17
|
|
|
A wrapper for DCMDJPEG for decompression |
|
18
|
|
|
:param input_file: |
|
19
|
|
|
:param out_put: |
|
20
|
|
|
:return: |
|
21
|
|
|
""" |
|
22
|
|
|
logger = logging.getLogger(__name__) |
|
23
|
|
|
|
|
24
|
|
|
if os.path.exists(out_put): |
|
25
|
|
|
return False, "Output_exist already." |
|
26
|
|
|
|
|
27
|
|
|
try: |
|
28
|
|
|
# SUPER IMPORTANT! MAKE SURE DCMDJPEG is in the system path! |
|
29
|
|
|
subprocess.check_output(['dcmdjpeg', input_file, out_put]) |
|
30
|
|
|
|
|
31
|
|
|
# When dcmdjpeg has errors |
|
32
|
|
|
except subprocess.CalledProcessError as e: |
|
|
|
|
|
|
33
|
|
|
logger.info(e) |
|
34
|
|
|
ErrorMessage = "File type not compatible for " + input_file |
|
|
|
|
|
|
35
|
|
|
logger.info(ErrorMessage) |
|
36
|
|
|
return False, ErrorMessage |
|
37
|
|
|
except Exception as e: |
|
|
|
|
|
|
38
|
|
|
logger.info(e) |
|
39
|
|
|
ErrorMessage = "DCMDJPEG decompression call failed! Make sure DCMDJPEG is in your SYSTEMOS PATH and then check your input file:" + input_file |
|
|
|
|
|
|
40
|
|
|
logger.info(ErrorMessage) |
|
41
|
|
|
return False, ErrorMessage |
|
42
|
|
|
|
|
43
|
|
|
# Ensure that data is actually written out. |
|
44
|
|
|
if not os.path.exists(out_put): |
|
45
|
|
|
ErrorMessage = "Cannot write out final file for some reason " + input_file |
|
|
|
|
|
|
46
|
|
|
logger.info(ErrorMessage) |
|
47
|
|
|
return False, ErrorMessage |
|
48
|
|
|
|
|
49
|
|
|
# Test read the data after writing. |
|
50
|
|
|
try: |
|
51
|
|
|
pydicom.read_file(out_put) |
|
52
|
|
|
except Exception as e: |
|
|
|
|
|
|
53
|
|
|
ErrorMessage = "Exception encountered while verifying the proper writing out of the DICOM data. Contact author to investigate, attach " + input_file |
|
|
|
|
|
|
54
|
|
|
logger.info(e) |
|
55
|
|
|
logger.info(ErrorMessage) |
|
56
|
|
|
return False, ErrorMessage |
|
57
|
|
|
|
|
58
|
|
|
logger.info("Success written " + input_file + " to " + out_put) |
|
59
|
|
|
return True, "All good" |
|
60
|
|
|
|
|
61
|
|
|
@staticmethod |
|
62
|
|
|
def check_decompression(transfer_syntax): |
|
63
|
|
|
""" |
|
64
|
|
|
Determine if the transfer syntax symbolize LEE or JPEG compressed! |
|
65
|
|
|
:param transfer_syntax: |
|
66
|
|
|
:return: whether the DICOM files are compressed. |
|
67
|
|
|
""" |
|
68
|
|
|
|
|
69
|
|
|
if not ("1.2.840.10008.1.2" in transfer_syntax): |
|
|
|
|
|
|
70
|
|
|
raise ValueError |
|
71
|
|
|
elif transfer_syntax == "1.2.840.10008.1.2" or transfer_syntax[18] == '1' or transfer_syntax[18] == '2': |
|
|
|
|
|
|
72
|
|
|
return False |
|
73
|
|
|
elif transfer_syntax[18] == '4' or transfer_syntax[18] == '5' or transfer_syntax[18] == '6': |
|
74
|
|
|
return True |
|
75
|
|
|
else: |
|
76
|
|
|
raise ValueError |
|
77
|
|
|
|
|
78
|
|
|
@staticmethod |
|
79
|
|
|
def get_transferSyntax(file_path): |
|
|
|
|
|
|
80
|
|
|
""" |
|
81
|
|
|
Used to find if a file is compressed |
|
82
|
|
|
:param file_path: |
|
83
|
|
|
:return: |
|
84
|
|
|
""" |
|
85
|
|
|
|
|
86
|
|
|
# Validity check: |
|
87
|
|
|
success, DICOM = DICOM_validate.file(file_path) |
|
|
|
|
|
|
88
|
|
|
if not success: |
|
89
|
|
|
raise IOError |
|
90
|
|
|
|
|
91
|
|
|
# Now read the meta information. |
|
92
|
|
|
dicom_file = read_file_meta_info(file_path) |
|
93
|
|
|
transfer_syntax = dicom_file.TransferSyntaxUID |
|
94
|
|
|
|
|
95
|
|
|
return transfer_syntax |
|
96
|
|
|
|
|
97
|
|
|
@staticmethod |
|
98
|
|
|
def check_decompression_quick(file_path): |
|
|
|
|
|
|
99
|
|
|
# Validity check: |
|
100
|
|
|
success, DICOM = DICOM_validate.file(file_path) |
|
|
|
|
|
|
101
|
|
|
if not success: |
|
102
|
|
|
raise IOError |
|
103
|
|
|
import pydicom.uid |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
# Now read the meta information. |
|
106
|
|
|
if DICOM.file_meta.TransferSyntaxUID in pydicom.uid.UncompressedPixelTransferSyntaxes: |
|
|
|
|
|
|
107
|
|
|
return True |
|
108
|
|
|
else: |
|
109
|
|
|
return False |
|
110
|
|
|
|
|
111
|
|
|
@staticmethod |
|
112
|
|
|
def filelist(file_list): |
|
113
|
|
|
""" |
|
114
|
|
|
Decompress all compressed files in the list OVERWRITE the files. |
|
115
|
|
|
:param file_list: |
|
116
|
|
|
:return: |
|
117
|
|
|
""" |
|
118
|
|
|
logger = logging.getLogger("Decompressing files") |
|
119
|
|
|
|
|
120
|
|
|
for file in tqdm(file_list): |
|
121
|
|
|
|
|
122
|
|
|
logger.info("Decompressing: " + file) |
|
123
|
|
|
|
|
124
|
|
|
# find if the file is DICOM, if not, skip this file. |
|
125
|
|
|
is_DICOM_file, _ = DICOM_validate.file(file) |
|
|
|
|
|
|
126
|
|
|
if not is_DICOM_file: |
|
127
|
|
|
continue |
|
128
|
|
|
|
|
129
|
|
|
# check if the file is compressed. |
|
130
|
|
|
TransferSyntax = DICOM_decompress.get_transferSyntax(file) |
|
|
|
|
|
|
131
|
|
|
try: |
|
132
|
|
|
RequireDecompression = DICOM_decompress.check_decompression(TransferSyntax) |
|
|
|
|
|
|
133
|
|
|
if RequireDecompression: |
|
134
|
|
|
DICOM_decompress.save_as(file, file) |
|
135
|
|
|
|
|
136
|
|
|
except ValueError: |
|
137
|
|
|
logger.info("Unknwonw DICOM syntax. You sure it is DICOM?") |
|
138
|
|
|
continue |
|
139
|
|
|
|
|
140
|
|
|
@staticmethod |
|
141
|
|
|
def decompress_folder(input_folder): |
|
|
|
|
|
|
142
|
|
|
files_list = recursive_list(input_folder) |
|
143
|
|
|
DICOM_decompress.filelist(files_list) |
|
|
|
|
|
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.