def pr_plot(y, preds, fname=None, figsize=(8, 6), verbose=True): """ Plot a precision-recall curve and compute the AUC. :param y: numpy 1-d array of true target data :param preds: numpy 1-d array of target predictions :param fname: filename of saved plot, default is None (do not save) :param figsize: (width in, height in) tuple :param verbose: boolean, if True write precision-recall plot AUC to logger :return: None """ precision, recall, thresh = precision_recall_curve(y, probas_pred=preds) pr_auc = auc(recall, precision) fig = plt.figure(figsize=figsize) plt.step(recall, precision, color='b', alpha=0.2, where='post') plt.fill_between(recall, precision, step='post', alpha=0.2, color='b') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title('Precision-recall curve ---- AUC = {:6.4f}'.format(pr_auc)) if fname: plt.savefig(fname) if verbose: logging.info('Precision-recall AUC = {:6.4f}'.format(pr_auc))
def test_sparse_fused_lasso(n=500,l1=2.,ratio=1.,control=control): D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:] D = np.vstack([D, ratio*np.identity(n)]) M = np.linalg.eigvalsh(np.dot(D.T, D)).max() Y = np.random.standard_normal(n) Y[int(0.1*n):int(0.3*n)] += 3. p1 = sapprox.signal_approximator((D, Y),L=M) p1.assign_penalty(l1=l1) t1 = time.time() opt1 = regreg.FISTA(p1) opt1.fit(tol=control['tol'], max_its=control['max_its']) beta1 = opt1.problem.coefs t2 = time.time() ts1 = t2-t1 beta, _ = opt1.output X = np.arange(n) if control['plot']: pylab.clf() pylab.step(X, beta, linewidth=3, c='red') pylab.scatter(X, Y) pylab.show()
def plot(self, ylog10scale = False, timescale = "years", year = 25): """ Generate figure and axis for the population structure timescale choose from "2N0", "4N0", "generation" or "years" """ time = self.Time pop = self.pop for i in range(1,len(self.pop)): if type(pop[i]) == type(""): # ignore migration commands, and replace by (unchanged) pop size pop[i] = pop[i-1] if time[0] != 0 : time.insert(0, float(0)) pop.insert(0, float(1)) if timescale == "years": time = [ti * 4 * self.scaling_N0 * year for ti in time ] pl.xlabel("Time (years, "+`year`+" years per generation)", fontsize=20) #pl.xlabel("Years") elif timescale == "generation": time = [ti * 4 * self.scaling_N0 for ti in time ] pl.xlabel("Generations)") elif timescale == "4N0": time = [ti*1 for ti in time ] pl.xlabel("Time (4N generations)") elif timescale == "2N0": time = [ti*2 for ti in time ] pl.xlabel("Time (2N generations)") else: print "timescale must be one of \"4N0\", \"generation\", or \"years\"" return time[0] = time[1] / float(20) time.append(time[-1] * 2) yaxis_scaler = 10000 pop = [popi * self.scaling_N0 / float(yaxis_scaler) for popi in pop ] pop.insert(0, pop[0]) pl.xscale ('log', basex = 10) #pl.xlim(min(time), max(time)) pl.xlim(1e3, 1e7) if ylog10scale: pl.ylim(0.06, 10000) pl.yscale ('log', basey = 10) else: pl.ylim(0, max(pop)+2) pl.ylim(0,5) pl.tick_params(labelsize=20) #pl.step(time, pop , color = "blue", linewidth=5.0) pl.step(time, pop , color = "red", linewidth=5.0) pl.grid() #pl.step(time, pop , color = "black", linewidth=5.0) #pl.title ( self.case + " population structure" ) #pl.ylabel("Pop size ($*$ "+`yaxis_scaler` +")") pl.ylabel("Effective population size",fontsize=20 )
def observe_maihara_region_custom(combos=[(3200, .3, "k")], labels=[""]): pl.figure(1) for combo in combos: R, frac, fmt = combo lp, sp, spl, prof_l, prof = to_observed(ll, ss, 16000 / R * AA, N=125 * 512 * frac * 3, nsamp=2.8) pl.step(lp, -2.5 * np.log10(np.abs(spl)), fmt) #pl.step(lp, spl) pl.ylim(5, -2) #pl.ylim(-0.1, 0.5) pl.legend(labels) pl.xlim(16400, 16900) pl.axvline(16600, color='red') pl.axvline(16700, color='red') pl.axhline(3 + .75, color='black') pl.grid(True) pl.xlabel(r"Wavelength [$\rm \AA$]") pl.ylabel(r"$F_\lambda$ [magnitude]")
def plot_attenuation_sim(rmArray, f, days_in_season=82, n_iter=1000, label=''): N = days_in_season l2 = (0.3 / f) ** 2 hist, bins = np.histogram(rm, bins=np.arange(min(rmArray), max(rmArray) + bin_width, bin_width)) bin_midpoints = bins[:-1] + np.diff(bins) / 2 cdf = np.cumsum(hist) cdf = cdf / cdf[-1] values = np.random.rand(n_iter) value_bins = np.searchsorted(cdf, values) random_from_cdf = bin_midpoints[value_bins] factors = [] for i, RM in enumerate(random_from_cdf): atten = 0 for j in range(i + 1, N, 1): atten += np.cos(2 * (random_from_cdf[j] - RM) * l2) factor = (float(N) + (2. * atten)) / pow(float(N), 2.) factors.append(factor) Pv, v = np.histogram(factors, bins=10, density=True) print('Epsilon (sim) =', np.mean(factors), '+/-', np.std(factors)) v = 0.5 * (v[1:] + v[:-1]) pylab.step(v, Pv, label=label) return [np.mean(factors), np.std(factors)]
def measure_flexure_y(fine, HDUlist, profwidth=5, plot=False, outname=None): dat = HDUlist[0].data exptime = HDUlist[0].header["EXPTIME"] profs = [] xx = np.arange(profwidth * 2) for ix in np.arange(500, 1200, 10): f = fine[ix] profile = np.zeros(profwidth * 2) # bad fit if not f.ok: continue # noisy fit if f.lamnrms > 1: continue # short spectrum if f.xrange[1] - f.xrange[0] < 200: continue yfun = np.poly1d(f.poly) for xpos in np.arange(f.xrange[0], f.xrange[1]): try: ypos = int(np.round(yfun(xpos))) except: continue try: profile += dat[ypos - profwidth : ypos + profwidth, xpos] except: continue profs.append(profile - np.min(profile)) if plot: pl.figure(1) ffun = FF.mpfit_residuals(FF.gaussian4) parinfo = [{"value": 1}, {"value": profwidth}, {"value": 2}, {"value": 0}, {"value": 0}] profposys = [] for prof in profs: if plot: pl.step(xx, prof) parinfo[0]["value"] = np.max(prof) fit = FF.mpfit_do(ffun, xx, prof, parinfo) if plot: pl.plot(xx, FF.gaussian4(fit.params, xx)) profposys.append(fit.params[1] - profwidth - 1) if plot: pl.show() profposys = np.array(profposys) mn = np.mean(profposys) sd = np.std(profposys) ok = np.abs(profposys - mn) / sd < 3 required_shift = np.mean(profposys[ok]) print "dY = %3.2f pixel shift" % required_shift return required_shift
def _cumdist(sample, annotation='', color='k'): order = np.argsort(sample['giso_insol'].values)[::-1] w = np.cumsum(sample['weight'].values[order]) / num_stars n = np.array(range(len(sample['weight'].values))) / float(len(sample)) f = sample['giso_insol'].values[order] pl.step(f, n, 'k-', lw=2, linestyle='dashed', alpha=0.25, color=color, label=None) pl.step(f, w / np.max(w), 'k-', lw=2, color=color) aloc = (0.1, 0.85) #pl.annotate(annotation, xy=aloc, xycoords='axes fraction', fontsize=20, # horizontalalignment='left') pl.semilogx() pl.xlim(2000, 30) pl.ylim(0, 1) ax = pl.gca() ax.xaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter()) ax.xaxis.set_major_formatter( matplotlib.ticker.FormatStrFormatter('%0.0f')) ax.xaxis.set_ticks([30, 100, 300, 1000, 3000]) pl.xlabel('Stellar light intensity relative to Earth (S)') # pl.ylabel('cumulative fraction of planet detections') pl.ylabel(r'Fraction of planets with S$_{\rm inc} < S$')
def test_sparse_fused_lasso(n=500, l1=2., ratio=1., control=control): D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:] D = np.vstack([D, ratio * np.identity(n)]) M = np.linalg.eigvalsh(np.dot(D.T, D)).max() Y = np.random.standard_normal(n) Y[int(0.1 * n):int(0.3 * n)] += 3. p1 = sapprox.signal_approximator((D, Y), L=M) p1.assign_penalty(l1=l1) t1 = time.time() opt1 = regreg.FISTA(p1) opt1.fit(tol=control['tol'], max_its=control['max_its']) beta1 = opt1.problem.coefs t2 = time.time() ts1 = t2 - t1 beta, _ = opt1.output X = np.arange(n) if control['plot']: pylab.clf() pylab.step(X, beta, linewidth=3, c='red') pylab.scatter(X, Y) pylab.show()
def test_metropolis(Nsamp=int(1e4), Nburnin=0, sampleSig=5, thetaInit=-5): from scipy import stats import pylab as plt def testfunc(theta): return (stats.cauchy(-10, 2).pdf(theta) + 4 * stats.cauchy(10, 4).pdf(theta)) def lnp(theta): """ interface to metrop """ return (0, np.log10(testfunc(theta))) x = np.linspace(-50, 50, 1e4) y = testfunc(x) # compute normalization function: used later to put on same as histogram Zfunc = np.sum(y * x.ptp() / len(x)) plt.plot(x, y / Zfunc, 'k-', lw=2) plt.xlabel('theta') plt.ylabel('f') lnps, samples = metropolis(lnp, [thetaInit], Nburnin=Nburnin, Nsamp=Nsamp, sampleCov=sampleSig ** 2) n, b = np.histogram(samples.ravel(), bins=np.arange(-50, 50, 1), normed=False) Zhist = (n * np.diff(b)).sum() plt.step(b[1:], n.astype(float) / Zhist, color='r') plt.title('test metropolis')
def test_metropolis(Nsamp=int(1e4), Nburnin=0, sampleSig=5, thetaInit=-5): from scipy import stats import pylab as plt def testfunc(theta): return (stats.cauchy(-10, 2).pdf(theta) + 4 * stats.cauchy(10, 4).pdf(theta)) def lnp(theta): """ interface to metrop """ return (0, np.log10(testfunc(theta))) x = np.linspace(-50, 50, 1e4) y = testfunc(x) # compute normalization function: used later to put on same as histogram Zfunc = np.sum(y * x.ptp() / len(x)) plt.plot(x, y / Zfunc, 'k-', lw=2) plt.xlabel('theta') plt.ylabel('f') lnps, samples = metropolis(lnp, [thetaInit], Nburnin=Nburnin, Nsamp=Nsamp, sampleCov=sampleSig**2) n, b = np.histogram(samples.ravel(), bins=np.arange(-50, 50, 1), normed=False) Zhist = (n * np.diff(b)).sum() plt.step(b[1:], n.astype(float) / Zhist, color='r') plt.title('test metropolis')
def psmc_figure(cum_change, tmrca, position, prefix): X, Y, Z, site, time = psmc_XYZ(prefix) fig = pl.figure(figsize=(24, 7)) pl.pcolor(X, Y, Z, vmin=0, vmax=0.15) my_axes = fig.gca() #ylabels = ["%.4g" % (float(y)*2 * default_pop_size) for y in my_axes.get_yticks()] ylabels = ["%.4g" % (float(y)) for y in my_axes.get_yticks()] my_axes.set_yticklabels(ylabels) #pl.colorbar() pl.step( cum_change, [x * 2 for x in tmrca], color="red", linewidth=15.0 ) # becasue x is generated by ms, which is scaled to 4N0, psmc scale to 2N0. Thereofore, needs to multiply by 2 #pl.step(cum_change, [x for x in tmrca] , color = "red", linewidth=5.0) pl.plot(position, [0.9 * max(time)] * len(position), "wo", markersize=15) pl.axis([0, max(site), 0, max(time)]) pl.title("PSMC heat map", fontsize=25) pl.xlabel("Sequence base", fontsize=20) #pl.ylabel("TMRCA (generation)") pl.ylabel("TMRCA (2N0)", fontsize=20) #my_axes.set_yticklabels([]) #my_axes.set_xticklabels([]) pl.savefig(prefix + "psmc_heat" + ".png", transparent=True) print "done" pl.close()
def createPlot(dataY, dataX, ticksX, annotations, axisY, axisX, dostep, doannotate): if not ticksX: ticksX = dataX if dostep: py.step(dataX, dataY, where='post', linestyle='-', label=axisY) # where=post steps after point else: py.plot(dataX, dataY, marker='o', ms=5.0, linestyle='-', label=axisY) if annotations and doannotate: for note, x, y in zip(annotations, dataX, dataY): py.annotate(note, (x, y), xytext=(2,2), xycoords='data', textcoords='offset points') py.xticks(np.arange(1, len(dataX)+1), ticksX, horizontalalignment='left', rotation=30) leg = py.legend() leg.draggable() py.xlabel(axisX) py.ylabel('time (s)') # Set X axis tick labels as rungs #print zip(dataX, dataY) py.draw() py.show() return
def OnZPlotProfile(self, event): x, p, d, pi = self.vp.GetProfile(50, background=[7, 7]) pylab.figure(1) pylab.clf() pylab.step(x, p) pylab.step(x, 10 * d - 30) pylab.ylim(-35, pylab.ylim()[1]) pylab.xlim(x.min(), x.max()) pylab.xlabel('Time [%3.2f ms frames]' % (1e3 * self.mdh.getEntry('Camera.CycleTime'))) pylab.ylabel('Intensity [counts]') fr = self.fitResults[pi] if not len(fr) == 0: pylab.figure(2) pylab.clf() pylab.subplot(211) pylab.errorbar( fr['tIndex'], fr['fitResults']['x0'] - self.vp.do.xp * 1e3 * self.mdh.getEntry('voxelsize.x'), fr['fitError']['x0'], fmt='xb') pylab.xlim(x.min(), x.max()) pylab.xlabel('Time [%3.2f ms frames]' % (1e3 * self.mdh.getEntry('Camera.CycleTime'))) pylab.ylabel('x offset [nm]') pylab.subplot(212) pylab.errorbar( fr['tIndex'], fr['fitResults']['y0'] - self.vp.do.yp * 1e3 * self.mdh.getEntry('voxelsize.y'), fr['fitError']['y0'], fmt='xg') pylab.xlim(x.min(), x.max()) pylab.xlabel('Time [%3.2f ms frames]' % (1e3 * self.mdh.getEntry('Camera.CycleTime'))) pylab.ylabel('y offset [nm]') pylab.figure(3) pylab.clf() pylab.errorbar( fr['fitResults']['x0'] - self.vp.do.xp * 1e3 * self.mdh.getEntry('voxelsize.x'), fr['fitResults']['y0'] - self.vp.do.yp * 1e3 * self.mdh.getEntry('voxelsize.y'), fr['fitError']['x0'], fr['fitError']['y0'], fmt='xb') #pylab.xlim(x.min(), x.max()) pylab.xlabel('x offset [nm]') pylab.ylabel('y offset [nm]')
def plot_t(self): import pylab super(ConvexProblem2D, self).plot_t() pylab.step( [0., 2. * self.switch_times[-1]], [self.omega_i ** 2] * 2, 'g', linestyle='--', where='post') pylab.step( [0., 2. * self.switch_times[-1]], [self.omega_f ** 2] * 2, 'k', linestyle='--', where='post')
def plot_s(self): import pylab super(ConvexProblem2D, self).plot_s() pylab.step( [0., 1.], [self.omega_i ** 2] * 2, 'g', linestyle='--', where='post') pylab.step( [0., 1.], [self.omega_f ** 2] * 2, 'k', linestyle='--', where='post')
def graphical_output(account_balance_giro, account_balance_extra, depot_balance_extra, giro_data, daily_account_balance): years = mdates.YearLocator() # every year months = mdates.MonthLocator() weeks = mdates.WeekdayLocator() days = mdates.DayLocator() dateFormat = mdates.DateFormatter('%Y-%m-%d') yearFormat = mdates.DateFormatter('%Y-%m') fig = pl.figure() pl.xlabel("date") pl.ylabel("balance") ax1 = pl.subplot(121) pl.xlabel("date") pl.ylabel("account balance") pl.title("account balances") ax1.step(daily_account_balance[:, 0], daily_account_balance[:, 1]) ax1.step(daily_account_balance[:, 0], daily_account_balance[:, 2]) ax1.step(daily_account_balance[:, 0], daily_account_balance[:, 3]) ax1.step(daily_account_balance[:, 0], daily_account_balance[:, 4]) ax1.xaxis.set_major_locator(months) ax1.xaxis.set_major_formatter(yearFormat) # ax1.xaxis.set_minor_locator(weeks) ax1.format_xdata = mdates.DateFormatter('%Y-%m-%d') ax1.xaxis_date() pl.legend(["Giro Account", "Extra Account", "Depot", "Total"], loc="best") ax2 = pl.subplot(122) pl.xlabel("date") pl.ylabel("bookings") pl.title("giro account bookings") ax2.bar(giro_data[:, 0], giro_data[:, 4]) # ax2.bar(extra_data[:,0], extra_data[:,4]) too much stuff going on ax2.xaxis.set_major_locator(months) ax2.xaxis.set_major_formatter(yearFormat) ax2.xaxis.set_minor_locator(weeks) ax2.format_xdata = mdates.DateFormatter('%Y-%m-%d') ax2.xaxis_date() fig.autofmt_xdate() fig = pl.figure() daily_account_balance_diff = daily_account_balance[30:, :] - daily_account_balance[:-30, :] daily_account_balance_diff[:, 0] = daily_account_balance[30:, 0] # print daily_account_balance_diff pl.xlabel("date") pl.ylabel("balance") pl.step(daily_account_balance_diff[:, 0], daily_account_balance_diff[:, -1]) pl.axhline(y=0, xmin=0, xmax=1) pl.legend(["Total 30 day difference"], loc="best") pl.show()
def interpret_msmc(top_param = program_parameters(), scaling_method = "2N0", year = 1, ylog10scale = False ): if scaling_method == "generation": scaling_method = "years" year = 1 #msmc_para = program_parameters() #top_param = msmc_para ####### This two lines should be outside, after called replicates = top_param.replicates nsample = top_param.nsample case = top_param.case dir_name = "msmc-experiment/" + case + "Samples" +`nsample` ms_param = param.ms_param_of_case(case) ms_param.plot(ylog10scale = ylog10scale, timescale = scaling_method, year = year) mu = ms_param.t / ms_param.seqlen / float(4) / ms_param.scaling_N0 for ith_run in range(replicates): ms_out_file_prefix = ms_param.case + \ "Samples" +`nsample` + \ "msdata" outputFile_name = dir_name + "/" + ms_out_file_prefix + ".final.txt" if os.path.isfile( outputFile_name ): #print outputFile_name outputFile = open ( outputFile_name, "r") tmp_time , tmp_lambda = [] , [] skipline = outputFile.readline() for line in outputFile: tmp_time.append( float(line.split()[1]) ) tmp_lambda.append( float(line.split()[3]) ) #print tmp_time, tmp_lambda if scaling_method == "years": time = [ ti / mu * year for ti in tmp_time] elif scaling_method == "2N0": time = [ ti / mu / ( 2* float(ms_param.scaling_N0) ) for ti in tmp_time] elif scaling_method == "4N0": time = [ ti / mu / ( 4* float(ms_param.scaling_N0)) for ti in tmp_time] time[0] = time[1]/float(100) time.append(time[-1]*float(100)) yaxis_scaler = float(10000) pop = [ 1 / lambda_i / (2*mu) / yaxis_scaler for lambda_i in tmp_lambda] pop.insert(0, pop[0]) pylab.step(time, pop, color = "purple", linewidth=2.0) #print pop outputFile.close() pylab.savefig( dir_name + ".pdf" ) pylab.close()
def my_cdf(data): data.sort() n = len(data) y = [] for i in range(n): y.append((float(i)+1)/n) p.step(data, y) p.title('CDF') p.show() pass
def plot_data(xs, data, tellurics=False, telluric_xs=[0.0], plotname="data.png"): # plot the data plt.clf() for n in range(8): plt.step(xs, data[n, :] + 0.25 * n, color="k") if tellurics: assert telluric_xs.all() > 0.0 for t in telluric_xs: plt.axvline(t, color='b', alpha=0.25, lw=1) plt.title("examples of data") plt.savefig(plotname)
def plot_beam_events(events, side=None, timerange=None, height=1.0, offset=0.0, color='b'): b = db.sel(events, event='beam', data0=side, timerange=timerange) if len(b) == 0: return pylab.step(b[:, consts.TIME_COLUMN], b[:, consts.BEAM_STATE_COLUMN] * height + offset, where='post', color=color)
def plot_touch_binary_events(events, side=None, timerange=None, height=1.0, offset=0.0, color='g'): b = db.sel(events, event='touch_binary', data0=side, timerange=timerange) if len(b) == 0: return pylab.step(b[:, consts.TIME_COLUMN], b[:, consts.TOUCH_STATE_COLUMN] * height + offset, where='post', color=color)
def my_ccdf(data): # in-place sort data.sort() n = len(data) y = [] for i in range(n): y.append(1-(float(i)+1)/n) p.step(data, y) p.ylabel('ccdf(x)') p.xlabel('x') p.title('CCDF') p.show() pass
def plotFluxE(fluxVec, energyVec=None, fnameOut='fluxE', figNum=0, label='fluxE'): if not energyVec: # assume 10 energy grp structure by default energyVec = np.array([1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7]) fluxVec = np.append(np.array([0]), fluxVec) plt.figure(figNum) plt.step(energyVec, fluxVec / np.sum(fluxVec), linewidth='4', label=label) plt.ylim([5e-3, 1]) plt.xscale('log') plt.yscale('log') plt.xlabel("Energy (eV)") plt.ylabel("Log Flux (Arbitrary Scaling)") plt.legend() plt.savefig(fnameOut)
def measure_flexure(sky): ''' Measure expected (589.3 nm) - measured emission line in nm''' ll, ss = sky['nm'], sky['ph_10m_nm'] pl.figure() pl.step(ll, ss) pix = SG.argrelmax(ss, order=20)[0] skynms = chebval(pix, sky['coefficients']) for s in skynms: pl.axvline(s) ixmin = np.argmin(np.abs(skynms - 589.3)) dnm = 589.3 - skynms[ixmin] return dnm
def diCal_figure (cum_change, tmrca, position, prefix): tmrca = [x*2 for x in tmrca] # convert tmrca from unit of 4Ne to 2Ne site, absorptionTime = extract_diCal_time ( prefix ) maxtime = max( max(absorptionTime), max(tmrca) ) fig = pl.figure(figsize=(24,7)) pl.plot(site, absorptionTime, color = "green", linewidth=3.0) pl.step(cum_change, tmrca, color = "red", linewidth=5.0) pl.plot(position, [maxtime*1.05]*len(position), "bo") pl.axis([min(site), max(site) , 0, maxtime*1.1]) pl.title("Dical Absorption time (Green)") pl.xlabel("Sequence base") pl.ylabel("TMRCA (2N0)") pl.savefig( prefix + "diCal_absorption_time" + ".png") pl.close()
def diCal_figure(cum_change, tmrca, position, prefix): tmrca = [x * 2 for x in tmrca] # convert tmrca from unit of 4Ne to 2Ne site, absorptionTime = extract_diCal_time(prefix) maxtime = max(max(absorptionTime), max(tmrca)) fig = pl.figure(figsize=(24, 7)) pl.plot(site, absorptionTime, color="green", linewidth=3.0) pl.step(cum_change, tmrca, color="red", linewidth=5.0) pl.plot(position, [maxtime * 1.05] * len(position), "bo") pl.axis([min(site), max(site), 0, maxtime * 1.1]) pl.title("Dical Absorption time (Green)") pl.xlabel("Sequence base") pl.ylabel("TMRCA (2N0)") pl.savefig(prefix + "diCal_absorption_time" + ".png") pl.close()
def xsPlot(xs, energyVec=None, micro=True, label='xs1', style='-', fnameOut='xsPlt', figNum=4): if not energyVec: # assume 10 energy grp structure by default energyVec = np.array([1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7]) xsVec = np.append(np.array([0]), xs) plt.figure(figNum) plt.step(energyVec, xsVec, linewidth='4', label=label, linestyle=style) plt.yscale('log') plt.xscale('log') plt.xlabel("Energy (eV)") if micro: plt.ylabel("Micro Xsection [barns]") else: plt.ylabel("Macro Xsection [1/cm]") plt.legend() plt.savefig(fnameOut)
def measure_flexure_y(fine, HDUlist, profwidth=5, plot=False, outname=None): dat = HDUlist[0].data exptime = HDUlist[0].header['EXPTIME'] profs = [] xx = np.arange(profwidth*2) for ix in np.arange(500, 1200, 10): f = fine[ix] profile = np.zeros(profwidth*2) if not f.ok: continue if f.lamrms > 1: continue if f.xrange[1] - f.xrange[0] < 200: continue yfun = np.poly1d(f.poly) for xpos in np.arange(f.xrange[0], f.xrange[1]): try: ypos = int(np.round(yfun(xpos))) except: continue try: profile += dat[ypos-profwidth:ypos+profwidth, xpos] except: continue profs.append(profile - np.min(profile)) if plot: pl.figure(1) ffun = FF.mpfit_residuals(FF.gaussian4) parinfo= [{'value': 1}, {'value': profwidth}, {'value': 2}, {'value': 0}, {'value': 0}] profposys = [] for prof in profs: if plot: pl.step(xx, prof) parinfo[0]['value'] = np.max(prof) fit = FF.mpfit_do(ffun, xx, prof, parinfo) if plot: pl.plot(xx, FF.gaussian4(fit.params, xx)) profposys.append(fit.params[1] - profwidth-1) if plot: pl.show() profposys = np.array(profposys) mn = np.mean(profposys) sd = np.std(profposys) ok = np.abs(profposys - mn)/sd < 3 required_shift = np.mean(profposys[ok]) print "dY = %3.2f pixel shift" % required_shift return required_shift
def plot_t(self): """ Plot :math:`\\lambda(t)` and :math:`\\omega(t)^2` curves. """ from pylab import grid, legend, plot, step, xlabel, ylim times = list(self.switch_times) + [2 * self.switch_times[-1]] lambda_ = list(self.lambda_[-1::-1]) trange = linspace(0, max(times), 20) omega_ = [self.omega_from_t(t)**2 for t in trange] step(times, lambda_, marker='o', where='pre') plot(trange, omega_, 'r-', marker='o') legend(('$\\lambda(t)$', '$\\omega(t)^2$')) xlabel('$t$', fontsize=18) ymin = 0.9 * min(min(self.lambda_), min(omega_)) ymax = 1.1 * max(max(self.lambda_), max(omega_)) ylim((ymin, ymax)) grid()
def makePlot(XS, YS, XP, YP, XG, YG, XF, YF): pylab.figure(figsize=(20, 8)) pylab.subplot(1, 2, 1) pylab.plot(XS, YS, 'o', XP, YP, '+') pylab.axis([0, 100, 0, 100]) pylab.grid(True) pylab.xlabel("x-coordinate") pylab.ylabel("y-coordinate") pylab.subplot(1, 2, 2) pylab.step(XG, YG, XF, YF, where='post', linewidth=1) pylab.axis([0, 100, -0.1, 1.1]) pylab.grid(True) pylab.xlabel("Distance (d)") pylab.ylabel("G(d), F(d)") pylab.show()
def PrintHistogram(self, spec): """ print histogramm """ nbins = spec.hist.hist.GetNbinsX() # create values for x axis en = numpy.arange(nbins) # shift in order to get values at bin center en = en - 0.5 # calibrate en = self.ApplyCalibration(en, spec.cal) # extract bin contents to numpy array data = numpy.zeros(nbins) for i in range(nbins): data[i] = spec.hist.hist.GetBinContent(i) # create spectrum plot (r, g, b) = hdtv.color.GetRGB(spec.color) pylab.step(en, data, color=(r, g, b), label=spec.name)
def plot_precision_recall(precision, recall, f1_score, plotting_string): pylab.figure() pylab.step(recall, precision, color='b', alpha=0.2, where='post') pylab.fill_between(recall, precision, step='post', alpha=0.2, color='b') pylab.xlabel('Recall') pylab.ylabel('Precision') pylab.ylim([0.0, 1.05]) pylab.xlim([0.0, 1.0]) print('Saving figure %sprecision_recall_%s.pdf' % (analysis_directory, plotting_string)) pylab.savefig(os.path.expanduser('%sprecision_recall_%s.pdf' % (analysis_directory, plotting_string)), bbox_inches='tight')
def plot_attenuation(rmArray, f, label): N = rmArray.shape[0] l2 = (0.3 / f) ** 2 factors = [] for i, RM in enumerate(rmArray): atten = 0 for j in range(i + 1, N, 1): atten += np.cos(2 * (rmArray[j] - RM) * l2) factor = (float(N) + (2. * atten)) / pow(float(N), 2.) factors.append(factor) Pv, v = np.histogram(factors, bins=10, density=True) print('Epsilon=', np.mean(factors), '+/-', np.std(factors)) v = 0.5 * (v[1:] + v[:-1]) pylab.step(v, Pv, label=label) return [np.mean(factors), np.std(factors)]
def plot_1d_pdfs(cldata): grid = cldata['match']['grid'] logpr = grid['fit'] pr = np.exp(-0.5 * logpr) pr /= pr.sum() plt.figure(figsize=(15, 4)) plt.subplot(141) y = np.unique(grid['age']) dy = get_bins_from_center(y) n, b = plt.histogram(cldata['match']['grid']['age'], bins=dy, weights=pr) c = figrc.get_centers_from_bins(b) p = n * np.diff(b) * c plt.step(10**c, p / p.sum(), where='mid', color='k', lw=2) plt.xscale('log') figrc.hide_axis('top right'.split()) plt.xlabel('age [yr]') plt.subplot(142) Y = grid['av'] y = np.unique(Y) dy = get_bins_from_center(y) n, b = plt.histogram(Y, bins=dy, weights=pr) c = figrc.get_centers_from_bins(b) plt.step(c, n / n.sum(), where='mid', color='k', lw=2) figrc.hide_axis('top right'.split()) plt.xlabel('Av [mag]') plt.subplot(143) Y = grid['z'] y = np.unique(Y) dy = get_bins_from_center(y) n, b = plt.histogram(Y, bins=dy, weights=pr) c = figrc.get_centers_from_bins(b) plt.step(c, n / n.sum(), where='mid', color='k', lw=2) figrc.hide_axis('top right'.split()) plt.xlabel(r'Log(Z/Z$_\odot$)') plt.subplot(144) Y = grid['mass'] dlogm = 0.1 y = np.unique(Y) dy = np.arange(dlogm, 6, dlogm) n, b = plt.histogram(Y, bins=dy, weights=pr) c = figrc.get_centers_from_bins(b) p = n * np.diff(b) * c # plt.step(c, n / n.sum(), where='mid') plt.step(10**c, p / p.sum(), where='mid', color='k', lw=2) plt.xscale('log') figrc.hide_axis('top right'.split()) plt.xlabel(r'Mass [M$_\odot$]') plt.tight_layout()
def radius_dist_old(): physmerge = io.load_table('cks3').dropna(subset=['gdir_prad']) weights = pd.read_csv print(len(physmerge), (physmerge.gdir_srad_err1 / physmerge.gdir_srad).median()) rmask, rbin_centers, rN, re, result1, result2 = fitting.histfit( physmerge, completeness=False, verbose=False) mask, bin_centers, N, e, result1, result2 = fitting.histfit( physmerge, completeness=True, boot_errors=False) histfitplot(physmerge, bin_centers, N, e, mask, result1, result2, completeness=True, plotmod=False) pl.grid(False) rN = rN / (float(num_stars) * np.mean(physmerge['tr_prob'])) pl.step(rbin_centers, rN, color='0.5', where='mid', lw=3, linestyle='dotted') c2 = (0, 146 / 255., 146 / 255.) c1 = (146 / 255., 0, 0) # pl.axvline(1.0, color=c1, linestyle='dashed', lw=2) # pl.axvline(1.745, color=c1, linestyle='dashed', lw=2) # pl.axvspan(1.0, 1.75, color=c1, alpha=0.1) # pl.axvline(1.760, color=c2, linestyle='dashed', lw=2) # pl.axvline(3.5, color=c2, linestyle='dashed', lw=2) # pl.axvspan(1.75, 3.5, color=c2, alpha=0.1) pl.ylim(0, 0.16)
def money_plot_plain(): physmerge = io.load_table(config.filtered_sample) print(len(physmerge), (physmerge.gdir_srad_err1 / physmerge.gdir_srad).median()) rmask, rbin_centers, rN, re, result1, result2 = fitting.histfit( physmerge, completeness=False, verbose=False) mask, bin_centers, N, e, result1, result2 = fitting.histfit( physmerge, completeness=True, boot_errors=False) histfitplot(physmerge, bin_centers, N, e, mask, result1, result2, completeness=True, plotmod=False) pl.grid(False) rN = rN / (float(num_stars) * np.mean(physmerge['tr_prob'])) pl.step(rbin_centers, rN, color='0.5', where='mid', lw=3, linestyle='dotted') c2 = (0, 146 / 255., 146 / 255.) c1 = (146 / 255., 0, 0) # pl.axvline(1.0, color=c1, linestyle='dashed', lw=2) # pl.axvline(1.745, color=c1, linestyle='dashed', lw=2) # pl.axvspan(1.0, 1.75, color=c1, alpha=0.1) # pl.axvline(1.760, color=c2, linestyle='dashed', lw=2) # pl.axvline(3.5, color=c2, linestyle='dashed', lw=2) # pl.axvspan(1.75, 3.5, color=c2, alpha=0.1) pl.ylim(0, 0.125)
def plotFluxE(fluxVec, energyVec=None, fnameOut='fluxE', figNum=0, label='fluxE'): if not energyVec: # assume 10 energy grp structure by default energyVec = np.array( [1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7]) fluxVec = np.append(np.array([0]), fluxVec) plt.figure(figNum) plt.step(energyVec, fluxVec / np.sum(fluxVec), linewidth='4', label=label) plt.ylim([5e-3, 1]) plt.xscale('log') plt.yscale('log') plt.xlabel("Energy (eV)") plt.ylabel("Log Flux (Arbitrary Scaling)") plt.legend() plt.savefig(fnameOut)
def plot_clocks(trial,speeds,probs): speeds = [str(i) for i in speeds] probs = [str(i) for i in probs] with open("trial%d/log_0.log"%trial) as f0, open("trial%d/log_1.log"%trial) as f1, open("trial%d/log_2.log"%trial) as f2: pt.figure(1, figsize=(6, 6)) content0 = f0.readlines() content1 = f1.readlines() content2 = f2.readlines() contents = [content0,content1,content2] for i, content in enumerate(contents): data = [(get_time(line,"System Time","|"), int(line.split("Logical Clock")[1].strip())) for line in content] times = [packet[0] for packet in data] clocks = [packet[1] for packet in data] pt.step(times,clocks, label="Speed (%s)" % (speeds[i])) pt.grid(True) pt.xlabel('time (s)') pt.ylabel('Logical Clock Value') pt.title('Clock Comparison, Speeds (%s), Probabilities (%s)' % (",".join(speeds), ",".join(probs))) pt.legend(loc=2) pt.savefig('speeds%s_probs%s.png' % ("_".join(speeds), "_".join(probs)))
def plotpdf(name, data, weights, label, xmin, xmax, bestfit, bdelta=0.1): """ make pdf plots for age, av, and z inputs: cluster name; array of parameters (age, av, or z) from matchoutput file; fit values from matchoutput file; string name of parameter; minimum value of parameter to plot; maximum value of parameter to plot; best fit value of parameter; bin size output: apXXX/apXXX_XXXptiles.txt""" hdelta=bdelta/2. nbins=np.around((data.max()-data.min())/bdelta)+1 xbins=np.linspace(np.around(data.min()-hdelta,3),np.around(data.max()+bdelta+hdelta,3),nbins+2) #calculate bins h = np.histogram(data, bins=xbins, weights=weights) #create histogram of data hist = h[0]/np.sum(h[0]) #normalize histogram ptiles = output.weighted_percentile(data, weights, [0.16, 0.5, 0.84]) #compute percentiles np.savetxt(name+'/'+name+'_'+label+'ptiles.txt', ptiles) #save these weighted percentiles plt.axvspan(ptiles[0], ptiles[2], color='0.8', alpha=0.5) plt.step(np.linspace(data.min(), data.max(), len(h[0])), hist, color='black', lw=3) plt.axvline(ptiles[1], color='r', lw=2) plt.axvline(bestfit, color='b', lw=2) plt.ylim(np.min(hist.min(), 0), hist.max()*1.05) plt.xlim(xmin, xmax) plt.xlabel(label)
def fused_lasso(n=100, l1=2., **control): D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:] Dsp = scipy.sparse.lil_matrix(D) M = np.linalg.eigvalsh(np.dot(D.T, D)).max() Y = np.random.standard_normal(n) Y[int(0.1 * n):int(0.3 * n)] += 6. p1 = sapprox.gengrad_sparse((Dsp, Y), L=M) p1.assign_penalty(l1=l1) p2 = sapprox.gengrad((D, Y), L=M) p2.assign_penalty(l1=l1) t1 = time.time() opt1 = regreg.FISTA(p1) opt1.debug = True opt1.fit(tol=control['tol'], max_its=control['max_its']) beta1 = opt1.problem.coefs t2 = time.time() ts1 = t2 - t1 t1 = time.time() opt2 = regreg.FISTA(p2) opt2.fit(tol=control['tol'], max_its=control['max_its']) beta2 = opt2.problem.coefs t2 = time.time() ts2 = t2 - t1 beta1, _ = opt1.output beta2, _ = opt2.output np.testing.assert_almost_equal(beta1, beta2) X = np.arange(n) if control['plot']: pylab.clf() pylab.step(X, beta1, linewidth=3, c='red') pylab.step(X, beta2, linewidth=3, c='green') pylab.scatter(X, Y) pylab.show()
def run(trueID, predID): # Connect to DB conn = psycopg2.connect(database='taos-ii', user='******') cur = conn.cursor() cur.execute('''select id, flux_a, flux_b, flux_c, b, t, a, d from synthetic2 where id = %s or id = %s order by id''', (trueID, predID)) rows = cur.fetchall() pl.figure(figsize=(16, 6)) gs = gridspec.GridSpec(1, 3) title = '' figure_name = '' for row in rows: figure_name += '%d_' % row[0] title += '[ %d: Distance = %d, Size = %.1f ] ' \ % (row[0], row[6], row[7]) #title += 'b: %.2f, t: %.3f, a: %d, d:%.1f ' \ # % (row[4], row[5], row[6], row[7]) for i in range(3): time = np.arange(len(row[i + 1])) / 20. time -= np.median(time) pl.subplot(gs[i]) pl.step(time, row[i + 1], marker='x', label=row[0], where='mid') pl.legend(loc='lower right', numpoints=1) print len(time) if i % 3 == 0: pl.ylabel('Flux scale to one') #pl.subplot(gs[i + 3]) #pl.plot(time, np.array(rows[0][i + 1]) - np.array(rows[1][i + 1])) pl.grid() if i % 3 == 0: pl.ylabel('Scaled flux') pl.xlabel('Time/sec') pl.suptitle(title, fontsize=15) #pl.savefig('%s' % figure_name[:-1]) pl.show()
def fused_lasso(n=100,l1=2.,**control): D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:] Dsp = scipy.sparse.lil_matrix(D) M = np.linalg.eigvalsh(np.dot(D.T, D)).max() Y = np.random.standard_normal(n) Y[int(0.1*n):int(0.3*n)] += 6. p1 = sapprox.gengrad_sparse((Dsp, Y),L=M) p1.assign_penalty(l1=l1) p2 = sapprox.gengrad((D, Y),L=M) p2.assign_penalty(l1=l1) t1 = time.time() opt1 = regreg.FISTA(p1) opt1.debug=True opt1.fit(tol=control['tol'], max_its=control['max_its']) beta1 = opt1.problem.coefs t2 = time.time() ts1 = t2-t1 t1 = time.time() opt2 = regreg.FISTA(p2) opt2.fit(tol=control['tol'], max_its=control['max_its']) beta2 = opt2.problem.coefs t2 = time.time() ts2 = t2-t1 beta1, _ = opt1.output beta2, _ = opt2.output np.testing.assert_almost_equal(beta1, beta2) X = np.arange(n) if control['plot']: pylab.clf() pylab.step(X, beta1, linewidth=3, c='red') pylab.step(X, beta2, linewidth=3, c='green') pylab.scatter(X, Y) pylab.show()
def plotStepChart(simulation_no, iteration_no, start_date, end_date, resolution, field, country): """Plots step chart based on database data, recieved based on inputs simulation_no and iteration_no start_date and end_date, date resolution field - field which used for chart """ y_all_values = db.getIterationField( simulation_no, iteration_no, field) #get all 'field' values from database keys = db.getIterationField( simulation_no, iteration_no, 'project_days') #get dates for filed from database keys = map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').date(), keys) #converting string dates into datetime y_all_values = dict( (x, y) for x, y in zip(keys, y_all_values)) #getting dict [date]=field value x_values = [] y_values = [] sm = 0 for day_from, day_to in getResolutionStartEnd( start_date, end_date, resolution): #get dates for axis using resolution value delta = (day_to - day_from).days + 1 # +1 -> to include last day sum_period = sum([ y_all_values[day_from + datetime.timedelta(days=days)] for days in range(delta) ]) y_values.append(sum_period) x_values.append(sm + delta) sm += delta title = "{country}\n Sim. N. {simulation_no}; Iter. N. {iteration_no}. {field} {start_date}-{end_date} - res. {resolution} days".format( **locals()) pylab.step(x_values, y_values) pylab.title(title, fontsize=12) pylab.show()
def plot_pixel_spectrum(i, j, data_cube, hist, params, zoom=None): ''' ''' if zoom == None: zoom = 1.0 #---------------------------------- #plotting the spectrum for a trial pixel of the map inds_in_pixel = hist.get_indicies([i, j]) n = inds_in_pixel.size print 'number of sph particles in pixel = %d' % n #ax_map.plot(x[inds_in_pixel], y[inds_in_pixel], 'y.', markersize=1, color='k') #constructing the spectrum corresponding to that pixel v_min, v_max = params['cube']['spec_rng'] v_res = params['cube']['spec_res'] #the velocity bins v = numpy.linspace(v_min, v_max, v_res) #the spectrum of all the particles in the pixel spect_pixel = data_cube[i,j,:] fig_spec = pylab.figure() ax_spec = fig_spec.add_axes([0.2, 0.2, 0.6, 0.6]) #plotting the spectrum ax_spec.plot(v, spect_pixel, 'r') #rebinnign the spectrum to a lower resolution v_new = scipy.ndimage.zoom(v , zoom) spect_pixel_new = scipy.ndimage.zoom(spect_pixel, zoom) pylab.step(v_new, spect_pixel_new, 'b') pylab.plot(params['cube']['spec_rng'], [0.0,0.0], 'b--') pylab.xlabel(r'$v ({\rm km s}^{-1})$') pylab.ylabel(r'T$_{\rm antenna}$') pylab.draw() pylab.show()
def plot_s(self): """ Plot :math:`\\lambda(s)` and :math:`\\omega(s)^2` curves. """ from pylab import grid, legend, plot, step, xlabel, ylim def subsample(s): s2 = [(s[i] + s[i + 1]) / 2. for i in xrange(len(s) - 1)] s2.extend(s) s2.sort() return s2 s_more = subsample(subsample(self.s)) omega_ = [self.omega_from_s(s)**2 for s in s_more] step(self.s, self.lambda_, 'b-', where='post', marker='o') plot(s_more, omega_, 'r-', marker='o') legend(('$\\lambda(s)$', '$\\omega(s)^2$'), loc='upper left') xlabel('$s$', fontsize=18) ymin = 0.9 * min(min(self.lambda_), min(omega_)) ymax = 1.1 * max(max(self.lambda_), max(omega_)) ylim((ymin, ymax)) grid()
def errors_advection_down(plot_results=True, T_final=1000.0, dt=100, Mz=101): """Test the enthalpy solver using a 'pure advection' problem with Neumann (in-flow) B.C. at the surface. We use Dirichlet B.C. at the base but they are irrelevant due to upwinding. """ w = -1.0 config.set_double("constants.ice.thermal_conductivity", 0.0) column = EnthalpyColumn(Mz, dt) config.set_double("constants.ice.thermal_conductivity", k) Lz = column.Lz z = np.array(column.sys.z()) E_exact, dE_exact = exact_advection(Lz, w) with PISM.vec.Access(nocomm=[ column.enthalpy, column.u, column.v, column.w, column.strain_heating ]): column.sys.fine_to_coarse(E_exact(z, 0), 1, 1, column.enthalpy) column.reset_flow() column.w.set(w) column.reset_strain_heating() t = 0.0 while t < T_final: column.init_column() column.sys.set_basal_dirichlet_bc(E_exact(0, t + dt)) column.sys.set_surface_neumann_bc(dE_exact(Lz, t + dt)) x = column.sys.solve() column.sys.fine_to_coarse(x, 1, 1, column.enthalpy) t += dt if plot_results: plt.figure() plt.xlabel("z, meters") plt.ylabel("E, J/kg") plt.hold(True) plt.step(z, E_exact(z, 0), color="blue", label="initial condition") plt.step(z, E_exact(z, t), color="green", label="exact solution") plt.grid(True) plt.step(z, x, label="T={} seconds".format(t), color="red") plt.legend(loc="best") errors = E_exact(z, t) - x max_error = np.max(np.fabs(errors)) avg_error = np.average(np.fabs(errors)) return max_error, avg_error
def OnZPlotProfile(self, event): x,p,d, pi = self.vp.GetProfile(50, background=[7,7]) pylab.figure(1) pylab.clf() pylab.step(x,p) pylab.step(x, 10*d - 30) pylab.ylim(-35,pylab.ylim()[1]) pylab.xlim(x.min(), x.max()) pylab.xlabel('Time [%3.2f ms frames]' % (1e3*self.mdh.getEntry('Camera.CycleTime'))) pylab.ylabel('Intensity [counts]') fr = self.fitResults[pi] if not len(fr) == 0: pylab.figure(2) pylab.clf() pylab.subplot(211) pylab.errorbar(fr['tIndex'], fr['fitResults']['x0'] - self.vp.do.xp*1e3*self.mdh.getEntry('voxelsize.x'), fr['fitError']['x0'], fmt='xb') pylab.xlim(x.min(), x.max()) pylab.xlabel('Time [%3.2f ms frames]' % (1e3*self.mdh.getEntry('Camera.CycleTime'))) pylab.ylabel('x offset [nm]') pylab.subplot(212) pylab.errorbar(fr['tIndex'], fr['fitResults']['y0'] - self.vp.do.yp*1e3*self.mdh.getEntry('voxelsize.y'), fr['fitError']['y0'], fmt='xg') pylab.xlim(x.min(), x.max()) pylab.xlabel('Time [%3.2f ms frames]' % (1e3*self.mdh.getEntry('Camera.CycleTime'))) pylab.ylabel('y offset [nm]') pylab.figure(3) pylab.clf() pylab.errorbar(fr['fitResults']['x0'] - self.vp.do.xp*1e3*self.mdh.getEntry('voxelsize.x'),fr['fitResults']['y0'] - self.vp.do.yp*1e3*self.mdh.getEntry('voxelsize.y'), fr['fitError']['x0'], fr['fitError']['y0'], fmt='xb') #pylab.xlim(x.min(), x.max()) pylab.xlabel('x offset [nm]') pylab.ylabel('y offset [nm]')
def profile_plot(layers): dz, rho, rhoM, thetaM, phiM = np.asarray(layers).T z = np.cumsum([np.hstack((-dz[0],dz))]) rho, rhoM, thetaM = [np.hstack((v[0],v)) for v in (rho, rhoM, thetaM)] pylab.step(z, rho, label='rho') pylab.step(z, rhoM, label='rhoM', hold=True) pylab.step(z, thetaM*2*np.pi/360., label='thetaM', hold=True) pylab.legend()
def vizualizationCurves (clusters, picFormat = "png"): colors = ['r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w''r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b', 'g', 'c', 'k', 'm' ,'y','w', 'r', 'b'] fig = pl.figure() for Ncl,cl in enumerate(clusters): for curves in cl.points: x, y = [], [] sumX, sumY = 0,0 for el in curves.elements: x.append(sumX + el[1]) sumX = sumX + el[1] y.append(el[0]) pl.step(x,y, color = colors[Ncl]) # pl.xlim(50000, 130000) # pl.ylim(0, 5000) pl.xlabel("Volume, MWh") pl.ylabel("Price, Rubles per MWh") pl.savefig("./pic/"+str(pathToPic) + "/curvesFromCluster." + picFormat, format=picFormat) pl.close(fig)
def psmc_figure(cum_change, tmrca, position, prefix): X, Y, Z, site, time = psmc_XYZ( prefix ) fig = pl.figure(figsize=(24,7)) pl.pcolor(X, Y, Z, vmin=0, vmax=0.15) my_axes = fig.gca() #ylabels = ["%.4g" % (float(y)*2 * default_pop_size) for y in my_axes.get_yticks()] ylabels = ["%.4g" % (float(y)) for y in my_axes.get_yticks()] my_axes.set_yticklabels(ylabels) #pl.colorbar() pl.step(cum_change, [x*2 for x in tmrca] , color = "red", linewidth=15.0) # becasue x is generated by ms, which is scaled to 4N0, psmc scale to 2N0. Thereofore, needs to multiply by 2 #pl.step(cum_change, [x for x in tmrca] , color = "red", linewidth=5.0) pl.plot(position, [0.9*max(time)]*len(position), "wo", markersize=15) pl.axis([0, max(site) , 0, max(time)]) pl.title("PSMC heat map", fontsize=25) pl.xlabel("Sequence base", fontsize=20) #pl.ylabel("TMRCA (generation)") pl.ylabel("TMRCA (2N0)", fontsize=20) #my_axes.set_yticklabels([]) #my_axes.set_xticklabels([]) pl.savefig(prefix + "psmc_heat" + ".png", transparent=True) print "done" pl.close()
def smcsmc_figure(cum_change, tmrca, position, prefix): X, Y, Z, site, time = smcsmc_XYZ( prefix ) fig = pl.figure(figsize=(24,7)) pl.pcolor(X, Y, Z, vmin=0, vmax=0.15) my_axes = fig.gca() ylabels = ["%.4g" % (float(y)+shifting) for y in my_axes.get_yticks()] my_axes.set_yticklabels(ylabels) #pl.colorbar() #pl.step(cum_change, [x*4*default_pop_size for x in tmrca] , color = "red", linewidth=5.0) pl.step(cum_change, tmrca , color = "red", linewidth=15.0) pl.plot(position, [0.9*max(time)]*len(position), "wo", markersize=15) pl.axis([min(site), max(site) , 0, max(time)]) pl.axis([min(site), max(site) , shifting, max(time)]) # for the case of split #my_axes.set_yticklabels([]) #my_axes.set_xticklabels([]) pl.title("PFPSMC heat map", fontsize=25) pl.xlabel("Sequence base", fontsize=20) #pl.ylabel("TMRCA (generation)") pl.ylabel("TMRCA (4N0)", fontsize=20) pl.savefig( prefix + "smcsmc_heat" + ".png", transparent=True) pl.close()
def errors_advection_down(plot_results=True, T_final=1000.0, dt=100, Mz=101): """Test the enthalpy solver using a 'pure advection' problem with Neumann (in-flow) B.C. at the surface. We use Dirichlet B.C. at the base but they are irrelevant due to upwinding. """ w = -1.0 config.set_double("constants.ice.thermal_conductivity", 0.0) column = EnthalpyColumn(Mz, dt) config.set_double("constants.ice.thermal_conductivity", k) Lz = column.Lz z = np.array(column.sys.z()) E_exact, dE_exact = exact_advection(Lz, w) with PISM.vec.Access(nocomm=[column.enthalpy, column.u, column.v, column.w, column.strain_heating]): column.sys.fine_to_coarse(E_exact(z, 0), 1, 1, column.enthalpy) column.reset_flow() column.w.set(w) column.reset_strain_heating() t = 0.0 while t < T_final: column.init_column() column.sys.set_basal_dirichlet_bc(E_exact(0, t+dt)) column.sys.set_surface_neumann_bc(dE_exact(Lz, t+dt)) x = column.sys.solve() column.sys.fine_to_coarse(x, 1, 1, column.enthalpy) t += dt if plot_results: plt.figure() plt.xlabel("z, meters") plt.ylabel("E, J/kg") plt.step(z, E_exact(z, 0), color="blue", label="initial condition") plt.step(z, E_exact(z, t), color="green", label="exact solution") plt.grid(True) plt.step(z, x, label="T={} seconds".format(t), color="red") plt.legend(loc="best") errors = E_exact(z, t) - x max_error = np.max(np.fabs(errors)) avg_error = np.average(np.fabs(errors)) return max_error, avg_error
def plotStep(data, Daily_Total, Hrs, k): import pylab import matplotlib.pyplot as plt import numpy data.sort(['Hr_Length'], inplace = True, ascending=[True]) data.cumSpec = numpy.cumsum(data['Total_Specimens']) x = data.Hr_Length y = data.cumSpec pylab.figure(figsize=(12, 9)) ax = pylab.subplot(111) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() pylab.xticks(fontsize=14) pylab.yticks(fontsize=14) pylab.xlim(0,Hrs + 1) pylab.xlabel("Hours", fontsize=16) pylab.ylabel("Total Specimens", fontsize=16) pylab.title("Daily Specimens Deliveries", fontsize=20) pylab.step(x, y , color="#F0501A", lw=2.0, label="Actual Deliveries") pylab.plot([0,Hrs], [0,Daily_Total], color = "#3F5D7D", lw=2.0, label = 'Hypothetical Constant Flow') pylab.legend(loc='upper left') pylab.text(11, 450, "Cluster Size = %i"%k, fontsize=10) pylab.savefig('Step_Plot_clustersize_%i.png'%k, bbox_inches="tight"); plot = pylab.show() return plot
def archive(objname): corrname = "std-correction.npy" if not os.path.isfile(corrname): corrname = '/scr2/npk/sedm/OUTPUT/2015mar25/std-correction.npy' ss = np.load(specname)[0] corr = np.load(corrname)[0] corr = np.load('atm-corr.npy')[0] corf = interp1d(corr['nm'], corr['correction'], bounds_error=False, fill_value=1.0) ec = 0 ext = None if ss.has_key('extinction_corr'): ext = ss['extinction_corr'] ec = np.median(ext) elif ss.has_key('extinction_corr_A'): ext = ss['extinction_corr_A'] ec = np.median(ext) et = ss['exptime'] pl.title("%s\n(airmass corr factor ~ %1.2f Exptime: %i)" % (specname, ec, et)) pl.xlabel("Wavelength [nm]") pl.ylabel("erg/s/cm2/ang") pl.step(ss['nm'], ss['ph_10m_nm'] * corf(ss['nm']), linewidth=2) try: pl.step(ss['skynm'], ss['skyph'] * corf(ss['skynm'])) except: pl.step(ss['nm'], ss['skyph'] * (ss['N_spaxA'] + ss['N_spaxB']) * corf(ss['nm'])) try: pl.step(ss['nm'], np.sqrt(np.abs(ss['var'])) * corf(ss['nm'])) except: pass pl.legend(['obj', 'sky', 'err']) pl.xlim(360, 1100) pl.grid(True) pl.ion() pl.show()
def test_fused_lasso(n=100,l1=2.,**control): D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:] M = np.linalg.eigvalsh(np.dot(D.T, D)).max() Y = np.random.standard_normal(n) Y[int(0.1*n):int(0.3*n)] += 6. p1 = signal_approximator((D, Y),L=M) p1.assign_penalty(l1=l1) p2 = glasso.generalized_lasso((np.identity(n), D, Y),L=M) p2.assign_penalty(l1=l1) t1 = time.time() opt1 = regreg.FISTA(p1) opt1.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts1 = t2-t1 t1 = time.time() opt2 = regreg.FISTA(p2) opt2.fit(tol=control['tol'], max_its=control['max_its']) t2 = time.time() ts2 = t2-t1 beta1, _ = opt1.output beta2, _ = opt2.output X = np.arange(n) print (np.fabs(beta1-beta2).sum() / np.fabs(beta1).sum()) if control['plot']: pylab.clf() pylab.step(X, beta1, linewidth=3, c='red') pylab.step(X, beta2, linewidth=3, c='blue') pylab.scatter(X, Y) pylab.show()
def plot_initial_and_optimized_controls(time_points, \ udata_init, udata_opt, umin, umax): pl.rc("text", usetex = True) pl.rc("font", family="serif") pl.subplot2grid((2, 1), (0, 0)) pl.step(time_points[:-1], udata_init[:,0], label = "$\delta_{init}$") pl.step(time_points[:-1], udata_init[:,1], label = "$D_{init}$") pl.plot([time_points[0], time_points[-2]], [umin[0], umin[0]], \ color = "b", linestyle = "dashed", label = "$\delta_{min}$") pl.plot([time_points[0], time_points[-2]], [umax[0], umax[0]], \ color = "b", linestyle = "dotted", label = "$\delta_{max}$") pl.plot([time_points[0], time_points[-2]], [umin[1], umin[1]], \ color = "g", linestyle = "dashed", label = "$D_{min}$") pl.plot([time_points[0], time_points[-2]], [umax[1], umax[1]], \ color = "g", linestyle = "dotted", label = "$D_{max}$") pl.ylabel("$\delta,\,D$", rotation = 0) pl.ylim(-0.6, 1.1) pl.title("Initial and optimized controls") pl.legend(loc = "upper right") pl.subplot2grid((2, 1), (1, 0)) pl.step(time_points[:-1], udata_opt[:,0], label = "$\delta_{opt,coll}$") pl.step(time_points[:-1], udata_opt[:,1], label = "$D_{opt,coll}$") pl.plot([time_points[0], time_points[-2]], [umin[0], umin[0]], \ color = "b", linestyle = "dashed", label = "$\delta_{min}$") pl.plot([time_points[0], time_points[-2]], [umax[0], umax[0]], \ color = "b", linestyle = "dotted", label = "$\delta_{max}$") pl.plot([time_points[0], time_points[-2]], [umin[1], umin[1]], \ color = "g", linestyle = "dashed", label = "$D_{min}$") pl.plot([time_points[0], time_points[-2]], [umax[1], umax[1]], \ color = "g", linestyle = "dotted", label = "$D_{max}$") pl.xlabel("$t$") pl.ylabel("$\delta,\,D$", rotation = 0) pl.ylim(-0.6, 1.1) pl.legend(loc = "upper right") pl.show()
def myplot_hist(x,y,xlim=None,ylim=None, symbol=None,alpha=1, offset=0, fig = None, fcolor=None, ax=None, nbins=None): if symbol: color=symbol[:-1] marker=symbol[-1] if not fcolor: fcolor=color else: color = 'blue' marker='o' fcolor=color if fig: pl.figure(fig) # print "from myplot_err:",symbol,xlim,ylim if xlim : pl.xlim(xlim) if ylim : pl.ylim(ylim) print xlim,ylim,x,y if not symbol : symbol='ko' print nbins if nbins is None: nbins=int((xlim[1]-xlim[0])/10) else: nbins=int((xlim[1]-xlim[0])/nbins) print nbins print xlim[0],xlim[1], np.asarray(x),np.asarray(y),nbins,ax print "\n\n\n" X,Y,Ystd=binMeanStdPlot(np.asarray(x),np.asarray(y),numBins=nbins,xmin=xlim[0],xmax=xlim[1], ax=ax) pl.errorbar(X,Y+offset, yerr=Ystd, fmt=None,color=color, ecolor=color,alpha=alpha) pl.errorbar(X,Y+offset, yerr=Ystd, fmt='.',color=color, ecolor=color,alpha=alpha) try: binsz=X[1]-X[0] except: binsz=0 newx=[] newy=[] newx=[newx+[x,x] for x in X-binsz/2] newx =np.asarray(newx).flatten()[1:] newy=[newy+[y,y] for y in (Y)] newy=np.asarray(newy).flatten() newx=np.insert(newx,len(newx),newx[-1]+binsz) return pl.step(newx,np.asarray(newy)+offset,"",alpha=alpha,color=color)
def normplot(a, outp): ''' Plots histograms describing the PDF and CDF corresponding to a 1-D array of data and saves the figure as a PNG file. ''' # Regular expression that determines the input filename minus the extension to use as the base for the figure filename. name_base = re.search(r'\b(.+)\b\.', outp) # Set the figure size and generate the PDF plot canvas. fig = plt.figure(figsize=(12, 6)) p = plt.subplot(121) # Determine how many normality tests were passed. count = normtest(a) # If 2 or more of the 4 normality tests are passed, decide that data is normally distributed; otherwise, decide that it is not. if count >= 2: p.set_title('PDF\n(Data appears to be normal...)', fontsize=12) elif count < 2: p.set_title('PDF\n(Data doesn\'t appear to be normal...)', fontsize=12) # Add labels to the PDF plot. p.set_xlabel('K-eff') p.set_ylabel('Frequency') # Plot a histogram of the data (the PDF). count, bins, ignored = plt.hist(a, normed=True, histtype='stepfilled', alpha=0.5) mu = numpy.mean(a) sigma = numpy.std(a) plt.step(bins, 1/(sigma * numpy.sqrt(2 * numpy.pi)) * numpy.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='g') # Generate the CDF plot canvas and add labels. p = plt.subplot(122) p.set_title('CDF', fontsize=12) p.set_xlabel('K-eff') p.set_ylabel('Frequency') # Plot a histogram of the data (the CDF). #h = plt.hist(a, histtype='step', cumulative=True, normed=True, bins=100) ecdf = sm.distributions.ECDF(a) x = numpy.linspace(min(a), max(a)) y = ecdf(x) plt.step(x, y) s = numpy.random.normal(mu, sigma, 10000) ecdf = sm.distributions.ECDF(s) x = numpy.linspace(min(a), max(a)) y = ecdf(x) plt.step(x, y) #plt.show() # Save the figure of the PDF and CDF plots as a PNG file with the input file name. plt.savefig(name_base.group(1) + '_pdf_cdf.png', bbox_inches='tight')