Beispiel #1
0
def graph(toKeep):
    """Used to display the graphs. Text will be shown before hand as
    the graphs take time to generate"""
    global firstTime

    #Display some text stating that graphs are being generated 
    if toKeep == TwelveHours:
        displayText('Creating', 35, 1,(200,200,1),True)
        displayText('12 Hour Graph', 32, 2,(150,150,255), False)
    elif toKeep == TwentyFourHours:
        displayText('Creating', 35, 1,(200,200,1), True)
        displayText('24 Hour Graph', 32, 2,(150,150,255), False)
    elif toKeep == OneWeek:
        displayText('Creating', 35, 1,(200,200,1), True)
        displayText('One Week Graph', 28, 2,(150,150,255), False)
    pygame.display.flip()

    #Get temperature and time data from data file
    temp = genfromtxt('temperature_logging', dtype=None, usecols=(0), skip_header = lines - toKeep)
    timecaptured = genfromtxt('temperature_logging', dtype=None, usecols=(1), skip_header = lines - toKeep)

    #Site the size of the font for the axis
    for label in ax.get_xticklabels():
        label.set_fontsize(8)
    for label in ax.get_yticklabels():
        label.set_fontsize(8)

    #Create xaxis labels and only show every 12th or 96th
    xlabels = range(toKeep)
    if toKeep == OneWeek:
        pylab.xticks(xlabels[::96], [v[:5:] for v in timecaptured[::96]])
    else:
        pylab.xticks(xlabels[::12], [v[11:16] for v in timecaptured[::12]])

    #Plot the graph
    pylab.plot(temp,linewidth=2, antialiased=True)

    #Change some colours
    ax.patch.set_facecolor('#FFFFCC')
    ax.patch.set_alpha(0.5)

    #Rotate the text in the xaxis
    fig.autofmt_xdate(rotation=90)
    #Set the yaxsis limits
    pylab.ylim(0,40)

    #Save the graph as an image and the re-open it, rotate it and then display to TFT.
    pylab.savefig('gp.png', facecolor=fig.get_facecolor(),bbox_inches='tight', dpi=80,pad_inches=0.03)
    pil_im = Image.open('gp.png')
    pil_out = pil_im.rotate(-90)
    pil_out.save("gp.png")
    img = pygame.image.load('gp.png')
    screen.blit(img,(0,0))
    pygame.display.flip()

    #Clear graph data in preparation for next plot
    pylab.cla()

    #Reset the firstTime Variable
    firstTime = 0
def do_Plot_item_a(Ustar, U, grad_phi):
    """ plotting stuff: here we plot the first row with Ustar and
        the second rows with Ud and grad phi, to see how they sum up """
    pylab.clf()
    pylab.cla()
    
    f = pylab.figure() 
    f.text(.5, .95, r"Top: $U = U_d + \nabla \phi$, where "  r"$U_d$ =  - $\sin^2 ( \pi x)\sin(2 \pi y)\hat x$ + $\sin^2(\pi y)\sin(2\pi x)\hat y$ and $\phi = \frac{1}{10} \cos(2\pi y) \cos(2\pi x$) ",  horizontalalignment='center', size=9)

    pylab.subplot(221) 
    pylab.imshow(Ustar[0])
    pylab.xlabel("Vector "r"$U_d$ in $\hat x$:" ,size=8)
    pylab.ylabel("U in terms of # of cells in " r"$\hat x$", size=8)

    pylab.subplot(222)
    pylab.imshow(Ustar[1])
    pylab.xlabel(r"Vector $\nabla \phi$ in $ \hat x $:", size=8)
    pylab.ylabel("U in terms of # of cells in " r"$\hat y$",size=8)

    pylab.subplot(223)
    pylab.imshow(U [0])
    pylab.ylabel("# of cells",size=8)
    pylab.xlabel("# of cells",size=8)
    
    
    pylab.subplot(224)
    pylab.imshow(grad_phi [0])
    pylab.xlabel("# of cells",size=8)
    pylab.ylabel("# of cells",size=8)
    pylab.savefig("plots/item_a_Ustar.png")
    
    return 0
Beispiel #3
0
	def add_point(event):
		x, y = event.xdata, event.ydata
		points.append((x, y))
		pylab.cla()
		pylab.scatter(*zip(*points))
		pylab.xlim(-10, 10)
		pylab.ylim(-10, 10)

		pylab.draw()
		if len(points) < 3: return

		c, r = three_point_circle(*points)
		cir = pylab.Circle(c, r)
		pylab.gca().add_patch(cir)

		for p in points:
			angle = angle_at_point(c, p)
			if angle < 0:
				angle += 2*np.pi
			if angle >= np.pi:
				angle = angle - np.pi
			print np.degrees(angle)
			dx, dy = np.array((np.cos(angle), np.sin(angle)))
			pylab.text(p[0], p[1], "%.2f"%np.degrees(angle))
			pylab.arrow(p[0], p[1], dx, dy)
		pylab.show()
def draw():
    pl.cla()
    nodeSize=[1000*net.in_degree(n) for n in nx.nodes(net)]
    nx.draw(net, pos = positions, node_color = [net.node[i]['id'] for i in net.nodes_iter()], with_labels = True, edge_color = 'c', cmap = pl.cm.RdBu, vmin = 0, vmax = 1,node_size=nodeSize, alpha=0.75)
    pl.axis('image')
    pl.title('t = ' + str(time))
    plt.show() 
def PlotGrids(gs, grids=None, drawnow=True):
	"""
	Plot the grids, each on a separate subplot if there are more than one, to be sure of the
	row and column coordinates inferred for each numbered electrode.
	
	NB: requires installation of the matplotlib python package (including the pylab module)
		for plotting.
	"""###

	import pylab
	each = EachGrid(gs, grids=grids)
	ngrids = len(each)
	for rows in range(int(ngrids**0.5), 0, -1):
		if ngrids % rows == 0: cols = ngrids / rows; break
	else: rows = int(ngrids**0.5); cols = int(math.ceil(ngrids/float(rows)))
	if ngrids != 1: pylab.clf()
	for i,(gridname,g) in enumerate(each):
		if ngrids != 1: pylab.subplot(rows, cols, i+1)
		pylab.cla()
		xcoords,ycoords = [],[]
		for trode in g:
			row,col,val = trode.localRowIndex,trode.localColumnIndex,trode.localNumber
			xcoords.append(col); ycoords.append(row)
			pylab.text(col, row, str(val), horizontalalignment='center', verticalalignment='center')
		pylab.xlim(min(xcoords)-1, max(xcoords)+1)
		pylab.ylim(max(ycoords)+1, min(ycoords)-1)
		pylab.grid('on')
		pylab.title(gridname)
	if drawnow:
		pylab.draw()
		pylab.show()
Beispiel #6
0
 def updatePlot(self):
     """Clear, re-draw, and save.
     Allows viewing "real-time" as an image
     """
     # clear figure and axes
     pylab.clf()
     pylab.cla()
     pylab.grid(True, axis='y', linewidth=1, color='gray', linestyle='--')
     # plot each line
     for line in self.toPlot:
         values = self.toPlot[line].get('values')
         label = self.toPlot[line].get('label')
         color = self.toPlot[line].get('color')
         style = self.toPlot[line].get('style')
         # print values,label,color,style
         pylab.plot(values, label=label, color=color, linestyle=style)
     ylims = self.getYlims()
     pylab.ylim(ylims)
     # labels
     pylab.title("Moving Averages against Price of %s" % self.pair)
     pylab.xlabel("Ticks")
     pylab.ylabel("Price")
     # legend top-left
     pylab.legend(loc=2)
     # save and close
     pylab.savefig('graph.png', dpi=self.DPI)
     pylab.close(self.graph)
    def plot(self, frame, position, offset):

        global plot

        if plot:
            fname = "/tmp/autoguider.fits"
            if os.path.exists(fname):
                os.remove(fname)
            frame.save(fname)
            img = fits.getdata(fname)
            hdr = fits.getheader(fname)
            py.figure(1)
            py.cla()
            py.imshow(img, origin="lower", interpolation="nearest", vmax=np.mean(img) * 1.1, cmap=py.cm.gray)
            py.plot(position["XWIN_IMAGE"], position["YWIN_IMAGE"], "b+")
            py.plot(position["XWIN_IMAGE"] + offset["X"], position["YWIN_IMAGE"] + offset["Y"], "rx")
            py.plot(position["XWIN_IMAGE"] - hdr["offsetY"] + 1, position["YWIN_IMAGE"] - hdr["offsetX"], "r.")
            err = np.sqrt(((-hdr["offsetY"] + 1 - offset["X"]) ** 2) + ((-hdr["offsetX"] - offset["Y"]) ** 2))

            py.text(
                -3,
                -3,
                "Offset: (%.2f/%.2f) px, Calculated: (%.2f/%.2f) px, Error: %3.2f"
                % (-hdr["offsetY"] + 1, -hdr["offsetX"], offset["X"], offset["Y"], err),
            )
            py.savefig(os.path.join(SYSTEM_CONFIG_DIRECTORY, "autoguider_%05i.png") % self.index)
            py.show()
            self.index += 1
Beispiel #8
0
 def stat_all_by_age(self):
     bmi_by_age = []
     sum_of_bmi = 0
     count = 0
     for i in range(1, 151):
         infos = Info.objects.filter(age = i)
         count += len(infos)
         if infos:
             for info in infos:
                 sum_of_bmi += info.get_bmi()
         else:
             sum_of_bmi += 0
         if not i%10:
             bmi_by_age.append((i, sum_of_bmi, count))
             sum_of_bmi = 0
             count = 0
     ages = [i for i in range(10, 151, 10)]
     avebmi = [0 if i[1] == 0 else round(i[1]/i[2], 1) for i in bmi_by_age]
     pl.figure('all0', figsize = (5.2, 2.8), dpi = 100)
     pl.xlabel(u'年龄段', fontproperties = zhfont)
     pl.ylabel('平均BMI', fontproperties = zhfont)
     pl.ylim(0.0, 50.0)
     pl.plot(ages, avebmi, '*', color = '#20b2aa')
     pl.savefig(self.file_path + 'all0.jpg')
     pl.cla()
     avecount = [i[2] for i in bmi_by_age]
     ages = ['%d-%d'%(i-9, i) for i in ages]
     return list(zip(ages, avebmi, avecount))
Beispiel #9
0
def scan():
    pylab.ion()
    pylab.figure(1)
    
    
    with Communicate('', None, debug=True) as serial:
        serial.timeout = 0.0001
        camera = Camera()
        camera.setupmeasure()
        
        controller = Controller(serial)
        controller.setupscan()
        
        out = []
        for x,y in controller.scan():
            camera.update()
            camera.interact()
            
            z = camera.measure()
            out.append([x,y,z])
            
            if camera.status == 'quit':
                break
            camera.show()
            
            if len(out) > 0:
                pylab.cla()
                tmp = zip(*out)
                sc = pylab.scatter(tmp[0],tmp[1],s=tmp[2], c=tmp[2], vmin=0, vmax=400)
                print '{: 8.3f} {: 8.3f} {: 8.3f}'.format(x,y,z)
            
        pylab.ioff()
        pylab.show()
Beispiel #10
0
    def quiver(self, sparsity=None, scale=None):
        var = self.vars[0]
        mesh = var.getMesh()

        if isinstance(var, FaceVariable):
            N = mesh._getNumberOfFaces() 
            V = mesh._getFaceAreas()
            X, Y = mesh.getFaceCenters()
        elif isinstance(var, CellVariable):
            N = mesh.getNumberOfCells() 
            V = mesh.getCellVolumes()
            X, Y = mesh.getCellCenters()

        if sparsity is not None and N > sparsity:
            self.indices = numerix.random.rand(N) * V
            self.indices = self.indices.argsort()[-sparsity:]
        else:
            self.indices = numerix.arange(N)

        X = numerix.take(X, self.indices)
        Y = numerix.take(Y, self.indices)
        
        U = V = numerix.ones(X.shape)
        
        import pylab
        
        pylab.ion()
        pylab.cla()
        self._quiver = pylab.quiver(X, Y, U, V, scale=scale)
        pylab.ioff()
Beispiel #11
0
def do_plots_c(Ud, Unew):
    """ plot Ud,new and Ud with zoom on the bug """
    pylab.clf()
    pylab.cla()

    f = pylab.figure()
    f.text(.5, .95, r"$U_{\rm d}$ (left) and $U_{\rm d, new}$ (right) ",
           horizontalalignment='center')

    pylab.subplot(221)
    pylab.imshow(Ud[0])
    pylab.ylabel("# of cells", size=8)

    pylab.subplot(223)
    pylab.imshow(Ud[1])
    pylab.xlim(1, 32)
    pylab.xlabel("# of cells", size=8)
    pylab.ylabel("# of cells", size=8)

    pylab.subplot(222)
    pylab.imshow(Unew[0])
    pylab.ylabel("# of cells", size=8)

    pylab.subplot(224)
    pylab.imshow(Unew[1])
    pylab.xlim(1, 32)
    pylab.xlabel("# of cells", size=8)
    pylab.ylabel("# of cells", size=8)

    pylab.savefig("plots/item_c_Udnew.png")
Beispiel #12
0
def updatePlot(G, position, spinUp, spinDown, upColor, downColor, ax, E, step):
    """
    Updates plot of network for viewing MC moves.
    """
    pl.cla()
    position = nx.circular_layout(G)
    nx.draw_networkx_nodes(G,position, nodelist=spinUp,
        node_color=upColor)
    nx.draw_networkx_nodes(G,position, nodelist=spinDown,
        node_color=downColor)
    nx.draw_networkx_edges(G,position)
    nx.draw_networkx_labels(G,position)
    ax.text(-0.1, 0.98, 'Spin Up', style='italic',
            bbox={'facecolor':upColor, 'alpha':0.9, 'pad':10})
    ax.text(-0.1, 1.1, 'Spin Down', style='italic',color='White',
            bbox={'facecolor':downColor, 'alpha':0.9, 'pad':10})

    if E:
        os.chdir('./data/')
        if os.path.exists('./animate'):
            pass
        else:
            os.mkdir('./animate/')
        os.chdir('./animate/')
        pl.savefig('animate_'+str(step)+'.png')
        os.chdir('../..')

    pl.draw()
Beispiel #13
0
def draw_hist_cdf(data,fig=None,nbins=None,subpl=None,nohist=False,**figargs):
    '''input data is a list of dicts with keys: data,histcolor,plotline
    '''

    bins = None

    if fig: pylab.figure(fig,**figargs)
    if subpl: pylab.subplot(subpl)
    
    results = []
    for d in data:
        if bins is not None:
            n,bins,patches=pylab.hist(d['data'],bins=bins,normed=1,fc=histcolors[d['plotline'][0]])
        elif nbins is not None:
            n,bins,patches=pylab.hist(d['data'],bins=nbins,normed=1,fc=histcolors[d['plotline'][0]])
        else:
            n,bins,patches=pylab.hist(d['data'],normed=1,fc=histcolors[d['plotline'][0]])
        results.append(n)

    if nohist:
        pylab.cla()
    else:
        pylab.twinx()

    for i,d in enumerate(data):
        y = pylab.plot(bins,numpy.cumsum(results[i])/sum(results[i]), d['plotline'], linewidth=2)
def weightedAffineInvCompWarpCost(targetIMG, templateIMG, templateWeight, curParameters, displayStuff, targetIMGMask = None):
	#function [ImageWarpedToTemplate, TX, TY, ErrorIMG, CostValue] = lk_weighted_run_affine_inv_comp_warpcost(targetIMG, templateIMG, TemplateW, CurParameters, displayStuff)
#	[ImageWarpedToTemplate, TX, TY] = lk_warp_image_affine(targetIMG, size(templateIMG), CurParameters);
	targetIMGToTemplate, TX, TY = warpImageAffine(targetIMG, templateIMG.shape, curParameters)
	if displayStuff == True:
		pylab.subplot(4, 3, 5);	CCSegUtils.showIMG(targetIMGToTemplate); pylab.title('template coordinates warped to image');
		pylab.subplot2grid((4, 3), (1, 2), rowspan = 2, colspan = 1); pylab.cla(); CCSegUtils.showIMG(targetIMG);
			
		pylab.plot(TX[:, 0], TY[:, 0], 'b-')
		pylab.plot(TX[0, :], TY[0, :], 'b-')
		pylab.plot(TX[:, -1], TY[:, -1], 'b-')
		pylab.plot(TX[-1, :], TY[-1, :], 'b-')
		pylab.title('Coordinates on target')
	#print "oiajfdoijadsf"

	errorIMG = targetIMGToTemplate - templateIMG

	
	LKCost = numpy.sum(errorIMG * templateWeight * errorIMG)
	
	# find out if any coordinates are not in the mask
	if targetIMGMask != None:
		T = CCSegUtils.interp2q(numpy.arange(1, targetIMG.shape[1] + 1), numpy.arange(1, targetIMG.shape[0] + 1), targetIMGMask, TX, TY, extrapval = 0)
		if numpy.any(T == 0):
			LKCost = numpy.inf
	
	return (targetIMGToTemplate, TX, TY, errorIMG, LKCost)
def plotDC(vp,block,trial):
    ''' plot gaze during drift correction'''
    from Preprocess import readEyelink
    plt.interactive(False)
#    vp=1
#    from readETData import readEyelink
#    for b in range(4,23):
#        print 'block ', b
#        data=readEyelink(vp,b)
#        for i in range(0,len(data)):
    b=block;i=trial
    data=readEyelink(vp,b)
    d=data[i]
    gg=d.getGaze(phase=3)
    plt.plot(gg[:,0],gg[:,1],'g--')
    plt.plot(gg[:,0],gg[:,2],'r--')
    plt.plot(gg[:,0],gg[:,4],'b--')
    plt.plot(gg[:,0],gg[:,5],'k--')
    d.extractBasicEvents()
    d.driftCorrection(jump=manualDC(vp,b,i))
    gg=d.getGaze(phase=3)
    plt.plot(gg[:,0],gg[:,1],'g')
    plt.plot(gg[:,0],gg[:,2],'r')
    plt.plot(gg[:,0],gg[:,4],'b')
    plt.plot(gg[:,0],gg[:,5],'k')
    plt.plot([gg[0,0],gg[-1,0]],[0,0],'k')
    plt.plot(d.dcfix,[-0.45,-0.45],'k',lw=2)
    plt.grid()
    plt.ylim([-0.5,0.5])
    plt.legend(['left x','left y','right x','right y'])
    plt.savefig(PATH+'dc'+os.path.sep+'vp%03db%02dtr%02d'%(vp,b,i))
    plt.cla()
Beispiel #16
0
def plotDistHist(filtered, options):
    shifts = []
    oldShifts = []
    for chrom in filtered:
        for p in filtered[chrom]:
            p.getCC()
            p.getOldShift()
            shifts.append(p.shift)
            oldShifts.append(p.old_shift)

            #if options.rpm == 20:
            #    dirname = "%s/%d"%(getOutput(options),p.shift/10)
            #    if not os.path.exists(dirname):
            #        os.system("mkdir -p " + dirname)
            #    p.plot(dirname)

    import pylab as pl
    pl.figure()
    pl.subplot(211)
    pl.hist(shifts, bins=100, range=(0,200))
    pl.subplot(212)
    pl.hist(oldShifts, bins=100, range=(0,200))
    pl.savefig(getOutput(options)+"_shifts.png",dpi=600)
    pl.cla()
    pl.clf()
    pl.close()
def draw(figure):
    global resources, time, theWorld, cmap, files
    PL.cla()
    
    cmap.set_under()
    PL.pcolormesh(theWorld.foraging_resources, cmap = cmap, vmin=0,
                  vmax=W.max_resource)
    PL.axis('scaled')
    PL.hold(True)
    xyp = zip(*[theWorld.hh_locations[hh] for hh in theWorld.households])
    xy = [list(t) for t in xyp]
    
    if len(xy)>0:
        x = [i+0.5 for i in xy[0]]
        y = [i+0.5 for i in xy[1]]
        lineage = [hh.lineage for hh in (theWorld.households)]
        hh_size = [20*hh.size() for hh in (theWorld.households)]
        PL.scatter(y, x, c = lineage, s=hh_size, vmin=0, vmax=W.starting_agents, cmap = plt.get_cmap('hsv'))
        message = r't = {0}     Pop.: {1}     HHs: {2}    max HHs: {3}'
        PL.title(message.format(time, theWorld.population, len(theWorld.households), max(lineage))) 
    PL.hold(False)
    figure.tight_layout()
    if MOVIES:
        fname = dirName+('\\_temp%05d.png'%time)
        figure.savefig(fname)
        files.append(fname)
    drawPlots()
def update(val):
    global psi
    global scale
    global slider_scale
    scale = slider_scale.val
    M.cla()
    plotvertices(psi)
    def draw(self):
        pl.cla()

        pos_net = nx.layout.fruchterman_reingold_layout(self.G,
                                                        pos=self.pos_old_net,
                                                        iterations=33)

        acarnevale = Author.get(141821)
        if acarnevale in self.G.nodes():
            pos_net[acarnevale] = [1,.001]
            pos_net = nx.layout.fruchterman_reingold_layout(self.G,
                                                            pos=pos_net,
                                                            fixed=[acarnevale,],
                                                            iterations=33)


        colors=[self.countries.index(self.G.edge[edge[0]][edge[1]]['cit'].country()) for edge in self.G.edges()]

        
        nx.draw(self.G,
                pos = pos_net,
                with_labels = False,
                node_color = 'white',
                node_alpha=.005,
                node_size=0,
                edge_color = colors,
                edge_cmap = plt.cm.Dark2,)
        pl.axis('image')
        pl.title(str(date))
        plt.savefig(str(date)+'.png')
        pos_old_net = pos_net
Beispiel #20
0
def barGraph(xs1, fs1, f, xlabel, ylabel):
	pylab.cla()
	pylab.bar(xs1, fs1, width=0.8, facecolor='blue')
	pylab.xlim([0,24])
	pylab.ylabel(ylabel)
	pylab.xlabel(xlabel)
	pylab.savefig(f)
def main():
    filenames = glob.glob("*.dat")
    filenames = sort(filenames) #yeah, python is easy

    N = len(filenames)
    picture = zeros((N,N))

    pylab.cla()
    stepcount = 1 
    for i in filenames:
        file = open(i)
        data = array(file.read().strip().split(" "))

        #put the data into the 2D picture array
        for j in range(N):
   #         print data[j]==str(1), " dataj ", type(data[j])
            if data[j] == str(1):
                picture[0:stepcount,j] = data[j]

        pylab.imshow(picture)
        
        fname = re.sub("dat", "png", i)
        pylab.savefig(fname)

        stepcount += 1
        print i

    make_pngs_into_an_avi(.5)

    os.system("rm *dat")
    os.system("rm *png")

    return
Beispiel #22
0
def plotDistHist(filtered, options):
    shifts = []
    oldShifts = []
    for chrom in filtered:
        count = 0
        for p in filtered[chrom]:
            p.getCC()
            p.getOldShift()
            shifts.append(p.shift)
            oldShifts.append(p.old_shift)

            if options.savefig >= 0:
                if options.rpm == options.savefig and count < 20 and np.random.rand() >= 0.5:
                    dirname = "%s/%d"%(getOutput(options),p.shift/10)
                    if not os.path.exists(dirname):
                        os.system("mkdir -p " + dirname)
                    p.plot(dirname)
                    count += 1

    import pylab as pl
    pl.figure()
    pl.subplot(211)
    pl.hist(shifts, bins=100, range=(0,200))
    pl.subplot(212)
    pl.hist(oldShifts, bins=100, range=(0,200))
    pl.savefig(getOutput(options)+"_shifts.png",dpi=600)
    pl.cla()
    pl.clf()
    pl.close()
Beispiel #23
0
def plot_adc_brams(new_pasp):

    run = True

    pylab.ion()
    # pylab.yscale('log')

    # read in the adc data
    adcscope_pol1, adcscope_pol2 = new_pasp.get_adc_brams(100)
    print adcscope_pol1
    pylab.cla()
    adcscope_power_line, = pylab.plot(adcscope_pol1)

    # while(run):
    """
    for i in range(1,10):
        try:
            fftscope_power = get_fft_brams_power()
            fftscope_power_line.set_ydata(fftscope_power)
            pylab.draw()

        except KeyboardInterrupt:
            run = False
    """

    raw_input("Press enter to quit: ")

    pylab.cla()
Beispiel #24
0
def plotPhaseSpace(b, aTheta, aOmega, t, power, k):
    pylab.clf()
    pylab.cla()

    label = str(b)

    pylab.subplot(221)
    pylab.plot(aTheta, aOmega, color="m", lw=2)
    pylab.xlabel(r"$\theta$ (radians) ", fontsize=10)
    pylab.ylabel('$\omega$ (radians/seconds)', fontsize=10)
    pylab.grid(True)

    pylab.subplot(222)
    pylab.plot(t, aTheta, color="g", lw=2)
    pylab.ylabel(r"$\theta$ (radians)", fontsize=10)
    pylab.xlabel('t (seconds)', fontsize=10)
    pylab.grid(True)

    pylab.subplot(223)
    pyplot.grid(True)
    pyplot.plot(k, power, color="c", lw=2)
    pyplot.ylabel("|F(k)$|^{2}$", fontsize=10)
    pyplot.xlabel(r"$\nu_k$ ($s^{-1}$)", fontsize=10)

    pylab.subplot(224)
    pyplot.yscale('log')
    pyplot.plot(2.0 * numpy.pi * k, power, color="b", lw=1)
    pylab.xlim(0, 6)
    pyplot.grid(True)
    pyplot.xlabel(r"$\nu_k$ ($s^{-1}$)", fontsize=10)
    pyplot.ylabel("log |F(k)$|^{2}$", fontsize=10)

    pylab.savefig("plots/b-%s_phase_space.png" % (label))

    return 0
Beispiel #25
0
def twoDimOrderPlot(outpath, base_name, title, obj_name, base_filename, order_num, data, x):
    pl.figure('2d order image', facecolor='white', figsize=(8, 5))
    pl.cla()
    pl.title(title + ', ' + base_name + ", order " + str(order_num), fontsize=14)
    pl.xlabel('wavelength($\AA$)', fontsize=12)
    pl.ylabel('row (pixel)', fontsize=12)
    #pl.imshow(img, aspect='auto')
    #pl.imshow(data, vmin=0, vmax=1024, aspect='auto')
    
    pl.imshow(exposure.equalize_hist(data), origin='lower', 
                  extent=[x[0], x[-1], 0, data.shape[0]], aspect='auto')      
#     from matplotlib import colors
#     norm = colors.LogNorm(data.mean() + 0.5 * data.std(), data.max(), clip='True')
#     pl.imshow(data, norm=norm, origin='lower',
#                   extent=[x[0], x[-1], 0, data.shape[0]], aspect='auto')               
    pl.colorbar()
    pl.set_cmap('jet')
#     pl.set_cmap('Blues_r')
    fn = constructFileName(outpath, base_name, order_num, base_filename)
    pl.savefig(fn)
    log_fn(fn)
    pl.close()
    
#     np.save(fn[:fn.rfind('.')], data)
    
    return
Beispiel #26
0
def plot_fft_brams(new_pasp):

    run = True

    pylab.ion()
    pylab.cla()
    pylab.yscale("log")

    # read in initial data from the fft brams
    fftscope_power = new_pasp.get_fft_brams_power()
    # set up bars for each pasp channel
    fftscope_power_line = pylab.bar(range(0, new_pasp.numchannels), fftscope_power)

    pylab.ylim(1, 1000000)

    # plot forever
    # for i in range(1,10):
    while run:
        try:
            fftscope_power = new_pasp.get_fft_brams_power()

            # update the rectangles
            for j in range(0, new_pasp.numchannels):
                fftscope_power_line[j].set_height(fftscope_power[j])
            pylab.draw()
        except KeyboardInterrupt:
            run = False

    # after receiving an interrupt wait before closing the plot
    raw_input("Press enter to quit: ")

    pylab.cla()
Beispiel #27
0
def three_subplots_position(x1, y1, x2, y2, x3, y3, title, x_label, y_label, save_name):
    pt.subplot(2,2, 1)
    pt.title(str(title))
    pt.plot(x1,y1, "r.")
    pt.plot([0],[0], "bo")
    pt.xlim(limits_detection(x1,y1)[0]*1.4, limits_detection(x1,y1)[1]*1.4)
    pt.ylim(limits_detection(x1,y1)[0]*1.4, limits_detection(x1,y1)[1]*1.4)
    pt.xlabel(x_label[0])
    pt.ylabel(y_label[0])
    
    
    pt.subplot(2,2, 2)
    pt.plot(x2,y2, "r.")
    pt.plot([0],[0], "bo")
    pt.xlim(limits_detection(x2,y2)[0]*1.4, limits_detection(x2,y2)[1]*1.4)
    pt.ylim(limits_detection(x3,y2)[0]*1.4, limits_detection(x2,y2)[1]*1.4)
    pt.xlabel(x_label[1])
    pt.ylabel(y_label[1])
    
    pt.subplot(2,2, 3)
    pt.plot(x3,y3, "r.")
    pt.plot([0],[0], "bo")
    pt.xlim(limits_detection(x3,y3)[0]*1.4, limits_detection(x3,y3)[1]*1.4)
    pt.ylim(limits_detection(x3,y3)[0]*1.4, limits_detection(x3,y3)[1]*1.4)
    pt.xlabel(x_label[2])
    pt.ylabel(y_label[2])
    
    pt.savefig(save_name)
    pt.clf()
    pt.cla()
  def plot(self, x, y, filename=None, title=None):
    """Plot scatterplot of variables x and y.
    Default plot filename:

    Args:
      x: str of variable name in self.rows.keys()
      y: str of variable name in self.rows.keys(), x != y
      filename: str of filename of plot image. 
      title: str of plot title
    """
    # Clear pylab plotting canvas (in case it is populated)
    pylab.clf()
    pylab.cla()
    
    DFT_FILENAME = "%s_vs_%s.png" % (x, y)
    DFT_TITLE = "%s versus %s" % (x,y)
    if filename is None:
      filename = DFT_FILENAME
    if title is None:
      title = DFT_TITLE
    try:
      pylab.plot(self.rows[x], self.rows[y], '.')
    except KeyError, e:
      # Try cleaning the variable name
      try:
        pylab.plot(self.rows[clean(x)], self.rows[clean(y)], '.')
      except Exception, e:
        print "Could not plot (%s,%s), exception: %s" % (x,y,e)
        return None
def plot_fft_brams():
    import pylab
    run = True
    
    # turn on live updating
    pylab.ion()
    # plot the power in log scale
    pylab.yscale('log')
    
    # get an initial power spectrum and plot the power lines as rectangles
    fftscope_power = get_fft_brams_power()
    fftscope_power_line=pylab.bar(range(0,numchannels),fftscope_power)
    
    pylab.ylim(1,1000000)
    
    # plot until an interrupt is received
    # for i in range(1,10):
    while(run):
        try:
            # read in a new spectrum
            fftscope_power = get_fft_brams_power()
            
            # update the rectangles based on the new power spectrum
            for j in range(0,numchannels):
                fftscope_power_line[j].set_height(fftscope_power[j])
            #update the plot
            pylab.draw()
        except KeyboardInterrupt:
            run = False
    
    # after stopping the liveupdate leave the plot up until the user is done with it
    raw_input('Press enter to quit: ')
            
    pylab.cla() 
Beispiel #30
0
def barGraphTwo(xs1, fs1, xs2, fs2, f, xlabel, ylabel, legend):
	pylab.cla()
	bar1= pylab.bar(xs1, fs1, width=1, facecolor='red')
	bar2=pylab.bar(xs2, fs2, width=1, facecolor='cyan')
	# got from http://stackoverflow.com/questions/2194299/in-matplotlib-how-to-draw-a-bar-graphs-of-multiple-datasets-to-put-smallest-bars
	all_patches = pylab.axes().patches
	patch_at_x = {}
	for patch in all_patches:
		if patch.get_x() not in patch_at_x: patch_at_x[patch.get_x()] = []
		patch_at_x[patch.get_x()].append(patch)

	# custom sort function, in reverse order of height
	def yHeightSort(i,j):
		if j.get_height() > i.get_height(): return 1
		else: return -1

	# loop through sort assign z-order based on sort
	for x_pos, patches in patch_at_x.iteritems():
		if len(patches) == 1: continue
		patches.sort(cmp=yHeightSort)
		[patch.set_zorder(patches.index(patch)) for patch in patches]


	fontP = FontProperties()
	fontP.set_size('small')
	#pylab.xlim([0,24])
	pylab.ylabel(ylabel)
	pylab.xlabel(xlabel)
	pylab.legend((bar2[0], bar1[0]), legend, prop = fontP)
	pylab.savefig(f)
Beispiel #31
0
#ys = testLattice.coords[:,1]
#pylab.sca(ax)
#pylab.scatter(xs, ys ,c =  'goldenrod', s = 34, marker = 'o', edgecolors = 'k', zorder = 5)
#
#
#ax.set_aspect('equal')
#ax.axis('off')
#pylab.title('checking vertex dictionary : ' + testLattice.name)
#fig1.set_size_inches(5, 5)
#pylab.tight_layout()
#pylab.show()

fig5 = pylab.figure(5)
pylab.clf()
ax = pylab.subplot(1, 2, 1)
pylab.cla()
testLattice.draw_resonator_lattice(ax,
                                   color='mediumblue',
                                   alpha=1,
                                   linewidth=2.5)
xs = testLattice.coords[:, 0]
ys = testLattice.coords[:, 1]
pylab.sca(ax)
pylab.scatter(xs,
              ys,
              c='goldenrod',
              s=30,
              marker='o',
              edgecolors='k',
              zorder=5)
ax.set_aspect('equal')
Beispiel #32
0
def logplotter(X,Y,z,ticks,colors):
    pl.cla()
    pl.contourf(X,Y,z.T,ticks,colors=colors,locator=ticker.LogLocator())
    pl.contour(X,Y,z,ticks,colors="black")#colors)#,locator=ticker.LogLocator())
    pl.tick_params(axis='both',bottom='off',left='off',labelbottom='off',labelleft='off',right='off',top='off')
Beispiel #33
0
def logplotter_spheres(X,Y,z,ticks,colors,pos,atombounds,atoms,atomcolors):
    pl.cla()
    pl.contourf(X,Y,z.T,ticks,colors=colors,locator=ticker.LogLocator())
    pl.contour(X,Y,z.T,ticks,colors="black",linestyles='dotted')
    plotspheres(pos,atombounds,atoms,atomcolors)
    pl.tick_params(axis='both',bottom='off',left='off',labelbottom='off',labelleft='off',right='off',top='off')
Beispiel #34
0
def image_spheres(bounds,z,pos,atombounds,atoms,atomcolors):
    pl.cla()
    plotspheres(pos,atombounds,atoms,atomcolors)
    pl.imshow(flipud(z),extent=bounds,vmin=0,vmax=1)
def drawPlots():
    global plots, theWorld, time, times, num_households

    if plots == None or plots.canvas.manager.window == None:
        plots = PL.figure(2)
        PL.ion()
    PL.figure(2)
    PL.hold(True)
    PL.subplot(4, 3, 1)
    PL.cla()
    tot = theWorld.food_shared_totals
    food_shared_avg = [tot[i] / (i + 1) for i in range(len(tot))]
    PL.plot(food_shared_avg, color='black')
    PL.plot(theWorld.food_shared, color='pink')
    PL.plot(theWorld.brn_sharing, color='brown')
    PL.plot(theWorld.grn_sharing, color='green')
    PL.title("Food shared each year (BRN, GRN, tot) and average")
    PL.hold(True)
    PL.subplot(4, 3, 2)
    PL.cla()
    interval = 20
    step = 0.5
    b = [-interval + step * i for i in range(2 * int(interval / step) + 1)]
    if theWorld.hh_prestige:
        PL.hist(theWorld.hh_prestige, bins=b, color='brown')
        PL.title("hh count by prestige")
    PL.hold(True)
    PL.subplot(4, 3, 3)
    PL.cla()
    PL.plot(theWorld.populations, color='pink')
    PL.plot(theWorld.avg_pop, color='black')
    PL.plot(theWorld.avg_pop_100, color='blue')
    PL.title("Population, average, and 100-year average")
    PL.hold(True)
    PL.subplot(4, 3, 4)
    PL.cla()
    PL.plot(theWorld.avg_ages, color='pink')
    PL.plot(theWorld.avg_adult_ages, color='red')
    PL.plot(theWorld.avg_hh_age, color='black')
    PL.title(
        "Average household(black), forager(pink), and adult forager age (red) at end"
    )
    PL.hold(True)
    PL.subplot(4, 3, 5)
    PL.cla()
    interval = (W.max_founder_kin_span - W.min_founder_kin_span)
    step = 0.1
    b = [(W.min_founder_kin_span + step * i)
         for i in range(int(interval / step) + 1)]
    if theWorld.kinship_spans:
        PL.hist(theWorld.kinship_spans, bins=b, color='blue')
        #     PL.axis()
        PL.title("population count by kinship span")
    PL.hold(True)
    PL.subplot(4, 3, 6)
    PL.cla()
    interval = (W.max_founder_kin_span - W.min_founder_kin_span)
    step = 1
    b = [(W.min_founder_kin_span + step * i)
         for i in range(int(interval / step) + 1)]
    if theWorld.kinship_spans:
        PL.hist(theWorld.kinship_spans, bins=b, color='blue')
        #     PL.axis()
        PL.title("population count by kinship span")
    PL.hold(True)
    PL.subplot(4, 3, 7)
    PL.cla()
    interval = 10
    step = 0.05
    b = [step * i for i in range(int(interval / step) + 1)]
    if theWorld.hh_food_stored:
        PL.hist(theWorld.hh_food_stored, bins=b, color='cyan')
        #     PL.axis()
        PL.title("hh counts by food stored")
    PL.hold(True)
    PL.subplot(4, 3, 8)
    PL.cla()
    interval = max(theWorld.pop_expertise) - min(theWorld.pop_expertise)
    step = 0.05
    b = [
        min(theWorld.pop_expertise) + step * i
        for i in range(int(interval / step) + 1)
    ]
    if theWorld.pop_expertise:
        PL.hist(theWorld.pop_expertise, bins=b, color='cyan')
        #     PL.axis()
        PL.title("population counts by foraging expertise")
    PL.hold(True)
    PL.subplot(4, 3, 9)
    PL.cla()
    PL.plot(theWorld.median_storage, color='black')
    PL.title("Median food stored")
    PL.hold(True)
    PL.subplot(4, 3, 10)
    PL.cla()
    PL.plot(theWorld.hoover, color='black')
    PL.title("Hoover index")
    PL.hold(True)
    PL.subplot(4, 3, 10)
    PL.cla()
    PL.plot(theWorld.avg_food_stored, color='black')
    PL.title("average food stored")
    PL.hold(False)
    plots.tight_layout()
    plots.canvas.manager.window.update()
    PL.figure(1)
def performFeatureTracking(template_size,
                           search_area,
                           initCooTemplate,
                           templateImage,
                           searchImage,
                           shiftSearchArea,
                           performLSM=True,
                           lsm_buffer=3,
                           thresh=0.001,
                           subpixel=False,
                           plot_result=False):
    #template_size: np.array([template_width, template_height])
    #search_area: np.array([search_area_x_CC, search_area_y_CC])
    #initCooTemplate: np.array([x,y])
    #shiftSearchArea: np.array([shiftFromCenter_x, shiftFromCenter_y])
    template_width = template_size[0]
    template_height = template_size[1]
    search_area_x = search_area[0]
    search_area_y = search_area[1]
    shiftSearchArea_x = shiftSearchArea[0]
    shiftSearchArea_y = shiftSearchArea[1]

    #check if template sizes even and correct correspondingly
    if int(template_width) % 2 == 0:
        template_width = template_width + 1
    if int(template_height) % 2 == 0:
        template_height = template_height + 1
    if int(search_area_x) % 2 == 0:
        search_area_x = search_area_x + 1
    if int(search_area_y) % 2 == 0:
        search_area_y = search_area_y + 1

    #get patch clip
    if plot_result:
        plt.imshow(templateImage)
        plt.plot(initCooTemplate[0], initCooTemplate[1], "r.", markersize=10)
        plt.waitforbuttonpress()
        plt.cla()
        plt.close('all')

    try:
        patch, _ = getTemplate(templateImage, initCooTemplate, template_width,
                               template_height, True)
    except Exception as e:
        #        _, _, exc_tb = sys.exc_info()
        #        print(e, 'line ' + str(exc_tb.tb_lineno))
        print('template patch reaches border')
        return 1 / 0

    #shift search area to corresponding position considering movement direction
    templateCoo_init_shift = np.array([
        initCooTemplate[0] + shiftSearchArea_x,
        initCooTemplate[1] + shiftSearchArea_y
    ])

    #get lsm search clip
    try:
        search_area, lowerLeftCoo_lsm_search = getTemplate(
            searchImage, templateCoo_init_shift, search_area_x, search_area_y,
            True)

    except Exception as e:
        #        _, _, exc_tb = sys.exc_info()
        #        print(e, 'line ' + str(exc_tb.tb_lineno))
        print('search patch reaches border')
        return 1 / 0

    if plot_result:
        plt.ion()

    CC_xy = crossCorrelation(search_area, patch, lowerLeftCoo_lsm_search,
                             plot_result, subpixel)
    if CC_xy[0] == -999:
        return 1 / 0

    if plot_result:
        plt.close('all')
        print(CC_xy)

    TrackedFeature = CC_xy

    if performLSM:
        #perform least square matching (subpixel accuracy possible)
        try:
            lsm_search, lowerLeftCoo_lsm_search = getTemplate(
                searchImage, CC_xy, search_area_x, search_area_y, True)
        except Exception as e:
            #            _, _, exc_tb = sys.exc_info()
            #            print(e, 'line ' + str(exc_tb.tb_lineno))
            print('lsm patch reaches border')
            return 1 / 0

        if plot_result:
            plt.imshow(lsm_search)
            plt.waitforbuttonpress()
            plt.close('all')

        pointAdjusted_ = pointAdjusted()

        try:
            result_lsm = lsm_matching(patch, lsm_search, pointAdjusted_,
                                      lsm_buffer, thresh)
            print('sigma LSM tracking: ' + str(result_lsm.s0))

            if plot_result:
                plt.imshow(searchImage, cmap='gray')
                plt.plot(result_lsm.y + lowerLeftCoo_lsm_search[0],
                         result_lsm.x + lowerLeftCoo_lsm_search[1],
                         "b.",
                         markersize=10)
                plt.waitforbuttonpress()
                plt.close('all')

            TrackedFeature = np.asarray([result_lsm.x, result_lsm.y])

        except Exception as e:
            #            _, _, exc_tb = sys.exc_info()
            #            print(e, 'line ' + str(exc_tb.tb_lineno))
            print('lsm failed')

    return TrackedFeature
Beispiel #37
0
    for file in files:
        if file.endswith("1.npz"):
            print((os.path.join(root, file)))
            fls.append(os.path.join(root, file))

fls.sort()
for fl in fls:
    print(fl)
    with np.load(fl) as ld:
        print((list(ld.keys())))

        tmpls = ld['template']
        lq, hq = np.percentile(tmpls, [5, 95])
        pl.imshow(tmpls, cmap='gray', vmin=lq, vmax=hq)
        pl.pause(.001)
        pl.cla()
#%%
all_movs = []
for f in fls:
    with np.load(f) as fl:
        print(f)
        #        pl.subplot(1,2,1)
        #        pl.imshow(fl['template'],cmap=pl.cm.gray)
        #        pl.subplot(1,2,2)

        all_movs.append(fl['template'][np.newaxis, :, :])
#        pl.plot(fl['shifts'])
#        pl.pause(.001)
all_movs = cb.movie(np.concatenate(all_movs, axis=0), fr=30)
all_movs, shifts, _, _ = all_movs.motion_correct(20, 20, template=None)
all_movs[30:80].play(backend='opencv', gain=5., fr=10)
Beispiel #38
0
def profilePlot(outpath, base_name, order_num, profile, peak, centroid,
                ext_range, sky_range_top, sky_range_bot, top_bg_mean,
                bot_bg_mean, gaussian, snr):

    pl.figure('spatial profile', facecolor='white')
    pl.cla()
    pl.title('spatial profile, ' + base_name + ', order ' + str(order_num),
             fontsize=14)

    pl.xlabel('relative row (pixels)')
    pl.ylabel('flux (counts)')

    # set axes limits
    yrange = profile.max() - profile.min()
    ymin = profile.min() - (0.1 * yrange)
    ymax = profile.max() + (0.1 * yrange)
    pl.ylim(ymin, ymax)

    # set ticks and grid
    pl.minorticks_on()
    pl.grid(False)

    # plot profile
    pl.plot(profile, "ko-", mfc='none', ms=3.0, linewidth=1)

    # plot vertical line to indicate location of centroid
    pl.plot([centroid, centroid], [ymin, profile.max()],
            "g-",
            linewidth=0.5,
            label='peak')

    # draw extraction window
    wvlh = 0.01 * yrange
    ewh = 0.05 * yrange

    pl.plot((ext_range[0], ext_range[-1]), (profile.max(), profile.max()),
            'r',
            linewidth=0.5,
            label='extraction window')
    pl.plot((ext_range[0], ext_range[0]),
            (profile.max() - wvlh, profile.max() + wvlh),
            'r',
            linewidth=1.5)
    pl.plot((ext_range[-1], ext_range[-1]),
            (profile.max() - wvlh, profile.max() + wvlh),
            'r',
            linewidth=1.5)

    # draw annotation showing centroid, width and SNR
    if gaussian is None:
        width = 'unknown'
    else:
        width = '{:.1f}'.format(abs(gaussian[2]))

    aStr = 'centroid = {:.1f} pixels'.format(centroid)
    aStr = aStr + '\nwidth = {} pixels'.format(width)
    if snr is not None:
        aStr = aStr + '\nSNR = {:.1f}'.format(snr)

    if peak > (len(profile) / 2):
        pl.annotate(aStr, (peak - (len(ext_range) / 2) - 20,
                           ((ymax - ymin) * 3 / 5) + ymin))
    else:
        pl.annotate(aStr, (peak + (len(ext_range) / 2) + 5,
                           ((ymax - ymin) * 3 / 5) + ymin))

    # draw sky windows

    if sky_range_top is not None and len(sky_range_top) > 0:

        pl.plot((sky_range_top[0], sky_range_top[-1]),
                (top_bg_mean, top_bg_mean),
                'b',
                linewidth=0.5,
                label='sky window')
        pl.plot((sky_range_top[0], sky_range_top[0]),
                (top_bg_mean - wvlh, top_bg_mean + wvlh),
                'b',
                linewidth=1.5)
        pl.plot((sky_range_top[-1], sky_range_top[-1]),
                (top_bg_mean - wvlh, top_bg_mean + wvlh),
                'b',
                linewidth=1.5)

    if sky_range_bot is not None and len(sky_range_bot) > 0:

        pl.plot((sky_range_bot[0], sky_range_bot[-1]),
                (bot_bg_mean, bot_bg_mean),
                'b',
                linewidth=0.5)
        pl.plot((sky_range_bot[0], sky_range_bot[0]),
                (bot_bg_mean - wvlh, bot_bg_mean + wvlh),
                'b',
                linewidth=1.5)
        pl.plot((sky_range_bot[-1], sky_range_bot[-1]),
                (bot_bg_mean - wvlh, bot_bg_mean + wvlh),
                'b',
                linewidth=1.5)

    # draw best fit Gaussian
    if gaussian is not None:
        min = np.amin(profile)
        if min < 0:
            offset = 0
        else:
            offset = min
        pl.plot(image_lib.gaussian(range(len(profile)), gaussian[0],
                                   gaussian[1], gaussian[2]) + offset,
                'k--',
                linewidth=0.5,
                label='Gaussian fit')

    pl.legend(loc='best', prop={'size': 8})

    fn = constructFileName(outpath, base_name, order_num, 'profile.png')
    pl.savefig(fn)
    pl.close()
    log_fn(fn)
    return
Beispiel #39
0
def main():
    pass  # For compatibility between running under Spyder and the CLI

    #%%  download and list all files to be processed

    # folder inside ./example_movies where files will be saved
    fld_name = 'Mesoscope'
    download_demo('Tolias_mesoscope_1.hdf5', fld_name)
    download_demo('Tolias_mesoscope_2.hdf5', fld_name)
    download_demo('Tolias_mesoscope_3.hdf5', fld_name)

    # folder where files are located
    folder_name = os.path.join(caiman_datadir(), 'example_movies', fld_name)
    extension = 'hdf5'  # extension of files
    # read all files to be processed
    fls = glob.glob(folder_name + '/*' + extension)

    # your list of files should look something like this
    print(fls)

    #%%   Set up some parameters

    # frame rate (Hz)
    fr = 15
    # approximate length of transient event in seconds
    decay_time = 0.5
    # expected half size of neurons
    gSig = (3, 3)
    # order of AR indicator dynamics
    p = 1
    # minimum SNR for accepting new components
    min_SNR = 2.5
    # correlation threshold for new component inclusion
    rval_thr = 0.85
    # spatial downsampling factor (increases speed but may lose some fine structure)
    ds_factor = 1
    # number of background components
    gnb = 2
    # recompute gSig if downsampling is involved
    gSig = tuple(np.ceil(np.array(gSig) / ds_factor).astype('int'))
    # flag for online motion correction
    mot_corr = True
    # maximum allowed shift during motion correction
    max_shift = np.ceil(10. / ds_factor).astype('int')

    # set up some additional supporting parameters needed for the algorithm (these are default values but change according to dataset characteristics)

    # number of shapes to be updated each time (put this to a finite small value to increase speed)
    max_comp_update_shape = np.inf
    # number of files used for initialization
    init_files = 1
    # number of files used for online
    online_files = len(fls) - 1
    # number of frames for initialization (presumably from the first file)
    initbatch = 200
    # maximum number of expected components used for memory pre-allocation (exaggerate here)
    expected_comps = 300
    # initial number of components
    K = 2
    # number of timesteps to consider when testing new neuron candidates
    N_samples = np.ceil(fr * decay_time)
    # exceptionality threshold
    thresh_fitness_raw = scipy.special.log_ndtr(-min_SNR) * N_samples
    # number of passes over the data
    epochs = 2
    # upper bound for number of frames in each file (used right below)
    len_file = 1000
    # total length of all files (if not known use a large number, then truncate at the end)
    T1 = len(fls) * len_file * epochs

    #%%    Initialize movie

    # load only the first initbatch frames and possibly downsample them
    if ds_factor > 1:
        Y = cm.load(fls[0], subindices=slice(0, initbatch, None)).astype(
            np.float32).resize(1. / ds_factor, 1. / ds_factor)
    else:
        Y = cm.load(fls[0], subindices=slice(0, initbatch,
                                             None)).astype(np.float32)

    if mot_corr:  # perform motion correction on the first initbatch frames
        mc = Y.motion_correct(max_shift, max_shift)
        Y = mc[0].astype(np.float32)
        borders = np.max(mc[1])
    else:
        Y = Y.astype(np.float32)

    # minimum value of movie. Subtract it to make the data non-negative
    img_min = Y.min()
    Y -= img_min
    img_norm = np.std(Y, axis=0)
    # normalizing factor to equalize the FOV
    img_norm += np.median(img_norm)
    Y = Y / img_norm[None, :, :]  # normalize data

    _, d1, d2 = Y.shape
    dims = (d1, d2)  # dimensions of FOV
    Yr = Y.to_2D().T  # convert data into 2D array

    Cn_init = Y.local_correlations(swap_dim=False)  # compute correlation image
    #pl.imshow(Cn_init)
    #pl.title('Correlation Image on initial batch')
    #pl.colorbar()

    bnd_Y = np.percentile(Y, (0.001, 100 - 0.001))  # plotting boundaries for Y

    #%% initialize OnACID with bare initialization

    cnm_init = bare_initialization(Y[:initbatch].transpose(1, 2, 0),
                                   init_batch=initbatch,
                                   k=K,
                                   gnb=gnb,
                                   gSig=gSig,
                                   p=p,
                                   minibatch_shape=100,
                                   minibatch_suff_stat=5,
                                   update_num_comps=True,
                                   rval_thr=rval_thr,
                                   thresh_fitness_raw=thresh_fitness_raw,
                                   batch_update_suff_stat=True,
                                   max_comp_update_shape=max_comp_update_shape,
                                   deconv_flag=False,
                                   use_dense=False,
                                   simultaneously=False,
                                   n_refit=0,
                                   max_num_added=3,
                                   min_num_trial=3,
                                   sniper_mode=False,
                                   use_peak_max=False,
                                   expected_comps=expected_comps)

    #%% Plot initialization results

    crd = plot_contours(cnm_init.estimates.A.tocsc(), Cn_init, thr=0.9)
    A, C, b, f, YrA, sn = cnm_init.estimates.A, cnm_init.estimates.C, cnm_init.estimates.b, cnm_init.estimates.f, \
                          cnm_init.estimates.YrA, cnm_init.estimates.sn
    view_patches_bar(Yr,
                     scipy.sparse.coo_matrix(A.tocsc()[:, :]),
                     C[:, :],
                     b,
                     f,
                     dims[0],
                     dims[1],
                     YrA=YrA[:, :],
                     img=Cn_init)

    bnd_AC = np.percentile(A.dot(C), (0.001, 100 - 0.005))
    bnd_BG = np.percentile(b.dot(f), (0.001, 100 - 0.001))

    #%% create a function for plotting results in real time if needed

    def create_frame(cnm2, img_norm, captions):
        cnm2_est = cnm2.estimates
        A, b = cnm2_est.Ab[:, gnb:], cnm2_est.Ab[:, :gnb].toarray()
        C, f = cnm2_est.C_on[gnb:cnm2.M, :], cnm2_est.C_on[:gnb, :]
        # inferred activity due to components (no background)
        frame_plot = (frame_cor.copy() - bnd_Y[0]) / np.diff(bnd_Y)
        comps_frame = A.dot(C[:, t - 1]).reshape(cnm2.dims, order='F')
        bgkrnd_frame = b.dot(f[:, t - 1]).reshape(
            cnm2.dims, order='F')  # denoised frame (components + background)
        denoised_frame = comps_frame + bgkrnd_frame
        denoised_frame = (denoised_frame.copy() - bnd_Y[0]) / np.diff(bnd_Y)
        comps_frame = (comps_frame.copy() - bnd_AC[0]) / np.diff(bnd_AC)

        if show_residuals:
            #all_comps = np.reshape(cnm2.Yres_buf.mean(0), cnm2.dims, order='F')
            all_comps = np.reshape(cnm2_est.mean_buff, cnm2.dims, order='F')
            all_comps = np.minimum(np.maximum(all_comps, 0) * 2 + 0.25, 255)
        else:
            all_comps = np.array(A.sum(-1)).reshape(cnm2.dims, order='F')
            # spatial shapes
        frame_comp_1 = cv2.resize(
            np.concatenate([frame_plot, all_comps * 1.], axis=-1),
            (2 * np.int(cnm2.dims[1] * resize_fact),
             np.int(cnm2.dims[0] * resize_fact)))
        frame_comp_2 = cv2.resize(
            np.concatenate([comps_frame, denoised_frame], axis=-1),
            (2 * np.int(cnm2.dims[1] * resize_fact),
             np.int(cnm2.dims[0] * resize_fact)))
        frame_pn = np.concatenate([frame_comp_1, frame_comp_2], axis=0).T
        vid_frame = np.repeat(frame_pn[:, :, None], 3, axis=-1)
        vid_frame = np.minimum((vid_frame * 255.), 255).astype('u1')

        if show_residuals and cnm2_est.ind_new:
            add_v = np.int(cnm2.dims[1] * resize_fact)
            for ind_new in cnm2_est.ind_new:
                cv2.rectangle(vid_frame,
                              (int(ind_new[0][1] * resize_fact),
                               int(ind_new[1][1] * resize_fact) + add_v),
                              (int(ind_new[0][0] * resize_fact),
                               int(ind_new[1][0] * resize_fact) + add_v),
                              (255, 0, 255), 2)

        cv2.putText(vid_frame,
                    captions[0], (5, 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    captions[1], (np.int(cnm2.dims[0] * resize_fact) + 5, 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    captions[2], (5, np.int(cnm2.dims[1] * resize_fact) + 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    captions[3], (np.int(cnm2.dims[0] * resize_fact) + 5,
                                  np.int(cnm2.dims[1] * resize_fact) + 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 0),
                    thickness=1)
        cv2.putText(vid_frame,
                    'Frame = ' + str(t),
                    (vid_frame.shape[1] // 2 - vid_frame.shape[1] // 10,
                     vid_frame.shape[0] - 20),
                    fontFace=5,
                    fontScale=0.8,
                    color=(0, 255, 255),
                    thickness=1)
        return vid_frame

#%% Prepare object for OnACID

    cnm2 = deepcopy(cnm_init)

    save_init = False  # flag for saving initialization object. Useful if you want to check OnACID with different parameters but same initialization
    if save_init:
        cnm_init.dview = None
        save_object(cnm_init, fls[0][:-4] + '_DS_' + str(ds_factor) + '.pkl')
        cnm_init = load_object(fls[0][:-4] + '_DS_' + str(ds_factor) + '.pkl')

    cnm2._prepare_object(np.asarray(Yr), T1, idx_components=None)

    cnm2.thresh_CNN_noisy = 0.5

    #%% Run OnACID and optionally plot results in real time
    epochs = 1
    cnm2.estimates.Ab_epoch = []  # save the shapes at the end of each epoch
    t = initbatch  # current timestep
    tottime = []
    Cn = Cn_init.copy()

    # flag for removing components with bad shapes
    remove_flag = False
    T_rm = 650  # remove bad components every T_rm frames
    rm_thr = 0.1  # CNN classifier removal threshold
    # flag for plotting contours of detected components at the end of each file
    plot_contours_flag = False
    # flag for showing results video online (turn off flags for improving speed)
    play_reconstr = True
    # flag for saving movie (file could be quite large..)
    save_movie = False
    movie_name = os.path.join(
        folder_name, 'sniper_meso_0.995_new.avi')  # name of movie to be saved
    resize_fact = 1.2  # image resizing factor

    if online_files == 0:  # check whether there are any additional files
        process_files = fls[:init_files]  # end processing at this file
        init_batc_iter = [initbatch]  # place where to start
        end_batch = T1
    else:
        process_files = fls[:init_files + online_files]  # additional files
        # where to start reading at each file
        init_batc_iter = [initbatch] + [0] * online_files

    shifts = []
    show_residuals = True
    if show_residuals:
        caption = 'Mean Residual Buffer'
    else:
        caption = 'Identified Components'
    captions = ['Raw Data', 'Inferred Activity', caption, 'Denoised Data']
    if save_movie and play_reconstr:
        fourcc = cv2.VideoWriter_fourcc('8', 'B', 'P', 'S')
        #    fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(
            movie_name, fourcc, 30.0,
            tuple([int(2 * x * resize_fact) for x in cnm2.dims]))

    for iter in range(epochs):
        if iter > 0:
            # if not on first epoch process all files from scratch
            process_files = fls[:init_files + online_files]
            init_batc_iter = [0] * (online_files + init_files)

        # np.array(fls)[np.array([1,2,3,4,5,-5,-4,-3,-2,-1])]:
        for file_count, ffll in enumerate(process_files):
            print('Now processing file ' + ffll)
            Y_ = cm.load(ffll,
                         subindices=slice(init_batc_iter[file_count], T1,
                                          None))

            # update max-correlation (and perform offline motion correction) just for illustration purposes
            if plot_contours_flag:
                if ds_factor > 1:
                    Y_1 = Y_.resize(1. / ds_factor, 1. / ds_factor, 1)
                else:
                    Y_1 = Y_.copy()
                if mot_corr:
                    templ = (
                        cnm2.estimates.Ab.data[:cnm2.estimates.Ab.indptr[1]] *
                        cnm2.estimates.C_on[0, t - 1]).reshape(
                            cnm2.estimates.dims, order='F') * img_norm
                    newcn = (Y_1 - img_min).motion_correct(
                        max_shift, max_shift,
                        template=templ)[0].local_correlations(swap_dim=False)
                    Cn = np.maximum(Cn, newcn)
                else:
                    Cn = np.maximum(Cn, Y_1.local_correlations(swap_dim=False))

            old_comps = cnm2.N  # number of existing components
            for frame_count, frame in enumerate(Y_):  # now process each file
                if np.isnan(np.sum(frame)):
                    raise Exception('Frame ' + str(frame_count) +
                                    ' contains nan')
                if t % 100 == 0:
                    print(
                        'Epoch: ' + str(iter + 1) + '. ' + str(t) +
                        ' frames have beeen processed in total. ' +
                        str(cnm2.N - old_comps) +
                        ' new components were added. Total number of components is '
                        + str(cnm2.estimates.Ab.shape[-1] - gnb))
                    old_comps = cnm2.N

                t1 = time()  # count time only for the processing part
                frame_ = frame.copy().astype(np.float32)  #
                if ds_factor > 1:
                    frame_ = cv2.resize(frame_,
                                        img_norm.shape[::-1])  # downsampling

                frame_ -= img_min  # make data non-negative

                if mot_corr:  # motion correct
                    templ = cnm2.estimates.Ab.dot(
                        cnm2.estimates.C_on[:cnm2.M, t - 1]).reshape(
                            cnm2.dims, order='F') * img_norm
                    frame_cor, shift = motion_correct_iteration_fast(
                        frame_, templ, max_shift, max_shift)
                    shifts.append(shift)
                else:
                    templ = None
                    frame_cor = frame_

                frame_cor = frame_cor / img_norm  # normalize data-frame
                cnm2.fit_next(t, frame_cor.reshape(
                    -1, order='F'))  # run OnACID on this frame
                # store time
                tottime.append(time() - t1)

                t += 1

                if t % T_rm == 0 and remove_flag:
                    prd, _ = evaluate_components_CNN(
                        cnm2.estimates.Ab[:, gnb:], dims, gSig)
                    ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist()
                    cnm2.remove_components(ind_rem)
                    print('Removing ' + str(len(ind_rem)) + ' components')

                if t % 1000 == 0 and plot_contours_flag:
                    pl.cla()
                    A = cnm2.estimates.Ab[:, gnb:]
                    # update the contour plot every 1000 frames
                    crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
                    pl.pause(1)

                if play_reconstr:  # generate movie with the results
                    vid_frame = create_frame(cnm2, img_norm, captions)
                    if save_movie:
                        out.write(vid_frame)
                        if t - initbatch < 100:
                            #for rp in np.int32(np.ceil(np.exp(-np.arange(1,100)/30)*20)):
                            for rp in range(len(cnm2.estimates.ind_new) * 2):
                                out.write(vid_frame)
                    cv2.imshow('frame', vid_frame)
                    if t - initbatch < 100:
                        for rp in range(len(cnm2.estimates.ind_new) * 2):
                            cv2.imshow('frame', vid_frame)
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break

            print('Cumulative processing speed is ' +
                  str((t - initbatch) / np.sum(tottime))[:5] +
                  ' frames per second.')
        # save the shapes at the end of each epoch
        cnm2.estimates.Ab_epoch.append(cnm2.estimates.Ab.copy())

    if save_movie:
        out.release()
    cv2.destroyAllWindows()

    #%%  save results (optional)
    save_results = False

    if save_results:
        np.savez('results_analysis_online_MOT_CORR.npz',
                 Cn=Cn,
                 Ab=cnm2.estimates.Ab,
                 Cf=cnm2.estimates.C_on,
                 b=cnm2.estimates.b,
                 f=cnm2.estimates.f,
                 dims=cnm2.dims,
                 tottime=tottime,
                 noisyC=cnm2.estimates.noisyC,
                 shifts=shifts)

    #%% extract results from the objects and do some plotting
    A, b = cnm2.estimates.Ab[:, gnb:], cnm2.estimates.Ab[:, :gnb].toarray()
    C, f = cnm2.estimates.C_on[gnb:cnm2.M, t - t //
                               epochs:t], cnm2.estimates.C_on[:gnb, t -
                                                              t // epochs:t]
    noisyC = cnm2.estimates.noisyC[:, t - t // epochs:t]
    b_trace = [osi.b for osi in cnm2.estimates.OASISinstances] if hasattr(
        cnm2, 'OASISinstances') else [0] * C.shape[0]

    pl.figure()
    crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9)
    view_patches_bar(Yr,
                     scipy.sparse.coo_matrix(A.tocsc()[:, :]),
                     C[:, :],
                     b,
                     f,
                     dims[0],
                     dims[1],
                     YrA=noisyC[gnb:cnm2.M] - C,
                     img=Cn)
Beispiel #40
0
def detrend_data(paths, filterCode):
    polyFitRequest = 1  # Currently only works with one or two coefficients

    # Load in list of used files
    fileList = []
    for line in (paths['parent'] / "usedImages.txt").read_text():
        fileList.append(line.strip())

    # Detect Filter Set being used

    fileList = paths['outcatPath'].glob('*diffExcel*csv')
    r = 0
    #logger.debug(fileList)
    for file in fileList:
        photFile = numpy.genfromtxt(file, dtype=float, delimiter=',')
        exists = os.path.isfile(str(file).replace('diff', 'calib'))
        if exists:
            calibFile = numpy.genfromtxt(str(file).replace('diff', 'calib'),
                                         dtype=float,
                                         delimiter=',')
            logger.debug("Calibration difference")
            logger.debug(-(photFile[:, 1] - calibFile[:, 1])[0])
            calibDiff = -((photFile[:, 1] - calibFile[:, 1])[0])
        #logger.debug(photFile[:,1])
        #logger.debug(photFile[:,0])
        logger.debug(file)
        logger.debug(photFile[:, 1])

        baseSubDate = min(photFile[:, 0])
        logger.debug(baseSubDate)
        logger.debug(math.floor(baseSubDate))

        photFile[:, 0] = photFile[:, 0] - baseSubDate

        leftMost = click.prompt("Enter left side most valid date:")
        leftFlat = click.prompt("Enter left side end of flat region:")

        rightFlat = click.prompt("Enter right side start of flat region:")
        rightMost = click.prompt("Enter right side most valid date:")

        # Clip off edges
        clipReject = []
        for i in range(photFile.shape[0]):
            if photFile[i,
                        0] < float(leftMost) or photFile[i,
                                                         0] > float(rightMost):
                clipReject.append(i)
                logger.debug(photFile[i, 1])
                logger.debug("REJECT")
        logger.debug(photFile.shape[0])
        photFile = numpy.delete(photFile, clipReject, axis=0)
        logger.debug(photFile.shape[0])

        # Get an array holding only the flat bits
        transitReject = []
        flatFile = numpy.asarray(photFile)
        for i in range(flatFile.shape[0]):
            if (flatFile[i, 0] > float(leftMost)
                    and flatFile[i, 0] < float(leftFlat)) or (
                        flatFile[i, 0] > float(rightFlat)
                        and flatFile[i, 0] < float(rightMost)):
                logger.debug("Keep")
            else:
                transitReject.append(i)
                logger.debug(flatFile[i, 0])
                logger.debug("REJECT")
        logger.debug(flatFile.shape[0])
        flatFile = numpy.delete(flatFile, transitReject, axis=0)
        logger.debug(flatFile.shape[0])

        #
        polyFit = numpy.polyfit(flatFile[:, 0], flatFile[:, 1], polyFitRequest)
        logger.debug(polyFit)

        #Remove trend from flat bits
        if polyFitRequest == 2:
            for i in range(flatFile.shape[0]):
                flatFile[i, 1] = flatFile[i, 1] - (
                    polyFit[2] + (polyFit[1] * flatFile[i, 0]) +
                    (polyFit[0] * pow(flatFile[i, 0], 2)))
        elif polyFitRequest == 1:
            for i in range(flatFile.shape[0]):
                flatFile[i,
                         1] = flatFile[i, 1] - (polyFit[1] +
                                                (polyFit[0] * flatFile[i, 0]))

        #Remove trend from actual data
        if polyFitRequest == 2:
            for i in range(photFile.shape[0]):
                photFile[i, 1] = photFile[i, 1] - (
                    polyFit[2] + (polyFit[1] * photFile[i, 0]) +
                    (polyFit[0] * pow(photFile[i, 0], 2)))
        elif polyFitRequest == 1:
            for i in range(photFile.shape[0]):
                photFile[i,
                         1] = photFile[i, 1] - (polyFit[1] +
                                                (polyFit[0] * photFile[i, 0]))

        #return basedate to the data
        photFile[:, 0] = photFile[:, 0] + baseSubDate

        # ax=plt.plot(photFile[:,0],photFile[:,1],'ro')
        # plt.gca().invert_yaxis()
        # #logger.debug(ax.lines)
        # plt.show()
        # del ax
        # plt.clf()
        # plt.cla()
        # plt.close()
        # plt.close("all")

        # Output trimmed files
        numpy.savetxt(paths['outcatPath'] /
                      'V{}_diffPeranso.txt'.format(str(r + 1)),
                      photFile,
                      delimiter=" ",
                      fmt='%0.8f')
        numpy.savetxt(paths['outcatPath'] /
                      'V{}_diffExcel.csv'.format(str(r + 1)),
                      photFile,
                      delimiter=",",
                      fmt='%0.8f')

        # Output astroImageJ file
        outputPeransoCalib = []
        #i=0
        for i in range(numpy.asarray(photFile).shape[0]):
            outputPeransoCalib.append(
                [photFile[i][0] - 2450000.0, photFile[i][1], photFile[i][2]])
            #i=i+1

        numpy.savetxt(paths['outcatPath'] /
                      'V{}_diffAIJ.txt'.format(str(r + 1)),
                      outputPeransoCalib,
                      delimiter=" ",
                      fmt='%0.8f')
        numpy.savetxt(paths['outcatPath'] /
                      'V{}_diffAIJ.csv'.format(str(r + 1)),
                      outputPeransoCalib,
                      delimiter=",",
                      fmt='%0.8f')

        # Output replot
        pylab.cla()
        outplotx = photFile[:, 0]
        outploty = photFile[:, 1]
        pylab.xlabel('BJD')
        pylab.ylabel('Differential ' + filterCode + ' Mag')
        pylab.plot(outplotx, outploty, 'bo')
        pylab.ylim(max(outploty) + 0.02, min(outploty) - 0.02, 'k-')
        pylab.xlim(min(outplotx) - 0.01, max(outplotx) + 0.01)
        pylab.grid(True)
        pylab.savefig(paths['outputPath'] /
                      'V{}_EnsembleVarDiffMag.png'.format(str(r + 1)))
        pylab.savefig(paths['outputPath'] /
                      'V{}_EnsembleVarDiffMag.eps'.format(str(r + 1)))
        pylab.cla()
        pylab.clf()
        pylab.close()
        pylab.close("all")

        if exists:
            measureReject = []
            for i in range(calibFile.shape[0]):
                if calibFile[i, 1] < float(brightD) + calibDiff or calibFile[
                        i, 1] > float(brightV) + calibDiff:
                    measureReject.append(i)
                    logger.debug(calibFile[i, 1])
                    logger.debug("REJECT")
            logger.debug(calibFile.shape[0])
            calibFile = numpy.delete(calibFile, measureReject, axis=0)
            logger.debug(calibFile.shape[0])

            # Output trimmed files
            numpy.savetxt(paths['outcatPath'] /
                          'V{}_calibPeranso.txt'.format(str(r + 1)),
                          calibFile,
                          delimiter=" ",
                          fmt='%0.8f')
            numpy.savetxt(paths['outcatPath'] /
                          'V{}_calibExcel.csv'.format(str(r + 1)),
                          calibFile,
                          delimiter=",",
                          fmt='%0.8f')

            # Output astroImageJ file
            outputPeransoCalib = []
            #i=0
            for i in range(numpy.asarray(calibFile).shape[0]):
                outputPeransoCalib.append([
                    calibFile[i][0] - 2450000.0, calibFile[i][1],
                    calibFile[i][2]
                ])
                #i=i+1

            numpy.savetxt(paths['outcatPath'] /
                          'V{}_calibAIJ.txt'.format(str(r + 1)),
                          outputPeransoCalib,
                          delimiter=" ",
                          fmt='%0.8f')
            numpy.savetxt(paths['outcatPath'] /
                          'V{}_calibAIJ.csv'.format(str(r + 1)),
                          outputPeransoCalib,
                          delimiter=",",
                          fmt='%0.8f')
            #r=r+1

        r = r + 1
    return
def crossCorrelation(SearchImg,
                     PatchImg,
                     xyLowerLeft,
                     illustrate=False,
                     subpixel=False):
    #perform template matching with normalized cross correlation (NCC)
    res = cv2.matchTemplate(SearchImg, PatchImg, cv2.TM_CCORR_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(
        res)  #min_loc for TM_SQDIFF
    match_position_x = max_loc[0] + PatchImg.shape[1] / 2
    match_position_y = max_loc[1] + PatchImg.shape[0] / 2
    del min_val, min_loc

    if subpixel:
        #         zoom_factor = 10.0
        #         SearchImg_new, xyLowerLeft_upscale = getTemplate(SearchImg, [match_position_x, match_position_y], PatchImg.shape[0]+2, PatchImg.shape[1]+2)
        #         SearchImg_upscale = ndimage.zoom(SearchImg_new, zoom_factor)
        #         PatchImg_upscale = ndimage.zoom(PatchImg, zoom_factor)
        #         res_upscale = cv2.matchTemplate(SearchImg_upscale, PatchImg_upscale, cv2.TM_CCORR_NORMED)
        #         min_val, max_val, min_loc, max_loc_upscale = cv2.minMaxLoc(res_upscale) #min_loc for TM_SQDIFF
        #         match_position_x_upscale = np.float((max_loc_upscale[0] + PatchImg_upscale.shape[1]/2)) / zoom_factor
        #         match_position_y_upscale = np.float((max_loc_upscale[1] + PatchImg_upscale.shape[0]/2)) / zoom_factor
        #
        #         match_position_x = match_position_x_upscale + xyLowerLeft_upscale[0]
        #         match_position_y = match_position_y_upscale + xyLowerLeft_upscale[1]
        #
        #         if illustrate:
        #             plt.subplot(131),plt.imshow(res_upscale,cmap = 'gray')
        #             plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
        #             plt.plot(match_position_x_upscale*zoom_factor-PatchImg_upscale.shape[1]/2,
        #                      match_position_y_upscale*zoom_factor-PatchImg_upscale.shape[0]/2, "r.", markersize=10)
        #             plt.subplot(132),plt.imshow(SearchImg_upscale,cmap = 'gray')
        #             plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        #             plt.plot(match_position_x_upscale*zoom_factor-3, match_position_y_upscale*zoom_factor+3, "r.", markersize=10)
        #             plt.subplot(133),plt.imshow(PatchImg_upscale,cmap = 'gray')
        #             plt.title('Template'), plt.xticks([]), plt.yticks([])
        #             plt.show()
        #             plt.waitforbuttonpress()
        #             plt.cla()

        #perform subpixel matching with template and search area in frequency domain
        SearchImg_32, _ = getTemplate(SearchImg,
                                      [match_position_x, match_position_y],
                                      PatchImg.shape[0], PatchImg.shape[1])
        SearchImg_32 = np.float32(SearchImg_32)
        PatchImg_32 = np.float32(PatchImg)
        shiftSubpixel, _ = cv2.phaseCorrelate(
            SearchImg_32, PatchImg_32)  #subpixle with fourier transform
        match_position_x = match_position_x - shiftSubpixel[
            0]  #match_position_x - shiftSubpixel[1]
        match_position_y = match_position_y - shiftSubpixel[
            1]  #match_position_y + shiftSubpixel[0]

    if illustrate:
        plt.subplot(131), plt.imshow(res, cmap='gray')
        plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
        plt.plot(match_position_x - PatchImg.shape[1] / 2,
                 match_position_y - PatchImg.shape[0] / 2,
                 "r.",
                 markersize=10)
        plt.subplot(132), plt.imshow(SearchImg, cmap='gray')
        plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        plt.plot(match_position_x, match_position_y, "r.", markersize=10)
        plt.subplot(133), plt.imshow(PatchImg, cmap='gray')
        plt.title('Template'), plt.xticks([]), plt.yticks([])
        plt.show()
        plt.waitforbuttonpress()
        plt.cla()
        plt.close('all')
        print('correlation value: ' + str(max_val))

    del res

    if max_val > 0.9:  #998:
        #keep only NCC results with high correlation values
        xyMatched = np.asarray([
            match_position_x + xyLowerLeft[0],
            match_position_y + xyLowerLeft[1]
        ],
                               dtype=np.float32)
        return xyMatched

    else:
        print('NCC matching not successful')
        return [-999, -999]
Beispiel #42
0
            section_dict[section].append(i)
        except KeyError:
            section_dict[section] = [i]
# Now find any sub-sequences found in both sequences
matches = set(dict_SirC).intersection(dict_mutant)
print("%i unique matches found in the two sequences for the dot plot" % len(matches))
# Create lists of x and y co-ordinates for scatter plot
x = []
y = []
for section in matches:
    for i in dict_SirC[section]:
        for j in dict_mutant[section]:
            x.append(i)
            y.append(j)

pylab.cla()  # clear any prior graph
pylab.gray()
pylab.scatter(x, y)
pylab.xlim(0, len(SirC) - window)
pylab.ylim(0, len(mutant) - window)
pylab.xlabel("%s (length %i bp)" % (SirC.id, len(SirC)))
pylab.ylabel("%s (length %i bp)" % (mutant.id, len(mutant)))
pylab.title("Dot plot using window size %i\n(allowing no mis-matches)" % window)
pylab.savefig("dot plot.png")
pylab.close()

#### HTML output ####

def openHTML(f,title):
    f.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
Beispiel #43
0
def plot_clustering(data, target_labels, cut='none', pre_processing=''):
    """Plots 3d KMeans clustering from the first three indexed features used
    ARGS
      data: array-like or sparse matrix, shape=(n_samples, n_features)
      target_labels: labels of data points <number array>
      cut: suffix to filename to indicate if data was prunned <str>
      pre_processing: suffix to filename to indicate pre-processig applied <str>
  """
    n_samples, n_features = data.shape

    labels = np.unique(target_labels)
    n_labels = len(labels)
    sample_size = 300

    pca = PCA(n_components=n_labels).fit(data)

    estimators = {
        'k-means++': KMeans(init='k-means++',
                            n_clusters=n_labels,
                            n_init=n_init),
        'random': KMeans(init='random', n_clusters=n_labels, n_init=n_init),
        'pca': KMeans(init=pca.components_, n_clusters=n_labels, n_init=1)
    }

    fig_num = 1
    for name, estimator in estimators.items():
        fig = plt.figure(fig_num, figsize=(4, 3))
        plt.clf()
        ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

        plt.cla()
        estimator.fit(data)
        estimated_labels = estimator.labels_

        p = ax.scatter(data[:, 0],
                       data[:, 1],
                       data[:, 2],
                       c=estimated_labels.astype(np.float))

        ax.set_xlabel('feature-1')
        ax.set_ylabel('feature-2')
        ax.set_zlabel('feature-3')
        fig.suptitle(name)
        fig_num = fig_num + 1
        fig.colorbar(p)
        plt.show()
        plt.savefig(
            'knn_clustering_<%s>classes<%d>cut<%s>pre_processing<%s>fft<1-2-3>'
            % (name, n_labels, cut, pre_processing))

    # Plot the ground truth
    fig = plt.figure(fig_num, figsize=(4, 3))

    plt.clf()
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

    plt.cla()

    #add labels to ground truth
    for idx in range(len(labels)):
        label = labels[idx]
        color = float(idx) / len(labels)
        ax.text3D(data[target_labels == label, 0].mean(),
                  data[target_labels == label, 1].mean(),
                  data[target_labels == label, 2].mean(),
                  'lbl ' + str(label),
                  horizontalalignment='center',
                  bbox=dict(alpha=.7, edgecolor='r', facecolor='w'))
        ax.scatter(data[target_labels == label, 0],
                   data[target_labels == label, 1],
                   data[target_labels == label, 2],
                   c=str(color))

    ax.set_xlabel('feature-1')
    ax.set_ylabel('feature-2')
    ax.set_zlabel('feature-3')
    fig.suptitle("Ground truth")
    plt.show()
    plt.savefig(
        'knn_clustering<%s>classes<%d>cut<%s>pre_processing<%s>mfccs<1-2-3>' %
        ('truth', n_labels, cut, pre_processing))
Beispiel #44
0
                coordinates = []
                for xc in range(0,(544-(patchsize[0])), 32):
                    for yc in range(0, (544-(patchsize[0])), 32):
                        coordinates.append((xc,yc))

                for loci in range(patchsize[1]):
                    for patchx in range(patchsize[0]):
                        for patchy in range(patchsize[0]):
                            probmap[coordinates[loci][0]+patchx, coordinates[loci][1]+patchy, loci] = (origProb-(patches['corProb'][loci]))/(origProb+(patches['corProb'][loci]))

                meanarray = np.nanmean(probmap, axis=2)
                meanmap = pd.DataFrame(meanarray)

                if not os.path.exists(os.path.join('/media/noor/DataNS/Onderzoek/Projects/Backgroundtypicality/Maps/patch'+ str(patchsize[0]) + '/', imID.rsplit('/')[-3], imID.rsplit('/')[-2])):
                    os.makedirs(os.path.join('/media/noor/DataNS/Onderzoek/Projects/Backgroundtypicality/Maps/patch' + str(patchsize[0]) + '/', imID.rsplit('/')[-3], imID.rsplit('/')[-2]))


                meanmap.to_csv(os.path.join('/media/noor/DataNS/Onderzoek/Projects/Backgroundtypicality/Maps/patch' + str(patchsize[0]) + '/',(imID.rsplit('/')[-3]), imID.rsplit('/')[-2], imID[-20:]+ '_probmap_'+resnet+'.csv'))

                im = plt.imshow(meanmap, cmap='viridis')
                plt.axis('equal')
                text(3, 3,str(origProb))
                plt.colorbar(im, orientation='vertical')
                plt.clim(-1,1)
                plt.title('ImgID: %s' % imID)
                if not os.path.exists(os.path.join('/media/noor/DataNS/Onderzoek/Projects/Backgroundtypicality/Maps/Plot/patch'+ str(patchsize[0]) + '/', imID.rsplit('/')[-3], imID.rsplit('/')[-2])):
                    os.makedirs(os.path.join('/media/noor/DataNS/Onderzoek/Projects/Backgroundtypicality/Maps/Plot/patch' + str(patchsize[0]) + '/', imID.rsplit('/')[-3], imID.rsplit('/')[-2]))

                plt.savefig(os.path.join('/media/noor/DataNS/Onderzoek/Projects/Backgroundtypicality/Maps/Plot/patch' + str(patchsize[0]) +'/',(imID.rsplit('/')[-3]),imID.rsplit('/')[-2],imID[-20:]+'_probmap_'+resnet+'.jpg'))   # save the figure to file
                plt.clf() ; plt.cla()
Beispiel #45
0
    def write_spectrum(self, row, col, states, meas, geom):
        """Write data from a single inversion to all output buffers."""

        self.writes = self.writes + 1

        if len(states) == 0:

            # Write a bad data flag
            atm_bad = s.zeros(len(self.fm.statevec)) * -9999.0
            state_bad = s.zeros(len(self.fm.statevec)) * -9999.0
            data_bad = s.zeros(self.fm.instrument.n_chan) * -9999.0
            to_write = {
                'estimated_state_file': state_bad,
                'estimated_reflectance_file': data_bad,
                'estimated_emission_file': data_bad,
                'modeled_radiance_file': data_bad,
                'apparent_reflectance_file': data_bad,
                'path_radiance_file': data_bad,
                'simulated_measurement_file': data_bad,
                'algebraic_inverse_file': data_bad,
                'atmospheric_coefficients_file': atm_bad,
                'radiometry_correction_file': data_bad,
                'spectral_calibration_file': data_bad,
                'posterior_uncertainty_file': state_bad
            }

        else:

            # The inversion returns a list of states, which are
            # intepreted either as samples from the posterior (MCMC case)
            # or as a gradient descent trajectory (standard case). For
            # gradient descent the last spectrum is the converged solution.
            if self.iv.method == 'MCMC':
                state_est = states.mean(axis=0)
            else:
                state_est = states[-1, :]

            # Spectral calibration
            wl, fwhm = self.fm.calibration(state_est)
            cal = s.column_stack(
                [s.arange(0, len(wl)), wl / 1000.0, fwhm / 1000.0])

            # If there is no actual measurement, we use the simulated version
            # in subsequent calculations.  Naturally in these cases we're
            # mostly just interested in the simulation result.
            if meas is None:
                meas = self.fm.calc_rdn(state_est, geom)

            # Rodgers diagnostics
            lamb_est, meas_est, path_est, S_hat, K, G = \
                self.iv.forward_uncertainty(state_est, meas, geom)

            # Simulation with noise
            meas_sim = self.fm.instrument.simulate_measurement(meas_est, geom)

            # Algebraic inverse and atmospheric optical coefficients
            x_surface, x_RT, x_instrument = self.fm.unpack(state_est)
            rfl_alg_opt, Ls, coeffs = invert_algebraic(self.fm.surface,
                                                       self.fm.RT, self.fm.instrument, x_surface, x_RT,
                                                       x_instrument, meas, geom)
            rhoatm, sphalb, transm, solar_irr, coszen, transup = coeffs
            atm = s.column_stack(list(coeffs[:4]) +
                                 [s.ones((len(wl), 1)) * coszen])

            # Upward emission & glint and apparent reflectance
            Ls_est = self.fm.calc_Ls(state_est, geom)
            apparent_rfl_est = lamb_est + Ls_est

            # Radiometric calibration
            factors = s.ones(len(wl))
            if 'radiometry_correction_file' in self.outfiles:
                if 'reference_reflectance_file' in self.infiles:
                    reference_file = self.infiles['reference_reflectance_file']
                    self.rfl_ref = reference_file.read_spectrum(row, col)
                    self.wl_ref = reference_file.wl
                    w, fw = self.fm.instrument.calibration(x_instrument)
                    resamp = resample_spectrum(self.rfl_ref, self.wl_ref,
                                               w, fw, fill=True)
                    meas_est = self.fm.calc_meas(state_est, geom, rfl=resamp)
                    factors = meas_est / meas
                else:
                    logging.warning('No reflectance reference')

            # Assemble all output products
            to_write = {
                'estimated_state_file': state_est,
                'estimated_reflectance_file': s.column_stack((self.fm.surface.wl, lamb_est)),
                'estimated_emission_file': s.column_stack((self.fm.surface.wl, Ls_est)),
                'estimated_reflectance_file': s.column_stack((self.fm.surface.wl, lamb_est)),
                'modeled_radiance_file': s.column_stack((wl, meas_est)),
                'apparent_reflectance_file': s.column_stack((self.fm.surface.wl, apparent_rfl_est)),
                'path_radiance_file': s.column_stack((wl, path_est)),
                'simulated_measurement_file': s.column_stack((wl, meas_sim)),
                'algebraic_inverse_file': s.column_stack((self.fm.surface.wl, rfl_alg_opt)),
                'atmospheric_coefficients_file': atm,
                'radiometry_correction_file': factors,
                'spectral_calibration_file': cal,
                'posterior_uncertainty_file': s.sqrt(s.diag(S_hat))
            }

        for product in self.outfiles:
            logging.debug('IO: Writing '+product)
            self.outfiles[product].write_spectrum(row, col, to_write[product])
            if (self.writes % flush_rate) == 0:
                self.outfiles[product].flush_buffers()

        # Special case! samples file is matlab format.
        if 'mcmc_samples_file' in self.output:
            logging.debug('IO: Writing mcmc_samples_file')
            mdict = {'samples': states}
            s.io.savemat(self.output['mcmc_samples_file'], mdict)

        # Special case! Data dump file is matlab format.
        if 'data_dump_file' in self.output:

            logging.debug('IO: Writing data_dump_file')
            x = state_est
            Seps_inv, Seps_inv_sqrt = self.iv.calc_Seps(x, meas, geom)
            meas_est_window = meas_est[self.iv.winidx]
            meas_window = meas[self.iv.winidx]
            xa, Sa, Sa_inv, Sa_inv_sqrt = self.iv.calc_prior(x, geom)
            prior_resid = (x - xa).dot(Sa_inv_sqrt)
            rdn_est = self.fm.calc_rdn(x, geom)
            x_surface, x_RT, x_instrument = self.fm.unpack(x)
            Kb = self.fm.Kb(x, geom)
            xinit = invert_simple(self.fm, meas, geom)
            Sy = self.fm.instrument.Sy(meas, geom)
            cost_jac_prior = s.diagflat(x - xa).dot(Sa_inv_sqrt)
            cost_jac_meas = Seps_inv_sqrt.dot(K[self.iv.winidx, :])
            meas_Cov = self.fm.Seps(x, meas, geom)
            lamb_est, meas_est, path_est, S_hat, K, G = \
                self.iv.forward_uncertainty(state_est, meas, geom)
            A = s.matmul(K, G)

            # Form the MATLAB dictionary object and write to file
            mdict = {
                'K': K,
                'G': G,
                'S_hat': S_hat,
                'prior_mu': xa,
                'Ls': Ls,
                'prior_Cov': Sa,
                'meas': meas,
                'rdn_est': rdn_est,
                'x': x,
                'x_surface': x_surface,
                'x_RT': x_RT,
                'x_instrument': x_instrument,
                'meas_Cov': meas_Cov,
                'wl': wl,
                'fwhm': fwhm,
                'lamb_est': lamb_est,
                'coszen': coszen,
                'cost_jac_prior': cost_jac_prior,
                'Kb': Kb,
                'A': A,
                'cost_jac_meas': cost_jac_meas,
                'winidx': self.iv.winidx,
                'prior_resid': prior_resid,
                'noise_Cov': Sy,
                'xinit': xinit,
                'rhoatm': rhoatm,
                'sphalb': sphalb,
                'transm': transm,
                'solar_irr': solar_irr
            }
            s.io.savemat(self.output['data_dump_file'], mdict)

        # Write plots, if needed
        if len(states) > 0 and 'plot_directory' in self.output:

            if 'reference_reflectance_file' in self.infiles:
                reference_file = self.infiles['reference_reflectance_file']
                self.rfl_ref = reference_file.read_spectrum(row, col)
                self.wl_ref = reference_file.wl

            for i, x in enumerate(states):

                # Calculate intermediate solutions
                lamb_est, meas_est, path_est, S_hat, K, G = \
                    self.iv.forward_uncertainty(state_est, meas, geom)

                plt.cla()
                red = [0.7, 0.2, 0.2]
                wl, fwhm = self.fm.calibration(x)
                xmin, xmax = min(wl), max(wl)
                fig = plt.subplots(1, 2, figsize=(10, 5))
                plt.subplot(1, 2, 1)
                meas_est = self.fm.calc_meas(x, geom)
                for lo, hi in self.iv.windows:
                    idx = s.where(s.logical_and(wl > lo, wl < hi))[0]
                    p1 = plt.plot(wl[idx], meas[idx], color=red, linewidth=2)
                    p2 = plt.plot(wl, meas_est, color='k', linewidth=1)
                plt.title("Radiance")
                plt.title("Measurement (Scaled DN)")
                ymax = max(meas)*1.25
                ymax = max(meas)+0.01
                ymin = min(meas)-0.01
                plt.text(500, ymax*0.92, "Measured", color=red)
                plt.text(500, ymax*0.86, "Model", color='k')
                plt.ylabel(r"$\mu$W nm$^{-1}$ sr$^{-1}$ cm$^{-2}$")
                plt.ylabel("Intensity")
                plt.xlabel("Wavelength (nm)")
                plt.ylim([-0.001, ymax])
                plt.ylim([ymin, ymax])
                plt.xlim([xmin, xmax])

                plt.subplot(1, 2, 2)
                lamb_est = self.fm.calc_lamb(x, geom)
                ymax = min(max(lamb_est)*1.25, 0.10)
                for lo, hi in self.iv.windows:

                    # black line
                    idx = s.where(s.logical_and(wl > lo, wl < hi))[0]
                    p2 = plt.plot(wl[idx], lamb_est[idx], 'k', linewidth=2)
                    ymax = max(max(lamb_est[idx]*1.2), ymax)

                    # red line
                    if 'reference_reflectance_file' in self.infiles:
                        idx = s.where(s.logical_and(
                            self.wl_ref > lo, self.wl_ref < hi))[0]
                        p1 = plt.plot(self.wl_ref[idx], self.rfl_ref[idx],
                                      color=red, linewidth=2)
                        ymax = max(max(self.rfl_ref[idx]*1.2), ymax)

                    # green and blue lines - surface components
                    if hasattr(self.fm.surface, 'components') and \
                            self.output['plot_surface_components']:
                        idx = s.where(s.logical_and(self.fm.surface.wl > lo,
                                                    self.fm.surface.wl < hi))[0]
                        p3 = plt.plot(self.fm.surface.wl[idx],
                                      self.fm.xa(x, geom)[idx], 'b', linewidth=2)
                        for j in range(len(self.fm.surface.components)):
                            z = self.fm.surface.norm(
                                lamb_est[self.fm.surface.idx_ref])
                            mu = self.fm.surface.components[j][0] * z
                            plt.plot(self.fm.surface.wl[idx], mu[idx], 'g:',
                                     linewidth=1)
                plt.text(500, ymax*0.86, "Remote estimate", color='k')
                if 'reference_reflectance_file' in self.infiles:
                    plt.text(500, ymax*0.92, "In situ reference", color=red)
                if hasattr(self.fm.surface, 'components') and \
                        self.output['plot_surface_components']:
                    plt.text(500, ymax*0.80, "Prior mean state ",
                             color='b')
                    plt.text(500, ymax*0.74, "Surface components ",
                             color='g')
                plt.ylim([-0.0010, ymax])
                plt.xlim([xmin, xmax])
                plt.title("Reflectance")
                plt.title("Source Model")
                plt.xlabel("Wavelength (nm)")
                fn = self.output['plot_directory'] + ('/frame_%i.png' % i)
                plt.savefig(fn)
                plt.close()
Beispiel #46
0
def skyLinesPlot(outpath, order):
    """
    Always uses frame A.
    """

    pl.figure('sky lines', facecolor='white', figsize=(8, 6))
    pl.cla()
    pl.suptitle("sky lines" + ', ' + order.baseNames['A'] + ", order " +
                str(order.flatOrder.orderNum),
                fontsize=14)
    #     pl.rcParams['ytick.labelsize'] = 8

    syn_plot = pl.subplot(2, 1, 1)
    syn_plot.set_title('synthesized sky')
    syn_plot.set_xlim([0, 1024])
    ymin = np.amin(order.synthesizedSkySpec) - (
        (np.amax(order.synthesizedSkySpec) - np.amin(order.synthesizedSkySpec))
        * 0.1)
    ymax = np.amax(order.synthesizedSkySpec) + (
        (np.amax(order.synthesizedSkySpec) - np.amin(order.synthesizedSkySpec))
        * 0.1)
    syn_plot.set_ylim(ymin, ymax)
    syn_plot.plot(order.synthesizedSkySpec, 'g-', linewidth=1)
    syn_plot.annotate('shift = ' + "{:.3f}".format(order.waveShift),
                      (0.3, 0.8),
                      xycoords="figure fraction")

    sky_plot = pl.subplot(2, 1, 2)
    sky_plot.set_title('sky')
    sky_plot.set_xlim([0, 1024])
    ymin = np.amin(order.skySpec['A']) - (
        (np.amax(order.skySpec['A']) - np.amin(order.skySpec['A'])) * 0.1)
    ymax = np.amax(order.skySpec['A']) + (
        (np.amax(order.skySpec['A']) - np.amin(order.skySpec['A'])) * 0.1)
    sky_plot.set_ylim([ymin, ymax])
    sky_plot.plot(order.skySpec['A'], 'b-', linewidth=1)

    ymin, ymax = sky_plot.get_ylim()
    dy = (ymax - ymin) / 4
    y = ymin + dy / 8
    for line in order.lines:
        if line.frameFitOutlier == False:
            c = 'k--'
        else:
            c = 'r--'
        sky_plot.plot([line.col, line.col], [ymin, ymax], c, linewidth=0.5)
        pl.annotate(str(line.waveAccepted), (line.col, y), size=8)
        pl.annotate(
            str(line.col) + ', ' +
            '{:.3f}'.format(order.flatOrder.gratingEqWaveScale[line.col]),
            (line.col, y + (dy / 4)),
            size=8)
        y += dy
        if y > (ymax - dy):
            y = ymin + dy / 8

    fn = constructFileName(outpath, order.baseNames['A'],
                           order.flatOrder.orderNum, 'skylines.png')

    pl.savefig(fn)
    pl.close()
    return
Beispiel #47
0
def completeness():  #measure completeness on final image

    #combinepath=bcdpath+'pbcd/Combine/output_apex_step2'
    if SqDegS > 0.1:
        combinepath = bcdpath + '/output_apex_step2'
        os.chdir(combinepath)
        os.system('cp mosaic_extract_raw.tbl mosaic_extract_final.tbl')
    else:
        combinepath = bcdpath + 'pbcd/Combine/apex_1frame_step2'
        os.chdir(combinepath)

    file = 'mosaic_extract_final.tbl'
    input = open(file, 'r')
    xgal = []  #positions of previous detections with snr > 3
    ygal = []
    fap4gal = []
    for line in input:
        if line.find('#') > -1:  #skip lines with '#' in them
            continue
        if line.find('\\') > -1:  #skip lines with '#' in them
            continue
        if line.find('|') > -1:  #skip lines with '#' in them
            continue
        t = line.split()
        xgal.append(float(t[8]))
        ygal.append(float(t[10]))
        fap4gal.append(float(t[28]))
    input.close()
    xgal = N.array(xgal, 'f')
    ygal = N.array(ygal, 'f')

    fsimall = []
    matchflagsimall = []
    f2all = []
    f3all = []
    f4all = []
    deblendsimall = []
    snrsimall = []

    myminmag = 24.75
    mymaxmag = 27.4
    myfmin = 10.**((25. - mymaxmag) / 2.5)  #ZP=25
    myfmax = 10.**((25. - myminmag) / 2.5)  #ZP=25

    #below is loop to create image w/artificial sources, extract, and compare

    for k in range(100):
        createflag = 1.  #create image w/artificial sources?
        detectflag = 1.  #detect sources in image?
        if createflag > 0.1:
            xnoise = []
            ynoise = []
            infile = open(
                'noisecoords.dat', 'r'
            )  #read in list of coordinates corresponding to positions where no real source exists.  These are generated by spitzergetnoise.py.
            for line in infile:
                t = line.split()
                xnoise.append(float(t[0]))
                ynoise.append(float(t[1]))
        infile.close()

        nstar = 10

        xsim = N.zeros(nstar, 'd')
        ysim = N.zeros(nstar, 'd')
        msim = N.zeros(nstar, 'd')
        outfile = open('stars.coords.dat', 'w')
        for i in range(nstar):
            #j=int(round(1.*len(xnoise)*random.uniform(0,1)))

            #xsim[i]=xnoise[j]
            #ysim[i]=ynoise[j]
            j = 0
            for j in range(10000):
                xt = int(round(random.uniform(5., 125.)))
                yt = int(round(random.uniform(5., 140.)))
                d = pylab.sqrt(
                    (xt - xgal)**2 + (yt - ygal)**
                    2)  #make sure sim galaxies are not near real galaxies
                if min(d) > -1.:
                    d2 = pylab.sqrt(
                        (xt - xsim)**2 + (yt - ysim)**
                        2)  #make sure sim points are not on top of each other
                    if min(d2) > 5.:
                        print i, 'got a good point after ', j, ' tries', xt, yt
                        break
                    j = j + 1
            xsim[i] = xt
            ysim[i] = yt
            k = random.uniform(myfmin, myfmax)
            msim[i] = 25. - 2.5 * pylab.log10(k)
            #print k,msim[i]
            s = '%8.2f %8.2f %8.2f \n' % (xsim[i], ysim[i], msim[i])
            outfile.write(s)
        outfile.close()

        #os.system('rm stars.coords.dat')
        #iraf.starlist('stars.coords.dat',nstars=100,spatial='uniform',xmax=130,ymax=145,luminosity='uniform',minmag=22.,maxmag=30.0,mzero=22.0,sseed='INDEF',power=0.6,alpha=0.74,lseed='INDEF')

        os.system('rm mosaic-completeness.fits')
        #iraf.mkobjects(input='mosaic_minus_median_extract.fits',output='mosaic-completeness.fits',objects='stars.coords.dat',radius=1.13,magzero=25.,background=0.,gain=5.,rdnoise=0.,poisson='no')#don't convolve w/PRF
        #os.system('cp ../cal/MIPS24_PRF_HD_center.fits .')#convolve star w/SSC PRF
        os.system('cp ../cal/mips24_prf_mosaic_2.45_4x.fits .'
                  )  #convolve star w/SSC PRF
        iraf.mkobjects(input='mosaic_minus_median_extract.fits',
                       output='mosaic-completeness.fits',
                       objects='stars.coords.dat',
                       radius=14,
                       star='mips24_prf_mosaic_2.45_4x.fits',
                       magzero=25.,
                       background=0.,
                       gain=5.,
                       rdnoise=0.,
                       poisson='no')
        #os.system('cp ../cal/PRF_estimate.fits .')#convolve gaussian w/measured PRF
        #iraf.mkobjects(input='mosaic_minus_median_extract.fits',output='mosaic-completeness.fits',objects='stars.coords.dat',radius=15,star='PRF_estimate.fits',magzero=25.,background=0.,gain=5.,rdnoise=0.,poisson='no')
        os.system('ls *.fits')
        os.system('pwd')
        iraf.display('mosaic_minus_median_extract.fits', 1, contrast=0.01)
        iraf.display('mosaic-completeness.fits', 2, contrast=0.01)
        iraf.tvmark(1, 'stars.coords.dat')
        iraf.tvmark(2, 'stars.coords.dat')
        fsim = 10.**((25. - msim) / 2.5)  #ZP=25

        if createflag < .1:  #read in positions and magnitudes of artdata sources
            xsim = []
            ysim = []
            msim = []
            infile = open('stars.coords.dat', 'r')
            for line in infile:
                if line.find('#') > -1:
                    continue
                t = line.split()
                xsim.append(float(t[0]))
                ysim.append(float(t[1]))
                msim.append(float(t[2]))
            infile.close()
            xsim = N.array(xsim, 'f')
            ysim = N.array(ysim, 'f')
            msim = N.array(msim, 'f')

            fsim = 10.**((25. - msim) / 2.5)  #ZP=25

        if detectflag > 0.1:  #now run detection on mosaic-completeness.fits
            if SqDegS > 0.1:
                combinepath = bcdpath
            else:
                combinepath = bcdpath + 'pbcd/Combine/'
            os.chdir(combinepath)
            print combinepath
            #os.system('apex_1frame.pl -n apex_1frame_MIPS24_step2.nl -i output_apex_step2/mosaic-completeness.fits')

            #combinepath=bcdpath+'pbcd/Combine/output_apex_step2'

            if SqDegS > 0.1:
                s = 'cp /Users/rfinn/clusters/spitzer/apex_1frame_step2all_400.nl ' + bcdpath + 'cdf/'
                os.system(s)
                os.system(
                    'apex_1frame.pl -n apex_1frame_step2all_400.nl -i output_apex_step2/mosaic-completeness.fits'
                )
                combinepath = bcdpath + 'output_apex_step2/'
            else:
                os.system(
                    'apex_1frame.pl -n apex_1frame_step2all.nl -i apex_1frame_step2/mosaic-completeness.fits'
                )
                combinepath = bcdpath + 'pbcd/Combine/apex_1frame_step2'
            os.chdir(combinepath)
            print combinepath
            file = 'mosaic-completeness_extract_raw.tbl'
            input = open(file, 'r')
            ngal = 0
            for line in input:
                if line.find('Conversion') > -1:
                    t = line.split('=')
                    convfactor = float(t[1])  #conversion from ADU to uJy
                    #aperr=aveaperr*convfactor #convert noise in ADU to uJy using conv factor from apex
                    print "Conversion Factor = ", convfactor
                    #print "aveaperr = ",aveaperr
                    #print "aperr = ",aperr
                    continue
                if line.find('#') > -1:  #skip lines with '#' in them
                    continue
                if line.find('\\') > -1:  #skip lines with '#' in them
                    continue
                if line.find('|') > -1:  #skip lines with '#' in them
                    continue
                ngal = ngal + 1
            input.close()

        id24 = N.zeros(ngal, 'f')
        imagex24 = N.zeros(ngal, 'f')
        imagey24 = N.zeros(ngal, 'f')
        ra24 = N.zeros(ngal, 'f')
        dec24 = N.zeros(ngal, 'f')
        f24 = N.zeros(ngal, 'd')  #flux
        errf24 = N.zeros(ngal, 'd')
        fap1 = N.zeros(
            ngal,
            'd')  #flux in aperture 1 (1,1.5,2,2.6,3,3.5,4,4.5,5.,5.5) pixels
        fap2 = N.zeros(ngal, 'd')  #flux
        fap3 = N.zeros(ngal, 'd')  #flux
        fap4 = N.zeros(
            ngal, 'd'
        )  #flux in ap 4 - this is one w/ap cor of 1.67 (Calzetti et al 2007)
        fap5 = N.zeros(ngal, 'd')  #flux
        fap6 = N.zeros(ngal, 'd')  #flux
        fap7 = N.zeros(ngal, 'd')  #flux
        fap8 = N.zeros(ngal, 'd')  #flux
        fap9 = N.zeros(ngal, 'd')  #flux
        fap10 = N.zeros(ngal, 'd')  #flux
        snr24 = N.zeros(ngal, 'd')  #SNR calculated by mopex
        deblend = N.zeros(ngal, 'f')  #SNR calculated by mopex

        input = open(file, 'r')
        i = 0
        output = open('xy24raw.dat', 'w')
        for line in input:
            if line.find('#') > -1:  #skip lines with '#' in them
                continue
            if line.find('\\') > -1:  #skip lines with '#' in them
                continue
            if line.find('|') > -1:  #skip lines with '#' in them
                continue

            t = line.split()
            #print "length of t = ",len(t)
            #print (t[8]),(t[10]),(t[13]),(t[14]),(t[18]),(t[2]),(t[23]),(t[24]),(t[25]),(t[26]),(t[27]),(t[28]),(t[29]),(t[30]),(t[31]),(t[32])

            (imagex24[i], imagey24[i], f24[i], errf24[i], snr24[i], deblend[i],
             fap1[i], fap2[i], fap3[i], fap4[i], fap5[i], fap6[i], fap7[i],
             fap8[i],
             fap9[i], fap10[i]) = (float(t[8]), float(t[10]), float(t[13]),
                                   float(t[14]), float(t[18]), float(t[2]),
                                   float(t[25]), float(t[26]), float(t[27]),
                                   float(t[28]), float(t[29]), float(t[30]),
                                   float(t[31]), float(t[32]), float(t[33]),
                                   float(t[34]))
            s = '%6.2f %6.2f \n' % (imagex24[i], imagey24[i])
            output.write(s)

            i = i + 1
        input.close()  #44 -> 43
        output.close()
        iraf.tvmark(1, 'xy24raw.dat', color=204, radi=2)
        iraf.tvmark(2, 'xy24raw.dat', color=204, radi=2)

        delta = 1.  #max number of pixels for a match

        #get rid of objects that were detected in original image.  Otherwise, matching will think any object near a sim galaxy is the sim galaxy.  A faint galaxy placed on type of a pre-existing bright galaxy will be detected.

        newgalflag = N.ones(len(imagex24), 'i')
        for i in range(len(imagex24)):
            (imatch, matchflag, nmatch) = findnearest(imagex24[i], imagey24[i],
                                                      xgal, ygal, delta)
            if matchflag > 0.:
                dflux = abs(fap4gal[imatch] - fap4[i]) / fap4[i]
                if dflux < .1:  #position of real galaxy, flux difference less than 10% -> not a new galaxy
                    newgalflag[i] = 0
        #keep only galaxies that are new
        imagex24 = N.compress(newgalflag, imagex24)
        imagey24 = N.compress(newgalflag, imagey24)
        fap1 = N.compress(newgalflag, fap1)
        fap2 = N.compress(newgalflag, fap2)
        fap3 = N.compress(newgalflag, fap3)
        fap4 = N.compress(newgalflag, fap4)
        fap5 = N.compress(newgalflag, fap5)
        fap6 = N.compress(newgalflag, fap6)
        fap7 = N.compress(newgalflag, fap7)
        fap8 = N.compress(newgalflag, fap8)
        fap9 = N.compress(newgalflag, fap9)
        fap10 = N.compress(newgalflag, fap10)
        snr24 = N.compress(newgalflag, snr24)
        deblend = N.compress(newgalflag, deblend)

        delta = 2.  #max number of pixels for a match
        matchflagsim = N.zeros(len(xsim), 'i')
        fmeas1 = N.zeros(len(xsim), 'f')
        fmeas2 = N.zeros(len(xsim), 'f')
        fmeas3 = N.zeros(len(xsim), 'f')
        fmeas4 = N.zeros(len(xsim), 'f')
        fmeas5 = N.zeros(len(xsim), 'f')
        fmeas6 = N.zeros(len(xsim), 'f')
        fmeas7 = N.zeros(len(xsim), 'f')
        fmeas8 = N.zeros(len(xsim), 'f')
        fmeas9 = N.zeros(len(xsim), 'f')
        fmeas10 = N.zeros(len(xsim), 'f')
        fmeas24 = N.zeros(len(xsim), 'f')
        deblendsim = N.zeros(len(xsim), 'f')
        snrsim = N.zeros(len(xsim), 'f')
        for i in range(len(xsim)):
            (imatch, matchflag, nmatch) = findnearest(xsim[i], ysim[i],
                                                      imagex24, imagey24,
                                                      delta)
            matchflagsim[i] = matchflag
            if matchflag > .1:
                fmeas1[i] = fap1[int(imatch)]
                fmeas2[i] = fap2[int(imatch)]
                fmeas3[i] = fap3[int(imatch)]
                fmeas4[i] = fap4[int(imatch)]
                fmeas5[i] = fap5[int(imatch)]
                fmeas6[i] = fap6[int(imatch)]
                fmeas7[i] = fap7[int(imatch)]
                fmeas8[i] = fap8[int(imatch)]
                fmeas9[i] = fap9[int(imatch)]
                fmeas10[i] = fap10[int(imatch)]
                fmeas24[i] = f24[int(imatch)]
                deblendsim[i] = deblend[int(imatch)]
                snrsim[i] = snr24[int(imatch)]

        fsimall = fsimall + list(fsim)
        matchflagsimall = matchflagsimall + list(matchflagsim)
        f2all = f2all + list(fmeas2)
        f3all = f3all + list(fmeas3)
        f4all = f4all + list(fmeas4)
        deblendsimall = deblendsimall + list(deblendsim)
        snrsimall = snrsimall + list(snrsim)

    fsim = N.array(fsimall, 'f')
    matchflagsim = N.array(matchflagsimall, 'f')
    fmeas2 = N.array(f2all, 'f')
    fmeas3 = N.array(f3all, 'f')
    fmeas4 = N.array(f4all, 'f')
    deblendsim = N.array(deblendsimall, 'f')
    snrsim = N.array(snrsimall, 'f')

    #make plots using all realizations
    pylab.cla()
    pylab.clf()
    fsim = fsim * convfactor
    fs = pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5), fsim)
    #f1=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas1)
    f2 = pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5), fmeas2)
    f3 = pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5), fmeas3)
    f4 = pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5), fmeas4)
    #f242=pylab.compress((matchflagsim > 0.1) & (deblendsim < 1.5),fmeas24)

    r4 = pylab.median(fs / f4)
    r3 = pylab.median(fs / f3)
    r2 = pylab.median(fs / f2)
    print "average ratios ap 4", pylab.average(fs / f4), r4, pylab.std(
        (fs / f4) / pylab.average(fs / f2))
    print "average ratios ap 3", pylab.average(fs / f3), pylab.median(
        fs / f3), pylab.std((fs / f3) / pylab.average(fs / f3))
    print "average ratios ap 2", pylab.average(fs / f2), pylab.median(
        fs / f2), pylab.std((fs / f2) / pylab.average(fs / f2))

    s = 'f4 w/apcor = %3.2f(%4.2f)' % (r4, pylab.average(
        abs(fs - f4 * r4) / fs))
    pylab.plot(fs, f4 * r4, 'b.', label=s)
    pylab.plot(fs, f4, 'bo', label='f4')
    s = 'f3 w/apcor = %3.2f(%4.2f)' % (r3, pylab.average(
        abs(fs - f3 * r3) / fs))
    pylab.plot(fs, f3 * r3, 'g.', label=s)
    pylab.plot(fs, f3, 'go', label='f3')
    s = 'f2 w/apcor = %3.2f(%4.2f)' % (r2, pylab.average(
        abs(fs - f2 * r2) / fs))
    pylab.plot(fs, f2 * r2, 'r.', label=s)
    pylab.plot(fs, f2, 'ro', label='f2')
    #pylab.plot(fs,f1,'co',label='f1')
    #pylab.plot(fs,f242,'k.',label='f24')
    pylab.legend(loc='best')
    x = N.arange(0., max(fs), 10.)
    y = x
    pylab.plot(x, y, 'k-')
    #y=2.*x
    #pylab.plot(x,y,'k--')
    #y=3.*x
    #pylab.plot(x,y,'k--')
    #y=4.*x
    #pylab.plot(x,y,'k--')
    #y=5.*x
    #pylab.plot(x,y,'k--')
    pylab.xlabel('F(24) Input')
    pylab.ylabel('F(24) measured')
    #pylab.axis([0.,50.,0.,50.])
    s = str(prefix) + 'fluxcomp.eps'
    pylab.savefig(s)

    pylab.cla()
    pylab.clf()

    nbins = 20
    fmin = 10.  #min(fsim)
    fmax = max(fsim)
    df = 5.  #(fmax-fmin)/(1.*nbins)
    bins = N.arange(fmin, (fmax + df), df)

    (xbin, ybin, ybinerr) = mystuff.completeness(bins, fsim, matchflagsim)
    s = str(prefix) + 'FracComplvsFlux.dat'
    outdat = open(s, 'w')
    print "Completeness vs Input Flux"
    for i in range(len(xbin)):
        print i, xbin[i], ybin[i], ybinerr[i]
        t = '%8.2f %8.4f %8.4f\n' % (xbin[i], ybin[i], ybinerr[i])
        outdat.write(t)
    outdat.close()
    #for i in range(len(fsim)):
    #if snrsim[i] > 3.:
    #    print i, fsim[i],matchflagsim[i],deblendsim[i],abs(fsim[i]-fmeas4[i]*1.67)/fsim[i],snrsim[i]
    #(xbin,ybin2,ybin2err)=mystuff.scipyhist2(bins,fmeas4)
    #pylab.plot(xbin,ybin,'bo')
    #pylab.plot(xbin,ybin2,'ro')
    #s=str(prefix)+'NDetectvsFlux.eps'
    #pylab.savefig(s)

    pylab.cla()
    pylab.clf()
    pylab.plot(xbin, ybin, 'ko')
    pylab.errorbar(xbin, ybin, yerr=ybinerr, fmt=None, ecolor='k')
    s = str(prefix) + 'FracComplvsFlux.eps'
    pylab.axhline(y=1.0, ls='-')
    pylab.axhline(y=.8, ls='--')
    pylab.axvline(x=80.0, ls=':', color='b')
    pylab.xlabel('Input Flux (uJy)')
    pylab.ylabel('Completeness')
    pylab.axis([0., max(xbin) + df, -.05, 1.05])

    pylab.savefig(s)

    if SqDegS < 0.1:

        os.system('cp *.eps /Users/rfinn/clusters/spitzer/completeness/.')
        os.system(
            'cp *vsFlux.dat /Users/rfinn/clusters/spitzer/completeness/.')
Beispiel #48
0
#out = utils.filter_data(out, window=9, order=2)
#inp = utils.filter_data(inp, window=9, order=2)

# Set up a figure twice as tall as it is wide
fig = plt.figure(figsize=plt.figaspect(1))
fig.subplots_adjust(left=0, right=1, bottom=0, top=0.97)
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=40, azim=140)

writer = FFMpegWriter(fps=10)
xlim, ylim, zlim = None,None,None
thist = 20
with writer.saving(fig, "LiftPose3D_prediction.mp4", 100):
    for t in tqdm(range(out.shape[0])):
              
        plt.cla()
        
        pos_pred = []
        for j in range(out.shape[1]):
            tmin = max(0,t-thist+1)
            pos_pred.append((inp[tmin:(t+1), 2*j], inp[tmin:(t+1), 2*j+1], out[tmin:(t+1), j]))
                
        pos_pred = np.array(pos_pred)
        
        #plot skeleton
        plotting.plot_3d_graph(G, pos_pred[:,:,-1], ax, color_edge=color_edge) 
            
        #plot trailing points
        plotting.plot_trailing_points(pos_pred[legtips,:,:],min(thist,t+1),ax)
        
        if xlim is None:
Beispiel #49
0
def draw():
    PL.cla()
    PL.pcolor(config, vmin = 0, vmax = 3, cmap = PL.cm.binary)
    PL.axis('image')
    PL.title('t = ' + str(time))
Beispiel #50
0
def raytrace(model, nimgs=None, ipeps=None, speps=None, initial_guess=None, verbose=False, viz=False):
    """Find the positions of images by raytracing back to the source.

        ipeps - Radius on the image plane to consider two image as one.
        speps - Radius on the source plane to determine if a pixel maps near to the source
    """
                
    global fig

    obj,ps,src_index = model
#   if len(model) == 2:
#       [obj,ps], src_index = model
#   else:
#       obj_index, src_index = model[1:]
#       obj,ps = model[0]['obj,data'][obj_index]

    ploc    = obj.basis.ploc
    src     = ps['src'][src_index]
    zcap    = obj.sources[src_index].zcap
    srcdiff = None
    #zcap=1

    if ipeps is None:
        #ipeps = 2 * obj.basis.top_level_cell_size
        ipeps = 0.01 * obj.basis.mapextent

    if speps is None:
        speps = obj.basis.top_level_cell_size #/ sqrt(2)
        #speps = 0.01 * obj.basis.mapextent

    #---------------------------------------------------------------------------
    # (1) Make an initial guess where the images are. 
    #
    # srcdiff is a matrix giving the distance between the src and the point on
    # the source plane where each pixel maps back to. Taking the n pixels with
    # the smallest of those differences gives a good estimate for the image
    # positions.
    #---------------------------------------------------------------------------

    if not initial_guess:
        srcdiff = obj.basis.srcdiff(ps, src_index)
        initial_guess = []
        asort = argsort(srcdiff)

        for j in asort:
            if srcdiff[j] > speps: break
            initial_guess.append(ploc[j])

#   if not initial_guess:
#       initial_guess = []

#       for i in range(20):
#           r = obj.basis.mapextent * random()
#           t  = 2 * np.pi * random()
#           sx = r * np.cos(t)
#           sy = r * np.sin(t)
#           initial_guess.append(complex(sx,sy))
    



#-------------------------------------------------------------------------------

#           for ii in initial_guess:
#               if abs(ploc[j] - ii) <= eps: break
#           else:

#        if len(initial_guess) >= nimgs: break

#       raw_input()

#   #figure(f.number)
#   figure()

#       if not any(abs(ploc[j] - ploc[asort[:i]]) <= eps):
#           srcdiff[j] = 10
#           initial_guess.append(ploc[j])


#-------------------------------------------------------------------------------

    #---------------------------------------------------------------------------
    # (2) Minimize the lens equation beginning from each of the initial guesses.
    # Only those images that truly satisfy the equation are accepted.
    #---------------------------------------------------------------------------

    initial_guess.append(src)
    if verbose: print 'Initial guesses', initial_guess

    def lenseq(theta0):
        theta = complex(*theta0)
        r = src - theta + obj.basis.deflect(theta, ps) / zcap
        return r.real, r.imag

    def lenseq_prime(theta0):
        theta = complex(*theta0)
        dist  = theta - obj.basis.ploc
        dxdx = -1 + dot(ps['kappa'], (poten_dxdx(dist,obj.basis.cell_size))) / zcap
        dydy = -1 + dot(ps['kappa'], (poten_dydy(dist,obj.basis.cell_size))) / zcap
        dxdy = -1 + dot(ps['kappa'], (poten_dxdy(dist,obj.basis.cell_size))) / zcap
        dydx = -1 + dot(ps['kappa'], (poten_dydx(dist,obj.basis.cell_size))) / zcap
        return [ [dxdx, dydx], [dxdy, dydy] ]

    xs = []
    #if obj.shear: s1,s2 = ps['shear']
    for img in initial_guess:
        #x,_,ier,mesg = fsolve(lenseq, [img.real,img.imag], fprime=lenseq_prime, full_output=True) #, xtol=1e-12)
        x,_,ier,mesg = fsolve(lenseq, [img.real,img.imag], full_output=True, xtol=1e-12)
        #x = fmin(lenseq, [img.real,img.imag], full_output=False, disp=False, xtol=1e-10, ftol=1e-10)

        if not ier: 
            print mesg
            continue

        r = complex(*x)

        # if an initial guess was poor then the minimum will not be near zero.
        # Only accept solutions that are very close to zero.
        leq = abs(complex(*lenseq(x)))
        if leq < 2e-10:
            #print leq
            xs.append([img, r, leq])
        else:
            #print 'Image at %s rejected. %e' % (r, leq)
            pass


    #---------------------------------------------------------------------------
    # (3) Sort by how well we were able to minimize each function.
    #---------------------------------------------------------------------------

    xs.sort(lambda x,y: -1 if x[2] < y[2] else 1 if x[2] > y[2] else 0)

    #---------------------------------------------------------------------------
    # (4) Only accept a solution if it is distinct from previous solutions.
    #---------------------------------------------------------------------------

    imgs0 = []
    for img,r0,t0 in xs:

        for r,t in imgs0:
            if abs(r0-r) < ipeps: break
        else:
            tau  = abs(r0-src)**2 / 2
            tau *= zcap
            tau -= dot(ps['kappa'], poten(r0 - obj.basis.ploc, obj.basis.cell_size, obj.basis.maprad))
            tau -= np.sum( [ ps[e.name] * e.poten(r0).T for e in obj.extra_potentials ] )
            #print tau
            #print '!!', poten(i - obj.basis.ploc, obj.basis.cell_size)[0]
            #print '!!', dot(ps['kappa'], poten(complex(1,0) - obj.basis.ploc, obj.basis.cell_size))
            imgs0.append([r0,tau])

    if viz:
        if fig == None:
            fig = pl.figure()

        if srcdiff is None:
            srcdiff = obj.basis.srcdiff(ps, src_index)

        #print obj.sources
        reorder = empty_like(srcdiff)
        reorder.put(obj.basis.pmap, srcdiff)
        sd = zeros((2*obj.basis.pixrad+1)**2)
        sd[obj.basis.insideL] = reorder
        #sd[:len(srcdiff)] = srcdiff #reorder
        sd = sd.reshape((2*obj.basis.pixrad+1,2*obj.basis.pixrad+1))
        R = obj.basis.mapextent
        kw = {'extent': [-R,R,-R,R],
              'interpolation': 'nearest',
              'aspect': 'equal',
              'origin': 'upper',
              #'cmap': cm.terrain,
              'fignum': False,
              #'vmin': -1,
              #'vmax':  1
              }
        pl.cla()
        pl.matshow(sd, **kw)
        #pl.colorbar()
#       print '??'
#       for i in initial_guess: print i
#       print '??'
        #arrival_plot({'obj,data':[[obj,ps]]}, src_index=src_index, clevels=150)
        xs = [r.real for r in initial_guess]
        ys = [r.imag for r in initial_guess]
        pl.scatter(xs,ys)

#       print '**'
#       for i in obj.sources[src_index].images: print i
#       print '**'

#       xs = [r.pos.real for r in obj.sources[src_index].images]
#       ys = [r.pos.imag for r in obj.sources[src_index].images]
#       pl.scatter(xs,ys, color='r', s=40)

        xs = [r.real for r,tau in imgs0]
        ys = [r.imag for r,tau in imgs0]
        pl.scatter(xs,ys, color='g')
        pl.draw()
        print '*'*80
        print 'PRESS ANY KEY TO CONTINUE'
        print '*'*80
        raw_input()


    #---------------------------------------------------------------------------
    # (5) Determine magnification information
    #---------------------------------------------------------------------------
    from scipy.linalg import det

    imgs = []
    for img in imgs0:
        #print 'tau', tau
        r, tau = img
        theta = arctan2(r.imag, r.real) * 180/pi
        A = zcap*identity(2) - obj.basis.magnification(r, theta, ps)

        detA = det(A)
        trA  = A.trace()
        mu   = 1. / detA # magnification factor

        parity = ['sad', 'sad', 'max', 'min'][(detA > 0)*2 + (trA > 0)]

        print mu
        imgs.append(img + [ [mu, A], parity ])

    #Mavg = average(map(lambda x: (x[3] != 'max') * x[2][3], imgs))

    #imgs = filter(lambda x: abs(x[2][3]) > Mavg*0.8, imgs)

    #print imgs

    #---------------------------------------------------------------------------
    # (6) Sort by arrival time
    #---------------------------------------------------------------------------
    imgs.sort(lambda x,y: -1 if x[1] < y[1] else 1 if x[1] > y[1] else 0)

#   if fig == None:
#       fig = figure()

#   f = pl.gcf()
##  figure() #fig.number)
##  reorder = empty_like(srcdiff)
##  reorder.put(obj.basis.pmap, srcdiff)
##  sd = zeros((2*obj.basis.pixrad+1)**2)
##  sd[obj.basis.insideL] = reorder
##  #sd[:len(srcdiff)] = srcdiff #reorder
##  sd = sd.reshape((2*obj.basis.pixrad+1,2*obj.basis.pixrad+1))
##  R = obj.basis.mapextent
##  kw = {'extent': [-R,R,-R,R],
##        'interpolation': 'nearest',
##        'aspect': 'equal',
##        'origin': 'upper',
##        'cmap': cm.terrain,
##        'fignum': False,
##        'vmin': 0,
##        'vmax': R}
##  matshow(sd, **kw) #, fignum=fig.number)
##  kw.pop('cmap')
##  over(contour, sd, 100, extend='both', colors='w', alpha=1.0, **kw)

#   #figure(f.number)
#   figure()

    #print imgs

    return imgs
Beispiel #51
0
def order_location_plot(outpath, obj_base_name, flat_base_name, flat_img,
                        obj_img, orders):

    pl.figure('orders', facecolor='white', figsize=(8, 5))
    pl.cla()
    pl.suptitle('order location and identification', fontsize=14)
    pl.set_cmap('Blues_r')
    pl.rcParams['ytick.labelsize'] = 8

    obj_plot = pl.subplot(1, 2, 1)
    try:
        obj_plot.imshow(exposure.equalize_hist(obj_img))
    except:
        obj_plot.imshow(obj_img)
    obj_plot.set_title('object ' + obj_base_name)
    obj_plot.set_ylim([1023, 0])
    obj_plot.set_xlim([0, 1023])

    flat_plot = pl.subplot(1, 2, 2)
    try:
        flat_plot.imshow(exposure.equalize_hist(flat_img))
    except:
        flat_plot.imshow(flat_img)
    flat_plot.set_title('flat ' + flat_base_name)
    flat_plot.set_ylim([1023, 0])
    flat_plot.set_xlim([0, 1023])

    for order in orders:
        obj_plot.plot(np.arange(1024),
                      order.flatOrder.topEdgeTrace,
                      'k-',
                      linewidth=1.0)
        obj_plot.plot(np.arange(1024),
                      order.flatOrder.botEdgeTrace,
                      'k-',
                      linewidth=1.0)
        obj_plot.plot(np.arange(1024),
                      order.flatOrder.smoothedSpatialTrace,
                      'y-',
                      linewidth=1.0)
        obj_plot.text(10,
                      order.flatOrder.topEdgeTrace[0] - 10,
                      str(order.flatOrder.orderNum),
                      fontsize=10)

        flat_plot.plot(np.arange(1024),
                       order.flatOrder.topEdgeTrace,
                       'k-',
                       linewidth=1.0)
        flat_plot.plot(np.arange(1024),
                       order.flatOrder.botEdgeTrace,
                       'k-',
                       linewidth=1.0)
        flat_plot.plot(np.arange(1024),
                       order.flatOrder.smoothedSpatialTrace,
                       'y-',
                       linewidth=1.0)
        flat_plot.text(10,
                       order.flatOrder.topEdgeTrace[0] - 10,
                       str(order.flatOrder.orderNum),
                       fontsize=10)

    pl.tight_layout()
    pl.savefig(constructFileName(outpath, obj_base_name, None, 'traces.png'))
    pl.close()
Beispiel #52
0
def omni_view(reds,
              vis,
              pol,
              int=10,
              chan=500,
              norm=False,
              cursor=True,
              save=None,
              colors=None,
              symbols=None,
              ex_ants=[]):
    if not colors:
        colors = [
            "#006BA4", "#FF7F0E", "#2CA02C", "#D61D28", "#9467BD", "#8C564B",
            "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF"
        ]
    if not symbols:
        symbols = ["o", "v", "^", "<", ">", "*"]
    points = []
    sym = []
    col = []
    bl = []
    ngps = len(reds)
    if save:
        plt.clf()
        plt.cla()
    for i, gp in enumerate(reds):
        c = colors[i % len(colors)]
        s = symbols[i / len(colors)]
        for r in gp:
            if np.any([ant in r for ant in ex_ants]): continue
            try:
                points.append(vis[r][pol][int, chan])
                bl.append(r)
            except (KeyError):
                points.append(np.conj(vis[r[::-1]][pol][int, chan]))
                bl.append(r[::-1])
            sym.append(s)
            col.append(c)
    points = np.array(points)
    max_x = 0
    max_y = 0
    ax = plt.subplots(111)
    for i, pt in enumerate(points):
        if norm:
            ax.scatter(pt.real / np.abs(pt),
                       pt.imag / np.abs(pt),
                       c=col[i],
                       marker=sym[i],
                       s=50,
                       label='{}'.format(bl[i]))
        else:
            ax.scatter(pt.real,
                       pt.imag,
                       c=col[i],
                       marker=sym[i],
                       s=50,
                       label='{}'.format(bl[i]))
            if np.abs(pt.real) > max_x: max_x = np.abs(pt.real)
            if np.abs(pt.imag) > max_y: max_y = np.abs(pt.imag)

    if norm:
        plt.xlim(-1, 1)
        plt.ylim(-1, 1)
    else:
        plt.xlim(-max_x - .1 * max_x, max_x + .1 * max_x)
        plt.ylim(-max_y - .1 * max_y, max_y + .1 * max_y)
    plt.ylabel('imag(V)')
    plt.xlabel('real(V)')
    if cursor:
        from mpldatacursor import datacursor
        datacursor(formatter='{label}'.format)
    if save:
        plt.savefig(save)
    return None
Beispiel #53
0
def sample(ndim,
           nwalkers,
           nsteps,
           burnin,
           start,
           ur,
           sigma_ur,
           nuvu,
           sigma_nuvu,
           age,
           id,
           ra,
           dec,
           get_c_one,
           use_table,
           thegrid,
           lu=None,
           savedir="./"):
    """ Function to implement the emcee EnsembleSampler function for the sample of galaxies input. Burn in is run and calcualted fir the length specified before the sampler is reset and then run for the length of steps specified. 
        
        :ndim:
        The number of parameters in the model that emcee must find. In this case it always 2 with tq, tau.
        
        :nwalkers:
        The number of walkers that step around the parameter space. Must be an even integer number larger than ndim. 
        
        :nsteps:
        The number of steps to take in the final run of the MCMC sampler. Integer.
        
        :burnin:
        The number of steps to take in the inital burn-in run of the MCMC sampler. Integer. 
        
        :start:
        The positions in the tq and tau parameter space to start for both disc and smooth parameters. An array of shape (1,4).
        
        
        :ur:
        Observed u-r colour of a galaxy; k-corrected. An array of shape (N,1) or (N,).
        
        :sigma_ur:
        Error on the observed u-r colour of a galaxy. An array of shape (N,1) or (N,).
        
        :nuvu:
        Observed nuv-u colour of a galaxy; k-corrected. An array of shape (N,1) or (N,).
        
        :sigma_nuvu:
        Error on the observed nuv-u colour of a galaxy. An array of shape (N,1) or (N,).
        
        :age:
        Observed age of a galaxy, often calculated from the redshift i.e. at z=0.1 the age ~ 12.5. Must be in units of Gyr. An array of shape (N,1) or (N,).
        
        :id:
        ID number to specify which galaxy this run is for.
        
        :ra:
        right ascension of source, used for identification purposes
        
        :dec:
        declination of source, used for identification purposes
        
        RETURNS:
        :samples:
        Array of shape (nsteps*nwalkers, 4) containing the positions of the walkers at all steps for all 4 parameters.
        :samples_save:
        Location at which the :samples: array was saved to. 
        
        """
    tq, tau, ages = thegrid
    grid = N.array(list(product(ages, tau, tq)))

    if use_table:
        global u
        global v
        a = N.searchsorted(ages, age)
        b = N.array([a - 1, a])
        print 'interpolating function, bear with...'
        g = grid[N.where(
            N.logical_or(grid[:, 0] == ages[b[0]], grid[:, 0] == ages[b[1]]))]
        values = lu[N.where(
            N.logical_or(grid[:, 0] == ages[b[0]], grid[:, 0] == ages[b[1]]))]
        f = LinearNDInterpolator(g, values, fill_value=(-N.inf))
        look = f(age, grid[:10000, 1], grid[:10000, 2])
        lunuv = look[:, 0].reshape(100, 100)
        v = interp2d(tq, tau, lunuv)
        luur = look[:, 1].reshape(100, 100)
        u = interp2d(tq, tau, luur)
    else:
        pass
    print 'emcee running...'
    p0 = [start + 1e-4 * N.random.randn(ndim) for i in range(nwalkers)]
    sampler = emcee.EnsembleSampler(nwalkers,
                                    ndim,
                                    lnprob,
                                    threads=2,
                                    args=(ur, sigma_ur, nuvu, sigma_nuvu, age,
                                          get_c_one))
    """ Burn in run here..."""
    pos, prob, state = sampler.run_mcmc(p0, burnin)
    lnp = sampler.flatlnprobability
    N.save(
        savedir + 'lnprob_burnin_' + str(int(id)) + '_' + str(ra) + '_' +
        str(dec) + '_' + str(time.strftime('%H_%M_%d_%m_%y')) + '.npy', lnp)
    samples = sampler.chain[:, :, :].reshape((-1, ndim))
    samples_save = savedir + 'samples_burn_in_' + str(
        int(id)) + '_' + str(ra) + '_' + str(dec) + '_' + str(
            time.strftime('%H_%M_%d_%m_%y')) + '.npy'
    N.save(samples_save, samples)
    sampler.reset()
    print 'Burn in complete...'
    """ Main sampler run here..."""
    sampler.run_mcmc(pos, nsteps)
    lnpr = sampler.flatlnprobability
    N.save(
        savedir + 'lnprob_run_' + str(int(id)) + '_' + str(ra) + '_' +
        str(dec) + '_' + str(time.strftime('%H_%M_%d_%m_%y')) + '.npy', lnpr)
    samples = sampler.chain[:, :, :].reshape((-1, ndim))
    samples_save = savedir + 'samples_' + str(
        int(id)) + '_' + str(ra) + '_' + str(dec) + '_' + str(
            time.strftime('%H_%M_%d_%m_%y')) + '.npy'
    N.save(samples_save, samples)
    print 'Main emcee run completed.'
    P.close('all')
    P.clf()
    P.cla()
    return samples, samples_save
Beispiel #54
0
def plot_all_cyclones(cyclones):
    plt.figure(1)
    plt.cla()
    for cyclone in cyclones:
        plot_cyclone(cyclone)
Beispiel #55
0
        ax.annotate('Best fitting planet parameters: ' + '\n' + 'Period: ' + str(bestFperiod) + '\n' + 'RPRS: ' + str(bestFrprs), xy = (time[5], 0.04), bbox = bbox_props)
        fig.savefig('/Users/sheilasagear/OneDrive/K2_Research/CorrectedLC_EPICID/EPIC' + str(targetname) + '/EPIC' + str(targetname) + 'KRANSITfit.png')

        #show()
        """

##########################
#END LOOP
##########################


##########################
#INITIAL LIGHT CURVE
##########################

"""
pylab.cla()

fig, ax = plt.subplots(1, 1, figsize=[11,8])

ax.scatter(time, flux, color='k', s=2)
ax.plot(time, q, color='mediumaquamarine')
ax.set_xlabel('Time')
ax.set_ylabel('Corrected Flux (Normalized)')
ax.set_title('EPIC ' + str(targetname))

#show()
"""


#color for outliers