Exemplo n.º 1
0
 def plot(self,scatter=None,screen=True,out=None):
     """ Plot the VLEP landscape. """
     X = np.linspace(0.5,3.2,100)
     Y = np.linspace(-2,1.4,100)
     Z=np.zeros((100,100))
     for i,x in enumerate(X):
         for j,y in enumerate(Y):
             Z[j,i]=self._function(x,y)
             
     pl.contourf(X,Y,Z,100)
     pl.hot()
     
     if scatter!=None:
         pl.scatter(scatter[0,:],scatter[1,:],color='b')
     #if plot!=None:
         #pl.plot(plot[0,:],plot[1,:],color='y')
         #pl.scatter(plot[0,:],plot[1,:],color='y')
         
     if screen==True:
         pl.show()
     else:
         assert out!=None
         pl.savefig(out)
         pl.clf()
     self.it+=1
Exemplo n.º 2
0
    def plot(self, scatter=None, screen=True, out=None):
        """ Plot the VLEP landscape. """
        X = np.linspace(0.5, 3.2, 100)
        Y = np.linspace(-2, 1.4, 100)
        Z = np.zeros((100, 100))
        for i, x in enumerate(X):
            for j, y in enumerate(Y):
                Z[j, i] = self._function(x, y)

        pl.contourf(X, Y, Z, 100)
        pl.hot()

        if scatter != None:
            pl.scatter(scatter[0, :], scatter[1, :], color='b')
        #if plot!=None:
        #pl.plot(plot[0,:],plot[1,:],color='y')
        #pl.scatter(plot[0,:],plot[1,:],color='y')

        if screen == True:
            pl.show()
        else:
            assert out != None
            pl.savefig(out)
            pl.clf()
        self.it += 1
Exemplo n.º 3
0
def plothist(x, y, nbins=100, log=False,
			 doclf=True, docolorbar=True, dohot=True,
			 plo=None, phi=None,
			 imshowargs={}, **hist2dargs):
	if log:
		return loghist(x, y, nbins=nbins, doclf=doclf, docolorbar=docolorbar,
					   dohot=dohot, imshowargs=imshowargs) #, **kwargs)
					   
	if doclf:
		plt.clf()
	(H,xe,ye) = np.histogram2d(x, y, nbins, **hist2dargs)
	myargs = dict(extent=(min(xe), max(xe), min(ye), max(ye)),
				  aspect='auto',
				  interpolation='nearest', origin='lower')
	vmin = None
	if plo is not None:
		vmin = np.percentile(H.ravel(), plo)
		myargs.update(vmin=vmin)
	if phi is not None:
		vmin = imshowargs.get('vmin', vmin)
		vmax = np.percentile(H.ravel(), phi)
		if vmax != vmin:
			myargs.update(vmax=vmax)
	myargs.update(imshowargs)
	plt.imshow(H.T, **myargs)
	if dohot:
		plt.hot()
	if docolorbar:
		plt.colorbar()
	return H, xe, ye
Exemplo n.º 4
0
def S_QE(environ):
    '''
    A nice plot of S(Q,E)

    After loading a sqehist.pkl file, run the following 
    command will give you a very nice plot ready to
    be sent to PRL :)
    '''

    import pylab
    
    #change color palette
    pylab.hot()
    
    #show color bar
    pylab.colorbar()
    
    #change axis limits
    pylab.xlim(0, 10)
    pylab.ylim(-50, 50)
    
    #use latex
    pylab.rcParams['text.usetex'] = TexInstalled()
    
    #change labels, title
    pylab.xlabel(r"$Q {\rm(\AA^{-1})}$")
    pylab.ylabel(r"$E {\rm(eV)}$")
    pylab.title(r"$S(Q,E)$")
    
    #save picture
    pylab.savefig("sqe.png")
    return
Exemplo n.º 5
0
def S_QE(environ):
    '''
    A nice plot of S(Q,E)

    After loading a sqehist.pkl file, run the following 
    command will give you a very nice plot ready to
    be sent to PRL :)
    '''

    import pylab
    
    #change color palette
    pylab.hot()
    
    #show color bar
    pylab.colorbar()
    
    #change axis limits
    pylab.xlim(0,10)
    pylab.ylim(-50, 50)
    
    #use latex
    pylab.rcParams['text.usetex'] = TexInstalled()
    
    #change labels, title
    pylab.xlabel( r"$Q {\rm(\AA^{-1})}$" )
    pylab.ylabel( r"$E {\rm(eV)}$" )
    pylab.title( r"$S(Q,E)$" )
    
    #save picture
    pylab.savefig( "sqe.png" )
    return
Exemplo n.º 6
0
 def viz2D(self, filename, directory_="./", const_dir="z", logplot=False):
     """Visualize a slice."""
     directory = fixdir(directory_)
     data = load_fortran_unformatted(directory + filename)
     if self.param is None:
         self.load_param(directory)
     x = numpy.linspace(
         self.param.dx / 2.0,
         self.param.dx * self.param.mx - self.param.dx / 2.0,
         self.param.mx,
     )
     y = numpy.linspace(
         self.param.dy / 2.0,
         self.param.dy * self.param.my - self.param.dy / 2.0,
         self.param.my,
     )
     z = numpy.linspace(
         self.param.dz / 2.0,
         self.param.dz * self.param.mz - self.param.dz / 2.0,
         self.param.mz,
     )
     if const_dir == "x":
         n1 = self.param.my
         n2 = self.param.mz
         x1 = y
         x2 = z
         x1label = "y"
         x2label = "z"
     elif const_dir == "y":
         n1 = self.param.mx
         n2 = self.param.mz
         x1 = x
         x2 = z
         x1label = "x"
         x2label = "z"
     else:
         n1 = self.param.mx
         n2 = self.param.my
         x1 = x
         x2 = y
         x1label = "x"
         x2label = "y"
     data = data.reshape((n2, n1))
     pylab.clf()
     if logplot:
         data = 20 * numpy.log10(numpy.abs(data).clip(1e-30, 1e30))
         pylab.jet()
     else:
         pylab.hot()
     pylab.contour(x1, x2, data, 64)
     pylab.colorbar()
     pylab.axis("image")
     pylab.xlabel(x1label + " /um")
     pylab.ylabel(x2label + " /um")
     pylab.show()
Exemplo n.º 7
0
def visualize():
    '''
    Just some ugly plotting to make an animation for the lecture.
    '''
    import pylab as pp
    pp.ion()
    def remove_spines(axes):
        axes.set_xticks([])
        axes.set_yticks([])
        axes.spines['right'].set_color('none')
        axes.spines['top'].set_color('none')
        axes.spines['bottom'].set_color('none')
        axes.spines['left'].set_color('none')
    pfields, arena, aX, aY, centers = setup()
    theta = linspace(0,2*pi,250)
    x = cos(theta)*5 + 5
    y = sin(theta)*5 + 5
    points, estimates_bayes, estimates_direct = [],[], []
    for rx, ry in zip(x,y):
        handles = []
        rates, obs_spikes = simulate_spikes(pfields, rx,ry)
        pp.subplot(1,2,1)
        a = decode_bayes(pfields, obs_spikes, arena)
        pp.contour(aX,aY,a)
        pp.plot(centers[:,0],centers[:,1],'k+', alpha=.5)
        pp.hot()
        ey,ex = unravel_index(argmax(a), a.shape)
        estimates_bayes.append((aX[0,ex],aY[ey,0]))
        points.append((rx,ry))
        pp.plot(array(points)[:,0], array(points)[:,1], 'b-', alpha=0.5)
        pp.plot(rx,ry,'bo')
        pp.plot(aX[0,ex],aY[ey,0], 'ko')
        pp.plot(array(estimates_bayes)[:,0], array(estimates_bayes)[:,1], 'k-', alpha=0.5)
        pp.ylim([-.5,10.5])
        pp.xlim([-.5,10.5])
        remove_spines(pp.gca())

        pp.subplot(1,2,2)
        a = decode_directbasis(pfields, obs_spikes, arena)
        pp.contour(aX,aY,a)
        pp.plot(centers[:,0],centers[:,1],'k+', alpha=.5)
        pp.hot()
        ey,ex = unravel_index(argmax(a), a.shape)
        estimates_direct.append((aX[0,ex],aY[ey,0]))
        points.append((rx,ry))
        pp.plot(array(points)[:,0], array(points)[:,1], 'b-',alpha=.5)
        pp.plot(rx,ry,'bo')
        pp.plot(aX[0,ex],aY[ey,0], 'ko')
        pp.plot(array(estimates_direct)[:,0], array(estimates_direct)[:,1], 'k-', alpha=.5)
        pp.ylim([-.5,10.5])
        pp.xlim([-.5,10.5])
        remove_spines(pp.gca())
        yield(handles)
Exemplo n.º 8
0
Arquivo: FDTD.py Projeto: seghil/EMpy
 def viz2D(self, filename, directory_='./', const_dir='z', logplot=False):
     """Visualize a slice."""
     directory = fixdir(directory_)
     data = load_fortran_unformatted(directory + filename)
     if self.param is None:
         self.load_param(directory)
     x = numpy.linspace(self.param.dx / 2.,
                        self.param.dx * self.param.mx - self.param.dx / 2.,
                        self.param.mx)
     y = numpy.linspace(self.param.dy / 2.,
                        self.param.dy * self.param.my - self.param.dy / 2.,
                        self.param.my)
     z = numpy.linspace(self.param.dz / 2.,
                        self.param.dz * self.param.mz - self.param.dz / 2.,
                        self.param.mz)
     if const_dir == 'x':
         n1 = self.param.my
         n2 = self.param.mz
         x1 = y
         x2 = z
         x1label = 'y'
         x2label = 'z'
     elif const_dir == 'y':
         n1 = self.param.mx
         n2 = self.param.mz
         x1 = x
         x2 = z
         x1label = 'x'
         x2label = 'z'
     else:
         n1 = self.param.mx
         n2 = self.param.my
         x1 = x
         x2 = y
         x1label = 'x'
         x2label = 'y'
     data = data.reshape((n2, n1))
     pylab.clf()
     if logplot:
         data = 20 * numpy.log10(numpy.abs(data).clip(1e-30, 1e30))
         pylab.jet()
     else:
         pylab.hot()
     pylab.contour(x1, x2, data, 64)
     pylab.colorbar()
     pylab.axis('image')
     pylab.xlabel(x1label + ' /um')
     pylab.ylabel(x2label + ' /um')
     pylab.show()
Exemplo n.º 9
0
def plotCorrespondences2(imgsources, refsources, matches, wcs, W, H, prefix,
                         saveplot=True, format='png'):
    print 'ix,iy'
    ix = np.array([s.getXAstrom() for s in imgsources])
    iy = np.array([s.getYAstrom() for s in imgsources])

    print 'rx,ry'
    rx,ry = [],[]
    for r in refsources:
        xy = wcs.skyToPixel(r.getRaDec())
        rx.append(xy[0])
        ry.append(xy[1])
    rx = np.array(rx)
    ry = np.array(ry)

    ixy = np.vstack((ix, iy)).T
    rxy = np.vstack((rx, ry)).T

    print 'plotshift...'
    cell = 10
    plotshift(ixy, rxy, dcell=cell, ncells=9, W=W, H=H)
    fn = prefix + '-shift1.' + format
    P1 = _output(fn, format, saveplot)

    print 'plotshift 2...'
    plt.clf()
    plt.hot()
    plotshift(ixy, rxy, dcell=cell, ncells=9, W=W, H=H, hist=True, nhistbins=2*cell+1)
    fn = prefix + '-shift2.' + format
    P2 = _output(fn, format, saveplot)

    print 'plotshift 3...'
    cell = 2
    plotshift(ixy, rxy, dcell=cell, ncells=9, W=W, H=H)
    fn = prefix + '-shift3.' + format
    P3 = _output(fn, format, saveplot)

    print 'plotshift 4...'
    plt.clf()
    plt.hot()
    plotshift(ixy, rxy, dcell=cell, ncells=9, W=W, H=H, hist=True, nhistbins=10*cell+1)
    fn = prefix + '-shift4.' + format
    P4 = _output(fn, format, saveplot)

    if not saveplot:
        P1.update(P2)
        P1.update(P3)
        P1.update(P4)
        return P1
 def plot(self, nx=100, ny=100, out='screen'):
     """ Make 2D contour-color plot of the data. """
     b = np.empty((nx, ny))
     X = np.linspace(self.xgrid[0], self.xgrid[1], nx)
     Y = np.linspace(self.ygrid[0], self.ygrid[1], ny)
     for i, x in enumerate(X):
         for j, y in enumerate(Y):
             b[i, j] = self(x, y)
     pl.contourf(b, 100)
     pl.hot()
     pl.colorbar()
     if out == 'screen':
         pl.show()
     else:
         pl.savefig('linter.png')
Exemplo n.º 11
0
 def plot(self,nx=100,ny=100,out='screen'):
     """ Make 2D contour-color plot of the data. """
     b=np.empty((nx,ny))
     X=np.linspace(self.xgrid[0],self.xgrid[1],nx)
     Y=np.linspace(self.ygrid[0],self.ygrid[1],ny)
     for i,x in enumerate(X):
         for j,y in enumerate(Y):
             b[i,j]=self(x,y)
     pl.contourf(b,100)
     pl.hot()
     pl.colorbar()
     if out=='screen':
         pl.show()
     else:
         pl.savefig('linter.png')
Exemplo n.º 12
0
 def plot(self, sumx = 1, nxy = 100, nz = 100, z0 = 0):
     if sumx is None: # sum in y
         x = self.solver.modes[0].get_y(nxy)
         axis = 0
     else: # sum in x
         x = self.solver.modes[0].get_x(nxy)
         axis = 1
     z = numpy.linspace(0, self.length, nz)
     const_z = numpy.ones_like(z)
     f = numpy.zeros((len(x), len(z)), dtype = complex)
     for im, (coeffLHS,coeffRHS) in enumerate(zip(self.inputLHS, self.inputRHS)):
         m = self.solver.modes[im]
         beta = 2 * numpy.pi / self.solver.wl * m.neff
         tmp = numpy.sum(m.intensity(x, x), axis = axis)
         f += numpy.abs(coeffLHS)**2 * tmp[:, numpy.newaxis] * const_z + numpy.abs(coeffRHS)**2 * tmp[:, numpy.newaxis] * const_z
     pylab.hot()
     pylab.contourf(x, z0 + z, numpy.abs(f).T, 16)
Exemplo n.º 13
0
 def plot(self, sumx=1, nxy=100, nz=100, z0=0):
     if sumx is None: # sum in y
         x = self.solver.modes[0].get_y(nxy)
         axis = 0
     else: # sum in x
         x = self.solver.modes[0].get_x(nxy)
         axis = 1
     z = numpy.linspace(0, self.length, nz)
     const_z = numpy.ones_like(z)
     f = numpy.zeros((len(x), len(z)), dtype=complex)
     for im, (coeffLHS,coeffRHS) in enumerate(zip(self.inputLHS, self.inputRHS)):
         m = self.solver.modes[im]
         beta = 2 * numpy.pi / self.solver.wl * m.neff
         tmp = numpy.sum(m.intensity(x, x), axis=axis)
         f += numpy.abs(coeffLHS)**2 * tmp[:, numpy.newaxis] * const_z + \
              numpy.abs(coeffRHS)**2 * tmp[:, numpy.newaxis] * const_z
     pylab.hot()
     pylab.contourf(x, z0 + z, numpy.abs(f).T, 16)
Exemplo n.º 14
0
Arquivo: FDTD.py Projeto: rshegde/EMpy
 def viz2D(self, filename, directory_="./", const_dir="z", logplot=False):
     """Visualize a slice."""
     directory = fixdir(directory_)
     data = load_fortran_unformatted(directory + filename)
     if self.param is None:
         self.load_param(directory)
     x = numpy.linspace(self.param.dx / 2.0, self.param.dx * self.param.mx - self.param.dx / 2.0, self.param.mx)
     y = numpy.linspace(self.param.dy / 2.0, self.param.dy * self.param.my - self.param.dy / 2.0, self.param.my)
     z = numpy.linspace(self.param.dz / 2.0, self.param.dz * self.param.mz - self.param.dz / 2.0, self.param.mz)
     if const_dir == "x":
         n1 = self.param.my
         n2 = self.param.mz
         x1 = y
         x2 = z
         x1label = "y"
         x2label = "z"
     elif const_dir == "y":
         n1 = self.param.mx
         n2 = self.param.mz
         x1 = x
         x2 = z
         x1label = "x"
         x2label = "z"
     else:
         n1 = self.param.mx
         n2 = self.param.my
         x1 = x
         x2 = y
         x1label = "x"
         x2label = "y"
     data = data.reshape((n2, n1))
     pylab.clf()
     if logplot:
         data = 20 * numpy.log10(numpy.abs(data).clip(1e-30, 1e30))
         pylab.jet()
     else:
         pylab.hot()
     pylab.contour(x1, x2, data, 64)
     pylab.colorbar()
     pylab.axis("image")
     pylab.xlabel(x1label + " /um")
     pylab.ylabel(x2label + " /um")
     pylab.show()
Exemplo n.º 15
0
 def visualiseNormObject(self):
     shape = (2 * self.extent, 2 * self.extent)
     pylab.ion()
     pylab.clf()
     #pylab.set_cmap("bone")
     pylab.hot()
     pylab.title("image: %s" % self.fitsFile)
     pylab.imshow(np.reshape(self.signPreserveNorm(), shape, order="F"),
                  interpolation="nearest")
     pylab.plot(np.arange(0, 2 * self.extent),
                self.extent * np.ones((2 * self.extent, )), "r--")
     pylab.plot(self.extent * np.ones((2 * self.extent, )),
                np.arange(0, 2 * self.extent), "r--")
     pylab.colorbar()
     pylab.ylim(-1, 2 * self.extent)
     pylab.xlim(-1, 2 * self.extent)
     pylab.xlabel("Pixels")
     pylab.ylabel("Pixels")
     pylab.show()
Exemplo n.º 16
0
def plothist(x, y, nbins=100, log=False,
			 doclf=True, docolorbar=True, dohot=True,
			 imshowargs={}, **hist2dargs):
	if log:
		return loghist(x, y, nbins=nbins, doclf=doclf, docolorbar=docolobar,
					   dohot=dohit, imshowargs=imshowargs, **kwargs)
					   
	if doclf:
		plt.clf()
	(H,xe,ye) = np.histogram2d(x, y, nbins, **hist2dargs)
	myargs = dict(extent=(min(xe), max(xe), min(ye), max(ye)),
				  aspect='auto',
				  interpolation='nearest', origin='lower')
	myargs.update(imshowargs)
	plt.imshow(H.T, **myargs)
	if dohot:
		plt.hot()
	if docolorbar:
		plt.colorbar()
	return H, xe, ye
Exemplo n.º 17
0
 def plot(self, n):
     """Plot the sensor's fields."""
     pylab.clf()
     pylab.hot()
     pylab.subplot(2,2,1)
     pylab.contour(numpy.abs(self.E1[:,:,n].T), 16)
     pylab.axis('image')
     pylab.title('E1')
     pylab.subplot(2,2,2)
     pylab.contour(numpy.abs(self.H1[:,:,n].T), 16)
     pylab.axis('image')
     pylab.title('H1')
     pylab.subplot(2,2,3)
     pylab.contour(numpy.abs(self.E2[:,:,n].T), 16)
     pylab.axis('image')
     pylab.title('E2')
     pylab.subplot(2,2,4)
     pylab.contour(numpy.abs(self.H2[:,:,n].T), 16)
     pylab.axis('image')
     pylab.title('H2')
     pylab.show()
Exemplo n.º 18
0
Arquivo: FDTD.py Projeto: rshegde/EMpy
 def plot(self, n):
     """Plot the sensor's fields."""
     pylab.clf()
     pylab.hot()
     pylab.subplot(2, 2, 1)
     pylab.contour(numpy.abs(self.E1[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("E1")
     pylab.subplot(2, 2, 2)
     pylab.contour(numpy.abs(self.H1[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("H1")
     pylab.subplot(2, 2, 3)
     pylab.contour(numpy.abs(self.E2[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("E2")
     pylab.subplot(2, 2, 4)
     pylab.contour(numpy.abs(self.H2[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("H2")
     pylab.show()
Exemplo n.º 19
0
Arquivo: FDTD.py Projeto: seghil/EMpy
 def plot(self, n):
     """Plot the sensor's fields."""
     pylab.clf()
     pylab.hot()
     pylab.subplot(2, 2, 1)
     pylab.contour(numpy.abs(self.E1[:, :, n].T), 16)
     pylab.axis('image')
     pylab.title('E1')
     pylab.subplot(2, 2, 2)
     pylab.contour(numpy.abs(self.H1[:, :, n].T), 16)
     pylab.axis('image')
     pylab.title('H1')
     pylab.subplot(2, 2, 3)
     pylab.contour(numpy.abs(self.E2[:, :, n].T), 16)
     pylab.axis('image')
     pylab.title('E2')
     pylab.subplot(2, 2, 4)
     pylab.contour(numpy.abs(self.H2[:, :, n].T), 16)
     pylab.axis('image')
     pylab.title('H2')
     pylab.show()
Exemplo n.º 20
0
 def plot(self, n):
     """Plot the sensor's fields."""
     pylab.clf()
     pylab.hot()
     pylab.subplot(2, 2, 1)
     pylab.contour(numpy.abs(self.E1[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("E1")
     pylab.subplot(2, 2, 2)
     pylab.contour(numpy.abs(self.H1[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("H1")
     pylab.subplot(2, 2, 3)
     pylab.contour(numpy.abs(self.E2[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("E2")
     pylab.subplot(2, 2, 4)
     pylab.contour(numpy.abs(self.H2[:, :, n].T), 16)
     pylab.axis("image")
     pylab.title("H2")
     pylab.show()
Exemplo n.º 21
0
    for i in xrange(nodes-1):
        topology.append(('right', i, i+1))
        topology.append(('left', i+1, i))
    
    # starts the task
    task = start_task(HeatSolver, # name of the task class
                      cpu = nodes, # use <nodes> CPUs on the local machine
                      topology = topology,
                      args=(split_y, dx, dt, iterations))
                                              
    # Retrieves the result, as a list with one element returned by MonteCarlo.get_result per node
    result = task.get_result()
    result = np.hstack(result)
    
    return result

if __name__ == '__main__':
    result = heat2d(100,2)
    pl.hot()
    pl.imshow(result)

#    x = np.linspace(-1.,1.,98)
#    X,Y= np.meshgrid(x,x)
#    import matplotlib.pyplot as plt
#    from matplotlib import cm
#    from mpl_toolkits.mplot3d import Axes3D
#    fig = pl.figure()
#    ax = fig.add_subplot(111, projection='3d')
#    ax.plot_wireframe(X,Y,result)

    pl.show()
Exemplo n.º 22
0
def alignAndPlot(Tme, Tref, rad, ps, doweighted=True, emrad=None, nearest=False, **kwargs):
    aliargs = dict(cutrange=emrad)
    aliargs.update(kwargs)
    A = Alignment(Tme, Tref, searchradius=rad, **aliargs)
    if nearest:
        # There is something badly wrong with spherematch.nearest().
        assert(False)
        A.findMatches(nearest=True)
        M = A.match
        print 'dra,ddec arcsec:', M.dra_arcsec[:100], M.ddec_arcsec[:100]

    if A.shift() is None:
        print 'Shift not found!'
        return None
    M = A.match
    print 'Shift:', A.arcsecshift()
    sr,sd = A.arcsecshift()

    sumd2 = np.sum(A.fore * ((M.dra_arcsec [A.subset] - sr)**2 +
                             (M.ddec_arcsec[A.subset] - sd)**2))
    sumw  = np.sum(A.fore)
    # / 2. to get std per coord.
    std   = sqrt(sumd2 / (sumw * 2.))
    angles = np.linspace(0, 2.*pi, 100)

    modstr = ''
    if A.cov:
        eigs = A.getEllipseSize() * 1000.
        if eigs[0] > 100:
            modstr = '%.0fx%.0f' % (eigs[0], eigs[1])
        else:
            modstr = '%.1fx%.1f' % (eigs[0], eigs[1])
    else:
        modstr = '%.1f' % (1000. * A.sigma)

    W = np.zeros_like(A.subset).astype(float)
    W[A.subset] = A.fore

    rl,rh = Tme.ra.min(), Tme.ra.max()
    dl,dh = Tme.dec.min(), Tme.dec.max()

    if doweighted:
        rounds = [ {}, { 'weights': W } ]
    else:
        rounds = [ {} ]

    for i,args in enumerate(rounds):
        tsuf = '' if i == 0 else ' (weighted)'
        N = len(M.dra_arcsec) if i == 0 else sumw

        plotresids(Tme, M, '%s-%s match residuals%s' % (Tme.cam, Tref.cam, tsuf),
                   bins=100, **args)
        ps.savefig()

        dst = 1000. * np.sqrt(M.dra_arcsec ** 2 + M.ddec_arcsec ** 2)
        loghist(Tme.mag[M.I], dst, 100, **args)
        plt.xlabel(Tme.filter)
        plt.ylabel('Match residual (mas)')
        ps.savefig()

        loghist(Tref.mag[M.J], dst, 100, **args)
        plt.xlabel(Tref.filter)
        plt.ylabel('Match residual (mas)')
        ps.savefig()

        H,xe,ye = plotalignment(A)
        # show EM circle
        ax = plt.axis()
        angles = np.linspace(0, 2.*pi, 100)
        c = A.cutcenter
        r = A.cutrange
        plt.plot(c[0] + r * np.cos(angles), c[1] + r * np.sin(angles), 'g--')
        plt.axis(ax)
        plt.title('%s-%s (%i matches, std %.1f mas, model %s)%s' %
                  (Tme.cam, Tref.cam, int(sumw), std*1000., modstr, tsuf))
        ps.savefig()

        bins = 200
        edges = np.linspace(-rad, rad, bins)
        DR,DD = np.meshgrid(edges, edges)
        em = A.getModel(DR.ravel(), DD.ravel()).reshape(DR.shape)
        em *= len(M.dra_arcsec) * (edges[1]-edges[0])**2
        R2 = DR**2 + DD**2
        em[R2 > (A.match.rad)**2] = 0.
        plt.clf()
        plt.imshow(em, extent=(-rad, rad, -rad, rad),
                   aspect='auto', interpolation='nearest', origin='lower',
                   vmin=H.min(), vmax=H.max())
        plt.hot()
        plt.colorbar()
        plt.xlabel('dRA (arcsec)')
        plt.ylabel('dDec (arcsec)')
        plt.title('EM model')
        ps.savefig()

        plotfitquality(H, xe, ye, A)
        ps.savefig()




        rng = ((-5*std, 5*std), (-5*std, 5*std))
        myargs = args.copy()
        myargs.update({'range':rng})
        plothist(M.dra_arcsec - sr, M.ddec_arcsec - sd, 200, **myargs)
        ax = plt.axis()
        plt.xlabel('dRA (arcsec)')
        plt.ylabel('dDec (arcsec)')
        for nsig in [1,2]:
            X,Y = A.getContours(nsig)
            plt.plot(X-sr, Y-sd, 'b-')
        plt.axis(ax)
        plt.title('%s-%s (matches: %i, std: %.1f mas, model %s)%s' %
                  (Tme.cam, Tref.cam, int(sumw), std*1000., modstr, tsuf))
        ps.savefig()

        #plothist(Tme.mag[M.I], Tref.mag[M.J], 100, **args)
        #plt.xlabel('%s %s (mag)' % (Tme.cam, Tme.filter))
        #plt.ylabel('%s %s (mag)' % (Tref.cam, Tref.filter))
        #fn = '%s-%s.png' % (basefn, chr(ploti))
        #plt.title('%s-%s%s' % (Tme.cam, Tref.cam, tsuf))
        #plt.savefig(fn)
        #print 'saved', fn
        #ploti += 1

        loghist(Tme.mag[M.I], Tref.mag[M.J], 100, **args)
        plt.xlabel('%s (mag)' % (Tme.filter))
        plt.ylabel('%s (mag)' % (Tref.filter))
        plt.title('%s-%s%s' % (Tme.cam, Tref.cam, tsuf))
        ps.savefig()

        plothist(Tme.ra[M.I], Tme.dec[M.I], 100, range=((rl,rh),(dl,dh)))
        setRadecAxes(rl,rh,dl,dh)
        plt.title('%s-%s: %i matches%s' % (Tme.cam, Tref.cam, N, tsuf))
        ps.savefig()

    return A
#Do parameter variations

#import library functions
from pylab import plot, show, figure, xlabel, ylabel, legend, title, autumn,\
                  hot, cm
from numpy import array, linspace, logspace, hstack
#import the compiled simulation objects
from competition import Case1, Case2, Case3, Case4

lsp = linspace

#Case 3 ----------------------------------------------
#vary the growt rate of organism 1
mo = Case3() #create a simulaton object instance
figure() #create new figure window
hot()
#create a couple of initial values for x and y
r1Vals = linspace(0.5, 3, 10)
col =    linspace(0, 1, 10)

#do simulations with the different parameter values, and put the results into a
#phase-plane plot
for i in range(len(r1Vals)):
    #initialize the simulation object, override some of the parameter values
    mo.initialize('m.r1', r1Vals[i],
                  'N1_init', 1,
                  'N2_init', 2,
                  'solutionParameters.simulationTime', 20,
                  'showGraph', 0)
    mo.simulateDynamic()   #solve ODE
    res = mo.getResults()  #get results as a storage.DictStore object
Exemplo n.º 24
0
    if r == 0:
        return 1 / 4

    Lambda = 0.5  # in micrometers
    kr = 2 * pi / Lambda * r
    return (J(1, kr) / kr)**2


side = 2  # length in micrometers
points = 200  # number of grid points in each direction
spacing = side / points

# Calculate the position of the center
x_center = side / 2
y_center = side / 2

# Make an empty array to store values
intensities = empty([points, points], float)

# Calculate the values in the array
for i in range(points):
    y = spacing * i
    for j in range(points):
        x = spacing * j
        dist = r(x - x_center, y - y_center)
        intensities[i, j] = I(dist)

imshow(intensities, origin="lower", extent=[0, side, 0, side], vmax=0.01)
hot()
show()
Exemplo n.º 25
0
        psf.scale = scale
        im = psf.instantiateAt(0., 0.)

        ny, nx = im.shape
        XX, YY = np.meshgrid(np.arange(nx), np.arange(ny))
        print('cx', (np.sum(im * XX) / np.sum(im)))
        print('cy', (np.sum(im * YY) / np.sum(im)))

        plt.clf()
        mx = im.max()
        plt.imshow(im,
                   origin='lower',
                   interpolation='nearest',
                   vmin=-0.1 * mx,
                   vmax=mx * 1.1)
        plt.hot()
        plt.colorbar()
        ps.savefig()

    print('PSF scale', psf.sampling)
    print('1./scale', 1. / psf.sampling)

    YY = np.linspace(0, 4096, 5)
    XX = np.linspace(0, 2048, 5)

    yims = []
    for y in YY:
        xims = []
        for x in XX:
            im = psf.instantiateAt(x, y)
            im /= im.sum()
Exemplo n.º 26
0
		def show(im):
			plt.imshow(np.log10(np.maximum(1e-3, im + 1e-3)), vmin=-3, vmax=0, **ima)
			plt.hot()
			plt.colorbar()
Exemplo n.º 27
0
def display_maps(Detect, Latencies, fig, inner_grid, expe):

    ax = Subplot(fig, inner_grid[0])

    St = Detect.Sig_strength
    #----------------------------------------
    # NEED TO REMAP FOR EXPS 20 22 and 23, and 27!!
    if expe <= 22:
        aux = copy(St[24])
        for w in [24, 23, 22, 21]:
            St[w] = copy(St[w - 1])
        St[20] = copy(St[11])
        St[11] = aux
    if expe == 23:
        for w in [0, 1, 2, 3]:
            St[w] = copy(St[w + 1])
        St[4] = [0, 0]
    #if exp == 27:
    #    St[16]== [0,0]
    #---------------------------------------------

    Stup = np.reshape(St[:, 0], (5, 5)) / St.max()
    Stdn = np.reshape(St[:, 1], (5, 5)) / St.max()

    # only do if they exist
    if St.max() > 0:

        im = ax.imshow(Stup, interpolation='none', cmap=pylab.gray())

        # create an axes on the right side of ax. The width of cax will be 5%
        # of ax and the padding between cax and ax will be fixed at 0.05 inch.
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_xticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_yticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_xticklabels(['St', '1', '2', '3',
                            '4'])  # range of values in edges
        ax.set_yticklabels(['A', 'B ', 'C ', 'D',
                            'E '])  # range of values in edges
        ax.tick_params('both', length=0, width=0)

        ax.set_title('Caudal Strength')
        fig.add_subplot(ax)
        #---------------------------------------------------------------------------------------------
        ax = Subplot(fig, inner_grid[1])

        im = ax.imshow(Stdn, interpolation='none', cmap=pylab.gray())

        ax.set_xticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_yticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_xticklabels(['St', '1', '2', '3',
                            '4'])  # range of values in edges
        ax.set_yticklabels(['A', 'B ', 'C ', 'D',
                            'E '])  # range of values in edges
        ax.tick_params('both', length=0, width=0)

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_title('Rostral Strength')
        fig.add_subplot(ax)
        #---------------------------------------------------------------------------------------------
        ax = Subplot(fig, inner_grid[2])

        Stboth = np.nan_to_num((Stup - Stdn) / (Stup + Stdn))
        # make a color map of fixed colors
        cmap = colors.ListedColormap([
            '#00cc00', '#00aa00', '#006600', '#003300', 'black', '#000066',
            '#000099', '#0000cc', 'blue'
        ])
        bounds = [-1, -0.75, -0.5, -0.25, -0.01, 0.01, 0.25, 0.5, 0.75, 1]
        norm = colors.BoundaryNorm(bounds, cmap.N)

        im = ax.imshow(Stboth, interpolation='none', cmap=cmap, norm=norm)

        ax.set_xticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_yticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_xticklabels(['St', '1', '2', '3',
                            '4'])  # range of values in edges
        ax.set_yticklabels(['A', 'B ', 'C ', 'D',
                            'E '])  # range of values in edges
        ax.tick_params('both', length=0, width=0)

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_title('Directionality')
        fig.add_subplot(ax)
        #---------------------------------------------------------------------------------------------
        ax = Subplot(fig, inner_grid[3])

        Lat = zeros(25)
        for i in arange(25):
            if Latencies[i, 0] > 5 and Latencies[i, 1] > 5:
                Lat[i] = min(Latencies[i, 0], Latencies[i, 1])
            elif Latencies[i, 0] > 5:
                Lat[i] = Latencies[i, 0]
            elif Latencies[i, 1] > 5:
                Lat[i] = Latencies[i, 1]
        #----------------------------------------
        # NEED TO REMAP FOR EXPS 20 22 and 23, and 27!!
        if expe <= 22:
            aux = copy(Lat[24])
            for w in [24, 23, 22, 21]:
                Lat[w] = copy(Lat[w - 1])
            Lat[20] = copy(Lat[11])
            Lat[11] = aux
        if expe == 23:
            for w in [0, 1, 2, 3]:
                Lat[w] = copy(Lat[w + 1])
            Lat[4] = 0
        #---------------------------------------------

        Lat = np.reshape(Lat, (5, 5))
        im = ax.imshow(Lat, interpolation='none', cmap=pylab.hot())

        ax.set_xticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_yticks([0, 1, 2, 3, 4])  # range of values in edges
        ax.set_xticklabels(['St', '1', '2', '3',
                            '4'])  # range of values in edges
        ax.set_yticklabels(['A', 'B ', 'C ', 'D',
                            'E '])  # range of values in edges
        ax.tick_params('both', length=0, width=0)

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_title('Latency')
        fig.add_subplot(ax)
Exemplo n.º 28
0
def test_vs_stars_on(run, camcol, field, band, ps):

	bandnum = band_index(band)
	datadir = os.path.join(os.path.dirname(__file__), 'testdata')
	sdss = DR9(basedir=datadir)

	sdss.retrieve('psField', run, camcol, field)
	psfield = sdss.readPsField(run, camcol, field)

	sdss.retrieve('frame', run, camcol, field, band)
	frame = sdss.readFrame(run, camcol, field, band)
	img = frame.image
	H,W = img.shape

	fn = sdss.retrieve('photoObj', run, camcol, field)
	T = fits_table(fn)
	T.mag  = T.get('psfmag')[:,bandnum]
	T.flux = T.get('psfflux')[:,bandnum]
	# !!
	T.x = T.colc[:,bandnum] - 0.5
	T.y = T.rowc[:,bandnum] - 0.5
	T.cut(T.prob_psf[:,bandnum] == 1)
	T.cut(T.nchild == 0)
	T.cut(T.parent == -1)
	T.cut(T.flux > 1.)
	print len(T), 'after flux cut'
	#T.cut(T.flux > 10.)
	#print len(T), 'after flux cut'
	#T.cut(T.flux > 20.)
	#print len(T), 'after flux cut'
	T.cut(np.argsort(-T.flux)[:25])
	# margin
	m = 30
	T.cut((T.x >= m) * (T.x < (W-m)) * (T.y >= m) * (T.y < (H-m)))
	#T.cut(np.argsort(T.mag))
	T.cut(np.argsort(-np.abs(T.x - T.y)))
	
	print len(T), 'PSF stars'
		
	#R,C = 5,5
	#plt.clf()

	eigenpsfs  = psfield.getEigenPsfs(bandnum)
	eigenpolys = psfield.getEigenPolynomials(bandnum)
	RR,CC = 2,2
	
	xx,yy = np.meshgrid(np.linspace(0, W, 12), np.linspace(0, H, 8))

	ima = dict(interpolation='nearest', origin='lower')
	
	plt.clf()
	mx = None
	for i,(psf,poly) in enumerate(zip(eigenpsfs, eigenpolys)):
		print
		print 'Eigen-PSF', i
		XO,YO,C = poly
		kk = np.zeros_like(xx)
		for xo,yo,c in zip(XO,YO,C):
			dk = (xx ** xo) * (yy ** yo) * c
			#print 'xo,yo,c', xo,yo,c, '-->', dk
			kk += dk
		print 'Max k:', kk.max(), 'min', kk.min()
		print 'PSF range:', psf.min(), psf.max()
		print 'Max effect:', max(np.abs(kk.min()), kk.max()) * max(np.abs(psf.min()), psf.max())
		
		plt.subplot(RR,CC, i+1)
		plt.imshow(psf * kk.max(), **ima)
		if mx is None:
			mx = (psf * kk.max()).max()
		else:
			plt.clim(-mx * 0.05, mx * 0.05)
		plt.colorbar()
	ps.savefig()

	psfs = psfield.getPsfAtPoints(bandnum, xx,yy)
	#print 'PSFs:', psfs
	ph,pw = psfs[0].shape
	psfs = np.array(psfs).reshape(xx.shape + (ph,pw))
	print 'PSFs shape:', psfs.shape
	psfs = psfs[:,:,15:36,15:36]
	ny,nx,ph,pw = psfs.shape
	psfmos = np.concatenate([psfs[i,:,:,:] for i in range(ny)], axis=1)
	print 'psfmos', psfmos.shape
	psfmos = np.concatenate([psfmos[i,:,:] for i in range(nx)], axis=1)
	print 'psfmos', psfmos.shape
	plt.clf()
	plt.imshow(np.log10(np.maximum(psfmos + 1e-3, 1e-3)), **ima)
	ps.savefig()
	
	diffs = None
	rmses = None
	
	for i in range(len(T)):

		xx = T.x[i]
		yy = T.y[i]
		
		ix = int(np.round(xx))
		iy = int(np.round(yy))
		dx = xx - ix
		dy = yy - iy

		S = 25
		stamp = img[iy-S:iy+S+1, ix-S:ix+S+1]

		L = 6
		Lx = lanczos_filter(L, np.arange(-L, L+1) - dx)
		Ly = lanczos_filter(L, np.arange(-L, L+1) - dy)
		sx = correlate1d(stamp, Lx, axis=1, mode='constant')
		shim = correlate1d(sx,  Ly, axis=0, mode='constant')
		shim /= (Lx.sum() * Ly.sum())
		
		psf = psfield.getPsfAtPoints(bandnum, xx, yy)
		mod = psf / psf.sum() * T.flux[i]

		psf2 = psfield.getPsfAtPoints(bandnum, yy, xx)
		mod2 = psf2 / psf2.sum() * T.flux[i]

		psf3 = psfield.getPsfAtPoints(bandnum, xx, yy).T
		mod3 = psf3 / psf3.sum() * T.flux[i]

		mods = [mod, mod2, mod3]

		if diffs is None:
			diffs = [np.zeros_like(m) for m in mods]
			rmses = [np.zeros_like(m) for m in mods]
			
		for m,rms,diff in zip(mods, rmses, diffs):
			diff += (m - shim)
			rms +=  (m - shim)**2

		if i > 10:
			continue
		
		def show(im):
			plt.imshow(np.log10(np.maximum(1e-3, im + 1e-3)), vmin=-3, vmax=0, **ima)
			plt.hot()
			plt.colorbar()
		
		R,C = 3,4
		plt.clf()
		plt.subplot(R,C,1)
		show(shim)
		plt.subplot(R,C,2)
		show(mods[0])
		plt.subplot(R,C,2+C)
		plt.imshow(mods[0] - shim, vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.subplot(R,C,3)
		show(mods[1])

		plt.subplot(R,C,3+C)
		plt.imshow(mods[1] - shim, vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()
		
		plt.subplot(R,C,4)
		show(mods[2])
		
		plt.subplot(R,C,4+C)
		plt.imshow(mods[2] - shim, vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.subplot(R,C,3+2*C)
		plt.imshow(mods[1] - mods[0], vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.subplot(R,C,4+2*C)
		plt.imshow(mods[2] - mods[0], vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.suptitle('%.0f, %.0f' % (xx,yy))
		ps.savefig()

	for diff in diffs:
		diff /= len(T)

	rmses = [np.sqrt(rms / len(T)) for rms in rmses]

	print 'rms median', np.median(rmses[0]), 'mean', np.mean(rmses[0])

	r0,r1 = [np.percentile(rmses[0], p) for p in [10,90]]
	
	R,C = 2,len(diffs)
	plt.clf()

	for i,(d,r) in enumerate(zip(diffs, rmses)):
		plt.subplot(R,C, 1+i)
		plt.imshow(d, vmin=-0.05, vmax=0.05, **ima)
		plt.colorbar()
		plt.hot()

		plt.subplot(R,C, 1+i+C)
		plt.imshow(r, vmin=r0, vmax=r1, **ima)
		plt.colorbar()
		plt.hot()
	
	ps.savefig()
Exemplo n.º 29
0
def makeplots(tractor, step, suffix):
	print 'Making plots'

	ds = tractor.getCatalog()[0]
	logsa, logt, emis = ds.getArrays()
	suff = '.pdf'
	
	#plt.figure(figsize=(8,6))
	#plt.figure(figsize=(4.5,4))
	#plt.figure(figsize=(4,3.5))

	#spa = dict(left=0.01, right=0.99, bottom=0.02, top=0.92)
	spa = dict(left=0.005, right=0.995, bottom=0.02, top=0.92)
	H = 3.
	W1 = 3.5
	W2 = (0.90 / 0.99) * H
	print 'W2', W2

	plt.figure(figsize=(W1,H))
	plt.subplots_adjust(**spa)

	logsa = np.fliplr(logsa)
	logt = np.fliplr(logt)
	emis = np.fliplr(emis)
	
	# plt.clf()
	# plt.imshow(logsa, interpolation='nearest', origin='lower')
	# plt.gray()
	# plt.colorbar()
	# plt.title('Dust: log(solid angle)')
	# plt.savefig('logsa-%02i%s.png' % (step, suffix))
	
	plt.clf()
	#plt.imshow(np.exp(logsa), interpolation='nearest', origin='lower')
	plt.imshow(np.exp(logsa) * 1000., interpolation='nearest', origin='lower',
			   vmin=0., vmax=6.)
	plt.hot()
	plt.colorbar(ticks=[0,2,4,6])
	plt.xticks([])
	plt.yticks([])
	plt.title('Dust Column Density (arb units)') #: solid angle')
	plt.savefig(('sa-%02i%s'+suff) % (step, suffix))
	
	# plt.clf()
	# plt.imshow(logt, interpolation='nearest', origin='lower')
	# plt.gray()
	# plt.colorbar()
	# plt.title('Dust: log(temperature)')
	# plt.savefig('logt-%02i%s'+suff % (step, suffix))
	
	plt.clf()
	plt.imshow(np.exp(logt), interpolation='nearest', origin='lower', vmin=0, vmax=30)
	plt.hot()
	plt.colorbar(ticks=[0,10,20,30])
	plt.xticks([])
	plt.yticks([])
	plt.title('Dust Temperature (K)')
	plt.savefig(('t-%02i%s'+suff) % (step, suffix))
	
	plt.clf()
	plt.imshow(emis, interpolation='nearest', origin='lower',
			   vmin=1, vmax=2.5)
	plt.gray()
	plt.colorbar(ticks=[1.0, 1.5, 2.0, 2.5])
	plt.xticks([])
	plt.yticks([])
	plt.title('Dust Emissivity Index')
	plt.savefig(('emis-%02i%s'+suff) % (step, suffix))

	#plt.figure(figsize=(4,4))
	#plt.figure(figsize=(3,3))
	#plt.figure(figsize=(2.5,2.5))
	#plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.9)

	plt.figure(figsize=(W2,H))
	#plt.figure(figsize=(3,3))
	plt.subplots_adjust(**spa)

	for i,tim in enumerate(tractor.getImages()):
	 	ima = dict(interpolation='nearest', origin='lower',
	 			   vmin=tim.zr[0], vmax=tim.zr[1])
	 	plt.clf()
	 	plt.imshow(tim.getImage(), **ima)
	 	plt.gray()
	 	#plt.colorbar()
	 	#plt.title(tim.name)
		name = ['PACS 100', 'PACS 160', 'SPIRE 250', 'SPIRE 350', 'SPIRE 500'][i]
		plt.title('Data: ' + name)
		plt.xticks([])
		plt.yticks([])
	 	plt.savefig(('data-%i-%02i%s'+suff) % (i, step, suffix))

	mods = []
	for i,tim in enumerate(tractor.getImages()):
	 	ima = dict(interpolation='nearest', origin='lower',
	 			   vmin=tim.zr[0], vmax=tim.zr[1])

		print 'Getting model image', i
		mod = tractor.getModelImage(i)
		mods.append(mod)
	 	plt.clf()
	 	plt.imshow(mod, **ima)
	 	plt.gray()
		name = ['PACS 100', 'PACS 160', 'SPIRE 250', 'SPIRE 350', 'SPIRE 500'][i]
		plt.title('Model: ' + name)
		#plt.title(name)
		plt.xticks([])
		plt.yticks([])
	 	plt.savefig(('model-%i-%02i%s'+suff) % (i, step, suffix))

	# 
	# 	if step == 0:
	# 		plt.clf()
	# 		plt.imshow(tim.getImage(), **ima)
	# 		plt.gray()
	# 		plt.colorbar()
	# 		plt.title(tim.name)
	# 		plt.savefig('data-%i.png' % (i))

	#mods = tractor.getModelImages()

	# plt.figure(figsize=(8,6))
	# for i,mod in enumerate(mods):
	# 	tim = tractor.getImage(i)
	# 	ima = dict(interpolation='nearest', origin='lower',
	# 			   vmin=tim.zr[0], vmax=tim.zr[1])
	# 	plt.clf()
	# 	plt.imshow(mod, **ima)
	# 	plt.gray()
	# 	plt.colorbar()
	# 	plt.title(tim.name)
	# 	plt.savefig('model-%i-%02i%s.png' % (i, step, suffix))
	# 
	# 	if step == 0:
	# 		plt.clf()
	# 		plt.imshow(tim.getImage(), **ima)
	# 		plt.gray()
	# 		plt.colorbar()
	# 		plt.title(tim.name)
	# 		plt.savefig('data-%i.png' % (i))
	# 
	# 	plt.clf()
	# 	# tractor.getChiImage(i), 
	# 	plt.imshow((tim.getImage() - mod) * tim.getInvError(),
	# 			   interpolation='nearest', origin='lower',
	# 			   vmin=-5, vmax=+5)
	# 	plt.gray()
	# 	plt.colorbar()
	# 	plt.title(tim.name)
	# 	plt.savefig('chi-%i-%02i%s.png' % (i, step, suffix))

	plt.figure(figsize=(16,8))
	plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.9, wspace=0.1, hspace=0.1)
	R,C = 3,len(mods)
	plt.clf()
	for i,mod in enumerate(mods):
		tim = tractor.getImage(i)

		print 'Image', tim.name, ': data median', np.median(tim.getImage()),
		print 'model median:', np.median(mod)

		ima = dict(interpolation='nearest', origin='lower',
				   vmin=tim.zr[0], vmax=tim.zr[1])

		plt.subplot(R, C, i + 1)
		plt.imshow(tim.getImage(), **ima)
		plt.xticks([])
		plt.yticks([])
		plt.gray()
		plt.colorbar()
		plt.title(tim.name)

		plt.subplot(R, C, i + 1 + C)
		plt.imshow(mod, **ima)
		plt.xticks([])
		plt.yticks([])
		plt.gray()
		plt.colorbar()
		#plt.title(tim.name)

		plt.subplot(R, C, i + 1 + 2*C)
		plt.imshow((tim.getImage() - mod) * tim.getInvError(),
				   interpolation='nearest', origin='lower',
				   vmin=-5, vmax=+5)
		plt.xticks([])
		plt.yticks([])
		plt.gray()
		plt.colorbar()
		#plt.title(tim.name)
	plt.savefig('all-%02i%s.png' % (step, suffix))

	ds = tractor.getCatalog()[0]
	logsa, logt, emis = ds.getArrays()

	plt.figure(figsize=(16,5))
	plt.clf()

	plt.subplot(1,3,1)
	plt.imshow(np.exp(logsa), interpolation='nearest', origin='lower')
	plt.hot()
	plt.colorbar()
	plt.title('Dust: solid angle (Sr?)')

	plt.subplot(1,3,2)
	plt.imshow(np.exp(logt), interpolation='nearest', origin='lower') #, vmin=0)
	plt.hot()
	plt.colorbar()
	plt.title('Dust: temperature (K)')

	plt.subplot(1,3,3)
	plt.imshow(emis, interpolation='nearest', origin='lower')
	plt.gray()
	plt.colorbar()
	plt.title('Dust: emissivity')
	plt.savefig('dust-%02i%s.png' % (step, suffix))
Exemplo n.º 30
0
def forced2():
    from bigboss_test import radecroi
    ps = PlotSequence('forced')

    basedir = os.environ.get('BIGBOSS_DATA', '/project/projectdirs/bigboss')
    wisedatadir = os.path.join(basedir, 'data', 'wise')
    l1bdir = os.path.join(wisedatadir, 'level1b')
    wisecat = fits_table(os.path.join(
        wisedatadir, 'catalogs', 'wisecat2.fits'))

    # CAS PhotoObjAll.resolveStatus bits
    sprim = 0x100
    #sbad = 0x800
    sedge = 0x1000
    sbest = 0x200

    (ra0, ra1, dec0, dec1) = radecroi
    ra = (ra0 + ra1) / 2.
    dec = (dec0 + dec1) / 2.

    cas = fits_table('sdss-cas-testarea-3.fits')
    print('Read', len(cas), 'CAS sources')
    cas.cut((cas.resolvestatus & sedge) == 0)
    print('Cut to ', len(cas), 'without SURVEY_EDGE set')

    # Drop "sbest" sources that have an "sprim" nearby.
    Ibest = (cas.resolvestatus & (sprim | sbest)) == sbest
    Iprim = (cas.resolvestatus & (sprim | sbest)) == sprim
    I, J, d = match_radec(
        cas.ra[Ibest], cas.dec[Ibest], cas.ra[Iprim], cas.dec[Iprim], 2. / 3600.)

    Ibest[np.flatnonzero(Ibest)[I]] = False
    #Ikeep = np.ones(len(Ibest), bool)
    #Ikeep[I] = False
    cas.cut(np.logical_or(Ibest, Iprim))
    print('Cut to', len(cas), 'PRIMARY + BEST-not-near-PRIMARY')

    I, J, d = match_radec(cas.ra, cas.dec, cas.ra,
                          cas.dec, 2. / 3600., notself=True)
    plt.clf()
    loghist((cas.ra[I] - cas.ra[J]) * 3600.,
            (cas.dec[I] - cas.dec[J]) * 3600., 200)
    plt.title('CAS self-matches')
    ps.savefig()

    psf = pyfits.open('wise-psf-w1-500-500.fits')[0].data
    S = psf.shape[0]
    # number of Gaussian components
    K = 3
    w, mu, sig = em_init_params(K, None, None, None)
    II = psf.copy()
    II = np.maximum(II, 0)
    II /= II.sum()
    xm, ym = -(S / 2), -(S / 2)
    res = em_fit_2d(II, xm, ym, w, mu, sig)
    if res != 0:
        raise RuntimeError('Failed to fit PSF')
    print('W1 PSF:')
    print('  w', w)
    print('  mu', mu)
    print('  sigma', sig)
    w1psf = GaussianMixturePSF(w, mu, sig)
    w1psf.computeRadius()

    print('PSF radius:', w1psf.getRadius(), 'pixels')

    T = fits_table('wise-roi.fits')
    for i in range(len(T)):
        basefn = os.path.join(l1bdir, '%s%i-w1' %
                              (T.scan_id[i], T.frame_num[i]))
        fn = basefn + '-int-1b.fits'
        print('Looking for', fn)
        if not os.path.exists(fn):
            continue
        print('  -> Found it!')

        tim = read_wise_image(basefn, nanomaggies=True)
        tim.psf = w1psf

        wcs = tim.wcs.wcs
        r0, r1, d0, d1 = wcs.radec_bounds()
        print('RA,Dec bounds:', r0, r1, d0, d1)

        w, h = wcs.imagew, wcs.imageh
        rd = np.array([wcs.pixelxy2radec(x, y) for x, y in
                       [(1, 1), (w, 1), (w, h), (1, h), (1, 1)]])

        I = np.flatnonzero((cas.ra > r0) * (cas.ra < r1) *
                           (cas.dec > d0) * (cas.dec < d1))
        J = point_in_poly(cas.ra[I], cas.dec[I], rd)
        I = I[J]
        cashere = cas[I]
        # 10-20k sources...

        wbands = ['w1']
        sdssband = 'i'
        tsrcs = get_tractor_sources_cas_dr9(cashere, nanomaggies=True,
                                            bandname=sdssband, bands=[
                                                sdssband],
                                            extrabands=wbands)
        #keepsrcs = []
        for src in tsrcs:
            for br in src.getBrightnesses():
                f = br.getBand(sdssband)
                # if f < 0:
                #   continue
                for wb in wbands:
                    br.setBand(wb, f)
                # keepsrcs.append(src)
        #tsrcs = keepsrcs

        print('Created', len(tsrcs), 'tractor sources in this image')

        I = np.flatnonzero((wisecat.ra > r0) * (wisecat.ra < r1) *
                           (wisecat.dec > d0) * (wisecat.dec < d1))
        J = point_in_poly(wisecat.ra[I], wisecat.dec[I], rd)
        I = I[J]
        print('Found', len(I), 'WISE catalog sources in this image')

        wc = wisecat[I]
        tra = np.array([src.getPosition().ra for src in tsrcs])
        tdec = np.array([src.getPosition().dec for src in tsrcs])

        R = 4.
        I, J, d = match_radec(wc.ra, wc.dec, tra, tdec,
                              R / 3600., nearest=True)
        # cashere.ra, cashere.dec,
        print('Found', len(I), 'SDSS-WISE matches within', R, 'arcsec')

        for i, j in zip(I, J):
            w1 = wc.w1mpro[i]
            w1 = NanoMaggies.magToNanomaggies(w1)
            bb = tsrcs[j].getBrightnesses()
            for b in bb:
                b.setBand('w1', w1 / float(len(bb)))

        keepsrcs = []
        for src in tsrcs:
            # for b in src.getBrightness():
            b = src.getBrightness()
            if b.getBand(sdssband) > 0 or b.getBand(wbands[0]) > 0:
                keepsrcs.append(src)
        tsrcs = keepsrcs
        print('Keeping', len(tsrcs), 'tractor sources from SDSS')

        unmatched = np.ones(len(wc), bool)
        unmatched[I] = False
        wun = wc[unmatched]
        print(len(wun), 'unmatched WISE sources')
        for i in range(len(wun)):
            pos = RaDecPos(wun.ra[i], wun.dec[i])
            nm = NanoMaggies.magToNanomaggies(wun.w1mpro[i])
            br = NanoMaggies(i=25., w1=nm)
            tsrcs.append(PointSource(pos, br))

        plt.clf()
        plt.plot(rd[:, 0], rd[:, 1], 'k-')
        plt.plot(cashere.ra, cashere.dec, 'r.', alpha=0.1)
        plt.plot(wc.ra, wc.dec, 'bx', alpha=0.1)
        setRadecAxes(r0, r1, d0, d1)
        ps.savefig()

        zlo, zhi = tim.zr
        ima = dict(interpolation='nearest', origin='lower', vmin=zlo, vmax=zhi)
        imchi = dict(interpolation='nearest', origin='lower',
                     vmin=-5, vmax=5)

        plt.clf()
        plt.imshow(tim.getImage(), **ima)
        plt.hot()
        plt.title(tim.name + ': data')
        ps.savefig()

        wsrcs = []
        for i in range(len(wc)):
            pos = RaDecPos(wc.ra[i], wc.dec[i])
            nm = NanoMaggies.magToNanomaggies(wc.w1mpro[i])
            br = NanoMaggies(i=25., w1=nm)
            wsrcs.append(PointSource(pos, br))

        tr = Tractor([tim], wsrcs)
        tr.freezeParam('images')

        for jj in range(2):
            print('Rendering WISE model image...')
            wmod = tr.getModelImage(0)
            plt.clf()
            plt.imshow(wmod, **ima)
            plt.hot()
            plt.title(tim.name + ': WISE sources only')
            ps.savefig()

            assert(np.all(np.isfinite(wmod)))
            assert(np.all(np.isfinite(tim.getInvError())))
            assert(np.all(np.isfinite(tim.getImage())))

            wchi = tr.getChiImage(0)
            plt.clf()
            plt.imshow(wchi, **imchi)
            plt.title(tim.name + ': chi, WISE sources only')
            plt.gray()
            ps.savefig()

            if jj == 1:
                break

            tr.optimize()

        tr = Tractor([tim], tsrcs)
        print('Rendering model image...')
        mod = tr.getModelImage(0)

        plt.clf()
        plt.imshow(mod, **ima)
        plt.title(tim.name + ': SDSS + WISE sources')
        ps.savefig()

        print('tim', tim)
        print('tim.photocal:', tim.photocal)

        wsrcs = []
        for i in range(len(wc)):
            pos = RaDecPos(wc.ra[i], wc.dec[i])
            nm = NanoMaggies.magToNanomaggies(wc.w1mpro[i])
            br = NanoMaggies(i=25., w1=nm)
            wsrcs.append(PointSource(pos, br))

        tr = Tractor([tim], wsrcs)
        print('Rendering WISE model image...')
        wmod = tr.getModelImage(0)

        plt.clf()
        plt.imshow(wmod, **ima)
        plt.title(tim.name + ': WISE sources only')
        ps.savefig()
Exemplo n.º 31
0
def plotCorrespondences2(imgsources,
                         refsources,
                         matches,
                         wcs,
                         W,
                         H,
                         prefix,
                         saveplot=True,
                         format='png'):
    print('ix,iy')
    ix = np.array([s.getXAstrom() for s in imgsources])
    iy = np.array([s.getYAstrom() for s in imgsources])

    print('rx,ry')
    rx, ry = [], []
    for r in refsources:
        xy = wcs.skyToPixel(r.getRaDec())
        rx.append(xy[0])
        ry.append(xy[1])
    rx = np.array(rx)
    ry = np.array(ry)

    ixy = np.vstack((ix, iy)).T
    rxy = np.vstack((rx, ry)).T

    print('plotshift...')
    cell = 10
    plotshift(ixy, rxy, dcell=cell, ncells=9, W=W, H=H)
    fn = prefix + '-shift1.' + format
    P1 = _output(fn, format, saveplot)

    print('plotshift 2...')
    plt.clf()
    plt.hot()
    plotshift(ixy,
              rxy,
              dcell=cell,
              ncells=9,
              W=W,
              H=H,
              hist=True,
              nhistbins=2 * cell + 1)
    fn = prefix + '-shift2.' + format
    P2 = _output(fn, format, saveplot)

    print('plotshift 3...')
    cell = 2
    plotshift(ixy, rxy, dcell=cell, ncells=9, W=W, H=H)
    fn = prefix + '-shift3.' + format
    P3 = _output(fn, format, saveplot)

    print('plotshift 4...')
    plt.clf()
    plt.hot()
    plotshift(ixy,
              rxy,
              dcell=cell,
              ncells=9,
              W=W,
              H=H,
              hist=True,
              nhistbins=10 * cell + 1)
    fn = prefix + '-shift4.' + format
    P4 = _output(fn, format, saveplot)

    if not saveplot:
        P1.update(P2)
        P1.update(P3)
        P1.update(P4)
        return P1
Exemplo n.º 32
0
def main():

    if True:
        P = pyfits.open('jbf108bzq_flt.fits')
        img = P[1].data
        err = P[2].data
        #dq  = P[3].data

        P = pyfits.open('jbf108bzq_dq1.fits')
        dq = P[0].data

        #cut = [slice(900,1200), slice(2300,2600)]
        cut = [slice(1400, 1700), slice(2300, 2600)]
        img = img[cut]
        err = err[cut]
        dq = dq[cut]

        skyvar = measure_sky_variance(img)
        skymed = np.median(img.ravel())
        skysig = np.sqrt(skyvar)
        print('Estimate sky value', skymed, 'and sigma', skysig)
        zrange = np.array([-3., +10.]) * skysig + skymed
        invvar = 1. / (err**2)
        invvar[dq > 0] = 0

    else:
        # Dealing with cosmic rays is a PITA so use DRZ for now...
        P = pyfits.open('jbf108020_drz.fits')
        img = P[1].data
        wht = P[2].data
        ctx = P[3].data
        cut = [slice(1000, 1300), slice(2300, 2600)]
        img = img[cut]
        wht = wht[cut]

        # HACKs abound in what follows...
        skyvar = measure_sky_variance(img)
        invvar = wht / np.median(wht) / skyvar

        skymed = np.median(img.ravel())
        skysig = np.sqrt(skyvar)
        zrange = np.array([-3., +10.]) * skysig + skymed

        # add in source noise to variance map
        # problem for the reader:  why *divide* by wht?
        srcvar = np.maximum(0, (img - skymed) /
                            np.maximum(wht,
                                       np.median(wht) * 1e-6))
        invvar = invvar / (1.0 + invvar * srcvar)

    plt.clf()
    plt.hist(img.ravel(), bins=np.linspace(zrange[0], zrange[1], 100))
    plt.savefig('hist.png')

    plt.clf()
    plt.imshow(img,
               interpolation='nearest',
               origin='lower',
               vmin=zrange[0],
               vmax=zrange[1])  #vmin=50, vmax=500)
    plt.hot()
    plt.colorbar()
    plt.savefig('img.png')

    plt.clf()
    plt.imshow(invvar,
               interpolation='nearest',
               origin='lower',
               vmin=0.,
               vmax=2. / (skysig**2))
    plt.hot()
    plt.colorbar()
    plt.savefig('invvar.png')

    plt.clf()
    plt.imshow((img - skymed) * np.sqrt(invvar),
               interpolation='nearest',
               origin='lower')
    #vmin=-3, vmax=10.)
    plt.hot()
    plt.colorbar()
    plt.savefig('chi.png')

    plt.clf()
    plt.imshow((img - skymed) * np.sqrt(invvar),
               interpolation='nearest',
               origin='lower',
               vmin=-3,
               vmax=10.)
    plt.hot()
    plt.colorbar()
    plt.savefig('chi2.png')

    # Initialize with a totally bogus Gaussian PSF model.
    psf = NCircularGaussianPSF([2.0], [1.0])

    # test it...
    if False:
        for i, x in enumerate(np.arange(17, 18.7, 0.1)):
            p = psf.getPointSourcePatch(x, x)
            img = p.getImage()
            print('x', x, '-> sum', img.sum())
            plt.clf()
            x0, y0 = p.getX0(), p.getY0()
            h, w = img.shape
            plt.imshow(img,
                       extent=[x0, x0 + w, y0, y0 + h],
                       origin='lower',
                       interpolation='nearest')
            plt.axis([10, 25, 10, 25])
            plt.title('x=%.1f' % x)
            plt.savefig('psf-%02i.png' % i)

    # We'll start by working in pixel coords
    wcs = NullWCS()

    # And counts
    photocal = NullPhotoCal()

    data = Image(data=img,
                 invvar=invvar,
                 psf=psf,
                 wcs=wcs,
                 sky=skymed,
                 photocal=photocal)

    tractor = HSTTractor([data])

    if False:
        X = tractor.getChiImages()
        chi = X[0]
        plt.clf()
        plt.imshow(chi, interpolation='nearest', origin='lower')
        plt.hot()
        plt.colorbar()
        plt.savefig('chi3.png')

    Nsrc = 10
    steps = (['plots'] + ['source'] * Nsrc + ['plots'] + ['psf'] + ['plots'] +
             ['psf2']) * 3 + ['plots'] + ['break']

    chiArange = None

    for i, step in enumerate(steps):

        if step == 'plots':
            print('Making plots...')
            NS = len(tractor.getCatalog())

            chis = tractor.getChiImages()
            chi = chis[0]

            tt = 'sources: %i, chi^2 = %g' % (NS, np.sum(chi**2))

            mods = tractor.getModelImages()
            mod = mods[0]

            plt.clf()
            plt.imshow(mod,
                       interpolation='nearest',
                       origin='lower',
                       vmin=zrange[0],
                       vmax=zrange[1])
            plt.hot()
            plt.colorbar()
            ax = plt.axis()
            img = tractor.getImage(0)
            wcs = img.getWcs()
            x = []
            y = []
            for src in tractor.getCatalog():
                pos = src.getPosition()
                px, py = wcs.positionToPixel(pos)
                x.append(px)
                y.append(py)
            plt.plot(x, y, 'b+')
            plt.axis(ax)
            plt.title(tt)
            plt.savefig('mod-%02i.png' % i)

            if chiArange is None:
                chiArange = (chi.min(), chi.max())

            plt.clf()
            plt.imshow(chi,
                       interpolation='nearest',
                       origin='lower',
                       vmin=chiArange[0],
                       vmax=chiArange[1])
            plt.hot()
            plt.colorbar()
            plt.title(tt)
            plt.savefig('chiA-%02i.png' % i)

            plt.clf()
            plt.imshow(chi,
                       interpolation='nearest',
                       origin='lower',
                       vmin=-3,
                       vmax=10.)
            plt.hot()
            plt.colorbar()
            plt.title(tt)
            plt.savefig('chiB-%02i.png' % i)

        #print 'Driving the tractor...'

        elif step == 'break':
            break

        elif step == 'source':
            print()
            print('Before createSource, catalog is:', end=' ')
            tractor.getCatalog().printLong()
            print()
            rtn = tractor.createSource()
            print()
            print('After  createSource, catalog is:', end=' ')
            tractor.getCatalog().printLong()
            print()

            if False:
                (sm, tryxy) = rtn[0]
                plt.clf()
                plt.imshow(sm, interpolation='nearest', origin='lower')
                plt.hot()
                plt.colorbar()
                ax = plt.axis()
                plt.plot([x for x, y in tryxy], [y for x, y in tryxy], 'b+')
                plt.axis(ax)
                plt.savefig('create-%02i.png' % i)

        elif step == 'psf':
            baton = (i, )
            tractor.optimizeAllPsfAtFixedComplexityStep(
                derivCallback=(psfDerivCallback, baton))

        elif step == 'psf2':
            tractor.increaseAllPsfComplexity()

        print('Tractor cache has', len(tractor.cache), 'entries')
Exemplo n.º 33
0
                files[k] = os.path.join(dirname, fn)
                data[k] = fits.getdata(os.path.join(dirname, fn))

    if len(files) == 0:
        return

    peaks = {}
    for ii, (k, fn) in enumerate(files.iteritems()):
        d = data[k]
        OK = d == d
        y, x = np.indices(d.shape, dtype='float')
        rr = ((x - d.shape[1] / 2)**2 + (y - d.shape[0] / 2)**2)**0.5
        OK *= rr < 10

        if OK.sum() == 0:
            continue
        print k, fn, d[OK].max()
        peaks[k] = d[OK].max()

    return peaks


if __name__ == "__main__":
    pl.hot()  # select colormap
    peaks = {}
    for dirpath, dirnames, filenames in os.walk('./'):
        if dirpath != './':
            if 'neptune' in dirpath or 'g34.3' in dirpath:
                continue
            peaks[dirpath] = get_peaks(dirpath, filenames)
Exemplo n.º 34
0
def display_maps(Detect, Latencies,  fig, inner_grid,expe):

    ax= Subplot(fig, inner_grid[0])

    St = Detect.Sig_strength
    #----------------------------------------    
    # NEED TO REMAP FOR EXPS 20 22 and 23, and 27!!
    if expe<=22:
        aux = copy(St[24])
        for w in [24,23,22,21]:
            St[w] = copy(St[w-1])
        St[20] = copy(St[11])
        St[11] = aux
    if expe == 23:
        for w in [0,1,2,3]:
            St[w] = copy(St[w+1])
        St[4] = [0,0]
    #if exp == 27:
    #    St[16]== [0,0]
    #---------------------------------------------                        
    
    Stup = np.reshape(St[:,0],(5,5)) / St.max()
    Stdn = np.reshape(St[:,1],(5,5))/ St.max()        
    
    # only do if they exist
    if St.max()>0:
        
        im = ax.imshow(Stup,interpolation='none',cmap=pylab.gray())

        # create an axes on the right side of ax. The width of cax will be 5%
        # of ax and the padding between cax and ax will be fixed at 0.05 inch.
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_xticks([0,1,2,3,4]) # range of values in edges
        ax.set_yticks([0,1,2,3,4]) # range of values in edges
        ax.set_xticklabels(['St','1','2','3','4']) # range of values in edges
        ax.set_yticklabels(['A','B ','C ','D','E ']) # range of values in edges
        ax.tick_params('both', length=0, width=0)        

        ax.set_title('Caudal Strength')    
        fig.add_subplot(ax)
        #---------------------------------------------------------------------------------------------
        ax= Subplot(fig, inner_grid[1])
        
        im =ax.imshow(Stdn,interpolation='none',cmap=pylab.gray())

        ax.set_xticks([0,1,2,3,4]) # range of values in edges
        ax.set_yticks([0,1,2,3,4]) # range of values in edges
        ax.set_xticklabels(['St','1','2','3','4']) # range of values in edges
        ax.set_yticklabels(['A','B ','C ','D','E ']) # range of values in edges
        ax.tick_params('both', length=0, width=0)        

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_title('Rostral Strength')    
        fig.add_subplot(ax)
        #---------------------------------------------------------------------------------------------
        ax= Subplot(fig, inner_grid[2])
        
        Stboth = np.nan_to_num((Stup-Stdn)/(Stup+Stdn))
        # make a color map of fixed colors
        cmap = colors.ListedColormap(['#00cc00','#00aa00','#006600','#003300', 'black','#000066','#000099','#0000cc' ,'blue'])
        bounds=[-1,-0.75,-0.5,-0.25,-0.01,0.01,0.25,0.5,0.75,1]
        norm = colors.BoundaryNorm(bounds, cmap.N)    

        im = ax.imshow(Stboth,interpolation='none',cmap=cmap,norm=norm)

        ax.set_xticks([0,1,2,3,4]) # range of values in edges
        ax.set_yticks([0,1,2,3,4]) # range of values in edges
        ax.set_xticklabels(['St','1','2','3','4']) # range of values in edges
        ax.set_yticklabels(['A','B ','C ','D','E ']) # range of values in edges
        ax.tick_params('both', length=0, width=0)        

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_title('Directionality')    
        fig.add_subplot(ax)
        #---------------------------------------------------------------------------------------------
        ax= Subplot(fig, inner_grid[3])

        Lat = zeros(25)
        for i in arange(25):
            if Latencies[i,0]>5 and Latencies[i,1]>5:
                Lat[i] = min(Latencies[i,0],Latencies[i,1])
            elif Latencies[i,0]>5:
                Lat[i]= Latencies[i,0]
            elif Latencies[i,1]>5:
                Lat[i]= Latencies[i,1]
        #----------------------------------------    
        # NEED TO REMAP FOR EXPS 20 22 and 23, and 27!!
        if expe<=22:
            aux = copy(Lat[24])
            for w in [24,23,22,21]:
                Lat[w] = copy(Lat[w-1])
            Lat[20] = copy(Lat[11])
            Lat[11] = aux
        if expe == 23:
            for w in [0,1,2,3]:
                Lat[w] = copy(Lat[w+1])
            Lat[4] = 0
        #---------------------------------------------          
                
        Lat = np.reshape(Lat,(5,5))
        im = ax.imshow(Lat,interpolation='none',cmap=pylab.hot())

        ax.set_xticks([0,1,2,3,4]) # range of values in edges
        ax.set_yticks([0,1,2,3,4]) # range of values in edges
        ax.set_xticklabels(['St','1','2','3','4']) # range of values in edges
        ax.set_yticklabels(['A','B ','C ','D','E ']) # range of values in edges
        ax.tick_params('both', length=0, width=0)        

        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="10%", pad=0.15)
        colorbar(im, cax=cax)

        ax.set_title('Latency')    
        fig.add_subplot(ax)
Exemplo n.º 35
0
		def show(im):
			plt.imshow(np.log10(np.maximum(1e-3, im + 1e-3)), vmin=-3, vmax=0, **ima)
			plt.hot()
			plt.colorbar()
Exemplo n.º 36
0
     states.append(inputs[-1])
     refs.append((-1) * np.ones((10, 10)))
 for i in range(5):
     inputs.append(np.random.random((10, 10)))
     #inputs.append(np.diag(np.ones((10))*np.random.random()))
     inputs[-1][:] = smooth(inputs[-1], 9)  #+0.1
     #states.append(np.zeros((10,10)))
     states.append(inputs[-1])
     refs.append(np.ones((10, 10)) * (1))
 ct = CNNTrainer(t_sim=40, errfunc="mean_abs_diff", opt_method="simplex")
 #ct = CNNTrainer(t_sim=40,errfunc="sum_sqr_diff", opt_method="powell")
 #ct = CNNTrainer(t_sim=40,errfunc="mean_abs_diff", opt_method="anneal")
 a, b, z = ct(inputs, states, refs)
 #p.ioff()
 #print "Test"
 p.hot()
 p.figure(1)
 for i in range(10):
     print i,
     cnn = CNN2d(inputs[i], states[i], a, b, z)
     try:
         o = cnn.integrate(np.arange(1, 21, 1))
         #p.figure(i+2)
         #for i2 in range(20):
         #    p.subplot(4,5,i2+1)
         #    #p.axes(frameon=False)
         #    p.imshow(cnn.sigmoid(o[i2].reshape((10,10))),interpolation="nearest",vmin=-1,vmax=1)
         #    p.show()
         #assert 1==0
         time.sleep(1)
         p.ioff()
Exemplo n.º 37
0
#def apply_action(control, action):
#    actions = ((0,0),(0,1),(1,0),(1,1),(0,-1),(-1,0),(-1,-1))
#    act = actions[action]
#    control.left_engine(act[0])
#    control.right_engine(act[1])
#
#def do_next_action(control, image):
#    global q_net, action, red_pixels
#    splitted_image = split_image(image, 4)
#    red_pixels = list(map(get_red_pixels, splitted_image))
#    action = q_net.activate(red_pixels)
#    arg = np.argmax(action)
#    apply_action(control, arg)

pylab.ion()
pylab.hot()
pylab.show()

with CurrentController(3) as control:
    environment = ControllerEnvironment(control)
    task = MdpRedCubeTask(environment, False)

    control.cubes_x = 2
    control.cubes_y = 3
    control.cubes_size = 4
    task.max_samples = 500

    actions = len(environment.actions)

    actionValueNetwork = ActionValueTable(task.outdim, task.indim)
    actionValueNetwork.stdParams = 0.0001
Exemplo n.º 38
0
def loghist(x, y, nbins=100,
			hot=True, doclf=True, docolorbar=True, lo=0.3,
			imshowargs={},
			clampxlo=False, clampxlo_val=None, clampxlo_to=None,
			clampxhi=False, clampxhi_val=None, clampxhi_to=None,
			clampylo=False, clampylo_val=None, clampylo_to=None,
			clampyhi=False, clampyhi_val=None, clampyhi_to=None,
			clamp=None, clamp_to=None,
			**kwargs):
	#np.seterr(all='warn')
	if doclf:
		plt.clf()
	myargs = kwargs.copy()
	if not 'bins' in myargs:
		myargs['bins'] = nbins

	rng = kwargs.get('range', None)
	x = np.array(x)
	y = np.array(y)

	if not (np.all(np.isfinite(x)) and np.all(np.isfinite(y))):
		K = np.flatnonzero(np.isfinite(x) * np.isfinite(y))
		print 'loghist: cutting to', len(K), 'of', len(x), 'finite values'
		x = x[K]
		y = y[K]
		
	if clamp is True:
		clamp = rng
	if clamp is not None:
		((clampxlo_val, clampxhi_val),(clampylo_val, clampyhi_val)) = clamp
	if clamp_to is not None:
		((clampxlo_to, clampxhi_to),(clampylo_to, clampyhi_to)) = clamp_to
	if clampxlo:
		if clampxlo_val is None:
			if rng is None:
				raise RuntimeError('clampxlo, but no clampxlo_val or range')
			clampxlo_val = rng[0][0]
	if clampxlo_val is not None:
		if clampxlo_to is None:
			clampxlo_to = clampxlo_val
		x[x < clampxlo_val] = clampxlo_to
	if clampxhi:
		if clampxhi_val is None:
			if rng is None:
				raise RuntimeError('clampxhi, but no clampxhi_val or range')
			clampxhi_val = rng[0][1]
	if clampxhi_val is not None:
		if clampxhi_to is None:
			clampxhi_to = clampxhi_val
		x[x > clampxhi_val] = clampxhi_to
	if clampylo:
		if clampylo_val is None:
			if rng is None:
				raise RuntimeError('clampylo, but no clampylo_val or range')
			clampylo_val = rng[1][0]
	if clampylo_val is not None:
		if clampylo_to is None:
			clampylo_to = clampylo_val
		y[y < clampylo_val] = clampylo_to
	if clampyhi:
		if clampyhi_val is None:
			if rng is None:
				raise RuntimeError('clampyhi, but no clampyhi_val or range')
			clampyhi_val = rng[1][1]
	if clampyhi_val is not None:
		if clampyhi_to is None:
			clampyhi_to = clampyhi_val
		y[y > clampyhi_val] = clampyhi_to




	(H,xe,ye) = np.histogram2d(x, y, **myargs)

	L = np.log10(np.maximum(lo, H.T))
	myargs = dict(extent=(min(xe), max(xe), min(ye), max(ye)),
				  aspect='auto',
				  interpolation='nearest', origin='lower')
	myargs.update(imshowargs)
	plt.imshow(L, **myargs)
	if hot:
		plt.hot()
	if docolorbar:
		r = [np.log10(lo)] + range(int(np.ceil(L.max())))
		# print 'loghist: L max', L.max(), 'r', r
		plt.colorbar(ticks=r, format=FixedFormatter(
			['0'] + ['%i'%(10**ri) for ri in r[1:]]))
	#set_fp_err()
	return H, xe, ye
Exemplo n.º 39
0
	def show_fractal(self, width=200, height=200, mode="iterations"):
		data = self.newtons_method(self.make_grid(width, height), mode)
		pylab.imshow(data)
		pylab.hot()
		pylab.show()
Exemplo n.º 40
0
def test_vs_stars_on(run, camcol, field, band, ps):

	bandnum = band_index(band)
	datadir = os.path.join(os.path.dirname(__file__), 'testdata')
	sdss = DR9(basedir=datadir)

	sdss.retrieve('psField', run, camcol, field)
	psfield = sdss.readPsField(run, camcol, field)

	sdss.retrieve('frame', run, camcol, field, band)
	frame = sdss.readFrame(run, camcol, field, band)
	img = frame.image
	H,W = img.shape

	fn = sdss.retrieve('photoObj', run, camcol, field)
	T = fits_table(fn)
	T.mag  = T.get('psfmag')[:,bandnum]
	T.flux = T.get('psfflux')[:,bandnum]
	# !!
	T.x = T.colc[:,bandnum] - 0.5
	T.y = T.rowc[:,bandnum] - 0.5
	T.cut(T.prob_psf[:,bandnum] == 1)
	T.cut(T.nchild == 0)
	T.cut(T.parent == -1)
	T.cut(T.flux > 1.)
	print len(T), 'after flux cut'
	#T.cut(T.flux > 10.)
	#print len(T), 'after flux cut'
	#T.cut(T.flux > 20.)
	#print len(T), 'after flux cut'
	T.cut(np.argsort(-T.flux)[:25])
	# margin
	m = 30
	T.cut((T.x >= m) * (T.x < (W-m)) * (T.y >= m) * (T.y < (H-m)))
	#T.cut(np.argsort(T.mag))
	T.cut(np.argsort(-np.abs(T.x - T.y)))
	
	print len(T), 'PSF stars'
		
	#R,C = 5,5
	#plt.clf()

	eigenpsfs  = psfield.getEigenPsfs(bandnum)
	eigenpolys = psfield.getEigenPolynomials(bandnum)
	RR,CC = 2,2
	
	xx,yy = np.meshgrid(np.linspace(0, W, 12), np.linspace(0, H, 8))

	ima = dict(interpolation='nearest', origin='lower')
	
	plt.clf()
	mx = None
	for i,(psf,poly) in enumerate(zip(eigenpsfs, eigenpolys)):
		print
		print 'Eigen-PSF', i
		XO,YO,C = poly
		kk = np.zeros_like(xx)
		for xo,yo,c in zip(XO,YO,C):
			dk = (xx ** xo) * (yy ** yo) * c
			#print 'xo,yo,c', xo,yo,c, '-->', dk
			kk += dk
		print 'Max k:', kk.max(), 'min', kk.min()
		print 'PSF range:', psf.min(), psf.max()
		print 'Max effect:', max(np.abs(kk.min()), kk.max()) * max(np.abs(psf.min()), psf.max())
		
		plt.subplot(RR,CC, i+1)
		plt.imshow(psf * kk.max(), **ima)
		if mx is None:
			mx = (psf * kk.max()).max()
		else:
			plt.clim(-mx * 0.05, mx * 0.05)
		plt.colorbar()
	ps.savefig()

	psfs = psfield.getPsfAtPoints(bandnum, xx,yy)
	#print 'PSFs:', psfs
	ph,pw = psfs[0].shape
	psfs = np.array(psfs).reshape(xx.shape + (ph,pw))
	print 'PSFs shape:', psfs.shape
	psfs = psfs[:,:,15:36,15:36]
	ny,nx,ph,pw = psfs.shape
	psfmos = np.concatenate([psfs[i,:,:,:] for i in range(ny)], axis=1)
	print 'psfmos', psfmos.shape
	psfmos = np.concatenate([psfmos[i,:,:] for i in range(nx)], axis=1)
	print 'psfmos', psfmos.shape
	plt.clf()
	plt.imshow(np.log10(np.maximum(psfmos + 1e-3, 1e-3)), **ima)
	ps.savefig()
	
	diffs = None
	rmses = None
	
	for i in range(len(T)):

		xx = T.x[i]
		yy = T.y[i]
		
		ix = int(np.round(xx))
		iy = int(np.round(yy))
		dx = xx - ix
		dy = yy - iy

		S = 25
		stamp = img[iy-S:iy+S+1, ix-S:ix+S+1]

		L = 6
		Lx = lanczos_filter(L, np.arange(-L, L+1) - dx)
		Ly = lanczos_filter(L, np.arange(-L, L+1) - dy)
		sx = correlate1d(stamp, Lx, axis=1, mode='constant')
		shim = correlate1d(sx,  Ly, axis=0, mode='constant')
		shim /= (Lx.sum() * Ly.sum())
		
		psf = psfield.getPsfAtPoints(bandnum, xx, yy)
		mod = psf / psf.sum() * T.flux[i]

		psf2 = psfield.getPsfAtPoints(bandnum, yy, xx)
		mod2 = psf2 / psf2.sum() * T.flux[i]

		psf3 = psfield.getPsfAtPoints(bandnum, xx, yy).T
		mod3 = psf3 / psf3.sum() * T.flux[i]

		mods = [mod, mod2, mod3]

		if diffs is None:
			diffs = [np.zeros_like(m) for m in mods]
			rmses = [np.zeros_like(m) for m in mods]
			
		for m,rms,diff in zip(mods, rmses, diffs):
			diff += (m - shim)
			rms +=  (m - shim)**2

		if i > 10:
			continue
		
		def show(im):
			plt.imshow(np.log10(np.maximum(1e-3, im + 1e-3)), vmin=-3, vmax=0, **ima)
			plt.hot()
			plt.colorbar()
		
		R,C = 3,4
		plt.clf()
		plt.subplot(R,C,1)
		show(shim)
		plt.subplot(R,C,2)
		show(mods[0])
		plt.subplot(R,C,2+C)
		plt.imshow(mods[0] - shim, vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.subplot(R,C,3)
		show(mods[1])

		plt.subplot(R,C,3+C)
		plt.imshow(mods[1] - shim, vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()
		
		plt.subplot(R,C,4)
		show(mods[2])
		
		plt.subplot(R,C,4+C)
		plt.imshow(mods[2] - shim, vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.subplot(R,C,3+2*C)
		plt.imshow(mods[1] - mods[0], vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.subplot(R,C,4+2*C)
		plt.imshow(mods[2] - mods[0], vmin=-0.05, vmax=0.05, **ima)
		plt.hot()
		plt.colorbar()

		plt.suptitle('%.0f, %.0f' % (xx,yy))
		ps.savefig()

	for diff in diffs:
		diff /= len(T)

	rmses = [np.sqrt(rms / len(T)) for rms in rmses]

	print 'rms median', np.median(rmses[0]), 'mean', np.mean(rmses[0])

	r0,r1 = [np.percentile(rmses[0], p) for p in [10,90]]
	
	R,C = 2,len(diffs)
	plt.clf()

	for i,(d,r) in enumerate(zip(diffs, rmses)):
		plt.subplot(R,C, 1+i)
		plt.imshow(d, vmin=-0.05, vmax=0.05, **ima)
		plt.colorbar()
		plt.hot()

		plt.subplot(R,C, 1+i+C)
		plt.imshow(r, vmin=r0, vmax=r1, **ima)
		plt.colorbar()
		plt.hot()
	
	ps.savefig()
Exemplo n.º 41
0
                    continue
                files[k] = os.path.join(dirname,fn)
                data[k] = fits.getdata(os.path.join(dirname,fn))

    if len(files) == 0:
        return

    peaks = {}
    for ii,(k,fn) in enumerate(files.iteritems()):
        d = data[k]
        OK = d==d
        y,x = np.indices(d.shape,dtype='float')
        rr = ((x-d.shape[1]/2)**2 + (y-d.shape[0]/2)**2)**0.5
        OK *= rr < 10

        if OK.sum() == 0:
            continue
        print k,fn,d[OK].max()
        peaks[k] = d[OK].max()

    return peaks

if __name__ == "__main__":
    pl.hot() # select colormap
    peaks = {}
    for dirpath, dirnames, filenames in os.walk('./'):
        if dirpath != './':
            if 'neptune' in dirpath or 'g34.3' in dirpath:
                continue
            peaks[dirpath] = get_peaks(dirpath,filenames)
    # plot the sampled trajectories
    f1 = pylab.figure()
    path_colours = ['r','g','b']
    path_linestyles = ['-','--','-.']
    for g in range(num_groups):
        pylab.plot(routes[g][:,1],routes[g][:,0],c=path_colours[g],ls=path_linestyles[g],linewidth=2)
    ax = pylab.gca()
    ax.set_xticks([])
    ax.set_yticks([])

    if save_figures:
        f1.set_figwidth(4)
        f1.set_figheight(4)
        pylab.savefig('sampled_trajectories.png',bbox_inches='tight')

    # plot the sampled mean disease density
    pylab.matshow(M)
    pylab.colorbar()
    pylab.hot()
    ax = pylab.gca()
    ax.set_xticks([])
    ax.set_yticks([])

    if save_figures:
        f2 = pylab.gcf()
        f2.set_figwidth(8)
        f2.set_figheight(4)
        pylab.savefig('sampled_mean_density.png',bbox_inches='tight')

Exemplo n.º 43
0
def alignAndPlot(Tme,
                 Tref,
                 rad,
                 ps,
                 doweighted=True,
                 emrad=None,
                 nearest=False,
                 **kwargs):
    aliargs = dict(cutrange=emrad)
    aliargs.update(kwargs)
    A = Alignment(Tme, Tref, searchradius=rad, **aliargs)
    if nearest:
        # There is something badly wrong with spherematch.nearest().
        assert (False)
        A.findMatches(nearest=True)
        M = A.match
        print 'dra,ddec arcsec:', M.dra_arcsec[:100], M.ddec_arcsec[:100]

    if A.shift() is None:
        print 'Shift not found!'
        return None
    M = A.match
    print 'Shift:', A.arcsecshift()
    sr, sd = A.arcsecshift()

    sumd2 = np.sum(A.fore * ((M.dra_arcsec[A.subset] - sr)**2 +
                             (M.ddec_arcsec[A.subset] - sd)**2))
    sumw = np.sum(A.fore)
    # / 2. to get std per coord.
    std = sqrt(sumd2 / (sumw * 2.))
    angles = np.linspace(0, 2. * pi, 100)

    modstr = ''
    if A.cov:
        eigs = A.getEllipseSize() * 1000.
        if eigs[0] > 100:
            modstr = '%.0fx%.0f' % (eigs[0], eigs[1])
        else:
            modstr = '%.1fx%.1f' % (eigs[0], eigs[1])
    else:
        modstr = '%.1f' % (1000. * A.sigma)

    W = np.zeros_like(A.subset).astype(float)
    W[A.subset] = A.fore

    rl, rh = Tme.ra.min(), Tme.ra.max()
    dl, dh = Tme.dec.min(), Tme.dec.max()

    if doweighted:
        rounds = [{}, {'weights': W}]
    else:
        rounds = [{}]

    for i, args in enumerate(rounds):
        tsuf = '' if i == 0 else ' (weighted)'
        N = len(M.dra_arcsec) if i == 0 else sumw

        plotresids(Tme,
                   M,
                   '%s-%s match residuals%s' % (Tme.cam, Tref.cam, tsuf),
                   bins=100,
                   **args)
        ps.savefig()

        dst = 1000. * np.sqrt(M.dra_arcsec**2 + M.ddec_arcsec**2)
        loghist(Tme.mag[M.I], dst, 100, **args)
        plt.xlabel(Tme.filter)
        plt.ylabel('Match residual (mas)')
        ps.savefig()

        loghist(Tref.mag[M.J], dst, 100, **args)
        plt.xlabel(Tref.filter)
        plt.ylabel('Match residual (mas)')
        ps.savefig()

        H, xe, ye = plotalignment(A)
        # show EM circle
        ax = plt.axis()
        angles = np.linspace(0, 2. * pi, 100)
        c = A.cutcenter
        r = A.cutrange
        plt.plot(c[0] + r * np.cos(angles), c[1] + r * np.sin(angles), 'g--')
        plt.axis(ax)
        plt.title('%s-%s (%i matches, std %.1f mas, model %s)%s' %
                  (Tme.cam, Tref.cam, int(sumw), std * 1000., modstr, tsuf))
        ps.savefig()

        bins = 200
        edges = np.linspace(-rad, rad, bins)
        DR, DD = np.meshgrid(edges, edges)
        em = A.getModel(DR.ravel(), DD.ravel()).reshape(DR.shape)
        em *= len(M.dra_arcsec) * (edges[1] - edges[0])**2
        R2 = DR**2 + DD**2
        em[R2 > (A.match.rad)**2] = 0.
        plt.clf()
        plt.imshow(em,
                   extent=(-rad, rad, -rad, rad),
                   aspect='auto',
                   interpolation='nearest',
                   origin='lower',
                   vmin=H.min(),
                   vmax=H.max())
        plt.hot()
        plt.colorbar()
        plt.xlabel('dRA (arcsec)')
        plt.ylabel('dDec (arcsec)')
        plt.title('EM model')
        ps.savefig()

        plotfitquality(H, xe, ye, A)
        ps.savefig()

        rng = ((-5 * std, 5 * std), (-5 * std, 5 * std))
        myargs = args.copy()
        myargs.update({'range': rng})
        plothist(M.dra_arcsec - sr, M.ddec_arcsec - sd, 200, **myargs)
        ax = plt.axis()
        plt.xlabel('dRA (arcsec)')
        plt.ylabel('dDec (arcsec)')
        for nsig in [1, 2]:
            X, Y = A.getContours(nsig)
            plt.plot(X - sr, Y - sd, 'b-')
        plt.axis(ax)
        plt.title('%s-%s (matches: %i, std: %.1f mas, model %s)%s' %
                  (Tme.cam, Tref.cam, int(sumw), std * 1000., modstr, tsuf))
        ps.savefig()

        #plothist(Tme.mag[M.I], Tref.mag[M.J], 100, **args)
        #plt.xlabel('%s %s (mag)' % (Tme.cam, Tme.filter))
        #plt.ylabel('%s %s (mag)' % (Tref.cam, Tref.filter))
        #fn = '%s-%s.png' % (basefn, chr(ploti))
        #plt.title('%s-%s%s' % (Tme.cam, Tref.cam, tsuf))
        #plt.savefig(fn)
        #print 'saved', fn
        #ploti += 1

        loghist(Tme.mag[M.I], Tref.mag[M.J], 100, **args)
        plt.xlabel('%s (mag)' % (Tme.filter))
        plt.ylabel('%s (mag)' % (Tref.filter))
        plt.title('%s-%s%s' % (Tme.cam, Tref.cam, tsuf))
        ps.savefig()

        plothist(Tme.ra[M.I], Tme.dec[M.I], 100, range=((rl, rh), (dl, dh)))
        setRadecAxes(rl, rh, dl, dh)
        plt.title('%s-%s: %i matches%s' % (Tme.cam, Tref.cam, N, tsuf))
        ps.savefig()

    return A
Exemplo n.º 44
0
def main():

	if True:
		P = pyfits.open('jbf108bzq_flt.fits')
		img = P[1].data
		err = P[2].data
		#dq  = P[3].data

		P = pyfits.open('jbf108bzq_dq1.fits')
		dq  = P[0].data

		#cut = [slice(900,1200), slice(2300,2600)]
		cut = [slice(1400,1700), slice(2300,2600)]
		img = img[cut]
		err = err[cut]
		dq  = dq[cut]

		skyvar = measure_sky_variance(img)
		skymed = np.median(img.ravel())
		skysig = np.sqrt(skyvar)
		print 'Estimate sky value', skymed, 'and sigma', skysig
		zrange = np.array([-3.,+10.]) * skysig + skymed
		invvar = 1. / (err**2)
		invvar[dq > 0] = 0

	else:
		# Dealing with cosmic rays is a PITA so use DRZ for now...
		P = pyfits.open('jbf108020_drz.fits')
		img = P[1].data
		wht = P[2].data
		ctx = P[3].data
		cut = [slice(1000,1300), slice(2300,2600)]
		img = img[cut]
		wht = wht[cut]

		# HACKs abound in what follows...
		skyvar = measure_sky_variance(img)
		invvar = wht / np.median(wht) / skyvar

		skymed = np.median(img.ravel())
		skysig = np.sqrt(skyvar)
		zrange = np.array([-3.,+10.]) * skysig + skymed

		# add in source noise to variance map
		# problem for the reader:  why *divide* by wht?
		srcvar = np.maximum(0, (img - skymed) / np.maximum(wht, np.median(wht)*1e-6))
		invvar = invvar / (1.0 + invvar * srcvar)


	plt.clf()
	plt.hist(img.ravel(), bins=np.linspace(zrange[0], zrange[1], 100))
	plt.savefig('hist.png')

	plt.clf()
	plt.imshow(img, interpolation='nearest', origin='lower',
			  vmin=zrange[0], vmax=zrange[1]) #vmin=50, vmax=500)
	plt.hot()
	plt.colorbar()
	plt.savefig('img.png')
	
	plt.clf()
	plt.imshow(invvar, interpolation='nearest', origin='lower',
			  vmin=0., vmax=2./(skysig**2))
	plt.hot()
	plt.colorbar()
	plt.savefig('invvar.png')
	
	plt.clf()
	plt.imshow((img-skymed) * np.sqrt(invvar), interpolation='nearest', origin='lower')
	#vmin=-3, vmax=10.)
	plt.hot()
	plt.colorbar()
	plt.savefig('chi.png')

	plt.clf()
	plt.imshow((img-skymed) * np.sqrt(invvar), interpolation='nearest', origin='lower',
			  vmin=-3, vmax=10.)
	plt.hot()
	plt.colorbar()
	plt.savefig('chi2.png')

	# Initialize with a totally bogus Gaussian PSF model.
	psf = NCircularGaussianPSF([2.0], [1.0])

	# test it...
	if False:
		for i,x in enumerate(np.arange(17, 18.7, 0.1)):
			p = psf.getPointSourcePatch(x, x)
			img = p.getImage()
			print 'x', x, '-> sum', img.sum()
			plt.clf()
			x0,y0 = p.getX0(),p.getY0()
			h,w = img.shape
			plt.imshow(img, extent=[x0, x0+w, y0, y0+h],
					   origin='lower', interpolation='nearest')
			plt.axis([10,25,10,25])
			plt.title('x=%.1f' % x)
			plt.savefig('psf-%02i.png' % i)


	# We'll start by working in pixel coords
	wcs = NullWCS()

	# And counts
	photocal = NullPhotoCal()

	data = Image(data=img, invvar=invvar, psf=psf, wcs=wcs, sky=skymed,
				 photocal=photocal)
	
	tractor = HSTTractor([data])

	if False:
		X = tractor.getChiImages()
		chi = X[0]
		plt.clf()
		plt.imshow(chi, interpolation='nearest', origin='lower')
		plt.hot()
		plt.colorbar()
		plt.savefig('chi3.png')

	Nsrc = 10
	steps = (['plots'] + ['source']*Nsrc + ['plots'] + ['psf'] + ['plots'] + ['psf2'])*3 + ['plots'] + ['break']

	chiArange = None

	for i,step in enumerate(steps):

		if step == 'plots':
			print 'Making plots...'
			NS = len(tractor.getCatalog())

			chis = tractor.getChiImages()
			chi = chis[0]

			tt = 'sources: %i, chi^2 = %g' % (NS, np.sum(chi**2))

			mods = tractor.getModelImages()
			mod = mods[0]

			plt.clf()
			plt.imshow(mod, interpolation='nearest', origin='lower',
					   vmin=zrange[0], vmax=zrange[1])
			plt.hot()
			plt.colorbar()
			ax = plt.axis()
			img = tractor.getImage(0)
			wcs = img.getWcs()
			x = []
			y = []
			for src in tractor.getCatalog():
				pos = src.getPosition()
				px,py = wcs.positionToPixel(pos)
				x.append(px)
				y.append(py)
			plt.plot(x, y, 'b+')
			plt.axis(ax)
			plt.title(tt)
			plt.savefig('mod-%02i.png' % i)

			if chiArange is None:
				chiArange = (chi.min(), chi.max())

			plt.clf()
			plt.imshow(chi, interpolation='nearest', origin='lower',
					   vmin=chiArange[0], vmax=chiArange[1])
			plt.hot()
			plt.colorbar()
			plt.title(tt)
			plt.savefig('chiA-%02i.png' % i)

			plt.clf()
			plt.imshow(chi, interpolation='nearest', origin='lower',
					   vmin=-3, vmax=10.)
			plt.hot()
			plt.colorbar()
			plt.title(tt)
			plt.savefig('chiB-%02i.png' % i)
		

		#print 'Driving the tractor...'

		elif step == 'break':
			break

		elif step == 'source':
			print
			print 'Before createSource, catalog is:',
			tractor.getCatalog().printLong()
			print
			rtn = tractor.createSource()
			print
			print 'After  createSource, catalog is:',
			tractor.getCatalog().printLong()
			print

			if False:
				(sm,tryxy) = rtn[0]
				plt.clf()
				plt.imshow(sm, interpolation='nearest', origin='lower')
				plt.hot()
				plt.colorbar()
				ax = plt.axis()
				plt.plot([x for x,y in tryxy], [y for x,y in tryxy], 'b+')
				plt.axis(ax)
				plt.savefig('create-%02i.png' % i)

		elif step == 'psf':
			baton = (i,)
			tractor.optimizeAllPsfAtFixedComplexityStep(
				derivCallback=(psfDerivCallback, baton))

		elif step == 'psf2':
			tractor.increaseAllPsfComplexity()

		print 'Tractor cache has', len(tractor.cache), 'entries'
Exemplo n.º 45
0
    def plot_results(self):
        # Plot the cropped picture
        plt.subplot(421)
        plt.xticks([])
        plt.yticks([])
        plt.title(self.name)
        pylab.imshow(self.image_orig)
        pylab.gray()

        # Plot thresholded image
        pylab.gray()
        plt.subplot(422)
        plt.xticks([])
        plt.yticks([])
        plt.title('coverage = %.2f' % self.coverage)
        pylab.imshow(self.image > self.thresh)

        # Plot distinguished image
        pylab.hot()
        plt.subplot(423)
        plt.xticks([])
        plt.yticks([])
        plt.title('%d seperate islands' % self.num_islands)
        pylab.imshow(self.labeled_by_area)
        plt.colorbar()

        # Plot distinguished image
        plt.subplot(424)
        plt.xticks([])
        plt.yticks([])
        plt.title('%d seperate islands' % self.num_islands)
        pylab.imshow(self.labeled_p)

        # Plot histogram

        plt.subplot(425)
        plt.hist(self.size_dist * (1.92**2), normed=True, align='mid')
        plt.xlabel('Projected Area (nm$^2$)')
        plt.ylabel('Probability')

        # Plot histogram
        plt.subplot(426)
        plt.hist(self.perimeters[1:-1] * 1.92, normed=True, align='mid')
        plt.xlabel('Perimeter (nm)')
        plt.ylabel('Probability')

        # Plot histogram
        plt.subplot(427)
        plt.hist(self.perimeter_dist, normed=True, align='mid')
        plt.xlabel('Fractal Coeffecient')
        plt.ylabel('Probability')

        plt.subplot(428)
        plt.plot(self.perimeters[1:-1] * 1.92, self.size_dist[1:-1] * 1.92**2,
                 'o')
        plt.xlabel('Perimeter [nm]')
        plt.ylabel('Area [nm$^2$]')
        plt.savefig(self.name + ".png")
        plt.show()
        """
        self.image_bool = input('Use this image? (True or False)')
        self.image_bool = bool(self.image_bool)
        if not self.image_bool:
            print 'image removed'
            os.system('rm %s.tif' % self.name)
        """

        return
Exemplo n.º 46
0
    ps = PlotSequence("ex")

    for scale in [False, True]:
        # reset fit
        psf.scale = scale
        im = psf.instantiateAt(0.0, 0.0)

        ny, nx = im.shape
        XX, YY = np.meshgrid(np.arange(nx), np.arange(ny))
        print("cx", (np.sum(im * XX) / np.sum(im)))
        print("cy", (np.sum(im * YY) / np.sum(im)))

        plt.clf()
        mx = im.max()
        plt.imshow(im, origin="lower", interpolation="nearest", vmin=-0.1 * mx, vmax=mx * 1.1)
        plt.hot()
        plt.colorbar()
        ps.savefig()

    print("PSF scale", psf.sampling)
    print("1./scale", 1.0 / psf.sampling)

    YY = np.linspace(0, 4096, 5)
    XX = np.linspace(0, 2048, 5)

    yims = []
    for y in YY:
        xims = []
        for x in XX:
            im = psf.instantiateAt(x, y)
            im /= im.sum()
Exemplo n.º 47
0
#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#Plotting block
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#Make a beam profile plot in the z-r plane.
py.ion()
py.clf()
py.imshow(P_tot,
          norm=colors.LogNorm(vmin=1e-6, vmax=1.),
          interpolation='nearest')
ax = py.gca()
py.ylim(ax.get_ylim()[::-1])  #Reverse y-axis
#py.xlim((-floor(z_ap - z_c),50))
py.hot()
py.colorbar(ticks=[1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1.],
            label='Power Relative to Optical Axis')
#py.contour(P_tot, [1e-1,1e-2,1e-3,1e-4,1e-5,1e-6], colors='b')
#py.plot([0./resolution,5./resolution],[2.8/resolution,2.8/resolution], 'k')
#py.plot([2.134/resolution,2.134/resolution],[0./resolution,5./resolution], 'k')
locs = zeros(10)
labels = zeros(10)
for i in range(10):
    locs[i] = i * length / resolution / 10.
    labels[i] = i * length / 10.
#locs = range(0,int(length/resolution),int(length/(resolution*10.)))
#labels = range(0,int(length),int(length/len(locs)))
py.yticks(locs, labels)
py.xticks(locs, labels)
py.xlabel('Distance from horn aperture (mm)')
Exemplo n.º 48
0
        topology.append(('left', i + 1, i))

    # starts the task
    task = start_task(
        HeatSolver,  # name of the task class
        cpu=nodes,  # use <nodes> CPUs on the local machine
        topology=topology,
        args=(split_y, dx, dt, iterations))

    # Retrieves the result, as a list with one element returned by MonteCarlo.get_result per node
    result = task.get_result()
    result = np.hstack(result)

    return result


if __name__ == '__main__':
    result = heat2d(100, 2)
    pl.hot()
    pl.imshow(result)

    #    x = np.linspace(-1.,1.,98)
    #    X,Y= np.meshgrid(x,x)
    #    import matplotlib.pyplot as plt
    #    from matplotlib import cm
    #    from mpl_toolkits.mplot3d import Axes3D
    #    fig = pl.figure()
    #    ax = fig.add_subplot(111, projection='3d')
    #    ax.plot_wireframe(X,Y,result)

    pl.show()
Exemplo n.º 49
0
def visualize():
    '''
    Just some ugly plotting to make an animation for the lecture.
    '''
    import pylab as pp
    pp.ion()

    def remove_spines(axes):
        axes.set_xticks([])
        axes.set_yticks([])
        axes.spines['right'].set_color('none')
        axes.spines['top'].set_color('none')
        axes.spines['bottom'].set_color('none')
        axes.spines['left'].set_color('none')

    pfields, arena, aX, aY, centers = setup()
    theta = linspace(0, 2 * pi, 250)
    x = cos(theta) * 5 + 5
    y = sin(theta) * 5 + 5
    points, estimates_bayes, estimates_direct = [], [], []
    for rx, ry in zip(x, y):
        handles = []
        rates, obs_spikes = simulate_spikes(pfields, rx, ry)
        pp.subplot(1, 2, 1)
        a = decode_bayes(pfields, obs_spikes, arena)
        pp.contour(aX, aY, a)
        pp.plot(centers[:, 0], centers[:, 1], 'k+', alpha=.5)
        pp.hot()
        ey, ex = unravel_index(argmax(a), a.shape)
        estimates_bayes.append((aX[0, ex], aY[ey, 0]))
        points.append((rx, ry))
        pp.plot(array(points)[:, 0], array(points)[:, 1], 'b-', alpha=0.5)
        pp.plot(rx, ry, 'bo')
        pp.plot(aX[0, ex], aY[ey, 0], 'ko')
        pp.plot(array(estimates_bayes)[:, 0],
                array(estimates_bayes)[:, 1],
                'k-',
                alpha=0.5)
        pp.ylim([-.5, 10.5])
        pp.xlim([-.5, 10.5])
        remove_spines(pp.gca())

        pp.subplot(1, 2, 2)
        a = decode_directbasis(pfields, obs_spikes, arena)
        pp.contour(aX, aY, a)
        pp.plot(centers[:, 0], centers[:, 1], 'k+', alpha=.5)
        pp.hot()
        ey, ex = unravel_index(argmax(a), a.shape)
        estimates_direct.append((aX[0, ex], aY[ey, 0]))
        points.append((rx, ry))
        pp.plot(array(points)[:, 0], array(points)[:, 1], 'b-', alpha=.5)
        pp.plot(rx, ry, 'bo')
        pp.plot(aX[0, ex], aY[ey, 0], 'ko')
        pp.plot(array(estimates_direct)[:, 0],
                array(estimates_direct)[:, 1],
                'k-',
                alpha=.5)
        pp.ylim([-.5, 10.5])
        pp.xlim([-.5, 10.5])
        remove_spines(pp.gca())
        yield (handles)
Exemplo n.º 50
0
def makeplots(tractor, step, suffix):
    print('Making plots')

    ds = tractor.getCatalog()[0]
    logsa, logt, emis = ds.getArrays()
    suff = '.pdf'
    
    #plt.figure(figsize=(8,6))
    #plt.figure(figsize=(4.5,4))
    #plt.figure(figsize=(4,3.5))

    #spa = dict(left=0.01, right=0.99, bottom=0.02, top=0.92)
    spa = dict(left=0.005, right=0.995, bottom=0.02, top=0.92)
    H = 3.
    W1 = 3.5
    W2 = (0.90 / 0.99) * H
    print('W2', W2)

    plt.figure(1, figsize=(W1,H))
    plt.subplots_adjust(**spa)

    logsa = np.fliplr(logsa)
    logt = np.fliplr(logt)
    emis = np.fliplr(emis)
    
    # plt.clf()
    # plt.imshow(logsa, interpolation='nearest', origin='lower')
    # plt.gray()
    # plt.colorbar()
    # plt.title('Dust: log(solid angle)')
    # plt.savefig('logsa-%02i%s.png' % (step, suffix))
    
    plt.clf()
    #plt.imshow(np.exp(logsa), interpolation='nearest', origin='lower')
    plt.imshow(np.exp(logsa) * 1000., interpolation='nearest', origin='lower',
               vmin=0., vmax=6.)
    plt.hot()
    plt.colorbar(ticks=[0,2,4,6])
    plt.xticks([])
    plt.yticks([])
    plt.title('Dust Column Density (arb units)') #: solid angle')
    plt.savefig(('sa-%02i%s'+suff) % (step, suffix))
    
    # plt.clf()
    # plt.imshow(logt, interpolation='nearest', origin='lower')
    # plt.gray()
    # plt.colorbar()
    # plt.title('Dust: log(temperature)')
    # plt.savefig('logt-%02i%s'+suff % (step, suffix))
    
    plt.clf()
    plt.imshow(np.exp(logt), interpolation='nearest', origin='lower', vmin=0, vmax=30)
    plt.hot()
    plt.colorbar(ticks=[0,10,20,30])
    plt.xticks([])
    plt.yticks([])
    plt.title('Dust Temperature (K)')
    plt.savefig(('t-%02i%s'+suff) % (step, suffix))
    
    plt.clf()
    plt.imshow(emis, interpolation='nearest', origin='lower',
               vmin=1, vmax=2.5)
    plt.gray()
    plt.colorbar(ticks=[1.0, 1.5, 2.0, 2.5])
    plt.xticks([])
    plt.yticks([])
    plt.title('Dust Emissivity Index')
    plt.savefig(('emis-%02i%s'+suff) % (step, suffix))

    #plt.figure(figsize=(4,4))
    #plt.figure(figsize=(3,3))
    #plt.figure(figsize=(2.5,2.5))
    #plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.9)

    plt.figure(2, figsize=(W2,H))
    #plt.figure(figsize=(3,3))
    plt.subplots_adjust(**spa)

    for i,tim in enumerate(tractor.getImages()):
        ima = dict(interpolation='nearest', origin='lower',
                   vmin=tim.zr[0], vmax=tim.zr[1])
        plt.clf()
        plt.imshow(tim.getImage(), **ima)
        plt.gray()
        #plt.colorbar()
        #plt.title(tim.name)
        name = ['PACS 100', 'PACS 160', 'SPIRE 250', 'SPIRE 350', 'SPIRE 500'][i]
        plt.title('Data: ' + name)
        plt.xticks([])
        plt.yticks([])
        plt.savefig(('data-%i-%02i%s'+suff) % (i, step, suffix))

    mods = []
    for i,tim in enumerate(tractor.getImages()):
        ima = dict(interpolation='nearest', origin='lower',
                   vmin=tim.zr[0], vmax=tim.zr[1])

        print('Getting model image', i)
        mod = tractor.getModelImage(i)
        mods.append(mod)
        plt.clf()
        plt.imshow(mod, **ima)
        plt.gray()
        plt.title('Model: ' + tim.name)
        #plt.title(name)
        plt.xticks([])
        plt.yticks([])
        plt.savefig(('model-%i-%02i%s'+suff) % (i, step, suffix))

    # 
    #   if step == 0:
    #       plt.clf()
    #       plt.imshow(tim.getImage(), **ima)
    #       plt.gray()
    #       plt.colorbar()
    #       plt.title(tim.name)
    #       plt.savefig('data-%i.png' % (i))

    #mods = tractor.getModelImages()

    # plt.figure(figsize=(8,6))
    # for i,mod in enumerate(mods):
    #   tim = tractor.getImage(i)
    #   ima = dict(interpolation='nearest', origin='lower',
    #              vmin=tim.zr[0], vmax=tim.zr[1])
    #   plt.clf()
    #   plt.imshow(mod, **ima)
    #   plt.gray()
    #   plt.colorbar()
    #   plt.title(tim.name)
    #   plt.savefig('model-%i-%02i%s.png' % (i, step, suffix))
    # 
    #   if step == 0:
    #       plt.clf()
    #       plt.imshow(tim.getImage(), **ima)
    #       plt.gray()
    #       plt.colorbar()
    #       plt.title(tim.name)
    #       plt.savefig('data-%i.png' % (i))
    # 
    #   plt.clf()
    #   # tractor.getChiImage(i), 
    #   plt.imshow((tim.getImage() - mod) * tim.getInvError(),
    #              interpolation='nearest', origin='lower',
    #              vmin=-5, vmax=+5)
    #   plt.gray()
    #   plt.colorbar()
    #   plt.title(tim.name)
    #   plt.savefig('chi-%i-%02i%s.png' % (i, step, suffix))

    plt.figure(3, figsize=(16,8))
    plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.9, wspace=0.1, hspace=0.1)
    R,C = 3,len(mods)
    plt.clf()
    for i,mod in enumerate(mods):
        tim = tractor.getImage(i)

        print('Image', tim.name, ': data median', np.median(tim.getImage()), end=' ')
        print('model median:', np.median(mod))

        ima = dict(interpolation='nearest', origin='lower',
                   vmin=tim.zr[0], vmax=tim.zr[1])

        plt.subplot(R, C, i + 1)
        plt.imshow(tim.getImage(), **ima)
        plt.xticks([])
        plt.yticks([])
        plt.gray()
        plt.colorbar()
        plt.title(tim.name)

        plt.subplot(R, C, i + 1 + C)
        plt.imshow(mod, **ima)
        plt.xticks([])
        plt.yticks([])
        plt.gray()
        plt.colorbar()
        #plt.title(tim.name)

        plt.subplot(R, C, i + 1 + 2*C)
        plt.imshow((tim.getImage() - mod) * tim.getInvError(),
                   interpolation='nearest', origin='lower',
                   vmin=-5, vmax=+5)
        plt.xticks([])
        plt.yticks([])
        plt.gray()
        plt.colorbar()
        #plt.title(tim.name)
    plt.savefig('all-%02i%s.png' % (step, suffix))

    ds = tractor.getCatalog()[0]
    logsa, logt, emis = ds.getArrays()

    plt.figure(4, figsize=(16,5))
    plt.clf()

    plt.subplot(1,3,1)
    plt.imshow(np.exp(logsa), interpolation='nearest', origin='lower')
    plt.hot()
    plt.colorbar()
    plt.title('Dust: solid angle (Sr?)')

    plt.subplot(1,3,2)
    plt.imshow(np.exp(logt), interpolation='nearest', origin='lower') #, vmin=0)
    plt.hot()
    plt.colorbar()
    plt.title('Dust: temperature (K)')

    plt.subplot(1,3,3)
    plt.imshow(emis, interpolation='nearest', origin='lower')
    plt.gray()
    plt.colorbar()
    plt.title('Dust: emissivity')
    plt.savefig('dust-%02i%s.png' % (step, suffix))
Exemplo n.º 51
0
     states.append(inputs[-1])
     refs.append((-1)*np.ones((10,10)))
 for i in range(5):
     inputs.append(np.random.random((10,10)))
     #inputs.append(np.diag(np.ones((10))*np.random.random()))
     inputs[-1][:] = smooth(inputs[-1],9)#+0.1
     #states.append(np.zeros((10,10)))
     states.append(inputs[-1])
     refs.append(np.ones((10,10))*(1))
 ct = CNNTrainer(t_sim=40,errfunc="mean_abs_diff", opt_method="simplex")
 #ct = CNNTrainer(t_sim=40,errfunc="sum_sqr_diff", opt_method="powell")
 #ct = CNNTrainer(t_sim=40,errfunc="mean_abs_diff", opt_method="anneal")
 a,b,z = ct(inputs,states,refs)
 #p.ioff()
 #print "Test"
 p.hot()
 p.figure(1)
 for i in range(10):
     print i,
     cnn = CNN2d(inputs[i],states[i],a,b,z)
     try:
         o = cnn.integrate(np.arange(1,21,1))
         #p.figure(i+2)
         #for i2 in range(20):
         #    p.subplot(4,5,i2+1)
         #    #p.axes(frameon=False)
         #    p.imshow(cnn.sigmoid(o[i2].reshape((10,10))),interpolation="nearest",vmin=-1,vmax=1)
         #    p.show()
         #assert 1==0
         time.sleep(1)
         p.ioff()