-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_refine.py
More file actions
322 lines (272 loc) · 14.4 KB
/
preprocess_refine.py
File metadata and controls
322 lines (272 loc) · 14.4 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import argparse
import os
import numpy as np
from matplotlib import pyplot as plt
from batchgenerators.utilities.file_and_folder_operations import *
import scipy.misc
from skimage.morphology import skeletonize_3d
from scipy import ndimage as ndi
from dvn.utils import get_itk_array, make_itk_image, write_itk_image, get_itk_image
# from batchgenerators.utilities.file_and_folder_operations import *
from skimage.io import imread
import SimpleITK as sitk
from evaluation import metric_dice, f1_socre, accuracy_bin, sensitivity, specificity, precision
from skimage.measure import label, regionprops
import nibabel as nib
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
def parse_args():
parser = argparse.ArgumentParser(description='Preprocessing-based segmentation refinement')
# parser.add_argument('--filenames1', dest='filenames1', type=str,
# default='/public/yangxiaodu/vessap2/data_self/raw_seg_vessels_result.txt')
# parser.add_argument('--filenames2', dest='filenames2', type=str,
# default='/public/yangxiaodu/vessap2/data_self/preprocessed_seg_vessels_result.txt')
parser.add_argument('--maskFilename', dest='maskFn', type=str,
default=None,
help='a mask file to be applied to the predictions')
parser.add_argument('--output', dest='output', type=str,
default='/home/xdyang/FineVess/results/pre_refine',
help='output folder for storing refinement results (default: current working directory)')
parser.add_argument('--f', dest='format', type=str, default='.nii.gz',
help='NIFTI file format for saving outputs (default: .nii.gz)')
parser.add_argument('--txt', dest='txt', type=bool, default=False,
help='choose iuput way. If using txt files, choose txt=True (default: .nii.gz)')
args = parser.parse_args()
return args
def save_data(data, img, filename):
out_img = make_itk_image(data, img)
write_itk_image(out_img, filename)
def run():
args = parse_args()
outputFn = args.output
txt=args.txt
fmt = args.format
# filenames1 = args.filenames1
# filenames2 = args.filenames2
masks = args.maskFn
print('----------------------------------------')
print(' Postprocessing Parameters ')
print('----------------------------------------')
print('txt',txt)
# print('Input files:', filenames1)
# print('Input files:', filenames2)
print('Mask file:', masks)
print('Output folder:', outputFn)
print('Output format:', fmt)
with open(os.path.abspath(args.filenames1)) as f:
iFn1 = f.readlines()
iFn1 = [x.strip() for x in iFn1]
with open(os.path.abspath(args.filenames2)) as f:
iFn2 = f.readlines()
iFn2 = [x.strip() for x in iFn2]
if masks is not None:
with open(os.path.abspath(args.maskFn)) as f:
mFn = f.readlines()
mFn = [x.strip() for x in mFn]
else:
mFn=[]
i = 0
dice1={}
acc_voxel1 = {}
sensitivity2={}
specificity2={}
precision2={}
name = {}
source_folder = '/home/xdyang/FineVess/results/nnunet'
train_cases = subfiles(join(source_folder, 'raw_data_seg_results'), suffix=".nii.gz", join=False)
if txt==False:
iFn1=[]
iFn2=[]
mFn=[]
for i, t in enumerate(train_cases):
iFn1.append(join(source_folder, 'raw_data_seg_results',t))
iFn2.append(join(source_folder, 'pre_data_seg_results',t))
if mFn!=[]:
for ifn1, ifn2,mfn in zip(iFn1, iFn2, mFn):
prefix = os.path.basename(ifn1).split('.')[0]
raw_result = get_itk_array(ifn1)
preprocessed_result = get_itk_array(ifn2)
# dice_test = round(metric_dice(raw_result, preprocessed_result), 4)
# print(prefix, dice_test)
dice = []
for j in range(raw_result.shape[0]):
dice.append(round(metric_dice(raw_result[j, :, :], preprocessed_result[j, :, :]), 4))
# print(prefix, dice)
fig = plt.figure()
plt.plot(dice)
plt.title(prefix)
# plt.title('acc')
plt.ylabel('dice')
# plt.ylabel('acc')
plt.xlabel('slice')
# plt.legend(loc='lower right')
y1_min = np.argmin(dice)
y1_max = np.argmax(dice)
show_min = '[' + str(y1_min) + ' ' + str(dice[y1_min]) + ']'
show_max = '[' + str(y1_max) + ' ' + str(dice[y1_max]) + ']'
plt.plot(y1_min, dice[y1_min], 'ko')
plt.plot(y1_max, dice[y1_max], 'ko')
plt.annotate(show_min, xy=(y1_min, dice[y1_min]), xytext=(y1_min, dice[y1_min]))
plt.annotate(show_max, xy=(y1_max, dice[y1_max]), xytext=(y1_max, dice[y1_max]))
plt.show()
# predict
fig.savefig(outputFn+"/dice_plot/"+ prefix + ".jpg")
prefix = os.path.basename(ifn1).split('.')[0]
raw_result = get_itk_array(ifn1)
preprocessed_result=get_itk_array(ifn2)
print(dice[y1_min])
if dice[y1_min] >= 0.4:
print(prefix)
preprocessed_result_int = preprocessed_result.astype(np.int)
label_img, num = label(preprocessed_result_int, connectivity=preprocessed_result.ndim, return_num=True)
preprocessed_region = regionprops(label_img)
pre_region_all_coord = []
for o in range(len(preprocessed_region)):
coord_list = preprocessed_region[o].coords
for v in range(len(coord_list)):
pre_region_all_coord.append(tuple([coord_list[v][0], coord_list[v][1], coord_list[v][2]]))
# record the coordinates
raw_result_int = raw_result.astype(np.int)
label_img, num = label(raw_result_int, connectivity=raw_result_int.ndim, return_num=True)
raw_region = regionprops(label_img)
raw_region_all_coord = []
for o in range(len(raw_region)):
coord_list = raw_region[o].coords
for v in range(len(coord_list)):
raw_region_all_coord.append(tuple([coord_list[v][0], coord_list[v][1], coord_list[v][2]]))
for o in range(len(preprocessed_region)):
coord_list2 = preprocessed_region[o].coords
preprocessed_region_list2 = []
for v in range(len(coord_list2)):
preprocessed_region_list2.append(tuple([coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]]))
if len(list(set(preprocessed_region_list2).intersection(set(raw_region_all_coord)))) == 0:
for v in range(len(coord_list2)):
preprocessed_result[coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]] = 0
for o in range(len(raw_region)):
coord_list2 = raw_region[o].coords
raw_region_list2 = []
for v in range(len(coord_list2)):
raw_region_list2.append(tuple([coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]]))
if len(list(set(raw_region_list2).intersection(set(pre_region_all_coord)))) == 0:
for v in range(len(coord_list2)):
preprocessed_result[coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]] = 1
else:
preprocessed_result =raw_result
preprocessed_result = preprocessed_result.astype(np.float32)
ofn2 = os.path.join(outputFn + '/' + prefix + '.nii.gz')
save_data(data=preprocessed_result, img=get_itk_image(ifn1), filename=ofn2)
mask = get_itk_array(mfn)
index = []
for ind in range(mask.shape[0]):
if np.any(mask[ind, :, :] != 2) == True:
# if np.any(mask[ind, :, :]) == True:
index.append(ind)
# print(index)
data1 = preprocessed_result[index, :, :]
mask1 = mask[index, :, :]
dice1[i] = round(metric_dice(mask1, data1), 4)
acc_voxel1[i] = round(accuracy_bin(mask1, data1), 4)
sensitivity2[i] = round(sensitivity(mask1, data1), 4)
specificity2[i] = round(specificity(mask1, data1), 4)
precision2[i] = round(precision(mask1, data1), 4)
name[i] = prefix
i = i + 1
Len = {}
Sum = {}
Avg = {}
for k, p in enumerate([dice1, acc_voxel1,sensitivity2, specificity2, precision2]):
Len[k] = len(p)
Sum[k] = sum(p.values())
Avg[k] = Sum[k] / Len[k]
path = args.output + '/metric_other.txt'
f = open(path, 'w')
f.writelines([path, "\n", "dice1:", str(dice1), str(Avg[0]),
# "\n", "f1_score1:",str(f1score1),str(Avg[1]),"\n","f1score2:",str(f1score2),str(Avg[2]),
"\n", "acc_voxel1:", str(acc_voxel1), str(Avg[1]), # "\n","acc_voxel2:",str(acc_voxel2),str(Avg[4]),
"\n", "sensitivity2:", str(sensitivity2), str(Avg[2]), "\n", "specificity2:", str(specificity2),
str(Avg[3]),
"\n", "precision2:", str(precision2), str(Avg[4]), "\n", "name:", str(name)])
f.close()
# print("dice_probs", dice_acc)
print("dice1", dice1)
print("acc_voxel1", acc_voxel1)
print("sensitivity2", sensitivity2)
print("specificity2", specificity2)
print("precision2", precision2)
print('finished!')
else:
for ifn1, ifn2 in zip(iFn1, iFn2):
prefix = os.path.basename(ifn1).split('.')[0]
raw_result = get_itk_array(ifn1)
preprocessed_result = get_itk_array(ifn2)
# dice_test = round(metric_dice(raw_result, preprocessed_result), 4)
# print(prefix, dice_test)
dice = []
for j in range(raw_result.shape[0]):
dice.append(round(metric_dice(raw_result[j, :, :], preprocessed_result[j, :, :]), 4))
# print(prefix, dice)
fig = plt.figure()
plt.plot(dice)
plt.title(prefix)
# plt.title('acc')
plt.ylabel('dice')
# plt.ylabel('acc')
plt.xlabel('slice')
# plt.legend(loc='lower right')
y1_min = np.argmin(dice)
y1_max = np.argmax(dice)
show_min = '[' + str(y1_min) + ' ' + str(dice[y1_min]) + ']'
show_max = '[' + str(y1_max) + ' ' + str(dice[y1_max]) + ']'
plt.plot(y1_min, dice[y1_min], 'ko')
plt.plot(y1_max, dice[y1_max], 'ko')
plt.annotate(show_min, xy=(y1_min, dice[y1_min]), xytext=(y1_min, dice[y1_min]))
plt.annotate(show_max, xy=(y1_max, dice[y1_max]), xytext=(y1_max, dice[y1_max]))
plt.show()
# predict
# save_path = '/public/yangxiaodu/nnunet/save_data/nnUNet_trained_models/nnUNet/3d_fullres/Task508_other_vessels_raw/nnUNetTrainerV2_loss_ignore_label2_cew_sb_CBAM_decoder_btc_moreDA_BN__nnUNetPlansv2.1/new_all_pre_add_out/dice_plot/'
fig.savefig(outputFn+"/dice_plot/"+ prefix + ".jpg")
prefix = os.path.basename(ifn1).split('.')[0]
raw_result = get_itk_array(ifn1)
preprocessed_result = get_itk_array(ifn2)
print(dice[y1_min])
if dice[y1_min] >= 0.4:
print(prefix)
preprocessed_result_int = preprocessed_result.astype(np.int)
label_img, num = label(preprocessed_result_int, connectivity=preprocessed_result.ndim, return_num=True)
preprocessed_region = regionprops(label_img)
pre_region_all_coord = []
for o in range(len(preprocessed_region)):
coord_list = preprocessed_region[o].coords
for v in range(len(coord_list)):
pre_region_all_coord.append(tuple([coord_list[v][0], coord_list[v][1], coord_list[v][2]]))
raw_result_int = raw_result.astype(np.int)
label_img, num = label(raw_result_int, connectivity=raw_result_int.ndim, return_num=True)
raw_region = regionprops(label_img)
raw_region_all_coord = []
for o in range(len(raw_region)):
coord_list = raw_region[o].coords
for v in range(len(coord_list)):
raw_region_all_coord.append(tuple([coord_list[v][0], coord_list[v][1], coord_list[v][2]]))
for o in range(len(preprocessed_region)):
coord_list2 = preprocessed_region[o].coords
preprocessed_region_list2 = []
for v in range(len(coord_list2)):
preprocessed_region_list2.append(
tuple([coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]]))
if len(list(set(preprocessed_region_list2).intersection(set(raw_region_all_coord)))) == 0:
for v in range(len(coord_list2)):
preprocessed_result[coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]] = 0
for o in range(len(raw_region)):
coord_list2 = raw_region[o].coords
raw_region_list2 = []
for v in range(len(coord_list2)):
raw_region_list2.append(tuple([coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]]))
if len(list(set(raw_region_list2).intersection(set(pre_region_all_coord)))) == 0:
for v in range(len(coord_list2)):
preprocessed_result[coord_list2[v][0], coord_list2[v][1], coord_list2[v][2]] = 1
else:
preprocessed_result = raw_result
preprocessed_result = preprocessed_result.astype(np.float32)
ofn2 = os.path.join(outputFn + '/' + prefix + '.nii.gz')
save_data(data=preprocessed_result, img=get_itk_image(ifn1), filename=ofn2)
if __name__ == '__main__':
run()