-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature_extractor.py
288 lines (250 loc) · 9.48 KB
/
feature_extractor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'''
Expected folder structure:
- PATIENTS_FOLDER
- Patient1
- Day1
- Day2
- Day3
- ...
- Patient2
- Day1
- Day2
- Day3
- ...
- Pattent3
- Day1
- Day2
- Day3
- ...
...
- RESULTS_FOLDER
'''
'''
TO MODIFY:
'''
# folder where each patient folder is located
PATIENTS_FOLDER = 'Patients'
# folder where results for each patient will be saved
RESULTS_FOLDER = 'Patients_results'
# organ of interest. code might use MASK_OPTIONS dictionary defined below
organ = 'rectum'
# ______________________________________________________________________________________________________________________
import os, time, subprocess, shutil
import SimpleITK as sitk
import radiomics as rad
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from radiomics import featureextractor
'''
Function to instantiate extractor
- modify extractor parameters as needed
- check pyradiomics docs for more information: https://pyradiomics.readthedocs.io/en/latest/radiomics.html
'''
def instantiate_extractor():
print('Instantiating extractor...')
extractor = featureextractor.RadiomicsFeaturesExtractor(normalize=True,
correctMask=True,
resampledPixelSpacing=[0.5,0.5,0.5])
extractor.addProvenance()
extractor.enableAllImageTypes()
return extractor
'''
Function to generate masks using RT-struct
- plastimatch must be installed and added to $PATH
- input:
- src_folder: path to folder containing RT-struct file (recommended to be in the same folder as the rest of the dicom files)
- msks_folder: destination path for the masks to be generated
'''
def generate_masks(src_folder, msks_folder):
msks_cmd = 'plastimatch convert --input %s --output-prefix %s'%(src_folder, msks_folder)
#print(msks_cmd)
if os.path.isdir(msks_folder) and len(os.listdir(msks_folder)) > 0:
print('Masks already at: ', msks_folder)
else:
print('Generating masks...')
if len(os.listdir(src_folder)) == 0:
print('Source folder is empty: ', src_folder)
return 0
proc = os.system(msks_cmd)
#proc.wait()
print('Masks generated at: ', msks_folder)
return 1
'''
Function to combine all dicom slices to one nrrd file
- plastimatch must be installed and added to $PATH
- input:
- src_folder: path to folder containing the dicom slices
- scan_path: destination path for the combined image
'''
def generate_combined_image(src_folder, scan_path):
scan_cmd = 'plastimatch convert --input %s --output-img %s'%(src_folder, scan_path)
print(scan_cmd)
if os.path.exists(scan_path):
print('Full nrrd image already at: ', scan_path)
else:
print('Combining dicoms and converting to nrrd...')
if len(os.listdir(src_folder)) == 0:
print('Source folder is empty: ', src_folder)
return 0
proc = os.system(scan_cmd)
#proc.wait()
print('Full nrrd image generated at: ', scan_path)
return 1
# dictionary used for translation (mask names were in portuguese)
MASK_OPTIONS = {#'prostate':'prostata',
'bladder':'bexiga',
'penile_bulb':'bulbo peniano',
#'rectum':'reto',
'pelvic_lymph':'linf pelvicos',
'sem_vesicles':'vesiculas sem',
'femur_esq':'femur esq',
'femur_dir':'femur dir',
'intestinal_sac':'saco intestinal',
'body':'corpo'
}
'''
Function to fetch mask file corresponding to specified organ
- input:
- organ: string name of organ ('bladder', 'rectum', 'prostate', etc.)
- msks_folder: path to folder containing the generated masks
'''
def get_mask_for_organ(organ, msks_folder):
# check translation dictionary
if organ.lower() in MASK_OPTIONS.keys():
organ_str = MASK_OPTIONS[organ]
else:
organ_str = organ.lower()
# generate list of mask paths ignoring hidden files (names of hidden files start with '.')
msk_filenames = sorted([f for f in os.listdir(msks_folder) if not f.startswith('.')], key=lambda f: f.lower())
for msk_filename in msk_filenames:
if organ_str in msk_filename.lower():
msk_path = os.path.join(msks_folder, msk_filename)
print('Found mask path: ', msk_path)
return msk_path
return None
'''
Function to get list of extracted features
- input:
- results: data returned by 'execute' function of RadiomicsFeaturesExtractor instance
- extractor: instance of RadiomicsFeaturesExtractor
'''
def get_features_list(results, extractor):
features_list = []
imageTypes = ['original','exponential','gradient','lbp-2D','logarithm','square','squareroot','wavelet']
waveletTypes = ['LLH','LHL','LHH','HLL','HLH','HHL','HHH','LLL']
for waveletType in waveletTypes:
imageTypes.append('wavelet-' + waveletType)
for imageType in imageTypes:
for feature_class in rad.getFeatureClasses().keys():
for feature in extractor.getFeatureNames(feature_class):
feature_name = '_'.join([imageType, feature_class, feature])
if feature_name in results.keys():
features_list.append(feature_name)
return features_list
'''
Function to start extraction with some added options
- extractor: instance of RadiomicsFeaturesExtractor
- scan_path: path to combined image
- msk_path: path to mask
- day_name: folder for specific day of patient
- voxelbased: boolean. Set as True for voxelbased extraction
- verbose: boolean. Set as True for more info printed during extraction
'''
def execute_extraction(extractor, scan_path, msk_path, day_name, voxelbased, verbose):
if verbose:
rad.setVerbosity(20)
print('Extracting features...')
t0 = time.time()
if voxelbased:
results = extractor.execute(scan_path, msk_path, voxelBased=True)
else:
results = extractor.execute(scan_path, msk_path)
features_list = get_features_list(results, extractor)
data = {'Day':day_name}
for feat in features_list:
data[feat] = results[feat]
tf = time.time()
print('Feature extraction completed')
if (tf-t0) > 3600:
print('\tElapsed Time: %f hours'%((tf-t0)/3600))
elif (tf-t0) > 60:
print('\tElapsed Time: %f min'%((tf-t0)/60))
else:
print('\tElapsed Time: %f sec'%(tf-t0))
return data
'''
Function to save data from freature extraction
- input:
- data: dictionary contaning 'feature_name':value
- csv_path: path to csv file where data will be stored
'''
def save_data(data, csv_path):
data_frame = pd.DataFrame({f:[data[f]] for f in data.keys()})
print('Saving data...')
# check if csv file already exists
if os.path.isfile(csv_path):
existing_data = pd.read_csv(csv_path)
if len(existing_data.columns) == len(data_frame.columns):
with open(csv_path, 'a') as f:
data_frame.to_csv(f, header=False, index=False)
else:
print('Data NOT saved: new data does not have the same length as existing data')
return
else:
with open(csv_path, 'w') as f:
data_frame.to_csv(f, index=False)
print('Data saved at: ', csv_path)
'''
Function to execute feature extraction pipeline
- input:
- patient_name: string corresponding to name of patient folder (patient folder has folders corresponding to each day)
- day_name: string corresponding to name of day folder
- organ: string corresponding to specified organ ('rectum', 'prostate', etc.)
- voxelbased: boolean. Set as True for voxelbased extraction
- verbose: boolean. Set as True for more info printed during extraction
'''
def execute_extraction_pipeline(patient_name, day_name, organ, voxelbased=False, verbose=False):
if verbose:
rad.setVerbosity(50)
# define paths
src_folder = os.path.join(PATIENTS_FOLDER, patient_name, day_name)
dest_folder = os.path.join(RESULTS_FOLDER, patient_name, day_name)
msks_folder = os.path.join(dest_folder, 'Masks')
scan_path = os.path.join(dest_folder, '_'.join([patient_name, day_name])+'.nrrd')
if voxelbased:
csv_path = os.path.join(RESULTS_FOLDER, patient_name, organ+'_results_vox.csv')
else:
csv_path = os.path.join(RESULTS_FOLDER, patient_name, organ+'_results.csv')
# generate masks, and combine and convert dicoms
if generate_masks(src_folder, msks_folder) == 0:
return
if generate_combined_image(src_folder, scan_path) == 0:
return
msk_path = get_mask_for_organ(organ, msks_folder)
if msk_path is None:
print('No mask available for %s'%(organ))
print('\tTry: %a'%[x[:-4].lower() for x in os.listdir(msks_folder)])
exit()
return
extractor = instantiate_extractor()
if os.path.isfile(csv_path):
dat = pd.read_csv(csv_path)
for day in dat['Day']:
if day == day_name:
print('Features for %s is already extracted'%day_name)
return
data = execute_extraction(extractor, scan_path, msk_path, day_name, voxelbased, verbose)
save_data(data, csv_path)
return data
#___________________________________________________________________
# main program
def main():
for patient in os.listdir(PATIENTS_FOLDER):
print('Patient: ', patient, '\n')
patient_path = os.path.join(PATIENTS_FOLDER, patient)
for day in os.listdir(patient_path):
print('Day: ', day)
execute_extraction_pipeline(patient, day, organ)
if __name__ == "__main__":
main()