Skip to content
Snippets Groups Projects
Commit 0cf8e39b authored by Nikolai.Hartmann's avatar Nikolai.Hartmann
Browse files

switching pixel pots to pcolormesh

parent fab391d6
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import math ...@@ -6,7 +6,7 @@ import math
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.colors import matplotlib.colors
from matplotlib.ticker import LogFormatter from matplotlib.ticker import LogFormatter
from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1 import ImageGrid, make_axes_locatable
import numpy as np import numpy as np
from .keras_visualize_activations.read_activations import get_activations from .keras_visualize_activations.read_activations import get_activations
...@@ -48,7 +48,9 @@ def plot_NN_vs_var_2D(plotname, means, ...@@ -48,7 +48,9 @@ def plot_NN_vs_var_2D(plotname, means,
var2_label=None, var2_label=None,
logscale=False, logscale=False,
ncontours=20, ncontours=20,
black_contourlines=False): only_pixels=False,
black_contourlines=False,
cmap="inferno"):
print("Creating varied events (2d)") print("Creating varied events (2d)")
# example: vary var1 vs var2 # example: vary var1 vs var2
...@@ -79,14 +81,23 @@ def plot_NN_vs_var_2D(plotname, means, ...@@ -79,14 +81,23 @@ def plot_NN_vs_var_2D(plotname, means,
zmax = np.max(scores) zmax = np.max(scores)
if logscale: if logscale:
if zmin <= 0:
zmin = 1e-5
print("Setting zmin to {}".format(zmin))
lvls = np.logspace(math.log10(zmin), math.log10(zmax), ncontours) lvls = np.logspace(math.log10(zmin), math.log10(zmax), ncontours)
pcm = ax.contourf(sequence1, sequence2, scores, levels=lvls, norm=matplotlib.colors.LogNorm(vmin=zmin, vmax=zmax)) if only_pixels:
pcm = ax.pcolormesh(sequence1, sequence2, scores, norm=matplotlib.colors.LogNorm(vmin=zmin, vmax=zmax), cmap=cmap)
else:
pcm = ax.contourf(sequence1, sequence2, scores, levels=lvls, norm=matplotlib.colors.LogNorm(vmin=zmin, vmax=zmax), cmap=cmap)
if black_contourlines: if black_contourlines:
ax.contour(sequence1, sequence2, scores, levels=lvls, colors="k", linewidths=1) ax.contour(sequence1, sequence2, scores, levels=lvls, colors="k", linewidths=1)
l_f = LogFormatter(10, labelOnlyBase=False, minor_thresholds=(np.inf, np.inf)) l_f = LogFormatter(10, labelOnlyBase=False, minor_thresholds=(np.inf, np.inf))
cbar = fig.colorbar(pcm, ax=ax, extend='max', ticks=lvls, format=l_f) cbar = fig.colorbar(pcm, ax=ax, extend='max', ticks=lvls, format=l_f)
else: else:
pcm = ax.contourf(sequence1, sequence2, scores, ncontours, norm=matplotlib.colors.Normalize(vmin=0, vmax=1)) if only_pixels:
pcm = ax.pcolormesh(sequence1, sequence2, scores, norm=matplotlib.colors.Normalize(vmin=zmin, vmax=zmax), cmap=cmap)
else:
pcm = ax.contourf(sequence1, sequence2, scores, ncontours, norm=matplotlib.colors.Normalize(vmin=zmin, vmax=zmax), cmap=cmap)
if black_contourlines: if black_contourlines:
ax.contour(sequence1, sequence2, scores, ncontours, colors="k", linewidths=1) ax.contour(sequence1, sequence2, scores, ncontours, colors="k", linewidths=1)
cbar = fig.colorbar(pcm, ax=ax, extend='max') cbar = fig.colorbar(pcm, ax=ax, extend='max')
...@@ -107,7 +118,9 @@ def plot_NN_vs_var_2D_all(plotname, model, means, ...@@ -107,7 +118,9 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
var1_label=None, var1_label=None,
var2_label=None, var2_label=None,
zrange=None, logz=False, zrange=None, logz=False,
plot_last_layer=False): plot_last_layer=False,
log_default_ymin=1e-5,
cmap="inferno"):
"Similar to plot_NN_vs_var_2D, but creates a grid of plots for all neurons." "Similar to plot_NN_vs_var_2D, but creates a grid of plots for all neurons."
...@@ -130,8 +143,6 @@ def plot_NN_vs_var_2D_all(plotname, model, means, ...@@ -130,8 +143,6 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
acts = get_activations(model, events, print_shape_only=True) acts = get_activations(model, events, print_shape_only=True)
aspect = (var1_vals[-1]-var1_vals[0])/(var2_vals[-1]-var2_vals[0])
if plot_last_layer: if plot_last_layer:
n_neurons = [len(i[0]) for i in acts] n_neurons = [len(i[0]) for i in acts]
else: else:
...@@ -142,8 +153,11 @@ def plot_NN_vs_var_2D_all(plotname, model, means, ...@@ -142,8 +153,11 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
fig = plt.figure(1, figsize=nrows_ncols) fig = plt.figure(1, figsize=nrows_ncols)
grid = ImageGrid(fig, 111, nrows_ncols=nrows_ncols[::-1], axes_pad=0, grid = ImageGrid(fig, 111, nrows_ncols=nrows_ncols[::-1], axes_pad=0,
label_mode="1", label_mode="1",
aspect=False,
cbar_location="top", cbar_location="top",
cbar_mode="single",) cbar_mode="single",
cbar_pad=.2,
cbar_size="5%",)
grid_array = np.array(grid) grid_array = np.array(grid)
grid_array = grid_array.reshape(*nrows_ncols[::-1]) grid_array = grid_array.reshape(*nrows_ncols[::-1])
...@@ -158,11 +172,10 @@ def plot_NN_vs_var_2D_all(plotname, model, means, ...@@ -158,11 +172,10 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
output_max_default = 1 output_max_default = 1
if global_min <= 0 and logz: if global_min <= 0 and logz:
min_exponent = -5 global_min = log_default_ymin
global_min = 10**min_exponent print("Changing global_min to {}".format(log_default_ymin))
output_min_default = global_min
print("Changing global_min to {}".format(global_min))
ims = []
for layer in range(layers): for layer in range(layers):
for neuron in range(len(acts[layer][0])): for neuron in range(len(acts[layer][0])):
acts_neuron = acts[layer][:,neuron] acts_neuron = acts[layer][:,neuron]
...@@ -179,12 +192,13 @@ def plot_NN_vs_var_2D_all(plotname, model, means, ...@@ -179,12 +192,13 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
extra_opts["norm"] = norm(vmin=zrange[0], vmax=zrange[1]) extra_opts["norm"] = norm(vmin=zrange[0], vmax=zrange[1])
else: else:
extra_opts["norm"] = norm(vmin=global_min, vmax=global_max) extra_opts["norm"] = norm(vmin=global_min, vmax=global_max)
im = ax.imshow(acts_neuron, origin="lower", extent=[var1_vals[0], var1_vals[-1], var2_vals[0], var2_vals[-1]], aspect=aspect, cmap="jet", **extra_opts) im = ax.pcolormesh(var1_vals, var2_vals, acts_neuron, cmap=cmap, linewidth=0, rasterized=True, **extra_opts)
ax.set_facecolor("black")
if var1_label is not None: if var1_label is not None:
ax.set_xlabel(var1_label) ax.set_xlabel(var1_label)
if var2_label is not None: if var2_label is not None:
ax.set_ylabel(var2_label) ax.set_ylabel(var2_label)
ax.text(0., 0.5, "{}, {}".format(layer, neuron), transform=ax.transAxes) ax.text(0., 0.5, "{}, {}".format(layer, neuron), transform=ax.transAxes, color="white")
cb = fig.colorbar(im, cax=grid[0].cax, orientation="horizontal") cb = fig.colorbar(im, cax=grid[0].cax, orientation="horizontal")
cb.ax.xaxis.set_ticks_position('top') cb.ax.xaxis.set_ticks_position('top')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment