|
1
|
|
|
import sys |
|
|
|
|
|
|
2
|
|
|
import logging |
|
3
|
|
|
import os |
|
4
|
|
|
import subprocess |
|
5
|
|
|
import re |
|
6
|
|
|
|
|
7
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
8
|
|
|
|
|
9
|
|
|
class DICOM_convert: |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
@staticmethod |
|
12
|
|
|
def to_nii(input_folder, output_folder): |
|
13
|
|
|
""" |
|
14
|
|
|
|
|
15
|
|
|
:param input_folder: Input_folder can be a root folder or flat. |
|
16
|
|
|
:return: |
|
17
|
|
|
""" |
|
18
|
|
|
logger = logging.getLogger("DICOM to NII conversion") |
|
19
|
|
|
if not os.path.exists(input_folder) or not os.path.exists(output_folder): |
|
20
|
|
|
return False, "Argument input or output folder does not exist" |
|
21
|
|
|
|
|
22
|
|
|
try: |
|
23
|
|
|
|
|
24
|
|
|
# SUPER IMPORTANT! MAKE SURE dcm2niix by Chris Roden is in the system path! |
|
25
|
|
|
subprocess.check_output(['dcm2niix', |
|
26
|
|
|
'-b', 'y', |
|
27
|
|
|
'-z', 'y', |
|
28
|
|
|
'-v', 'y', |
|
29
|
|
|
'-f', "%s_%p", |
|
30
|
|
|
'-o', output_folder, |
|
31
|
|
|
input_folder]) |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
# When dcmdjpeg has errors |
|
34
|
|
|
except subprocess.CalledProcessError as e: |
|
|
|
|
|
|
35
|
|
|
logger.info(e) |
|
36
|
|
|
ErrorMessage = "File type not compatible" |
|
|
|
|
|
|
37
|
|
|
logger.info(ErrorMessage) |
|
38
|
|
|
return False, ErrorMessage |
|
39
|
|
|
except Exception as e: |
|
|
|
|
|
|
40
|
|
|
logger.info(e) |
|
41
|
|
|
ErrorMessage = "dcm2niix decompression call failed! Make sure dcm2niix is in your SYSTEM OS PATH and then check your input file:" |
|
|
|
|
|
|
42
|
|
|
logger.info(ErrorMessage) |
|
43
|
|
|
return False, ErrorMessage |
|
44
|
|
|
|
|
45
|
|
|
return True, "All conversion successfully finished." |
|
46
|
|
|
|
|
47
|
|
|
@staticmethod |
|
48
|
|
|
def fix_series(input_folder): |
|
|
|
|
|
|
49
|
|
|
os.chdir(input_folder) |
|
50
|
|
|
for file in os.listdir(input_folder): |
|
51
|
|
|
if re.match(r'^[0-9]_', file): |
|
52
|
|
|
os.rename(file, '00'+ file) |
|
53
|
|
|
elif re.match(r'^[0-9][0-9]_', file): |
|
54
|
|
|
os.rename(file, '0'+ file) |
|
55
|
|
|
|
|
56
|
|
|
@staticmethod |
|
57
|
|
|
def to_minc(input_folder, output_folder): |
|
|
|
|
|
|
58
|
|
|
raise NotImplementedError |
|
59
|
|
|
|
|
60
|
|
|
@staticmethod |
|
61
|
|
|
def to_BIDS(input_folder, output_folder): |
|
|
|
|
|
|
62
|
|
|
raise NotImplementedError |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if __name__ == "__main__": |
|
66
|
|
|
DICOM_convert.fix_series(r"C:\FullyAnonymizedSubjects\Wed3ConvertResult\raw_sorted") |
|
67
|
|
|
#DICOM_convert.to_nii(r"C:\FullyAnonymizedSubjects\2018-08-15_TestSubject2\Wed2-Decompressed", r"C:\FullyAnonymizedSubjects\2018-08-15_TestSubject2\Test") |
|
|
|
|
|
|
68
|
|
|
|
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.