-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicom_to_nifti.py
98 lines (72 loc) · 3.85 KB
/
dicom_to_nifti.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#author @t_sanf
from parsing_VOI import *
import pydicom
import math
import nibabel
import re
import dicom2nifti
import shutil
class Dicom2Nifti():
def __init__(self):
self.basePATH = r''
self.databases=['']
def dicom_to_nifti(self,t2_only=True):
'''
convert all dicom files in each database to .nifti format
:return:
'''
databases = self.databases
if t2_only==True:
series_all=['t2']
else:
series_all=['t2','adc','highb']
exception_logger=[]
for database in databases:
for patient in self.check_for_nifti_completion():
print("converting files to nifti for patient {}".format(patient))
#make nifti file if one does not already exist
if not os.path.exists(os.path.join(self.basePATH, database, patient, 'nifti')):
os.mkdir(os.path.join(self.basePATH, database, patient, 'nifti'))
for series in series_all:
#account for aligned data structure
if series=='t2':
dicom_name = series; nifti_name = series
if series=='adc' or series=='highb':
nifti_name=series
dicom_name = os.path.join(series, 'aligned')
#make folder if not already made:
if not os.path.exists(os.path.join(self.basePATH,database,patient,'nifti',nifti_name)):
os.mkdir(os.path.join(self.basePATH,database,patient,'nifti',nifti_name))
dicom_directory=os.path.join(self.basePATH,database,patient,'dicoms',dicom_name)
nifti_directory=os.path.join(self.basePATH, database, patient, 'nifti', nifti_name)
dicom2nifti.convert_directory(dicom_directory, nifti_directory, reorient=False)
print("the following patients still need to be processed {}".format(exception_logger))
def check_for_nifti_completion(self):
'''iterate over files and check if files have been converted from dicom to nifti format for all series'''
need_to_process=[]
for database in self.databases:
for patient in os.listdir(os.path.join(self.basePATH,database)):
if not os.path.exists(os.path.join(self.basePATH, database, patient, 'nifti')):
need_to_process+=[patient]
elif os.path.exists(os.path.join(self.basePATH, database, patient, 'nifti')):
for val in ['t2', 'adc', 'highb']:
file=os.path.join(self.basePATH, database, patient, 'nifti')
if not os.path.isdir(os.path.join(file,val)):
need_to_process += [patient]
elif os.path.isdir(os.path.join(file,val)):
if len(os.listdir(os.path.join(file, val))) == 0:
need_to_process += [patient]
print("patient {} has already had dicoms converted to nifti".format(patient))
print('total of {} patients to convert to nifti masks'.format(len(set(need_to_process))))
return set(need_to_process)
def remove_nifti_files(self,database):
'''iterate over files and remove emtpy nifti files (if there is an error)'''
need_to_process=[]
for patient in os.listdir(os.path.join(self.basePATH,database)):
print(patient)
if os.path.exists(os.path.join(self.basePATH, database, patient, 'nifti')):
shutil.rmtree(os.path.join(self.basePATH, database, patient, 'nifti'))
print('removing data for patient {}'.format(patient))
if __name__=='__main__':
c=Dicom2Nifti()
c.dicom_to_nifti()