def plot_adsorbed_circles(adsorbed_x,adsorbed_y,radius, width, label=""): import pylab from matplotlib.patches import Circle # Plot each run fig = pylab.figure() ax = fig.add_subplot(111) for p in range(len(adsorbed_x)): ax.add_patch(Circle((adsorbed_x[p], adsorbed_y[p]), radius)) # Plot "image" particles to verify that periodic boundary conditions are working # if adsorbed_x[p] < radius: # ax.add_patch(Circle((adsorbed_x[p] + width,adsorbed_y[p]), radius, facecolor='red')) # elif adsorbed_x[p] > (width-radius): # ax.add_patch(Circle((adsorbed_x[p] - width,adsorbed_y[p]), radius, facecolor='red')) # if adsorbed_y[p] < radius: # ax.add_patch(Circle((adsorbed_x[p],adsorbed_y[p] + width), radius, facecolor='red')) # elif adsorbed_y[p] > (width-radius): # ax.add_patch(Circle((adsorbed_x[p],adsorbed_y[p] - width), radius, facecolor='red')) ax.set_aspect(1.0) pylab.axhline(y=0, color='k') pylab.axhline(y=width, color='k') pylab.axvline(x=0, color='k') pylab.axvline(x=width, color='k') pylab.axis([-0.1*width, width*1.1, -0.1*width, width*1.1]) pylab.xlabel("non-dimensional x") pylab.ylabel("non-dimensional y") pylab.title("Adsorbed particles at theta="+label) return ax
def rmsdSpreadSubplot(multiplier=1.0, layout=(-1, -1)): rmsd_data = dict( (e, rad_data[e]['innov'][quant]) for e in rad_data.iterkeys() ) spread_data = dict( (e, rad_data[e]['spread'][quant]) for e in rad_data.iterkeys() ) times = temp.getTimes() n_t = len(times) for exp, exp_name in exp_names.iteritems(): pylab.plot(sawtooth(times, times)[:(n_t + 1)], rmsd_data[exp][:(n_t + 1)], color=colors[exp], linestyle='-') pylab.plot(times[(n_t / 2):], rmsd_data[exp][n_t::2], color=colors[exp], linestyle='-') for exp, exp_name in exp_names.iteritems(): pylab.plot(sawtooth(times, times)[:(n_t + 1)], spread_data[exp][:(n_t + 1)], color=colors[exp], linestyle='--') pylab.plot(times[(n_t / 2):], spread_data[exp][n_t::2], color=colors[exp], linestyle='--') ylim = pylab.ylim() pylab.plot(times, -1 * np.ones((len(times),)), color='#999999', linestyle='-', label="RMS Innovation") pylab.plot(times, -1 * np.ones((len(times),)), color='#999999', linestyle='--', label="Spread") pylab.axhline(y=7, color='k', linestyle=':') pylab.axvline(x=14400, color='k', linestyle=':') pylab.ylabel("RMS Innovation/Spread (dBZ)", size='large') pylab.xlim(times[0], times[-1]) pylab.ylim(ylim) pylab.legend(loc=4) pylab.xticks(times[::2], [ "" for t in times[::2] ]) pylab.yticks(size='x-large') return
def plot_mads(allsigs, names, loc='upper left'): allsigs = np.array(allsigs) allsigs = np.atleast_2d(allsigs) #print('Allsigs shape', allsigs.shape) rsigs = allsigs[:,np.array([-1])] allsigs = allsigs[:,:-1] dsigs = allsigs[:,::3] xsigs = allsigs[:,1::3] ysigs = allsigs[:,2::3] dsigs = np.hstack((dsigs, rsigs)) nr,nc = xsigs.shape plt.clf() p1 = plt.plot(dsigs.T, 'bo-', label='Diagonal') p2 = plt.plot(np.repeat(np.arange(nc)[:,np.newaxis], 2, axis=1), xsigs.T, 'go-', label='X') p3 = plt.plot(np.repeat(np.arange(nc)[:,np.newaxis], 2, axis=1), ysigs.T, 'ro-', label='Y') plt.xticks(np.arange(nc+1), ['%i pix'%o for o in offsets] + ['Random']) plt.axhline(1., color='k', alpha=0.3) plt.ylabel('Error estimate / sig1') plt.legend([p1[0],p2[0],p3[0]], ['Diagonal', 'X', 'Y'], loc=loc) names = np.array(names) yy = dsigs[:,-1] ii = np.argsort(yy) yy = np.linspace(min(yy), max(yy), len(ii)) for i,y in zip(ii, yy): plt.text(nc+0.1, y, names[i], ha='left', va='center', color='b', fontsize=10) plt.xlim(-0.5, nc+3)
def imshow_box(f,im, x,y,s): '''imshow_box(f,im, x,y,s) f: figure im: image x: center coordinate for box y: center coord s: box shape, (width, height) ''' global coord P.figure(f.number) P.clf(); P.imshow(im); P.axhline(y-s[1]/2.) P.axhline(y+s[1]/2.) P.axvline(x-s[0]/2.) P.axvline(x+s[0]/2.) xy=crop(m,s,y,x) coord=(0.5*(xy[2]+xy[3]), 0.5*(xy[0]+xy[1])) P.title(str('x: %d y: %d' % (x,y))); P.figure(999); P.imshow(master[xy[0]:xy[1],xy[2]:xy[3]]) P.title('Master'); P.figure(998); df=(master[xy[0]:xy[1],xy[2]:xy[3]]-slave) P.imshow(np.abs(df)) P.title(str('RMS: %0.6f' % np.sqrt((df**2.).mean()) ));
def plot_vel_vs_h3(maps, out_suffix=''): xaxis = (np.arange(len(maps.velocity[0])) - ppxf_m31.bhpos_pix[0]) * 0.05 yaxis = (np.arange(len(maps.velocity)) - ppxf_m31.bhpos_pix[1]) * 0.05 yy, xx = np.meshgrid(xaxis, yaxis) radius = np.hypot(xx, yy) good = np.where((np.abs(yy) < 0.5) & (np.abs(xx) < 1.0)) plt.scatter(maps.velocity[good], maps.h3[good], c=maps.sigma[good], s=5, marker='o', vmin=0, vmax=450) plt.xlim(-700, 0) plt.ylim(-0.5, 0.5) plt.colorbar(label='Sigma (km/s)') plt.axhline(linestyle='--', color='grey') plt.xlabel('Velocity (km/s)') plt.ylabel('h3') plt.savefig(plot_dir + 'vel_vs_h3' + out_suffix + '.png') plt.clf() plt.scatter(maps.sigma[good], maps.h3[good], c=maps.velocity[good], s=5, marker='o', vmin=-700, vmax=0) plt.xlim(0, 450) plt.ylim(-0.5, 0.5) plt.colorbar(label='Velocity (km/s)') plt.axhline(linestyle='--', color='grey') plt.xlabel('Sigma (km/s)') plt.ylabel('h3') plt.savefig(plot_dir + 'sig_vs_h3' + out_suffix + '.png') return
def test(): from pandas import DataFrame X = np.linspace(0.01, 1.0, 10) Y = np.log(X) Y -= Y.min() Y /= Y.max() Y *= 0.95 #Y = X df = DataFrame({'X': X, 'Y': Y}) P = Pareto(df, 'X', 'Y') data = [] for val in np.linspace(0,1,15): data.append(dict(val=val, x=P.lookup_x(val), y=P.lookup_y(val))) pl.axvline(val, alpha=.5) pl.axhline(val, alpha=.5) dd = DataFrame(data) pl.scatter(dd.y, dd.val, lw=0, c='r') pl.scatter(dd.val, dd.x, lw=0, c='g') print dd #P.scatter(c='r', lw=0) P.show_frontier(c='r', lw=4) pl.show()
def equil(): eq_m = -0.0001 rise_rate = 0.0125 mag = 20 ts = linspace(0, 400, 500) f = F(ts, mag=mag) # g = eq_m * f g = G(ts, f, rate=eq_m) b = rise_rate * ts fg = f + g + b plot(ts, f, label='F Equilibration') plot(ts, g, label='G Consumption') plot(ts, fg, label='F-G (Sniff Evo)') rf, rollover_F, vi_F = calc_optimal_eqtime(ts, f) rfg, rollover_FG, vi_FG = calc_optimal_eqtime(ts, fg) m = 2 * eq_m * mag axvline(x=rollover_F, ls='--', color='blue') axvline(x=rollover_FG, ls='--', color='red') idx = list(ts).index(rollover_F) b = fg[idx] - m * rollover_F evo = polyval((m, b), ts) plot(ts, evo, ls='-.', color='blue', label='Static Evo. A') # ee = where(evo > mag)[0] # to_F = ts[max(ee)] # print 'F', rollover_F, b, to_F b = vi_FG - m * rollover_FG evo = polyval((m, b), ts) plot(ts, evo, ls='-.', color='red', label='Static Evo. B') print polyval((m, b), 200) ee = where(evo > mag)[0] # to_FG = ts[max(ee)] # print 'FG', rollover_FG, b, to_FG # axvline(x=to_FG, ls='-', color='red') # axvline(x=to_F, ls='-', color='blue') axhline(y=mag, ls='-', color='black') # plot([ti], [mag], 'bo') legend(loc=0) # ylim(2980, 3020) ylim(18, 21) xlim(0, 20) ylabel('Intensity') xlabel('t (s)') # fig = gcf() # fig.text(0.1, 0.01, 'asdfasfasfsadfsdaf') show()
def _show_rates(rate, wo, wt, attenuator, tau_NP, tau_P): import pylab #pylab.figure() pylab.errorbar(rate, wt[0], yerr=wt[1], fmt='g.', label='attenuated') pylab.errorbar(rate, wo[0], yerr=wo[1], fmt='b.', label='unattenuated') pylab.xscale('log') pylab.yscale('log') pylab.xlabel('incident rate (counts/second)') pylab.ylabel('observed rate (counts/second)') pylab.legend(loc='best') pylab.grid(True) pylab.plot(rate, rate/attenuator, 'g-', label='target') pylab.plot(rate, rate, 'b-', label='target') Ipeak, Rpeak = peak_rate(tau_NP=tau_NP, tau_P=tau_P) if rate[0] <= Ipeak <= rate[-1]: pylab.axvline(x=Ipeak, ls='--', c='b') pylab.text(x=Ipeak, y=0.05, s=' %g'%Ipeak, ha='left', va='bottom', transform=pylab.gca().get_xaxis_transform()) if False: pylab.axhline(y=Rpeak, ls='--', c='b') pylab.text(y=Rpeak, x=0.05, s=' %g\n'%Rpeak, ha='left', va='bottom', transform=pylab.gca().get_yaxis_transform())
def playgame(): grid = g.Grid() score = 0 learner = Learner(grid.getNumRows(), grid.getNumCols(), grid.getMaxScore()) values = learner.learn(grid) print "Values:", values while grid.reward(score) == 0: grid.printGrid() x, y = learner.action(score) print "aiming for", x, y round = grid.noisyThrow(x, y) score += round print "You got ",round," Current score:", score pylab.plot(values) pylab.axhline(y=0.54, xmin=0, xmax=grid.getMaxScore(), color="r") pylab.title("Value by State") pylab.xlabel("State") pylab.ylabel("Value") pylab.savefig('values.pdf', format='PDF') #pylab.show() if grid.reward(score) == -1: print "You lose." return if grid.reward(score) == 1: print "You win!" return
def show_table(table_name,ls="none", fmt="o", legend=False, name="m", do_half=0): bt = fi.FITS(table_name)[1].read() rgpp = (np.unique(bt["rgp_lower"])+np.unique(bt["rgp_upper"]))/2 nbins = rgpp.size plt.xscale("log") colours=["purple", "forestgreen", "steelblue", "pink", "darkred", "midnightblue", "gray", "sienna", "olive", "darkviolet"] pts = ["o", "D", "x", "^", ">", "<", "1", "s", "*", "+", "."] for i,r in enumerate(rgpp): sel = (bt["i"]==i) snr = 10** ((np.log10(bt["snr_lower"][sel]) + np.log10(bt["snr_upper"][sel]))/2) if do_half==1 and i>nbins/2: continue elif do_half==2 and i<nbins/2: continue if legend: plt.errorbar(snr, bt["%s"%name][i*snr.size:(i*snr.size)+snr.size], bt["err_%s"%name][i*snr.size:(i*snr.size)+snr.size], color=colours[i], ls=ls, fmt=pts[i], lw=2.5, label="$R_{gpp}/R_p = %1.2f-%1.2f$"%(np.unique(bt["rgp_lower"])[i],np.unique(bt["rgp_upper"])[i])) else: plt.errorbar(snr, bt["%s"%name][i*snr.size:(i*snr.size)+snr.size], bt["err_%s"%name][i*snr.size:(i*snr.size)+snr.size], color=colours[i], ls=ls, fmt=pts[i], lw=2.5) plt.xlim(10,300) plt.axhline(0, lw=2, color="k") plt.xlabel("Signal-to-Noise $SNR_w$") if name=="m": plt.ylim(-0.85,0.05) plt.ylabel("Multiplicative Bias $m \equiv (m_1 + m_2)/2$") elif name=="alpha": plt.ylabel(r"PSF Leakage $\alpha \equiv (\alpha _1 + \alpha _2)/2$") plt.ylim(-0.5,2) plt.legend(loc="lower right")
def simFlips(numFlips, numTrials): # performs and displays the simulation result diffs = [] # diffs to know if there was a fair Trial. It has the absolute differences of heads and tails in each trial for i in xrange(0, numTrials): heads, tails = flipTrial(numFlips) diffs.append(abs(heads - tails)) diffs = pylab.array(diffs) # create an array of diffs diffMean = sum(diffs)/len(diffs) # average of absolute differences of heads and tails from each trial diffPercent = (diffs/float(numFlips)) * 100 # create an array of percentage of each diffs from its no. of flips. percentMean = sum(diffPercent)/len(diffPercent) # create a percent mean of all diffPercents in the array pylab.hist(diffs) # displays the distribution of elements in diffs array pylab.axvline(diffMean, color = 'r', label = 'Mean') pylab.legend() titleString = str(numFlips) + ' Flips, ' + str(numTrials) + ' Trials' pylab.title(titleString) pylab.xlabel('Difference between heads and tails') pylab.ylabel('Number of Trials') pylab.figure() pylab.plot(diffPercent) pylab.axhline(percentMean, color = 'r', label = 'Mean') pylab.legend() pylab.title(titleString) pylab.xlabel('Trial Number') pylab.ylabel('Percent Difference between heads and tails')
def gfe4(): x2=plt.linspace(1e-20,.13,90000) xmin2=((4*np.pi*(SW.R)**3)/3)*1e-20 xmax2=((4*np.pi*(SW.R)**3)/3)*.13 xff2 = plt.linspace(xmin2,xmax2,90000) thigh=100 plt.figure() plt.title('Grand free energy per volume vs ff @ T=%0.4f'%Tlist[thigh]) plt.ylabel('Grand free energy per volume') plt.xlabel('filling fraction') plt.plot(xff2,SW.phi(Tlist[thigh],x2,nR[thigh]),color='#f36118',linewidth=3) #plt.axvline(nL[thigh]) #plt.axvline(nR[thigh]) #plt.axhline(SW.phi(Tlist[thigh],nR[thigh])) #plt.plot(x2,x2-x2,'c') plt.plot(nL[thigh]*((4*np.pi*(SW.R)**3)/3),SW.phi(Tlist[thigh],nL[thigh],nR[thigh]),'ko') plt.plot(nR[thigh]*((4*np.pi*(SW.R)**3)/3),SW.phi(Tlist[thigh],nR[thigh],nR[thigh]),'ko') plt.axhline(SW.phi(Tlist[thigh],nR[thigh],nR[thigh]),color='c',linewidth=2) print(Tlist[100]) print(nL[100],nR[100]) plt.savefig('figs/gfe_cotangent.pdf') plt.figure() plt.plot(xff2,SW.phi(Tlist[thigh],x2,nR[thigh]),color='#f36118',linewidth=3) plt.plot(nL[thigh]*((4*np.pi*(SW.R)**3)/3),SW.phi(Tlist[thigh],nL[thigh],nR[thigh]),'ko') plt.plot(nR[thigh]*((4*np.pi*(SW.R)**3)/3),SW.phi(Tlist[thigh],nR[thigh],nR[thigh]),'ko') plt.axhline(SW.phi(Tlist[thigh],nR[thigh],nR[thigh]),color='c',linewidth=2) plt.xlim(0,0.0003) plt.ylim(-.000014,0.000006) print(Tlist[100]) print(nL[100],nR[100]) plt.savefig('figs/gfe_insert_cotangent.pdf')
def test(): if 0: from pandas import DataFrame X = np.linspace(0.01, 1.0, 10) Y = np.log(X) Y -= Y.min() Y /= Y.max() Y *= 0.95 df = DataFrame({'X': X, 'Y': Y}) P = Pareto(df, 'X', 'Y') data = [] for val in np.linspace(0,1,15): data.append(dict(val=val, x=P.lookup_x(val), y=P.lookup_y(val))) pl.axvline(val, alpha=.5) pl.axhline(val, alpha=.5) dd = DataFrame(data) pl.scatter(dd.y, dd.val, lw=0, c='r') pl.scatter(dd.val, dd.x, lw=0, c='g') print dd #P.scatter(c='r', lw=0) P.show_frontier(c='r', lw=4) pl.show() X,Y = np.random.normal(0,1,size=(2, 30)) for maxX in [0,1]: for maxY in [0,1]: pl.figure() pl.title('max x: %s, max y: %s' % (maxX, maxY)) pl.scatter(X,Y,lw=0) show_frontier(X, Y, maxX=maxX, maxY=maxY) pl.show()
def plot_resid(x, resid): import pylab pylab.plot(x, resid, '.') pylab.gca().locator_params(axis='y', tight=True, nbins=4) pylab.axhline(y=1, ls='dotted') pylab.axhline(y=-1, ls='dotted') pylab.ylabel("Residuals")
def test_band_unpolarized(self): """polarizedのPROCAR用""" # import seaborn path = os.path.join(self.path, 'unpolarized', 'C_P63mmmc') dos = collect_vasp.Doscar(os.path.join(path, 'DOSCAR_polarized')) dos.get_data() e_fermi = dos.fermi_energy # DOSCARからのEf band = collect_vasp.Procar(os.path.join(path, 'PROCAR_band'), is_polarized=False) band['energy'] = band['energy'] - e_fermi band.output_keys = ['kpoint_id', 'energy', "spin"] kp_label = band.get_turning_kpoints_pmg(os.path.join(path, 'KPOINTS_band')) # plot plt = pylab.figure(figsize=(7, 7/1.618)) ax1 = plt.add_subplot(111) ax1.set_ylabel("Energy from $E_F$(meV)") ax1.set_xlabel("BZ direction") # 枠 for i in kp_label[0]: pylab.axvline(x=i, ls=':', color='gray') pylab.axhline(y=0, ls=':', color='gray') ax1.scatter(band['kpoint_id'], band['energy'], s=3, c='blue', linewidths=0) ax1.set_xlim(0, band['kpoint_id'][-1]) pylab.xticks(kp_label[0], kp_label[1]) pylab.show()
def Cross(x0=0.0, y0=0.0, clr='black', ls='dashed', lw=1, zorder=0): """ Draw cross through zero ======================= """ axvline(x0, color=clr, linestyle=ls, linewidth=lw, zorder=zorder) axhline(y0, color=clr, linestyle=ls, linewidth=lw, zorder=zorder)
def plot_acceptance(acc): """ Plot acceptance fraction for each walker Parameters ---------- acc: array Acceptance fraction for each walker Returns ------- fig: matplotlib figure The figure with the plot """ nwalkers = acc.size mean = acc.mean() fig = pl.figure() ax = fig.gca() pl.bar(xrange(1, nwalkers+1), acc, align="center", color="k", edgecolor="c") pl.axhline(mean, color="r", linestyle="--", dash_capstyle="round") ax.annotate("mean acceptance = %.1f %%" % (100*mean), color="r", xy=(10, mean), xytext=(10, mean+0.02)) ax.set_xlim(0, nwalkers+1) ax.set_ylim(0, 1) ax.set_xlabel("walker index") ax.set_ylabel("acceptance fraction") return fig
def getRotation(self, plot = True): if self.nComponents != 3: raise Exception("ERROR : not 3 centers") ## choice only surface based criteria areas = np.zeros(self.nComponents) for i in xrange(self.nComponents): areas[i] = self.params[6*i+3]*self.params[6*i+4]*np.pi goodcenters = range(self.nComponents) other = np.argmax(areas) goodcenters.pop(other) x3 = self.params[6*other+1] y3 = self.params[6*other+2] x1 = self.params[6*goodcenters[0]+1] y1 = self.params[6*goodcenters[0]+2] x2 = self.params[6*goodcenters[1]+1] y2 = self.params[6*goodcenters[1]+2] cx = (x1 + x2)/2. cy = (y1 + y2)/2. rot = acos((cy-y3) /1./ self.euclidian_dist(x3, y3, cx, cy)) if x3-cx < 0: rot = -1.*rot if plot: pylab.subplot(2,2,1) pylab.plot([y3, cy], [x3, cx], 'm-', label = (x3-cx)>0) pylab.plot(y3, x3, 'go', label = (y3-cy)>0) pylab.axhline(y = x3, c= 'y', ls = '--') pylab.plot([y1, y2], [x1, x2], 'r+') pylab.subplot(2,2,2) pylab.plot(self.rotation(x3, y3, rot)[1], self.rotation(x3, y3, rot)[0], 'go') pylab.plot(self.rotation(cx, cy, rot)[1], self.rotation(cx, cy, rot)[0], 'ro') pylab.plot([self.rotation(x1, y1, rot)[1], self.rotation(x2, y2, rot)[1]], [self.rotation(x1, y1, rot)[0], self.rotation(x2, y2, rot)[0]], 'r+') pylab.xlim([-50., 50.]) pylab.ylim([-50., 50.]) return rot, [(x1,y1), (x2,y2), (x3,y3), (int((x3+cx)/2.), int((y3+cy)/2.))]
def OffsetPlot(): import pylab as P import scipy.stats as S offsp = S.spearmanr(data[:,dict['medianOffset']], telfocusCorrected) offreg = S.linregress(data[:,dict['medianOffset']], telfocusCorrected) offreg2 = S.linregress(data[:,dict['medianOffset']], telfocusOld) min = -50. max = 50. print '\nOffset Spearman rank-order:', offsp print 'Offset fit:', offreg print 'and For unCorrected data:', offreg2 P.plot(data[:,dict['medianOffset']], telfocusCorrected, 'bo', label = 'Data') P.plot([min,max], [min*offreg[0] + offreg[1], max*offreg[0] + offreg[1]], 'r-', label ='Linear Fit (Corrected)', lw = 2.0) P.plot([min,max], [min*offreg2[0] + offreg2[1], max*offreg2[0] + offreg2[1]], 'g--', label ='Linear Fit (UnCorrected)', lw = 1.5) P.axhline(medianNew, color ='b') P.xlim(min, max) P.xlabel('Median Offset (telescope units)') P.ylabel('Temperature Corrected Telescope Focus + Median Offset') P.legend(shadow=True) P.savefig('offsetCorrelation.png') P.close()
def measure_tae(): print "Measuring initial perception of all orientations..." before=test_all_orientations(0.0,0.0) pylab.figure(figsize=(5,5)) vectorplot(degrees(before.keys()), degrees(before.keys()),style="--") # add a dashed reference line vectorplot(degrees(before.values()),degrees(before.keys()),\ title="Initial perceived values for each orientation") print "Adapting to pi/2 gaussian at the center of retina for 90 iterations..." for p in ["LateralExcitatory","LateralInhibitory","LGNOnAfferent","LGNOffAfferent"]: # Value is just an approximate match to bednar:nc00; not calculated directly topo.sim["V1"].projections(p).learning_rate = 0.005 inputs = [pattern.Gaussian(x = 0.0, y = 0.0, orientation = pi/2.0, size=0.088388, aspect_ratio=4.66667, scale=1.0)] topo.sim['Retina'].input_generator.generators = inputs topo.sim.run(90) print "Measuring adapted perception of all orientations..." after=test_all_orientations(0.0,0.0) before_vals = array(before.values()) after_vals = array(after.values()) diff_vals = before_vals-after_vals # Sign flipped to match conventions pylab.figure(figsize=(5,5)) pylab.axvline(90.0) pylab.axhline(0.0) vectorplot(wrap(-90.0,90.0,degrees(diff_vals)),degrees(before.keys()),\ title="Difference from initial perceived value for each orientation")
def video(data, Ti=None, Tf=None): fig, ax = py.subplots() py.xlim([-0.5, 9.5]) py.ylim([-0.5, 9.5]) py.axvline(xc[1] - 0.5, color="black", linestyle="--") py.axhline(yc[2] - 0.5, color="black", linestyle="--") if Tf == None: Tf = data.shape[2] - 1 if Ti == None: Ti = 0 lines = [] for i in range(num_walkers): x = data[i, 0, Ti : Ti + 1] y = data[i, 1, Tf : Tf + 1] line, = py.plot(x, y, "o") lines.append(line) def animate(i): py.title(i) for j, line in enumerate(lines): line.set_xdata(data[j, 0, i : i + 1]) line.set_ydata(data[j, 1, i : i + 1]) return lines ani = animation.FuncAnimation(fig, animate, np.arange(Ti, Tf), interval=300, blit=False) py.show()
def plot_complex (content,title): """Plots x vs y"""; # plot errors bars, if available pylab.axhline(0,color='lightgrey') pylab.axvline(1,color='lightgrey') pylab.errorbar( [x.real for l1,l2,(x,xe),(y,ye) in content],[x.imag for l1,l2,(x,xe),(y,ye) in content], [xe for l1,l2,(x,xe),(y,ye) in content],[xe for l1,l2,(x,xe),(y,ye) in content], fmt=None,ecolor="lightgrey" ); pylab.errorbar( [y.real for l1,l2,(x,xe),(y,ye) in content],[y.imag for l1,l2,(x,xe),(y,ye) in content], [ye for l1,l2,(x,xe),(y,ye) in content],[ye for l1,l2,(x,xe),(y,ye) in content], fmt=None,ecolor="lightgrey" ); # max plot amplitude -- max point plus 1/4th of the error bar maxa = max([ max(abs(x),abs(y)) for l1,l2,(x,xe),(y,ye) in content ]); # plotlim = max([ abs(numpy.array([ # getattr(v,attr)+sign*e/4 for v,e in (x,xe),(y,ye) for attr in 'real','imag' for sign in 1,-1 # ])).max() # for l1,l2,(x,xe),(y,ye) in content ]); minre, maxre, minim, maxim = 2, -2, 2, -2 for l1,l2,(x,xe),(y,ye) in content: offs = numpy.array([ getattr(v,attr)+sign*e/4 for v,e in (x,xe),(y,ye) for attr in 'real','imag' for sign in 1,-1 ]) minre, maxre = min(x.real-xe/4, y.real-ye/4, minre), max(x.real+xe/4, y.real+ye/4, maxre)
def findSeriesLength(teamProb): numSeries = 200 maxLen = 2500 step = 10 def fracWon(teamProb, numSeries, seriesLen): won = 0.0 for series in range(numSeries): if playSeries(seriesLen, teamProb): won += 1 return won/numSeries winFrac = [] xVals = [] for seriesLen in range(1, maxLen, step): xVals.append(seriesLen) winFrac.append(fracWon(teamProb, numSeries, seriesLen)) pylab.plot(xVals, winFrac, linewidth=5) pylab.xlabel("Length of Series") pylab.ylabel("Probability of winning a series") pylab.title(str(round(teamProb, 4)) + ' probability of team winning a game') # draw horizontal line at 0.95 pylab.axhline(0.95) pylab.show()
def plotramp(sci, err, tsamp, nsamp, plotfit=False, **kwargs): """ plot the up-the-ramp sampling sequence for the given pixel, given in _ima coordinates. kwargs are passed to pylab.errorbar(). """ from pylab import plot, errorbar, legend, xlabel, ylabel, axes, axhline # sci, err, tsamp, nsamp = getrampdat(imafile, x, y ) if plotfit: ax1 = axes([0.1, 0.35, 0.85, 0.6]) m, b = fitramp(sci, err, tsamp, nsamp) fit = m * nsamp + b plot(nsamp, fit, ls="--", color="k", marker="") errorbar(nsamp, sci * tsamp, err * tsamp, **kwargs) ylabel("cumulative counts (sci*tsamp)") legend(loc="upper left") ax2 = axes([0.1, 0.1, 0.85, 0.25], sharex=ax1) kwargs["ls"] = " " errorbar(nsamp[1:], sci[1:] * tsamp[1:] - fit[1:], err[1:] * tsamp[1:], **kwargs) axhline(ls="--", color="k") xlabel("SAMP NUMBER") else: errorbar(nsamp, sci * tsamp, err * tsamp, **kwargs) xlabel("SAMP NUMBER") ylabel("cumulative counts (sci*tsamp)") legend(loc="upper left")
def PlotSlice(LogLikelihood,par,low,upp,par_in,func_args=(),plot_samp=100): """ Plot the conditional distributions for each variable parameter. Used to visualise the conditional errors, and get sensible inputs to ConditionalErrors function. """ i = par_in op_par = np.copy(par) max_loglik = LogLikelihood(op_par,*func_args) par_range = np.linspace(low,upp,plot_samp) log_lik = np.zeros(plot_samp) temp_par = np.copy(op_par) for q,par_val in enumerate(par_range): temp_par[i] = par_val log_lik[q] = LogLikelihood(temp_par,*func_args) print np.exp(log_lik-max_loglik) pylab.clf() pylab.subplot(211) pylab.plot(par_range,log_lik) pylab.axhline(max_loglik-0.5,color='g',ls='--') pylab.xlabel("p[%s]" % str(i)) pylab.ylabel("log Posterior") pylab.subplot(212) pylab.plot(par_range,np.exp(log_lik-max_loglik)) pylab.axvline(op_par[i],color='r') pylab.axhline(0.6065,color='g',ls='--') pylab.xlabel("p[%s]" % str(i)) pylab.ylabel("Posterior")
def plot_tm_rbf_decision_values(class_ids, dec_values, plot_title = '', plot_file = ''): import pylab as pl from collections import defaultdict print true_class = defaultdict(list) for i, class_id in enumerate(class_ids): print '#%d true class: %d decision value: %.5f' % (i, class_id, dec_values[i]) true_class[class_id] += [dec_values[i]] print pl.clf() pl.plot(true_class[IRRELEVANT_CLASS_ID], 'bo', label='Irrelevant') x2 = range(len(true_class[IRRELEVANT_CLASS_ID]), len(class_ids)) pl.plot(x2, true_class[RELEVANT_CLASS_ID], 'r+', label='Relevant') pl.axhline(0, color='black') pl.xlabel('Documents') pl.ylabel('Decision values') pl.title(plot_title) pl.legend(loc='lower right', prop={'size':9}) pl.grid(True) if (plot_file == ''): pl.show() else: pl.savefig(plot_file, dpi=300, bbox_inches='tight', pad_inches=0.1) pl.close() pl.clf()
def plot_one_PSTH(arr, n_trial, ax, rng=DEF_PSTH_RANGE_MS, xrng=DEF_PSTH_RANGE_MS, \ aggregate=10, ymax=250, color='#999999', nticks=5, visible=False, txt=None, nondraw=False): arr = np.array(arr) # plot PSTH if n_trial > 0: arr = arr/1000. # to ms interval = rng[1] - rng[0] bins = int(interval / aggregate) weight = 1000. / aggregate / n_trial # to convert into Spiks/s. weights = np.array([weight] * len(arr)) if nondraw: rtn = np.histogram(arr, range=rng, bins=bins, weights=weights) pl.axhline(y=0, color='#333333') else: rtn = pl.hist(arr, range=rng, bins=bins, weights=weights, fc=color, ec=color) # beutify axes pl.xlim(xrng) pl.ylim([0,ymax]) ax.xaxis.set_major_locator(pl.MaxNLocator(nticks)) ax.yaxis.set_major_locator(pl.MaxNLocator(nticks)) if not visible: ax.set_xticklabels([''] * nticks) ax.set_yticklabels([''] * nticks) pl.axvline(x=0, ymin=0, ymax=1, lw=0.5, color='r') if txt != None: if nondraw: pl.text(xrng[1] - 20, ymax - 70, txt, size=6, va='top', ha='right') else: pl.text(xrng[1] - 20, ymax - 20, txt, size=6, va='top', ha='right') return rtn
def PlotConditionals(LogLikelihood,par,err,low,upp,func_args=(),plot_samp=100,opt=False,par_in=None,wait=False): """ Plot the conditional distributions for each variable parameter. Used to visualise the conditional errors, and get sensible inputs to ConditionalErrors function. """ #first optimise the log likelihood? if opt: op_par = Optimise(LogLikelihood,par[:],func_args,fixed=(np.array(err) == 0)*1) else: op_par = np.copy(par) max_loglik = LogLikelihood(op_par,*func_args) if par_in == None: par_in = np.where(np.array(err) != 0.)[0] for i in par_in: par_range = np.linspace(low[i],upp[i],plot_samp) log_lik = np.zeros(plot_samp) temp_par = np.copy(op_par) for q,par_val in enumerate(par_range): temp_par[i] = par_val log_lik[q] = LogLikelihood(temp_par,*func_args) pylab.clf() pylab.plot(par_range,log_lik) pylab.plot(par_range,max_loglik-(par_range-op_par[i])**2/2./err[i]**2,'r--') pylab.axvline(op_par[i],color='r') pylab.axvline(op_par[i]+err[i],color='g') pylab.axvline(op_par[i]-err[i],color='g') pylab.axhline(max_loglik-0.5,color='g',ls='--') pylab.xlabel("p[%s]" % str(i)) pylab.ylabel("log Posterior") #pylab.xlims(low[i],upp[i]) if wait: raw_input("")
def doplot(data, title, ylim=None, yaxis = 'Quantity', meanval=False, color='b', label='quantity', showfig = False): pl.title(title, fontsize = 15, color='k') fig = pl.plot(range(len(data)), data, color, label=label, linewidth=1.5) pl.ylabel(yaxis) #pl.xticks( np.arange(0, len(data)+len(data)*0.1, len(data)*0.1 ) ) x_mean = np.mean(data) if meanval==True: pl.axhline( x_mean, 0, len(data), color='r', linewidth=1.3, label='mean') if ylim<>None: pl.ylim(ylim) pl.xlabel('Number of maps') pl.xticks(np.arange(0,len(data),1)) pl.yticks(np.arange(0,1.1,0.1)) pl.grid(False) pl.gca().yaxis.grid(True) pl.legend(loc='upper left', numpoints = 1) if showfig: pl.show() return fig
def random_test(): rand_periods = np.zeros(1000) periods = np.zeros(1000) for i in range(1000): rand_periods[i] = np.random.uniform(low = 0.0, high = 2.0) periods[i] = 10**rand_periods[i] true_periods = np.zeros(1000) for i in range(1000): data = np.genfromtxt('/Users/angusr/angusr/ACF/star_spot_sim/tests/sim_period%s.txt' %(i+1)) true_periods[i] = data p.close(4) p.figure(4) p.subplot(3,1,1) p.plot(rand_periods, 'k.') p.subplot(3,1,2) p.plot(periods, 'k.') p.subplot(3,1,3) p.plot(np.log10(true_periods) ,'k.') ''' Plotting as close to original periods as I can''' p.close(10) p.figure(10) p.subplot(1,2,1) orig_periods = np.zeros(100) for i in range(100): data = np.genfromtxt('/Users/angusr/angusr/ACF/star_spot_sim/sim_period%s.txt' %(i+1)).T p.axhline(np.log10(data[4]), color = 'k') p.subplot(1,2,2) for i in range(100): data = np.genfromtxt('/Users/angusr/angusr/ACF/star_spot_sim/grid/%sparams.txt' %(i+1)).T p.axhline(np.log10(data[7]), color = 'k')
t_det_field_run = np.append(t_det_field_run, np.mean(t_det_field[i:i+3])) t_don_field_run = np.append(t_don_field_run, np.mean(t_don_field[i:i+3])) t_sed_1_field_run = np.append(t_sed_1_field_run, np.mean(t_sed_1_field[i:i+3])) t_ips_1_field_run = np.append(t_ips_1_field_run, np.mean(t_ips_1_field[i:i+3])) temp_field_run = np.append(temp_field_run, np.mean(temp_field[i:i+3])) salt_field_run = np.append(salt_field_run, np.mean(salt_field[i:i+3])) x = np.arange((len(temp_field))) x = x+1850 temp_mean = np.mean(temp_field) temp_std = np.std(temp_field) fit = np.polyfit(x,temp_field,1) fit_fn = np.poly1d(fit) pl.figure() pl.plot(x, temp_field, '-b', x, fit_fn(x), '--k') pl.axhline(y=temp_mean, xmin=0, xmax=1, hold=None, color='red') pl.axhline(y=temp_mean+temp_std, xmin=0, xmax=1, hold=None, color='green') pl.axhline(y=temp_mean-temp_std, xmin=0, xmax=1, hold=None, color='green') a = 1 if plot == True: a = pl.figure() ax1 = pl.subplot(411) pl.plot(x, temp_field) pl.plot(x,fit_fn(x), '--r') pl.setp(ax1.get_xticklabels(), fontsize=6) pl.setp(ax1.get_yticklabels(), fontsize=6) pl.ylabel('T in [C]') # share x only
# without access to an X server. import matplotlib #matplotlib.use('Agg') import pylab, numpy, sys if len(sys.argv) != 2: print("Usage: " + sys.argv[0] + " out-filename.pdf") exit(1) pdffilename = sys.argv[1] pylab.figure(1) pylab.title('$g_{HS}(r)$') pylab.axvline(x=1, color='k', linestyle=':') pylab.axhline(y=1, color='k', linestyle=':') pylab.figure(2) pylab.axvline(x=1, color='k', linestyle=':') pylab.axhline(y=0, color='k', linestyle='-') def read_ghs(ff): mcdatafilename = "figs/mc-inner-sphere-2-0.%d.dat" % (10 * ff) print 'Using', mcdatafilename mcdata = numpy.loadtxt(mcdatafilename) nA_mc = mcdata[:, 11] n0_mc = mcdata[:, 10] r_mc = mcdata[:, 0] n_mc = mcdata[:, 1] ghs = n_mc[r_mc > 2] * 4 * numpy.pi / 3 / ff
# top_edge - mean(right-edge) sensor_1_array = density_hydro[q2_index, q1_index] - np.mean(density_hydro[:, -1]) sensor_2_array = density_ballistic[q2_index, q1_index] - np.mean( density_ballistic[:, -1]) sensor_3_array = density_ohmic[q2_index, q1_index] - np.mean(density_ohmic[:, -1]) np.savetxt("R_vs_L_hydro.txt", sensor_1_array) np.savetxt("R_vs_L_ballistic.txt", sensor_2_array) np.savetxt("R_vs_L_ohmic.txt", sensor_3_array) np.savetxt("bottom_edge.txt", x[q1_index, q2_index]) pl.plot(x[q1_index, q2_index], sensor_1_array, label="Hydro") pl.plot(x[q1_index, q2_index], sensor_2_array, label="Ballistic") pl.plot(x[q1_index, q2_index], sensor_3_array, label="Ohmic") pl.axhline(0, color='k', ls='--') pl.ylim(ymax=150) #pl.gca().set_aspect('equal') pl.ylabel(r'$n$') pl.xlabel(r'x ($\mu$m)') pl.legend(loc='best') #pl.suptitle('$\\tau_\mathrm{mc} = \infty$, $\\tau_\mathrm{mr} = \infty$') #pl.suptitle('$\\tau_\mathrm{mc} = 0.1$, $\\tau_\mathrm{mr} = \infty$') pl.savefig('images/iv.png') pl.clf()
from numpy import * import numpy as np from matplotlib import * from pylab import * import pylab title('Choice of K=20: Number of posts vs Number of users ', {'fontsize':'24'}) xlabel('Number of Posts', {'fontsize':'24'}) ylabel('Number of Users', {'fontsize':'24'}) fname='data/k_vs_user_count.csv' X = genfromtxt(fname, delimiter=',') #xlim(0, max(X[:, 1])) #ylim(0, 1) #yticks(linspace(0, 1, 11)) pylab.loglog(X[:, 0], X[:, 1], '+-', linewidth='4') pylab.axhline(y=0.35, color='m', linestyle='dashed', linewidth='4', label='35%') pylab.axhline(y=0.94, color='r', linestyle='dashed', linewidth='4', label='94%') pylab.axvline(x=20, color='g', linestyle='dashed', linewidth='4', label='20 posts') legend() show()
import pylab as plt import numpy as np ps_0 = np.load('./planning_step_0.npy') ps_5 = np.load('./planning_step_5.npy') ps_10 = np.load('./planning_step_10.npy') ps_50 = np.load('./planning_step_50.npy') SCORES = [sum(i) / len(i) for i in [ps_0, ps_5, ps_10, ps_50]] plt.figure(figsize=(50, 6)) plt.title('Dyna-Q: various planning steps') plt.plot(ps_0) plt.plot(ps_5) plt.plot(ps_10) plt.plot(ps_50) plt.legend(('planning step 0', 'planning step 5', 'planning step 10', 'planning step 50'), loc='best') plt.axhline(y=np.max(SCORES), color='black', linestyle='--') print("The max score is {} from index {}.".format(np.max(SCORES), np.argmax(SCORES))) plt.show()
def checkAvgFR(showRasters=False,params={},antagInjectionSite='none',antag='',logFileName=''): nest.ResetNetwork() initNeurons() showPotential = False # Switch to True to graph neurons' membrane potentials - does not handle well restarted simulations dataPath='log/' nest.SetKernelStatus({"overwrite_files":True}) # when we redo the simulation, we erase the previous traces nstrand.set_seed(params['nestSeed'], params['pythonSeed']) # sets the seed for the simulation simulationOffset = nest.GetKernelStatus('time') print('Simulation Offset: '+str(simulationOffset)) offsetDuration = 1000. simDuration = 5000. # ms #------------------------- # measures #------------------------- spkDetect={} # spike detectors used to record the experiment multimeters={} # multimeters used to record one neuron in each population expeRate={} antagStr = '' if antagInjectionSite != 'none': antagStr = antagInjectionSite+'_'+antag+'_' for N in NUCLEI: # 1000ms offset period for network stabilization spkDetect[N] = nest.Create("spike_detector", params={"withgid": True, "withtime": True, "label": antagStr+N, "to_file": storeGDF, 'start':offsetDuration+simulationOffset,'stop':offsetDuration+simDuration+simulationOffset}) nest.Connect(Pop[N], spkDetect[N]) if showPotential: # multimeter records only the last 200ms in one neuron in each population multimeters[N] = nest.Create('multimeter', params = {"withgid": True, 'withtime': True, 'interval': 0.1, 'record_from': ['V_m'], "label": antagStr+N, "to_file": False, 'start':offsetDuration+simulationOffset+simDuration-200.,'stop':offsetDuration+simDuration+simulationOffset}) nest.Connect(multimeters[N], [Pop[N][0]]) #------------------------- # Simulation #------------------------- nest.Simulate(simDuration+offsetDuration) score = 0 text=[] frstr = "#" + str(params['LG14modelID'])+ " , " + antagInjectionSite + ', ' s = '----- RESULTS -----' print s text.append(s+'\n') if antagInjectionSite == 'none': validationStr = "\n#" + str(params['LG14modelID']) + " , " frstr += "none , " for N in NUCLEI: strTestPassed = 'NO!' expeRate[N] = nest.GetStatus(spkDetect[N], 'n_events')[0] / float(nbSim[N]*simDuration) * 1000 if expeRate[N] <= FRRNormal[N][1] and expeRate[N] >= FRRNormal[N][0]: # if the measured rate is within acceptable values strTestPassed = 'OK' score += 1 validationStr += N + "=OK , " else: # out of the ranges if expeRate[N] > FRRNormal[N][1] : difference = expeRate[N] - FRRNormal[N][1] validationStr += N + "=+%.2f , " % difference else: difference = expeRate[N] - FRRNormal[N][0] validationStr += N + "=%.2f , " % difference frstr += '%f , ' %(expeRate[N]) s = '* '+N+' - Rate: '+str(expeRate[N])+' Hz -> '+strTestPassed+' ('+str(FRRNormal[N][0])+' , '+str(FRRNormal[N][1])+')' print s text.append(s+'\n') else: validationStr = "" frstr += str(antag) + " , " for N in NUCLEI: expeRate[N] = nest.GetStatus(spkDetect[N], 'n_events')[0] / float(nbSim[N]*simDuration) * 1000 if N == antagInjectionSite: strTestPassed = 'NO!' if expeRate[N] <= FRRAnt[N][antag][1] and expeRate[N] >= FRRAnt[N][antag][0]: # if the measured rate is within acceptable values strTestPassed = 'OK' score += 1 validationStr += N + "_" + antag + "=OK , " else: # out of the ranges if expeRate[N] > FRRNormal[N][1] : difference = expeRate[N] - FRRNormal[N][1] validationStr += N + "_" + antag + "=+%.2f , " % difference else: difference = expeRate[N] - FRRNormal[N][0] validationStr += N + "_" + antag + "=%.2f , " % difference s = '* '+N+' with '+antag+' antagonist(s): '+str(expeRate[N])+' Hz -> '+strTestPassed+' ('+str(FRRAnt[N][antag][0])+' , '+str(FRRAnt[N][antag][1])+')' print s text.append(s+'\n') else: s = '* '+N+' - Rate: '+str(expeRate[N])+' Hz' print s text.append(s+'\n') frstr += '%f , ' %(expeRate[N]) s = '-------------------' print s text.append(s+'\n') frstr+='\n' firingRatesFile=open(dataPath+'firingRates.csv','a') firingRatesFile.writelines(frstr) firingRatesFile.close() #print "************************************** file writing",text #res = open(dataPath+'OutSummary_'+logFileName+'.txt','a') res = open(dataPath+'OutSummary.txt','a') res.writelines(text) res.close() validationFile = open("validationArray.csv",'a') validationFile.write(validationStr) validationFile.close() #------------------------- # Displays #------------------------- if showRasters and interactive: displayStr = ' ('+antagStr[:-1]+')' if (antagInjectionSite != 'none') else '' for N in NUCLEI: nest.raster_plot.from_device(spkDetect[N],hist=True,title=N+displayStr) if showPotential: pl.figure() nsub = 231 for N in NUCLEI: pl.subplot(nsub) nest.voltage_trace.from_device(multimeters[N],title=N+displayStr+' #0') nest.Disconnect(Pop[N], multimeters[N]) pl.axhline(y=BGparams[N]['V_th'], color='r', linestyle='-') nsub += 1 pl.show() return score, 5 if antagInjectionSite == 'none' else 1
#!/usr/bin/env python import pmag, matplotlib, math matplotlib.use("TkAgg") import pylab k = 29.2 Dels, A95s, CSDs, Ns = [], [], [], range(4, 31) DIs = [] for i in range(31): dec, inc = pmag.fshdev(k) DIs.append([dec, inc]) pars = pmag.fisher_mean(DIs) pDIs = [] for i in range(3): pDIs.append(DIs[i]) for n in Ns: pDIs.append(DIs[n]) fpars = pmag.fisher_mean(pDIs) A95s.append(fpars['alpha95']) CSDs.append(fpars['csd']) Dels.append((180. / math.pi) * math.acos(fpars['r'] / float(n))) pylab.plot(Ns, A95s, 'ro') pylab.plot(Ns, A95s, 'r-') pylab.plot(Ns, CSDs, 'bs') pylab.plot(Ns, CSDs, 'b-') pylab.plot(Ns, Dels, 'g^') pylab.plot(Ns, Dels, 'g-') pylab.axhline(pars['csd'], color='k') pylab.show() raw_input()
import numpy as np import matplotlib.pyplot as plt import pylab as pl import os rootdir = '~/OneDrive/Programming/Python/pyprograms/plotting/datafile_plotting/Plotting_all_files_in_a_folder/Plot/' for subdir, dirs, files in os.walk(rootdir): for file in files: f = open(os.path.join(subdir, file), 'r') print file data = np.loadtxt(f) #plot data pl.plot(data[:, 1], data[:, 2], 'gs') #Put in the errors pl.errorbar(data[:, 1], data[:, 2], data[:, 3], data[:, 4], fmt='ro') #Dashed lines showing pmRa=0 and pmDec=0 pl.axvline(0, linestyle='--', color='k') pl.axhline(0, linestyle='--', color='k') fileNameTemplate = r'~/OneDrive/Programming/Python/pyprograms/plotting/datafile_plotting/Plotting_all_files_in_a_folder/Plot{0:02d}.png' for subdir, dirs, files in os.walk(rootdir): for count, file in enumerate(files): # Generate a plot in `pl` pl.savefig(fileNameTemplate.format(count), format='png') pl.clf() # Clear the figure for the next loop
def sky_residuals(infile, fibstart=1, fibend=2000, allfib=False, verbose=False, plot=False, summarise=True): """ Function to check sky subtraction accuracy in SAMI data. It returns the average and median continuum and line sky flux and the residuals for these. the optional flags are: fibstart - first fibre to use, starting at fibre 1 (i.e. not zero indexed). fibend - last fibre to use. allfib - use all fibres, not just sky. verbose - output to the screen plot - make plots summarise - return summary dict of results, otherwise results for each fibre are returned """ # half-width of window around the sky lines: hwidth = 10 # open file: hdulist = pf.open(infile) # get primary header: primary_header = hdulist['PRIMARY'].header # check file is an object frame: obstype = primary_header['OBSTYPE'] # raise exception if not an object frame: if (obstype != 'OBJECT'): print('OBSTYPE keyword = ', obstype) raise IOError('Input file was not an OBJECT frame') # get data and variance im = hdulist[0].data var = hdulist['VARIANCE'].data # get array sizes: (ys, xs) = im.shape # try and get the sky spectum, raise an exception if no # sky spectrum found: try: sky = hdulist['SKY'].data except KeyError: print("SKY extension not found!") raise IOError('No sky extension found') # get wavelength info: # crval1=primary_header['CRVAL1'] # cdelt1=primary_header['CDELT1'] # crpix1=primary_header['CRPIX1'] # naxis1=primary_header['NAXIS1'] # x=np.arange(naxis1)+1 # L0=crval1-crpix1*cdelt1 #Lc-pix*dL # lam=L0+x*cdelt1 lam = get_coords(primary_header, 1) # get binary table info: fib_tab_hdu = find_fibre_table(hdulist) table_data = hdulist[fib_tab_hdu].data types = table_data.field('TYPE') # next add back in the sky spectrum to the data: im_sky = im + sky # define sky lines: #skylines=[5577.346680,6300.308594,6363.782715,6533.049805,6553.625977,6923.192383,7316.289551,7340.900879,7358.680176] # use a shorter version as some of the reddest lines are not always in all fibres: skylines = [ 5577.346680, 6300.308594, 6363.782715, 6533.049805, 6553.625977, 6923.192383 ] # calculate some basic statistics for each sky spectrum. # what do we want: # 1) summed residual flux as a fraction of total sky # 2) summed residual flux as fraction for strong emission lines ns = 0 fracs = np.zeros(ys) skyflux = np.zeros(ys) fibs = np.zeros(ys) line_fracs = np.zeros(ys) line_skyflux = np.zeros(ys) if (verbose): print(' | Cont flux | Line flux ') print('fib|f(sky) f(res) frac| f(sky) f(res) frac ') for i in range(ys): if ((types[i] == 'S' or allfib) and (i >= fibstart - 1 and i <= fibend - 1)): spec_test = im[i, :] spec_test = spec_test[~np.isnan(spec_test)] sub_med = np.median(spec_test) spec_test = im_sky[i, :] spec_test = spec_test[~np.isnan(spec_test)] sky_med = np.median(spec_test) frac = sub_med / sky_med skyflux[ns] = sky_med fracs[ns] = frac fibs[ns] = i + 1 line_res = 0 line_flux = 0 line_res_s = 0 line_flux_s = 0 ibad = 0 nlines_used = 0 for line in skylines: # only use lines in range: if (line > lam[0] and line < lam[xs - 1]): #print('testing...',line,lam[0],lam[xs-1]) ll = lam ff = im ss = im_sky nlines_used = nlines_used + 1 # get the index of the pixel nearest the sky line: iloc = min(range(len(ll)), key=lambda i: abs(ll[i] - line)) # get the data around the sky line xx = ll[iloc - hwidth:iloc + hwidth + 1] yy = ff[i, iloc - hwidth:iloc + hwidth + 1] # get median filtered continuum near the line: cont = median_filter(ff[i, :], size=51) cont_sky = median_filter(ss[i, :], size=51) cc = cont[iloc - hwidth:iloc + hwidth + 1] cc_sky = cont_sky[iloc - hwidth:iloc + hwidth + 1] #sig = np.sqrt(yy) # sum the flux over the line line_res = np.sum(ff[i, iloc - hwidth:iloc + hwidth + 1] - cont[iloc - hwidth:iloc + hwidth + 1]) line_flux = np.sum(ss[i, iloc - hwidth:iloc + hwidth + 1] - cont_sky[iloc - hwidth:iloc + hwidth + 1]) # get the residual line flux if (np.isnan(line_res) or np.isnan(line_flux)): ibad = ibad + 1 else: line_res_s = line_res_s + line_res line_flux_s = line_flux_s + line_flux #print('test:',line_res_s,line_flux_s,ibad) if (line_flux_s > 0): line_fracs[ns] = line_res_s / line_flux_s line_skyflux[ns] = line_flux_s else: line_fracs[ns] = 0.0 line_skyflux[ns] = 0.0 if (verbose): print( '{0:3d} {1:6.2f} {2:6.2f} {3:6.3f} {4:8.2f} {5:7.2f} {6:6.3f}' .format(i + 1, sky_med, sub_med, frac, line_flux_s, line_res_s, line_fracs[ns])) #print(i+1,sky_med_r,sub_med_r,frac_r,line_flux_r,line_res_r,line_fracs_r[ns]) ns = ns + 1 # print('number of lines used:',nlines_used) if summarise: # get the median/mean fractional sky residuals: medsky_cont = np.median(abs(fracs[0:ns])) medsky_line = np.median(abs(line_fracs[0:ns])) meansky_cont = np.mean(abs(fracs[0:ns])) meansky_line = np.mean(abs(line_fracs[0:ns])) # get the median/mean fluxes: medskyflux_cont = np.median(abs(skyflux[0:ns])) medskyflux_line = np.median(abs(line_skyflux[0:ns])) meanskyflux_cont = np.mean(abs(skyflux[0:ns])) meanskyflux_line = np.mean(abs(line_skyflux[0:ns])) if (verbose): print('median absolute continuum residuals:', medsky_cont) print('median absolute line residuals:', medsky_line) print('mean absolute continuum residuals:', meansky_cont) print('mean absolute line residuals:', meansky_line) if (plot): py.figure(1) lab = infile + ' cont residual' py.plot(fibs[0:ns], fracs[0:ns], '-', color='r', label=lab) for i in range(ys): if (types[i] == 'S'): py.plot(fibs[i], fracs[i], 'x', color='g') else: py.plot(fibs[i], fracs[i], '.', color='r') lab = infile + ' line residual' py.plot(fibs[0:ns], line_fracs[0:ns], '-', color='b', label=lab) for i in range(ys): if (types[i] == 'S'): py.plot(fibs[i], line_fracs[i], 'x', color='m') else: py.plot(fibs[i], line_fracs[i], '.', color='b') py.axhline(0.0, color='k', linestyle='--') py.xlabel('Fibre') py.ylabel('fractional sky residual') py.title('Fractional sky residuals') py.legend(prop={'size': 10}) # put results into a dictonary: sky_sub_res = { 'med_frac_skyres_cont': medsky_cont, 'med_frac_skyres_line': medsky_line, 'med_skyflux_cont': medskyflux_cont, 'med_skyflux_line': medskyflux_line, 'mean_frac_skyres_cont': meansky_cont, 'mean_frac_skyres_line': meansky_line, 'mean_skyflux_cont': meanskyflux_cont, 'mean_skyflux_line': meanskyflux_line } return sky_sub_res else: return fracs, line_fracs
def aspcap_residue_plot(self, test_predictions, test_labels, test_pred_error=None, test_labels_err=None): """ NAME: aspcap_residue_plot PURPOSE: plot aspcap residue INPUT: test_predictions (ndarray): Test result from neural network test_labels (ndarray): Gound truth for tests result test_pred_error (ndarray): (Optional) 1-sigma error for tests result from Baysian neural network. test_labels_err (ndarray): (Optional) Ground truth for tests result OUTPUT: None, just plots to be saved HISTORY: 2018-Jan-28 - Written - Henry Leung (University of Toronto) """ import pylab as plt import numpy as np import seaborn as sns print("Start plotting residues") resid = test_predictions - test_labels # Some plotting variables for asthetics plt.rcParams['axes.facecolor'] = 'white' sns.set_style("ticks") plt.rcParams['axes.grid'] = True plt.rcParams['grid.color'] = 'gray' plt.rcParams['grid.alpha'] = '0.4' x_lab = 'ASPCAP' y_lab = 'astroNN' fullname = self.targetname aspcap_residue_path = os.path.join(self.fullfilepath, 'ASPCAP_residue') if not os.path.exists(aspcap_residue_path): os.makedirs(aspcap_residue_path) mad_labels = np.zeros(test_labels.shape[1]) for i in range(test_labels.shape[1]): not9999_index = np.where(test_labels[:, i] != MAGIC_NUMBER) mad_labels[i] = mad((test_labels[:, i])[not9999_index], axis=0) if test_pred_error is None: # To deal with prediction from non-Bayesian Neural Network test_pred_error = np.zeros(test_predictions.shape) for i in range(self._labels_shape): plt.figure(figsize=(15, 11), dpi=200) plt.axhline(0, ls='--', c='k', lw=2) not9999 = np.where(test_labels[:, i] != -9999.)[0] plt.errorbar((test_labels[:, i])[not9999], (resid[:, i])[not9999], yerr=(test_pred_error[:, i])[not9999], markersize=2, fmt='o', ecolor='g', capthick=2, elinewidth=0.5) plt.xlabel('ASPCAP ' + target_name_conversion(fullname[i]), fontsize=25) plt.ylabel('$\Delta$ ' + target_name_conversion(fullname[i]) + '\n(' + y_lab + ' - ' + x_lab + ')', fontsize=25) plt.tick_params(labelsize=20, width=1, length=10) if self._labels_shape == 1: plt.xlim([ np.min((test_labels[:])[not9999]), np.max((test_labels[:])[not9999]) ]) else: plt.xlim([ np.min((test_labels[:, i])[not9999]), np.max((test_labels[:, i])[not9999]) ]) ranges = (np.max((test_labels[:, i])[not9999]) - np.min( (test_labels[:, i])[not9999])) / 2 plt.ylim([-ranges, ranges]) bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=2) bias = np.median((resid[:, i])[not9999], axis=0) scatter = mad((resid[:, i])[not9999], axis=0) plt.figtext(0.6, 0.75, '$\widetilde{m}$=' + '{0:.3f}'.format(bias) + ' $\widetilde{s}$=' + '{0:.3f}'.format(scatter / float(mad_labels[i])) + ' s=' + '{0:.3f}'.format(scatter), size=25, bbox=bbox_props) plt.tight_layout() plt.savefig(aspcap_residue_path + f'/{fullname[i]}_test.png') plt.close('all') plt.clf() if test_labels_err is not None: for i in range(self._labels_shape): plt.figure(figsize=(15, 11), dpi=200) plt.axhline(0, ls='--', c='k', lw=2) not9999 = np.where(test_labels[:, i] != -9999.)[0] plt.scatter((test_labels_err[:, i])[not9999], (resid[:, i])[not9999], s=0.7) plt.xlabel('ASPCAP Error of ' + target_name_conversion(fullname[i]), fontsize=25) plt.ylabel('$\Delta$ ' + target_name_conversion(fullname[i]) + '\n(' + y_lab + ' - ' + x_lab + ')', fontsize=25) plt.tick_params(labelsize=20, width=1, length=10) if self._labels_shape == 1: plt.xlim([ np.percentile((test_labels_err[:])[not9999], 5), np.percentile((test_labels_err[:])[not9999], 95) ]) else: plt.xlim([ np.min((test_labels_err[:, i])[not9999]), np.percentile((test_labels_err[:, i])[not9999], 90) ]) ranges = (np.percentile( (resid[:, i])[not9999], 5) - np.percentile( (resid[:, i])[not9999], 95)) plt.ylim([-ranges, ranges]) plt.tight_layout() plt.savefig(aspcap_residue_path + f'/{fullname[i]}_test_err.png') plt.close('all') plt.clf() print("Finished plotting residues")
thistext += '$\n' vexp = pylab.floor(pylab.log10(maxnorm)) if maxnorm == 0: vexp = 0 vmant = maxnorm / 10**vexp thistext += r'$\Vert$E$\Vert_\infty \! = \, %4.2f' % vmant if vexp != 0: thistext += r' \times \, 10^{%2d}' % vexp thistext += '$' pylab.text(xpos, ypos, thistext, va='top') # Density Plot a = pylab.axes([0.09, 0.57, 0.38, 0.38]) pylab.axhline(0, color='k', linestyle='dotted') pylab.plot(exact['x'], exact['Density']) pylab.plot(ray['x'], ray['Density'], 'ro', ms=4) #pylab.plot(ray_exact['x'],ray_exact['Density'], 'g+', ms=4) pylab.axis([0, 1, 0.0, 7.0]) pylab.xlabel('Position') pylab.ylabel('Density') error_label(norm['Density'], maxnorm['Density'], 0.1, 6.5) # Velocity Plot a = pylab.axes([0.59, 0.57, 0.38, 0.38]) pylab.axhline(0, color='k', linestyle='dotted') pylab.plot(exact['x'], exact['x-velocity']) pylab.plot(ray['x'], ray['x-velocity'], 'ro', ms=4)
def orig_adv_dist(orig_img=None, target_img=None, plot=False, bestC=None, iteration=1): if orig_img is None: orig_img = np.random.randint(0, len(test_x)) if target_img is None: target_label = test_y[orig_img] while target_label == test_y[orig_img]: target_img = np.random.randint(0, len(test_x)) target_label = test_y[target_img] noise_dist = [] orig_dist = [] adv_dist = [] target_recon_dist = [] recon_dist = [] orig_target_dist = [] orig_target_recon_dist = [] target_orig_recon_dist = [] C = np.logspace(-5, 20, 50, base=2, dtype=np.float32) for c in C: noise, od, ad, ore, tre, recd, otd, otrd, tord = adv_test( orig_img, target_img, C=c, plot=False, iteration=iteration) noise_dist.append(noise) orig_dist.append(od) adv_dist.append(ad) target_recon_dist.append(tre) recon_dist.append(recd) orig_target_dist.append(otd) orig_target_recon_dist.append(otrd) target_orig_recon_dist.append(tord) noise_dist = np.array(noise_dist) orig_dist = np.array(orig_dist) adv_dist = np.array(adv_dist) target_recon_dist = np.array(target_recon_dist) recon_dist = np.array(recon_dist) orig_target_dist = np.array(orig_target_dist) orig_target_recon_dist = np.array(orig_target_recon_dist) target_orig_recon_dist = np.array(target_orig_recon_dist) if bestC is None: bestC = C[np.argmax(adv_dist - orig_dist >= 0) - 1] print(orig_img, target_img, bestC) best_noise, best_orig_dist, best_adv_dist, orig_reconstruction_dist, target_reconstruction_dist, _, _, _, _ = adv_test( orig_img, target_img, C=bestC, plot=plot, iteration=iteration) plt.ioff() if plot: fig = plt.figure() plt.axhline(y=target_reconstruction_dist, linewidth=2, color='cyan', label="Dist(Target reconstruction - Target)") plt.axvline(x=orig_reconstruction_dist, linewidth=2, color='DarkOrange', label="Dist(Original reconstruction - Original)") plt.scatter(orig_dist, adv_dist) plt.scatter([best_orig_dist], [best_adv_dist], color="red") plt.xlabel("Dist(reconstructed Adversarial image - Original image)") plt.ylabel("Dist(reconstructed Adversarial image - Target image)") plt.legend() plt.plot() #output_dir = '/Users/rishikaagarwal/Desktop/cs597/adv_vae-master/results/' + model_filename + '/' output_dir = 'results/compare_attacks/' + model_filename + '/' #os.path.join(output_dir, {}/exp_'+ str(iteration)+ '.png') fig.savefig( os.path.join(output_dir, ('exp_' + str(iteration) + 'graph1.png'))) plt.close(fig) fig = plt.figure() plt.axhline(y=target_reconstruction_dist, linewidth=2, color='cyan', label="Dist(Target reconstruction - Target)") plt.axvline(x=np.linalg.norm(test_x[orig_img] - test_x[target_img]), linewidth=2, color='DarkOrange', label="Dist(Original - Target)") plt.scatter(noise_dist, adv_dist) plt.scatter([best_noise], [best_adv_dist], color="red") plt.ylabel("Dist(reconstructed Adversarial image - Target image)") plt.xlabel("Dist(noise)") plt.legend() fig.savefig( os.path.join(output_dir, ('exp_' + str(iteration) + 'graph2.png'))) plt.plot() plt.close(fig) bestCind = np.where(C == bestC) print('orig_img : ', orig_img) print('target_img : ', target_img) print('orig_reconstruction_dist : ', orig_reconstruction_dist) print('target_reconstruction_dist : ', target_reconstruction_dist) print('original_target_dist : ', orig_target_dist[bestCind]) print('orig_target_recon_dist : ', orig_target_recon_dist[bestCind]) print('target_orig_recon_dist : ', target_orig_recon_dist[bestCind]) print() print('bestC : ', bestC) print('adv_adv_recon_dist : ', recon_dist[bestCind]) print('best noise_dist : ', noise_dist[bestCind]) print('best orig_dist : ', orig_dist[bestCind]) print('best adv_dist : ', adv_dist[bestCind]) print() df = pd.DataFrame({ 'orig_img': orig_img, 'target_img': target_img, 'bestC': bestC, 'orig_reconstruction_dist': orig_reconstruction_dist, 'target_reconstruction_dist': target_reconstruction_dist, 'noise_dist': noise_dist, 'orig_dist': orig_dist, 'adv_dist': adv_dist, 'target_recon_dist': target_recon_dist, 'recon_dist': recon_dist, 'orig_target_dist': orig_target_dist, 'orig_target_recon_dist': orig_target_recon_dist, 'target_orig_recon_dist': target_orig_recon_dist, 'C': C }) return df
'left': 0.10, 'right': 0.95, 'top': 0.88, 'bottom': 0.12 }, # Space on left side of plot font_family='Roboto Condensed', font_size=26, do_show=do_show, color='k' # scatter_args={'c': 'k'} ) plot_customizations['color'] = mplt.colors.to_rgba('#333333') msim.plot_result('r_eff', **plot_customizations) #sim.plot_result('r_eff') pl.axhline(1.0, linestyle='--', c=[0.8, 0.4, 0.4], alpha=0.8, lw=4) # Add a line for the R_eff = 1 cutoff pl.xticks(fontsize=22) pl.yticks(fontsize=22) pl.title('') pl.savefig('R.pdf') plot_customizations['color'] = mplt.colors.to_rgba('black') msim.plot_result('cum_deaths', **plot_customizations) pl.xticks(fontsize=22) pl.yticks(fontsize=22) pl.title('') pl.savefig('Deaths.pdf') plot_customizations['color'] = mplt.colors.to_rgba('#c21945') msim.plot_result('new_infections', **plot_customizations) pl.xticks(fontsize=22)
for ii in range(len(WT)): clmplt = [] grnplt = [] for jj in range(len(S)): clmplt.append(clmbrate[ii][jj]) grnplt.append(grndroll[ii][jj]) pyl.plot(clmplt, grnplt, ls='--', lw=2) lgnd.append('W = %2.0f (lbf)' % (WT[ii] / LBF)) for jj in range(len(S)): clmplt = [] grnplt = [] for ii in range(len(WT)): clmplt.append(clmbrate[ii][jj]) grnplt.append(grndroll[ii][jj]) pyl.plot(clmplt, grnplt, lw=2) lgnd.append(r'S = %4.0f (in$^2$)' % (arealist[jj] / IN**2)) pyl.plot() pyl.axhline(y=190, color='k', lw=1) pyl.axvline(x=MinCumpRate, color='k', lw=1) pyl.title('Groundroll and Climb Rate for Varying Wing Area and Total Weight') pyl.xlabel('Climb Rate (ft/min)') pyl.ylabel('Groundroll (ft)') pyl.legend(lgnd, loc='best', numpoints=1, labelspacing=0.0) pyl.ylim(0, 400) pyl.xlim(50, 350) pyl.show()
def diagnostic(self, ymax=None): """Shows detected peaks for each well and confidence. We use MAD instead of standard deviation since we may have very heterogenous data. If there are several types of experiments, the enveloppe may not have nay meaning. If we have homogenous experiments, then outliers may biased the estimation of the standard deviation significantly. We therefore use a MAD metric (see :meth:`get_mad)`. .. plot:: :include-source: from pylab import figure from fragment_analyser import Line, fa_data l = Line(fa_data('alternate/peaktable.csv')) l.diagnostic() figure(2) from fragment_analyser import Line, fa_data l = Line(fa_data('standard_with_flat_cases/peak_table.csv')) l.diagnostic() figure(3) from fragment_analyser import Line, fa_data l = Line(fa_data('standard_mix_cases/peak_table.csv')) l.diagnostic() """ peaks = self.get_peaks() names = [well.name for well in self.wells] pylab.clf() pylab.plot(peaks, 'o-', mfc='red', label="selected peak") pylab.axhline(self.guess_peak(), linestyle='--', lw=2, color='k', label='median') pylab.grid(True) pylab.xlabel('Wells\' names') pylab.ylabel('Size bp') pylab.xticks(range(0, self._nwells - 1), names) pylab.ylim([0, pylab.ylim()[1] * 1.4]) pylab.xlim([-0.5, 10.5]) peaks = pylab.array(self.get_peaks()) # sigma is biased the presence of outliers, so we better off using the MAD sigma = self.get_mad() def nanadd(X, value): return [x + value if x is not None else x for x in X] X = [] Y = [] for i, peak in enumerate(peaks): if peak is None and len(X) == 0: # nothing to do. This may be the first point (len(X)==0) continue elif peak is None or (i == len(peaks) - 1): # we ended a chunk of valid data, let us plot it if peak is not None: X.append(i) Y.append(peak) if len( X ) == 1: # expand the window so that fill_between shows something Y.append(Y[0]) X = [X[0] - 0.5, X[0] + 0.5] else: X.insert(0, X[0] - 0.5) X.append(X[-1] + 0.5) Y.append(Y[-1]) Y.insert(0, Y[0]) X = np.array(X) Y = np.array(Y) pylab.fill_between(X, nanadd(Y, -sigma * 3), nanadd(Y, sigma * 3), color='red', alpha=0.5) pylab.fill_between(X, nanadd(Y, -sigma * 2), nanadd(Y, sigma * 2), color='orange', alpha=0.5) pylab.fill_between(X, nanadd(Y, -sigma), nanadd(Y, sigma), color='green', alpha=0.5) X = [] Y = [] else: # we are sliding inside a contiguous chunk X.append(i) Y.append(peak) pylab.legend()
if __name__ == '__main__': prob = 0 if prob == 0: x = linspace(0, 10, 100) y = x**1.5 plot(x, y, 'b-', label='sim') ver = int(sys.version[0]) if ver < 3: Arc(0, 0, 10) Arc(0, 0, 20, clr='magenta') Arrow(-10, 0, max(x), max(y)) Text(0, 25, r'$\sigma$') Text(0, 25, r'$\sigma$', y_offset=-10) axvline(0, color='black', zorder=-1) axhline(0, color='black', zorder=-1) FigGrid() axis('equal') legend(loc='upper left') xlabel(r'$x$') ylabel(r'$y$') show() if prob == 1: ITout = get_itout( [0., 0.1, 0.15, 0.2, 0.23, 0.23, 0.23, 0.3, 0.8, 0.99], [0., 0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 1.]) print(ITout) if prob == 2: ITout = get_itout([
times = getBMData('bm_results2012.txt')['time'] meanOfMeans, stdOfMeans = [], [] sampleSizes = range(50, 2000, 200) for sampleSize in sampleSizes: sampleMeans = [] for t in range(20): sample = random.sample(times, sampleSize) sampleMeans.append(sum(sample) / sampleSize) meanOfMeans.append(sum(sampleMeans) / len(sampleMeans)) stdOfMeans.append(stdDev(sampleMeans)) pylab.errorbar(sampleSizes, meanOfMeans, yerr=1.96 * pylab.array(stdOfMeans), label='Estimated Mean and 95% Confidence Interval') pylab.xlim(0, max(sampleSizes) + 50) pylab.axhline(sum(times) / len(times), linestyle='--', label="Population Mean") pylab.title("Estimates of Mean Finishing Time") pylab.xlabel("Sample Size") pylab.ylabel("Finishing Time (Minutes)") pylab.legend(loc='best') #%% Standard Error of the Mean # SE = (std dev of popn)/(sqrt(size of popn)) times = getBMData('bm_results2012.txt')['time'] popStd = stdDev(times) sampleSizes = range(2, 200, 2) diffsMeans = [] for sampleSize in sampleSizes: diffs = []
def derive_wifes_calibration(cube_fn_list, calib_out_fn, stdstar_name_list=None, extract_in_list=None, airmass_list=None, ref_dir=metadata_dir, ref_fname_list=None, plot_stars=False, plot_sensf=False, norm_stars=False, method='poly', polydeg=30, excise_cut=0.5, wave_min=None, wave_max=None, extinction_fn=None, ytrim=5): if plot_stars or plot_sensf: import pylab # get extinction curve if extinction_fn == None: extinct_interp = sso_extinct_interp else: ext_data = numpy.loadtxt(extinction_fn) extinct_interp = scipy.interpolate.interp1d(ext_data[:, 0], ext_data[:, 1], bounds_error=False, fill_value=numpy.nan) # first extract stdstar spectra and compare to reference fratio_results = [] for i in range(len(cube_fn_list)): f = pyfits.open(cube_fn_list[i]) cube_hdr = f[1].header f.close() #------------------------------------ # figure out which star it is # NEW VERSION 0.7.0: smart star name lookup! # # top priority: user forces the name if stdstar_name_list != None: star_name = stdstar_name_list[i] # if you forced an unknown star name, reset name to None if star_name not in ref_fname_lookup.keys(): star_name = None else: star_name = None # try to find the nearest standard in the list if star_name == None: try: star_name, dist = find_nearest_stdstar(cube_fn_list[i]) if dist > 200.0: star_name = None except: # last resort: use the object name from the fits header # and pray it's correct star_name = cube_hdr['OBJECT'] #------------------------------------ #print star_name if airmass_list != None: secz = airmass_list[i] else: try: secz = cube_hdr['AIRMASS'] except: print 'AIRMASS header missing for %s' % cube_fn_list[i].split( '/')[-1] secz = 1.0 # check if there is a calib spectrum... if ref_fname_list != None: ref_fname = ref_name_list[i] elif star_name in ref_fname_lookup.keys(): ref_fname = ref_fname_lookup[star_name] else: continue # get observed data if extract_in_list == None: obs_wave, obs_flux = extract_wifes_stdstar(cube_fn_list[i], ytrim=ytrim) else: ex_data = numpy.loadtxt(extract_in_list[i]) obs_wave = ex_data[:, 0] obs_flux = ex_data[:, 1] if wave_min == None: wave_min = numpy.min(obs_wave) if wave_max == None: wave_max = numpy.max(obs_wave) # get reference data ref_data = numpy.loadtxt(ref_dir + ref_fname) ref_interp = scipy.interpolate.interp1d(ref_data[:, 0], ref_data[:, 1], bounds_error=False, fill_value=numpy.nan) ref_flux = ref_interp(obs_wave) std_ext = extinct_interp(obs_wave) good_inds = numpy.nonzero( (ref_flux == ref_flux) * (std_ext == std_ext) * (obs_wave >= wave_min) * (obs_wave <= wave_max) * (obs_flux > 0.0))[0] init_flux_ratio = -2.5 * numpy.log10( obs_flux[good_inds] / ref_flux[good_inds]) flux_ratio = init_flux_ratio + (secz - 1.0) * std_ext[good_inds] fratio_results.append([obs_wave[good_inds], init_flux_ratio]) if plot_stars: scaled_flux = obs_flux[good_inds] / numpy.mean( 10.0**(-0.4 * flux_ratio)) pylab.figure() pylab.plot(obs_wave, ref_flux, color='b', label='Reference star flux') pylab.plot(obs_wave[good_inds], scaled_flux, color='r', label='Scaled observed flux') pylab.title(star_name) pylab.xlabel(r'Wavelength [$\AA$]') pylab.legend(loc='lower right', fancybox=True, shadow=True) # from all comparisons, derive a calibration solution # EVENTUALLY WILL FIT AN EXTINCTION TERM TOO if norm_stars: i_mid = len(fratio_results[0][0]) / 2 fscale_max = min([x[1][i_mid] for x in fratio_results]) init_full_y = numpy.concatenate( [x[1] - x[1][i_mid] + fscale_max for x in fratio_results]) else: init_full_y = numpy.concatenate([x[1] for x in fratio_results]) init_full_x = numpy.concatenate([x[0] for x in fratio_results]) init_good_inds = numpy.nonzero( (init_full_y == init_full_y) * (init_full_y < numpy.median(init_full_y) + 20.0) * (strong_telluric_mask(init_full_x)) * (halpha_mask(init_full_x)))[0] # do a first fit next_full_y = init_full_y[init_good_inds] next_full_x = init_full_x[init_good_inds] sort_order = next_full_x.argsort() temp_full_x = next_full_x[sort_order] temp_full_y = next_full_y[sort_order] # ----------- Fred's update 3 ------------------- if method == 'smooth_SG': # Savitzky-Golay requires continuous data. ->need to fill the 'holes' # It is a problem for red spectra (at this point at least) # Check if there are gaps (telluric, Halpha, etc ...) init_bad_inds = \ numpy.nonzero(1- ((init_full_y == init_full_y)* (init_full_y < numpy.median(init_full_y)+20.0)* (telluric_mask(init_full_x))* (halpha_mask(init_full_x))))[0] if len(init_bad_inds) > 0: # if yes, first fit a polynomial, then use it to 'fill the gaps. temp_calib = numpy.polyfit(temp_full_x, temp_full_y, polydeg) temp_fvals = numpy.polyval(temp_calib, init_full_x) init_full_y[init_bad_inds] = temp_fvals[init_bad_inds] temp_full_y = init_full_y # to ensure this case is then compatible temp_full_x = init_full_x # Fails if multiple stars ... need to order the array ! this_sort_order = temp_full_x.argsort() temp_full_x = temp_full_x[this_sort_order] temp_full_y = temp_full_y[this_sort_order] # Then fit SG normally temp_fvals = savitzky_golay(temp_full_y, 101, 1, 0) excise_cut = 0.003 else: temp_best_calib = numpy.polyfit(temp_full_x, temp_full_y, polydeg) temp_fvals = numpy.polyval(temp_best_calib, temp_full_x) # excise outliers final_good_inds = numpy.nonzero( numpy.abs(temp_fvals - temp_full_y) / numpy.abs(temp_fvals) < excise_cut)[0] full_x = temp_full_x[final_good_inds] full_y = temp_full_y[final_good_inds] # ------------ Fred's update 3 ---------------- if method == 'smooth_SG': # Fails if multiple stars ... need to order the array ! this_sort_order = full_x.argsort() full_x = full_x[this_sort_order] full_y = full_y[this_sort_order] final_fvals = savitzky_golay(full_y, 101, 1, 0) this_f = scipy.interpolate.interp1d(full_x, final_fvals, bounds_error=False, kind='linear') all_final_fvals = this_f(init_full_x) final_x = full_x final_y = full_y else: best_calib = numpy.polyfit(full_x, full_y, polydeg) final_fvals = numpy.polyval(best_calib, full_x) final_x = numpy.arange( numpy.min(full_x), 1.000001 * numpy.max(full_x), 0.0001 * (numpy.max(full_x) - numpy.min(full_x))) final_y = numpy.polyval(best_calib, final_x) # plot if requested if plot_sensf: pylab.figure() # MC update - raw fit on top pylab.axes([0.10, 0.35, 0.85, 0.60]) pylab.plot(temp_full_x, temp_full_y, 'r.', markerfacecolor='none', markeredgecolor='r', label='Raw sensitivity (initial regions)') pylab.plot(full_x, full_y, color='b', label='Raw sensitivity (valid regions)') pylab.plot(temp_full_x, temp_fvals, color=r'#FF6103', lw=2, label='Initial fit') if method == 'smooth_SG': pylab.plot(init_full_x, all_final_fvals, color=r'#00FF00', lw=2, label='Final fit') else: pylab.plot(full_x, final_fvals, color=r'#00FF00', lw=2, label='Final fit') #pylab.hlines(-37.5,numpy.min(full_x),numpy.max(full_x), 'k') pylab.xlim([numpy.min(full_x), numpy.max(full_x)]) curr_ylim = pylab.ylim() curr_xlim = pylab.xlim() pylab.ylim(curr_ylim[::-1]) pylab.title('Derived sensitivity function') pylab.legend(loc='lower right', fancybox=True, shadow=True) # lower plot - residuals! pylab.axes([0.10, 0.10, 0.85, 0.25]) pylab.plot(full_x, full_y - final_fvals, 'k.', mec=r'#666666', markerfacecolor='none', label='Residuals') pylab.axhline(0.0, color='k') pylab.xlim(curr_xlim) pylab.ylim([-0.2, 0.2]) pylab.xlabel(r'Wavelength [$\AA$]') pylab.ylabel('Residuals') if plot_stars or plot_sensf: pylab.show() # Fred's update ... now, careful, because that's dirty ... # the function does not always return the same thing ! # SAVE IN THE PICKLE FILE THE WAVELENGTH AND CALIB FVAL ARRAYS save_calib = {'wave': final_x, 'cal': final_y} f1 = open(calib_out_fn, 'w') pickle.dump(save_calib, f1) f1.close() return
# intial guesses xhat[0] = 0.0 P[0] = 1.0 for k in range(1,n_iter): # time update xhatminus[k] = xhat[k-1] Pminus[k] = P[k-1]+Q # measurement update K[k] = Pminus[k]/( Pminus[k]+R ) xhat[k] = xhatminus[k]+K[k]*(l[k]-xhatminus[k]) P[k] = (1-K[k])*Pminus[k] pylab.figure() pylab.plot(l,'k+',label='noisy measurements') pylab.plot(xhat,'b-',label='a posteri estimate') pylab.axhline(x,color='g',label='truth value') pylab.legend() pylab.xlabel('Iteration') pylab.ylabel('Voltage') pylab.figure() valid_iter = range(1,n_iter) # Pminus not valid at step 0 pylab.plot(valid_iter,Pminus[valid_iter],label='a priori error estimate') pylab.xlabel('Iteration') pylab.ylabel('$(Voltage)^2$') pylab.setp(pylab.gca(),'ylim',[0,.01]) pylab.show()
print('Mean of rolling 100 dice =', round(mean, 4), 'Variance =', round(var, 4)) pylab.title('Rolling Continuous Dice') pylab.xlabel('Value') pylab.ylabel('Probability') pylab.legend() #Figure 17.10 has no code #Figure 17.11 times = getBMData('bm_results2012.txt')['time'] meanOfMeans, stdOfMeans = [], [] sampleSizes = range(50, 2000, 200) for sampleSize in sampleSizes: sampleMeans = [] for t in range(20): sample = random.sample(times, sampleSize) sampleMeans.append(sum(sample) / sampleSize) meanOfMeans.append(sum(sampleMeans) / len(sampleMeans)) stdOfMeans.append(stdDev(sampleMeans)) pylab.errorbar(sampleSizes, meanOfMeans, yerr=1.96 * pylab.array(stdOfMeans), label='Estimated mean and 95% confidence interval') pylab.xlim(0, max(sampleSizes) + 50) pylab.axhline(sum(times) / len(times), linestyle='--', label='Population mean') pylab.title('Estimates of Mean Finishing Time') pylab.xlabel('Sample Size') pylab.ylabel('Finshing Time (minutes)') pylab.legend(loc='best')
# new State sB = snB # Data append states.append(sB[1] * R2D) command.append(uB[1] * R2D) time.append(timer) Lat.append(sB[5] * R2D) Long.append(sB[6] * R2D) Alt.append(sB[7]) timer = timer + dt if timer > Epoch: done = True # Plot pylab.figure(1) pylab.plot(time, states, label='Theta State') pylab.plot(time, command, label='Theta Command') pylab.axhline(theta_cmd * R2D, color='k', linestyle='dashed', linewidth=1, label='Desire Theta') pylab.xlabel('time') pylab.ylabel('angle') pylab.grid() pylab.legend() pylab.figure(2) pylab.gca(projection='3d') pylab.plot(Lat, Long, Alt) pylab.title('') pylab.show()
def identify_spectra(spectra, outname=None, low=-np.inf, hi=np.inf, plot=False): raise Exception("This code is outdated") ms = [] ixs = [] segids = [] ds9 = "physical\n" for ix, spectrum in enumerate(spectra): if spectrum.__dict__.has_key('spec') and spectrum.spec is not None \ and spectrum.lamcoeff is not None: ixs.append(ix) segids.append(spectrum.seg_id) try: l, s = spectrum.get_counts(the_spec='specw') except: ms.append(0) continue ms.append(np.median(s)) X = spectrum.X_as Y = spectrum.Y_as ds9 += 'point(%s,%s) # point=cross text={%s:%4.2f}\n' % \ (X,Y, segids[-1],ms[-1]) ixs = np.array(ixs) ms = np.array(ms) bgd = np.median(ms) sd = np.std(ms) segids = np.array(segids) ms -= bgd ok = (ms <= sd * low) | (ms >= sd * hi) pl.figure(1) pl.clf() bgd = (ms > sd * low) & (ms < sd * hi) pl.plot(segids[bgd], ms[bgd], '.') pl.plot(segids[ok], ms[ok], 'x') pl.axhline(sd * low, color='orange') pl.axhline(sd * hi, color='red') pl.xlabel("Sextractor Segment ID number") pl.ylabel("Median spectral irradiance [photon/10 m/nm]") pl.legend(["Bgd spectra", "Selected spectra"]) pl.grid(True) if outname is not None: pl.savefig("selected_%s.pdf" % outname) if plot: pl.show() f = open(outname + ".reg", "w") f.write(ds9) f.close() to_image(outname) return ixs[ok]
top10_scores = np.zeros(*coefs[0].shape) for i in range(9): top50_scores += (np.abs(coefs[i]) >= ord_coef[i, -50]) * 1 top25_scores += (np.abs(coefs[i]) >= ord_coef[i, -25]) * 1 top10_scores += (np.abs(coefs[i]) >= ord_coef[i, -10]) * 1 #%% Plotting sorted scores fig = plt.figure(figsize=(20, 20)) for i, coef in enumerate(coefs): plt.subplot(3, 3, i + 1) coef = np.sort(np.abs(coef)) plt.plot(coef[::-1], ) plt.axvline(x=50, color='r') plt.axhline(y=coef[-50], color='g', label='50 feat threshold = {:.3}'.format(coef[-50])) plt.legend(fontsize=12) plt.title("Reg Coef for {!s:.12} + {!s:.10}".format( str(pipes[i][0]).split("(")[0], str(pipes[i][1]).split("CV")[0]), fontsize=15) plt.tight_layout() plt.savefig(pj(scripts_dir, 'NineCoefPlot.png'), bbox_inches='tight') #%% #Writing the dataframe with the scores of different top ranks scores_df = pd.DataFrame( [top50_scores, top25_scores, top10_scores], columns=data_train.loc[:, 'lh_bankssts_area':'rh.Whole_hippocampus'].columns) scores_df = scores_df.T
return omega def f2(teta): return -(9.81 * np.sin(teta)) for i in range(0, 100): w1 = w1 + p * f2(a1) w2 = w2 + p * f2(a2) a1 = a1 + p * f1(w1) a2 = a2 + p * f1(w2) count.append(i) Wx.append(w1) Wy.append(w2) Ax.append(a1) Ay.append(a2) margemh = (np.amax(Wx) - np.amin(Wx)) / 20 margemv = (np.amax(Wy) - np.amin(Wy)) / 20 pyl.plot(count, Ax, 'b-', label='W') pyl.plot(count, Ay, 'r--', label='A') pyl.title('W e A') pyl.legend(loc='lower left') pyl.grid(True) pyl.axhline(y=0, color='k') pyl.axvline(x=0, color='k') pyl.show() pyl.close()
def execs_timeit_2vector(exprs, fname=None): """ exprs is a list of list of expr to evaluate The first level of list is put into different graph section in the same graph. The second level is the expression to put in each section """ #exp = [(1,100000),(1e1,100000),(1e2,100000),(1e3,100000), (5e3,50000), exp = [(1e3,100000),(5e3,50000), \ (1e4,10000),(5e4,5000),(1e5,2000),(1e6,200),(1e7,10) ] ### TO TEST UNCOMMENT THIS LINE # exp = [(1,1000),(1e1,1000),(1e2,1000),] times = [] str_expr = [] for g_exprs in exprs: for expr in g_exprs: nb_call_scal = 1 if isinstance(expr, tuple): nb_call_scal = expr[1] expr = expr[0] str_expr.append(expr) time = [] for nb_e, nb_c in exp: time.append( timeit_2vector(nb_element=nb_e, nb_repeat=3, nb_call=nb_c * nb_call_scal, expr=expr, do_amd=False)) times.append(time) if 'pylab' not in globals(): return nb_calls = [e[0] for e in exp] legends = [] colors = ['b', 'r', 'g', 'c', 'm', 'y'] assert len(colors) >= len(times) fig = pylab.figure() for idx, (time, expr) in enumerate(zip(times, str_expr)): ### ### ### # Creating each subplot ### ### ### ### pylab.subplot(220 + idx + 1) pylab.subplots_adjust(wspace=0.25, hspace=0.25) #legend=[] #plot = fig.add_subplot(1,len(exprs),idx) speedup = [t["numpy"].min() / t["numexpr"].min() for t in time] pylab.semilogx(nb_calls, speedup, linewidth=1.0, color='r') speedup = [t["numpy"].min() / t["theano"].min() for t in time] pylab.semilogx(nb_calls, speedup, linewidth=1.0, color='b') pylab.grid(True) if (idx == 2) or (idx == 3): pylab.xlabel('Dimension of vectors a and b', fontsize=15) if (idx == 0) or (idx == 2): pylab.ylabel('Speed up vs NumPy', fontsize=15) pylab.axhline(y=1, linewidth=1.0, color='black') pylab.xlim(1e3, 1e7) pylab.xticks([1e3, 1e5, 1e7], ['1e3', '1e5', '1e7']) pylab.title(expr) if fname: fig.savefig(fname) pylab.clf() else: pylab.show()
def normalized_plot(what, alphaGammaName, N=1000, stride=50, aHi=[], legendAlpha=.5): ''' Create normalized plot as it appears in :cite:`Maugis1992` including range of axes with normalized quantities. This function is mainly useful for documentation of Woo itself. :param what: one of ``a(delta)``, ``F(delta)``, ``a(F)`` :param alphaGammaName: list of (alpha,gamma,name) tuples which are passed to the :obj:`SchwarzModel` constructor :param N: numer of points in linspace for each axis :param stride: stride for plotting inverse relationships (with points) :param aHi: with ``what==a(delta)``, show upper bracket for $a$ for i-th model, if *i* appears in the list ''' assert what in ('a(delta)', 'F(delta)', 'a(F)') import pylab invKw = dict(linewidth=4, alpha=.3) kw = dict(linewidth=2) pylab.figure() for i, (alpha, gamma, name) in enumerate(alphaGammaName): m = SchwarzModel.makeDefault(alpha=alpha, gamma=gamma, name=name) if what == 'a(delta)': ddHat = numpy.linspace(-1.3, 2., num=N) aaHat = numpy.linspace(0, 2.1, num=N) pylab.plot(m.deltaHat_aHat(aaHat), aaHat, label='$\\alpha$=%g %s' % (m.alpha, m.name), **kw) ddH = m.deltaHat_aHat(aaHat) if i in aHi: pylab.plot( ddH, [m.aHat(m.aHi(m.deltaUnhat(dh))) for dh in ddH], label='$\\alpha$=%g $a_{\mathrm{hi}}$' % m.alpha, linewidth=1, alpha=.4) pylab.plot(ddHat[::stride], m.aHat_deltaHat(ddHat[::stride], loading=True), 'o', **invKw) pylab.plot(ddHat[::stride], m.aHat_deltaHat(ddHat[::stride], loading=False), 'o', **invKw) elif what == 'F(delta)': ffHat = numpy.linspace(-2.1, 1.5, num=N) ddHat = numpy.linspace(-1.3, 2., num=N) pylab.plot(ddHat, m.fHat_aHat(m.aHat_deltaHat(ddHat)), label='$\\alpha$=%g %s' % (m.alpha, m.name), **kw) pylab.plot(ddHat, m.fHat_aHat(m.aHat_deltaHat(ddHat, loading=False)), **kw) pylab.plot([ m.deltaHat(m.delta_a(m.a_F(m.fUnhat(fh))[0])) for fh in ffHat[::stride] ], ffHat[::stride], 'o', **invKw) pylab.plot([ m.deltaHat(m.delta_a(m.a_F(m.fUnhat(fh))[1])) for fh in ffHat[::stride] ], ffHat[::stride], 'o', **invKw) elif what == 'a(F)': aaHat = numpy.linspace(0, 2.5, num=N) ffHat = numpy.linspace(-3, 4, num=N) pylab.plot(ffHat, [a[0] for a in m.aHat_fHat(ffHat)], label='$\\alpha$=%g %s' % (m.alpha, m.name), **kw) pylab.plot(ffHat, [a[1] for a in m.aHat_fHat(ffHat)], **kw) pylab.plot(m.fHat_aHat(aaHat[::stride]), aaHat[::stride], 'o', label=None, **invKw) if what == 'a(delta)': ddH = [ m.deltaHat(HertzModel.delta_a(m, m.aUnhat(a))) for a in aaHat ] pylab.plot(ddH, aaHat, label='Hertz', **kw) pylab.xlabel('$\hat\delta$') pylab.ylabel('$\hat a$') pylab.ylim(ymax=2.1) pylab.xlim(xmax=2.) elif what == 'F(delta)': pylab.plot(ddHat, [ m.fHat(HertzModel.F_delta(m, m.deltaUnhat(d))) for d in ddHat ], label='Hertz', **kw) pylab.xlabel('$\hat\delta$') pylab.ylabel('$\hat P$') pylab.xlim(-1.3, 2) pylab.ylim(-2.1, 2) pylab.axhline(color='k') pylab.axvline(color='k') elif what == 'a(F)': pylab.plot(ffHat, [m.aHat(HertzModel.a_F(m, m.fUnhat(f))) for f in ffHat], label='Hertz', **kw) pylab.xlabel('$\hat P$') pylab.ylabel('$\hat a$') pylab.xlim(xmax=4) pylab.grid(True) try: pylab.legend(loc='best', framealpha=legendAlpha) except: pylab.legend(loc='best')
lineR, = pylab.plot(X,psi_r[PR],'b',alpha=0.7,label='Real') lineI, = pylab.plot(X,psi_i[PR],'r',alpha=0.7,label='Imag') lineP, = pylab.plot(X,6*psi_p,'k',label='Prob') pylab.title('Potential height: %.2e' % V0) # For non-zero potentials, plot them and shade the classically forbidden region # in light red, as well as drawing a green line at the wavepacket's total # energy, in the same units the potential is being plotted. if Vmax !=0 : # Scaling factor for energies, so they fit in the same plot as the # wavefunctions Efac = ymax/2.0/Vmax V_plot = V*Efac pylab.plot(X,V_plot,':k',zorder=0) # Potential line. fillax(X,V_plot, facecolor='y', alpha=0.2,zorder=0) # Plot the wavefunction energy, in the same scale as the potential pylab.axhline(E*Efac,color='g',label='Energy',zorder=1) pylab.legend(loc='lower right') pylab.draw() # I think there's a problem with pylab, because it resets the xlim after # plotting the E line. Fix it back manually. pylab.xlim(xmin,xmax) # Direct index assignment is MUCH faster than using a spatial FOR loop, so # these constants are used in the update equations. Remember that Python uses # zero-based indexing. IDX1 = range(1,N-1) # psi [ k ] IDX2 = range(2,N) # psi [ k + 1 ] IDX3 = range(0,N-2) # psi [ k - 1 ] for t in range(T+1): # Precompute a couple of indexing constants, this speeds up the computation psi_rPR = psi_r[PR] psi_iPR = psi_i[PR]
def exec_timeit_2vector(expr, nb_call_scal=1, fname=None, do_unalign=False, do_amd=True): #exp = [(1,100000),(1e1,100000),(1e2,100000),(1e3,100000), (5e3,50000), exp = [(1e3,100000),(5e3,50000), \ (1e4,10000),(5e4,5000),(1e5,2000),(1e6,200),(1e7,10) ] exp = [(1e3, 100000), (5e3, 50000)] runtimes = [] for nb_e, nb_c in exp: runtimes.append( timeit_2vector(nb_element=nb_e, nb_repeat=3, nb_call=nb_c * nb_call_scal, expr=expr, do_amd=do_amd)) if do_unalign: runtimes_unalign = [] for nb_e, nb_c in exp: runtimes_unalign.append( timeit_2vector(nb_element=nb_e, nb_repeat=3, nb_call=nb_c * nb_call_scal, expr=expr, do_unalign=True, do_amd=do_amd)) print('Runtimes list = ', runtimes) numexpr_speedup = np.asarray( [t['numpy'].min() / t['numexpr'].min() for t in runtimes], "float32") print("time(NumPy) / time(numexpr)", end=' ') print(numexpr_speedup, numexpr_speedup.min(), numexpr_speedup.max()) theano_speedup = np.asarray( [t['numpy'].min() / t['theano'].min() for t in runtimes], "float32") print("time(NumPy) / time(Theano)", end=' ') print(theano_speedup, theano_speedup.min(), theano_speedup.max()) theano_numexpr_speedup = np.asarray( [t['numexpr'].min() / t['theano'].min() for t in runtimes], "float32") print("time(numexpr) / time(Theano)", end=' ') print(theano_numexpr_speedup, theano_numexpr_speedup.min(), theano_numexpr_speedup.max()) if do_amd: theano_speedup2 = np.asarray( [t['numpy'].min() / t['theano_amd'].min() for t in runtimes], "float32") print("time(NumPy) / time(theano+amdlibm)", end=' ') print(theano_speedup, theano_speedup.min(), theano_speedup.max()) theano_numexpr_speedup2 = np.asarray( [t['numexpr'].min() / t['theano_amd'].min() for t in runtimes], "float32") print("time(numexpr) / time(theano+amdlibm)", end=' ') print(theano_numexpr_speedup, theano_numexpr_speedup.min(), theano_numexpr_speedup.max()) if 'pylab' not in globals(): return nb_calls = [e[0] for e in exp] for cmp in range(1, len(time[0])): speedup = np.asarray([t[0].min() / t[cmp].min() for t in time], "float32") pylab.semilogx(nb_calls, speedup, linewidth=1.0) if do_unalign: for cmp in range(1, len(time[0])): speedup = np.asarray( [t[0].min() / t[cmp].min() for t in time_unalign], "float32") pylab.semilogx(nb_calls, speedup, linewidth=1.0) pylab.axhline(y=1, linewidth=1.0, color='black') pylab.xlabel('Dimension of real valued vectors a and b') pylab.ylabel('Speed up vs NumPy') if do_unalign and do_amd: pylab.legend( ("Numexpr", "Theano", "Theano(amdlibm)", "Numexpr(unalign)", "Theano(unalign)", "Theano(amdlibm,unalign)"), loc='upper left') elif do_unalign and not do_amd: pylab.legend(( "Numexpr", "Theano", "Numexpr(unalign)", "Theano(unalign)", ), loc='upper left') elif not do_unalign and do_amd: pylab.legend(("Numexpr", "Theano", "Theano(amdlibm)"), loc='upper left') else: pylab.legend(("Numexpr", "Theano"), loc='upper left') pylab.grid(True) if fname: pylab.savefig(fname) pylab.clf() else: pylab.show()
import CoolProp as CP import pylab, numpy as np from ACHP.Correlations import ShahEvaporation_Average x = np.linspace(0, 1, 1000) h = np.zeros_like(x) TsatL, TsatV = 300., 300. AS = CP.AbstractState('HEOS', 'R134a') AS.update(CP.QT_INPUTS, 0.0, TsatL) p = AS.p() #[Pa] for i in range(len(x)): h[i] = ShahEvaporation_Average(x[i], x[i], AS, 300, 0.01, p, 200, TsatL, TsatV) havg = np.trapz(h, x=x) pylab.figure(figsize=(7, 5)) pylab.axhline(havg, ls='--') pylab.text(0.2, havg, r'$\alpha_{avg}$', ha='center', va='bottom') pylab.text( 0.7, 1300, 'R134a\nG=300 kg/m$^2$\nD=0.01 m\nq\"=200 W/m$^2$\np=%0.1f kPa' % (p / 1000)) pylab.gca().set_xlabel('x [-]') pylab.gca().set_ylabel(r'$\alpha$ [W/m$^2$/K]') pylab.plot(x, h) pylab.title('Shah Evaporation HTC as a function of quality') pylab.show()
svr_mse=[float(r[6])/float(r[5])for r in svr[1:] if int(r[1])==component] eavg_mse=[float(eavg[i][2])/float(svr[i][5]) for i in range(1,len(eavg)) if int(eavg[i][1])==component] pl.figure(figsize=(5,2.5)) for i in range(len(svr_r2)): pl.plot((svr_r2[i],eavg_r2[i]),(svr_mse[i],eavg_mse[i]), color='black',alpha=0.4) pl.plot(eavg_r2,eavg_mse,'o',color='blue',alpha=0.3) pl.plot(svr_r2,svr_mse,'*',color='red') pl.ylabel('NRMSE') pl.xlabel('R-squared') pl.subplots_adjust(left=0.2,bottom=0.2) pl.yscale('log') pl.xlim((-2.1,1.1)) pl.ylim((0.001,10)) pl.text(-1.8,2,names[component-1],fontsize=14) pl.axhline(y=0.2,ls=':',color='grey') pl.axvline(x=0.5,ls=':',color='grey') pl.savefig('all_scatter_both_'+str(month)+'_'+str(component)+'.pdf') pl.close() ####performance globally month=10 svr=list(csv.reader(open('all_users_global'+str(month)+'_noneg.csv','r'))) eavg=list(csv.reader(open('all_users_global'+str(month)+'avg.csv','r')))#this is the EAM performance - obtain by running one_user_apply.py with no models trained svr_r2=[max(float(r[4]),-2)for r in svr[1:] ] eavg_r2=[max(float(eavg[i][4]),-2) for i in range(1,len(eavg)) ] svr_mse=[float(r[3])/float(r[2])for r in svr[1:]]