def plot(all_models): import matplotlib.pylab as plt import numpy.random plt.close("all") plt.figure() plt.subplot(211) alt = np.arange(0., 500., 2.) sza = 0. for m in all_models: d = m(alt, sza) plt.plot(ne_to_fp(d)/1E6, alt,lw=2) # plt.plot(m(alt, sza),alt,lw=2) plt.ylim(0., 400.) plt.ylabel('Altitude / km') # plt.xlabel(r'$n_e / cm^{-3}$') plt.xlabel(r'$f / MHz$') plt.subplot(212) for m in all_models: delay, freq = m.ais_response() plt.plot(freq/1E6, delay*1E3, lw=2.) plt.hlines(-2 * np.amax(alt) / speed_of_light_kms * 1E3, *plt.xlim(), linestyle='dashed') # plt.vlines(ne_to_fp(1E5)/1E6, *plt.ylim()) # plt.hlines( -(500-150) * 2 / speed_of_light_kms * 1E3, *plt.xlim()) plt.ylim(-10,0) plt.ylabel('Delay / ms') plt.xlim(0, 7) plt.xlabel('f / MHz') plt.show()
def make_corr1d_fig(dosave=False): corr = make_corr_both_hemi() lw=2; fs=16 pl.figure(1)#, figsize=(8, 7)) pl.clf() pl.xlim(4,300) pl.ylim(-400,+500) lambda_titles = [r'$20 < \lambda < 30$', r'$30 < \lambda < 40$', r'$\lambda > 40$'] colors = ['blue','green','red'] for i in range(3): corr1d, rcen = corr_1d_from_2d(corr[i]) ipdb.set_trace() pl.semilogx(rcen, corr1d*rcen**2, lw=lw, color=colors[i]) #pl.semilogx(rcen, corr1d*rcen**2, 'o', lw=lw, color=colors[i]) pl.xlabel(r'$s (Mpc)$',fontsize=fs) pl.ylabel(r'$s^2 \xi_0(s)$', fontsize=fs) pl.legend(lambda_titles, 'lower left', fontsize=fs+3) pl.plot([.1,10000],[0,0],'k--') s_bao = 149.28 pl.plot([s_bao, s_bao],[-9e9,+9e9],'k--') pl.text(s_bao*1.03, 420, 'BAO scale') pl.text(s_bao*1.03, 370, '%0.1f Mpc'%s_bao) if dosave: pl.savefig('xi1d_3bin.pdf')
def test1(): x = [0.5]*3 xbounds = [(-5, 5) for y in x] GA = GenAlg(fitcalc1, x, xbounds, popMult=100, bitsPerGene=9, mutation=(1./9.), crossover=0.65, crossN=2, direction='min', maxGens=60, hammingDist=False) results = GA.run() print "*** DONE ***" #print results plt.ioff() #generate pareto frontier numerically x1_ = np.arange(-5., 0., 0.05) x2_ = np.arange(-5., 0., 0.05) x3_ = np.arange(-5., 0., 0.05) pfn = [] for x1 in x1_: for x2 in x2_: for x3 in x3_: pfn.append(fitcalc1([x1,x2,x3])) pfn.sort(key=lambda x:x[0]) plt.figure() i = 0 for x in results: plt.scatter(x[1][0], x[1][1], 20, c='r') plt.scatter([x[0] for x in pfn], [x[1] for x in pfn], 1.0, c='b', alpha=0.1) plt.xlim([-20,-1]) plt.ylim([-12, 2]) plt.draw()
def test_abu_evolution(self): from NuGridPy import ppn, utils import matplotlib matplotlib.use('agg') import matplotlib.pylab as mpy import os # Perform tests within temporary directory with TemporaryDirectory() as tdir: # wget the data for a ppn run from the CADC VOspace os.system("wget -q --content-disposition --directory '" + tdir + "' "\ + "'http://www.canfar.phys.uvic.ca/vospace/synctrans?TARGET="\ + "vos%3A%2F%2Fcadc.nrc.ca%21vospace%2Fnugrid%2Fdata%2Fprojects%2Fppn%2Fexamples%2F"\ + "ppn_Hburn_simple%2Fx-time.dat&DIRECTION=pullFromVoSpace&PROTOCOL"\ + "=ivo%3A%2F%2Fivoa.net%2Fvospace%2Fcore%23httpget'") #nugrid_dir= os.path.dirname(os.path.dirname(ppn.__file__)) #NuPPN_dir= nugrid_dir + "/NuPPN" #test_data_dir= NuPPN_dir + "/examples/ppn_Hburn_simple/RUN_MASTER" symbs=utils.symbol_list('lines2') x=ppn.xtime(tdir) specs=['PROT','HE 4','C 12','N 14','O 16'] i=0 for spec in specs: x.plot('time',spec,logy=True,logx=True,shape=utils.linestyle(i)[0],show=False,title='') i += 1 mpy.ylim(-5,0.2) mpy.legend(loc=0) mpy.xlabel('$\log t / \mathrm{min}$') mpy.ylabel('$\log X \mathrm{[mass fraction]}$') abu_evol_file = 'abu_evolution.png' mpy.savefig(abu_evol_file) self.assertTrue(os.path.exists(abu_evol_file))
def study_multiband_planck(quick=True): savename = datadir+'cl_multiband.pkl' bands = [100, 143, 217, 'mb'] if quick: cl = pickle.load(open(savename,'r')) else: cl = {} mask = load_planck_mask() mask_factor = np.mean(mask**2.) for band in bands: this_map = load_planck_data(band) this_cl = hp.anafast(this_map*mask, lmax=lmax)/mask_factor cl[band] = this_cl pickle.dump(cl, open(savename,'w')) cl_theory = {} pl.clf() for band in bands: l_theory, cl_theory[band] = get_cl_theory(band) this_cl = cl[band] pl.plot(this_cl/cl_theory[band]) pl.legend(bands) pl.plot([0,4000],[1,1],'k--') pl.ylim(.7,1.3) pl.ylabel('data/theory')
def plot(self, bit_stream): if self.previous_bit_stream != bit_stream.to_list(): self.previous_bit_stream = bit_stream x = [] y = [] bit = None for bit_time in bit_stream.to_list(): if bit is None: x.append(bit_time) y.append(0) bit = 0 elif bit == 0: x.extend([bit_time, bit_time]) y.extend([0, 1]) bit = 1 elif bit == 1: x.extend([bit_time, bit_time]) y.extend([1, 0]) bit = 0 plt.clf() plt.plot(x, y) plt.xlim([0, 10000]) plt.ylim([-0.1, 1.1]) plt.show() plt.pause(0.005)
def plot_svc(X, y, mysvc, bounds=None, grid=50): if bounds is None: xmin = np.min(X[:, 0], 0) xmax = np.max(X[:, 0], 0) ymin = np.min(X[:, 1], 0) ymax = np.max(X[:, 1], 0) else: xmin, ymin = bounds[0], bounds[0] xmax, ymax = bounds[1], bounds[1] aspect_ratio = (xmax - xmin) / (ymax - ymin) xgrid, ygrid = np.meshgrid(np.linspace(xmin, xmax, grid), np.linspace(ymin, ymax, grid)) plt.gca(aspect=aspect_ratio) plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) plt.xticks([]) plt.yticks([]) plt.hold(True) plt.plot(X[y == 1, 0], X[y == 1, 1], 'bo') plt.plot(X[y == -1, 0], X[y == -1, 1], 'ro') box_xy = np.append(xgrid.reshape(xgrid.size, 1), ygrid.reshape(ygrid.size, 1), 1) if mysvc is not None: scores = mysvc.decision_function(box_xy) else: print 'You must have a valid SVC object.' return None; CS=plt.contourf(xgrid, ygrid, scores.reshape(xgrid.shape), alpha=0.5, cmap='jet_r') plt.contour(xgrid, ygrid, scores.reshape(xgrid.shape), levels=[0], colors='k', linestyles='solid', linewidths=1.5) plt.contour(xgrid, ygrid, scores.reshape(xgrid.shape), levels=[-1,1], colors='k', linestyles='dashed', linewidths=1) plt.plot(mysvc.support_vectors_[:,0], mysvc.support_vectors_[:,1], 'ko', markerfacecolor='none', markersize=10) CB = plt.colorbar(CS)
def plot_cell(self,cell_number=0,label='insert_label'): current_cell = self.cell_list[cell_number] temp = current_cell.temp cd_signal = current_cell.cd_signal cd_calc = current_cell.cd_calc() ax = pylab.gca() pylab.plot(temp,cd_signal,'o',color='black') pylab.plot(temp,cd_calc,color='black') pylab.xlabel(r'Temperature ($^{\circ}$C)') pylab.ylabel('mdeg') pylab.ylim([-25,-4]) dH = numpy.round(current_cell.dH, decimals=1) Tm = numpy.round(current_cell.Tm-273.15, decimals=1) nf = current_cell.nf nu = current_cell.nu textstr_dH = '${\Delta}H_{m}$ = %.1f kcal/mol' %dH textstr_Tm ='$T_{m}$ = %.1f $^{\circ}$C' %Tm textstr_nf ='$N_{folded}$ = %d' %nf textstr_nu ='$N_{unfolded}$ = %d'%nu ax.text(8,-6,textstr_dH, fontsize=16,ha='left',va='top') ax.text(8,-7.5,textstr_Tm, fontsize=16,ha='left',va='top') ax.text(8,-9,textstr_nf, fontsize=16,ha='left',va='top') ax.text(8,-10.5,textstr_nu, fontsize=16,ha='left',va='top') pylab.title(label) pylab.show() return
def visualization2(self, sp_to_vis=None): if sp_to_vis: species_ready = list(set(sp_to_vis).intersection(self.all_sp_signatures.keys())) else: raise Exception('list of driver species must be defined') if not species_ready: raise Exception('None of the input species is a driver') for sp in species_ready: # Setting up figure plt.figure() plt.subplot(313) mon_val = OrderedDict() signature = self.all_sp_signatures[sp] for idx, mon in enumerate(list(set(signature))): if mon[0] == 'C': mon_val[self.all_comb[sp][mon] + (-1,)] = idx else: mon_val[self.all_comb[sp][mon]] = idx mon_rep = [0] * len(signature) for i, m in enumerate(signature): if m[0] == 'C': mon_rep[i] = mon_val[self.all_comb[sp][m] + (-1,)] else: mon_rep[i] = mon_val[self.all_comb[sp][m]] # mon_rep = [mon_val[self.all_comb[sp][m]] for m in signature] y_pos = numpy.arange(len(mon_val.keys())) plt.scatter(self.tspan[1:], mon_rep) plt.yticks(y_pos, mon_val.keys()) plt.ylabel('Monomials', fontsize=16) plt.xlabel('Time(s)', fontsize=16) plt.xlim(0, self.tspan[-1]) plt.ylim(0, max(y_pos)) plt.subplot(312) for name in self.model.odes[sp].as_coefficients_dict(): mon = name mon = mon.subs(self.param_values) var_to_study = [atom for atom in mon.atoms(sympy.Symbol)] arg_f1 = [numpy.maximum(self.mach_eps, self.y[str(va)][1:]) for va in var_to_study] f1 = sympy.lambdify(var_to_study, mon) mon_values = f1(*arg_f1) mon_name = str(name).partition('__')[2] plt.plot(self.tspan[1:], mon_values, label=mon_name) plt.ylabel('Rate(m/sec)', fontsize=16) plt.legend(bbox_to_anchor=(-0.1, 0.85), loc='upper right', ncol=1) plt.subplot(311) plt.plot(self.tspan[1:], self.y['__s%d' % sp][1:], label=parse_name(self.model.species[sp])) plt.ylabel('Molecules', fontsize=16) plt.legend(bbox_to_anchor=(-0.15, 0.85), loc='upper right', ncol=1) plt.suptitle('Tropicalization' + ' ' + str(self.model.species[sp])) # plt.show() plt.savefig('s%d' % sp + '.png', bbox_inches='tight', dpi=400)
def plot_q(frame,file_prefix='claw',file_format='petsc',path='./_output/',plot_pcolor=True,plot_slices=True,slices_xlimits=None): import sys sys.path.append('.') import gaussian_1d sol=Solution(frame,file_format=file_format,read_aux=False,path=path,file_prefix=file_prefix) x=sol.state.grid.x.centers mx=len(x) bathymetry = 0.5 eta=sol.state.q[0,:] + bathymetry if frame < 10: str_frame = "00"+str(frame) elif frame < 100: str_frame = "0"+str(frame) else: str_frame = str(frame) fig = pl.figure(figsize=(40,10)) ax = fig.add_subplot(111) ax.set_aspect(aspect=1) ax.plot(x,eta) #pl.title("t= "+str(sol.state.t),fontsize=20) #pl.xticks(size=20); pl.yticks(size=20) #pl.xlim([0, gaussian_1d.Lx]) pl.ylim([0.5, 1.0]) pl.xlim([0., 4.0]) #pl.axis('equal') pl.savefig('./_plots/eta_'+str_frame+'_slices.png') pl.close()
def plot_part2(filename): """ Plots the result of count ones test """ fig1 = pl.figure() iterations, runtimes, fvals = extract(filename) algos = ["SA", "GA", "MIMIC"] iters_sa, iters_ga, iters_mimic = [np.array(iterations[a]) for a in algos] runtime_sa, runtime_ga, runtime_mimic = [np.array(runtimes[a]) for a in algos] fvals_sa, fvals_ga, fvals_mimic = [np.array(fvals[a]) for a in algos] plotfunc = getattr(pl, "loglog") plotfunc(runtime_sa, fvals_sa, "bs", mew=0) plotfunc(runtime_ga, fvals_ga, "gs", mew=0) plotfunc(runtime_mimic, fvals_mimic, "rs", mew=0) # plotfunc(iters_sa, fvals_sa/(runtime_sa * iters_sa), "bs", mew=0) # plotfunc(iters_ga, fvals_ga/(runtime_ga * iters_ga), "gs", mew=0) # plotfunc(iters_mimic, fvals_mimic/(runtime_mimic * iters_mimic), "rs", mew=0) pl.xlabel("Runtime (seconds)") pl.ylabel("Objective function value") pl.ylim([min(fvals_sa) / 2, max(fvals_mimic) * 2]) pl.legend(["SA", "GA", "MIMIC"], loc=4) pl.savefig(filename.replace(".csv", ".png"), bbox_inches="tight")
def draw_lineplot(x, y, title="title", xlab="x", ylab="y", odir="", xlim=None, ylim=None, outfmt='eps'): if len(x) == 0 or len(y) == 0: return; #fi plt.cla(); plt.plot(x, y, marker='x'); plt.xlabel(xlab); plt.ylabel(ylab); plt.title(title); if xlim == None: xmin = min(x); xmax = max(x); xlim = [xmin, xmax]; #fi if ylim == None: ymin = min(y); ymax = max(y); ylim = [ymin, ymax]; #fi plt.xlim(xlim); plt.ylim(ylim); plt.savefig('%s%s.%s' % (odir + ('/' if odir else ""), '_'.join(title.split(None)), outfmt), format=outfmt); return '%s.%s' % ('_'.join(title.split(None)), outfmt), title;
def plotFirstTacROC(dataset): import matplotlib.pylab as plt from os.path import join from src.utils import PROJECT_DIR plt.figure(figsize=(6, 6)) time_sampler = TimeSerieSampler(n_time_points=12) evaluator = Evaluator() time_series_idx = 0 methods = { "cross_correlation": "Cross corr. ", "kendall": "Kendall ", "symbol_mutual": "Symbol MI ", "symbol_similarity": "Symbol sim.", } for method in methods: print method predictor = SingleSeriesPredictor(good_methods[method], time_sampler) prediction = predictor.predictAllInstancesCombined(dataset, time_series_idx) roc_auc, fpr, tpr = evaluator.evaluate(prediction) plt.plot(fpr, tpr, label=methods[method] + " (auc = %0.3f)" % roc_auc) plt.legend(loc="lower right") plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") plt.grid() plt.savefig(join(PROJECT_DIR, "output", "firstTACROC.pdf"))
def _on_button_press(event): if event.button != 1 or not event.inaxes: return lon, lat = m(event.xdata, event.ydata, inverse=True) # Convert to colat to ease indexing. colat = rotations.lat2colat(lat) x_range = (self.setup["physical_boundaries_x"][1] - self.setup["physical_boundaries_x"][0]) x_frac = (colat - self.setup["physical_boundaries_x"][0]) / x_range x_index = int(((self.setup["boundaries_x"][1] - self.setup["boundaries_x"][0]) * x_frac) + self.setup["boundaries_x"][0]) y_range = (self.setup["physical_boundaries_y"][1] - self.setup["physical_boundaries_y"][0]) y_frac = (lon - self.setup["physical_boundaries_y"][0]) / y_range y_index = int(((self.setup["boundaries_y"][1] - self.setup["boundaries_y"][0]) * y_frac) + self.setup["boundaries_y"][0]) plt.figure(1, figsize=(3, 8)) depths = available_depths values = data[x_index, y_index, :] plt.plot(values, depths) plt.grid() plt.ylim(depths[-1], depths[0]) plt.show() plt.close() plt.figure(0)
def plot_prob_effector(sens, fpr, xmax=1, baserate=0.1): """Plots a line graph of P(effector|positive test) against the baserate of effectors in the input set to the classifier. The baserate argument draws an annotation arrow indicating P(pos|+ve) at that baserate """ assert 0.1 <= xmax <= 1, "Max x axis value must be in range [0,1]" assert 0.01 <= baserate <= 1, "Baserate annotation must be in range [0,1]" baserates = pylab.arange(0, 1.05, xmax * 0.005) probs = [p_correct_given_pos(sens, fpr, b) for b in baserates] pylab.plot(baserates, probs, 'r') pylab.title("P(eff|pos) vs baserate; sens: %.2f, fpr: %.2f" % (sens, fpr)) pylab.ylabel("P(effector|positive)") pylab.xlabel("effector baserate") pylab.xlim(0, xmax) pylab.ylim(0, 1) # Add annotation arrow xpos, ypos = (baserate, p_correct_given_pos(sens, fpr, baserate)) if baserate < xmax: if xpos > 0.7 * xmax: xtextpos = 0.05 * xmax else: xtextpos = xpos + (xmax-xpos)/5. if ypos > 0.5: ytextpos = ypos - 0.05 else: ytextpos = ypos + 0.05 pylab.annotate('baserate: %.2f, P(pos|+ve): %.3f' % (xpos, ypos), xy=(xpos, ypos), xytext=(xtextpos, ytextpos), arrowprops=dict(facecolor='black', shrink=0.05)) else: pylab.text(0.05 * xmax, 0.95, 'baserate: %.2f, P(pos|+ve): %.3f' % (xpos, ypos))
def plot_behavior_count(agent_id, behavior_count): """Create a plot for the behavior count. Args: agent_id: The identifier of the agent. behavior_count: The count of the agent behaviors. """ fig = plt.figure() ax = fig.add_subplot(111) plt.xlabel(u'Número de jogos') plt.ylabel(u'Probabilidades de selecionar comportamento') plt.title(u'Probabilidades do agente %d selecionar comportamento' % agent_id) plt.xlim([0, 115]) plt.ylim([-0.1, 1.1]) data = np.array([b for b in behavior_count.values()]) prob = data/np.sum(data, axis=0) for i, behavior in enumerate(behavior_count): coeff = calculate_regression_coefficients(prob[i], degree=4) regression = [calculate_regression_y(x, coeff) for x in range(len(prob[i]))] ax.plot(regression, label=T[behavior], c=COLOR_TABLE[COLOR_LIST[i]], linewidth=2.0) ax.scatter(range(len(prob[i])), prob[i], c=COLOR_TABLE[COLOR_LIST[i]], alpha=1) ax.legend()
def plot_average(filenames, save_plot=True, show_plot=False, dpi=100): ''' Plot Signal average from a list of averaged files. ''' fname = get_files_from_list(filenames) # plot averages pl.ioff() # switch off (interactive) plot visualisation factor = 1e15 for fnavg in fname: name = fnavg[0:len(fnavg) - 4] basename = os.path.splitext(os.path.basename(name))[0] print fnavg # mne.read_evokeds provides a list or a single evoked based on condition. # here we assume only one evoked is returned (requires further handling) avg = mne.read_evokeds(fnavg)[0] ymin, ymax = avg.data.min(), avg.data.max() ymin *= factor * 1.1 ymax *= factor * 1.1 fig = pl.figure(basename, figsize=(10, 8), dpi=100) pl.clf() pl.ylim([ymin, ymax]) pl.xlim([avg.times.min(), avg.times.max()]) pl.plot(avg.times, avg.data.T * factor, color='black') pl.title(basename) # save figure fnfig = os.path.splitext(fnavg)[0] + '.png' pl.savefig(fnfig, dpi=dpi) pl.ion() # switch on (interactive) plot visualisation
def plot_df(self,show=False): from matplotlib import pylab as plt if self.afp is None: print 'afp not initilized. call update afp' return -1 linecords,td,df,rtn,minmaxy = self.afp formatter = PlotDateFormatter(df.index) #fig = plt.figure() #ax = plt.addsubplot() fig, ax = plt.subplots() ax.xaxis.set_major_formatter(formatter) ax.plot(np.arange(len(df)), df['p']) for cord in linecords: plt.plot(cord[0],cord[1],color='red') fig.autofmt_xdate() plt.xlim(-10,len(df.index) + 10) plt.ylim(df.p.min() - 10,df.p.max() + 10) plt.grid(ax) #if show: # plt.show() #"{0}{1}.png".format("./data/",datetime.datetime.strftime(datetime.datetime.now(),'%Y%M%m%S')) if self.plot_file: save_path = self.plot_file.format(self.symbol) if os.path.exists(os.path.dirname(save_path)): plt.savefig(save_path) plt.clf() plt.close()
def nova_plot(): erg2mev=624151. fig=plot.figure() yrange = [1e-6,2e-4] xrange = [1e-1,1e5] plot.fill_between([0.2,10e3],[yrange[1],yrange[1]],[yrange[0],yrange[0]],facecolor='yellow',interpolate=True,color='yellow',alpha=0.5) plot.annotate('AMEGO',xy=(3,9e-5),xycoords='data',fontsize=26,color='black') lat=ascii.read("data/NMon2012.LAT.dat",names=['energy','en_low','en_high','flux','flux_err','tmp']) plot.scatter(lat['energy'],lat['flux']*erg2mev,color='red') plot.errorbar(lat['energy'],lat['flux']*erg2mev,xerr=[lat['en_low'],lat['en_high']],yerr=lat['flux_err']*erg2mev,ecolor='red',capsize=0,fmt='none') latul=ascii.read("data/NMon2012.LAT.limits.dat",names=['energy','en_low','en_high','flux','tmp1','tmp2','tmp3','tmp4']) plot.errorbar(latul['energy'],latul['flux']*erg2mev,xerr=[latul['en_low'],latul['en_high']],yerr=0.5*latul['flux']*erg2mev,uplims=True,ecolor='red',capsize=0,fmt='none') plot.scatter(latul['energy'],latul['flux']*erg2mev,color='red') leptonic=ascii.read("data/sp-NMon12-IC-best-fit-1MeV-30GeV.txt",names=['energy','flux'],data_start=1) hadronic=ascii.read("data/sp-NMon12-pi0-and-secondaries.txt",names=['energy','flux1','flux2'],data_start=1) plot.plot(leptonic['energy'],leptonic['flux']*erg2mev,'r--',color='black',lw=2,label='Leptonic') plot.plot(hadronic['energy'],hadronic['flux2']*erg2mev,color='black',lw=2,label='Hadronic+Secondary Leptons') plot.legend(loc='upper right',fontsize='small',frameon=False,framealpha=0.5) plot.xscale('log') plot.yscale('log') plot.ylim(yrange) plot.xlim(xrange) plot.xlabel(r'Energy (MeV)') plot.ylabel(r'Energy$^2 \times $ Flux (Energy) (erg cm$^{-2}$ s$^{-1}$)') plot.title('Nova V339 Del 2013') plot.savefig('Nova_SED.png', bbox_inches='tight') plot.savefig('Nova_SED.eps', bbox_inches='tight') plot.show() plot.close()
def plot_roc(test_cat, plot_data, savefig=False): # Plot ROC curve plt.clf() results = [] # calcualte and sort labels by roc_auc for method, method_results in plot_data.items(): fpr, tpr, roc_auc = compute_roc(test_cat, method_results, method) label = "[%s] area = %0.2f" % (method, roc_auc) res = {"label": label, "fpr": fpr, "tpr": tpr, "roc_auc": roc_auc} results.append(res) results = sorted(results, key=lambda k: k['roc_auc'], reverse=True) # plot according to roc_auc ranking for r in results: plt.plot(r["fpr"], r["tpr"], label=r["label"]) plt.title('Receiver Operating Characteristic Curve (ROC)') plt.legend(loc="lower right") plt.plot([0, 1], [0, 1], 'k--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.0]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') if savefig: plt.savefig("classifiers_roc.png") plt.show()
def chooseDegree(npts, mindegree=0, maxdegree=20, filename=None): """Gets noisy data, uses cross validation to estimate error, and fits new data with best model.""" x, y = bv.noisyData(npts) degrees = numpy.arange(mindegree,maxdegree+1) errs = numpy.zeros_like(degrees,dtype=numpy.float) for i,d in enumerate(degrees): errs[i] = estimateError(x, y, d) plt.subplot(1,2,1) plt.plot(degrees,errs,'bo-') plt.xlabel("Degree") plt.ylabel("CV Error") besti = numpy.argmin(errs) bestdegree = degrees[besti] plt.subplot(1,2,2) x2, y2 = bv.noisyData(npts) plt.plot(x2,y2,'ro') xs = numpy.linspace(min(x),max(x),150) fitf = numpy.poly1d(numpy.polyfit(x2,y2,bestdegree)) plt.plot(xs,fitf(xs),'g-',lw=2) plt.xlim((bv.MIN,bv.MAX)) plt.ylim((-2.,2.)) plt.suptitle('Selected Degree '+str(bestdegree)) bv.outputPlot(filename)
def plot_runs(runs): """ Plot population evolutions """ ts = range(len(runs[0])) cmap = plt.get_cmap('viridis') for i, r in enumerate(runs): mean, var = zip(*r) bm, cm = zip(*mean) bv, cv = zip(*var) color = cmap(float(i)/len(runs)) plt.errorbar(ts, bm, fmt='-', yerr=bv, c=color) plt.errorbar(ts, cm, fmt='--', yerr=cv, c=color) plt.title('population evolution overview') plt.xlabel('time') plt.ylabel('value') plt.ylim((0, 1)) plt.plot(0, 0, '-', c='black', label='benefit value') plt.plot(0, 0, '--', c='black', label='cost value') plt.legend(loc='best') plt.savefig('result.pdf') plt.show()
def plotClusters(self, clusters, Size=(11, 8)): """ Plots events belong to five different clusters. **Parameters** clusters : int (array or list) The index of the cluster from which the events will be plotted """ fig = plt.figure(figsize=Size) fig.subplots_adjust(wspace=.3, hspace=.3) ax = fig.add_subplot(511) self.plot_event(self.evts[self.goodEvts, :] [np.array(clusters) == 0, :]) plt.ylim([-15, 20]) ax = fig.add_subplot(512) self.plot_event(self.evts[self.goodEvts, :] [np.array(clusters) == 1, :]) ax.set_ylim([-15, 20]) ax = fig.add_subplot(513) self.plot_event(self.evts[self.goodEvts, :] [np.array(clusters) == 2, :]) ax.set_ylim([-15, 20]) ax = fig.add_subplot(514) self.plot_event(self.evts[self.goodEvts, :] [np.array(clusters) == 3, :]) ax.set_ylim([-15, 20]) ax = fig.add_subplot(515) self.plot_event(self.evts[self.goodEvts, :] [np.array(clusters) == 4, :]) ax.set_ylim([-15, 20])
def plot_eye(Nodes,axes = None): """ Create a movie of eye growth. To be used with EyeGrowthFunc :param Nodes: structure containing nodes :type Nodes: struct :param INTSTEP: time step used for integration :type INTSTEP: int :returns: plot handle for Node plot. Used to update during for loop. .. note:: Called in EyeGrowthFunc """ #set plotting parameters: if axes == None: fig = plt.figure(figsize=(10, 8)) axes = fig.add_subplot(111,aspect='equal') plt.xlim([-13, 13]) plt.ylim([-13, 13]) axes.plot(np.r_[ Nodes['x'][0],Nodes['x'][0,0] ] * Nodes['radius'], np.r_[ Nodes['y'][0], Nodes['y'][0,0] ] * Nodes['radius'], '-ok', markerfacecolor = 'k',linewidth = 4, markersize = 10) axes = pf.TufteAxis(axes,['left','bottom']) #axes.set_axis_bgcolor('w') return axes
def plotParamPath(chain, OBJi, save_opt=None): import matplotlib.pylab as plt import variable_house as vHouse nWalkers = np.shape(chain)[0] nSteps = np.shape(chain)[1] nDim = np.shape(chain)[2] stepidx = np.arange(1,nSteps+1) for param_i in range(0,nDim-vHouse.nDram): for walker_i in range(0,nWalkers): plt.plot(stepidx, chain[walker_i,:,param_i], color='k', alpha= 0.1) plt.xlabel('STEP INDEX') plt.ylabel(vHouse.nameL[param_i]) plt.title('param value of walkers after trimming') #show entire range of possible parameter values plt.ylim([np.min(vHouse.paramRanges[param_i,:]), np.max(vHouse.paramRanges[param_i,:])]) if save_opt != None: plt.savefig(save_opt+'param'+str(param_i)+'path.png') plt.close('all') else: plt.show()
def read_table(args): tblfilename = "bf_optimize_mavlink.h5" h5file = tb.open_file(tblfilename, mode = "r") # print h5file # a = h5file.root # print a # a = h5file.get_node(where = "/20150507-run1/params/_20150507155408") # print a # table = h5file.root.v1.evaluations table = h5file.root.v2.evaluations print "table", table # mse = [x["mse"] for x in table.iterrows() if x["alt_p"] < 20.] # mse = [x["mse"] for x in table.iterrows()] logdata = [x["timeseries"] for x in table.iterrows() if x["mse"] < 2000] alt_pid = [(x["alt_p"], x["alt_i"], x["alt_d"], x["vel_p"], x["vel_i"], x["vel_d"]) for x in table.iterrows() if x["mse"] < 1000] # alt_pid = [(x["alt_p"], x["alt_i"], x["alt_d"]) for x in table.iterrows() if x["alt_p"] == 17 and x["alt_i"] == 0.] print "alt_pid", alt_pid # print mse # pl.plot(mse) print len(logdata) for i in range(len(logdata)): pl.subplot(len(logdata), 1, i+1) pl.plot(logdata[i][:,1:3]) pl.ylim((-300, 1000)) pl.show()
def demo(): ''' Load and plot a few CIB spectra. ''' # define ell array. l = np.arange(100,4000) # get dictionary of CIBxCIB spectra. cl_cibcib = get_cl_cibcib(l) # plot import matplotlib.pylab as pl pl.ion() lw=2 fs=18 leg = [] pl.clf() for band in ['857','545','353']: pl.semilogy(l, cl_cibcib['545',band],linewidth=lw) leg.append('545 x '+band) pl.xlabel(r'$\ell$',fontsize=fs) pl.ylabel(r'$C_\ell^{TT, CIB} [\mu K^2]$',fontsize=fs) pl.ylim(5e-2,6e3) pl.legend(leg, fontsize=fs)
def km_emp_mean(df_pca,krange,empcol,crtcol): ''' apply kmeans to pca-processed dataframe, with a range of k. Then check with k yields clearest employed rate and correct rate. plot total number of k against good cluster ratio :param df_pca: numpy array :param krange: int, range of k :param empcol: panda series :param crtcol: panda series :raise: string, data frame, and plot ''' lis = [] df = pd.concat([empcol,crtcol],axis=1) for k in xrange(4,krange): km = KMeans(n_clusters=k,random_state=0) km.fit(df_pca) df['cluster'] = km.labels_ temp = df.groupby('cluster').agg(np.mean) res = temp[(temp['employed'] >0.7) | (temp['employed'] < 0.4) &(temp['correct'] >0.7) ] print "{} clusters".format(k) print res print "---" print "{} out of {} clusters split the target ideally. Good cluster rate: {}".format(res.shape[0],k,res.shape[0]/k) print '*'*20 lis.append(res.shape[0]/k) plt.plot(range(4,krange),lis,lw = 2) plt.xlabel('K') plt.ylabel('good cluster rate') plt.ylim(0.5,1.2)
def plot_lift_data(lift_data, with_ellipses=True): np.random.seed(42113) fig = plt.figure() ax = fig.add_subplot(111) alpha = [l['fit']['alpha'] for l in lift_data.values()] alpha_error = [l['fit']['alpha_error'] for l in lift_data.values()] beta = [l['fit']['beta'] for l in lift_data.values()] beta_error = [l['fit']['beta_error'] for l in lift_data.values()] message_class = lift_data.keys() num = len(beta) beta_jitter = np.random.randn(num) np.random.seed(None) beta = np.array(beta) + beta_jitter*0.0 ax.plot(beta, alpha, color='red', linestyle='', marker='o', markersize=10) if not with_ellipses: ax.errorbar(beta, alpha, xerr=beta_error, yerr=alpha_error, linestyle='') else: for x, y, xerr, yerr, in zip(beta, alpha, beta_error, alpha_error): width = 2*xerr height = 2*yerr ellipse = patches.Ellipse((x, y), width, height, angle=0.0, linewidth=2, fill=True, alpha=0.15, color='gray') ax.add_patch(ellipse) for a, b, c in zip(alpha, beta, message_class): ax.annotate(c, xy=(b, a), xytext=(b+2, a+.01), fontsize=17) plt.xlim(0, max(beta)+30) plt.ylim(0, 0.9) plt.xlabel('Duration (days)') plt.ylabel('Initial Lift') plt.show()
def plot(x,y,field,filename,c=200): plt.figure() # define grid. xi = np.linspace(min(x),max(x),100) yi = np.linspace(min(y),max(y),100) # grid the data. si_lin = griddata((x, y), field, (xi[None,:], yi[:,None]), method='linear') si_cub = griddata((x, y), field, (xi[None,:], yi[:,None]), method='linear') print np.min(field) print np.max(field) plt.subplot(211) # contour the gridded data, plotting dots at the randomly spaced data points. CS = plt.contour(xi,yi,si_lin,c,linewidths=0.5,colors='k') CS = plt.contourf(xi,yi,si_lin,c,cmap=plt.cm.jet) plt.colorbar() # draw colorbar # plot data points. # plt.scatter(x,y,marker='o',c='b',s=5) plt.xlim(min(x),max(x)) plt.ylim(min(y),max(y)) plt.title('Lineaarinen interpolointi') #plt.tight_layout() plt.subplot(212) # contour the gridded data, plotting dots at the randomly spaced data points. CS = plt.contour(xi,yi,si_cub,c,linewidths=0.5,colors='k') CS = plt.contourf(xi,yi,si_cub,c,cmap=plt.cm.jet) plt.colorbar() # draw colorbar # plot data points. # plt.scatter(x,y,marker='o',c='b',s=5) plt.xlim(min(x),max(x)) plt.ylim(min(y),max(y)) plt.title('Kuubinen interpolointi') plt.savefig(filename)
import matplotlib.pylab as plt import numpy as np from matplotlib.backends.backend_pdf import PdfPages # mp.rc('font', family = 'serif', serif = 'cmr10') mp.rcParams['mathtext.fontset'] = 'cm' mp.rcParams.update({'font.size': 16}) n=3 α = np.array([0.8,1.0,1.2]) X = np.array([8/15,5/15,2/15]) with PdfPages('water_filling_plot.pdf') as pdf: axis = np.arange(0.5,n+1.5,1) index = axis+0.5 # X = np.asarray(x).flatten() Y = α + X # to include the last data point as a step, we need to repeat it A = np.concatenate((α,[α[-1]])) X = np.concatenate((X,[X[-1]])) Y = np.concatenate((Y,[Y[-1]])) plt.xticks(index) plt.xlim(0.5,n+0.5) plt.ylim(0,1.5) plt.step(axis,A,where='post',label =r'$\alpha$',lw=2) plt.step(axis,Y,where='post',label=r'$\alpha + x$',lw=2) plt.legend(loc='lower right') plt.xlabel('channel number') plt.ylabel('power level') pdf.savefig(bbox_inches='tight')
detector='H1', dist=distMpcFiducial * 1.e6 * lal.LAL_PC_SI) df = findDeltaF(Psig) Psig.deltaF = df Psig.print_params() data = lsu.non_herm_hoff(Psig) psd = lal.LIGOIPsd IP = ComplexOverlap(30, 2048, data.deltaF, psd, True, True) # Generate and plot hlm(f) and the weights we would use in an inner product Psig.dist = distMpcReference * 1.e6 * lal.LAL_PC_SI # Fiducial distance hlms = hlmoff(Psig, 2) plt.figure(1) fvals = np.linspace(-IP.fNyq, IP.fNyq, IP.wvlen) plt.plot(fvals, 1 / np.sqrt(IP.longweights)) plt.xlim(-500, 500) plt.ylim(0, 1e-21) plt.figure(2) plt.plot(np.log10(np.abs(fvals)), np.log10(1 / np.sqrt(IP.longweights))) for m in range(-2, 3): plt.figure(1) hlm = lalsim.SphHarmFrequencySeriesGetMode(hlms, 2, m) plt.plot(fvals, np.sqrt(np.abs(fvals)) * np.abs(hlm.data.data), label=m) plt.figure(2) plt.plot(np.log10(np.abs(fvals)), np.log10(np.sqrt(np.abs(fvals)) * np.abs(hlm.data.data)), label=m) plt.show()
def plot_traces(ax, v_ext, dt, mycmap, colormap_range, layers, x_range, y_range, y_values, x_value=-100, get_params=True, final_figs_dir='', ext='.pdf'): #max_time = 10 max_time = cs.pts2ms(v_ext.shape[0], dt) timeline = np.linspace(0, max_time, v_ext.shape[0]) x_value_xaxis = x_value - x_range[0] squeeze = (x_range[1] - x_range[0]) / v_ext.shape[2] x_value_vext = x_value_xaxis / squeeze max_field = np.max(np.abs(v_ext)) if max_field >= 1000: # convert to microvolts v_ext = v_ext / 10e2 # change to microvolts scale_type = 'micro' max_field /= 10e2 else: scale_type = 'nano' scale = int(np.ceil(max_field)) # use smaller scale all_loc = np.zeros(len(y_values)) abs_max_all = np.zeros(len(y_values)) if get_params: time_to_peaks, psp_ampls, idxs = [], [], [] time_to_mins, time_to_maxs, start_times = [], [], [] for idx, y_value in enumerate(y_values): #y_location = (y_value/v_ext.shape[1])*np.diff(y_range)+y_range[0] try: trace = v_ext[:len(timeline), y_value, x_value_vext] except: import pdb pdb.set_trace() #ax1.plot(x_value, y_location, '*', color = colors[idx], ms = 10) loc = (idx + 1) * scale # *0.5 exc abs_max = max(trace.min(), trace.max(), key=abs) # get absolute max element ax.hlines(y=loc, xmin=0, xmax=timeline[-1], color='0.7') # normalize abs_max to between 0 and 1 were ranges are colormap_range mapped_value = normalize_color_value(abs_max, colormap_range) ax.plot(timeline[:], trace + loc, color='k', alpha=0.7, lw=2) ax.plot(timeline[:], trace + loc, color=mycmap(mapped_value), alpha=0.7, lw=2) #'k')#color = colors[idx]) if idx % 5 == 0: ax.text(timeline[-1], loc - 0.8, str(idx), color='0.2') abs_max = max(trace.min(), trace.max(), key=abs) # get absolute max element abs_max_all[idx] = abs_max all_loc[idx] = loc if get_params: # we are here searching for the local mins and maxs and getting the first one which appears as peak of the trace #abs_trace = np.abs(trace) #local_minmax = (np.r_[True, abs_trace[1:] > abs_trace[:-1]] & np.r_[abs_trace[:-1] > abs_trace[1:], True]) #peak_pt = np.where(local_minmax)[0][0] peak_pt = np.argmax(np.abs(trace)) peak_pt_min = np.argmin(trace) peak_pt_max = np.argmax(trace) psp_start = np.where(trace != 0)[0][0] time_to_peak = (peak_pt - psp_start) * dt psp_ampl = trace[peak_pt_max] - trace[peak_pt_min] ax.fill_between(timeline[psp_start:peak_pt], loc, trace[psp_start:peak_pt] + loc, color='w') time_to_peaks.append(time_to_peak) psp_ampls.append(psp_ampl) idxs.append(idx) time_to_mins.append(peak_pt_min * dt) time_to_maxs.append(peak_pt_max * dt) start_times.append(psp_start * dt) #print 'electrode: ', idx, ' t2min:',peak_pt_min*dt,', t2max:', peak_pt_max * dt,', start_t: ',psp_start*dt, ', max ampl: ', trace[peak_pt_max],', min ampl: ', trace[peak_pt_min], 'p2p: ', psp_ampl, 't2argmax:', time_to_peak new_cords = [scale, ((len(y_values)) * scale)] if layers != None: min_electr_possible = translate(0, 4, 95, 0, len(y_values) - 1) max_electr_possible = translate(100, 4, 95, 0, len(y_values) - 1) min_electr_in_space = translate(0, min_electr_possible, max_electr_possible, y_range[0], y_range[1]) max_electr_in_space = translate( len(y_values) - 1, min_electr_possible, max_electr_possible, y_range[0], y_range[1]) y_scale_space = [ translate(lay, min_electr_in_space, max_electr_in_space, new_cords[0], new_cords[-1]) for lay in np.array(layers)[:, 0] ] draw_layers(ax, y_scale_space, timeline) #plt.ylim([-5, scale*25]) plt.ylim(new_cords) ylims = ax.get_ylim() ticks = np.arange(ylims[0], ylims[1] + scale, scale) ticklabels = ['' for idx in ticks] ticklabels[2] = 0 ticklabels[3] = scale plt.yticks(ticks, ticklabels) props = dict(color='black', linewidth=2, markeredgewidth=0.5) ax.axison = False make_yaxis(ax, -0.5, offset=0.7, label_txt=ticklabels, ylims=[ticks[2], ticks[3]], **props) props = dict(color='black', linewidth=2, markeredgewidth=0.5) make_xaxis(ax, yloc=ylims[0], offset=2, **props) plt.title('electrode recordings') ax.set_xlabel('time (ms)') ax.set_xlim([timeline[0], timeline[-1]]) clean_plot(ax) plt.title('x: ' + str(x_value) + 'um, scale: ' + scale_type + 'V') plt.savefig(os.path.join(final_figs_dir, 'trace_' + str(x_value)) + ext) if get_params: plt.figure() ax = plt.subplot(1, 2, 1) plt.plot( start_times, idxs, '*', lw=2, label='start', ) y_axis = v_ext[:, :, x_value_vext] min_all = np.argmin(y_axis, 0) max_all = np.argmax(y_axis, 0) abs_max_all = np.argmax(np.abs(y_axis), 0) #in_electr = np.linspace(0, len(idxs)-1, len(min_all)) # zle ar_space2electr(0, [0, np.size(v_ext, 2)], y_values) #import pdb; pdb.set_trace() #in_electr = np.linspace(ar_space2electr(0,y_values), ar_space2electr(np.size(v_ext,2), y_values), len(min_all)) space_min_in_electr = translate(0., 4., 95., 0., 19.) space_max_in_electr = translate(100., 4., 95., 0., 19.) in_electr = np.linspace(space_min_in_electr, space_max_in_electr, len(min_all)) plt.plot(abs_max_all * dt, in_electr, 'o', lw=3, label='abs max') plt.plot(min_all * dt, in_electr, '.', lw=2, label='min') plt.plot(max_all * dt, in_electr, '.', lw=2, label='max') assert len( np.unique(start_times) ) == 1 # make sure that all the start time of the psp are the same; if not # it's not a problem but different procedure will be needed time_to_largest_peak = abs_max_all * dt - start_times[0] #print str(x_value), 'um : ', time_to_largest_peak, ', max: ', np.max(time_to_largest_peak), ', min:', np.min(time_to_largest_peak) plt.legend() plt.title('x: ' + str(x_value) + 'um' + ',scale: ' + scale_type + 'V') plt.ylabel('electrode number') plt.xlabel('time (ms)') plt.ylim([space_min_in_electr, space_max_in_electr]) if layers != None: np.size(v_ext, 1) y = [ translate(lay, -500, 800, min_electr_possible, max_electr_possible) for lay in np.array(layers)[:, 0] ] y_scale_space #y = np.array(layers)[:,0] #y = ((y-y[0])*(1.0)/(np.array(layers)[-1,-1]-y[0]))*idxs[-1] draw_layers(ax, y, timeline) ax2 = plt.subplot(1, 2, 2) place = np.linspace(0, np.size(y_axis, 1) - 1, np.size(y_axis, 1)) place = place.astype(int) peak_to_peak = y_axis[max_all, place] + y_axis[min_all, place] plt.vlines(0, space_min_in_electr, space_max_in_electr, colors='0.5', linestyle='-') plt.plot(peak_to_peak, in_electr, 'o', lw=2, label='peak_to_peak') plt.title('peak to peak deflection') plt.xlabel('deflection ' + '(' + scale_type + 'V)') plt.ylim([space_min_in_electr, space_max_in_electr]) if layers != None: deflection_axis = np.linspace(np.min(peak_to_peak), np.max(peak_to_peak), len(y)) draw_layers(ax2, y, deflection_axis) plt.savefig( os.path.join(final_figs_dir, 'trace_params' + str(x_value)) + ext) plt.figure() ax = plt.subplot(111) #import pdb; pdb.set_trace() plt.boxplot(np.array(time_to_peaks), patch_artist=True) plt.ylabel('time to absoulte peak (ms)') plt.ylim([0, 4]) plt.savefig( os.path.join(final_figs_dir, 't2peak_boxplot' + str(x_value)) + ext)
def plot_memb_current_for_cell(time_pt, params, plot_morpho=False, plot_field=True, plot_synapses=False, plot_current=False, ax=None): # this is used for frames of the movie v_ext, xx, yy, seg_coords, x_range, y_range, dt = params #v_ext, xx, yy, seg_coords, x_range, y_range, inh_syn_coords, exc_syn_coords, dt = params max_field = np.max(v_ext) if max_field >= 1000: # convert to microvolts v_ext = v_ext / 10e2 # change to microvolts scale_type = 'micro' max_field /= 10e2 else: scale_type = 'nano' #v_ext = v_ext / 10e2 # change nano to micro volts import matplotlib.colors as colors if ax == None: ax = plt.subplot(1, 1, 1) # draw field mycmap, colormap_range_ceil, aspect, one_forth_colormap, colormap_range = helper_provide_imshow_details( v_ext, x_range, y_range) if plot_field: pcm = plt.imshow( v_ext[time_pt, :, :], interpolation="nearest", #norm=colors.SymLogNorm(linthresh=0.01 * np.max(v_ext), # linscale=1.0, # vmin=colormap_range_ceil[0], vmax=colormap_range_ceil[1]), origin='lower', aspect=0.8, extent=(x_range[0], x_range[1], y_range[0], y_range[1]), cmap=mycmap) plt.clim(colormap_range[0], colormap_range[1]) if plot_morpho: # draw morpho import pdb pdb.set_trace() col = graph.plot_neuron(seg_coords, colors='k', autolim=True) soma_idcs, = np.where(seg_coords['name'] == 'soma') draw_soma(ax, x0=seg_coords[soma_idcs[0]]['x0'], x1=seg_coords[soma_idcs[-1]]['x1'], y0=seg_coords[soma_idcs[0]]['y0'], y1=seg_coords[soma_idcs[-1]]['y1'], color='k') plt.xlim(x_range) plt.ylim(y_range) x_tic_label, xtics, y_tic_label, ytics = helper_define_tick_space( x_range, y_range) ax.set_yticks(ytics) ax.set_yticklabels(y_tic_label) ax.set_xticks(xtics) ax.set_xticklabels(x_tic_label) if plot_field: cbar = plt.colorbar(pcm, extend='both', drawedges=False) # ax=ax[0], cbar.set_ticks([ colormap_range[0], -one_forth_colormap, 0, one_forth_colormap, colormap_range[1] ]) cbar.set_ticklabels([ str(colormap_range[0]), str(-one_forth_colormap), '0', str(one_forth_colormap), colormap_range[1] ]) if scale_type == 'micro': cbar.set_label(r"voltage ($\mu$V)", fontsize=18) elif scale_type == 'nano': cbar.set_label(r"voltage (nV)", fontsize=18) #cbar.set_label(r"voltage ($\mu$V)", fontsize=18) #cbar.set_label(r"voltage (nV)", fontsize=18) cbar.ax.tick_params(labelsize=16) if plot_current: # draw streamplots U = -np.diff(v_ext[time_pt, :, :], axis=0)[:, :-1] V = -np.diff(v_ext[time_pt, :, :], axis=1)[:-1, :] plt.streamplot(xx[0, :-1], yy[:-1, 0], V, U, density=1.0, color='g') plt.title('time: ' + str(("%0.2f" % (time_pt * dt))) + 'ms') ax.set_xlabel(r"space ($\mu$m)") ax.set_ylabel(r"space ($\mu$m)") clean_plot(ax) plt.tight_layout() return mycmap, colormap_range
sol = odeint(bala, z0, t) x = sol[:, 0] y = sol[:, 1] plt.plot(x, y, label = f"V = {i} m/s") # Damos las características a nuestro grafico: plt.title("Trayectoria para distintos vientos") plt.xlabel("X (m)") plt.ylabel("Y (m)") plt.grid(True) plt.xlim([0, 150]) plt.ylim([0, 50]) plt.legend() plt.tight_layout() plt.savefig("Entrega 1.png",dpi=500)
def evolve(x0, y0, z0, px0, py0, pz0, aini, da): an = aini x = x0.copy() y = y0.copy() z = z0.copy() px = px0.copy() py = py0.copy() pz = pz0.copy() rhoi = np.zeros([N, N, N]) rho = computeRho(x, y, z, rhoi) ax, ay, az, phi = updateAcceleration(rho, an) gx, gy, gz = gintp(x, y, z, ax, ay, az) g_array = np.array([gx]) phi_array = np.array([phi]) n = 0 while (an + da < (10 * aini)): ax, ay, az, phi = updateAcceleration(rho, an) gx, gy, gz = gintp(x, y, z, ax, ay, az) px += f(an) * gx * da * N / Lbox py += f(an) * gy * da * N / Lbox pz += f(an) * gz * da * N / Lbox x += (px * da * f(an - 0.5 * da + da)) / ((an - 0.5 * da + da)**2) y += (py * da * f(an - 0.5 * da + da)) / ((an - 0.5 * da + da)**2) z += (pz * da * f(an - 0.5 * da + da)) / ((an - 0.5 * da + da)**2) xa, ya, za, pxa, pya, pza, phia, gxa = zeldovich(an, da) x_array = x[:, N / 2, N / 2] px_array = px[:, N / 2, N / 2] xp_analytic = xa[:, N / 2, N / 2] px_analytic = pxa[:, N / 2, N / 2] # plt.figure(figsize=(12,12)) # plt.title(r'Zeldovich') # plt.rc('text', usetex=True) # plt.rc('font',size=16) # plt.rc('xtick.major',pad=5); plt.rc('xtick.minor',pad=5) # plt.rc('ytick.major',pad=5); plt.rc('ytick.minor',pad=5) # # plt.ylim(-3,3); plt.xlim(0,N) # plt.plot(x_array,px_array,'bo',label=r'Numerical') # plt.xlabel('$P_x$',fontsize=16) # plt.ylabel('$x$',fontsize=16) # plt.plot(xp_analytic,px_analytic,c='m',linewidth=2.5,label=r'Analytical') # plt.legend() rho = computeRho(x, y, z, rhoi) an += da n += 1 plt.figure(figsize=(12, 12)) plt.title(r'Zeldovich') plt.rc('text', usetex=True) plt.rc('font', size=16) plt.rc('xtick.major', pad=5) plt.rc('xtick.minor', pad=5) plt.rc('ytick.major', pad=5) plt.rc('ytick.minor', pad=5) plt.ylim(-3, 3) plt.xlim(0, N) plt.plot(x_array, px_array, 'bo', label=r'Numerical') plt.xlabel('$P_x$', fontsize=16) plt.ylabel('$x$', fontsize=16) plt.plot(xp_analytic, px_analytic, c='m', linewidth=2.5, label=r'Analytical') plt.legend() plt.show() return
# 恒等関数 # 入力信号をそのまま出力する活性化関数 # 回帰問題で使用するのが一般的 import numpy as np import matplotlib.pylab as plt def identity_function(x): return x x = np.arange(-5.0, 5.0, 0.1) y = identity_function(x) plt.plot(x, y) plt.ylim(-6.1, 6.1) plt.show()
def main(): np.random.seed(0) #MG = map2grid n = 100 R = np.random.rand(n) T = np.random.rand(n) Y = (1 - R**2)**1.5 * (1 - T**3 / 3) Yerr = np.ones_like(Y) / 3 Y += np.random.randn(n) * Yerr log_trans = lambda x: np.log(np.maximum( x, 0) / .1 + 1), lambda x: np.maximum(np.exp( x) - 1, 1.e-6) * .1, lambda x: 1 / (.1 + np.maximum(0, x) ) #not a real logarithm.. #radial resolution strongly increases computation time MG = map2grid(R, T, Y, Yerr / 10, dt=0.01, nr_new=100) MG.PrepareCalculation(zero_edge=True, transformation=None) MG.PreCalculate() MG.Calculate(0.4, 0.6) subplot(121) plot(MG.g_r[0], MG.g[::10].T, 'r') plot( MG.g_r[::10].T, np.maximum(1 - MG.g_r.T**2, 0)[:, ::10]**1.5 * np.maximum(0, 1 - MG.g_t.T**3 / 3)[:, ::10], 'b--') subplot(122) plot(MG.g_t[:, 0], MG.g[:, ::10], 'r') plot( MG.g_t[:, ::10], np.maximum(1 - MG.g_r**2, 0)[:, ::10]**1.5 * np.maximum(0, 1 - MG.g_t**3 / 3)[:, ::10], 'b--') show() embed() exit() #10, 0.01 #30, 0.089 #100, 0.26 #300, 1.36 #1000, 8 discontinuties = np.load('discontinuties.npz') data = np.load('map2grid.npz') MG = map2grid(data['R'], data['T'], data['Y'], data['Yerr'], data['P'], data['W'], data['nr_new'], data['nt_new']) MG.PrepareCalculation(zero_edge=False, core_discontinuties=[], edge_discontinuties=[], transformation=None, even_fun=True, robust_fit=False, pedestal_rho=None, elm_phase=None) #print(discontinuties['arr_0']) #print(discontinuties['arr_1']) #print(discontinuties['arr_2']) #MG.PrepareCalculation( zero_edge=False) #MG.PrepareCalculation( zero_edge=False, edge_discontinuties = discontinuties['arr_1'], elm_phase = discontinuties['arr_2'].T) MG.PreCalculate() MG.Calculate(0.1, 0.1) exit() import pickle as pkl from matplotlib.pylab import plt with open('data_for_tomas.pkl', 'rb') as f: data = pkl.load(f) out_ne, out_Te = data rhop_ne, ne, ne_err, rhop_ne_err = out_ne rhop_Te, Te, Te_err, rhop_Te_err = out_ne nt = 1 nr = 201 nd = len(ne) MG = map2grid(rhop_Te, np.zeros(nd), Te, Te_err, np.arange(nd), np.ones(nd), nr, nt) MG.r_max = 1.04 transformation = lambda x: np.sqrt(np.maximum( 0, x)), np.square, lambda x: .5 / np.sqrt(np.maximum(1e-5, x)) transformation = lambda x: np.log(np.maximum( x, 0) / .1 + 1), lambda x: np.maximum(np.exp( x) - 1, 1.e-6) * .1, lambda x: 1 / (.1 + np.maximum(0, x) ) #not a real logarithm.. MG.PrepareCalculation(transformation=transformation, zero_edge=False) MG.PreCalculate() MG.Calculate(0.1, 0.1) plt.errorbar(rhop_Te, Te, Te_err, fmt='.') plt.plot(MG.r_new[0], MG.g_samples[:, 0].T, lw=.2, c='r') plt.plot(MG.r_new[0], MG.g[0], lw=2, c='r') plt.xlim(0, 1.1) plt.ylim(0, None) plt.show()
import matplotlib as mpl # 生成样本数为500 ,分类数为5 的数据集 data2 = make_blobs(n_samples=500, centers=5, random_state=8) X2, y2 = data2 clf = KNeighborsClassifier() clf.fit(X2, y2) # 下面的代码用于画图 x_min, x_max = X2[:, 0].min() - 1, X2[:, 0].max() + 1 y_min, y_max = X2[:, 1].min() - 1, X2[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, .02), np.arange(y_min, y_max, .02)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) z = Z.reshape(xx.shape) cm_light = mpl.colors.ListedColormap( ['#A0FFA0', '#FFA0A0', '#448ced', '#6600FF', '#FF00FF']) plt.pcolormesh(xx, yy, z, cmap=cm_light) # 用散点图将数据集进行可视化 plt.scatter(X2[:, 0], X2[:, 1], c=y2, cmap=plt.cm.spring, edgecolor='k') plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.title("Classifier : KNN") plt.show() print(' 代码运行结果') print('==============================') print('模型正确率{:.2f}'.format(clf.score(X2, y2))) print('==============================')
import numpy as np import matplotlib.pylab as plt def u(t): y = t > 0 # numpy数组中的每个元素都与0比较大小,得到一个布尔型numpy数组 return y.astype(np.int) # astype()方法将numpy数组的布尔型转换为int型 t = np.arange(-5.0, 5.0, 0.1) # 生成一个numpy数组,范围是(-5.0,5.0),步长为0.1 y = u(t) plt.plot(t, y) plt.ylim(-0.1, 1.1) plt.show()
import numpy as np import matplotlib.pylab as plt from chapter4.numericalDiff import numerical_diff def function_1(x): # 0.01(x^2) + 0.1x return 0.01 * x**2 + 0.1 * x def numerical_diff_plot(x, func): f = func(x) df = numerical_diff(x, func) r = plt.axis() # range <- [xmin, xmax, ymin, ymax] px = np.linspace(r[0], r[1], 3) # xmin から xmax まで3等分 py = df * (px - x) + f # 直線の方程式 plt.plot(px, py, "g-") if __name__ == "__main__": x = np.arange(0.0, 20.0, 0.1) y = function_1(x) plt.xlabel("x") plt.ylabel("f(x)") plt.ylim(0, 6) plt.plot(x, y, "b-") numerical_diff_plot(10, function_1) plt.show()
import numpy as np import matplotlib.pylab as plt def step_function(x): y = x > 0 return y.astype(np.int) def sigmoid_function(x): return 1 / (1 + np.exp(-x)) #broadcast x = np.arange(-5.0, 5.0, 0.1) y1 = step_function(x) y2 = sigmoid_function(x) plt.plot(x, y1) plt.plot(x, y2) plt.ylim(-0.1, 1.1) #y축의 범위 지정 plt.show()
#plt.show() #only interested in responses to final few stimuli to observe plasticity effects stimulus_times_final = [] for stimulus_time in stimulus_times: stimulus_times_final.append(stimulus_time[-10:]) counts,selective_neuron_ids,significant_spike_count = neuron_correlation(spike_train,time_window,stimulus_times_final,max_id) print "significant spike count: {}".format(significant_spike_count) import matplotlib.pyplot as plt max_count = counts.max() plt.figure() title = "{}ms post-stimulus spike count for AC A1 layer".format(time_window) plt.title(title) plt.xlabel("neuron ID") plt.ylabel("spike count") plt.plot(counts.T) plt.legend(["stimulus 'one'", "stimulus 'two'"]) plt.ylim((0,max_count+1)) #plt.figure() #plt.hist(counts[0]) #plt.figure() #plt.hist(counts[1]) np.save('./selective_ids.npy',selective_neuron_ids) for i in range(len(selective_neuron_ids)): print selective_neuron_ids[i] plt.show()
fileheader = 'DoubPen_LsMsEq1_9p8_ICP1' npz = '.npz' datafile = loadnpzfile(datadir + fileheader + npz) x1 = datafile['x1'] x3 = datafile['x3'] plt.plot(t, x1, color='orange', linestyle='dotted', linewidth=0.75) plt.plot(t, x3, color='blue', linewidth=2.0) ax1.set_xticklabels([]) #plt.vlines(81,0,1,color='red',linestyle='dotted',linewidth=0.5) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.xlabel('Time [s]', fontsize=15) plt.ylabel('Mass 1 x(t)', fontsize=15) plt.xlim(0, 30.0) plt.ylim(-1.1, 1.1) #plt.legend(loc='lower right',fontsize=5,frameon=False,handlelength=5) #savefilename='Timeseries_ICP1.png' #savefile = os.path.normpath(datadir+savefilename) #plt.savefig(savefile,dpi=300,facecolor='w',edgecolor='k') ax1 = plt.subplot(3, 2, 2) lyax = np.log(np.abs(x3 - x1) / 1e-9) plt.plot(t, lyax, color='red') #colors[1,:]) #plt.vlines(81,0,1,color='red',linestyle='dotted',linewidth=0.5) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.xlabel('Time [s]', fontsize=15)
segcount += 1 tracecount += 1 sc = mc.LineCollection(traces, colors=tracecolours, linewidths=1, cmap=cmo.cm.haline, norm=colors.PowerNorm(gamma=0.5)) # get plot limits nodelist = xnodelist nodelist.extend(ynodelist) xmin, xmax, ymin, ymax = fpq.getPlotLimits(nodelist) # plot the traces fig, ax = plt.subplots(figsize=(xSize, ySize)) p = ax.add_collection(sc) plt.xlabel('X, pixels') plt.ylabel('Y, pixels') plt.xlim(xmin, xmax) plt.ylim(ymin, ymax) plt.gca().set_aspect('equal') plt.grid(bGrid) plt.colorbar(sc, label='Trace length, pixels', orientation='horizontal', fraction=0.04) p.set_clim(0, number_of_lines) plt.title('Trace length map, n=%i' % nTraces) plt.savefig("fpqTraceLengthMap.png", dpi=600) print('Plotted %5d traces & lengths' % nTraces)
import numpy as np def step_function(x): y = x > 0 return y.astype(np.int) x = np.arange(-5, 5, 0.1) import matplotlib.pylab as plt def sigmoid(x): return 1 / (1 + np.exp(-x)) y = sigmoid(x) plt.plot(x, y) plt.ylim(-0.1, 1) plt.show() #sigmoid 函数的平滑性对神经网络的学习具有重要意义 #激活函数不能使 用线性函数 #无法发挥多层网络带来的优势。因此,为了发挥叠加层所 带来的优势,激活函数必须使用非线性函数。 def relu(x): return np.maximum(0, x) #更常用的激活函数
import numpy as np import matplotlib.pylab as plt def relu(x): return np.maximum(0, x) x = np.arange(-5.0, 5.0, 0.1) y = relu(x) plt.plot(x, y) plt.ylim(-0.1, 5) plt.show()
import numpy as np import matplotlib.pylab as plt def step_function(x): return np.array(x > 0, dtype=np.int) x = np.arange(-0.5, 0.5, 0.1) y = step_function(x) plt.plot(x, y) plt.ylim(-0.1, 1.1) #範囲 plt.show()
occupancy_grid = OccupancyGrid(occupancy_grid, data['origin'], data['resolution']) # Run RRT. start_node, final_node = rrt(START_POSE, GOAL_POSITION, occupancy_grid) # Plot environment. fig, ax = plt.subplots() occupancy_grid.draw() plt.scatter(.3, .2, s=10, marker='o', color='green', zorder=1000) draw_solution(start_node, final_node) plt.scatter(START_POSE[0], START_POSE[1], s=10, marker='o', color='green', zorder=1000) plt.scatter(GOAL_POSITION[0], GOAL_POSITION[1], s=10, marker='o', color='red', zorder=1000) plt.axis('equal') plt.xlabel('x') plt.ylabel('y') plt.xlim([-.5 - 2., 2. + .5]) plt.ylim([-.5 - 2., 2. + .5]) plt.show()
noise = np.random.randn(5 * batch_size, noise_dim) feed_dict = {prior_input: z, x_input: xin, noise_input: noise} dl, NELBO = sess.run([ratio_loss, nelbo], feed_dict=feed_dict) ISTHISLOSS[indexuu] = dl #print(z_samples) #Plots for i in range(5): plt.subplot(2, 5, i + 1) sns.kdeplot(z_samples[i, :, 0], z_samples[i, :, 1], cmap='Greens') #plt.scatter(z_samples[i,:,0],z_samples[i,:,1]) plt.axis('square') plt.title('q(z|x={})'.format(y[i])) plt.xlim([xmin, xmax]) plt.ylim([xmin, xmax]) plt.xticks([]) plt.yticks([]) plt.subplot(2, 5, 5 + i + 1) plt.contour(xrange, xrange, np.exp(logprior + llh[i]).reshape(300, 300).T, cmap='Greens') plt.axis('square') plt.title('p(z|x={})'.format(y[i])) plt.xlim([xmin, xmax]) plt.ylim([xmin, xmax]) plt.xticks([]) plt.yticks([]) plt.text( -60, 20,
def compute_prior_prob(image_files, width, height, do_plot, pts_in_hull_path, prior_prob_path): """ Compute color prior probabilities for pts in hull Reference: https://github.com/foamliu/Colorful-Image-Colorization/blob/master/class_rebal.py Usage: df_data = pd.read_hdf(os.path.join(data_dir, "preprocessing", "DIV2K", "div2k.hdf5"), "data") list_types = ["'train'"] df_select_data = df_data.query("type in [" + ",".join(list_types) + "]") image_dir = os.path.join(dataset_dir, "DIV2K").replace("\\", "/") image_files = image_dir + "/" + df_select_data["path"].values image_files[0:3], len(image_files) info = dict( image_files = image_files, pts_in_hull_path = os.path.join(data_dir, "colorization_richard_zhang", "pts_in_hull.npy"), prior_prob_path = os.path.join(data_dir, "preprocessing", "DIV2K", "prior_prob_train_div2k.npy"), width = 112, height = 112, do_plot = True ) locals().update(**info) prior_prob = compute_prior_prob(**info) """ # Load ab image X_ab = [] for image_path in tqdm.tqdm(image_files): result = read_image(image_path, is_resize=True, width=width, height=height) X_ab.append(result["res_img_Lab"][:, :, 1:]) # for X_ab = np.array(X_ab) X_ab = X_ab - 128.0 # Load the gamut points location q_ab = np.load(pts_in_hull_path) if do_plot: plt.figure(figsize=(8, 8)) plt.title("ab quantize") gs = gridspec.GridSpec(1, 1) ax = plt.subplot(gs[0]) for i in range(q_ab.shape[0]): ax.scatter(q_ab[:, 0], q_ab[:, 1]) ax.annotate(str(i), (q_ab[i, 0], q_ab[i, 1]), fontsize=6) ax.set_xlim([-110, 110]) ax.set_ylim([-110, 110]) # for # if npts, c, h, w = X_ab.shape X_a_ravel = np.ravel(X_ab[:, :, :, 0]) X_b_ravel = np.ravel(X_ab[:, :, :, 1]) X_ab_ravel = np.vstack((X_a_ravel, X_b_ravel)).T if do_plot: plt.title("Prior Distribution in ab space\n", fontsize=16) plt.hist2d(X_ab_ravel[:, 0], X_ab_ravel[:, 1], bins=100, density=True, norm=LogNorm(), cmap=plt.cm.jet) plt.xlim([-120, 120]) plt.ylim([-120, 120]) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.xlabel("b channel", fontsize=14) plt.ylabel("a channel", fontsize=14) plt.colorbar() plt.show() plt.clf() plt.close() # if # Create nearest neighbord instance with index = q_ab NN = 1 nearest = nn.NearestNeighbors(n_neighbors=NN, algorithm='ball_tree').fit(q_ab) # Find index of nearest neighbor for X_ab dists, ind = nearest.kneighbors(X_ab_ravel) # We now count the number of occurrences of each color ind = np.ravel(ind) counts = np.bincount(ind) idxs = np.nonzero(counts)[0] prior_prob = np.zeros((q_ab.shape[0])) prior_prob[idxs] = counts[idxs] # We turn this into a color probability prior_prob = prior_prob / (1.0 * np.sum(prior_prob)) # Save if prior_prob_path is not None: save_dir = os.path.dirname(prior_prob_path) if save_dir != "" and os.path.exists(save_dir) == False: os.makedirs(save_dir) pts_in_hull_name = os.path.basename(pts_in_hull_path) safe_copy(pts_in_hull_path, os.path.join(save_dir, pts_in_hull_name)) np.save(prior_prob_path, prior_prob) # if if do_plot: plt.hist(prior_prob, bins=100) plt.xlabel("Prior probability") plt.ylabel("Frequency") plt.yscale("log") plt.show() # if return prior_prob pass
def main(): X = np.arange(-5.0, 5.0, 0.1) Y = relu(X) plt.plot(X, Y) plt.ylim(-1.0, 5.5) plt.show()
color='#F97304', linewidth=2.0, label=u'left-handed Néel') plt.plot(nrightnv[ploth][0], nrightnv[ploth][1], color='#97CC04', linewidth=2.0, label=u'right-handed Néel') plt.errorbar(ffcut[0], ffcut[1], yerr=ffcut[2], color='#ED1035', fmt='.', label="data") plt.legend(loc=1, borderaxespad=1, prop={'size': 10}) pylab.ylim([0, 40]) pylab.xlim([0, 2.5]) fp.format_plot(plt, 450, 450, 0, 50) # #pylab.savefig('/Users/alec/UCSB/scan_data/images/noaxes/linecut_'+str(filenum)+'.pdf') # # # #fig = plt.gcf() # #fig.canvas.manager.window.raise_() # # #ploth = 1 # plt.fill_between(blochnv[ploth][0],blochnv[0][1],blochnv[2][1],color='#2D7DD2',alpha=0.5,linewidth=1.0) # #plt.fill_between(nleftnv[ploth][0],nleftnv[0][1],nleftnv[2][1],color='#F97304',alpha=0.5,linewidth=1.0) # plt.fill_between(nrightnv[ploth][0],nrightnv[0][1],nrightnv[2][1],color='#97CC04',alpha=0.5,linewidth=1.0) # plt.xlabel(r'$x \quad (\mu m)$') # plt.ylabel(r'$B_{NV} \quad (G)$') # plt.tight_layout() # #pylab.savefig('/Users/alec/UCSB/scan_data/images/linecut_lrgunc_'+str(filenum)+'.pdf')
x_history = [] for i in range(step_num): x_history.append(x.copy()) grad = numerical_gradient(f, x) x -= lr * grad return x, np.array(x_history) def function_2(x): return x[0]**2 + x[1]**2 init_x = np.array([-3.0, 4.0]) lr = 0.1 step_num = 20 x, x_history = gradient_descent(function_2, init_x, lr=lr, step_num=step_num) plt.plot([-5, 5], [0, 0], '--b') plt.plot([0, 0], [-5, 5], '--b') plt.plot(x_history[:, 0], x_history[:, 1], 'o') plt.xlim(-3.5, 3.5) plt.ylim(-4.5, 4.5) plt.xlabel("X0") plt.ylabel("X1") plt.show()
import matplotlib.pylab as plt import numpy as np def f(x): return x * x - 2 * x - 3 if __name__ == "__main__": x = np.arange(-5, 5, 0.1) y = f(x) plt.plot(x, y) plt.ylim(-4.5, +5) plt.show()
def compute_prior_prob_v1(image_files, is_resize, width, height, do_plot, pts_in_hull_path, prior_prob_path, ab_hist_path): """ Compute color prior probabilities for pts in hull Reference: https://github.com/foamliu/Colorful-Image-Colorization/blob/master/class_rebal.py Usage: df_data = pd.read_hdf(os.path.join(dataset_dir, "DIV2K", "div2k.hdf5"), "data") list_types = ["'train'"] df_select_data = df_data.query("type in [" + ",".join(list_types) + "]") image_dir = os.path.join(dataset_dir, "DIV2K").replace("\\", "/") image_files = image_dir + "/" + df_select_data["path"].values image_files[0:3], len(image_files) info = dict( image_files = image_files, pts_in_hull_path = os.path.join(module_dir, "data", "pts_in_hull.npy"), prior_prob_path = os.path.join(module_dir, "data", "prior_prob_train_div2k.npy"), ab_hist_path = os.path.join(data_dir, "preprocessing", "DIV2K", "ab_hist_train_div2k.npy"), is_resize = False, width = 112, height = 112, do_plot = True ) locals().update(**info) prior_prob = compute_prior_prob(**info) """ # Load ab image ab_hist = np.zeros((256, 256), dtype=np.uint64) for image_path in tqdm.tqdm(image_files): result = read_image(image_path, is_resize=is_resize, width=width, height=height) I_ab = result["res_img_Lab"][:, :, 1:] if is_resize == True else result[ "org_img_Lab"][:, :, 1:] I_ab = I_ab.reshape(-1, 2).astype(np.uint) (ab_vals, ab_cnts) = np.unique(I_ab, return_counts=True, axis=0) ab_hist[ab_vals[:, 0], ab_vals[:, 1]] += ab_cnts.astype(np.uint64) # for # Load the gamut points location q_ab = np.load(pts_in_hull_path) if do_plot: plt.figure(figsize=(8, 8)) gs = gridspec.GridSpec(1, 1) ax = plt.subplot(gs[0]) for i in range(q_ab.shape[0]): ax.scatter(q_ab[:, 0], q_ab[:, 1]) ax.annotate(str(i), (q_ab[i, 0], q_ab[i, 1]), fontsize=6) ax.set_xlim([-110, 110]) ax.set_ylim([-110, 110]) # for plt.title("Prior Distribution in ab space\n", fontsize=16) plt.imshow(ab_hist.transpose(), norm=LogNorm(), cmap=plt.cm.jet, extent=(-128, 127, -128, 127), origin="uper") plt.xlim([-120, 120]) plt.ylim([-120, 120]) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.xlabel("b channel", fontsize=14) plt.ylabel("a channel", fontsize=14) plt.colorbar() plt.show() plt.clf() plt.close() # if X_ab_ravel_h = np.vstack(np.nonzero(ab_hist)).T X_ab_ravel_h = X_ab_ravel_h - 128 # Create nearest neighbord instance with index = q_ab NN = 1 nearest = nn.NearestNeighbors(n_neighbors=NN, algorithm='ball_tree').fit(q_ab) # Find index of nearest neighbor for X_ab dists, ind = nearest.kneighbors(X_ab_ravel_h) # We now count the number of occurrences of each color ind = np.ravel(ind) counts = np.zeros(np.max(ind) + 1, np.uint64) for idx, (a, b) in enumerate(X_ab_ravel_h): counts[ind[idx]] = counts[ind[idx]] + ab_hist[(a + 128, b + 128)] pass # for idxs = np.nonzero(counts)[0] prior_prob = np.zeros((q_ab.shape[0])) prior_prob[idxs] = counts[idxs] # We turn this into a color probability prior_prob = prior_prob / (1.0 * np.sum(prior_prob)) # Save if prior_prob_path is not None: save_dir = os.path.dirname(prior_prob_path) if save_dir != "" and os.path.exists(save_dir) == False: os.makedirs(save_dir) np.save(prior_prob_path, prior_prob) # if # Save if ab_hist_path is not None: save_dir = os.path.dirname(ab_hist_path) if save_dir != "" and os.path.exists(save_dir) == False: os.makedirs(save_dir) np.save(ab_hist_path, ab_hist) # if if do_plot: plt.hist(prior_prob, bins=100) plt.xlabel("Prior probability") plt.ylabel("Frequency") plt.yscale("log") plt.show() # if return prior_prob, ab_hist pass
def plot_ROC_curves(sample, y_true, y_prob, ROC_type, output_dir): LLH_fpr, LLH_tpr = LLH_rates(sample, y_true) fpr, tpr, threshold = metrics.roc_curve(y_true, y_prob[:, 0], pos_label=0) signal_ratio = np.sum(y_true == 0) / len(y_true) accuracy = tpr * signal_ratio + (1 - fpr) * (1 - signal_ratio) best_tpr, best_fpr = tpr[np.argmax(accuracy)], fpr[np.argmax(accuracy)] colors = ['red', 'blue', 'green'] labels = ['LLH tight', 'LLH medium', 'LLH loose'] sig_eff, bkg_eff = '$\epsilon_{\operatorname{sig}}$', '$\epsilon_{\operatorname{bkg}}$' plt.figure(figsize=(12, 8)) pylab.grid(True) axes = plt.gca() if ROC_type == 1: plt.xlim([0.6, 1]) plt.ylim([0.9, 1 - 1e-4]) plt.xticks([0.6, 0.7, 0.8, 0.9, 1], [60, 70, 80, 90, 100]) plt.yscale('logit') plt.yticks([0.9, 0.99, 0.999, 0.9999], [90, 99, 99.9, 99.99]) axes.xaxis.set_minor_locator(AutoMinorLocator(10)) axes.xaxis.set_minor_formatter(plt.NullFormatter()) axes.yaxis.set_minor_formatter(plt.NullFormatter()) plt.xlabel('Signal Efficiency ' + sig_eff + ' (%)', fontsize=25) plt.ylabel('Background Rejection $1\!-\!$' + bkg_eff + ' (%)', fontsize=25) plt.text(0.8, 0.67, 'AUC: ' + format(metrics.auc(fpr, tpr), '.4f'), { 'color': 'black', 'fontsize': 22 }, va='center', ha='center', transform=axes.transAxes) val = plt.plot(tpr, (1 - fpr), label='Signal vs Background', color='#1f77b4', lw=2) plt.scatter(best_tpr, (1 - best_fpr), s=40, marker='o', c=val[0].get_color(), label="{0:<16s} {1:>3.2f}%".format('Best Accuracy:', 100 * max(accuracy))) for LLH in zip(LLH_tpr, LLH_fpr, colors, labels): plt.scatter(LLH[0], 1 - LLH[1], s=40, marker='o', c=LLH[2], label='(' + format(100 * LLH[0], '.1f') + '%, ' + format(100 * (1 - LLH[1]), '.1f') + ')' + r'$\rightarrow$' + LLH[3]) plt.legend(loc='upper right', fontsize=15, numpoints=3) if ROC_type == 2: pylab.grid(False) len_0 = np.sum(fpr == 0) x_min = min(60, 10 * np.floor(10 * LLH_tpr[0])) if fpr[np.argwhere(tpr >= x_min / 100)[0]] != 0: y_max = 100 * np.ceil( max(1 / fpr[np.argwhere(tpr >= x_min / 100)[0]], 1 / LLH_fpr[0]) / 100) else: y_max = 10000 plt.xlim([x_min, 100]) plt.ylim([1, y_max]) axes.xaxis.set_major_locator(MultipleLocator(10)) axes.xaxis.set_minor_locator(AutoMinorLocator(10)) axes.yaxis.set_minor_locator(AutoMinorLocator(5)) LLH_scores = [ 1 / fpr[np.argwhere(tpr >= value)[0]] for value in LLH_tpr ] for n in np.arange(len(LLH_scores)): axes.axhline(LLH_scores[n], xmin=(LLH_tpr[n] - x_min / 100) / (1 - x_min / 100), xmax=1, ls='--', linewidth=0.5, color='#1f77b4') axes.axvline( 100 * LLH_tpr[n], ymin=abs(1 / LLH_fpr[n] - 1) / (plt.yticks()[0][-1] - 1), ymax=abs(LLH_scores[n] - 1) / (plt.yticks()[0][-1] - 1), ls='--', linewidth=0.5, color='tab:blue') for val in LLH_scores: plt.text(100.2, val, str(int(val)), { 'color': '#1f77b4', 'fontsize': 10 }, va="center", ha="left") axes.yaxis.set_ticks(np.append([1], plt.yticks()[0][1:])) plt.xlabel('Signal Efficiency ' + sig_eff + ' (%)', fontsize=25) plt.ylabel('Background Rejection 1/' + bkg_eff, fontsize=25) val = plt.plot(100 * tpr[len_0:], 1 / fpr[len_0:], label='Signal vs Background', color='#1f77b4', lw=2) if best_fpr != 0: plt.scatter(100 * best_tpr, 1 / best_fpr, s=40, marker='o', c=val[0].get_color(), label="{0:<15s} {1:>3.2f}%".format( 'Best Accuracy:', 100 * max(accuracy)), zorder=10) for LLH in zip(LLH_tpr, LLH_fpr, colors, labels): plt.scatter(100 * LLH[0], 1 / LLH[1], s=40, marker='o', c=LLH[2], label='(' + format(100 * LLH[0], '.1f') + '%, ' + str(format(1 / LLH[1], '>3.0f')) + ')' + r'$\rightarrow$' + LLH[3]) plt.legend(loc='upper right', fontsize=15, numpoints=3) if ROC_type == 3: def make_plot(location): plt.xlabel('Signal Probability as Threshold (%)', fontsize=25) plt.ylabel('(%)', fontsize=25) val_1 = plt.plot(threshold[1:], tpr[1:], color='tab:blue', label='Signal Efficiency', lw=2) val_2 = plt.plot(threshold[1:], 1 - fpr[1:], color='tab:orange', label='Background Rejection', lw=2) val_3 = plt.plot(threshold[1:], accuracy[1:], color='black', label='Accuracy', zorder=10, lw=2) for LLH in zip(LLH_tpr, LLH_fpr): p1 = plt.scatter(threshold[np.argwhere(tpr >= LLH[0])[0]], LLH[0], s=40, marker='o', c=val_1[0].get_color()) p2 = plt.scatter(threshold[np.argwhere(tpr >= LLH[0])[0]], 1 - LLH[1], s=40, marker='o', c=val_2[0].get_color()) l1 = plt.legend([p1, p2], ['LLH ' + sig_eff, 'LLH $1\!-\!$' + bkg_eff], loc='lower left', fontsize=13) #std_accuracy = valid_accuracy(y_true, y_prob) #plt.scatter( 0.5, std_accuracy, s=30, marker='D', c=val_3[0].get_color(), # label="{0:<10s} {1:>5.2f}%".format('Standard Accuracy:', std_accuracy), zorder=10 ) plt.scatter(best_threshold, max(accuracy), s=40, marker='o', c=val_3[0].get_color(), label='{0:<10s} {1:>5.2f}%'.format( 'Best Accuracy:', 100 * max(accuracy)), zorder=10) plt.legend(loc=location, fontsize=15, numpoints=3) plt.gca().add_artist(l1) best_threshold = threshold[np.argmax(accuracy)] plt.figure(figsize=(12, 16)) plt.subplot(2, 1, 1) pylab.grid(True) axes = plt.gca() plt.xlim([0, 1]) plt.xticks(np.arange(0, 1.01, 0.1), np.arange(0, 101, 10)) plt.ylim([0.6, 1]) plt.yticks(np.arange(0.6, 1.01, 0.05), np.arange(60, 101, 5)) make_plot('lower center') plt.subplot(2, 1, 2) pylab.grid(True) axes = plt.gca() x_min = -2 x_max = 3 y_min = 0.1 y_max = 1 - 1e-4 pylab.ylim(y_min, y_max) pylab.xlim(10**x_min, 1 - 10**(-x_max)) pos = [10**float(n) for n in np.arange(x_min, 0)] pos += [0.5 ] + [1 - 10**float(n) for n in np.arange(-1, -x_max - 1, -1)] lab = ['0.' + n * '0' + '1' for n in np.arange(abs(x_min) - 3, -1, -1)] lab += [1, 10, 50, 90, 99 ] + ['99.' + n * '9' for n in np.arange(1, x_max - 1)] plt.xscale('logit') plt.xticks(pos, lab) plt.yscale('logit') plt.yticks([0.1, 0.5, 0.9, 0.99, 0.999, 0.9999], [10, 50, 90, 99, 99.9, 99.99]) axes.xaxis.set_minor_formatter(plt.NullFormatter()) axes.yaxis.set_minor_formatter(plt.NullFormatter()) make_plot('upper center') if ROC_type == 4: best_tpr = tpr[np.argmax(accuracy)] plt.xlim([60, 100.0]) plt.ylim([80, 100.0]) plt.xticks(np.arange(60, 101, step=5)) plt.yticks(np.arange(80, 101, step=5)) plt.xlabel('Signal Efficiency (%)', fontsize=25) plt.ylabel('(%)', fontsize=25) plt.plot(100 * tpr[1:], 100 * (1 - fpr[1:]), label='Background rejection', color='darkorange', lw=2) val = plt.plot(100 * tpr[1:], 100 * accuracy[1:], label='Accuracy', color='black', lw=2, zorder=10) plt.scatter(100 * best_tpr, 100 * max(accuracy), s=40, marker='o', c=val[0].get_color(), label="{0:<10s} {1:>5.2f}%".format('Best Accuracy:', 100 * max(accuracy)), zorder=10) plt.legend(loc='lower center', fontsize=15, numpoints=3) file_name = output_dir + '/ROC' + str(ROC_type) + '_curve.png' print('Saving test sample ROC' + str(ROC_type) + ' curve to: ', file_name) plt.savefig(file_name)
bins=timeBins) madeAttemptsAsFunctionOfTime, b = np.histogram( train.ix[train['shot_made_flag'] == 1, 'secondsFromGameStart'], bins=timeBins) accuracyAsFunctionOfTime = madeAttemptsAsFunctionOfTime.astype( float) / attemptsAsFunctionOfTime #accuracyAsFunctionOfTime[attemptsAsFunctionOfTime <= 50] = 0 # zero accuracy in bins that don't have enough samples maxHeight = max(attemptsAsFunctionOfTime) + 30 barWidth = 0.999 * (timeBins[1] - timeBins[0]) plt.figure() plt.subplot(2, 1, 1) plt.bar(timeBins[:-1], attemptsAsFunctionOfTime, align='edge', width=barWidth) plt.xlim((-20, 3200)) plt.ylim((0, maxHeight)) plt.ylabel('attempts') plt.title(str(binSizeInSeconds) + ' second time bins') plt.vlines(x=[ 0, 12 * 60, 2 * 12 * 60, 3 * 12 * 60, 4 * 12 * 60, 4 * 12 * 60 + 5 * 60, 4 * 12 * 60 + 2 * 5 * 60, 4 * 12 * 60 + 3 * 5 * 60 ], ymin=0, ymax=maxHeight, colors='r') plt.subplot(2, 1, 2) plt.bar(timeBins[:-1], accuracyAsFunctionOfTime, align='edge', width=barWidth) plt.xlim((-20, 3200)) plt.ylabel('accuracy') plt.xlabel('time [seconds from start of game]') plt.vlines(x=[
def plot_distributions_DG(sample, y_true, y_prob, output_dir, separation=False, bkg='bkg'): label_dict = { 0: 'iso electron', 1: 'charge flip', 2: 'photon conversion', 3: 'b/c hadron', 4: 'light flavor ($\gamma$/e$^\pm$)', 5: 'light flavor (hadron)', 'bkg': 'background' } color_dict = { 0: 'tab:blue', 1: 'tab:orange', 2: 'tab:green', 3: 'tab:red', 4: 'tab:purple', 5: 'tab:brown', 'bkg': 'tab:orange' } if y_prob.shape[1] > 2 or separation: label_dict.pop('bkg') else: label_dict = { 0: 'iso electron', 1: label_dict[bkg] } color_dict = { 0: 'tab:blue', 1: color_dict[bkg] } n_classes = len(label_dict) def logit(x, delta=1e-16): x = np.float64(x) x = np.minimum(x, 1 - delta) x = np.maximum(x, delta) return np.log10(x) - np.log10(1 - x) def print_JSD(P, Q, idx, color, text): plt.text(0.945, 1.01 - 3 * idx / 100, 'JSD$_{0,\!' + text + '}$:', { 'color': 'black', 'fontsize': 10 }, va='center', ha='right', transform=axes.transAxes) plt.text(0.990, 1.01 - 3 * idx / 100, format(distance.jensenshannon(P, Q), '.3f'), { 'color': color, 'fontsize': 10 }, va='center', ha='right', transform=axes.transAxes) def class_histo(y_true, y_prob, bins, colors): h = np.full((len(bins) - 1, n_classes), 0.) from utils import make_labels class_labels = make_labels(sample, n_classes) for n in np.arange(n_classes): class_probs = y_prob[:, 0][class_labels == n] class_weights = len(class_probs) * [ 100 / len(y_true) ] #len(class_probs)*[100/len(class_probs)] h[:, n] = pylab.hist(class_probs, bins=bins, label='class ' + str(n) + ': ' + label_dict[n], histtype='step', weights=class_weights, log=True, color=colors[n], lw=2)[0] if n_classes == 2: colors = len(colors) * ['black'] if True: for n in np.arange(1, n_classes): new_y_true = y_true[np.logical_or(y_true == 0, class_labels == n)] new_y_prob = y_prob[np.logical_or(y_true == 0, class_labels == n)] fpr, tpr, threshold = metrics.roc_curve(new_y_true, new_y_prob[:, 0], pos_label=0) axes.axvline(threshold[np.argmax(tpr - fpr)], ymin=0, ymax=1, ls='--', lw=1, color=colors[n]) for n in np.arange(1, n_classes): print_JSD(h[:, 0], h[:, n], n, colors[n], str(n)) if n_classes > 2: print_JSD(h[:, 0], np.sum(h[:, 1:], axis=1), n_classes, 'black', '\mathrm{bkg}') plt.figure(figsize=(12, 16)) plt.subplot(2, 1, 1) pylab.grid(True) axes = plt.gca() pylab.xlim(0, 100) pylab.ylim(1e-5 if n_classes > 2 else 1e-5, 1e2) plt.xticks(np.arange(0, 101, step=10)) #pylab.xlim(0,10); pylab.ylim(1e-2 if n_classes>2 else 1e-2, 1e2) #plt.xticks(np.arange(0,11,step=1)) bin_step = 0.5 bins = np.arange(0, 100 + bin_step, bin_step) class_histo(y_true, 100 * y_prob, bins, color_dict) plt.xlabel('Signal Probability (%)', fontsize=25) plt.ylabel('Distribution (% per ' + str(bin_step) + ' % bin)', fontsize=25) plt.legend(loc='upper center', fontsize=16 if n_classes == 2 else 14, numpoints=3) plt.subplot(2, 1, 2) pylab.grid(True) axes = plt.gca() x_min = -10 x_max = 6 pylab.xlim(x_min, x_max) pylab.ylim(1e-4 if n_classes > 2 else 1e-4, 1e1) pos = [10**float(n) for n in np.arange(x_min, 0)] pos += [0.5] + [1 - 10**float(n) for n in np.arange(-1, -x_max - 1, -1)] lab = ['$10^{' + str(n) + '}$' for n in np.arange(x_min + 2, 0)] lab += [1, 10, 50, 90, 99 ] + ['99.' + n * '9' for n in np.arange(1, x_max - 1)] #x_min=-10; x_max=-1; pylab.xlim(x_min, x_max); pylab.ylim(1e-2 if n_classes>2 else 1e-4, 1e2) #pos = [ 10**float(n) for n in np.arange(x_min,0) ] #lab = ['$10^{'+str(n)+'}$' for n in np.arange(x_min+2,0) ] + [1,10] #lab += ['0.50 '] + ['$1\!-\!10^{'+str(n)+'}$' for n in np.arange(-1,-x_max-1,-1)] plt.xticks(logit(np.array(pos)), lab, rotation=15) bin_step = 0.1 bins = np.arange(x_min - 1, x_max + 1, bin_step) y_prob[:, 0] = logit(y_prob[:, 0]) class_histo(y_true, y_prob, bins, color_dict) plt.xlabel('Signal Probability (%)', fontsize=25) plt.ylabel('Distribution (% per bin ' + str(bin_step) + ')', fontsize=25) location = 'upper left' if n_classes == 2 else 'upper left' plt.legend(loc=location, fontsize=16 if n_classes == 2 else 14, numpoints=3) plt.subplots_adjust(top=0.95, bottom=0.1, hspace=0.2) file_name = output_dir + '/distributions.png' print('Saving test sample distributions to:', file_name) plt.savefig(file_name)
import matplotlib.pylab as plt import numpy as np import pandas as pd from scipy.stats import norm import seaborn as sns from math import cos, sin, log, tan, gamma, pi, exp, sqrt, cosh, sinh from cmath import exp as cexp, log as clog f = lambda x: x / 64 + x * x * x / 50 N = 12000 S = np.cumsum([cexp(f(n) * complex(0, 2 * pi)) for n in range(1, N)]) p = plt.figure(figsize=(14, 14), facecolor='black', dpi=100) p = plt.axis('off') for k in range(330): plt.xlim(-2.5, 1.5) plt.ylim(-1, 3) plt.plot([S[i].real for i in range(N - 1)][k * 5:(k + 1) * 5 + 1], [S[i].imag for i in range(N - 1)][k * 5:(k + 1) * 5 + 1], lw=1, color=plt.cm.spring(k / 300), alpha=0.8) p = plt.savefig( f'C:/Users/Alejandro/Pictures/RandomPlots/01032020/plot{k}.PNG', facecolor='black')