我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用matplotlib.pylab.close()。
def plot_x_y_yhat(x, y, y_hat, xsz, ysz, binz=False): """Plot x, y and y_hat side by side.""" plt.close("all") f = plt.figure(figsize=(15, 10.8), dpi=300) gs = gridspec.GridSpec(1, 3) if binz: y_hat = (y_hat > 0.5) * 1. ims = [x, y, y_hat] tils = [ "x:" + str(xsz) + "x" + str(xsz), "y:" + str(ysz) + "x" + str(ysz), "yhat:" + str(ysz) + "x" + str(ysz)] for n, ti in zip([0, 1, 2], tils): f.add_subplot(gs[n]) if n == 0: plt.imshow(ims[n], cmap=cm.Greys_r) else: plt.imshow(ims[n], cmap=cm.Greys_r) plt.title(ti) return f
def plot_x_x_yhat(x, x_hat): """Plot x, y and y_hat side by side.""" plt.close("all") f = plt.figure() # figsize=(15, 10.8), dpi=300 gs = gridspec.GridSpec(1, 2) ims = [x, x_hat] tils = [ "xin:" + str(x.shape[0]) + "x" + str(x.shape[1]), "xout:" + str(x.shape[1]) + "x" + str(x_hat.shape[1])] for n, ti in zip([0, 1], tils): f.add_subplot(gs[n]) plt.imshow(ims[n], cmap=cm.Greys_r) plt.title(ti) ax = f.gca() ax.set_axis_off() return f
def save_images(self, X, imgfile, density=False): ax = plt.axes() x = X[:, 0] y = X[:, 1] if density: xy = np.vstack([x,y]) z = scipy.stats.gaussian_kde(xy)(xy) ax.scatter(x, y, c=z, marker='o', edgecolor='') else: ax.scatter(x, y, marker='o', c=range(x.shape[0]), cmap=plt.cm.coolwarm) if self.collection is not None: self.collection.set_transform(ax.transData) ax.add_collection(self.collection) ax.text(x[0], y[0], str('start'), transform=ax.transAxes) ax.axis([-0.2, 1.2, -0.2, 1.2]) fig = plt.gcf() plt.savefig(imgfile) plt.close()
def plot_volcano(logFC,p_val,sample_name,saveName,logFC_thresh): fig=pl.figure() ## To plot and save pl.scatter(logFC[(p_val>0.05)|(abs(logFC)<logFC_thresh)],-np.log10(p_val[(p_val>0.05)|(abs(logFC)<logFC_thresh)]),color='blue',alpha=0.5); pl.scatter(logFC[(p_val<0.05)&(abs(logFC)>logFC_thresh)],-np.log10(p_val[(p_val<0.05)&(abs(logFC)>logFC_thresh)]),color='red'); pl.hlines(-np.log10(0.05),min(logFC),max(logFC)) pl.vlines(-logFC_thresh,min(-np.log10(p_val)),max(-np.log10(p_val))) pl.vlines(logFC_thresh,min(-np.log10(p_val)),max(-np.log10(p_val))) pl.xlim(-3,3) pl.xlabel('Log Fold Change') pl.ylabel('-log10(p-value)') pl.savefig(saveName) pl.close(fig) # def plot_histograms(df_peaks,pntr_list): # # for pntr in pntr_list: # colName =pntr[2]+'_Intragenic_position' # pl.hist(df_peaks[colName]) # pl.xlabel(colName) # pl.ylabel() # pl.show()
def plot_1d(dataset, nbins, data): with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=3) plt.figure(1) plt.hist(data, bins=np.arange(nbins+1), color='blue') plt.ylabel('Count', weight='bold', fontsize=24) xticks = list(plt.gca().get_xticks()) while (nbins-1) / float(xticks[-1]) < 1.1: xticks = xticks[:-1] while xticks[0] < 0: xticks = xticks[1:] xticks.append(nbins-1) xticks = list(sorted(xticks)) plt.gca().set_xticks(xticks) plt.xlim([int(np.ceil(-0.05*nbins)),int(np.ceil(nbins*1.05))]) plt.legend(loc='upper right') plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def plot_2d(dataset, nbins, data, extra=None): with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=2) rows, cols = nbins im = np.zeros(nbins) for i in xrange(rows): for j in xrange(cols): im[i,j] = ((data[:,0] == i) & (data[:,1] == j)).sum() plt.imshow(im, cmap='gray_r', interpolation='none') if extra is not None: dataset += extra plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def plot_1d(dataset, nbins): data = np.loadtxt('experiments/uci/data/splits/{0}_all.csv'.format(dataset), skiprows=1, delimiter=',')[:,-1] with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=3) plt.figure(1) plt.hist(data, bins=np.arange(nbins+1), color='blue') plt.ylabel('Count', weight='bold', fontsize=24) xticks = list(plt.gca().get_xticks()) while (nbins-1) / float(xticks[-1]) < 1.1: xticks = xticks[:-1] while xticks[0] < 0: xticks = xticks[1:] xticks.append(nbins-1) xticks = list(sorted(xticks)) plt.gca().set_xticks(xticks) plt.xlim([int(np.ceil(-0.05*nbins)),int(np.ceil(nbins*1.05))]) plt.legend(loc='upper right') plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def plot_2d(dataset, nbins, data=None, extra=None): if data is None: data = np.loadtxt('experiments/uci/data/splits/{0}_all.csv'.format(dataset), skiprows=1, delimiter=',')[:,-2:] with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=2) rows, cols = nbins im = np.zeros(nbins) for i in xrange(rows): for j in xrange(cols): im[i,j] = ((data[:,0] == i) & (data[:,1] == j)).sum() plt.imshow(im, cmap='gray_r', interpolation='none') if extra is not None: dataset += extra plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def plotValueFunction(self, valueFunction, prefix): '''3d plot of a value function.''' fig, ax = plt.subplots(subplot_kw = dict(projection = '3d')) X, Y = np.meshgrid(np.arange(self.numCols), np.arange(self.numRows)) Z = valueFunction.reshape(self.numRows, self.numCols) for i in xrange(len(X)): for j in xrange(len(X[i])/2): tmp = X[i][j] X[i][j] = X[i][len(X[i]) - j - 1] X[i][len(X[i]) - j - 1] = tmp my_col = cm.jet(np.random.rand(Z.shape[0],Z.shape[1])) ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = plt.get_cmap('jet')) plt.gca().view_init(elev=30, azim=30) plt.savefig(self.outputPath + prefix + 'value_function.png') plt.close()
def test_email_message(): subject = 'test subject' body = 'test body' recipient = ['recipient.email.address'] sender = 'sender.email.address' attachment = 'file.txt' f = open('file.txt','w') f.write('test attachment') f.close() msg = pecos.io._create_email_message(subject, body, recipient, sender, attachment) assert_true(subject in msg.as_string()) assert_true(body in msg.as_string()) assert_true(recipient[0] in msg.as_string()) assert_true(sender in msg.as_string()) assert_true(attachment in msg.as_string())
def test_plot_timeseries1(): filename = abspath(join(testdir, 'plot_timeseries1.png')) if isfile(filename): os.remove(filename) periods = 5 index = pd.date_range('1/1/2016', periods=periods, freq='H') data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]]) df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C']) plt.figure() pecos.graphics.plot_timeseries(df,yaxis_min=0, yaxis_max=20) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def test_plot_timeseries2(): filename = abspath(join(testdir, 'plot_timeseries2.png')) if isfile(filename): os.remove(filename) periods = 5 index = pd.date_range('1/1/2016', periods=periods, freq='H') data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]]) df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C']) tfilter = pd.Series(data = (df.index < index[3]), index = df.index) plt.figure() pecos.graphics.plot_timeseries(df,tfilter, yaxis_min=0, yaxis_max=20) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def test_plot_heatmap1(): filename = abspath(join(testdir, 'plot_heatmap1.png')) if isfile(filename): os.remove(filename) periods = 5 index = pd.date_range('1/1/2016', periods=periods, freq='D') data = np.random.rand(periods, 4) df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C', 'D']) plt.figure() pecos.graphics.plot_heatmap(df) plt.savefig(filename, format='png', bbox_inches='tight', pad_inches = 0) plt.close() assert_true(isfile(filename))
def test_plot_doy_heatmap1(): filename = abspath(join(testdir, 'plot_doy_heatmap1.png')) if isfile(filename): os.remove(filename) periods = 5*24 # 5 days index = pd.date_range('3/1/2016', periods=periods, freq='H') data = np.random.rand(periods) df = pd.DataFrame(data=data, index=index, columns=['A']) plt.figure() pecos.graphics.plot_doy_heatmap(df['A']) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def plot_classes(y, cord, names, test_error, message=""): plt.close("all") cord = np.array(cord) colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k') un = np.unique(y) fig, ax = plt.subplots() for u, col in zip(un, colors): ind = np.argwhere(y == u) x = cord[ind, :] x = x.reshape(x.shape[0], cord.shape[1]) ax.scatter(x[:, 0], x[:, 1], label="class:" + str(u), color=col) plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8}) fig.suptitle( "Output prediction. Test error:" + str(test_error*100) + "%. " + message, fontsize=8) return fig
def plot_penalty_vl(debug, tag, fold_exp): plt.close("all") vl = np.array(debug["penalty"]) fig = plt.figure(figsize=(15, 10.8), dpi=300) names = debug["names"] for i in range(vl.shape[1]): if vl.shape[1] > 1: plt.plot(vl[:, i], label="layer_"+str(names[i])) else: plt.plot(vl[:], label="layer_"+str(names[i])) plt.xlabel("mini-batchs") plt.ylabel("value of penlaty") plt.title( "Penalty value over layers:" + "_".join([str(k) for k in names]) + ". tag:" + tag) plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8}) plt.grid(True) fig.savefig(fold_exp+"/penalty.png", bbox_inches='tight') plt.close('all') del fig
def test_plot_fragility_curve1(): from scipy.stats import lognorm filename = abspath(join(testdir, 'plot_fragility_curve1.png')) if isfile(filename): os.remove(filename) FC = wntr.scenario.FragilityCurve() FC.add_state('Minor', 1, {'Default': lognorm(0.5,scale=0.3)}) FC.add_state('Major', 2, {'Default': lognorm(0.5,scale=0.7)}) plt.figure() wntr.graphics.plot_fragility_curve(FC) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def plot_true_and_augmented_data(sample,noised_sample,label,n_examples): output_dir = os.path.split(FLAGS.output)[0] # Save augmented data plt.clf() fig, ax = plt.subplots(3,1) for t in range(noised_sample.shape[1]): ax[t].plot(noised_sample[:,t]) ax[t].set_xlabel('time (samples)') ax[t].set_ylabel('amplitude') ax[0].set_title('window {:03d}, cluster_id: {}'.format(n_examples,label)) plt.savefig(os.path.join(output_dir, "augmented_data", 'augmented_{:03d}.pdf'.format(n_examples))) plt.close() # Save true data plt.clf() fig, ax = plt.subplots(3,1) for t in range(sample.shape[1]): ax[t].plot(sample[:,t]) ax[t].set_xlabel('time (samples)') ax[t].set_ylabel('amplitude') ax[0].set_title('window {:03d}, cluster_id: {}'.format(n_examples,label)) plt.savefig(os.path.join(output_dir, "true_data", 'true__{:03d}.pdf'.format(n_examples))) plt.close()
def create_simpleDS9region(outputfile,ralist,declist,color='red',circlesize=0.5,textlist=None,clobber=False): """ Generate a basic DS9 region file with circles around a list of coordinates --- INPUT --- outputfile Path and name of file to store reigion file to ralist List of R.A. to position circles at declist List of Dec. to position circles at color Color of circles size Size of circles (radius in arcsec) text Text string for each circle clobber Overwrite existing files? """ if not clobber: if os.path.isfile(outputfile): sys.exit('File already exists and clobber = False --> ABORTING') fout = open(outputfile,'w') fout.write("# Region file format: DS9 version 4.1 \nfk5\n") for rr, ra in enumerate(ralist): string = 'circle('+str(ra)+','+str(declist[rr])+','+str(circlesize)+'") # color='+color+' width=3 ' if textlist is not None: string = string+' font="times 10 bold roman" text={'+textlist[rr]+'}' fout.write(string+' \n') fout.close() # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def close(self): """ Close the plot. """ if self._figure: pylab.close(self._figure) self._figure = None self._plot = None self._canvas = None
def releaseObject(self): """ Releases the plot and cleans up resources. """ self.stop() super(PlotBase,self).releaseObject() self.close()
def save(self, out_path): '''Saves a figure for the monitor Args: out_path: str ''' plt.clf() np.set_printoptions(precision=4) font = { 'size': 7 } matplotlib.rc('font', **font) y = 2 x = ((len(self.d) - 1) // y) + 1 fig, axes = plt.subplots(y, x) fig.set_size_inches(20, 8) for j, (k, v) in enumerate(self.d.iteritems()): ax = axes[j // x, j % x] ax.plot(v, label=k) if k in self.d_valid.keys(): ax.plot(self.d_valid[k], label=k + '(valid)') ax.set_title(k) ax.legend() plt.tight_layout() plt.savefig(out_path, facecolor=(1, 1, 1)) plt.close()
def plot_multi_format(plot_funcs, plot_kwargs=None, usetex=False, outdir='plots', setting_funcs=['single', 'span', 'slides', 'thumbnails']): """ Outputs plots formatted 4 ways: Publication ready (narrow and wide), PowerPoint ready, and png thumbnails. input ----- plot_funcs : List of functions that return a mpl figure and a filename (or list of figures and filenames) """ setting_dict = {'single': mpl_single_column, 'span': mpl_span_columns, 'slides': mpl_slides, 'thumbnails': mpl_thumbnails} if not os.path.exists(outdir): os.makedirs(outdir) # For python 3.4 # os.makedirs(outdir, exist_ok=True) if plot_kwargs is None: plot_kwargs=[{}]*len(plot_funcs) for key in setting_funcs: setting_dict[key](usetex=usetex) for plot_func,pkwargs in zip(plot_funcs,plot_kwargs): figs, names = plot_func(**pkwargs) for fig,name in zip(figs,names): fig.savefig(os.path.join(outdir, key+'_'+name)) plt.close('all')
def plot_graphs(df, trending_daily, day_from, day_to, limit, country_code, folder_out=None): days = pd.DatetimeIndex(start=day_from, end=day_to, freq='D') for day in days: fig = plt.figure() ax = fig.add_subplot(111) plt.rc('lines', linewidth=2) data = trending_daily.get_group(str(day.date())) places, clusters = top_trending(data, limit) for cluster in clusters: places.add(max_from_cluster(cluster, data)) ax.set_prop_cycle(plt.cycler('color', ['r', 'b', 'yellow'] + [plt.cm.Accent(i) for i in np.linspace(0, 1, limit-3)] ) + plt.cycler('linestyle', ['-', '-', '-', '-', '-', '--', '--', '--', '--', '--'])) frame = export(places, clusters, data) frame.sort_values('trending_rank', ascending=False, inplace=True) for i in range(len(frame)): item = frame.index[i] lat, lon, country = item result_items = ReverseGeoCode().get_address_attributes(lat, lon, 10, 'city', 'country_code') if 'city' not in result_items.keys(): mark = "%s (%s)" % (manipulate_display_name(result_items['display_name']), result_items['country_code'].upper() if 'country_code' in result_items.keys() else country) else: if check_eng(result_items['city']): mark = "%s (%s)" % (result_items['city'], result_items['country_code'].upper()) else: mark = "%.2f %.2f (%s)" % (lat, lon, result_items['country_code'].upper()) gp = df.loc[item].plot(ax=ax, x='date', y='count', label=mark) ax.tick_params(axis='both', which='major', labelsize=10) ax.set_yscale("log", nonposy='clip') plt.xlabel('Date', fontsize='small', verticalalignment='baseline', horizontalalignment='right') plt.ylabel('Total number of views (log)', fontsize='small', verticalalignment='center', horizontalalignment='center', labelpad=6) gp.legend(loc='best', fontsize='xx-small', ncol=2) gp.set_title('Top 10 OSM trending places on ' + str(day.date()), {'fontsize': 'large', 'verticalalignment': 'bottom'}) plt.tight_layout() db = TrendingDb() db.update_table_img(plt, str(day.date()), region=country_code) plt.close()
def plotBasisFunctions(self, eigenvalues, eigenvectors): '''3d plot of the basis function. Right now I am plotting eigenvectors, so each coordinate of the eigenvector correspond to the value to be plotted for the correspondent state.''' for i in xrange(len(eigenvalues)): fig, ax = plt.subplots(subplot_kw = dict(projection = '3d')) X, Y = np.meshgrid(np.arange(self.numRows), np.arange(self.numCols)) Z = eigenvectors[:,i].reshape(self.numCols, self.numRows) for ii in xrange(len(X)): for j in xrange(len(X[ii])/2): tmp = X[ii][j] X[ii][j] = X[ii][len(X[ii]) - j - 1] X[ii][len(X[ii]) - j - 1] = tmp my_col = cm.jet(np.random.rand(Z.shape[0],Z.shape[1])) ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = plt.get_cmap('jet')) plt.gca().view_init(elev=30, azim=30) plt.savefig(self.outputPath + str(i) + '_eig' + '.png') plt.close() plt.plot(eigenvalues, 'o') plt.savefig(self.outputPath + 'eigenvalues.png')
def test_write_dashboard2(): # with text, graphics (encoded and linked), tables, and links filename1 = abspath(join(testdir, 'test_write_dashboard2_linked_graphics.html.html')) filename2 = abspath(join(testdir, 'test_write_dashboard2_encoded_graphics.html.html')) graphics_filename = abspath(join(testdir, 'dashboard_graphic.png')) if isfile(filename1): os.remove(filename1) if isfile(filename2): os.remove(filename2) if isfile(graphics_filename): os.remove(graphics_filename) plt.figure() plt.plot([1, 2, 3],[1, 2, 3]) plt.savefig(graphics_filename, format='png') plt.close() column_names = ['loc1', 'loc2'] row_names = ['sys1', 'sys2'] content = {} content[('sys1', 'loc1')] = {'text': 'sys1-loc1 text', 'graphics': [graphics_filename], 'link': {'Google': 'https://www.google.com', 'Pecos': 'http://pecos.readthedocs.io'} } content[('sys1', 'loc2')] = {'text': 'sys1-loc2 text', 'table': pd.DataFrame({'sys1': [1,2,3]}).to_html()} content[('sys2', 'loc1')] = {'text': 'sys2-loc1 text', 'graphics': [graphics_filename], 'link': {'Google': 'https://www.google.com', 'Pecos': 'http://pecos.readthedocs.io'} } content[('sys2', 'loc2')] = {'text': 'sys2-loc2 text', 'table': pd.DataFrame({'sys2': [2,4,6]}).to_html()} pecos.io.write_dashboard(filename1, column_names, row_names, content, encode=False) assert_true(isfile(filename1)) pecos.io.write_dashboard(filename2, column_names, row_names, content, encode=True) assert_true(isfile(filename2))
def test_plot_scatter1(): filename = abspath(join(testdir, 'plot_scatter1.png')) if isfile(filename): os.remove(filename) x = pd.DataFrame({'x1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])}) y = pd.DataFrame({'y1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])}) plt.figure() pecos.graphics.plot_scatter(x,y,xaxis_min=0.5, xaxis_max=6.5, yaxis_min=0.5, yaxis_max=3.5) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def test_plot_scatter2(): filename = abspath(join(testdir, 'plot_scatter2.png')) if isfile(filename): os.remove(filename) x = pd.DataFrame({'x1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'x2' : pd.Series([4., 5., 6.], index=['a', 'b', 'c'])}) y = pd.DataFrame({'y1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])}) plt.figure() pecos.graphics.plot_scatter(x,y,xaxis_min=0.5, xaxis_max=6.5, yaxis_min=0.5, yaxis_max=3.5) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def test_plot_heatmap2(): filename = abspath(join(testdir, 'plot_heatmap2.png')) if isfile(filename): os.remove(filename) data = np.array([[1,2],[3,4]]) plt.figure() pecos.graphics.plot_heatmap(data, cmap='jet', show_axis=True) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def animate(y, ndim, cmap) : plt.ion() if ndim == 5: plt.figure() plt.show() for i in range(y.shape[1]) : print "Showing batch", i plt.close('all') for j in range(y.shape[0]) : plt.imshow(y[j,i], interpolation='none', cmap=cmap) plt.pause(0.1) time.sleep(1) else: for i in range(y.shape[1]) : print "Showing batch", i plt.close('all') for j in range(y.shape[0]) : plt.figure(0) plt.imshow(y[j,i], interpolation='none', cmap=cmap) plt.figure(1) plt.imshow(x[j,i], interpolation='none', cmap=cmap) plt.pause(0.2) time.sleep(1)
def plot_debug_grad(debug, tag, fold_exp, trg): plt.close("all") # f = plt.figure(figsize=(15, 10.8), dpi=300) nbr_rows = int(len(debug["grad_sup"][0])/2) f, axs = plt.subplots(nbr_rows, 2, sharex=True, sharey=False, figsize=(15, 12.8), dpi=300) if trg == "sup": grad = np.array(debug["grad_sup"]) elif trg == "hint": grad = np.array(debug["grad_hint"]) print grad.shape, trg j = 0 for i in range(0, nbr_rows*2, 2): w_vl = grad[:, i] b_vl = grad[:, i+1] axs[j, 0].plot(w_vl, label=trg) axs[j, 0].set_title("w"+str(j)) axs[j, 1].plot(b_vl, label=trg) axs[j, 1].set_title("b"+str(j)) axs[j, 0].grid(True) axs[j, 1].grid(True) j += 1 f.suptitle("Grad sup/hint:" + tag, fontsize=8) plt.legend() f.savefig(fold_exp+"/grad_" + trg + ".png", bbox_inches='tight') plt.close("all") del f
def plot_debug_ratio_grad(debug, fold_exp, r="h/s"): plt.close("all") # f = plt.figure(figsize=(15, 10.8), dpi=300) nbr_rows = int(len(debug["grad_sup"][0])/2) f, axs = plt.subplots(nbr_rows, 2, sharex=True, sharey=False, figsize=(15, 12.8), dpi=300) grads = np.array(debug["grad_sup"]) gradh = np.array(debug["grad_hint"]) if gradh.size != grads.size: print "Can't calculate the ratio. It looks like you divided the " +\ "hint batch..." return 0 print gradh.shape, grads.shape j = 0 for i in range(0, nbr_rows*2, 2): w_vls = grads[:, i] b_vls = grads[:, i+1] w_vl_h = gradh[:, i] b_vlh = gradh[:, i+1] if r == "h/s": ratio_w = np.divide(w_vl_h, w_vls) ratio_b = np.divide(b_vlh, b_vls) elif r == "s/h": ratio_w = np.divide(w_vls, w_vl_h) ratio_b = np.divide(b_vls, b_vlh) else: raise ValueError("Either h/s or s/h.") axs[j, 0].plot(ratio_w, label=r) axs[j, 0].set_title("w"+str(j)) axs[j, 1].plot(ratio_b, label=r) axs[j, 1].set_title("b"+str(j)) axs[j, 0].grid(True) axs[j, 1].grid(True) j += 1 f.suptitle("Ratio gradient: " + r, fontsize=8) plt.legend() f.savefig(fold_exp+"/ratio_grad_" + r.replace("/", "-") + ".png", bbox_inches='tight') plt.close("all") del f
def plot_representations(X, y, title): """Plot distributions and thier labels.""" x_min, x_max = np.min(X, 0), np.max(X, 0) X = (X - x_min) / (x_max - x_min) f = plt.figure(figsize=(15, 10.8), dpi=300) # ax = plt.subplot(111) for i in range(X.shape[0]): plt.text(X[i, 0], X[i, 1], str(y[i]), color=plt.cm.Set1(y[i] / 10.), fontdict={'weight': 'bold', 'size': 9}) # if hasattr(offsetbox, 'AnnotationBbox'): # # only print thumbnails with matplotlib > 1.0 # shown_images = np.array([[1., 1.]]) # just something big # for i in range(digits.data.shape[0]): # dist = np.sum((X[i] - shown_images) ** 2, 1) # if np.min(dist) < 4e-3: # # don't show points that are too close # continue # shown_images = np.r_[shown_images, [X[i]]] # imagebox = offsetbox.AnnotationBbox( # offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r), # X[i]) # ax.add_artist(imagebox) plt.xticks([]), plt.yticks([]) if title is not None: plt.title(title) return f
def test_plot_network1(): filename = abspath(join(testdir, 'plot_network1.png')) if isfile(filename): os.remove(filename) inp_file = join(ex_datadir,'Net6.inp') wn = wntr.network.WaterNetworkModel(inp_file) plt.figure() wntr.graphics.plot_network(wn) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def test_plot_tank_curve1(): filename = abspath(join(testdir, 'plot_pump_curve1.png')) if isfile(filename): os.remove(filename) inp_file = join(ex_datadir,'Net3.inp') wn = wntr.network.WaterNetworkModel(inp_file) pump = wn.get_link('10') plt.figure() wntr.graphics.plot_pump_curve(pump) plt.savefig(filename, format='png') plt.close() assert_true(isfile(filename))
def generate_setup_template_modify(outputfile='./tdose_setup_template_modify.txt',clobber=False,verbose=True): """ Generate setup text file template for modifying data cubes --- INPUT --- outputfile The name of the output which will contain the TDOSE setup template clobber Overwrite files if they exist verbose Toggle verbosity --- EXAMPLE OF USE --- import tdose_utilities as tu filename = './tdose_setup_template_modify_new.txt' tu.generate_setup_template_modify(outputfile=filename,clobber=True) setup = tu.load_setup(setupfile=filename) """ if verbose: print ' --- tdose_utilities.generate_setup_template_modify() --- ' #------------------------------------------------------------------------------------------------------ if os.path.isfile(outputfile) & (clobber == False): sys.exit(' ---> Outputfile already exists and clobber=False ') else: if verbose: print ' - Will store setup template in '+outputfile if os.path.isfile(outputfile) & (clobber == True): if verbose: print ' - Output already exists but clobber=True so overwriting it ' setuptemplate = """ #---------------------------------------------START OF TDOSE MODIFY SETUP--------------------------------------------- # # Template for TDOSE (http://github.com/kasperschmidt/TDOSE) setup file for modifyinf data cubes # Generated with tdose_utilities.generate_setup_template_modify() on %s # Cube modifications are run independent of tdose.perform_extraction() with tdose.modify_cube() # # - - - - - - - - - - - - - - - - - - - - - - - - - MODIFYING CUBE - - - - - - - - - - - - - - - - - - - - - - - - - - data_cube /path/datacube.fits # Path and name of fits file containing data cube to modify cube_extension DATA_DCBGC # Name or number of fits extension containing data cube source_model_cube /path/tdose_source_modelcube.fits # Path and name of fits file containing source model cube source_extension DATA_DCBGC # Name or number of fits extension containing source model cube modyified_cube tdose_modified_datacube # Name extension of file containing modified data cube. modify_sources_list [1,2,5] # List of IDs of sources to remove from data cube using source model cube. # For long list of IDs provide path and name of file containing IDs (only) sources_action remove # Indicate how to modify the data cube. Chose between: # 'remove' Sources in modify_sources_list are removed from data cube # 'keep' All sources except the sources in modify_sources_list are removed from data cube #----------------------------------------------END OF TDOSE MODIFY SETUP---------------------------------------------- """ % (tu.get_now_string()) fout = open(outputfile,'w') fout.write(setuptemplate) fout.close() # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def galfit_model_ds9region(models,regionfileextension='ds9region',regcolor='red',clobber=False,verbose=True): """ Generate DS9 region file to indicate GALFIT components --- INPUT --- model List of GALFIT models to generate region files for regionfileextension Extension for naming the DS9 region file regcolor Color of regions to draw clobber Overwrite existing file? verbose Toggle verbosity --- EXAMPLE OF USE --- models = glob.glob('/path/to/models/model*.fits') tu.galfit_model_ds9region(models,clobber=False) """ Nmodels = len(models) if verbose: print ' - Generating DS9 region files for '+str(Nmodels)+' GALFIT models provided ' for model in models: modelhdr = pyfits.open(model)[2].header comkeys = [] regionfile = model.replace('.fits','_'+regionfileextension+'.reg') for key in modelhdr.keys(): if 'COMP_' in key: comkeys.append(key) if os.path.isfile(regionfile): if not clobber: sys.exit(' ---> File already exists and clobber = False') fout = open(regionfile,'w') fout.write("# Region file format: DS9 version 4.1 \nimage\n") for comp in comkeys: compNo = comp.split('OMP_')[-1] if not modelhdr[comp] == 'sky': XC, XCERR = tu.galfit_getheadervalue(compNo,'XC',modelhdr) YC, YCERR = tu.galfit_getheadervalue(compNo,'YC',modelhdr) regstr = '# text(%s,%s) color=%s font="times 20 bold roman" text={%s} \n' % (XC,YC,regcolor,compNo) fout.write(regstr) fout.close() if verbose: print ' - Saved region file to \n '+regionfile # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def plot_fig(values, title, x_str, y_str, path, best_iter, std_vals=None): """Plot some values. Input: values: list or numpy.ndarray of values to plot (y) title: string; the title of the plot. x_str: string; the name of the x axis. y_str: string; the name of the y axis. path: string; path where to save the figure. best_iter: integer. The epoch of the best iteration. std_val: List or numpy.ndarray of standad deviation values that corresponds to each value in 'values'. """ floating = 6 prec = "%." + str(floating) + "f" if best_iter >= 0: if isinstance(values, list): if best_iter >= len(values): best_iter = -1 if isinstance(values, np.ndarray): if best_iter >= np.size: best_iter = -1 v = str(prec % np.float(values[best_iter])) else: v = str(prec % np.float(values[-1])) best_iter = -1 if best_iter == -1: best_iter = len(values) fig = plt.figure() plt.plot( values, label="lower val: " + v + " at " + str(best_iter) + " " + x_str) plt.xlabel(x_str) plt.ylabel(y_str) plt.title(title, fontsize=8) plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8}) plt.grid(True) fig.savefig(path, bbox_inches='tight') plt.close('all') del fig
def test_abu_chart(self): from nugridpy import utils,ppn,data_plot import matplotlib matplotlib.use('agg') import matplotlib.pylab as mpy import os # Perform tests within temporary directory with TemporaryDirectory() as tdir: # wget the data for a ppn run from the CADC VOspace n = 3 for cycle in range(0,n): cycle_str = str(cycle).zfill(2) os.system("wget -q --content-disposition --directory '" + tdir + "' " + "'http://www.canfar.phys.uvic.ca/vospace/synctrans?TARGET="\ + "vos%3A%2F%2Fcadc.nrc.ca%21vospace%2Fnugrid%2Fdata%2Fprojects%2Fppn%2Fexamples%2F"\ + "ppn_Hburn_simple%2Fiso_massf000" + cycle_str + ".DAT&DIRECTION=pullFromVoSpace&PROTOCOL"\ + "=ivo%3A%2F%2Fivoa.net%2Fvospace%2Fcore%23httpget'") # test_data_dir should point to the correct location of a set of abundances data file #nugrid_dir= os.path.dirname(os.path.dirname(ppn.__file__)) #NuPPN_dir= nugrid_dir + "/NuPPN" #test_data_dir= NuPPN_dir + "/examples/ppn_C13_pocket/master_results" p=ppn.abu_vector(tdir) # TODO: this function fails to raise an exception if path is not found! mp=p.get('mod') if len(mp) == 0: raise IOError("Cannot locate a set of abundance data files") sparse=10 cycles=mp[:1000:sparse] form_str='%6.1F' form_str1='%4.3F' i=0 for cyc in cycles: T9 = p.get('t9',fname=cyc) Rho = p.get('rho',fname=cyc) mod = p.get('mod',fname=cyc) # time= p.get('agej',fname=cyc)*utils.constants.one_year time= p.get('agej',fname=cyc) mpy.close(i);mpy.figure(i);i += 1 p.abu_chart(cyc,mass_range=[0,41],plotaxis=[-1,22,-1,22],lbound=(-6,0),show=False) mpy.title(str(mod)+' t='+form_str%time+'yr $T_9$='+form_str1%T9+' $\\rho$='+str(Rho)) png_file='abu_chart_'+str(cyc).zfill(len(str(max(mp))))+'.png' mpy.savefig(png_file) self.assertTrue(os.path.exists(png_file)) os.remove(png_file)
def plot_profiles_to_file(annot, pntr, ups=200, smooth_param=50): pp = PdfPages(options.save_path + 'Figures/individual_signals.pdf') clrs_ = ['red', 'blue', 'black', 'orange', 'magenta', 'cyan'] vec_sense = {} vec_antisense = {} # for qq in tq(range(annot.shape[0])): for qq in tq(range(100)): chname = annot['chr'].iloc[qq] if annot['strand'].iloc[qq] == '+': start = annot['start'].iloc[qq] - ups stop = annot['end'].iloc[qq] for key in pntr.keys(): vec_sense[key] = pntr[key][0].get_nparray(chname, start, stop - 1) vec_antisense[key] = pntr[key][1].get_nparray(chname, start, stop - 1) xran = np.arange(start, stop) else: start = annot['start'].iloc[qq] stop = annot['end'].iloc[qq] + ups for key in pntr.keys(): vec_sense[key] = np.flipud(pntr[key][1].get_nparray(chname, start, stop)) vec_antisense[key] = np.flipud(pntr[key][0].get_nparray(chname, start, stop)) xran = np.arange(stop, start, -1) ax = {} fig = pl.figure() pl.title(annot['name'].iloc[qq]) for i, key in enumerate(pntr.keys()): sm_vec_se = sm.smooth(vec_sense[key], smooth_param)[(smooth_param - 1):-(smooth_param - 1)] sm_vec_as = sm.smooth(vec_antisense[key], smooth_param)[(smooth_param - 1):-(smooth_param - 1)] ax[key] = pl.subplot(len(pntr), 1, i+1) ax[key].plot(xran, vec_sense[key], label=key, color=clrs_[i], alpha=0.5) ax[key].plot(xran, -vec_antisense[key], color=clrs_[i], alpha=0.5) ax[key].plot(xran, sm_vec_se, color=clrs_[i], linewidth=2) ax[key].plot(xran, -sm_vec_as, color=clrs_[i], linewidth=2) ax[key].legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), fontsize=6, ncol=1) pp.savefig() pl.close() pp.close() for pn in pntr.values(): pn[0].close() pn[1].close()
def plot_2d(params_dir): model_dirs = [name for name in os.listdir(params_dir) if os.path.isdir(os.path.join(params_dir, name))] if len(model_dirs) == 0: model_dirs = [params_dir] colors = plt.get_cmap('plasma') plt.figure(figsize=(20, 10)) ax = plt.subplot(111) ax.set_xlabel('Learning Rate') ax.set_ylabel('Error rate') i = 0 for model_dir in model_dirs: model_df = pd.DataFrame() for param_path in glob.glob(os.path.join(params_dir, model_dir) + '/*.h5'): param = dd.io.load(param_path) gd = {'learning rate': param['hyperparameters']['learning_rate'], 'momentum': param['hyperparameters']['momentum'], 'dropout': param['hyperparameters']['dropout'], 'val. objective': param['best_epoch']['validate_objective']} model_df = model_df.append(pd.DataFrame(gd, index=[0]), ignore_index=True) if i != len(model_dirs) - 1: ax.scatter(model_df['learning rate'], model_df['val. objective'], s=128, marker=(i+3, 0), edgecolor='black', linewidth=model_df['dropout'], label=model_dir, c=model_df['momentum'], cmap=colors) else: im = ax.scatter(model_df['learning rate'], model_df['val. objective'], s=128, marker=(i+3, 0), edgecolor='black', linewidth=model_df['dropout'], label=model_dir, c=model_df['momentum'], cmap=colors) i += 1 plt.colorbar(im, label='Momentum') plt.legend() plt.show() plt.savefig('{}.eps'.format(os.path.join(IMAGES_DIRECTORY, 'params2d')), format='eps', dpi=1000) plt.close()
def plotPolicy(self, policy, prefix): plt.clf() for idx in xrange(len(policy)): i, j = self.env.getStateXY(idx) dx = 0 dy = 0 if policy[idx] == 0: # up dy = 0.35 elif policy[idx] == 1: #right dx = 0.35 elif policy[idx] == 2: #down dy = -0.35 elif policy[idx] == 3: #left dx = -0.35 elif self.matrixMDP[i][j] != -1 and policy[idx] == 4: # termination circle = plt.Circle( (j + 0.5, self.numRows - i + 0.5 - 1), 0.025, color='k') plt.gca().add_artist(circle) if self.matrixMDP[i][j] != -1: plt.arrow(j + 0.5, self.numRows - i + 0.5 - 1, dx, dy, head_width=0.05, head_length=0.05, fc='k', ec='k') else: plt.gca().add_patch( patches.Rectangle( (j, self.numRows - i - 1), # (x,y) 1.0, # width 1.0, # height facecolor = "gray" ) ) plt.xlim([0, self.numCols]) plt.ylim([0, self.numRows]) for i in xrange(self.numCols): plt.axvline(i, color='k', linestyle=':') plt.axvline(self.numCols, color='k', linestyle=':') for j in xrange(self.numRows): plt.axhline(j, color='k', linestyle=':') plt.axhline(self.numRows, color='k', linestyle=':') plt.savefig(self.outputPath + prefix + 'policy.png') plt.close()
def test_write_monitoring_report2():# with test results and graphics (encoded and linked) filename1 = abspath(join(testdir, 'test_write_monitoring_report2_linked_graphics.html')) filename2 = abspath(join(testdir, 'test_write_monitoring_report2_encoded_graphics.html')) graphics_filename = abspath(join(testdir, 'custom_graphic.png')) if isfile(filename1): os.remove(filename1) if isfile(filename2): os.remove(filename2) if isfile(graphics_filename): os.remove(graphics_filename) pecos.logger.initialize() logger = logging.getLogger('pecos') pm = pecos.monitoring.PerformanceMonitoring() periods = 5 index = pd.date_range('1/1/2016', periods=periods, freq='H') data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]]) df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C']) tfilter = pd.Series(data = (df.index < index[3]), index = df.index) pm.add_dataframe(df, 'test', True) pm.add_time_filter(tfilter) pm.check_range([0,7]) # 2 test failures filename_root = abspath(join(testdir, 'monitoring_report_graphic')) test_results_graphics = pecos.graphics.plot_test_results(filename_root, pm) plt.figure() plt.plot([1, 2, 3],[1, 2, 3]) plt.savefig(graphics_filename, format='png') plt.close() custom_graphics = [graphics_filename] logger.warning('Add a note') pecos.io.write_monitoring_report(filename1, pm, test_results_graphics, custom_graphics, encode=False) assert_true(isfile(filename1)) pecos.io.write_monitoring_report(filename2, pm, test_results_graphics, custom_graphics, encode=True) assert_true(isfile(filename2))
def gaussian_twoD_testing(): """ Implement and check the estimator for a 2D gaussian fit. """ data = np.empty((121,1)) amplitude=np.random.normal(3e5,1e5) center_x=91+np.random.normal(0,0.8) center_y=14+np.random.normal(0,0.8) sigma_x=np.random.normal(0.7,0.2) sigma_y=np.random.normal(0.7,0.2) offset=0 x = np.linspace(90,92,11) y = np.linspace(13,15,12) xx, yy = np.meshgrid(x, y) axes=(xx.flatten(), yy.flatten()) theta_here=10./360.*(2*np.pi) # data=qudi_fitting.twoD_gaussian_function((xx,yy),*(amplitude,center_x,center_y,sigma_x,sigma_y,theta_here,offset)) gmod,params = qudi_fitting.make_twoDgaussian_model() data = gmod.eval(x=axes, amplitude=amplitude, center_x=center_x, center_y=center_y, sigma_x=sigma_x, sigma_y=sigma_y, theta=theta_here, offset=offset) data += 50000*np.random.random_sample(np.shape(data)) gmod, params = qudi_fitting.make_twoDgaussian_model() para=Parameters() # para.add('theta',vary=False) # para.add('center_x',expr='0.5*center_y') # para.add('sigma_x',min=0.2*((92.-90.)/11.), max= 10*(x[-1]-y[0]) ) # para.add('sigma_y',min=0.2*((15.-13.)/12.), max= 10*(y[-1]-y[0])) # para.add('center_x',value=40,min=50,max=100) result = qudi_fitting.make_twoDgaussian_fit(xy_axes=axes, data=data)#,add_parameters=para) # # FIXME: What does "Tolerance seems to be too small." mean in message? # print(result.message) plt.close('all') fig, ax = plt.subplots(1, 1) ax.hold(True) ax.imshow(result.data.reshape(len(y),len(x)), cmap=plt.cm.jet, origin='bottom', extent=(x.min(), x.max(), y.min(), y.max()),interpolation="nearest") ax.contour(x, y, result.best_fit.reshape(len(y),len(x)), 8 , colors='w') plt.show() # plt.close('all') print(result.fit_report()) # print('Message:',result.message)