def plotDepth(): sns.set_style("whitegrid", {"grid.color": "1", 'axes.linewidth': .5, "grid.linewidth": ".09"}) sns.set_context("notebook", font_scale=1.4, rc={"lines.linewidth": 2.5}) d = pd.read_pickle(utl.outpath + 'real/CD.F59.df').xs('D', level='READ', axis=1) (d.min(1) > 50).sum() (d > 50).sum().sum() z = pd.Series(np.ndarray.flatten(d.values)) fontsize = 6 mpl.rcParams.update({'font.size': fontsize}) plt.figure(figsize=(6, 4), dpi=300); plt.subplot(2, 2, 1); z.value_counts().sort_index().plot() plt.xlim([0, 200]); plt.xlabel('Depth'); plt.ylabel('Number of Measurments' + '\n (out of {:.1f}M)'.format(z.shape[0] / 1e6)); plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0)) plt.title('Scaled PDF') pplt.annotate('(A)', xpad=0.85, ypad=0.45, fontsize=fontsize) plt.axvline(50, linestyle='--', linewidth=1, color='k') pplt.setSize(plt.gca(), fontsize) plt.subplot(2, 2, 2); z.value_counts().sort_index().cumsum().plot() plt.xlim([0, 200]) plt.ylim([-3e5, 2.05 * 1e7]) plt.xlabel('Depth'); plt.title('Scaled CDF') pplt.annotate('(B)', xpad=0.85, ypad=0.45, fontsize=fontsize) plt.axvline(50, linestyle='--', linewidth=1, color='k') pplt.setSize(plt.gca(), fontsize) plt.subplot(2, 2, 3); d.min(1).value_counts().sort_index().plot() plt.xlim([0, 100]); plt.xlabel('Minimum Depth of each Variant'); plt.ylabel('Number of Variants' + '\n (out of {:.1f}M)'.format(d.shape[0] / 1e6)); plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0)) plt.rc('font', size=fontsize) pplt.annotate('(C)', xpad=0.85, ypad=0.45, fontsize=fontsize) plt.axvline(50, linestyle='--', linewidth=1, color='k') pplt.setSize(plt.gca(), fontsize) plt.subplot(2, 2, 4); d.min(1).value_counts().sort_index().cumsum().plot() plt.xlim([0, 60]) plt.ylim([0.25 * -1e5, plt.ylim()[1]]) plt.xlabel('Minimum Depth of each Variant'); plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0)) pplt.annotate('(D)', xpad=0.85, ypad=0.45, fontsize=fontsize) plt.axvline(50, linestyle='--', linewidth=1, color='k') pplt.setSize(plt.gca(), fontsize) plt.gcf().subplots_adjust(bottom=0.15) plt.gcf().tight_layout(h_pad=0.1) fontsize = 6 mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern'], 'size': fontsize}); mpl.rc('text', usetex=True) mpl.rcParams.update({'font.size': 1}) pplt.savefig('depth', 300) plt.show()
def analyseHill(ekindicts): """make hist of n coefficents for hill fits""" import pylab pylab.rc('text', usetex=True) f=pylab.figure() f.suptitle('n distributions- No linear (case 3)') i=1 for e in ekindicts: ekindata = ekindicts[e] proteins = ekindata.keys() nvals = [] for prot in proteins: edata = ekindata[prot] E = EkinProject(data=edata) for d in E.datasets: fdata = E.getMetaData(d) if fdata != None and fdata.has_key('model'): if fdata['model'] == 'Modified Hill': n=fdata['n'] if n<5 and n>-5: nvals.append(n) print 'n=', n ax = f.add_subplot(2,2,i) n, b, patches = pylab.hist(nvals, 30, histtype='bar', alpha=0.8) std = round(numpy.std(nvals), 2) ave = round(numpy.mean(nvals), 2) ax.set_title(e +' mean= '+str(ave)+r' $\sigma$= '+str(std)) i+=1 f.subplots_adjust(hspace=0.4) f.savefig('n_hist.png') return
def main(): src_cv_img_1 = cv.LoadImage("data/gorskaya/images/1/g126.jpg") src_cv_img_gray_1 = cv.LoadImage("data/gorskaya/images/1/g126.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) src_cv_img_2 = cv.LoadImage("data/gorskaya/images/1/g127.jpg") src_cv_img_gray_2 = cv.LoadImage("data/gorskaya/images/1/g127.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) (keypoints_1, descriptors_1) = \ cv.ExtractSURF(src_cv_img_gray_1, None, cv.CreateMemStorage(), (0, 30000, 3, 1)) (keypoints_2, descriptors_2) = \ cv.ExtractSURF(src_cv_img_gray_2, None, cv.CreateMemStorage(), (0, 30000, 3, 1)) print("Found {0} and {1} keypoints".format( len(keypoints_1), len(keypoints_2))) src_arr_1 = array(src_cv_img_1[:, :])[:, :, ::-1] src_arr_2 = array(src_cv_img_2[:, :])[:, :, ::-1] pylab.rc('image', interpolation='nearest') pylab.subplot(121) pylab.imshow(src_arr_1) pylab.plot(*zip(*[k[0] for k in keypoints_1]), marker='.', color='r', ls='') pylab.subplot(122) pylab.imshow(src_arr_2) pylab.plot(*zip(*[k[0] for k in keypoints_2]), marker='.', color='r', ls='') pylab.show()
def plot_all(): from pylab import xlabel, ylabel, xlim, ylim, subplot, title, rc rc('text', usetex=True) subplot(141) title(r'(a)') plot_poly('xi',1,[.1,.2,.3],[.1,-10,-.5]) xlabel(r'$\xi$') ylabel(r'$p_\xi$') subplot(142) title(r'(b)') plot_poly('xi',1,[.6,.2,.3],[.1,.5,-.5]) xlim(-3,3) ylim(-12,12) xlabel(r'$\xi$') ylabel(r'$p_\xi$') subplot(143) title(r'(c)') plot_poly('xi',1,[.8,.2,.3],[.1,.5,-.5]) xlim(-3,3) ylim(-12,12) xlabel(r'$\xi$') ylabel(r'$p_\xi$') subplot(144) title(r'(d)') plot_poly('eta',1,[70.,.2,.3],[.1,.5,-.5]) xlim(-10,10) ylim(-200,200) xlabel(r'$\eta$') ylabel(r'$p_\eta$')
def sim_results(obs, modes, stars, model, data): synth = model.generate_data(modes) synth_stats = model.summary_stats(synth) obs_stats = model.summary_stats(obs) f = plt.figure(figsize=(15,3)) plt.suptitle('Obs Cand.:{}; Sim Cand.:{}'.format(obs.size, synth.size)) plt.rc('legend', fontsize='xx-small', frameon=False) plt.subplot(121) bins = opt_bin(obs_stats[0],synth_stats[0]) plt.hist(obs_stats[0], bins=bins, histtype='step', label='Data', lw=2) plt.hist(synth_stats[0], bins=bins, histtype='step', label='Simulation', lw=2) plt.xlabel(r'$\xi$') plt.legend() plt.subplot(122) bins = opt_bin(obs_stats[1],synth_stats[1]) plt.hist(obs_stats[1], bins=np.arange(bins.min()-0.5, bins.max()+1.5, 1), histtype='step', label='Data', log=True, lw=2) plt.hist(synth_stats[1], bins=np.arange(bins.min()-0.5, bins.max()+1.5, 1), histtype='step', label='Simulation', log=True, lw=2) plt.xlabel(r'$N_p$') plt.legend()
def plot_earth_moon_obliquity(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. #params = {'m2':1.9e27,'r2':70000.e3,'rot2':0.00017585125448028,\ # 'r1':1,'rot1':6.28*100,'i_a':0.5,'ht': 0,\ # 'a':421700E3 / 4,'e':0.01,'i':0.8,'h':2} rot1 = 7.978282018665196e-08 rot2 = 2.972e-06 params = {'m2':1.98892e30,'r2':695500000.0,'rot2':rot2,\ 'r1':384748.e3,'rot1':rot1,'i_a':10. * 2*mpmath.pi()/360,'ht': 0,\ 'a':149597870700. * 0.1,'e':0.017,'i':7. * 2*mpmath.pi()/360,'h':mpmath.pi()/2} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (Ma)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2) xticks(arange(xlim()[0],xlim()[1],365*24*3600*100000000),[r'$'+str(int(_)*100)+r'$' for _ in [t[0] for t in enumerate(arange(xlim()[0],xlim()[1],365*24*3600*100000000))]])
def plot_io_obliquity_2(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. #params = {'m2':1.9e27,'r2':70000.e3,'rot2':0.00017585125448028,\ # 'r1':1,'rot1':6.28*100,'i_a':0.5,'ht': 0,\ # 'a':421700E3 / 4,'e':0.01,'i':0.8,'h':2} params = {'m2':1.9e27,'r2':70000.e3,'rot2':0.00017585125448028,\ 'r1':1821E3,'rot1':6.28/(21 * 3600),'i_a':0.17453,'ht': 0,\ 'a':421700E3,'e':0.05,'i':0.349,'h':0.35} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (Ma)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2) xticks(arange(xlim()[0],xlim()[1],365*24*3600*1000000),[r'$'+str(int(_)*1)+r'$' for _ in [t[0] for t in enumerate(arange(xlim()[0],xlim()[1],365*24*3600*1000000))]])
def plot_pulsar_obliquity(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. rot1 = 1.76e-4 rot2 = 1256. params = {'m2': 1.4 * 1.98892e30,'r2':20000.0,'rot2':rot2,\ 'r1':70000.e3/2,'rot1':rot1,'i_a':.5,'ht':0.,\ 'a':600000.e3,'e':.1,'i':0.8,'h':2} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (years)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2) xticks(arange(xlim()[0],xlim()[1],365*24*3600*20),[r'$'+str(int(_)*20)+r'$' for _ in [t[0] for t in enumerate(arange(xlim()[0],xlim()[1],365*24*3600*20))]]) return params = {'m2': 1.4 * 1.98892e30,'r2':20000.0,'rot2':rot2,\ 'r1':15000.e3/2,'rot1':rot1,'i_a':.5,'ht':0.,\ 'a':600000.e3,'e':.1,'i':0.8,'h':2} # Set parameters. sp.parameters = params # Period. ob = sp.obliquity_time ob_series2 = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series-ob_series2,'k-',linewidth=2)
def plot_probe_earth_obliquity(): from numpy import linspace, array, arange from pylab import rc, plot, xlim, xticks, xlabel, ylabel import matplotlib.pyplot as plt import mpmath rc('text', usetex=True) fig = plt.figure() # Initial params. #params = {'m2':5.97E24,'r2':6371.e3,'rot2':6.28/(24.*3600),\ #'r1':0.038,'rot1':6.28*3500/60,'i_a':0.5,'ht': 0,\ #'a':7027E3,'e':0.01,'i':0.8,'h':2} params = {'m2':5.97E24,'r2':6371.e3,'rot2':6.28/(24.*3600),\ 'r1':0.038,'rot1':6.28*3400/60,'i_a':90.007 * 2*mpmath.pi()/360,'ht': 0,\ 'a':7013E3,'e':0.0014,'i':mpmath.pi()/2,'h':-mpmath.pi()/2} # Set parameters. sp.parameters = params # Period. period = sp.wp_period ob = sp.obliquity_time lspace = linspace(0,5*period,250) xlim(0,float(lspace[-1])) xlabel(r'$\textnormal{Time (ka)}$') ylabel(r'$\textnormal{Obliquity }\left( ^\circ \right)$') ob_series = array([ob(t).real*(360/(2*mpmath.pi())) for t in lspace]) plot(lspace,ob_series,'k-',linewidth=2)
def usetexTrue(): py.rc('text', usetex=True) #py.rc('font', family='serif', size=16) py.rc('font', **{'family':'sans-serif', 'size':16}) py.rc('axes', titlesize=20, labelsize=20) py.rc('xtick', labelsize=16) py.rc('ytick', labelsize=16)
def plot_masses(ps, rs, scs, save=False, name=''): p.figure p.rc('text', usetex=True) p.rc('font', size=18) p.xlabel('$am_1 + am_2$') p.ylabel('$aM_{vs}$') legend = () # Pions. xs, ys, es = zip(*[q.pt for q in ps]) # Unpack data. legend += p.errorbar(xs, ys, es, fmt='o')[0], # Rhoxs. #xs, ys, es = zip(*[r.pt for r in rxs]) # Unpack data. #legend += p.errorbar(xs, ys, es, fmt='o')[0], # Rhos. xs, ys, es = zip(*[r.pt for r in rs]) # Unpack data. legend += p.errorbar(xs, ys, es, fmt='o')[0], # Scalars. xs, ys, es = zip(*[r.pt for r in scs]) # Unpack data. legend += p.errorbar(xs, ys, es, fmt='o')[0], p.legend(legend, ('$\pi$', r'$\rho$', '$a_0$'), 'best') if save: p.savefig(name) else: p.show()
def plot_dis_sessions(sessions): fig=plt.figure(figsize=(12, 10), dpi=80) leg=[] sum=np.zeros(22) count =np.zeros(22) for i in range(0,len(sessions)-1): for j in range(i+1,len(sessions)): userA= get_rating_vector(sessions[i]) userB= get_rating_vector(sessions[j]) if len(userA)<len(userB): userB=userB[:len(userA)] elif len(userA)>len(userB): userA=userA[:len(userB)] # leg.append( ' vs '.join( [ get_name_from_session(sessions[i]) , get_name_from_session(sessions[j])])) dis=[get_kappa(A, B) for A,B in zip(userA, userB)] dis.append(0) dis=dis[-1:] + dis[:-1] sum[:len(dis)] += np.array(dis) count[:len(dis)] +=1 idx= np.where(count) meandis= sum[idx]/count[idx] plt.plot(range(len(meandis)),meandis,linewidth=2) plt.rc('font', **{'family': 'serif', 'size':20, 'serif': ['Computer Modern']}) # plt.rc('text', usetex=True) plt.xlabel('Iteration',fontsize=30) plt.title('Avg Pairwise Disagreement between Subjects ',fontsize=30, y=1.02) plt.ylabel('$\kappa$',fontsize=30) plt.grid() plt.savefig(path+'dis.png') plt.xlim([-0.5,plt.xlim()[1]+1]) plt.ylim([-0.1,plt.ylim()[1]+1]) plt.legend(['Avg Pairwise Disagreement']) plt.show()
def plot_dwaf_data(realtime, file_name='data_plot.png', gauge=True): x = pl.date2num(realtime.date) y = realtime.q pl.clf() pl.figure(figsize=(7.5, 4.5)) pl.rc('text', usetex=True)# TEX fonts pl.plot_date(x,y,'b-',linewidth=1) pl.grid(which='major') pl.grid(which='minor') if gauge: pl.ylabel(r'Flow rate (m$^3$s$^{-1}$)') title = 'Real-time flow -- %s [%s]' % (realtime.station_id[0:6], realtime.station_desc) else: title = 'Real-time capacity -- %s [%s]' % (realtime.station_id[0:6], realtime.station_desc) pl.ylabel('Percentage of F.S.C') labeled_days = DayLocator(interval=3) ticked_days = DayLocator() dayfmt = DateFormatter('%d/%m/%Y') ax = pl.gca() ax.xaxis.set_major_locator(labeled_days) ax.xaxis.set_major_formatter(dayfmt) ax.xaxis.set_minor_locator(ticked_days) pl.xticks(fontsize=10) pl.yticks(fontsize=10) pl.title(title, fontsize=14) pl.savefig(file_name, dpi=100)
def myfig( fig, small=True, marker=False ): all_defaults = get_all_defaults( small ) params = get_params( small ) pylab.rcParams.update( params ) pylab.rc('font', **all_defaults[ 'font' ]) for i, line in enumerate( fig.axes[0].lines ): defaults = get_defaults( i ) keys = defaults.keys() keys.sort() for k in keys: k2=k if k.find('_marker') > -1: if marker: k2 = k[:-7] else: continue attr = getattr( line, 'set_' + k2, 'NOTFOUND' ) if attr == 'NOTFOUND': continue attr( defaults[ k ] ) leg = fig.axes[0].legend() if leg <> None: leg.set_visible(True) pylab.ion() return fig
def computePlotROCs(paths,mode,mutpaths): allalphas = set() exps = [] pylab.rc('text', usetex=True) for i in range(len(paths)): path = paths[i] mutpath = mutpaths[i] mutseq = open(mutpath,"r").readlines()[0] seq,struc,nbMut,profiles = loadMutationalProfiles(path) mutpos = getMutatedPositions(seq,mutseq) exps.append((path,mutseq,seq,struc,nbMut,profiles,mutpos)) allalphas.update(profiles.keys()) rocs = [] lbls = [] alphas = list(allalphas) alphas.sort() for alpha in alphas: c = [] delta = 0 for (path,mutseq,seq,struc,nbMut,profiles,mutpos) in exps: c += classifyProfile(profiles[alpha],seq,struc,mode,mutseq) delta += evaluatePrediction(profiles[alpha],seq,len(mutpos)) roc = ROCData(c) rocs.append(roc) lbls.append("$\\alpha=%.1f,$ {\\sf AUC} $=%.1f\\%%$"%(alpha,(100.*roc.auc(0)))) if len(paths)==1: cap = paths[0].split(os.sep)[-1] else: cap = "Multiple" print "%s\t%s\t%s\t%.2f\t%s\t%s\t%s" % (cap,mode,alpha,(100.*roc.auc(0)),delta,nbMut,len(mutpos)) plt = plot_multiple_roc(rocs,"", lbls) return plt
def setOptions(self, shape=None, grid=None, xscale=None, yscale=None, showlegend=None, legendloc=None, linewidth=None, graphtype=None, font=None, fontsize=None): """Set the options before plotting""" if shape != None: self.shape = shape if grid != None: self.grid = grid if xscale != None: self.xscale = xscale if yscale != None: self.yscale = yscale if showlegend != None: self.showlegend = showlegend if legendloc != None: self.legendloc = legendloc if linewidth != None: self.linewidth = linewidth if graphtype !=None: self.graphtype = graphtype if font != None: self.font = font if fontsize != None: self.fontsize = fontsize pylab.rc("font", family=self.font, size=self.fontsize) return
def plot_objectivefunctiontraces(results,evaluation,algorithms,filename='Like_trace'): import matplotlib.pyplot as plt from matplotlib import colors cnames=list(colors.cnames) font = {'family' : 'calibri', 'weight' : 'normal', 'size' : 20} plt.rc('font', **font) fig=plt.figure(figsize=(16,3)) xticks=[5000,15000] for i in range(len(results)): ax = plt.subplot(1,len(results),i+1) likes=calc_like(results[i],evaluation) ax.plot(likes,'b-') ax.set_ylim(0,25) ax.set_xlim(0,len(results[0])) ax.set_xlabel(algorithms[i]) ax.xaxis.set_ticks(xticks) if i==0: ax.set_ylabel('RMSE') ax.yaxis.set_ticks([0,10,20]) else: ax.yaxis.set_ticks([]) plt.tight_layout() fig.savefig(str(filename)+'.png')
def stellar_massftn(): gadget2msun=10.e10 boxsize = 47.0 max_mag=-16. min_mag = -23. nbins=14 hubble_h = 0.7 #subfind_folder = "/mnt/lustre/scratch/cs390/nIFTy/62.5_dmSF/outputs/" #ahf_folder = "/mnt/lustre/scratch/cs390/nIFTy/62.5_dm/outputs/" firstfile = first1 lastfile = last1 filter = LGalaxyStruct.properties_used filter['DiskMass'] = True filter['BulgeMass'] = True #file_prefix = "SA_z0.00" (nTrees,nGals,nTreeGals,gal) = read_lgal.readsnap_lgal(folder1,file_prefix,first1,last1,filter) massf = gadget2msun*gal['DiskMass']+gadget2msun*gal['BulgeMass'] mass = numpy.log10(massf) stellarmass = pylab.histogram(mass,bins=20,range=(9.0,14.0)) print stellarmass massftn_y = stellarmass[0] massftn_x = [] for i in range(len(stellarmass[0])): massftn_x.append((stellarmass[1][i]+stellarmass[1][i+1])/2.) delta_logM = massftn_x[1]-massftn_x[0] pylab.rc('text', usetex=True) fig = pylab.figure() ax = fig.add_subplot(111) ax.plot(massftn_x,massftn_y/boxsize1**3./delta_logM,'r-',label=label1) firstfile = first2 lastfile = last2 (nTrees,nGals,nTreeGals,gal) = read_lgal.readsnap_lgal(folder2,file_prefix,first2,last2,filter) massf = gadget2msun*gal['DiskMass']+gadget2msun*gal['BulgeMass'] mass = numpy.log10(massf) stellarmass = pylab.histogram(mass,bins=20,range=(9.0,14.0)) print stellarmass massftn_y = stellarmass[0] massftn_x = [] for i in range(len(stellarmass[0])): massftn_x.append((stellarmass[1][i]+stellarmass[1][i+1])/2.) ax.set_xlabel(r"$\log(M_\star/M_\odot$ $h)$") ax.set_ylabel(r"galaxies$/(Mpc^3 h^{-3})/\Delta \log(M_\star/M_\odot$ $h)$") ax.plot(massftn_x,massftn_y/boxsize2**3./delta_logM,'b-',label=label2) print "Stellar mass" for i in range(len(massftn_x)): print massftn_x[i],"\t",massftn_y[i]/boxsize**3./delta_logM ax.set_yscale("log") ax.legend(loc='upper right',ncol=1, fancybox=True) #pylab.show() pylab.savefig('stellar_mass.pdf',bbox_inches='tight')
def plot(t, inp, wavenet, param, orig=None, target=None, xlabel='', ylabel=''): plb.rc('font', family='serif') plb.rc('font', size=13) plb.figure('Апроксимация') plb.subplot(212) if target is not None: plb.plot(t, target, label='d(t)', linestyle=':') if orig is not None: plb.plot(t, orig, label='y(t)') plb.xlabel('t') plb.plot(t, wavenet.sim(t, inp), linestyle='--', label='$\hat{y}(t)$') plb.legend(loc=0) plb.subplot(211) plb.ylabel('Энергия ошибки, E') plb.plot(param['e'][0]) plb.xlabel('Эпохи, n') plb.figure("Основные веса") plb.subplot(131) plb.title('Масштабы, a') plb.plot(np.transpose(param['a'])) plb.subplot(132) plb.title('Сдвиги, b') plb.plot(np.transpose(param['b'])) plb.subplot(133) plb.title('Веса, w') plb.plot(np.transpose(param['w'])) plb.figure("Расширенные веса") plb.subplot(121) plb.title('Параметры, p') plb.plot(np.transpose(param['p'])) plb.subplot(122) plb.title('Смещение, c') plb.plot(param['c'][0])
def plot_measurements(time_points, ydata): pl.rc("text", usetex = True) pl.rc("font", family="serif") pl.subplot2grid((4, 2), (0, 0)) pl.plot(time_points, ydata[:,0]) pl.title("Considered measurement data") pl.xlabel("t") pl.ylabel("X", rotation = 0, labelpad = 20) pl.subplot2grid((4, 2), (1, 0)) pl.plot(time_points, ydata[:,1]) pl.xlabel("t") pl.ylabel("Y", rotation = 0, labelpad = 15) pl.subplot2grid((4, 2), (2, 0)) pl.plot(time_points, ydata[:,2]) pl.xlabel("t") pl.ylabel(r"\phi", rotation = 0, labelpad = 15) pl.subplot2grid((4, 2), (3, 0)) pl.plot(time_points, ydata[:,3]) pl.xlabel("t") pl.ylabel("v", rotation = 0, labelpad = 20) pl.subplot2grid((4, 2), (0, 1), rowspan = 4) pl.plot(ydata[:,0], ydata[:, 1]) pl.title("Considered racecar track (measured)") pl.xlabel("X") pl.ylabel("Y", rotation = 0, labelpad = 20) pl.show()
def plot_simulation_results_initial_controls(time_points, y_sim): pl.rc("text", usetex = True) pl.rc("font", family="serif") pl.subplot2grid((4, 2), (0, 0)) pl.plot(time_points, y_sim[:,0]) pl.title("Simulation results for initial controls") pl.xlabel("t") pl.ylabel("X", rotation = 0, labelpad = 20) pl.subplot2grid((4, 2), (1, 0)) pl.plot(time_points, y_sim[:,1]) pl.xlabel("t") pl.ylabel("Y", rotation = 0, labelpad = 15) pl.subplot2grid((4, 2), (2, 0)) pl.plot(time_points, y_sim[:,2]) pl.xlabel("t") pl.ylabel(r"\phi", rotation = 0, labelpad = 15) pl.subplot2grid((4, 2), (3, 0)) pl.plot(time_points, y_sim[:,3]) pl.xlabel("t") pl.ylabel("v", rotation = 0, labelpad = 20) pl.subplot2grid((4, 2), (0, 1), rowspan = 4) pl.plot(y_sim[:,0], y_sim[:, 1]) pl.title("Simulated race car path for initial controls") pl.xlabel("X") pl.ylabel("Y", rotation = 0, labelpad = 20) pl.show()
def fooplot(curs,ax1=None,ax2=None): #P.figure(figsize=(10,4.96)) if not ax1: ax1=P.axes([0.38,0.01,0.30,0.32]) if not ax2: ax2=P.axes([0.68,0.01,0.31,0.32],sharey=ax1) P.rc('xtick.major', pad=-12) P.rc('xtick', labelsize=10) curs.execute('SELECT DISTINCT sid,z,Ha_w from sel ORDER BY fuv_lum desc') data=curs.fetchall() for sid,z,ha in data: fuv,beta=curs.execute('select max(fuv_lum),beta from sel where objID=%s'%sid).fetchone() if ha < 70: color='g';marker='o' elif ha > 70 and ha < 140: color='y';marker='s' elif ha > 140: color='r';marker='D' ax1.plot(-1*(beta or N.nan),N.log10(fuv),color=color,marker=marker) ax2.plot(N.log10(ha),N.log10(fuv),color=color,marker=marker) P.setp(ax1.get_yticklabels(), visible=False) P.setp(ax2.get_yticklabels(), fontsize=10) ax1.xaxis.labelpad=-19 ax2.xaxis.labelpad=-19 ax2.set_xlabel(r'$\log\,W(H_\alpha)$') ax1.set_ylabel(r'$\log(L_{FUV})$') ax1.set_xlabel(r'$\beta$') ax1.axis([-0.4,-1.8,8.75,10.45]) ax2.axis([1.55,2.45,8.75,10.45]) ax1.set_yticks([8.8, 9.2, 9.6,10,10.4]) ax1.set_xticks([-0.5,-1.0,-1.5]) ax2.set_xticks([1.8,2.0,2.2,2.4])
def plot_ge( name_plot ): # distance between axes and ticks pl.rcParams['xtick.major.pad']='8' pl.rcParams['ytick.major.pad']='8' # set latex font pl.rc('text', usetex=True) pl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern'], 'size': 20}) pl.close('all') fig = pl.figure(figsize=(10.0, 5.0)) ax = fig.add_subplot(111) fig.suptitle('GE, Janaury-February 2007', fontsize=20, fontweight='bold') den = 0.008 N_max_day = 50 sel_side = (df_ge.side==1) & (df_ge.day_trade_n < N_max_day) df_ge_buy = df_ge[sel_side] pl.hlines(df_ge_buy.day_trade_n, df_ge_buy.mm_s, df_ge_buy.mm_e, linestyles='solid', lw= pl.array(df_ge_buy.eta/den), color='blue', alpha=0.3) sel_side = (df_ge.side==-1) & (df_ge.day_trade_n < N_max_day) df_ge_sell = df_ge[sel_side] pl.hlines(df_ge_sell.day_trade_n, df_ge_sell.mm_s, df_ge_sell.mm_e, linestyles='solid', lw= pl.array(df_ge_sell.eta/den), color='red', alpha=0.3) ax.set_xlim([0,390]) ax.set_ylim([N_max_day,-1]) ax.set_aspect('auto') ax.set_xlabel('Trading minute') ax.set_ylabel('Trading day') pl.subplots_adjust(bottom=0.15) pl.savefig("../plot/" + name_plot + ".pdf")
def plot_benchmark_time_per_frame(ball_quantities, benchmark_results, title, pic_filename): import pylab fig = pylab.figure(figsize=(6.0, 11.0)) #size in inches fig.suptitle(title, fontsize=12) pylab.axis([0.0, 500, -10 * len(benchmark_results), 150]) # axis extension pylab.rc('axes', linewidth=3) # thickening axes # axis labels pylab.xlabel(r"num balls", fontsize = 12, fontweight='bold') pylab.ylabel(r"time per frame in ms", fontsize = 12, fontweight='bold') # plot cases x = ball_quantities case_names = [ k for k in benchmark_results] case_names.sort() colors = [ 'b', 'r', 'g', 'm', '#95B9C7', '#EAC117', '#827839' ] for case, color in zip(case_names, colors): # convert time to ms res = [ v*1000 for v in benchmark_results[case]] pylab.plot(x, res, color=color, label=case) # show the plot labels pylab.legend(loc='lower center') #pylab.show() # show the figure in a new window pylab.savefig(pic_filename)
def plot_tmp_imp( name_plot ): # distance between axes and ticks pl.rcParams['xtick.major.pad']='8' pl.rcParams['ytick.major.pad']='8' # set latex font pl.rc('text', usetex=True) pl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern'], 'size': 20}) # plotting x_plf = pow(10,pl.linspace(-6,0,1000)) pl.clf() p_pl, = pl.plot(x_plf,ff_pl(x_plf,par_pl[0],par_pl[1]), ls='--', color='Red') p_lg, = pl.plot(x_plf,ff_lg(x_plf,par_lg[0],par_lg[1]), ls='-', color='RoyalBlue') p_points, = pl.plot(df_imp_1d.pi,df_imp_1d.imp,'.', color='Black',ms=10) pl.xscale('log') pl.yscale('log') pl.xlabel('$\phi$') pl.ylabel('$\mathcal{I}_{tmp}(\Omega=\{ \phi \})$') pl.grid() pl.axis([0.00001,1,0.0001,0.1]) leg_1 = '$\hat{Y} = $' + str("%.4f" % round(par_pl[0],4)) + '$\pm$' + str("%.4f" % round(vv_pl[0][0],4)) + ' $\hat{\delta} = $' + str("%.4f" % round(par_pl[1],4)) + '$\pm$' + str("%.4f" % round(vv_pl[1][1],4)) + ' $E_{RMS} = $' + str("%.4f" % round(pl.sqrt(chi_pl/len(df_imp_1d.imp)),4)) leg_2 = '$\hat{a} = $' + str("%.3f" % round(par_lg[0],3)) + '$\pm$' + str("%.3f" % round(vv_lg[0][0],3)) + ' $\hat{b} = $' + str("%.0f" % round(par_lg[1],3)) + '$\pm$' + str("%.0f" % round(vv_lg[1][1],3)) + ' $E_{RMS} = $' + str("%.4f" % round(pl.sqrt(chi_lg/len(df_imp_1d.imp)),4)) l1 = pl.legend([p_pl,p_lg], ['$f(\phi) = Y\phi^{\delta}$', '$g(\phi)= a \log_{10}(1+b\phi)$'], loc=2, prop={'size':15}) l2 = pl.legend([p_pl,p_lg], [leg_1 ,leg_2 ], loc=4, prop={'size':15}) pl.gca().add_artist(l1) pl.subplots_adjust(bottom=0.15) pl.subplots_adjust(left=0.17) pl.savefig("../plot/" + name_plot + ".pdf")
def graph_file(obj, func_name, param, range, highlights): ## plots the named function on obj, taking param over its range (start, end) ## highlights are pairs of (x_value, "Text_to_show") to annotate on the graph plot ## saves the plot to a file and returns full file name import numpy, pylab func = getattr(obj, func_name) f = numpy.vectorize(func) x = numpy.linspace(*range) y = f(x) pylab.rc('font', family='serif', size=20) pylab.plot(x, y) pylab.xlabel(param) pylab.ylabel(func_name) hi_pts = [pt for pt, txt in highlights] pylab.plot(hi_pts, f(hi_pts), 'rD') for pt, txt in highlights: pt_y = func(pt) ann = txt + "\n(%s,%s)" % (pt, pt_y) pylab.annotate(ann, xy=(pt, pt_y)) import os, time file = os.path.dirname(__file__) + "/generated_graphs/%s.%s.%s.pdf" % (type(obj).__name__, func_name, time.time()) pylab.savefig(file, dpi=300) pylab.close() return file
def plot(t, inp, target, wavenet, param, orig=None, xlabel='', ylabel=''): plb.rc('font', family='serif') plb.rc('font', size=13) plb.figure('Апроксимация') plb.subplot(212) plb.plot(t, target, label='Модельный сигнал') if orig is not None: plb.plot(t, orig, label='Оригинал') plb.plot(t, wavenet.sim(t, inp), linestyle='--', label='Аппроксимация') plb.legend(loc=0) plb.subplot(211) plb.title('Суммарная квадратичная ошибка') plb.plot(param['e'][0]) plb.xlabel('Эпохи') plb.figure("Основные веса") plb.subplot(131) plb.title('Масштабы, a') plb.plot(np.transpose(param['a'])) plb.subplot(132) plb.title('Сдвиги, b') plb.plot(np.transpose(param['b'])) plb.subplot(133) plb.title('Веса, w') plb.plot(np.transpose(param['w'])) plb.figure("Расширенные веса") plb.subplot(121) plb.title('Параметры, p') plb.plot(np.transpose(param['p'])) plb.subplot(122) plb.title('Смещение, c') plb.plot(param['c'][0])
def generate(sample_sequence_tuples, save_path = None): samples = [x[0] for x in sample_sequence_tuples] number_of_sequences = [x[1] for x in sample_sequence_tuples] pos = pylab.arange(len(number_of_sequences))+.5 width = len(samples) / 5 if width < 5: width = 5 if width > 15: width = 15 fig = pylab.figure(figsize=(width, 4)) pylab.rcParams.update({'axes.linewidth' : 0, 'axes.axisbelow': False}) pylab.rc('grid', color='0.80', linestyle='-', linewidth=0.1) pylab.grid(True) pylab.bar(pos, number_of_sequences, align='center', color='#EFADAD', linewidth=0.1) pylab.xticks(pos, samples, rotation=90, size='xx-small') pylab.xlim(xmax=len(samples)) pylab.yticks(size='xx-small') pylab.ylabel('Number of sequences (%d samples)' % len(samples), size="small") if save_path: pylab.savefig(save_path) else: pylab.show()
def plot_color_maps(reverse=False): """ Simple plotting function to run through and plot each color map Help for choosing which colormap to use """ import pylab as plt from numpy import outer plt.rc('text', usetex=False) a=outer(plt.ones(10,),plt.arange(0,1,0.01)) plt.figure(figsize=(5,15)) plt.subplots_adjust(top=0.8,bottom=0.08,left=0.03,right=0.99) if reverse: maps=[m for m in plt.cm.datad] rr = 2 else: maps=[m for m in plt.cm.datad if not m.endswith("_r")] rr = 1 maps.sort() l=len(maps)+1 title_dict = {'fontsize': 10, 'verticalalignment': 'center', 'horizontalalignment': 'left'} for i, m in enumerate(maps): plt.subplot(l,rr,i+1) plt.axis("off") plt.imshow(a,aspect='auto',cmap=plt.get_cmap(m),origin="lower") plt.text(1.01,0.5,m,fontdict=title_dict,transform=plt.gca().transAxes)
def plot(self): specs = pl.array( list(set([ l['spec'] for l in self.lines ])) ) specs.sort() self.specs = specs pl.figure() pl.hold('on') pl.grid('on') pl.jet() lines = [] lines_spec = list(pl.zeros(len(specs))) for i in range(0,len(self.lines)): ispc = pl.find( specs == self.lines[i]['spec'] ) self.colr = pl.cm.get_cmap()( float(ispc)/len(specs) ) wl = self.lines[i]['wave'] ri = float(self.lines[i]['rel_int']) lines.append( pl.plot( [wl, wl], [0., ri if not isnan(ri) else 0.], '.-', color=self.colr )[0] ) lines_spec[ispc] = lines[-1] datacursor(lines,formatter='x={x:8.3f}\ny={y:8.3f}'.format) pl.rc('text',usetex=True) pl.xlabel('$\lambda ~ [\AA]$') pl.ylabel('relative intensity [arb]') pl.title('Spectrum for '+self.spec+' from NIST ASD') if len(specs) > 1: pl.legend( lines_spec,specs ) pl.show()
def rc(self, *args, **kwargs): pl.rc(*args, **kwargs)
order='F') msk[msk < 0.01] = np.nan pl.ylim([0, ylimit]) pl.imshow(msk, cmap='hot', alpha=.3) pl.axis('off') pl.subplot(3, 1, 3) pl.imshow(np.reshape(A.tocsc()[:, sure_in_idx[idx_to_show]].mean(-1), dims, order='F'), cmap='hot') pl.ylim([0, ylimit]) pl.axis('off') font = {'family': 'Myriad Pro', 'weight': 'regular', 'size': 30} pl.rc('font', **font) #% pl.figure() counter = 0 for iid in sure_in_idx[np.hstack([idx_to_show, 7])]: counter += 1 pl.subplot(7, 7, counter) mmsk = np.reshape(A.tocsc()[:, iid].todense(), dims, order='F') cx, cy = scipy.ndimage.measurements.center_of_mass(np.array(mmsk)) cx = np.int(cx) cy = np.int(cy) print((cx, cy)) pl.imshow(mmsk[np.maximum(cx - 15, 0):cx + 15, np.maximum(cy - 15, 0):cy + 15], cmap='gray')
def createKerdenSOMPlots(self): apFile.removeFilePattern( os.path.join(self.params['rundir'], self.spectraTemporalFilesMask + ".png")) #logging.debug('Inside createKerdenSOMPlots') apDisplay.printMsg("Create Plots") codeVectorFileName = os.path.join(self.params['rundir'], self.timestamp + '.cod') f1 = open(codeVectorFileName, 'r') #Read first line, I need number of harmonic plus size line = f1.readline() splitline = line.split() numberHarmonic = int(splitline[0]) xx = int(splitline[2]) yy = int(splitline[3]) numberCodevectors = xx * yy xmin = int(self.params['spectralowharmonic']) xmax = int(self.params['spectrahighharmonic']) #array with x and y values xvalues = [] #fill x array with harmonic number for colNo in pylab.arange(xmin, xmax + 1): xvalues.append(colNo) #figure size in inches pylab.rcParams['figure.figsize'] = 1, 1 pylab.rc("lines", linewidth=1.5) pylab.rc(('xtick', 'ytick', 'axes'), labelsize=4.0) #fontsize #read code vector #compute y maximum ymax = 0. ymin = 150. for rowNo in range(numberCodevectors): line = f1.readline() splitLine = line.split() for colNo in pylab.arange(numberHarmonic): yval = float(splitLine[colNo]) if ymax < yval: ymax = yval if ymin > yval: ymin = yval f1.close() ymax = math.ceil(ymax) + 1 print "ymax ", ymax ymin = max(math.floor(ymin) - 1, 0) print "ymin ", ymin f1 = open(codeVectorFileName, 'r') #skip first line line = f1.readline() for rowNo in range(numberCodevectors): line = f1.readline() splitLine = line.split() #print line data = [] for colNo in pylab.arange(numberHarmonic): data.append(float(splitLine[colNo])) print xvalues print data #clear previous plot pylab.clf() lines = pylab.plot(xvalues, data) pylab.ylim(ymin, ymax) pylab.xlim(xmin, xmax) pylab.xlabel('fold symmetry') pylab.ylabel('likelihood') pylab.xticks(xvalues) basefilename = os.path.join( self.params['rundir'], self.spectraTemporalFiles % (int(rowNo / yy), rowNo % xx)) pylab.savefig(basefilename + ".png", dpi=256, format='png')
def plotter(xaxis, yaxis, options, mode, colorless=0, legend=''): ''' FORMAT AXES ''' matplotlib.rc('xtick', labelsize=16) matplotlib.rc('ytick', labelsize=16) font = {'weight': 'bold', 'size': 16} matplotlib.rc('font', **font) #ax=plt.axes(frameon=False) ax = plt.axes() pylab.rc("axes", linewidth=2.0) yaxislabel = options.yaxislabel xaxislabel = options.yaxislabel if not yaxislabel: yaxislabel = "Y" if not xaxislabel: xaxislabel = "X" if options.logplot and yaxislabel: if "log" not in yaxislabel and "LOG" not in yaxislabel: yaxislabel = 'LOG(' + yaxislabel + ')' pylab.xlabel(options.xaxislabel, fontsize=14, fontweight='bold') pylab.ylabel(options.yaxislabel, fontsize=14, fontweight='bold') #pylab.ylim([-1,ylimvalmax+10]) #xmin = min(xaxis) #pylab.xlim([-1,float(max(xaxis))+10]) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() #ax.axes.get_xaxis().set_visible(True) #ax.axes.get_yaxis().set_visible(True) #xmin, xmax = ax.get_xaxis().get_view_interval() #ymin, ymax = ax.get_yaxis().get_view_interval() #ax.add_artist(Line2D((xmin, xmax+10), (ymin, ymin), color='k', linewidth=4)) #ax.add_artist(Line2D((xmin, xmin), (ymin, ymax+10), color='k', linewidth=4)) ax.tick_params(axis='both', reset=False, which='both', length=8, width=3) markernum = '' idee = '' #print "BOLD IS ON!" LW = 3 if not markernum: LW = 2 if colorless: #if not yminnonconvex: # # print "in colorless plot, linest is", linest if mode != 'scatter': linest = '-' plt.plot(xaxis, yaxis, linewidth=LW, linestyle=linest, alpha=1, color='k', zorder=0, label=idee) else: plt.scatter(xaxis, yaxis, marker='x', edgecolor='k', alpha=1, zorder=1, s=40, facecolor='white', linewidth=2) # if idee and options.legend: # #print "Idee is", idee # legend(loc='upper left') #elif yminnonconvex: #plt.plot(xaxis, yaxis, linewidth=LW,linestyle=linest,alpha=1,color='k',zorder=0,label=idee) #plt.scatter(xaxis,yaxis,marker='x',edgecolor='k',alpha=1,zorder=1,s=40,facecolor='white',linewidth=2) if idee and legend: print("Idee is", idee) legend(loc='upper left') #if mark: # plt.scatter(xaxis,yaxis,marker=mark,edgecolor='k',alpha=1,zorder=1,s=40,facecolor='white',linewidth=2) else: #if not yminnonconvex: # print "I did NOT receive yminnonxonvex" # if mode != 'scatter': plt.plot(xaxis, yaxis, linewidth=LW, alpha=1, zorder=0, label=idee) else: plt.scatter(xaxis, yaxis, marker='x', alpha=1, zorder=1, s=40, linewidth=2) # # if idee and options.legend: # print "Idee is", idee # legend(loc='upper left') #elif yminnonconvex: # print "I DID receive yminnonxonvex" # plt.plot(xaxis, yaxis, linewidth=LW,linestyle=linest,alpha=1,zorder=0,label=idee) if idee and legend: print("Idee is", idee) legend(loc='upper left') #plt.scatter(xaxis,yaxis,marker='x',alpha=0.5,zorder=1,s=40,linewidth=2) #if mark: # plt.scatter(xaxis,yaxis,marker=mark,alpha=0.5,zorder=1,s=40,linewidth=2) return
def spinparam_2x2(): #Ez=[0.00, 0.05, 0.10, 0.20, 0.25, 0.30, 0.40, 0.50, 0.75, 1.00, 3.00] #Rz=[0.00, 0.25, 0.31, 0.39, 0.50, 0.75, 1.00, 1.50, 2.01, 2.64, 3.00, 3.55, 4.00, 5.06] #Rz=[1.00, 1.50, 2.01, 2.64, 3.00, 3.55, 4.00, 5.06] #Az=[1.532, 1.82, 3.00, 4.94] #Artz=[2.03, 2.99, 4.0] #Fz=[0.80, 0.90, 1.00, 1.10, 1.20, 1.30, 1.40, 1.50, 1.60, 1.70, 1.80, 1.90, 2.00, 2.20, 2.50, 2.60, 2.80, 3.00, 3.40, 3.82, 3.90, 4.00, 4.50, 5.00] #Fz=[1.00, 1.10, 1.20, 1.30, 1.40, 1.50, 1.60, 1.70, 1.80, 1.90, 2.00, 2.20, 2.50, 2.60, 2.80, 3.00, 3.40, 3.82, 3.90, 4.00, 4.50, 5.00] #Fz=[1.70, 1.80, 1.90, 2.00, 2.20, 2.50, 2.60, 2.80, 3.00, 3.40, 3.82, 3.90, 4.00, 4.50, 5.00] Ez = [1.00, 3.00] Rz = [1.00, 1.50, 2.01, 2.64, 3.00] Az = [1.532, 1.82, 2.0, 3.00] Artz = [2.03, 2.33, 2.99] Fz = [1.00, 1.20, 1.40, 1.60, 1.80, 2.00, 2.20, 2.50, 2.60, 2.80, 3.00] E_LBtime = cosmo.lookback_time(Ez) R_LBtime = cosmo.lookback_time(Rz) A_LBtime = cosmo.lookback_time(Az) Art_LBtime = cosmo.lookback_time(Artz) F_LBtime = cosmo.lookback_time(Fz) endstring = "_spinparameters.txt" #this will average over everything in the halo #create 2-D numpy arrays. First index is redshift, 2nd index is which lambda parameter: Xz_array[redshift,lambda] simulation = "Enzo" redshift_array = Ez Ez_array = np.zeros(E_LBtime.shape[0] * 7).reshape(E_LBtime.shape[0], 7) for i in range(E_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Ez_array[i][:] = np.loadtxt(fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Ramses" redshift_array = Rz Rz_array = np.zeros(R_LBtime.shape[0] * 7).reshape(R_LBtime.shape[0], 7) for i in range(R_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Rz_array[i][:] = np.loadtxt(fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Art" redshift_array = Artz Artz_array = np.zeros(Art_LBtime.shape[0] * 7).reshape( Art_LBtime.shape[0], 7) for i in range(Art_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Artz_array[i][:] = np.loadtxt( fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Arepo" redshift_array = Az Az_array = np.zeros(A_LBtime.shape[0] * 7).reshape(A_LBtime.shape[0], 7) for i in range(A_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Az_array[i][:] = np.loadtxt(fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Fire" redshift_array = Fz Fz_array = np.zeros(F_LBtime.shape[0] * 7).reshape(F_LBtime.shape[0], 7) for i in range(F_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Fz_array[i][:] = np.loadtxt(fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) endstring = "_spinparameters_haloONLY.txt" #this will average over everything in the halo but exclude the innter 0.1Rvir of material #create 2-D numpy arrays. First index is redshift, 2nd index is which lambda parameter: Xz_array[redshift,lambda] simulation = "Enzo" redshift_array = Ez Ez_array_H = np.zeros(E_LBtime.shape[0] * 7).reshape(E_LBtime.shape[0], 7) for i in range(E_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Ez_array_H[i][:] = np.loadtxt( fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Ramses" redshift_array = Rz Rz_array_H = np.zeros(R_LBtime.shape[0] * 7).reshape(R_LBtime.shape[0], 7) for i in range(R_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Rz_array_H[i][:] = np.loadtxt( fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Art" redshift_array = Artz Artz_array_H = np.zeros(Art_LBtime.shape[0] * 7).reshape( Art_LBtime.shape[0], 7) for i in range(Art_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Artz_array_H[i][:] = np.loadtxt( fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Arepo" redshift_array = Az Az_array_H = np.zeros(A_LBtime.shape[0] * 7).reshape(A_LBtime.shape[0], 7) for i in range(A_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Az_array_H[i][:] = np.loadtxt( fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) simulation = "Fire" redshift_array = Fz Fz_array_H = np.zeros(F_LBtime.shape[0] * 7).reshape(F_LBtime.shape[0], 7) for i in range(F_LBtime.shape[0]): fname = "saved_data/" + simulation + "_z" + str( abs(round(redshift_array[i], 2))) + endstring names, trash, Fz_array_H[i][:] = np.loadtxt( fname, dtype={ 'names': ('variable_name', 'equalsign', 'value'), 'formats': ('|S15', '|S8', np.float) }, unpack=True) print Ez_array[:, 5] print Ez_array_H[:, 5] #FOR PLOTS RECALL: First index is redshift, 2nd index is which lambda parameter: Xz_array[redshift,lambda] #FOR lambda variables, recall the order is: tot, dark, stars, particles, gas, cold, hot z_tick_values = [4, 3, 2, 1.5, 1.25, 1.0, 0.9] #max and min of this are limits of the plot. z_tick_value_labels = [' ', '3', '2', '1.5', '1.25', '1', ' '] #max and min of this are limits of the plot. z_tick_locations = cosmo.lookback_time(z_tick_values).value LBT_max = cosmo.lookback_time(np.max(z_tick_values)).value LBT_min = cosmo.lookback_time(np.min(z_tick_values)).value #make a 2x2 shared axes plot of the following 4 individual plots (shown afterwards) #fig = plt.figure(figsize=(10,10)) fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(3, 2, sharex='col', sharey='row', figsize=(10, 15)) fig.subplots_adjust(hspace=0) fig.subplots_adjust(wspace=0) ax1.set_ylabel('Spin Parameter $\lambda$') ax3.set_ylabel('Spin Parameter $\lambda$') ax5.set_ylabel('Spin Parameter $\lambda$') ax3.set_xlabel('Lookback Time [Gyr]') ax4.set_xlabel('Lookback Time [Gyr]') ax5.set_xlabel('Lookback Time [Gyr]') ax6.set_xlabel('Lookback Time [Gyr]') ax1.set_ylim(0.01, 0.3) ax3.set_ylim(0.00, 0.3) ax5.set_ylim(0.00, 0.3) ax3.set_xlim(LBT_max, LBT_min) ax4.set_xlim(LBT_max, LBT_min) ax1b = ax1.twiny() ax1b.set_xlabel('Redshift') ax1b.set_xlim(LBT_max, LBT_min) ax1b.set_xticks(z_tick_locations) ax1b.set_xticklabels(z_tick_value_labels) ax2b = ax2.twiny() ax2b.set_xlabel('Redshift') ax2b.set_xlim(LBT_max, LBT_min) ax2b.set_xticks(z_tick_locations) ax2b.set_xticklabels(z_tick_value_labels) ax1.plot(E_LBtime, Ez_array[:, 1], color='k', linewidth=2) ax1.plot(E_LBtime, Ez_array_H[:, 2], '--', color='y', linewidth=2) #ax1.plot(E_LBtime,Ez_array[:,4], color='m', linewidth=2) ax1.plot(E_LBtime, Ez_array_H[:, 5], ':', color='b', linewidth=2) ax1.plot(E_LBtime, Ez_array_H[:, 6], '-.', color='r', linewidth=2) ax2.plot(R_LBtime, Rz_array[:, 1], color='k', linewidth=2) ax2.plot(R_LBtime, Rz_array_H[:, 2], '--', color='y', linewidth=2) #ax2.plot(R_LBtime,Rz_array[:,4], color='m', linewidth=2) ax2.plot(R_LBtime, Rz_array_H[:, 5], ':', color='b', linewidth=2) ax2.plot(R_LBtime, Rz_array_H[:, 6], '-.', color='r', linewidth=2) ax3.plot(A_LBtime, Az_array[:, 1], color='k', linewidth=2) ax3.plot(A_LBtime, Az_array_H[:, 2], '--', color='y', linewidth=2) #ax3.plot(A_LBtime,Az_array[:,4], color='m', linewidth=2) ax3.plot(A_LBtime, Az_array_H[:, 5], ':', color='b', linewidth=2) ax3.plot(A_LBtime, Az_array_H[:, 6], '-.', color='r', linewidth=2) ax4.plot(F_LBtime, Fz_array[:, 1], color='k', linewidth=2) ax4.plot(F_LBtime, Fz_array_H[:, 2], '--', color='y', linewidth=2) #ax4.plot(F_LBtime,Fz_array[:,4], color='m', linewidth=2) ax4.plot(F_LBtime, Fz_array_H[:, 5], ':', color='b', linewidth=2) ax4.plot(F_LBtime, Fz_array_H[:, 6], '-.', color='r', linewidth=2) ax5.plot(Art_LBtime, Artz_array[:, 1], color='k', linewidth=2) ax5.plot(Art_LBtime, Artz_array_H[:, 2], '--', color='y', linewidth=2) #ax5.plot(Art_LBtime,Artz_array[:,4], color='m', linewidth=2) ax5.plot(Art_LBtime, Artz_array_H[:, 5], ':', color='b', linewidth=2) ax5.plot(Art_LBtime, Artz_array_H[:, 6], '-.', color='r', linewidth=2) #ax6.plot(Art_LBtime,Artz_array[:,6]*0, '-.', color='k', linewidth=2) ax2.legend(("Dark", "Stars", "Cold Gas", "Hot Gas"), frameon=False, loc=2) #ax6.legend(("Dark", "Stars", "Cold Gas", "Hot Gas"), frameon=False, loc=2) ax1.text(9.0, 0.27, 'Enzo', size='x-large') ax2.text(9.0, 0.27, 'Ramses', size='x-large') ax3.text(9.0, 0.27, 'Arepo', size='x-large') ax4.text(9.5, 0.27, 'Gizmo-PSPH', size='x-large') ax5.text(9.0, 0.27, 'Art', size='x-large') plt.savefig("ALL_2x2_allspins_vs_time.png", bbox_inches='tight') plt.close() Ecold_ratio = Ez_array_H[:, 5] / Ez_array[:, 1] Rcold_ratio = Rz_array_H[:, 5] / Rz_array[:, 1] Acold_ratio = Az_array_H[:, 5] / Az_array[:, 1] Fcold_ratio = Fz_array_H[:, 5] / Fz_array[:, 1] Artcold_ratio = Artz_array_H[:, 5] / Artz_array[:, 1] Ehot_ratio = Ez_array_H[:, 6] / Ez_array[:, 1] Rhot_ratio = Rz_array_H[:, 6] / Rz_array[:, 1] Ahot_ratio = Az_array_H[:, 6] / Az_array[:, 1] Fhot_ratio = Fz_array_H[:, 6] / Fz_array[:, 1] Arthot_ratio = Artz_array_H[:, 6] / Artz_array[:, 1] #Plot ratio of cold-gas to DM fig = plt.figure() plt.ylabel('$\lambda_{cold}$ / $\lambda_{DM}$') plt.ylim(0, 10) ax1 = fig.add_subplot(111) ax1.set_xlabel('Lookback Time [Gyr]') ax1.set_xlim(LBT_max, LBT_min) ax2 = ax1.twiny() ax2.set_xlabel('Redshift') ax2.set_xlim(LBT_max, LBT_min) ax2.set_xticks(z_tick_locations) ax2.set_xticklabels(z_tick_value_labels) ax1.plot(E_LBtime, Ez_array[:, 5] / Ez_array[:, 1], linewidth=2, color='k') ax1.plot(R_LBtime, Rz_array[:, 5] / Rz_array[:, 1], linewidth=2, color='g') ax1.plot(Art_LBtime, Artz_array[:, 5] / Artz_array[:, 1], linewidth=2, color='b') ax1.plot(A_LBtime, Az_array[:, 5] / Az_array[:, 1], linewidth=2, color='r') ax1.plot(F_LBtime, Fz_array[:, 5] / Fz_array[:, 1], linewidth=2, color='y') ax1.plot(E_LBtime, Ez_array[:, 6] / Ez_array[:, 1], ':', linewidth=2, color='k') ax1.plot(R_LBtime, Rz_array[:, 6] / Rz_array[:, 1], linewidth=2, color='g') ax1.plot(Art_LBtime, Artz_array[:, 6] / Artz_array[:, 1], ':', linewidth=2, color='b') ax1.plot(A_LBtime, Az_array[:, 6] / Az_array[:, 1], ':', linewidth=2, color='r') ax1.plot(F_LBtime, Fz_array[:, 6] / Fz_array[:, 1], ':', linewidth=2, color='y') ax1.plot([14, 0], [1.0, 1.0], linewidth=1, color='k') ax1.legend(("Enzo", "Ramses", "Art", "Arepo", "Fire"), frameon=False) plt.savefig("ALL_spin_ratio_vs_time.png") plt.close() P.rc('font', family='serif', size=15) bins = [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9 ] P.figure() P.xlabel("$j_{\\rm cold}(>0.1R_{\\rm vir})$ / $j_{\\rm DM}$", fontsize=20) P.ylabel("Frequency", fontsize=20) P.xlim(0, 8) n, bins, patches = P.hist( [Ecold_ratio, Artcold_ratio, Rcold_ratio, Acold_ratio, Fcold_ratio], stacked=True, histtype='bar', bins=bins, normed=1) P.legend(["Enzo", "Art", "Ramses", "Arepo", "Fire"], frameon=False) P.tight_layout() P.savefig("ALL_coldspin_histogram.png") P.close() P.figure() P.xlim(0, 8) P.xlabel("$j_{\\rm hot}(>0.1R_{\\rm vir})$ / $j_{\\rm DM}$", fontsize=20) P.ylabel("Frequency", fontsize=20) n, bins, patches = P.hist( [Ehot_ratio, Arthot_ratio, Rhot_ratio, Ahot_ratio, Fhot_ratio], stacked=True, histtype='bar', bins=bins, normed=1) P.legend(["Enzo", "Art", "Ramses", "Arepo", "Fire"], frameon=False) P.tight_layout() P.savefig("ALL_hotspin_histogram.png") P.close() cold = [] cold.extend(Ecold_ratio) cold.extend(Acold_ratio) cold.extend(Rcold_ratio) cold.extend(Fcold_ratio) cold.extend(Artcold_ratio) cold.sort() cold = np.array(cold) print "median, mean, stdev for cold ratios.....", cold[ cold.shape[0] / 2], cold.mean(), cold.std() hot = [] hot.extend(Ehot_ratio) hot.extend(Ahot_ratio) hot.extend(Rhot_ratio) hot.extend(Fhot_ratio) hot.extend(Arthot_ratio) hot.sort() hot = np.array(hot) print "median, mean, stdev for hot ratios......", hot[ hot.shape[0] / 2], hot.mean(), hot.std() #do same kinds of histograms, but just for spin param (not ratio) bins = np.array(range(21)) / 50.0 P.figure() P.xlabel("$\lambda_{cold gas}$") P.ylabel("Number") P.xlim(0, 0.4) n, bins, patches = P.hist([ Ez_array[:, 5], Artz_array[:, 5], Rz_array[:, 5], Az_array[:, 5], Fz_array[:, 5] ], stacked=True, histtype='bar', bins=bins) P.legend(["Enzo", "Art", "Ramses", "Arepo", "Fire"], frameon=False) P.savefig("ALL_coldspinparam_histogram.png") P.close() P.figure() P.xlabel("$\lambda_{hot gas}$") P.ylabel("Number") n, bins, patches = P.hist([ Ez_array[:, 6], Artz_array[:, 6], Rz_array[:, 6], Az_array[:, 6], Fz_array[:, 6] ], stacked=True, histtype='bar', bins=bins) P.legend(["Enzo", "Art", "Ramses", "Arepo", "Fire"], frameon=False) P.savefig("ALL_hotspinparam_histogram.png") P.close()
from pylab import plot, legend, xlabel, ylabel, savefig, show, rc from scipy.stats import gamma from numpy import linspace x = linspace(0, 15, 100) rc('font', size=15) rc('text', usetex=False) plot(x, gamma.pdf(x, 4, 0, 2), 'r*-', label="$\\alpha=4,\\beta=2$") plot(x, gamma.pdf(x, 4, 0, 1), 'bp-', label="$\\alpha=4,\\beta=1$") plot(x, gamma.pdf(x, 4, 0, 0.5), '.k-', label="$\\alpha=4,\\beta=0.5$") plot(x, gamma.pdf(x, 2, 0, 0.5), '>g-', label="$\\alpha=2,\\beta=0.5$") legend() xlabel("$x$") ylabel('$f(x)$') savefig('figure2_46.png', dpi=500) show()
import numpy as np import networkx as nx import pandas as pd import pylab as plt a = pd.read_excel("Pdata10_14.xlsx", header=None) b = a.values b[np.isnan(b)] = 0 c = np.zeros((8, 8)) # 邻接矩阵初始化 c[0:7, 1:8] = b # 构造图的邻接矩阵 G = nx.Graph(c) T = nx.minimum_spanning_tree(G) # 返回可迭代对象 d = nx.to_numpy_matrix(T) # 返回最小生成树的邻接矩阵 print("邻接矩阵c=\n", d) W = d.sum() / 2 + 5 # 求油管长度 print("油管长度W=", W) s = dict(zip(range(8), range(1, 9))) # 构造用于顶点标注的标号字典 plt.rc('font', size=16) pos = nx.shell_layout(G) nx.draw(T, pos, node_size=280, labels=s, node_color='r') w = nx.get_edge_attributes(T, 'weight') nx.draw_networkx_edge_labels(T, pos, edge_labels=w) plt.savefig('figure10_14.png') plt.show()
#程序文件Pex4_2.py from scipy.stats import norm from pylab import plot, fill_between, show, text, savefig, rc from numpy import array, linspace, zeros alpha = array([0.001, 0.005, 0.01, 0.025, 0.05, 0.10]) za = norm.ppf(1 - alpha, 0, 1) #求上alpha分位数 print("上alpha分位数分别为", za) x = linspace(-4, 4, 100) y = norm.pdf(x, 0, 1) rc('font', size=16) rc('text', usetex=True) plot(x, y) #画标准正态分布密度曲线 x2 = linspace(za[-1], 4, 100) y2 = norm.pdf(x2) y1 = [0] * len(x2) fill_between(x2, y1, y2, color='r') #y1,y2对应的点之间填充 plot([-4, 4], [0, 0]) #画水平线 text(1.9, 0.07, "$\\leftarrow\\alpha$=0.1") #标注 savefig("figure4_2.png", dpi=500) show()
def main(): print("I have entered main") #progname = os.path.basename(sys.argv[0]) #usage = """Aligns a 3d volume to another by executing e2spt_classaverage.py and then calculates the FSC between them by calling e2proc3d.py . It returns both a number for the resolution based on the FSC0.5 #criterion(on the screen) and a plot as an image in .png format.""" progname = os.path.basename(sys.argv[0]) usage = """Aligns a 3d volume to another by executing e2spt_classaverage.py and then calculates the FSC between them by calling e2proc3d.py . It returns both a number for the resolution based on the FSC0.5 criterion(on the screen) and a plot as an image in .png format.""" parser = EMArgumentParser(usage=usage, version=EMANVERSION) parser.add_argument( "--averager", type=str, default="mean.tomo", help= """Default=mean.tomo. The type of averager used to produce the class average. Default=mean.tomo.""" ) parser.add_argument( "--cutoff", type=float, default=None, help= """Fraction of particles (as a decimal, where 1.0 is the entire set, 0.8 is 80 percent. 0.5 is 50 percent, etc); where to make the cutoff to divide the set into two groups. For example, if you specify --cutoff=0.2, the 20 percent of particles with the highest correlation scores will be bundled into the first group, and the remaining 80 percent into the second group.""" ) parser.add_argument( "--groups", type=int, default=None, help= "Number of groups you want the data to be divided into based on correlation." ) parser.add_argument( "--input", type=str, default='', help= """Default=None. Stack of particles aligned with e2spt_classaverage.p.y""" ) parser.add_argument( "--lowpass", type=str, default='default', help= """Filter applied to averages when --groups is used. Default=filter.lowpass.gauss:cutoff_freq=1/F, where F is Nyquist frequency, automatically calculated. To disable type --lowpass=None""" ) parser.add_argument( "--nbins", type=int, default=0, help= """Used for histogram plot. Default=0 (not used). Number of bins for histogram. If not provided, the optimal bin number will be automatically calculated based on bin-width, computed using Scott's normal reference rule, width = (3.5*std)/cuberoot(n), where 'std' is the standard deviation of the distribution of scores and n is the number of values considered. Then, bins will be nbins = (max(scores) - min(scores)) / width.""" ) parser.add_argument( "--normalizeplot", action="store_true", help= "Make maximum correlation value on plot equal to 1 and the minimum equal to 0, and scale all other values accordingly.", default=False) parser.add_argument( "--path", type=str, default='spt_group', help= "Results directory. If not specified, defaults to a numbered series starting with spt_group_01." ) parser.add_argument( "--ppid", type=int, default=-1, help="Set the PID of the parent process, used for cross platform PPID") parser.add_argument( "--scores", type=str, default='', help= """Default=None. Text file containing a list of correlations scores.""" ) parser.add_argument( "--sigmaprune", type=float, default=0.0, help= """Number of standard deviations below the mean to cut off particles; that is, the mean cross correlation coefficient of all particles will be computed, and those that are --sigmaprune=N standard deviations below the mean will not be considered.""" ) parser.add_argument( "--topn", type=int, default=None, help= "Number of particles from best to worst that you want to be written as a substack, averaged, and generate a coordinates .txt file with their coordinates." ) parser.add_argument( "--verbose", "-v", dest="verbose", action="store", metavar="n", type=int, default=0, help= "verbose level [0-9], higner number means higher level of verboseness") (options, args) = parser.parse_args() options = checkinput(options) if options.groups and (options.cutoff or options.topn): print( "ERROR: you cannot specify --cutoff, --groups and --topn all at the same time. Choose one." ) sys.exit() if options.cutoff and (options.groups or options.topn): print( "ERROR: you cannot specify --cutoff, --groups and --topn all at the same time. Choose one." ) sys.exit() if options.topn and (options.cutoff or options.groups): print( "ERROR: you cannot specify --cutoff, --groups and --topn all at the same time. Choose one." ) sys.exit() logger = E2init(sys.argv, options.ppid) options = makepath(options, 'sptcoeff') print("\nI have read the parameters") if options.lowpass: if options.lowpass == "default": hdr = EMData(options.alistack, 0, True) apix = hdr['apix_x'] nyquist = 2.0 * apix nyquistfreq = 1.0 / nyquist options.lowpass = '******' + str( nyquistfreq) + ':apix=' + str(apix) if apix == '1.0': print( "\nWARNING: apix is 1.0, most likely wrong (default empty value). You can fix/change it with e2fixheaderparam.py" ) elif 'None' in options.lowpass or 'none' in options.lowpass: options.lowpass = None if options.averager: options.align = parsemodopt(options.averager) if options.lowpass: options.lowpass = parsemodopt(options.lowpass) scores = [] dataset = {} #x=[] k = 0 usenewstack = False newstack = options.alistack.replace('.hdf', '_prepruned.hdf') if options.alistack: n = EMUtil.get_image_count(options.alistack) for i in range(n): ahdr = EMData(options.alistack, i, True) #print "ahdr is", ahdr score = None if options.verbose: print("\nanalyzing header for particle", i) if 'spt_score' in ahdr.get_attr_dict(): score = -1 * ahdr['spt_score'] print("spt_score is", score) elif 'spt_coefficient' in ahdr.get_attr_dict(): score = ahdr['spt_coefficient'] * -1 print("spt_coefficient is", score) else: print( "\nERROR: no score found in header for particle %d. Skipping it! A prepruned stack will be made with all the particles that did have score info in their header" % (i)) a = EMData(options.alistack, i) a.write_image(newstack, k) usenewstack = True if score: scores.append(score) elif options.scores and not options.alistack: f = open(options.scores, 'r') lines = f.readlines() f.close() scores = [round(float(line.replace('\n', '')), 6) for line in lines] elif not options.scores and not options.alistack: print( "\n(e2spt_coeffplot)(main) ERROR: you need to supply either --alistack or --scores, with the former taking precedence over the latter." ) sys.exit() if usenewstack: options.alistack = newstack n = len(scores) nstack = EMUtil.get_image_count(options.alistack) if n != nstack: print( "\n!!!! WARNING: the number of scores %d does not match the number of images %d in the stack %s" % (n, nstack, options.alistack)) if scores and n > 1: print("\nThe set has these many particles", len(scores)) else: print( "\nERROR: There seems to be no information on particle scores. The number of scores must be larger than one to do any statistics with them." ) sys.exit() for i in range(n): #dataset.append({'score':float(score), 'particle':a}) dataset.update({i: scores[i]}) #x.append(i) dataset = sorted(dataset.items(), key=itemgetter(1), reverse=True) if options.normalizeplot: #for s in scores: #val = values[ele] minv1 = min(scores) #maxv1 = max(scores) #print "Min max before normalization was", minv,maxv1 for k in range(len(scores)): scores[k] = scores[k] - minv1 #minv2 = min(scores) maxv2 = max(scores) #print "After subtracting min, the are", minv2,maxv #print "Max before normalization was", maxv for k in range(len(scores)): scores[k] = scores[k] / maxv2 scores.sort() scores.reverse() #scores=scores[:-2] ''' c:plot the distribution of scores ''' import matplotlib.pyplot as plt import pylab import matplotlib std = np.std(scores) mean = np.mean(scores) statistics = ['mean=' + str(mean) + ' std=' + str(std) + '\n'] print("\nthe standard deviation %.6f, mean %.6f" % (std, mean)) if not std: print("\nERROR: std=0, which means all intensity values are the same.") sys.exit() cuberoot = np.power(len(scores), 1.0 / 3.0) width = (3.5 * std) / cuberoot print( "\naccording to Scott's normal reference rule, width = (3.5*std)/cuberoot(n), the width of the histogram bins will be", width) calcbins = (max(scores) - min(scores)) / width if options.nbins: calcbins = options.nbins print("\overwriting number of bins to be", options.nbins) print( "\nand the number of bins n = ( max(scores) - min(scores) ) / width will thus be", calcbins) calcbins = int(round(calcbins)) print("rounding to", calcbins) statistics.append('bins=' + str(calcbins) + ' , binwidth=' + str(width) + '\n') if not calcbins: print( "WARNING: nbins=0, which means max and min intensity are the same, which probably means all scores are zero. Defaulting nbins to number of partilces." ) calcbins = len(scores) datafile = '' if options.alistack: datafile = os.path.basename(options.alistack) elif options.scores: datafile = os.path.basename(options.scores) f = open(options.path + '/stats.txt', 'w') f.writelines(statistics) f.close() plt.hist(scores, calcbins, alpha=0.30, label=datafile) plottitle = os.path.basename(options.alistack).replace( '.hdf', '') + ' CC scores distribution histogram' plt.title(plottitle) matplotlib.rc('xtick', labelsize=16) matplotlib.rc('ytick', labelsize=16) font = {'weight': 'bold', 'size': 16} matplotlib.rc('font', **font) pylab.rc("axes", linewidth=2.0) pylab.xlabel('CC score (au)', fontsize=16, fontweight='bold') pylab.ylabel('Number of particles', fontsize=16, fontweight='bold') plt.savefig(options.path + '/scores_histogram.png') plt.clf() ''' c:plot decay in ranked correlation scores ''' x = [i for i in range(len(scores))] plt.plot(x, scores, color='k', linewidth=3) plottitle = os.path.basename(options.alistack).replace( '.hdf', '') + ' CC scores decay' plt.title(plottitle) pylab.xlim([min(x), max(x) + 0.1 * max(x)]) font = {'weight': 'bold', 'size': 16} matplotlib.rc('font', **font) pylab.xlabel('Particle index', fontsize=16, fontweight='bold') pylab.ylabel('CC score (au)', fontsize=16, fontweight='bold') plt.savefig(options.path + '/scores_decay.png') plt.clf() ''' c:plot the distance to mean value in # of standard deviations ''' distancestomean = [(scores[i] - mean) / std for i in range(len(scores))] plt.plot( x, distancestomean, color='b', linewidth=2, ) plottitle = os.path.basename(options.alistack).replace( '.hdf', '') + ' CC scores distance to mean' plt.title(plottitle) pylab.xlim([min(x), max(x) + 0.1 * max(x)]) font = {'weight': 'bold', 'size': 16} matplotlib.rc('font', **font) pylab.xlabel('Particle index', fontsize=16, fontweight='bold') pylab.ylabel('Distance from mean (sigmas)', fontsize=16, fontweight='bold') plt.savefig(options.path + '/scores_distance2mean.png') plt.clf() ''' c:plot the distance to max value in # of standard deviations ''' maxi = max(scores) distancestomax = [(maxi - scores[i]) / std for i in range(len(scores))] plt.plot( x, distancestomax, color='b', linewidth=2, ) plottitle = os.path.basename(options.alistack).replace( '.hdf', '') + ' CC scores distance to max' plt.title(plottitle) pylab.xlim([min(x), max(x) + 0.1 * max(x)]) font = {'weight': 'bold', 'size': 16} matplotlib.rc('font', **font) pylab.xlabel('Particle index', fontsize=16, fontweight='bold') pylab.ylabel('Distance from max (sigmas)', fontsize=16, fontweight='bold') plt.savefig(options.path + '/scores_distance2max.png') plt.clf() print('sorted dataset is', dataset) ''' c:prune and/or divide into groups ''' newscores = [] if options.sigmaprune: #print "Will analyze scores to remove aberrantly low ones" #mu=numpy.mean(scores) #sigma=numpy.std(scores) mu = mean sigma = std print("\nMean is", mu) print("Std is", sigma) filter = mu - sigma * (options.sigmaprune) print("Therefore, filter is", filter) for d in dataset: if float(d['score']) < float(filter): dataset.remove(d) print( "I have removed this aberrant particle from the dataset due to its low score", d['score']) else: newscores.append(float(d['score'])) print("This score is high enough to survive", d['score']) newscores.sort() newscores.reverse() x = range(len(newscores)) plottitle = os.path.basename(options.alistack).replace( '.hdf', '_prunedSCORES') plt.plot(x, newscores, color='k', linewidth=2) pylab.xlim([min(x), max(x) + 0.1 * max(x)]) #plt.title(plottitle) plt.ylabel('CC coefficient') plt.xlabel('Particle number') #a = plt.gca() plotfile = options.path + '/' + plottitle + '.png' plt.savefig(plotfile) plt.clf() newN = len(dataset) if options.groups: if not options.alistack: print("\nERROR: --groups requires --alistack") sys.exit() halfptclnum = int(round(n / 2.0)) halfptcl = dataset[halfptclnum][0] print("half ptclnum is", halfptclnum) print("which happens to be ptcl indx", halfptcl) halfscore = dataset[halfptcl][1] print("with halfscore being", halfscore) subdatasetsSET = [] N = len(dataset) #print "THe type of dataset is", type(dataset) print("The len of the dataset is", N) subN = N / options.groups print("The len of each subset, except the last, should be", subN) for g in range(options.groups): #subscores = scores[g*subN : (g+1)*subN] subdataset = dataset[g * subN:(g + 1) * subN] if g == options.groups - 1: subdataset = dataset[g * subN:] subdatasetsSET.append(subdataset) kk = 0 for subdataset in subdatasetsSET: print("The len of subset %d is %d" % (kk, len(subdataset))) jj = 0 groupname = options.path + '/' + os.path.basename( options.alistack).replace( '.', '_' + str(kk).zfill(len(str(options.groups))) + '.') particleLIST = [] lines = [] print("options.averager is", options.averager) #avgr = Averagers.get(options.averager[0], options.averager[1]) avgr = Averagers.get('mean.tomo') for element in subdataset: #particle = element['particle'] ptclnum = element[0] img = EMData(options.alistack, ptclnum) img.write_image(groupname, jj) jj += 1 particleLIST.append(ptclnum) avgr.add_image(img) averageNAME = groupname.replace('.hdf', '_AVG.hdf') average = avgr.finish() average['origin_x'] = 0 average['origin_y'] = 0 average['origin_z'] = 0 average.process_inplace('normalize.edgemean') if options.lowpass: print("(e2spt_coeffplot)(main) --lowpass provided:", options.lowpass) average.process_inplace(options.lowpass[0], options.lowpass[1]) average.write_image(averageNAME, 0) cmd = 'cd ' + options.path + ' && e2orthoproject.py --input ' + os.path.basename( averageNAME) cmd += ' && e2slicer.py --input ' + os.path.basename(averageNAME) runcmd(options, cmd) #print "\nThe group average has been written to", averageNAME kk += 1 if options.cutoff: if not options.alistack: print("\nERROR: --cutoff requires --alistack") sys.exit() threshptclnum = int(round(newN / (1 / options.cutoff))) threshptcl = dataset[threshptclnum] print("The new threshptcl is", threshptcl) threshscore = dataset[threshptclnum][1] threshlabel = "%0.2f" % (options.cutoff) threshlabel = str(threshlabel).replace('.', 'p') group1 = [] group2 = [] k1 = 0 k2 = 0 g1name = options.path + '/' + os.path.basename( options.alistack).replace('.', '_G1.') g2name = options.path + '/' + os.path.basename( options.alistack).replace('.', '_G2.') avgr1 = avgr = Averagers.get('mean.tomo') avgr2 = avgr = Averagers.get('mean.tomo') for i in dataset: img = EMData(options.alistack, i[0]) if i[1] >= threshscore: group1.append(i[0]) img.write_image(g1name, k1) avgr1.add_image(img) k1 += 1 else: group2.append(i[0]) img.write_image(g2name, k2) avgr2.add_image(img) k2 += 1 #else: # print "\n\n@@@@ Found a garbage particle, and thus did not consider it!\n\n" if group1: g1avg = avgr1.finish() g1avg.write_image(g1name.replace('.hdf', '_avg.hdf'), 0) if group2: g2avg = avgr1.finish() g2avg.write_image(g2name.replace('.hdf', '_avg.hdf'), 0) if options.topn: if not options.alistack: print("\nERROR: --topn requires --alistack") sys.exit() topndataset = dataset[0:options.topn] bottomdataset = dataset[options.topn:] outnamestack = options.path + '/' + os.path.basename( options.alistack).replace('.', 'top' + str(options.topn) + '.') coordsname = options.path + '/' + os.path.basename(outnamestack).split( '.')[0] + '_coords.txt' indxsname = options.path + '/' + os.path.basename(coordsname).replace( 'coords', 'indxs') k = 0 linescoords = [] linesindxs = [] topptcls = [] avgr = Averagers.get('mean.tomo') for ptcl in topndataset: p = ptcl[0] img = EMData(options.alistack, p) img.write_image(outnamestack, k) avgr.add_imag(img) topptcls.append(p) linescoords.append(str(p['ptcl_source_coord']) + '\n') linesindxs.append(str(p['source_n']) + '\n') k += 1 avg = avgr.finish() avgname = options.path + '/' + os.path.basename(outnamestack).replace( '.', '_avg.') avg.write_image(avgname, 0) f = open(coordsname, 'w') f.writelines(linescoords) f.close() f = open(indxsname, 'w') f.writelines(linesindxs) f.close() #x=range(len(scores)) #plottitle = os.path.basename(options.alistack).replace('.hdf', '_prunedSCORES') #plt.plot(x, scores, marker='o', color='r', linewidth=2) #plt.title(plottitle) #plt.ylabel('score') #plt.xlabel('n ptcl') #a = plt.gca() #plotfile = options.path + '/' + plottitle + '.png' #plt.savefig(plotfile) #plt.clf() E2end(logger) return
#-*- coding: utf-8 -*- import pylab import numpy as np import math from numpy import arctan pylab.rc('text', usetex=True) pylab.rc('font', family='serif') omega = np.logspace(0, 7, num=4000) C = 5e-8 R = 13000 O = omega * R * C # phi=abs((1-4*O**2)/np.sqrt((1-4*O**2)**2+64*O**2)) phi = arctan(8 * O / (1 - 4 * O**2)) # fi = np.loadtxt('data/chem6.tsv', skiprows=1, delimiter='\t', usecols=(0, 6)) # print(fi) # Результаты эксперимента # om=fi.T[0]*2*np.pi # fi=fi.T[1] pylab.semilogx(omega, phi, "r-") # pylab.semilogx(om, fi, "bo", markersize=5) pylab.xlabel(r"$\omega$") pylab.ylabel(r"$\phi$")
def __init__(self): self.prediction = [] self.true_value = [] self.score = [] data = Data() data.prediction = np.array(data_frame[:, 0]) data.true_value = np.array(data_frame[:, 1]) data.error = data.prediction - data.true_value font = {'size': 18} pylab.rc('font', **font) pylab.figure(1) pylab.hist(data.error, bins=1000) var = np.var(data.error) print "Var: ", var data.error = [n / (var**(0.5)) for n in data.error] pylab.savefig(filename + "_prediction_error_hist.pdf", box_inches='tight') pylab.figure(2) stats.probplot(data.error, dist="norm", plot=pylab) pylab.savefig(filename + "_prediction_error_hist.pdf", box_inches='tight') from scipy import stats
plt.rcParams['xtick.major.width'] = 1 plt.rcParams['xtick.minor.size'] = 5 plt.rcParams['xtick.minor.width'] = 1 plt.rcParams['ytick.major.size'] = 9 plt.rcParams['ytick.major.width'] = 1 plt.rcParams['ytick.minor.size'] = 5 plt.rcParams['ytick.minor.width'] = 1 font = {'family': 'serif', 'weight': 'normal', 'size': 15} plt.rc('font', **font) #===============FIRST PLOT++++++++++++++++++++++++++++++++++++++++++++++ #Load Data========================================================== data = np.loadtxt('../TimeSeries.dat', comments='#') t = data[:, 0] Re = data[:, 1] Nu = data[:, 2] Div = data[:, 3] dt = data[:, 4] #Define Figure and subplots========================================= fig = plt.figure(1) f_ax1 = fig.add_subplot(2, 2, 1) f_ax2 = fig.add_subplot(2, 2, 2)
def main(): matlab_path = '/Applications/MATLAB_R2020b.app/bin/matlab' ## THE FOLLOWING FLAGS TO TRUE/FALSE CONTROL WHICH TESTS ARE DONE do_ibp_tests = True do_maaipm_tests = True do_colgen_tests = True do_comparison_plots = True # Control which data set to use: 'sparsesquare', 'sparsedisc', 'noisygrid', 'translatedgrid' test_type = 'sparsesquare' k_vals = [10] n_vals = [10] # Control parameters for IBP vs. MAAIPM method. ibp_eps_vals = [0.1, 0.01] # Support size gridding for IBP. ibp_filter_sizes = [1] # the regularization parameter is eta = (1/eps)^2 / (2 * filter_size^2) maaipm_eps_inv_vals = [10] # eps_inv x eps_inv grid for MAAIPM. # Plot parameters y_limits = (1e-10 - 1e-11, 10.0) x_limits = (0.009,100) for k in k_vals: for n in n_vals: np.random.seed(19238412) random.seed(12834981) data_identifier = test_type + '_n' + str(n) + 'k' + str(k) ########################################################################## ## A) Generate the sparse test data if test_type == 'sparsesquare': sparse_data = get_sparsesquare_test(k=k,n=n) elif test_type == 'sparsedisc': sparse_data = get_sparsedisc_test(k=k,n=n) elif test_type == 'noisygrid': sparse_data = get_noisygrid_test(k=k,n=n) elif test_type == 'translatedgrid': sparse_data = get_translatedgrid_test(k=k,n=n) else: assert(0) # show_sparse_data(sparse_data) # And save it for use by IBP and MAAIPM data_filename = 'comparison_test_files/experiment_data/' + data_identifier # Save it as gridded images (for IBP): eps_vals = ibp_eps_vals for i, eps_val in enumerate(eps_vals): dense_data = get_dense_data_representation(sparse_data, eps=eps_val, bounding_box=((-1,-1),(1,1))) eps_desc = str(eps_val).split('.')[1] dense_data_filename = data_filename + 'eps' + eps_desc save_dense_data_representation(dense_data, dense_data_filename) # Save it as a list of points (for MAAIPM): save_sparse_data_representation(sparse_data, data_filename) ########################################################################## # B) Run our column generation method, collecting an error plot. if do_colgen_tests: filename_colgen = 'comparison_test_files/experiment_results/colgen_results/' + data_identifier + '.txt'; if os.path.exists(filename_colgen): print('ALREADY EXISTS: ' + filename_colgen) else: bary_prob = BarycenterProblem(sparse_data, method='powerdiagram', log_file=filename_colgen) bary_prob.solve() ######################################################################## ## C) Run IBP method, gridding the space. ## 1) Run the IBP code (MATLAB) computing regularized barycenters (different regularizations and epsilons). if do_ibp_tests: eps_vals = ibp_eps_vals filter_sizes = ibp_filter_sizes # the regularization parameter is eta = (1/eps)^2 / (2 * filter_size^2) for i, eps_val in enumerate(eps_vals): eps_desc = str(eps_val).split('.')[1] for filter_size in filter_sizes: ibp_res_prefix = 'comparison_test_files/experiment_results/ibp_results/' + data_identifier + 'eps' + eps_desc + 'f' + str(filter_size); ibp_res_file = ibp_res_prefix + '.txt' if os.path.exists(ibp_res_file): print('ALREADY EXISTS: ' + ibp_res_file) continue time.sleep(0.05) matlab_command = matlab_path + ' -nodisplay -r "cd(\'comparison_test_files/IBP_code\'); test_barycenters_ibp(' + str(n) + ', ' + str(k) + ',\'' + eps_desc + '\',\'' + test_type + '\',' + str(filter_size) + ',{imgortime:d});exit"'; ## i) Assign scores to the IBP barycenters w.r.t. actual point cloud. ## Use the Lemon solver to obtain the exact optimal transport distance between pairs of points. costs_filename = ibp_res_prefix + '_costs.txt' costs_lemon = [] if not os.path.exists(costs_filename): print(matlab_command) os.system(matlab_command.format(imgortime=1)) # Write the barycenter at each iteration to the disk. t = 1 while True: curr_filename = ibp_res_prefix + '/b_' + str(t) + '.png' if not os.path.exists(curr_filename): break if t % 40 == 1 or (t < 80 and t % 10 == 1): f1 = pylab.imread(curr_filename) print(f1) f1 = f1 / np.sum(f1) sparsedatum_f1 = get_sparse_data_representation([f1], bounding_box=((-1,-1),(1,1)))[0] print(t) curr_lemon = 0 for i in range(k): print(t,i) curr_lemon += exact_cost_lemon(sparse_data[i], sparsedatum_f1, lemon_solver_location='../lemon_solver/LemonNetworkSimplex') / k costs_lemon.append(curr_lemon) else: costs_lemon.append(-1) t += 1 costs_file = open(costs_filename, 'w') for i in range(len(costs_lemon)): costs_file.write('{v:.9f}\n'.format(v=costs_lemon[i])) costs_file.close() else: print('USING PREVIOUSLY-COMPUTED COSTS FILE: ' + costs_filename) costs_file = open(costs_filename, 'r') costs_lemon = costs_file.readlines() costs_lemon = [float(i) for i in costs_lemon] costs_file.close() # ii) Separately re-run the IBP code to get the timing information. timings_filename = ibp_res_prefix + "_timing.txt"; if not os.path.exists(timings_filename): os.system(matlab_command.format(imgortime=2)) # Write the cumulative time to each iteration to the disk. else: print('USING PREVIOUSLY-COMPUTED TIMINGS FILE: ' + timings_filename) timings_file = open(timings_filename, 'r') times = timings_file.readlines() times = [float(i) for i in times] timings_file.close() res_file = open(ibp_res_file, 'w') assert(len(times) == len(costs_lemon)) for i in range(len(times)): res_file.write('{v:.9f} {t:.9f}\n'.format(v=costs_lemon[i], t=times[i])); res_file.close() ########################################################################## ## D) Run MAAIPM method with fixed-support assumption. if do_maaipm_tests: for eps_inv in maaipm_eps_inv_vals: # eps_inv x eps_inv-size grid maaipm_res_file = 'comparison_test_files/experiment_results/ipm_results/' + data_identifier + 'epsinv' + str(eps_inv) + '.txt' if os.path.exists(maaipm_res_file): print('ALREADY EXISTS: ' + maaipm_res_file) continue time.sleep(0.05) matlab_command = matlab_path + ' -nodisplay -r "cd(\'comparison_test_files/MAAIPM_code\'); test_barycenters_maaipm_grid_support(' + str(n) + ', ' + str(k) + ',' + str(eps_inv) + ',\'' + test_type + '\');exit"'; os.system(matlab_command) os.system('mv comparison_test_files/experiment_results/ipm_results/latest_maaipm_results.txt ' + maaipm_res_file) ############################################################################## ## E) Create the comparison plots between the algorithms if do_comparison_plots: print('Comparison plots here') markers = ['x', 'o', 'v', '^', '.']*100 best_val = 10; last_time = 0; title_text = ''; legend_names = None; # Which data to plot. # Example comparison plot: n = 5, k = 5, sparsesquare. series = ['colgen_results/sparsesquare_n5k5.txt', 'ipm_results/sparsesquare_n5k5epsinv10.txt', 'ibp_results/sparsesquare_n5k5eps1f1.txt'] legend_names = ['Proposed algorithm', 'MAAIPM 10x10 grid', 'IBP 10 x 10 grid, filter-size 1'] assert(len(k_vals) == 1) assert(len(n_vals) == 1) k = k_vals[0] n = n_vals[0] test_prefix = test_type + '_n' + str(n) + 'k' + str(k); title_text = test_type + ', n = ' + str(n) + ', k = ' + str(k) series = [] legend_names = [] if do_colgen_tests: series.append('colgen_results/' + test_prefix + '.txt') legend_names.append('Proposed algorithm') if do_ibp_tests: for eps_val in ibp_eps_vals: eps_desc = str(eps_val).split('.')[1] for filter_size in ibp_filter_sizes: series.append('ibp_results/' + test_prefix + 'eps' + eps_desc + 'f' + str(filter_size) + '.txt') legend_names.append('IBP eps=' + str(eps_val) + ' filter-size=' + str(filter_size)) if do_maaipm_tests: for eps_inv in maaipm_eps_inv_vals: series.append('ipm_results/' + test_prefix + 'epsinv' + str(eps_inv) + '.txt') legend_names.append('IPM epsinv=' + str(eps_inv)) # series = ['colgen_results/sparsesquare_n25k10.txt', 'ibp_results/sparsesquare_n25k10eps1f1.txt', 'ibp_results/sparsesquare_n25k10eps1f2.txt', 'ibp_results/sparsesquare_n25k10eps05f1.txt', 'ibp_results/sparsesquare_n25k10eps05f2.txt'] # series = ['colgen_results/sparsesquare_n25k10.txt', 'ipm_results/sparsesquare_n25k10epsinv10.txt', 'ipm_results/sparsesquare_n25k10epsinv30.txt', 'ipm_results/sparsesquare_n25k10epsinv70.txt'] ## # IPM plot: n = 20, k = 20, noisygrid. ## series = ['colgen_results/noisygrid_n20k10.txt', 'ipm_results/noisygrid_n20k10epsinv10.txt', 'ipm_results/noisygrid_n20k10epsinv30.txt', 'ipm_results/noisygrid_n20k10epsinv50.txt'] ## # IBP plot: n = 20, k = 20, sparsesquare. ## series = ['colgen_results/sparsesquare_n20k10.txt', 'ibp_results/sparsesquare_n20k10eps04f1.06.txt', 'ibp_results/sparsesquare_n20k10eps01f7.txt', 'ibp_results/sparsesquare_n20k10eps004f12.txt'] ## legend_names = ['Proposed algorithm', 'IBP 25x25 grid, $\eta=100$', 'IBP 100x100 grid, $\eta=100$', 'IBP 250x250 grid, $\eta=200$'] ## title_text = 'Comparison with IBP' # # IPM plot: n = 20, k = 20, sparsesquare. # series = ['colgen_results/sparsesquare_n20k10.txt', 'ipm_results/sparsesquare_n20k10epsinv10.txt', 'ipm_results/sparsesquare_n20k10epsinv40.txt', 'ipm_results/sparsesquare_n20k10epsinv70.txt'] # legend_names = ['Proposed algorithm', 'MAAIPM 10x10 grid', 'MAAIPM 40x40 grid', 'MAAIPM 70x70 grid'] # title_text = 'Comparison with MAAIPM' # # Comparison plot: n = 5, k = 5, sparsesquare. # series = ['colgen_results/sparsesquare_n5k5.txt', 'ipm_results/sparsesquare_n5k5epsinv10.txt', 'ibp_results/sparsesquare_n5k5eps1f1.txt'] # legend_names = ['Proposed algorithm', 'MAAIPM 10x10 grid', 'IBP 10 x 10 grid, filter-size 1'] # title_text = 'Example comparison with MAAIPM and IBP' if legend_names is None: legend_names = series val_data = [] time_data = [] # Sanitize data (remove places where cost was not computed.) for i in range(len(series)): temp_vals, temp_times = read_val_time_file('comparison_test_files/experiment_results/' + series[i]) vals = [] times = [] for j in range(len(temp_vals)): if temp_vals[j] >= 0: vals.append(temp_vals[j]) times.append(temp_times[j]) val_data.append(vals) time_data.append(times) for i, serie in enumerate(series): vals = val_data[i] times = time_data[i] best_val = min(best_val, np.min(vals)) # Provided that column generation is one of the methods run, best_val will be the true optimum for the problem. last_time = max(last_time, np.max(times)) pylab.figure(figsize=(12,8)) for i, serie in enumerate(series): vals = val_data[i] times = time_data[i] vals.append(vals[-1]) times.append(last_time) pylab.plot(times, vals-best_val + 1e-18, marker=markers[i], label=legend_names[i], linewidth=3.0) pylab.ylim(y_limits) pylab.xlim(x_limits) pylab.yscale('log') pylab.xscale('log') font = {'family' : 'sans-serif', 'size' : 30} pylab.rc('font', **font) pylab.rcParams['text.usetex'] = True ax = pylab.gca(); ax.set_ylabel('Suboptimality gap') ax.set_xlabel('Time (seconds)') ax.set_title(title_text) for item in ([ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(30) item.set_usetex(True) ax.title.set_fontsize(40) ax.title.set_usetex(True) pylab.legend() pylab.show()
# Floor, Boston, MA 02110-1301, USA. # # # Code: from collections import defaultdict import numpy as np import pylab as plt from util import makepath from traubdata import TraubData from trbhist import get_spiketime_hist from util import makepath, smooth_gaussian from config import cellcolor from plotutil import plot_spike_raster plt.rc('font', size=12) plt.rc('figure', figsize=(3, 4)) data_filename = 'data_20140526_110240_20695.h5' #'data_20140525_130513_20265.h5' #'data_20140825_113047_105368_compute-0-2.local.h5' if __name__ == '__main__': figfilename = 'figures/Figure_5B_spikeraster_80DB.png' trange = (5, 8) fig, ax = plot_spike_raster(makepath(data_filename), trange) plt.savefig(figfilename, transparent=True) plt.show() # # fig_5_b_spikeraster_30DB.py ends here
#!/usr/bin/env python import pandas import pylab as pl import numpy as np import seaborn as sns from argparse import ArgumentParser from arsenal import colors from arsenal.math.pareto import show_frontier from ldp.viz.cone import lambda_cone, arrow sns.set(font_scale=2.1) pl.rc('text', usetex=True) pl.rc('font', family="Times New Roman") # Light colors for trajectory and cone. c_baseline = '#a1d76a' c_lols = '#9e4c79' # darker color for vector and distinguished point c_vec_lols = '#862058' c_vec_baseline = '#558524' # TODO: use consistent color for each method. c_cp = '#6a6bd7' c_dp = '#d76ad7' def main(): p = ArgumentParser() p.add_argument('filename', help='Path to csv file containing the results.')
import pylab import numpy from scipy.optimize import curve_fit import matplotlib import math n, V, dV = pylab.loadtxt( 'C:\\Users\\Lisa\\Desktop\\Lab3\\Lock-In\\dati2marco.txt', unpack=True) y = V dy = dV pylab.figure(1) pylab.rc('font', size=13) pylab.title('Assorbimento', fontsize="16") pylab.xlabel('numero lastrine', size="14") pylab.ylabel('$V_{out}$ [$V$]', size="14") pylab.xlim(-0.5, 7.5) pylab.grid(color="gray") pylab.plot(n, y, '.', label='data', color='black') pylab.errorbar(n, y, dy, linestyle='', marker='+', color='black') def exponential(x, v0, n0): return v0 * pylab.exp(-x / n0)
outbase = './balff_data/dataarraySim_pdist'+args.probdist+\ '_Ntot'+str(args.Ntotal)+\ '_k'+str(args.kval).replace('.','p')+\ '_Lstar'+str(args.Lstar).replace('.','p')+\ '_Llim'+str(args.Llim).replace('.','p')+\ '_Nobj'+str(Nobj) if args.nochoice: outbase = outbase + ncstring outputfile = outbase + '.fits' #------------------------------------------------------------------------------------------------------------- plotsamples = 1 if args.nochoice: plotsamples = 0 # ignore plotting when nochoice is set if plotsamples == 1: plotname = outbase + '.pdf' import pylab as plt Fsize = 10 plt.rc('text', usetex=True) # enabling LaTex rendering of text plt.rc('font', family='serif', size=Fsize) # setting text font plt.rc('xtick', labelsize=Fsize) plt.rc('ytick', labelsize=Fsize) hist = plt.hist(np.log10(Ldraw_total), bins=100, color='0.3', label='Total sample drawn from dist: k=' + str(args.kval) + ', L*/[1e44erg/s]=' + str(args.Lstar)) plt.hist(np.log10(Ldraw_toterr), bins=hist[1], color='b', alpha=0.5, label='Total sample added measurement errors') plt.hist(np.log10(Ldraw), bins=hist[1],
from __future__ import print_function import pylab as pl import os import json import sys try: import urllib2 as urllib except ImportError: import urllib.request as urllib pl.rc('font', size=15) #MTA_KEY = "d3ccd72f-edb3-433f-8935-1ec3de863290" url = 'http://bustime.mta.info/api/siri/vehicle-monitoring.json?key='\ +sys.argv[1]+'&VehicleMonitoringDetailLevel=calls&LineRef='+sys.argv[2] #url="http://bustime.mta.info/api/siri/vehicle-monitoring.json?key=" + MTAAPIKEY + \ #"VehicleMonitoringDetailLevel=calls&LineRef=B52" #print (url) response = urllib.urlopen(url) data = response.read().decode("utf-8") data = json.loads(data) busx = data['Siri']['ServiceDelivery']['VehicleMonitoringDelivery'][0][ 'VehicleActivity'] s = "Latitude,Longitude,Stop Name,Stop Status\n" for i in busx: #print(type(i)) i = i['MonitoredVehicleJourney'] #print('Onward Calls', len(i['OnwardCalls']))
import pylab as P import emcee import time import os import matplotlib.image as mpimg from astropy.cosmology import Planck15 from scipy.stats import kde from scipy.interpolate import RegularGridInterpolator from scipy.interpolate import interp2d from itertools import product import sys from emcee.autocorr import AutocorrError from scipy.interpolate import interp2d font = {'family': 'serif', 'size': 16} P.rc('font', **font) P.rc('xtick', labelsize='medium') P.rc('ytick', labelsize='medium') P.rc('axes', labelsize='medium') pad_zmet = np.array([ 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0008, 0.001, 0.0012, 0.0016, 0.0020, 0.0025, 0.0031, 0.0039, 0.0049, 0.0061, 0.0077, 0.0096, 0.012, 0.015, 0.019, 0.024, 0.03 ]) pad_zmetsol = 0.019 pad_solmet = pad_zmet / pad_zmetsol zmets = (np.linspace(0, 10, 11) * 2 + 1).astype(int) zsolmets = pad_solmet[(zmets - 1).astype(int)]
#程序文件Pex18_6.py import pandas as pd from statsmodels.graphics.tsaplots import plot_acf import pylab as plt from statsmodels.tsa.arima_model import ARIMA plt.rc('axes', unicode_minus=False) plt.rc('font', size=16) plt.rc('font', family='SimHei') df = pd.read_csv('austa.csv') plt.subplot(121) plt.plot(df.value.diff()) plt.title('一次差分') ax2 = plt.subplot(122) plot_acf(df.value.diff().dropna(), ax=ax2, title='自相关') md = ARIMA(df.value, order=(2, 1, 0)) mdf = md.fit(disp=0) print(mdf.summary()) residuals = pd.DataFrame(mdf.resid) fig, ax = plt.subplots(1, 2) residuals.plot(title="残差", ax=ax[0]) residuals.plot(kind='kde', title='密度', ax=ax[1]) plt.legend('') plt.ylabel('') mdf.plot_predict() #原始数据与预测值对比图 plt.show()
#程序文件Pex19_2.py import numpy as np import pylab as plt from sklearn.svm import SVR np.random.seed(123) x = np.arange(200).reshape(-1, 1) y = (np.sin(x) + 3 + np.random.uniform(-1, 1, (200, 1))).ravel() model = SVR(gamma='auto') print(model) model.fit(x, y) pred_y = model.predict(x) print("原始数据与预测值前15个值对比:") for i in range(15): print(y[i], pred_y[i]) plt.rc('font', family='SimHei') plt.rc('font', size=15) plt.scatter(x, y, s=5, color="blue", label="原始数据") plt.plot(x, pred_y, '-r*', lw=1.5, label="预测值") plt.legend(loc=1) score = model.score(x, y) print("score:", score) ss = ((y - pred_y)**2).sum() #计算残差平方和 print("残差平方和:", ss) plt.show()
import pylab # Load the data from the file. t, s, ds = pylab.loadtxt('data/scatter_plot.txt', unpack=True) # Format the plot. pylab.rc('font', size=18) pylab.title('Law of motion', y=1.02) pylab.xlabel('t [s]') pylab.ylabel('s [m]', labelpad=25) pylab.xlim(0, 10) pylab.ylim(0, 18) pylab.grid(color='gray') # Create the scatter plot. pylab.errorbar(t, s, ds, linestyle='', color='black', marker='o') # Save the plot to a pdf file for later use (maybe in a writeup?). pylab.savefig('scatter_plot_fancier.pdf') # Finally: show the plot on the screen. pylab.show()
import matplotlib as mpl import pylab as plt from pylab import rc rc('axes', linewidth=1.2) mpl.rcParams.update(mpl.rcParamsDefault) mpl.rcParams['font.size'] = 26. mpl.rcParams['font.family'] = 'serif' #mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.serif'] = [ 'Times New Roman', 'Times', 'Palatino', 'Charter', 'serif' ] mpl.rcParams['font.sans-serif'] = ['Helvetica'] mpl.rcParams['axes.labelsize'] = 24 mpl.rcParams['xtick.labelsize'] = 22. mpl.rcParams['ytick.labelsize'] = 22. mpl.rcParams['xtick.major.size'] = 15. mpl.rcParams['xtick.minor.size'] = 10. mpl.rcParams['ytick.major.size'] = 15. mpl.rcParams['ytick.minor.size'] = 10. #mpl.rcParams['figure.autolayout']= True #fontsize=26 #mpl.rc('axes', titlesize=fontsize) #mpl.rc('axes', labelsize=fontsize) #mpl.rc('xtick', labelsize=fontsize) #mpl.rc('ytick', labelsize=fontsize) #mpl.rc('font', size=fontsize, family='serif', serif='Utopia', # style='normal', variant='normal', # stretch='normal', weight='normal') #mpl.rc('font',**{'family':'serif','serif':[ 'Times New Roman', 'Times', 'serif'],
import aplpy as ap import numpy as np import pylab as pl import coords as co mom0_map = '../moment_maps/spt0348_C+_dirty_contsub_briggs_robust05.image.mom0.fits' mom1_map = '../moment_maps/spt0348_C+_dirty_contsub_briggs_robust05.image.mom1.fits' mom2_map = '../moment_maps/spt0348_C+_dirty_contsub_briggs_robust05.image.mom2.fits' ############ #MOMENT MAPS ############ #set up figure fig = pl.figure() pl.rc('font', size=30) pl.rc('mathtext', default='regular') RA_c = co.convHMS('03:48:42.271') DEC_c = co.convDMS('-62:20:50.85') #moment-0 map mom0 = ap.FITSFigure(mom0_map, figure=fig, subplot=[0, 0, 0.75, 1]) mom0.tick_labels.set_xformat('hh:mm:ss') mom0.tick_labels.set_yformat('dd:mm:ss') mom0.hide_xaxis_label() mom0.hide_xtick_labels() mom0.hide_yaxis_label() mom0.hide_ytick_labels() mom0.recenter(RA_c, DEC_c, width=5. / 3600, height=5. / 3600) mom0.show_colorscale(cmap='jet', vmax=12, vmin=0.0, interpolation='nearest')
def test_with_nested_CV(folder='model', folds=5, plot=True, steps=['hashing', 'tfidf']): ''' Evaluates the classifer by doing nested CV i.e. keeping 1/folds of the data out of the training and doing training (including model selection for regularizer) on the training set and testing on the held-out data Also prints some stats and figures INPUT folder folder with model files folds number of folds ''' # start timer import time t0 = time.time() # create bag of words representations vv = Vectorizer(steps=steps) # load data vec = Vectorizer(folder=folder) data = get_speech_text(folder=folder) for key in data.keys(): data[key] = vec.transform(data[key]) # create numerical labels Y = hstack( map((lambda x: ones(data[data.keys()[x]].shape[0]) * x), range(len(data)))) # create data matrix X = vstack(data.values()) # permute data fsize = len(Y) / folds randidx = permutation(len(Y)) Y = Y[randidx] X = X[randidx, :] idx = reshape(arange(fsize * folds), (folds, fsize)) Y = Y[:fsize * folds] # allocate matrices for predictions predicted = zeros(fsize * folds) predicted_prob = zeros((fsize * folds, len(data))) # the regularization parameters to choose from parameters = {'C': (10.**arange(-4, 4, 1.)).tolist()} # do nested CV for ifold in range(folds): testidx = idx[ifold, :] trainidx = idx[setdiff1d(arange(folds), ifold), :].flatten() text_clf = LogisticRegression(class_weight='auto', dual=True) # for nested CV, do folds-1 CV for parameter optimization # within inner CV loop and use the outer testfold as held-out data # for model validation gs_clf = GridSearchCV(text_clf, parameters, n_jobs=-1, cv=(folds - 1)) gs_clf.fit(X[trainidx, :], Y[trainidx]) predicted[testidx] = gs_clf.predict(X[testidx, :]) predicted_prob[testidx, :] = gs_clf.predict_proba(X[testidx, :]) print '************ Fold %d *************' % (ifold + 1) print metrics.classification_report(Y[testidx], predicted[testidx], target_names=data.keys()) t1 = time.time() total_time = t1 - t0 timestr = 'Wallclock time: %f sec\n' % total_time dimstr = 'Vocabulary size: %d\n' % X.shape[-1] report = timestr + dimstr # extract some metrics print '********************************' print '************ Total *************' print '********************************' report += metrics.classification_report(Y, predicted, target_names=data.keys()) # dump metrics to file open(folder + '/report_%s.txt' % '_'.join(sorted(steps)), 'wb').write(report) print(report) conf_mat = metrics.confusion_matrix(Y, predicted) open(folder + '/conf_mat_%s.txt' % '_'.join(sorted(steps)), 'wb').write(json.dumps(conf_mat.tolist())) print(conf_mat) if plot: # print confusion matrix import pylab pylab.figure(figsize=(16, 16)) pylab.imshow(metrics.confusion_matrix(Y, predicted), interpolation='nearest') pylab.colorbar() pylab.xticks(arange(4), [x.decode('utf-8') for x in data.keys()]) pylab.yticks(arange(4), [x.decode('utf-8') for x in data.keys()]) pylab.xlabel('Predicted') pylab.ylabel('True') font = {'family': 'normal', 'size': 30} pylab.rc('font', **font) pylab.savefig(folder + '/conf_mat.pdf', bbox_inches='tight')
# Second Normalisation (mean norm) min_val, max_val = curve.min().min( ), curve.max().max() + fill.max().max() / 2 curve = (curve - min_val) / (max_val - min_val) elif VAR_ == 'ratio': # Fluorescence correction delta_val = 1 - curve[0].min() if delta_val < 0: curve = curve - abs(delta_val) ## Curve labelling gf_lbl = df.groupby([LVL_ORDER[1]]) lbl_list = gf_lbl[LABEL].first().values.astype('str') # normally same name # Figure fig = plt.figure(figsize=(W, H)) plt.rc('font', size=S) plt.rc('axes', titlesize=S) ax = fig.add_subplot() ax.set_title(CURVE_FOLDER + ' ' + label, fontsize=L) ax.set_ylabel(NAME_VAR + ' ' + VAR_, fontsize=L) ax.set_xlabel('time (h)', fontsize=L) #curve.T.plot(ax=ax) # Automatic legends, otherwise : #name = curve.index.values i = 0 if not (np.any(EXP_IDX != None)): EXP_IDX = np.arange(lbl_list.size) for stack in np.dstack((curve.values, fill, time))[EXP_IDX]: name = lbl_list[EXP_IDX[i]] i += 1 z = stack.T ax.plot(z[-1], z[0], label=name)
params = { 'text.usetex': True, 'axes.labelsize': fontsize, 'font.size': fontsize, 'legend.fontsize': legendfontsize, 'xtick.labelsize': fontsize, 'ytick.labelsize': fontsize, 'figure.figsize': fig_size, 'font.weight': 'normal' } import pylab pylab.rcParams.update(params) pylab.rcParams['axes.linewidth'] = 1 pylab.rc('axes', linewidth=1) def touchbox(ax): ax.minorticks_on() ax.tick_params('both', length=5, width=1, which='major') ax.tick_params('both', length=3.5, width=1, which='minor') ax.spines['top'].set_linewidth(1) ax.spines['bottom'].set_linewidth(1) ax.spines['right'].set_linewidth(1) ax.spines['left'].set_linewidth(1) return # plt.style.use('seaborn-darkgrid')
def animate_interactive(data, t = [], dimOrder = (0,1,2), fps = 10.0, title = '', xlabel = 'x', ylabel = 'y', fontsize = 24, cBar = 0, sloppy = True, rangeMin = [], rangeMax = [], extent = [-1,1,-1,1], shade = False, azdeg = 0, altdeg = 65, arrowsX = np.array(0), arrowsY = np.array(0), arrowsRes = 10, arrowsPivot = 'mid', arrowsWidth = 0.002, arrowsScale = 5, arrowsColor = 'black', plotArrowsGrid = False, movieFile = '', bitrate = 1800, keepImages = False, figsize = (8, 7), dpi = None, **kwimshow): """ Assemble a 2D animation from a 3D array. call signature:: animate_interactive(data, t = [], dimOrder = (0,1,2), fps = 10.0, title = '', xlabel = 'x', ylabel = 'y', fontsize = 24, cBar = 0, sloppy = True, rangeMin = [], rangeMax = [], extent = [-1,1,-1,1], shade = False, azdeg = 0, altdeg = 65, arrowsX = np.array(0), arrowsY = np.array(0), arrowsRes = 10, arrowsPivot = 'mid', arrowsWidth = 0.002, arrowsScale = 5, arrowsColor = 'black', plotArrowsGrid = False, movieFile = '', bitrate = 1800, keepImages = False, figsize = (8, 7), dpi = None, **kwimshow) Assemble a 2D animation from a 3D array. *data* has to be a 3D array who's time index has the same dimension as *t*. The time index of *data* as well as its x and y indices can be changed via *dimOrder*. Keyword arguments: *dimOrder*: [ (i,j,k) ] Ordering of the dimensions in the data array (t,x,y). *fps*: Frames per second of the animation. *title*: Title of the plot. *xlabel*: Label of the x-axis. *ylabel*: Label of the y-axis. *fontsize*: Font size of the title, x and y label. The size of the x- and y-ticks is 0.7*fontsize and the colorbar ticks's font size is 0.5*fontsize. *cBar*: [ 0 | 1 | 2 ] Determines how the colorbar changes: (0 - no cahnge; 1 - keep extreme values constant; 2 - change extreme values). *sloppy*: [ True | False ] If True the update of the plot lags one frame behind. This speeds up the plotting. *rangeMin*, *rangeMax*: Range of the colortable. *extent*: [ None | scalars (left, right, bottom, top) ] Data limits for the axes. The default assigns zero-based row, column indices to the *x*, *y* centers of the pixels. *shade*: [ False | True ] If True plot a shaded relief plot instead of the usual colormap. Note that with this option cmap has to be specified like cmap = plt.cm.hot instead of cmap = 'hot'. Shading cannot be used with the cBar = 0 option. *azdeg*, *altdeg*: Azimuth and altitude of the light source for the shading. *arrowsX*: Data containing the x-component of the arrows. *arrowsy*: Data containing the y-component of the arrows. *arrowsRes*: Plot every arrowRes arrow. *arrowsPivot*: [ 'tail' | 'middle' | 'tip' ] The part of the arrow that is at the grid point; the arrow rotates about this point. *arrowsWidth*: Width of the arrows. *arrowsScale*: Scaling of the arrows. *arrowsColor*: Color of the arrows. *plotArrowsGrid*: [ False | True ] If 'True' the grid where the arrows are aligned to is shown. *movieFile*: [ None | string ] The movie file where the animation should be saved to. If 'None' no movie file is written. Requires 'mencoder' to be installed. *bitrate*: Bitrate of the movie file. Set to higher value for higher quality. *keepImages*: [ False | True ] If 'True' the images for the movie creation are not deleted. *figsize*: Size of the figure in inches. *dpi*: Dots per inch of the frame. **kwimshow: Remaining arguments are identical to those of pylab.imshow. Refer to that help. """ import pylab as plt import time import os # for making the movie try: import thread # for GUI except: import _thread as thread from matplotlib.colors import LightSource global tStep, sliderTime, pause # plot the current frame def plotFrame(): global tStep, sliderTime if movieFile: ax.set_title(title+r'$\quad$'+r'$t={0}$'.format(t[tStep]), fontsize = fontsize) if shade == False: image.set_data(data[tStep,:,:]) else: image.set_data(rgb[tStep,:,:,:]) if (cBar == 0): pass if (cBar == 1): colorbar.set_clim(vmin=data[tStep,:,:].min(), vmax=data[tStep,:,:].max()) if (cBar == 2): colorbar.set_clim(vmin=data[tStep,:,:].min(), vmax=data[tStep,:,:].max()) colorbar.update_bruteforce(data[tStep,:,:]) if plotArrows: arrows.set_UVC(U = arrowsX[tStep,::arrowsRes,::arrowsRes], V = arrowsY[tStep,::arrowsRes,::arrowsRes]) if (sloppy == False) or (movieFile): manager.canvas.draw() # play the movie def play(threadName): global tStep, sliderTime, pause pause = False while (tStep < nT) & (pause == False): # write the image files for the movie if movieFile: plotFrame() frameName = movieFile + '%06d.png'%tStep fig.savefig(frameName, dpi = dpi) movieFiles.append(frameName) else: start = time.clock() # time slider sliderTime.set_val(t[tStep]) # wait for the next frame (fps) while (time.clock() - start < 1.0/fps): pass # do nothing tStep += 1 tStep -= 1 # call the play function as a separate thread (for GUI) def play_thread(event): global pause if pause == True: try: thread.start_new_thread(play, ("playThread", )) except: print("Error: unable to start play thread") def pausing(event): global pause pause = True def reverse(event): global tStep, sliderTime tStep -= 1 if tStep < 0: tStep = 0 # plot the frame and update the time slider sliderTime.set_val(t[tStep]) def forward(event): global tStep, sliderTime tStep += 1 if tStep > len(t)-1: tStep = len(t)-1 # plot the frame and update the time slider sliderTime.set_val(t[tStep]) pause = True plotArrows = False # check if the data has the right dimensions if (len(data.shape) != 3 and len(data.shape) != 4): print("error: data dimensions are invalid: {0} instead of 3".format(len(data.shape))) return -1 # transpose the data according to dimOrder unOrdered = data data = np.transpose(unOrdered, dimOrder) unOrdered = [] # check if arrows should be plotted if len(arrowsX.shape) == 3: # transpose the data according to dimOrder unOrdered = arrowsX arrowsX = np.transpose(unOrdered, dimOrder) unOrdered = [] if len(arrowsY.shape) == 3: # transpose the data according to dimOrder unOrdered = arrowsY arrowsY = np.transpose(unOrdered, dimOrder) unOrdered = [] # check if the dimensions of the arrow arrays match each other if ((len(arrowsX[:,0,0]) != len(arrowsY[:,0,0])) or (len(arrowsX[0,:,0]) != len(arrowsY[0,:,0])) or (len(arrowsX[0,0,:]) != len(arrowsY[0,0,:]))): print("error: dimensions of arrowX do not match with dimensions of arrowY") return -1 else: plotArrows = True # check if time array has the right length nT = len(t) if (nT != len(data[:,0,0])): print("error: length of time array doesn\'t match length of data array") return -1 if plotArrows: if (nT != len(arrowsX[:,0,0]) or nT != len(arrowsX[:,0,0])): print("error: length of time array doesn\'t match length of arrows array") return -1 # check if fps is positive if (fps < 0.0): print("error: fps is not positive, fps = {0}".format(fps)) return -1 # determine the size of the array nX = len(data[0,:,0]) nY = len(data[0,0,:]) # determine the minimum and maximum values of the data set if not(rangeMin): rangeMin = np.min(data) if not(rangeMax): rangeMax = np.max(data) # setup the plot if movieFile: plt.rc("figure.subplot", bottom=0.15) plt.rc("figure.subplot", top=0.95) plt.rc("figure.subplot", right=0.95) plt.rc("figure.subplot", left=0.15) fig = plt.figure(figsize = figsize) ax = plt.axes([0.1, 0.1, .90, .85]) else: plt.rc("figure.subplot", bottom=0.05) plt.rc("figure.subplot", top=0.95) plt.rc("figure.subplot", right=0.95) plt.rc("figure.subplot", left=0.15) fig = plt.figure(figsize = figsize) ax = plt.axes([0.1, 0.25, .85, .70]) ax.set_title(title, fontsize = fontsize) ax.set_xlabel(xlabel, fontsize = fontsize) ax.set_ylabel(ylabel, fontsize = fontsize) plt.xticks(fontsize = 0.7*fontsize) plt.yticks(fontsize = 0.7*fontsize) if shade: plane = np.zeros((nX,nY,3)) else: plane = np.zeros((nX,nY)) # apply shading if True if shade: ls = LightSource(azdeg = azdeg, altdeg = altdeg) rgb = [] # shading can be only used with cBar = 1 or cBar = 2 at the moment if cBar == 0: cBar = 1 # check if colormap is set, if not set it to 'copper' if not 'cmap' in kwimshow.keys(): kwimshow['cmap'] = plt.cm.copper for i in range(len(data[:,0,0])): tmp = ls.shade(data[i,:,:], kwimshow['cmap']) rgb.append(tmp.tolist()) rgb = np.array(rgb) tmp = [] # calibrate the displayed colors for the data range image = ax.imshow(plane, vmin=rangeMin, vmax=rangeMax, origin='lower', extent = extent, **kwimshow) colorbar = fig.colorbar(image) # change the font size of the colorbar's ytickslabels cbytick_obj = plt.getp(colorbar.ax.axes, 'yticklabels') plt.setp(cbytick_obj, fontsize = 0.5*fontsize) # plot the arrows # TODO: add some more options if plotArrows: # prepare the mash grid where the arrows will be drawn arrowGridX, arrowGridY = np.meshgrid(np.arange(extent[0], extent[1], float(extent[1]-extent[0])*arrowsRes/len(data[0,:,0])), np.arange(extent[2], extent[3], float(extent[3]-extent[2])*arrowsRes/len(data[0,0,:]))) arrows = ax.quiver(arrowGridX, arrowGridY, arrowsX[0,::arrowsRes,::arrowsRes], arrowsY[0,::arrowsRes,::arrowsRes], units = 'width', pivot = arrowsPivot, width = arrowsWidth, scale = arrowsScale, color = arrowsColor) # plot the grid for the arrows if plotArrowsGrid == True: ax.plot(arrowGridX, arrowGridY, 'k.') # for real-time image display if (sloppy == False) or (movieFile): manager = plt.get_current_fig_manager() manager.show() tStep = 0 if movieFile: movieFiles = [] # start the animation play('noThread') # write the movie file mencodeCommand = "mencoder 'mf://"+movieFile+"*.png' -mf type=png:fps="+np.str(fps)+" -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate="+np.str(bitrate)+" -ffourcc MP4S -oac copy -o "+movieFile+".mpg" os.system(mencodeCommand) # clean up the image files if (keepImages == False): print("cleaning up files") for fname in movieFiles: os.remove(fname) else: # set up the gui plt.ion() axPlay = plt.axes([0.1, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonPlay = plt.Button(axPlay, 'play', color='lightgoldenrodyellow', hovercolor='0.975') buttonPlay.on_clicked(play_thread) axPause = plt.axes([0.3, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonPause = plt.Button(axPause, 'pause', color='lightgoldenrodyellow', hovercolor='0.975') buttonPause.on_clicked(pausing) axReverse = plt.axes([0.5, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonReverse = plt.Button(axReverse, 'reverse', color='lightgoldenrodyellow', hovercolor='0.975') buttonReverse.on_clicked(reverse) axForward = plt.axes([0.7, 0.05, 0.15, 0.05], axisbg='lightgoldenrodyellow') buttonForward = plt.Button(axForward, 'forward', color='lightgoldenrodyellow', hovercolor='0.975') buttonForward.on_clicked(forward) # create the time slider fig.subplots_adjust(bottom=0.2) sliderTimeAxes = plt.axes([0.2, 0.12, 0.6, 0.03], axisbg='lightgoldenrodyellow') sliderTime = plt.Slider(sliderTimeAxes, 'time', t[0], t[-1], valinit = 0.0) def update(val): global tStep # find the closest time step to the slider time value for i in range(len(t)): if t[i] < sliderTime.val: tStep = i if (tStep != len(t)-1): if (t[tStep+1] - sliderTime.val) < (sliderTime.val - t[tStep]): tStep += 1 plotFrame() sliderTime.on_changed(update) plt.show() print("done")
def usetexFalse(): pylab.rc('text', usetex=False) pylab.rc('font', size=14) pylab.rc('axes', titlesize=16, labelsize=16) pylab.rc('xtick', labelsize=14) pylab.rc('ytick', labelsize=14)
def inv_convolve(h1, h3, length): x = np.random.standard_normal(10000) u = signal.lfilter(h1, 1, x) d = signal.lfilter(h3, 1, x) h = np.zeros(length, np.float64) nlms(u, d, h, 0.1) return h h1 = np.fromfile("h1.txt", sep="\n") h1 /= np.max(h1) h3 = np.fromfile("h3.txt", sep="\n") h3 /= np.max(h3) pl.rc('legend', fontsize=10) pl.subplot(411) pl.plot(h3, label="h3") pl.plot(h1, label="h1") pl.legend() pl.gca().set_yticklabels([]) for idx, length in enumerate([128, 256, 512]): pl.subplot(412 + idx) h2 = inv_convolve(h1, h3, length) pl.plot(np.convolve(h1, h2)[:len(h3)], label="h1*h2(%s)" % length) pl.legend() pl.gca().set_yticklabels([]) pl.gca().set_xticklabels([]) pl.show()