def set_limits(l, plt): try: plt.xlim((-l ,l)) plt.ylim((-l ,l)) except: plt.set_xlim(-l ,l) plt.set_ylim(-l ,l)
def trim_Monthly_Timeseries_Figure(plt, draw_fy, draw_ly, fy, plt_or_ax = 'plt'): # fyから始まるmonthlyの時系列データを、draw_fy~draw_lyでトリミングする。 if plt_or_ax == 'plt': plt.xlim((draw_fy - fy)*12 + 1, (draw_ly - fy + 1)*12 + 1) elif plt_or_ax == 'ax': plt.set_xlim((draw_fy - fy)*12 + 1, (draw_ly - fy + 1)*12 + 1) return plt
def drawPRC(result, sp, methodlabel): ''' Accept a result dataframe whose 'cond' coulmn is binary series representing the ture condition and 'pred' column is the normalized score of predicton. ''' print("drawing prc curve...") sp.set_xlabel('Recall') sp.set_ylabel('Precision') sp.set_ylim([0.0, 1.0]) sp.set_xlim([0.0, 1.0]) sp.set_title('2-class Precision-Recall curve') precision, recall, threshold = precision_recall_curve( result['cond'], result['pred']) average_precision = average_precision_score(result['cond'], result['pred']) myauc = auc(recall, precision) step_kwargs = ({ 'step': 'post' } if 'step' in signature(sp.fill_between).parameters else {}) sp.step(recall, precision, alpha=0.2, where='post') sp.fill_between(recall, precision, alpha=0.2, label=methodlabel + " (AUC = %.3f)" % myauc, **step_kwargs) return myauc
def submit_start(text): global start, stop, dx, a, n, x, l start = float(text) x_list, y_list = solver_matrix(start, stop, dx, a, n, x) plotto.set_xlim(xmin=start) l.set_ydata(y_list) ax.set_ylim(np.min(y_list), np.max(y_list))
def plotSolnAndGrad(self, plt, solVec, title=""): plt.set_title(title) # plt.set_xlabel('x',size=14,weight='bold') # plt.set_ylabel('y',size=14,weight='bold') plt.set_aspect('equal') plt.set_xlim(-0.1, 5.1) plt.set_ylim(-0.1, 1.2) xy = np.asarray(self.mesh.vertices) tci = tri.CubicTriInterpolator(self.mesh.dtri, solVec) (Ex, Ey) = tci.gradient(self.mesh.dtri.x, self.mesh.dtri.y) E_norm = np.sqrt(Ex**2 + Ey**2) vals = plt.tricontourf(self.mesh.dtri, solVec, cmap="jet") plt.quiver(self.mesh.dtri.x, self.mesh.dtri.y, -Ex / E_norm, -Ey / E_norm, units='xy', scale=20., zorder=3, color='blue', width=0.002, headwidth=2., headlength=2.) return vals
def plotHistogram(graph_number, weight_range, optimizer, weight_decay): weights_histogram_small = small_batch_weights_by_graph_number[graph_number] weights_histogram_big = big_batch_weights_by_graph_number[graph_number] #plot a histogram of the weights fig, plt = subplots() plt.hist(weights_histogram_small, bins=number_bins, range=weight_range, facecolor='blue', alpha=0.5, label=str(small_batch_size)) plt.hist(weights_histogram_big, bins=number_bins, range=weight_range, facecolor='orange', alpha=0.5, label=str(big_batch_size)) #set axis labels and titles plt.set_xlabel('Weight Bins') plt.set_ylabel('Number of Weights') plt.set_title('Histogram of Weights for Configuration' + str(optimizer) + ', WD = ' + str(weight_decay)) #obtain min and max values of tuple mi, mx = weight_range #set axis limits plt.set_xlim([mi, mx]) plt.set_ylim([0, 5000000]) plt.legend(loc='upper right')
def plotPolys( plt, fullDataset, fittingData, echemRegion, polyQ=False): # Generates polynomials and plots them to a given graph polyArray = np.zeros(len(fullDataset[:, 0])) for i in range(3, 11): coefficients = np.polyfit(x=fittingData[:, 0], y=fittingData[:, 1], deg=i) poly = np.poly1d(coefficients) baseline_y = poly(fullDataset[:, 0]) plt.plot(fullDataset[:, 0], baseline_y, alpha=0.5, label=i, linestyle="--") polyArray = np.c_[polyArray, baseline_y] plt.plot(echemRegion[:, 0], echemRegion[:, 1], 'r-') plt.legend() plt.set_xlim((0.7 * min(echemRegion[:, 0])), (1.3 * max(echemRegion[:, 0]))) plt.set_ylim((0.7 * min(echemRegion[:, 1])), (1.3 * max(echemRegion[:, 1]))) if (polyQ == True): polyArray = np.delete(polyArray, 0, 1) np.savetxt(".out/polys.out", polyArray)
def plot_cpu(plt): data = None plt.set_xlim([xmin, xmax]) with open("data/pidstats.json") as f: data = json.loads(f.read()) x = [] from collections import defaultdict ys = defaultdict(dict) for ts in data: values = data[ts] x.append(ts) for obj in values: key = obj['command'] ys[key][ts] = obj keys = list(ys.keys()) keys.sort() x.sort(key=lambda x: int(x)) for i, key in enumerate(keys): y = [] for ts in x: if not ts in ys[key]: y.append(None) else: y.append(ys[key][ts]['%cpu']) plt.plot(to_ts(x), y, 'x', label=key) plt.legend([key])
def plotMesh(self, plt, title=""): plt.set_title("Mesh") # plt.set_xlabel('x',size=14,weight='bold') # plt.set_ylabel('y',size=14,weight='bold') plt.set_aspect('equal'); plt.set_xlim(-0.1,5.1); plt.set_ylim(-0.1,1.2); xy = np.asarray(self.mesh.vertices); vals=plt.triplot(xy[:,0],xy[:,1],self.mesh.elements,'b-',linewidth=0.5); return vals
def zero(ax=None, dim='y'): if ax: if dim == 'y': ax.set_ylim([0, ax.get_ylim()[1]]) if dim == 'x': ax.set_xlim([0, ax.get_xlim()[1]]) else: if dim == 'y': plt.ylim([0, plt.gca().get_ylim()[1]]) if dim == 'x': plt.set_xlim([0, plt.gca().get_xlim()[1]])
def sub_training_plot(plt, df, cols, index_col= None, main='',xlabel='xname', ylabel='yname', ylims=None, xlims=None, iflegend= 1 ): x= df[index_col] if index_col else df.index for col in cols: plt.plot(x, df[col],'-', label=col) #plt.bar(x, df[col],width=2/4, label='fps' ) if ylims and len(ylims)== 2: # a tuple plt.set_ylim(ylims) if xlims and len(xlims)== 2: # a tuple plt.set_xlim(xlims) if iflegend: plt.legend(loc = 'best') plt.set_title(main, fontsize=11)
def size_residual(m, dT, min_mused, legend=None, color=None): import numpy as np import matplotlib.pyplot as plt min_mag = 13.5 max_mag = 21 #min_mused = 15 # Bin by mag mag_bins = np.linspace(min_mag, max_mag, 71) print('mag_bins = ', mag_bins) index = np.digitize(m, mag_bins) print('len(index) = ', len(index)) bin_dT = [dT[index == i].mean() for i in range(1, len(mag_bins))] print('bin_dT = ', bin_dT) bin_dT_err = [ np.sqrt(dT[index == i].var() / len(dT[index == i])) for i in range(1, len(mag_bins)) ] print('bin_dT_err = ', bin_dT_err) # Fix up nans for i in range(1, len(mag_bins)): if i not in index: bin_dT[i - 1] = 0. bin_dT_err[i - 1] = 0. print('fixed nans') print('index = ', index) print('bin_dT = ', bin_dT) if legend is None: legend = [r'$\delta T$'] plt.plot([min_mag, max_mag], [0, 0], color='black') plt.plot([min_mused, min_mused], [-1, 1], color='Grey') plt.fill([min_mag, min_mag, min_mused, min_mused], [-1, 1, 1, -1], fill=False, hatch='/', color='Grey') t_line = plt.errorbar(mag_bins[:-1], bin_dT, yerr=bin_dT_err, color=color, fmt='o') plt.legend([t_line], [legend]) plt.set_ylabel(r'$T_{\rm PSF} - T_{\rm model} ({\rm arcsec}^2)$') plt.set_ylim(-0.002, 0.012) plt.set_xlim(min_mag, max_mag) plt.set_xlabel('Magnitude')
def make_gif(frame): plt.pcolormesh(np.array(frame), norm=colors.Normalize(vmin=np.min(frames),vmax=np.max(frames)),cmap=pickcol) plt.set_ylim(0,30) plt.set_xlim(0,20) plt.axis('square') plt.axis('off') buf = io.BytesIO() plt.savefig(buf, format='png', bbox_inches='tight') plt.clf() buf.seek(0) im = Image.open(buf) images.append(im)
def make_plot(): with open('i2.dump', 'rb') as dump_file: polies = pickle.load(dump_file) plt = GeoPlot() plt.add_collection( PatchCollection(polies, alpha=0.5, edgecolor='black', linewidth=0.001, match_original=True)) plt.set_xlim(-120, -40) plt.set_ylim(-15, 80) plt.save_fig('../figures/i2.pdf')
def shape1_residual(m, de1, min_mused, legend=None, color=None): import numpy as np import matplotlib.pyplot as plt min_mag = 13.5 max_mag = 21 #min_mused = 15 # Bin by mag mag_bins = np.linspace(min_mag, max_mag, 71) print('mag_bins = ', mag_bins) index = np.digitize(m, mag_bins) print('len(index) = ', len(index)) bin_de1 = [de1[index == i].mean() for i in range(1, len(mag_bins))] print('bin_de1 = ', bin_de1) bin_de1_err = [ np.sqrt(de1[index == i].var() / len(de1[index == i])) for i in range(1, len(mag_bins)) ] print('bin_de1_err = ', bin_de1_err) # Fix up nans for i in range(1, len(mag_bins)): if i not in index: bin_de1[i - 1] = 0. bin_de1_err[i - 1] = 0. print('fixed nans') print('index = ', index) print('bin_de1 = ', bin_de1) print('bin_de2 = ', bin_de2) print('bin_dT = ', bin_dT) if legend is None: legend = [r'$\delta e_1$'] plt.plot([min_mag, max_mag], [0, 0], color='black') plt.plot([min_mused, min_mused], [-1, 1], color='Grey') plt.fill([min_mag, min_mag, min_mused, min_mused], [-1, 1, 1, -1], fill=False, hatch='/', color='Grey') e1_line = plt.errorbar(mag_bins[:-1], bin_de1, yerr=bin_de1_err, color=color, fmt='o') plt.set_ylim(-3.e-4, 6.5e-4) plt.set_xlim(min_mag, max_mag) plt.set_xlabel('Magnitude')
def mesh_plot(plt, X, Y, Z, title): plt.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.7, cmap=cm.jet) # DEBUG plt.plot_wireframe(X, Y, Z, rstride=1, cstride=1) plt.contourf(X, Y, Z, zdir='z', cmap=cm.jet, offset=-400) # These used to use coolwarm plt.contourf(X, Y, Z, zdir='x', cmap=cm.jet, offset=41.6) plt.contourf(X, Y, Z, zdir='y', cmap=cm.jet, offset=-87.4) plt.set_xlabel('Lat') plt.set_xlim(41.6, 42.2) plt.set_ylabel('Long') plt.set_ylim(-88.0, -87.4) plt.set_zlabel('Number of Crimes') plt.set_zlim(-100, 1000) plt.set_title(f'{title}')
def plot_battery(plt): data = None plt.set_xlim([xmin, xmax]) with open("data/battery.json") as f: data = json.loads(f.read()) x = [] y = [] for ts in data: values = data[ts] x.append(ts) y.append(list(map(lambda x: x['charge_now'], values))) plt.plot(to_ts(x), y, label='battery')
def scatterhw12(F=0.4, dt=0.001, o=0): '''scatter method ''' oTitle = str(o * 100) odes = solve_odes(0, 0, F, T=1000 * np.pi) for i in range(1000): plt.scatter(odes["x"][(int)(o + (i / dt) * 2 * np.pi)], odes["y"][(int)(o + (i / dt) * 2 * np.pi)], marker='.', color='k') plt.set_xlim(-1.5, 1.5) plt.set_ylim(-1.1, 1.1) plt.set_xlabel("x") plt.set_ylabel("x'") plt.savefig('frame' + oTitle + '.png')
def plot_hist_line(plt, data, bins, label, color=None, alpha=0.75): global right, top, font avg = data.mean() std = data.std() # label = f'{label}\nmean={avg:.0f},std={std:.0f}' label = f'{label}' # n, bins, patches = plt.hist(data, bins=bins, density=True, color=color, # alpha=alpha, label=label, histtype='step', kde=True # ) sns.distplot(data, hist=False, kde=True, color=color, ax=plt, label=label) # color='tab:red' plt.set_xlim(right=right) plt.set_ylim(top=top) plt.legend(fontsize=font['size']-1) plt.grid(axis='y', alpha=0.4)
def plot_convergence(p_1,p_2,p_3, zoom=3): plt.plot( p_1[:,0], p_1[:,1], '-', label="planet 1", c=c1) plt.plot( p_2[:,0], p_2[:,1], 's', label="planet 2", c=c2) plt.plot( p_3[:,0], p_3[:,1], '^', label="planet 3", c=c3) plt.set_xlabel("x$_{position}$ [AU]", fontsize=20) plt.set_ylabel("y$_{position}$ [AU]", fontsize=20) plt.set_xlim(-zoom, zoom) plt.set_ylim(-zoom, zoom) plt.legend(prop={'size': 15},markerscale=1,fancybox=True,loc=2) plt.tight_layout() axes.axis('equal') plt.show()
def plot_hist(plt, data, bins, label, color=None, alpha=0.75): global right, top avg = data.mean() std = data.std() label = f'{label}\nmean={avg:.0f},std={std:.0f}' n, bins, patches = plt.hist(data, bins=bins, density=True, color=color, alpha=alpha, label=label) # color='tab:red' plt.axvline(avg, color=color, linestyle='-', linewidth=1, alpha=alpha+0.2) plt.axvline(avg+std, color=color, linestyle='--', linewidth=1, alpha=alpha) plt.axvline(avg-std, color=color, linestyle='--', linewidth=1, alpha=alpha) plt.set_xlim(right=right) plt.set_ylim(top=top) plt.legend(fontsize=font['size']-1) plt.grid(axis='y', alpha=0.4) return n, bins, patches
def plotstft(plt, samples, samplerate, binsize=2**10, plotpath=None, colormap="jet"): s = stft(samples, binsize) sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate) ims = 20. * np.log10(np.abs(sshow) / 10e-6) # amplitude to decibel timebins, freqbins = np.shape(ims) print("timebins: ", timebins) print("freqbins: ", freqbins) # plt.figure() plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none") # plt.figure.colorbar() plt.set_xlabel("time (s)") plt.set_ylabel("frequency (Hz)") plt.set_xlim([0, timebins - 1]) plt.set_ylim([0, freqbins]) xlocs = np.float32(np.linspace(0, timebins - 1, 5)) plt.set_xticks(xlocs, [ "%.02f" % l for l in ((xlocs * len(samples) / timebins) + (0.5 * binsize)) / samplerate ]) ylocs = np.int16(np.round(np.linspace(0, freqbins - 1, 10))) plt.set_yticks(ylocs, ["%.02f" % freq[i] for i in ylocs]) # if plotpath: # plt.savefig(plotpath, bbox_inches="tight") # else: # plt.show() # plt.clf() return ims
def plotSoln(self, plt, solVec, title=""): plt.set_title(title) # plt.set_xlabel('x',size=14,weight='bold') # plt.set_ylabel('y',size=14,weight='bold') plt.set_aspect('equal') plt.set_xlim(-0.1, 5.1) plt.set_ylim(-0.1, 1.2) xy = np.asarray(self.mesh.vertices) if xy.size < 10000: plt.triplot(xy[:, 0], xy[:, 1], self.mesh.elements, 'b-', linewidth=0.5) vals = plt.tricontourf(self.mesh.dtri, solVec, cmap="jet") return vals
def drawROC(result, sp, methodlabel): print("drawing roc curve...") sp.set_xlabel('False positive rate (FPR)') sp.set_ylabel('Ture positive rate (TPR)') sp.set_ylim([0.0, 1.0]) sp.set_xlim([0.0, 1.0]) #sp.set_title('2-class ROC curve') fpr, tpr, thresholds = roc_curve(result['cond'], result['pred']) myauc = auc(fpr, tpr) step_kwargs = ({ 'step': 'post' } if 'step' in signature(sp.fill_between).parameters else {}) sp.step(fpr, tpr, color="green", alpha=1, where='post') print("AUC: %.3f" % myauc) #sp.fill_between(fpr, tpr, color="green", alpha=1, label=methodlabel+" (AUC = %.3f)"%myauc, **step_kwargs) return myauc
def Plot_normal(x_bird, x_sky, x_cloud, mu_bird, mu_sky, mu_cloud, sigma_bird, sigma_sky, sigma_cloud): temp = np.array([i for i in range(-1000, 1000)]) # y_pdf_bird_r = norm.pdf(temp, mu_bird[0], sigma_bird[0,0]) y_pdf_bird_g = norm.pdf(temp, mu_bird[1], sigma_bird[0, 0]) # y_pdf_bird_b = norm.pdf(temp, mu_bird[2], sigma_bird[0,0]) # # plt.plot(temp,y_pdf_bird_r,label='Bird Normal Distribution',color = 'r') plt.plot(temp, y_pdf_bird_g, label='Bird Normal Distribution', color='r') # plt.plot(temp,y_pdf_bird_b,label='Bird Normal Distribution', color = 'r') # plt.axvline(x=mu_bird[0],color='r') plt.axvline(x=mu_bird[1], color='r') # plt.axvline(x=mu_bird[2],color='r') # plt.axvline(x=mu_sky[0],color='g') plt.axvline(x=mu_sky[1], color='g') # plt.axvline(x=mu_sky[2],color='g') # plt.axvline(x=mu_cloud[0],color='b') plt.axvline(x=mu_cloud[1], color='b') # plt.axvline(x=mu_cloud[2],color='b') # y_pdf_sky_r = norm.pdf(temp, mu_sky[0], sigma_sky[1,1]) y_pdf_sky_g = norm.pdf(temp, mu_sky[1], sigma_sky[1, 1]) # y_pdf_sky_b = norm.pdf(temp, mu_sky[2], sigma_sky[1,1]) # # plt.plot(temp,y_pdf_sky_r,label='Bird Normal Distribution',color = 'g') # plt.plot(temp,y_pdf_sky_g,label='Bird Normal Distribution', color = 'g') # plt.plot(temp,y_pdf_sky_b,label='Bird Normal Distribution', color = 'g') # y_pdf_cloud_r = norm.pdf(temp, mu_cloud[0], sigma_cloud[2,2]) y_pdf_cloud_g = norm.pdf(temp, mu_cloud[1], sigma_cloud[2, 2]) # y_pdf_cloud_b = norm.pdf(temp, mu_cloud[2], sigma_cloud[2,2]) # # plt.plot(temp,y_pdf_cloud_r,label='Bird Normal Distribution',color = 'b') plt.plot(temp, y_pdf_cloud_g, label='Bird Normal Distribution', color='b') # plt.plot(temp,y_pdf_cloud_b,label='Bird Normal Distribution', color = 'b') # plt.set_xlim([-255, 255]) plt.show
def fix_hplot(df, statistic, xlabel, ylabel, hue, fontsize): data = df.query('statistic == "{}"'.format(statistic)) pastel = ["#92C6FF", "#97F0AA", "#FF9F9A", "#D0BBFF", "#FFFEA3", "#B0E0E6"] pal = dict(Validation=pastel[0], Test=pastel[2]) plt = sns.barplot(x='data', y='label', data=data, hue=None, errwidth=1.0, capsize=0.15) [ticklabel.set_fontsize(fontsize) for ticklabel in (plt.get_yticklabels())] [ticklabel.set_fontsize(fontsize) for ticklabel in (plt.get_xticklabels())] plt.set_yticklabels([ticklabel._text.capitalize() for ticklabel in (plt.get_yticklabels())], ha='left') plt.get_yaxis().get_label().set_fontsize(fontsize) plt.get_xaxis().get_label().set_fontsize(fontsize) plt.get_yaxis().set_tick_params(pad=fontsize*10-10) plt.set_ylabel(ylabel) plt.set_xlabel(xlabel) plt.set_xlim(0, 1.05) return plt
def plotSentiment(self): fig = plt.figure() fig.suptitle('Sentiment') ax1 = fig.add_subplot(311) bins = np.arange(-1.05, 1.05, .1) n, bins, patches = ax1.hist(self.tb_data, bins) ax1.set_xlim([-1, 1]) ax1.set_ylabel('TextBlob'.format(np.mean(self.tb_data))) ax2 = fig.add_subplot(312) ax2.hist(self.vader_diff, bins) ax2.set_ylabel('NLTK Difference'.format(np.mean(self.vader_diff))) plt.set_xlim([-1, 1]) ax3 = fig.add_subplot(313) ax3.hist(self.vader_comp, bins) ax3.set_ylabel('NLTK Compound'.format(np.mean(self.vader_comp))) ax3.set_xlim([-1, 1]) return FigureCanvas(fig)
def plot_decision_regions(X, y, classifier, plt, resolution=0.02): #setup marker generator and color map markers = ('s','x','o','^','v') colors = ('red','blue','lightgreen','gray','cyan') cmap = ListedColormap(colors[:len(np.unique(y))]) x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx1, xx2 = np.meshgrid( np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) Z = Z.reshape(xx1.shape) plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap) plt.set_xlim(xx1.min(), xx1.max()) plt.set_ylim(xx2.min(), xx2.max()) for idx, cl in enumerate(np.unique(y)): plt.scatter(x=X[y==cl, 0], y=X[y==cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl)
fig = figure(1, figsize=(8.5, 4)) ax = fig.add_subplot(111) PL01 = ax.plot_date(dia_mes1, var_mes1, '-', xdate=True, ydate=False, lw=3, color='blue') # this is superfluous, since the autoscaler should get it right, but # use date2num and num2date to to convert between dates and floats if # you want; both date2num and num2date convert an instance or sequence ax.set_xlim(d_ini, d_end) ax.set_ylim(0.001, 10) ax.xaxis.set_major_locator(DayLocator()) ax.xaxis.set_minor_locator(HourLocator(arange(0, 25, 6))) ax.xaxis.set_major_formatter(DateFormatter('%a\n%b-%d')) ax.fmt_xdata = DateFormatter('%d/%m/%y %H:%M') #fig.autofmt_xdate() #ax.legend( (TPS1,TPS2,TPS3), ('DTL','NR','PR'), bbox_to_anchor=(0., 1.02, 1., .702), #loc=3,ncol=3, mode="expand", borderaxespad=0.) ax.axhline(y=0, lw=1, color='black') xlabel('Dia', fontsize=12,
def weight_analysis(net, pars, epoch): decays = pars['decays'] scales = pars['scales'] if net.is_W_parametrized: W = net.W.detach() elif net.is_2_2_parametrized: logging.error('Only W parametrized nets available for now') raise RuntimeError if net.is_dale_constrained: W = net.W.mm(tch.diag(net.synapse_signs)).detach() # Total In/Out going weights sum_in, sum_out = W.sum(dim=1).cpu().numpy(), W.sum(dim=0).cpu().numpy() fig = sns.jointplot(sum_in, sum_out).set_axis_labels("Sum of incoming weights", "Sum of outgoing weights") fig.savefig(net.save_folder + '{}/'.format(epoch) + 'in_out_jointplot.pdf') plt.close() #Plot the weight-matrix, useful mostly for Dale and disjoint initialization w_abs = max(W.min().abs(), W.max().abs()) fig, ax = plt.subplots() seismic = plt.get_cmap('seismic') sns.heatmap(W.cpu().numpy(), center=0, cmap=seismic, ax=ax) # ax.invert_yaxis() fig.savefig(net.save_folder + '{}/'.format(epoch) + 'weights_heatmap.png') plt.close(fig) # Histogram of the weights themselves W_np = W.flatten().detach().cpu().numpy() W_nonzero = W_np[np.where(W_np != 0)] if W_nonzero is None: plt.figure() plt.axhline(y=0) plt.set_xlim(0, 1) plt.title('Fraction of zero weights : {}'.format(np.mean(W_np == 0))) plt.savefig(net.save_folder + '{}/'.format(epoch) + 'weights_histogram.pdf') plt.close() else: plt.figure() plt.hist(W_nonzero, bins=30) plt.title('Fraction of zero weights : {}'.format(np.mean(W_np == 0))) plt.savefig(net.save_folder + '{}/'.format(epoch) + 'weights_histogram.pdf') plt.close() plt.figure() plt.hist(W_nonzero, bins=30, log=True) plt.title('Fraction of zero weights : {}'.format(np.mean(W_np == 0))) plt.savefig(net.save_folder + '{}/'.format(epoch) + 'weights_histogram_log.pdf') plt.close() # Checking if Dale's Law is satisfied tmp_1 = np.mean(np.sum(W.cpu().numpy() <= 0., axis=0) == net.n) tmp_2 = np.mean(np.sum(W.cpu().numpy() >= 0., axis=0) == net.n) plt.figure() plt.scatter(np.mean((W.cpu().numpy() >= 0), axis=0), np.mean((W.cpu().numpy() < 0), axis=0)) plt.xlabel('Fraction of positive out-going connections') plt.ylabel('Fraction of strictly negative out-going connections') plt.title('Fraction of only negative out : {}, only positive {}'.format( tmp_1, tmp_2)) plt.savefig(net.save_folder + '{}/'.format(epoch) + 'dale_satisfaction.pdf') plt.close() # Study of eigenvalues; only really useful for linear networks, in all other cases svd is more relevant # (in particular for ReLU our optimal solution is not diagonalizable) if net.saturations == [-1e8, 1e8] and net.activation_type == 'ReLU': eigs, _ = tch.eig(W, eigenvectors=False) eigs = eigs.detach().cpu().numpy() fig, axes = plt.subplots(1, 2, figsize=(16, 8)) axes[0].scatter(eigs[:, 0], eigs[:, 1], marker='x') axes[1].scatter(eigs[:, 0], eigs[:, 1], marker='x') circles = [ plt.Circle((0, 0), radius=decays[c], facecolor='None', edgecolor='k', ls='--') for c in range(net.n_channels) ] for circ in circles: axes[0].add_patch(circ) circles = [ plt.Circle((0, 0), radius=decays[c], facecolor='None', edgecolor='k', ls='--') for c in range(net.n_channels) ] for circ in circles: axes[1].add_patch(circ) axes[0].axhline(y=0) axes[0].axvline(x=0) axes[0].set_xlim(-1.1, 1.1) axes[0].set_ylim(-1.1, 1.1) axes[1].axhline(y=0) axes[1].set_xlim(0.92, 1.02) axes[1].set_ylim(-0.1, 0.1) fig.savefig(net.save_folder + '{}/'.format(epoch) + 'eigs_density.pdf') plt.close(fig) else: U, sigmas, V = tch.svd(W, compute_uv=((epoch == 'final') or net.is_dale_constrained)) sigmas = sigmas.detach().cpu().numpy() np.savetxt(net.save_folder + '{}/'.format(epoch) + 'sigmas.txt', sigmas) plt.figure() plt.hist(sigmas, density=False, log=True, bins=100) plt.title( 'First (D+1) s.v. : ' + ', '.join(['{:.3}'.format(s) for s in sigmas[:net.n_channels + 1]])) plt.savefig(net.save_folder + '{}/'.format(epoch) + 'svd_hist_log.pdf') plt.close() if epoch == 'final': np.save(net.save_folder + 'final/lefts.npy', U[:, :net.n_channels].detach().cpu().numpy()) np.save(net.save_folder + 'final/rights.npy', V[:, :net.n_channels].detach().cpu().numpy()) if net.is_dale_constrained: exc_idx = range(net.n_excit) inh_idx = range(net.n_excit, net.n) U = U.detach().cpu().numpy() V = V.detach().cpu().numpy() dots_lr = np.zeros((net.n_channels + 1, net.n_channels + 1)) for i in range(net.n_channels + 1): for j in range(net.n_channels + 1): dots_lr[i, j] = V[:, i].dot(U[:, j]).item() fig, ax = plt.subplots() sns.heatmap(dots_lr, center=0, cmap=seismic, ax=ax, annot=True, fmt=".2") fig.savefig(net.save_folder + '{}/'.format(epoch) + 'dot_products_right_left_heatmap.png') plt.close(fig) dots_dl = np.zeros((net.n_channels, net.n_channels + 1)) for i in range(net.n_channels): for j in range(net.n_channels + 1): dots_dl[i, j] = net.decoders[i].detach().cpu().numpy().dot( U[:, j]).item() fig, ax = plt.subplots() sns.heatmap(dots_dl, center=0, cmap=seismic, ax=ax, annot=True, fmt=".2") fig.savefig(net.save_folder + '{}/'.format(epoch) + 'dot_products_decoders_left_heatmap.png') plt.close(fig) for c in range(net.n_channels + 1): fig, axes = plt.subplots(2) axes[0].scatter(exc_idx, U[exc_idx, c], c='r', marker='x', s=4) axes[0].scatter(inh_idx, U[inh_idx, c], c='b', marker='x', s=4) axes[0].axhline(y=0, ls='--') axes[0].set_ylabel('Left eigenvector component') axes[1].scatter(exc_idx, V[exc_idx, c], c='r', marker='x', s=4) axes[1].scatter(inh_idx, V[inh_idx, c], c='b', marker='x', s=4) axes[1].axhline(y=0, ls='--') axes[1].set_ylabel('Right eigenvector component') fig.savefig(net.save_folder + '{}/'.format(epoch) + 'singular_vectors_structure_{}.pdf'.format(c))
def run(inputArgument, maxX, maxY): if os.path.isdir(inputArgument): inputFiles = os.listdir(inputArgument) num = len(inputFiles) numCols = int(math.ceil(math.sqrt(num))) numRows = int(2 * numCols) plt.figure(figsize=(18, 10)) gs = gridspec.GridSpec(numRows, numCols) gs.update(hspace=0.3) for i in range(num): lines = open(inputArgument + '/' + inputFiles[i], 'r').read().split('\n') x1 = [] y1 = [] x2 = [] y2 = [] for line in lines: fields = line.split() if len(fields) == 4: x1.append(float(fields[0])) y1.append(float(fields[1])) x2.append(float(fields[2])) y2.append(float(fields[3])) ax1 = plt.subplot(gs[int(2 * int(i / numCols)), i % numCols]) ax2 = plt.subplot(gs[int(1 + (2 * int(i / numCols))), i % numCols]) vis = False ax1.get_xaxis().set_visible(vis) ax1.get_yaxis().set_visible(vis) ax2.get_xaxis().set_visible(vis) ax2.get_yaxis().set_visible(vis) ax1.set_xlim([0, maxX]) ax1.set_ylim([0, maxY]) ax2.set_xlim([0, maxX]) ax2.set_ylim([0, maxY]) ax1.plot(x1, y1, 'b.') ax1.set_title(inputArgument + '/' + inputFiles[i], fontsize=6) ax2.plot(x2, y2, 'r.') #ax2.set_title(inputFiles[i], fontsize=6) else: lines = open(inputArgument, 'r').read().split('\n') x1 = [] y1 = [] x2 = [] y2 = [] for line in lines: fields = line.split() if len(fields) == 4: x1.append(float(fields[0])) y1.append(float(fields[1])) x2.append(float(fields[2])) y2.append(float(fields[3])) plt.subplot(2, 1, 1) plt.plot(x1, y1, 'b.') plt.title(inputArgument) plt.subplot(2, 1, 2) plt.plot(x2, y2, 'r.') plt.set_xlim([0, maxX]) plt.set_ylim([0, maxY]) plt.show()
def plotZ(Theta, Phi, Z, counter , resolution , sourceLocation=[0,0] , sideHists=False ): f = plt.figure(1 , figsize=(8,4)) if( sideHists == True): left, width = 0.1, 0.65 bottom, height = 0.1, 0.65 bottom_h = left_h = left + width + 0.03 rect_scatter = [left, bottom, width, height] rect_histx = [left, bottom_h , width, 0.2] rect_histy = [left_h, bottom, 0.2, height] axHistx = plt.axes(rect_histx) axHisty = plt.axes(rect_histy) axHistx.xaxis.set_visible(False) axHisty.yaxis.set_visible(False) axHisty.set_ylim(-90 , 90 ) axHistx.set_xlim(-180 , 180 ) axScatter = plt.axes(rect_scatter) axScatter.pcolormesh(Theta,Phi,Z, cmap='jet') #http://matplotlib.org/users/colormaps.html axScatter.set_xlabel(r"Azimuthal Angle [$\theta$]") axScatter.set_ylabel(r"Altitude Angle [$\phi$]") axScatter.set_xlim(-180 , 180 ) axScatter.set_ylim(-90 , 90 ) axScatter.scatter( sourceLocation[0] , sourceLocation[1] , s=50 , facecolors='none', edgecolors='r' , label='actual source location') axScatter.xaxis.labelpad = -1 axScatter.legend() plt.text( 205 , 130 , "Cones: "+str(counter) ,fontsize=16 ) # integrate Z over each dimension Azimuth = np.linspace(-180 , 180 ,resolution[0]) Altitude = np.linspace(-90 , 90 ,resolution[1]) thetaDist = np.sum(Z , axis=0) thetaDist = thetaDist / np.sum(thetaDist) phiDist = np.sum(Z , axis=1) phiDist = phiDist / np.sum(phiDist) adjustment = (max(phiDist) - min(phiDist))*0.05 axHistx.set_ylim(min(thetaDist) , max(thetaDist)*1.1 ) axHisty.set_xlim( min(phiDist) + adjustment , max(phiDist)*1.1 ) # get max theta , phi thetaMax , phiMax = Azimuth[ np.argmax(thetaDist) ] , Altitude[ np.argmax(phiDist) ] # plot integrated theta, phi distributions axHistx.plot( Azimuth , thetaDist , linewidth=3 ) axHisty.plot( phiDist , Altitude , linewidth=3 ) axHistx.fill_between( Azimuth , thetaDist , facecolor='b') axHisty.fill_between( np.append( phiDist , 0) , np.append( Altitude , 0) , facecolor='b') # plot crosshairs over centers of the distribution axHisty.plot( [0, max(phiDist)] , [ phiMax , phiMax ] , 'r--' ) axHistx.plot( [thetaMax , thetaMax ] , [0, max(thetaDist)] , 'r--' ) axScatter.plot( [-180 , 180 ] , [phiMax , phiMax] , 'r--') axScatter.plot( [thetaMax , thetaMax] , [-90 , 90] , 'r--') else: plt.title("Cones: "+str(counter)) plt.pcolormesh(Theta,Phi,Z, cmap='jet') #http://matplotlib.org/users/colormaps.html plt.set_xlabel(r"Azimuthal Angle [$\theta$]") plt.set_ylabel(r"Altitude Angle [$\phi$]") plt.set_xlim(-180 , 180 ) plt.set_ylim(-90 , 90 ) plt.scatter( sourceLocation[0] , sourceLocation[1] , s=50 , facecolors='none', edgecolors='r' , label='actual source location') plt.legend() plt.show() plt.close()