def wykres(katalog): import numpy as np import os import scipy.io.wavfile as siw import json import scipy.fftpack as sf import matplotlib.pylab as plt os.chdir(katalog) song = open('song.txt', 'r') songlist = list() defs = json.load(open('defs.txt', 'r')) beat = defs["bpm"] nframes = 60/beat*44100 for songline in song: track = open('track'+songline.strip('\n')+'.txt', 'r') tracklist = list() for trackline in track: y = np.zeros([nframes, 2]) for i in range(len(trackline.split())): y = y + siw.read('sample'+''.join(trackline.split()[i])+'.wav')[1][0:nframes] y = np.mean(y,axis=1) y /= 32767 tracklist = np.r_[tracklist, y] songlist = np.r_[songlist, tracklist] yf = sf.fft(songlist) n = yf.size xf = np.linspace(0, 44100/2, n/2) plt.plot(xf, 2*(yf[1:int(n/2)+1])/n) plt.xscale('log') plt.show()
def nova_plot(): erg2mev=624151. fig=plot.figure() yrange = [1e-6,2e-4] xrange = [1e-1,1e5] plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5) plot.annotate('AMEGO',xy=(3,9e-5),xycoords='data',fontsize=26,color='black') lat=ascii.read("data/NMon2012.LAT.dat",names=['energy','en_low','en_high','flux','flux_err','tmp']) plot.scatter(lat['energy'],lat['flux']*erg2mev,color='red') plot.errorbar(lat['energy'],lat['flux']*erg2mev,xerr=[lat['en_low'],lat['en_high']],yerr=lat['flux_err']*erg2mev,ecolor='red',capsize=0,fmt='none') latul=ascii.read("data/NMon2012.LAT.limits.dat",names=['energy','en_low','en_high','flux','tmp1','tmp2','tmp3','tmp4']) plot.errorbar(latul['energy'],latul['flux']*erg2mev,xerr=[latul['en_low'],latul['en_high']],yerr=0.5*latul['flux']*erg2mev,uplims=True,ecolor='red',capsize=0,fmt='none') plot.scatter(latul['energy'],latul['flux']*erg2mev,color='red') leptonic=ascii.read("data/sp-NMon12-IC-best-fit-1MeV-30GeV.txt",names=['energy','flux'],data_start=1) hadronic=ascii.read("data/sp-NMon12-pi0-and-secondaries.txt",names=['energy','flux1','flux2'],data_start=1) plot.plot(leptonic['energy'],leptonic['flux']*erg2mev,'r--',color='black',lw=2,label='Leptonic') plot.plot(hadronic['energy'],hadronic['flux2']*erg2mev,color='black',lw=2,label='Hadronic+Secondary Leptons') plot.legend(loc='upper right',fontsize='small',frameon=False,framealpha=0.5) plot.xscale('log') plot.yscale('log') plot.ylim(yrange) plot.xlim(xrange) plot.xlabel(r'Energy (MeV)') plot.ylabel(r'Energy$^2 \times $ Flux (Energy) (erg cm$^{-2}$ s$^{-1}$)') plot.title('Nova V339 Del 2013') plot.savefig('Nova_SED.png', bbox_inches='tight') plot.savefig('Nova_SED.eps', bbox_inches='tight') plot.show() plot.close()
def plot_degreeRate(db, keynames, save_path): degRate_x_name = 'degRateDistr_x' degRate_y_name = 'degRateDistr_y' plt.clf() plt.figure(figsize = (8, 5)) plt.subplot(1, 2, 1) plt.plot(db[keynames['mog']][degRate_x_name], db[keynames['mog']][degRate_y_name], 'b-', lw=5, label = 'fairyland') plt.plot(db[keynames['mblg']][degRate_x_name], db[keynames['mblg']][degRate_y_name], 'r:', lw=5, label = 'twitter') plt.plot(db[keynames['im']][degRate_x_name], db[keynames['im']][degRate_y_name], 'k--', lw=5, label = 'yahoo') plt.xscale('log') plt.grid(True) plt.title('interaction') plt.legend(('fairyland', 'twitter', 'yahoo'), loc = 4, prop = {'size': 10}) plt.xlabel('In-degree to Out-degree Ratio') plt.ylabel('CDF') plt.subplot(1, 2, 2) plt.plot(db[keynames['mogF']][degRate_x_name], db[keynames['mogF']][degRate_y_name], 'b-', lw=5, label = 'fairyland') plt.plot(db[keynames['mblgF']][degRate_x_name], db[keynames['mblgF']][degRate_y_name], 'r:', lw=5, label = 'twitter') #plt.plot(db[keynames['imF']][degRate_x_name], db[keynames['imF']][degRate_y_name], 'k--', lw=5, label = 'yahoo') plt.xscale('log') plt.grid(True) plt.title('ally') plt.xlabel('In-degree to Out-degree Ratio') plt.ylabel('CDF') plt.savefig(os.path.join(save_dir, save_path))
def plotFeaturePDF(ift, pft, outbase, fmin=0.0, fmax=1.0, fstep=0.01): """ Plot a comparison between the input feature distribution and the feature distribution of the predicted halos """ plt.clf() nfbins = ( fmax - fmin ) / fstep fbins = np.logspace( fmin, fmax, nfbins ) fcen = ( fbins[:-1] + fbins[1:] ) / 2 plt.xscale( 'log', nonposx='clip' ) plt.yscale( 'log', nonposy='clip' ) ic, e, p = plt.hist( ift, fbins, label='Original Halos', alpha=0.5, normed=True ) pc, e, p = plt.hist( pft, fbins, label='Added Halos', alpha=0.5, normed=True ) plt.legend() plt.xlabel( r'$\delta$' ) plt.savefig( outbase+'_fpdf.png' ) fdtype = np.dtype( [ ('fcen', float), ('ifcounts', float), ('pfcounts', float) ] ) fd = np.ndarray( len(fcen), dtype = fdtype ) fd[ 'mcen' ] = fcen fd[ 'imcounts' ] = ic fd[ 'pmcounts' ] = pc fitsio.write( outbase+'_fpdf.fit', fd )
def plotMassFunction(im, pm, outbase, mmin=9, mmax=13, mstep=0.05): """ Make a comparison plot between the input mass function and the predicted projected correlation function """ plt.clf() nmbins = ( mmax - mmin ) / mstep mbins = np.logspace( mmin, mmax, nmbins ) mcen = ( mbins[:-1] + mbins[1:] ) /2 plt.xscale( 'log', nonposx = 'clip' ) plt.yscale( 'log', nonposy = 'clip' ) ic, e, p = plt.hist( im, mbins, label='Original Halos', alpha=0.5, normed = True) pc, e, p = plt.hist( pm, mbins, label='Added Halos', alpha=0.5, normed = True) plt.legend() plt.xlabel( r'$M_{vir}$' ) plt.ylabel( r'$\frac{dN}{dM}$' ) #plt.tight_layout() plt.savefig( outbase+'_mfcn.png' ) mdtype = np.dtype( [ ('mcen', float), ('imcounts', float), ('pmcounts', float) ] ) mf = np.ndarray( len(mcen), dtype = mdtype ) mf[ 'mcen' ] = mcen mf[ 'imcounts' ] = ic mf[ 'pmcounts' ] = pc fitsio.write( outbase+'_mfcn.fit', mf )
def test_simple_gen(self): self_con = .8 other_con = 0.05 g = self.gen.gen_stoch_blockmodel(min_degree=1, blocks=5, self_con=self_con, other_con=other_con, powerlaw_exp=2.1, degree_seq='powerlaw', num_nodes=1000, num_links=3000) deg_hist = vertex_hist(g, 'total') res = fit_powerlaw.Fit(g.degree_property_map('total').a, discrete=True) print 'powerlaw alpha:', res.power_law.alpha print 'powerlaw xmin:', res.power_law.xmin if len(deg_hist[0]) != len(deg_hist[1]): deg_hist[1] = deg_hist[1][:len(deg_hist[0])] print 'plot degree dist' plt.plot(deg_hist[1], deg_hist[0]) plt.xscale('log') plt.xlabel('degree') plt.ylabel('#nodes') plt.yscale('log') plt.savefig('deg_dist_test.png') plt.close('all') print 'plot graph' pos = sfdp_layout(g, groups=g.vp['com'], mu=3) graph_draw(g, pos=pos, output='graph.png', output_size=(800, 800), vertex_size=prop_to_size(g.degree_property_map('total'), mi=2, ma=30), vertex_color=[0., 0., 0., 1.], vertex_fill_color=g.vp['com'], bg_color=[1., 1., 1., 1.]) plt.close('all') print 'init:', self_con / (self_con + other_con), other_con / (self_con + other_con) print 'real:', gt_tools.get_graph_com_connectivity(g, 'com')
def test_power_spectra(r0, N, delta, L0, l0): N*= 10 phase_screen = atmosphere.ft_phase_screen(r0, N, delta, L0, l0) phase_screen = phase_screen[:N/10, :N/10] power_spec_2d = numpy.fft.fft2(phase_screen, s=(N*2, N*2)) plt.figure() plt.imshow(numpy.abs(numpy.fft.fftshift(power_spec_2d)), interpolation='nearest') power_spec = circle.aziAvg(numpy.abs(numpy.fft.fftshift(power_spec_2d))) power_spec /= power_spec.sum() freqs = numpy.fft.fftfreq(power_spec_2d.shape[0], delta) # Theoretical Model of Power Spectrum print freqs plt.figure() plt.plot(freqs[:freqs.size/2], power_spec) plt.xscale('log') plt.yscale('log') plt.show() return None
def plot_ccdf(values, xscale, yscale): pylab.yscale(yscale) cdf = Cdf.MakeCdfFromList(values) values, prob = cdf.Render() pylab.xscale(xscale) compProb = [1 - e for e in prob] pylab.plot(values, compProb)
def plot_scatter(times): pl.scatter(times[:, 0], times[:, 1]) pl.xlabel('Time in seconds') pl.ylabel('Ratio') pl.title('Time in seconds versus the ratio') pl.xlim(0, 300) pl.xscale('log') pl.show()
def plot_times(): res = [] with open('times', 'r') as f: for l in f.readlines(): x = eval(l) res += [(x['samples'], x['auto'], x['tri'])] res = list(zip(*res)) pl_auto = pl.plot(res[0], res[1]) pl_tri = pl.plot(res[0], res[2]) pl.xscale('log') pl.ylabel("time (in seconds)") pl.xlabel("points (logarithmic scale)") pl.legend((pl_auto[0], pl_tri[0]), ('auto', 'tri')) pl.show()
def doPlot(parallel_aucs, serial_aucs, times, errors): times = list(times) times_histo = np.histogram(parallel_aucs,bins=times) #values,edges = times_histo parallel_values = parallel_aucs[1:] edges = times print(len(parallel_values)) print(len(edges)) serial_values = np.array(serial_aucs[1:]) errors = np.array(errors[1:]) edges = np.array(times[:-1]) print(errors.shape) print(edges.shape) print(serial_values.shape) plt.figure() plt.plot(edges, parallel_values,label = "Distributed search") #, width=np.diff(edges), ec="k", align="edge") plt.plot(edges, serial_values, label="Sequential search") #, width=np.diff(edges), ec="k", align="edge") #plt.fill_between(edges, serial_values-errors,serial_values+errors) plt.legend(loc = (0.6,0.7)) plt.xlabel("Time [minutes]", fontsize=20) #plt.yscale('log') plt.ylabel('Best validation AUC', fontsize=20) plt.savefig("times.png") plt.figure() plt.plot(edges, parallel_values,label = "Distributed search") #, width=np.diff(edges), ec="k", align="edge") plt.plot(edges, serial_values, label="Sequential search") #, width=np.diff(edges), ec="k", align="edge") #plt.fill_between(edges, serial_values-errors,serial_values+errors) plt.legend(loc = (0.6,0.7)) plt.xlabel("Time [minutes]", fontsize=20) plt.xscale('log') plt.xlim([0,100]) plt.ylabel('Best validation AUC', fontsize=20) plt.savefig("times_logx_start.png") plt.figure() plt.plot(edges, parallel_values,label = "Distributed search") #, width=np.diff(edges), ec="k", align="edge") plt.plot(edges, serial_values, label="Sequential search") #, width=np.diff(edges), ec="k", align="edge") #plt.fill_between(edges, serial_values-errors,serial_values+errors) plt.legend(loc = (0.6,0.7)) plt.xlabel("Time [minutes]", fontsize=20) plt.xscale('log') plt.xlim([100,10000]) plt.ylabel('Best validation AUC', fontsize=20) plt.savefig("times_logx.png")
def plot(self,bins=1000,der=0,log=False,error=False,color=None): if log==True: x=np.logspace(log10(self.minX)+1e-7,log10(self.maxX)-1e-7,num=bins) plt.xscale("log") else: x=np.linspace(self.minX,self.maxX,num=bins) y=self.__call__(x,der=der) plt.plot(x,y,color=color) if(error==True): ymin=self.__call__(x,der=der,value="top") ymax=self.__call__(x,der=der,value="bottom") plt.plot(x,ymax,color=color) plt.plot(x,ymin,color=color) plt.fill_between(x, ymin,ymax,alpha=0.5,color=color)
def plotPSD(lcInt, shortExp,**kwargs): ''' plot power spectral density of lc return frequencies and powers from periodogram ''' freq = 1.0/shortExp f, p = periodogram(lcInt,fs = 1./shortExp) plt.plot(f,p/np.max(p),**kwargs) plt.xlabel(r"Frequency (Hz)",fontsize=14) plt.xscale('log') plt.ylabel(r"Normalized PSD",fontsize=14) plt.yscale('log') plt.title(r"Lightcurve Power Spectrum",fontsize=14) plt.show() return f,p
def main(): arg_parser = argparse.ArgumentParser( description="extracts statistical information from a given CSV video file created with video2csv" ) arg_parser.add_argument("--csvFile", help="the movie file to convert with ffmpeg") args = arg_parser.parse_args() if args.csvFile is None: print("no CSV file specified!") if not os.path.isfile(args.csvFile): print("No CSV file given or file does not exist") sys.exit(127) iframes = [] pframes = [] with open(args.csvFile) as csvfile: video_reader = csv.reader(csvfile, delimiter=";") video_reader.next() # skip first line comment for row in video_reader: if "I" in row[1]: iframes.append(float(row[3])) else: pframes.append(float(row[3])) print("Number of I-frames: %s" % (len(iframes))) print("Average I-frame size: %s" % (np.average(iframes))) print("Number of P-frames: %s" % (len(pframes))) print("Average P-frame size: %s" % (np.average(pframes))) x1, y1 = list_to_ccdf(iframes) x2, y2 = list_to_ccdf(pframes) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.plot(x1, y1, label="IFrames") ax.plot(x2, y2, label="PFrames") plt.xscale("log") plt.xlabel("coded picture size in bytes") plt.ylabel(("P")) plt.grid() plt.legend() plt.show()
def SMBH_mass(save=False): fig=plot.figure() #a=ascii.read('data/BHmass_dist.dat',names=['mass','N'],data_start=1) #mass=(np.round(a['mass']*100.)/100.) #N=np.array(np.round(a['N']*100),dtype=np.int64) high=ascii.read('data/BH_mass_High_z.txt',names=['mass']) low=ascii.read('data/BH_mass_Low_z.txt',names=['mass']) loghigh=np.log10(high['mass']) loglow=np.log10(low['mass']) #ind1=np.arange(0,13,1) #ind2=np.arange(13,len(mass),1) #m1=np.repeat(mass[ind1],N[ind1]) #w1=np.repeat(np.repeat(1./max(N[ind1]),len(ind1)),N[ind1]) #m2=np.repeat(mass[ind2],N[ind2]) #w2=np.repeat(np.repeat(1./max(N[ind2]),len(ind2)),N[ind2]) low_bin=np.logspace(np.min(loglow),np.max(loglow),num=14) plot.hist(low['mass'],bins=low_bin,color='blue',weights=np.repeat(1./28,len(low))) high_bin=np.logspace(np.min(loghigh),np.max(loghigh),num=10) plot.hist(high['mass'],bins=high_bin,color='red',alpha=0.7,weights=np.repeat(1./28,len(high))) plot.annotate('Low Redshift (z < 3) Blazars',xy=(1.5e9,0.8),xycoords='data',fontsize=14,color='blue') plot.annotate('High Redshift (z > 3) Blazars',xy=(1.5e9,0.5),xycoords='data',fontsize=14,color='red') plot.xlim([5e7,5e10]) plot.ylim([0,1.05]) plot.xscale('log') # plot.yscale('log') plot.xlabel(r'Black Hole Mass (M$_{\odot}$)') plot.ylabel('Fraction of Known Blazars') # plot.title('Supermassive Black Hole Mass Evolution') if save: plot.savefig('SMBH_mass.png', bbox_inches='tight') plot.savefig('SMBH_mass.eps', bbox_inches='tight') else: plot.show() return
def plot(result): from matplotlib import pylab as pl import scipy as sp if not result: result = sp.loadtxt('morph.csv', delimiter=',', skiprows=1).T x, y = result[0], result[1:] for i in y: pl.plot(x, i) pl.xlabel('Number of characters') pl.ylabel('Time (sec)') pl.xscale('log') pl.grid(True) pl.savefig("images/time.png") pl.show()
def analyze_neuron(cell, mapping, Mea, signal_range): start_t, stop_t = signal_range start_t_ixd = np.argmin(np.abs(cell.tvec - start_t)) stop_t_ixd = np.argmin(np.abs(cell.tvec - stop_t)) t_array = cell.tvec[start_t_ixd:stop_t_ixd] imem = cell.imem[:, start_t_ixd:stop_t_ixd] amps_at_comps = find_amplitude_at_comp(imem, debug=False) comp_dist_from_soma = find_comps_dist_from_soma(cell) #pl.figure() #pl.plot(comp_dist_from_soma, amps_at_comps, 'o') #pl.show() for elec in xrange(Mea.n_elecs): #if not elec == 110: # continue print elec comp_dist_from_elec = find_comp_dist_from_elec(elec, cell, Mea) comp_impact_on_elec = mapping[elec,:]*amps_at_comps if 0: print np.argmax(comp_dist_from_elec) print np.argmin(comp_dist_from_elec) pl.figure() pl.axis('equal') pl.plot(cell.zmid/1000, cell.ymid/1000, 'o') pl.plot(Mea.elec_z, Mea.elec_y, 'o') pl.plot(cell.zmid[381]/1000, cell.ymid[381]/1000, 'D') pl.plot(cell.zmid[791]/1000, cell.ymid[791]/1000, 'D') pl.plot(Mea.elec_z[elec], Mea.elec_y[elec], 'x') pl.show() pl.plot(comp_dist_from_elec, comp_impact_on_elec, 'o') pl.yscale('log') pl.xscale('log') pl.title('Electrode at distal tuft dendrite, when spike originates in tuft,\n'\ +'and does not propagate to soma.') pl.xlabel('Compartment distance from electrode [mm]') pl.ylabel('Compartment impact on electrode [uV]') pl.show()
d1 = [] dn = [] for f in fractions: t = np.array(dissolution[nn + str(f)]) * scale x = np.arange(1, len(dissolution[nn + str(f)]) + 1) * 1e-6 D = (x**2) / 2 / t d1.append(D[0]) dn.append(D[-1]) plt.figure() plt.plot(d1, fractions, label="1") #plt.plot(fractions, dn, label = "20") plt.xlabel("D (m2/s)") plt.ylabel("Fraction") plt.xscale("log") plt.yscale("log") plt.legend() plt.show() #%% rate scale = 100 dim = 10**(-15) * 10**3 * 10**8 # convert to mmol/l/s/cm2 plt.figure(figsize=(6, 6)) for f in fractions: plt.plot(np.abs(dch[nn + str(f)][1:]) * scale * dim, label=f) plt.ylabel(r"Rate of dissolution $k$ $(mmol / (l\cdot s\cdot cm^2)$") plt.xlabel(r"Dissolved portlandite length $(\mu)$") plt.yscale("log") #plt.xscale("log") plt.legend()
def magentar_plot(): xmm=ascii.read('data/xmm_SED_paper.dat',names=['energy','err_energy','flux','err_flux']) integral=ascii.read('data/integral_SED_paper.dat',names=['energy','err_energy','flux','err_flux']) comptel=ascii.read('data/comptel_SED_paper.dat',names=['energy','err_energy','flux','err_flux']) lat={'energy_min':[1e5,1e5,1e6],'energy_max':[1e7,1e6,1e7],'flux':[1.6e-4,2.1e-4,8.13e-5]} #magic={'energy_min':[200e3,775e3],'energy_max':[774e3,3000e3],'flux':[6.29e-5,8.99e-5]} models=ascii.read('data/magnetar_4U0142+614.dat',names=['energy','flux']) fig=plot.figure() xrange=[1e-4,1e5] yrange=[5e-13,3e-10] s=1e-8 plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5) plot.annotate('AMEGO',xy=(1.5,1.7e-10),xycoords='data',fontsize=26,color='black') plot.errorbar(xmm['energy']*1e-3,xmm['flux']*s,xerr=xmm['err_energy']*1e-3,yerr=xmm['err_flux']*s,capsize=0, fmt='.',color='blue') plot.errorbar(integral['energy']*1e-3,integral['flux']*s,xerr=integral['err_energy']*1e-3,yerr=integral['err_flux']*s,capsize=0, fmt='.',color='blue') plot.errorbar(comptel['energy']*1e-3,comptel['flux']*s,xerr=comptel['err_energy']*1e-3,yerr=0.5*comptel['flux']*s,capsize=0, fmt='.',color='blue',uplims=True) m=np.array(10**((np.log10(lat['energy_min'])+np.log10(lat['energy_max']))/2))*1e-3 lowe=m-np.array(lat['energy_min'])*1e-3 highe=np.array(lat['energy_max'])*1e-3-m plot.errorbar(m,np.array(lat['flux'])*s,xerr=[lowe,highe],yerr=0.3*np.array(lat['flux'])*s,capsize=0, fmt='.',color='blue',uplims=True) # m=np.array(10**((np.log10(magic['energy_min'])+np.log10(magic['energy_max']))/2)) # lowe=m-np.array(magic['energy_min']) # highe=np.array(magic['energy_max'])-m # #plot.errorbar(m,np.array(magic['flux'])*s,xerr=[lowe,highe],yerr=0.2*np.array(magic['flux'])*s,capsize=0, fmt='.',color='blue',uplims=True) erg2kev=624151.*1e3 color=['red','magenta','black','black'] lines=['','','','r--'] num=[0,18,38,67,len(models['energy'])] for i in range(0,4): n0=num[i] n1=num[i+1] ind=np.arange(n0,n1) x=np.array(models['energy'])*1e-3 y=np.array(models['flux'])/erg2kev # n2=(n1-n0)/2+n0 w=np.where(y[ind]-max(y[ind]) == 0) n2=ind[w[0][0]] x0=loginterpol(y[n0:n2],x[n0:n2],yrange[0]) x1=loginterpol(y[n2:n1],x[n2:n1],yrange[0]) y=np.append(np.append(yrange[0],y[ind]),yrange[0]) x=np.append(np.append(x0,x[ind]),x1) # y=np.append(yrange[0],y) # x=np.append(x0,x) plot.plot(x,y,lines[i],color=color[i]) #y=np.append(np.append(yrange[0],models['flux'][ind]/erg2kev),yrange[0]) #x=loginterpol(models['flux'][ind[0:9]]/erg2kev,models['energy'][ind[0:9]*1e-3,y) #plot.plot(x,y,color='red') #print x[0],x[19] #print y[0],y[19] # ind=np.arange(18,38) # plot.plot(models['energy'][ind]*1e-3,models['flux'][ind]/erg2kev,color='magenta') # ind=np.arange(38,67) # plot.plot(models['energy'][ind]*1e-3,models['flux'][ind]/erg2kev,color='black') # ind=np.arange(67,len(models['flux'])) # plot.plot(models['energy'][ind]*1e-3,models['flux'][ind]/erg2kev,'r--',color='black') plot.xscale('log') plot.yscale('log') plot.ylim(yrange) plot.xlim(xrange) plot.xlabel(r'Energy (MeV)') plot.ylabel(r'Energy$^2 \times $ Flux (Energy) (erg cm$^{-2}$ s$^{-1}$)') plot.show()
def FillingTheGap(save=False): ## ComPair fig = plot.figure() yrange = [1e-6,4e-3]#[1e-13, 1e2] xrange = [1e-3,1e7] plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5) plot.annotate('AMEGO',xy=(4,1.5e-3),xycoords='data',fontsize=26,color='black') ## AGN a=ascii.read("data/marco_AGN_data_points_fig_6.txt",names=['logfreq','logflux']) logfreq=a['logfreq'] logflux=a['logflux'] h=6.6261e-27 #erg s erg2mev=624151. agn_energy=10**logfreq*h*erg2mev #Hz * erg s arbfact=5 agn_flux=10**logflux*erg2mev*arbfact #erg cm-2 s-1 i = np.where(agn_energy < 0.1) plot.plot(agn_energy[i],agn_flux[i],color='navy',lw=2) i = np.where((agn_energy > 0.1) & (agn_energy < 200)) plot.plot(agn_energy[i],agn_flux[i],'r--',color='navy',lw=2) i=np.where(agn_energy > 200) plot.plot(agn_energy[i],agn_flux[i],color='navy',lw=2) b=ascii.read("data/0641_0320nustar_torus.dat",names=['x','y','z','tot','f','fbump','ftot','syn','ssc','ext1c']) logfreq=b['x'] lognufnu=b['ftot'] agn_energy2=10**logfreq*h*erg2mev agn_flux2=10**lognufnu*erg2mev*arbfact i = np.where(agn_energy2 < 0.1) plot.plot(agn_energy2[i],agn_flux2[i],color='cornflowerblue',lw=2) i = np.where((agn_energy2 > 0.1) & (agn_energy2 < 200)) plot.plot(agn_energy2[i],agn_flux2[i],'r--',color='cornflowerblue',lw=2) i=np.where(agn_energy2 > 200) plot.plot(agn_energy2[i],agn_flux2[i],color='cornflowerblue',lw=2) xrt=ascii.read("data/0641_0320xrt.dat",names=['logfreq','logflux']) xrt_energy=(10**xrt['logfreq'])*h*erg2mev xrt_flux=(10**xrt['logflux'])*erg2mev*arbfact plot.scatter(xrt_energy,xrt_flux,color='blue') nustar=ascii.read("data/0641_0320nustar_ajello_fab.dat",names=['logfreq','logflux','logflux_yerr0','logflux_yerr1']) ns_energy=(10**nustar['logfreq'])*h*erg2mev ns_flux=(10**nustar['logflux'])*erg2mev*arbfact plot.scatter(ns_energy,ns_flux,color='blue') lat=ascii.read("data/LAT_spec_NuSTARobs2.txt",names=['ener','ed_ener','eu_ener','flux','ed_flux','eu_flux','ulim_flux','TS','Npred']) plot.scatter(lat['ener'],lat['flux']*arbfact) plot.errorbar(lat['ener'][0:3],lat['flux'][0:3]*arbfact,xerr=[lat['ed_ener'][0:3],lat['eu_ener'][0:3]],yerr=[lat['ed_flux'][0:3]*arbfact,lat['eu_flux'][0:3]*arbfact],capsize=0,fmt="none") ## Pulsar example pulsar_eng=np.array([0.012943256,0.018285165,0.031053474,0.05153211,0.08552302,0.21973862,137.03448,237.55414]) pulsar_flux=np.array([1.7420283E-5,2.2255874E-5,3.0082629E-5,3.842357E-5,5.0966246E-5,7.149577E-5,1.4489453E-5,6.674534E-6]) pulsar_eng_ul=np.array([421.64273,748.32324]) pulsar_flux_ul=np.array([4.0049385E-6,2.314023E-6]) xs = np.linspace(np.min(np.log10(1e-3)), np.max(np.log10(1e3)), 300) pulsar_Energy = 10**xs e0=[0.100518,0.100518*0.4,0.100518] # norm energy k=[1.574e-2,1.574e-2*2,1.574e-2*5] # normalization gamma=[-1.233,-1.18,-1.2] # ec=[0.078,0.8,5e-4] #cutoff energy beta=[0.286,0.4,0.18] # cutoff slope arbfact=1 color=['darkgreen','sage'] for j in range(0,2): flux=k[j]*(pulsar_Energy/e0[j])**gamma[j]*np.exp(-(pulsar_Energy/ec[j])**beta[j]) pulsar_Flux=flux*pulsar_Energy**2 i = np.where(pulsar_Energy < 0.2) plot.plot(pulsar_Energy[i],pulsar_Flux[i]*arbfact,color=color[j],lw=2) i = np.where((pulsar_Energy > 0.2) & (pulsar_Energy < 100)) plot.plot(pulsar_Energy[i],pulsar_Flux[i]*arbfact,'r--',color=color[j],lw=2) i=np.where(pulsar_Energy > 100) plot.plot(pulsar_Energy[i],pulsar_Flux[i]*arbfact,color=color[j],lw=2) plot.scatter(pulsar_eng,pulsar_flux*arbfact,color='green') errfrac=np.concatenate((np.repeat(0.1,6),(0.4,0.6)),axis=0) plot.errorbar(pulsar_eng,pulsar_flux*arbfact,yerr=errfrac*pulsar_flux*arbfact,color='green',ecolor='green',capsize=0,fmt="none") plot.errorbar(pulsar_eng_ul,pulsar_flux_ul*arbfact,yerr=0.5*pulsar_flux_ul*arbfact,color='green',ecolor='green',capsize=0,uplims=True,fmt="none") plot.scatter(pulsar_eng_ul,pulsar_flux_ul*arbfact,color='green') ## Nova arbfact=0.5 #osse=ascii.read("data/NVel1999.OSSE.dat",names=['energy','en_low','en_high','flux','flux_err']) #plot.scatter(osse['energy'],osse['flux']*erg2mev*arbfact,color='red') #plot.errorbar(osse['energy'],osse['flux']*erg2mev*arbfact,xerr=[osse['en_low'],osse['en_high']],yerr=osse['flux_err']*erg2mev*arbfact,ecolor='red',capsize=0,fmt='none') lat=ascii.read("data/NMon2012.LAT.dat",names=['energy','en_low','en_high','flux','flux_err','tmp']) plot.scatter(lat['energy'],lat['flux']*erg2mev*arbfact,color='red') plot.errorbar(lat['energy'],lat['flux']*erg2mev*arbfact,xerr=[lat['en_low'],lat['en_high']],yerr=lat['flux_err']*erg2mev*arbfact,ecolor='red',capsize=0,fmt='none') latul=ascii.read("data/NMon2012.LAT.limits.dat",names=['energy','en_low','en_high','flux','tmp1','tmp2','tmp3','tmp4']) plot.errorbar(latul['energy'],latul['flux']*erg2mev*arbfact,xerr=[latul['en_low'],latul['en_high']],yerr=0.5*latul['flux']*erg2mev*arbfact,uplims=True,ecolor='red',capsize=0,fmt='none') plot.scatter(latul['energy'],latul['flux']*erg2mev*arbfact,color='red') #models=ascii.read("data/data-NovaMon2012.txt",names=['energy','leptonic','hadronic'],data_start=1) #mo=['leptonic','hadronic'] colors=['orangered','coral','darkred'] leptonic=ascii.read("data/sp-NMon12-IC-best-fit-1MeV-30GeV.txt",names=['energy','flux'],data_start=1) hadronic=ascii.read("data/sp-NMon12-pi0-and-secondaries.txt",names=['energy','flux1','flux2'],data_start=1) for j in range(0,2): if (j == 0): energy=leptonic['energy'] flux=leptonic['flux'] if (j == 1): energy=hadronic['energy'] flux=hadronic['flux2'] if (j == 2): flux=hadronic['flux1'] i=np.where(energy < 0.2) plot.plot(energy[i],flux[i]*erg2mev*arbfact,color=colors[j],lw=2) i=np.where((energy > 0.2) & (energy <100 )) plot.plot(energy[i],flux[i]*erg2mev*arbfact,'r--',color=colors[j],lw=2) i=np.where(energy > 100) plot.plot(energy[i],flux[i]*erg2mev*arbfact,color=colors[j],lw=2) # PWNe # From Torres et al. JHEAP, 2014, 1, 31 # G54.1+0.3 # arbfact=1#e-3 # pwne_model_eng=np.array([860.1604,1866.5002,3879.583,7087.079,10898.913,17498.09,28093.0,34838.23,41383.043,299664.66,6644004.5,3.5596056E7,1.29464152E8,4.91573856E8,3.71615181E9,2.36500337E10,8.2392531E10,2.52272067E11,5.47416343E11,8.7887118E11])*1e-6 # pwne_model_flux=np.array([1.2608298E-11,9.389613E-12,5.4490882E-12,2.8233574E-12,1.1928516E-12,4.0173412E-13,1.7362255E-13,1.3226478E-13,1.4482099E-13,3.1305714E-13,7.409742E-13,8.684221E-13,8.2992185E-13,6.3223027E-13,2.9917822E-13,8.0318205E-14,1.925147E-14,4.119833E-15,8.816484E-16,1.0E-16])*erg2mev*arbfact # xs = np.linspace(np.min(np.log10(1e-3)), np.max(np.log10(5e3)), 300) # pwne_Energy=10**xs # tck=interpolate.splrep(np.log10(pwne_model_eng),np.log10(pwne_model_flux),s=0) # pwne_Flux=10**interpolate.splev(np.log10(pwne_Energy),tck,der=0) # pwne_data_eng=np.array([1.7125564E7,5.4741636E7,1.74980896E8,2.46901296E8,3.9639744E8,6.3641197E8,1.11359936E9,1.64041331E9,2.52272051E9,4.0502016E9])*1e-6 # pwne_data_upper_flux=np.array([4.2462813E-12,4.7560116E-12,1.5817023E-11,3.7911818E-12,1.2202063E-12,2.2001422E-12,1.8772538E-12,2.6377E-12,1.1144125E-12,2.1508195E-12])*erg2mev*arbfact # pwne_data_flux=np.array([0,0,0,1.4628772E-12,3.2757992E-13,8.489537E-13,7.579663E-13,1.2202063E-12,2.3313897E-13,6.9224937E-13])*erg2mev*arbfact # pwne_data_lower_flux=np.array([8.883369E-13,1.1144125E-12,3.3089764E-12,3.5867788E-13,3.242955E-14,3.5867788E-13,2.4395433E-13,5.039727E-13,1.7188176E-14,1.2356735E-13])*erg2mev*arbfact # i = np.where(pwne_Energy < 0.5) # plot.plot(pwne_Energy[i],pwne_Flux[i],color='red',lw=2) # i = np.where((pwne_Energy > 0.5) & (pwne_Energy < 200)) # plot.plot(pwne_Energy[i],pwne_Flux[i],'r--',color='red',lw=2) # i=np.where(pwne_Energy > 200) # plot.plot(pwne_Energy[i],pwne_Flux[i],color='red',lw=2) # plot.errorbar(pwne_data_eng[3:],pwne_data_flux[3:],yerr=[pwne_data_flux[3:]-pwne_data_lower_flux[3:],pwne_data_upper_flux[3:]-pwne_data_flux[3:]],color='tomato',fmt='o',ecolor='tomato',capsize=0,lw=2) # plot.errorbar(pwne_data_eng[0:3],pwne_data_upper_flux[0:3],yerr=pwne_data_upper_flux[0:3]-pwne_data_lower_flux[0:3],color='tomato',fmt='o',ecolor='tomato',uplims=True,lw=2) # plot stuff plot.xscale('log') plot.yscale('log') plot.ylim(yrange) plot.xlim(xrange) plot.xlabel(r'Energy (MeV)') plot.ylabel(r'Energy$^2 \times $ Flux (Energy) (arbitrarily scaled)') im1=plot.imread('data/AGN_UnifiedModel.jpg') newax1=fig.add_axes([0.73,0.65,0.15,0.15],anchor='NE') newax1.imshow(im1) for axis in ['top','bottom','right','left']: newax1.spines[axis].set_linewidth(4) newax1.spines[axis].set_color('blue') newax1.set_xticks([]) newax1.set_yticks([]) # newax1.axis('off') plot.title('Jets',color='blue',fontsize=12) im2=plot.imread('data/pulsar.jpg') newax2=fig.add_axes([0.73,0.4,0.15,0.16],anchor='NE') newax2.imshow(im2) for axis in ['top','bottom','right','left']: newax2.spines[axis].set_linewidth(4) newax2.spines[axis].set_color('green') newax2.set_xticks([]) newax2.set_yticks([]) # newax2.axis('off') plot.title('Compact Objects',color='green',fontsize=12) im3=plot.imread('data/Classical_Nova_Final.jpg') newax3=fig.add_axes([0.73,0.18,0.15,0.15],anchor='NE') newax3.imshow(im3) for axis in ['top','bottom','right','left']: newax3.spines[axis].set_linewidth(4) newax3.spines[axis].set_color('red') newax3.set_xticks([]) newax3.set_yticks([]) # newax3.axis('off') plot.title('Shocks',color='red',fontsize=12) if save: #plot.savefig('SED_science_themes.eps', bbox_inches='tight') plot.savefig('SED_science_themes.pdf', bbox_inches='tight') plot.savefig('SED_science_themes.png', bbox_inches='tight') plot.show() plot.close() ## to do ### add colored box around each image ### add labels for jets, compact objects, shocks + maybe source type return
def UNIDplot(save=False): import FigureOfMeritPlotter from scipy import interpolate data=FigureOfMeritPlotter.parseEventAnalysisLogs(directory='../Simulations/PerformancePlotTraFiles/',silent=True) ComPair=FigureOfMeritPlotter.plotAllSourceSensitivities(data,angleSelection=1.0,doplot=False) latcat=fits.open('data/gll_psc_v14.fit') data=latcat[1].data #print latcat.info() #print latcat[1].columns wunid=np.where((data.field('ASSOC1') == ' ') & (data.field('ASSOC2') == ' ')) Tot=len(wunid[-1]) print Tot w=np.where((data.field('ASSOC1') == ' ') & (data.field('ASSOC2') == ' ') & (data.field('SpectrumType')=='PowerLaw')) tot=len(w[-1]) print tot flux=data[w].field('Flux1000') emin=1 emax=1000. erg2mev=624151. index=data[w].field('Spectral_Index') fig=plot.figure() yrange = [1e-15,4e-15] plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5) plot.annotate('AMEGO',xy=(4,1.5e-15),xycoords='data',fontsize=26,color='black') yrange = [5e-15,2.5e-14] plot.fill_between([100,300e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='magenta',interpolate=True,color='magenta',alpha=0.5) plot.annotate('LAT',xy=(400,8e-15),xycoords='data',fontsize=26,color='black') logenergy=np.arange(-1,6,0.1) energy=10**logenergy tck=interpolate.splrep(np.log10(ComPair[0][1:]),np.log10(ComPair[1][1:]/erg2mev),s=0) w=np.where((energy > 0.5) & (energy < 500)) compflux=10**interpolate.splev(np.log10(energy[w]),tck,der=0) N_nodet=0. N_det=0. N_highpeak=0. for i in range(len(index)): norm=flux[i]*(1.-index[i])/(emax**(1-index[i])-emin**(1-index[i])) f=norm*(energy/1e3)**(-index[i])*1e3#(energy**(2.-index[i])/(2-index[i])) e2f=f*energy**2/erg2mev**2 if (index[i] < 2): color='grey' N_nodet+=1 if (index[i] > 2) & (max(e2f[w]-compflux) > 0): color='red' N_det+=1 if (max(e2f[w]-compflux) < 0) & (index[i] > 2): color='blue' N_highpeak+=1 plot.plot(energy,e2f,'r:',color=color) print 'N_nodet: ',N_nodet, N_nodet/tot, N_nodet/Tot print 'N_det: ',N_det, N_det/tot, N_det/Tot print 'N_highpeak: ',N_highpeak, N_highpeak/tot, N_highpeak/Tot plot.annotate(r"AMEGO Detectable (Peak < LAT)",xy=(21,5e-9),xycoords='data',fontsize=14,color='red') plot.annotate(r'AMEGO Non-Detectable (Peak < LAT)',xy=(9,2e-9),xycoords='data',fontsize=14,color='blue') plot.annotate(r'AMEGO Non-Detectable (Peak $\geq$ LAT)',xy=(8,8e-10),xycoords='data',fontsize=14,color='grey') #print data[w].field('Spectral_Index') #plot.plot(ComPair[0][1:],ComPair[1][1:]/erg2mev,color='blue',lw=3) plot.xlim([1e-1,1e5]) plot.ylim([1e-15,1e-8]) plot.xscale('log') plot.yscale('log') plot.xlabel(r'Energy (MeV)') plot.ylabel(r'Energy$^2 \times $ Flux (Energy) (erg cm$^{-2}$ s$^{-1}$)') plot.title('Fermi-LAT Unidentified Sources in the MeV Band') if save: plot.savefig('UNID_SED.png', bbox_inches='tight') plot.savefig('UNID_SED.eps', bbox_inches='tight') else: plot.show() plot.close() latcat.close() return
def plot_fitted_xprofiles(self): # fitted model for solution_idx, solution_val in enumerate( self.pickle_file['solutions']): fig = plt.figure(figsize=(7, 7 / phi)) ax = fig.add_subplot(111) N = len(self.pickle_file['params']['atm_active_gases'] + self.pickle_file['params']['atm_inactive_gases']) cols_mol = {} for mol_idx, mol_val in enumerate( self.pickle_file['params']['atm_active_gases']): cols_mol[mol_val] = self.cmap(float(mol_idx) / N) prof = solution_val['active_mixratio_profile'][mol_idx] plt.plot(prof[:, 1], prof[:, 0] / 1e5, color=cols_mol[mol_val], label=mol_val) #plt.fill_betweenx(prof[:,0]/1e5, prof[:,1]-prof[:,2], prof[:,1]+prof[:,2], color=self.cmap(float(mol_idx)/N), alpha=0.5) for mol_idx, mol_val in enumerate( self.pickle_file['params']['atm_inactive_gases']): cols_mol[mol_val] = self.cmap( float(mol_idx + len(self.pickle_file['params'] ['atm_inactive_gases'])) / N) prof = solution_val['inactive_mixratio_profile'][mol_idx] plt.plot(prof[:, 1], prof[:, 0] / 1e5, color=cols_mol[mol_val], label=mol_val) #plt.fill_betweenx(prof[:,0]/1e5, prof[:,1]-prof[:,2], prof[:,1]+prof[:,2], color=self.cmap(float(mol_idx)/N), alpha=0.5) # add input spectrum param if available if 'SPECTRUM_db' in self.pickle_file: for mol_idx, mol_val in enumerate(self.pickle_file['SPECTRUM_db'] ['params']['atm_active_gases']): prof = self.pickle_file['SPECTRUM_db']['data'][ 'active_mixratio_profile'][mol_idx] if mol_val in cols_mol: plt.plot(prof[:, 1], prof[:, 0] / 1e5, color=cols_mol[mol_val], ls='dashed') else: plt.plot(prof[:, 1], prof[:, 0] / 1e5, color=self.cmap( float( len(self.pickle_file['params'] ['atm_active_gases']) + len(self.pickle_file['params'] ['atm_inactive_gases']) + mol_idx) / N), label=mol_val, ls='dashed') for mol_idx, mol_val in enumerate( self.pickle_file['SPECTRUM_db']['params'] ['atm_inactive_gases']): prof = self.pickle_file['SPECTRUM_db']['data'][ 'inactive_mixratio_profile'][mol_idx] if mol_val in cols_mol: plt.plot(prof[:, 1], prof[:, 0] / 1e5, color=cols_mol[mol_val], ls='dashed') else: plt.plot(prof[:, 1], prof[:, 0] / 1e5, color=self.cmap( float( len(self.pickle_file['params'] ['atm_active_gases']) + len(self.pickle_file['params'] ['atm_inactive_gases']) + mol_idx) / N), label=mol_val, ls='dashed') plt.gca().invert_yaxis() plt.yscale('log') plt.xscale('log') plt.xlim(1e-12, 3) plt.xlabel('Mixing ratio') plt.ylabel('Pressure (bar)') plt.tight_layout() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), ncol=1, prop={'size': 11}, frameon=False) if self.title: plt.title(self.title, fontsize=14) plt.savefig( os.path.join( self.out_folder, '%s%s_mixratio.pdf' % (self.prefix, self.retrieval_type)))
Omega_m0_max = float(sys.argv[2]) n=11 Omega_m0 = np.linspace(Omega_m0_min, Omega_m0_max, n) if len(sys.argv) == 3: Omega_K = 1 - Omega_m0 - Lambda else: Omega_K = np.zeros(n) for j in range(n): Omega_K[j] = float(sys.argv[3]) omega = Omega_K + Omega_m0 + Lambda s0 = 0.5 #result = odeint(s, s0, z, args=(Lambda, Omega_m0, Omega_K)) for i in range(len(Omega_m0)): sofa = odeint(s_a, s0, a, args = (Omega_m0[i], Omega_K[i], Lambda, a0)) mpl.plot(a, sofa, linewidth = 0.75, label = r"s, $\Omega_{m0} =$ %.1f" % Omega_m0[i]) mpl.xlabel("a(t)") mpl.ylabel("s(a)") mpl.legend() mpl.yscale("log") mpl.xscale("log") mpl.show()
# -----------set up units properly------------------# mag_order=np.int((1)*np.round(np.log10(np.mean(sp.data)))) sp.xarr.units='angstroms' sp.xarr.xtype = 'wavelength' sp.units = r'$10^{'+str(mag_order)+'}$ erg s$^{-1}$ $cm^{-1}$' sp.data /= 10**(mag_order) #-------------- set up units properly------------# wlmin=sp.xarr[0] wlmax=sp.xarr[-1] pylab.yscale('log') pylab.xscale('log') #pylab.xscale('log',subsx=[3,4]) pylab.rcParams["figure.figsize"]=16,6 sp.plotter(figure=1,xmin=wlmin,xmax=wlmax,ymin=1.1*sp.data.min(),ymax=3.5*sp.data.max()) pylab.close() #-------------continuous subtraction-------------------# if w=="UV": continuous,wlmin_UV=continuous_substraction( i, sp, mag_order,UV_limits,w) backup=sp.copy() sp.data=sp.data - continuous balmer_template,balmer_tot, index=balmer_normalization(sp,balmer_cont_template,balmer_lines_template) sp.data=backup.data
def plot_mse_beta_compare_start_vectors(BASE_PATH_RANDOM, BASE_PATH_WARM, INPUT_DATA_X, INPUT_DATA_y): cv = os.listdir(BASE_PATH_WARM) for cv_index in range(len(cv)): print("CV : %s" % cv[cv_index]) PATH_WARM = os.path.join(BASE_PATH_WARM, cv[cv_index], "all") PATH_RANDOM = os.path.join(BASE_PATH_RANDOM, cv[cv_index], "all") params = glob.glob(os.path.join(PATH_WARM, "0*")) for p in params: p = os.path.basename(p) print(p) snap_path_warm = os.path.join(PATH_WARM, p, "conesta_ite_snapshots") conesta_ite_warm = sorted(os.listdir(snap_path_warm)) nb_conesta_warm = len(conesta_ite_warm) snap_path_random = os.path.join(PATH_RANDOM, p, "conesta_ite_snapshots") conesta_ite_random = sorted(os.listdir(snap_path_random)) nb_conesta_random = len(conesta_ite_random) ite_final_warm = np.load( os.path.join(snap_path_warm, conesta_ite_warm[-1])) beta_star_warm = ite_final_warm["beta"] gap_warm = ite_final_warm["gap"] mse_warm = np.zeros((nb_conesta_warm)) i = 0 for ite in conesta_ite_warm: path = os.path.join(snap_path_warm, ite) ite_warm = np.load(path) mse_warm[i] = sklearn.metrics.mean_squared_error( ite_warm["beta"][:, 0], beta_star_warm[:, 0]) i = i + 1 ite_final_random = np.load( os.path.join(snap_path_random, conesta_ite_random[-1])) beta_star_random = ite_final_random["beta"] gap_random = ite_final_random["gap"] mse_random = np.zeros((nb_conesta_random)) i = 0 for ite in conesta_ite_random: path = os.path.join(snap_path_random, ite) ite_random = np.load(path) mse_random[i] = sklearn.metrics.mean_squared_error( ite_random["beta"][:, 0], beta_star_random[:, 0]) i = i + 1 pdf_path = os.path.join(BASE_PATH_WARM, cv[cv_index], "all", p, "start_vector_effect_on_gap.pdf") pdf = PdfPages(pdf_path) fig = plt.figure(figsize=(11.69, 8.27)) plt.plot(gap_random, label="Random start") plt.plot(gap_warm, label="Warm start: Beta from all/all") plt.xscale('log') plt.yscale('log') plt.xlabel("iterations") plt.ylabel(r"$gap$") plt.legend(prop={'size': 15}) plt.title(p) pdf.savefig() plt.close(fig) pdf.close()
# lines = ["-","-","-","-"] points_o1var = [[1e-5, 1e5], [1e-4, 1e5], [1e-4, 1e6]] # points_o1var = [[1e-5, 1e-2], [2e-5, 2e-2], [1e-5, 2e-2]] triangle_o1var = plt.Polygon(points_o1var, fill=None ,edgecolor='grey') #VariancePlot for eps_i in range (0,len(eps_list)): plot_var = plt.plot(Nlist_inv, (sde_Jv_sq[eps_i] -sq_E_Jv[eps_i])/bins, lines[eps_i], label =r'$\varepsilon=10^{-%d}$' %eps_list_exponents[eps_i] , linewidth=3) plot_weighted = plt.plot(Nlist_inv, (sde_Jv_sq_weighted[eps_i] -sq_E_Jv_weighted[eps_i])/bins, lines[eps_i], linewidth=3.5) plt.ylabel('Var ($\mathbf{\hat{Jv}} $)', fontsize = 16) plt.xlabel('$1/N$', fontsize = 16) plt.xscale(log_flag) plt.yscale(log_flag) # plt.legend([plot_var], loc='best') #plt.legend(bbox_to_anchor=(0.95, 1), numpoints = 1 ) plt.gca().set_ylim([1e-4, 1e15]) plt.gca().add_patch(triangle_o1var) plt.legend([plot_var], loc='best') plt.legend(bbox_to_anchor=(1, 0.65), numpoints = 1 ) order= r'$\mathcal{O}(1/N)$' plt.annotate(order, xy=(4.8e-5, 1.5e5), xytext=(4.8e-5, 1.5e5), fontsize=9, color='grey') plt.annotate('with variance reduction', xy=(4e-6, 1e-1), xytext=(4e-6, 1e-1), fontsize=12,rotation=5.5, color='grey') if(save_flag): plt.savefig("plots/Var_N_eps_nw.pdf") plt.show() # for eps_i in range (0,len(eps_list)): # plot_var = plt.plot(Nlist_inv, (rho_sq -sq_E_rho)/bins, lines[eps_i],
#----------starting plotter--------------# #-------------continuous fitting-------------------# exclude_file = "./excludes/exclude_cont.txt" exclude_cont=np.loadtxt(exclude_file,skiprows=2) #limit_file = "./cont_limits.txt" #cont_limits=np.loadtxt(limit_file) slope_break=4230#cont_limits[:,1] slope_lim=4200#cont_limits[:,0] wlmin=sp.xarr[0] wlmax=sp.xarr[-1] pylab.xscale('log')#,subsx=[3,4]) """ copy.baseline.powerlaw=True copy.baseline(xmin=2100, xmax=xmax, exclude=exclude_cont[i,:], subtract=False, reset_selection=False, highlight_fitregion=True,powerlaw=True,quiet=False,LoudDebug=False,annotate=True) copy.crop(2200.0,copy.xarr[-1]) copy3.baseline.powerlaw=True copy3.baseline(xmin=xmin, xmax=2250, exclude=exclude_cont[i,:], subtract=False, reset_selection=False, highlight_fitregion=True,powerlaw=True,quiet=False,LoudDebug=False,annotate=True) copy3.crop(copy3.xarr[0],2199.6) copy1.crop(1400,1600) pylab.ylim(ymin=-1.3*np.abs(copy1.data.min()),ymax=1.1*copy1.data.max()) pylab.xlim(xmin=xmin,xmax=6900) pylab.ylabel(r'$10^{-'+str(mag_order)+'}$ erg s$^{-1}$ cm$^{-2}$ $\AA^{-1}$') pylab.xlabel(r'$\AA$') pylab.xscale('log')
def plot_fitted_spectrum(self, plot_contrib=True): # fitted model fig = plt.figure(figsize=(5.3, 3.5)) ax = fig.add_subplot(111) obs = self.pickle_file['obs_spectrum'] plt.errorbar(obs[:, 0], obs[:, 1], obs[:, 2], lw=1, color='black', alpha=0.5, ls='none', zorder=99, label='Observed') N = len(self.pickle_file['solutions']) for solution_idx, solution_val in enumerate( self.pickle_file['solutions']): if N > 1: label = 'Fitted model (%i)' % (solution_idx + 1) else: label = 'Fitted model' spectra = solution_val['obs_spectrum'] fit_highres = solution_val['fit_spectrum_xsecres'] plot_spectrum = np.zeros((len(fit_highres[:, 0]), 2)) plot_spectrum[:, 0] = fit_highres[:, 0] plot_spectrum[:, 1] = fit_highres[:, 1] plot_spectrum = plot_spectrum[plot_spectrum[:, 0].argsort( axis=0)] # sort in wavelength if self.pickle_file['params']['in_opacity_method'][:4] == 'xsec': plot_spectrum = binspectrum(plot_spectrum, self.plot_resolution) plt.plot(plot_spectrum[:, 0], plot_spectrum[:, 1], zorder=0, color=self.cmap(float(solution_idx) / N), label=label) plt.scatter(spectra[:, 0], spectra[:, 3], marker='d', zorder=1, **{ 's': 10, 'edgecolors': 'grey', 'c': self.cmap(float(solution_idx) / N) }) # add sigma spread if self.pickle_file['params']['out_sigma_spectrum']: plot_spectrum_std = np.zeros((len(fit_highres[:, 0]), 2)) plot_spectrum_std[:, 0] = fit_highres[:, 0] plot_spectrum_std[:, 1] = fit_highres[:, 2] plot_spectrum_std = plot_spectrum_std[ plot_spectrum_std[:, 0].argsort(axis=0)] # sort in wavelength if self.pickle_file['params'][ 'in_opacity_method'][: 4] == 'xsec' and self.plot_resolution > 0: plot_spectrum_std = binspectrum(plot_spectrum_std, self.plot_resolution) # 1 sigma plt.fill_between(plot_spectrum[:, 0], plot_spectrum[:, 1] - plot_spectrum_std[:, 1], plot_spectrum[:, 1] + plot_spectrum_std[:, 1], alpha=0.5, zorder=-2, color=self.cmap(float(solution_idx) / N), edgecolor='none') # 2 sigma plt.fill_between( plot_spectrum[:, 0], plot_spectrum[:, 1] - 2 * plot_spectrum_std[:, 1], plot_spectrum[:, 1] + 2 * plot_spectrum_std[:, 1], alpha=0.2, zorder=-3, color=self.cmap(float(solution_idx) / N), edgecolor='none') plt.xlim( np.min(obs[:, 0]) - 0.05 * np.min(obs[:, 0]), np.max(obs[:, 0]) + 0.05 * np.max(obs[:, 0])) plt.xlabel('Wavelength ($\mu$m)') if self.pickle_file['params']['gen_type'] == 'emission': plt.ylabel('$F_p/F_*$') else: plt.ylabel('$(R_p/R_*)^2$') # set log scale only if interval is greater than 5 micron if np.max(obs[:, 0]) - np.min(obs[:, 0]) > 5: plt.xscale('log') plt.tick_params(axis='x', which='minor') ax.xaxis.set_minor_formatter(FormatStrFormatter("%i")) ax.xaxis.set_major_formatter(FormatStrFormatter("%i")) plt.legend(loc='best', ncol=2, frameon=False, prop={'size': 11}) if self.title: plt.title(self.title, fontsize=14) plt.tight_layout() plt.savefig( os.path.join( self.out_folder, '%s%s_spectrum.pdf' % (self.prefix, self.retrieval_type))) # contribution if plot_contrib: N = len(self.pickle_file['solutions'][0]['opacity_contrib']) for solution_idx, solution_val in enumerate( self.pickle_file['solutions']): fig = plt.figure(figsize=(7, 3.5)) ax = fig.add_subplot(111) plot_spectrum = np.zeros((len(fit_highres[:, 0]), 2)) plot_spectrum[:, 0] = fit_highres[:, 0] plot_spectrum[:, 1] = fit_highres[:, 1] plot_spectrum = plot_spectrum[plot_spectrum[:, 0].argsort( axis=0)] # sort in wavelength if self.pickle_file['params'][ 'in_opacity_method'][: 4] == 'xsec' and self.plot_resolution > 0: plot_spectrum = binspectrum(plot_spectrum, self.plot_resolution) #plt.plot(plot_spectrum[:,0], plot_spectrum[:,1], alpha=0.7, color='black', label='Fitted model') plt.errorbar(obs[:, 0], obs[:, 1], obs[:, 2], lw=1, color='black', alpha=0.5, ls='none', zorder=99, label='Observed') for idx, val in enumerate(self.pickle_file['solutions'] [solution_idx]['opacity_contrib']): plot_spectrum = self.pickle_file['solutions'][ solution_idx]['opacity_contrib'][val] plot_spectrum = plot_spectrum[plot_spectrum[:, 0].argsort( axis=0)] # sort in wavelength if self.pickle_file['params'][ 'in_opacity_method'][:4] == 'xsec': plot_spectrum = binspectrum(plot_spectrum, 300) plt.plot(plot_spectrum[:, 0], plot_spectrum[:, 1], color=self.cmap(float(idx) / N), label=val) plt.xlim( np.min(obs[:, 0]) - 0.05 * np.min(obs[:, 0]), np.max(obs[:, 0]) + 0.05 * np.max(obs[:, 0])) plt.xlabel('Wavelength ($\mu$m)') plt.ylabel('$(R_p/R_*)^2$') plt.tick_params(axis='x', which='minor') # set log scale only if interval is greater than 5 micron if np.max(obs[:, 0]) - np.min(obs[:, 0]) > 5: plt.xscale('log') plt.tick_params(axis='x', which='minor') ax.xaxis.set_minor_formatter(FormatStrFormatter("%i")) ax.xaxis.set_major_formatter(FormatStrFormatter("%i")) plt.tight_layout() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.7, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), ncol=1, prop={'size': 11}, frameon=False) if self.title: plt.title(self.title, fontsize=14) plt.savefig( os.path.join( self.out_folder, '%s%s_spectrum_contrib_sol%i.pdf' % (self.prefix, self.retrieval_type, solution_idx)))
def plot_forward_spectrum(self, plot_contrib=True): if self.plot_type == 'create_spectrum': fig = plt.figure(figsize=(5.3, 3.5)) ax = fig.add_subplot(111) spectrum = self.pickle_file['data']['spectrum'] if self.pickle_file['params'][ 'in_opacity_method'][: 4] == 'xsec' and self.plot_resolution > 0: # if opacity method is xsec, reduce resolution to plot_resolution spectrum = binspectrum(spectrum, self.plot_resolution) plt.plot(spectrum[:, 0], spectrum[:, 1], lw=1, color='black', zorder=99) plt.xlim( np.min(spectrum[:, 0]) - 0.05 * np.min(spectrum[:, 0]), np.max(spectrum[:, 0]) + 0.05 * np.max(spectrum[:, 0])) plt.xlabel('Wavelength ($\mu$m)') if self.pickle_file['params']['gen_type'] == 'emission': plt.ylabel('$F_p/F_*$') else: plt.ylabel('$(R_p/R_*)^2$') # set log scale only if interval is greater than 5 micron if np.max(spectrum[:, 0]) - np.min(spectrum[:, 0]) > 5: plt.xscale('log') plt.tick_params(axis='x', which='minor') ax.xaxis.set_minor_formatter(FormatStrFormatter("%i")) ax.xaxis.set_major_formatter(FormatStrFormatter("%i")) plt.legend(loc='best', ncol=2, frameon=False, prop={'size': 11}) if self.title: plt.title(self.title, fontsize=14) plt.tight_layout() plt.savefig( os.path.join( self.out_folder, '%s%s_spectrum.pdf' % (self.prefix, 'SPECTRUM_PLOT'))) # contribution if plot_contrib: # if (self.pickle_file['params']['gen_type'] == 'transmission' and not 'transmittance' in self.pickle_file['data']) or \ # (self.pickle_file['params']['gen_type'] == 'emission' and not 'contrib_func' in self.pickle_file['data']): # print('Cannot plot opacity contribution function/transmittance function as the specturm instance does not contain it') # print('Rerun create_spectrum.py with the flag --contrib_func for emission, and --trasmissivity in transmission') N = len(self.pickle_file['data']['opacity_contrib']) fig = plt.figure(figsize=(7, 3.5)) ax = fig.add_subplot(111) plt.plot(spectrum[:, 0], spectrum[:, 1], alpha=0.7, color='black', label='Forward model') for idx, val in enumerate( self.pickle_file['data']['opacity_contrib']): plot_spectrum = self.pickle_file['data'][ 'opacity_contrib'][val] plot_spectrum = plot_spectrum[plot_spectrum[:, 0].argsort( axis=0)] # sort in wavelength if self.pickle_file['params'][ 'in_opacity_method'][:4] == 'xsec': # if opacity method is xsec, reduce resolution to plot_resolution plot_spectrum = binspectrum(plot_spectrum, self.plot_resolution) plt.plot(plot_spectrum[:, 0], plot_spectrum[:, 1], color=self.cmap(float(idx) / N), label=val) plt.xlim( np.min(spectrum[:, 0]) - 0.05 * np.min(spectrum[:, 0]), np.max(spectrum[:, 0]) + 0.05 * np.max(spectrum[:, 0])) plt.xlabel('Wavelength ($\mu$m)') plt.ylabel('$(R_p/R_*)^2$') plt.tick_params(axis='x', which='minor') # set log scale only if interval is greater than 5 micron if np.max(spectrum[:, 0]) - np.min(spectrum[:, 0]) > 5: plt.xscale('log') plt.tick_params(axis='x', which='minor') ax.xaxis.set_minor_formatter(FormatStrFormatter("%i")) ax.xaxis.set_major_formatter(FormatStrFormatter("%i")) plt.tight_layout() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.7, box.height]) ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), ncol=1, prop={'size': 11}, frameon=False) if self.title: plt.title(self.title, fontsize=14) plt.savefig( os.path.join( self.out_folder, '%s%s_spectrum_contrib.pdf' % (self.prefix, 'SPECTRUM_PLOT')))
def plot(self): if not hasattr(self, 't'): self._construct_calibration_array() if not hasattr(self, 'cal'): self.calibrate() # plt.close('all') plt.figure() plt.plot(self.x, self.ly, 'k.') plt.xlabel('Ionogram Signal') plt.ylabel(r'$log_10 n_e / cm^{-3}$') plt.hlines(np.log10(150.), plt.xlim()[0], plt.xlim()[1], color='green', linestyles='dashed') for i in range(self.cal.shape[1]): c = self.cal[:,i] print(c) plt.plot((c[0], c[0]), (c[1]-c[2], c[1]+c[2]), 'r-') plt.plot(c[0], c[1], 'r.') p = np.polyfit(self.x, self.ly, 10) x = np.arange(plt.xlim()[0], plt.xlim()[1], 0.01) plt.plot(x, np.poly1d(p)(x), 'b-') plt.figure() dists = np.empty_like(self.t) sigmas = np.empty_like(self.t) for i in range(self.t.shape[0]): # if i % 10 != 0: continue val = 10.**np.interp(self.x[i], self.cal[0], self.cal[1]) err = 10.**np.interp(self.x[i], self.cal[0], self.cal[2]) plt.plot(self.y[i],val,'k.') plt.plot((self.y[i],self.y[i]), (val-err, val+err), 'k-') dists[i] = self.y[i] - val sigmas[i] = np.abs(np.log10(dists[i])/np.log10(err)) dists[i] /= self.y[i] x = np.array((10., 1E4)) plt.plot(x,x, 'r-') plt.plot(x, x*2., 'r-') plt.plot(x, x*.5, 'r-') plt.yscale('log') plt.xscale('log') plt.figure() plt.hist(np.abs(dists), bins=20, range=(0., 4.)) # plt.figure() # plt.hist(sigmas, bins=20) dists = np.abs(dists) # some statistics: s = 100. / float(self.t.shape[0]) print() print('%f%% with relative error < 0.1' % (np.sum(dists < 0.1) * s)) print('%f%% with relative error < 0.5' % (np.sum(dists < 0.5) * s)) print('%f%% with relative error < 1.0' % (np.sum(dists < 1.0) * s)) print('%f%% with relative error < 2.0' % (np.sum(dists < 2.0) * s)) print() print('%f%% with sigma < 1.0' % (np.sum(sigmas < 1.0) * s)) print('%f%% with sigma < 2.0' % (np.sum(sigmas < 2.0) * s)) print('%f%% with sigma < 4.0' % (np.sum(sigmas < 4.0) * s)) plt.show()
residual3_Ne7 = np.loadtxt("Newton/residual3(t)_Ne7_tol1e-2.out") # Dtlist = [5e-3, 1e-2, 2e-2, 5e-2, 1e-1, 2e-1, 5e-1, 1] ax = plt.subplot(111) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.85, box.height]) # Nlist = scipy.array([1e4,5e4, 1e5, 5e5, 1e6, 5e6, 1e7, 5e7]) # fig = plt.figure() # ax = fig.add_subplot(1,1,1) # ax = plt.subplot(111) # box = ax.get_position() # ax.set_position([box.x0, box.y0, box.width * 0.82, box.height]) plt.xscale("log") plt.yscale("log") plt.plot(Dtlist, residual1, "g--") plt.plot(Dtlist, residual2, "r--") plt.plot(Dtlist, residual3, "y--") plt.plot(Dtlist, residual_directsim, "c--") plt.plot(Dtlist, residual1_Ne6, "g-.") plt.plot(Dtlist, residual2_Ne6, "r-.") plt.plot(Dtlist, residual3_Ne6, "y-.") plt.plot(Dtlist, residual_directsim_Ne6, "c-.") plt.plot(Dtlist, residual1_Ne7, "g-", label=r"$k_{Newton} = 1$") plt.plot(Dtlist, residual2_Ne7, "r-", label=r"$k_{Newton} = 2$") plt.plot(Dtlist, residual3_Ne7, "y-", label=r"$k_{Newton} = 3$") plt.plot(Dtlist, residual_directsim_Ne7, "c-", label="direct sim")
def compare_start_vectors(BASE_PATH_RANDOM, BASE_PATH_WARM, INPUT_DATA_X, INPUT_DATA_y): for cv in range(5): params = glob.glob(os.path.join(BASE_PATH_RANDOM, "all/all/*")) for p in params: p = os.path.basename(p) print(p) pdf_path = "/neurospin/brainomics/2017_parsimony_settings/warm_restart/NUSDAST_30yo/VBM/no_covariates/warm_restart/influence_of_start_vector/cv0%s" % cv os.makedirs(pdf_path, exist_ok=True) pdf = PdfPages(os.path.join(pdf_path, "%s.pdf" % p)) fig = plt.figure(figsize=(11.69, 8.27)) snap_path_random = os.path.join(BASE_PATH_RANDOM, "cv0%s/all" % cv, p, "conesta_ite_snapshots/") conesta_ite_random = sorted(os.listdir(snap_path_random)) nb_conesta_random = len(conesta_ite_random) ite_final_random = np.load( os.path.join(snap_path_random, conesta_ite_random[-1])) beta_star_random = ite_final_random["beta"] gap_random = ite_final_random["gap"] mse_random = np.zeros((nb_conesta_random)) i = 0 for ite in conesta_ite_random: path = os.path.join(snap_path_random, ite) ite_random = np.load(path) mse_random[i] = sklearn.metrics.mean_squared_error( ite_random["beta"][:, 0], beta_star_random[:, 0]) i = i + 1 plt.plot(gap_random, label="Random start") for warm_num in range(5): PATH_WARM = BASE_PATH_WARM + "cv0%s_all_as_start_vector/model_selectionCV/cv0%s/all" % ( warm_num, cv) snap_path_warm = os.path.join(PATH_WARM, p, "conesta_ite_snapshots") conesta_ite_warm = sorted(os.listdir(snap_path_warm)) nb_conesta_warm = len(conesta_ite_warm) ite_final_warm = np.load( os.path.join(snap_path_warm, conesta_ite_warm[-1])) beta_star_warm = ite_final_warm["beta"] gap_warm = ite_final_warm["gap"] mse_warm = np.zeros((nb_conesta_warm)) i = 0 for ite in conesta_ite_warm: path = os.path.join(snap_path_warm, ite) ite_warm = np.load(path) mse_warm[i] = sklearn.metrics.mean_squared_error( ite_warm["beta"][:, 0], beta_star_warm[:, 0]) i = i + 1 plt.plot(gap_warm, label="Warm start: Beta from cv0%s/all" % warm_num) plt.xscale('log') plt.yscale('log') plt.xlabel("iterations") plt.ylabel(r"$gap$") plt.legend(prop={'size': 15}) plt.title(p) plt.tight_layout() pdf.savefig() plt.close(fig) pdf.close()
def eval(directory,h5fileName): h5file = h5py.File(directory+'/'+h5fileName,'r') snapshots = h5file.attrs["SnapshotTimes"] if not os.path.exists(directory+"/runRegFit"): os.makedirs(directory+"/runRegFit") borders = [] borders.append([]) borders.append([]) borders.append([]) borders.append([]) for times in snapshots: kSetName = str(times) + "/Observables/KVector" kvector = np.array(h5file[kSetName]) nSetname = str(times) + "/Observables/OccupationNumber" nvector = np.array(h5file[nSetname]) kvector = np.delete(kvector,0) nvector = np.delete(nvector,0) borders[0].append(kvector.min()) borders[1].append(kvector.max()) borders[2].append(nvector.min()) borders[3].append(nvector.max()) borders[0] = np.array(borders[0]).min() borders[1] = np.array(borders[1]).max() borders[2] = np.array(borders[2]).min() borders[3] = np.array(borders[3]).max() for times in snapshots: print("Fitting " + str(times)) kSetName = str(times) + "/Observables/KVector" kvector = h5file[kSetName] nSetname = str(times) + "/Observables/OccupationNumber" nvector = h5file[nSetname] obs = h5file[str(times) + "/Observables"] meta = obs.attrs["Meta"] the_time = meta[0] * 1000 #in ms kLog = np.log(kvector) kLog = np.delete(kLog,0) nLog = np.log(nvector) nLog = np.delete(nLog,0) total_bins = 200 bins = np.linspace(kLog.min(),kLog.max(),total_bins) delta = bins[1] - bins[0] idx = np.digitize(kLog,bins) running_median = [np.median(nLog[idx == k]) for k in range(total_bins)] data = running_median data = np.array(data) # data = np.nan_to_num(data) nans, x= nan_helper(data) data[nans]= np.interp(x(nans), x(~nans), data[~nans]) data = data.tolist() max_error = 0.1 #sliding window with regression figure() yscale('log') xscale('log') # subplot() # segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error, max_error) segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error, max_error) # segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error, max_error) # segments = segment.slidingwindowsegment(data, fit.interpolate, fit.sumsquared_error, max_error) # segments = segment.bottomupsegment(data, fit.interpolate, fit.sumsquared_error, max_error) # segments = segment.topdownsegment(data, fit.interpolate, fit.sumsquared_error, max_error) data = np.array(data) # data = np.exp(data) # bins = np.exp(bins) # segments = np.exp(segments) draw_plot(bins,data,"Regression Fit of the Spectrum at " + str(the_time) + " ms",borders) draw_segments(bins,segments) # fig = plt.figure() # fig.set_size_inches(10.0,12.0) # ax1 = fig.add_subplot(211) # subplot() # set_yscale('log') # set_xscale('log') # plot(kvector,nvector,'.',color='r',label=r'$R_x$ $GPE$') # ax2 = fig.add_subplot(212) # ax2.plot(bins-delta/2,running_median,'.') # plot(kLog,nLog) # # ax2.plot(kLog,interpolate.splev(kLog,tck,der=0)) # plt.tight_layout() # show() # # fig.text(.001,.001,txt) savefig(str(directory)+'/runRegFit/'+str(h5fileName)+'_'+str(times)+'_regfit.png',dpi=150) close()
i = 1 for line in f: s = line word = s.split() # separating words and frequencies wordList.append(word[0]) # storing words freqList.append(word[1]) # storing frequencies rankList.append(i) # storing ranks i = i + 1 for i in range(0, len(freqList)): freqList[i] = int(freqList[i]) rankList = np.log(rankList) # converting to log values freqList = np.log(freqList) pl.yscale("log") # defining log-log scale of graph pl.xscale("log") pl.plot(rankList, freqList) # pl.axis([0,75000,164438,23135851162]) po = np.polyfit(rankList, freqList, 5) # for the smoothed graph yfit = np.polyval(po, rankList) pl.plot(rankList, yfit) # plotting the graph pl.xlabel("Log Rank") pl.ylabel("Log Frequency") # defining labels, titles and legends pl.suptitle("Solution 3 : Frequency vs Rank") pl.legend(["Log Frequency vs Log Rank", "Smoothened Graph"]) pl.show()
def compute_perf(n_vect=None, p_vect=None, dur_vect=None, plot=True, save=True, device=None): n_cpus = int(os.cpu_count()) n_gpus = int(torch.cuda.device_count() * device == 'cuda') # Batch size if n_vect is None: n_vect = [1, 10, 20, 40] if len(n_vect): n_vect = np.array(n_vect, dtype=int) T = 200 colnames = ['version'] + [str(n) for n in n_vect] + [ 'number', 'time_length', 'cpus', 'gpus' ] fname = os.path.join('tests', 'batch_size_perf.csv') if os.path.exists(fname): df = pd.read_csv(fname, header=0, index_col=0, dtype={ 'version': str, 'number': int, 'time_length': int, 'cpus': int, 'gpus': int }) df = df.rename(columns=lambda x: x.strip()) if version in df.version.values: warnings.warn(f'Version label {version} already exists.') else: df = pd.DataFrame(columns=colnames) df = df.append({'version': version}, ignore_index=True) number = 200 times = [] tm = TransitModule(torch.linspace(5, 7, T)).to(device) for n in n_vect: tm.reset_params() tm.set_params(**get_pars(n)) tm = tm.to(device) times += [timeit.timeit(tm, number=number) / number] df.iloc[-1, range(1, 1 + len(n_vect))] = times df.loc[df.index[-1], 'cpus'] = int(n_cpus) df.loc[df.index[-1], 'gpus'] = int(n_gpus) df.loc[df.index[-1], 'number'] = int(number) df.loc[df.index[-1], ['time_length']] = int(T) if save: df.to_csv(fname) if plot: plt.figure() for i in range(len(df)): if i < len(df) - 1: kwargs = {'alpha': 0.7, 'linewidth': 0.7} else: kwargs = {'color': 'red', 'linewidth': 1.5} p = plt.plot(n_vect, df.iloc[i, range(1, 1 + len(n_vect))], label=str(df.version.iloc[i]) + '-' + str(df.index[i]) + f'({device})', **kwargs) plt.scatter(n_vect, df.iloc[i, range(1, 1 + len(n_vect))], s=7, c=p[0].get_color()) plt.xlabel('batch size ') plt.ylabel('exec time [s]') plt.legend() if save: plt.savefig(os.path.join('tests', 'batch_size_perf.png')) else: plt.show() # Transit duration if p_vect is None: p_vect = np.arange(2, 20, 7) if len(p_vect): p_vect = np.array(p_vect, dtype=int) number = 50 batch_size = 32 colnames = ['version'] + [str(P) for P in p_vect ] + ["batch_size", "number", 'cpus', 'gpus'] fname = os.path.join('tests', 'transit_dur_perf.csv') if os.path.exists(fname): df = pd.read_csv(fname, header=0, index_col=0, dtype={ 'version': str, 'cpus': int, 'gpus': int, 'batch_size': int, 'number': int }) df = df.rename(columns=lambda x: x.strip()) if version in df.version.values: warnings.warn(f'Version label {version} already exists.') else: df = pd.DataFrame(columns=colnames) df = df.append({'version': version}, ignore_index=True) times = [] durations = [] tm = TransitModule(torch.linspace(4, 6, 1000)).to(device) for P in p_vect: tm.reset_params() tm.set_params(**get_pars(batch_size)) tm.set_param('P', P) tm = tm.to(device) durations += [tm.duration[0]] times += [timeit.timeit(lambda: tm(), number=number) / number] df.iloc[df.index[-1], range(1, 1 + len(p_vect))] = times df.loc[df.index[-1], 'cpus'] = int(n_cpus) df.loc[df.index[-1], 'gpus'] = int(n_gpus) df.loc[df.index[-1], 'number'] = int(number) df.loc[df.index[-1], 'batch_size'] = int(batch_size) if save: df.to_csv(fname) if plot: plt.figure() for i in range(len(df)): if i < len(df) - 1: kwargs = {'alpha': 0.7, 'linewidth': 0.7} else: kwargs = {'color': 'red', 'linewidth': 1.5} p = plt.plot(p_vect, df.iloc[i, range(1, 1 + len(p_vect))], label=str(df.version.iloc[i]) + '-' + str(df.index[i]) + f'({device})', **kwargs) plt.scatter(p_vect, df.iloc[i, range(1, 1 + len(p_vect))], s=7, c=p[0].get_color()) plt.xlabel('Transit duration [d]') # plt.yscale('log') plt.ylabel('exec time [s]') plt.legend() if save: plt.savefig(os.path.join('tests', 'transit_dur_perf.png')) else: plt.show() # Time vector length if dur_vect is None: dur_vect = np.int_(10**torch.arange(6)) if len(dur_vect): dur_vect = np.array(dur_vect, dtype=int) number = 10 batch_size = 4 colnames = ['version'] + [str(int(T)) for T in dur_vect ] + ["batch_size", "number", 'cpus', 'gpus'] fname = os.path.join('tests', 'time_length_perf.csv') if os.path.exists(fname): df = pd.read_csv(fname, header=0, index_col=0, dtype={ 'version': str, 'cpus': int, 'gpus': int, 'batch_size': int, 'number': int }) df = df.rename(columns=lambda x: x.strip()) if version in df.version.values: warnings.warn(f'Version label {version} already exists.') else: df = pd.DataFrame(columns=colnames) df = df.append({'version': version}, ignore_index=True) times = [] tm = TransitModule() for T in dur_vect: tm.reset_time() tm.set_time(torch.linspace(4, 6, T)) tm.reset_params() tm.set_params(**get_pars(batch_size)) tm = tm.to(device) times += [timeit.timeit(lambda: tm(), number=number) / number] df.iloc[-1, range(1, 1 + len(dur_vect))] = times df.loc[df.index[-1], 'cpus'] = int(n_cpus) df.loc[df.index[-1], 'gpus'] = int(n_gpus) df.loc[df.index[-1], 'number'] = int(number) df.loc[df.index[-1], 'batch_size'] = int(batch_size) if save: df.to_csv(fname) if plot: plt.figure() for i in range(len(df)): if i < len(df) - 1: kwargs = {'alpha': 0.7, 'linewidth': 0.7} else: kwargs = {'color': 'red', 'linewidth': 1.5} p = plt.plot(dur_vect, df.iloc[i, range(1, 1 + len(dur_vect))], label=str(df.version.iloc[i]) + '-' + str(df.index[i]) + f'({device})', **kwargs) plt.scatter(dur_vect, df.iloc[i, range(1, 1 + len(dur_vect))], s=7, c=p[0].get_color()) plt.legend() plt.yscale('log') plt.xscale('log') plt.ylabel('exec time [s]') plt.xlabel('Time vector length [time steps]') if save: plt.savefig(os.path.join('tests', 'time_length_perf.png')) else: plt.show()
def compute_weights(correlation, verbose=0, uselatex=False): """ computes and returns weights of the same length as `correlation.correlation_fit` `correlation` is an instance of Correlation """ corr = correlation model = corr.fit_model model_parms = corr.fit_parameters ival = corr.fit_ival weight_data = corr.fit_weight_data weight_type = corr.fit_weight_type #parameters = corr.fit_parameters #parameters_range = corr.fit_parameters_range #parameters_variable = corr.fit_parameters_variable cdat = corr.correlation if cdat is None: raise ValueError("Cannot compute weights; No correlation given!") cdatfit = corr.correlation_fit x_full = cdat[:,0] y_full = cdat[:,1] x_fit = cdatfit[:,0] #y_fit = cdatfit[:,1] dataweights = np.ones_like(x_fit) try: weight_spread = int(weight_data) except: if verbose > 1: warnings.warn("Could not get weight spread for spline. Setting it to 3.") weight_spread = 3 if weight_type[:6] == "spline": # Number of knots to use for spline try: knotnumber = int(weight_type[6:]) except: if verbose > 1: print("Could not get knot number. Setting it to 5.") knotnumber = 5 # Compute borders for spline fit. if ival[0] < weight_spread: # optimal case pmin = ival[0] else: # non-optimal case # we need to cut pmin pmin = weight_spread if x_full.shape[0] - ival[1] < weight_spread: # optimal case pmax = x_full.shape[0] - ival[1] else: # non-optimal case # we need to cut pmax pmax = weight_spread x = x_full[ival[0]-pmin:ival[1]+pmax] y = y_full[ival[0]-pmin:ival[1]+pmax] # we are fitting knots on a base 10 logarithmic scale. xs = np.log10(x) knots = np.linspace(xs[1], xs[-1], knotnumber+2)[1:-1] try: tck = spintp.splrep(xs, y, s=0, k=3, t=knots, task=-1) ys = spintp.splev(xs, tck, der=0) except: if verbose > 0: raise ValueError("Could not find spline fit with "+\ "{} knots.".format(knotnumber)) return if verbose > 0: try: # If plotting module is available: name = "spline fit: "+str(knotnumber)+" knots" plotting.savePlotSingle(name, 1*x, 1*y, 1*ys, dirname=".", uselatex=uselatex) except: # use matplotlib.pylab try: from matplotlib import pylab as plt plt.xscale("log") plt.plot(x, ys, x, y) plt.show() except ImportError: # Tell the user to install matplotlib print("Couldn't import pylab! - not Plotting") ## Calculation of variance # In some cases, the actual cropping interval from ival[0] # to ival[1] is chosen, such that the dataweights must be # calculated from unknown datapoints. # (e.g. points+endcrop > len(correlation) # We deal with this by multiplying dataweights with a factor # corresponding to the missed points. for i in range(x_fit.shape[0]): # Define start and end positions of the sections from # where we wish to calculate the dataweights. # Offset at beginning: if i + ival[0] < weight_spread: # The offset that occurs offsetstart = weight_spread - i - ival[0] offsetcrop = 0 elif ival[0] > weight_spread: offsetstart = 0 offsetcrop = ival[0] - weight_spread else: offsetstart = 0 offsetcrop = 0 # i: counter on correlation array # start: counter on y array start = i - weight_spread + offsetstart + ival[0] - offsetcrop end = start + 2*weight_spread + 1 - offsetstart dataweights[i] = (y[start:end] - ys[start:end]).std() # The standard deviation at the end and the start of the # array are multiplied by a factor corresponding to the # number of bins that were not used for calculation of the # standard deviation. if offsetstart != 0: reference = 2*weight_spread + 1 dividor = reference - offsetstart dataweights[i] *= reference/dividor # Do not substitute len(y[start:end]) with end-start! # It is not the same! backset = 2*weight_spread + 1 - len(y[start:end]) - offsetstart if backset != 0: reference = 2*weight_spread + 1 dividor = reference - backset dataweights[i] *= reference/dividor elif weight_type == "model function": # Number of neighboring (left and right) points to include if ival[0] < weight_spread: pmin = ival[0] else: pmin = weight_spread if x_full.shape[0] - ival[1] < weight_spread: pmax = x_full.shape[0] - ival[1] else: pmax = weight_spread x = x_full[ival[0]-pmin:ival[1]+pmax] y = y_full[ival[0]-pmin:ival[1]+pmax] # Calculated dataweights for i in np.arange(x_fit.shape[0]): # Define start and end positions of the sections from # where we wish to calculate the dataweights. # Offset at beginning: if i + ival[0] < weight_spread: # The offset that occurs offsetstart = weight_spread - i - ival[0] offsetcrop = 0 elif ival[0] > weight_spread: offsetstart = 0 offsetcrop = ival[0] - weight_spread else: offsetstart = 0 offsetcrop = 0 # i: counter on correlation array # start: counter on correlation array start = i - weight_spread + offsetstart + ival[0] - offsetcrop end = start + 2*weight_spread + 1 - offsetstart #start = ival[0] - weight_spread + i #end = ival[0] + weight_spread + i + 1 diff = y - model(model_parms, x) dataweights[i] = diff[start:end].std() # The standard deviation at the end and the start of the # array are multiplied by a factor corresponding to the # number of bins that were not used for calculation of the # standard deviation. if offsetstart != 0: reference = 2*weight_spread + 1 dividor = reference - offsetstart dataweights[i] *= reference/dividor # Do not substitute len(diff[start:end]) with end-start! # It is not the same! backset = 2*weight_spread + 1 - len(diff[start:end]) - offsetstart if backset != 0: reference = 2*weight_spread + 1 dividor = reference - backset dataweights[i] *= reference/dividor elif weight_type == "none": pass else: # This means that the user knows the dataweights and already # gave it to us. weights = weight_data assert weights is not None, "User defined weights not given: "+weight_type # Check if these other weights have length of the cropped # or the full array. if weights.shape[0] == x_fit.shape[0]: dataweights = weights elif weights.shape[0] == x_full.shape[0]: dataweights = weights[ival[0]:ival[1]] else: raise ValueError, \ "`weights` must have length of full or cropped array." return dataweights
def agn_plot(): fig = plot.figure() yrange = [1e-14,1e-8]#[1e-8,1e-3]#[1e-13, 1e2] xrange = [1e-11,1e6] plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5) plot.annotate('AMEGO',xy=(0.7,2e-9),xycoords='data',fontsize=26,color='black') plot.annotate('PMN J0641-0320',xy=(1e-10,3e-9),xycoords='data',fontsize=16,color='black') plot.annotate('z=1.196',xy=(1e-10,1e-9),xycoords='data',fontsize=16,color='black') ## AGN a=ascii.read("data/marco_AGN_data_points_fig_6.txt",names=['logfreq','logflux']) logfreq=a['logfreq'] logflux=a['logflux'] h=6.6261e-27 #erg s erg2mev=624151. agn_energy=10**logfreq*h*erg2mev #Hz * erg s agn_flux=10**logflux#*erg2mev #erg cm-2 s-1 agn_energy=np.append(agn_energy,2.6e4) agn_flux=np.append(agn_flux,1e-14) b=ascii.read("data/0641_0320nustar_torus.dat",names=['x','y','z','tot','f','fbump','ftot','syn','ssc','ext1c']) logfreq=b['x'] lognufnu=b['ftot'] agn_energy2=10**logfreq*h*erg2mev agn_flux2=10**lognufnu#*erg2mev ssc=10**b['ssc']#*erg2mev syn=10**b['syn']#*erg2mev fbump=10**b['fbump']#*erg2mev f=10**b['f']#*erg2mev plot.plot(agn_energy,agn_flux,color='darkblue',lw=2) plot.plot(agn_energy2,agn_flux2,color='cornflowerblue',lw=2) #plot.plot(agn_energy2,fbump,'r--',color='lightblue',lw=2) #plot.plot(agn_energy2,ssc,'r--',color='cyan',lw=2) xrt=ascii.read("data/0641_0320xrt.dat",names=['logfreq','logflux']) xrt_energy=(10**xrt['logfreq'])*h*erg2mev xrt_flux=(10**xrt['logflux'])#*erg2mev #plot.scatter(xrt_energy,xrt_flux,color='blue') opt=ascii.read("data/0641_0320radio_optical.dat",names=['logfreq','logflux']) opt_energy=(10**opt['logfreq'])*h*erg2mev opt_flux=(10**opt['logflux'])#*erg2mev plot.scatter(opt_energy,opt_flux,color='blue') nustar=ascii.read("data/0641_0320nustar_ajello_fab.dat",names=['logfreq','logflux','logflux_yerr0','logflux_yerr1']) ns_energy=(10**nustar['logfreq'])*h*erg2mev ns_flux=(10**nustar['logflux'])#*erg2mev plot.scatter(ns_energy,ns_flux,color='blue') lat=ascii.read("data/LAT_spec_NuSTARobs2.txt",names=['ener','ed_ener','eu_ener','flux','ed_flux','eu_flux','ulim_flux','TS','Npred']) plot.scatter(lat['ener'][0:3],lat['flux'][0:3]/erg2mev) plot.errorbar(lat['ener'][0:3],lat['flux'][0:3]/erg2mev,xerr=[lat['ed_ener'][0:3],lat['eu_ener'][0:3]],yerr=[lat['ed_flux'][0:3]/erg2mev,lat['eu_flux'][0:3]/erg2mev],capsize=0,fmt="none") plot.xscale('log') plot.yscale('log') plot.ylim(yrange) plot.xlim(xrange) plot.xlabel(r'Energy (MeV)') plot.ylabel(r'$\nu$ F$_{\nu}$ (erg cm$^{-2}$ s$^{-1}$)') plot.savefig('../plots/agn_plot.eps', bbox_inches='tight') plot.savefig('../plots/agn_plot.png', bbox_inches='tight') plot.show() plot.close()
tsmap_P8_P301_Error90 = numpy.array(tsmap_P8_P301_Error90).astype(float) tsmap_P8_P301_Error95 = numpy.array(tsmap_P8_P301_Error95).astype(float) # Set the plotting format try: IDL.plotformat() # Plot P7_P203 TS vs P8_P301 TS GRB130427A = numpy.where(GRBs == '130427324') good = numpy.where((tsmap_P7_P203_MaxTS > 0) & (tsmap_P8_P301_MaxTS > 0))[0] plt.scatter(tsmap_P7_P203_MaxTS[good], tsmap_P8_P301_MaxTS[good]) plt.annotate('GRB130427A', xy=(tsmap_P7_P203_MaxTS[GRB130427A],tsmap_P8_P301_MaxTS[GRB130427A]), xytext=(-35,10), textcoords='offset points', ha='center', va='bottom') plt.plot([1,10000],[1,10000], '--') plt.xscale('log') plt.yscale('log') plt.xlim(1,10000) plt.ylim(1,10000) plt.xlabel('TS (P7_203)') plt.ylabel('TS (P8_301)') plt.show() # Plot P7_P203 90% Error vs P8_P301 90% Error good = numpy.where((tsmap_P7_P203_MaxTS > 0) & (tsmap_P8_P301_MaxTS > 0))[0] plt.scatter(tsmap_P7_P203_Error90[good], tsmap_P8_P301_Error90[good], c=numpy.log10(tsmap_P8_P301_MaxTS[good])) plt.annotate('GRB130427A', xy=(tsmap_P7_P203_Error90[GRB130427A],tsmap_P8_P301_Error90[GRB130427A]), xytext=(40,-10), textcoords='offset points', ha='center', va='bottom') cbar = plt.colorbar(pad = 0.02) cbar.set_label(r'log TS$_{\rm P8}$') plt.plot([0.001,10],[0.001,10], '--') plt.xlim(0.01,10)
# sys.exit() # data = np.loadtxt(datafile) # data2 = np.loadtxt(datafile2) # data3 = np.loadtxt(datafile3) # line1 = pl.plot(data[:,0], data[:,1] , 'r',marker='o', markersize=3, label=r'$\bar{\nu}=0.5$') # line2 = pl.plot(data2[:,0], data2[:,1], 'b',marker='o', markersize=3, label=r'$\bar{\nu}=0.05$' ) # line3 = pl.plot(data3[:,0], data3[:,1], 'g',marker='o',markersize=3, label= r'$\bar{\nu}=0.5$' ) #xmin =0 #xmax = 100 #ymin = 0 #ymax = 1 #plt.set_xlim(1,M) plt.xscale('log') plt.yscale('log') #pl.ylabel(r'$T^*$', fontsize = 20) plt.xlabel('m', fontsize = 16) #plt.ylabel(r'$||\mathbb{E}(\mathbf{J}(\mathbf{U}_{SDE}) \cdot \mathbf{V})||_2 - ||\mathbf{J}(\mathbf{U}_{FP}) \cdot \mathbf{V})||_2 $', fontsize = 20) plt.ylabel(r'$||\mathbb{E}\left[\mathbf{J}(\mathbf{U}_{SDE}) \cdot \mathbf{V} \right]- \mathbf{J}(\mathbf{U}_{FP}) \cdot \mathbf{V} )||_2 $', fontsize = 20) plt.legend([plot1, plot2], loc='best') #, ['x=0.05', 'x=0.01']) #, loc='best') #, numpoints=1) plt.legend([plot1, plot2], loc='best') #, ['x=0.05', 'x=0.01']) #, loc='best') #, numpoints=1) #pl.legend([line1, line2, line3], 'best') #pl.legend( loc='best', numpoints = 1 ) #first_legend = pl.legend([line1], loc=1) plt.legend(bbox_to_anchor=(1, 1), numpoints = 1 ) #pl.legend([line3], bbox_to_anchor=(0.3, 0.6)) #ax = pl.gca().add_artist(first_legend) #plt.savefig("results/Jve_sde-Jv_pde_compare.pdf")