-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathplotting.py
2139 lines (1885 loc) · 96.8 KB
/
plotting.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2019 Division of Medical Image Computing, German Cancer Research Center (DKFZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import matplotlib
# matplotlib.rcParams['font.family'] = ['serif']
# matplotlib.rcParams['font.serif'] = ['Times New Roman']
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
matplotlib.use('Agg') #complains with spyder editor, bc spyder imports mpl at startup
from matplotlib.ticker import FormatStrFormatter
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.patches as mpatches
from matplotlib.ticker import StrMethodFormatter, ScalarFormatter
import SimpleITK as sitk
from tensorboard.backend.event_processing.event_multiplexer import EventMultiplexer
import sys
import os
import warnings
import time
from copy import deepcopy
import numpy as np
import pandas as pd
import scipy.interpolate as interpol
from utils.exp_utils import IO_safe
warnings.filterwarnings("ignore", module="matplotlib.image")
def make_colormap(seq):
""" Return a LinearSegmentedColormap
seq: a sequence of floats and RGB-tuples. The floats should be increasing
and in the interval (0,1).
"""
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
if isinstance(item, float):
r1, g1, b1 = seq[i - 1]
r2, g2, b2 = seq[i + 1]
cdict['red'].append([item, r1, r2])
cdict['green'].append([item, g1, g2])
cdict['blue'].append([item, b1, b2])
return mcolors.LinearSegmentedColormap('CustomMap', cdict)
bw_cmap = make_colormap([(1.,1.,1.), (0.,0.,0.)])
#------------------------------------------------------------------------
#------------- plotting functions, not all are used ---------------------
def shape_small_first(shape):
"""sort a tuple so that the smallest entry is swapped to the beginning
"""
if len(shape) <= 2: # no changing dimensions if channel-dim is missing
return shape
smallest_dim = np.argmin(shape)
if smallest_dim != 0: # assume that smallest dim is color channel
new_shape = np.array(shape) # to support mask indexing
new_shape = (new_shape[smallest_dim],
*new_shape[(np.arange(len(shape), dtype=int) != smallest_dim)])
return new_shape
else:
return shape
def RGB_to_rgb(RGB):
rgb = np.array(RGB) / 255.
return rgb
def mod_to_rgb(arr, cmap=None):
"""convert a single-channel modality img to 3-color-channel img.
:param arr: input img, expected in shape (b,c,)x,y with c=1
:return: img of shape (...,c') with c'=3
"""
if len(arr.shape) == 3:
arr = np.squeeze(arr)
elif len(arr.shape) != 2:
raise Exception("Invalid input arr shape: {}".format(arr.shape))
if cmap is None:
cmap = "gray"
norm = matplotlib.colors.Normalize()
norm.autoscale(arr)
arr = norm(arr)
arr = np.stack((arr,) * 3, axis=-1)
return arr
def to_rgb(arr, cmap):
"""
Transform an integer-labeled segmentation map using an rgb color-map.
:param arr: img_arr w/o a color-channel
:param cmap: dictionary mapping from integer class labels to rgb values
:return: img of shape (...,c)
"""
new_arr = np.zeros(shape=(arr.shape) + (3,))
for l in cmap.keys():
ixs = np.where(arr == l)
new_arr[ixs] = np.array([cmap[l][i] for i in range(3)])
return new_arr
def to_rgba(arr, cmap):
"""
Transform an integer-labeled segmentation map using an rgba color-map.
:param arr: img_arr w/o a color-channel
:param cmap: dictionary mapping from integer class labels to rgba values
:return: new array holding rgba-image
"""
new_arr = np.zeros(shape=(arr.shape) + (4,))
for lab, val in cmap.items():
# in case no alpha, complement with 100% alpha
if len(val) == 3:
cmap[lab] = (*val, 1.)
assert len(cmap[lab]) == 4, "cmap has color with {} entries".format(len(val))
for lab in cmap.keys():
ixs = np.where(arr == lab)
rgb = np.array(cmap[lab][:3])
new_arr[ixs] = np.append(rgb, cmap[lab][3])
return new_arr
def bin_seg_to_rgba(arr, color):
"""
Transform a continuously labelled binary segmentation map using an rgba color-map.
values are expected to be 0-1, will give alpha-value
:param arr: img_arr w/o a color-channel
:param color: color to give img
:return: new array holding rgba-image
"""
new_arr = np.zeros(shape=(arr.shape) + (4,))
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
new_arr[i][j] = (*color, arr[i][j])
return new_arr
def suppress_axes_lines(ax):
"""
:param ax: pyplot axes object
"""
ax.axes.get_xaxis().set_ticks([])
ax.axes.get_yaxis().set_ticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
return
def label_bar(ax, rects, labels=None, colors=None, fontsize=10):
"""Attach a text label above each bar displaying its height
:param ax:
:param rects: rectangles as returned by plt.bar()
:param labels:
:param colors:
"""
for ix, rect in enumerate(rects):
height = rect.get_height()
if labels is not None and labels[ix] is not None:
label = labels[ix]
else:
label = '{:g}'.format(height)
if colors is not None and colors[ix] is not None and np.any(np.array(colors[ix])<1):
color = colors[ix]
else:
color = 'black'
ax.text(rect.get_x() + rect.get_width() / 2., 1.007 * height, label, color=color, ha='center', va='bottom',
bbox=dict(facecolor=(1., 1., 1.), edgecolor='none', clip_on=True, pad=0, alpha=0.75), fontsize=fontsize)
def draw_box_into_arr(arr, box_coords, box_color=None, lw=2):
"""
:param arr: imgs shape, (3,y,x)
:param box_coords: (x1,y1,x2,y2), in ascending order
:param box_color: arr of shape (3,)
:param lw: linewidth in pixels
"""
if box_color is None:
box_color = [1., 0.4, 0.]
(x1, y1, x2, y2) = box_coords[:4]
arr = np.swapaxes(arr, 0, -1)
arr[..., y1:y2, x1:x1 + lw, :], arr[..., y1:y2 + lw, x2:x2 + lw, :] = box_color, box_color
arr[..., y1:y1 + lw, x1:x2, :], arr[..., y2:y2 + lw, x1:x2, :] = box_color, box_color
arr = np.swapaxes(arr, 0, -1)
return arr
def draw_boxes_into_batch(imgs, batch_boxes, type2color=None, cmap=None):
"""
:param imgs: either the actual batch imgs or a tuple with shape of batch imgs,
need to have 3 color channels, need to be rgb;
"""
if isinstance(imgs, tuple):
img_oshp = imgs
imgs = None
else:
img_oshp = imgs[0].shape
img_shp = shape_small_first(img_oshp) # c,x/y,y/x now
imgs = np.reshape(imgs, (-1, *img_shp))
box_imgs = np.empty((len(batch_boxes), *(img_shp)))
for sample, boxes in enumerate(batch_boxes):
# imgs in batch have shape b,c,x,y, swap c to end
sample_img = np.full(img_shp, 1.) if imgs is None else imgs[sample]
for box in boxes:
if len(box["box_coords"]) > 0:
if type2color is not None and "box_type" in box.keys():
sample_img = draw_box_into_arr(sample_img, box["box_coords"].astype(np.int32),
type2color[box["box_type"]])
else:
sample_img = draw_box_into_arr(sample_img, box["box_coords"].astype(np.int32))
box_imgs[sample] = sample_img
return box_imgs
def plot_prediction_hist(cf, spec_df, outfile, title=None, fs=11, ax=None):
labels = spec_df.class_label.values
preds = spec_df.pred_score.values
type_list = spec_df.det_type.tolist() if hasattr(spec_df, "det_type") else None
if title is None:
title = outfile.split('/')[-1] + ' count:{}'.format(len(labels))
close=False
if ax is None:
fig = plt.figure(tight_layout=True)
ax = fig.add_subplot(1,1,1)
close=True
ax.set_yscale('log')
ax.set_xlabel("Prediction Score", fontsize=fs)
ax.set_ylabel("Occurences", fontsize=fs)
ax.hist(preds[labels == 0], alpha=0.3, color=cf.red, range=(0, 1), bins=50, label="fp")
ax.hist(preds[labels == 1], alpha=0.3, color=cf.blue, range=(0, 1), bins=50, label="fn at score 0 and tp")
ax.axvline(x=cf.min_det_thresh, alpha=1, color=cf.orange, linewidth=1.5, label="min det thresh")
if type_list is not None:
fp_count = type_list.count('det_fp')
fn_count = type_list.count('det_fn')
tp_count = type_list.count('det_tp')
pos_count = fn_count + tp_count
title += '\ntp:{} fp:{} fn:{} pos:{}'.format(tp_count, fp_count, fn_count, pos_count)
ax.set_title(title, fontsize=fs)
ax.tick_params(axis='both', which='major', labelsize=fs)
ax.tick_params(axis='both', which='minor', labelsize=fs)
if close:
ax.legend(loc="best", fontsize=fs)
if cf.server_env:
IO_safe(plt.savefig, fname=outfile, _raise=False)
else:
plt.savefig(outfile)
pass
plt.close()
def plot_wbc_n_missing(cf, df, outfile, fs=11, ax=None):
""" WBC (weighted box clustering) has parameter n_missing, which shows how many boxes are missing per cluster.
This function plots the average relative amount of missing boxes sorted by cluster score.
:param cf: config.
:param df: dataframe.
:param outfile: path to save image under.
:param fs: fontsize.
:param ax: axes object.
"""
bins = np.linspace(0., 1., 10)
names = ["{:.1f}".format((bins[i]+(bins[i+1]-bins[i])/2.)*100) for i in range(len(bins)-1)]
classes = df.pred_class.unique()
colors = [cf.class_id2label[cl_id].color for cl_id in classes]
binned_df = df.copy()
binned_df.loc[:,"pred_score"] = pd.cut(binned_df["pred_score"], bins)
close=False
if ax is None:
ax = plt.subplot()
close=True
width = 1 / (len(classes) + 1)
group_positions = np.arange(len(names))
legend_handles = []
for ix, cl_id in enumerate(classes):
cl_df = binned_df[binned_df.pred_class==cl_id].groupby("pred_score").agg({"cluster_n_missing": 'mean'})
ax.bar(group_positions + ix * width, cl_df.cluster_n_missing.values, width=width, color=colors[ix],
alpha=0.4 + ix / 2 / len(classes), edgecolor=colors[ix])
legend_handles.append(mpatches.Patch(color=colors[ix], label=cf.class_dict[cl_id]))
title = "Fold {} WBC Missing Preds\nAverage over scores and classes: {:.1f}%".format(cf.fold, df.cluster_n_missing.mean())
ax.set_title(title, fontsize=fs)
ax.legend(handles=legend_handles, title="Class", loc="best", fontsize=fs, title_fontsize=fs)
ax.set_xticks(group_positions + (len(classes) - 1) * width / 2)
# ax.xaxis.set_major_formatter(StrMethodFormatter('{x:.1f}')) THIS WONT WORK... no clue!
ax.set_xticklabels(names)
ax.tick_params(axis='both', which='major', labelsize=fs)
ax.tick_params(axis='both', which='minor', labelsize=fs)
ax.set_axisbelow(True)
ax.grid()
ax.set_ylabel(r"Average Missing Preds per Cluster (%)", fontsize=fs)
ax.set_xlabel("Prediction Score", fontsize=fs)
if close:
if cf.server_env:
IO_safe(plt.savefig, fname=outfile, _raise=False)
else:
plt.savefig(outfile)
plt.close()
def plot_stat_curves(cf, stats, outfile, fill=False):
""" Plot precision-recall and/or receiver-operating-characteristic curve(s).
:param cf: config.
:param stats: statistics as supplied by Evaluator.
:param outfile: path to save plot under.
:param fill: whether to colorize space between plot and x-axis.
:return:
"""
for c in ['roc', 'prc']:
plt.figure()
empty_plot = True
for ix, s in enumerate(stats):
if s[c] is not np.nan:
plt.plot(s[c][1], s[c][0], label=s['name'] + '_' + c, marker=None,
color=cf.color_palette[ix%len(cf.color_palette)])
empty_plot = False
if fill:
plt.fill_between(s[c][1], s[c][0], alpha=0.33, color=cf.color_palette[ix%len(cf.color_palette)])
if not empty_plot:
plt.title(outfile.split('/')[-1] + '_' + c)
plt.legend(loc=3 if c == 'prc' else 4)
plt.ylabel('precision' if c == 'prc' else '1-spec.')
plt.ylim((0.,1))
plt.xlabel('recall')
plt.savefig(outfile + '_' + c)
plt.close()
def plot_grouped_bar_chart(cf, bar_values, groups, splits, colors=None, alphas=None, errors=None, ylabel='', xlabel='',
xticklabels=None, yticks=None, yticklabels=None, ylim=None, label_format="{:.3f}",
title=None, ax=None, out_file=None, legend=False, fs=11):
""" Plot a categorically grouped bar chart.
:param cf: config.
:param bar_values: values of the bars.
:param groups: groups/categories that bars belong to.
:param splits: splits within groups, i.e., names of bars.
:param colors: colors.
:param alphas: 1-opacity.
:param errors: values for errorbars.
:param ylabel: label of y-axis.
:param xlabel: label of x-axis.
:param title: plot title.
:param ax: axes object to draw into. if None, new is created.
:param out_file: path to save plot.
:param legend: whether to show a legend.
:param fs: fontsize.
:return: legend handles.
"""
bar_values = np.array(bar_values)
if alphas is None:
alphas = [1.,] * len(splits)
if colors is None:
colors = [cf.color_palette[ix%len(cf.color_palette)] for ix in range(len(splits))]
if errors is None:
errors = np.zeros_like(bar_values)
# patterns = ('/', '\\', '*', 'O', '.', '-', '+', 'x', 'o')
# patterns = tuple([patterns[ix%len(patterns)] for ix in range(len(splits))])
close=False
if ax is None:
ax = plt.subplot()
close=True
width = 1 / (len(splits) +0.25)
group_positions = np.arange(len(groups))
for ix, split in enumerate(splits):
rects = ax.bar(group_positions + ix * width, bar_values[ix], width=width, color=(*colors[ix], 0.8),
edgecolor=colors[ix], yerr=errors[ix], ecolor=(*np.array(colors[ix])*0.8, 1.), capsize=5)
# for ix, bar in enumerate(rects):
# bar.set_hatch(patterns[ix])
labels = [label_format.format(val) for val in bar_values[ix]]
label_bar(ax, rects, labels, [colors[ix]]*len(labels), fontsize=fs)
legend_handles = [mpatches.Patch(color=colors[ix], alpha=alphas[ix], label=split) for ix, split in
enumerate(splits)]
if legend:
ax.legend(handles=legend_handles, fancybox=True, framealpha=1., loc="lower center")
legend_handles = [(colors[ix], alphas[ix], split) for ix, split in enumerate(splits)]
if title is not None:
ax.set_title(title, fontsize=fs)
ax.set_xticks(group_positions + (len(splits) - 1) * width / 2)
if xticklabels is None:
ax.set_xticklabels(groups, fontsize=fs)
else:
ax.set_xticklabels(xticklabels, fontsize=fs)
ax.set_axisbelow(True)
ax.set_xlabel(xlabel, fontsize=fs)
ax.tick_params(labelsize=fs)
ax.grid(axis='y')
ax.set_ylabel(ylabel, fontsize=fs)
if yticks is not None:
ax.set_yticks(yticks)
if yticklabels is not None:
ax.set_yticklabels(yticklabels, fontsize=fs)
if ylim is not None:
ax.set_ylim(ylim)
if out_file is not None:
plt.savefig(out_file, dpi=600)
if close:
plt.close()
return legend_handles
def plot_binned_rater_dissent(cf, binned_stats, out_file=None, ax=None, legend=True, fs=11):
""" LIDC-specific plot: rater disagreement as standard deviations within each bin.
:param cf: config.
:param binned_stats: list, ix==bin_id, item: [(roi_mean, roi_std, roi_max, roi_bin_id-roi_max_bin_id) for roi in bin]
:return:
"""
dissent = [np.array([roi[1] for roi in bin]) for bin in binned_stats]
avg_dissent_first_degree = [np.mean(bin) for bin in dissent]
groups = list(cf.bin_id2label.keys())
splits = [r"$1^{st}$ std. dev.",]
colors = [cf.bin_id2label[bin_id].color[:3] for bin_id in groups]
#colors = [cf.blue for bin_id in groups]
alphas = [0.9,]
#patterns = ('/', '\\', '*', 'O', '.', '-', '+', 'x', 'o')
#patterns = tuple([patterns[ix%len(patterns)] for ix in range(len(splits))])
close=False
if ax is None:
ax = plt.subplot()
close=True
width = 1/(len(splits)+1)
group_positions = np.arange(len(groups))
#total_counts = [df.loc[split].sum() for split in splits]
dissent = np.array(avg_dissent_first_degree)
ix=0
rects = ax.bar(group_positions+ix*width, dissent, color=colors, alpha=alphas[ix],
edgecolor=colors)
#for ix, bar in enumerate(rects):
#bar.set_hatch(patterns[ix])
labels = ["{:.2f}".format(diss) for diss in dissent]
label_bar(ax, rects, labels, colors, fontsize=fs)
bin_edge_color = cf.blue
ax.axhline(y=0.5, color=bin_edge_color)
ax.text(2.5, 0.38, "bin edge", color=cf.white, fontsize=fs, horizontalalignment="center",
bbox=dict(boxstyle='round', facecolor=(*bin_edge_color, 0.85), edgecolor='none', clip_on=True, pad=0))
if legend:
legend_handles = [mpatches.Patch(color=cf.blue ,alpha=alphas[ix], label=split) for ix, split in enumerate(splits)]
ax.legend(handles=legend_handles, loc='lower center', fontsize=fs)
title = "LIDC-IDRI: Average Std Deviation per Lesion"
plt.title(title)
ax.set_xticks(group_positions + (len(splits)-1)*width/2)
ax.set_xticklabels(groups, fontsize=fs)
ax.set_axisbelow(True)
#ax.tick_params(axis='both', which='major', labelsize=fs)
#ax.tick_params(axis='both', which='minor', labelsize=fs)
ax.grid()
ax.set_ylabel(r"Average Dissent (MS)", fontsize=fs)
ax.set_xlabel("binned malignancy-score value (ms)", fontsize=fs)
ax.tick_params(labelsize=fs)
if out_file is not None:
plt.savefig(out_file, dpi=600)
if close:
plt.close()
return
def plot_confusion_matrix(cf, cm, out_file=None, ax=None, fs=11, cmap=plt.cm.Blues, color_bar=True):
""" Plot a confusion matrix.
:param cf: config.
:param cm: confusion matrix, e.g., as supplied by metrics.confusion_matrix from scikit-learn.
:return:
"""
close=False
if ax is None:
ax = plt.subplot()
close=True
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
if color_bar:
ax.figure.colorbar(im, ax=ax)
# Rotate the tick labels and set their alignment.
#plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
fmt = '.0%' if np.mod(cm, 1).any() else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
ax.set_ylabel(r"Binned Mean MS", fontsize=fs)
ax.set_xlabel("Single-Annotator MS", fontsize=fs)
#ax.tick_params(labelsize=fs)
if close and out_file is not None:
plt.savefig(out_file, dpi=600)
if close:
plt.close()
else:
return ax
def plot_data_stats(cf, df, labels=None, out_file=None, ax=None, fs=11):
""" Plot data-set statistics. Shows target counts. Mainly used by Dataset Class in dataloader.py.
:param cf: configs obj
:param df: pandas dataframe
:param out_file: path to save fig in
"""
names = df.columns
if labels is not None:
colors = [label.color for name in names for label in labels if label.name==name]
else:
colors = [cf.color_palette[ix%len(cf.color_palette)] for ix in range(len(names))]
#patterns = ('/', '\\', '*', 'O', '.', '-', '+', 'x', 'o')
#patterns = tuple([patterns[ix%len(patterns)] for ix in range(len(splits))])
if ax is None:
fig, ax = plt.subplots(figsize=(14,6), dpi=300)
return_ax = False
else:
return_ax = True
plt.margins(x=0.01)
plt.subplots_adjust(bottom=0.15)
bar_positions = np.arange(len(names))
name_counts = df.sum()
total_count = name_counts.sum()
rects = ax.bar(bar_positions, name_counts, color=colors, alpha=0.9, edgecolor=colors)
labels = ["{:.0f}%".format(count/ total_count*100) for count in name_counts]
label_bar(ax, rects, labels, colors, fontsize=fs)
title= "Data Set RoI-Target Balance\nTotal #RoIs: {}".format(int(total_count))
ax.set_title(title, fontsize=fs)
ax.set_xticks(bar_positions)
rotation = "vertical" if np.any([len(str(name)) > 3 for name in names]) else None
if all([isinstance(name, (float, int)) for name in names]):
ax.set_xticklabels(["{:.2f}".format(name) for name in names], rotation=rotation, fontsize=fs)
else:
ax.set_xticklabels(names, rotation=rotation, fontsize=fs)
ax.set_axisbelow(True)
ax.grid()
ax.set_ylabel(r"#RoIs", fontsize=fs)
ax.set_xlabel(str(df._metadata[0]), fontsize=fs)
ax.tick_params(axis='both', which='major', labelsize=fs)
ax.tick_params(axis='both', which='minor', labelsize=fs)
if out_file is not None:
plt.savefig(out_file)
if return_ax:
return ax
else:
plt.close()
def plot_fold_stats(cf, df, labels=None, out_file=None, ax=None):
""" Similar as plot_data_stats but per single cross-val fold.
:param cf: configs obj
:param df: pandas dataframe
:param out_file: path to save fig in
"""
names = df.columns
splits = df.index
if labels is not None:
colors = [label.color for name in names for label in labels if label.name==name]
else:
colors = [cf.color_palette[ix%len(cf.color_palette)] for ix in range(len(names))]
#patterns = ('/', '\\', '*', 'O', '.', '-', '+', 'x', 'o')
#patterns = tuple([patterns[ix%len(patterns)] for ix in range(len(splits))])
if ax is None:
ax = plt.subplot()
return_ax = False
else:
return_ax = True
width = 1/(len(names)+1)
group_positions = np.arange(len(splits))
legend_handles = []
total_counts = [df.loc[split].sum() for split in splits]
for ix, name in enumerate(names):
rects = ax.bar(group_positions+ix*width, df.loc[:,name], width=width, color=colors[ix], alpha=0.9,
edgecolor=colors[ix])
#for ix, bar in enumerate(rects):
#bar.set_hatch(patterns[ix])
labels = ["{:.0f}%".format(df.loc[split, name]/ total_counts[ii]*100) for ii, split in enumerate(splits)]
label_bar(ax, rects, labels, [colors[ix]]*len(group_positions))
legend_handles.append(mpatches.Patch(color=colors[ix] ,alpha=0.9, label=name))
title= "Fold {} RoI-Target Balances\nTotal #RoIs: {}".format(cf.fold,
int(df.values.sum()))
plt.title(title)
ax.legend(handles=legend_handles)
ax.set_xticks(group_positions + (len(names)-1)*width/2)
ax.set_xticklabels(splits, rotation="vertical" if len(splits)>2 else None, size=12)
ax.set_axisbelow(True)
ax.grid()
ax.set_ylabel(r"#RoIs")
ax.set_xlabel("Set split")
if out_file is not None:
plt.savefig(out_file)
if return_ax:
return ax
plt.close()
def plot_batchgen_distribution(cf, pids, p_probs, balance_target, out_file=None):
"""plot top n_pids probabilities for drawing a pid into a batch.
:param cf: experiment config object
:param pids: sorted iterable of patient ids
:param p_probs: pid's drawing likelihood, order needs to match the one of pids.
:param out_file:
:return:
"""
n_pids = len(pids)
zip_sorted = np.array(sorted(list(zip(p_probs, pids)), reverse=True))
names, probs = zip_sorted[:n_pids,1], zip_sorted[:n_pids,0].astype('float32') * 100
try:
names = [str(int(n)) for n in names]
except ValueError:
names = [str(n) for n in names]
lowest_p = min(p_probs)*100
fig, ax = plt.subplots(1,1,figsize=(17,5), dpi=200)
rects = ax.bar(names, probs, color=cf.blue, alpha=0.9, edgecolor=cf.blue)
ax = plt.gca()
ax.text(0.8, 0.92, "Lowest prob.: {:.5f}%".format(lowest_p), transform=ax.transAxes, color=cf.white,
bbox=dict(boxstyle='round', facecolor=cf.blue, edgecolor='none', alpha=0.9))
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:g}'))
ax.set_xticklabels(names, rotation="vertical", fontsize=7)
plt.margins(x=0.01)
plt.subplots_adjust(bottom=0.15)
if balance_target=="class_targets":
balance_target = "Class"
elif balance_target=="lesion_gleasons":
balance_target = "GS"
ax.set_title(str(balance_target)+"-Balanced Train Generator: Sampling Likelihood per PID")
ax.set_axisbelow(True)
ax.grid(axis='y')
ax.set_ylabel("Sampling Likelihood (%)")
ax.set_xlabel("PID")
plt.tight_layout()
if out_file is not None:
plt.savefig(out_file)
plt.close()
def plot_batchgen_stats(cf, stats, empties, target_name, unique_ts, out_file=None):
"""Plot bar chart showing RoI frequencies and empty-sample count of batch stats recorded by BatchGenerator.
:param cf: config.
:param stats: statistics as supplied by BatchGenerator class.
:param out_file: path to save plot.
"""
total_samples = cf.num_epochs*cf.num_train_batches*cf.batch_size
if target_name=="class_targets":
target_name = "Class"
label_dict = {cl_id: label for (cl_id, label) in cf.class_id2label.items()}
elif target_name=="lesion_gleasons":
target_name = "Lesion's Gleason Score"
label_dict = cf.gs2label
elif target_name=="rg_bin_targets":
target_name = "Regression-Bin ID"
label_dict = cf.bin_id2label
else:
raise NotImplementedError
names = [label_dict[t_id].name for t_id in unique_ts]
colors = [label_dict[t_id].color for t_id in unique_ts]
title = "Training Target Frequencies"
title += "\nempty samples: {}".format(empties)
rects = plt.bar(names, stats['roi_counts'], color=colors, alpha=0.9, edgecolor=colors)
ax = plt.gca()
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:g}'))
ax.set_title(title)
ax.set_axisbelow(True)
ax.grid()
ax.set_ylabel(r"#RoIs")
ax.set_xlabel(target_name)
total_count = np.sum(stats["roi_counts"])
labels = ["{:.0f}%".format(count/total_count*100) for count in stats["roi_counts"]]
label_bar(ax, rects, labels, colors)
if out_file is not None:
plt.savefig(out_file)
plt.close()
def view_3D_array(arr, outfile, elev=30, azim=30):
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect("equal")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
ax.voxels(arr)
ax.view_init(elev=elev, azim=azim)
plt.savefig(outfile)
def view_batch(cf, batch, res_dict=None, out_file=None, legend=True, show_info=True, has_colorchannels=False,
isRGB=True, show_seg_ids="all", show_seg_pred=True, show_gt_boxes=True, show_gt_labels=False,
roi_items="all", sample_picks=None, vol_slice_picks=None, box_score_thres=None, plot_mods=True,
dpi=300, vmin=None, return_fig=False, get_time=True):
r""" View data and target entries of a batch.
Batch expected as dic with entries 'data' and 'seg' holding np.arrays of
size :math:`batch\_size \times modalities \times h \times w` for data
and :math:`batch\_size \times classes \times h \times w` or
:math:`batch\_size \times 1 \times h \times w` for segs.
Classes, even if just dummy, are always needed for plotting since they determine colors.
Pyplot expects dimensions in order y,x,chans (height, width, chans) for imshow.
:param cf: config.
:param batch: batch.
:param res_dict: results dictionary.
:param out_file: path to save plot.
:param legend: whether to show a legend.
:param show_info: whether to show text info about img sizes and type in plot.
:param has_colorchannels: whether image has color channels.
:param isRGB: if image is RGB.
:param show_seg_ids: "all" or None or list with seg classes to show (seg_ids)
:param show_seg_pred: whether to the predicted segmentation.
:param show_gt_boxes: whether to show ground-truth boxes.
:param show_gt_labels: whether to show labels of ground-truth boxes.
:param roi_items: which roi items to show: strings "all" or "targets". --> all roi items in cf.roi_items or only
those which are targets, or list holding keys/names of entries in cf.roi_items to plot additionally on roi boxes.
empty iterator to show none.
:param sample_picks: which indices of the batch to display. None for all.
:param vol_slice_picks: when batch elements are 3D: which slices to display. None for all, or tuples
("random", int: amt) / (float€[0,1]: fg_prob, int: amt) for random pick / fg_slices pick w probability fg_prob
of amt slices. fg pick requires gt seg.
:param box_score_thres: plot only boxes with pred_score > box_score_thres. None or 0. for no threshold.
:param plot_mods: whether to plot input modality/modalities.
:param dpi: graphics resolution.
:param vmin: min value for gray-scale cmap in imshow, set to a fix value for inter-batch normalization, or None for
intra-batch.
:param return_fig: whether to return created figure.
"""
stime = time.time()
# pfix = prefix, ptfix = postfix
patched_patient = 'patch_crop_coords' in list(batch.keys())
pfix = 'patient_' if patched_patient else ''
ptfix = '_2d' if (patched_patient and cf.dim == 2 and pfix + 'class_targets_2d' in batch.keys()) else ''
# -------------- get data, set flags -----------------
try:
btype = type(batch[pfix + 'data'])
data = batch[pfix + 'data'].astype("float32")
seg = batch[pfix + 'seg']
except AttributeError: # in this case: assume it's single-annotator ground truths
btype = type(batch[pfix + 'data'])
data = batch[pfix + 'data'].astype("float32")
seg = batch[pfix + 'seg'][0]
print("Showing only gts of rater 0")
data_init_shp, seg_init_shp = data.shape, seg.shape
seg = np.copy(seg) if show_seg_ids else None
plot_bg = batch['plot_bg'] if 'plot_bg' in batch.keys() and not isinstance(batch['plot_bg'], (int, float)) else None
plot_bg_chan = batch['plot_bg'] if 'plot_bg' in batch.keys() and isinstance(batch['plot_bg'], (int, float)) else 0
gt_boxes = batch[pfix+'bb_target'+ptfix] if pfix+'bb_target'+ptfix in batch.keys() and show_gt_boxes else None
class_targets = batch[pfix+'class_targets'+ptfix] if pfix+'class_targets'+ptfix in batch.keys() else None
cf_roi_items = [pfix+it+ptfix for it in cf.roi_items]
if roi_items == "all":
roi_items = [it for it in cf_roi_items]
elif roi_items == "targets":
roi_items = [it for it in cf_roi_items if 'targets' in it]
else:
roi_items = [it for it in cf_roi_items if it in roi_items]
if res_dict is not None:
seg_preds = res_dict["seg_preds"] if (show_seg_pred is not None and 'seg_preds' in res_dict.keys()
and show_seg_ids) else None
if '2D_boxes' in res_dict.keys():
assert cf.dim==2
pr_boxes = res_dict["2D_boxes"]
elif 'boxes' in res_dict.keys():
pr_boxes = res_dict["boxes"]
else:
pr_boxes = None
else:
seg_preds = None
pr_boxes = None
# -------------- get shapes, apply sample selection -----------------
(n_samples, mods, h, w), d = data.shape[:4], 0
z_ics = [slice(None)]
if has_colorchannels: #has to be 2D
data = np.transpose(data, axes=(0, 2, 3, 1)) # now b,y,x,c
mods = 1
else:
if len(data.shape) == 5: # 3dim case
d = data.shape[4]
if vol_slice_picks is None:
z_ics = np.arange(0, d)
elif hasattr(vol_slice_picks, "__iter__") and vol_slice_picks[0]=="random":
z_ics = np.random.choice(np.arange(0, d), size=min(vol_slice_picks[1], d), replace=False)
else:
z_ics = vol_slice_picks
sample_ics = range(n_samples)
# 8000 approx value of pixels that are displayable in one figure dim (pyplot has a render limit), depends on dpi however
if data.shape[0]*data.shape[2]*len(z_ics)>8000:
n_picks = max(1, int(8000/(data.shape[2]*len(z_ics))))
if len(z_ics)>1 and vol_slice_picks is None:
z_ics = np.random.choice(np.arange(0, data.shape[4]),
size=min(data.shape[4], max(1,int(8000/(n_picks*data.shape[2])))), replace=False)
if sample_picks is None:
sample_picks = np.random.choice(data.shape[0], n_picks, replace=False)
if sample_picks is not None:
sample_ics = [s for s in sample_picks if s in sample_ics]
n_samples = len(sample_ics)
if not plot_mods:
mods = 0
if show_seg_ids=="all":
show_seg_ids = np.unique(seg)
if seg_preds is not None and not type(show_seg_ids)==str:
seg_preds = np.copy(seg_preds)
seg_preds = np.where(np.isin(seg_preds, show_seg_ids), seg_preds, 0)
if seg is not None:
if not type(show_seg_ids)==str: #to save time
seg = np.where(np.isin(seg, show_seg_ids), seg, 0)
legend_items = {cf.seg_id2label[seg_id] for seg_id in np.unique(seg) if seg_id != 0} # add seg labels
else:
legend_items = set()
# -------------- setup figure -----------------
if isRGB:
data = RGB_to_rgb(data)
if plot_bg is not None:
plot_bg = RGB_to_rgb(plot_bg)
n_cols = mods
if seg is not None or gt_boxes is not None:
n_cols += 1
if seg_preds is not None or pr_boxes is not None:
n_cols += 1
n_rows = n_samples*len(z_ics)
grid = gridspec.GridSpec(n_rows, n_cols, wspace=0.01, hspace=0.0)
fig = plt.figure(figsize=((n_cols + 1)*2, n_rows*2), tight_layout=True)
title_fs = 12 # fontsize
sample_ics, z_ics = sorted(sample_ics), sorted(z_ics)
row = 0 # current row
for s_count, s_ix in enumerate(sample_ics):
for z_ix in z_ics:
col = 0 # current col
# ----visualise input data -------------
if has_colorchannels:
if plot_mods:
ax = fig.add_subplot(grid[row, col])
ax.imshow(data[s_ix][...,z_ix])
ax.axis("off")
if row == 0:
plt.title("Input", fontsize=title_fs)
if col == 0:
specs = batch.get('spec', batch['pid'])
intra_patient_ix = s_ix if type(z_ix) == slice else z_ix
ylabel = str(specs[s_ix])[-5:] + "/" + str(intra_patient_ix) if show_info else str(specs[s_ix])[-5:]
ax.set_ylabel("{:s}".format(ylabel), fontsize=title_fs) # show id-number
col += 1
bg_img = plot_bg[s_ix][...,z_ix] if plot_bg is not None else data[s_ix][...,z_ix]
else:
for mod in range(mods):
ax = fig.add_subplot(grid[row, col])
ax.imshow(data[s_ix, mod][...,z_ix], cmap="gray", vmin=vmin)
suppress_axes_lines(ax)
if row == 0:
plt.title("Mod. " + str(mod), fontsize=title_fs)
if col == 0:
specs = batch.get('spec', batch['pid'])
intra_patient_ix = s_ix if type(z_ix)==slice else z_ix
ylabel = str(specs[s_ix])[-5:]+"/"+str(intra_patient_ix) if show_info else str(specs[s_ix])[-5:]
ax.set_ylabel("{:s}".format(ylabel), fontsize=title_fs) # show id-number
col += 1
bg_img = plot_bg[s_ix][...,z_ix] if plot_bg is not None else data[s_ix, plot_bg_chan][...,z_ix]
# ---evtly visualise groundtruths-------------------
if seg is not None or gt_boxes is not None:
# img as bg for gt
ax = fig.add_subplot(grid[row, col])
ax.imshow(bg_img, cmap="gray", vmin=vmin)
if row == 0:
plt.title("Ground Truth", fontsize=title_fs)
if col == 0:
specs = batch.get('spec', batch['pid'])
intra_patient_ix = s_ix if type(z_ix) == slice else z_ix
ylabel = str(specs[s_ix])[-5:] + "/" + str(intra_patient_ix) if show_info else str(specs[s_ix])[-5:]
ax.set_ylabel("{:s}".format(ylabel), fontsize=title_fs) # show id-number
suppress_axes_lines(ax)
else:
plt.axis('off')
col += 1
if seg is not None and seg.shape[1] == 1:
ax.imshow(to_rgba(seg[s_ix][0][...,z_ix], cf.cmap), alpha=0.8)
elif seg is not None:
ax.imshow(to_rgba(np.argmax(seg[s_ix][...,z_ix], axis=0), cf.cmap), alpha=0.8)
# gt bounding boxes
if gt_boxes is not None and len(gt_boxes[s_ix]) > 0:
for j, box in enumerate(gt_boxes[s_ix]):
if d > 0:
[z1, z2] = box[4:]
if not (z1<=z_ix and z_ix<=z2):
box = []
if len(box) > 0:
[y1, x1, y2, x2] = box[:4]
width, height = x2 - x1, y2 - y1
if class_targets is not None:
label = cf.class_id2label[class_targets[s_ix][j]]
legend_items.add(label)
if show_gt_labels:
text_poss, p = [(x1, y1), (x1, (y1+y2)//2)], 0
text_fs = title_fs // 3
if roi_items is not None:
for name in roi_items:
if name in cf_roi_items and batch[name][s_ix][j] is not None:
if 'class_targets' in name and cf.plot_class_ids:
text_x = x2 #- 2 * text_fs * (len(str(class_targets[s_ix][j]))) # avoid overlap of scores
text_y = y1 #+ 2 * text_fs
text_str = '{}'.format(class_targets[s_ix][j])
elif 'regression_targets' in name:
text_x, text_y = (x2, y2)
text_str = "[" + " ".join(
["{:.1f}".format(x) for x in batch[name][s_ix][j]]) + "]"
elif 'rg_bin_targets' in name:
text_x, text_y = (x1, y2)
text_str = '{}'.format(batch[name][s_ix][j])
else:
text_pos = text_poss.pop(0)
text_x = text_pos[0] #- 2 * text_fs * len(str(batch[name][s_ix][j]))
text_y = text_pos[1] #+ 2 * text_fs
text_str = '{}'.format(batch[name][s_ix][j])
ax.text(text_x, text_y, text_str, color=cf.white, fontsize=text_fs,
bbox=dict(facecolor=label.color, alpha=0.7, edgecolor='none', clip_on=True,
pad=0))
p+=1
bbox = mpatches.Rectangle((x1, y1), width, height, linewidth=0.6, edgecolor=label.color,
facecolor='none')
ax.add_patch(bbox)
# -----evtly visualise predictions -------------
if pr_boxes is not None or seg_preds is not None:
ax = fig.add_subplot(grid[row, col])
ax.imshow(bg_img, cmap="gray")
ax.axis("off")
col += 1