def view_raw_templates(file_name, n_temp=2, square=True):
N_e, N_t, N_tm = templates.shape
if not numpy.iterable(n_temp):
if square:
idx = numpy.random.permutation(numpy.arange(N_tm//2))[:n_temp**2]
else:
idx = numpy.random.permutation(numpy.arange(N_tm//2))[:n_temp]
else:
idx = n_temp
import matplotlib.colors as colors
my_cmap = pylab.get_cmap('winter')
cNorm = colors.Normalize(vmin=0, vmax=N_e)
scalarMap = pylab.cm.ScalarMappable(norm=cNorm, cmap=my_cmap)
pylab.figure()
for count, i in enumerate(idx):
if square:
pylab.subplot(n_temp, n_temp, count + 1)
if (numpy.mod(count, n_temp) != 0):
pylab.setp(pylab.gca(), yticks=[])
if (count < n_temp*(n_temp - 1)):
pylab.setp(pylab.gca(), xticks=[])
else:
pylab.subplot(len(idx), 1, count + 1)
if count != (len(idx) - 1):
pylab.setp(pylab.gca(), xticks=[])
for j in xrange(N_e):
colorVal = scalarMap.to_rgba(j)
pylab.plot(templates[j, :, i], color=colorVal)
pylab.title('Template %d' %i)
pylab.tight_layout()
pylab.show()
python类get_cmap()的实例源码
def set_cmap_synthetic(self, cmap):
self._cmap = pylab.get_cmap(cmap)
self._cNorm = colors.Normalize(vmin=0, vmax=self.nb_cells)
self._scalarMap_synthetic = pylab.cm.ScalarMappable(norm=self._cNorm, cmap=self._cmap)
def set_cmap_circus(self, cmap):
self._cmap = pylab.get_cmap(cmap)
self._cNorm = colors.Normalize(vmin=0, vmax=self.nb_templates)
self._scalarMap_circus = pylab.cm.ScalarMappable(norm=self._cNorm, cmap=self._cmap)
def plot_nodes_over_data_scattermatrix_hexbin(X, Y, mdl, predictions, distances, activities):
"""plot single components X over Y with SOM sample"""
idim = X.shape[1]
odim = Y.shape[1]
numplots = idim * odim + 2
fig3 = pl.figure()
fig3.suptitle("Predictions over data xy scattermatrix/hexbin (%s)" % (mdl.__class__.__name__))
gs = gridspec.GridSpec(idim, odim)
fig3axes = []
for i in range(idim):
fig3axes.append([])
for o in range(odim):
fig3axes[i].append(fig3.add_subplot(gs[i,o]))
err = 0
# colsa = ["k", "r", "g", "c", "m", "y"]
# colsb = ["k", "r", "g", "c", "m", "y"]
colsa = ["k" for col in range(idim)]
colsb = ["r" for col in range(odim)]
for i in range(odim): # odim * 2
for j in range(idim):
# pl.subplot(numplots, 1, (i*idim)+j+1)
ax = fig3axes[j][i]
# target = Y[h,i]
# X__ = X_[j] # X[h,j]
# err += np.sum(np.square(target - prediction))
# ax.plot(X__, [target], colsa[j] + ".", alpha=0.25, label="target_%d" % i)
# ax.plot(X__, [prediction[0,i]], colsb[j] + "o", alpha=0.25, label="pred_%d" % i)
# ax.plot(X[:,j], Y[:,i], colsa[j] + ".", alpha=0.25, label="target_%d" % i)
ax.hexbin(X[:,j], Y[:,i], gridsize = 20, alpha=0.75, cmap=pl.get_cmap("gray"))
ax.plot(X[:,j], predictions[:,i], colsb[j] + "o", alpha=0.15, label="pred_%d" % i, markersize=8)
# pred1 = mdl.filter_e.neuron(mdl.filter_e.flat_to_coords(activities_sorted[-1]))
# ax.plot(X__, [pred1], "ro", alpha=0.5)
# pred2 = mdl.filter_e.neuron(mdl.filter_e.flat_to_coords(activities_sorted[-2]))
# ax.plot(X__, [pred2], "ro", alpha=0.25)
print("accum total err = %f" % (err / X.shape[0] / (idim * odim)))
fig3.show()
def plot_hebbsom_links_distances_activations(X, Y, mdl, predictions, distances, activities):
"""plot the hebbian link matrix, and all node distances and activities for all inputs"""
hebblink_log = np.log(mdl.hebblink_filter.T + 1.0)
fig4 = pl.figure()
fig4.suptitle("Debugging SOM: hebbian links, distances, activities (%s)" % (mdl.__class__.__name__))
gs = gridspec.GridSpec(4, 1)
# pl.plot(X, Y, "k.", alpha=0.5)
# pl.subplot(numplots, 1, numplots-1)
ax1 = fig4.add_subplot(gs[0])
# im1 = ax1.imshow(mdl.hebblink_filter, interpolation="none", cmap=pl.get_cmap("gray"))
im1 = ax1.pcolormesh(hebblink_log, cmap=pl.get_cmap("gray"))
ax1.set_xlabel("in (e)")
ax1.set_ylabel("out (p)")
cbar = fig4.colorbar(mappable = im1, ax=ax1, orientation="horizontal")
ax2 = fig4.add_subplot(gs[1])
distarray = np.array(distances)
print("distarray.shape", distarray.shape)
pcm = ax2.pcolormesh(distarray.T)
cbar = fig4.colorbar(mappable = pcm, ax=ax2, orientation="horizontal")
# pl.subplot(numplots, 1, numplots)
ax3 = fig4.add_subplot(gs[2])
actarray = np.array(activities)
print("actarray.shape", actarray.shape)
pcm = ax3.pcolormesh(actarray.T)
cbar = fig4.colorbar(mappable = pcm, ax=ax3, orientation="horizontal")
ax4 = fig4.add_subplot(gs[3])
ax4.plot(hebblink_log.flatten())
print("hebblink_log", hebblink_log)
fig4.show()
def plot_rels(data, labels=None, colors=None, outfile="rels", latent=None, alpha=0.8, title=''):
ns, n = data.shape
if labels is None:
labels = map(str, range(n))
ncol = 5
nrow = int(np.ceil(float(n * (n - 1) / 2) / ncol))
fig, axs = pylab.subplots(nrow, ncol)
fig.set_size_inches(5 * ncol, 5 * nrow)
pairs = list(combinations(range(n), 2))
if colors is not None:
colors = (colors - np.min(colors)) / (np.max(colors) - np.min(colors))
for ax, pair in zip(axs.flat, pairs):
diff_x = max(data[:, pair[0]]) - min(data[:, pair[0]])
diff_y = max(data[:, pair[1]]) - min(data[:, pair[1]])
ax.set_xlim([min(data[:, pair[0]]) - 0.05 * diff_x, max(data[:, pair[0]]) + 0.05 * diff_x])
ax.set_ylim([min(data[:, pair[1]]) - 0.05 * diff_y, max(data[:, pair[1]]) + 0.05 * diff_y])
ax.scatter(data[:, pair[0]], data[:, pair[1]], c=colors, cmap=pylab.get_cmap("jet"),
marker='.', alpha=alpha, edgecolors='none', vmin=0, vmax=1)
ax.set_xlabel(shorten(labels[pair[0]]))
ax.set_ylabel(shorten(labels[pair[1]]))
for ax in axs.flat[axs.size - 1:len(pairs) - 1:-1]:
ax.scatter(data[:, 0], data[:, 1], marker='.')
fig.suptitle(title, fontsize=16)
pylab.rcParams['font.size'] = 12 #6
# pylab.draw()
# fig.set_tight_layout(True)
pylab.tight_layout()
pylab.subplots_adjust(top=0.95)
for ax in axs.flat[axs.size - 1:len(pairs) - 1:-1]:
ax.set_visible(False)
filename = outfile + '.png'
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
fig.savefig(outfile + '.png')
pylab.close('all')
return True
# Hierarchical graph visualization utilities
def plot_rels(data, labels=None, colors=None, outfile="rels", latent=None, alpha=0.8):
ns, n = data.shape
if labels is None:
labels = list(map(str, range(n)))
ncol = 5
# ncol = 4
nrow = int(np.ceil(float(n * (n - 1) / 2) / ncol))
#nrow=1
#pylab.rcParams.update({'figure.autolayout': True})
fig, axs = pylab.subplots(nrow, ncol)
fig.set_size_inches(5 * ncol, 5 * nrow)
#fig.set_canvas(pylab.gcf().canvas)
pairs = list(combinations(range(n), 2)) #[:4]
pairs = sorted(pairs, key=lambda q: q[0]**2+q[1]**2) # Puts stronger relationships first
if colors is not None:
colors = (colors - np.min(colors)) / (np.max(colors) - np.min(colors)).clip(1e-7)
for ax, pair in zip(axs.flat, pairs):
if latent is None:
ax.scatter(data[:, pair[0]], data[:, pair[1]], marker='.', edgecolors='none', alpha=alpha)
else:
# cs = 'rgbcmykrgbcmyk'
markers = 'x+.o,<>^^<>,+x.'
for j, ind in enumerate(np.unique(latent)):
inds = (latent == ind)
ax.scatter(data[inds, pair[0]], data[inds, pair[1]], c=colors[inds], cmap=pylab.get_cmap("jet"),
marker=markers[j], alpha=0.5, edgecolors='none', vmin=0, vmax=1)
ax.set_xlabel(shorten(labels[pair[0]]))
ax.set_ylabel(shorten(labels[pair[1]]))
for ax in axs.flat[axs.size - 1:len(pairs) - 1:-1]:
ax.scatter(data[:, 0], data[:, 1], marker='.')
pylab.rcParams['font.size'] = 12 #6
pylab.draw()
#fig.set_tight_layout(True)
fig.tight_layout()
for ax in axs.flat[axs.size - 1:len(pairs) - 1:-1]:
ax.set_visible(False)
filename = outfile + '.png'
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
fig.savefig(outfile + '.png') #df')
pylab.close('all')
return True
def plot_nodes_over_data_scattermatrix_hexbin(X, Y, mdl, predictions, distances, activities, saveplot = False):
"""models_actinf.plot_nodes_over_data_scattermatrix_hexbin
Plot models nodes (if applicable) over the hexbinned data
expanding dimensions as a scattermatrix.
"""
idim = X.shape[1]
odim = Y.shape[1]
numplots = idim * odim + 2
fig = pl.figure()
fig.suptitle("Predictions over data xy scattermatrix/hexbin (%s)" % (mdl.__class__.__name__))
gs = gridspec.GridSpec(idim, odim)
figaxes = []
for i in range(idim):
figaxes.append([])
for o in range(odim):
figaxes[i].append(fig.add_subplot(gs[i,o]))
err = 0
# colsa = ["k", "r", "g", "c", "m", "y"]
# colsb = ["k", "r", "g", "c", "m", "y"]
colsa = ["k" for col in range(idim)]
colsb = ["r" for col in range(odim)]
for i in range(odim): # odim * 2
for j in range(idim):
# pl.subplot(numplots, 1, (i*idim)+j+1)
ax = figaxes[j][i]
# target = Y[h,i]
# X__ = X_[j] # X[h,j]
# err += np.sum(np.square(target - prediction))
# ax.plot(X__, [target], colsa[j] + ".", alpha=0.25, label="target_%d" % i)
# ax.plot(X__, [prediction[0,i]], colsb[j] + "o", alpha=0.25, label="pred_%d" % i)
# ax.plot(X[:,j], Y[:,i], colsa[j] + ".", alpha=0.25, label="target_%d" % i)
ax.hexbin(X[:,j], Y[:,i], gridsize = 20, alpha=0.75, cmap=pl.get_cmap("gray"))
ax.plot(X[:,j], predictions[:,i], colsb[j] + "o", alpha=0.15, label="pred_%d" % i, markersize=8)
# pred1 = mdl.filter_e.neuron(mdl.filter_e.flat_to_coords(activities_sorted[-1]))
# ax.plot(X__, [pred1], "ro", alpha=0.5)
# pred2 = mdl.filter_e.neuron(mdl.filter_e.flat_to_coords(activities_sorted[-2]))
# ax.plot(X__, [pred2], "ro", alpha=0.25)
# print("accum total err = %f" % (err / X.shape[0] / (idim * odim)))
if saveplot:
filename = "plot_nodes_over_data_scattermatrix_hexbin_%s.jpg" % (mdl.__class__.__name__,)
savefig(fig, filename)
fig.show()
def plot_hebbsom_links_distances_activations(X, Y, mdl, predictions, distances, activities, saveplot = False):
"""plot the hebbian link matrix, and all node distances and activities for all inputs"""
hebblink_log = np.log(mdl.hebblink_filter.T + 1.0)
fig = pl.figure()
fig.suptitle("Debugging SOM: hebbian links, distances, activities (%s)" % (mdl.__class__.__name__))
gs = gridspec.GridSpec(4, 1)
# pl.plot(X, Y, "k.", alpha=0.5)
# pl.subplot(numplots, 1, numplots-1)
ax1 = fig.add_subplot(gs[0])
ax1.set_title('hebbian associative links')
# im1 = ax1.imshow(mdl.hebblink_filter, interpolation="none", cmap=pl.get_cmap("gray"))
im1 = ax1.pcolormesh(hebblink_log, cmap=pl.get_cmap("gray"))
ax1.set_xlabel("in (e)")
ax1.set_ylabel("out (p)")
cbar = fig.colorbar(mappable = im1, ax=ax1, orientation="horizontal")
ax2 = fig.add_subplot(gs[1])
ax2.set_title('distances over time')
distarray = np.array(distances)
# print("distarray.shape", distarray.shape)
pcm = ax2.pcolormesh(distarray.T)
cbar = fig.colorbar(mappable = pcm, ax=ax2, orientation="horizontal")
# pl.subplot(numplots, 1, numplots)
ax3 = fig.add_subplot(gs[2])
ax3.set_title('activations propagated via hebbian links')
actarray = np.array(activities)
# print("actarray.shape", actarray.shape)
pcm = ax3.pcolormesh(actarray.T)
cbar = fig.colorbar(mappable = pcm, ax=ax3, orientation="horizontal")
ax4 = fig.add_subplot(gs[3])
ax4.set_title('flattened link table')
ax4.plot(hebblink_log.flatten())
# print("hebblink_log", hebblink_log)
if saveplot:
filename = "plot_hebbsom_links_distances_activations_%s.jpg" % (mdl.__class__.__name__,)
savefig(fig, filename)
fig.show()
def plot_predictions_over_data(X, Y, mdl, saveplot = False, ax = None, datalim = 1000):
do_hexbin = False
if X.shape[0] > 4000:
do_hexbin = False # True
X = X[-4000:]
Y = Y[-4000:]
# plot prediction
idim = X.shape[1]
odim = Y.shape[1]
numsamples = 1 # 2
Y_samples = []
for i in range(numsamples):
Y_samples.append(mdl.predict(X))
# print("Y_samples[0]", Y_samples[0])
fig = pl.figure()
fig.suptitle("Predictions over data xy (numsamples = %d, (%s)" % (numsamples, mdl.__class__.__name__))
gs = gridspec.GridSpec(odim, 1)
for i in range(odim):
ax = fig.add_subplot(gs[i])
target = Y[:,i]
if do_hexbin:
ax.hexbin(X, Y, gridsize = 20, alpha=1.0, cmap=pl.get_cmap("gray"))
else:
ax.plot(X, target, "k.", label="Y_", alpha=0.5)
for j in range(numsamples):
prediction = Y_samples[j][:,i]
# print("X", X.shape, "prediction", prediction.shape)
# print("X", X, "prediction", prediction)
if do_hexbin:
ax.hexbin(X[:,i], prediction, gridsize = 30, alpha=0.6, cmap=pl.get_cmap("Reds"))
else:
ax.plot(X[:,i], prediction, "r.", label="Y_", alpha=0.25)
# get limits
xlim = ax.get_xlim()
ylim = ax.get_ylim()
error = target - prediction
mse = np.mean(np.square(error))
mae = np.mean(np.abs(error))
xran = xlim[1] - xlim[0]
yran = ylim[1] - ylim[0]
ax.text(xlim[0] + xran * 0.1, ylim[0] + yran * 0.3, "mse = %f" % mse)
ax.text(xlim[0] + xran * 0.1, ylim[0] + yran * 0.5, "mae = %f" % mae)
if saveplot:
filename = "plot_predictions_over_data_%s.jpg" % (mdl.__class__.__name__,)
savefig(fig, filename)
fig.show()
def _plot_features(out_dir, signal, sampling_rate, logmel, delta, delta_delta, specgram, filename):
try:
os.makedirs(out_dir)
except:
pass
sampling_interval = 1.0 / sampling_rate
times = np.arange(len(signal)) * sampling_interval
pylab.clf()
plt.rcParams['font.size'] = 18
pylab.figure(figsize=(len(signal) / 2000, 16))
ax1 = pylab.subplot(511)
pylab.plot(times, signal)
pylab.title("Waveform")
pylab.xlabel("Time [sec]")
pylab.ylabel("Amplitude")
pylab.xlim([0, len(signal) * sampling_interval])
ax2 = pylab.subplot(512)
specgram = np.log(specgram)
pylab.pcolormesh(np.arange(0, specgram.shape[0]), np.arange(0, specgram.shape[1]) * 8000 / specgram.shape[1], specgram.T, cmap=pylab.get_cmap("jet"))
pylab.title("Spectrogram")
pylab.xlabel("Time [sec]")
pylab.ylabel("Frequency [Hz]")
pylab.colorbar()
ax3 = pylab.subplot(513)
pylab.pcolormesh(np.arange(0, logmel.shape[0]), np.arange(1, 41), logmel.T, cmap=pylab.get_cmap("jet"))
pylab.title("Log mel filter bank features")
pylab.xlabel("Frame")
pylab.ylabel("Filter number")
pylab.colorbar()
ax4 = pylab.subplot(514)
pylab.pcolormesh(np.arange(0, delta.shape[0]), np.arange(1, 41), delta.T, cmap=pylab.get_cmap("jet"))
pylab.title("Deltas")
pylab.xlabel("Frame")
pylab.ylabel("Filter number")
pylab.colorbar()
ax5 = pylab.subplot(515)
pylab.pcolormesh(np.arange(0, delta_delta.shape[0]), np.arange(1, 41), delta_delta.T, cmap=pylab.get_cmap("jet"))
pylab.title("Delta-deltas")
pylab.xlabel("Frame")
pylab.ylabel("Filter number")
pylab.colorbar()
pylab.tight_layout()
pylab.savefig(os.path.join(out_dir, filename), bbox_inches="tight")