def newPlot(axes): global data, plot button = axes2button(axes) field = convertField(plot.graphOptions[button].quantity, plot.graphOptions[button].component) plot.images[axes] = plot.axes[axes].imshow(data.slices[field+plot.graphOptions[button].slices][data.timeStep,...], cmap=P.cm.hot, aspect='auto', origin='lower') # plot the slice P.draw()
def update(self): if self.plot_gnss and self.gnss != []: plt.figure(1) gnssT = zip(*self.gnss) gnss_plt = plot(gnssT[0],gnssT[1],'black') ref_gnssT = zip(*self.ref_gnss) ref_gnss_plt = plot(ref_gnssT[0],ref_gnssT[1],'b') if self.plot_pose and self.pose_pos != []: plt.figure(1) poseT = zip(*self.pose_pos) pose_plt = plot(poseT[0],poseT[1],'r') if self.plot_odometry and self.odo != []: plt.figure(2) odoT = zip(*self.odo) odo_plt = plot(odoT[0],odoT[1],'b') if self.plot_yaw: if self.odo_yaw != []: plt.figure(3) odo_yaw_plt = plot(self.odo_yaw,'b') if self.ahrs_yaw != []: plt.figure(3) ahrs_yaw_plt = plot(self.ahrs_yaw,'g') if self.gnss_yaw != []: plt.figure(3) gnss_yaw_plt = plot(self.gnss_yaw, 'black') if self.pose_yaw != []: plt.figure(3) pose_yaw_plt = plot(self.pose_yaw,'r') if self.plot_gnss or self.plot_pose or self.plot_odometry or self.plot_yaw: draw()
def sliderUpdate(val): spec = specGrid.interpGrid(tuple([slider.val for slider in sliders])) specPlot.set_ydata(spec) if autoScale: plotAxis.relim() plotAxis.autoscale_view() pylab.draw()
def run(): colors = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'b--', 'g--', 'r--', 'c--', 'm--', 'y--', 'k--', 'bo', 'go', 'ro', 'co', 'mo', 'yo', 'ko', 'b+', 'g+', 'r+', 'c+', 'm+', 'y+', 'k+', 'b*', 'g*', 'r*', 'c*', 'm*', 'y*', 'k*', 'b|', 'g|', 'r|', 'c|', 'm|', 'y|', 'k|', ] plots = defaultdict(list) heap_size = [] order = ['Heap change'] manager = pylab.get_current_fig_manager() manager.resize(1400, 1350) pylab.ion() for entry in read_data(): heap_size.append(entry["after"]["size_bytes"]) pylab.subplot(2, 1, 1) pylab.plot(heap_size, 'r', label='Heap size') pylab.legend(["Heap size"], loc=2) pylab.subplot(2, 1, 2) plots["Heap change"].append(entry["change"]["size_bytes"]) for thing in entry["change"]["details"]: if thing["what"] not in order: order.append(thing["what"]) plots[thing["what"]].append(thing["size_bytes"]) for what, color in zip(order, colors): pylab.plot(plots[what], color, label=what) pylab.legend(order, loc=3) pylab.draw()
def plotLists(xList, xLabel=None, eListTitle=None, eList=None, eLabel=None, fListTitle=None, fList=None, fLabel=None): if h2o.python_username!='kevin': return import pylab as plt print "xList", xList print "eList", eList print "fList", fList font = {'family' : 'normal', 'weight' : 'normal', 'size' : 26} ### plt.rc('font', **font) plt.rcdefaults() if eList: if eListTitle: plt.title(eListTitle) plt.figure() plt.plot (xList, eList) plt.xlabel(xLabel) plt.ylabel(eLabel) plt.draw() if fList: if fListTitle: plt.title(fListTitle) plt.figure() plt.plot (xList, fList) plt.xlabel(xLabel) plt.ylabel(fLabel) plt.draw() if eList or fList: plt.show()
def drawPr(tp,fp,tot,show=True): """ draw the precision recall curve """ det=numpy.array(sorted(tp+fp)) atp=numpy.array(tp) afp=numpy.array(fp) #pylab.figure() #pylab.clf() rc=numpy.zeros(len(det)) pr=numpy.zeros(len(det)) #prc=0 #ppr=1 for i,p in enumerate(det): pr[i]=float(numpy.sum(atp>=p))/numpy.sum(det>=p) rc[i]=float(numpy.sum(atp>=p))/tot #print pr,rc,p ap=0 for c in numpy.linspace(0,1,num=11): if len(pr[rc>=c])>0: p=numpy.max(pr[rc>=c]) else: p=0 ap=ap+p/11 if show: pylab.plot(rc,pr,'-g') pylab.title("AP=%.3f"%(ap)) pylab.xlabel("Recall") pylab.ylabel("Precision") pylab.grid() pylab.show() pylab.draw() return rc,pr,ap
def createPlot(dataY, dataX, ticksX, annotations, axisY, axisX, dostep, doannotate): if not ticksX: ticksX = dataX if dostep: py.step(dataX, dataY, where='post', linestyle='-', label=axisY) # where=post steps after point else: py.plot(dataX, dataY, marker='o', ms=5.0, linestyle='-', label=axisY) if annotations and doannotate: for note, x, y in zip(annotations, dataX, dataY): py.annotate(note, (x, y), xytext=(2,2), xycoords='data', textcoords='offset points') py.xticks(np.arange(1, len(dataX)+1), ticksX, horizontalalignment='left', rotation=30) leg = py.legend() leg.draggable() py.xlabel(axisX) py.ylabel('time (s)') # Set X axis tick labels as rungs #print zip(dataX, dataY) py.draw() py.show() return
def run( self , props , globdat ): a = [] for i,col in enumerate(self.columndata): if col.type in globdat.outputNames: data = globdat.getData( col.type , col.node ) elif hasattr(globdat,col.type): b = getattr( globdat , col.type ) if type(b) is ndarray: data = b[globdat.dofs.getForType(col.node,col.dof)] else: data = b else: data = globdat.getData( col.type , col.node ) data = data * col.factor a.append(data) self.outfile.write(str(data)+' ',) self.outfile.write('\n') if self.onScreen: self.output.append( a ) plot( [x[0] for x in self.output], [x[1] for x in self.output], 'ro-' ) draw() if not globdat.active: self.outfile.close
def drawPrfastscore(tp,fp,scr,tot,show=True): tp=numpy.cumsum(tp) fp=numpy.cumsum(fp) rec=tp/tot prec=tp/(fp+tp) #dif=numpy.abs(prec[1:]-rec[1:]) dif=numpy.abs(prec[::-1]-rec[::-1]) pos=dif.argmin() pos=len(dif)-pos-1 ap=0 for t in numpy.linspace(0,1,11): pr=prec[rec>=t] if pr.size==0: pr=0 p=numpy.max(pr); ap=ap+p/11; if show: pylab.plot(rec,prec,'-g') pylab.title("AP=%.3f EPRthr=%.3f"%(ap,scr[pos])) pylab.xlabel("Recall") pylab.ylabel("Precision") pylab.grid() pylab.show() pylab.draw() return rec,prec,scr,ap,scr[pos]
def click(event): print [event.key] if event.key == 'm': mode = raw_input('Enter new mode: ') for k in plots: try: d = data_mode(plt_data[k], mode) plots[k].set_data(d) except(ValueError): print 'Unrecognized plot mode' p.draw() elif event.key == 'd': max = raw_input('Enter new max: ') try: max = float(max) except(ValueError): max = None drng = raw_input('Enter new drng: ') try: drng = float(drng) except(ValueError): drng = None for k in plots: _max,_drng = max, drng if _max is None or _drng is None: d = plots[k].get_array() if _max is None: _max = d.max() if _drng is None: _drng = _max - d.min() plots[k].set_clim(vmin=_max-_drng, vmax=_max) print 'Replotting...' p.draw()
def set_ticks(xmajor,ymajor,xminor,yminor): ax=pylab.gca() ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(xmajor)) ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(xminor)) ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(ymajor)) ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(yminor)) pylab.draw()
def test_path(): "generate and draw a random path" path = genpath() P.ion() P.clf() draw_path(P.gca(), path) P.draw()
def run(steps=10): for i in range(steps): Q.time_step() #Q.plot_links() py.xlim((0,config['XSIZE'])) py.ylim((0,config['YSIZE'])) py.draw()
def plot_cumulative_score(smod, seqs, size=(6, 2), fname=None): """plot_cumulative_score.""" sig = cumulative_score(seqs, smod) plt.figure(figsize=size) sigp = np.copy(sig) sigp[sigp < 0] = 0 plt.bar(range(len(sigp)), sigp, alpha=0.3, color='g') sign = np.copy(sig) sign[sign >= 0] = 0 plt.bar(range(len(sign)), sign, alpha=0.3, color='r') plt.grid() plt.xlabel('Position') plt.ylabel('Importance score') if fname: plt.draw() figname = '%s_importance.png' % (fname) plt.savefig( figname, bbox_inches='tight', transparent=True, pad_inches=0) else: figname = None plt.show() plt.close() return figname
def update(self): if self.pose != []: plt.figure(1) clf() self.fig1 = plt.figure(num=1, figsize=(self.window_size, \ self.window_size), dpi=80, facecolor='w', edgecolor='w') title (self.title) xlabel('Easting [m]') ylabel('Northing [m]') axis('equal') grid (True) poseT = zip(*self.pose) pose_plt = plot(poseT[1],poseT[2],'#ff0000') if self.wptnav != []: mode = self.wptnav[-1][MODE] if not (self.wptnav[-1][B_E] == 0 and self.wptnav[-1][B_N] == 0 and self.wptnav[-1][A_E] == 0 and self.wptnav[-1][A_N] == 0): b_dot = plot(self.wptnav[-1][B_E],self.wptnav[-1][B_N],'ro',markersize=8) a_dot = plot(self.wptnav[-1][A_E],self.wptnav[-1][A_N],'go',markersize=8) ab_line = plot([self.wptnav[-1][B_E],self.wptnav[-1][A_E]],[self.wptnav[-1][B_N],self.wptnav[-1][A_N]],'g') target_dot = plot(self.wptnav[-1][TARGET_E],self.wptnav[-1][TARGET_N],'ro',markersize=5) if mode == -1: pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'b^',markersize=8) elif mode == 1: pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'bs',markersize=8) elif mode == 2: pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'bo',markersize=8) if self.save_images: self.fig1.savefig ('plot_map%05d.jpg' % self.image_count) self.image_count += 1 draw()
def plot_location(needle, haystack, cluster_id=None, nbins=20, size=(17, 2), fname=None): """plot_location.""" locs = [] for h, s in haystack: for match in re.finditer(needle, s): s = match.start() e = match.end() m = s + (e - s) / 2 locs.append(m) plt.figure(figsize=size) n, bins, patches = plt.hist( locs, nbins, normed=0, facecolor='blue', alpha=0.3) plt.grid() plt.title(needle) plt.xlabel('Position') plt.ylabel('Num occurrences') if fname: plt.draw() figname = '%s_loc_%d.png' % (fname, cluster_id) plt.savefig( figname, bbox_inches='tight', transparent=True, pad_inches=0) else: figname = None plt.show() plt.close() return figname
def plot_distance(cluster_id_i, cluster_id_j, regex_i, regex_j, distances, nbins=5, size=(6, 2), fname=None): """plot_distance.""" ds = distances[(cluster_id_i, cluster_id_j)] plt.figure(figsize=size) n, bins, patches = plt.hist( ds, nbins, normed=0, facecolor='green', alpha=0.3) plt.grid() plt.title('%s vs %s' % (regex_i, regex_j)) plt.xlabel('Relative position') plt.ylabel('Num occurrences') if fname: plt.draw() figname = '%s_dist_%d_vs_%d.png' % (fname, cluster_id_i, cluster_id_j) plt.savefig( figname, bbox_inches='tight', transparent=True, pad_inches=0) else: figname = None plt.show() plt.close() return figname
def plot_coupe(sol): ax1.cla() ax2.cla() mx = int(sol.domain.N[0]/2-1) my = int(sol.domain.N[1]/2-1) x = sol.domain.x[0][1:-1] y = sol.domain.x[1][1:-1] u = sol.m[0][1][1:-1,1:-1] / rhoo for i in [0,mx,-1]: ax1.plot(y+x[i], u[i, :], 'b') for j in [0,my,-1]: ax1.plot(x+y[j], u[:,j], 'b') ax1.set_ylabel('velocity', color='b') for tl in ax1.get_yticklabels(): tl.set_color('b') ax1.set_ylim(-.5*rhoo*vmax, 1.5*rhoo*vmax) p = sol.m[0][0][1:-1,my] * la**2 / 3.0 p -= np.average(p) ax2.plot(x, p, 'r') ax2.set_ylabel('pressure', color='r') for tl in ax2.get_yticklabels(): tl.set_color('r') ax2.set_ylim(pressure_gradient*L, -pressure_gradient*L) plt.title('Poiseuille flow at t = {0:f}'.format(sol.t)) plt.draw() plt.pause(1.e-3)
def plot_spectrum(): #get the data... a_0=struct.unpack('>1024l',fpga.read('even',1024*4,0)) a_1=struct.unpack('>1024l',fpga.read('odd',1024*4,0)) interleave_a=[] for i in range(1024): interleave_a.append(a_0[i]) interleave_a.append(a_1[i]) pylab.figure(num=1,figsize=(10,10)) pylab.ioff() pylab.plot(interleave_a) #pylab.semilogy(interleave_a) pylab.title('Integration number %i.'%prev_integration) pylab.ylabel('Power (arbitrary units)') pylab.grid() pylab.xlabel('Channel') pylab.xlim(0,2048) pylab.ioff() pylab.hold(False) pylab.show() pylab.draw()
def dovis(self): """ Do runtime visualization. """ pylab.clf() phi = self.cc_data.get_var("phi") myg = self.cc_data.grid pylab.imshow(numpy.transpose(phi[myg.ilo:myg.ihi+1, myg.jlo:myg.jhi+1]), interpolation="nearest", origin="lower", extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax]) pylab.xlabel("x") pylab.ylabel("y") pylab.title("phi") pylab.colorbar() pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t) pylab.draw()
def tracks_movie(base, skip=1, frames=500, size=10): """ A movie of each particle as a point """ conf, track, pegs = load(base) fig = pl.figure(figsize=(size,size*conf['top']/conf['wall'])) plot = None for t in xrange(1,max(frames, track.shape[1]/skip)): tmp = track[:,t*skip,:] if not ((tmp[:,0] > 0) & (tmp[:,1] > 0) & (tmp[:,0] < conf['wall']) & (tmp[:,1] < conf['top'])).any(): continue if plot is None: plot = pl.plot(tmp[:,0], tmp[:,1], 'k,', alpha=1.0, ms=0.1)[0] pl.xticks([]) pl.yticks([]) pl.xlim(0,conf['wall']) pl.ylim(0,conf['top']) pl.tight_layout() else: plot.set_xdata(tmp[:,0]) plot.set_ydata(tmp[:,1]) pl.draw() pl.savefig(base+'-movie-%05d.png' % (t-1))
def plotSpectrum6(spectrum1On,spectrum2On,spectrum3On,spectrum4On,spectrum1Off,spectrum2Off,spectrum3Off,spectrum4Off,thetaL,thetaR,filename=""): pylab.ion() pylab.figure(0) pylab.clf() pylab.grid() pylab.subplot(321) pylab.plot(spectrum1On,'r') pylab.plot(spectrum1Off) pylab.title('Channel1') pylab.subplot(322) pylab.title('Channel2') pylab.plot(spectrum2On,'r') pylab.plot(spectrum2Off) pylab.subplot(323) pylab.title('Channel3') pylab.plot(spectrum3On,'r') pylab.plot(spectrum3Off) pylab.subplot(324) pylab.title('Channel4') pylab.plot(spectrum4On,'r') pylab.plot(spectrum4Off) pylab.subplot(325) pylab.title('ThetaL') pylab.plot(thetaL,'r') pylab.subplot(326) pylab.title('ThetaR') pylab.plot(thetaR,'r') pylab.draw() pylab.savefig(filename)
def plot(self): if not self.plot_state: return history = self.plot_state import pylab with self.pylab_interface: dream_views.plot_Med(history) pylab.draw()
def ukViz(fgi, axap, ap, axac, y): axap.set_xdata(ap[1,:]) axap.set_ydata(ap[0,:]) axac.set_xdata(y[1,:]) axac.set_ydata(y[0,:]) plt.draw() fgi.show()
def gui_repr(self): """Generate a GUI to represent the sentence alignments """ if __pylab_loaded__: fig_width = max(len(self.text_e), len(self.text_f)) + 1 fig_height = 3 pylab.figure(figsize=(fig_width*0.8, fig_height*0.8), facecolor='w') pylab.box(on=False) pylab.subplots_adjust(left=0, right=1, bottom=0, top=1) pylab.xlim(-1, fig_width - 1) pylab.ylim(0, fig_height) pylab.xticks([]) pylab.yticks([]) e = [0 for _ in xrange(len(self.text_e))] f = [0 for _ in xrange(len(self.text_f))] for (i, j) in self.align: e[i] = 1 f[j] = 1 # draw the middle line pylab.arrow(i, 2, j - i, -1, color='r') for i in xrange(len(e)): # draw e side line pylab.text(i, 2.5, self.text_e[i], ha = 'center', va = 'center', rotation=30) if e[i] == 1: pylab.arrow(i, 2.5, 0, -0.5, color='r', alpha=0.3, lw=2) for i in xrange(len(f)): # draw f side line pylab.text(i, 0.5, self.text_f[i], ha = 'center', va = 'center', rotation=30) if f[i] == 1: pylab.arrow(i, 0.5, 0, 0.5, color='r', alpha=0.3, lw=2) pylab.draw()
def keypress(event): global pos,cb,fig,dataset,nticks,ticks,colors,cmap if event.key==",": pos -= 1 if event.key==".": pos += 10 if event.key=="a": nticks+=1 ticks=[i/float(nticks) for i in range(nticks)] colors=[float2rgb(sqrt(i+1),mic,mxc) for i in range(len(ticks))] if event.key=="z": nticks-=1 ticks=[i/float(nticks) for i in range(nticks)] colors=[float2rgb(sqrt(i+1),mic,mxc) for i in range(len(ticks))] pos=pos%gridsz[0] if pstyle==1: ax=logplotter(X,Y,dataset[pos,:,:],ticks,colors) pl.title("%d Log plot energy density, use < and > to change plots"%pos) #P.colorbar(ax,ticks=ticks,drawedges=True) cb.update_bruteforce(ax) elif pstyle==2: plotter(X,Y,dataset[pos,:,:]) pl.title("%d Vacancy in Red, use < and > to change plots"%pos) elif pstyle==3: pl.imshow(dataset[pos,:,:],extent=[0,gridsz[0],0,gridsz[1]],cmap=cmap) pl.draw()
def save_plot(self, filename): plt.ion() targarr = np.array(self.targvalue) self.posi[0].set_xdata(self.wt_positions[:,0]) self.posi[0].set_ydata(self.wt_positions[:,1]) while len(self.plotel)>0: self.plotel.pop(0).remove() self.plotel = self.shape_plot.plot(np.array([self.wt_positions[[i,j],0] for i, j in self.elnet_layout.keys()]).T, np.array([self.wt_positions[[i,j],1] for i, j in self.elnet_layout.keys()]).T, 'y-', linewidth=1) for i in range(len(self.posb)): self.posb[i][0].set_xdata(self.iterations) self.posb[i][0].set_ydata(targarr[:,i]) self.legend.texts[i].set_text('%s = %8.2f'%(self.targname[i], targarr[-1,i])) self.objf_plot.set_xlim([0, self.iterations[-1]]) self.objf_plot.set_ylim([0.5, 1.2]) if not self.title == '': plt.title('%s = %8.2f'%(self.title, getattr(self, self.title))) plt.draw() #print self.iterations[-1] , ': ' + ', '.join(['%s=%6.2f'%(self.targname[i], targarr[-1,i]) for i in range(len(self.targname))]) with open(self.result_file+'.results','a') as f: f.write( '%d:'%(self.inc) + ', '.join(['%s=%6.2f'%(self.targname[i], targarr[-1,i]) for i in range(len(self.targname))]) + '\n') #plt.show() #plt.savefig(filename) display(plt.gcf()) #plt.show() clear_output(wait=True)
def plotSpectrum(spectrum,filename=""): pylab.ion() pylab.figure(0) pylab.clf() pylab.plot(spectrum) pylab.draw() pylab.savefig(filename)
def PSFrange(self,junkAx): """ Display function that you shouldn't call directly. """ #ca=pyl.gca() pyl.sca(self.sp1) print self.starsScat newLim=[self.sp1.get_xlim(),self.sp1.get_ylim()] self.psfPlotLimits=newLim[:] w=num.where((self.points[:,0]>=self.psfPlotLimits[0][0])&(self.points[:,0]<=self.psfPlotLimits[0][1])&(self.points[:,1]>=self.psfPlotLimits[1][0])&(self.points[:,1]<=self.psfPlotLimits[1][1]))[0] if self.starsScat<>None: self.starsScat.remove() self.starsScat=None for ii in range(len(self.showing)): if self.showing[ii]: self.moffPatchList[ii][0].remove() for ii in range(len(self.showing)): if ii not in w: self.showing[ii]=0 else: self.showing[ii]=1 for ii in range(len(self.showing)): if self.showing[ii]: self.moffPatchList[ii]=self.sp4.plot(self.moffr,self.moffs[ii]) self.sp4.set_xlim(0,30) self.sp4.set_ylim(0,1.02) pyl.draw()
def show(self, lenmavlist, block=True): """show graph""" if self.labels is not None: labels = self.labels.split(",") if len(labels) != len(fields) * lenmavlist: print( "Number of labels (%u) must match number of fields (%u)" % (len(labels), len(fields) * lenmavlist) ) return else: labels = None for fi in range(0, lenmavlist): timeshift = 0 for i in range(0, len(self.x)): if self.first_only[i] and fi != 0: self.x[i] = [] self.y[i] = [] if labels: lab = labels[fi * len(self.fields) : (fi + 1) * len(self.fields)] else: lab = self.fields[:] if self.multi: col = colors[:] else: col = colors[fi * len(self.fields) :] self.plotit(self.x, self.y, lab, colors=col) for i in range(0, len(self.x)): self.x[i] = [] self.y[i] = [] pylab.draw() pylab.show(block=block)
dev_fgen.write('func:user %s' % wf) wf_readback = dev_fgen.ask('func:user?').strip() # wait a second and sample waveform using scope time.sleep(1.0) samples = rigol_ds2000.data2array(dev_scope.ask('wav:data?')) # convert sample bytes to voltages yref = float(dev_scope.ask('wav:yref?')) yinc = float(dev_scope.ask('wav:yinc?')) yori = float(dev_scope.ask('wav:yorigin?')) samples = [(s - yref) * yinc - yori for s in samples] # plot data and create png files pylab.clf() pylab.plot(samples[200:1200]) if wf == wf_readback: pylab.title('%s' % wf) else: pylab.title('%s (%s)' % (wf, wf_readback)) pylab.draw() pylab.savefig('dg1000_%s.png' % wf.lower()) print('written dg1000_%s.png.' % wf.lower()) # create html file f = open('dg1000_builtins.html', 'w') for wf in waveform_names: print('<img src="dg1000_%s.png"/><br/>' % wf.lower(), file=f) f.close() print('written dg1000.html.')
def azeqview( map=None, fig=None, rot=None, zat=None, coord=None, unit="", xsize=800, ysize=None, reso=1.5, lamb=False, half_sky=False, title=None, nest=False, remove_dip=False, remove_mono=False, gal_cut=0, min=None, max=None, flip="astro", format="%.3g", cbar=True, cmap=None, norm=None, aspect=None, hold=False, sub=None, margins=None, notext=False, return_projected_map=False, ): """Plot a healpix map (given as an array) in Azimuthal equidistant projection or Lambert azimuthal equal-area projection. Parameters ---------- map : float, array-like or None An array containing the map, supports masked maps, see the `ma` function. If None, will display a blank map, useful for overplotting. fig : int or None, optional The figure number to use. Default: create a new figure rot : scalar or sequence, optional Describe the rotation to apply. In the form (lon, lat, psi) (unit: degrees) : the point at longitude *lon* and latitude *lat* will be at the center. An additional rotation of angle *psi* around this direction is applied. coord : sequence of character, optional Either one of 'G', 'E' or 'C' to describe the coordinate system of the map, or a sequence of 2 of these to rotate the map from the first to the second coordinate system. unit : str, optional A text describing the unit of the data. Default: '' xsize : int, optional The size of the image. Default: 800 ysize : None or int, optional The size of the image. Default: None= xsize reso : float, optional Resolution (in arcmin). Default: 1.5 arcmin lamb : bool, optional If True, plot Lambert azimuthal equal area instead of azimuthal equidistant. Default: False (az equidistant) half_sky : bool, optional Plot only one side of the sphere. Default: False title : str, optional The title of the plot. Default: 'Azimuthal equidistant view' or 'Lambert azimuthal equal-area view' (if lamb is True) nest : bool, optional If True, ordering scheme is NESTED. Default: False (RING) min : float, optional The minimum range value max : float, optional The maximum range value flip : {'astro', 'geo'}, optional Defines the convention of projection : 'astro' (default, east towards left, west towards right) or 'geo' (east towards roght, west towards left) remove_dip : bool, optional If :const:`True`, remove the dipole+monopole remove_mono : bool, optional If :const:`True`, remove the monopole gal_cut : float, scalar, optional Symmetric galactic cut for the dipole/monopole fit. Removes points in latitude range [-gal_cut, +gal_cut] format : str, optional The format of the scale label. Default: '%g' cbar : bool, optional Display the colorbar. Default: True notext : bool, optional If True, no text is printed around the map norm : {'hist', 'log', None} Color normalization, hist= histogram equalized color mapping, log= logarithmic color mapping, default: None (linear color mapping) hold : bool, optional If True, replace the current Axes by an Equidistant AzimuthalAxes. use this if you want to have multiple maps on the same figure. Default: False sub : int, scalar or sequence, optional Use only a zone of the current figure (same syntax as subplot). Default: None margins : None or sequence, optional Either None, or a sequence (left,bottom,right,top) giving the margins on left,bottom,right and top of the axes. Values are relative to figure (0-1). Default: None return_projected_map : bool if True returns the projected map in a 2d numpy array See Also -------- mollview, gnomview, cartview, orthview """ # Create the figure import pylab if not (hold or sub): f = pylab.figure(fig, figsize=(8.5, 5.4)) extent = (0.02, 0.05, 0.96, 0.9) elif hold: f = pylab.gcf() left, bottom, right, top = np.array(f.gca().get_position()).ravel() extent = (left, bottom, right - left, top - bottom) f.delaxes(f.gca()) else: # using subplot syntax f = pylab.gcf() if hasattr(sub, "__len__"): nrows, ncols, idx = sub else: nrows, ncols, idx = sub // 100, (sub % 100) // 10, (sub % 10) if idx < 1 or idx > ncols * nrows: raise ValueError("Wrong values for sub: %d, %d, %d" % (nrows, ncols, idx)) c, r = (idx - 1) % ncols, (idx - 1) // ncols if not margins: margins = (0.01, 0.0, 0.0, 0.02) extent = ( c * 1. / ncols + margins[0], 1. - (r + 1) * 1. / nrows + margins[1], 1. / ncols - margins[2] - margins[0], 1. / nrows - margins[3] - margins[1], ) extent = ( extent[0] + margins[0], extent[1] + margins[1], extent[2] - margins[2] - margins[0], extent[3] - margins[3] - margins[1], ) # extent = (c*1./ncols, 1.-(r+1)*1./nrows,1./ncols,1./nrows) # f=pylab.figure(fig,figsize=(8.5,5.4)) # Starting to draw : turn interactive off wasinteractive = pylab.isinteractive() pylab.ioff() try: if map is None: map = np.zeros(12) + np.inf cbar = False ax = PA.HpxAzimuthalAxes( f, extent, coord=coord, rot=rot, format=format, flipconv=flip ) f.add_axes(ax) if remove_dip: map = pixelfunc.remove_dipole( map, gal_cut=gal_cut, nest=nest, copy=True, verbose=True ) elif remove_mono: map = pixelfunc.remove_monopole( map, gal_cut=gal_cut, nest=nest, copy=True, verbose=True ) img = ax.projmap( map, nest=nest, xsize=xsize, ysize=ysize, reso=reso, lamb=lamb, half_sky=half_sky, coord=coord, vmin=min, vmax=max, cmap=cmap, norm=norm, ) if cbar: im = ax.get_images()[0] b = im.norm.inverse(np.linspace(0, 1, im.cmap.N + 1)) v = np.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N) if matplotlib.__version__ >= "0.91.0": cb = f.colorbar( im, ax=ax, orientation="horizontal", shrink=0.5, aspect=25, ticks=PA.BoundaryLocator(), pad=0.05, fraction=0.1, boundaries=b, values=v, format=format, ) else: # for older matplotlib versions, no ax kwarg cb = f.colorbar( im, orientation="horizontal", shrink=0.5, aspect=25, ticks=PA.BoundaryLocator(), pad=0.05, fraction=0.1, boundaries=b, values=v, format=format, ) cb.solids.set_rasterized(True) if title is None: if lamb: title = "Lambert azimuthal equal-area view" else: title = "Azimuthal equidistant view" ax.set_title(title) if not notext: ax.text( 0.86, 0.05, ax.proj.coordsysstr, fontsize=14, fontweight="bold", transform=ax.transAxes, ) if cbar: cb.ax.text( 0.5, -1.0, unit, fontsize=14, transform=cb.ax.transAxes, ha="center", va="center", ) f.sca(ax) finally: pylab.draw() if wasinteractive: pylab.ion() # pylab.show() if return_projected_map: return img
def evolve(nx, kappa, tau, tmax, dovis=0, returnInit=0): """ the main evolution loop. Evolve phi_t = kappa phi_{xx} + (1/tau) R(phi) from t = 0 to tmax """ # create the grid gr = grid(nx, ng=1, xmin=0.0, xmax=100.0, vars=["phi", "phi1", "phi2"]) # pointers to the data at various stages phi = gr.data["phi"] phi1 = gr.data["phi1"] phi2 = gr.data["phi2"] # initialize gr.initialize(20, 1) phiInit = phi.copy() # runtime plotting if dovis == 1: pylab.ion() t = 0.0 while (t < tmax): dt = estDt(gr, kappa, tau) if (t + dt > tmax): dt = tmax - t # react for dt/2 phi1[:] = react(gr, phi, tau, dt / 2) gr.fillBC("phi1") # diffuse for dt phi2[:] = diffuse(gr, phi1, kappa, dt) gr.fillBC("phi2") # react for dt/2 -- this is the updated solution phi[:] = react(gr, phi2, tau, dt / 2) gr.fillBC("phi") t += dt if dovis == 1: pylab.clf() pylab.plot(gr.x, phi) pylab.xlim(gr.xmin, gr.xmax) pylab.ylim(0.0, 1.0) pylab.draw() print(t) if returnInit == 1: return phi, gr.x, phiInit else: return phi, gr.x
def main(): """ NAME core_depthplot.py DESCRIPTION plots various measurements versus core_depth or age. plots data flagged as 'FS-SS-C' as discrete samples. SYNTAX core_depthplot.py [command line optins] OPTIONS -h prints help message and quits -f FILE: specify input magic_measurments format file from magi -fsum FILE: specify input LIMS database (IODP) core summary csv file -fwig FILE: specify input depth,wiggle to plot, in magic format with sample_core_depth key for depth -fsa FILE: specify input er_samples format file from magic for depth -fa FILE: specify input er_ages format file from magic for age NB: must have either -fsa OR -fa (not both) -fsp FILE sym size: specify input zeq_specimen format file from magic, sym and size NB: PCAs will have specified color, while fisher means will be white with specified color as the edgecolor -fres FILE specify input pmag_results file from magic, sym and size -LP [AF,T,ARM,IRM, X] step [in mT,C,mT,mT, mass/vol] to plot -S do not plot blanket treatment data (if this is set, you don't need the -LP) -sym SYM SIZE, symbol, size for continuous points (e.g., ro 5, bs 10, g^ 10 for red dot, blue square, green triangle), default is blue dot at 5 pt -D do not plot declination -M do not plot magnetization -log plot magnetization on a log scale -L do not connect dots with a line -I do not plot inclination -d min max [in m] depth range to plot -n normalize by weight in er_specimen table -Iex: plot the expected inc at lat - only available for results with lat info in file -ts TS amin amax: plot the GPTS for the time interval between amin and amax (numbers in Ma) TS: [ck95, gts04, gts12] -ds [mbsf,mcd] specify depth scale, mbsf default -fmt [svg, eps, pdf, png] specify output format for plot (default: svg) -sav save plot silently DEFAULTS: Measurements file: magic_measurements.txt Samples file: er_samples.txt NRM step Summary file: none """ meas_file = 'magic_measurements.txt' intlist = [ 'measurement_magnitude', 'measurement_magn_moment', 'measurement_magn_volume', 'measurement_magn_mass' ] samp_file = 'er_samples.txt' depth_scale = 'sample_core_depth' wt_file = '' verbose = pmagplotlib.verbose width = 10 sym, size = 'bo', 5 Ssym, Ssize = 'cs', 5 method, fmt = "LT-NO", '.svg' step = 0 pcol = 3 pel = 3 pltD, pltI, pltM, pltL, pltS = 1, 1, 1, 1, 1 logit = 0 maxInt = -1000 minInt = 1e10 maxSuc = -1000 minSuc = 10000 plotexp, pTS = 0, 0 dir_path = "." sum_file = "" suc_file = "" age_file = "" spc_file = "" res_file = "" ngr_file = "" wig_file = "" title, location = "", "" if '-WD' in sys.argv: ind = sys.argv.index('-WD') dir_path = sys.argv[ind + 1] norm = 0 if '-h' in sys.argv: print(main.__doc__) sys.exit() if '-L' in sys.argv: pltL = 0 if '-S' in sys.argv: pltS = 0 # don't plot the bulk measurements at all if '-D' in sys.argv: pltD = 0 pcol -= 1 pel -= 1 width -= 2 if '-I' in sys.argv: pltI = 0 pcol -= 1 pel -= 1 width -= 2 if '-M' in sys.argv: pltM = 0 pcol -= 1 pel -= 1 width -= 2 if '-log' in sys.argv: logit = 1 if '-ds' in sys.argv and 'mcd' in sys.argv: depth_scale = 'sample_composite_depth' if '-sym' in sys.argv: ind = sys.argv.index('-sym') sym = sys.argv[ind + 1] size = float(sys.argv[ind + 2]) if '-f' in sys.argv: ind = sys.argv.index('-f') meas_file = sys.argv[ind + 1] if '-fsa' in sys.argv: ind = sys.argv.index('-fsa') samp_file = sys.argv[ind + 1] if '-fa' in sys.argv: print(main.__doc__) print('only -fsa OR -fa - not both') sys.exit() elif '-fa' in sys.argv: ind = sys.argv.index('-fa') age_file = sys.argv[ind + 1] if '-fsp' in sys.argv: ind = sys.argv.index('-fsp') spc_file = dir_path + '/' + sys.argv[ind + 1] spc_sym = sys.argv[ind + 2] spc_size = float(sys.argv[ind + 3]) if '-fres' in sys.argv: ind = sys.argv.index('-fres') res_file = dir_path + '/' + sys.argv[ind + 1] res_sym = sys.argv[ind + 2] res_size = float(sys.argv[ind + 3]) if '-fwig' in sys.argv: ind = sys.argv.index('-fwig') wig_file = dir_path + '/' + sys.argv[ind + 1] pcol += 1 width += 2 if '-fsum' in sys.argv: ind = sys.argv.index('-fsum') sum_file = dir_path + '/' + sys.argv[ind + 1] if '-fmt' in sys.argv: ind = sys.argv.index('-fmt') fmt = '.' + sys.argv[ind + 1] if '-sav' in sys.argv: plots = 1 verbose = 0 if '-LP' in sys.argv: ind = sys.argv.index('-LP') meth = sys.argv[ind + 1] if meth == "AF": step = round(float(sys.argv[ind + 2]) * 1e-3, 6) method = 'LT-AF-Z' elif meth == 'T': step = round(float(sys.argv[ind + 2]) + 273, 6) method = 'LT-T-Z' elif meth == 'ARM': method = 'LT-AF-I' step = round(float(sys.argv[ind + 2]) * 1e-3, 6) elif meth == 'IRM': method = 'LT-IRM' step = round(float(sys.argv[ind + 2]) * 1e-3, 6) elif meth == 'X': method = 'LP-X' pcol += 1 if sys.argv[ind + 2] == 'mass': suc_key = 'measurement_chi_mass' elif sys.argv[ind + 2] == 'vol': suc_key = 'measurement_chi_volume' else: print('error in susceptibility units') sys.exit() else: print('method not supported') sys.exit() if '-n' in sys.argv: ind = sys.argv.index('-n') wt_file = dir_path + '/' + sys.argv[ind + 1] norm = 1 dmin, dmax = -1, -1 if '-d' in sys.argv: ind = sys.argv.index('-d') dmin = float(sys.argv[ind + 1]) dmax = float(sys.argv[ind + 2]) if '-ts' in sys.argv: ind = sys.argv.index('-ts') ts = sys.argv[ind + 1] amin = float(sys.argv[ind + 2]) amax = float(sys.argv[ind + 3]) pTS = 1 pcol += 1 width += 2 # # # get data read in meas_file = dir_path + '/' + meas_file if age_file == "": samp_file = dir_path + '/' + samp_file Samps, file_type = pmag.magic_read(samp_file) else: depth_scale = 'age' age_file = dir_path + '/' + age_file Samps, file_type = pmag.magic_read(age_file) age_unit = "" if spc_file != "": Specs, file_type = pmag.magic_read(spc_file) if res_file != "": Results, file_type = pmag.magic_read(res_file) if norm == 1: ErSpecs, file_type = pmag.magic_read(wt_file) print(len(ErSpecs), ' specimens read in from ', wt_file) Cores = [] core_depth_key = "Top depth cored CSF (m)" if sum_file != "": input = open(sum_file, 'r').readlines() if "Core Summary" in input[0]: headline = 1 else: headline = 0 keys = input[headline].replace('\n', '').split(',') if "Core Top (m)" in keys: core_depth_key = "Core Top (m)" if "Core Label" in keys: core_label_key = "Core Label" if "Core label" in keys: core_label_key = "Core label" for line in input[2:]: if 'TOTALS' not in line: CoreRec = {} for k in range(len(keys)): CoreRec[keys[k]] = line.split(',')[k] Cores.append(CoreRec) if len(Cores) == 0: print( 'no Core depth information available: import core summary file' ) sum_file = "" Data = [] if depth_scale == 'sample_core_depth': ylab = "Depth (mbsf)" elif depth_scale == 'age': ylab = "Age" else: ylab = "Depth (mcd)" # collect the data for plotting declination Depths, Decs, Incs, Ints = [], [], [], [] SDepths, SDecs, SIncs, SInts = [], [], [], [] SSucs = [] samples = [] methods, steps, m2 = [], [], [] if pltS: # plot the bulk measurement data Meas, file_type = pmag.magic_read(meas_file) meas_key = 'measurement_magn_moment' print(len(Meas), ' measurements read in from ', meas_file) for m in intlist: # find the intensity key with data meas_data = pmag.get_dictitem( Meas, m, '', 'F') # get all non-blank data for this specimen if len(meas_data) > 0: meas_key = m break m1 = pmag.get_dictitem(Meas, 'magic_method_codes', method, 'has') # fish out the desired method code if method == 'LT-T-Z': m2 = pmag.get_dictitem(m1, 'treatment_temp', str(step), 'eval') # fish out the desired step elif 'LT-AF' in method: m2 = pmag.get_dictitem(m1, 'treatment_ac_field', str(step), 'eval') elif 'LT-IRM' in method: m2 = pmag.get_dictitem(m1, 'treatment_dc_field', str(step), 'eval') elif 'LT-X' in method: m2 = pmag.get_dictitem(m1, suc_key, '', 'F') if len(m2) > 0: for rec in m2: # fish out depths and weights D = pmag.get_dictitem(Samps, 'er_sample_name', rec['er_sample_name'], 'T') if not D: # if using an age_file, you may need to sort by site D = pmag.get_dictitem(Samps, 'er_site_name', rec['er_site_name'], 'T') depth = pmag.get_dictitem(D, depth_scale, '', 'F') if len(depth) > 0: if ylab == 'Age': ylab = ylab + ' (' + depth[0][ 'age_unit'] + ')' # get units of ages - assume they are all the same! rec['core_depth'] = float(depth[0][depth_scale]) rec['magic_method_codes'] = rec[ 'magic_method_codes'] + ':' + depth[0][ 'magic_method_codes'] if norm == 1: specrecs = pmag.get_dictitem(ErSpecs, 'er_specimen_name', rec['er_specimen_name'], 'T') specwts = pmag.get_dictitem(specrecs, 'specimen_weight', "", 'F') if len(specwts) > 0: rec['specimen_weight'] = specwts[0][ 'specimen_weight'] Data.append( rec ) # fish out data with core_depth and (if needed) weights else: Data.append( rec ) # fish out data with core_depth and (if needed) weights if title == "": pieces = rec['er_sample_name'].split('-') location = rec['er_location_name'] title = location SData = pmag.sort_diclist(Data, 'core_depth') for rec in SData: # fish out bulk measurement data from desired depths if dmax == -1 or float(rec['core_depth']) < dmax and float( rec['core_depth']) > dmin: Depths.append((rec['core_depth'])) if method == "LP-X": SSucs.append(float(rec[suc_key])) else: if pltD == 1: Decs.append(float(rec['measurement_dec'])) if pltI == 1: Incs.append(float(rec['measurement_inc'])) if norm == 0 and pltM == 1: Ints.append(float(rec[meas_key])) if norm == 1 and pltM == 1: Ints.append( old_div(float(rec[meas_key]), float(rec['specimen_weight']))) if len(SSucs) > 0: maxSuc = max(SSucs) minSuc = min(SSucs) if len(Ints) > 1: maxInt = max(Ints) minInt = min(Ints) if len(Depths) == 0: print('no bulk measurement data matched your request') SpecDepths, SpecDecs, SpecIncs = [], [], [] FDepths, FDecs, FIncs = [], [], [] if spc_file != "": # add depths to spec data print('spec file found') BFLs = pmag.get_dictitem( Specs, 'magic_method_codes', 'DE-BFL', 'has') # get all the discrete data with best fit lines for spec in BFLs: if location == "": location = spec['er_location_name'] samp = pmag.get_dictitem(Samps, 'er_sample_name', spec['er_sample_name'], 'T') if len(samp) > 0 and depth_scale in list( samp[0].keys()) and samp[0][depth_scale] != "": if ylab == 'Age': ylab = ylab + ' (' + samp[0][ 'age_unit'] + ')' # get units of ages - assume they are all the same! if dmax == -1 or float(samp[0][depth_scale]) < dmax and float( samp[0][depth_scale]) > dmin: # filter for depth SpecDepths.append(float( samp[0][depth_scale])) # fish out data with core_depth SpecDecs.append(float( spec['specimen_dec'])) # fish out data with core_depth SpecIncs.append(float( spec['specimen_inc'])) # fish out data with core_depth else: print('no core_depth found for: ', spec['er_specimen_name']) FMs = pmag.get_dictitem( Specs, 'magic_method_codes', 'DE-FM', 'has') # get all the discrete data with best fit lines for spec in FMs: if location == "": location = spec['er_location_name'] samp = pmag.get_dictitem(Samps, 'er_sample_name', spec['er_sample_name'], 'T') if len(samp) > 0 and depth_scale in list( samp[0].keys()) and samp[0][depth_scale] != "": if ylab == 'Age': ylab = ylab + ' (' + samp[0][ 'age_unit'] + ')' # get units of ages - assume they are all the same! if dmax == -1 or float(samp[0][depth_scale]) < dmax and float( samp[0][depth_scale]) > dmin: # filter for depth FDepths.append(float( samp[0][depth_scale])) # fish out data with core_depth FDecs.append(float( spec['specimen_dec'])) # fish out data with core_depth FIncs.append(float( spec['specimen_inc'])) # fish out data with core_depth else: print('no core_depth found for: ', spec['er_specimen_name']) ResDepths, ResDecs, ResIncs = [], [], [] if 'age' in depth_scale: # set y-key res_scale = 'average_age' else: res_scale = 'average_height' if res_file != "": #creates lists of Result Data for res in Results: meths = res['magic_method_codes'].split(":") if 'DE-FM' in meths: if dmax == -1 or float(res[res_scale]) < dmax and float( res[res_scale]) > dmin: # filter for depth ResDepths.append(float( res[res_scale])) # fish out data with core_depth ResDecs.append(float( res['average_dec'])) # fish out data with core_depth ResIncs.append(float( res['average_inc'])) # fish out data with core_depth Susc, Sus_depths = [], [] if dmin == -1: if len(Depths) > 0: dmin, dmax = Depths[0], Depths[-1] if len(FDepths) > 0: dmin, dmax = Depths[0], Depths[-1] if pltS == 1 and len(SDepths) > 0: if SDepths[0] < dmin: dmin = SDepths[0] if SDepths[-1] > dmax: dmax = SDepths[-1] if len(SpecDepths) > 0: if min(SpecDepths) < dmin: dmin = min(SpecDepths) if max(SpecDepths) > dmax: dmax = max(SpecDepths) if len(ResDepths) > 0: if min(ResDepths) < dmin: dmin = min(ResDepths) if max(ResDepths) > dmax: dmax = max(ResDepths) if suc_file != "": sucdat = open(suc_file, 'r').readlines() keys = sucdat[0].replace('\n', '').split(',') # splits on underscores for line in sucdat[1:]: SucRec = {} for k in range(len(keys)): SucRec[keys[k]] = line.split(',')[k] if float(SucRec['Top Depth (m)']) < dmax and float( SucRec['Top Depth (m)'] ) > dmin and SucRec['Magnetic Susceptibility (80 mm)'] != "": Susc.append(float(SucRec['Magnetic Susceptibility (80 mm)'])) if Susc[-1] > maxSuc: maxSuc = Susc[-1] if Susc[-1] < minSuc: minSuc = Susc[-1] Sus_depths.append(float(SucRec['Top Depth (m)'])) WIG, WIG_depths = [], [] if wig_file != "": wigdat, file_type = pmag.magic_read(wig_file) swigdat = pmag.sort_diclist(wigdat, depth_scale) keys = list(wigdat[0].keys()) for key in keys: if key != depth_scale: plt_key = key break for wig in swigdat: if float(wig[depth_scale]) < dmax and float( wig[depth_scale]) > dmin: WIG.append(float(wig[plt_key])) WIG_depths.append(float(wig[depth_scale])) tint = 4.5 plt = 1 if len(Decs) > 0 and len(Depths) > 0 or ( len(SpecDecs) > 0 and len(SpecDepths) > 0 ) or (len(ResDecs) > 0 and len(ResDepths) > 0) or (len(SDecs) > 0 and len(SDepths) > 0) or ( len(SInts) > 0 and len(SDepths) > 0) or (len(SIncs) > 0 and len(SDepths) > 0): pylab.figure(1, figsize=(width, 8)) version_num = pmag.get_version() pylab.figtext(.02, .01, version_num) if pltD == 1: ax = pylab.subplot(1, pcol, plt) if pltL == 1: pylab.plot(Decs, Depths, 'k') if len(Decs) > 0: pylab.plot(Decs, Depths, sym, markersize=size) if len(Decs) == 0 and pltL == 1 and len(SDecs) > 0: pylab.plot(SDecs, SDepths, 'k') if len(SDecs) > 0: pylab.plot(SDecs, SDepths, Ssym, markersize=Ssize) if spc_file != "": pylab.plot(SpecDecs, SpecDepths, spc_sym, markersize=spc_size) if spc_file != "" and len(FDepths) > 0: pylab.scatter(FDecs, FDepths, marker=spc_sym[-1], edgecolor=spc_sym[0], facecolor='white', s=spc_size**2) if res_file != "": pylab.plot(ResDecs, ResDepths, res_sym, markersize=res_size) if sum_file != "": for core in Cores: depth = float(core[core_depth_key]) if depth > dmin and depth < dmax: pylab.plot([0, 360.], [depth, depth], 'b--') if pel == plt: pylab.text(360, depth + tint, core[core_label_key]) if pel == plt: pylab.axis([0, 400, dmax, dmin]) else: pylab.axis([0, 360., dmax, dmin]) pylab.xlabel('Declination') pylab.ylabel(ylab) plt += 1 pmagplotlib.delticks(ax) # dec xticks are too crowded otherwise if pltI == 1: pylab.subplot(1, pcol, plt) if pltL == 1: pylab.plot(Incs, Depths, 'k') if len(Incs) > 0: pylab.plot(Incs, Depths, sym, markersize=size) if len(Incs) == 0 and pltL == 1 and len(SIncs) > 0: pylab.plot(SIncs, SDepths, 'k') if len(SIncs) > 0: pylab.plot(SIncs, SDepths, Ssym, markersize=Ssize) if spc_file != "" and len(SpecDepths) > 0: pylab.plot(SpecIncs, SpecDepths, spc_sym, markersize=spc_size) if spc_file != "" and len(FDepths) > 0: pylab.scatter(FIncs, FDepths, marker=spc_sym[-1], edgecolor=spc_sym[0], facecolor='white', s=spc_size**2) if res_file != "": pylab.plot(ResIncs, ResDepths, res_sym, markersize=res_size) if sum_file != "": for core in Cores: depth = float(core[core_depth_key]) if depth > dmin and depth < dmax: if pel == plt: pylab.text(90, depth + tint, core[core_label_key]) pylab.plot([-90, 90], [depth, depth], 'b--') pylab.plot([0, 0], [dmax, dmin], 'k-') if pel == plt: pylab.axis([-90, 110, dmax, dmin]) else: pylab.axis([-90, 90, dmax, dmin]) pylab.xlabel('Inclination') pylab.ylabel('') plt += 1 if pltM == 1 and len(Ints) > 0 or len(SInts) > 0: pylab.subplot(1, pcol, plt) for pow in range(-10, 10): if maxInt * 10**pow > 1: break if logit == 0: for k in range(len(Ints)): Ints[k] = Ints[k] * 10**pow for k in range(len(SInts)): SInts[k] = SInts[k] * 10**pow if pltL == 1 and len(Ints) > 0: pylab.plot(Ints, Depths, 'k') if len(Ints) > 0: pylab.plot(Ints, Depths, sym, markersize=size) if len(Ints) == 0 and pltL == 1 and len(SInts) > 0: pylab.plot(SInts, SDepths, 'k-') if len(SInts) > 0: pylab.plot(SInts, SDepths, Ssym, markersize=Ssize) if sum_file != "": for core in Cores: depth = float(core[core_depth_key]) pylab.plot([0, maxInt * 10**pow + .1], [depth, depth], 'b--') if depth > dmin and depth < dmax: pylab.text(maxInt * 10**pow - .2 * maxInt * 10**pow, depth + tint, core[core_label_key]) pylab.axis([0, maxInt * 10**pow + .1, dmax, dmin]) if norm == 0: pylab.xlabel('%s %i %s' % ('Intensity (10^-', pow, ' Am^2)')) else: pylab.xlabel('%s %i %s' % ('Intensity (10^-', pow, ' Am^2/kg)')) else: if pltL == 1: pylab.semilogx(Ints, Depths, 'k') if len(Ints) > 0: pylab.semilogx(Ints, Depths, sym, markersize=size) if len(Ints) == 0 and pltL == 1 and len(SInts) > 0: pylab.semilogx(SInts, SDepths, 'k') if len(Ints) == 0 and pltL == 1 and len(SInts) > 0: pylab.semilogx(SInts, SDepths, 'k') if len(SInts) > 0: pylab.semilogx(SInts, SDepths, Ssym, markersize=Ssize) if sum_file != "": for core in Cores: depth = float(core[core_depth_key]) pylab.semilogx([minInt, maxInt], [depth, depth], 'b--') if depth > dmin and depth < dmax: pylab.text(maxInt - .2 * maxInt, depth + tint, core[core_label_key]) pylab.axis([0, maxInt, dmax, dmin]) if norm == 0: pylab.xlabel('Intensity (Am^2)') else: pylab.xlabel('Intensity (Am^2/kg)') plt += 1 if suc_file != "" or len(SSucs) > 0: pylab.subplot(1, pcol, plt) if len(Susc) > 0: if pltL == 1: pylab.plot(Susc, Sus_depths, 'k') if logit == 0: pylab.plot(Susc, Sus_depths, sym, markersize=size) if logit == 1: pylab.semilogx(Susc, Sus_depths, sym, markersize=size) if len(SSucs) > 0: if logit == 0: pylab.plot(SSucs, SDepths, sym, markersize=size) if logit == 1: pylab.semilogx(SSucs, SDepths, sym, markersize=size) if sum_file != "": for core in Cores: depth = float(core[core_depth_key]) if logit == 0: pylab.plot([minSuc, maxSuc], [depth, depth], 'b--') if logit == 1: pylab.semilogx([minSuc, maxSuc], [depth, depth], 'b--') pylab.axis([minSuc, maxSuc, dmax, dmin]) pylab.xlabel('Susceptibility') plt += 1 if wig_file != "": pylab.subplot(1, pcol, plt) pylab.plot(WIG, WIG_depths, 'k') if sum_file != "": for core in Cores: depth = float(core[core_depth_key]) pylab.plot([WIG[0], WIG[-1]], [depth, depth], 'b--') pylab.axis([min(WIG), max(WIG), dmax, dmin]) pylab.xlabel(plt_key) plt += 1 if pTS == 1: ax1 = pylab.subplot(1, pcol, plt) ax1.axis([-.25, 1.5, amax, amin]) plt += 1 TS, Chrons = pmag.get_TS(ts) X, Y, Y2 = [0, 1], [], [] cnt = 0 if amin < TS[1]: # in the Brunhes Y = [amin, amin] # minimum age Y1 = [TS[1], TS[1]] # age of the B/M boundary ax1.fill_between(X, Y, Y1, facecolor='black') # color in Brunhes, black for d in TS[1:]: pol = cnt % 2 cnt += 1 if d <= amax and d >= amin: ind = TS.index(d) Y = [TS[ind], TS[ind]] Y1 = [TS[ind + 1], TS[ind + 1]] if pol: ax1.fill_between( X, Y, Y1, facecolor='black') # fill in every other time ax1.plot([0, 1, 1, 0, 0], [amin, amin, amax, amax, amin], 'k-') ax2 = ax1.twinx() pylab.ylabel("Age (Ma): " + ts) for k in range(len(Chrons) - 1): c = Chrons[k] cnext = Chrons[k + 1] d = cnext[1] - old_div((cnext[1] - c[1]), 3.) if d >= amin and d < amax: ax2.plot([1, 1.5], [c[1], c[1]], 'k-') # make the Chron boundary tick ax2.text(1.05, d, c[0]) # ax2.axis([-.25, 1.5, amax, amin]) figname = location + '_m:_' + method + '_core-depthplot' + fmt pylab.title(location) if verbose: pylab.draw() ans = input("S[a]ve plot? ") if ans == 'a': pylab.savefig(figname) print('Plot saved as ', figname) elif plots: pylab.savefig(figname) print('Plot saved as ', figname) sys.exit()
invert=True, stretch=stretch, vmin=vmin, vmax=vmax) FF.show_lines([ np.array([[origin, origin], [ pars['cv'] - pars['wv'] / 2., pars['cv'] + pars['wv'] / 2., ]]) ], color='r') FF.recenter(pars['cx'], pars['cv'], width=pars['wx'], height=pars['wv']) # show() is unfortunately required before the text labels are set pl.draw() pl.show() # may have to find a better way to force this to work: FF._ax1.set_yticklabels( [str(float(L.get_text()) / 1e3) for L in FF._ax1.get_yticklabels()]) FF._ax1.set_ylabel("Velocity (km/s)") FF._ax1.set_xticklabels( [str(float(L.get_text()) * 3600) for L in FF._ax1.get_xticklabels()]) FF._ax1.set_xlabel("Offset (arcsec)") FF.save(paths.fpath('outflows_pv/' + outname.format(extension='png'))) h2pixels = pars['path'].sample_points(1.0, h2wcs) h2velos = [ h2velomap[0].data[x, y] for y, x in zip(*h2pixels) if x > 0 and y > 0 and x < h2velomap[0].data.shape[1] and y < h2velomap[0].data.shape[0] ]
def crosscorrelate(sua1, sua2, lag=None, n_pred=1, predictor=None, display=False, kwargs={}): """Cross-correlation between two series of discrete events (e.g. spikes). Calculates the cross-correlation between two vectors containing event times. Returns ``(differeces, pred, norm)``. See below for details. Adapted from original script written by Martin P. Nawrot for the FIND MATLAB toolbox [1]_. Parameters ---------- sua1, sua2 : 1D row or column `ndarray` or `SpikeTrain` Event times. If sua2 == sua1, the result is the autocorrelogram. lag : float Lag for which relative event timing is considered with a max difference of +/- lag. A default lag is computed from the inter-event interval of the longer of the two sua arrays. n_pred : int Number of surrogate compilations for the predictor. This influences the total length of the predictor output array predictor : {None, 'shuffle'} Determines the type of bootstrap predictor to be used. 'shuffle' shuffles interevent intervals of the longer input array and calculates relative differences with the shorter input array. `n_pred` determines the number of repeated shufflings, resulting differences are pooled from all repeated shufflings. display : boolean If True the corresponding plots will be displayed. If False, int, int_ and norm will be returned. kwargs : dict Arguments to be passed to np.histogram. Returns ------- differences : np array Accumulated differences of events in `sua1` minus the events in `sua2`. Thus positive values relate to events of `sua2` that lead events of `sua1`. Units are the same as the input arrays. pred : np array Accumulated differences based on the prediction method. The length of `pred` is ``n_pred * length(differences)``. Units are the same as the input arrays. norm : float Normalization factor used to scale the bin heights in `differences` and `pred`. ``differences/norm`` and ``pred/norm`` correspond to the linear correlation coefficient. Examples -------- >> crosscorrelate(np_array1, np_array2) >> crosscorrelate(spike_train1, spike_train2) >> crosscorrelate(spike_train1, spike_train2, lag = 150.0) >> crosscorrelate(spike_train1, spike_train2, display=True, kwargs={'bins':100}) See also -------- ccf .. [1] Meier R, Egert U, Aertsen A, Nawrot MP, "FIND - a unified framework for neural data analysis"; Neural Netw. 2008 Oct; 21(8):1085-93. """ assert predictor is 'shuffle' or predictor is None, "predictor must be \ either None or 'shuffle'. Other predictors are not yet implemented." #Check whether sua1 and sua2 are SpikeTrains or arrays sua = [] for x in (sua1, sua2): #if isinstance(x, SpikeTrain): if hasattr(x, 'spike_times'): sua.append(x.spike_times) elif x.ndim == 1: sua.append(x) elif x.ndim == 2 and (x.shape[0] == 1 or x.shape[1] == 1): sua.append(x.ravel()) else: raise TypeError("sua1 and sua2 must be either instances of the" \ "SpikeTrain class or column/row vectors") sua1 = sua[0] sua2 = sua[1] if sua1.size < sua2.size: if lag is None: lag = np.ceil(10 * np.mean(np.diff(sua1))) reverse = False else: if lag is None: lag = np.ceil(20 * np.mean(np.diff(sua2))) sua1, sua2 = sua2, sua1 reverse = True #construct predictor if predictor is 'shuffle': isi = np.diff(sua2) sua2_ = np.array([]) for ni in xrange(1, n_pred + 1): idx = np.random.permutation(isi.size - 1) sua2_ = np.append( sua2_, np.add(np.insert((np.cumsum(isi[idx])), 0, 0), sua2.min() + (np.random.exponential(isi.mean())))) #calculate cross differences in spike times differences = np.array([]) pred = np.array([]) for k in xrange(0, sua1.size): differences = np.append( differences, sua1[k] - sua2[np.nonzero((sua2 > sua1[k] - lag) & (sua2 < sua1[k] + lag))]) if predictor == 'shuffle': for k in xrange(0, sua1.size): pred = np.append( pred, sua1[k] - sua2_[np.nonzero((sua2_ > sua1[k] - lag) & (sua2_ < sua1[k] + lag))]) if reverse is True: differences = -differences pred = -pred norm = np.sqrt(sua1.size * sua2.size) # Plot the results if display=True if display: subplot = get_display(display) if not subplot or not HAVE_PYLAB: return differences, pred, norm else: # Plot the cross-correlation try: counts, bin_edges = np.histogram(differences, **kwargs) edge_distances = np.diff(bin_edges) bin_centers = bin_edges[1:] - edge_distances / 2 counts = counts / norm xlabel = "Time" ylabel = "Cross-correlation coefficient" #NOTE: the x axis corresponds to the upper edge of each bin subplot.plot(bin_centers, counts, label='cross-correlation', color='b') if predictor is None: set_labels(subplot, xlabel, ylabel) pylab.draw() elif predictor is 'shuffle': # Plot the predictor norm_ = norm * n_pred counts_, bin_edges_ = np.histogram(pred, **kwargs) counts_ = counts_ / norm_ subplot.plot(bin_edges_[1:], counts_, label='predictor') subplot.legend() pylab.draw() except ValueError: print("There are no correlated events within the selected lag"\ " window of %s" % lag) else: return differences, pred, norm
def grid(g, psi_in=None, psi_out=None, nrd=None, npl=None): # plot and check the field fig = figure(num=0, figsize=(10, 12)) ax = fig.add_subplot(111) nlev = 100 minf = numpy.min(g.psi) maxf = numpy.max(g.psi) levels = numpy.arange( numpy.float(nlev)) * (maxf - minf) / numpy.float(nlev - 1) + minf ax.contour(g.r, g.z, g.psi, levels=levels) ax.set_aspect('equal') show(block=False) plot(g.xlim, g.ylim, 'g-') draw() npsigrid = old_div( numpy.arange(numpy.size(g.pres)).astype(float), (numpy.size(g.pres) - 1)) #fpsi = numpy.zeros((2, numpy.size(fpol)), numpy.float64) #fpsi[0,:] = (simagx + npsigrid * ( sibdry -simagx )) #fpsi[1,:] = fpol rz_grid = Bunch( nr=g.nx, nz=g.ny, # Number of grid points r=g.r[:, 0], z=g.z[0, :], # R and Z as 1D arrays simagx=g.simagx, sibdry=g.sibdry, # Range of psi psi=g.psi, # Poloidal flux in Weber/rad on grid points npsigrid=npsigrid, # Normalised psi grid for fpol, pres and qpsi fpol=g.fpol, # Poloidal current function on uniform flux grid pres=g.pres, # Plasma pressure in nt/m^2 on uniform flux grid qpsi=g.qpsi, # q values on uniform flux grid nlim=g.nlim, rlim=g.xlim, zlim=g.ylim) # Wall boundary critical = analyse_equil(g.psi, g.r[:, 0], g.z[0, :]) settings = Bunch(psi_inner=psi_in, psi_outer=psi_out, nrad=nrd, npol=npl, rad_peaking=[0.0], pol_peaking=[0.0], parweight=0.0) boundary = numpy.array([rz_grid.rlim, rz_grid.zlim]) mesh = create_grid.create_grid(g.psi, g.r[:, 0], g.z[0, :], settings, critical=critical, boundary=boundary, iter=0, fpsi=None, fast='fast') #save mesh object for faster re-iterations of process_grid (optional) saveobject(mesh, 'mesh') #read mesh object for faster re-iterations of process_grid (optional) with open('mesh', 'rb') as input: mesh = pickle.load(input) process_grid(rz_grid, mesh) return
def run_evaluation(input_folder, output_folder, params, is_mat=False): """Run CPM with input as an image.""" suffix = ['.jpg', '.png'] files = [ os.path.join(dirpath, filename) for dirpath, dirnames, filenames in os.walk(input_folder) for filename in filenames if filename[-4:] in suffix ] files.sort() save_fig = params['save_fig'] if save_fig: fig = None try: sym, arg_params, aux_params = mx.model.load_checkpoint(params['model'], 0) except Exception as e: print(e) return data_names = ['data'] label_names = [] max_data_shape = [[('data', (1, 3, 256, 256))]] provide_data = [[('data', (1, 3, 256, 256))]] provide_label = [None] predictor = Predictor( sym, data_names, label_names, context=[params['context']], max_data_shapes=max_data_shape, provide_data=provide_data, provide_label=provide_label, arg_params=arg_params, aux_params=aux_params) full_landmarks = dict() for nframe, imfile in enumerate(files): nframe += 1 if not is_running: break tic = time.time() print("Frame: %5d/%5d" % (nframe, len(files)), end='') output_file = imfile.replace(input_folder, output_folder) out_dir = os.path.dirname(output_file) if not isdir(out_dir): makedirs(out_dir) if isfile(output_file + ".zip"): try: with gzip.open(output_file + ".zip", 'rb') as f: buffer = f.read() map_data = pickle.loads(buffer) full_landmarks[imfile] = map_data print(". Already done.") continue except Exception as e: pass image = cv.imread(imfile) try: if is_mat: pts_file = imfile[:-4] + '.mat' gt = sio.loadmat(pts_file)['pt3d_68'] gt_pts = gt.T[:,:2] else: pts_file = imfile[:-4] + '.t7' gt_pts = torchfile.load(pts_file) except Exception as e: print("\nException", e) print("Could not find groundtruth file of %s" % imfile) continue print(", load GT: %.3fs" % (time.time() - tic), end='') points, face, run_time = get_landmarks(cmodel=predictor, params=params, image=image, gt_pts=gt_pts) if points is None: continue print(", landmarks: %.3fs" % run_time, end='') full_landmarks[imfile] = points if save_fig: img2 = image[:, :, ::-1] if fig is None: # fig = plt.figure(figsize=(19.2, 10.8)) def handle_close(evt): global is_running is_running = False # plt.close(evt.canvas.figure) # sys.exit('Closed Figure!') fig = plt.figure(figsize=(19.2, 10.8)) fig.canvas.mpl_connect('close_event', handle_close) ax = fig.add_subplot(111) ax.axis('off') else: ax.clear() ax.axis('off') ax.imshow(img2) put_2dface(ax=ax, face=None, points=points, color='b') put_2dface(ax=ax, face=None, points=gt_pts, color='r') plt.pause(.0001) plt.draw() output_file = imfile.replace(input_folder, join(output_folder,"figs")) fig.savefig(fname=output_file + ".pdf", transparent=True, bbox_inches='tight', pad_inches=0) zip_file = gzip.GzipFile(output_file + ".zip", 'wb') zip_file.write(pickle.dumps(points, pickle.HIGHEST_PROTOCOL)) zip_file.close() print(", Time: %.3fs" % (time.time() - tic)) zip_file = gzip.GzipFile(join(output_folder, "all_pred") + ".zip", 'wb') zip_file.write(pickle.dumps(full_landmarks, pickle.HIGHEST_PROTOCOL)) zip_file.close()
def fit(self, data, command="", settings={}): """ This generates xdata, ydata, and eydata from the three scripts (or auto-sets the error and updates it depending on the fit), fits the data, stores the results (and scripts) in the data file's header and saves the data in a new file. data instance of a data class command initial interactive fit command interactive set to False to automatically fit without confirmation """ iterable_settings = ["min", "max", "xb1", "xb2", "auto_error", "subtract", "smooth", "coarsen", "show_guess", "show_error", "show_background", "plot_all", "xscript", "yscript", "eyscript"] # dictionary of settings like "min" and "skip" default_settings = {"min" : None, "max" : None, "xb1" : 0, "xb2" : -1, "auto_error" : False, "subtract" : False, "smooth" : 0, "coarsen" : 0, "show_guess" : False, "show_error" : True, "show_background" : True, "plot_all" : False, "eyscript" : None, "output_path" : None, "output_columns" : None, "skip" : True, "guess" : None, "save_file" : True, "file_tag" : 'fit_', "figure" : 0, "autofit" : False, "fullsave" : False, } if not settings.has_key('eyscript'): default_settings["auto_error"] = True # fill in the non-supplied settings with defaults for k in default_settings.keys(): if not k in settings.keys(): settings[k] = default_settings[k] # determine the number of parallel fits from the yscript if _s.fun.is_iterable(settings['yscript']): number_of_fits = len(settings['yscript']) else: number_of_fits = 1 # In general we're going to have a list of datas and scripts etc, so make # sure we're in a position to do this. if not _s.fun.is_iterable(data): data = [data] # fill out the arrays so they match the number of fits while len(data) < number_of_fits: data.append(data[-1]) # make sure the various settings are lists too for k in iterable_settings: # make them all iterable if not _s.fun.is_iterable(settings[k]): settings[k] = [settings[k]] # make sure they're all the right length while len(settings[k]) < number_of_fits: settings[k].append(settings[k][-1]) # Initialize the fit_parameters (we haven't any yet!) fit_parameters = None fit_errors = None format_figures = True # set up the figures axes2s = [] axes1s = [] figs = [] for n in range(len(data)): figs.append(_pylab.figure(settings["figure"]+n)) figs[n].clear() axes2s.append(_pylab.subplot(211)) axes1s.append(_pylab.subplot(212, sharex=axes2s[n])) axes2s[n].set_position([0.15, 0.78, 0.70, 0.13]) axes1s[n].set_position([0.15, 0.08, 0.70, 0.64]) # Now keep trying to fit until the user says its okay or gives up. hold_plot=False while True: # Plot everything. if hold_plot: hold_plot=False else: if settings["skip"]: print "Plotting but not optimizing... (<enter> to fit)" else: print "Beginning fit routine..." # assemble all the data xdatas = [] ydatas = [] eydatas = [] xs = [] ys = [] eys = [] for n in range(len(data)): # get the data based on the scripts xdatas.append(data[n](settings["xscript"][n])) ydatas.append(data[n](settings["yscript"][n])) if settings["eyscript"][n] == None: eydatas.append(xdatas[n]*0.0 + (max(ydatas[n])-min(ydatas[n]))/20.0) else: eydatas.append(data[n](settings["eyscript"][n])) # now sort the data in case it's jaggy! matrix_to_sort = _n.array([xdatas[n], ydatas[n], eydatas[n]]) sorted_matrix = _fun.sort_matrix(matrix_to_sort, 0) xdatas[n] = sorted_matrix[0] ydatas[n] = sorted_matrix[1] eydatas[n] = sorted_matrix[2] # now trim all the data based on xmin and xmax xmin = settings["min"][n] xmax = settings["max"][n] if xmin==None: xmin = min(xdatas[n])-1 if xmax==None: xmax = max(xdatas[n])+1 [x, y, ey] = _fun.trim_data(xdatas[n], ydatas[n], eydatas[n], [xmin, xmax]) # smooth and coarsen [x,y,ey] = _fun.smooth_data( x,y,ey,settings["smooth"][n]) [x,y,ey] = _fun.coarsen_data(x,y,ey,settings["coarsen"][n]) # append to the temporary trimmed data sets. xs.append(x) ys.append(y) eys.append(ey) # now do the first optimization. Start by guessing parameters from # the data's shape. This writes self.p0 if settings["guess"]==None: self.guess(xs, ys, settings["xb1"], settings["xb2"]) else: self.write_to_p0(settings['guess']) print "\n FUNCTION:" for s in self.function_string: print " "+s print "\n GUESS:" for n in range(len(self.pnames)): print " "+self.pnames[n]+" = "+str(self.p0[n]) print # now do the first optimization if not settings["skip"]: # actually do the least-squares optimization fit_output = self.optimize(xs, ys, eys, self.p0) # optimize puts out a float if there's only one parameter. Annoying. if not _s.fun.is_iterable(fit_output[0]): fit_parameters = _n.array([fit_output[0]]) else: fit_parameters = fit_output[0] # If we're doing auto error, now we should scale the error so that # the reduced xi^2 is 1 if settings["auto_error"]: # guess the correction to the y-error we're fitting (sets the reduced chi^2 to 1) rms = _n.sqrt(self.residuals_variance(fit_parameters,xs,ys,eys)) print " initial reduced chi^2 =", list(rms**2) print " scaling errors by", list(rms), "and re-optimizing..." for n in range(len(eys)): eys[n] = rms[n] * eys[n] eydatas[n] = rms[n] * eydatas[n] # optimize with new improved errors, using the old fit to start fit_output = self.optimize(xs,ys,eys,p0=fit_parameters) # optimize puts out a float if there's only one parameter. Annoying. if not _s.fun.is_iterable(fit_output[0]): fit_parameters = _n.array([fit_output[0]]) else: fit_parameters = fit_output[0] # Now that the fitting is done, show the output # grab all the information from fit_output fit_covariance = fit_output[1] fit_reduced_chi_squared = list(self.residuals_variance(fit_parameters,xs,ys,eys)) if not fit_covariance == None: # get the error vector and correlation matrix from (scaled) covariance [fit_errors, fit_correlation] = _fun.decompose_covariance(fit_covariance) else: print " WARNING: No covariance matrix popped out of model.optimize()" fit_errors = fit_parameters fit_correlation = None print " reduced chi^2 is now", fit_reduced_chi_squared # print the parameters print "\n FUNCTION:" for s in self.function_string: print " "+s print "\n FIT:" for n in range(0,len(self.pnames)): print " "+self.pnames[n]+" =", fit_parameters[n], "+/-", fit_errors[n] print # get the data to plot and plot it. for n in range(len(axes1s)): if settings["plot_all"][n]: x_plot = xdatas[n] y_plot = ydatas[n] ey_plot = eydatas[n] [x_plot, y_plot, ey_plot] = _fun.smooth_data (x_plot, y_plot, ey_plot, settings["smooth"][n]) [x_plot, y_plot, ey_plot] = _fun.coarsen_data(x_plot, y_plot, ey_plot, settings["coarsen"][n]) else: # this data is already smoothed and coarsened before the fit. x_plot = xs[n] y_plot = ys[n] ey_plot = eys[n] # now plot everything # set up the axes axes1 = axes1s[n] axes2 = axes2s[n] _pylab.hold(True) axes1.clear() axes2.clear() # by default, the thing to subtract is 0. thing_to_subtract = y_plot*0.0 # get the fit data if we're supposed to so we can know the thing to subtract if not fit_parameters==None: # get the fit and fit background for plotting (so we can subtract it!) y_fit = self.evaluate (fit_parameters, x_plot, n) y_fit_background = self.background(fit_parameters, x_plot, n) if settings["subtract"][n]: thing_to_subtract = y_fit_background # plot the guess if settings["show_guess"][n]: y_guess = self.evaluate(self.p0, x_plot, n) axes1.plot(x_plot, y_guess-thing_to_subtract, color='gray', label='guess') if settings["show_background"]: y_guess_background = self.background(self.p0, x_plot, n) axes1.plot(x_plot, y_guess_background-thing_to_subtract, color='gray', linestyle='--', label='guess background') # Plot the data if settings["show_error"][n]: axes1.errorbar(x_plot, y_plot-thing_to_subtract, ey_plot, linestyle='', marker='D', mfc='blue', mec='w', ecolor='b', label='data') else: axes1.plot( x_plot, y_plot-thing_to_subtract, linestyle='', marker='D', mfc='blue', mec='w', label='data') # plot the fit if not fit_parameters == None and not settings["skip"]: axes1.plot( x_plot, y_fit-thing_to_subtract, color='red', label='fit') if settings["show_background"][n]: axes1.plot(x_plot, y_fit_background-thing_to_subtract, color='red', linestyle='--', label='fit background') # plot the residuals in the upper graph axes2.errorbar(x_plot, (y_plot-y_fit)/ey_plot, ey_plot*0.0+1.0, linestyle='', marker='o', mfc='blue', mec='w', ecolor='b') axes2.plot (x_plot, 0*x_plot, linestyle='-', color='k') # come up with a title title1 = data[n].path # second line of the title is the model title2 = "eyscript="+str(settings["eyscript"][n])+", model: " + str(self.function_string[n]) # third line is the fit parameters title3 = "" if not settings["skip"] and not fit_parameters==None: t = [] for i in range(0,len(self.pnames)): t.append(self.pnames[i]+"=%.4g+/-%.2g" % (fit_parameters[i], fit_errors[i])) title3 = title3+_fun.join(t[0:4],", ") if len(t)>3: title3 = title3+'\n'+_fun.join(t[4:],", ") else: title3 = title3+"(no fit performed)" # Start by formatting the previous plot axes2.set_title(title1+"\n"+title2+"\nFit: "+title3) axes1.set_xlabel(settings["xscript"][n]) axes1.set_ylabel(settings["yscript"][n]) # set the position of the legend axes1.legend(loc=[1.01,0], borderpad=0.02, prop=_FontProperties(size=7)) # set the label spacing in the legend axes1.get_legend().labelsep = 0.01 # set up the title label axes2.title.set_horizontalalignment('right') axes2.title.set_size(8) axes2.title.set_position([1.0,1.010]) fig = _pylab.figure(axes1.get_figure().number) if format_figures: _st.format_figure(fig) _st.auto_zoom(axes1) _pylab.draw() _wx.Yield()
def pso_fit_sim(varname, xd, yd, sim, parameters, n_particles=30, n_iterations=-1, progress_interval=100, plot=False): extra = { 'x': xd, 'y': yd, 'model': sim, 'varname': varname, 'parameters': parameters } old_plots = [] for c in sim.components: old_plots.append(c.plot) c.plot = False s = swarm(parameters, mse_from_sim, n_particles, extra) iterations = 0 stop = False try: while not stop: s.update() if iterations % progress_interval == 0: print("iterations", iterations, " min fitness: ", s.best_val, " with vals ", s.best) if plot: pylab.clf() pylab.plot(xd, yd, 'o-') params = s.best y = model(params, xd, sim, varname, parameters) pylab.plot(xd, y, '-', linewidth=2) pylab.draw() #pylab.show() iterations += 1 if n_iterations > 0 and iterations >= n_iterations: stop = True except KeyboardInterrupt: pass for c, op in zip(sim.components, old_plots): c.plot = op params = s.best params_dict = {} for i, p in enumerate(parameters): params_dict[p[0]] = params[i] return params_dict
def clean(dir, var): ''' Remove masked values in the two arrays, where if a direction data is masked, the var data will also be removed in the cleaning process (and vice-versa) ''' dirmask = dir.mask == False varmask = var.mask == False ind = dirmask * varmask return dir[ind], var[ind] if __name__ == '__main__': from pylab import figure, show, setp, random, grid, draw vv = random(500) * 6 dv = random(500) * 360 fig = figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='w') rect = [0.1, 0.1, 0.8, 0.8] ax = WindroseAxes(fig, rect, axisbg='w') fig.add_axes(ax) # ax.contourf(dv, vv, bins=np.arange(0,8,1), cmap=cm.hot) # ax.contour(dv, vv, bins=np.arange(0,8,1), colors='k') # ax.bar(dv, vv, normed=True, opening=0.8, edgecolor='white') ax.box(dv, vv, normed=True) l = ax.legend(axespad=-0.10) setp(l.get_texts(), fontsize=8) draw() #print ax._info show()
for i in xrange(X.size): griddata.addSample([X.ravel()[i], Y.ravel()[i]], [0]) griddata._convertToOneOfMany( ) # this is still needed to make the fnn feel comfy for i in range(20): trainer.trainEpochs(1) trnresult = percentError(trainer.testOnClassData(), trndata['class']) tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class']) print "epoch: %4d" % trainer.totalepochs, \ " train error: %5.2f%%" % trnresult, \ " test error: %5.2f%%" % tstresult out = fnn.activateOnDataset(griddata) out = out.argmax(axis=1) # the highest output activation gives the class out = out.reshape(X.shape) figure(1) ioff() # interactive graphics off clf() # clear the plot hold(True) # overplot on for c in [0, 1, 2]: here, _ = where(tstdata['class'] == c) plot(tstdata['input'][here, 0], tstdata['input'][here, 1], 'o') if out.max() != out.min(): # safety check against flat field contourf(X, Y, out) # plot the contour ion() # interactive graphics on draw() # update the plot ioff() show()
def plotgauge(gaugeno, plotdata, verbose=False): #========================================== """ Plot all requested plots for a single gauge from the computation. The plots are requested by setting attributes of plotdata to ClawPlotFigure objects with plot_type="each_gauge". """ if verbose: gaugesoln = plotdata.getgauge(gaugeno) print ' Plotting gauge %s at x = %s, y = %s ... ' \ % (gaugeno, gaugesoln.location[0], gaugesoln.location[1]) if plotdata.mode() == 'iplotclaw': pylab.ion() try: plotfigure_dict = plotdata.plotfigure_dict except: print '*** Error in plotgauge: plotdata missing plotfigure_dict' print '*** This should not happen' return None if len(plotfigure_dict) == 0: print '*** Warning in plotgauge: plotdata has empty plotfigure_dict' print '*** Apparently no figures to plot' # initialize current_data containing data that will be passed # to beforegauge, aftergauge, afteraxes commands current_data = clawdata.ClawData() current_data.add_attribute("user", {}) # for user specified attributes # to avoid potential conflicts current_data.add_attribute('plotdata', plotdata) current_data.add_attribute('gaugeno', gaugeno) # call beforegauge if present, which might define additional # attributes in current_data or otherwise set up plotting for this # gauge. beforegauge = getattr(plotdata, 'beforegauge', None) if beforegauge: if isinstance(beforegauge, str): # a string to be executed exec(beforegauge) else: # assume it's a function try: output = beforegauge(current_data) if output: current_data = output except: print '*** Error in beforegauge ***' raise # iterate over each single plot that makes up this gauge: # ------------------------------------------------------- if plotdata._mode == 'iplotclaw': gaugesoln = plotdata.getgauge(gaugeno) print ' Plotting Gauge %s at x = %s, y = %s ... ' \ % (gaugeno, gaugesoln.location[0], gaugesoln.location[1]) requested_fignos = plotdata.iplotclaw_fignos else: requested_fignos = plotdata.print_fignos plotted_fignos = [] plotdata = set_show(plotdata) # set _show attributes for which figures # and axes should be shown. # loop over figures to appear for this gauge: # ------------------------------------------- for figname in plotdata._fignames: plotfigure = plotdata.plotfigure_dict[figname] if (not plotfigure._show) or (plotfigure.type != 'each_gauge'): continue # skip to next figure figno = plotfigure.figno if requested_fignos != 'all': if figno not in requested_fignos: continue # skip to next figure plotted_fignos.append(figno) if not plotfigure.kwargs.has_key('facecolor'): # use Clawpack's default bg color (tan) plotfigure.kwargs['facecolor'] = '#ffeebb' # create figure and set handle: plotfigure._handle = pylab.figure(num=figno, **plotfigure.kwargs) pylab.ioff() if plotfigure.clf_each_gauge: pylab.clf() try: plotaxes_dict = plotfigure.plotaxes_dict except: print '*** Error in plotgauge: plotdata missing plotaxes_dict' print '*** This should not happen' return None if (len(plotaxes_dict) == 0) or (len(plotfigure._axesnames) == 0): print '*** Warning in plotgauge: plotdata has empty plotaxes_dict' print '*** Apparently no axes to plot in figno ', figno # loop over axes to appear on this figure: # ---------------------------------------- for axesname in plotfigure._axesnames: plotaxes = plotaxes_dict[axesname] if not plotaxes._show: continue # skip this axes if no items show # create the axes: axescmd = getattr(plotaxes, 'axescmd', 'subplot(1,1,1)') axescmd = 'plotaxes._handle = pylab.%s' % axescmd exec(axescmd) pylab.hold(True) # loop over items: # ---------------- for itemname in plotaxes._itemnames: plotitem = plotaxes.plotitem_dict[itemname] outdir = plotitem.outdir if outdir is None: outdir = plotdata.outdir gaugesoln = plotdata.getgauge(gaugeno, outdir) current_data.add_attribute('gaugesoln', gaugesoln) current_data.add_attribute('q', gaugesoln.q) current_data.add_attribute('t', gaugesoln.t) if plotitem._show: try: output = plotgauge1(gaugesoln, plotitem, current_data) if output: current_data = output if verbose: print ' Plotted plotitem ', itemname except: print '*** Error in plotgauge: problem calling plotgauge1' traceback.print_exc() return None # end of loop over plotitems for itemname in plotaxes._itemnames: plotitem = plotaxes.plotitem_dict[itemname] pylab.title("%s at gauge %s" % (plotaxes.title, gaugeno)) # call an afteraxes function if present: afteraxes = getattr(plotaxes, 'afteraxes', None) if afteraxes: if isinstance(afteraxes, str): # a string to be executed exec(afteraxes) else: # assume it's a function try: current_data.add_attribute("plotaxes", plotaxes) current_data.add_attribute("plotfigure", plotaxes._plotfigure) output = afteraxes(current_data) if output: current_data = output except: print '*** Error in afteraxes ***' raise if plotaxes.scaled: pylab.axis('scaled') # set axes limits: if (plotaxes.xlimits is not None) & (type(plotaxes.xlimits) is not str): try: pylab.xlim(plotaxes.xlimits[0], plotaxes.xlimits[1]) except: pass # let axis be set automatically if (plotaxes.ylimits is not None) & (type(plotaxes.ylimits) is not str): try: pylab.ylim(plotaxes.ylimits[0], plotaxes.ylimits[1]) except: pass # let axis be set automatically # end of loop over plotaxes # end of loop over plotfigures # call an aftergauge function if present: aftergauge = getattr(plotdata, 'aftergauge', None) if aftergauge: if isinstance(aftergauge, str): # a string to be executed exec(aftergauge) else: # assume it's a function try: output = aftergauge(current_data) if output: current_data = output except: print '*** Error in aftergauge ***' raise if plotdata.mode() == 'iplotclaw': pylab.ion() for figno in plotted_fignos: pylab.figure(figno) pylab.draw() if verbose: print ' Done with plotgauge for gauge %i' % (gaugeno) # print the figure(s) to file(s) if requested: if (plotdata.mode() != 'iplotclaw') & plotdata.printfigs: # iterate over all figures that are to be printed: for figno in plotted_fignos: printfig(gaugeno=gaugeno, figno=figno, \ format=plotdata.print_format, plotdir=plotdata.plotdir,\ verbose=verbose) return current_data
def PlotEdge(self, sconfig, econfig, color='k--', lwidth=0.2): """Plot edge between two points on map.""" pl.plot([sconfig[0], econfig[0]], [sconfig[1], econfig[1]], color, linewidth=lwidth) pl.draw()
def colorfunc(label): l.set_color(label) pylab.draw()
def offset(workingdir, vis='', plotfile='', imfitlog=False, spw='', verbose=False): """ Takes a pipeline working directory and find all images of the checksource and produces a plot showing the relative directions of the first two science targets, the phase calibrator, and the checksource, and a vector showing the offset of the checksource from its catalog position (computed using the results of the CASA task imfit), and a text label showing the RAO and DECO offsets. workingdir: path to pipeline working directory vis: alternate location for a measurement set to consult (ignores *_target.ms) Looks first for *chk*iter2.image; if not found, then *chk*iter1.image plotfile: default = img+'_offset.png' imfitlog: if True, then request imfit to generate log files (*.imfit) spw: int or comma-delimited string, if specified, limit to this or these spws verbose: print more messages explaining what images it is operating on """ mymsmd = au.createCasaTool(msmdtool) if verbose: print("workingdir: ", workingdir) imglist = sorted(glob.glob(os.path.join(workingdir, '*_chk.spw*image'))) if len(imglist) == 0: print("No check source images found in this directory.") return # If iter2.image is found, then drop the iter1 version from the list for i in imglist: if i.find('iter2') > 0: imglist.remove(i.replace('iter2', 'iter1')) if verbose: print("Processing %d images:" % (len(imglist))) for i in imglist: print(i) if vis == '': searchpath = os.path.join(workingdir, '*.ms') if verbose: print("searchpath: ", searchpath) allvislist = sorted(glob.glob(searchpath)) if verbose: print("all vis found: ", allvislist) vislist = [] for vis in allvislist: if vis.find('_target') < 0: vislist.append(vis) else: vislist = [vis] raos = [] decos = [] totals = [] sourcenames = [] spws = au.parseSpw(vis, spw) scienceSpws = au.getScienceSpws(vis, returnString=False) spws = np.intersect1d(scienceSpws, spws) if verbose: print("using spws: ", spws) newimglist = [] for img in imglist: # there will be an image for each spw if img.find('spw') > 0 and spw != '': myspw = int(img.split('spw')[1].split('.')[0]) if myspw in spws: sourcenames.append(au.imageSource(img)) newimglist.append(img) if verbose: print("Using %s" % (img)) elif verbose: print("Skipping %s" % (img)) else: sourcenames.append(au.imageSource(img)) newimglist.append(img) sourcenames = np.unique(sourcenames) pngs = [] print("vislist = ", vislist) imglist = newimglist for sourcename in sourcenames: for ispw, img in enumerate( imglist): # there will be an image for each spw if 'spw' not in img: print("No spw in the image name: ", img) continue spw = int(img.split('spw')[1].split('.')[0]) # find the first vis that observed this target as check source checkid = -1 for vis in vislist: # print "Checking ", vis mymsmd.open(vis) if spw >= mymsmd.nspw(): print("Guessing that spw %d is spw %d in the split ms." % (spw, ispw)) spw = ispw if 'OBSERVE_CHECK_SOURCE#ON_SOURCE' in mymsmd.intents(): checksources = mymsmd.fieldsforintent( 'OBSERVE_CHECK_SOURCE*', True) else: checksources = mymsmd.fieldsforintent( 'CALIBRATE_DELAY*', True) if sourcename in checksources: check = checksources[0] checkid = mymsmd.fieldsforname(sourcename)[0] checkpos = mymsmd.phasecenter(checkid) # Phase calibrator phase = mymsmd.fieldsforintent('CALIBRATE_PHASE*', True)[0] phaseid = mymsmd.fieldsforintent('CALIBRATE_PHASE*', False)[0] phasepos = mymsmd.phasecenter(phaseid) if ('OBSERVE_TARGET#ON_SOURCE' in mymsmd.intents()): nScienceFields = len( mymsmd.fieldsforintent('OBSERVE_TARGET*', False)) science = mymsmd.fieldsforintent( 'OBSERVE_TARGET*', True)[0] scienceid = mymsmd.fieldsforintent( 'OBSERVE_TARGET*', False)[0] sciencepos = mymsmd.phasecenter(scienceid) if nScienceFields > 1: science2 = mymsmd.fieldsforintent( 'OBSERVE_TARGET*', True)[1] science2id = mymsmd.fieldsforintent( 'OBSERVE_TARGET*', False)[1] science2pos = mymsmd.phasecenter(science2id) else: nScienceFields = 0 rxBand = mymsmd.namesforspws(spw)[0].split('#')[1].split( '_')[-1].lstrip('0') # string break else: mymsmd.close() if checkid < 0: print( "Could not find an ms that observed this check source: %s. Try including the vis parameter." % (sourcename)) continue info = au.getFitsBeam(img) imsize = info[5] # size in RA direction region = 'circle[[%dpix , %dpix], 15pix ]' % (int( imsize / 2), int(imsize / 2)) freq = mymsmd.meanfreq(spw, unit='GHz') if imfitlog: logfile = img + '.imfit' else: logfile = '' imagefit = imfit(imagename=img, region=region, logfile=logfile) fitresults = au.imfitparse(imagefit, deconvolved=True) synthMajor, synthMinor = info[0:2] synthBeam = (info[0] * info[1])**0.5 # Compare the Positions checkpos_obs = au.direction2radec(checkpos) if fitresults is not None: checkpos_fit = ','.join(fitresults.split()[:2]) print("spw %d: checksource fitted position: " % (spw), checkpos_fit) result = au.angularSeparationOfStrings(checkpos_fit, checkpos_obs, True, verbose=False) checkpos_diff, deltaLong, deltaLat, deltaLongCosDec, pa = result total = checkpos_diff * 3600. rao = deltaLongCosDec * 3600. deco = deltaLat * 3600. print( "spw %d: %s offset=%.4f arcsec, RAO=%+.4f, DECO=%+.4f, PA=%.1fdeg" % (spw, sourcename, total, rao, deco, pa)) totals.append(total) raos.append(rao) decos.append(deco) mymsmd.close() if nScienceFields > 1: scienceDeg = np.degrees( au.angularSeparationOfDirections(science2pos, sciencepos, True)) phaseDeg = np.degrees( au.angularSeparationOfDirections(phasepos, sciencepos, True)) checkDeg = np.degrees( au.angularSeparationOfDirections(checkpos, sciencepos, True)) if len(raos) == 1: pl.clf() desc = pl.subplot(111) if nScienceFields > 1: pl.plot([0, scienceDeg[3], phaseDeg[3], checkDeg[3]], [0, scienceDeg[2], phaseDeg[2], checkDeg[2]], 'b+', ms=10, mew=2) else: pl.plot([0, phaseDeg[3], checkDeg[3]], [0, phaseDeg[2], checkDeg[2]], 'b+', ms=10, mew=2) pl.hold(True) pl.axis('equal') yrange = np.diff(pl.ylim())[0] # reverse RA axis x0, x1 = pl.xlim() xoffset = 0.15 * (x1 - x0) # Keep a fixed scale among the spws/images xscale = 0.5 * xoffset / np.max(np.abs([rao, deco])) # draw the arrow for each spw's image pl.arrow(checkDeg[3], checkDeg[2], rao * xscale, deco * xscale, lw=1, shape='full', head_width=0.15 * xoffset, head_length=0.2 * xoffset, fc='b', ec='b') if len(raos) == 1: pl.xlim([x1 + xoffset, x0 - xoffset]) yoffset = yrange * 0.025 pl.text(0, 0 + yoffset, 'science', ha='center', va='bottom') if nScienceFields > 1: pl.text(scienceDeg[3], scienceDeg[2] + yoffset, 'science (%.1fdeg)' % scienceDeg[0], ha='center', va='bottom') pl.text(scienceDeg[3], scienceDeg[2] - yoffset, science2, ha='center', va='top') pl.text(phaseDeg[3], phaseDeg[2] + yoffset, 'phase (%.1fdeg)' % phaseDeg[0], ha='center', va='bottom') pl.text(checkDeg[3], checkDeg[2] + yoffset, 'check (%.1fdeg)' % checkDeg[0], ha='center', va='bottom') pl.text(0, 0 - yoffset, science, ha='center', va='top') pl.text(phaseDeg[3], phaseDeg[2] - yoffset, phase, ha='center', va='top') pl.text(checkDeg[3], checkDeg[2] - yoffset, check, ha='center', va='top') pl.xlabel('RA offset (deg)') pl.ylabel('Dec offset (deg)') projCode = au.projectCodeFromDataset(vis) if type(projCode) == str: if verbose: print("Did not find project code") projCode = '' else: projCode = projCode[0] + ', Band %s, ' % (rxBand) pl.title(projCode + os.path.basename(img).split('.spw')[0] + ', spws=%s' % spws, size=12) pl.ylim( [pl.ylim()[0] - yoffset * 8, pl.ylim()[1] + yoffset * 8]) minorLocator = MultipleLocator(0.5) # degrees desc.xaxis.set_minor_locator(minorLocator) desc.yaxis.set_minor_locator(minorLocator) # end 'for' loop over spws/images if len(raos) < 1: return pl.ylim([pl.ylim()[0] - yoffset * 7, pl.ylim()[1] + yoffset * 15]) rao = np.median(raos) raostd = np.std(raos) deco = np.median(decos) decostd = np.std(decos) total = np.median(totals) totalstd = np.std(totals) raoBeams = rao / synthBeam raostdBeams = raostd / synthBeam decoBeams = deco / synthBeam decostdBeams = decostd / synthBeam # draw the median arrow in thick black line pl.arrow(checkDeg[3], checkDeg[2], rao * xscale, deco * xscale, lw=2, shape='full', head_width=0.12 * xoffset, head_length=0.18 * xoffset, ec='k', fc='k') print( "median +- std: offset=%.4f+-%.4f, RAO=%.4f+-%.4f, DECO=%.4f+-%.4f" % (total, totalstd, rao, raostd, deco, decostd)) # pl.text(checkDeg[3], checkDeg[2]-0.6*xoffset, '$\Delta\\alpha$: %+.4f"+-%.4f"' % (rao,raostd), ha='center') # pl.text(checkDeg[3], checkDeg[2]-0.85*xoffset, '$\Delta\\delta$: %+.4f"+-%.4f"' % (deco,decostd), ha='center') pl.text(0.05, 0.95, '$\Delta\\alpha$: %+.4f"+-%.4f" = %+.2f+-%.2f beams' % (rao, raostd, raoBeams, raostdBeams), ha='left', transform=desc.transAxes) pl.text(0.05, 0.91, '$\Delta\\delta$: %+.4f"+-%.4f" = %+.2f+-%.2f beams' % (deco, decostd, decoBeams, decostdBeams), ha='left', transform=desc.transAxes) if plotfile == '': png = img + '_offset.png' else: png = plotfile pl.savefig(png, bbox_inches='tight') pl.draw() pngs.append(png) print("Wrote ", png)
def callback(*args): H.set_array(pylab.random((10,10))) pylab.draw() wx.WakeUpIdle() # ensure that the idle event keeps firing
def invoke(self, args, from_tty): s = str(args).split(" ") if not s[0]: gdb.write("What do you want to plot?\n") else: r = re.compile('(?P<base>[A-Za-z_\*]+)' '(?P<targs><[0-9A-Za-z_ \*,]+>)?' '(?: \[(?P<numel>[0-9]+)\])?') arrs = [] for word in s: #gdb.write("Word: '{}'\n".format(word)) val = gdb.parse_and_eval(word) match = r.match(str(val.type)).groupdict() #gdb.write("Lazy: '{}'\n".format(val.is_lazy)) #gdb.write("Type: '{}'\n".format(val.type)) #gdb.write("Type: '{}'\n".format(match['base'])) #gdb.write("Numel: '{}'\n".format(match['numel'])) if not match['numel']: gdb.write("This isn't an array\n") continue numel = int(match['numel']) base = match['base'] if base == 'float': arr = np.empty(numel, dtype=np.float32) for i in range(numel): arr[i] = val[i] elif base in ['int32', 'int32_t', 'int']: arr = np.empty(numel, dtype=np.int32) for i in range(numel): arr[i] = val[i] elif base in ['uint32_t', 'uint32']: arr = np.empty(numel, dtype=np.uint32) for i in range(numel): arr[i] = val[i] elif base in ['sFractional', 'uFractional']: parts = re.match( '<(?P<i>[0-9\.]+)[A-Za-z]+, ?' '(?P<f>[0-9a-z\.]+)[A-Za-z]+>', match['targs']).groupdict() fbits = int(parts['f']) arr = np.empty(numel, dtype=np.float32) for i in range(min(50, numel)): gdb.write("VAL: {}\n".format(val[i]['i'])) for i in range(numel): arr[i] = float(int(val[i]['i'])) / (2**fbits) else: gdb.write("Yeah don't know what to do with " "type '{}'\n".format(base)) continue arrs.append(( word, arr, )) def plotlauncher(data): try: title = ', '.join(map(lambda x: x[0], data)) fig = pylab.gcf() fig.canvas.set_window_title(title) for a in data: pylab.plot(a[1]) pylab.title(title) pylab.show() except KeyboardInterrupt: pass if arrs: pylab.figure() #multiprocessing.Process(target=plotlauncher, # args=(arrs,)).start() title = ', '.join(map(lambda x: x[0], arrs)) fig = pylab.gcf() fig.canvas.set_window_title(title) for a in arrs: pylab.plot(a[1]) pylab.draw()
def ShowPlan(self, plan, color='r'): for i in range(len(plan) - 1): self.PlotEdgePlan(plan[i], plan[i + 1], color) pl.draw()
def update(val): amp = samp.val freq = sfreq.val l.set_ydata(amp*scipy.sin(2*scipy.pi*freq*t)) pylab.draw()
def plot_polar(theta, r, x_label=None, y_label=None, title=None, show_legend=True, axis_equal=False, ax=None, *args, **kwargs): ''' plots polar data on a polar plot and optionally label axes. Parameters ------------ theta : array-like data to plot r : array-like x_label : string x-axis label y_label : string y-axis label title : string plot title show_legend : Boolean controls the drawing of the legend ax : :class:`matplotlib.axes.AxesSubplot` object axes to draw on *args,**kwargs : passed to pylab.plot See Also ---------- plot_rectangular : plots rectangular data plot_complex_rectangular : plot complex data on complex plane plot_polar : plot polar data plot_complex_polar : plot complex data on polar plane plot_smith : plot complex data on smith chart ''' if ax is None: ax = plb.gca(polar=True) ax.plot(theta, r, *args, **kwargs) if x_label is not None: ax.set_xlabel(x_label) if y_label is not None: ax.set_ylabel(y_label) if title is not None: ax.set_title(title) if show_legend: # only show legend if they provide a label if 'label' in kwargs: ax.legend() if axis_equal: ax.axis('equal') if plb.isinteractive(): plb.draw()
# -*- coding: utf-8 -*- """ Created on Mon May 05 09:52:09 2014 @author: Jboeye """ import random as rnd import pylab as plt x = [] y = [] plt.ion() maxtime = 2000 title = 'random_output.txt' output = open(title, 'w') for t in range(maxtime): y_value = rnd.randint(0, 15) output.write(str(t) + '\t' + str(y_value) + '\n') x.append(t) y.append(y_value) if t % 5 == 0: plt.clf() graph = plt.plot(x, y, 'r')[0] plt.draw() plt.pause(0.0001) output.close()
def event_triggered_average(self, events, average=True, t_min=0, t_max=100, display=False, with_time=False, kwargs={}): """ Return the spike triggered averaged of an analog signal according to selected events, on a time window t_spikes - tmin, t_spikes + tmax Can return either the averaged waveform (average = True), or an array of all the waveforms triggered by all the spikes. Inputs: events - Can be a SpikeTrain object (and events will be the spikes) or just a list of times average - If True, return a single vector of the averaged waveform. If False, return an array of all the waveforms. t_min - Time (>0) to average the signal before an event, in ms (default 0) t_max - Time (>0) to average the signal after an event, in ms (default 100) display - if True, a new figure is created. Could also be a subplot. kwargs - dictionary contening extra parameters that will be sent to the plot function Examples: >> vm.event_triggered_average(spktrain, average=False, t_min = 50, t_max = 150) >> vm.event_triggered_average(spktrain, average=True) >> vm.event_triggered_average(range(0,1000,10), average=False, display=True) """ if isinstance(events, SpikeTrain): events = events.spike_times ylabel = "Spike Triggered Average" else: assert numpy.iterable( events ), "events should be a SpikeTrain object or an iterable object" ylabel = "Event Triggered Average" assert (t_min >= 0) and (t_max >= 0), "t_min and t_max should be greater than 0" assert len( events ) > 0, "events should not be empty and should contained at least one element" time_axis = numpy.linspace(-t_min, t_max, (t_min + t_max) / self.dt) N = len(time_axis) Nspikes = 0. subplot = get_display(display) if average: result = numpy.zeros(N, float) else: result = [] # recalculate everything into timesteps, is more stable against rounding errors # and subsequent cutouts with different sizes events = numpy.floor(numpy.array(events) / self.dt) t_min_l = numpy.floor(t_min / self.dt) t_max_l = numpy.floor(t_max / self.dt) t_start = numpy.floor(self.t_start / self.dt) t_stop = numpy.floor(self.t_stop / self.dt) for spike in events: if ((spike - t_min_l) >= t_start) and ((spike + t_max_l) < t_stop): spike = spike - t_start if average: result += self.signal[(spike - t_min_l):(spike + t_max_l)] else: result.append(self.signal[(spike - t_min_l):(spike + t_max_l)]) Nspikes += 1 if average: result = result / Nspikes else: result = numpy.array(result) if not subplot or not HAVE_PYLAB: if with_time: return result, time_axis else: return result else: xlabel = "Time (ms)" set_labels(subplot, xlabel, ylabel) if average: subplot.plot(time_axis, result, **kwargs) else: for idx in xrange(len(result)): subplot.plot(time_axis, result[idx, :], c='0.5', **kwargs) subplot.hold(1) result = numpy.sum(result, axis=0) / Nspikes subplot.plot(time_axis, result, c='k', **kwargs) xmin, xmax, ymin, ymax = subplot.axis() subplot.plot([0, 0], [ymin, ymax], c='r') set_axis_limits(subplot, -t_min, t_max, ymin, ymax) pylab.draw()
def plot_smith(z, smith_r=1, chart_type='z', x_label='Real', y_label='Imaginary', title='Complex Plane', show_legend=True, axis='equal', ax=None, force_chart=False, *args, **kwargs): ''' plot complex data on smith chart Parameters ------------ z : array-like, of complex data data to plot smith_r : number radius of smith chart chart_type : ['z','y'] Contour type for chart. * *'z'* : lines of constant impedance * *'y'* : lines of constant admittance x_label : string x-axis label y_label : string y-axis label title : string plot title show_legend : Boolean controls the drawing of the legend axis_equal: Boolean sets axis to be equal increments (calls axis('equal')) force_chart : Boolean forces the re-drawing of smith chart ax : :class:`matplotlib.axes.AxesSubplot` object axes to draw on *args,**kwargs : passed to pylab.plot See Also ---------- plot_rectangular : plots rectangular data plot_complex_rectangular : plot complex data on complex plane plot_polar : plot polar data plot_complex_polar : plot complex data on polar plane plot_smith : plot complex data on smith chart ''' if ax is None: ax = plb.gca() # test if smith chart is already drawn if not force_chart: if len(ax.patches) == 0: smith(ax=ax, smithR=smith_r, chart_type=chart_type) plot_complex_rectangular(z, x_label=x_label, y_label=y_label, title=title, show_legend=show_legend, axis=axis, ax=ax, *args, **kwargs) ax.axis(smith_r * npy.array([-1.1, 1.1, -1.1, 1.1])) if plb.isinteractive(): plb.draw()
def on_key_press(self, event): """Process a key press hotkeys:: ? - print help x - pick a new point and add it to the 2D point list i - intersect picked points in the 2D point list c - clear the 2D point list a - all intersected 3D points are printed to console h - save all intersected 3D points to 'points.h5' """ print 'received key', repr(event.key) if event.key == 'c': del self.cam_ids_and_points2d[:] for ax, line in self.to_del: ax.lines.remove(line) self.to_del = [] pylab.draw() elif event.key == 'i': if self.reconstructor is None: return X = self.reconstructor.find3d(self.cam_ids_and_points2d, return_X_coords=True, return_line_coords=False) self.points3d.append(X) print 'maximum liklihood intersection:' print repr(X) if 1: print 'reprojection errors:' for (cam_id, value_tuple) in self.cam_ids_and_points2d: newx, newy = self.reconstructor.find2d(cam_id, X, distorted=True) #origx,origy=self.reconstructor.undistort(cam_id, value_tuple[:2] ) origx, origy = value_tuple[:2] reproj_error = numpy.sqrt((newx - origx)**2 + (newy - origy)**2) print ' %s: %.1f' % (cam_id, reproj_error) print for cam_id, ax in self.subplot_by_cam_id.iteritems(): newx, newy = self.reconstructor.find2d(cam_id, X, distorted=True) xlim = ax.get_xlim() ylim = ax.get_ylim() ax.plot([newx], [newy], 'co', ms=5) ax.set_xlim(xlim) ax.set_ylim(ylim) pylab.draw() elif event.key == 'x': # new point -- project onto other images if not event.inaxes: print 'not in axes -- nothing to do' return ax = event.inaxes # the axes instance cam_id = self.find_cam_id(ax) print 'click on', cam_id xlim = ax.get_xlim() ylim = ax.get_ylim() line, = ax.plot([event.xdata], [event.ydata], 'bx') self.to_del.append((ax, line)) ax.set_xlim(xlim) ax.set_ylim(ylim) if self.reconstructor is None: print 'no reconstructor... cannot plot projection' return x, y = self.reconstructor.undistort(cam_id, [event.xdata, event.ydata]) self.cam_ids_and_points2d.append((cam_id, (x, y))) line3d = self.reconstructor.get_projected_line_from_2d( cam_id, [x, y]) cam_ids = self.subplot_by_cam_id.keys() cam_ids.sort() for other_cam_id in cam_ids: if other_cam_id == cam_id: continue xs, ys = self.reconstructor.get_distorted_line_segments( other_cam_id, line3d) # these are distorted ax = self.subplot_by_cam_id[other_cam_id] #print xs #print ys xlim = ax.get_xlim() ylim = ax.get_ylim() line, = ax.plot(xs, ys, 'b-') self.to_del.append((ax, line)) ax.set_xlim(xlim) ax.set_ylim(ylim) pylab.draw() elif event.key == '?': sys.stdout.write(""" %s current list of 2D points ------------------------- """ % self.on_key_press.__doc__) for cam_id, (x, y) in self.cam_ids_and_points2d: sys.stdout.write('%s: %s %s\n' % (cam_id, x, y)) sys.stdout.write('\n') elif event.key == 'a': def arrstr(arr): return '[ ' + ', '.join([repr(elem) for elem in arr]) + ' ]' sys.stdout.write('---------- 3D points so far\n') fd = sys.stdout fd.write('[\n ') fd.write(',\n '.join([arrstr(pt) for pt in self.points3d])) fd.write('\n]\n') sys.stdout.write('---------- \n') elif event.key == 'h': fname = os.path.abspath('points.h5') if os.path.exists(fname): raise RuntimeError('will not overwrite file %s' % fname) if self.reconstructor is None: raise RuntimeError('will not save .h5 file without 3D data') with contextlib.closing(tables.open_file(fname, mode='w')) as h5file: self.reconstructor.save_to_h5file(h5file) ct = h5file.create_table # shorthand root = h5file.root # shorthand h5data3d = ct(root, 'kalman_estimates', KalmanEstimatesVelOnly, "3d data") row = h5data3d.row for i, pt in enumerate(self.points3d): row['obj_id'] = 0 row['frame'] = i row['timestamp'] = i row['x'], row['y'], row['z'] = pt row['xvel'], row['yvel'], row['zvel'] = 0, 0, 0 row.append() sys.stdout.write('saved %d points to file %s\n' % (len(self.points3d), fname))
def mollzoom(map=None, fig=None, rot=None, coord=None, unit='', xsize=800, title='Mollweide view', nest=False, min=None, max=None, flip='astro', remove_dip=False, remove_mono=False, gal_cut=0, format='%g', cmap=None, norm=None, hold=False, margins=None, sub=None): """Interactive mollweide plot with zoomed gnomview. Parameters: ----------- map : float, array-like shape (Npix,) An array containing the map, supports masked maps, see the `ma` function. if None, use map with inf value (white map), useful for overplotting fig : a figure number. Default: create a new figure rot : scalar or sequence, optional Describe the rotation to apply. In the form (lon, lat, psi) (unit: degrees) : the point at longitude *lon* and latitude *lat* will be at the center. An additional rotation of angle *psi* around this direction is applied. coord : sequence of character, optional Either one of 'G', 'E' or 'C' to describe the coordinate system of the map, or a sequence of 2 of these to rotate the map from the first to the second coordinate system. unit : str, optional A text describing the unit of the data. Default: '' xsize : int, optional The size of the image. Default: 800 title : str, optional The title of the plot. Default: 'Mollweide view' nest : bool, optional If True, ordering scheme is NESTED. Default: False (RING) min : float, optional The minimum range value max : float, optional The maximum range value flip : {'astro', 'geo'}, optional Defines the convention of projection : 'astro' (default, east towards left, west towards right) or 'geo' (east towards roght, west towards left) remove_dip : bool, optional If :const:`True`, remove the dipole+monopole remove_mono : bool, optional If :const:`True`, remove the monopole gal_cut : float, scalar, optional Symmetric galactic cut for the dipole/monopole fit. Removes points in latitude range [-gal_cut, +gal_cut] format : str, optional The format of the scale label. Default: '%g' """ import pylab # create the figure (if interactive, it will open the window now) f = pylab.figure(fig, figsize=(10.5, 5.4)) extent = (0.02, 0.25, 0.56, 0.72) # Starting to draw : turn interactive off wasinteractive = pylab.isinteractive() pylab.ioff() try: if map is None: map = np.zeros(12) + np.inf map = pixelfunc.ma_to_array(map) ax = PA.HpxMollweideAxes(f, extent, coord=coord, rot=rot, format=format, flipconv=flip) f.add_axes(ax) if remove_dip: map = pixelfunc.remove_dipole(map, gal_cut=gal_cut, nest=nest, copy=True, verbose=True) elif remove_mono: map = pixelfunc.remove_monopole(map, gal_cut=gal_cut, nest=nest, copy=True, verbose=True) ax.projmap(map, nest=nest, xsize=xsize, coord=coord, vmin=min, vmax=max, cmap=cmap, norm=norm) im = ax.get_images()[0] b = im.norm.inverse(np.linspace(0, 1, im.cmap.N + 1)) v = np.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N) if matplotlib.__version__ >= '0.91.0': cb = f.colorbar(ax.get_images()[0], ax=ax, orientation='horizontal', shrink=0.5, aspect=25, ticks=PA.BoundaryLocator(), pad=0.05, fraction=0.1, boundaries=b, values=v) else: # for older matplotlib versions, no ax kwarg cb = f.colorbar(ax.get_images()[0], orientation='horizontal', shrink=0.5, aspect=25, ticks=PA.BoundaryLocator(), pad=0.05, fraction=0.1, boundaries=b, values=v) ax.set_title(title) ax.text(0.86, 0.05, ax.proj.coordsysstr, fontsize=14, fontweight='bold', transform=ax.transAxes) cb.ax.text(1.05, 0.30, unit, fontsize=14, fontweight='bold', transform=cb.ax.transAxes, ha='left', va='center') f.sca(ax) ## Gnomonic axes #extent = (0.02,0.25,0.56,0.72) g_xsize = 600 g_reso = 1. extent = (0.60, 0.04, 0.38, 0.94) g_ax = PA.HpxGnomonicAxes(f, extent, coord=coord, rot=rot, format=format, flipconv=flip) f.add_axes(g_ax) if remove_dip: map = pixelfunc.remove_dipole(map, gal_cut=gal_cut, nest=nest, copy=True) elif remove_mono: map = pixelfunc.remove_monopole(map, gal_cut=gal_cut, nest=nest, copy=True) g_ax.projmap(map, nest=nest, coord=coord, vmin=min, vmax=max, xsize=g_xsize, ysize=g_xsize, reso=g_reso, cmap=cmap, norm=norm) im = g_ax.get_images()[0] b = im.norm.inverse(np.linspace(0, 1, im.cmap.N + 1)) v = np.linspace(im.norm.vmin, im.norm.vmax, im.cmap.N) if matplotlib.__version__ >= '0.91.0': cb = f.colorbar(g_ax.get_images()[0], ax=g_ax, orientation='horizontal', shrink=0.5, aspect=25, ticks=PA.BoundaryLocator(), pad=0.08, fraction=0.1, boundaries=b, values=v) else: cb = f.colorbar(g_ax.get_images()[0], orientation='horizontal', shrink=0.5, aspect=25, ticks=PA.BoundaryLocator(), pad=0.08, fraction=0.1, boundaries=b, values=v) g_ax.set_title(title) g_ax.text(-0.07, 0.02, "%g '/pix, %dx%d pix" % (g_ax.proj.arrayinfo['reso'], g_ax.proj.arrayinfo['xsize'], g_ax.proj.arrayinfo['ysize']), fontsize=12, verticalalignment='bottom', transform=g_ax.transAxes, rotation=90) g_ax.text(-0.07, 0.8, g_ax.proj.coordsysstr, fontsize=14, fontweight='bold', rotation=90, transform=g_ax.transAxes) lon, lat = np.around(g_ax.proj.get_center(lonlat=True), g_ax._coordprec) g_ax.text(0.5, -0.03, 'on (%g,%g)' % (lon, lat), verticalalignment='center', horizontalalignment='center', transform=g_ax.transAxes) cb.ax.text(1.05, 0.30, unit, fontsize=14, fontweight='bold', transform=cb.ax.transAxes, ha='left', va='center') # Add graticule info axes grat_ax = pylab.axes([0.25, 0.02, 0.22, 0.25]) grat_ax.axis('off') # Add help text help_ax = pylab.axes([0.02, 0.02, 0.22, 0.25]) help_ax.axis('off') t = help_ax.transAxes help_ax.text(0.1, 0.8, 'r/t .... zoom out/in', transform=t, va='baseline') help_ax.text(0.1, 0.65, 'p/v .... print coord/val', transform=t, va='baseline') help_ax.text(0.1, 0.5, 'c ...... go to center', transform=t, va='baseline') help_ax.text(0.1, 0.35, 'f ...... next color scale', transform=t, va='baseline') help_ax.text(0.1, 0.2, 'k ...... save current scale', transform=t, va='baseline') help_ax.text(0.1, 0.05, 'g ...... toggle graticule', transform=t, va='baseline') f.sca(g_ax) # Set up the zoom capability zt = ZoomTool(map, fig=f.number, nest=nest, cmap=cmap, norm=norm, coord=coord) finally: pylab.draw() if wasinteractive: pylab.ion()
def run(params): """ Run simulation of the model for the game using the parameters specified in params map. If a results.pkl file is present in the current folder, then the script replays the simulation. Otherwise, the simulation is run anew. """ # Canvas for drawing. pylab.figure(1, figsize=(7, 4.5)) pylab.ion() pylab.draw() c_list = [] time_steps = [] # Replay of an experiment. if os.path.isfile("results.pkl"): fh = open("results.pkl", "r") pickle.load(fh) population = pickle.load(fh) fh.close() done = False for time_step in range(0, params["generations"] / \ params["report_freq"] + 1): print("Generation %d of %d" % (time_step * params["report_freq"], params["generations"])) time_steps.append(time_step * params["report_freq"]) c_count = 0 d_count = 0 for p in population: if time_step >= len(p.get_trait_list()): done = True else: if p.get_trait_list()[time_step] == Player.C: c_count += 1 else: d_count += 1 if done == True: break c_list.append(1.0 * c_count / params["population"]) plot(time_steps, c_list) # New experiment. else: start = 0 # Create a population. population = [Player(i) for i in range(0, params["population"])] # Create a network. net = network.build_network(population, params["network_topology"], params["network_params"]) # Seed the random number generator for the experiment. random.seed(params["seed"]) # Start out with a population with the specified initial fraction of # the population playing trait C and the remaining fraction # playing trait D. for p in population: if random.random() < params["init_cooperators"]: p.inherit_trait(Player.C) else: p.inherit_trait(Player.D) p.commit_inheritance() # Create a dynamics module based on network (complete or other) # type and the type of update rule selected. dynamics = dynamics_module(params["network_topology"], params["update_rule"])(net, params) # The dynamics. for time_step in range(0, params["generations"]): # Pre interaction. dynamics.pre_interaction() # Plot results at report_freq and stop simulation if # population has fixated to a trait. if time_step % params["report_freq"] == 0: print "Generation %d of %d" % (time_step, params["generations"]) time_steps.append(time_step) c_count = 0 d_count = 0 for p in population: if p.get_trait() == Player.C: c_count += 1 else: d_count += 1 c_list.append(1.0 * c_count / params["population"]) plot_c_d_curves(time_steps, c_list) if c_count == 0 or d_count == 0: break # Interact. for count in range(0, params["population"]): dynamics.interact() # Post interaction. dynamics.post_interaction() # Update. for count in range(0, params["population"]): dynamics.update() # Post update; commit trait inheritance. dynamics.post_update() # Keep the final plot window around until the user shuts it. pylab.show() pylab.close(1)
def test(): import DDFacet.ToolsDir.Gaussian _,_,PSF=DDFacet.ToolsDir.Gaussian.Gaussian(10,311,1.) #PSF.fill(1.) #import scipy.signal #PP=scipy.signal.fftconvolve(PSF,PSF, mode='same') #print Fact import pylab pylab.clf() pylab.imshow(PSF,interpolation="nearest") pylab.colorbar() pylab.draw() pylab.show(False) pylab.pause(0.1) Dirty=np.zeros_like(PSF) nx,_=Dirty.shape Dirty[nx//2,nx//2+10]+=2. Dirty[nx//2+10,nx//2+10]+=2. Dirty=np.random.randn(*(Dirty.shape)) PSF=PSF.reshape((1,1,nx,nx))*np.ones((2,1,1,1)) Dirty=Dirty.reshape((1,1,nx,nx))*np.ones((2,1,1,1)) Dirty[1,:,:,:]=Dirty[0,:,:,:]*2 x,y=np.mgrid[0:nx,0:nx] dx=10 nc=nx//2 x=x[nc-dx:nc+dx,nc-dx:nc+dx].flatten() y=y[nc-dx:nc+dx,nc-dx:nc+dx].flatten() ListPixParms=[(x[i],y[i]) for i in range(x.size)] x,y=np.mgrid[0:nx,0:nx] dx=10 x=x[nc-dx:nc+dx,nc-dx:nc+dx].flatten() y=y[nc-dx:nc+dx,nc-dx:nc+dx].flatten() ListPixData=[(x[i],y[i]) for i in range(x.size)] CC=ClassConvMachine(PSF,ListPixParms,ListPixData,"Matrix") NFreqBands,_,_,_=Dirty.shape NPixListParms=len(ListPixParms) NPixListData=len(ListPixData) Array=np.zeros((NFreqBands,1,NPixListParms),np.float32) x0,y0=np.array(ListPixParms).T for iBand in range(NFreqBands): Array[iBand,0,:]=Dirty[iBand,0,x0,y0] Array=Array.reshape((NFreqBands,NPixListParms)) import pylab Lchi0=[] Lchi1=[] NTries=5000 ArrKeep0=np.zeros((NTries,NPixListParms),Array.dtype) ArrKeep1=np.zeros((NTries,NPixListParms),Array.dtype) for i in range(NTries): Array=np.random.randn(*Array.shape) #T=ClassTimeIt.ClassTimeIt() chi0=np.sum(Array**2) Lchi0.append(chi0) ConvArray0=CC.Convolve(Array) chi1=np.sum(ConvArray0**2) #T.timeit("0") #ConvArray1=CC.Convolve(Array,ConvMode="Vector").ravel() #T.timeit("1") #r=chi1/chi0 #print "%f -> %f [%r]"%(chi0,chi1,r) NChan,_,NN=ConvArray0.shape NN=int(np.sqrt(NN)) ArrKeep0[i]=Array[0].ravel() ArrKeep1[i]=ConvArray0[0].ravel() # pylab.clf() # pylab.imshow(ConvArray0.reshape((2,NN,NN))[0],interpolation="nearest") # pylab.draw() # pylab.show(False) # pylab.pause(0.1) Lchi1.append(chi1) #print np.var(Array),np.var(ConvArray0)/Fact Fact=CC.NormData[0] print(np.median(np.std(ArrKeep0,axis=0)**2)) print(np.median(np.std(ArrKeep1,axis=0)**2/Fact)) return from scipy.stats import chi2 from DDFacet.ToolsDir.GeneDist import ClassDistMachine DM=ClassDistMachine() rv = chi2(Array.size) x=np.linspace(0,2*rv.moment(1),1000) P=rv.cdf(x) pylab.clf() pylab.subplot(2,1,1) #yd,xe=pylab.histogram(Lchi0,bins=100,normed=True) #xd=(xe[1::]+xe[0:-1])/2. #yd/=np.sum(yd) xd,yd=DM.giveCumulDist(np.array(Lchi0),Ns=100) #dx=xd[1]-xd[0] #yd/=dx pylab.plot(xd,yd) pylab.plot(x,P) pylab.xlim(0,1600) pylab.subplot(2,1,2) xd,yd=DM.giveCumulDist(np.array(Lchi1),Ns=20) # yd,xe=pylab.histogram(Lchi1,bins=100,normed=True) # xd=(xe[1::]+xe[0:-1])/2. # dx=xd[1]-xd[0] # yd/=np.sum(yd) # yd/=dx print(np.mean(Lchi1)/Fact) print(np.mean(Lchi0)) # #pylab.xlim(0,800) # #pylab.hist(Lchi1,bins=100) import scipy.interpolate cdf=scipy.interpolate.interp1d(xd, yd,"cubic") x=np.linspace(xd.min(),xd.max(),1000) #pylab.plot(x,cdf(x),ls="",marker=".") #pylab.plot(xd,yd,ls="",marker="s") y=cdf(x) x,y=xd, yd y=y[1::]-y[0:-1] x=(x[1::]+x[0:-1])/2. pylab.plot(x,y,ls="",marker=".") #pylab.xlim(0,1600) pylab.draw() pylab.show(False) # import pylab # pylab.clf() # #pylab.plot(ConvArray0.ravel()) # pylab.imshow(PSF[0,0]) # #pylab.plot(ConvArray1) # #pylab.plot(ConvArray1-ConvArray0) # pylab.draw() # pylab.show(False) stop
def TimingWindow(filename='.', ind=-1, save=None): """ Recreate BCI2000's timing window offline, from a saved .dat file specified by <filename>. It is also possible to supply a directory name as <filename>, and an index <ind> (default value -1 meaning "the last run") to choose a file automatically from that directory. Based on BCI2000's src/shared/modules/signalsource/DataIOFilter.cpp where the timing window content is computed in DataIOFilter::Process(), this is what appears to happen: Begin SampleBlock #t: Enter SignalSource module's first Process() method (DataIOFilter::Process) Save previous SampleBlock to file Wait to acquire new SampleBlock from hardware +--------- Measure SourceTime in SignalSource module | | | Make a local copy of all states (NB: all except SourceTime were set during #t-1) ---+ B| R| S| Pipe the signal through the rest of BCI2000 | | | +- Measure StimulusTime in Application module, on leaving last Process() method | | | | | | | | | Begin SampleBlock #t+1: | | +----- Enter SignalSource module's first Process() method (DataIOFilter::Process) | | Save data from #t, SourceTime state from #t, and other states from #t-1, to file <--+ | Wait to acquire new SampleBlock from hardware +--------- Measure SourceTime in SignalSource module Make a local copy of all states (NB: all except SourceTime were set during #t) Leave DataIOFilter::Process() and pipe the signal through the rest of BCI2000 Measure StimulusTime in Application module, on leaving last Process() method B stands for Block duration. R stands for Roundtrip time (visible in VisualizeTiming, not reconstructable from the .dat file) S is the filter cascade time (marked "Stimulus" in the VisualizeTiming window). Note that, on any given SampleBlock as saved in the file, SourceTime will be *greater* than any other timestamp states (including StimulusTime), because it is the only state that is updated in time to be saved with the data packet it belongs to. All the others lag by one packet. This is corrected for at the point commented with ??? in the Python code. """ if hasattr(filename, 'filename'): filename = filename.filename b = bcistream(filename=filename, ind=ind) out = sstruct() out.filename = b.filename #print "decoding..." sig,states = b.decode('all') #print "done" b.close() dT,T,rT = {},{},{} statenames = ['SourceTime', 'StimulusTime'] + ['PythonTiming%02d' % (x+1) for x in range(2)] statenames = [s for s in statenames if s in states] for key in statenames: dT[key],T[key] = unwrapdiff(states[key].flatten(), base=65536, dtype=numpy.float64) sel, = numpy.where(dT['SourceTime']) for key in statenames: dT[key] = dT[key][sel[1:]] if key == 'SourceTime': tsel = sel[:-1] # ??? why the shift else: tsel = sel[1:] # ??? relative to here? T[key] = T[key][tsel+1] t0 = T['SourceTime'][0] for key in statenames: T[key] -= t0 t = T['SourceTime'] / 1000 expected = b.samples2msec(b.params['SampleBlockSize']) datestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(b.datestamp)) paramstr = ', '.join(['%s=%s' % (x,b.params[x]) for x in ['SampleBlockSize', 'SamplingRate', 'VisualizeTiming', 'VisualizeSource']]) chainstr = '-'.join([x for x,y in b.params['SignalSourceFilterChain']+b.params['SignalProcessingFilterChain']+b.params['ApplicationFilterChain']]) titlestr = '\n'.join([b.filename, datestamp, paramstr, chainstr]) plot(t[[0,-1]], [expected]*2, drawnow=False) plot(t, dT['SourceTime'], hold=True, drawnow=False) for key in statenames: if key == 'SourceTime': continue rT[key] = T[key] - T['SourceTime'] plot(t, rT[key], hold=True, drawnow=False) import pylab pylab.title(titlestr) pylab.grid(True) pylab.xlabel('seconds') pylab.ylabel('milliseconds') ymin,ymax = pylab.ylim(); pylab.ylim(ymax=max(ymax,expected*2)) pylab.xlim(xmax=t[-1]) pylab.draw() out.params = sstruct(b.params) out.summarystr = titlestr out.t = t out.SourceTime = T['SourceTime'] out.StimulusTime = T['StimulusTime'] out.BlockDuration = dT['SourceTime'] out.BlockDuration2 = dT['StimulusTime'] out.ProcessingTime = out.StimulusTime - out.SourceTime out.ExpectedBlockDuration = expected out.rT = rT out.dT = dT out.T = T if save: pylab.gcf().savefig(save, orientation='landscape') return out
def show_it( self, fig, filename, kalman_filename=None, frame_start=None, frame_stop=None, show_nth_frame=None, obj_only=None, reconstructor_filename=None, options=None, ): if show_nth_frame == 0: show_nth_frame = None results = result_utils.get_results(filename, mode='r') opened_kresults = False kresults = None if kalman_filename is not None: if os.path.abspath(kalman_filename) == os.path.abspath(filename): kresults = results else: kresults = PT.open_file(kalman_filename, mode='r') opened_kresults = True # copied from plot_timeseries_2d_3d.py ca = core_analysis.get_global_CachingAnalyzer() (xxobj_ids, xxuse_obj_ids, xxis_mat_file, xxdata_file, extra) = ca.initial_file_load(kalman_filename) fps = extra['frames_per_second'] dynamic_model_name = None if dynamic_model_name is None: dynamic_model_name = extra.get('dynamic_model_name', None) if dynamic_model_name is None: dynamic_model_name = dynamic_models.DEFAULT_MODEL warnings.warn('no dynamic model specified, using "%s"' % dynamic_model_name) else: print 'detected file loaded with dynamic model "%s"' % dynamic_model_name if dynamic_model_name.startswith('EKF '): dynamic_model_name = dynamic_model_name[4:] print ' for smoothing, will use dynamic model "%s"' % dynamic_model_name if hasattr(results.root, 'images'): img_table = results.root.images else: img_table = None reconstructor_source = None if reconstructor_filename is None: if kresults is not None: reconstructor_source = kresults elif hasattr(results.root, 'calibration'): reconstructor_source = results else: reconstructor_source = None else: if os.path.abspath(reconstructor_filename) == os.path.abspath( filename): reconstructor_source = results elif ((kalman_filename is not None) and (os.path.abspath(reconstructor_filename) == os.path.abspath(kalman_filename))): reconstructor_source = kresults else: reconstructor_source = reconstructor_filename if reconstructor_source is not None: self.reconstructor = flydra_core.reconstruct.Reconstructor( reconstructor_source) if options.stim_xml: file_timestamp = results.filename[4:19] stim_xml = xml_stimulus.xml_stimulus_from_filename( options.stim_xml, timestamp_string=file_timestamp) if self.reconstructor is not None: stim_xml.verify_reconstructor(self.reconstructor) if self.reconstructor is not None: self.reconstructor = self.reconstructor.get_scaled() camn2cam_id, cam_id2camns = result_utils.get_caminfo_dicts(results) data2d = results.root.data2d_distorted # make sure we have 2d data table print 'reading frames...' frames = data2d.read(field='frame') print 'OK' if frame_start is not None: print 'selecting frames after start' #after_start = data2d.get_where_list( 'frame>=frame_start') after_start = numpy.nonzero(frames >= frame_start)[0] else: after_start = None if frame_stop is not None: print 'selecting frames before stop' #before_stop = data2d.get_where_list( 'frame<=frame_stop') before_stop = numpy.nonzero(frames <= frame_stop)[0] else: before_stop = None print 'finding all frames' if after_start is not None and before_stop is not None: use_idxs = numpy.intersect1d(after_start, before_stop) elif after_start is not None: use_idxs = after_start elif before_stop is not None: use_idxs = before_stop else: use_idxs = numpy.arange(data2d.nrows) # OK, we have data coords, plot print 'reading cameras' frames = frames[ use_idxs] #data2d.read_coordinates( use_idxs, field='frame') if len(frames): print 'frame range: %d - %d (%d frames total)' % ( frames[0], frames[-1], len(frames)) camns = data2d.read(field='camn') camns = camns[use_idxs] #camns = data2d.read_coordinates( use_idxs, field='camn') unique_camns = numpy.unique(camns) unique_cam_ids = list(set([camn2cam_id[camn] for camn in unique_camns])) unique_cam_ids.sort() print '%d cameras with data' % (len(unique_cam_ids), ) # plot all cameras, not just those with data all_cam_ids = cam_id2camns.keys() all_cam_ids.sort() unique_cam_ids = all_cam_ids if len(unique_cam_ids) == 1: n_rows = 1 n_cols = 1 elif len(unique_cam_ids) <= 6: n_rows = 2 n_cols = 3 elif len(unique_cam_ids) <= 12: n_rows = 3 n_cols = 4 else: n_rows = 4 n_cols = int(math.ceil(len(unique_cam_ids) / n_rows)) for i, cam_id in enumerate(unique_cam_ids): ax = auto_subplot(fig, i, n_rows=n_rows, n_cols=n_cols) ax.set_title('%s: %s' % (cam_id, str(cam_id2camns[cam_id]))) ## ax.set_xticks([]) ## ax.set_yticks([]) ax.this_minx = np.inf ax.this_maxx = -np.inf ax.this_miny = np.inf ax.this_maxy = -np.inf self.subplot_by_cam_id[cam_id] = ax for cam_id in unique_cam_ids: ax = self.subplot_by_cam_id[cam_id] if img_table is not None: bg_arr_h5 = getattr(img_table, cam_id) bg_arr = bg_arr_h5.read() ax.imshow(bg_arr.squeeze(), origin='lower', cmap=cm.pink) ax.set_autoscale_on(True) ax.autoscale_view() pylab.draw() ax.set_autoscale_on(False) if self.reconstructor is not None: if cam_id in self.reconstructor.get_cam_ids(): res = self.reconstructor.get_resolution(cam_id) ax.set_xlim([0, res[0]]) ax.set_ylim([res[1], 0]) if options.stim_xml is not None: stim_xml.plot_stim_over_distorted_image(ax, cam_id) for camn in unique_camns: cam_id = camn2cam_id[camn] ax = self.subplot_by_cam_id[cam_id] this_camn_idxs = use_idxs[camns == camn] xs = data2d.read_coordinates(this_camn_idxs, field='x') valid_idx = numpy.nonzero(~numpy.isnan(xs))[0] if not len(valid_idx): continue ys = data2d.read_coordinates(this_camn_idxs, field='y') if options.show_orientation: slope = data2d.read_coordinates(this_camn_idxs, field='slope') idx_first_valid = valid_idx[0] idx_last_valid = valid_idx[-1] tmp_frames = data2d.read_coordinates(this_camn_idxs, field='frame') ax.plot([xs[idx_first_valid]], [ys[idx_first_valid]], 'ro', label='first point') ax.this_minx = min(np.min(xs[valid_idx]), ax.this_minx) ax.this_maxx = max(np.max(xs[valid_idx]), ax.this_maxx) ax.this_miny = min(np.min(ys[valid_idx]), ax.this_miny) ax.this_maxy = max(np.max(ys[valid_idx]), ax.this_maxy) ax.plot(xs[valid_idx], ys[valid_idx], 'g.', label='all points') if options.show_orientation: angle = np.arctan(slope) r = 20.0 dx = r * np.cos(angle) dy = r * np.sin(angle) x0 = xs - dx x1 = xs + dx y0 = ys - dy y1 = ys + dy segs = [] for i in valid_idx: segs.append(((x0[i], y0[i]), (x1[i], y1[i]))) line_segments = collections.LineCollection( segs, linewidths=[1], colors=[(0, 1, 0)], ) ax.add_collection(line_segments) ax.plot([xs[idx_last_valid]], [ys[idx_last_valid]], 'bo', label='first point') if show_nth_frame is not None: for i, f in enumerate(tmp_frames): if f % show_nth_frame == 0: ax.text(xs[i], ys[i], '%d' % (f, )) if 0: for x, y, frame in zip(xs[::5], ys[::5], tmp_frames[::5]): ax.text(x, y, '%d' % (frame, )) fig.canvas.mpl_connect('key_press_event', self.on_key_press) if options.autozoom: for cam_id in self.subplot_by_cam_id.keys(): ax = self.subplot_by_cam_id[cam_id] ax.set_xlim((ax.this_minx - 10, ax.this_maxx + 10)) ax.set_ylim((ax.this_miny - 10, ax.this_maxy + 10)) if options.save_fig: for cam_id in self.subplot_by_cam_id.keys(): ax = self.subplot_by_cam_id[cam_id] ax.set_xticks([]) ax.set_yticks([]) if kalman_filename is None: return if 0: # Do same as above for Kalman-filtered data kobs = kresults.root.ML_estimates kframes = kobs.read(field='frame') if frame_start is not None: k_after_start = numpy.nonzero(kframes >= frame_start)[0] else: k_after_start = None if frame_stop is not None: k_before_stop = numpy.nonzero(kframes <= frame_stop)[0] else: k_before_stop = None if k_after_start is not None and k_before_stop is not None: k_use_idxs = numpy.intersect1d(k_after_start, k_before_stop) elif k_after_start is not None: k_use_idxs = k_after_start elif k_before_stop is not None: k_use_idxs = k_before_stop else: k_use_idxs = numpy.arange(kobs.nrows) obj_ids = kobs.read(field='obj_id')[k_use_idxs] obs_2d_idxs = kobs.read(field='obs_2d_idx')[k_use_idxs] kframes = kframes[k_use_idxs] kobs_2d = kresults.root.ML_estimates_2d_idxs xys_by_obj_id = {} for obj_id, kframe, obs_2d_idx in zip(obj_ids, kframes, obs_2d_idxs): if obj_only is not None: if obj_id not in obj_only: continue obs_2d_idx_find = int( obs_2d_idx) # XXX grr, why can't pytables do this? obj_id_save = int(obj_id) # convert from possible numpy scalar xys_by_cam_id = xys_by_obj_id.setdefault(obj_id_save, {}) kobs_2d_data = kobs_2d.read(start=obs_2d_idx_find, stop=obs_2d_idx_find + 1) assert len(kobs_2d_data) == 1 kobs_2d_data = kobs_2d_data[0] this_camns = kobs_2d_data[0::2] this_camn_idxs = kobs_2d_data[1::2] this_use_idxs = use_idxs[frames == kframe] d2d = data2d.read_coordinates(this_use_idxs) for this_camn, this_camn_idx in zip(this_camns, this_camn_idxs): this_idxs_tmp = numpy.nonzero(d2d['camn'] == this_camn)[0] this_camn_d2d = d2d[d2d['camn'] == this_camn] found = False for this_row in this_camn_d2d: # XXX could be sped up if this_row['frame_pt_idx'] == this_camn_idx: found = True break if not found: if 1: print 'WARNING:point not found in data -- 3D data starts before 2D I guess.' continue else: raise RuntimeError('point not found in data!?') this_cam_id = camn2cam_id[this_camn] xys = xys_by_cam_id.setdefault(this_cam_id, ([], [])) xys[0].append(this_row['x']) xys[1].append(this_row['y']) for obj_id in xys_by_obj_id: xys_by_cam_id = xys_by_obj_id[obj_id] for cam_id, (xs, ys) in xys_by_cam_id.iteritems(): ax = self.subplot_by_cam_id[cam_id] ax.plot(xs, ys, 'x-', label='obs: %d' % obj_id) ax.text(xs[0], ys[0], '%d:' % (obj_id, )) ax.text(xs[-1], ys[-1], ':%d' % (obj_id, )) if 1: # do for core_analysis smoothed (or not) data for obj_id in xxuse_obj_ids: try: rows = ca.load_data(obj_id, kalman_filename, use_kalman_smoothing=True, frames_per_second=fps, dynamic_model_name=dynamic_model_name) except core_analysis.NotEnoughDataToSmoothError: warnings.warn( 'not enough data to smooth obj_id %d, skipping.' % (obj_id, )) if frame_start is not None: c1 = rows['frame'] >= frame_start else: c1 = np.ones((len(rows), ), dtype=np.bool) if frame_stop is not None: c2 = rows['frame'] <= frame_stop else: c2 = np.ones((len(rows), ), dtype=np.bool) valid = c1 & c2 rows = rows[valid] if len(rows) > 1: X3d = np.array((rows['x'], rows['y'], rows['z'], np.ones_like(rows['z']))).T for cam_id in self.subplot_by_cam_id.keys(): ax = self.subplot_by_cam_id[cam_id] newx, newy = self.reconstructor.find2d(cam_id, X3d, distorted=True) ax.plot(newx, newy, '-', label='k: %d' % obj_id) results.close() if opened_kresults: kresults.close()