def plotStrainHeatmap(self, alldata): img = np.zeros((len(alldata[0, :, 0]), len(alldata[0, 0, :]), 4), dtype=np.uint8) # for i in range(len(alldata[:, 0, 0])/2+1): # rawdata = alldata[i, :, :] # print i, np.max(rawdata) # img[:, :, 3] = 255 # -20*i # img[:, :, 1] = (255-i*10)*rawdata/np.max(rawdata) # img[:, :, 2] = 0 # 255*rawdata/np.max(rawdata) # img[:, :, 0] = 0 # 255*rawdata/np.max(rawdata) # # for i in range(len(alldata[:, 0, 0])/2): # print i+len(alldata[:, 0, 0])/2+1, np.max(rawdata) # rawdata = alldata[i+len(alldata[:, 0, 0])/2+1, :, :] # img[:, :, 3] = 255 # -20*i # img[:, :, 1] = 0 # img[:, :, 2] = (180+i*10)*rawdata/np.max(rawdata) # img[:, :, 0] = (180+i*10)*rawdata/np.max(rawdata) img = alldata[10, :, :] print np.mean(img), np.max(img) print np.mean(alldata[2, :, :]), np.max(alldata[2, :, :]) linestart = [999, 0] linestop = [0, 999] z, length = self.getProjection(img, linestart[0], linestart[1], linestop[0], linestop[1]) fig, ax = plt.subplots() fig2, ax2 = plt.subplots() ax2.plot(z) ax.imshow(img)
def plotVolume(self): full = self.revolution(1000) volu = self.volume(full) import matplotlib as mpl #mpl.use('Qt4Agg') from matplotlib.pyplot import plot, show import matplotlib.pylab as plt if self.DEBUG: fig, axs = plt.subplots(2, 1, sharex=True) rev = self.revolution(2000) pos = self._position(rev) axs[0].plot(rev*180/pi,pos*100) axs[0].plot(self.TDC()*180/pi,self._position(self.TDC())*100,'o') iMin = np.where(pos==pos.min()) axs[0].plot(rev[iMin]*180/pi,self._position(rev[iMin])*100,'o') iMax = np.where(pos==pos.max()) axs[0].plot(rev[iMax]*180/pi,self._position(rev[iMax])*100,'o') axs[0].set_ylabel(r'Piston position (cm)') ax = axs[1] self.autolog("Position: ", str(pos.min()), str(pos.max()), str(self.stroke())) self.autolog("Volume: ",str(volu.min()), str(volu.max())) else: fig, ax = plt.subplots(1, 1) ax.plot(full*180/pi,volu*1e6) ax.plot(self.TDC()*180/pi,self.volume(self.TDC())*1e6,'o') iMin = np.where(volu==volu.min()) ax.plot(full[iMin]*180/pi,self.volume(full[iMin])*1e6,'o') iMax = np.where(volu==volu.max()) ax.plot(full[iMax]*180/pi,self.volume(full[iMax])*1e6,'o') ax.set_xlabel(r'Crankshaft angle $\theta$ (deg)') ax.set_ylabel(r'Cylinder volume $V$ (cm$^3$)') show()
def plot_data_objs(data_to_plot_list, figure_name, same=False): rows = 2 cols = 2 topo = figure_name.split(':')[0] # print "topo:",topo # print "figname:",figure_name if same: fig_id = figure_name.split(':')[-1] fig, ax = plt.subplots(rows, cols, num=fig_id) else: fig, ax = plt.subplots(rows, cols) for i in range(rows): for j in range(cols): if len(data_to_plot_list) > i*rows+j: obj = data_to_plot_list[i*rows+j] if obj.scale == 'log' and max(obj.data) <= 0: ydata = map(lambda x: -x, obj.data) else: ydata = obj.data ax[i][j].plot(range(len(obj.data)), ydata, label=topo, linewidth=2.0) ax[i][j].set_yscale(obj.scale) ax[i][j].set_title(obj.label) if same: ax[i][j].legend(loc='lower left', shadow=True) ax[i][j].legend().set_visible(False) # fig.legend(loc='lower left', shadow=True) if fig._suptitle is None: suptitle = ':'.join(figure_name.split(':')[1:]) fig.suptitle(suptitle, fontsize=18)
def plotStrain(self, strainpic, imgarray, label): # import matplotlib.ticker as ticker # sns.set_context("talk") print "strainpic dimensions: " + str(np.shape(strainpic)) # gradient = self.adjustGradient() figstrain, axstrain = plt.subplots(2, 1) strainpic_adjusted = strainpic - np.mean(strainpic) strainpic_adjusted[strainpic_adjusted > 0.00004] = 0.00004 strainpic_adjusted[strainpic_adjusted < -0.00004] = -0.00004 im = axstrain[0].imshow(strainpic_adjusted, cmap="BrBG") # im = axstrain[0].imshow(strainpic+gradient, cmap="BrBG") im2 = axstrain[1].imshow(imgarray[len(imgarray[:, 0, 0])/2-4, :, :], cmap="Greens") axstrain[1].set_title("%g %g %g %g" % (self.roi[0], self.roi[1], self.roi[2], self.roi[3])) axstrain[0].set_title(r'$\epsilon_{220}$') def fmt(x, pos): a, b = '{:.2e}'.format(x).split('e') b = int(b) return r'${} \times 10^{{{}}}$'.format(a, b) figstrain.subplots_adjust(right=0.8) cbar_ax1 = figstrain.add_axes([0.85, 0.55, 0.02, 0.35]) cbar_ax2 = figstrain.add_axes([0.85, 0.1, 0.02, 0.35]) clb = figstrain.colorbar(im, cax=cbar_ax1) # , format=ticker.FuncFormatter(fmt)) figstrain.colorbar(im2, cax=cbar_ax2) linestart = [100, 50] linestop = [100, 350] clb.set_clim(-0.00004, 0.00004) axstrain[0].autoscale(False) axstrain[0].plot([linestart[0], linestop[0]], [linestart[1], linestop[1]]) z, length = self.getProjection(strainpic, linestart[0], linestart[1], linestop[0], linestop[1], 500) f3, ax3 = plt.subplots() linerange = np.linspace(0, 90*length/1000, len(z)) ax3.plot(linerange, z) ax3.set_ylabel(r'Strain [$\Delta\theta/\theta$]') ax3.set_xlabel(r'[$\mu m$]') # np.save(self.directory + '/strainmap_array.txt', strainpic) f3.savefig(self.directory + '/strainmap_line.pdf') figstrain.savefig(self.directory + '/strainmap_%s.pdf' % str(label)) # f4, ax4 = plt.subplots() # strain = np.reshape(strainpic, len(strainpic[:, 0])*len(strainpic[0, :])) # # strain[strain<-0.0005] = 0 # # strain[strain>0.0005] = 0 # # sns.distplot(strain, kde=False, rug=False) # ax4.set_xlim(np.min(strain)-abs(0.1*np.min(strain)), np.max(strain)+0.1*np.max(strain)) # # ax4.set_xlim(-0.0004,0.0004) # ax4.set_xlabel(r'$\theta$ offset [$^o$]') # ax4.set_title('Strain distribution') # f4.savefig(self.directory + '/straindistribution.pdf') return figstrain, axstrain
def plot_norm(samples, fields=None, filename=None): # get time from timestamp and sample time t = samples.bicycle.dt.mean() * samples.ts n = t.shape[0] if fields is None: # Set default to be fields that are not scalar with data available for # at least 10% of the timerange fields = [] for name in samples.dtype.names: mask = samples.mask[name] if len(mask.shape) < 2: continue if reduce(mul, mask.shape[1:], 1) == 1: continue if np.count_nonzero(mask) < n/10: fields.append(name) if isinstance(fields, str): fields = (fields,) n = len(fields) if n > 6: color = sns.husl_palette(n) else: color = sns.color_palette('muted', n) if n > 1: fig, axes = plt.subplots(math.ceil(n/2), 2) axes = np.ravel(axes) if len(axes) > n: axes[-1].axis('off') else: fig, ax = plt.subplots() axes = [ax] for n, f in enumerate(fields): ax = axes[n] X = samples.__getattribute__(f) x = np.linalg.norm(X, axis=(1, 2)) ax.set_xlabel('{} [{}]'.format('time', unit('time'))) ax.plot(t, x, color=color[n], label=f) ax.legend() if n > 0: title = 'Norms' else: ax.legend().remove() field_parts = fields[0].split('.') title = ' '.join([f.title() for f in field_parts[:-1]] + field_parts[-1:]) title = 'Norm of ' + title axes = ax _set_suptitle(fig, title, filename) return fig, axes
def apply_unrot(filename): import read_idb as ri import dbutil as db import copy from util import lobe, Time import matplotlib.pylab as plt import numpy as np blah = np.load('/common/tmp/Feed_rotation/20170702121949_delay_phase.npz') dph = blah['dph'] fghz = blah['fghz'] out = ri.read_npz([filename]) nbl, npol, nfrq, nt = out['x'].shape # Correct data for phase #n = [0,0,0,1,1,0,1,0,1,1,0,0,0] for i in range(13): a1 = lobe(dph[i] - dph[13]) a2 = -dph[13] + np.pi/2 a3 = dph[i] - np.pi/2 for j in range(nt): out['x'][ri.bl2ord[i,13],1,:,j] *= np.exp(1j*a1) out['x'][ri.bl2ord[i,13],2,:,j] *= np.exp(1j*a2) out['x'][ri.bl2ord[i,13],3,:,j] *= np.exp(1j*a3) trange = Time(out['time'][[0,-1]],format='jd') times, chi = db.get_chi(trange) nskip = len(times)/nt chi = np.transpose(chi[::nskip+1]) chi[[8,9,10,12]] = 0.0 outp = copy.deepcopy(out) for i in range(nt): for k in range(13): outp['x'][ri.bl2ord[k,13],0] = out['x'][ri.bl2ord[k,13],0]*np.cos(chi[k,i]) + out['x'][ri.bl2ord[k,13],3]*np.sin(chi[k,i]) outp['x'][ri.bl2ord[k,13],2] = out['x'][ri.bl2ord[k,13],2]*np.cos(chi[k,i]) + out['x'][ri.bl2ord[k,13],1]*np.sin(chi[k,i]) outp['x'][ri.bl2ord[k,13],3] = out['x'][ri.bl2ord[k,13],3]*np.cos(chi[k,i]) - out['x'][ri.bl2ord[k,13],0]*np.sin(chi[k,i]) outp['x'][ri.bl2ord[k,13],1] = out['x'][ri.bl2ord[k,13],1]*np.cos(chi[k,i]) - out['x'][ri.bl2ord[k,13],2]*np.sin(chi[k,i]) amp0 = np.abs(np.sum(out['x'][ri.bl2ord[:,13]],3)) amp2 = np.abs(np.sum(outp['x'][ri.bl2ord[:,13]],3)) f, ax = plt.subplots(4,13) for i in range(13): for j in range(4): ax[j,i].cla() ax[j,i].plot(fghz, amp0[i,j],'.',color='lightgreen') ax[j,i].plot(fghz, amp2[i,j],'k.') ph0 = np.angle(np.sum(out['x'][ri.bl2ord[:,13]],3)) ph2 = np.angle(np.sum(outp['x'][ri.bl2ord[:,13]],3)) f, ax = plt.subplots(4,13) for i in range(13): for j in range(4): ax[j,i].cla() ax[j,i].plot(fghz, ph0[i,j],'.',color='lightgreen') ax[j,i].plot(fghz, ph2[i,j],'k.')
def makePlots(sampletitle, amplpic, midppic, fwhmpic): fig0, ax0 = plt.subplots(1, 1, dpi=150, figsize=[5, 5]) # plt.tight_layout() fig1, ax1 = plt.subplots(1, 1, dpi=150, figsize=[5, 5]) # plt.tight_layout() fig2, ax2 = plt.subplots(1, 1, dpi=150, figsize=[5, 5]) # plt.tight_layout() # fig0.set_figsize_inches(7,7) # fig1.set_size_inches(7,7) # fig2.set_size_inches(7,7) im0 = ax0.imshow(amplpic[3:-3, 3:-3], cmap='jet', interpolation='None') im1 = ax1.imshow(fwhmpic[3:-3, 3:-3], cmap='BrBG', interpolation='None') im2 = ax2.imshow(midppic[3:-3, 3:-3] , cmap='BrBG', interpolation='None') ax0.set_title('AMPL') ax1.set_title('FWHM') ax2.set_title('MIDP') def fmt(x, pos): a, b = '{:.2e}'.format(x).split('e') b = int(b) return r'${} \times 10^{{{}}}$'.format(a, b) fig0.subplots_adjust(right=0.8) fig1.subplots_adjust(right=0.8) fig2.subplots_adjust(right=0.8) cbar_ax0 = fig0.add_axes([0.85, 0.1, 0.05, 0.8]) cbar_ax1 = fig1.add_axes([0.85, 0.1, 0.05, 0.8]) cbar_ax2 = fig2.add_axes([0.85, 0.1, 0.05, 0.8]) clb0 = fig0.colorbar(im0, cax=cbar_ax0) # , format=ticker.FuncFormatter(fmt)) clb1 = fig1.colorbar(im1, cax=cbar_ax1) # , format=ticker.FuncFormatter(fmt)) clb2 = fig2.colorbar(im2, cax=cbar_ax2) # , format=ticker.FuncFormatter(fmt)) # # clb0.set_clim(0., 200.) # clb2.set_clim(-0.1, 0.1) # clb1.set_clim(-0.03, 0.03) fig0.savefig('plots/%s-ampl.pdf' % (sampletitle)) fig1.savefig('plots/%s-fwhm.pdf' % (sampletitle)) fig2.savefig('plots/%s-midp.pdf' % (sampletitle)) fig0.clf() fig1.clf() fig2.clf()
def exampleNetworks(): names = [u'Yoachim, P', u'Bellm, E', u'Williams, B', u'Williams, B', u'Capelo, P'] # add some caching so it only querries once. if not hasattr(exampleNetworks,'results'): exampleNetworks.results = [None for name in names] exampleNetworks.graphs = [None for name in names] years = [2007, 2011, 2002, 2010, 2012] texts = ['(a)', '(b)','(c)', '(d)','(e)'] count = 1 figs = [] filenames = [] for name,year,txt in zip(names,years,texts): fig,ax = plt.subplots() figDummy, axDummy = plt.subplots() phdA = list(ads.SearchQuery(q=u'bibstem:*PhDT', author=name, year=year, database='astronomy'))[-1] if exampleNetworks.results[count-1] is None: result, graph = phdArticle2row(phdA, checkUSA=False, verbose=True, returnNetwork=True) exampleNetworks.results[count-1] = result exampleNetworks.graphs[count-1] = graph else: result = exampleNetworks.results[count-1] graph = exampleNetworks.graphs[count-1] years = [] for node in graph.nodes(): years.append(float(node[0:4])) years = np.array(years) # Make the graph repeatable pos = {} for i, node in enumerate(graph.nodes()): pos[node] = (years[i],i**2) layout = nx.spring_layout(graph, pos=pos) nx.draw_networkx(graph, pos=layout, ax=ax, node_size=100, node_color=years, alpha=0.5, with_labels=False) #nx.draw_spring(graph, ax=ax, node_size=100, # node_color=years, alpha=0.5, with_labels=False) mappableDummy = axDummy.scatter(years,years,c=years) cbar = plt.colorbar(mappableDummy, ax=ax, format='%i') cbar.set_label('Year') ax.text(.1,.8, txt, fontsize=24, transform=ax.transAxes) ax.set_axis_off() figs.append(fig) filenames.append('example_network_%i' %count) count += 1 print result return figs, filenames
def plot_ohlc(df, maDay=5, maType='simple', **kwarg): """ df: pandas DataFrame generated from yahoo_finance or it need to have these five columns: 'Open', 'High', 'Low', 'Close', 'Volume' maDay: int number of days to do moving average maType: string 'simple' or "exp" """ # set default and unpack kwarg opt = { "title" : "Historical data", "xlabel" : "", "ylabel" : "Price", "lowerVolume" : 0, 'colorup' : 'r', 'colordown' : 'g' } opt.update(kwarg) # filter days when the market is not open. df = df[df['Volume']>opt['lowerVolume']].copy() # initialise figures fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(8,8)) # adjust plot sizes l1, b1, w1, h1 = 0.10, 0.30, 0.85, 0.60 # top plot l2, b2, w2, h2 = 0.10, 0.10, 0.85, 0.20 # bottom plot ax1.set_position([l1, b1, w1, h1]) ax2.set_position([l2, b2, w2, h2]) # convert to mdates and plot volumes df['mdates'] = map(lambda date: mdates.date2num(date), df.index.to_pydatetime()) df.plot(x='mdates', y='Volume', ax=ax2, legend=False, ls='steps') ax2.set_yscale("log") ax2.set_ylabel("Volume") ax2.xaxis.set_major_formatter(mdates.DateFormatter('%d\n%h\n%Y')) # plot candlesticks sticks = candlestick_ohlc( ax1, df[['mdates', 'Open', 'High', 'Low', 'Close']].values, colorup=opt['colorup'], colordown=opt['colordown'], width=0.8, alpha=0.7) # create medium price df['median'] = df[['Open', 'High', 'Low', 'Close']].median(axis=1) df.plot(x='mdates', y='median', ax=ax1, kind='scatter', c='k', marker='_') # moving average maLabel = "{:d}D Moving Average".format(maDay) df['MA'] = moving_average(df['median'], maDay, maType) # true MA df[maLabel] = df['MA']+df['median'].mean()*0.05 df.plot(x='mdates', y=maLabel, ax=ax1, c='m') # set title and other stuff ax1.set_title(opt["title"]) ax1.set_ylabel(opt["ylabel"]) ax2.set_xlabel(opt["xlabel"]) plt.show()
def plot(self, ax = None, legend = True, label = None, **kwargs): """Plot each parameter separately versus time Arguments --------- same as pandas.plot Returns ------- list of matplotlib axes object """ # a = self.data.plot(**kwargs) if not ax: f,ax = _plt.subplots() else: f = ax.get_figure() for k in self.data.keys(): if not label: label_t = k else: label_t = label ax.plot(self.data.index, self.data[k].values, label = label_t, **kwargs) ax.set_xlabel(self._x_label) ax.set_ylabel(self._y_label) if len(self.data.keys()) > 1: ax.legend() f.autofmt_xdate() return ax
def plot_cell_to_prob_dist(height, width, ripl, source_cells, year=0, day=0, order='F', name=''): assert isinstance( source_cells, (list,tuple) ) assert isinstance( source_cells[0], int) fig,ax = plt.subplots(len(source_cells), 1, figsize=(5,2.5*len(source_cells))) for count, cell in enumerate(source_cells): grid_cell_to_prob_dist = cell_to_prob_dist(height, width, ripl, cell,year,day,order) im= ax[count].imshow(grid_cell_to_prob_dist, cmap='hot', vmin=0, vmax=1, interpolation='none', extent=[0,width,height,0]) ax[count].set_title('%s P(i,j) for Cell i=%i, day:%i'%(name,cell,day)) ax[count].set_xticks(range(width+1)) ax[count].set_yticks(range(height+1)) ij_cell = ind_to_ij( height, width, cell, order=order) ij_cell = ij_cell[1]+.1, ij_cell[0]+.5 # switch order for annotation ax[count].annotate('Cell i', xy = ij_cell, xytext = ij_cell, color='c') fig.tight_layout() fig.subplots_adjust(right=0.8) cbar_ax = fig.add_axes([0.75, 0.7, 0.05, 0.2]) fig.colorbar(im, cax=cbar_ax)
def plotGetRetangle(): """ Area selection from selected pen. """ selRect = [] if len(ds.EpmDatasetAnalysisPens.SelectedPens) != 1: sr.msgBox('EPM Python Plugin - Demo Tools', 'Please select a single pen before applying this function!', 'Warning') return 0 epmData = ds.EpmDatasetAnalysisPens.SelectedPens[0].values y = epmData['Value'].copy() x = np.arange(len(y)) fig, current_ax = pl.subplots() pl.plot(x, y, lw=2, c='g', alpha=.3) def line_select_callback(eclick, erelease): 'eclick and erelease are the press and release events' x1, y1 = eclick.xdata, eclick.ydata x2, y2 = erelease.xdata, erelease.ydata print ("\n(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2)) selRect.append((int(x1), y1, int(x2), y2)) def toggle_selector(event): if event.key in ['Q', 'q'] and toggle_selector.RS.active: toggle_selector.RS.set_active(False) if event.key in ['A', 'a'] and not toggle_selector.RS.active: toggle_selector.RS.set_active(True) toggle_selector.RS = RectangleSelector(current_ax, line_select_callback, drawtype='box', useblit=True, button=[1,3], minspanx=5, minspany=5, spancoords='pixels') pl.connect('key_press_event', toggle_selector) pl.show() return selRect
def plot_locking_states(df, meta, num_joints=None): marker_style = dict(linestyle=':', marker='o', s=100,) def format_axes(ax): ax.margins(0.2) ax.set_axis_off() if num_joints is None: num_joints = determine_num_joints(df) points = np.ones(num_joints) fig, ax = plt.subplots() for j in range(num_joints): ax.text(-1.5, j, "%d" % j) ax.text(0, -1.5, "time") for t in df.index: lock_states = df.loc[t][ [ "LockingState%d" % k for k in range(num_joints) ] ].tolist() c = ["orange" if l else "k" for l in lock_states] ax.scatter((t+0.1) * points, range(num_joints), color=c, **marker_style) format_axes(ax) ax.set_title('Locking state evolution') ax.set_xlabel("t") plt.plot()
def plot_MheF(isotracks=None, labels=None, colors=None): """ plot the minimum initial mass for He Fusion """ if isotracks is None: isotracks = ['isotrack/parsec/CAF09_MC_S13v3_OV0.3.dat', 'isotrack/parsec/CAF09_MC_S13v3_OV0.4.dat', 'isotrack/parsec/CAF09_MC_S13v3_OV0.5.dat', 'isotrack/parsec/CAF09_MC_S13v3_OV0.6.dat', 'isotrack/parsec/CAF09_S12D_NS_1TP.dat'] isotracks = [os.path.join(os.environ['TRILEGAL_ROOT'], i) for i in isotracks] if labels is None: labels = ['$\Lambda_c=0.3$', '$\Lambda_c=0.4$', '$\Lambda_c=0.5$', '$\Lambda_c=0.6$', '$S12D\_NS\_1TP$'] if colors is None: colors = ['darkred', 'orange', 'navy', 'purple', 'k'] fig, ax = plt.subplots() for i, isotrack in enumerate(isotracks): isot = trilegal.IsoTrack(isotrack) ax.plot(isot.Z, isot.mhefs, lw=2, label=labels[i], color=colors[i]) ax.plot(isot.Z, isot.mhefs, 'o', color=colors[i]) ax.grid() ax.set_xlim(0.001, 0.0085) ax.set_ylim(1.55, 2.05) return ax
def plotting(): # plt.ion() countries = ['France', 'Spain', 'Sweden', 'Germany', 'Finland', 'Poland', 'Italy', 'United Kingdom', 'Romania', 'Greece', 'Bulgaria', 'Hungary', 'Portugal', 'Austria', 'Czech Republic', 'Ireland', 'Lithuania', 'Latvia', 'Croatia', 'Slovakia', 'Estonia', 'Denmark', 'Netherlands', 'Belgium'] extensions = [547030, 504782, 450295, 357022, 338145, 312685, 301340, 243610, 238391, 131940, 110879, 93028, 92090, 83871, 78867, 70273, 65300, 64589, 56594, 49035, 45228, 43094, 41543, 30528] populations = [63.8, 47, 9.55, 81.8, 5.42, 38.3, 61.1, 63.2, 21.3, 11.4, 7.35, 9.93, 10.7, 8.44, 10.6, 4.63, 3.28, 2.23, 4.38, 5.49, 1.34, 5.61, 16.8, 10.8] life_expectancies = [81.8, 82.1, 81.8, 80.7, 80.5, 76.4, 82.4, 80.5, 73.8, 80.8, 73.5, 74.6, 79.9, 81.1, 77.7, 80.7, 72.1, 72.2, 77, 75.4, 74.4, 79.4, 81, 80.5] data = {'extension': pd.Series(extensions, index=countries), 'population': pd.Series(populations, index=countries), 'life expectancy': pd.Series(life_expectancies, index=countries)} df = pd.DataFrame(data) print(df) df = df.sort('life expectancy') fig, axes = plt.subplots(nrows=3, ncols=1) for i, c in enumerate(df.columns): df[c].plot(kind='bar', ax=axes[i], figsize=(12, 10), title=c) plt.show()
def test_params(): #x = np.linspace(.8, 1.2, 1e2) x = np.linspace(-.2, .2, 1e2) num = 5 range_a = np.linspace(1, 2, num) range_b = np.linspace(1., 1.1, num) range_p = np.linspace(.1, .4, num) range_q = np.linspace(.1, .4, num) range_T = np.linspace(30, 365, num) / 365 args_def = {'a' : range_a.mean(), 'b' : range_b.mean(), 'p' : range_p.mean(), 'q' : range_q.mean(), 'T' : range_T.mean()} ranges = {'a' : range_a, 'b' : range_b, 'p' : range_p, 'q' : range_q, 'T' : range_T} fig, axes = plt.subplots(nrows = len(ranges), figsize = (6,12)) for name, a in zip(sorted(ranges.keys()), axes): args = args_def.copy() for pi in ranges[name]: args[name] = pi f = GB2(**args).density(x) a.plot(x, f, label = pi) a.legend(title = name) plt.show()
def SVD_plot(SVStreams, SValues, stachans, title=False): r"""Function to plot the singular vectors from the clustering routines, one\ plot for each stachan :type SVStreams: list of :class:Obspy.Stream :param SVStreams: See clustering.SVD_2_Stream - will assume these are\ ordered by power, e.g. first singular vector in the first stream :type SValues: list of float :param SValues: List of the singular values corresponding to the SVStreams :type stachans: list :param stachans: List of station.channel """ for stachan in stachans: print(stachan) plot_traces = [SVStream.select(station=stachan.split('.')[0], channel=stachan.split('.')[1])[0] for SVStream in SVStreams] fig, axes = plt.subplots(len(plot_traces), 1, sharex=True) axes = axes.ravel() for i, tr in enumerate(plot_traces): y = tr.data x = np.linspace(0, len(y) * tr.stats.delta, len(y)) axes[i].plot(x, y, 'k', linewidth=1.1) ylab = 'SV '+str(i+1)+'='+str(round(SValues[i] / len(SValues), 2)) axes[i].set_ylabel(ylab, rotation=0) axes[i].yaxis.set_ticks([]) print(i) axes[-1].set_xlabel('Time (s)') plt.subplots_adjust(hspace=0) if title: axes[0].set_title(title) else: axes[0].set_title(stachan) plt.show() return
def plot_integrated_colors(filenames, labels='Z'): if type(filenames) is str: filenames = [filenames] ax = None cols = ['k'] else: fig, ax = plt.subplots() cols = brewer2mpl.get_map('Spectral', 'Diverging', len(filenames)).mpl_colors if labels == 'Z': fmt = '$Z=%.4f$' labels = [fmt % float(l.replace('.dat', '').split('Z')[1]) for l in filenames] else: print 'need to fix labels' labels = [''] * len(filenames) for i, filename in enumerate(filenames): data = rsp.fileIO.readfile(filename) ycol = 'V-K' xcol = 'Age' ax = rg.color_color(data, xcol, ycol, xscale='log', ax=ax, plt_kw={'lw': 2, 'color': cols[i], 'label': labels[i]}) plot_cluster_data(ax) ax.legend(frameon=False, loc=0, numpoints=1) ax.set_xlabel(r'${\rm %s}$' % xcol, fontsize=20) ax.set_ylabel(r'${\rm %s}$' % ycol, fontsize=20) plt.tick_params(labelsize=16) return ax
def plot_dataframe_meshgrid(df, xaxis = 0, ax = None): axes_list = [df.index, df.columns] x_index = axes_list[xaxis] if xaxis !=0: df = df.swapaxes(0,1) y_index = axes_list[0] else: y_index = axes_list[1] z = df.values.transpose() x = np.repeat(np.array([x_index]), y_index.shape[0], axis = 0) y = np.repeat(np.array([y_index]), x_index.shape[0], axis = 0).transpose() if ax: a = ax f = a.get_figure() else: f,a = plt.subplots() pc = a.pcolormesh(x, y , z) if 'datetime' in df.index.dtype_str: f.autofmt_xdate() cb = f.colorbar(pc) a.set_xlabel(df.index.name) a.set_ylabel(df.columns.name) # nans, screw up the scaling, therefore ... if np.any(np.isnan(df.values)): values = df.values values = values[~ np.isnan(values)] pc.set_clim((values.min(),values.max())) return f,a,pc,cb
def interev_mag(times, mags): r"""Function to plot interevent times against magnitude for given times and magnitudes. :type times: list of datetime :param times: list of the detection times, must be sorted the same as mags :type mags: list of float :param mags: list of magnitudes """ l = [(times[i], mags[i]) for i in xrange(len(times))] l.sort(key=lambda tup: tup[0]) times = [x[0] for x in l] mags = [x[1] for x in l] # Make two subplots next to each other of time before and time after fig, axes = plt.subplots(1, 2, sharey=True) axes = axes.ravel() pre_times = [] post_times = [] for i in range(len(times)): if i > 0: pre_times.append((times[i] - times[i - 1]) / 60) if i < len(times) - 1: post_times.append((times[i + 1] - times[i]) / 60) axes[0].scatter(pre_times, mags[1:]) axes[0].set_title('Pre-event times') axes[0].set_ylabel('Magnitude') axes[0].set_xlabel('Time (Minutes)') plt.setp(axes[0].xaxis.get_majorticklabels(), rotation=30) axes[1].scatter(pre_times, mags[:-1]) axes[1].set_title('Post-event times') axes[1].set_xlabel('Time (Minutes)') plt.setp(axes[1].xaxis.get_majorticklabels(), rotation=30) plt.show()
def plot_t(t, sol, axes = None): if axes is None: fig_t, (ax_s, ax_i) = pl.subplots(1,2, sharey=True) fig_t.subplots_adjust(wspace=0) else: ax_s, ax_i = axes a_sol = abs(sol)**2 ## Plot time evolution # Pump Envelope ax_s.plot(t, a_sol[0,:,2],'--') ax_s.plot(t, a_sol[0,:,3],'--') ax_i.plot(t, a_sol[0,:,2],'--') ax_i.plot(t, a_sol[0,:,3],'--') selection = np.linspace(0, sol.shape[0]-1, 15, dtype = np.int32) for i in selection: ax_s.plot(t, a_sol[i,:,0], c = pl.cm.coolwarm(i/sol.shape[0])) ax_i.plot(t, a_sol[i,:,1], c = pl.cm.coolwarm(i/sol.shape[0])) #ax_s.set_xlim(-75,75) #ax_i.set_xlim(-75,75) ax_s.set_ylim(0,1.1) ax_i.set_ylim(0,1.1) ax_s.set_title('Signal') ax_i.set_title('Idler')
def avg_scores_plot(fit_results, predictor_variable): n_array = [] r2_array = [] rmse_array = [] for key in fit_results: value = fit_results[key] i = 0 sum_r2 = 0 sum_rmse = 0 while i < len(value): sum_r2 = float(sum_r2) + value[i][1] sum_rmse = float(sum_rmse) + value[i][2] i = i + 1 avg_r2 = sum_r2/len(value) avg_rmse = sum_rmse/len(value) n_array.append(key) r2_array.append(avg_r2) rmse_array.append(avg_rmse) print 'For n = ' + str(key) + ': Average R^2 = ' + str(avg_r2) + ', Average RMSE = ' + str(avg_rmse) print 'Minimum Average RMSE is '+str(min(rmse_array)) #Plot Average Values to determine best fit f, ax = plt.subplots(2, sharex=True) ax[0].scatter(n_array, r2_array, color='r', label='Average R^2 Value') ax[0].set_ylabel('Average R^2 Value') ax[0].xaxis.grid() ax[0].yaxis.grid() ax[1].scatter(n_array, rmse_array, color='blue', label='Average RMSE Value') ax[1].set_ylabel('Average RMSE Value') ax[1].xaxis.grid() ax[1].yaxis.grid() plt.xlabel('Polynomial Fit Order') ax[0].set_title('Avg. R^2 & RMSE in ' + str(len(value))+ '-fold Cross Validation of ' + str(predictor_variable) + ' to No. 311 Incidents') plt.show()
def show_3D(self): fig, axes = plt.subplots(1, 2, subplot_kw={'projection':'3d'}) ax0, ax1 = axes for label in self.labels: ax0, ax1 = self.show_segment(label, axes) return ax0, ax1
def draw_several_cams(geom, ncams=4): cmaps = ['jet', 'afmhot', 'terrain', 'autumn'] fig, axs = plt.subplots(1, ncams, figsize=(15, 4), sharey=True, sharex=True) for ii in range(ncams): disp = visualization.CameraDisplay( geom, ax=axs[ii], title="CT{}".format(ii + 1), ) disp.cmap = cmaps[ii] model = mock.generate_2d_shower_model( centroid=(0.2 - ii * 0.1, -ii * 0.05), width=0.005 + 0.001 * ii, length=0.1 + 0.05 * ii, psi=ii * 20 * u.deg, ) image, sig, bg = mock.make_mock_shower_image( geom, model.pdf, intensity=50, nsb_level_pe=1000, ) clean = image.copy() clean[image <= 3.0 * image.mean()] = 0.0 hillas = hillas_parameters(geom.pix_x, geom.pix_y, clean) disp.image = image disp.add_colorbar(ax=axs[ii]) disp.set_limits_percent(95) disp.overlay_moments(hillas, linewidth=3, color='blue')
def psd_with_bands_plot(f, psd, figsize=(12,8)): """ Plot a static PSD. INPUTS f : 1D array containing frequencies of the PSD psd : 1D array containing the power at each frequency in f figsize : figure size """ bands = collections.OrderedDict() bands[r'$\delta$'] = (0,4) bands[r'$\theta$'] = (4,8) bands[r'$\alpha$'] = (8,13) bands[r'$\beta$'] = (13, 30) bands[r'$\gamma$'] = (30, 120) fig, ax = plt.subplots(figsize=figsize) ax.plot(f, psd) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Power (dB)') ylim = ax.get_ylim() for i, [bkey, bfreq] in enumerate(bands.iteritems()): ind = (f>=bfreq[0]) & (f<=bfreq[1]) f1 = f[ind] y1 = psd[ind] ax.fill_between(f1, y1, ylim[0], facecolor=[(0.7, i/5., 0.7)], alpha=0.5) ax.text(np.mean(f1), (ylim[0] + ylim[1])/1.22, bkey, fontsize=16, verticalalignment='top', horizontalalignment='center') ax.set_xlim([min(f), max(f)])
def plot_locality_regression(snps,cob,gene_limit=10): # Get degree and bootstrap degree log('Fetching Empirical Degree') degree = cob.locality(cob.refgen.candidate_genes(snps,gene_limit=gene_limit,chain=True)).sort('local') log('Fetching BS Degree') #bsdegree = pd.concat([cob.locality(cob.refgen.bootstrap_candidate_genes(snps,gene_limit=gene_limit,chain=True)) for x in range(50)]).sort('local') # get OLS for the bootstrapped degree log('Fitting models') model = sm.OLS(degree['global'],degree.local) res = model.fit() std, iv_l, iv_u = wls_prediction_std(res) # plot the bootstrapped data fig,ax = pylab.subplots(figsize=(8,6)) fig.hold(True) ax.set_xlim(0,max(degree.local)) ax.set_ylim(0,max(degree['global'])) # plot the bootstraps std # plot the true data log('Plotting Empirical') ax.plot(degree.local,degree['global'],'o',label='Empirical') log('Plotting Residuals') ax.plot(degree.local,res.fittedvalues,'--') ax.plot(degree.local,res.fittedvalues+2.5*std,'r--') ax.plot(degree.local,res.fittedvalues-2.5*std,'r--') ax.set_xlabel('Number Local Interactions') ax.set_ylabel('Number Global Interactions') log('Saving Figure') fig.savefig('{}_locality.png'.format(cob.name))
def make_lf(ghist, gbins, shist, sbins, gerrs=None, serrs=None, colors=['black', 'darkred'], label='', ax=None): if ax is None: fig, ax = plt.subplots(figsize=(8, 8)) if not None in [gerrs, serrs]: plt_kw = {'drawstyle': 'steps-mid', 'color': colors[0], 'lw': 2} ax.errorbar(gbins[1:], ghist, yerr=gerrs, **plt_kw) plt_kw['color'] = colors[1] plt_kw['label'] = label plt_kw['alpha'] = 0.3 ax.errorbar(sbins[1:], shist, yerr=serrs, **plt_kw) else: plt_kw = {'linestyle': 'steps-mid', 'color': colors[0], 'lw': 2} ax.plot(gbins[1:], ghist, **plt_kw) plt_kw['color'] = colors[1] plt_kw['label'] = label plt_kw['alpha'] = 0.3 ax.plot(sbins[1:], shist, **plt_kw) ax.set_yscale('log') ax.tick_params(labelsize=16) ax.set_ylabel(r'${\rm Number\ of\ Stars}$', fontsize=20) return ax
def __init__(self, data={}, elements=None, pair_data={}, **kwargs): # Add custom elemental data to existing data init_data = dict(elt_data) elts = {} for elt, value in init_data.items(): assert isinstance(value, dict) if elements: if not elt in elements: continue elts[elt] = init_data[elt] if elt in data: elts[elt].update(data[elt]) self._pairs = pair_data self._elts = elts self.squares = [] self.collections = [] self.functions = [] self.groups = [] self.values = [] self.cmaps = [] if not 'axes' in kwargs: fig, axes = plt.subplots() fig.patch.set_visible(False) else: axes = kwargs.get('axes') self._ax = axes
def check_models(self): temp = np.logspace(0, np.log10(600)) num = len(self.available_models()) fig, ax = plt.subplots(1) self.plotting_colours(num, fig, ax, repeats=2) for author in self.available_models(): Nc, Nv = self.update(temp=temp, author=author) # print Nc.shape, Nv.shape, temp.shape ax.plot(temp, Nc, '--') ax.plot(temp, Nv, '.', label=author) ax.loglog() leg1 = ax.legend(loc=0, title='colour legend') Nc, = ax.plot(np.inf, np.inf, 'k--', label='Nc') Nv, = ax.plot(np.inf, np.inf, 'k.', label='Nv') plt.legend([Nc, Nv], ['Nc', 'Nv'], loc=4, title='Line legend') plt.gca().add_artist(leg1) ax.set_xlabel('Temperature (K)') ax.set_ylabel('Density of states (cm$^{-3}$)') plt.show()
def plot(self,frequency=True,phase=False,dB=True,cal=True,fig=[],ax=[],color='k'): """ """ if fig==[]: fig,ax=plt.subplots(8,self.Nt,sharex=True,sharey=True) if cal: H = self.Hcal else: H = self.H for iR in range(self.Nr): for iT in range(self.Nt): k = iR*4+iT if frequency: if not phase: if dB: #ax[iR,iT].plot(H.x,20*np.log10(abs(H.y[k,:])),color=color) ax[iR,iT].plot(H.x,20*np.log10(abs(H.y[iR,iT,:])),color=color) else: #ax[iR,iT].plot(H.x,abs(H.y[k,:]),color='k') ax[iR,iT].plot(H.x,abs(H.y[iR,iT,:]),color='k') else: #ax[iR,iT].plot(H.x,np.unwrap(np.angle(H.y[k,:])),color=color) ax[iR,iT].plot(H.x,np.unwrap(np.angle(H.y[iR,iT,:])),color=color) else: ax[iR,iT].plot(self.h.x,abs(self.h.y[iR,iT,:]),color=color) if (iR==7): ax[iR,iT].set_xlabel('f (GHz)') ax[iR,iT].set_title(str(iR+1)+'x'+str(iT+1)) return(fig,ax)
import matplotlib as mpl import matplotlib.pylab as plt import numpy as np fig, ax = plt.subplots(1, 1, figsize=(6, 6)) # setup the plot x = np.random.rand(20) # define the data y = np.random.rand(20) # define the data tag = np.random.randint(0, 20, 20) tag[10:12] = 0 # make sure there are some 0 values to show up as grey print(tag) cmap = plt.cm.jet # define the colormap # extract all colors from the .jet map cmaplist = [cmap(i) for i in range(cmap.N)] # force the first color entry to be grey cmaplist[0] = (.5, .5, .5, 1.0) # create the new map cmap = mpl.colors.LinearSegmentedColormap.from_list('Custom cmap', cmaplist, cmap.N) # define the bins and normalize bounds = np.linspace(0, 20, 21) norm = mpl.colors.BoundaryNorm(bounds, cmap.N) # make the scatter scat = ax.scatter( x, y, c=tag, # s=np.random.randint(100, 500, 20),
def pretty_results(sumsdict, numcards, numberofmax=10): # make a nice graph of all the things I care about # takes in a rotated-sum dictionary explicitly from rotate_sum # num cards is number of cards that we're testing #The only frame which I don't like is count number (it should display the count above) #and also uh the histogram by int has a useless y bar, but I really dont know what to do about that one.... # Im aware that this function is far too long and pretty ugly # I don't really know how to improve it # breaking it up into a function for each subplot would do it, but each function is only used once, so # labels are commented out because they made it all wonky # first put it into a dataframe for ease of use sums = pd.DataFrame.from_dict(sumsdict, orient='index', columns=['CountClass', 'CountWeight']).sort_index() # normalize # Count weight is easy- divide by 13**numcards countWeightNormalizer = 13**numcards # Countclass- stars and bars- (13+numcards-1)! choose numcards countClassNormalizer = math.factorial(13 + numcards - 1) / ( math.factorial(numcards) * math.factorial(13 - 1)) sums['CountClass'] = sums['CountClass'] / countClassNormalizer sums['CountWeight'] = sums['CountWeight'] / countWeightNormalizer #Now we get to plot! On each graph, # class is default one, weight is default 2 (blue and orange respectively) # Setting up the figure fig, axs = plt.subplots(4, 2) fig.set_figheight(8) fig.set_figwidth(10) #I dont get how the zooming works, but this much is enough that theres detail,so fig.suptitle('Reverse 24', size=16) # Plotting integer answers xmin = -15 xmax = 70 intAnswers = sums[[x.is_integer() and xmin < x < xmax for x in sums.index]] xticks = [ x for x in range(int(intAnswers.index.min()), int(intAnswers.index.max())) if x % 5 == 0 ] ymax = intAnswers.max() axs[0, 0].plot(intAnswers, '.') axs[0, 0].vlines(x=xticks, ymin=0, ymax=1, linestyles='dotted') axs[0, 0].set_xticks(xticks[::2]) axs[0, 0].hlines(y=axs[0, 0].get_yticks()[2:-2], xmin=min(xticks), xmax=max(xticks), linestyles='dotted') axs[0, 0].set_title('Small Integer solutions ') axs[0, 0].set_xlabel('Number') axs[0, 0].set_ylabel('% hands \n with solution') # Plot int differences intdifference = intAnswers['CountWeight'] - intAnswers['CountClass'] axs[0, 1].plot(intdifference, 'r') axs[0, 1].vlines(x=xticks, ymin=intdifference.min() * 1.2, ymax=intdifference.max() * 1.2, linestyles='dotted') axs[0, 1].set_xticks(xticks[::2]) axs[0, 1].hlines(y=axs[0, 1].get_yticks()[1:-1], xmin=min(xticks), xmax=max(xticks), linestyles='dotted') axs[0, 1].set_title('(Weight-Class) \n for each small integer solution') axs[0, 1].set_xlabel('Number') axs[0, 1].set_ylabel('% Difference ') # Plotting every single value looks bad; this 'smooths it out' by grouping by nearest int # Creating a histogram, using likelyhood of getting that number as its weight # All buckets are all the results that fall larger than that integer # this does mean that reading any given column is absolutely useless, becuase of the way the things doublecount # the rightway to do it would probably be to do the summary statistics all over again, and keep track to only # count each class the correct number of times (i duplicate a class if say it can make both .2 and .4) # But the distribution is still interesting, I think # It summarizes the 'tail' of the data in the leftmost and rightmost buckets histmin = -50 histmax = 500 histprep = sums[(sums.index > histmin) & (sums.index < histmax)] histprep = histprep.append( pd.DataFrame([ sums[sums.index <= histmin].sum(), sums[sums.index >= histmax].sum() ], columns=['CountClass', 'CountWeight'], index=[histmin, histmax])) n = histmax - histmin axs[1, 0].hist(histprep.index, bins=n, weights=histprep['CountClass']) # axs[1, 0].plot(sums) #this just looks very messy # a bucket for every single entry. Also messy because bucket size changes. Also takes forever # axs[1, 0].hist(sums.index,bins=sums.index, weights=sums['CountClass']) axs[1, 0].set_title('Histogram by Int') axs[1, 0].set_yscale('log') axs[1, 0].set_xlabel('Number') axs[1, 0].set_ylabel('Non-independent \n % sum by int') # Same plot, but for weights instead of class; the 'duplicity' shows up more, in that the sums are well above 1! axs[1, 1].hist(histprep.index, bins=int(n), weights=histprep['CountWeight'], color='orange') axs[1, 1].set_title('Histogram by Int') axs[1, 1].set_yscale('log') axs[1, 1].set_xlabel('Number') axs[1, 1].set_ylabel('Non-independent \n % sum by int') # histogram of counts, i.e. buckets by how many times a number is hit axs[2, 0].hist(sums.transpose(), bins=[x / 10 for x in range(0, 10)], log=True) axs[2, 0].set_title('Chance succeed vs Count') axs[2, 0].set_xlabel('% hands that hit a given Number') axs[2, 0].set_ylabel('Count Number') # Plot the most common answers, as a bar graph, so that its directly comparable # I do slightly fancy manipulations to insure top 10 of both weights and calsses show up commonIndices = set(sums.nlargest(numberofmax, 'CountClass').index) commonIndices = commonIndices.union( sums.nlargest(numberofmax, 'CountWeight').index) commonIndices = commonIndices.union([ 24 ]) # Specifically to test the 24 game hypothesis. rarely pushes an error commonAnswers = sums.transpose()[commonIndices].transpose() commonAnswers = commonAnswers.sort_values('CountClass', ascending=False) ylow = commonAnswers.min().min() # take minimum of both columns yhigh = commonAnswers.max().max() # the maximum value commonAnswers.plot.bar(ax=axs[2, 1], legend=False) axs[2, 1].set_title('Chance of most common Answers, and 24 ') axs[2, 1].set_ylim([ylow * .9, min([yhigh * 1.1, 1])]) axs[2, 1].hlines(y=axs[2, 1].get_yticks()[1:-1], xmin=min(axs[2, 1].get_xticks()), xmax=max(axs[2, 1].get_xticks()), linestyles='dotted') axs[2, 1].set_yticks(axs[2, 1].get_yticks()[1:-1]) axs[2, 1].set_xlabel('Number') axs[2, 1].set_ylabel('% Hands that solve N') #Plot the most common answers as a scatter plot. #I'd like to take the difference of weights and counts but i think all the other graphs are more important; #they're pretty close most of the time I think commonIndices = set(sums.nlargest(numberofmax * 5, 'CountClass').index) commonIndices = commonIndices.union( sums.nlargest(numberofmax * 5, 'CountWeight').index) commonAnswers = sums.transpose()[commonIndices].transpose() commonAnswers = commonAnswers.sort_values('CountClass', ascending=False) xmin = math.floor(min(commonAnswers.index)) xmin = xmin - xmin % 5 xmax = math.ceil(max(commonAnswers.index)) xmax = xmax + 5 - xmax % 5 xticks = range(xmin, xmax, 5) ylow = commonAnswers.min().min() # take minimum of both columns yhigh = commonAnswers.max().max() # the maximum value numIndices = len(commonIndices) axs[3, 0].plot(commonAnswers, '.') axs[3, 0].set_ylim([ylow * .9, min([yhigh * 1.1, 1])]) axs[3, 0].vlines(x=xticks, ymin=0, ymax=ymax * 1.2, linestyles='dotted') axs[3, 0].set_xticks(xticks[::2]) axs[3, 0].set_title(str(numIndices) + ' most likely targets ') axs[3, 0].hlines(y=axs[3, 0].get_yticks()[1:-1], xmin=min(axs[3, 0].get_xticks()), xmax=max(axs[3, 0].get_xticks()), linestyles='dotted') axs[3, 0].set_xlabel('Number') axs[3, 0].set_ylabel('% Hands that solve N') handles, labels = axs[2, 1].get_legend_handles_labels() labels = [ 'By Class, i.e. \n [1,1,1,1] counts once, \n [1,2,3,4] counts once', 'By Weight i.e. \n [1,1,1,1] counts once \n [1,2,3,4] counts 24 times' ] axs[3, 1].axis('off') axs[3, 1].legend(handles, labels, prop={'size': 8}, loc='center') plt.tight_layout() fig.subplots_adjust(top=0.88) return plt
def calculateUserReading(): # userRel: (user,doc) -> rel userRel = defaultdict(lambda:-1) # objectRel: (query, doc) -> rel objectRel = defaultdict(lambda:-1) for l in open('../data/relevance_user.tsv'): user, docid,rel =l.strip().split('\t') userRel[ ( int(user), int(docid))] = int(rel) for l in open('../data/relevance_9_all.tsv'): query,docid, rel = l.strip().split('\t') objectRel[ ( query, int(docid))] = int(rel) doclength = defaultdict(lambda:-1) imagelength = defaultdict(lambda:-1) for f in os.listdir('../data/pages-content'): docid = f.replace('.txt','') doclength[int(docid)] = len(open('../data/pages-content/'+f).read()) for f in os.listdir('../data/screenshots'): docid = f.replace('.png','') from PIL import Image im = Image.open('../data/screenshots/'+f) width, height = im.size imagelength[int(docid)] = height sessions = parse() uservariance = userVariance(sessions) #user reading behavior user -> dwell time, doc length, picture length userReadBehvOnDoc = defaultdict(lambda:[[],[],[]]) for s in sessions: for i in s.interactions: for c in i.clicks: duration = c.endtime - c.starttime docid = int(c.docno) user = int(s.userid) rel = userRel[(user,docid)] if rel < 3: continue dlength = doclength[docid] dheight = imagelength[docid] if dlength >100 and dheight >700 and duration > 10 and dlength < 30000: userReadBehvOnDoc[user][0].append(duration) userReadBehvOnDoc[user][1].append(dlength) userReadBehvOnDoc[user][2].append(dheight) fout = open('../data/readingBehaviorFit34-new.csv','w') fout.write('user,#samples,slope,intercept,r-value,p-value,stderr,slope,intercept,r-value,p-value,stderr\n') # raw raw = open('../data/raw-user-reading.csv','w') raw.write('user,dwelltime,dlength,dheight\n') for u in userReadBehvOnDoc: for i in range(0,len(userReadBehvOnDoc[u][0]),1): raw.write(','.join([str(item) for item in [u,userReadBehvOnDoc[u][0][i], userReadBehvOnDoc[u][1][i], userReadBehvOnDoc[u][2][i]]])) raw.write('\n') raw.close() check = open('../data/userbehavior-fine-grained.csv','w') for u in userReadBehvOnDoc: check.write(str(u)+'\n') check.write(','.join([str(item) for item in userReadBehvOnDoc[u][0] ])+'\n') check.write(','.join([str(item) for item in userReadBehvOnDoc[u][1] ])+'\n') check.write(','.join([str(item) for item in userReadBehvOnDoc[u][2] ])+'\n') fout.write(str(u)+','+str(len(userReadBehvOnDoc[u][0]))+',') slope1, intercept1, r1, p1, stderr1 = linregress(userReadBehvOnDoc[u][1],userReadBehvOnDoc[u][0]) slope2, intercept2, r2, p2, stderr2 = linregress(userReadBehvOnDoc[u][2],userReadBehvOnDoc[u][0]) import numpy as np length = np.array(userReadBehvOnDoc[u][1]) height = np.array(userReadBehvOnDoc[u][2]) fout.write(','.join([str(item) for item in [slope1, intercept1,r1,p1,stderr1,slope2,intercept2,r2,p2,stderr2]])) fout.write('\n') overall = [[],[],[]] for u in userReadBehvOnDoc: for i in range(0,len(userReadBehvOnDoc[u][0]),1): overall[0].append(userReadBehvOnDoc[u][0][i]) overall[1].append(userReadBehvOnDoc[u][1][i]) overall[2].append(userReadBehvOnDoc[u][2][i]) slope1, intercept1, r1, p1, stderr1 = linregress(overall[1],overall[0]) slope2, intercept2, r2, p2, stderr2 = linregress(overall[2],overall[0]) import matplotlib.pylab as plt fig, ax = plt.subplots() ax.plot(overall[1],overall[0],'+') ax.set_title('x = docment length; y = reading time') plt.savefig('../data/figs/doclength.eps') fig, ax = plt.subplots() ax.plot(overall[2],overall[0],'o') ax.set_title('x = webpage length; y = reading time') plt.savefig('../data/figs/webpagelength.eps') fout.write(','.join([str(item) for item in ['overall', '#',slope1,intercept1,r1,p1,stderr1,slope2,intercept2,r2,p2,stderr2]])) fout.close()
def linkCheck(firstA=False): """ Check to see if we are over or under-linking papers using the unique name flag """ if firstA: latest_key = 'latest_1st_year' else: latest_key = 'latest_year' figs = [] names = [] data = readYear(0, filename='output/all_years.dat') # Make a histogram of number of PhDs per year good = np.where((data['noAstroJournal'] == 'None') & (data['nonUS'] == 'None')) data = data[good] binsize = 2 year_mins = np.arange(1997, 2011 + binsize, binsize) year_maxes = np.arange(1998, 2012 + binsize, binsize) fig, ax = plt.subplots() fig2, ax2 = plt.subplots() fig3, ax3 = plt.subplots() colors = [plt.cm.jet(x) for x in np.linspace(0, 1, year_mins.size)] for ymin, ymax, color in zip(year_mins, year_maxes, colors): good = np.where((data['phd_year'] >= ymin) & (data['phd_year'] <= ymax)) baseline, bins1 = retentionCurve(data[good][latest_key], data[good]['phd_year']) anybaseline, bins1 = retentionCurve(data[good]['latest_year'], data[good]['phd_year']) good = np.where((data['phd_year'] >= ymin) & (data['phd_year'] <= ymax) & (data['uniqueName'] == 'True')) test1, bins2 = retentionCurve(data[good][latest_key], data[good]['phd_year']) test2, bins3 = retentionCurve(data[good]['latest_year_unlinked'], data[good]['phd_year']) ax.plot(bins1, test1 - baseline, '-', label='%i-%i' % (ymin, ymax), color=color) ax.plot(bins1, test2 - baseline, '--', color=color) resid1 = test1 - baseline resid2 = test2 - baseline yerr = [[], []] for r1, r2 in zip(resid1, resid2): min_resid = np.min([r1, r2]) max_resid = np.max([r1, r2]) if min_resid < 0: yerr[0].append(np.abs(min_resid)) else: yerr[0].append(0) if max_resid > 0: yerr[1].append(max_resid) else: yerr[1].append(0) label = '%s-%s' % (str(ymin)[-2:], str(ymax)[-2:]) if firstA: ax2.plot(bins1, baseline, color=color, label=label) #ax2.plot(bins1, anybaseline,'--', color=color, alpha=0.5) ax3.plot(bins1, anybaseline - baseline, color=color, label=label) else: ax2.errorbar(bins1, baseline, yerr=yerr, ecolor=color, color=color, fmt='-o', label=label, alpha=.8) ax.legend(numpoints=1, ncol=2) ax.set_xlabel('Years post PhD') if firstA: ax.set_ylabel('Active Fraction - Unique Name Active Fraction') figs.append(fig) names.append('linkCheck_1stA') ax3.set_ylabel('Active - Active 1st Author Fraction') ax3.set_xlabel('Years Post PhD') ax3.legend(numpoints=1, ncol=2) figs.append(fig3) names.append('staff_frac') else: ax.set_ylabel('Active Fraction - Unique Name Active Fraction') figs.append(fig) names.append('linkCheck') if firstA: ax2.legend(numpoints=1) ax2.set_xlim([0, 17]) ax2.set_xlabel('Years post PhD') ax2.set_ylabel('Fraction Still 1st Authors in ADS') figs.append(fig2) names.append('linkCheck_errorbars_1stA') else: ax2.legend(numpoints=1) ax2.set_xlim([0, 17]) ax2.set_xlabel('Years post PhD') ax2.set_ylabel('Fraction Still Active in ADS') figs.append(fig2) names.append('linkCheck_errorbars') return figs, names
rrel[i] = ((p[0] - parentPos[i][0])**2 + (p[1] - parentPos[i][1])**2 + (p[2] - parentPos[i][2])**2)**0.5 # and here we have rrel and parentPos in physical units. pl.figure() maxbin = int(max(np.log10(parentM200 * 1e10))) + 1 mb = [] for i in range(4): mb.append((maxbin - i - 1, maxbin - i)) plts = [(0, 0), (0, 1), (1, 0), (1, 1)] fig, axs = pl.subplots(2, 2, sharex='col') for i in range(4): massflt_bin = np.where((mb[i][0] <= np.log10(parentM200 * 1e10)) & (np.log10(parentM200 * 1e10) < mb[i][1]) & ((rrel / parentR200) <= 2.5))[0] x_bin = rrel[massflt_bin] / parentR200[massflt_bin] pram_curve = np.log10( Pram_newfit(parentM200[massflt_bin], parentR200[massflt_bin], scale, rrel[massflt_bin])) #pram_curve = Pram(rrel[massflt_bin]/parentR200[massflt_bin], parentM200[massflt_bin], -3.08, 6.82, 0.51, -5.54) ax = axs[plts[i]] ax.plot(rrel / parentR200, rampress, 'y.') ax.plot(rrel[massflt_bin] / parentR200[massflt_bin], rampress[massflt_bin], 'b.') ax.plot(x_bin, pram_curve, 'r.')
def exampleNetworks(): names = [ u'Yoachim, P', u'Bellm, E', u'Williams, B', u'Williams, B', u'Capelo, P' ] # add some caching so it only querries once. if not hasattr(exampleNetworks, 'results'): exampleNetworks.results = [None for name in names] exampleNetworks.graphs = [None for name in names] years = [2007, 2011, 2002, 2010, 2012] texts = ['(a)', '(b)', '(c)', '(d)', '(e)'] count = 1 figs = [] filenames = [] for name, year, txt in zip(names, years, texts): fig, ax = plt.subplots() figDummy, axDummy = plt.subplots() phdA = list( ads.SearchQuery(q=u'bibstem:*PhDT', author=name, year=year, database='astronomy'))[-1] if exampleNetworks.results[count - 1] is None: result, graph = phdArticle2row(phdA, checkUSA=False, verbose=True, returnNetwork=True) exampleNetworks.results[count - 1] = result exampleNetworks.graphs[count - 1] = graph else: result = exampleNetworks.results[count - 1] graph = exampleNetworks.graphs[count - 1] years = [] for node in graph.nodes(): years.append(float(node[0:4])) years = np.array(years) # Make the graph repeatable pos = {} for i, node in enumerate(graph.nodes()): pos[node] = (years[i], i**2) layout = nx.spring_layout(graph, pos=pos) nx.draw_networkx(graph, pos=layout, ax=ax, node_size=100, node_color=years, alpha=0.5, with_labels=False) #nx.draw_spring(graph, ax=ax, node_size=100, # node_color=years, alpha=0.5, with_labels=False) mappableDummy = axDummy.scatter(years, years, c=years) cbar = plt.colorbar(mappableDummy, ax=ax, format='%i') cbar.set_label('Year') ax.text(.1, .8, txt, fontsize=24, transform=ax.transAxes) ax.set_axis_off() figs.append(fig) filenames.append('example_network_%i' % count) count += 1 print result return figs, filenames
#------------------------------------------------------------------------------ print('\n\n======== PLOTTING COMBINED DEX CDFS ========') # Plot options color_map = {'Illumina_truseq': '#BD9020', 'Swift':'#315498', 'Swift_Rapid': '#A22382'} marker_map = {'10_ng': '+', '50_ng': 'x', '100_ng': '.', '200_ng': '^', '500_ng': None} # The path to data is like: <DEX_DIR>/<CONTROL GROUP>/<CASE GROUP>.csv control_group_paths = [DEX_RESULTS_DIR+'Illumina_truseq__500_ng', DEX_RESULTS_DIR+'Swift__100_ng', DEX_RESULTS_DIR+'Swift_Rapid__200_ng' ] (fig,ax) = plt.subplots(figsize=[9,3], sharex=True, sharey=True, nrows=1, ncols=3) for cg_path in control_group_paths: # the reference group control_group = cg_path.split('/')[-1] (control_lib, control_conc) = control_group.split('__') print('\nControl group:', control_group) dex_csvs = glob.glob(cg_path+'/*csv') for (k,dex_csv) in enumerate(dex_csvs): # string parsing label = os.path.basename(dex_csv).split('.')[0] (this_lib, this_conc) = label.split('__') if this_lib != control_lib: continue
def load_picoscope_rest_01122022(shot_number, maxrange=1, scopenum=5, time_range=[-6.0, 194.0], location='', plot=False): def butter_highpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = signal.butter(order, normal_cutoff, btype='highpass', analog=False) return b, a def butter_highpass_filter(data, cutoff, fs, order=5): b, a = butter_highpass(cutoff, fs, order=order) y = signal.filtfilt(b, a, data) return y if (type(scopenum) == int): if scopenum == 2: scopename = 'Pico2\\' elif scopenum == 3: scopename = 'Pico3\\' elif scopenum == 4: scopename = 'Pico4\\' else: scopename = 'Pico5\\' else: print(f'scopenum is not an int, {scopenum}') sys.exit() probe_dia = 0.003175 #m (1/8'' probe) probe_dia = 0.00158755 #m (1/16'' probe) ##hole_sep = 0.001016 #m (1/16''probe) ## Aparently unused variable r_probe_area = np.pi * (probe_dia / 2)**2 #tz_probe_area = probe_dia*hole_sep ## Aparently unused variable startintg_index = 0 #3000 meancutoff = 1000 location = 'C:\\Users\\Josh0\\Documents\\1. Josh Documents\\Graduate School - Bryn Mawr College\\Plasma Lab (BMX) Research\\Analysis\\Data\\2022\\01122022\\' filename = '20220112-0001 (' print(location + scopename + filename + str(shot_number) + ').mat') data = spio.loadmat(location + scopename + filename + str(shot_number) + ').mat', appendmat=True) dataraw = data Bdot3raw = dataraw['A'] Bdot4raw = dataraw['B'] Bdot5raw = dataraw['C'] Bdot6raw = dataraw['D'] start_time = dataraw['Tstart'] time_interval = dataraw['Tinterval'] iteration_array = np.array(range(1, 25001, 1)) time_interval_array = time_interval * iteration_array time_s = start_time + time_interval_array time_us = time_s * 1e6 timeB_s = time_s[:, 1:] timeB_us = time_us[:, 1:] Bdot3 = data['A'] - np.mean(data['A'][0:meancutoff]) neginfs = np.isneginf(Bdot3) Bdot3[np.where(neginfs)] = -maxrange posinfs = np.isposinf(Bdot3) Bdot3[np.where(posinfs)] = maxrange Bdot4 = data['B'] - np.mean(data['B'][0:meancutoff]) neginfs = np.isneginf(Bdot4) Bdot4[np.where(neginfs)] = -maxrange posinfs = np.isposinf(Bdot4) Bdot4[np.where(posinfs)] = maxrange Bdot5 = data['C'] - np.mean(data['C'][0:meancutoff]) neginfs = np.isneginf(Bdot5) Bdot5[np.where(neginfs)] = -maxrange posinfs = np.isposinf(Bdot5) Bdot5[np.where(posinfs)] = maxrange Bdot6 = data['D'] - np.mean(data['D'][0:meancutoff]) neginfs = np.isneginf(Bdot6) Bdot6[np.where(neginfs)] = -maxrange posinfs = np.isposinf(Bdot6) Bdot6[np.where(posinfs)] = maxrange B3_probe = np.array(Bdot3 / r_probe_area).T B4_probe = np.array(Bdot4 / r_probe_area).T B5_probe = np.array(Bdot5 / r_probe_area).T B6_probe = np.array(Bdot6 / r_probe_area).T B3 = sp.cumtrapz(B3_probe, time_us) * 1e4 #Gauss B4 = sp.cumtrapz(B4_probe, time_us) * 1e4 #Gauss B5 = sp.cumtrapz(B5_probe, time_us) * 1e4 #Gauss B6 = sp.cumtrapz(B6_probe, time_us) * 1e4 #Gauss B3filt = butter_highpass_filter(B3, 5e4, 125e6, order=3) B4filt = butter_highpass_filter(B4, 5e4, 125e6, order=3) B5filt = butter_highpass_filter(B5, 5e4, 125e6, order=3) B6filt = butter_highpass_filter(B6, 5e4, 125e6, order=3) if plot: fig, ax1 = plt.subplots(1) ax1.plot(timeB_us[0], B3filt[0]) ax1.plot(timeB_us[0], B4filt[0]) ax1.plot(timeB_us[0], B5filt[0]) ax1.plot(timeB_us[0], B6filt[0]) return time_s, time_us, timeB_s, timeB_us, Bdot3raw, Bdot4raw, Bdot5raw, Bdot6raw, Bdot3, Bdot4, Bdot5, Bdot6, B3, B4, B5, B6, B3filt, B4filt, B5filt, B6filt
def clean_data_BDC(df, lalo, d, mean_std, IQR, titlelabel): df.loc[:, 'flag_spike'] = 1 df_2 = df[df['temp_logged'] > -3] # second filter # plot_BDC(df,df_2,lalo,d,titlelabel,'Plot after second filter',IQR) print(mean_std, IQR.mean(), IQR.max()) if IQR.std() > 1: df_3 = df_2[(df_2['IQR'] < 5) & (df_2['stdday'] < 1)] # third filter # plot_BDC(df,df_3,lalo,d,titlelabel,'Plot after third filter',IQR) df_3['std'] = df_3['temp_logged'].rolling(5, center=True, min_periods=1).std() df_4 = df_3[ df_3['std'] < 0.8] # fourth filter rolls again 5 consecutives rows with cleaned data from the previousL filters # plot_BDC(df,df_4,lalo,d,titlelabel,'Plot after fourth filter',IQR) else: df_4 = df_2[df_2['std'] < 1] # third filter # plot_BDC(df,df_4,lalo,d,titlelabel,'Plot after third filter',IQR) idx_clean = df_4.index df.loc[~df.index.isin(idx_clean), 'flag_spike'] = 4 df_4['std'] = df_4['temp_logged'].rolling(5, center=True, min_periods=1).std() # df_5 = df_4[df_4['std'] < df_4['std'].quantile(0.98)] # fourth filter to smooth the data even more # plot_BDC(df,df_5,lalo,d,titlelabel,'Plot after fifth filter',IQR) if IQR.std() > 1: df_5 = df_4[df_4['std'] < df_4['std'].quantile( 0.98)] # fourth filter to smooth the data even more # plot_BDC(df,df_5,lalo,d,titlelabel,'Plot after fifth filter',IQR) df_5.loc[:, 'gap_time'] = df_5.index # - df_5.index.shift(1) df_5['first_time'] = (df_5['gap_time'].shift(-1) - df_5['gap_time']).dt.total_seconds() / 3600 / 24 df_5['last_time'] = (df_5['gap_time'] - df_5['gap_time'].shift(1) ).dt.total_seconds() / 3600 / 24 # display(df_5[df_5['first_time'] > 1]) first_date = df_5[df_5['first_time'] > 1.8].index[0] last_date = df_5[df_5['last_time'] > 1.8].index[-1] if IQR.max() < 15: df_5 = df_5[(df_5.index <= first_date) | (last_date <= df_5.index)] else: df_5 = df_5[df_5['temp_logged'] < 15] # df_5 = df_5[last_date <= df_5.index] else: df['temp_med'] = abs(df_4['temp_logged'] - df_4['temp_logged'].rolling( 5, center=True, min_periods=1).median()) df_4['temp_med'] = abs(df_4['temp_logged'] - df_4['temp_logged'].rolling( 5, center=True, min_periods=1).median()) # df_4['std_med'] = df_4['temp_med'].rolling(5, center=True, min_periods=1).std() fig, ax = plt.subplots(figsize=(15, 9)) ax.plot(df_4.index, df_4['temp_logged'], color='b', alpha=0.5, label="std data") # plots degF on right hand side # ax.plot(FFC_final.index,FFC_final['std'].rolling(5, center=True, min_periods=1).median(),color='black', label="data")# plots degF on right hand side ax.plot(df_4.index, df_4['temp_med'], color='r', label="IQR data", zorder=10) # plots degF on right hand side ax.set_ylabel('std') ax.set_xlabel('Time') plt.show() # display(df_4['temp_med'].describe()) print( 'Value to consider:', df_4['temp_med'].max() - (df_4['temp_med'].max() - df_4['temp_med'].quantile(0.98)) / 2) if df_4['temp_med'].max() - (df_4['temp_med'].max() - df_4['temp_med'].quantile(0.98)) / 2 > 1: df_4 = df_4[df_4['temp_med'] < 1] else: df_4 = df_4[df_4['temp_med'] < (df_4['temp_med'].max() - ( df_4['temp_med'].max() - df_4['temp_med'].quantile(0.98)) / 2)] df_5 = df_4.copy() idx_clean = df_5.index df.loc[(~df.index.isin(idx_clean)) & (df['flag_spike'] != 4), 'flag_spike'] = 3 return df
def plot_BDC(FFC, FFC_clean, lalo, d, title1, title2, IQR): def c2f(temp): """ Returns temperature in Celsius. """ return 1.8 * temp + 32 def convert_ax_f_to_fahrenheit(ax_c): """ Update second axis according with first axis. """ y1, y2 = ax_c.get_ylim() ax_f.set_ylim(c2f(y1), c2f(y2)) ax_f.figure.canvas.draw() # plots the original, the cleaned FFC (assumes temperature degC) and returns degF # "d" is the previously calculated movement of trap # puts the title (assumes fisherman's name) raw_data = FFC[FFC['temp_logged'].notnull( )] # pd.read_csv(asc_file,skiprows=skipr,parse_dates={'datet':[0,1]},index_col='datet',date_parser=parse,names=['Date','Time','temp_logged'],encoding= 'unicode_escape') fig, ax_c = plt.subplots(figsize=(15, 9)) ax_f = ax_c.twinx() # atuomatically update ylim of ax2 when ylim of ax1 changes ax_c.callbacks.connect("ylim_changed", convert_ax_f_to_fahrenheit) ax_c.plot(raw_data.index, raw_data['temp_logged'], '-', color='r', label="raw data", zorder=0) ax_c.set_ylabel('celsius') ax_c.set_xlabel('Time') ax_c.set_xlim( min(raw_data.index) - timedelta(hours=5), max(raw_data.index) + timedelta(hours=5)) # limit the plot to logged data ax_c.set_ylim( min(raw_data['temp_logged']) - 2, max(raw_data['temp_logged']) + 2) # display(FFC_clean[FFC_clean['flag_spike'] == 3]) try: ax_c.plot(FFC_clean[FFC_clean['flag_spike'] == 1].index, FFC_clean[FFC_clean['flag_spike'] == 1]['temp_logged'], color='b', label="clean data") # plots degF on right hand side ax_c.scatter( FFC_clean[FFC_clean['flag_logged_location'] != 1].index, FFC_clean[FFC_clean['flag_logged_location'] != 1]['temp_logged'], s=10, c='darkred', alpha=0.5, cmap='Wistia', label='flag location not recorded', zorder=100) # ,zorder=10) # plots flag depth ax_c.plot(lalo.index, lalo['temp'], 'c*', label='logged data', zorder=101) # plots logged data from LatLong file ax_c.scatter(FFC_clean[FFC_clean['flag_spike'] != 1].index, FFC_clean[FFC_clean['flag_spike'] != 1]['temp_logged'], c='red', s=10, cmap='Wistia', label="filtered data", zorder=502) # ax_c.scatter(FFC_clean[FFC_clean['flag_bathymetry'] != 1].index,FFC_clean[FFC_clean['flag_bathymetry'] != 1]['temp_logged'] ,c='cyan', alpha=0.1, s=10, cmap='Wistia',label="flag bathymetry",zorder=102) except: print('No good plot') ax_c.plot(FFC_clean.index, FFC_clean['temp_logged'], color='b', label="clean data") # plots degF on right hand side ax_c.plot(lalo.index, lalo['temp'], 'c*', label='logged data', zorder=101) # plots logged data from LatLong file ax_c.scatter(FFC.index, FFC['temp_logged'], c='red', s=10, cmap='Wistia', label="filtered data", zorder=102) FT = [FFC_clean['temp_logged']] LT = [np.array(lalo['temp'].values)] # logged temp in degC moveid = list( where(array(d) > 1.0)[0]) # movements logged greater than 1km dist = 3 tip = 0.5 c_move = 0 for kk in moveid: ax_c.annotate('%s' % float('%6.1f' % d[kk]) + ' kms', xy=(lalo.index[kk], LT[0][kk] + tip), xycoords='data', xytext=(lalo.index[kk], LT[0][kk] + dist), arrowprops=dict( facecolor='black', arrowstyle='->', connectionstyle="angle,angleA=0,angleB=90,rad=10"), horizontalalignment='left', verticalalignment='bottom') dist *= -1.2 tip *= -1 if c_move > 2: if dist > 0: dist = 3 else: dist = -3 c_move = 0 c_move += 1 # per_good = len(FFC_clean[(FFC_clean['flag_spike'] == 1) & (FFC_clean['flag_change_location'] == 1) & (FFC_clean['flag_change_depth'] == 1)])/len(FFC_clean) * 100 per_good = len(FFC_clean[(FFC_clean['flag_spike'] == 1)]) / len(FFC_clean) * 100 per_model = len( FFC_clean[FFC_clean['flag_bathy'] == 1]) / len(FFC_clean) * 100 savefig = True if per_good >= 99 and per_model > 50 and FFC_clean[ 'gebco'].mean() > 0 else False plt.suptitle(title1) plt.title(title2 + ' and %s' % float('%6.1f' % per_good) + '% of clean data respect to raw data') ax_f.set_ylabel('fahrenheit') fig.autofmt_xdate() ax_c.legend() if savefig: plt.savefig(path_save + 'Passed/' + year + '_FS' + LFAzone + '_' + str(int(FFC_final['gauge'].iloc[0])) + '_' + fisherman.replace(' ', '_') + '.png') else: plt.savefig(path_save + year + '_FS' + LFAzone + '_' + str(int(FFC_clean['gauge'].iloc[0])) + '_' + fisherman.replace(' ', '_') + '.png') plt.show() # Bathymetry plot fig, ax = plt.subplots(figsize=(15, 9)) ax.plot(FFC_final.index, -FFC_final['depth'], color='b', label="logged data") # plots degF on right hand side ax.plot(FFC_final.index, -FFC_final['gebco'], color='r', label="gebco data") # plots degF on right hand side # ax.plot(FFC_final.index,FFC_final['ngdc'],color='green',label="ngdc data")# plots degF on right hand side ax.set_ylabel('depth (m)') ax.set_xlabel('Time') # ax.set_xlim(min(imgtoup.index) - timedelta(hours=5),max(imgtoup.index) + timedelta(hours=5)) # limit the plot to logged data min_gebco = -FFC_final['gebco'].max() min_depth = -FFC_final['depth'].max() ax.set_ylim(min_gebco - 10, 20) if min_gebco < min_depth else ax.set_ylim( min_depth - 10, 20) plt.legend() if savefig: plt.savefig(path_save + 'Passed/' + year + '_FS' + LFAzone + '_' + str(int(FFC_final['gauge'].iloc[0])) + '_' + fisherman.replace(' ', '_') + '_bathymetry.png') else: plt.savefig(path_save + year + '_FS' + LFAzone + '_' + str(int(FFC_clean['gauge'].iloc[0])) + '_' + fisherman.replace(' ', '_') + '_bathymetry.png') plt.show() return savefig
cumulative_table[int(y_) - min_y][ journal_idx] = frac # + cumulative_table[int(y_)-min_y][journal_idx-1] journal_idx += 1 return cumulative_table, journal_to_idx_map if __name__ == "__main__": pubs_data = {} database_path = '../data/aps-dataset-metadata-abstracts-2016' # database_path = '../data_test' pubs_data = browse_papers(database_path, pubs_data) f, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 7)) top_journals = get_top_N(pubs_data, 6) tot_per_year = total_per_year(pubs_data) NN = len(top_journals) ctable, jmap = cumulative_freqs(tot_per_year, NN, pubs_data, top_journals) plt.subplot(1, 2, 1) plt.xlabel('Year', fontsize=20) plt.ylabel('Frequency', fontsize=20) m, n = ctable.shape for k_ in pubs_data.keys(): if k_ in top_journals: k_idx = jmap[k_]
meanI = np.zeros(max_frame); tic = time.time(); #'''MAIN LOOP STARTS''' for ind_frame in range(max_frame): if ind_frame % 100 == 0: toc = time.time(); print ind_frame, toc-tic; tic = toc; retval, image = vid.read() if not retval: break; # else: # meanI[ind_frame] = np.mean(image); # vid.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, INITIAL_FRAME); retval, image2 = vid.read() plt.figure() f, (ax1, ax2) = plt.subplots(1,2) ax1.imshow(image, interpolation = 'none', cmap = 'gray') ax2.imshow(image2, interpolation = 'none', cmap = 'gray') bgnd_fid.close() #mask_fid.close() #vid.release()
import matplotlib.pylab as plt import reda import reda.importers.eit_fzj as eit_fzj adc_data = eit_fzj.get_adc_data('data_eit_fzj_2013_ug/eit_data_mnu0.mat') with reda.CreateEnterDirectory('output_04_ug3'): frequencies = list( adc_data.swaplevel(0, 2).groupby('frequency').groups.keys()) # frequency to plot frequency = frequencies[1] fig, ax = plt.subplots(1, 1, figsize=(20 / 2.54, 25 / 2.54), sharex=True, sharey=True) subdata = adc_data.swaplevel(0, 2).loc[frequency] im = ax.imshow( subdata.swaplevel(0, 1, axis=1)['Ug3_1'].values, interpolation=None, ) ax.set_aspect('auto') ax.set_yticks(list(range(subdata.index.shape[0]))) ax.set_yticklabels(subdata.index.values) cb = fig.colorbar(im, ax=ax) cb.set_label(r'Ug [V]')
except np.linalg.linalg.LinAlgError: return 0 else: return 0 n = 10 eta = 1. size = 1000 P = lkj_random(n, eta, size) k = 0 for i, p in enumerate(P): k += is_pos_def(p) print("{0} % of the output matrix is positive definite.".format(k / size * 100)) import matplotlib.pylab as plt # Off diagnoal element C = P.transpose((1, 2, 0))[np.triu_indices(n, k=1)].T fig, ax = plt.subplots() ax.hist(C.flatten(), 100, normed=True) beta = eta - 1 + n / 2 C2 = 2 * stats.beta.rvs(size=C.shape, a=beta, b=beta) - 1 ax.hist(C2.flatten(), 100, normed=True, histtype='step', label='Beta() distribution') plt.legend(loc='upper right', frameon=False)
# Projection of field data onto eigenvector # (1st EOF should represent PDO mode) ####################################################################### t = 0 ntime = len(field_npac[:, 0, 0]) proj = np.zeros((ntime, nmodes)) while t < ntime: m = 0 while m < nmodes: proj[t, m] = proj_field(field_npac[t, :, :], field_eof[m, :, :]) m += 1 #print(t) t = t + 1 ds = save_result(proj, nc1.time, nc2.lev, copy_from_source=nc1[v]) if False: fig, ax = plt.subplots(2, 2) ax[0, 0].plot(nc1['time'], proj[:, MODE_PDO]) ax[0, 0].set_xlabel('PCA mode #') ax[0, 0].set_ylabel('projection index') ax[1, 0].contourf(nc2.lon[is_lon2], nc2.lat[is_lat2], field_eof[MODE_PDO, :, :], cmap=plt.cm.coolwarm) plt.show() i = i + 1 os.system("mv proj.nc " + OUTPATH + outfile) print("outfile: " + OUTPATH + outfile) nmodel += 1 print("done")
for m in range(-r, r): for n in range(-r, r): if blueq(0, 0, m, n) == 0: continue x = f(redq(0, 0, m, n), 1) y = f(greenq(0, 0, m, n), 1) x2 = f(redq(0, 0, m, n), blueq(0, 0, m, n)) y2 = f(greenq(0, 0, m, n), blueq(0, 0, m, n)) xs += [x] ys += [y] xs2 += [x2] ys2 += [y2] max = max(xs + ys) for i in range(0, len(xs)): xs[i] = 2 * f(xs[i], max) ys[i] = 2 * f(ys[i], max) print len(xs), 'points' import numpy as np import matplotlib.pylab as plt fig, ax = plt.subplots(figsize=(8, 8)) ax.set_ylim([-1.2, 1.2]) ax.set_xlim([-1.2, 1.2]) for i in range(0, len(xs)): xs[i] = xs[i] #+zs[i]/4 ys[i] = ys[i] #+zs[i]/4 ax.scatter(xs + xs2, ys + ys2) plt.show()
def visualize(self, compare=False, plotname=None, f=None, ax=None, usez=None, usecuts=None, **kwargs): mdens = (self.densbins[1:] + self.densbins[:-1]) / 2 if usez is None: usez = range(self.cdenspdf.shape[2]) if usecuts is None: usecuts = range(self.cdenspdf.shape[1]) ncuts = len(usecuts) nz = len(usez) if f is None: f, ax = plt.subplots(ncuts, nz, sharex=True, sharey=False, figsize=(8, 8)) ax = np.array(ax) ax = ax.reshape((ncuts, nz)) newaxes = True else: newaxes = False for i in range(nz): for j in range(ncuts): l1 = ax[j, i].semilogx(mdens, self.cdenspdf[:, j, i], **kwargs) if newaxes: sax = f.add_subplot(111) sax.patch.set_alpha(0.0) sax.patch.set_facecolor('none') sax.spines['top'].set_color('none') sax.spines['bottom'].set_color('none') sax.spines['left'].set_color('none') sax.spines['right'].set_color('none') sax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') if 'xlabel' in kwargs: sax.set_xlabel(kwargs['xlabel']) else: sax.set_xlabel(r'Density') if 'ylabel' in kwargs: sax.set_ylabel(kwargs['ylabel']) else: sax.set_ylabel(r'p(density|magnitude>M)') if (plotname is not None) & (not compare): plt.savefig(plotname) return f, ax, l1
def visualize(self, compare=False, plotname=None, f=None, ax=None, usez=None, colors=None, ncont=None, **kwargs): mdens = (self.densbins[1:] + self.densbins[:-1]) / 2 mmag = (self.magbins[1:] + self.magbins[:-1]) / 2 X, Y = np.meshgrid(mdens, mmag) if usez is None: usez = range(self.densmagpdf.shape[2]) nz = len(usez) if ncont is None: ncont = 5 if f is None: f, ax = plt.subplots(self.nzbins, sharex=True, sharey=True, figsize=(8, 8)) ax = np.array(ax) newaxes = True else: newaxes = False for i in range(nz): try: l1 = ax[i].contour(X, Y, self.densmagpdf[:, :, i].T, ncont, colors=colors, **kwargs) except ValueError as e: print('Caught error {0}'.format(e)) pass if newaxes: sax = f.add_subplot(111) sax.patch.set_alpha(0.0) sax.patch.set_facecolor('none') sax.spines['top'].set_color('none') sax.spines['bottom'].set_color('none') sax.spines['left'].set_color('none') sax.spines['right'].set_color('none') sax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') if 'xlabel' in kwargs: sax.set_xlabel(kwargs['xlabel']) else: sax.set_xlabel(r'Density') if 'ylabel' in kwargs: sax.set_ylabel(kwargs['ylabel']) else: sax.set_ylabel(r'Magnitude') if (plotname is not None) & (not compare): plt.savefig(plotname) return f, ax, l1
fileUrl = 'wineQuality.csv' #'irisFlower.csv'#input_boston_wPrice.csv'#'irisFlower.csv'#'circle.csv' data = pd.read_csv(fileUrl).values metricpar = {'metric': metric[0]} ecc = filterFuncs['eccentricity'](data, metricpar=metricpar) gaus = filterFuncs['Gauss_density'](data, sigma=1.0, metricpar=metricpar) knn = filterFuncs['kNN_distance'](data, k=2, metricpar=metricpar) d2m = filterFuncs['distance_to_measure'](data, k=2, metricpar=metricpar) pca = filterFuncs['dm_eigenvector'](data, metricpar=metricpar) y = [1] * len(ecc) bins = 150 f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=False, sharey=False) #ax1.scatter(ecc, y, s= 8, marker='|') ax1.hist(ecc, bins) ax1.set_title('eccentricity_%s' % fileUrl) #ax2.scatter(gaus, y,s= 8, marker='|') ax2.hist(gaus, bins) ax2.set_title('Gauss_density') #ax3.scatter(knn, y,s= 8, marker='|') ax3.hist(knn, bins) ax3.set_title('kNN_distance') #ax4.scatter(pca, y,s= 8, marker='|') ax4.hist(pca, bins) ax4.set_title('PCA')
with PdfPages(pdf_fh) as pdf: for jj in np.arange(0, nord, dtype=np.long): hull_tck = convex_hull_removal(spec_w[jj, m], spec_f[jj, m]) hull_fit = splev(spec_w[jj, m], hull_tck) # mid = np.long(len(spec_f[jj, m]) / 2.) ratio = np.percentile(hull_fit, 98) try: # blaze[jj, :] = convolve(blaze[jj, :], Box1DKernel(1)) # Blaze peaks just right of the center ratio /= np.max(blaze[jj, :]) fit = ratio * blaze[jj, :] except: ratio /= np.max(blaze[jj - 1, :]) fit = ratio * blaze[jj - 1, :] flat[jj, :] = spec_f[jj, :] - fit fig, ax = plt.subplots(2, 1, sharex=True, figsize=(11.5, 8)) ax[0].plot(spec_w[jj, :], spec_f[jj, :], label='data') ax[0].plot(spec_w[jj, :], fit, label='Fit') ax[0].legend(loc='upper left') ax[0].set_ylabel("IGRINS Flux") ax[0].set_title("%s - order %02d" % (fh, orders[jj])) ax[1].plot(spec_w[jj, :], flat[jj, :], label='Residuals') ax[1].axhline(1, lw=2, ls='--', c='k') ax[1].set_xlabel("Wavelength (microns)") ax[1].set_ylabel("Residual Flux") fig.tight_layout() pdf.savefig() plt.close() spec.append(fits.PrimaryHDU(flat)) spec.writeto(fh.replace('spec', 'flat'), overwrite=True) print('Wrote %s to disk' % fh.replace('spec', 'flat'))
print() # blank line, end of headers print("<H1>Error</H1>") print("Please fill in the required fields.") else: print("Content-Type: image/png") # HTML is following print() # blank line, end of headers # Reading the input variables from the user Vo = float(form["Vo"].value) L = float(form["L"].value) val = np.sqrt(2.0*9.10938356e-31*1.60217662e-19)*1e-10/(2.0*1.05457180013e-34) # equal to sqrt(2m*1eV)*1A/(2*hbar) # Generating the graph plt.rcParams.update({'font.size': 18, 'font.family': 'STIXGeneral', 'mathtext.fontset': 'stix'}) fig, axes = plt.subplots(1, 2, figsize=(13,4)) axes[0].axis([0.0,Vo,0.0,np.sqrt(Vo)*1.8]) axes[0].set_xlabel(r'$E$ (eV)') axes[0].set_ylabel(r'(eV$^{-1}$)') axes[0].set_title('Even solutions') axes[1].axis([0.0,Vo,0.0,np.sqrt(Vo)*1.8]) axes[1].set_xlabel(r'$E$ (eV)') axes[1].set_ylabel(r'') axes[1].set_title('Odd solutions') E = np.linspace(0.0, Vo, 10000) num = int(round((L*np.sqrt(Vo)*val-np.pi/2.0)/np.pi)) # Removing discontinuity points for n in range(10000): for m in range(num+2): if abs(E[n]-((2.0*float(m)+1.0)*np.pi/(2.0*L*val))**2)<0.01: E[n] = np.nan if abs(E[n]-(float(m)*np.pi/(L*val))**2)<0.01: E[n] = np.nan
total.append(values[0]) total.append(values2[0]) total.append(values[1]) total.append(values2[1]) total.append(values[2]) total.append(values2[2]) total.append(values[3]) total.append(values2[3]) total.append(values[4]) total.append(values2[4]) # fill with colors colors = ["lightgreen", "lightblue", "lightgreen", "lightblue", "lightgreen", "lightblue", "lightgreen", "lightblue", "lightgreen", "lightblue"] ticks = STRATEGIES fig, axes = plt.subplots() boxplot = axes.boxplot(total, patch_artist=True, widths=0.4, positions = [0.7, 1.3, 2.7, 3.3, 4.7, 5.3, 6.7, 7.3, 8.7, 9.3]) plt.xticks(x_pos + 1, STRATEGIES) plt.xticks(range(1, len(ticks) * 2, 2), ticks) for patch, color in zip(boxplot['boxes'], colors): patch.set_facecolor(color) custom_lines = [Line2D([0], [0], color="lightgreen", lw=4), Line2D([0], [0], color="lightblue", lw=4)] # adding horizontal grid lines axes.yaxis.grid(True) axes.set_title("Average number of people with a specific strategy, runs=65") axes.set_xlabel('Strategy') axes.set_ylabel('Number of people')
def compute_drag_polar(Mach, alphas, surfaces, trimmed=False): if isinstance(surfaces, dict): surfaces = [ surfaces, ] # Create the OpenMDAO problem prob = om.Problem() # Create an independent variable component that will supply the flow # conditions to the problem. indep_var_comp = om.IndepVarComp() indep_var_comp.add_output('v', val=248.136, units='m/s') indep_var_comp.add_output('alpha', val=0.0, units='deg') indep_var_comp.add_output('Mach_number', val=Mach) indep_var_comp.add_output('re', val=1.0e6, units='1/m') indep_var_comp.add_output('rho', val=0.38, units='kg/m**3') indep_var_comp.add_output('cg', val=np.zeros((3)), units='m') # Add this IndepVarComp to the problem model prob.model.add_subsystem('prob_vars', indep_var_comp, promotes=['*']) for surface in surfaces: name = surface['name'] # Create and add a group that handles the geometry for the # aerodynamic lifting surface geom_group = Geometry(surface=surface) prob.model.add_subsystem(name, geom_group) # Connect the mesh from the geometry component to the analysis point prob.model.connect(name + '.mesh', 'aero.' + name + '.def_mesh') # Perform the connections with the modified names within the # 'aero_states' group. prob.model.connect(name + '.mesh', 'aero.aero_states.' + name + '_def_mesh') # Create the aero point group, which contains the actual aerodynamic # analyses point_name = 'aero' aero_group = AeroPoint(surfaces=surfaces) prob.model.add_subsystem( point_name, aero_group, promotes_inputs=['v', 'alpha', 'Mach_number', 're', 'rho', 'cg']) # For trimmed polar, setup balance component if trimmed == True: bal = om.BalanceComp() bal.add_balance(name='tail_rotation', rhs_val=0.0, units='deg') prob.model.add_subsystem('balance', bal, promotes_outputs=['tail_rotation']) prob.model.connect('aero.CM', 'balance.lhs:tail_rotation', src_indices=[1]) prob.model.connect('tail_rotation', 'tail.twist_cp') prob.model.nonlinear_solver = om.NonlinearBlockGS(use_aitken=True) prob.model.nonlinear_solver.options['iprint'] = 2 prob.model.nonlinear_solver.options['maxiter'] = 100 prob.model.linear_solver = om.DirectSolver() prob.setup() # prob['tail_rotation'] = -0.75 prob.run_model() # prob.check_partials(compact_print = True) # prob.model.list_outputs(prom_name = True) prob.model.list_outputs(residuals=True) CLs = [] CDs = [] CMs = [] for a in alphas: prob['alpha'] = a prob.run_model() CLs.append(prob['aero.CL'][0]) CDs.append(prob['aero.CD'][0]) CMs.append(prob['aero.CM'][1]) # Take only the longitudinal CM # print(a, prob['aero.CL'], prob['aero.CD'], prob['aero.CM'][1]) # Plot CL vs alpha and drag polar fig, axes = plt.subplots(nrows=3) axes[0].plot(alphas, CLs) axes[1].plot(alphas, CMs) axes[2].plot(CLs, CDs) fig.savefig('drag_polar.pdf') # plt.show() return CLs, CDs, CMs
nu_param = [] rho_param = [] delta_time = [] for i in range(1, no_dates): delta_time.append( (float(parameters['date'][i]) - float(parameters['value_date'][i])) / 365.0) nu_param.append(float(parameters['nu'][i])) rho_param.append(float(parameters['rho'][i])) index = np.arange(0, no_dates) maturities = [44183, 44365, 44547, 44729, 44911, 45275, 46010, 46738, 47473] no_maturities = len(maturities) fig, axs = plt.subplots(3, 3, figsize=(20, 20)) for i in range(0, 3): date_str = str(ql.Date(maturities[i])) date_str = date_str.replace('th', '') axs[0, i].plot(z_i, sabr_iv_map[maturities[i]], linestyle='dashed', color='black', marker='.') axs[0, i].set_title(date_str, fontsize=18) axs[0, i].set_xlabel('ln(f/k)') axs[0, i].set_ylabel('iv') axs[0, i].set_ylim([0.0, 0.6]) axs[0, i].axes.xaxis.label.set_size(20)
data = [] for file in all_files: pp = np.load(file) pp = np.mean(pp, axis=0) data.append(pp) data = np.array(data) times = np.linspace(-0.6, 2.6, num=801) minimax = (0.25, 0.5, 0.75) diag_sem = sem(data, axis=0) diag_mean = np.mean(data, axis=0) fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(times, diag_mean) ax.fill_between(times, diag_mean - diag_sem, diag_mean + diag_sem, alpha=0.2, linewidth=0) ax.axhline(0.5, linewidth=.5, linestyle='-', color='black') ax.axvline(0, linewidth=.5, linestyle='--', color='black') ax.axvline(1.5, linewidth=.5, linestyle='--', color='black') ax.axvline(1.6, linewidth=.5, linestyle='--', color='black') ax.set_ylabel("Classification performance [Accuracy]") ax.set_xlabel("Training Time (s)")
p_test = proba2(x_test) x_init = rdm.uniform(*x_range, nb_init)[:, np.newaxis] y_init = counts(x_init, underlying_p = proba2, nb_measures=nb_meas) k_classi = GPy.kern.Matern52(input_dim = 1, variance = 1., lengthscale = (x_range[1]-x_range[0])/25) i_meth = GPy.inference.latent_function_inference.Laplace() lik = GPy.likelihoods.Binomial() m_classi = GPy.core.GP(X=x_init, Y=y_init, kernel=k_classi, inference_method=i_meth, likelihood=lik, Y_metadata = {'trials':np.ones_like(y_init) * nb_meas}) #m_classi['.*lengthscale'].constrain_bounded(0.10, 1., warning = False) _ = m_classi.optimize_restarts(num_restarts=5) #a, b, c, d, d_range, s = predict_p(m_classi, x_test) a, b, c, d, d_range, s = predict_p_v2(m_classi, x_test) fig, (ax1, ax2) = plt.subplots(2, 1,gridspec_kw={'hspace': 0.3,'height_ratios': [3, 1]}) #fig, ((ax1, ax3, ax5), (ax2, ax4, ax6)) = plt.subplots(2, 3, sharey=True,gridspec_kw={'hspace': 0.3,'wspace':0.05,'height_ratios': [3, 1]}) ax1.plot(x_test, a, color = col_custom, linewidth = 0.8, label = r'$model$') #plt.plot(x_test, a, color = col_custom, linewidth = 0.8, label = r'$\bar{f}$') ax1.plot(x_test, b, color = col_custom, alpha = 0.5, linewidth = 0.4) ax1.plot(x_test, c, color = col_custom, alpha = 0.5, linewidth = 0.4) ax1.imshow(np.power(d[np.arange(len(d)-1,-1, -1)]/np.max(d), 0.75), cmap = 'Blues', aspect='auto', interpolation='spline16', extent = (x_range[0], x_range[1], d_range[0], d_range[1]), alpha=1) ax1.scatter(x_init, y_init/nb_meas, label = r'$Observations$', marker = 'o', c='red', s=points_size) ax1.plot(x_test, p_test, 'r--', label='F') acq = (a + weight * s) ax2.plot(x_test, acq,color='r') x_next = next_ucb_std_v2(m_classi, x_test, weight) ax2.vlines(x_next, 0., np.max(acq), colors='r', linestyles='dashed') ax1.set_ylim([-0.05, 1.05]) ax1.set_xlim([-0.02, 4.02]) ax2.set_xlim([-0.02, 4.02])
omCI = e * B0 / mi cS = np.sqrt(Te0 / mi) rhoS = cS / omCI # From normalization of parallel current equation factor = (omCI**2) * n0 # Obtain the suptitle suptitle = fig.texts[0].get_text() # Close the figure plt.close(fig) #}}} # Make new ax to plot to fig, (normalAx ,nnAx) = plt.subplots(ncols=2,\ figsize = SizeMaker.array(2,1, aSingle=0.5)) size = "large" #{{{Extract and plot nn # Find the min and the max maxmin = [] for line in oldNnAx.get_lines(): x, y = line.get_data() x *= rhoS y *= factor maxmin.append((np.max(y), np.min(y))) nnAx.plot(x, y, color=line.get_color()) # Set the texts maxInd = np.argmax(tuple(curVal[0] for curVal in maxmin)) minInd = np.argmin(tuple(curVal[1] for curVal in maxmin))
def get_files(directory_path): dirpath = directory_path files = [ f for f in listdir(dirpath) if (isfile(join(dirpath, f)) and ".npy" in f) ] files = sorted(files) n_files = len(files) print("number of files=" + str(n_files)) return files, n_files fig1, ax1 = plt.subplots(1, 1, figsize=(10, 6)) fig2, ax2 = plt.subplots(1, 1, figsize=(10, 6)) ax1.set_yscale('log') ax2.set_yscale('log') ax1.grid(True) ax2.grid(True) colors = iter(cm.gist_ncar(np.linspace(0, 1, 8))) dirpath = "../outputpy" files, n_files = get_files(dirpath) for j, file_iter in enumerate(files): print(j, file_iter) if (("_pedsub_BPM" in file_iter) and ('40deg' not in file_iter) and ('20deg' not in file_iter)):
NGD_kernel, vjp = optimal_JJT(outputs, sampled_y, grad_org, acc_test, acc_hard_test) NGD_inv = torch.linalg.inv(NGD_kernel + DAMPING * torch.eye(BATCH_SIZE)) v = torch.matmul(NGD_inv, vjp.unsqueeze(1)) ####### rescale v: v_sc = v / (BATCH_SIZE * DAMPING) # plotting NGD kernel for some iterations if PLOT and batch_idx in [2, 10, 50, 500]: JJT_opt, JJT_linear, JJT_conv = optimal_JJT() fig, ax = plt.subplots() im = ax.imshow(JJT_opt, cmap='viridis') fig.colorbar(im, orientation='horizontal') plt.show() fig, ax = plt.subplots() im = ax.imshow(JJT_linear, cmap='viridis') fig.colorbar(im, orientation='horizontal') plt.show() fig, ax = plt.subplots() im = ax.imshow(JJT_conv, cmap='viridis') fig.colorbar(im, orientation='horizontal') plt.show() # fig.suptitle('NGD Kernel')
def makePlots(plot1=False, plot2=False, plot3=False, plot4=False): # Read in all the data figs = [] names = [] data = readYear(0, filename='output/all_years.dat') min_year = 1997 # Make a histogram of number of PhDs per year good = np.where((data['noAstroJournal'] == 'None') & (data['nonUS'] == 'None') & (data['phd_year'] >= min_year)) bins = np.arange(data['phd_year'][good].min() - .5, data['phd_year'][good].max() + 1.5, 1) if plot1: fig, ax = plt.subplots() blah = ax.hist(data['phd_year'][good], bins) ax.set_xlabel('Year') ax.set_ylabel('Number of US Astro PhDs') figs.append(fig) names.append('phdsperyear') if plot2: filename = 'active_curves' title = 'All PhDs' fig, name = curvePlot(data, good, filename, title) figs.extend(fig) names.extend(name) if plot3: filename = 'active_curves_uname' good = np.where((data['noAstroJournal'] == 'None') & (data['nonUS'] == 'None') & (data['uniqueName'] == 'True')) title = 'Unique PhD names' fig, name = curvePlot(data, good, filename, title) figs.extend(fig) names.extend(name) # Let's plot the retention at 3,6,10 years as a function of cohort if plot4: good = np.where((data['noAstroJournal'] == 'None') & (data['nonUS'] == 'None')) years = np.unique(data['phd_year']) retYears = [4, 6, 10] results = np.zeros((years.size, np.size(retYears)), dtype=float) lastYearDist = data['latest_year'][good] - data['phd_year'][good] for i, year in enumerate(years): inyear = np.where(data['phd_year'][good] == year) nInYear = np.size(inyear[0]) for j, retYear in enumerate(retYears): remaining = np.size( np.where(lastYearDist[inyear] >= retYear)[0]) results[i, j] = remaining / float(nInYear) fig, ax = plt.subplots() for i, year in enumerate(retYears): ack = np.where(results[:, i] != 0)[0][:-1] blah = ax.plot(years[ack], results[ack, i], label='%i years' % year) ax.set_xlabel('PhD Cohort Year') ax.set_ylabel('Fraction Active') ax.legend(loc='upper right') figs.append(fig) names.append('retention_by_class') return figs, names