def graph_rocchio(fname): with open(fname) as f: results = pickle.load(f) for percent in results.keys(): x = [] y = [] z = [] for alpha in results[percent]: for beta in results[percent][alpha]: x.append(alpha) y.append(beta) recalls = results[percent][alpha][beta]['recalls'] z.append(sum(recalls) / len(recalls)) fig = plt.figure() ax = fig.gca(projection='3d') xi = np.linspace(min(x), max(x)) yi = np.linspace(min(y), max(y)) X, Y = np.meshgrid(xi, yi) Z = griddata(x, y, z, xi, yi) surf = ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap=cm.jet, linewidth=1, antialiased=True) ax.set_zlim3d(np.min(Z), np.max(Z)) fig.colorbar(surf) ax.set_xlabel('alpha') ax.set_ylabel('beta') ax.set_zlabel('recall') plt.show()
def plot_signal(signal, shape=None, name=None): """ Plot a numeric signal as an image. """ s = np.copy(signal) if s.max() != 0.: s *= 255.0/s.max() s = s.astype(int) if shape: s = s.reshape(shape) plt.figure() plt.imshow(s, cmap=plt.cm.gray, interpolation='nearest') if name: plt.savefig(name) plt.close()
def show_timeorder_info(Dt, mesh_sizes, errors): '''Performs consistency check for the given problem/method combination and show some information about it. Useful for debugging. ''' # Compute the numerical order of convergence. orders = {} for key in errors: orders[key] = _compute_numerical_order_of_convergence(Dt, errors[key]) # Print the data to the screen for i, mesh_size in enumerate(mesh_sizes): print print('Mesh size %d:' % mesh_size) print('dt = %e' % Dt[0]), for label, e in errors.items(): print(' err_%s = %e' % (label, e[i][0])), print for j in range(len(Dt) - 1): print(' '), for label, o in orders.items(): print(' ord_%s = %e' % (label, o[i][j])), print print('dt = %e' % Dt[j+1]), for label, e in errors.items(): print(' err_%s = %e' % (label, e[i][j+1])), print # Create a figure for label, err in errors.items(): pp.figure() ax = pp.axes() # Plot the actual data. for i, mesh_size in enumerate(mesh_sizes): pp.loglog(Dt, err[i], '-o', label=mesh_size) # Compare with order curves. pp.autoscale(False) e0 = err[-1][0] for o in range(7): pp.loglog([Dt[0], Dt[-1]], [e0, e0 * (Dt[-1] / Dt[0]) ** o], color='0.7') pp.xlabel('dt') pp.ylabel('||%s-%s_h||' % (label, label)) # pp.title('Method: %s' % method['name']) ppl.legend(ax, loc=4) pp.show() return
def new_axes(self, figsize): fig = plt.figure(figsize=(figsize, figsize), facecolor='w', edgecolor='w') rect = [0.1, 0.1, 0.8, 0.8] ax = WindroseAxes(fig, rect, axisbg='w') fig.add_axes(ax) return fig, ax
def layerActivations(model, data, labels): print('Visualizing activations with tSNE...') if not os.path.exists(cc.cfg['plots']['layer_activations_dir']): os.makedirs(cc.cfg['plots']['layer_activations_dir']) numLabels = cc.cfg['plots']['layer_activations_label_cap'] data = data[:cc.cfg['plots']['layer_activations_points_cap']] labels = labels[:numLabels,:cc.cfg['plots']['layer_activations_points_cap']] subplotCols = numLabels subplotRows = len(model.layers)-1 subplotIdx = 1 plt.figure(figsize=(5*subplotCols,5*subplotRows)) for i in range(1,len(model.layers)): print('Running tSNE for layer {}/{}'.format(i+1,len(model.layers))) func = K.function([model.layers[0].input], [model.layers[i].output]) out = func([data])[0] tsneModel = TSNE(n_components = 2, random_state = 0) tsneOut = tsneModel.fit_transform(out).T # labeledTsneOut = np.hstack((tsneOut, labels[0].reshape(-1,1))) for j in range(numLabels): plt.subplot(subplotRows, subplotCols, subplotIdx) plt.title('{} / {}'.format(model.layers[i].name,cc.exp['params']['data']['labels'][j])) plt.scatter(tsneOut[0],tsneOut[1],c=labels[j],cmap = 'plasma') subplotIdx += 1 # tsneDF = pd.DataFrame(labeledTsneOut, columns = ('a', 'b', 'c')) # plot = tsneDF.plot.scatter(x = 'a', y = 'b', c = 'c', cmap = 'plasma') plt.tight_layout() plt.savefig('{}/activations.png'.format(cc.cfg['plots']['layer_activations_dir'])) plt.close() print('...done')
def histograms(modelLogger): if not cc.cfg['plots']['histograms']: return if not os.path.exists(cc.cfg['plots']['histograms_dir']): os.makedirs(cc.cfg['plots']['histograms_dir']) cntEpochs = len(modelLogger.epochLogs) cntLayers = len(modelLogger.epochLogs[-1]['weights']) logVals = [ {'name':'weights','color':'blue'}, {'name':'updates','color':'red'}, {'name':'ratios','color':'green'} ] subplotRows = len(logVals) subplotCols = cntLayers for x in range(cntEpochs): subplotIdx = 1 plt.figure(figsize=(5*subplotCols,5*subplotRows)) plt.suptitle('Histograms per layer, epoch {}/{}'.format(x,cntEpochs-1), fontsize=14) for logVal in logVals: for i,layer in enumerate(modelLogger.epochLogs[x][logVal['name']]): histmin = layer.min() histmax = layer.max() plt.subplot(subplotRows, subplotCols, subplotIdx) plt.title('{}, {}'.format(modelLogger.model.layers[modelLogger.loggedLayers[i]].name,logVal['name'])) plt.hist(layer, range = (histmin, histmax), bins = 30, color = logVal['color']) subplotIdx+=1 plt.tight_layout() plt.subplots_adjust(top=0.9) plt.savefig('{}/hist_{e:03d}.png'.format(cc.cfg['plots']['histograms_dir'], e = x)) plt.close()
env.reset(np.array([0.0])) code.reset() [mp,varp,spsp,sp,msep,parts,ws] = pf.particle_filter(code, env, timewindow=timewindow, dt=dt, nparticles=nparticles, mode='v', testf=(lambda x:x)) if gaussian: print "MSE of gaussian filter %f"% mseg print "MSE of particle filter %f"% msep if plotting: #matplotlib.rcParams['font.size']=10 plt.close() plt.figure() ax1 = plt.gcf().add_subplot(2,1,1) times = np.arange(0.0,dt*timewindow,dt) if gaussian: ax1.plot(times,sg,'r',label='Signal') if sum(sum(spsg)) !=0: (ts,neurs) = np.where(spsg == 1) spiketimes = times[ts] thetas = [code.neurons[i].theta for i in neurs] else: spiketimes = [] thetas = [] ax1.plot(spiketimes,thetas,'yo',label='Spike times') ax1.plot(times,mg,'b',label='Mean prediction') ax1.set_title('Gaussian Filter')
#ax.line([[10, 10], [16.5, 16.5]]) plt.xlabel("SDSS u magnitude, mag") plt.ylabel("GC u magnitude, mag") plt.savefig('test/u_SDSS_vs_GC') ''' limits = [[12, 18.5], [11.4, 16.5], [10, 16], [9.5, 16], [9.5, 16]] for i, band in enumerate(bands): lim_lo = limits[i][0] lim_hi = limits[i][1] x, m, b = makeDiagonalLine([lim_lo, lim_hi]) fig = plt.figure(figsize=(8, 8)) ax = plt.subplot(111) c = ppl.scatter(ax, petroMags[:, i], mags[:, i], s=8, c='k', edgecolor='k') ax.axis([lim_lo, lim_hi, lim_lo, lim_hi]) ax.errorbar(petroMags[:, i], mags[:, i], yerr=mag_err[:, i], mew=0, linestyle = "none", color="black") plt.plot(x,m*x + b, c='k') ax.xaxis.set_major_locator(majorLocator) ax.xaxis.set_minor_locator(minorLocator) ax.yaxis.set_major_locator(majorLocator) ax.yaxis.set_minor_locator(minorLocator) ax.minorticks_on() # Change the labels back to black ax.xaxis.label.set_color('black') ax.yaxis.label.set_color('black') # Change the axis title also back to black
[78, 376], [79, 164], [80, 194], [81, 342], [82, 314], [83, 68], [84, 54], [85, 131], [86, 52], [87, 220], [88, 145], [89, 63], [90, 608], [91, 68], [92, 155], [93, 49], [94, 108], [95, 59], [96, 67], [97, 58], [98, 42], [99, 210], [100, 174]]) plt.figure(figsize=(5,5)) plt.scatter(histdata.T[0],histdata.T[1]) plt.savefig('hist.png') plt.close()
# import matplotlib.pyplot as plt import prettyplotlib as ppl # makes nicer colors and generally better to look at graphs from prettyplotlib import plt # This is "import matplotlib.pyplot as plt" from the prettyplotlib library from prettyplotlib import mpl # This is "import matplotlib as mpl" from the prettyplotlib library # change font to Open Sans (has some kerning issues, though) # mpl.rcParams.update({'font.family':'Open Sans'}) # get name of file to process inputFileName = sys.argv[1] # keyPresses_Heatmap.csv # load csv file with EPOCHTIME;NOFPROCESSES data = np.loadtxt(inputFileName, delimiter=";") # data[:,1] = [x - notActuallyChromeTabs for x in data[:,1]] # print data[:,0], data[:,1] fig = plt.figure(figsize=(14,8)) ax = fig.add_subplot(111) # ppl.bar(ax, data[:,0], data[:,1]) ax.bar(data[:,0], data[:,1]) # ax.axis('tight') plt.show() # xy = [(x_int[i],y_int[i]) for i in range(0,len(x_int))] # xy_sorted = np.array(sorted(xy, key=lambda yz: yz[1], reverse=True)) # ax.bar(xy_sorted[:,0], xy_sorted[:,1])
## import sys if __name__ == '__main__': # Accumulate the counts fed into standard in (stdin) counts = [] for line in sys.stdin: topic, count = line.split() # Shift the ones up by a tiny amount to allow them to be visible on the graph counts.append(int(count) + 0.05) print 'Total page view counts: {}'.format(len(counts)) # Display the hourly page view distribution on a log-log plot # This matches our friendly and well understood Zipf distribution fig = plt.figure(figsize=(9, 5)) ax = fig.add_subplot(1, 1, 1) plt.plot(xrange(len(counts)), counts, linewidth=3, label='Pageviews per page') # ax.set_xscale('log') ax.set_yscale('log') # plt.title('Log-log plot of hourly Wikipedia page view distribution') plt.xlabel('Rank order') plt.ylabel('Frequency') plt.grid(color='black') plt.legend() # plt.savefig('hourly_wikipedia_zipf.pdf') plt.show(block=True)
import prettyplotlib as ppl from prettyplotlib import plt import numpy T = 20. dt = 0.001 plt.figure(figsize=(6,5)) def sample_and_plot(X0, A, H, dt, N, NSamples, ax): Xs = numpy.zeros((NSamples,N,2)) Xs[:,0] = X0 for i in range(1,N): for j in range(NSamples): Xs[j,i] = Xs[j,i-1] +dt*numpy.dot(A,Xs[j,i-1])\ + numpy.sqrt(dt)*numpy.dot(H, numpy.random.normal(size=(2,))) ts = numpy.arange(0.0,N*dt,dt) [ppl.plot(ts,Xs[i,:,0],ax=ax) for i in range(NSamples)] def sample(X0, A, H, dt, N, NSamples): Xs = numpy.zeros((NSamples,N,2)) Xs[:,0] = X0 for i in range(1,N): for j in range(NSamples): Xs[j,i] = Xs[j,i-1] +dt*numpy.dot(A,Xs[j,i-1])\ + numpy.sqrt(dt)*numpy.dot(H, numpy.random.normal(size=(2,))) ts = numpy.arange(0.0,N*dt,dt) return ts, Xs
cax = axs[1].imshow(masked,interpolation='nearest',aspect='auto', cmap = cmap) plt.colorbar(cax) axs[0].hist(sanitized.ravel(),bins=100,color='k') map(artist.adjust_spines,axs) axs[0].set_ylabel(artist.format('Frequency')) axs[0].set_xlabel(artist.format('Semantic Distance')) axs[1].set_ylabel(artist.format('Tweet')) axs[1].set_xlabel(artist.format('Tweet')) plt.tight_layout() plt.show() ''' pca =PCA(n_components=2) pca.fit(sanitized) decomp = plt.figure() panel = decomp.add_subplot(111) panel.scatter(pca.components_[0,:]*np.sqrt(pca.explained_variance_ratio_[0]), pca.components_[1,:]*np.sqrt(pca.explained_variance_ratio_[1]),s=20,c='k') artist.adjust_spines(panel) panel.set_xlabel(r'\Large $\mathbf{1^{st}}$ \textbf{Semantic Dimension}') panel.set_ylabel(r'\Large $\mathbf{2^{nd}}$ \textbf{Semantic Dimension}') plt.show() ''' x = SV('') x.frequencies(open('../data/alcohol.txt','rb').read().replace('\n','')) '''
def visualizeSequentialOutput(model, layerIdx, df): if not os.path.exists(cc.cfg['plots']['seq_output_dir']): os.makedirs(cc.cfg['plots']['seq_output_dir']) if cc.cfg['plots']['seq_output_seq_input_name'] == 'smiles': input = data.formatSequentialInput(df) elif cc.cfg['plots']['seq_output_seq_input_name'] == 'fasta': input = data.formatFastaInput(df) else: raise 'visual err' # model.layers[layerIdx].return_sequences = True # model.compile(loss="mean_squared_error", optimizer="rmsprop") cfg = model.get_config()[:4] cfg = model.get_config()[:layerIdx+1] del cfg[2] layerIdx -= 1 # print cfg cfg[layerIdx]['config']['return_sequences'] = True seqModel = Sequential.from_config(cfg) seqModel.set_weights(model.get_weights()) seqModel.layers[layerIdx].return_sequences = True outputFunction = K.function([seqModel.layers[0].input], [seqModel.layers[layerIdx].output]) output = outputFunction([input])[0] ''' sns.set() for i,smilesOutput in enumerate(output): g = sns.clustermap(smilesOutput.T, col_cluster=False, method='single',metric='cosine') g.savefig('{}/seq_output.png'.format(cc.cfg['plots']['seq_output_dir'])) ''' dropSet = Set(cc.cfg['plots']['seq_output_ignore_neurons']) if cc.cfg['plots']['seq_output_select_neurons']: arrMask = cc.cfg['plots']['seq_output_select_neurons'] else: arrMask = list(range(output.shape[2])) arrMask = np.array([x for x in arrMask if not x in dropSet]) fig = plt.figure(figsize=(input.shape[1] * 0.3,len(arrMask) * len(df) * 1.5)) for i,seqOutput in enumerate(output): # print seqOutput.shape # print seqOutput selected = seqOutput.T[arrMask] Z = sch.linkage(selected, method='single', metric='cosine') leaves = sch.leaves_list(Z) # leaves = range(len(selected)) reordered = selected[leaves] ax = fig.add_subplot(len(df),1,i+1) print 'foo' ppl.pcolormesh(fig, ax, reordered, xticklabels=list(df.values[i][0]), yticklabels=arrMask[leaves], vmin=-1, vmax=1) print 'foo' print 'bar' fig.savefig('{}/{}'.format(cc.cfg['plots']['seq_output_dir'],cc.cfg['plots']['seq_output_name'])) print 'bar'