-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_table.py
492 lines (434 loc) · 16.9 KB
/
generate_table.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import itertools
import os
import numpy as np
from pylatex import Tabular, MultiColumn, MultiRow
from pylatex.base_classes import Container
from pylatex.utils import bold, NoEscape
from scipy import stats
from analyse_result import summarise_result, load_result_dict, summarise_base_ins_result
from statstics import compute_cross_ins_p_value
from utils.train_eval_utils import get_parser
def get_result_table(ins, metric, by):
args = get_parser()
args.novel_ins = ins
args.query_ins = args.novel_ins
table = start_table(header=["model"], metric=metric, by=by)
args.model = "finetune"
add_exp(args, table, exp_name="3d_finetune", metric=metric, by=by)
args.model = "baseline_2d"
add_exp(args, table, exp_name="2d", metric=metric, by=by)
args.model = "ours"
if args.novel_ins == 3:
args.con, args.align = False, False
add_exp(args, table, exp_name="3d", metric=metric, by=by)
args.con, args.align = True, False
add_exp(args, table, exp_name="3d_con", metric=metric, by=by)
args.con, args.align = False, True
add_exp(args, table, exp_name="3d_align", metric=metric, by=by)
args.con, args.align = True, True
add_exp(args, table, exp_name="3d_con_align", metric=metric, by=by)
add_exp(args, table, exp_name="3d_supervised", metric=metric, by=by, supervised=True)
doc = Table(data=table)
doc.generate_tex(f"./table/ins{args.novel_ins}_{metric}")
def get_base_ins_result_table(ins, metric, by, base_only=True):
args = get_parser()
args.novel_ins = ins
args.query_ins = args.novel_ins
table = start_table(header=["model"], metric=metric, by=by, base_only=base_only)
# args.model = "finetune"
# add_exp(args, table, exp_name="3d_finetune", metric=metric, by=by)
args.model = "baseline_2d"
add_base_ins_exp(args, table, exp_name="2d", metric=metric, by=by, base_only=base_only)
args.model = "ours"
args.con, args.align = True, True
add_base_ins_exp(args, table, exp_name="3d_con_align", metric=metric, by=by, base_only=base_only)
# add_exp(args, table, exp_name="3d_supervised", metric=metric, by=by, supervised=True)
doc = Table(data=table)
doc.generate_tex(f"./table/base_ins_ins{args.novel_ins}_{metric}")
def add_base_ins_exp(args, table, exp_name, metric, by="fold", supervised=False, base_only=False):
cat_list = ["base"] if base_only else ["all", "base", "novel"]
metric_dict = {}
for m in metric:
result = summarise_base_ins_result(args, m, supervised=supervised)
print("summarised")
row_dict = {}
for cat in cat_list:
if by == "fold":
row = np.array([
*[result[f"fold{fold}"][cat] for fold in range(1, 5)], result["mean"][cat]
])
elif by == "class":
row = np.array([
*[result[cls][cat] for cls in [1, 5, 2, 6, 3, 7, 4, 8]], result["mean"][cat]
])
else:
raise ValueError(f"unrecognised by: {by}")
# convert dice to % value
if m == "dice":
row = row * 100
row_dict[cat] = row
if not base_only:
row_dict[NoEscape('$\Delta$')] = (row_dict["novel"] - row_dict["base"]) / row_dict["novel"] * 100
metric_dict[m] = row_dict
column_list = cat_list if base_only else cat_list + [NoEscape('$\Delta$')]
for i, cat in enumerate(column_list):
row = list(
itertools.chain.from_iterable(
[metric_dict[m][cat] for m in metric]
)
)
row = ["{:.2f}".format(i) for i in row]
if cat == NoEscape('$\Delta$'):
row = [f"{v}%" for v in row]
if i == 0:
if base_only:
table.add_row(
(MultiRow(1, data=exp_name), *row)
)
else:
table.add_row(
(MultiRow(1 if supervised else 4, data=exp_name), cat, *row)
)
else:
if base_only:
table.add_row(("", *row))
else:
table.add_row(("", cat, *row))
table.add_hline()
def get_k_shot_ablation_table(metric, by):
args = get_parser()
args.novel_ins = 3
args.query_ins = args.novel_ins
args.model = "ours"
args.con, args.align = True, True
table = start_table(header=["# of shot"], metric=metric, by=by)
for shot in range(1, 5):
args.shot = shot
add_exp(args, table, exp_name=shot, metric=metric, by=by)
doc = Table(data=table)
doc.generate_tex(f"./table/k_shot_ablation_{metric}")
def get_training_size_ablation_table(metric, by):
args = get_parser()
args.novel_ins = 3
args.query_ins = args.novel_ins
args.model = "ours"
args.con, args.align = True, True
table = start_table(header=["training", "data"], metric=metric, by=by)
args.train_ratio = 1.
add_exp(args, table, exp_name="whole", metric=metric, by=by)
args.train_ratio = 0.5
add_exp(args, table, exp_name="half", metric=metric, by=by)
args.train_ratio = "ins1only"
add_exp(args, table, exp_name="half_single_ins", metric=metric, by=by)
args.train_ratio = 0.25
add_exp(args, table, exp_name="quarter", metric=metric, by=by)
doc = Table(data=table)
doc.generate_tex(f"./table/training_size_ablation_{metric}")
def start_table(header, metric, by="fold", base_only=False):
"""
Generate table head
:param header:
:param metric
:return: table
"""
metric_name_dict = {
"dice": "Dice (%)",
"hausdorff": "95% Hausdorff distance (mm)",
"surface_distance": "Average Surface Distance (mm)"
}
if by == "fold":
if base_only:
table = Tabular('|c|' + 'ccccc|' * len(metric))
table.add_hline()
table.add_row((
MultiRow(2, data=header[0]) if len(header) == 1 else header[0],
*[MultiColumn(5, align='c|', data=metric_name_dict[m]) for m in metric]
))
row = [
"" if len(header) == 1 else header[1],
*([*[f"fold{fold}" for fold in range(1, 5)], "mean"] * len(metric)),
]
else:
table = Tabular('|c|c|' + 'ccccc|' * len(metric))
table.add_hline()
table.add_row((
MultiRow(2, data=header[0]) if len(header) == 1 else header[0], MultiRow(2, data="s_ins"),
*[MultiColumn(5, align='c|', data=metric_name_dict[m]) for m in metric]
))
row = [
"" if len(header) == 1 else header[1], "",
*([*[f"fold{fold}" for fold in range(1, 5)], "mean"] * len(metric)),
]
table.add_row(row)
table.add_hline()
return table
elif by == "class":
if base_only:
table = Tabular('|c|' + 'cc|cc|cc|cc|c|' * len(metric))
table.add_hline()
table.add_row((
MultiRow(len(header), data=header[0]),
*[MultiColumn(9, align='c|', data=metric_name_dict[m]) for m in metric]
))
table.add_hline(start=3)
row = [
"",
*([*[MultiColumn(2, align='c|', data=f"fold{fold}") for fold in range(1, 5)], "mean"] * len(metric))
]
table.add_row(row)
else:
table = Tabular('|c|c|' + 'cc|cc|cc|cc|c|' * len(metric))
table.add_hline()
table.add_row((
MultiRow(len(header), data=header[0]), MultiRow(4, data="s_ins"),
*[MultiColumn(9, align='c|', data=metric_name_dict[m]) for m in metric]
))
table.add_hline(start=3)
row = [
"", "",
*([*[MultiColumn(2, align='c|', data=f"fold{fold}") for fold in range(1, 5)], "mean"] * len(metric))
]
table.add_row(row)
organ_list = [("bladder", ""), ("bone", ""), ("obturator", "internus"), ("transition", "zone"),
("central", "gland"), ("rectum", ""), ("seminal", "vesicle"), ("neurovascular", "bundle")]
for i in range(2):
if base_only:
row = [
MultiRow(2, data=header[1]) if i == 0 and len(header) == 2 else "",
*([
*[organ_list[c][i] for c in [0, 4, 1, 5, 2, 6, 3, 7]], ""
] * len(metric))
]
else:
row = [
MultiRow(2, data=header[1]) if i == 0 and len(header) == 2 else "", "",
*([
*[organ_list[c][i] for c in [0, 4, 1, 5, 2, 6, 3, 7]], ""
] * len(metric))
]
table.add_row(row)
table.add_hline()
return table
def add_exp(args, table, exp_name, metric, by="fold", supervised=False):
cat_list = ["N/A"] if supervised else ["all", "base", "novel"]
metric_dict = {}
for m in metric:
result = summarise_result(args, m, supervised=supervised)
print(result)
row_dict = {}
for cat in cat_list:
if by == "fold":
row = np.array([
*[result[f"fold{fold}"][cat] for fold in range(1, 5)], result["mean"][cat]
])
elif by == "class":
row = np.array([
*[result[cls][cat] for cls in [1, 5, 2, 6, 3, 7, 4, 8]], result["mean"][cat]
])
else:
raise ValueError(f"unrecognised by: {by}")
# convert dice to % value
if m == "dice":
row = row * 100
row_dict[cat] = row
if not supervised:
row_dict[NoEscape('$\Delta$')] = (row_dict["novel"] - row_dict["base"]) / row_dict["novel"] * 100
metric_dict[m] = row_dict
column_list = cat_list if supervised else cat_list + [NoEscape('$\Delta$')]
for i, cat in enumerate(column_list):
row = list(
itertools.chain.from_iterable(
[metric_dict[m][cat] for m in metric]
)
)
row = ["{:.2f}".format(i) for i in row]
if cat == NoEscape('$\Delta$'):
row = [f"{v}%" for v in row]
if i == 0:
table.add_row((MultiRow(1 if supervised else 4, data=exp_name), cat, *row))
else:
table.add_row(("", cat, *row))
table.add_hline()
def get_ins_correlation(model):
"""
p-value refers to p-value from two tailed t-test performed per
query institution between the Dice scores where support institution
equals query institution and the max Dice scores achieved among
all support institutions.
:param model:
:return:
"""
args = get_parser()
args.novel_ins = 3
args.model = model
args.con, args.align = True, True
table = Tabular('|cc|ccccccc|ccc|')
table.add_hline()
table.add_row((
"", "", MultiColumn(7, align='c|', data='s_ins'), "std", "mean", "p-value"
))
table.add_row((
"", "", "ins1", "ins2", "ins3", "ins4", "ins5", "ins6", "ins7", "", "", ""
))
table.add_hline()
p_value_list = compute_cross_ins_p_value(model)
for query_ins in range(1, 8):
args.query_ins = query_ins
result = summarise_result(args, metric="dice") # {cls: {ins: v}}
row = [result["mean"][support_ins] for support_ins in range(1, 8)] # 7
row = [i * 100 for i in row]
# add std
row = [*row, np.std(np.array(row)), np.mean(np.array(row))]
# bold row maximum
print(row)
max = np.max(np.array(row))
row = ["{:.2f}".format(i) if i != max else bold("{:.2f}".format(i)) for i in row]
p_value = p_value_list[query_ins - 1]
row.append("{: .2e}".format(p_value) if p_value < 0.01 else "{: .2f}".format(p_value))
if query_ins == 1:
table.add_row((
MultiRow(7, data='q_ins'),
f"ins{query_ins}",
*row
))
else:
table.add_row(("", f"ins{query_ins}", *row))
table.add_hline()
doc = Table(data=table)
doc.generate_tex(f"./table/{model}_cross_ins_dice")
def get_cross_ins_p_value(model):
"""
p-value from two tailed t-test performed per query institution
between the Dice scores where support institution equals query
institution and the Dice scores achieved per support
institution.
:param model:
:return:
"""
args = get_parser()
args.novel_ins = 3
args.model = model
args.con, args.align = True, True
table = Tabular('|cc|ccccccc|')
table.add_hline()
table.add_row((
"", "", MultiColumn(7, align='c|', data='s_ins')
))
table.add_row((
"", "", "ins1", "ins2", "ins3", "ins4", "ins5", "ins6", "ins7"
))
table.add_hline()
for query_ins in range(1, 8):
args.query_ins = query_ins
result_dict = {}
for fold in range(1, 5):
args.fold = fold
result_dict.update(
load_result_dict(args, "dice")
)
# {class: {name: {ins: v}}}
same_ins_list = [
ins_dict[query_ins]
for _, name_dict in result_dict.items()
for _, ins_dict in name_dict.items()
]
row = []
for ins in range(1, 8):
if ins == query_ins:
p_value_string = "N/A"
else:
ins_list = [
ins_dict[ins]
for _, name_dict in result_dict.items()
for _, ins_dict in name_dict.items()
]
p_value = stats.ttest_rel(ins_list, same_ins_list)[1]
p_value_string = "{: .2e}".format(p_value) if p_value < 0.01 else "{: .2f}".format(p_value)
if p_value > 0.05:
p_value_string = bold(p_value_string)
row.append(p_value_string)
if query_ins == 1:
table.add_row((
MultiRow(7, data='q_ins'),
f"ins{query_ins}",
*row
))
else:
table.add_row(("", f"ins{query_ins}", *row))
table.add_hline()
doc = Table(data=table)
doc.generate_tex(f"./table/{model}_pvalue")
def get_p_value():
"""
p-value from two tailed t-test performed per query institution
except 5,6,7 between the Dice scores where support institution
equals query institution and the max Dice scores achieved among
all support institutions except 5,6,7 .
:return:
"""
args = get_parser()
args.novel_ins = 3
args.con, args.align = True, True
table = Tabular('|c|cccc|')
table.add_hline()
table.add_row((
MultiRow(2, data="model"), MultiColumn(4, align='c|', data='q_ins')
))
table.add_row((
"", "ins1", "ins2", "ins3", "ins4"
))
table.add_hline()
for model in ["ours", "baseline_2d"]:
args.model = model
row = []
for query_ins in range(1, 5):
args.query_ins = query_ins
result_dict = {}
for fold in range(1, 5):
args.fold = fold
result_dict.update(
load_result_dict(args, "dice")
)
# {class: {name: {ins: v}}}
same_ins_list = [
ins_dict[query_ins]
for _, name_dict in result_dict.items()
for _, ins_dict in name_dict.items()
]
diff_ins_list = [
max(np.array([ins_dict[ins] for ins in range(1, 5) if ins != query_ins]))
for _, name_dict in result_dict.items()
for _, ins_dict in name_dict.items()
]
print(len(same_ins_list), len(diff_ins_list))
print(np.array(same_ins_list) - np.array(diff_ins_list))
p_value = stats.ttest_rel(same_ins_list, diff_ins_list)[1]
print(p_value)
p_value_string = "{: .2e}".format(p_value) if p_value < 0.01 else "{: .2f}".format(p_value)
if p_value > 0.05:
p_value_string = bold(p_value_string)
row.append(p_value_string)
table.add_row((model, *row))
table.add_hline()
doc = Table(data=table)
doc.generate_tex(f"./table/pvalue")
class Table(Container):
def dumps(self):
content = self.dumps_content()
return content
if __name__ == '__main__':
if not os.path.exists("./table"):
os.mkdir("table")
by = "fold"
for metric in [["dice", "hausdorff"], ["dice"]]:
get_base_ins_result_table(ins=3, metric=metric, by=by)
# by = "class"
# for metric in [["dice", "hausdorff"], ["dice"]]:
# get_result_table(ins=3, metric=metric, by=by)
# get_result_table(ins=4, metric=metric, by=by)
# get_training_size_ablation_table(metric=metric, by=by)
# get_k_shot_ablation_table(metric=metric, by=by)
# for model in ["ours", "baseline_2d"]:
# get_ins_correlation(model)
# get_cross_ins_p_value(model)
# get_p_value()