def test_params(self): from itertools import islice import pylab for i, params in enumerate(self.parameters): test_name = str(params) pylab.figure() pylab.title(test_name) for seqs_name, (sequences, c) in self.sequences.iteritems(): lengths = [] ratios = [] LL_ratio_total = 0.0 num_sources = 0 for m, seqs in sequences: ratio = params.k_fold_cross_validation_test( seqs_from_strings(seqs)) LL_ratio_total += ratio lengths.append(len(seqs)) ratios.append(ratio) num_sources += 1 print 'average LL_ratio: %.3f : %s' % ( LL_ratio_total / num_sources, test_name) pylab.scatter(lengths, ratios, label=seqs_name, c=c) pylab.xlabel('# seqs') pylab.ylabel('LL ratio/per base') pylab.xlim(xmin=0) pylab.axhspan(0, 0) # draw a line through LL_ratio = 0 pylab.legend pylab.savefig('motif_%d.png' % i) pylab.savefig('motif_%d.ps' % i)
def draw_search_graph(plots): import pylab as pl fg = pl.figure() ax = fg.add_subplot(111) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) for (values, attrs) in plots: indexes, width = pl.arange(len(values)), 1.0 / len(plots) yvalues = [x.result for x in values] xoffset = width * plots.index((values, attrs)) ax.plot(indexes + xoffset, yvalues, **attrs) legend = ax.legend(loc='best') legend.get_frame().set_alpha(0.6) fg.canvas.draw() pl.ylabel('tradeoff improvement -->') pl.xlabel('number of tested configurations -->') pl.title('Search Graph') pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.grid(True) pl.show()
def plot(): (n, P, eP, A, eA, ALines) = numbers() pylab.subplot(2, 1, 1) pylab.errorbar(n, P, eP, ecolor='r', linewidth=3, fmt=None) pylab.semilogx() pylab.xticks([2, 4, 8, 16, 32, 64, 128], ['2', '4', '8', '16', '32', '64', '128']) pylab.axis([1, 256, 4, 18]) pylab.ylabel('Precision of 95% Confidence') pylab.title( 'Estimating Time Constant of RC circuit, Gaussian Noise, 1000 trials') pylab.subplot(2, 1, 2) pylab.plot(n, A, '*', color='r', markersize=12) #pylab.axhspan(ALines[0],ALines[0],color='b') pylab.axhspan(ALines[1], ALines[1], color='b') pylab.axhspan(ALines[2], ALines[2], color='b') pylab.semilogx() pylab.xticks([2, 4, 8, 16, 32, 64, 128], ['2', '4', '8', '16', '32', '64', '128']) pylab.axis([1, 256, 0.9, 1.0]) pylab.text(1.5, 0.95, 'Accurate') pylab.text(1.5, 0.99, 'Under-Confident') pylab.text(1.5, 0.91, 'Over-Confident') pylab.xlabel('Number of Data Points') pylab.ylabel('Accuracy of 95% Confidence') pylab.ion()
def draw_correl_graph(getgraph, opts): # http://matplotlib.sourceforge.net/index.html fg = pl.figure() ax = fg.add_subplot(111) bars = getgraph() for (values, attrs) in bars: indexes, width = pl.arange(len(values)), 1.0 / len(bars) yvalues = [x.speedup for x in values] xoffset = width * bars.index((values, attrs)) ax.bar(indexes + xoffset, yvalues, width, picker=4000, **attrs) ax.legend(loc='lower left') fg.canvas.draw() # dynamic annotations def on_pick(event): ind = int(event.mouseevent.xdata) point = bars[0][0][ind] tooltip.set_position((event.mouseevent.xdata, event.mouseevent.ydata)) tooltip.set_text(point_descr(point)) tooltip.set_visible(True) fg.canvas.draw() tooltip = ax.text(0, 0, "undef", bbox=dict(facecolor='white', alpha=0.8), verticalalignment='bottom', visible=False) # graph title try: title = 'Correlation Graph for %s' % (opts.id or opts.targets or bars[0][0][0].target) except: title = 'Correlation Graph' # redraw axis, set labels, legend, grid, ... def labelfmt(x, pos=0): return '%.2f%%' % (100.0 * x) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.ylabel('speedup (higher is better) -->') pl.xlabel('Configurations (ordered by decreasing speedup of ' + bars[0][1]['label'] + ') -->') pl.title(title) pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.grid(True) if opts.outfile: fg.savefig(opts.outfile) if opts.show: fg.canvas.mpl_connect('pick_event', on_pick) pl.show()
def test_params(self): from itertools import islice import pylab for i, params in enumerate(self.parameters): test_name = str(params) pylab.figure() pylab.title(test_name) for seqs_name, (sequences, c) in self.sequences.iteritems(): lengths = [] ratios = [] LL_ratio_total = 0.0 num_sources = 0 for m, seqs in sequences: ratio = params.k_fold_cross_validation_test(seqs_from_strings(seqs)) LL_ratio_total += ratio lengths.append(len(seqs)) ratios.append(ratio) num_sources += 1 print 'average LL_ratio: %.3f : %s' % (LL_ratio_total/num_sources, test_name) pylab.scatter(lengths, ratios, label=seqs_name, c=c) pylab.xlabel('# seqs') pylab.ylabel('LL ratio/per base') pylab.xlim(xmin=0) pylab.axhspan(0,0) # draw a line through LL_ratio = 0 pylab.legend pylab.savefig('motif_%d.png'%i) pylab.savefig('motif_%d.ps'%i)
def plotRes(data, errors, r): import pylab pylab.figure() nObs = len(data) n, bins, patches = pylab.hist(data, 2*np.sqrt(nObs), fc=[.7,.7,.7]) binSize = bins[1] - bins[0] x = np.arange(bins[0], bins[-1]) means, sigs, pis, mVars, weights = r inds = np.argmax(weights, 1) for i in range(means.size): #print i c = pylab.cm.hsv(float(i)/means.size) n, bin_s, patches = pylab.hist(data[inds == i], bins, alpha=0.3, facecolor=c) ys = np.zeros_like(x) i = 0 for m, s, p in zip(means, sigs, pis): c = pylab.cm.hsv(float(i)/means.size) y = nObs*p*binSize*np.exp(-(x-m)**2/(2*s**2))/np.sqrt(2*np.pi*s**2) ys += y i+= 1 pylab.plot(x,y, lw=2, color=c) #pylab.plot(x, ys, lw=3) pylab.figure() ci = (r[4]*np.arange(r[0].size)[None,:]).sum(1) I = np.argsort(ci) cis = ci[I] cil = 0 for i in range(means.size): c = pylab.cm.hsv(float(i)/means.size) print(c) pylab.axvline(means[i], color=c) pylab.axvspan(means[i] - sigs[i], means[i] + sigs[i], alpha=0.5, facecolor=c) cin = cis.searchsorted(i+0.5) pylab.axhspan(cil, cin, alpha=0.3, facecolor=c) cil = cin pylab.errorbar(data[I], np.arange(data.size), xerr=errors[I], fmt='.')
def draw_correl_graph(getgraph, opts): # http://matplotlib.sourceforge.net/index.html fg = pl.figure() ax = fg.add_subplot(111) bars = getgraph() for (values, attrs) in bars: indexes, width = pl.arange(len(values)), 1.0 / len(bars) yvalues = [x.speedup for x in values] xoffset = width * bars.index((values, attrs)) ax.bar(indexes + xoffset, yvalues, width, picker=4000, **attrs) ax.legend(loc='lower left') fg.canvas.draw() # dynamic annotations def on_pick(event): ind = int(event.mouseevent.xdata) point = bars[0][0][ind] tooltip.set_position( (event.mouseevent.xdata, event.mouseevent.ydata)) tooltip.set_text(point_descr(point)) tooltip.set_visible(True) fg.canvas.draw() tooltip = ax.text( 0, 0, "undef", bbox=dict(facecolor='white', alpha=0.8), verticalalignment='bottom', visible=False) # graph title try: title = 'Correlation Graph for %s' % ( opts.id or opts.targets or bars[0][0][0].target) except: title = 'Correlation Graph' # redraw axis, set labels, legend, grid, ... def labelfmt(x, pos=0): return '%.2f%%' % (100.0 * x) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.ylabel('speedup (higher is better) -->') pl.xlabel('Configurations (ordered by decreasing speedup of ' + bars[0][1]['label'] + ') -->') pl.title(title) pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.grid(True) if opts.outfile: fg.savefig(opts.outfile) if opts.show: fg.canvas.mpl_connect('pick_event', on_pick) pl.show()
def test_axhspan_epoch(): from datetime import datetime import matplotlib.testing.jpl_units as units units.register() t0 = units.Epoch( "ET", dt=datetime(2009, 1, 20) ) tf = units.Epoch( "ET", dt=datetime(2009, 1, 21) ) dt = units.Duration( "ET", units.day.convert( "sec" ) ) fig = pylab.figure() pylab.axhspan( t0, tf, facecolor="blue", alpha=0.25 ) ax = pylab.gca() ax.set_ylim( t0 - 5.0*dt, tf + 5.0*dt ) fig.savefig( 'axhspan_epoch' )
def test_axhspan_epoch(self): """Test the axhspan method with Epochs.""" fname = self.outFile("axhspan_epoch.png") t0 = units.Epoch("ET", dt=datetime(2009, 1, 20)) tf = units.Epoch("ET", dt=datetime(2009, 1, 21)) dt = units.Duration("ET", units.day.convert("sec")) fig = pylab.figure() pylab.axhspan(t0, tf, facecolor="blue", alpha=0.25) ax = pylab.gca() ax.set_ylim(t0 - 5.0 * dt, tf + 5.0 * dt) fig.savefig(fname) self.checkImage(fname)
def __call__(self, inputs): from pylab import axhspan res = axhspan(self.get_input('ymin'), self.get_input('ymax'), xmin=self.get_input('xmin'), xmax=self.get_input('xmax'), **self.get_input('kwargs (Patch)')) return res
def coo(self,tmin,tmax): self.lims = [tmin,tmax] if self.boxh: self.boxh.remove() if self.direction is 'horizontal': self.boxh = pl.axvspan(tmin,tmax,facecolor='r',alpha=0.5) if self.direction is 'vertical': self.boxh = pl.axhspan(tmin,tmax,facecolor='r',alpha=0.5) fig.canvas.draw()
def test_axhspan_epoch(): from datetime import datetime import matplotlib.testing.jpl_units as units units.register() # generate some data t0 = units.Epoch("ET", dt=datetime(2009, 1, 20)) tf = units.Epoch("ET", dt=datetime(2009, 1, 21)) dt = units.Duration("ET", units.day.convert("sec")) fig = pylab.figure() pylab.axhspan(t0, tf, facecolor="blue", alpha=0.25) ax = pylab.gca() ax.set_ylim(t0 - 5.0 * dt, tf + 5.0 * dt) fig.savefig('axhspan_epoch')
def make_pretty_motion_graph(fMRI, sub, mopar, FD): #Make Composite Graph plt.suptitle('{0} Task: All Head Motion\nSubject: {1}'.format(fMRI, sub)) plt.subplot(2, 1, 1) plt.axhspan(-vxsz, vxsz, hold=True, facecolor='0.75') plt.plot(mopar, linewidth=2.5) plt.ylabel('Head Displacement (mm)') plt.tight_layout(pad=3) # plt.subplot(2, 1, 2) plt.axhspan(FDthrsh, 0, hold=True, facecolor='0.75') plt.plot(FD, 'k', linewidth=2.5) plt.xlabel('Volume Index') plt.ylabel('FD (mm)') plt.ylim(ymin=0) plt.tight_layout(pad=3) plt.savefig(summarydir + sub + '_' + fMRI + '_MotionGraph.pdf') plt.close() return
def test_axhspan_epoch(self): """Test the axhspan method with Epochs.""" fname = self.outFile("axhspan_epoch.png") # generate some data t0 = units.Epoch("ET", dt=datetime(2009, 1, 20)) tf = units.Epoch("ET", dt=datetime(2009, 1, 21)) dt = units.Duration("ET", units.day.convert("sec")) fig = pylab.figure() pylab.axhspan(t0, tf, facecolor="blue", alpha=0.25) ax = pylab.gca() ax.set_ylim(t0 - 5.0 * dt, tf + 5.0 * dt) fig.savefig(fname) self.checkImage(fname)
def plot_fDs(results, save=False, saveas='', title=None, legend_spec='', xlabel='', xlim=[], ylim=[]): x, y, e = zip(*results) # Unpack results. legend = () p.figure() p.rc('text', usetex=True) p.rc('font', size=18) if title is not None: p.title(title) if xlabel: p.xlabel(xlabel) if xlim: p.xlim(xlim) p.ylabel('$f_{D_s} \mathrm{\,\, [MeV]}$') if ylim: p.ylim(ylim) p.errorbar(x, y, e, fmt='rs', ms=10), p.axhspan((248.6-2.7), (248.6+2.7),facecolor=(0.,0.5,1.), alpha=.3) if save: p.savefig(saveas) else: p.show()
def plot_timeline(packets): pylab.figure() for pkt in packets: pylab.axhspan(1,2,pkt[0],pkt[0]+pkt[1]) pylab.savefig('timeline.png')
i = int(np.random.uniform(0, len(samples_loc))) label = 'Model' if j == 0 else None preds = model(t, (samples_loc[i], samples_scale[i], samples_amp[i])) pl.plot(t, preds, '-', color='C1', alpha=0.1, label=label) pl.plot(time, counts, '.', color='C0', label='Data') pl.axvspan(np.percentile(samples_loc, 5), np.percentile(samples_loc, 95), facecolor='C2', edgecolor='none', alpha=0.25) pl.axhspan(np.percentile(samples_amp, 5), np.percentile(samples_amp, 95), facecolor='C2', edgecolor='none', alpha=0.25) pl.semilogy() pl.ylim(0.9, 70e6) pl.legend() pl.xlabel('Time (days since {})'.format(start_date.strftime('%Y-%m-%d'))) pl.ylabel('Number of cases') pl.savefig('cases_vs_time.png', dpi=200) # plot marginal posterior for inflection point pl.figure() pl.hist(samples_loc, 500, histtype='stepfilled') pl.xlabel('Inflection point (days since {})'.format( start_date.strftime('%Y-%m-%d')))
gibbs = h5py.File('tmp_mockI_brokenpl_gibbs_sample_fast.hdf5', 'r') nlens = gibbs['beta'].value.shape[0] nlens = 10 end = 400 day = 24.*3600. for i in range(nlens): pylab.subplot(3, 2, 1) pylab.plot(gibbs['xA_model'][i, 1:end]) pylab.axhspan(mock['lenses'][i].obs_images[0][0] - mock['lenses'][i].obs_images[1], mock['lenses'][i].obs_images[0][0] + mock['lenses'][i].obs_images[1], color='gray', alpha=0.5) pylab.subplot(3, 2, 2) pylab.plot(gibbs['xB_model'][i, 1:end]) pylab.axhspan(mock['lenses'][i].obs_images[0][1] - mock['lenses'][i].obs_images[1], mock['lenses'][i].obs_images[0][1] + mock['lenses'][i].obs_images[1], color='gray', alpha=0.5) pylab.subplot(3, 2, 3) pylab.plot(gibbs['radmagrat_model'][i, 1:end]) pylab.axhspan(mock['lenses'][i].obs_radmagrat[0] - mock['lenses'][i].obs_radmagrat[1], mock['lenses'][i].obs_radmagrat[0] + mock['lenses'][i].obs_radmagrat[1], color='gray', alpha=0.5) pylab.subplot(3, 2, 4) pylab.plot(gibbs['invH0'][1:end] * gibbs['dt_model'][i, 1:end] * 70.) pylab.axhspan(mock['lenses'][i].obs_timedelay[0]/day - mock['lenses'][i].obs_timedelay[1]/day, mock['lenses'][i].obs_timedelay[0]/day + mock['lenses'][i].obs_timedelay[1]/day, color='gray', alpha=0.5)
except: Pouts[kpl[ind]] = [Pout[:,ind]] Pouts_I[kpl[ind]] = [Pout_I[:,ind]] pCs[kpl[ind]] = [pC[:,ind]] pIs[kpl[ind]] = [pI[:,ind]] alphas[kpl[ind]] = [n.abs(Pin[:,ind]/Pout[:,ind])] alphas_I[kpl[ind]] = [n.abs(Pin[:,ind]/Pout_I[:,ind])] if opts.plot: p.figure(1) # Pin vs. Pout p.subplot(3, 7, ind+1) p.loglog(Pin[:,ind], Pout[:,ind], color) # points p.loglog([pklo, pkhi], [pklo, pkhi], 'k-') # diagonal line #p.axhline(pC[:,ind].max(), color=linecolor) # max pC p.axhspan(pC[:,ind].min(), pC[:,ind].max(), facecolor=linecolor, edgecolor=linecolor, alpha=0.5) p.grid(True) p.xlim(pklo, pkhi) p.ylim(pklo, pkhi) p.tick_params(axis='both', which='both', labelsize=6) p.title('kpl = '+str("%.4f" % kpl[ind]), fontsize=8) p.figure(2) # alpha vs. Pout p.subplot(3, 7, ind+1) p.loglog(Pin[:,ind]/Pout[:,ind],Pout[:,ind], color) # points p.axhline(pC[:,ind].max(), color=linecolor) # max pC p.grid(True) p.xlim(1e-3,1e7) p.ylim(pklo, pkhi) p.tick_params(axis='both', which='both', labelsize=6) p.title('kpl = '+str("%.4f" % kpl[ind]), fontsize=8)
ymax = 15 ax = plt.subplot(3, 2, 1) county_df.plot(x='date', y='new_cases_per_100k', ax=ax, color = 'blue') if len(county_df) > mean_len: county_df.plot(x='date', y='new_cases_per_100k_ra', ax=ax, color = 'black') plt.title(f'{state} - {county} - Cases per 100k') plt.xlabel('') plt.xticks(rotation =45, fontsize='small') xmin, xmax = plt.xlim() ymin_T, ymax_T = plt.ylim() ymax = max(ymax, ymax_T) plt.axhspan(7, ymax, color = "purple", alpha = 0.5) plt.axhspan(4, 7, color = "red", alpha = 0.5) plt.axhspan(1, 4, color = "orange", alpha = 0.5) plt.axhspan(ymin, 1, color = "yellow", alpha = 0.5) #plt.axhline(y = county_df['new_cases_per_100k'].iloc[-1], linestyle='dashed', color = 'blue') if len(county_df) > mean_len: plt.axhline(y = county_df['new_cases_per_100k_ra'].iloc[-1], linestyle='dashed', color = 'black') plt.axhline(y = county_df['new_cases_per_100k_ra'].iloc[-7], linestyle='dotted', color = 'black') yticks = list(plt.yticks()[0]) if len(county_df) > mean_len: yticks.append(county_df['new_cases_per_100k_ra'].iloc[-1]) yticks.append(county_df['new_cases_per_100k_ra'].iloc[-7]) yticks = sorted(yticks)
def draw_graph(getgraph, opts): # http://matplotlib.sourceforge.net/index.html fg = pl.figure() ax = fg.add_subplot(111) global graph_plots, all_points graph_plots, all_points = [], [] def draw_tradeoff_plots(ratio, points, attrs): # select tradeoff given ratio best = atos_lib.atos_client_results.select_tradeoff(points, ratio) # graph limits xmin = min([p.sizered for p in points]) xmax = max([p.sizered for p in points]) ymin = min([p.speedup for p in points]) ymax = max([p.speedup for p in points]) # number of points on ratio line nbtk = int((ratio >= 1 and 2 or 1 / ratio) * 32) # ratio line points coordinates xtk = [xmin + i * ((xmax - xmin) / nbtk) for i in range(nbtk + 1)] ytk = [best.speedup + (1.0 / ratio) * (best.sizered - x) for x in xtk] coords = filter(lambda (x, y): y >= ymin and y <= ymax, zip(xtk, ytk)) # first plot: selected tradeoff point attrs.update({'label': '_nolegend_'}) attrs.update({'markersize': 20, 'linewidth': 0, 'alpha': 0.4}) plots = [(([best.sizered], [best.speedup]), dict(attrs))] # second plot: ratio line attrs.update({'marker': '', 'linewidth': 2, 'linestyle': 'solid'}) plots += [((zip(*coords)[0], zip(*coords)[1]), dict(attrs))] return plots def draw_all(): global graph_plots, all_points, selected_points, similar_points # :( # remove old plots old_points = [(p.sizered, p.speedup, p.variant) for p in all_points] for x in list(graph_plots): graph_plots.remove(x) x.remove() # get graph values scatters, frontiers = getgraph() all_points = sum([x[0] for x in scatters + frontiers], []) # draw scatters for (points, attrs) in scatters: attrsmap = { 's': 20, 'label': '_nolegend_', 'zorder': 2, 'color': 'r', 'edgecolor': 'k'} attrsmap.update(attrs) xy = zip(*[(p.sizered, p.speedup) for p in points]) gr = ax.scatter(*xy, **attrsmap) graph_plots.append(gr) # draw frontiers (line plots) for (points, attrs) in frontiers: attrsmap = { 'color': 'r', 'marker': 'o', 'label': '_nolegend_', 'zorder': 2, 'markersize': 7, 'linestyle': 'dashed', 'linewidth': 2} attrsmap.update(attrs) xy = zip(*sorted([(p.sizered, p.speedup) for p in points])) gr, = ax.plot(xy[0], xy[1], **attrsmap) graph_plots.append(gr) # show tradeoffs for each frontier for ratio in opts.tradeoffs or []: for ((xcrd, ycrd), attrs) in \ draw_tradeoff_plots(ratio, points, dict(attrsmap)): graph_plots.append(ax.plot(xcrd, ycrd, **attrs)[0]) # draw selected points (hidden) if opts.show and all_points: # workaround pb with pick_event event ind (4000) attrsmap = { 'color': 'b', 'marker': 'o', 'markersize': 20, 'linewidth': 0, 'alpha': 0.4} xy = zip(*sorted([(p.sizered, p.speedup) for p in all_points])) selected_points, = \ ax.plot(xy[0], xy[1], visible=False, picker=4000, **attrsmap) graph_plots.append(selected_points) # similar point plot attrsmap.update({'color': 'g'}) similar_points, = ax.plot(None, None, visible=False, **attrsmap) graph_plots.append(similar_points) # highlight new points if opts.follow and old_points: new_points = [p for p in all_points if ( (p.sizered, p.speedup, p.variant) not in old_points)] attrsmap = { 'color': 'r', 'marker': 'o', 'markersize': 20, 'linewidth': 0, 'alpha': 0.4, 'zorder': 1} if new_points: xy = zip(*[(p.sizered, p.speedup) for p in new_points]) new_points, = ax.plot(*xy, **attrsmap) graph_plots.append(new_points) # redraw legend and figure if opts.xlim: pl.xlim([float(l) for l in opts.xlim.split(",")]) if opts.ylim: pl.ylim([float(l) for l in opts.ylim.split(",")]) ax.legend(loc='lower left') fg.canvas.draw() # dynamic annotations def on_pick(event): def closest(x, y): dp = [(math.hypot(p.sizered - x, p.speedup - y), p) for p in all_points] return sorted(dp)[0][1] def highlight(p): # print point on console print '-' * 40 + '\n' + point_str(p) # highlight point selected_points.set_visible(True) selected_points.set_data(p.sizered, p.speedup) # highlight similar points (same variant) sim = zip(*([(c.sizered, c.speedup) for c in all_points if c.variant == p.variant and c != p])) similar_points.set_visible(True) similar_points.set_data(sim and sim[0], sim and sim[1]) # selected point legend main_legend = ax.legend_ lg = point_str(p, short=True, no_id=opts.anonymous) lp = pl.legend( [selected_points], [lg], loc='lower right', numpoints=1) pl.setp(lp.get_texts(), fontsize='medium') lp.get_frame().set_alpha(0.5) pl.gca().add_artist(main_legend) fg.canvas.draw() ax.legend_ = main_legend highlight(closest(event.mouseevent.xdata, event.mouseevent.ydata)) # live plotting def on_timer(): draw_all() # draw graph for the first time draw_all() # graph title title = 'Optimization Space for %s' % ( opts.id or opts.targets or ( all_points and all_points[0].target)) if opts.refid: title += ' [ref=%s]' % opts.refid if opts.filter: title += ' [filter=%s]' % opts.filter # redraw axis, set labels, legend, grid, ... def labelfmt(x, pos=0): return '%.2f%%' % (100.0 * x) ax.xaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.title(title) pl.xlabel('size reduction (higher is better) -->') pl.ylabel('speedup (higher is better) -->') if opts.xlim: pl.xlim([float(l) for l in opts.xlim.split(",")]) if opts.ylim: pl.ylim([float(l) for l in opts.ylim.split(",")]) pl.grid(True) if opts.outfile: fg.savefig(opts.outfile) if opts.show: fg.canvas.mpl_connect('pick_event', on_pick) if opts.follow: timer = atos_lib.repeatalarm(on_timer, 5.0).start() pl.show() if opts.follow: timer.stop()
#! /usr/bin/env python import numpy as n import pylab as p file_eo = n.load('/data4/paper/ctc/PSA64/PAPER_METHODS/PSA64_FRF_RA.5_8.6_CHAN95_115_SEP0,1_JACKKNIFE_EVENODD/pspec_final_sep0,1.npz') file_bl = n.load('/data4/paper/ctc/PSA64/PAPER_METHODS/PSA64_FRF_RA.5_8.6_CHAN95_115_SEP0,1_JACKKNIFE_BASELINES/pspec_final_sep0,1.npz') file_lst = n.load('/data4/paper/ctc/PSA64/PAPER_METHODS/PSA64_FRF_RA.5_8.6_CHAN95_115_SEP0,1_JACKKNIFE_LST_FIRSTLAST/pspec_final_sep0,1.npz') data = n.load('/data4/paper/ctc/PSA64/PAPER_METHODS/PSA64_FRF_RA.5_8.6_CHAN95_115_SEP0,1_IDENTITYMULTWEIGHT/pspec_final_sep0,1_final.npz') p.errorbar(file_eo['kpl'],file_eo['pCv'],file_eo['pCv_err']*2,linestyle='',marker='.',color='b',label='Even/Odd Null Test') p.errorbar(file_bl['kpl']+0.005,file_bl['pCv'],file_bl['pCv_err']*2,linestyle='',marker='.',color='g',label='Baselines Null Test') p.errorbar(file_lst['kpl']+0.01,file_lst['pCv'],file_lst['pCv_err']*2,linestyle='',marker='.',color='m',label='LST Null Test') p.errorbar(data['kpl']-0.005,data['pCv'],data['pCv_err']*2,linestyle='',marker='.',color='k',label='Original Data') noise = 4436767.36822 # XXX p.axhline(0,color='k',linestyle='--') p.axhspan(-noise*2,noise*2,facecolor='0.5',edgecolor="none",alpha=0.5,label='Estimated $2\sigma$ Error') p.legend(numpoints=1,prop={'size':14},ncol=2) p.ylabel('$P(k)$ $[mK^{2}(h^{-1} Mpc)^{3}]$',fontsize=14) p.xlabel('$k_{\\parallel}$ [$h$ Mpc$^{-1}$]',fontsize=14) p.ylim(-3.5e8,10e8) p.grid() p.show()
radmagratmodel_grid[k] = lens.radmag_ratio else: logp_beta[k] = -np.inf logp_beta += -0.5*(beta_grid - mu_beta)**2/sig_beta**2 - np.log(sig_beta) logp_beta -= logp_beta.max() p_beta_grid = np.exp(logp_beta) pylab.plot(beta_grid, p_beta_grid) pylab.show() pylab.subplot(2, 2, 1) pylab.plot(beta_grid, dtmodel_grid) pylab.axhspan(dt_obs - dt_err, dt_obs + dt_err, color='gray', alpha=0.5) pylab.subplot(2, 2, 2) pylab.plot(beta_grid, radmagratmodel_grid) pylab.axhspan(radmagrat_obs - radmagrat_err, radmagrat_obs + radmagrat_err, color='gray', alpha=0.5) pylab.subplot(2, 2, 3) pylab.plot(beta_grid, xAmodel_grid) pylab.axhspan(xA_obs - xA_err, xA_obs + xA_err, color='gray', alpha=0.5) pylab.subplot(2, 2, 4) pylab.plot(beta_grid, xBmodel_grid) pylab.axhspan(xB_obs - xB_err, xB_obs + xB_err, color='gray', alpha=0.5) pylab.show()
def period_extract(all_data = False, ind_quarters = False, years = False, no_stars = 24): final_KID_list = [] ''' importing file of all KIDs''' all_targs = range(0,no_stars) #all_targs = ['01','02','03','04','05','06','07','08','09','10', ] #all_targs = np.genfromtxt('/Users/angusr/Documents/rotation/all_targets.txt').T ''' Extracting data from text files.''' data3 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss3_output/results.txt').T data4 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss4_output/results.txt').T data5 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss5_output/results.txt').T data6 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss6_output/results.txt').T data7 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss7_output/results.txt').T data8 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss8_output/results.txt').T data9 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss9_output/results.txt').T data10 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss10_output/results.txt').T data11 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss11_output/results.txt').T data12 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss12_output/results.txt').T data13 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss13_output/results.txt').T data14 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss14_output/results.txt').T KID3=np.array(range(1,no_stars+1)); periods3=data3[1][1:]; errors3=data3[6][1:]; sine3=data3[2][1:] KID4=np.array(range(1,no_stars+1)); periods4=data4[1][1:]; errors4=data3[6][1:]; sine4=data4[2][1:] KID5=np.array(range(1,no_stars+1)); periods5=data5[1][1:]; errors5=data3[6][1:]; sine5=data5[2][1:] KID6=np.array(range(1,no_stars+1)); periods6=data6[1][1:]; errors6=data3[6][1:]; sine6=data6[2][1:] KID7=np.array(range(1,no_stars+1)); periods7=data7[1][1:]; errors7=data3[6][1:]; sine7=data7[2][1:] KID8=np.array(range(1,no_stars+1)); periods8=data8[1][1:]; errors8=data3[6][1:]; sine8=data8[2][1:] KID9=np.array(range(1,no_stars+1)); periods9=data9[1][1:]; errors9=data3[6][1:]; sine9=data9[2][1:] KID10=np.array(range(1,no_stars+1)); periods10=data10[1][1:]; errors10=data3[6][1:]; sine10=data10[2][1:] KID11=np.array(range(1,no_stars+1)); periods11=data11[1][1:]; errors11=data3[6][1:]; sine11=data11[2][1:] KID12=np.array(range(1,no_stars+1)); periods12=data12[1][1:]; errors12=data3[6][1:]; sine12=data12[2][1:] KID13=np.array(range(1,no_stars+1)); periods13=data13[1][1:]; errors13=data3[6][1:]; sine13=data13[2][1:] KID14=np.array(range(1,no_stars+1)); periods14=data14[1][1:]; errors14=data3[6][1:]; sine14=data14[2][1:] # data3 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss3_output/Periods_ss3test.txt').T # data4 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss4_output/Periods_ss4test.txt').T # data5 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss5_output/Periods_ss5test.txt').T # data6 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss6_output/Periods_ss6test.txt').T # data7 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss7_output/Periods_ss7test.txt').T # data8 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss8_output/Periods_ss8test.txt').T # data9 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss9_output/Periods_ss9test.txt').T # data10 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss10_output/Periods_ss10test.txt').T # data11 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss11_output/Periods_ss11test.txt').T # data12 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss12_output/Periods_ss12test.txt').T # data13 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss13_output/Periods_ss13test.txt').T # data14 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss14_output/Periods_ss14test.txt').T # # data_q36 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss3-6_output/Periods_ss3-6test.txt').T # # data_q710 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss7-10_output/Periods_ss7-10test.txt').T # # data_q1114 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQss11-14_output/Periods_ss11-14test.txt').T # KID3 = data3[0]; periods3 = data3[1]; errors3 = data3[2]; sine3 = data3[3] # KID4 = data4[0]; periods4 = data4[1]; errors4 = data4[2]; sine4 = data4[3] # KID5 = data5[0]; periods5 = data5[1]; errors5 = data5[2]; sine5 = data5[3] # KID6 = data6[0]; periods6 = data6[1]; errors6 = data6[2]; sine6 = data6[3] # KID7 = data7[0]; periods7 = data7[1]; errors7 = data7[2]; sine7 = data7[3] # KID8 = data8[0]; periods8 = data8[1]; errors8 = data8[2]; sine8 = data8[3] # KID9 = data9[0]; periods9 = data9[1]; errors9 = data9[2]; sine9 = data9[3] # KID10 = data10[0]; periods10 = data10[1]; errors10 = data10[2]; sine10 = data10[3] # KID11 = data11[0]; periods11 = data11[1]; errors11 = data11[2]; sine11 = data11[3] # KID12 = data12[0]; periods12 = data12[1]; errors12 = data12[2]; sine12 = data12[3] # KID13 = data13[0]; periods13 = data13[1]; errors13 = data13[2]; sine13 = data13[3] # KID14 = data14[0]; periods14 = data14[1]; errors14 = data14[2]; sine14 = data14[3] # # KIDq710 = data_q710[0]; periodsq710 = data_q710[1]; errorsq710 = data_q710[2]; sineq710 = data_q710[3] # # KIDq1114 = data_q1114[0]; periodsq1114 = data_q1114[1]; errorsq1114 = data_q1114[2]; sineq1114 = data_q1114[3] # # KIDq36 = data_q36[0]; periodsq36 = data_q36[1]; errorsq36 = data_q36[2]; sineq36 = data_q36[3] print 'Found %d Targets in quarter 3' %len(KID3) print 'Found %d Targets in quarter 4' %len(KID4) print 'Found %d Targets in quarter 5' %len(KID5) print 'Found %d Targets in quarter 6' %len(KID6) print 'Found %d Targets in quarter 7' %len(KID7) print 'Found %d Targets in quarter 8' %len(KID8) print 'Found %d Targets in quarter 9' %len(KID9) print 'Found %d Targets in quarter 10' %len(KID10) print 'Found %d Targets in quarter 11' %len(KID11) print 'Found %d Targets in quarter 12' %len(KID12) print 'Found %d Targets in quarter 13' %len(KID13) print 'Found %d Targets in quarter 14' %len(KID14) # print 'Found %d Targets in quarters 3-6' %len(KIDq36) # print 'Found %d Targets in quarters 7-10' %len(KIDq710) # print 'Found %d Targets in quarters 11-14' %len(KIDq1114) if all_data == True: KID_method = [KID3, KID4, KID5, KID6, KID7, KID8, KID9, KID10, KID11, KID12, KID13, KID14, KIDq04, KIDq25, KIDq59, KIDq36, KIDq710, KIDq1114, KIDq09] period_method = [periods3, periods4, periods5, periods6, periods7, periods8, periods9, periods10, periods11, periods12, periods13, periods14, periodsq04, periodsq25, periodsq59, periodsq36, periodsq710, periodsq1114, periodsq09] error_method = [errors3, errors4, errors5, errors6, errors7, errors8, errors9 , errors10, errors11, errors12, errors13, errors14, errorsq04, errorsq25, errorsq59, errorsq36, errorsq710, errorsq1114, errorsq09] sine_method = [sine3, sine4, sine5, sine6, sine7, sine8, sine9, sine10, sine11, sine12, sine13, sine14, sineq04, sineq25, sineq59, sineq36, sineq710, sineq1114, sineq09] KID_meth_string = ['Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Qs 1-4', 'Qs 2-5', 'Qs 5-9', 'Qs 3-6', 'Qs 7-10', 'Qs 11-14', 'Qs 1-8'] xtick_values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] xtick_labels = ['3','4','5','6','7','8','9','10','11','12','13','14','1-4','2-5', '3-6', '5-9','7-10','11-14','1-8'] fig_dir = 'ss_all_data_figs' elif ind_quarters == True: KID_method = [KID3, KID4, KID5, KID6, KID7, KID8, KID9, KID10, KID11, KID12, KID13, KID14] period_method = [periods3, periods4, periods5, periods6, periods7, periods8, periods9, periods10, periods11, periods12, periods13, periods14] error_method = [errors3, errors4, errors5, errors6, errors7, errors8, errors9 , errors10, errors11, errors12, errors13, errors14] sine_method = [sine3, sine4, sine5, sine6, sine7, sine8, sine9, sine10, sine11, sine12, sine13, sine14] KID_meth_string = ['Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14'] xtick_values = [1,2,3,4,5,6,7,8,9,10,11,12] xtick_labels = ['3','4','5','6','7','8','9','10','11','12','13','14'] fig_dir = 'ss_ind_qs_figs' elif years == True: KID_method = [KIDq36, KIDq710, KIDq1114] period_method = [periodsq36, periodsq710, periodsq1114] error_method = [errorsq36, errorsq710, errorsq1114] sine_method = [sineq36, sineq710, sineq1114] KID_meth_string = ['Qs 3-6', 'Qs 7-10', 'Qs 11-14'] xtick_values = [1,2,3] xtick_labels = ['3-6','7-10','11-14'] fig_dir = 'ss_year_figs' '''Loop over all targets''' for i in range(0,len(all_targs)): print i if years == True: periods = [periodsq36[i], periodsq710[i], periodsq1114[i]] errors = [errorsq36[i], errorsq710[i], errorsq1114[i]] elif ind_quarters == True: periods = [periods3[i], periods4[i], periods5[i], periods6[i], periods7[i], periods8[i], periods9[i], periods10[i], periods11[i], \ periods12[i], periods13[i], periods14[i]] errors = [errors3[i], errors4[i], errors5[i], errors6[i], errors7[i], errors8[i], errors9[i], errors10[i], errors11[i], \ errors12[i], errors13[i], errors14[i]] ''' Find medians ''' acf_median = np.median(periods) '''PLOTTING''' KID1s = range(1,len(KID_method)+1) pylab.figure(1) pylab.clf() '''only plot if a period measurement was made. ''' for j in range(0,len(KID_method)): if periods[j] > 0. and errors[j] > 0.: pylab.errorbar(KID1s[j], periods[j], yerr = errors[j], marker = 'o', color = 'b', markersize = 5) else: pylab.axvline(j+1, linewidth = 0.5, color = 'r', linestyle = '--') pylab.ylabel('Period') upper_y_lim = max(periods) + 5 pylab.xlim(0, len(KID_meth_string)+1) pylab.ylim(0,upper_y_lim) '''Adding harmonic lines''' pylab.axhline(acf_median, linewidth = 0.5, color = 'b') pylab.axhline(acf_median/2., linewidth = 0.5, color = 'b', linestyle = '--') period_multiple = 0; harmonic=2 if acf_median > 0: while period_multiple < upper_y_lim: period_multiple = acf_median*harmonic pylab.axhline(period_multiple, linewidth = 0.5, color = 'b', linestyle = '--') harmonic += 1 KID_string = np.int(i) KID_string = np.str(KID_string) pylab.title('%s' %KID_string) '''Making sure that all measurements lie within 15% of the harmonic lines''' number_of_measurements = 0 number_of_good_measurements = 0 margin = acf_median/7.5 for p in range(0,len(periods)): if periods[p] > 0.0 and errors[p] > 0: number_of_measurements += 1 if acf_median - margin < periods[p] < acf_median + margin: number_of_good_measurements += 1 elif acf_median*2.0 - margin < periods[p] < acf_median*2.0 + margin: number_of_good_measurements += 1 elif acf_median*3.0 - margin < periods[p] < acf_median*3.0 + margin: number_of_good_measurements += 1 elif acf_median*0.5 - margin < periods[p] < acf_median*0.5 + margin: number_of_good_measurements += 1 pylab.axhspan(acf_median - margin, acf_median + margin, facecolor='b', alpha=0.1) pylab.axhspan(acf_median*2.0 - margin, acf_median*2.0 + margin, facecolor='b', alpha=0.1) pylab.axhspan(acf_median*3.0 - margin, acf_median*3.0 + margin, facecolor='b', alpha=0.1) pylab.axhspan(acf_median*0.5 - margin, acf_median*0.5 + margin, facecolor='b', alpha=0.1) '''Requires that periods are measured in at least 2/3''' if years == True: relax = 1.0 else: relax = 2./3. if number_of_measurements < int((len(KID_meth_string))*relax): pylab.text(0,0, 'MISSING MEASUREMENTS, require %s/%s' %( int((len(KID_meth_string))*2./3.), len(KID_meth_string))) pylab.axvspan(0, 120, facecolor='r', alpha=0.07) selected = False #require that 2/3 measurements are good! else: bonus = number_of_measurements - int((len(KID_meth_string))*relax) + 1 outliers = number_of_measurements - number_of_good_measurements if outliers > bonus: pylab.axvspan(0, 20, facecolor='r', alpha=0.07) pylab.text(0,0, 'INCONSISTENT MEASUREMENTS, require < %s outliers' %(outliers - bonus + 3)) selected = False else: final_KID_list.append((np.float(KID_string))+1) selected = True pylab.xticks(xtick_values, xtick_labels) #print KID_string #if years == True and selected == True: #print max(periods) - min(periods) pylab.savefig('/Users/angusr/angusr/ACF/%s/%s.pdf' %(fig_dir, KID_string)) '''The difference between newfigs2 and newfigs3 is that newfigs3 has data that has had the ACF code run on it more recently newfigs5 has all data, all_data_figs has all data, ind_qs_figs will have individual quarters only and year_figs will have just long term data''' print final_KID_list print '%s targets selected' %len(final_KID_list) if ind_quarters == True: txt_tit = 'ss_ind_quarterstest' elif years == True: txt_tit = 'ss_yearstest' elif all_data == True: txt_tit = 'ss_all_datatest' np.savetxt('/Users/angusr/angusr/ACF/star_spot_sim/%s.txt' %txt_tit, final_KID_list) return
def main(args): """Simulate thermal regulation (because it's easier to test here than in hardware) """ outpath = None if args: outpath = args[0] # general parameter tweaking #mAh = 3000.0 * 4 # BLF Q8 #mAh = 3000.0 mAh = 700.0 #mAh = 200.0 room_temp = 22 maxtemp = 50 mintemp = maxtemp - 10 thermal_mass = 32 # bigger heat sink = higher value thermal_lag = [room_temp] * 8 prediction_strength = 4 diff_attenuation = 6 lowpass = 8 samples = 8 total_power = 1.0 # max 1.0 timestep = 0.5 # thermal regulation runs every 0.5 seconds # Power level, with max=255 # 64 steps #ramp = [ 1,1,1,1,1,2,2,2,2,3,3,4,5,5,6,7,8,9,10,11,13,14,16,18,20,22,24,26,29,32,34,38,41,44,48,51,55,60,64,68,73,78,84,89,95,101,107,113,120,127,134,142,150,158,166,175,184,193,202,212,222,233,244,255 ] # 128 steps #ramp = [ 1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,10,10,11,12,12,13,14,15,16,17,17,18,19,20,21,22,24,25,26,27,28,30,31,32,34,35,37,39,40,42,44,45,47,49,51,53,55,57,59,61,63,66,68,70,73,75,78,80,83,86,88,91,94,97,100,103,106,109,113,116,119,123,126,130,134,137,141,145,149,153,157,161,166,170,174,179,183,188,193,197,202,207,212,217,222,228,233,238,244,249,255 ] # stable temperature at each level, with max=255 #temp_ramp = [max(room_temp,(lvl/255.0*total_power*200.0)) for lvl in ramp] ramp_7135 = [4, 4, 5, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 20, 22, 23, 25, 27, 30, 32, 34, 37, 40, 42, 45, 48, 52, 55, 59, 62, 66, 70, 74, 79, 83, 88, 93, 98, 104, 109, 115, 121, 127, 133, 140, 146, 153, 160, 168, 175, 183, 191, 200, 208, 217, 226, 236, 245] ramp_FET = [0, 2, 3, 4, 5, 7, 8, 9, 11, 12, 14, 15, 17, 18, 20, 22, 23, 25, 27, 29, 30, 32, 34, 36, 38, 40, 42, 44, 47, 49, 51, 53, 56, 58, 60, 63, 66, 68, 71, 73, 76, 79, 82, 85, 87, 90, 93, 96, 100, 103, 106, 109, 113, 116, 119, 123, 126, 130, 134, 137, 141, 145, 149, 153, 157, 161, 165, 169, 173, 178, 182, 186, 191, 196, 200, 205, 210, 214, 219, 224, 229, 234, 239, 244, 250, 255] tadd_7135 = 5.0 tadd_FET = 300.0 ramp = [x/57.0 for x in ramp_7135] + [x for x in ramp_FET] temp_ramp = [room_temp + (x/255.0*tadd_7135) for x in ramp_7135] temp_ramp.extend([room_temp + tadd_7135 + (x/255.0*tadd_FET) for x in ramp_FET]) title = 'mAh: %i, Mass: %i, Lag: %i, Ramp: %i, Samples: %i, Predict: %i, Lowpass: %i, Att: %i' % \ (mAh, thermal_mass, len(thermal_lag), len(ramp), samples, prediction_strength, lowpass, diff_attenuation) lvl = int(len(ramp) * 1.0) # never regulate lower than this lowest_stepdown = len(ramp) / 4 temperatures = [room_temp] * samples actual_lvl = lvl overheat_count = 0 underheat_count = 0 seconds = 0 max_seconds = int(mAh * 60.0 / 100.0 * 1.5) g_Temit = [] g_Tdrv = [] g_act_lvl = [] g_lvl = [] g_sag = [] g_lm = [] times = [] def voltage_sag(seconds): runtime = float(max_seconds) return math.pow((runtime - seconds) / runtime, 1.0/9) def get_drv_temp(): half = int(len(thermal_lag) / 2.0) this = sum(thermal_lag[:half]) / float(half) val = room_temp + ((this - room_temp) * 0.8) #val *= voltage_sag(seconds) val = int(val) #val += random.choice((-1, 0, 0, 1)) val += random.choice((-2, -1, 0, 1, 2)) #val += random.choice((-3, -2, -1, 0, 1, 2, 3)) del get_drv_temp.values[0] get_drv_temp.values.append(val) result = sum(get_drv_temp.values) / 1.0 result = max(room_temp*get_drv_temp.adjust, result) return int(result) get_drv_temp.values = [room_temp]*4 get_drv_temp.adjust = 4.0 def get_temp(): now = thermal_lag[-1] val = room_temp + (voltage_sag(seconds) * (now - room_temp)) #val *= voltage_sag(seconds) result = val #result = max(room_temp, result) return result while True: # apply heat step target_temp = temp_ramp[actual_lvl-1] target_temp = math.pow(target_temp, 1.0/1.01) #current_temp = thermal_lag[-1] current_temp = get_temp() diff = float(target_temp - current_temp) current_temp += (diff/thermal_mass) current_temp = max(room_temp, current_temp) # shift temperatures for i in range(len(thermal_lag)-1): thermal_lag[i] = thermal_lag[i+1] thermal_lag[-1] = current_temp # thermal regulation algorithm for i in range(len(temperatures)-1): temperatures[i] = temperatures[i+1] drv_temp = get_drv_temp() temperatures[i] = drv_temp diff = int(drv_temp - temperatures[0]) projected = drv_temp + (diff<<prediction_strength) #THERM_CEIL = maxtemp<<2 #THERM_FLOOR = mintemp<<2 THERM_CEIL = maxtemp * get_drv_temp.adjust THERM_FLOOR = mintemp * get_drv_temp.adjust if projected > THERM_CEIL: underheat_count = 0 if overheat_count > lowpass: overheat_count = 0 exceed = int(projected-THERM_CEIL) >> diff_attenuation exceed = max(1, exceed) stepdown = actual_lvl - exceed if (stepdown >= lowest_stepdown): actual_lvl = stepdown else: overheat_count += 1 elif projected < THERM_FLOOR: overheat_count = 0 if underheat_count > (lowpass/2): underheat_count = 0 if (actual_lvl < lvl): actual_lvl += 1 else: underheat_count += 1 # show progress #print('T=%i: %i/%i, Temit=%i, Tdrv=%i' % ( # seconds, actual_lvl, lvl, current_temp, drv_temp)) g_Temit.append(current_temp) g_Tdrv.append(drv_temp/get_drv_temp.adjust) g_act_lvl.append(150 * float(actual_lvl) / len(ramp)) g_lvl.append(150 * float(lvl) / len(ramp)) g_sag.append(voltage_sag(seconds) * 150.0) g_lm.append((voltage_sag(seconds)**2) * ramp[actual_lvl-1] / 255.0 * 150.0) times.append(seconds/60.0) #time.sleep(0.1) seconds += timestep if seconds > max_seconds: break pl.figure(dpi=100, figsize=(12,4)) pl.suptitle(title, fontsize=14) pl.plot(times, g_Temit, label='Temit', linewidth=2) pl.plot(times, g_Tdrv, label='Tdrv', linewidth=2) pl.plot(times, g_act_lvl, label='actual lvl', linewidth=2) pl.plot(times, g_lvl, label='target lvl', linewidth=2) pl.plot(times, g_sag, label='voltage sag', linewidth=2) pl.plot(times, g_lm, label='lumens', linewidth=2) pl.axhspan(mintemp, maxtemp, facecolor='grey', alpha=0.25) #pl.axhline(y=mintemp, color='black', linewidth=1) pl.xlim((-0.05,times[-1]+0.05)) pl.xlabel('Minutes') pl.ylabel('Temp (C), PWM') pl.legend(loc=0) if outpath: pl.savefig(outpath, dpi=70, frameon=False, bbox_inches='tight') pl.show()
def period_extract(all_data = False, ind_quarters = False, years = False): final_KID_list = [] # Load data data = np.genfromtxt('/Users/angusr/Desktop/astero_ages.txt').T all_targs = data[0] # Load results data3 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ3_output/Periods_3.txt').T data4 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ4_output/Periods_4.txt').T data5 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ5_output/Periods_5.txt').T data6 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ6_output/Periods_6.txt').T data7 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ7_output/Periods_7.txt').T data8 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ8_output/Periods_8.txt').T data9 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ9_output/Periods_9.txt').T data10 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ10_output/Periods_10.txt').T data11 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ11_output/Periods_11.txt').T data12 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ12_output/Periods_12.txt').T data13 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ13_output/Periods_13.txt').T data14 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ14_output/Periods_14.txt').T data15 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ15_output/Periods_15.txt').T data16 = np.genfromtxt('/Users/angusr/angusr/ACF/PDCQ16_output/Periods_16.txt').T KID3 = data3[0] periods3 = data3[1] errors3 = data3[2] sine3 = data3[3] KID4 = data4[0] periods4 = data4[1] errors4 = data4[2] sine4 = data4[3] KID5 = data5[0] periods5 = data5[1] errors5 = data5[2] sine5 = data5[3] KID6 = data6[0] periods6 = data6[1] errors6 = data6[2] sine6 = data6[3] KID7 = data7[0] periods7 = data7[1] errors7 = data7[2] sine7 = data7[3] KID8 = data8[0] periods8 = data8[1] errors8 = data8[2] sine8 = data8[3] KID9 = data9[0] periods9 = data9[1] errors9 = data9[2] sine9 = data9[3] KID10 = data10[0] periods10 = data10[1] errors10 = data10[2] sine10 = data10[3] KID11 = data11[0] periods11 = data11[1] errors11 = data11[2] sine11 = data11[3] KID12 = data12[0] periods12 = data12[1] errors12 = data12[2] sine12 = data12[3] KID13 = data13[0] periods13 = data13[1] errors13 = data13[2] sine13 = data13[3] KID14 = data14[0] periods14 = data14[1] errors14 = data14[2] sine14 = data14[3] KID15 = data15[0] periods15 = data15[1] errors15 = data15[2] sine15 = data15[3] KID16 = data16[0] periods16 = data16[1] errors16 = data16[2] sine16 = data16[3] print 'Found %d Targets in quarter 3' %len(KID3) print 'Found %d Targets in quarter 4' %len(KID4) print 'Found %d Targets in quarter 5' %len(KID5) print 'Found %d Targets in quarter 6' %len(KID6) print 'Found %d Targets in quarter 7' %len(KID7) print 'Found %d Targets in quarter 8' %len(KID8) print 'Found %d Targets in quarter 9' %len(KID9) print 'Found %d Targets in quarter 10' %len(KID10) print 'Found %d Targets in quarter 11' %len(KID11) print 'Found %d Targets in quarter 12' %len(KID12) print 'Found %d Targets in quarter 13' %len(KID13) print 'Found %d Targets in quarter 14' %len(KID14) print 'Found %d Targets in quarter 15' %len(KID15) print 'Found %d Targets in quarter 16' %len(KID16) KID_method = [KID3, KID4, KID5, KID6, KID7, KID8, KID9, KID10, KID11, KID12, \ KID13, KID14, KID15, KID16] period_method = [periods3, periods4, periods5, periods6, periods7, periods8, \ periods9, periods10, periods11, periods12, periods13, periods14, periods15, periods16] error_method = [errors3, errors4, errors5, errors6, errors7, errors8, \ errors9 , errors10, errors11, errors12, errors13, errors14, errors15, errors16] sine_method = [sine3, sine4, sine5, sine6, sine7, sine8, sine9, sine10, \ sine11, sine12, sine13, sine14, sine15, sine16] KID_meth_string = ['Q3', 'Q4', 'Q5', 'Q6', 'Q7', 'Q8', 'Q9', 'Q10', 'Q11', 'Q12', 'Q13', 'Q14', 'Q15', 'Q16'] xtick_values = [1,2,3,4,5,6,7,8,9,10,11,12, 13, 14] xtick_labels = ['$\mathrm{3}$','$\mathrm{4}$','$\mathrm{5}$','$\mathrm{6}$','$\mathrm{7}$','$\mathrm{8}$',\ '$\mathrm{9}$','$\mathrm{10}$','$\mathrm{11}$','$\mathrm{12}$','$\mathrm{13}$','$\mathrm{14}$',\ '$\mathrm{15}$', '$\mathrm{16}$'] fig_dir = 'ind_qs_figs' # Loop over all targets all_KIDs = [] all_periods = [] all_errors = [] all_sines = [] for i in all_targs: #Assemble master list of all targets with periods, errors and sines. KIDs, periods, errors, sines = find_periods(i, KID_method, period_method, error_method, sine_method) all_KIDs.append(KIDs) all_periods.append(periods) all_errors.append(errors) all_sines.append(sines) # Find medians acf_median = np.median(periods) acf_errs = np.median(errors) sine_median = np.median(sines) # Find rms about median acf_rms = np.sqrt((sum((periods - acf_median)**2))/np.float(len(periods))) sine_rms = np.sqrt((sum((sines - sine_median)**2))/np.float(len(sines))) diff = sine_median - acf_median # PLOTTING KID1s = range(1,len(KID_method)+1) fake_errors = np.zeros(len(KID_method)) pl.figure(1) pl.clf() # Only plot if a period measurement was made. for j in range(0,len(KID_method)): if periods[j] > 0. and errors[j] > 0.: pl.errorbar(KID1s[j], periods[j], yerr = errors[j], marker = 'o', color = 'k', markersize = 3, capsize = 0, ecolor = '0.7') # else: pl.axvline(j+1, linewidth = 0.5, color = cols[1], linestyle = '--') if sines[j] > 0.: red_herring = 0 #pl.errorbar(KID1s[j], sines[j], yerr = fake_errors[j], marker = 'o', color = cols[1], markersize = 5) pl.ylabel('$\mathrm{Rotation~Period~(Days)}$') pl.xlabel('$\mathrm{Quarter}$') #if max(sines)>max(periods): # upper_y_lim = max(sines) + 5 #else: upper_y_lim = max(periods) + 5 pl.xlim(0, len(KID_meth_string)+1) pl.ylim(0,upper_y_lim) #pl.axhline(sine_median, linewidth = 0.5, color = cols[1]) '''Adding harmonic lines''' pl.axhline(acf_median, linewidth = 0.5, color = cols[1]) pl.axhline(acf_median/2., linewidth = 0.5, color = cols[1], linestyle = '--') period_multiple = 0; harmonic=2 if acf_median != 0: while period_multiple < upper_y_lim: period_multiple = acf_median*harmonic pl.axhline(period_multiple, linewidth = 0.5, color = cols[1], linestyle = '--') harmonic += 1 #if 0<sine_median<1000 and sine_median != acf_median: #red_herring = 0 #pl.text(10.1, sine_median, 'Sine', color = cols[1]) #if 0<acf_median<1000: #pl.text(10.1, acf_median, 'ACF', color = cols[1]) KID_string = np.int(i) KID_string = np.str(KID_string) pl.title('$\mathrm{%s}$' %KID_string) '''Making sure that all measurements lie within 15% of the harmonic lines''' number_of_measurements = 0 number_of_good_measurements = 0 margin = acf_median/7.5 for p in range(0,len(periods)): if periods[p] > 0.0 and errors[p] > 0: number_of_measurements += 1 #if acf_median - acf_median/margin < periods[p] < acf_median + acf_median/margin: if acf_median - margin < periods[p] < acf_median + margin: number_of_good_measurements += 1 #elif acf_median*2.0 - acf_median*2.0/margin < periods[p] < acf_median*2.0 + acf_median*2.0/margin: elif acf_median*2.0 - margin < periods[p] < acf_median*2.0 + margin: number_of_good_measurements += 1 elif acf_median*3.0 - margin < periods[p] < acf_median*3.0 + margin: number_of_good_measurements += 1 #elif acf_median*0.5 - acf_median*0.5/margin < periods[p] < acf_median*0.5 + acf_median*0.5/margin: elif acf_median*0.5 - margin < periods[p] < acf_median*0.5 + margin: number_of_good_measurements += 1 ##elif sine_median - sine_median/(margin*2) < periods[p] < sine_median + sine_median/(margin*2): ##number_of_good_measurements += 1 #pl.axhspan(acf_median - acf_median/margin, acf_median + acf_median/margin, facecolor=cols[1], alpha=0.1) #pl.axhspan(acf_median*2.0 - acf_median*2.0/margin, acf_median*2.0 + acf_median*2.0/margin, facecolor=cols[1], alpha=0.1) #pl.axhspan(acf_median*0.5 - acf_median*0.5/margin, acf_median*0.5 + acf_median*0.5/margin, facecolor=cols[1], alpha=0.1) ##pl.axhspan(sine_median - sine_median/(margin*2), sine_median + sine_median/(margin*2), facecolor=cols[1], alpha=0.1) pl.axhspan(acf_median - margin, acf_median + margin, facecolor=cols[1], alpha=0.2) pl.axhspan(acf_median*2.0 - margin, acf_median*2.0 + margin, facecolor=cols[1], alpha=0.2) pl.axhspan(acf_median*3.0 - margin, acf_median*3.0 + margin, facecolor=cols[1], alpha=0.2) pl.axhspan(acf_median*0.5 - margin, acf_median*0.5 + margin, facecolor=cols[1], alpha=0.2) '''Requires that periods are measured in at least 2/3''' if years == True: relax = 1.0 else: relax = 2./3. if number_of_measurements < int((len(KID_meth_string))*relax): #pl.text(0,0, 'MISSING MEASUREMENTS, require %s/%s' %( int((len(KID_meth_string))*2./3.), len(KID_meth_string))) pl.axvspan(0, 120, facecolor=cols[1], alpha=0.07) selected = False #require that 2/3 measurements are good! else: bonus = number_of_measurements - int((len(KID_meth_string))*relax) + 1 outliers = number_of_measurements - number_of_good_measurements if outliers > bonus: pl.axvspan(0, 20, facecolor=cols[1], alpha=0.07) #pl.text(0,0, 'INCONSISTENT MEASUREMENTS, require < %s outliers' %(outliers - bonus + 3)) selected = False else: final_KID_list.append(KID_string) print acf_median, i mps.append(acf_median) ids.append(int(i)) nerrs.append(acf_errs) selected = True pl.xticks(xtick_values, xtick_labels) print KID_string #if years == True and selected == True: #print max(periods) - min(periods) pl.savefig('/Users/angusr/angusr/ACF/%s/%s.pdf' %(fig_dir, KID_string)) #raw_input('enter') print final_KID_list print '%s targets selected' %len(final_KID_list) if ind_quarters == True: txt_tit = 'ind_quarters' elif years == True: txt_tit = 'years' elif all_data == True: txt_tit = 'all_data' # np.savetxt('/Users/angusr/Desktop/results.txt', final_KID_list) np.savetxt('/Users/angusr/Desktop/results2.txt', (ids, mps, nerrs)) return
def _lss_corr(obs,interactive=True,maxcr=False,figno=20, rawxy=None, target='target', chatter=0): '''determine the LSS correction for the readout streak source ''' import os import numpy as np try: from astropy.io import fits except: import pyfits as fits from pylab import figure,imshow,ginput,axvspan,\ axhspan,plot,autumn,title,clf file = obs['infile'] ext = obs['extension'] cols = obs['streak_col_SN_CR_ERR'] kol = [] countrates=[] circle=[np.sin(np.arange(0,2*np.pi,0.05)),np.cos(np.arange(0,2*np.pi,0.05))] for k in cols: kol.append(k[1]) # S/N countrates.append(k[2]) kol = np.array(kol) if len(kol) == 0: print("zero array kol in _lss_corr ???!!!!") print("LSS correction cannot be determined.") return 1.0, (0,0), (1100.5,1100.5) k_sn_max = np.where(np.max(kol) == kol) # maximum s/n column print("k S/N max=",k_sn_max[0][0]) kol = [] for k in cols: kol.append(k[0]) # column number relative to bottom rh corner subimage if len(kol) == 0: print("LSS correction cannot be determined.") return 1.0, (0,0), (1100.5,1100.5) im=fits.getdata(file, ext=ext) hdr=fits.getheader(file, ext=ext) #binx = hdr['binx'] mn = im.mean() sig = im.std() # plot figure(figno) clf() autumn() imshow(im,vmin=mn-0.2*sig,vmax=mn+2*sig) if rawxy != None: rawx,rawy = rawxy R = hdr['windowdx']/15. plot(R*circle[0]+rawx-hdr['windowy0'],R*circle[1]+rawy-hdr['windowx0'], '-',color='m',alpha=0.7,lw=2) title(u"PUT CURSOR on your OBJECT",fontsize=16) if not maxcr: count = 0 for k in kol: axvspan(k-6,k+6,0.01,0.99, facecolor='k',alpha=0.3-0.02*count) count += 1 else: k = k_sn_max[0][0] axvspan(k-10,k+10,0.01,0.99, facecolor='k',alpha=0.2) happy = False skip = False count = 0 while not happy : print("put the cursor on the location of your source") count += 1 coord = ginput(timeout=0) print("selected:",coord) if len(coord[0]) == 2: print("window corner:", hdr['windowx0'],hdr['windowy0']) xloc = coord[0][0]+hdr['windowx0'] yloc = coord[0][1]+hdr['windowy0'] print("on detector (full raw image) should be :",xloc,yloc) #plot(coord[0][0],coord[0][1],'+',markersize=14,color='k',lw=2) #can't see it axhspan(coord[0][1]-6,coord[0][1]+6,0,1,facecolor='k',alpha=0.3) if rawxy != None: rawx,rawy = rawxy R = hdr['windowdx']/15. plot(R*circle[0]+rawx-hdr['windowx0'],R*circle[1]+rawy-hdr['windowy0'], '-',color='k',alpha=0.7,lw=1) #plot(rawx-hdr['windowx0'],rawy-hdr['windowy0'],'o',markersize=25,color='w',alpha=0.3) ans = raw_input("happy (yes,skip,no): ") if len(ans) > 0: if ans.upper()[0] == 'Y': happy = True if ans.upper()[0] == 'S': return 0.0, coord[0], (yloc+104,xloc+78) else: print("no position found") if count > 10: print("Too many tries: aborting" ) happy = True im = '' try: lss = 1.0 band = obs['band'] caldb = os.getenv('CALDB') command = "quzcif swift uvota - "+band.upper()+\ " SKYFLAT "+\ obs['dateobs'].split('T')[0]+" "+\ obs['dateobs'].split('T')[1]+" - > lssfile.1234.tmp" print(command) os.system(command) f = open('lssfile.1234.tmp') lssfile = f.readline() f.close() f = fits.getdata(lssfile.split()[0],ext=int(lssfile.split()[1])) lss = f[ yloc,xloc] print("lss correction = ",lss," coords=",coord[0], (yloc+104,xloc+78)) return lss, coord[0], (yloc+104,xloc+78) except: print("LSS correction cannot be determined.") return 1.0, (0,0), (1100.5,1100.5)
frameClock.reset() myStim.setPhase(1.0 / nFrames, '+') #advance the phase (add 1.0/nFrames to prev value) txt.setText('frameN = %i' % frameN) myStim.draw() txt.draw() print 'time to draw gabor: %.5f' % (frameClock.getTime()) myWin.flip() avg = myClock.getTime() / nFrames myWin.close() # plot in ms frameTimes = pylab.array(myWin.frameIntervals) * 1000 #convert to ms # horiz line at the mean pylab.axhspan(avg * 1000, avg * 1000, linewidth=1, linestyle='dotted') pylab.plot(frameTimes, '-o') # vertical line intersects sorted points at the median: pylab.axvspan(len(frameTimes) / 2, len(frameTimes) / 2, .05, .95, linewidth=1, linestyle='dotted') #frameTimes.sort() # plot sorted times on the same graph: #pylab.plot(frameTimes, '-o') # a faint box based on the refreshThreshold, relative to the measured average: pylab.axhspan(myWin._refreshThreshold * 1000, (2 * avg - myWin._refreshThreshold) * 1000,
def _lss_corr(obs, interactive=True, maxcr=False, figno=20, rawxy=None, target='target', chatter=0): '''determine the LSS correction for the readout streak source ''' import os import numpy as np try: from astropy.io import fits except: import pyfits as fits from pylab import figure,imshow,ginput,axvspan,\ axhspan,plot,autumn,title,clf file = obs['infile'] ext = obs['extension'] cols = obs['streak_col_SN_CR_ERR'] kol = [] countrates = [] circle = [ np.sin(np.arange(0, 2 * np.pi, 0.05)), np.cos(np.arange(0, 2 * np.pi, 0.05)) ] for k in cols: kol.append(k[1]) # S/N countrates.append(k[2]) kol = np.array(kol) if len(kol) == 0: print "zero array kol in _lss_corr ???!!!!" print "LSS correction cannot be determined." return 1.0, (0, 0), (1100.5, 1100.5) k_sn_max = np.where(np.max(kol) == kol) # maximum s/n column print "k S/N max=", k_sn_max[0][0] kol = [] for k in cols: kol.append(k[0]) # column number relative to bottom rh corner subimage if len(kol) == 0: print "LSS correction cannot be determined." return 1.0, (0, 0), (1100.5, 1100.5) im = fits.getdata(file, ext=ext) hdr = fits.getheader(file, ext=ext) #binx = hdr['binx'] mn = im.mean() sig = im.std() # plot figure(figno) clf() autumn() imshow(im, vmin=mn - 0.2 * sig, vmax=mn + 2 * sig) if rawxy != None: rawx, rawy = rawxy R = hdr['windowdx'] / 15. plot(R * circle[0] + rawx - hdr['windowy0'], R * circle[1] + rawy - hdr['windowx0'], '-', color='m', alpha=0.7, lw=2) title(u"PUT CURSOR on your OBJECT", fontsize=16) if not maxcr: count = 0 for k in kol: axvspan(k - 6, k + 6, 0.01, 0.99, facecolor='k', alpha=0.3 - 0.02 * count) count += 1 else: k = k_sn_max[0][0] axvspan(k - 10, k + 10, 0.01, 0.99, facecolor='k', alpha=0.2) happy = False skip = False count = 0 while not happy: print "put the cursor on the location of your source" count += 1 coord = ginput(timeout=0) print "selected:", coord if len(coord[0]) == 2: print "window corner:", hdr['windowx0'], hdr['windowy0'] xloc = coord[0][0] + hdr['windowx0'] yloc = coord[0][1] + hdr['windowy0'] print "on detector (full raw image) should be :", xloc, yloc #plot(coord[0][0],coord[0][1],'+',markersize=14,color='k',lw=2) #can't see it axhspan(coord[0][1] - 6, coord[0][1] + 6, 0, 1, facecolor='k', alpha=0.3) if rawxy != None: rawx, rawy = rawxy R = hdr['windowdx'] / 15. plot(R * circle[0] + rawx - hdr['windowx0'], R * circle[1] + rawy - hdr['windowy0'], '-', color='k', alpha=0.7, lw=1) #plot(rawx-hdr['windowx0'],rawy-hdr['windowy0'],'o',markersize=25,color='w',alpha=0.3) ans = raw_input("happy (yes,skip,no): ") if len(ans) > 0: if ans.upper()[0] == 'Y': happy = True if ans.upper()[0] == 'S': return 0.0, coord[0], (yloc + 104, xloc + 78) else: print "no position found" if count > 10: print "Too many tries: aborting" happy = True im = '' try: lss = 1.0 band = obs['band'] caldb = os.getenv('CALDB') command = "quzcif swift uvota - "+band.upper()+\ " SKYFLAT "+\ obs['dateobs'].split('T')[0]+" "+\ obs['dateobs'].split('T')[1]+" - > lssfile.1234.tmp" print command os.system(command) f = open('lssfile.1234.tmp') lssfile = f.readline() f.close() f = fits.getdata(lssfile.split()[0], ext=int(lssfile.split()[1])) lss = f[yloc, xloc] print "lss correction = ", lss, " coords=", coord[0], (yloc + 104, xloc + 78) return lss, coord[0], (yloc + 104, xloc + 78) except: print "LSS correction cannot be determined." return 1.0, (0, 0), (1100.5, 1100.5)
#Read in xfrac files f1 = np.loadtxt(one_filename) f2 = np.loadtxt(two_filename) f3 = np.loadtxt(three_filename) f4 = np.loadtxt(four_filename) # Planck value and uncertainty z_p = np.linspace(6,22) tau_p = 0.066*np.ones(len(z_p)) upper_tau = (0.066+0.013)#*np.ones(len(z_p)) lower_tau = (0.066-0.013)#*np.ones(len(z_p)) #Plot volume and mass neutral fractions pl.figure() pl.plot(z_p,tau_p,'k:') pl.axhspan(lower_tau, upper_tau, facecolor='0.5', alpha=0.1, edgecolor='none') pl.plot(f1[:,0],f1[:,1],'r-',lw=1.5,label='L1') pl.plot(f2[:,0],f2[:,1],'b:',lw=2,label='L2') pl.plot(f3[:,0],f3[:,1],'g--',lw=2,label='L3') pl.plot(f4[:,0],f4[:,1],'m-.',lw=2,label='L4') pl.xlim([5.7,16.2]) pl.ylim([0,0.09]) pl.minorticks_on() #pl.title('Mass-weighted') pl.xlabel('$z$') pl.ylabel('$\\tau_{\\rm{es}}$') leg = pl.legend(loc='lower right',prop={'size':12}) leg.draw_frame(False) pl.savefig('./png/tau_244Mpc_250.png') pl.savefig('./eps/tau_244Mpc_250.eps')
for frameN in range(nFrames): frameClock.reset() myStim.setPhase(1.0/nFrames, '+') #advance the phase (add 1.0/nFrames to prev value) txt.setText('frameN = %i' %frameN) myStim.draw() txt.draw() print 'time to draw gabor: %.5f' %(frameClock.getTime()) myWin.flip() avg = myClock.getTime()/nFrames myWin.close() # plot in ms frameTimes=pylab.array(myWin.frameIntervals)*1000 #convert to ms # horiz line at the mean pylab.axhspan(avg*1000, avg*1000, linewidth=1, linestyle='dotted') pylab.plot(frameTimes, '-o') # vertical line intersects sorted points at the median: pylab.axvspan(len(frameTimes)/2, len(frameTimes)/2, .05, .95, linewidth=1, linestyle='dotted') #frameTimes.sort() # plot sorted times on the same graph: #pylab.plot(frameTimes, '-o') # a faint box based on the refreshThreshold, relative to the measured average: pylab.axhspan(myWin._refreshThreshold*1000, (2*avg - myWin._refreshThreshold)*1000, linewidth=1, linestyle='dotted', alpha=.08) # transparent box above/below the mean # invisible points (alpha=0 --> fully transparent) help to set scale more nicely than autoscale: pylab.plot([5+max(frameTimes)],'-o',alpha=0) pylab.plot([0],'-o',alpha=0) # add some description:
def hspans(d, c): plt.axhline(np.mean(d), color=c, lw=2) plt.axhspan(np.mean(d)-np.std(d), np.mean(d)+np.std(d), facecolor=c, alpha=0.1, edgecolor=c)
plt.bar(edges, counts, widths) # highlighted lightcurve if lc: if nplots == 2: ax = fig.add_subplot(1, 2, 2) plt.scatter(data[timecolumn], data[bincolumn]) for b in range(len(bins)): try: plt.axhspan(data[bincolumn][bins[b][0][0]], data[bincolumn][bins[b + 1][0][0]], alpha=0.3) except IndexError: plt.axhspan(data[bincolumn][bins[b][0][0]], data[bincolumn][bins[b][0][-1]], alpha=0.3) if labels: for index in range(len(data[0])): plt.annotate( data[-1][index], xy=(data[timecolumn][index], data[bincolumn][index]), xytext=(-20, 20), textcoords="offset points", ha="right", va="bottom", bbox=dict(boxstyle="round,pad=0.5", fc="blue", alpha=0.5), arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0"), )
def plot_matches(self, matches, signed_nodes=False, highlight_nodes=[], light_highlight_nodes=[], title="", xlim=0): pylab.figure(figsize=self.fs) last_y = 0 nodes = [] if not matches: return tickpos = [] ticklabel = [] for m in matches: if abs(m.node) not in [abs(x) for x in nodes]: if signed_nodes: nodes.append(m.node) else: nodes.append(abs(m.node)) read_last_pos = max([x.qEnd for x in matches]) read_last_pos = max(xlim, read_last_pos) for n in nodes: nlength = len(self.lorm.getSequenceGraph().nodes[abs(n)].sequence) tickpos.append(last_y + nlength / 2) ticklabel.append("%d (%d bp)" % (n, nlength)) if abs(n) in highlight_nodes: pylab.axhspan(last_y, last_y + nlength, facecolor='y', alpha=0.25) if abs(n) in light_highlight_nodes: pylab.axhspan(last_y, last_y + nlength, facecolor='y', alpha=0.15) for m in matches: if abs(m.node) == abs(n): xpos = (m.qStart, m.qEnd) if m.node == n: ypos = (m.nStart + last_y, m.nEnd + last_y) else: ypos = (nlength - m.nStart - 15 + last_y, nlength - m.nEnd - 15 + last_y) pylab.plot(xpos, ypos, "x-") s = "%d%%" % (m.score * 100.0 / (m.nEnd - m.nStart)) pylab.text(mean(xpos), mean(ypos), s, ha="center", va="center", bbox=dict(boxstyle="round", color="white", alpha=.5)) last_y += nlength pylab.plot([0, read_last_pos], [last_y, last_y], "k:") if not title: title = 'Matches for read %d' % matches[0].read_id pylab.title(title) pylab.xlabel('Read position') pylab.ylabel('Node') pylab.xlim(left=0, right=read_last_pos) pylab.ylim(bottom=0, top=last_y) pylab.yticks(tickpos, ticklabel) pylab.show()
def main(args): """Simulate thermal regulation (because it's easier to test here than in hardware) """ outpath = None if args: outpath = args[0] # general parameter tweaking #mAh = 3000.0 * 4 # BLF Q8 #mAh = 3000.0 mAh = 700.0 #mAh = 200.0 room_temp = 22 maxtemp = 50 mintemp = maxtemp - 10 thermal_mass = 32 # bigger heat sink = higher value thermal_lag = [room_temp] * 8 prediction_strength = 4 diff_attenuation = 6 lowpass = 8 samples = 8 total_power = 1.0 # max 1.0 timestep = 0.5 # thermal regulation runs every 0.5 seconds # Power level, with max=255 # 64 steps #ramp = [ 1,1,1,1,1,2,2,2,2,3,3,4,5,5,6,7,8,9,10,11,13,14,16,18,20,22,24,26,29,32,34,38,41,44,48,51,55,60,64,68,73,78,84,89,95,101,107,113,120,127,134,142,150,158,166,175,184,193,202,212,222,233,244,255 ] # 128 steps #ramp = [ 1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,10,10,11,12,12,13,14,15,16,17,17,18,19,20,21,22,24,25,26,27,28,30,31,32,34,35,37,39,40,42,44,45,47,49,51,53,55,57,59,61,63,66,68,70,73,75,78,80,83,86,88,91,94,97,100,103,106,109,113,116,119,123,126,130,134,137,141,145,149,153,157,161,166,170,174,179,183,188,193,197,202,207,212,217,222,228,233,238,244,249,255 ] # stable temperature at each level, with max=255 #temp_ramp = [max(room_temp,(lvl/255.0*total_power*200.0)) for lvl in ramp] ramp_7135 = [ 4, 4, 5, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 20, 22, 23, 25, 27, 30, 32, 34, 37, 40, 42, 45, 48, 52, 55, 59, 62, 66, 70, 74, 79, 83, 88, 93, 98, 104, 109, 115, 121, 127, 133, 140, 146, 153, 160, 168, 175, 183, 191, 200, 208, 217, 226, 236, 245 ] ramp_FET = [ 0, 2, 3, 4, 5, 7, 8, 9, 11, 12, 14, 15, 17, 18, 20, 22, 23, 25, 27, 29, 30, 32, 34, 36, 38, 40, 42, 44, 47, 49, 51, 53, 56, 58, 60, 63, 66, 68, 71, 73, 76, 79, 82, 85, 87, 90, 93, 96, 100, 103, 106, 109, 113, 116, 119, 123, 126, 130, 134, 137, 141, 145, 149, 153, 157, 161, 165, 169, 173, 178, 182, 186, 191, 196, 200, 205, 210, 214, 219, 224, 229, 234, 239, 244, 250, 255 ] tadd_7135 = 5.0 tadd_FET = 300.0 ramp = [x / 57.0 for x in ramp_7135] + [x for x in ramp_FET] temp_ramp = [room_temp + (x / 255.0 * tadd_7135) for x in ramp_7135] temp_ramp.extend( [room_temp + tadd_7135 + (x / 255.0 * tadd_FET) for x in ramp_FET]) title = 'mAh: %i, Mass: %i, Lag: %i, Ramp: %i, Samples: %i, Predict: %i, Lowpass: %i, Att: %i' % \ (mAh, thermal_mass, len(thermal_lag), len(ramp), samples, prediction_strength, lowpass, diff_attenuation) lvl = int(len(ramp) * 1.0) # never regulate lower than this lowest_stepdown = len(ramp) / 4 temperatures = [room_temp] * samples actual_lvl = lvl overheat_count = 0 underheat_count = 0 seconds = 0 max_seconds = int(mAh * 60.0 / 100.0 * 1.5) g_Temit = [] g_Tdrv = [] g_act_lvl = [] g_lvl = [] g_sag = [] g_lm = [] times = [] def voltage_sag(seconds): runtime = float(max_seconds) return math.pow((runtime - seconds) / runtime, 1.0 / 9) def get_drv_temp(): half = int(len(thermal_lag) / 2.0) this = sum(thermal_lag[:half]) / float(half) val = room_temp + ((this - room_temp) * 0.8) #val *= voltage_sag(seconds) val = int(val) #val += random.choice((-1, 0, 0, 1)) val += random.choice((-2, -1, 0, 1, 2)) #val += random.choice((-3, -2, -1, 0, 1, 2, 3)) del get_drv_temp.values[0] get_drv_temp.values.append(val) result = sum(get_drv_temp.values) / 1.0 result = max(room_temp * get_drv_temp.adjust, result) return int(result) get_drv_temp.values = [room_temp] * 4 get_drv_temp.adjust = 4.0 def get_temp(): now = thermal_lag[-1] val = room_temp + (voltage_sag(seconds) * (now - room_temp)) #val *= voltage_sag(seconds) result = val #result = max(room_temp, result) return result while True: # apply heat step target_temp = temp_ramp[actual_lvl - 1] target_temp = math.pow(target_temp, 1.0 / 1.01) #current_temp = thermal_lag[-1] current_temp = get_temp() diff = float(target_temp - current_temp) current_temp += (diff / thermal_mass) current_temp = max(room_temp, current_temp) # shift temperatures for i in range(len(thermal_lag) - 1): thermal_lag[i] = thermal_lag[i + 1] thermal_lag[-1] = current_temp # thermal regulation algorithm for i in range(len(temperatures) - 1): temperatures[i] = temperatures[i + 1] drv_temp = get_drv_temp() temperatures[i] = drv_temp diff = int(drv_temp - temperatures[0]) projected = drv_temp + (diff << prediction_strength) #THERM_CEIL = maxtemp<<2 #THERM_FLOOR = mintemp<<2 THERM_CEIL = maxtemp * get_drv_temp.adjust THERM_FLOOR = mintemp * get_drv_temp.adjust if projected > THERM_CEIL: underheat_count = 0 if overheat_count > lowpass: overheat_count = 0 exceed = int(projected - THERM_CEIL) >> diff_attenuation exceed = max(1, exceed) stepdown = actual_lvl - exceed if (stepdown >= lowest_stepdown): actual_lvl = stepdown else: overheat_count += 1 elif projected < THERM_FLOOR: overheat_count = 0 if underheat_count > (lowpass / 2): underheat_count = 0 if (actual_lvl < lvl): actual_lvl += 1 else: underheat_count += 1 # show progress #print('T=%i: %i/%i, Temit=%i, Tdrv=%i' % ( # seconds, actual_lvl, lvl, current_temp, drv_temp)) g_Temit.append(current_temp) g_Tdrv.append(drv_temp / get_drv_temp.adjust) g_act_lvl.append(150 * float(actual_lvl) / len(ramp)) g_lvl.append(150 * float(lvl) / len(ramp)) g_sag.append(voltage_sag(seconds) * 150.0) g_lm.append( (voltage_sag(seconds)**2) * ramp[actual_lvl - 1] / 255.0 * 150.0) times.append(seconds / 60.0) #time.sleep(0.1) seconds += timestep if seconds > max_seconds: break pl.figure(dpi=100, figsize=(12, 4)) pl.suptitle(title, fontsize=14) pl.plot(times, g_Temit, label='Temit', linewidth=2) pl.plot(times, g_Tdrv, label='Tdrv', linewidth=2) pl.plot(times, g_act_lvl, label='actual lvl', linewidth=2) pl.plot(times, g_lvl, label='target lvl', linewidth=2) pl.plot(times, g_sag, label='voltage sag', linewidth=2) pl.plot(times, g_lm, label='lumens', linewidth=2) pl.axhspan(mintemp, maxtemp, facecolor='grey', alpha=0.25) #pl.axhline(y=mintemp, color='black', linewidth=1) pl.xlim((-0.05, times[-1] + 0.05)) pl.xlabel('Minutes') pl.ylabel('Temp (C), PWM') pl.legend(loc=0) if outpath: pl.savefig(outpath, dpi=70, frameon=False, bbox_inches='tight') pl.show()
#pylab.plot(x, australia, '-b', label='Australia') pylab.plot(x, oecd, '-p', label='OECD Average') pylab.plot(x, japan, '-v', label='Japan') pylab.plot(x, germany, '-k', label='Germany') pylab.plot(x, italy, '-g', label='Italy') pylab.plot(x, korea, '-s', label='Korea') plt.title("OECD Total Fertility Rates for Selected Countries", fontsize=18, y=1.02) plt.xlabel("Date", fontsize=14, labelpad=15) plt.ylabel("Fertility rate", fontsize=14, labelpad=15) plt.xlim(1970, 2018) for i in range(n): pylab.axhline(0 + i, color='gray', linewidth=1) #pylab.axhline(2.3, color='black', linewidth=1.5) pylab.axhspan(0, 2.1, alpha=0.5, color='gray') pylab.legend(loc='upper right') # place a text box in upper left in axes coords textstr = '\n'.join(( r'$\mathrm{JAIR MEDINA}%.2f$',)) # these are matplotlib.patch.Patch properties props = dict(boxstyle='round', facecolor='crimson', alpha=0.9) ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props)
#print count4 #count4 = count4 + 1 t2[count4] = range(400,1000) print np.average(t2[count4]) print np.average(t2[count4]) / 1000 print 60 / (np.average(t2[count4]) / 1000 #print " t is: beats per millisecond" #print t #print array[100] #string = array[3] #print string #print string[13] #mystring = string.replace('\t', '-') #mystring2 = mystring.split("-") #print mystring2[5] t = np.linspace(0,10000,10000) print t #ecg = np.sin(t) #print ecg pylab.xlabel('Time(ms)') pylab.ylabel('Milivolt') pylab.axhspan(0, 200, alpha=0.5, color='red') pylab.axhspan(750, 800, alpha=0.5, color='red') pylab.axvspan(600,1000,alpha=0.5, color='green') pylab.text(1,800,"Heart Rate" = HRate) #BPMverticalalignment='bottom', horizontalalignment='right', #transform = pylab.transAxes,color='black', fontsize=15): pylab.plot(t, ecg) pylab.show()
S, yerr=dS, markerfacecolor='steelblue', markeredgecolor='steelblue', ecolor='steelblue', linestyle='none', marker='o', markersize=4.5, label='$S_8$') plt.ylim(0.69, 0.85) plt.yticks([0.7, 0.75, 0.8, 0.85], fontsize=fontsize) #plt.plot(x,chi2/dof,color='darkmagenta',lw=1.5) plt.axhline(0.766, color='k', ls=':') plt.axhspan(0.766 - 0.023, 0.766 + 0.023, color='steelblue', alpha=0.2) #plt.yticks([1,1.01,1.02,1.03,1.04],fontsize=fontsize) plt.ylabel(r'$S_8$', fontsize=fontsize) plt.subplot(312) #,aspect=0.145) plt.xlim(0.5, 6.5) x = np.arange(1, 7, 1) chi2 = np.array([209.3, 208.31, 209.81, 207.95, 210.6, 205.89]) dof = np.array([206, 205, 204, 204, 203, 202]) plt.plot(x, chi2 / dof, color='darkmagenta', lw=1.5) plt.axhline(1, color='k', ls=':') plt.yticks([1, 1.01, 1.02, 1.03], fontsize=fontsize) plt.ylabel(r'$\chi^2 / N_\mathrm{dof}$', fontsize=fontsize) plt.subplot(313)
colors_line[j], linewidth=1) # Fill significant episodes for COPRA results pylab.fill_between( time[i][step_sequence[i]], surr_q_right[i][measure], q_right[i][measure], where=q_right[i][measure] > surr_q_right[i][measure], color=colors_line[j]) # Plot COPRA confidence band if SHOW_CONFIDENCE_BAND: pylab.axhspan(surr_q_left[i][measure], surr_q_right[i][measure], fill=True, color=colors_fill[j], zorder=0) pylab.xlim(min_age, max_age) if j == 1: pylab.ylim(0.4, 0.8) # Plot additional information # Plot RCC episodes #for k in xrange(RCC_EPISODES.shape[0]): # pylab.axvspan(RCC_EPISODES[k,0], RCC_EPISODES[k,1], fill=True, edgecolor="0.8", color="0.8", zorder=0) # Plot Bond events #pylab.scatter(BOND_EVENTS, 2.0 * np.ones(len(BOND_EVENTS)), c='k', marker='o', s=80)
def plot_stats(quarter_stats, time, flux, kid_x, acf_per_pos_in, acf_per_height_in, acf_per_err_in, locheight_in, asym_in): ''' Plot and calculate statistics of peaks ''' acf_per_pos_in = acf_per_pos_in[asym_in != -9999] acf_per_height_in = acf_per_height_in[asym_in != -9999] acf_per_err_in = acf_per_err_in[asym_in != -9999] locheight_in = locheight_in[asym_in != -9999] asym_in = asym_in[asym_in != -9999] if len(acf_per_pos_in) == 0: return -9999, -9999, -9999, -9999, -9999, -9999,\ -9999, -9999, -9999, -9999, 0, -9999, -9999, 1, 0.0 x = 10 #number of periods used for calc hdet = 0 #start with 0 harmonic, set to 1 if 1/2P is 1st peak # deal with cases where 1/2 P is 1st peak if len(acf_per_pos_in) >= 2: one_peak_only = 0 ind = scipy.r_[1:len(acf_per_pos_in)+1:1] if locheight_in[1] > locheight_in[0]: print '1/2 P found (1st)' hdet = 1 # mark harmonic found pk1 = acf_per_pos_in[1] acf_per_pos_in = acf_per_pos_in[1:] acf_per_height_in = acf_per_height_in[1:] acf_per_err_in = acf_per_err_in[1:] locheight_in = locheight_in[1:] asym_in = asym_in[1:] '''if 1 == 1: pknumin = int(raw_input('pk in: ')) if pknumin > 0: hdet = 1 # mark harmonic found pk1 = acf_per_pos_in[pknumin] acf_per_pos_in = acf_per_pos_in[pknumin:] acf_per_height_in = acf_per_height_in[pknumin:] acf_per_err_in = acf_per_err_in[pknumin:] locheight_in = locheight_in[pknumin:] asym_in = asym_in[pknumin:]''' else: print 'no harmonic found or harmonic not 1st peak' pk1 = acf_per_pos_in[0] else: print 'Only 1 peak' one_peak_only = 1 pk1 = acf_per_pos_in[0] # select only peaks which are ~multiples of 1st peak (within phase 0.2) acf_per_pos_0_test = scipy.append(0,acf_per_pos_in) ind_keep = scipy.r_[0:len(acf_per_pos_0_test):1] fin = False while fin == False: delta_lag_test = acf_per_pos_0_test[1:] - acf_per_pos_0_test[:-1] delta_lag_test = scipy.append(0, delta_lag_test) phase = ((delta_lag_test % pk1) / pk1) phase[phase > 0.5] -= 1.0 excl = abs(phase) > 0.2 ind_temp = scipy.r_[0:len(delta_lag_test):1] if len(phase[excl]) == 0: break else: ind_rem = ind_temp[excl][0] rem = acf_per_pos_0_test[ind_rem] ind_keep = ind_keep[acf_per_pos_0_test != rem] acf_per_pos_0_test = acf_per_pos_0_test[acf_per_pos_0_test != rem] ind_keep = ind_keep[1:] - 1 keep_pos = acf_per_pos_in[ind_keep] keep_pos = scipy.append(0, keep_pos) delta_keep_pos = keep_pos[1:] - keep_pos[:-1] # remove very small delta lags points (de-noise peak detections) if len(ind_keep) > 1: ind_keep = ind_keep[delta_keep_pos > 0.3*delta_keep_pos[0]] delta_keep_pos = delta_keep_pos[delta_keep_pos > 0.3*delta_keep_pos[0]] if len(ind_keep) > 1: ind_gap = ind_keep[delta_keep_pos > 2.2*delta_keep_pos[0]] if len(ind_gap) != 0: ind_keep = ind_keep[ind_keep < ind_gap[0]] delta_keep_pos = delta_keep_pos[ind_keep < ind_gap[0]] # limit to x lags for plot and calc if len(acf_per_pos_in[ind_keep]) < x: x = len(acf_per_pos_in[ind_keep]) acf_per_pos = acf_per_pos_in[ind_keep][:x] acf_per_height = acf_per_height_in[ind_keep][:x] acf_per_err = acf_per_err_in[ind_keep][:x] asym = asym_in[ind_keep][:x] locheight = locheight_in[ind_keep][:x] print '%d Peaks kept' %len(acf_per_pos) if len(acf_per_pos) == 1: return -9999, 0.0, pk1, acf_per_height[0], acf_per_err[0], locheight[0], -9999, -9999, -9999, -9999, 1, hdet, -9999, 1, 0.0 ''' Delta Lag ''' acf_per_pos_0 = scipy.append(0,acf_per_pos) delta_lag = acf_per_pos_0[1:] - acf_per_pos_0[:-1] av_delt = scipy.median(delta_lag) delt_mad = 1.483*scipy.median(abs(delta_lag - av_delt)) # calc MAD delt_mad = delt_mad / scipy.sqrt(float(len(delta_lag)-1.0)) # err = MAD / sqrt(n-1) med_per = av_delt mad_per_err = delt_mad pylab.figure(3,(12, 9)) pylab.clf() pylab.subplot(3,2,1) pylab.plot(acf_per_pos, delta_lag, 'bo') pylab.axhline(av_delt, ls = '--', c = 'k') pylab.axhspan(av_delt-delt_mad, av_delt+delt_mad, facecolor = 'k', alpha=0.2) pylab.xlim(0, 1.1*acf_per_pos.max()) pylab.ylim(0.99*delta_lag.min(), 1.01*delta_lag.max()) pylab.ylabel('Delta Lag (days)') # use 1st selected peak pylab.plot(pk1, delta_lag[acf_per_pos == pk1][0], 'ro') ''' Abs Height ''' pylab.subplot(3,2,2) pylab.plot(acf_per_pos, acf_per_height, 'bo') pylab.xlim(0, 1.1*acf_per_pos.max()) if acf_per_height.min() >= 0 and acf_per_height.max() >= 0: pylab.ylim(0.9*acf_per_height.min(), 1.1*(acf_per_height.max())) elif acf_per_height.min() < 0 and acf_per_height.max() >= 0: pylab.ylim(1.1*acf_per_height.min(), 1.1*(acf_per_height.max())) elif acf_per_height.max() < 0: pylab.ylim(1.1*acf_per_height.min(), 0.9*(acf_per_height.max())) pylab.plot(pk1, acf_per_height[acf_per_pos == pk1][0], 'ro') pylab.axhline(acf_per_height[acf_per_pos == pk1][0], ls = '--', c = 'k') ax = pylab.gca() pylab.text(0.42, 0.9, 'H1=%.3f' %(acf_per_height[acf_per_pos == pk1][0]), transform = ax.transAxes) pylab.ylabel('Abs Height') ''' Local Height ''' pylab.figure(3) pylab.subplot(3,2,3) pylab.plot(acf_per_pos, locheight, 'bo') pylab.xlim(0, 1.1*acf_per_pos.max()) if min(locheight) >= 0 and max(locheight) >= 0: pylab.ylim(0.99*min(locheight), 1.01*max(locheight)) ymax = 1.01*max(locheight) elif min(locheight) < 0 and max(locheight) >= 0: pylab.ylim(1.01*min(locheight), 1.01*max(locheight)) ymax = 1.01*max(locheight) elif max(locheight) < 0: pylab.ylim(1.01*min(locheight), 0.99*max(locheight)) ymax = 0.99*max(locheight) pylab.plot(pk1, locheight[acf_per_pos == pk1][0], 'ro') pylab.ylabel('Local Height') if len(acf_per_pos) > 2 and no_rpy == False: print 'Fitting line to Local Height...' st_line = lambda p, x: p[0] + p[1] * x '''st_line_err = lambda p, x, y, fjac: [0, (y - st_line(p, x)), None] fa = {'x': acf_per_pos, 'y': locheight} p = [scipy.median(locheight), 0.0] m = mpfit.mpfit(st_line_err, p, functkw = fa, quiet = True) p = m.params errs = m.perror h_grad = p[1] h_timescale = 1.0 / h_grad h_grad_err = errs[1]''' r.assign('xdf1', acf_per_pos) r.assign('ydf1', locheight) p1 = r(''' xdf <- c(xdf1) ydf <- c(ydf1) library(quantreg) rqmodel1 <- rq(ydf~xdf) plot(xdf,ydf) abline(rqmodel1,col=3) sumr = summary(rqmodel1, se = 'boot') grad <- rqmodel1[['coefficients']][['xdf']] interc <- rqmodel1[['coefficients']][['(Intercept)']] err_grad <- sumr[[3]][[3]] err_interc <- sumr[[3]][[4]] resids = resid(rqmodel1) output <- c(resids, grad, interc, err_grad, err_interc) ''') res =scipy.array(p1[0:len(acf_per_pos)]) pqr = scipy.array(p1[len(acf_per_pos):]) h_grad = pqr[0] h_timescale = 1.0 / h_grad h_grad_err = pqr[3] h_grad_scatter = sum((st_line([pqr[1],pqr[0]], acf_per_pos) - locheight) ** 2) / scipy.sqrt(float(len(acf_per_pos))) pylab.plot(acf_per_pos, st_line([pqr[1],pqr[0]], acf_per_pos), 'r-') ax = pylab.gca() pylab.text(0.3, 0.9,'m=%.5f, TS=%.2f' %(h_grad, abs(h_timescale)), transform = ax.transAxes) else: h_grad = -9999 h_timescale = -9999 h_grad_err = -9999 h_grad_scatter = -9999 ''' Width ''' pylab.subplot(3,2,4) pylab.plot(acf_per_pos, acf_per_err, 'bo') pylab.xlim(0, 1.1*acf_per_pos.max()) pylab.ylim(0.99*acf_per_err.min(), 1.01*acf_per_err.max()) ymax = 1.01*acf_per_err.max() pylab.plot(pk1, acf_per_err[acf_per_pos == pk1][0], 'ro') pylab.ylabel('Width (days)') pylab.xlabel('Lag (days)') if len(acf_per_pos) > 2 and no_rpy == False: print 'Fitting line to Width...' r.assign('xdf1', acf_per_pos) r.assign('ydf1', acf_per_err) p1 = r(''' xdf <- c(xdf1) ydf <- c(ydf1) library(quantreg) rqmodel1 <- rq(ydf~xdf) plot(xdf,ydf) abline(rqmodel1,col=3) sumr = summary(rqmodel1, se = 'boot') grad <- rqmodel1[['coefficients']][['xdf']] interc <- rqmodel1[['coefficients']][['(Intercept)']] err_grad <- sumr[[3]][[3]] err_interc <- sumr[[3]][[4]] resids = resid(rqmodel1) output <- c(resids, grad, interc, err_grad, err_interc) ''') res =scipy.array(p1[0:len(acf_per_pos)]) pqr = scipy.array(p1[len(acf_per_pos):]) w_grad = pqr[0] w_timescale = 1.0 / w_grad w_grad_err = pqr[3] w_grad_scatter = sum((st_line([pqr[1],pqr[0]], acf_per_pos) - acf_per_err) ** 2) / scipy.sqrt(float(len(acf_per_pos))) pylab.plot(acf_per_pos, st_line([pqr[1],pqr[0]], acf_per_pos), 'r-') ax = pylab.gca() pylab.text(0.3, 0.9, 'm=%.5f, TS=%.2f' %(w_grad, abs(w_timescale)), transform = ax.transAxes) else: w_grad = -9999 w_timescale = -9999 w_grad_err = -9999 w_grad_scatter = -9999 pylab.subplot(3,2,5) pylab.plot(acf_per_pos, asym , 'bo') pylab.xlim(0, 1.1*acf_per_pos.max()) pylab.plot(pk1, asym[acf_per_pos == pk1][0], 'ro') pylab.ylim(0.99*min(asym), 1.01*max(asym)) pylab.ylabel('Asymmetry') pylab.xlabel('Lag (days)') pylab.suptitle('ID: %s, P\_dlag = %.3fd +/-%.3fd, P\_pk = %.3fd' \ %(kid_x, med_per, mad_per_err, pk1), fontsize = 16) pylab.savefig('%s/PDCQ%s_output/plots_stats/%s_stats.png' % (dir, quarter_stats, kid_x)) peak_ratio = len(acf_per_pos)/len(acf_per_pos_in) return med_per, mad_per_err, pk1, acf_per_height[acf_per_pos == pk1][0], acf_per_err[acf_per_pos == pk1][0],\ locheight[acf_per_pos == pk1][0], h_grad, h_grad_scatter, w_grad, w_grad_scatter, len(acf_per_pos), hdet, acf_per_pos, one_peak_only, peak_ratio
def draw_graph(getgraph, opts): # http://matplotlib.sourceforge.net/index.html fg = pl.figure() ax = fg.add_subplot(111) global graph_plots, all_points graph_plots, all_points = [], [] def draw_tradeoff_plots(ratio, points, attrs): # select tradeoff given ratio best = atos_lib.atos_client_results.select_tradeoff(points, ratio) # graph limits xmin = min([p.sizered for p in points]) xmax = max([p.sizered for p in points]) ymin = min([p.speedup for p in points]) ymax = max([p.speedup for p in points]) # number of points on ratio line nbtk = int((ratio >= 1 and 2 or 1 / ratio) * 32) # ratio line points coordinates xtk = [xmin + i * ((xmax - xmin) / nbtk) for i in range(nbtk + 1)] ytk = [best.speedup + (1.0 / ratio) * (best.sizered - x) for x in xtk] coords = filter(lambda (x, y): y >= ymin and y <= ymax, zip(xtk, ytk)) # first plot: selected tradeoff point attrs.update({'label': '_nolegend_'}) attrs.update({'markersize': 20, 'linewidth': 0, 'alpha': 0.4}) plots = [(([best.sizered], [best.speedup]), dict(attrs))] # second plot: ratio line attrs.update({'marker': '', 'linewidth': 2, 'linestyle': 'solid'}) plots += [((zip(*coords)[0], zip(*coords)[1]), dict(attrs))] return plots def draw_all(): global graph_plots, all_points, selected_points, similar_points # :( # remove old plots old_points = [(p.sizered, p.speedup, p.variant) for p in all_points] for x in list(graph_plots): graph_plots.remove(x) x.remove() # get graph values scatters, frontiers = getgraph() all_points = sum([x[0] for x in scatters + frontiers], []) # draw scatters for (points, attrs) in scatters: attrsmap = { 's': 20, 'label': '_nolegend_', 'zorder': 2, 'color': 'r', 'edgecolor': 'k' } attrsmap.update(attrs) xy = zip(*[(p.sizered, p.speedup) for p in points]) gr = ax.scatter(*xy, **attrsmap) graph_plots.append(gr) # draw frontiers (line plots) for (points, attrs) in frontiers: attrsmap = { 'color': 'r', 'marker': 'o', 'label': '_nolegend_', 'zorder': 2, 'markersize': 7, 'linestyle': 'dashed', 'linewidth': 2 } attrsmap.update(attrs) xy = zip(*sorted([(p.sizered, p.speedup) for p in points])) gr, = ax.plot(xy[0], xy[1], **attrsmap) graph_plots.append(gr) # show tradeoffs for each frontier for ratio in opts.tradeoffs or []: for ((xcrd, ycrd), attrs) in \ draw_tradeoff_plots(ratio, points, dict(attrsmap)): graph_plots.append(ax.plot(xcrd, ycrd, **attrs)[0]) # draw selected points (hidden) if opts.show and all_points: # workaround pb with pick_event event ind (4000) attrsmap = { 'color': 'b', 'marker': 'o', 'markersize': 20, 'linewidth': 0, 'alpha': 0.4 } xy = zip(*sorted([(p.sizered, p.speedup) for p in all_points])) selected_points, = \ ax.plot(xy[0], xy[1], visible=False, picker=4000, **attrsmap) graph_plots.append(selected_points) # similar point plot attrsmap.update({'color': 'g'}) similar_points, = ax.plot(None, None, visible=False, **attrsmap) graph_plots.append(similar_points) # highlight new points if opts.follow and old_points: new_points = [ p for p in all_points if ((p.sizered, p.speedup, p.variant) not in old_points) ] attrsmap = { 'color': 'r', 'marker': 'o', 'markersize': 20, 'linewidth': 0, 'alpha': 0.4, 'zorder': 1 } if new_points: xy = zip(*[(p.sizered, p.speedup) for p in new_points]) new_points, = ax.plot(*xy, **attrsmap) graph_plots.append(new_points) # redraw legend and figure if opts.xlim: pl.xlim([float(l) for l in opts.xlim.split(",")]) if opts.ylim: pl.ylim([float(l) for l in opts.ylim.split(",")]) ax.legend(loc='lower left') fg.canvas.draw() # dynamic annotations def on_pick(event): def closest(x, y): dp = [(math.hypot(p.sizered - x, p.speedup - y), p) for p in all_points] return sorted(dp)[0][1] def highlight(p): # print point on console print '-' * 40 + '\n' + point_str(p) # highlight point selected_points.set_visible(True) selected_points.set_data(p.sizered, p.speedup) # highlight similar points (same variant) sim = zip(*([(c.sizered, c.speedup) for c in all_points if c.variant == p.variant and c != p])) similar_points.set_visible(True) similar_points.set_data(sim and sim[0], sim and sim[1]) # selected point legend main_legend = ax.legend_ lg = point_str(p, short=True, no_id=opts.anonymous) lp = pl.legend([selected_points], [lg], loc='lower right', numpoints=1) pl.setp(lp.get_texts(), fontsize='medium') lp.get_frame().set_alpha(0.5) pl.gca().add_artist(main_legend) fg.canvas.draw() ax.legend_ = main_legend highlight(closest(event.mouseevent.xdata, event.mouseevent.ydata)) # live plotting def on_timer(): draw_all() # draw graph for the first time draw_all() # graph title title = 'Optimization Space for %s' % (opts.id or opts.targets or (all_points and all_points[0].target)) if opts.refid: title += ' [ref=%s]' % opts.refid if opts.filter: title += ' [filter=%s]' % opts.filter # redraw axis, set labels, legend, grid, ... def labelfmt(x, pos=0): return '%.2f%%' % (100.0 * x) ax.xaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) ax.yaxis.set_major_formatter(pl.FuncFormatter(labelfmt)) pl.axhspan(0.0, 0.0) pl.axvspan(0.0, 0.0) pl.title(title) pl.xlabel('size reduction (higher is better) -->') pl.ylabel('speedup (higher is better) -->') if opts.xlim: pl.xlim([float(l) for l in opts.xlim.split(",")]) if opts.ylim: pl.ylim([float(l) for l in opts.ylim.split(",")]) pl.grid(True) if opts.outfile: fg.savefig(opts.outfile) if opts.show: fg.canvas.mpl_connect('pick_event', on_pick) if opts.follow: timer = atos_lib.repeatalarm(on_timer, 5.0).start() pl.show() if opts.follow: timer.stop()
pylab.yticks(fontsize=tsize) yticks = ax1.yaxis.get_major_ticks() yticks[0].label1.set_visible(False) yticks[-1].label1.set_visible(False) xticks = ax1.xaxis.get_major_ticks() xticks[-1].label1.set_visible(False) xticks[0].label1.set_visible(False) #xticks[1].label1.set_visible(False) ax1.set_xlabel("$\psi''^{\mathrm{(fit)}} - \psi''$", fontsize=fsize) ax1.set_ylabel("$H_0$", fontsize=fsize) #pylab.text(-0.1, 30., 'Composite model. Fit to image positions, $r_{\mu_r}$, and $\sigma_{e2}$', fontsize=tsize) pylab.text(-0.15, 59., 'truth: de Vauc. + NFW', fontsize=fsize) pylab.text(-0.15, 56., 'model: de Vauc. + gNFW (fixed $r_s$)', fontsize=fsize) pylab.text(-0.15, 53., 'constraints: image positions, $r_\mu$, $\sigma_{e2}$', fontsize=fsize) pylab.axes([0.15, 0.65, 0.4, 0.3]) pylab.axhspan(mean_H0 - std_H0, mean_H0 + std_H0, color='gray', alpha=0.5) pylab.scatter((psi2_inferred - psi2_mock)[good], ind_H0[good], color='b') pylab.axhline(70., linestyle='--', color='k') pylab.xlim(-0.02, 0.02) pylab.ylim(68., 72.) pylab.xticks(()) pylab.yticks(()) pylab.savefig('gnfw_individual_H0.pdf') #pylab.show()
def compare_reversibility_to_dG0(reaction_list, thermo, html_writer, cmap=None): html_writer.write('<h1>Reversibility index vs. equilibrium constants</h1>\n') cmap = cmap or GetEmptyConcentrationMap() x_range = (1e-9, 1e9) y_range = (1e-9, 1e9) x_threshold = 1e3 y_threshold = 1e3 regime_counters = {} stoich_counters = {} data_mat = pylab.zeros((0, 4)) debug_dict_list = [] for reaction in reaction_list: debug_dict = {'name':reaction.name, 'KEGG Reaction':reaction.to_hypertext()} try: reaction.Balance(balance_water=True, exception_if_unknown=True) except (KeggReactionNotBalancedException, OpenBabelError): continue dG0 = reaction.PredictReactionEnergy(thermo) if np.isnan(dG0): debug_dict['sortkey'] = 0 debug_dict['error'] = "Cannot calculate Gibbs energy" else: Keq = pylab.exp(-dG0/(R*thermo.T)) n_s = -sum([x for cid, x in reaction.sparse.iteritems() if (x < 0 and cid not in cmap)]) n_p = sum([x for cid, x in reaction.sparse.iteritems() if (x > 0 and cid not in cmap)]) if (n_p + n_s) == 0: continue stoich_counters.setdefault((n_s, n_p), 0) stoich_counters[n_s, n_p] += 1 log_gamma = CalculateReversability(reaction, thermo, concentration_map=cmap, logscale=True) if Keq < 1.0/x_threshold: Krev = -1 elif Keq < x_threshold: Krev = 0 else: Krev = 1 if log_gamma < -np.log(y_threshold): Grev = -1 elif log_gamma < np.log(y_threshold): Grev = 0 else: Grev = 1 regime_counters.setdefault((Krev, Grev), 0) regime_counters[Krev, Grev] += 1 data_mat = pylab.vstack([data_mat, [Keq, log_gamma, Krev, Grev]]) debug_dict['sortkey'] = log_gamma debug_dict['log(γ)'] = "%.2e" % log_gamma debug_dict[thermodynamic_constants.symbol_dr_G0_prime] = dG0 debug_dict_list.append(debug_dict) debug_dict_list.sort(key=lambda(x):x['sortkey']) div_id = html_writer.insert_toggle() html_writer.div_start(div_id) html_writer.write_table(debug_dict_list, headers=['log(γ)', thermodynamic_constants.symbol_dr_G0_prime, 'name', 'KEGG Reaction', 'error']) html_writer.div_end() html_writer.write('</br>\n') fig = pylab.figure(figsize=(6,6), dpi=90) pylab.xlabel("$K'$", figure=fig) pylab.ylabel(r"$\hat{\gamma} = \left( K' / Q'' \right)^{2/N}$", figure=fig) shading_color = (1.0, 0.7, 0.7) #pylab.axvspan(x_range[0], 1.0/x_threshold, ymin=0, ymax=1, color=x_color, alpha=0.3) #pylab.axvspan(x_threshold, x_range[1], ymin=0, ymax=1, color=x_color, alpha=0.3) #pylab.axhspan(y_range[0], 1.0/y_threshold, xmin=0, xmax=1, color=y_color, alpha=0.3) #pylab.axhspan(y_threshold, y_range[1], xmin=0, xmax=1, color=y_color, alpha=0.3) pylab.axvspan(x_range[0], 1.0/x_threshold, ymin=1.0/3.0, ymax=2.0/3.0, color=shading_color) pylab.axvspan(x_threshold, x_range[1], ymin=1.0/3.0, ymax=2.0/3.0, color=shading_color) pylab.axhspan(y_range[0], 1.0/y_threshold, xmin=1.0/3.0, xmax=2.0/3.0, color=shading_color) pylab.axhspan(y_threshold, y_range[1], xmin=1.0/3.0, xmax=2.0/3.0, color=shading_color) # draw the lines for the specific reaction stoichiometries stoichiometries = [(1, 1, '-', '#e49b1c'), (1 ,2, '--', '#1ce463'), (2, 1, '--', '#1d1de3'), (2, 2, '-', '#e41c63')] fig.hold(True) for n_s, n_p, style, color in stoichiometries: percent = 100.0 * stoich_counters.get((n_s, n_p), 0) / sum(stoich_counters.values()) gamma = [(Keq / thermo.c_mid**(n_p - n_s)) ** (2.0/(n_p + n_s)) for Keq in x_range] pylab.plot(x_range, gamma, style, color=color, linewidth=3, figure=fig, label="%d:%d (%d%%)" % (n_s, n_p, np.round(percent))) pylab.legend(loc='upper left') for Krev, Grev in regime_counters.keys(): x_pos = x_threshold ** (Krev*2) y_pos = y_threshold ** (Grev*2) pylab.text(x_pos, y_pos, "%.1f%%" % (100.0 * regime_counters[Krev, Grev] / data_mat.shape[0]), horizontalalignment='center', verticalalignment='center') pylab.xscale('log', figure=fig) pylab.yscale('log', figure=fig) pylab.ylim(y_range) pylab.xlim(x_range) pylab.xticks([1e-9, 1e-6, 1e-3, 1, 1e3, 1e6, 1e9]) pylab.yticks([1e-9, 1e-6, 1e-3, 1, 1e3, 1e6, 1e9]) html_writer.embed_matplotlib_figure(fig, width=400, height=400, name="reversibility_vs_keq") fig = pylab.figure(figsize=(2,2), dpi=90) abs_gamma = np.exp(abs(data_mat[:,1])) plotting.cdf(abs_gamma, label='gamma', figure=fig) pylab.plot([x_threshold, x_threshold], [0, 1], 'k--', figure=fig) pylab.xscale('log', figure=fig) #pylab.xlabel(r'$\hat{\gamma}$', figure=fig) #pylab.ylabel(r'CDF($\hat{\gamma}$)', figure=fig) pylab.text(1e6, 0.4, r'CDF($\hat{\gamma}$)', horizontalalignment='center', verticalalignment='center') pylab.xlim((1, 1e9)) pylab.xticks([1, 1e3, 1e6, 1e9]) pylab.yticks([0, 0.5, 1.0]) pylab.tight_layout() html_writer.embed_matplotlib_figure(fig, width=125, height=125, name="reversibility_cdf")
def plot_inference_result(y, w1, w2, x1, x2, tau, cell_lines=[], muts=[], title='', figname='test.png'): matplotlib.rcParams.update({'font.size': 12}) fig = PL.figure(figsize=(9, 6)) gs = gridspec.GridSpec(2, 2, width_ratios=[len(w1), len(x1)]) cell_lines = ['LNCaP' if ('LNCaP' in x) else x for x in cell_lines] mut_status = [ '(M)' if mut == "True" else ('' if mut == "False" else '(U)') for mut in muts ] #Signal ax = PL.subplot(gs[0, 0]) im = PL.imshow(y, aspect=1.15, interpolation='none', cmap=PL.get_cmap("coolwarm"), vmin=-3, vmax=3) ax = PL.gca() ax.set_xticks([]) ax.set_yticks(range(len(x1))) ax.set_yticklabels(['gRNA %d' % (grnano + 1) for grnano in range(len(x1))]) if len(cell_lines) > 0: ax.set_xticks(range(len(cell_lines))) ax.set_xticklabels(cell_lines, rotation='vertical') ax.xaxis.tick_top() for t in ax.xaxis.get_ticklines(): t.set_visible(False) for t in ax.yaxis.get_ticklines(): t.set_visible(False) for t, mt in zip(ax.xaxis.get_ticklabels(), mut_status): t.set_fontsize(10) if mt == '(M)': t.set_fontweight('bold') #x PL.subplot(gs[0, 1]) PL.plot([1, 1], [-1, len(x1) + 1], 'k--') PL.plot([0, 0], [-1, len(x1) + 1], 'k--') vdata = [ ST.norm.rvs(x1[i], (x2[i] - x1[i]**2)**0.5, size=5000) for i in range(len(x1)) ] vpos = (SP.arange(len(x1)) + 1)[::-1] clrs = ['#FFFFFF', '#BBCCEE'] for i in range(len(x1)): PL.axhspan(i + 0.5, i + 1.5, facecolor=clrs[i % 2], alpha=0.1) vplot = PL.violinplot(vdata, vpos, widths=0.5 * SP.ones(len(x1)), vert=False, showextrema=True, showmeans=True) for patch, val in zip(vplot['bodies'], x1): col_val = int(0.8 * min(max(256 - val * 128, 0), 255)) patch.set_color('#%02x%02x%02x' % (col_val, col_val, col_val)) vplot['cmeans'].set_color('darkblue') vplot['cmeans'].set_linewidth(2) vplot['cmins'].set_color('#444444') vplot['cmaxes'].set_color('#444444') vplot['cbars'].set_color('#444444') vplot['cbars'].set_visible(False) PL.ylim(0.5, len(x1) + 0.5) PL.xlim(-0.5, 2) ax = PL.gca() PL.xticks([0, 1]) PL.yticks([]) PL.xlabel("gRNA efficacy") PL.title(title) #w PL.subplot(gs[1, 0]) clrs = ['#FFFFFF', '#BBCCEE'] for i in range(len(w1)): PL.axvspan(i - 1, i, facecolor=clrs[i % 2], alpha=0.1) vplot = PL.violinplot([ ST.norm.rvs(w1[i], (w2[i] - w1[i]**2)**0.5, size=5000) for i in range(len(w1)) ], SP.arange(len(w1)) - 0.5, widths=0.9 * SP.ones(len(w1)), showmeans=True, showextrema=True) for patch, val in zip(vplot['bodies'], w1): col_val = int(0.95 * min(max(0, 256 + val * 128), 200)) patch.set_alpha(1.0) clr = im.cmap(im.norm(val)) patch.set_color(clr) vplot['cmeans'].set_color('darkblue') vplot['cmeans'].set_linewidth(2) vplot['cmins'].set_color('#444444') vplot['cmaxes'].set_color('#444444') vplot['cbars'].set_visible(False) PL.plot([-1, len(w1)], [0.0, 0.0], 'k--') PL.xlim(-1, len(w1) - 1) PL.ylim(-3.5, 1) mean_y = np.nanmean(y, axis=0) PL.ylabel("Gene essentiality") PL.xlabel("Cell lines") pws = [ 1.0 - ST.norm.cdf((w1[i]) / np.sqrt(w2[i] - w1[i] * w1[i])) for i in range(len(w1)) ] ax = PL.gca() if len(cell_lines) > 0: ax.set_xticks([]) for t in ax.xaxis.get_ticklines(): t.set_visible(False) PL.subplots_adjust(left=0.08, right=0.94, top=0.82, bottom=0.11, wspace=0.0, hspace=0.0) PL.rcParams['svg.fonttype'] = 'none' PL.savefig(figname, bbox_inches='tight') PL.show(block=False) return fig
def plotRes(data, errors, r): import pylab pylab.figure() nObs = len(data) n, bins, patches = pylab.hist(data, 2 * np.sqrt(nObs), fc=[.7, .7, .7]) binSize = bins[1] - bins[0] x = np.arange(bins[0], bins[-1]) means, sigs, pis, mVars, weights = r inds = np.argmax(weights, 1) for i in range(means.size): #print i c = pylab.cm.hsv(float(i) / means.size) n, bin_s, patches = pylab.hist(data[inds == i], bins, alpha=0.3, facecolor=c) ys = np.zeros_like(x) i = 0 for m, s, p in zip(means, sigs, pis): c = pylab.cm.hsv(float(i) / means.size) y = nObs * p * binSize * np.exp(-(x - m)**2 / (2 * s**2)) / np.sqrt(2 * np.pi * s**2) ys += y i += 1 pylab.plot(x, y, lw=2, color=c) #pylab.plot(x, ys, lw=3) pylab.figure() ci = (r[4] * np.arange(r[0].size)[None, :]).sum(1) I = np.argsort(ci) cis = ci[I] cil = 0 for i in range(means.size): c = pylab.cm.hsv(float(i) / means.size) print(c) pylab.axvline(means[i], color=c) pylab.axvspan(means[i] - sigs[i], means[i] + sigs[i], alpha=0.5, facecolor=c) cin = cis.searchsorted(i + 0.5) pylab.axhspan(cil, cin, alpha=0.3, facecolor=c) cil = cin pylab.errorbar(data[I], np.arange(data.size), xerr=errors[I], fmt='.')