Skip to content

Commit 3a9f6da

Browse files
committed
new perfusion measures added
1 parent 7727476 commit 3a9f6da

File tree

2 files changed

+113
-27
lines changed

2 files changed

+113
-27
lines changed

complete_workflow.py

+89-26
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515

1616
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
2121

2222
pxs = mask.shape[0] * mask.shape[1] * mask.shape[2]
2323
perfusion_mask = np.zeros(mask.shape)
@@ -27,15 +27,25 @@ def label_mask(image, mask, thresholds):
2727
# assign a label based on original value & thresholds & side
2828
# first digit is the purfusion zone, second digit (units) is the lung
2929
# !!! 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
3745

38-
return perfusion_mask
46+
47+
48+
return perfusion_mask, perf_mask2
3949

4050

4151
def do_prediction(input_image, force_cpu, dev=False):
@@ -118,7 +128,7 @@ def label_image(mask, image, tresholds, folder_path):
118128
return perfusion_mask
119129

120130

121-
def compute_stats(perf_arr, ignoreHighThreshold, spacing, dims, outdir):
131+
def compute_stats(perf_arr, perf_arr2, ignoreHighThreshold, spacing, dims, outdir):
122132

123133
# sum by label
124134
# 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):
129139
label_22 = np.count_nonzero(perf_arr == 22)
130140
label_32 = np.count_nonzero(perf_arr == 32)
131141

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+
132149
# compute total volume for each side
133150
if ignoreHighThreshold:
134151
tot_vol_left = label_11 + label_21
@@ -143,12 +160,25 @@ def compute_stats(perf_arr, ignoreHighThreshold, spacing, dims, outdir):
143160
low_perf_vol_left = label_11
144161
low_perf_vol_right = label_12
145162

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+
146169
print(low_perf_vol_left, low_perf_vol_right)
147170

148171
# compute low perfusion volume percentage
149172
perc_low_perf_left = (low_perf_vol_left / tot_vol_left) * 100
150173
perc_low_perf_right = (low_perf_vol_right / tot_vol_right) * 100
151174

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+
152182
print(perc_low_perf_left, perc_low_perf_right)
153183

154184
# compute conversion factor n_of_px -> volume
@@ -183,14 +213,47 @@ def compute_stats(perf_arr, ignoreHighThreshold, spacing, dims, outdir):
183213
perc_low_perf_right,
184214
]
185215
)
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+
186249

187250
# write a pdf file
188251
Titolo_tabella = "Summary"
189252
src = "temp/histogram.png"
190253

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}
194257

195258

196259
template_loader = jinja2.FileSystemLoader('./')
@@ -272,20 +335,20 @@ def examine_threshold(csv_path, thresholds):
272335
left_data_mdc = left_data["valore_con_mdc"]
273336
right_data_mdc = right_data["valore_con_mdc"]
274337
# select data inside the thresholds
275-
t1_low = thresholds[0]
338+
t1_low = thresholds[0]
276339
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)]
280343
right_data_low = right_data_mdc[
281344
(t1_low <= right_data_mdc) & (right_data_mdc < t2_low)
282-
]
345+
]
283346
left_data_high = left_data_mdc[
284347
(t1_high <= left_data_mdc) & (left_data_mdc < t2_high)
285-
]
348+
]
286349
right_data_high = right_data_mdc[
287350
(t1_high <= right_data_mdc) & (right_data_mdc < t2_high)
288-
]
351+
]
289352

290353
print(len(left_data_low), "\t", len(left_data_high))
291354
print(len(right_data_low), "\t", len(right_data_high))
@@ -378,7 +441,7 @@ def examine_threshold(csv_path, thresholds):
378441
nargs="+",
379442
type=float,
380443
help="array of tresholds",
381-
default=[-1000, -930, -770],
444+
default=[-1000, -920, -770],
382445
)
383446

384447
parser.add_argument(
@@ -409,15 +472,15 @@ def examine_threshold(csv_path, thresholds):
409472
segmentation_arr = sitk.GetArrayFromImage(segmentation)
410473

411474
# 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)
413476

414477
# generate the histogram
415478
maskToCSV(segmentation, image, args.thresholds, temp_path)
416479
examine_threshold(temp_path + "hist_output.csv", args.thresholds)
417480

418481
# compute volumes
419482
compute_stats(
420-
perfusion_mask,
483+
perfusion_mask, perf_mask2,
421484
args.ignore_high_threshold,
422485
image.GetSpacing(),
423486
image.GetSize(),

template_pdf.html

+24-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
img {
4040
display: block;
4141
margin: auto;
42-
height: 100%;
4342
width: 100%;
4443
}
4544
</style>
@@ -80,6 +79,30 @@ <h2><strong>{{Titolo_tabella}}</strong></h2>
8079
<td>{{vol_4}}</td>
8180
<td>{{perc_4}}</td>
8281
</tr>
82+
<tr>
83+
<td>Left V -1000/-770</td>
84+
<td>{{n_pix_5}}</td>
85+
<td>{{vol_5}}</td>
86+
<td>{{perc_5}}</td>
87+
</tr>
88+
<tr>
89+
<td>Left V -1000/-920</td>
90+
<td>{{n_pix_6}}</td>
91+
<td>{{vol_6}}</td>
92+
<td>{{perc_6}}</td>
93+
</tr>
94+
<tr>
95+
<td>Right V -1000/-770</td>
96+
<td>{{n_pix_7}}</td>
97+
<td>{{vol_7}}</td>
98+
<td>{{perc_7}}</td>
99+
</tr>
100+
<tr>
101+
<td>Right V -1000/-920</td>
102+
<td>{{n_pix_8}}</td>
103+
<td>{{vol_8}}</td>
104+
<td>{{perc_8}}</td>
105+
</tr>
83106
</tbody>
84107
</table>
85108
<p>&nbsp;</p>

0 commit comments

Comments
 (0)