opschonen van scripts. Update van saliency visualisatie.

This commit is contained in:
Stefan
2022-03-21 14:31:44 +01:00
parent 02d5b371d6
commit 01f458d0db
5 changed files with 157 additions and 128 deletions

View File

@ -1,90 +1,57 @@
import argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# import matplotlib.cm as cm
heatmap = np.load('saliency.npy')
print(np.shape(heatmap))
parser = argparse.ArgumentParser(
description='Calculate the froc metrics and store in froc_metrics.yml')
parser.add_argument('-experiment',
help='Title of experiment')
parser.add_argument('--series', '-s',
metavar='[series_name]', required=True, nargs='+',
help='List of series to include')
args = parser.parse_args()
########## constants #################
SERIES = args.series
series_ = '_'.join(args.series)
EXPERIMENT = args.experiment
SALIENCY_DIR = f'./../train_output/{EXPERIMENT}_{series_}/saliency.npy'
IMAGES_DIR = f'./../train_output/{EXPERIMENT}_{series_}/images_list.npy'
SEGMENTATION_DIR = f'./../train_output/{EXPERIMENT}_{series_}/segmentations.npy'
########## load saliency map ############
heatmap = np.load(SALIENCY_DIR)
heatmap = np.squeeze(heatmap)
######### load images and segmentations ###########
images_list = np.load(IMAGES_DIR)
images_list = np.squeeze(images_list)
segmentations = np.load(SEGMENTATION_DIR)
######## take average ##########
# len(heatmap) is smaller then maximum number of images
# if len(heatmap) < 100:
# heatmap = np.mean(abs(heatmap),axis=0)
heatmap = abs(heatmap)
fig, axes = plt.subplots(2,len(SERIES))
print(np.shape(axes))
print(np.shape(heatmap))
### take average over 5 #########
heatmap = np.mean(abs(heatmap),axis=0)
print(np.shape(heatmap))
SERIES = ['t2','b50','b400','b800','b1400','adc']
fig, axes = plt.subplots(1,6)
print(np.shape(images_list))
max_value = np.amax(heatmap)
pri
min_value = np.amin(heatmap)
# vmin vmax van hele heatmap voor scaling in imshow
# cmap naar grey
im = axes[0].imshow(np.squeeze(heatmap[:,:,12,0]))
axes[1].imshow(np.squeeze(heatmap[:,:,12,1]), vmin=min_value, vmax=max_value)
axes[2].imshow(np.squeeze(heatmap[:,:,12,2]), vmin=min_value, vmax=max_value)
axes[3].imshow(np.squeeze(heatmap[:,:,12,3]), vmin=min_value, vmax=max_value)
axes[4].imshow(np.squeeze(heatmap[:,:,12,4]), vmin=min_value, vmax=max_value)
axes[5].imshow(np.squeeze(heatmap[:,:,12,5]), vmin=min_value, vmax=max_value)
axes[0].set_title("t2")
axes[1].set_title("b50")
axes[2].set_title("b400")
axes[3].set_title("b800")
axes[4].set_title("b1400")
axes[5].set_title("adc")
for indx in range(len(SERIES)):
print(indx)
axes[0,indx].imshow(images_list[:,:,12,indx],cmap='gray')
im = axes[1,indx].imshow(np.squeeze(heatmap[:,:,12,indx]),vmin=min_value, vmax=max_value)
axes[0,indx].set_title(SERIES[indx])
axes[0,indx].set_axis_off()
axes[1,indx].set_axis_off()
cbar = fig.colorbar(im, ax=axes.ravel().tolist(), shrink=0.5, orientation='horizontal')
cbar.set_ticks([-0.1,0,0.1])
cbar.set_ticklabels(['less importance', '0', 'important'])
fig.suptitle('Average saliency maps over the 5 highest predictions', fontsize=16)
plt.show()
cbar.set_ticks([min_value,max_value])
cbar.set_ticklabels(['less important', 'important'])
fig.suptitle('Saliency map', fontsize=16)
plt.savefig(f'./../train_output/{EXPERIMENT}_{series_}/saliency_map.png', dpi=300)
quit()
#take one image out
heatmap = np.squeeze(heatmap[0])
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
class IndexTracker:
def __init__(self, ax, X):
self.ax = ax
ax.set_title('use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = self.slices//2
self.im = ax.imshow(self.X[:, :, self.ind], cmap='jet')
self.update()
def on_scroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
plt.figure(0)
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, heatmap[:,:,:,5])
fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
plt.show()
plt.figure(1)
fig, ax = plt.subplots(1, 1)
tracker = IndexTracker(ax, heatmap[:,:,:,3])
fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
plt.show()