def view_waveforms_clusters(data, halo, threshold, templates, amps_lim, n_curves=200, save=False):
nb_templates = templates.shape[1]
n_panels = numpy.ceil(numpy.sqrt(nb_templates))
mask = numpy.where(halo > -1)[0]
clust_idx = numpy.unique(halo[mask])
fig = pylab.figure()
square = True
center = len(data[0] - 1)//2
for count, i in enumerate(xrange(nb_templates)):
if square:
pylab.subplot(n_panels, n_panels, count + 1)
if (numpy.mod(count, n_panels) != 0):
pylab.setp(pylab.gca(), yticks=[])
if (count < n_panels*(n_panels - 1)):
pylab.setp(pylab.gca(), xticks=[])
subcurves = numpy.where(halo == clust_idx[count])[0]
for k in numpy.random.permutation(subcurves)[:n_curves]:
pylab.plot(data[k], '0.5')
pylab.plot(templates[:, count], 'r')
pylab.plot(amps_lim[count][0]*templates[:, count], 'b', alpha=0.5)
pylab.plot(amps_lim[count][1]*templates[:, count], 'b', alpha=0.5)
xmin, xmax = pylab.xlim()
pylab.plot([xmin, xmax], [-threshold, -threshold], 'k--')
pylab.plot([xmin, xmax], [threshold, threshold], 'k--')
#pylab.ylim(-1.5*threshold, 1.5*threshold)
ymin, ymax = pylab.ylim()
pylab.plot([center, center], [ymin, ymax], 'k--')
pylab.title('Cluster %d' %i)
if nb_templates > 0:
pylab.tight_layout()
if save:
pylab.savefig(os.path.join(save[0], 'waveforms_%s' %save[1]))
pylab.close()
else:
pylab.show()
del fig
python类close()的实例源码
def view_artefact(data, save=False):
fig = pylab.figure()
pylab.plot(data.T)
if save:
pylab.savefig(os.path.join(save[0], 'artefact_%s' %save[1]))
pylab.close()
else:
pylab.show()
del fig
def view_trigger_snippets(trigger_snippets, chans, save=None):
# Create output directory if necessary.
if os.path.exists(save):
for f in os.listdir(save):
p = os.path.join(save, f)
os.remove(p)
os.removedirs(save)
os.makedirs(save)
# Plot figures.
fig = pylab.figure()
for (c, chan) in enumerate(chans):
ax = fig.add_subplot(1, 1, 1)
for n in xrange(0, trigger_snippets.shape[2]):
y = trigger_snippets[:, c, n]
x = numpy.arange(- (y.size - 1) / 2, (y.size - 1) / 2 + 1)
b = 0.5 + 0.5 * numpy.random.rand()
ax.plot(x, y, color=(0.0, 0.0, b), linestyle='solid')
y = numpy.mean(trigger_snippets[:, c, :], axis=1)
x = numpy.arange(- (y.size - 1) / 2, (y.size - 1) / 2 + 1)
ax.plot(x, y, color=(1.0, 0.0, 0.0), linestyle='solid')
ax.grid(True)
ax.set_xlim([numpy.amin(x), numpy.amax(x)])
ax.set_title("Channel %d" %chan)
ax.set_xlabel("time")
ax.set_ylabel("amplitude")
if save is not None:
# Save plot.
filename = "channel-%d.png" %chan
path = os.path.join(save, filename)
pylab.savefig(path)
fig.clf()
if save is None:
pylab.show()
else:
pylab.close(fig)
return
def view_mahalanobis_distribution(data_1, data_2, save=None):
'''Plot Mahalanobis distribution Before and After'''
fig = pylab.figure()
ax = fig.add_subplot(1,2,1)
if len(data_1) == 3:
d_gt, d_ngt, d_noi = data_1
elif len(data_1) == 2:
d_gt, d_ngt = data_1
if len(data_1) == 3:
ax.hist(d_noi, bins=50, color='k', alpha=0.5, label="Noise")
ax.hist(d_ngt, bins=50, color='b', alpha=0.5, label="Non GT")
ax.hist(d_gt, bins=75, color='r', alpha=0.5, label="GT")
ax.grid(True)
ax.set_title("Before")
ax.set_ylabel("")
ax.set_xlabel('# Samples')
ax.set_xlabel('Distances')
if len(data_2) == 3:
d_gt, d_ngt, d_noi = data_2
elif len(data_2) == 2:
d_gt, d_ngt = data_2
ax = fig.add_subplot(1,2,2)
if len(data_2) == 3:
ax.hist(d_noi, bins=50, color='k', alpha=0.5, label="Noise")
ax.hist(d_ngt, bins=50, color='b', alpha=0.5, label="Non GT")
ax.hist(d_gt, bins=75, color='r', alpha=0.5, label="GT")
ax.grid(True)
ax.set_title("After")
ax.set_ylabel("")
ax.set_xlabel('Distances')
ax.legend()
if save is None:
pylab.show()
else:
pylab.savefig(save)
pylab.close(fig)
return
def plot_bar_chart(page, datasets, dataset_labels, dataset_colors, x_group_labels, err=0, title=None, xlabel='Bins', ylabel='Counts'):
assert len(datasets) == len(dataset_colors) == len(dataset_labels)
for dataset in datasets:
assert len(dataset) == len(datasets[0])
assert len(dataset) == len(x_group_labels)
num_x_groups = len(datasets[0])
x_group_locations = pylab.arange(num_x_groups)
width = 1.0 / float(len(datasets)+1)
figure = pylab.figure()
axis = figure.add_subplot(111)
bars = []
for i in xrange(len(datasets)):
bar = axis.bar(x_group_locations + (width*i), datasets[i], width, yerr=err, color=dataset_colors[i], error_kw=dict(ecolor='pink', lw=3, capsize=6, capthick=3))
bars.append(bar)
if title is not None:
axis.set_title(title)
if ylabel is not None:
axis.set_ylabel(ylabel)
if xlabel is not None:
axis.set_xlabel(xlabel)
axis.set_xticks(x_group_locations + width*len(datasets)/2)
x_tick_names = axis.set_xticklabels(x_group_labels)
rot = 0 if num_x_groups == 1 else 15
pylab.setp(x_tick_names, rotation=rot, fontsize=10)
axis.set_xlim(-width, num_x_groups)
y_tick_names = axis.get_yticklabels()
pylab.setp(y_tick_names, rotation=0, fontsize=10)
axis.legend([bar[0] for bar in bars], dataset_labels)
page.savefig()
pylab.close()
def output_groups(ws, moments, alpha, mis, column_label, thresh=0, prefix=''):
tc = moments["TC"]
tcs = moments["TCs"]
add = moments["additivity"]
dual = (moments['X_i Y_j'] * moments['X_i Z_j']).T
f = safe_open(prefix + '/summary/groups.txt', 'w+')
g = safe_open(prefix + '/summary/groups_no_overlaps.txt', 'w+')
h = safe_open(prefix + '/summary/summary.txt', 'w+')
h.write('Group, TC\n')
m, nv = mis.shape
f.write('variable, weight, MI\n')
g.write('variable, weight, MI\n')
for j in range(m):
f.write('Group num: %d, TC(X;Y_j): %0.6f\n' % (j, tcs[j]))
g.write('Group num: %d, TC(X;Y_j): %0.6f\n' % (j, tcs[j]))
h.write('%d, %0.6f\n' % (j, tcs[j]))
inds = np.where(alpha[j] > 0)[0]
inds = inds[np.argsort(-np.abs(ws)[j][inds])]
for ind in inds:
f.write(column_label[ind] + ', {:.3f}, {:.3f}\n'.format(ws[j][ind], mis[j][ind]))
inds = np.where(np.argmax(np.abs(ws), axis=0) == j)[0]
inds = inds[np.argsort(-np.abs(ws)[j][inds])]
for ind in inds:
g.write(column_label[ind] + ', {:.3f}, {:.3f}\n'.format(ws[j][ind], mis[j][ind]))
h.write('Total: {:f}\n'.format(np.sum(tcs)))
h.write('The total of individual TCs should approximately equal the objective: {:f}\n'.format(tc))
h.write('If not, this signals redundancy/synergy in the final solution (measured by additivity: {:f}'.format(add))
f.close()
g.close()
h.close()
def output_labels(labels, row_label, prefix=''):
f = safe_open(prefix + '/summary/labels.txt', 'w+')
ns, m = labels.shape
for l in range(ns):
f.write(row_label[l] + ',' + ','.join(map(str, labels[l, :])) + '\n')
f.close()
def on_epoch_end(self, epoch, logs={}):
self.model.save_weights(os.path.join(self.output_dir, 'weights%02d.h5' % (epoch)))
self.show_edit_distance(256)
word_batch = next(self.text_img_gen)[0]
res = decode_batch(self.test_func, word_batch['the_input'][0:self.num_display_words])
if word_batch['the_input'][0].shape[0] < 256:
cols = 2
else:
cols = 1
for i in range(self.num_display_words):
pylab.subplot(self.num_display_words // cols, cols, i + 1)
if K.image_data_format() == 'channels_first':
the_input = word_batch['the_input'][i, 0, :, :]
else:
the_input = word_batch['the_input'][i, :, :, 0]
pylab.imshow(the_input.T, cmap='Greys_r')
pylab.xlabel('Truth = \'%s\'\nDecoded = \'%s\'' % (word_batch['source_str'][i], res[i]))
fig = pylab.gcf()
fig.set_size_inches(10, 13)
pylab.savefig(os.path.join(self.output_dir, 'e%02d.png' % (epoch)))
pylab.close()
def plot_heatmaps(data, labels, alpha, mis, column_label, cont, topk=20, prefix='', focus=''):
cmap = sns.cubehelix_palette(as_cmap=True, light=.9)
m, nv = mis.shape
for j in range(m):
inds = np.where(np.logical_and(alpha[j] > 0, mis[j] > 0.))[0]
inds = inds[np.argsort(- alpha[j, inds] * mis[j, inds])][:topk]
if focus in column_label:
ifocus = column_label.index(focus)
if not ifocus in inds:
inds = np.insert(inds, 0, ifocus)
if len(inds) >= 2:
plt.clf()
order = np.argsort(cont[:,j])
subdata = data[:, inds][order].T
subdata -= np.nanmean(subdata, axis=1, keepdims=True)
subdata /= np.nanstd(subdata, axis=1, keepdims=True)
columns = [column_label[i] for i in inds]
sns.heatmap(subdata, vmin=-3, vmax=3, cmap=cmap, yticklabels=columns, xticklabels=False, mask=np.isnan(subdata))
filename = '{}/heatmaps/group_num={}.png'.format(prefix, j)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
plt.title("Latent factor {}".format(j))
plt.savefig(filename, bbox_inches='tight')
plt.close('all')
#plot_rels(data[:, inds], list(map(lambda q: column_label[q], inds)), colors=cont[:, j],
# outfile=prefix + '/relationships/group_num=' + str(j), latent=labels[:, j], alpha=0.1)
def plot_pairplots(data, labels, alpha, mis, column_label, topk=5, prefix='', focus=''):
cmap = sns.cubehelix_palette(as_cmap=True, light=.9)
plt.rcParams.update({'font.size': 32})
m, nv = mis.shape
for j in range(m):
inds = np.where(np.logical_and(alpha[j] > 0, mis[j] > 0.))[0]
inds = inds[np.argsort(- alpha[j, inds] * mis[j, inds])][:topk]
if focus in column_label:
ifocus = column_label.index(focus)
if not ifocus in inds:
inds = np.insert(inds, 0, ifocus)
if len(inds) >= 2:
plt.clf()
subdata = data[:, inds]
columns = [column_label[i] for i in inds]
subdata = pd.DataFrame(data=subdata, columns=columns)
try:
sns.pairplot(subdata, kind="reg", diag_kind="kde", size=5, dropna=True)
filename = '{}/pairplots_regress/group_num={}.pdf'.format(prefix, j)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
plt.suptitle("Latent factor {}".format(j), y=1.01)
plt.savefig(filename, bbox_inches='tight')
plt.clf()
except:
pass
subdata['Latent factor'] = labels[:,j]
try:
sns.pairplot(subdata, kind="scatter", dropna=True, vars=subdata.columns.drop('Latent factor'), hue="Latent factor", diag_kind="kde", size=5)
filename = '{}/pairplots/group_num={}.pdf'.format(prefix, j)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
plt.suptitle("Latent factor {}".format(j), y=1.01)
plt.savefig(filename, bbox_inches='tight')
plt.close('all')
except:
pass
def output_labels(labels, row_label, prefix=''):
f = safe_open(prefix + '/text_files/labels.txt', 'w+')
ns, m = labels.shape
for l in range(ns):
f.write(row_label[l] + ',' + ','.join(map(str, labels[l, :])) + '\n')
f.close()
def output_strong(tcs, alpha, mis, labels, prefix=''):
f = safe_open(prefix + '/text_files/most_deterministic_groups.txt', 'w+')
m, n = alpha.shape
topk = 5
ixy = np.clip(np.sum(alpha * mis, axis=1) - tcs, 0, np.inf)
hys = np.array([entropy(labels[:, j]) for j in range(m)]).clip(1e-6)
ntcs = [(np.sum(np.sort(alpha[j] * mis[j])[-topk:]) - ixy[j]) / ((topk - 1) * hys[j]) for j in range(m)]
f.write('Group num., NTC\n')
for j, ntc in sorted(enumerate(ntcs), key=lambda q: -q[1]):
f.write('%d, %0.3f\n' % (j, ntc))
f.close()
def anomalies(log_z, row_label=None, prefix=''):
from scipy.special import erf
ns = log_z.shape[1]
if row_label is None:
row_label = list(map(str, range(ns)))
a_score = np.sum(log_z[:, :, 0], axis=0)
mean, std = np.mean(a_score), np.std(a_score)
a_score = (a_score - mean) / std
percentile = 1. / ns
anomalies = np.where(0.5 * (1 - erf(a_score / np.sqrt(2)) ) < percentile)[0]
f = safe_open(prefix + '/text_files/anomalies.txt', 'w+')
for i in anomalies:
f.write(row_label[i] + ', %0.1f\n' % a_score[i])
f.close()
def plot_convergence(tc_history, prefix='', prefix2=''):
pylab.plot(tc_history)
pylab.xlabel('# iterations')
filename = '{}/text_files/convergence{}.pdf'.format(prefix, prefix2)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
pylab.savefig(filename)
pylab.close('all')
return True
def saveBEVImageWithAxes(data, outputname, cmap = None, xlabel = 'x [m]', ylabel = 'z [m]', rangeX = [-10, 10], rangeXpx = None, numDeltaX = 5, rangeZ = [7, 62], rangeZpx = None, numDeltaZ = 5, fontSize = 16):
'''
:param data:
:param outputname:
:param cmap:
'''
aspect_ratio = float(data.shape[1])/data.shape[0]
fig = pylab.figure()
Scale = 8
# add +1 to get axis text
fig.set_size_inches(Scale*aspect_ratio+1,Scale*1)
ax = pylab.gca()
#ax.set_axis_off()
#fig.add_axes(ax)
if cmap != None:
pylab.set_cmap(cmap)
#ax.imshow(data, interpolation='nearest', aspect = 'normal')
ax.imshow(data, interpolation='nearest')
if rangeXpx == None:
rangeXpx = (0, data.shape[1])
if rangeZpx == None:
rangeZpx = (0, data.shape[0])
modBev_plot(ax, rangeX, rangeXpx, numDeltaX, rangeZ, rangeZpx, numDeltaZ, fontSize, xlabel = xlabel, ylabel = ylabel)
#plt.savefig(outputname, bbox_inches='tight', dpi = dpi)
pylab.savefig(outputname, dpi = data.shape[0]/Scale)
pylab.close()
fig.clear()
def getPageSize():
import resource
f = open("/proc/meminfo")
mem = f.readline()
f.close()
return resource.getpagesize() / (1024 * float(mem[10:-3].strip()))
def save(GUI):
global txtResultPath
if GUI:
import pylab as pl
import nest.raster_plot
import nest.voltage_trace
logger.debug("Saving IMAGES into {0}".format(SAVE_PATH))
for key in spike_detectors:
try:
nest.raster_plot.from_device(spike_detectors[key], hist=True)
pl.savefig("spikes_" + str(key) +".png", dpi=dpi_n, format='png')
pl.close()
except Exception:
print("From spikes {0} is NOTHING".format(key))
for key in multimeters:
try:
nest.voltage_trace.from_device(multimeters[key])
pl.savefig("volt_" + str(key) +".png", dpi=dpi_n, format='png')
pl.close()
except Exception:
print("From MM {0} is NOTHING".format(key))
txtResultPath = SAVE_PATH + 'txt/'
logger.debug("Saving TEXT into {0}".format(txtResultPath))
if not os.path.exists(txtResultPath):
os.mkdir(txtResultPath)
for key in spike_detectors:
save_spikes(spike_detectors[key], name=key)
with open(txtResultPath + 'timeSimulation.txt', 'w') as f:
for item in times:
f.write(item)
def saveBEVImageWithAxes(data, outputname, cmap = None, xlabel = 'x [m]', ylabel = 'z [m]', rangeX = [-10, 10], rangeXpx = None, numDeltaX = 5, rangeZ = [7, 62], rangeZpx = None, numDeltaZ = 5, fontSize = 16):
'''
:param data:
:param outputname:
:param cmap:
'''
aspect_ratio = float(data.shape[1])/data.shape[0]
fig = pylab.figure()
Scale = 8
# add +1 to get axis text
fig.set_size_inches(Scale*aspect_ratio+1,Scale*1)
ax = pylab.gca()
#ax.set_axis_off()
#fig.add_axes(ax)
if cmap != None:
pylab.set_cmap(cmap)
#ax.imshow(data, interpolation='nearest', aspect = 'normal')
ax.imshow(data, interpolation='nearest')
if rangeXpx == None:
rangeXpx = (0, data.shape[1])
if rangeZpx == None:
rangeZpx = (0, data.shape[0])
modBev_plot(ax, rangeX, rangeXpx, numDeltaX, rangeZ, rangeZpx, numDeltaZ, fontSize, xlabel = xlabel, ylabel = ylabel)
#plt.savefig(outputname, bbox_inches='tight', dpi = dpi)
pylab.savefig(outputname, dpi = data.shape[0]/Scale)
pylab.close()
fig.clear()
def saveBEVImageWithAxes(data, outputname, cmap = None, xlabel = 'x [m]', ylabel = 'z [m]', rangeX = [-10, 10], rangeXpx = None, numDeltaX = 5, rangeZ = [7, 62], rangeZpx = None, numDeltaZ = 5, fontSize = 16):
'''
:param data:
:param outputname:
:param cmap:
'''
aspect_ratio = float(data.shape[1])/data.shape[0]
fig = pylab.figure()
Scale = 8
# add +1 to get axis text
fig.set_size_inches(Scale*aspect_ratio+1,Scale*1)
ax = pylab.gca()
#ax.set_axis_off()
#fig.add_axes(ax)
if cmap != None:
pylab.set_cmap(cmap)
#ax.imshow(data, interpolation='nearest', aspect = 'normal')
ax.imshow(data, interpolation='nearest')
if rangeXpx == None:
rangeXpx = (0, data.shape[1])
if rangeZpx == None:
rangeZpx = (0, data.shape[0])
modBev_plot(ax, rangeX, rangeXpx, numDeltaX, rangeZ, rangeZpx, numDeltaZ, fontSize, xlabel = xlabel, ylabel = ylabel)
#plt.savefig(outputname, bbox_inches='tight', dpi = dpi)
pylab.savefig(outputname, dpi = data.shape[0]/Scale)
pylab.close()
fig.clear()
def multipage(filename, figs=None):
pp = PdfPages(filename)
if figs is None:
figs = [plt.figure(n) for n in plt.get_fignums()]
for fig in figs:
fig.savefig(pp, format='pdf')
pp.close()