14
14
15
15
16
16
def label_mask (image , mask , thresholds ):
17
- t1_low = thresholds [0 ]
18
- t2_low = thresholds [1 ]
19
- t1_high = thresholds [1 ]
20
- t2_high = thresholds [2 ]
17
+ t1_low = thresholds [0 ] # -1000
18
+ t2_low = thresholds [1 ] # -920
19
+ t1_high = thresholds [1 ] # -920
20
+ t2_high = thresholds [2 ] # -770
21
21
22
22
pxs = mask .shape [0 ] * mask .shape [1 ] * mask .shape [2 ]
23
23
perfusion_mask = np .zeros (mask .shape )
@@ -27,15 +27,25 @@ def label_mask(image, mask, thresholds):
27
27
# assign a label based on original value & thresholds & side
28
28
# first digit is the purfusion zone, second digit (units) is the lung
29
29
# !!! don't touch the order !!! highest threshold first
30
- perfusion_mask [(mask == 1 ) & (image_arr > t2_high )] = 31
31
- perfusion_mask [(mask == 1 ) & (image_arr <= t2_high )] = 21
32
- perfusion_mask [(mask == 1 ) & (image_arr <= t2_low )] = 11
33
- perfusion_mask [(mask == 2 ) & (image_arr > t2_high )] = 32
34
- perfusion_mask [(mask == 2 ) & (image_arr <= t2_high )] = 22
35
- perfusion_mask [(mask == 2 ) & (image_arr <= t2_low )] = 12
36
- perfusion_mask [(mask > 0 ) & (image_arr < t1_low )] = 0
30
+ perfusion_mask [(mask == 1 ) & (image_arr > t2_high )] = 31 # maggiore di -770
31
+ perfusion_mask [(mask == 1 ) & (image_arr <= t2_high )] = 21 # minore di/uguale a -770
32
+ perfusion_mask [(mask == 1 ) & (image_arr <= t2_low )] = 11 # minore di/uguale a -920
33
+ perfusion_mask [(mask == 2 ) & (image_arr > t2_high )] = 32 # maggiore di -770
34
+ perfusion_mask [(mask == 2 ) & (image_arr <= t2_high )] = 22 # minore di/uguale a -770
35
+ perfusion_mask [(mask == 2 ) & (image_arr <= t2_low )] = 12 # minore di/uguale a -920
36
+ perfusion_mask [(mask > 0 ) & (image_arr < t1_low )] = 0
37
+
38
+
39
+ #perfusion zone that ranges from -1000 to -770 and from -1000 to -920
40
+ perf_mask2 = np .zeros (mask .shape )
41
+ perf_mask2 [(mask == 1 )& (t1_low <= image_arr )& (image_arr <= t2_high )] = 41 #-1000/-770 sx
42
+ perf_mask2 [(mask == 1 )& (t1_low <= image_arr )& (image_arr <= t2_low )] = 51 #-1000/-920 sx
43
+ perf_mask2 [(mask == 2 )& (t1_low <= image_arr )& (image_arr <= t2_high )] = 42 #-1000/-770 dx
44
+ perf_mask2 [(mask == 2 )& (t1_low <= image_arr )& (image_arr <= t2_low )] = 52 #-1000/-920 dx
37
45
38
- return perfusion_mask
46
+
47
+
48
+ return perfusion_mask , perf_mask2
39
49
40
50
41
51
def do_prediction (input_image , force_cpu , dev = False ):
@@ -118,7 +128,7 @@ def label_image(mask, image, tresholds, folder_path):
118
128
return perfusion_mask
119
129
120
130
121
- def compute_stats (perf_arr , ignoreHighThreshold , spacing , dims , outdir ):
131
+ def compute_stats (perf_arr , perf_arr2 , ignoreHighThreshold , spacing , dims , outdir ):
122
132
123
133
# sum by label
124
134
# first digit is the purfusion zone, second digit (units) is the lung
@@ -129,6 +139,13 @@ def compute_stats(perf_arr, ignoreHighThreshold, spacing, dims, outdir):
129
139
label_22 = np .count_nonzero (perf_arr == 22 )
130
140
label_32 = np .count_nonzero (perf_arr == 32 )
131
141
142
+
143
+ labels_41 = np .count_nonzero (perf_arr2 == 41 ) #left lung from -1000 to -770
144
+ labels_51 = np .count_nonzero (perf_arr2 == 51 ) #left lung from -1000 to -920
145
+ labels_42 = np .count_nonzero (perf_arr2 == 42 ) #right lung from -1000 to -770
146
+ labels_52 = np .count_nonzero (perf_arr2 == 52 ) #right lung from -1000 to -920
147
+
148
+
132
149
# compute total volume for each side
133
150
if ignoreHighThreshold :
134
151
tot_vol_left = label_11 + label_21
@@ -143,12 +160,25 @@ def compute_stats(perf_arr, ignoreHighThreshold, spacing, dims, outdir):
143
160
low_perf_vol_left = label_11
144
161
low_perf_vol_right = label_12
145
162
163
+ #assign perfusion range (-1000, -770) and (-1000,-920)
164
+ left_1000_770 = labels_41
165
+ left_1000_920 = labels_51
166
+ right_1000_770 = labels_42
167
+ right_1000_920 = labels_52
168
+
146
169
print (low_perf_vol_left , low_perf_vol_right )
147
170
148
171
# compute low perfusion volume percentage
149
172
perc_low_perf_left = (low_perf_vol_left / tot_vol_left ) * 100
150
173
perc_low_perf_right = (low_perf_vol_right / tot_vol_right ) * 100
151
174
175
+ #compute volume percentage range (-1000, -770) and (-1000,-920)
176
+ perc_left_1000_770 = (left_1000_770 / tot_vol_left ) * 100
177
+ perc_left_1000_920 = (left_1000_920 / tot_vol_left ) * 100
178
+ perc_right_1000_770 = (right_1000_770 / tot_vol_right ) * 100
179
+ perc_right_1000_920 = (right_1000_920 / tot_vol_right ) * 100
180
+
181
+
152
182
print (perc_low_perf_left , perc_low_perf_right )
153
183
154
184
# compute conversion factor n_of_px -> volume
@@ -183,14 +213,47 @@ def compute_stats(perf_arr, ignoreHighThreshold, spacing, dims, outdir):
183
213
perc_low_perf_right ,
184
214
]
185
215
)
216
+ writer .writerow (
217
+ [
218
+ "left_1000_770" ,
219
+ left_1000_770 ,
220
+ left_1000_770 * conversion_factor ,
221
+ perc_left_1000_770 ,
222
+ ]
223
+ )
224
+ writer .writerow (
225
+ [
226
+ "left_1000_770" ,
227
+ left_1000_920 ,
228
+ left_1000_920 * conversion_factor ,
229
+ perc_left_1000_920 ,
230
+ ]
231
+ )
232
+ writer .writerow (
233
+ [
234
+ "right_1000_770" ,
235
+ right_1000_770 ,
236
+ right_1000_770 * conversion_factor ,
237
+ perc_right_1000_770 ,
238
+ ]
239
+ )
240
+ writer .writerow (
241
+ [
242
+ "right_1000_920" ,
243
+ right_1000_920 ,
244
+ right_1000_920 * conversion_factor ,
245
+ perc_right_1000_920 ,
246
+ ]
247
+ )
248
+
186
249
187
250
# write a pdf file
188
251
Titolo_tabella = "Summary"
189
252
src = "temp/histogram.png"
190
253
191
- context = {'src' : src ,'Titolo_tabella' : Titolo_tabella , 'n_pix_1' : tot_vol_left , 'n_pix_2' : low_perf_vol_left , 'n_pix_3' : tot_vol_right , 'n_pix_4' : low_perf_vol_right ,
192
- 'vol_1' :tot_vol_left * conversion_factor , 'vol_2' : low_perf_vol_left * conversion_factor , 'vol_3' :tot_vol_right * conversion_factor , 'vol_4' : low_perf_vol_right * conversion_factor ,
193
- 'perc_1' : "" , 'perc_2' : perc_low_perf_left , 'perc_3' : "" , 'perc_4' : perc_low_perf_right }
254
+ context = {'src' : src ,'Titolo_tabella' : Titolo_tabella , 'n_pix_1' : tot_vol_left , 'n_pix_2' : low_perf_vol_left , 'n_pix_3' : tot_vol_right , 'n_pix_4' : low_perf_vol_right , 'n_pix_5' : left_1000_770 , 'n_pix_6' : left_1000_920 , 'n_pix_7' : right_1000_770 , 'n_pix_8' : right_1000_920 ,
255
+ 'vol_1' :tot_vol_left * conversion_factor , 'vol_2' : low_perf_vol_left * conversion_factor , 'vol_3' :tot_vol_right * conversion_factor , 'vol_4' : low_perf_vol_right * conversion_factor , 'vol_5' : left_1000_770 * conversion_factor , 'vol_6' : left_1000_920 * conversion_factor , 'vol_7' : right_1000_770 * conversion_factor , 'vol_8' : right_1000_920 * conversion_factor ,
256
+ 'perc_1' : "" , 'perc_2' : perc_low_perf_left , 'perc_3' : "" , 'perc_4' : perc_low_perf_right , 'perc_5' : perc_left_1000_770 , 'perc_6' : perc_left_1000_920 , 'perc_7' : perc_right_1000_770 , 'perc_8' : perc_right_1000_920 }
194
257
195
258
196
259
template_loader = jinja2 .FileSystemLoader ('./' )
@@ -272,20 +335,20 @@ def examine_threshold(csv_path, thresholds):
272
335
left_data_mdc = left_data ["valore_con_mdc" ]
273
336
right_data_mdc = right_data ["valore_con_mdc" ]
274
337
# select data inside the thresholds
275
- t1_low = thresholds [0 ]
338
+ t1_low = thresholds [0 ]
276
339
t2_low = thresholds [1 ]
277
- t1_high = thresholds [1 ]
278
- t2_high = thresholds [2 ]
279
- left_data_low = left_data_mdc [(t1_low <= left_data_mdc ) & (left_data_mdc < t2_low )]
340
+ t1_high = thresholds [1 ]
341
+ t2_high = thresholds [2 ]
342
+ left_data_low = left_data_mdc [(t1_low <= left_data_mdc ) & (left_data_mdc < t2_low )]
280
343
right_data_low = right_data_mdc [
281
344
(t1_low <= right_data_mdc ) & (right_data_mdc < t2_low )
282
- ]
345
+ ]
283
346
left_data_high = left_data_mdc [
284
347
(t1_high <= left_data_mdc ) & (left_data_mdc < t2_high )
285
- ]
348
+ ]
286
349
right_data_high = right_data_mdc [
287
350
(t1_high <= right_data_mdc ) & (right_data_mdc < t2_high )
288
- ]
351
+ ]
289
352
290
353
print (len (left_data_low ), "\t " , len (left_data_high ))
291
354
print (len (right_data_low ), "\t " , len (right_data_high ))
@@ -378,7 +441,7 @@ def examine_threshold(csv_path, thresholds):
378
441
nargs = "+" ,
379
442
type = float ,
380
443
help = "array of tresholds" ,
381
- default = [- 1000 , - 930 , - 770 ],
444
+ default = [- 1000 , - 920 , - 770 ],
382
445
)
383
446
384
447
parser .add_argument (
@@ -409,15 +472,15 @@ def examine_threshold(csv_path, thresholds):
409
472
segmentation_arr = sitk .GetArrayFromImage (segmentation )
410
473
411
474
# extract only values inside the target palette (thresholds)
412
- perfusion_mask = label_image (segmentation_arr , image , args .thresholds , args .outdir )
475
+ perfusion_mask , perf_mask2 = label_image (segmentation_arr , image , args .thresholds , args .outdir )
413
476
414
477
# generate the histogram
415
478
maskToCSV (segmentation , image , args .thresholds , temp_path )
416
479
examine_threshold (temp_path + "hist_output.csv" , args .thresholds )
417
480
418
481
# compute volumes
419
482
compute_stats (
420
- perfusion_mask ,
483
+ perfusion_mask , perf_mask2 ,
421
484
args .ignore_high_threshold ,
422
485
image .GetSpacing (),
423
486
image .GetSize (),
0 commit comments