예제 #1
0
    def romanzuniga07(wavelength, AKs, makePlot=False):
        filters = ['J', 'H', 'Ks', '[3.6]', '[4.5]', '[5.8]', '[8.0]']
        wave =      np.array([1.240, 1.664, 2.164, 3.545, 4.442, 5.675, 7.760])
        A_AKs =     np.array([2.299, 1.550, 1.000, 0.618, 0.525, 0.462, 0.455])
        A_AKs_err = np.array([0.530, 0.080, 0.000, 0.077, 0.063, 0.055, 0.059])
        
        # Interpolate over the curve
        spline_interp = interpolate.splrep(wave, A_AKs, k=3, s=0)

        A_AKs_at_wave = interpolate.splev(wavelength, spline_interp)
        A_at_wave = AKs * A_AKs_at_wave

        if makePlot:
            py.clf()
            py.errorbar(wave, A_AKs, yerr=A_AKs_err, fmt='bo', 
                        markerfacecolor='none', markeredgecolor='blue',
                        markeredgewidth=2)

            # Make an interpolated curve.
            wavePlot = np.arange(wave.min(), wave.max(), 0.1)
            extPlot = interpolate.splev(wavePlot, spline_interp)
            py.loglog(wavePlot, extPlot, 'k-')

            # Plot a marker for the computed value.
            py.plot(wavelength, A_AKs_at_wave, 'rs',
                    markerfacecolor='none', markeredgecolor='red',
                    markeredgewidth=2)
            py.xlabel('Wavelength (microns)')
            py.ylabel('Extinction (magnitudes)')
            py.title('Roman Zuniga et al. 2007')


        return A_at_wave
예제 #2
0
파일: blocks.py 프로젝트: gmelikian/regreg
def test1():
    import numpy as np
    import pylab
    from scipy import sparse

    from regreg.algorithms import FISTA
    from regreg.atoms import l1norm
    from regreg.container import container
    from regreg.smooth import quadratic

    Y = np.random.standard_normal(500); Y[100:150] += 7; Y[250:300] += 14

    sparsity = l1norm(500, lagrange=1.0)
    #Create D
    D = (np.identity(500) + np.diag([-1]*499,k=1))[:-1]
    D = sparse.csr_matrix(D)

    fused = l1norm.linear(D, lagrange=19.5)
    loss = quadratic.shift(-Y, lagrange=0.5)

    p = container(loss, sparsity, fused)
    
    soln1 = blockwise([sparsity, fused], Y)

    solver = FISTA(p)
    solver.fit(max_its=800,tol=1e-10)
    soln2 = solver.composite.coefs

    #plot solution
    pylab.figure(num=1)
    pylab.clf()
    pylab.scatter(np.arange(Y.shape[0]), Y, c='r')
    pylab.plot(soln1, c='y', linewidth=6)
    pylab.plot(soln2, c='b', linewidth=2)
예제 #3
0
파일: quick_look.py 프로젝트: BKJackson/vq
	def plot_stress(self, block_ids=None, fignum=0):
		block_ids = self.check_block_ids_list(block_ids)
		#
		plt.figure(fignum)
		ax1=plt.gca()
		plt.clf()
		plt.figure(fignum)
		plt.clf()
		ax0=plt.gca()

		#
		for block_id in block_ids:
			rws = numpy.core.records.fromarrays(zip(*filter(lambda x: x['block_id']==block_id, self.shear_stress_sequences)), dtype=self.shear_stress_sequences.dtype)
			stress_seq = []
			for rw in rws:
				stress_seq += [[rw['sweep_number'], rw['shear_init']]]
				stress_seq += [[rw['sweep_number'], rw['shear_final']]]
			X,Y = zip(*stress_seq)
			#
			ax0.plot(X,Y, '.-', label='block_id: %d' % block_id)
			#
			plt.figure(fignum+1)
			plt.plot(rws['sweep_number'], rws['shear_init'], '.-', label='block_id: %d' % block_id)
			plt.plot(rws['sweep_number'], rws['shear_final'], '.-', label='block_id: %d' % block_id)
			plt.figure(fignum)
		ax0.plot([min(self.shear_stress_sequences['sweep_number']), max(self.shear_stress_sequences['sweep_number'])], [0., 0.], 'k-')
		ax0.legend(loc=0, numpoints=1)
		plt.figure(fignum)
		plt.title('Block shear_stress sequences')
		plt.xlabel('sweep number')
		plt.ylabel('shear stress')
예제 #4
0
def VisualizeAlm(alm,figno=1,max_l=None):
    """ Visualize a healpy a_lm vector """
    lmax = hp.Alm.getlmax(f_lm.size)
    l,m = hp.Alm.getlm(lmax)
    mag = np.zeros([lmax+1,lmax+1])
    phs = np.zeros([lmax+1,lmax+1])
    mag[m,l] = np.abs(alm)
    phs[m,l] = np.angle(alm)
    cl = hp.alm2cl(alm)
    # Decide the range of l to plot
    if max_l != None:
        max_l = (max_l if (max_l <= lmax) else lmax)
    else:
        max_l = lmax 
    print max_l
    plt.figure(figno)
    plt.clf()
    plt.subplot(211)
    plt.imshow(mag[0:max_l,0:max_l],interpolation='nearest',origin='lower')
    plt.colorbar()
    plt.subplot(212)
    plt.imshow(phs[0:max_l,0:max_l],interpolation='nearest',origin='lower')
    plt.colorbar()
    # plt.subplot(313)
    #plt.semilogy(cl[0:max_l])
    return {'mag':mag,'phs':phs,'cl':cl}
예제 #5
0
def check_vpd_ks2_astrometry():
    """
    Check the VPD and quiver plots for our KS2-extracted, re-transformed astrometry.
    """
    catFile = workDir + '20.KS2_PMA/wd1_catalog.fits'
    tab = atpy.Table(catFile)

    good = (tab.xe_160 < 0.05) & (tab.ye_160 < 0.05) & \
        (tab.xe_814 < 0.05) & (tab.ye_814 < 0.05) & \
        (tab.me_814 < 0.05) & (tab.me_160 < 0.05)

    tab2 = tab.where(good)

    dx = (tab2.x_160 - tab2.x_814) * ast.scale['WFC'] * 1e3
    dy = (tab2.y_160 - tab2.y_814) * ast.scale['WFC'] * 1e3

    py.clf()
    q = py.quiver(tab2.x_814, tab2.y_814, dx, dy, scale=5e2)
    py.quiverkey(q, 0.95, 0.85, 5, '5 mas', color='red', labelcolor='red')
    py.savefig(workDir + '20.KS2_PMA/vec_diffs_ks2_all.png')

    py.clf()
    py.plot(dy, dx, 'k.', ms=2)
    lim = 30
    py.axis([-lim, lim, -lim, lim])
    py.xlabel('Y Proper Motion (mas)')
    py.ylabel('X Proper Motion (mas)')
    py.savefig(workDir + '20.KS2_PMA/vpd_ks2_all.png')

    idx = np.where((np.abs(dx) < 10) & (np.abs(dy) < 10))[0]
    print('Cluster Members (within dx < 10 mas and dy < 10 mas)')
    print(('   dx = {dx:6.2f} +/- {dxe:6.2f} mas'.format(dx=dx[idx].mean(),
                                                        dxe=dx[idx].std())))
    print(('   dy = {dy:6.2f} +/- {dye:6.2f} mas'.format(dy=dy[idx].mean(),
                                                        dye=dy[idx].std())))
예제 #6
0
def window_fn_matrix(Q,N,num_remov=None,save_tag=None,lms=None):
    Q = n.matrix(Q); N = n.matrix(N)
    Ninv = uf.pseudo_inverse(N,num_remov=None) # XXX want to remove dynamically
    #print Ninv 
    info = n.dot(Q.H,n.dot(Ninv,Q))
    M = uf.pseudo_inverse(info,num_remov=num_remov)
    W = n.dot(M,info)

    if save_tag!=None:
        foo = W[0,:]
        foo = n.real(n.array(foo))
        foo.shape = (foo.shape[1]),
        print foo.shape
        p.scatter(lms[:,0],foo,c=lms[:,1],cmap=mpl.cm.PiYG,s=50)
        p.xlabel('l (color is m)')
        p.ylabel('W_0,lm')
        p.title('First Row of Window Function Matrix')
        p.colorbar()
        p.savefig('{0}/{1}_W.pdf'.format(fig_loc,save_tag))
        p.clf()

        print 'W ',W.shape
        p.imshow(n.real(W))
        p.title('Window Function Matrix')
        p.colorbar()
        p.savefig('{0}/{1}_W_im.pdf'.format(fig_loc,save_tag))
        p.clf()


    return W
예제 #7
0
    def test_draw_xy_plot(self):
        """draw_xy_plot() properly produces an output html file
        """
        out_file = os.path.join(self.dir_name, "test.html")
        argv = (
            "p.plot -x x -y btrace ctrace -s o- --xlabel myxlabel "
            "--ylabel myylabel --title mytitle --theme darkgrid "
            "--context talk --palette muted -a .5 --nogrid "
            "--legend best --xlim 0 10 --ylim -10 10 "
            "--savefig {}".format(out_file)
        ).split()
        with patch("pandashells.lib.plot_lib.sys.argv", argv):
            pl.clf()
            df = pd.DataFrame({"x": range(10), "btrace": [-x for x in range(10)], "ctrace": [x for x in range(10)]})
            parser = argparse.ArgumentParser()
            arg_lib.add_args(parser, "io_in", "xy_plotting", "decorating", "example")

            parser.add_argument("-a", "--alpha", help="Set opacity", nargs=1, default=[1.0], type=float)
            args = parser.parse_args()
            plot_lib.draw_xy_plot(args, df)
            with open(out_file) as f:
                html = f.read()
                self.assertTrue("myxlabel" in html)
                self.assertTrue("myylabel" in html)
                self.assertTrue("mytitle" in html)
                self.assertTrue("btrace" in html)
                self.assertTrue("ctrace" in html)
                self.assertTrue("1" in html)
                self.assertTrue("10" in html)
예제 #8
0
파일: great3.py 프로젝트: barentsen/tractor
 def plotmodel(tractor, sig1, tt):
     mod = tractor.getModelImage(0)
     data = tractor.getImage(0).getImage()
     noise = np.random.normal(size=data.shape) * sig1
     imchi = dict(interpolation='nearest', origin='lower', cmap='RdBu',
                  vmin=-3, vmax=3)
     plt.clf()
     plt.subplot(2,2,1)
     plt.imshow(data, **ima)
     plt.title('Image')
     plt.xticks([]); plt.yticks([])
     plt.subplot(2,2,2)
     plt.imshow(mod, **ima)
     plt.title('Model')
     plt.xticks([]); plt.yticks([])
     plt.subplot(2,2,3)
     plt.imshow(mod+noise, **ima)
     plt.title('Model + noise')
     plt.xticks([]); plt.yticks([])
     plt.subplot(2,2,4)
     # show mod - img to match "red-to-blue" colormap.
     plt.imshow(-(data - mod)/sig1, **imchi)
     plt.xticks([]); plt.yticks([])
     plt.title('Chi')
     plt.suptitle(tt)
     ps.savefig()
예제 #9
0
def plotslice(pos,filename='',boxsize=100.):
    ng = pos.shape[0]
    M.clf()
    M.scatter(pos[ng/4,:,:,1].flatten(),pos[ng/4,:,:,2].flatten(),s=1.,lw=0.)
    M.axis('tight')
    if filename != '':
        M.savefig(filename)
예제 #10
0
	def update(self):
		if self.pose != []:
			plt.figure(1)
			clf()
			self.fig1 = plt.figure(num=1, figsize=(self.window_size, \
				self.window_size), dpi=80, facecolor='w', edgecolor='w')
			title (self.title)			
			xlabel('Easting [m]')
			ylabel('Northing [m]')
			axis('equal')
			grid (True)
			poseT = zip(*self.pose)	
			pose_plt = plot(poseT[1],poseT[2],'#ff0000')

			if self.wptnav != []:
				mode = self.wptnav[-1][MODE]

				if not (self.wptnav[-1][B_E] == 0 and self.wptnav[-1][B_N] == 0 and self.wptnav[-1][A_E] == 0 and self.wptnav[-1][A_N] == 0):
					b_dot = plot(self.wptnav[-1][B_E],self.wptnav[-1][B_N],'ro',markersize=8)
					a_dot = plot(self.wptnav[-1][A_E],self.wptnav[-1][A_N],'go',markersize=8)
					ab_line = plot([self.wptnav[-1][B_E],self.wptnav[-1][A_E]],[self.wptnav[-1][B_N],self.wptnav[-1][A_N]],'g')
					target_dot = plot(self.wptnav[-1][TARGET_E],self.wptnav[-1][TARGET_N],'ro',markersize=5)

				if mode == -1:
					pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'b^',markersize=8)
				elif mode == 1:
					pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'bs',markersize=8)
				elif mode == 2:
					pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'bo',markersize=8)

		if self.save_images:
			self.fig1.savefig ('plot_map%05d.jpg' % self.image_count)
			self.image_count += 1
		draw()
예제 #11
0
파일: sim.py 프로젝트: a-rahimi/sqp-control
def test_path():
  "generate and draw a random path"
  path = genpath()
  P.ion()
  P.clf()
  draw_path(P.gca(), path)
  P.draw()
예제 #12
0
def do_Plot_item_a(Ustar, U, grad_phi):
    """ plotting stuff: here we plot the first row with Ustar and
        the second rows with Ud and grad phi, to see how they sum up """
    pylab.clf()
    pylab.cla()
    
    f = pylab.figure() 
    f.text(.5, .95, r"Top: $U = U_d + \nabla \phi$, where "  r"$U_d$ =  - $\sin^2 ( \pi x)\sin(2 \pi y)\hat x$ + $\sin^2(\pi y)\sin(2\pi x)\hat y$ and $\phi = \frac{1}{10} \cos(2\pi y) \cos(2\pi x$) ",  horizontalalignment='center', size=9)

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

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

    pylab.subplot(223)
    pylab.imshow(U [0])
    pylab.ylabel("# of cells",size=8)
    pylab.xlabel("# of cells",size=8)
    
    
    pylab.subplot(224)
    pylab.imshow(grad_phi [0])
    pylab.xlabel("# of cells",size=8)
    pylab.ylabel("# of cells",size=8)
    pylab.savefig("plots/item_a_Ustar.png")
    
    return 0
예제 #13
0
    def __call__(self, n):
        if len(self.f.shape) == 3:
            # f = f[x,v,t], 2 dim in phase space
            ft = self.f[n,:,:]
            pylab.pcolormesh(self.X, self.V, ft.T, cmap = 'jet')
            pylab.colorbar()
            pylab.clim(0,0.38) # for Landau test case
            pylab.grid()
            pylab.axis([self.xmin, self.xmax, self.ymin, self.ymax])
            pylab.xlabel('$x$', fontsize = 18)
            pylab.ylabel('$v$', fontsize = 18)
            pylab.title('$N_x$ = %d, $N_v$ = %d, $t$ = %2.1f' % (self.x.N, self.v.N, self.it*self.t.width))
            pylab.savefig(self.path + self.filename)
            pylab.clf()
            return None

        if len(self.f.shape) == 2:
            # f = f[x], 1 dim in phase space
            ft = self.f[n,:]
            pylab.plot(self.x.gridvalues,ft,'ob')
            pylab.grid()
            pylab.axis([self.xmin, self.xmax, self.ymin, self.ymax])
            pylab.xlabel('$x$', fontsize = 18)
            pylab.ylabel('$f(x)$', fontsize = 18)
            pylab.savefig(self.path + self.filename)
            return None
예제 #14
0
    def show(self,filename=None):
        self.fig = fig = P.figure(self.fignum,self.figsize)
        P.clf()

        header = self.maps[0].header

        self.grid = grid = ImageGrid(fig, (1, 1, 1), 
                                     nrows_ncols = (self.nrows, self.ncols),
                                     share_all=True,
                                     axes_class=(pywcsgrid2.Axes,
                                                 dict(header=header)),
                                    **self.grid_kwargs)

        for i,(map,lower,upper) in enumerate(zip(self.maps,self.lower_energies,self.upper_energies)):
            map.show(axes=grid[i], cax=grid[i].cax)
            format_energy=lambda x: '%.1f' % (x/1000.) if x < 1000 else '%.0f' % (x/1000.)
            lower_string=format_energy(lower)
            upper_string=format_energy(upper)
            grid[i].add_inner_title("%s-%s GeV" % (lower_string,upper_string), loc=2)

        if self.title is not None:
            fig.suptitle(self.title)

        tight_layout(fig)
        if filename is not None: P.savefig(filename)
예제 #15
0
파일: pp.py 프로젝트: remosu/jobjob
def plot_ch():
    for job in jobs_orig:
        print "plane of", job.path
        pylab.clf()
        x_center = int((job.position(0)[0] + job.position(1)[0])/2)
        x_final = 50 + x_center
        #plane = np.concatenate((job.plane(y=50)[:, x_final:], 
        #                        job.plane(y=50)[:, :x_final]), axis=1)
        plane = job.plane(y=50)
        myplane = plane[plane < 0.0]
        p0 = myplane.min()
        p12 = np.median(myplane)
        p14 = np.median(myplane[myplane<p12])
        p34 = np.median(myplane[myplane>p12])
        p1 = myplane.max()
        contour_values = (p0, p14, p12, p34, p1)
        pylab.title(r'$u_x=%.4f,\  D_{-}=%.4f,\  D_{+}=%.4f,\ ch=%i$ ' %
                    (job.u_x, job.D_minus, job.D_plus, job.ch_objects))
        car = pylab.imshow(plane, vmin=-0.001, vmax=0.0, 
                           interpolation='nearest')
        pylab.contour(plane, contour_values, linestyles='dashed', 
                                             colors='white')
        pylab.grid(True)
        pylab.colorbar(car)
        #imgfilename = 'plane_r20-y50-u_x%.4fD%.4fch%03i.png' % \
        #              (job.u_x, job.D_minus, job.ch_objects)
        imgfilename = 'plane_%s.png' % job.job_id
        pylab.savefig(imgfilename)
예제 #16
0
 def imshow_box(f,im, x,y,s):
     '''imshow_box(f,im, x,y,s)
     f: figure
     im: image
     x: center coordinate for box
     y: center coord
     s: box shape, (width, height)
     '''
     global coord
     P.figure(f.number)
     P.clf();
     P.imshow(im);
     P.axhline(y-s[1]/2.)
     P.axhline(y+s[1]/2.)
     P.axvline(x-s[0]/2.)
     P.axvline(x+s[0]/2.)
     xy=crop(m,s,y,x)
     coord=(0.5*(xy[2]+xy[3]), 0.5*(xy[0]+xy[1]))
     P.title(str('x: %d y: %d' % (x,y)));        
     P.figure(999);
     P.imshow(master[xy[0]:xy[1],xy[2]:xy[3]])
     P.title('Master');
     P.figure(998);
     df=(master[xy[0]:xy[1],xy[2]:xy[3]]-slave)
     P.imshow(np.abs(df))
     P.title(str('RMS: %0.6f' % np.sqrt((df**2.).mean()) ));        
예제 #17
0
 def savepng(pre, img, title=None, **kwargs):
     fn = '%s-%s.png' % (pre, idstr)
     print 'Saving', fn
     plt.clf()
     plt.imshow(img, **kwargs)
     ax = plt.axis()
     if debug:
         print len(xplotx),len(allobjx)
         for i,(objx,objy,objc) in enumerate(zip(allobjx,allobjy,allobjc)):
             plt.plot(objx,objy,'-',c=objc)
             tempx = []
             tempx.append(xplotx[i])
             tempx.append(objx[0])
             tempy = []
             tempy.append(xploty[i])
             tempy.append(objy[0])
             plt.plot(tempx,tempy,'-',c='purple')
         plt.plot(pointx,pointy,'y.')
         plt.plot(xplotx,xploty,'xg')
     plt.axis(ax)
     if title is not None:
         plt.title(title)
     plt.colorbar()
     plt.gray()
     plt.savefig(fn)
예제 #18
0
파일: kepmask.py 프로젝트: rodluger/PyKE
def clicker2(event):

    global mask, aid, bid, cid, did, eid, fid, done

    if event.inaxes:
        if event.button == 1:
            if (event.x > 601 and event.x < 801 and
                event.y > 422 and event.y < 482):
                disconnect(aid)
                disconnect(bid)
                disconnect(cid)
                disconnect(did)
                disconnect(eid)
                disconnect(fid)
                try:
                    lines, status = kepio.openascii(maskfile,'r',None,False)
                    for line in lines:
                        mask = []
                        work = line.strip().split('|')
                        y0 = int(work[3])
                        x0 = int(work[4])
                        work = work[5].split(';')
                        for i in range(len(work)):
                            y = int(work[i].split(',')[0]) + y0
                            x = int(work[i].split(',')[1]) + x0
                            mask.append(str(x) + ',' + str(y))
                        pylab.clf()
                        plotimage(cmdLine)
                except:
                    txt = 'ERROR -- KEPMASK: Cannot open or read mask file ' + maskfile
                    kepmsg.err(logfile,txt,True)
                     
    return
예제 #19
0
    def draw(self):

        print self.edgeno

        pos = 0
        dy = 8
        edgeno = self.edgeno
        edge = self.edges[edgeno]
        edgeprev = self.edges[edgeno-1]
        p = np.round(edge["top"](1024))
        top = min(p+2*dy, 2048)
        bot = min(p-2*dy, 2048)
        self.cutout = self.flat[1][bot:top,:].copy()

        pl.figure(1)
        pl.clf()
        start = 0
        dy = 512
        for i in xrange(2048/dy):
            pl.subplot(2048/dy,1,i+1)
            pl.xlim(start, start+dy)

            if i == 0: pl.title("edge %i] %s|%s" % (edgeno,
                edgeprev["Target_Name"], edge["Target_Name"]))


            pl.subplots_adjust(left=.07,right=.99,bottom=.05,top=.95)

            pl.imshow(self.flat[1][bot:top,start:start+dy], extent=(start,
                start+dy, bot, top), cmap='Greys', vmin=2000, vmax=6000)

            pix = np.arange(start, start+dy)
            pl.plot(pix, edge["top"](pix), 'r', linewidth=1)
            pl.plot(pix, edgeprev["bottom"](pix), 'r', linewidth=1)
            pl.plot(edge["xposs_top"], edge["yposs_top"], 'o')
            pl.plot(edgeprev["xposs_bot"], edgeprev["yposs_bot"], 'o')


            hpp = edge["hpps"]
            pl.axvline(hpp[0],ymax=.5, color='blue', linewidth=5)
            pl.axvline(hpp[1],ymax=.5, color='red', linewidth=5)

            hpp = edgeprev["hpps"]
            pl.axvline(hpp[0],ymin=.5,color='blue', linewidth=5)
            pl.axvline(hpp[1],ymin=.5,color='red', linewidth=5)


            if False:
                L = top-bot
                Lx = len(edge["xposs"])
                for i in xrange(Lx):
                    xp = edge["xposs"][i]
                    frac1 = (edge["top"](xp)-bot-1)/L
                    pl.axvline(xp,ymin=frac1)

                for xp in edgeprev["xposs"]: 
                    frac2 = (edgeprev["bottom"](xp)-bot)/L
                    pl.axvline(xp,ymax=frac2)

            start += dy
예제 #20
0
def displayRetirementWithMonthlies(monthlies, rate, terms):
    plt.figure('retireMonth')
    plt.clf()
    for monthly in monthlies:
        xvals, yvals = retire(monthly, rate, terms)
        plt.plot(xvals, yvals, label = 'retire:'+str(monthly))
        plt.legend(loc = 'upper left')
예제 #21
0
  def  plot_item(self, m, ind, x, r, k, label):
    """plot_item(self, m, ind, x, r, k, label)

    Plot selection m (index ind, data in x) and its reconstruction r,
    with k and label to annotate of the plot.
    """
    
    if x == [] or r == []: 
      print "Error: No data in x and/or r."
      return
  
    pylab.clf()
    # xvals, x, and r need to be column vectors
    pylab.plot(self.xvals, r, color='0.6', label='Expected')
    # Color code:
    # positive residuals = red
    # negative residuals = blue
    pylab.plot(self.xvals, x, color='0.0', label='Observed')
    posres = np.where((x-r) > 0)[0]
    negres = np.where((x-r) < 0)[0]
    pylab.plot(self.xvals[posres], x[posres], 'r.', markersize=3, label='Higher')
    pylab.plot(self.xvals[negres], x[negres], 'b.', markersize=3, label='Lower')

    pylab.xlabel(self.xlabel)
    pylab.ylabel(self.ylabel)
    pylab.title('DEMUD selection %d (%s), item %d, using K=%d' % \
                (m, label, ind, k))
    pylab.legend() #fontsize=10)
  
    outdir = os.path.join('results', self.name)
    if not os.path.exists(outdir):
      os.mkdir(outdir)
    figfile = os.path.join(outdir, 'sel-%d-k-%d-(%s).png' % (m, k, label))
    pylab.savefig(figfile)
    print 'Wrote plot to %s' % figfile
예제 #22
0
    def compute_parameters(input_data):
        """
        Build the required HTML response to be displayed.
        """
        figure = pylab.figure(figsize=(7, 8))
        pylab.clf()

        matrix = input_data.ordered_weights
        matrix_size = input_data.number_of_regions
        labels = input_data.ordered_labels
        plot_title = "Connection Strength"

        order = numpy.arange(matrix_size)
        # Assumes order is shape (number_of_regions, )
        order_rows = order[:, numpy.newaxis]
        order_columns = order_rows.T

        axes = figure.gca()
        img = axes.matshow(matrix[order_rows, order_columns])
        axes.set_title(plot_title)
        figure.colorbar(img)

        if labels is None:
            return

        axes.set_yticks(numpy.arange(matrix_size))
        axes.set_yticklabels(list(labels[order]), fontsize=8)
        axes.set_xticks(numpy.arange(matrix_size))
        axes.set_xticklabels(list(labels[order]), fontsize=8, rotation=90)

        figure.canvas.draw()
        parameters = dict(mplh5ServerURL=TvbProfile.current.web.MPLH5_SERVER_URL,
                          figureNumber=figure.number, showFullToolbar=False)
        return parameters, {}
예제 #23
0
파일: ACF.py 프로젝트: RuthAngus/K-ACF
def EBTransitPhase(tset, kid_x):
    ebp = atpy.Table('%s/eb_pars.txt' %dir, type = 'ascii')
    
    lc = tset.tables[1]   
    time = lc.TIME
    flux = lc.PDCSAP_FLUX
    nobs = len(time)
    lg = scipy.isfinite(time)
    pylab.figure(52)
    pylab.clf()
    pylab.plot(time[lg], flux[lg])
    npl = 2
    phase = scipy.zeros((npl, nobs))
    inTr = scipy.zeros((npl, nobs), 'int')
    period = ebp.P[ebp.KID == kid_x]
    for ipl in scipy.arange(npl):
        if ipl == 0: t0 = ebp.Ep1[ebp.KID == kid_x]
        if ipl == 1: t0 = ebp.Ep2[ebp.KID == kid_x]
        if ipl == 0: dur = ebp.Dur1[ebp.KID == kid_x]
        if ipl == 1: dur = ebp.Dur2[ebp.KID == kid_x]
        dur /= period
        counter = 0
        while (time[lg] - t0).min() < 0:
            t0 -= period
            counter += 1
            if counter > 1000: break
        ph = ((time - t0) % period) / period
        ph[ph < -0.5] += 1
        ph[ph > 0.5] -= 1
        phase[ipl,:] = ph
        inTr[ipl,:] = (abs(ph) <= dur/1.5)
    return phase, inTr
예제 #24
0
    def dovis(self):
        """
        Do runtime visualization. 
        """

        pylab.clf()

        phi = self.cc_data.get_var("phi")

        myg = self.cc_data.grid

        pylab.imshow(numpy.transpose(phi[myg.ilo:myg.ihi+1,
                                         myg.jlo:myg.jhi+1]), 
                     interpolation="nearest", origin="lower",
                     extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

        pylab.xlabel("x")
        pylab.ylabel("y")
        pylab.title("phi")

        pylab.colorbar()
        
        pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t)

        pylab.draw()
예제 #25
0
def field_map_ndar(ndar_field,t,ar_coorx,ar_coory,X,image_out,variable):

    ar_field=ndar_field[t,:]
    max_val=int(np.max(ndar_field))
    if variable==4:
        max_val=100.
    xmin=min(ar_coorx);xmax=max(ar_coorx)
    ymin=min(ar_coory);ymax=max(ar_coory)
    step=X
    nx=(xmax-xmin)/step+1
    ny=(ymax-ymin)/step+1

    ar_indx=np.array((ar_coorx-xmin)/step,int)
    ar_indy=np.array((ar_coory-ymin)/step,int)

    ar_map=np.ones((ny,nx))*-99.9
    ar_map[ar_indy,ar_indx]=ar_field

    ar_map2 = M.masked_where(ar_map <0, ar_map)
    ut.check_file_exist(image_out)

    pl.clf()
    pl.axes(axisbg='gray')
    pl.imshow(ar_map2, cmap=pl.cm.RdBu,
              interpolation='Nearest', origin='lower', vmax=max_val, vmin=0)
    pl.title('time step= '+ut.string(t,len(str(t))))
    pl.colorbar()
    pl.savefig(image_out)
예제 #26
0
def plot_vector_diff_F160W():
    """
    Using the xym2mat analysis on the F160W filter in the 2010 data set,
    plot up the positional offset vectors for each reference star in
    each star list relative to the average list. This should show
    us if there are systematic problems with the distortion solution
    or PSF variations.
    """
    x, y, m, xe, ye, me, cnt = load_catalog()

    dx = x - x[0]
    dy = y - y[0]
    dr = np.hypot(dx, dy)

    py.clf()
    py.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95)

    for ii in range(1, x.shape[0]):
        idx = np.where((x[ii,:] != 0) & (y[ii,:] != 0) & (dr[ii,:] < 0.1) &
                       (xe < 0.05) & (ye < 0.05))[0]

        py.clf()
        q = py.quiver(x[0,idx], y[0,idx], dx[ii, idx], dy[ii, idx], scale=1.0)
        py.quiverkey(q, 0.9, 0.9, 0.03333, label='2 mas', color='red')
        py.title('{0} stars in list {1}'.format(len(idx), ii))
        py.xlim(0, 4500)
        py.ylim(0, 4500)

        foo = input('Continue?')
        if foo == 'q' or foo == 'Q':
            break
def plot_mcmc_results(chain):
    # Pull m and b arrays out of the Markov chain.
    mm = [m for b,m in chain]
    bb = [b for b,m in chain]
    # Scatterplot of m,b posterior samples
    plt.clf()
    plt.contour(bgrid, mgrid, posterior, pdf_contour_levels(posterior))
    plt.plot(bb, mm, 'b.', alpha=0.1)
    plot_mb_setup()
    plt.show()
    
    # Histograms
    import triangle
    triangle.corner(chain, labels=['b','m'], extents=[0.99]*2)
    plt.show()
    
    # Traces
    plt.clf()
    plt.subplot(2,1,1)
    plt.plot(mm, 'k-')
    plt.ylim(mlo,mhi)
    plt.ylabel('m')
    plt.subplot(2,1,2)
    plt.plot(bb, 'k-')
    plt.ylabel('b')
    plt.ylim(blo,bhi)
    plt.show()
예제 #28
0
 def newFigLayer():
     pylab.clf()
     pylab.figure(figsize=(8, 8))
     pylab.axes([0.15, 0.15, 0.8, 0.81])
     pylab.axis([0.6, -0.4, -0.4, 0.6])
     pylab.xlabel(r"$\Delta$\textsf{RA from Sgr A* (arcsec)}")
     pylab.ylabel(r"$\Delta$\textsf{Dec. from Sgr A* (arcsec)}")
예제 #29
0
    def test_pixpsf(self):
        tim,tinf = get_tractor_image_dr8(94, 2, 520, 'i', psf='kl-pix',
                                         roi=[500,600,500,600], nanomaggies=True)
        psf = tim.getPsf()
        print 'PSF', psf

        for i,(dx,dy) in enumerate([
                (0.,0.), (0.2,0.), (0.4,0), (0.6,0),
                (0., -0.2), (0., -0.4), (0., -0.6)]):
            px,py = 50.+dx, 50.+dy
            patch = psf.getPointSourcePatch(px, py)
            print 'Patch size:', patch.shape
            print 'x0,y0', patch.x0, patch.y0
            H,W = patch.shape
            XX,YY = np.meshgrid(np.arange(W), np.arange(H))
            im = patch.getImage()
            cx = patch.x0 + (XX * im).sum() / im.sum()
            cy = patch.y0 + (YY * im).sum() / im.sum()
            print 'cx,cy', cx,cy
            print 'px,py', px,py

            self.assertLess(np.abs(cx - px), 0.1)
            self.assertLess(np.abs(cy - py), 0.1)
            
            plt.clf()
            plt.imshow(patch.getImage(), interpolation='nearest', origin='lower')
            plt.title('dx,dy %f, %f' % (dx,dy))
            plt.savefig('pixpsf-%i.png' % i)
예제 #30
0
def graphTSPResults(resultsDir,numberOfTours):
    x=[]
    y=[]
    files=[open("%s/objectiveFunctionReport.txt" % resultsDir),
       open("%s/fitnessReport.txt" % resultsDir)]
    for f in files:
        x.append([])
        y.append([])
        i=len(x)-1
        for line in f:
            line=line.split(',')
            if line[0] != "gen":
                x[i].append(int(line[0]))
                y[i].append(float(line[1] if i==1 else line[2]))
            
    ylen=len(y[0])
    pl.subplot(2,1,1)
    pl.plot(x[0],y[0],'bo')
    pl.ylabel('Minimum Distance')
    pl.title("TSP with a %s City Tour" % numberOfTours)
    pl.annotate("{0:,}".format(y[0][0]),xy=(x[0][0],y[0][0]),  xycoords='data',
             xytext=(30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle="->") )
    pl.annotate("{0:,}".format(y[0][ylen-1]),xy=(x[0][ylen-1],y[0][ylen-1]),  xycoords='data',
             xytext=(-30,30), textcoords='offset points',
             arrowprops=dict(arrowstyle="->") )

    pl.subplot(2,1,2)
    pl.plot(x[1],y[1],'go')
    pl.xlabel('Generation')
    pl.ylabel('Fitness')
    pl.savefig("%s/tsp_result.png" % resultsDir)
    pl.clf()
예제 #31
0
def find_edge_pair(data, y, roi_width, edgeThreshold=450):
    '''
    find_edge_pair finds the edge of a slit pair in a flat

    data[2048x2048]: a well illuminated flat field [DN]
    y: guess of slit edge position [pix]

    Keywords:
    
    edgeThreshold: the pixel value below which we should ignore using
    to calculate edges.
    
    Moves along the edge of a slit image
            - At each location along the slit edge, determines
            the position of the demarcations between two slits

    Outputs:
    xposs []: Array of x positions along the slit edge [pix]
    yposs []: The fitted y positions of the "top" edge of the slit [pix]
    widths []: The fitted delta from the top edge of the bottom [pix]
    scatters []: The amount of light between slits


    The procedure is as follows
    1: starting from a guess spatial position (parameter y), march
        along the spectral direction in some chunk of pixels
    2: At each spectral location, construct a cross cut across the
        spatial direction; select_roi is used for this.
    3: Fit a two-sided error function Fit.residual_disjoint_pair
        on the vertical cross cut derived in step 2.
    4: If the fit fails, store it in the missing list
        - else if the top fit is good, store the top values in top vector
        - else if the bottom fit is good, store the bottom values in bottom
          vector.
    5: In the vertical cross-cut, there is a minimum value. This minimum
        value is stored as a measure of scattered light.

    Another procedure is used to fit polynomials to these fitted values.
    '''

    def select_roi(data, roi_width):
        v = data[y-roi_width:y+roi_width, xp-2:xp+2]
        v = np.median(v, axis=1) # Axis = 1 is spatial direction

        return v



    xposs_top = []
    yposs_top = []
    xposs_top_missing = []

    xposs_bot = []
    yposs_bot = []
    xposs_bot_missing = []
    yposs_bot_scatters = []

    #1 
    rng = np.linspace(10, 2040, 50).astype(np.int)
    for i in rng:
        xp = i
        #2
        v = select_roi(data, roi_width)
        xs = np.arange(len(v))

        # Modified from 450 as the hard coded threshold to one that
        # can be controlled by a keyword
        if (np.median(v) < edgeThreshold):
            xposs_top_missing.append(xp)
            xposs_bot_missing.append(xp)
            continue

        #3
        ff = Fit.do_fit(v, residual_fun=Fit.residual_disjoint_pair)
        fit_ok = 0 < ff[4] < 4

        if fit_ok:
            (sigma, offset, mult1, mult2, add, width) = ff[0]

            xposs_top.append(xp)
            yposs_top.append(y - roi_width + offset + width)

            xposs_bot.append(xp)
            yposs_bot.append(y - roi_width + offset)

            between = offset + width/2
            if 0 < between < len(v)-1:
                start = np.max([0, between-2])
                stop = np.min([len(v),between+2])
                yposs_bot_scatters.append(np.min(v[start:stop])) # 5

                if False:
                    pl.figure(2)
                    pl.clf()
                    tmppix = np.arange(y-roi_width, y+roi_width)
                    tmpx = np.arange(len(v))
                    pl.axvline(y - roi_width + offset + width, color='red')
                    pl.axvline(y - roi_width + offset, color='red')
                    pl.scatter(tmppix, v)
                    pl.plot(tmppix, Fit.fit_disjoint_pair(ff[0], tmpx))
                    pl.axhline(yposs_bot_scatters[-1])
                    pl.draw()

            else:
                yposs_bot_scatters.append(np.nan)

        else:
            xposs_bot_missing.append(xp)
            xposs_top_missing.append(xp)
            info("Skipping wavelength pixel): %i" % (xp))

    
    return map(np.array, (xposs_bot, xposs_bot_missing, yposs_bot, xposs_top,
        xposs_top_missing, yposs_top, yposs_bot_scatters))
예제 #32
0
def plot_cmd(allmags, i2mags, band, catflags, classstar):
    print('i2 mags shape', i2mags.shape)
    print('allmags shape', allmags.shape)
    print('catflags shape', catflags.shape)

    plt.figure(figsize=(6, 6))
    plt.clf()
    #plotpos0 = [0.15, 0.15, 0.84, 0.80]
    #plt.gca().set_position(plotpos0)
    xx, yy, xerr = [], [], []
    xx2, xerr2 = [], []
    for i2, rr in zip(i2mags, allmags):
        #print 'rr', rr

        # When the source is off the image, the optimizer doesn't change anything and
        # we end up with r = i2
        I = (rr != i2)
        rr = rr[I]
        ii2 = i2.repeat(len(rr))

        rr = np.minimum(rr, 25.)

        #plt.plot(rr - ii2, ii2, 'o', mfc='b', mec='none', ms=5, alpha=0.5)
        mr = np.mean(rr)
        sr = np.std(rr)
        #plt.plot([(mr-sr) - i2, (mr+sr) - i2], [i2,i2], 'b-', lw=3, alpha=0.25)

        medr = np.median(rr)
        iqr = (1. / 0.6745) * 0.5 * (np.percentile(rr, 75) -
                                     np.percentile(rr, 25))
        #plt.plot([(medr - iqr) - i2, (medr + iqr) - i2], [i2,i2], 'g-', lw=3, alpha=0.25)

        xx.append(mr - i2)
        yy.append(i2)
        xerr.append(sr)

        xx2.append(medr - i2)
        xerr2.append(iqr)

    yy2 = np.array(yy)
    xx2 = np.array(xx2)
    xerr2 = np.array(xerr2)
    I = (xerr2 < 1)
    xx2 = xx2[I]
    yy2 = yy2[I]
    xerr2 = xerr2[I]

    flag = catflags[I]
    cstar = classstar[I]

    plt.clf()
    #plt.plot(xx2, yy2, 'o', mfc='b', mec='none', mew=0, ms=5, alpha=0.8)

    LL = []
    for F, c in [((flag > 0), '0.5'), ((flag == 0) * (cstar < 0.5), 'b'),
                 ((flag == 0) * (cstar >= 0.5), 'g')]:
        p1 = plt.plot(xx2[F],
                      yy2[F],
                      'o',
                      mfc=c,
                      mec='none',
                      mew=0,
                      ms=5,
                      alpha=0.8)
        LL.append(p1[0])
        plt.plot([xx2[F] - xerr2[F], xx2[F] + xerr2[F]], [yy2[F], yy2[F]],
                 '-',
                 color=c,
                 lw=2,
                 mew='none',
                 alpha=0.5)

    #plt.axis([-3, 3, 21.5, 15.5])
    plt.legend(LL, ('flagged', 'galaxy', 'star'))
    plt.ylim(21.5, 15.5)
    cl, ch = {
        'u': (-3, 6),
        'g': (-1, 5),
        'r': (-2, 3),
        'i': (-2, 2),
        'z': (-2, 1),
        'w1': (-10, 10),
        'w2': (-10, 10),
        'w3': (-10, 10),
        'w4': (-10, 10),
    }[band]
    plt.xticks(range(cl, ch + 1))
    plt.xlim(cl, ch)
    plt.xlabel('SDSS %s - CFHT i (mag)' % band)
    plt.ylabel('CFHT i (mag)')
    plt.yticks(range(16, 21 + 1))
    plt.title('CS82 test patch: SDSS--CFHT CMD')
    plt.savefig('cmd-%s.png' % band)
    plt.savefig('cmd-%s.pdf' % band)

    I = np.flatnonzero((flag == 0) * (xx2 < -1))
    print('Not-flagged and c < -1:', I)
    return I
예제 #33
0
        mn1, mn2 = [], []
        for i, (s1, s2) in enumerate(zip(m1.T, m2.T)):
            print('src', i)
            print('i2mag', i2mags[i])
            print('mag 1', s1)
            print('mag 2', s2)
            s1 = s1[np.isfinite(s1)]
            s2 = s2[np.isfinite(s2)]
            if len(s1) == 0 or len(s2) == 0:
                continue
            mn1.append(np.median(s1))
            mn2.append(np.median(s2))

        mn1 = np.array(mn1)
        mn2 = np.array(mn2)
        plt.clf()
        #I = np.flatnonzero(np.isfinite(m1) * np.isfinite(m2))
        #if len(I) == 0:
        #	print 'No', b1, 'and', b2, 'mags'
        #	continue
        #plt.plot(m1[I], m1[I]-m2[I], 'k.')
        plt.plot(mn1, mn1 - mn2, 'k.')
        plt.xlabel('band ' + b1)
        plt.ylabel('band %s - %s' % (b1, b2))
        plt.savefig('cmd-%s-%s.png' % (b1, b2))

    from cs82 import *
    RA = 334.32
    DEC = 0.315
    sz = 0.12 * 3600.
    pixscale = 0.187
예제 #34
0
    def generateFigsAndDatafiles(self,
                                 overwrite,
                                 outdir=None,
                                 begin=None,
                                 includeraw=True,
                                 grid=False):
        """
        Our plots will start at time=begin (in nanoseconds). If begin is set to None,
        we will just start at the beginning of the data.
        """
        cols = 4
        if outdir is None:
            outdir = os.path.join(self.directory, 'props')
        datadir = os.path.join(outdir, 'data')
        if not overwrite:
            if os.path.exists(outdir):
                sys.exit(
                    'Output directory (%s) exists. Please remove it or set "overwrite" to True.'
                    % outdir)
        os.system('mkdir -p ' + datadir)
        propnames = []

        # We use a different definition of this funciton below to get relative links in the HTML file.
        def figname(pn):
            return os.path.join(datadir, pn + '.png')

        def dataname(pn):
            return os.path.join(datadir, pn + '.dat')

        def savedata(p, fname):
            d = self._props[p]
            x = d[:, 0]
            # convert ps to ns
            x = x / 1000.
            y = d[:, 1]
            f = file(fname, 'w')
            for xy in zip(x, y):
                f.write('%f\t%f\n' % xy)
            f.close()

        for p in self._props.keys():
            pn = ''.join([
                i for i in p
                if i in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
            ])
            print "Doing", p
            clf()
            if p in self._averprops:
                if p.startswith(
                        'H'
                ) and False:  # Hopefully dealt with by not plotting more than 200k points.
                    print "Trying to skip raw for", p
                    self.plotaver(p, begin=begin, includeraw=False, grid=grid)
                else:
                    self.plotaver(p,
                                  begin=begin,
                                  includeraw=includeraw,
                                  grid=grid)
            else:
                self.plotprop(p, begin=begin, grid=grid)
            ### Note: This call to savefig often fails with the
            ### following runtime error:

            ### RuntimeError: Agg rendering complexity exceeded. Consider downsampling or decimating your data.

            ### when plotting HFCTote with raw data
            ### shown. Unfortunately, catching the error, calling clf,
            ### and replotting without the raw data produces exactly
            ### the same error. The only thing that seems to help is
            ### just setting includeraw to false the first time through.
            ### Hence the check for p.startswith('H') above. FWIW, I think
            ### I tried it with just checking for HFCTote, but that failed.
            ### I might have gotten the syntax wrong, though.

            ### It looks like I've fixed that completely by just
            ### plotting 200k points, but I've left the comments in
            ### for clarity.
            savefig(figname(pn))
            savedata(p, dataname(p))
            propnames.append(pn)

        html = '''<html><head><title>CHARMM Output Properties</title></head><body><h1>CHARMM Output Properties</h1><br><h2>Click on thumbnails for larger pictures</h2><br><table border="0" cellpadding="7" cellspacing="0">'''

        def figname(pn):
            return os.path.join('data', pn + '.png')

        propnames.sort()
        for tr in splitseq(propnames, cols):
            html += '<tr>'
            for td in tr:
                html += '  <td>%s<br><a href="%s"><img src="%s" width="100%%"/></td>\n' % (
                    td, figname(td), figname(td))

            html += '</tr>'
        html += '</table></body></html>'
        f = file(os.path.join(outdir, 'index.html'), 'w')
        f.write(html)
        f.close()
예제 #35
0
 def _pp(self, clear, r, c, props):
     if clear: clf()
     for (i, p) in enumerate(props):
         subplot(r, c, i + 1)
         self.plotprop(p)
예제 #36
0
def plotdata(x,
             y=None,
             yerr=None,
             numblocks=10,
             numerrbars=50,
             colors=('blue', 'green'),
             clear=False,
             plottitle=None,
             xaxislabel=None,
             yaxislabel=None,
             description='',
             grid=False):
    """ Our standard plotting routine.

     - The first argument is the data. We try to extract x and y data from it if possible.
     - Plot 50 error bars if we're given errors.
     - Do block averaging.
     - description gets added to the legend and title.
     """
    txt = ''
    # Figure out x,y,err
    if y is None:
        if len(x.shape) > 1:
            x, y = x[:, 0], x[:, 1]
        else:
            x, y = array(range(len(x))), x
    if clear: clf()

    if len(x) >= 100000:
        step = divmod(len(x), 100000)[0]
    else:
        step = 1
    plot(x[::step],
         y[::step],
         color=colors[0],
         zorder=10,
         alpha=0.5,
         label=description + 'raw data')
    xmin, xmax, ymin, ymax = x.min(), x.max(), y.min(), y.max()
    #print y
    if yerr is not None:
        error_step = int(len(x) / numerrbars)
        if error_step == 0: error_step = len(x)
        errorbar(x[::error_step],
                 y[::error_step],
                 yerr[::error_step],
                 color=colors[0],
                 zorder=20,
                 label='FLUC>')
        ymin = ymin - abs(yerr.max())
        ymax = ymax + abs(yerr.max())
    else:
        ymin = ymin - abs(.1 * ymin)
        ymax = ymax - abs(.1 * ymax)
    if numblocks <= len(x):
        blocksize = int(len(x) / numblocks)
    else:
        blocksize = 1
        numblocks = len(x)
        print "Not enough data points, setting blocksize to", blocksize, "and numblocks to", numblocks
    blocksizes = [len(i) for i in splitseq(x, blocksize)]
    x = [average(i) for i in splitseq(x, blocksize)][:numblocks]
    yerr = array([std(i) for i in splitseq(y, blocksize)])[:numblocks]
    y = array([average(i) for i in splitseq(y, blocksize)])[:numblocks]
    txt += 'Avg: %.4f' % average(y)
    txt += ', std err: %.4f, avg err: %.4f' % (std(y), average(yerr))
    txt += '\nblocks of length %s' % blocksize
    if len(blocksizes) > numblocks:
        txt += ' discarding %s points at the end' % sum(blocksizes[numblocks:])
    txt += '\nslope of best-fit line to block averages:  %s' % u.fitline(x,
                                                                         y)[0]
    #print y
    #errorbar(x,y,yerr,elinewidth=20,color=colors[1],barsabove=True,zorder=30)
    errorbar(x,
             y,
             yerr,
             color=colors[1],
             barsabove=True,
             zorder=30,
             label=description + 'block averages')

    # After the last plotting, we need to set the axis explicitly,
    # due to the fact that someone might have previously plotted
    # raw data in plotaver(). In that case, we still want to be
    # zoomed in on the average data.
    axis([xmin, xmax, ymin, ymax])
    if plottitle: title(description + plottitle)
    if xaxislabel: xlabel(xaxislabel)
    if yaxislabel: ylabel(yaxislabel)
    if grid: P.grid('on')
    else: P.grid('off')
    annotation_location = (min(x) + (max(x) - min(x)) * 0.1,
                           min(y) + (max(y) - min(y)) * 0.1)
    annotation_location = (xmin + (xmax - xmin) * 0.1,
                           ymin + (ymax - ymin) * 0.1)
    #print annotation_location,max(y)
    annotate(txt, annotation_location)
    legend(shadow=True)
예제 #37
0
    def run(self):
        if self.debug:
            import pdb
            pdb.set_trace()
        db = Stock_250kDB.Stock_250kDB(drivername=self.drivername,
                                       username=self.db_user,
                                       password=self.db_passwd,
                                       hostname=self.hostname,
                                       database=self.dbname,
                                       schema=self.schema)
        db.setup(create_tables=False)
        session = db.session

        header_phen, strain_acc_list_phen, category_list_phen, data_matrix_phen = read_data(
            self.phenotype_fname, turn_into_integer=0)
        phenData = SNPData(
            header=header_phen,
            strain_acc_list=strain_acc_list_phen,
            data_matrix=data_matrix_phen
        )  #row label is that of the SNP matrix, because the phenotype matrix is gonna be re-ordered in that way
        phenData.data_matrix = Kruskal_Wallis.get_phenotype_matrix_in_data_matrix_order(
            phenData.row_id_ls, strain_acc_list_phen,
            phenData.data_matrix)  #tricky, using strain_acc_list_phen

        phenotype_col_index1 = self.findOutWhichPhenotypeColumn(
            phenData, Set([self.phenotype_method_id1]))[0]
        phenotype_col_index2 = self.findOutWhichPhenotypeColumn(
            phenData, Set([self.phenotype_method_id2]))[0]

        x_ls = []
        y_ls = []
        for i in range(phenData.data_matrix.shape[0]):
            if not numpy.isnan(
                    phenData.data_matrix[i]
                [phenotype_col_index1]) and not numpy.isnan(
                    phenData.data_matrix[i][phenotype_col_index2]):
                x_ls.append(phenData.data_matrix[i][phenotype_col_index1])
                y_ls.append(phenData.data_matrix[i][phenotype_col_index2])

        pylab.clf()
        pylab.title('Phenotype Contrast')
        pylab.plot(x_ls, y_ls, '.', alpha=0.6)
        pylab.grid(alpha=0.3)
        phenotype_method1 = Stock_250kDB.PhenotypeMethod.get(
            self.phenotype_method_id1)
        phenotype_method2 = Stock_250kDB.PhenotypeMethod.get(
            self.phenotype_method_id2)
        pylab.xlabel(phenotype_method1.short_name)
        pylab.ylabel(phenotype_method2.short_name)

        #draw diagonal line to show perfect correlation
        max_min_value = max(min(x_ls), min(y_ls))
        min_max_value = min(max(x_ls), max(y_ls))
        pylab.plot([max_min_value, min_max_value],
                   [max_min_value, min_max_value],
                   c='g',
                   alpha=0.7)

        png_output_fname = '%s.png' % self.output_fname_prefix
        pylab.savefig(png_output_fname, dpi=400)
        pylab.savefig('%s.svg' % self.output_fname_prefix)
예제 #38
0
def klf_simulation():
    """
    Plot up a simulated KLF for the young star cluster at the Galactic Center
    and show spectroscopic and photometric detections today with Keck and in the
    future with TMT.
    """
    t = atpy.Table('/u/jlu/work/tmt/tmt_sim_klf_t6.78.txt', type='ascii')
    nrowsOld = len(t)

    # Chop off everything below 0.1 Msun since we don't have models there anyhow
    t = t.where(t['Mass'] >= 0.1)
    nrows = len(t)

    print 'Found %d out of %d stars above 0.1 Msun' % (nrows, nrowsOld)

    # Assign arbitrary magnitudes for WR stars. Uniformly distributed from Kp=9-12
    idx = np.where(t['isWR'] == 'True')[0]
    t['Kp'][idx] = 9.0 + (np.random.rand(len(idx)) * 3)
    print t['Kp'][idx]

    kbins = np.arange(9.0, 24, 0.5)

    # Plot the KLF
    py.clf()
    py.hist(t['Kp'], bins=kbins, histtype='step', linewidth=2)
    py.gca().set_yscale('log')
    py.xlabel('Kp (d=8 kpc, AKs=2.7)')
    py.ylabel('Number of Stars')
    py.title('Galactic Center Young Cluster')
    py.xlim(9, 23)
    rng = py.axis()

    py.plot([15, 15], [rng[2], rng[3]], 'k--', linewidth=2)
    py.text(15.1, 4e4, r'13 M$_\odot$', horizontalalignment='left')
    ar1 = FancyArrow(15,
                     10**3,
                     -1,
                     0,
                     width=10,
                     color='black',
                     head_length=0.3)
    py.gca().add_patch(ar1)
    py.text(14.8,
            7e2,
            'Keck\nSpectra',
            horizontalalignment='right',
            verticalalignment='top')

    py.plot([20, 20], [rng[2], rng[3]], 'k-', linewidth=2)
    py.text(20.1, 4e4, r'0.5 M$_\odot$', horizontalalignment='left')
    ar1 = FancyArrow(20, 3e4, -1, 0, width=300, color='black', head_length=0.3)
    py.gca().add_patch(ar1)
    py.text(19.8,
            2.2e4,
            'TMT\nSpectra',
            horizontalalignment='right',
            verticalalignment='top')

    ar1 = FancyArrow(18.2, 10, 0, 13, width=0.1, color='blue', head_length=10)
    py.gca().add_patch(ar1)
    py.text(18,
            9,
            'Pre-MS\nTurn-On',
            color='blue',
            horizontalalignment='center',
            verticalalignment='top')

    py.savefig('/u/jlu/work/tmt/gc_klf_spectral_sensitivity.png')
예제 #39
0
    def runOptimization(self, id_to_name_nodemap, name_to_id_nodemap, max_stations):
        print '\nRunning Optimization with:'
        print '\tMAX BOOSTERS = ', max_stations
        
        solve_timelimit = None
        p = (1,"There was a problem with the 'model type' or 'model format' options")
        cmd = None
        Solution = {}
        Solution['detected scenarios'] = {}
        if self.getBoosterOption('model format') == 'AMPL':
            exe = self.getConfigureOption('ampl executable')
            inp = os.path.join(os.path.abspath(os.curdir),self.getConfigureOption('output prefix')+'-ampl-b'+str(max_stations)+'.run')
            out = os.path.join(os.path.abspath(os.curdir),self.getConfigureOption('output prefix')+'-ampl-b'+str(max_stations)+'.out')
            results_file, concentrations_file, mass_injections_file = self.createAmplRun(name_to_id_nodemap, max_stations, inp)
            cmd = '%s %s'%(exe,inp)
            # Delete the possibly existing results file with the same name before-hand,
            # that way we'll know if AMPL failed
            try:
                os.remove(results_file)
            except OSError:
                # the file did not exist
                pass
            try:
                os.remove(concentrations_file)
            except OSError:
                # the file did not exist
                pass
            try:
                os.remove(mass_injections_file)
            except OSError:
                # the file did not exist
                pass
            print 'Launching AMPL ...'
            p = pyutilib.subprocess.run(cmd,timelimit=solve_timelimit,outfile=out)
            if (p[0] or not os.path.isfile(results_file)):
                print >> sys.stderr, '\nAn error occured when running the optimization problem'
                print >> sys.stderr, 'Error Message: ', p[1]
                print >> sys.stderr, 'Command: ', cmd, '\n'
                raise RuntimeWarning('Optimization Failed')
            #try to load the results file
            f = open(results_file,'r')
            ampl_sol = yaml.load(f)
            f.close()
            if (ampl_sol['solve_result_num'] >= 100) or (ampl_sol['solve_result'] != 'solved') or (ampl_sol['solve_exitcode'] != 0):
                print >> sys.stderr, ' '
                print >> sys.stderr, 'WARNING: AMPL solver statuses indicate possible errors.'
                print >> sys.stderr, '         solve_result_num =', ampl_sol['solve_result_num']
                print >> sys.stderr, '         solve_result     =', ampl_sol['solve_result']
                print >> sys.stderr, '         solve_exitcode   =', ampl_sol['solve_exitcode']
                print >> sys.stderr, ' '
            

            min_conc_actual = None
            max_conc_actual = None
            try:
                import json
                f = open(concentrations_file,'r')
                concentrations = json.load(f)
                f.close()
                qs_idx = concentrations['quality start']
                min_conc_actual = min(min(_c for _c in c[qs_idx:] if _c >= concentrations['min quality']) for c in concentrations['nodes'].values())
                max_conc_actual = max(max(c[qs_idx:]) for c in concentrations['nodes'].values())
                import pylab
                from matplotlib.backends.backend_pdf import PdfPages
                pylab.figure()
                avg = concentrations['min quality']*0.5 + concentrations['max quality']*0.5
                pp = PdfPages(os.path.join(os.path.abspath(os.curdir),self.getConfigureOption('output prefix')+'-concentrations-b'+str(max_stations)+'.pdf'))
                for node in concentrations['nodes']:
                    pylab.clf()
                    pylab.title('Node - '+id_to_name_nodemap[int(node)])
                    pylab.plot(concentrations['nodes'][node])
                    pylab.xlabel('Timestep')
                    pylab.ylabel('Concentration g/m3')
                    pylab.ylim([max(0,concentrations['min quality']-avg*0.1),concentrations['max quality']+avg*0.1])
                    pylab.axhline(concentrations['min quality'],color='k',linestyle='--')
                    pylab.axhline(concentrations['max quality'],color='k',linestyle='--')
                    pylab.axvline(concentrations['quality start'],color='k')
                    pylab.savefig(pp,format='pdf')
                pp.close()

                f = open(mass_injections_file,'r')
                mass_injections = json.load(f)
                f.close()
                pylab.figure()
                pp = PdfPages(os.path.join(os.path.abspath(os.curdir),self.getConfigureOption('output prefix')+'-mass-injections-b'+str(max_stations)+'.pdf'))
                for node in mass_injections['nodes']:
                    if max(mass_injections['nodes'][node]) > 0.0:
                        pylab.clf()
                        pylab.title('Node - '+id_to_name_nodemap[int(node)])
                        pylab.plot(mass_injections['nodes'][node])
                        pylab.xlabel('Timestep')
                        pylab.ylabel('Mass Injected (Scaled by flow) g/m3')
                        pylab.ylim([0,1.1*max(mass_injections['nodes'][node][k] for k in range(mass_injections['quality start'],len(mass_injections['nodes'][node])))])
                        pylab.axvline(concentrations['quality start'],color='k')
                        pylab.savefig(pp,format='pdf')
                pp.close()

            except:
                print >> sys.stderr, ' '
                print >> sys.stderr, 'Failed to generate plot pdf'
                print >> sys.stderr, ' '
                raise

            Solution['objective'] = ampl_sol['objective']
            Solution['injected mass grams'] = ampl_sol['injected mass grams']
            Solution['average setpoint deviation'] = ampl_sol['average setpoint deviation']
            Solution['quality period minutes'] = ampl_sol['quality period minutes']
            Solution['min quality bound'] = ampl_sol['min quality']
            Solution['min quality actual'] = min_conc_actual
            Solution['max quality bound'] = ampl_sol['max quality']
            Solution['max quality actual'] = max_conc_actual
            Solution['booster nodes'] = [id_to_name_nodemap[name] for name in ampl_sol['booster ids']]
            Solution['booster nodes'].sort()
        else:
            raise RuntimeError("Bad model format option")

        # Summarize yaml options
        Solution['yaml options'] = copy.deepcopy(self.opts)
        Solution['yaml options']['booster']['max boosters'] = max_stations

        self.printSolutionSummary(Solution)
        
        out_prefix = ""
        if self.getConfigureOption('output prefix') not in self.none_list:
            out_prefix += self.getConfigureOption('output prefix')+'-'         
        results_fname = os.path.join(os.path.abspath(os.curdir),out_prefix+"booster_quality-results-b"+str(max_stations)+'.json')
        f = open(results_fname,'w')
        json.dump(Solution,f,indent=2)
        f.close()
        
        return results_fname
예제 #40
0
        self.delta = self.resynth - self.img.data
        self.offset, log = measure_offset(self.resynth,
                                          self.img.data,
                                          withLog=1)
        print(os.linesep.join(log))
        print(self.offset)


if __name__ == "__main__":
    cc = CheckCalib(sys.argv[1], sys.argv[2])
    cc.integrate()
    cc.rebuild()
    pylab.ion()

    pylab.imshow(cc.delta, aspect="auto", interpolation=None, origin="bottom")
    #    pylab.show()
    six.moves.input("Delta image")
    pylab.imshow(cc.img.data,
                 aspect="auto",
                 interpolation=None,
                 origin="bottom")
    six.moves.input("raw image")
    pylab.imshow(cc.resynth,
                 aspect="auto",
                 interpolation=None,
                 origin="bottom")
    six.moves.input("rebuild image")
    pylab.clf()
    pylab.plot(cc.r, cc.I)
    six.moves.input("powder pattern")
예제 #41
0
from pylab import plot, xlabel, ylabel, savefig, clf, sin
from numpy import arange
from os import system

t = arange(-6.3, 6.3, 0.1)

for i in range(1, 7):
	print "i = ", i
	clf()
	plot(t, sin(i*t))
	xlabel('$x$')
	ylabel('$\sin(%dx)$' % i)

	savefig("sin%d.pdf" % i)

latex = r"""
\documentclass{article}
\usepackage{graphicx}
\usepackage{subfigure}
\begin{document}
\begin{figure}
"""

for i in range(1, 7):
	latex += r"""\subfigure[$i=%d$]{
	\includegraphics[scale=0.3]{sin%d}"""% (i, i) + "}\n"
	
latex += r"""
\end{figure}
\end{document}"""
예제 #42
0
# import some data to play with

# The IRIS dataset
from scikits.learn import datasets, svm
iris = datasets.load_iris()

# Some noisy data not correlated
E = np.random.normal(size=(len(iris.data), 35))

# Add the noisy data to the informative features
x = np.hstack((iris.data, E))
y = iris.target

################################################################################
pl.figure(1)
pl.clf()

x_indices = np.arange(x.shape[-1])

################################################################################
# Univariate feature selection
from scikits.learn.feature_selection import SelectFpr, f_classif
# As a scoring function, we use a F test for classification
# We use the default selection function: the 10% most significant
# features

selector = SelectFpr(f_classif, alpha=0.1)
selector.fit(x, y)
scores = -np.log10(selector._pvalues)
scores /= scores.max()
pl.bar(x_indices - .45,
예제 #43
0
def image(sim,
          qty='rho',
          width="10 kpc",
          resolution=500,
          units=None,
          log=True,
          vmin=None,
          vmax=None,
          av_z=False,
          filename=None,
          z_camera=None,
          clear=True,
          cmap=None,
          title=None,
          qtytitle=None,
          show_cbar=True,
          subplot=False,
          noplot=False,
          ret_im=False,
          fill_nan=True,
          fill_val=0.0,
          linthresh=None,
          **kwargs):
    """

    Make an SPH image of the given simulation.

    **Keyword arguments:**

    *qty* (rho): The name of the array to interpolate

    *width* (10 kpc): The overall width and height of the plot. If
     ``width`` is a float or an int, then it is assumed to be in units
     of ``sim['pos']``. It can also be passed in as a string
     indicating the units, i.e. '10 kpc', in which case it is
     converted to units of ``sim['pos']``.

    *resolution* (500): The number of pixels wide and tall

    *units* (None): The units of the output

    *av_z* (False): If True, the requested quantity is averaged down
            the line of sight (default False: image is generated in
            the thin plane z=0, unless output units imply an integral
            down the line of sight). If a string, the requested quantity
            is averaged down the line of sight weighted by the av_z
            array (e.g. use 'rho' for density-weighted quantity;
            the default results when av_z=True are volume-weighted).

    *z_camera* (None): If set, a perspective image is rendered. See
                :func:`pynbody.sph.image` for more details.

    *filename* (None): if set, the image will be saved in a file

    *clear* (True): whether to call clf() on the axes first

    *cmap* (None): user-supplied colormap instance

    *title* (None): plot title

    *qtytitle* (None): colorbar quantity title

    *show_cbar* (True): whether to plot the colorbar

    *subplot* (False): the user can supply a AxesSubPlot instance on
    which the image will be shown

    *noplot* (False): do not display the image, just return the image array

    *ret_im* (False): return the image instance returned by imshow

    *num_threads* (None) : if set, specify the number of threads for
    the multi-threaded routines; otherwise the pynbody.config default is used

    *fill_nan* (True): if any of the image values are NaN, replace with fill_val

    *fill_val* (0.0): the fill value to use when replacing NaNs

    *linthresh* (None): if the image has negative and positive values
     and a log scaling is requested, the part between `-linthresh` and
     `linthresh` is shown on a linear scale to avoid divergence at 0
    """

    if not noplot:
        import matplotlib.pylab as plt

    global config
    if not noplot:
        if subplot:
            p = subplot
        else:
            p = plt

    if qtytitle is None:
        qtytitle = qty

    if isinstance(units, str):
        units = _units.Unit(units)

    if isinstance(width, str) or issubclass(width.__class__, _units.UnitBase):
        if isinstance(width, str):
            width = _units.Unit(width)
        width = width.in_units(sim['pos'].units, **sim.conversion_context())

    width = float(width)

    kernel = sph.Kernel()

    perspective = z_camera is not None
    if perspective and not av_z:
        kernel = sph.Kernel2D()

    is_projected = False
    if units is not None:
        is_projected = _units_imply_projection(sim, qty, units)

    if is_projected:
        kernel = sph.Kernel2D()

    if av_z:
        if isinstance(kernel, sph.Kernel2D):
            raise _units.UnitsException(
                "Units already imply projected image; can't also average over line-of-sight!"
            )
        else:
            kernel = sph.Kernel2D()
            if units is not None:
                aunits = units * sim['z'].units
            else:
                aunits = None

            if isinstance(av_z, str):
                if units is not None:
                    aunits = units * sim[av_z].units * sim['z'].units
                sim["__prod"] = sim[av_z] * sim[qty]
                qty = "__prod"

            else:
                av_z = "__one"
                sim["__one"] = np.ones_like(sim[qty])
                sim["__one"].units = "1"

            im = sph.render_image(sim,
                                  qty,
                                  width / 2,
                                  resolution,
                                  out_units=aunits,
                                  kernel=kernel,
                                  z_camera=z_camera,
                                  **kwargs)
            im2 = sph.render_image(sim,
                                   av_z,
                                   width / 2,
                                   resolution,
                                   kernel=kernel,
                                   z_camera=z_camera,
                                   **kwargs)

            top = sim.ancestor

            try:
                del top["__one"]
            except KeyError:
                pass

            try:
                del top["__prod"]
            except KeyError:
                pass

            im = im / im2

    else:
        im = sph.render_image(sim,
                              qty,
                              width / 2,
                              resolution,
                              out_units=units,
                              kernel=kernel,
                              z_camera=z_camera,
                              **kwargs)

    if fill_nan:
        im[np.isnan(im)] = fill_val

    if not noplot:

        # set the log or linear normalizations
        if log:
            try:
                im[np.where(im == 0)] = abs(im[np.where(abs(im != 0))]).min()
            except ValueError:
                raise ValueError(
                    "Failed to make a sensible logarithmic image. This probably means there are no particles in the view."
                )

            # check if there are negative values -- if so, use the symmetric
            # log normalization
            if (vmin is None and
                (im < 0).any()) or ((vmin is not None) and vmin < 0):

                # need to set the linear regime around zero -- set to by
                # default start at 1/1000 of the log range
                if linthresh is None:
                    linthresh = np.nanmax(abs(im)) / 1000.
                norm = matplotlib.colors.SymLogNorm(linthresh,
                                                    vmin=vmin,
                                                    vmax=vmax)
            else:
                norm = matplotlib.colors.LogNorm(vmin=vmin, vmax=vmax)

        else:
            norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)

        #
        # do the actual plotting
        #
        if clear and not subplot:
            p.clf()

        if ret_im:
            return p.imshow(im[::-1, :].view(np.ndarray),
                            extent=(-width / 2, width / 2, -width / 2,
                                    width / 2),
                            vmin=vmin,
                            vmax=vmax,
                            cmap=cmap,
                            norm=norm)

        ims = p.imshow(im[::-1, :].view(np.ndarray),
                       extent=(-width / 2, width / 2, -width / 2, width / 2),
                       vmin=vmin,
                       vmax=vmax,
                       cmap=cmap,
                       norm=norm)

        u_st = sim['pos'].units.latex()
        if not subplot:
            plt.xlabel("$x/%s$" % u_st)
            plt.ylabel("$y/%s$" % u_st)
        else:
            p.set_xlabel("$x/%s$" % u_st)
            p.set_ylabel("$y/%s$" % u_st)

        if units is None:
            units = im.units

        if units.latex() == "":
            units = ""
        else:
            units = "$" + units.latex() + "$"

        if show_cbar:
            plt.colorbar(ims).set_label(qtytitle + "/" + units)

        # colorbar doesn't work wtih subplot:  mappable is NoneType
        # elif show_cbar:
        #    import matplotlib.pyplot as mpl
        #    if qtytitle: mpl.colorbar().set_label(qtytitle)
        #    else:        mpl.colorbar().set_label(units)

        if title is not None:
            if not subplot:
                p.title(title)
            else:
                p.set_title(title)

        if filename is not None:
            p.savefig(filename)

        plt.draw()
        # plt.show() - removed by AP on 30/01/2013 - this should not be here as
        # for some systems you don't get back to the command prompt

    return im
예제 #44
0
def mctweak(wcs, xy, rd):
    obj = McTweak(wcs, xy, rd)

    # Initial args
    args, sigs = get_sip_args(wcs)

    print('Args:', args)
    print('Sigs:', sigs)
    print('Number of arguments:', len(args))
    print('Logodds:', obj(args))

    ndim, nwalkers = len(args), 100
    p0 = emcee.utils.sample_ball(args, sigs, size=nwalkers)
    print('p0', p0.shape)

    ps = PlotSequence('mctweak')

    W, H = wcs.get_width(), wcs.get_height()
    mywcs = Sip(wcs)

    sampler = emcee.EnsembleSampler(nwalkers, ndim, obj)
    lnp0, rstate = None, None
    pp = []
    for step in range(10000):
        print('Step', step)
        p0, lnp0, rstate = sampler.run_mcmc(p0,
                                            1,
                                            lnprob0=lnp0,
                                            rstate0=rstate)
        print('Best logprob:', np.max(lnp0))
        i = np.argmax(lnp0)
        print('Best args:', p0[i, :])

        pp.extend(sampler.flatchain)
        sampler.reset()

        if step % 100 != 0:
            continue

        plt.clf()
        plt.plot(obj.testxy[:, 0], obj.testxy[:, 1], 'r.')
        for args in p0[np.random.permutation(nwalkers)[:10], :]:
            set_sip_args(mywcs, args)
            sip_compute_inverse_polynomials(mywcs, 20, 20, 1, W, 1, H)
            ok, x, y = mywcs.radec2pixelxy(obj.refra, obj.refdec)
            plt.plot(x, y, 'bo', mec='b', mfc='none', alpha=0.25)

            ex = 10.
            ngridx = ngridy = 10
            stepx = stepy = 100
            xgrid = np.linspace(0, W, ngridx)
            ygrid = np.linspace(0, H, ngridy)
            X = np.linspace(0, W, int(np.ceil(W / stepx)))
            Y = np.linspace(0, H, int(np.ceil(H / stepy)))
            for x in xgrid:
                DX, DY = [], []
                xx, yy = [], []
                for y in Y:
                    dx, dy = mywcs.get_distortion(x, y)
                    xx.append(x)
                    yy.append(y)
                    DX.append(dx)
                    DY.append(dy)
                DX = np.array(DX)
                DY = np.array(DY)
                xx = np.array(xx)
                yy = np.array(yy)
                EX = DX + ex * (DX - xx)
                EY = DY + ex * (DY - yy)
                #plot(xx, yy, 'k-', alpha=0.5)
                plt.plot(EX, EY, 'b-', alpha=0.1)

            for y in ygrid:
                DX, DY = [], []
                xx, yy = [], []
                for x in X:
                    dx, dy = mywcs.get_distortion(x, y)
                    DX.append(dx)
                    DY.append(dy)
                    xx.append(x)
                    yy.append(y)
                DX = np.array(DX)
                DY = np.array(DY)
                xx = np.array(xx)
                yy = np.array(yy)
                EX = DX + ex * (DX - xx)
                EY = DY + ex * (DY - yy)
                #plot(xx, yy, 'k-', alpha=0.5)
                plt.plot(EX, EY, 'b-', alpha=0.1)

        for x in xgrid:
            plt.plot(x + np.zeros_like(Y), Y, 'k-', alpha=0.5)
        for y in ygrid:
            plt.plot(X, y + np.zeros_like(X), 'k-', alpha=0.5)

        plt.axis([1, W, 1, H])
        plt.axis('scaled')
        ps.savefig()

        pp = np.vstack(pp)
        print('pp', pp.shape)

        # plt.clf()
        # triangle.corner(pp, plot_contours=False)
        # ps.savefig()

        pp = []
예제 #45
0
def do_all_angles(rootname='sv',
                  smooth=21,
                  wmin=850,
                  wmax=1850,
                  fmax=0,
                  fig_no=1,
                  title=None):
    '''
    Plot each of the spectra where

    smooth is the number of bins to boxcar smooth
    fmax sets the ylim of the plot, assuming it is not zero

    and other values should be obvious.

    140907    ksl    Updated to include fmax
    140912    ksl    Updated so uses rootname 
    141124    ksl    Updated for new formats which use astropy
    '''

    filename = rootname + '.spec'

    try:
        data = ascii.read(filename)
    except IOError:
        print('Error: Could not find %s' % filename)
        return 'None'

    # print(data.colnames)

    # Now determine what the coluns containing real spectra are
    # while makeing the manes simpler
    cols = []
    i = 9
    while i < len(data.colnames):
        one = data.colnames[i]
        # print 'gotcha',one
        new_name = one.replace('P0.50', '')
        new_name = new_name.replace('A', '')
        data.rename_column(one, new_name)
        cols.append(new_name)
        i = i + 1

    root = rootname

    # if wmin and wmax are not specified, use wmin and wmax from input file

    if wmin == 0 or wmax == 0:
        xwave = numpy.array(data['Lambda'])
        xmin = numpy.min(xwave)
        xmax = numpy.max(xwave)
        if wmin == 0:
            wmin = xmin
        if wmax == 0:
            wmax = xmax

    pylab.figure(fig_no, (9, 6))
    pylab.clf()

    # print(cols)

    for col in cols:
        flux = data[col]
        # print(flux)
        xlabel = col + '$^{\circ}$'
        q = convolve(flux, boxcar(smooth) / float(smooth), mode='same')
        pylab.plot(data['Lambda'], q, '-', label=xlabel)
    pylab.xlabel(r'Wavelength ($\AA$)', size=16)
    pylab.ylabel('Flux', size=16)
    z = pylab.axis()
    if fmax == 0:
        pylab.axis((wmin, wmax, 0, z[3]))
    else:
        pylab.axis((wmin, wmax, 0, fmax))

    if title == None:
        title = root
    pylab.title(title, size=16)
    pylab.legend(loc='best')
    pylab.draw()
    plotfile = root + '.png'
    pylab.savefig(plotfile)
    return plotfile
예제 #46
0
def __print_stars(clist, smax, sig, out=True, save=None, data=None, imgw=None):
    #TODO: set the scale correctly for each graph
    import pylab
    global rejected, texts
    nx = int(np.sqrt(smax))
    ny = nx + 2
    i = 1
    im1 = []
    im2 = []
    im3 = []
    im4 = []
    im5 = []
    rejected = []
    texts = []
    slist = []
    pylab.figure(1)
    for c in clist:
        if c.grade > 2:
            print c
            slist += [c]
            pylab.subplot(nx, ny, i)
            pylab.title('candidate ' + str(i))
            extent = (0, c.img.shape[1], 0, c.img.shape[0])
            im1 += [
                pylab.imshow(c.img, extent=extent, interpolation="nearest")
            ]
            im2 += [
                pylab.imshow(c.chi,
                             extent=extent,
                             hold=True,
                             interpolation="nearest")
            ]
            pylab.colorbar(im2[-1])
            im2[-1].set_visible(False)
            if sig is not None:
                chisig1 = c.chi.copy()
                chisig2 = c.chi.copy()
                chisig3 = c.chi.copy()
                chisig1[np.where(abs(chisig1) > sig)] = 0.
                chisig2[np.where(abs(chisig2) > 2. * sig)] = 0.
                chisig3[np.where(abs(chisig3) > 3. * sig)] = 0.
                im3 += [
                    pylab.imshow(chisig1,
                                 extent=extent,
                                 hold=True,
                                 interpolation="nearest")
                ]
                im4 += [
                    pylab.imshow(chisig2,
                                 extent=extent,
                                 hold=True,
                                 interpolation="nearest")
                ]
                im5 += [
                    pylab.imshow(chisig3,
                                 extent=extent,
                                 hold=True,
                                 interpolation="nearest")
                ]
                im3[-1].set_visible(False)
                im4[-1].set_visible(False)
                im5[-1].set_visible(False)
            i += 1

    def toggle_images(event):
        key = event.key
        keys = '0'
        if sig is not None:
            keys = '1230'
        b1 = im1[0].get_visible()
        b2 = im2[0].get_visible() or b1 == False
        if key not in keys:
            return
        for im in im1:
            im.set_visible(not b1 and key == '0')
        for im in im2:
            im.set_visible(not b2 and key == '0')
        if sig is not None:
            for im in im3:
                im.set_visible(key == '1')
            for im in im4:
                im.set_visible(key == '2')
            for im in im5:
                im.set_visible(key == '3')
        pylab.draw()

    def click(event):
        canvas = event.canvas
        if event.inaxes and event.xdata > 1. and event.ydata > 1.:
            global rejected, texts
            for i, ax in enumerate(rejected):
                if event.inaxes is ax:
                    event.inaxes.texts.remove(texts[i])
                    del rejected[i]
                    del texts[i]
                    break

            else:
                texts += [
                    event.inaxes.annotate('REMOVED', xy=(1, 1), color='red')
                ]
                rejected += [event.inaxes]
        pylab.draw()

    pylab.connect('key_press_event', toggle_images)
    pylab.connect('button_press_event', click)
    if save != None:
        import src.lib.AImage, ImageDraw, Image
        #from PIL import Image
        split = save.split('.')
        ext = '.' + split[-1]
        del split[-1]
        base = ''
        for s in split:
            base += s + '.'
        if base[-1] == '.': base = base[:-1]
        split = save.split('/')[0:-1]
        dir = ''
        for s in split:
            dir += s + '/'
        pylab.savefig(save)
        for im in im2:
            im.set_visible(True)
        for im in im1:
            im.set_visible(False)
        pylab.savefig(base + '_sig' + ext)
        for im in im3:
            im.set_visible(True)
        for im in im2:
            im.set_visible(False)
        pylab.savefig(base + '_1sig' + ext)
        for im in im4:
            im.set_visible(True)
        for im in im3:
            im.set_visible(False)
        pylab.savefig(base + '_2sig' + ext)
        for im in im5:
            im.set_visible(True)
        for im in im4:
            im.set_visible(False)
        pylab.savefig(base + '_3sig' + ext)
        img = fn.makepilimage(src.lib.AImage.Image(data))
        img = img.convert("RGB")
        draw = ImageDraw.Draw(img)
        for c in clist:
            if c.grade > 2:
                draw.text((c.x, c.y - c.rad[0] - 10),
                          str(c.id),
                          fill="#00ff00")
                draw.ellipse([(c.x - c.rad[0], c.y - c.rad[1]),
                              (c.x + c.rad[1], c.y + c.rad[0])],
                             outline="#00ff00")
            else:
                draw.ellipse([(c.x - c.rad[0], c.y - c.rad[1]),
                              (c.x + c.rad[1], c.y + c.rad[0])],
                             outline=(255, 0, 0))  #"#ff0000")
                draw.text((c.x, c.y - c.rad[0] - 10),
                          str(c.id),
                          fill="#ff0000")
            r = max(c.rad) + 10
            thumb = img.crop((c.x - r, c.y - r, c.x + r, c.y + r))
            thumb = thumb.resize((128, 128), Image.ANTIALIAS)
            thumb.save(dir + 'thumb/candidate_' + str(c.id) + '.png', "PNG")
        del draw
        if imgw is not None:
            low = img.resize((imgw, img.size[1] * imgw / img.size[0]),
                             Image.ANTIALIAS)
            low.save(base + '_img_low.png', "PNG")
        img.save(base + '_img.png', "PNG")
    if out == True:
        pylab.show()
    final_slist = []
    for i, im in enumerate(im1):
        for ax in rejected:
            if ax is im.get_axes():
                break
        else:
            final_slist += [slist[i]]
    pylab.clf()
    return final_slist
예제 #47
0
def plot_stress_fields(atoms, r_range=None, initial_params=None, fix_params=None,
                       sigma=None, avg_sigma=None, avg_decay=0.005, calc=None):
    """
    Fit and plot atomistic and continuum stress fields

    Firstly a fit to the Irwin `K`-field solution is carried out using
    :func:`fit_crack_stress_field`, and parameters have the same
    meaning as for that function. Then plots of the
    :math:`\sigma_{xx}`, :math:`\sigma_{yy}`, :math:`\sigma_{xy}`
    fields are produced for atomistic and continuum cases, and for the
    residual error after fitting.
    """

    from pylab import griddata, meshgrid, subplot, cla, contourf, colorbar, draw, title, clf, gca

    params, err = fit_crack_stress_field(atoms, r_range, initial_params, fix_params, sigma,
                                         avg_sigma, avg_decay, calc)

    K, x0, y0, sxx0, syy0, sxy0 = (params['K'], params['x0'], params['y0'],
                                   params['sxx0'], params['syy0'], params['sxy0'])

    x = atoms.positions[:, 0]
    y = atoms.positions[:, 1]

    X = np.linspace((x-x0).min(), (x-x0).max(), 500)
    Y = np.linspace((y-y0).min(), (y-y0).max(), 500)

    t = np.arctan2(y-y0, x-x0)
    r = np.sqrt((x-x0)**2 + (y-y0)**2)

    if r_range is not None:
       rmin, rmax = r_range
       mask = (r > rmin) & (r < rmax)
    else:
       mask = Ellipsis

    atom_sigma = sigma
    if atom_sigma is None:
       atom_sigma = atoms.get_stresses()

    grid_sigma = np.dstack([griddata(x[mask]-x0, y[mask]-y0, atom_sigma[mask,0,0], X, Y),
                            griddata(x[mask]-x0, y[mask]-y0, atom_sigma[mask,1,1], X, Y),
                            griddata(x[mask]-x0, y[mask]-y0, atom_sigma[mask,0,1], X, Y)])

    X, Y = meshgrid(X, Y)
    R = np.sqrt(X**2+Y**2)
    T = np.arctan2(Y, X)

    grid_sigma[((R < rmin) | (R > rmax)),:] = np.nan # mask outside fitting region

    isotropic_sigma = isotropic_modeI_crack_tip_stress_field(K, R, T, x0, y0)
    isotropic_sigma[...,0,0] += sxx0
    isotropic_sigma[...,1,1] += syy0
    isotropic_sigma[...,0,1] += sxy0
    isotropic_sigma[...,1,0] += sxy0
    isotropic_sigma = ma.masked_array(isotropic_sigma, mask=grid_sigma.mask)

    isotropic_sigma[((R < rmin) | (R > rmax)),:,:] = np.nan # mask outside fitting region

    contours = [np.linspace(0, 20, 10),
                np.linspace(0, 20, 10),
                np.linspace(-10,10, 10)]

    dcontours = [np.linspace(0, 5, 10),
                np.linspace(0, 5, 10),
                np.linspace(-5, 5, 10)]

    clf()
    for i, (ii, jj), label in zip(range(3),
                                  [(0,0), (1,1), (0,1)],
                                  ['\sigma_{xx}', r'\sigma_{yy}', r'\sigma_{xy}']):
        subplot(3,3,i+1)
        gca().set_aspect('equal')
        contourf(X, Y, grid_sigma[...,i]*GPA, contours[i])
        colorbar()
        title(r'$%s^\mathrm{atom}$' % label)
        draw()

        subplot(3,3,i+4)
        gca().set_aspect('equal')
        contourf(X, Y, isotropic_sigma[...,ii,jj]*GPA, contours[i])
        colorbar()
        title(r'$%s^\mathrm{Isotropic}$' % label)
        draw()

        subplot(3,3,i+7)
        gca().set_aspect('equal')
        contourf(X, Y, abs(grid_sigma[...,i] -
                           isotropic_sigma[...,ii,jj])*GPA, dcontours[i])
        colorbar()
        title(r'$|%s^\mathrm{atom} - %s^\mathrm{isotropic}|$' % (label, label))
        draw()
예제 #48
0
def wise_cutouts(ra,
                 dec,
                 radius,
                 ps,
                 pixscale=2.75,
                 tractor_base='.',
                 unwise_dir='unwise-coadds'):
    '''
    radius in arcsec.
    pixscale: WISE pixel scale in arcsec/pixel;
        make this smaller than 2.75 to oversample.
    '''

    npix = int(np.ceil(radius / pixscale))
    print('Image size:', npix)
    W = H = npix
    pix = pixscale / 3600.
    wcs = Tan(ra, dec, (W + 1) / 2., (H + 1) / 2., -pix, 0., 0., pix, float(W),
              float(H))
    # Find DECaLS bricks overlapping
    decals = Decals()
    B = bricks_touching_wcs(wcs, decals=decals)
    print('Found', len(B), 'bricks overlapping')

    TT = []
    for b in B.brickname:
        fn = os.path.join(tractor_base, 'tractor', b[:3],
                          'tractor-%s.fits' % b)
        T = fits_table(fn)
        print('Read', len(T), 'from', b)
        primhdr = fitsio.read_header(fn)
        TT.append(T)
    T = merge_tables(TT)
    print('Total of', len(T), 'sources')
    T.cut(T.brick_primary)
    print(len(T), 'primary')
    margin = 20
    ok, xx, yy = wcs.radec2pixelxy(T.ra, T.dec)
    I = np.flatnonzero((xx > -margin) * (yy > -margin) * (xx < W + margin) *
                       (yy < H + margin))
    T.cut(I)
    print(len(T), 'within ROI')

    # Pull out DECaLS coadds (image, model, resid).
    dwcs = wcs.scale(2. * pixscale / 0.262)
    dh, dw = dwcs.shape
    print('DECaLS resampled shape:', dh, dw)
    tags = ['image', 'model', 'resid']
    coimgs = [np.zeros((dh, dw, 3), np.uint8) for t in tags]

    for b in B.brickname:
        fn = os.path.join(tractor_base, 'coadd', b[:3], b,
                          'decals-%s-image-r.fits' % b)
        bwcs = Tan(fn)
        try:
            Yo, Xo, Yi, Xi, nil = resample_with_wcs(dwcs, bwcs)
        except ResampleError:
            continue
        if len(Yo) == 0:
            continue
        print('Resampling', len(Yo), 'pixels from', b)
        xl, xh, yl, yh = Xi.min(), Xi.max(), Yi.min(), Yi.max()
        print(
            'python legacypipe/runbrick.py -b %s --zoom %i %i %i %i --outdir cluster --pixpsf --splinesky --pipe --no-early-coadds'
            % (b, xl - 5, xh + 5, yl - 5, yh + 5) +
            ' -P \'pickles/cluster-%(brick)s-%%(stage)s.pickle\'')
        for i, tag in enumerate(tags):
            fn = os.path.join(tractor_base, 'coadd', b[:3], b,
                              'decals-%s-%s.jpg' % (b, tag))
            img = plt.imread(fn)
            img = np.flipud(img)
            coimgs[i][Yo, Xo, :] = img[Yi, Xi, :]

    tt = dict(image='Image', model='Model', resid='Resid')
    for img, tag in zip(coimgs, tags):
        plt.clf()
        dimshow(img, ticks=False)
        plt.title('DECaLS grz %s' % tt[tag])
        ps.savefig()

    # Find unWISE tiles overlapping
    tiles = unwise_tiles_touching_wcs(wcs)
    print('Cut to', len(tiles), 'unWISE tiles')

    # Here we assume the targetwcs is axis-aligned and that the
    # edge midpoints yield the RA,Dec limits (true for TAN).
    r, d = wcs.pixelxy2radec(np.array([1, W, W / 2, W / 2]),
                             np.array([H / 2, H / 2, 1, H]))
    # the way the roiradec box is used, the min/max order doesn't matter
    roiradec = [r[0], r[1], d[2], d[3]]

    ra, dec = T.ra, T.dec

    T.shapeexp = np.vstack((T.shapeexp_r, T.shapeexp_e1, T.shapeexp_e2)).T
    T.shapedev = np.vstack((T.shapedev_r, T.shapedev_e1, T.shapedev_e2)).T
    srcs = read_fits_catalog(T, ellipseClass=EllipseE)

    wbands = [1, 2]
    wanyband = 'w'

    for band in wbands:
        T.wise_flux[:, band - 1] *= 10.**(primhdr['WISEAB%i' % band] / 2.5)

    coimgs = [np.zeros((H, W), np.float32) for b in wbands]
    comods = [np.zeros((H, W), np.float32) for b in wbands]
    con = [np.zeros((H, W), np.uint8) for b in wbands]

    for iband, band in enumerate(wbands):
        print('Photometering WISE band', band)
        wband = 'w%i' % band

        for i, src in enumerate(srcs):
            #print('Source', src, 'brightness', src.getBrightness(), 'params', src.getBrightness().getParams())
            #src.getBrightness().setParams([T.wise_flux[i, band-1]])
            src.setBrightness(
                NanoMaggies(**{wanyband: T.wise_flux[i, band - 1]}))
            # print('Set source brightness:', src.getBrightness())

        # The tiles have some overlap, so for each source, keep the
        # fit in the tile whose center is closest to the source.
        for tile in tiles:
            print('Reading tile', tile.coadd_id)

            tim = get_unwise_tractor_image(unwise_dir,
                                           tile.coadd_id,
                                           band,
                                           bandname=wanyband,
                                           roiradecbox=roiradec)
            if tim is None:
                print('Actually, no overlap with tile', tile.coadd_id)
                continue
            print('Read image with shape', tim.shape)

            # Select sources in play.
            wisewcs = tim.wcs.wcs
            H, W = tim.shape
            ok, x, y = wisewcs.radec2pixelxy(ra, dec)
            x = (x - 1.).astype(np.float32)
            y = (y - 1.).astype(np.float32)
            margin = 10.
            I = np.flatnonzero((x >= -margin) * (x < W + margin) *
                               (y >= -margin) * (y < H + margin))
            print(len(I), 'within the image + margin')

            subcat = [srcs[i] for i in I]
            tractor = Tractor([tim], subcat)
            mod = tractor.getModelImage(0)

            # plt.clf()
            # dimshow(tim.getImage(), ticks=False)
            # plt.title('WISE %s %s' % (tile.coadd_id, wband))
            # ps.savefig()

            # plt.clf()
            # dimshow(mod, ticks=False)
            # plt.title('WISE %s %s' % (tile.coadd_id, wband))
            # ps.savefig()

            try:
                Yo, Xo, Yi, Xi, nil = resample_with_wcs(wcs, tim.wcs.wcs)
            except ResampleError:
                continue
            if len(Yo) == 0:
                continue
            print('Resampling', len(Yo), 'pixels from WISE', tile.coadd_id,
                  band)

            coimgs[iband][Yo, Xo] += tim.getImage()[Yi, Xi]
            comods[iband][Yo, Xo] += mod[Yi, Xi]
            con[iband][Yo, Xo] += 1

    for img, mod, n in zip(coimgs, comods, con):
        img /= np.maximum(n, 1)
        mod /= np.maximum(n, 1)

    for band, img, mod in zip(wbands, coimgs, comods):
        lo, hi = np.percentile(img, [25, 99])
        plt.clf()
        dimshow(img, vmin=lo, vmax=hi, ticks=False)
        plt.title('WISE W%i Data' % band)
        ps.savefig()

        plt.clf()
        dimshow(mod, vmin=lo, vmax=hi, ticks=False)
        plt.title('WISE W%i Model' % band)
        ps.savefig()

        resid = img - mod
        mx = np.abs(resid).max()
        plt.clf()
        dimshow(resid, vmin=-mx, vmax=mx, ticks=False)
        plt.title('WISE W%i Resid' % band)
        ps.savefig()

    #kwa = dict(mn=-0.1, mx=2., arcsinh = 1.)
    kwa = dict(mn=-0.1, mx=2., arcsinh=None)
    rgb = _unwise_to_rgb(coimgs, **kwa)
    plt.clf()
    dimshow(rgb, ticks=False)
    plt.title('WISE W1/W2 Data')
    ps.savefig()

    rgb = _unwise_to_rgb(comods, **kwa)
    plt.clf()
    dimshow(rgb, ticks=False)
    plt.title('WISE W1/W2 Model')
    ps.savefig()

    kwa = dict(mn=-1, mx=1, arcsinh=None)
    rgb = _unwise_to_rgb([img - mod for img, mod in zip(coimgs, comods)],
                         **kwa)
    plt.clf()
    dimshow(rgb, ticks=False)
    plt.title('WISE W1/W2 Resid')
    ps.savefig()
    def run(self):
        """
		2007-03-29
		2007-04-03
		2007-05-01
			--db_connect()
			--FilterStrainSNPMatrix_instance.read_data()
			if self.comparison_only:
				--FilterStrainSNPMatrix_instance.read_data()
			else:
				--get_SNPpos2index()
				--create_SNP_matrix_2010()
					--get_align_length_from_fname()
						--get_positions_to_be_checked_ls()
					--get_align_matrix_from_fname()
						--get_positions_to_be_checked_ls()
				--get_mapping_info_regarding_strain_acc()
				--shuffle_data_matrix_according_to_strain_acc_ls()
				--FilterStrainSNPMatrix_instance.write_data_matrix()
			
			--extract_sub_data_matrix()
			if self.sub_justin_output_fname:
				--FilterStrainSNPMatrix_instance.write_data_matrix()
			--compare_two_SNP_matrix()
			--outputDiffType()
			
		"""
        from FilterStrainSNPMatrix import FilterStrainSNPMatrix
        FilterStrainSNPMatrix_instance = FilterStrainSNPMatrix()
        header, src_strain_acc_list, category_list, data_matrix = FilterStrainSNPMatrix_instance.read_data(
            self.input_fname)
        if self.comparison_only:
            header, strain_acc_ls, abbr_name_ls_sorted, SNP_matrix_2010_sorted = FilterStrainSNPMatrix_instance.read_data(
                self.output_fname)
            SNP_matrix_2010_sorted = Numeric.array(SNP_matrix_2010_sorted)
        else:
            (conn, curs) = db_connect(self.hostname, self.dbname, self.schema)
            #extract data from alignment
            snp_acc_ls = header[2:]
            SNPpos2index = self.get_SNPpos2index(curs, snp_acc_ls,
                                                 self.snp_locus_table)
            abbr_name_ls, SNP_matrix_2010 = self.create_SNP_matrix_2010(
                SNPpos2index, self.data_dir_2010)
            strain_acc_ls, strain_acc2abbr_name, strain_acc2index = self.get_mapping_info_regarding_strain_acc(
                curs, self.strain_info_table, self.strain_info_2010_table,
                abbr_name_ls)
            SNP_matrix_2010_sorted = self.shuffle_data_matrix_according_to_strain_acc_ls(
                SNP_matrix_2010, strain_acc_ls, strain_acc2index)
            abbr_name_ls_sorted = []
            for strain_acc in strain_acc_ls:
                abbr_name_ls_sorted.append(strain_acc2abbr_name[strain_acc])
            FilterStrainSNPMatrix_instance.write_data_matrix(
                SNP_matrix_2010_sorted, self.output_fname, header,
                strain_acc_ls, abbr_name_ls_sorted)

        #comparison
        data_matrix = Numeric.array(data_matrix)
        sub_data_matrix = self.extract_sub_data_matrix(src_strain_acc_list,
                                                       data_matrix,
                                                       strain_acc_ls)
        if self.sub_justin_output_fname:
            FilterStrainSNPMatrix_instance.write_data_matrix(
                sub_data_matrix, self.sub_justin_output_fname, header,
                strain_acc_ls, abbr_name_ls_sorted)
        diff_matrix, diff_tag_dict, diff_tag2counter = self.compare_two_SNP_matrix(
            SNP_matrix_2010_sorted, sub_data_matrix)
        if self.diff_output_fname:
            self.outputDiffType(diff_matrix, SNP_matrix_2010_sorted,
                                sub_data_matrix, diff_tag_dict,
                                self.diff_type_to_be_outputted,
                                abbr_name_ls_sorted, header[2:],
                                self.diff_output_fname)

        summary_result_ls = []
        for tag, counter in diff_tag2counter.iteritems():
            summary_result_ls.append('%s(%s):%s' %
                                     (tag, diff_tag_dict[tag], counter))
            print '\t%s(%s)\t%s' % (tag, diff_tag_dict[tag], counter)
        import pylab
        pylab.clf()
        diff_matrix_reverse = list(diff_matrix)
        diff_matrix_reverse.reverse()
        diff_matrix_reverse = Numeric.array(diff_matrix_reverse)
        pylab.imshow(diff_matrix_reverse, interpolation='nearest')
        pylab.title(' '.join(summary_result_ls))
        pylab.colorbar()
        pylab.show()

        #2007-11-01 do something as CmpAccession2Ecotype.py
        from CmpAccession2Ecotype import CmpAccession2Ecotype
        CmpAccession2Ecotype_ins = CmpAccession2Ecotype()
        nt_number2diff_matrix_index = CmpAccession2Ecotype_ins.get_nt_number2diff_matrix_index(
            nt2number)
        dc_placeholder = dict(
            zip(range(sub_data_matrix.shape[0]),
                range(sub_data_matrix.shape[1])))
        diff_matrix_ls = CmpAccession2Ecotype_ins.cmp_two_matricies(
            SNP_matrix_2010_sorted, sub_data_matrix,
            nt_number2diff_matrix_index, dc_placeholder, dc_placeholder,
            dc_placeholder)
        print diff_matrix_ls
예제 #50
0
def do_all_angles_ev(rootname='sv',
                     smooth=21,
                     emin=1000,
                     emax=9000,
                     fmax=0,
                     fig_no=1):
    '''
    Plot each of the spectra where

    smooth is the number of bins to boxcar smooth
    fmax sets the ylim of the plot, assuming it is not zero

    and other values should be obvious.

    160405 ksl This version of the routine is intended to plot the X-ray 
        regime
    '''

    if rootname.count('.') > 0:
        filename = rootname
        rootname = rootname[0:rootname.rindex('.')]
        # print('test',rootname)
    else:
        filename = rootname + '.spec'

    try:
        data = ascii.read(filename)
    except IOError:
        print('Error: Could not find %s' % filename)
        return 'None'

    print(data.colnames)

    HEV = 4.136e-15

    # Now determine what the coluns containing real spectra are
    # while makeing the manes simpler
    cols = []
    i = 9
    while i < len(data.colnames):
        one = data.colnames[i]
        # print 'gotcha',one
        new_name = one.replace('P0.50', '')
        new_name = new_name.replace('A', '')
        data.rename_column(one, new_name)
        cols.append(new_name)
        i = i + 1

    root = rootname

    pylab.figure(fig_no, (9, 6))
    pylab.clf()

    for col in cols:
        flux = data[col]
        # print(flux)
        xlabel = col + '$^{\circ}$'
        q = convolve(flux, boxcar(smooth) / float(smooth), mode='same')
        pylab.semilogx(data['Freq.'] * HEV,
                       q * data['Freq.'],
                       '-',
                       label=xlabel)
    pylab.xlabel(r'Energy (eV)', size=16)
    pylab.ylabel(r'$\nu F_{\nu}$', size=16)
    z = pylab.axis()
    if fmax == 0:
        pylab.axis((emin, emax, 0, z[3]))
    else:
        pylab.axis((emin, emax, 0, fmax))

    pylab.title(root)
    pylab.legend(loc='best')
    pylab.draw()
    plotfile = root + '.png'
    pylab.savefig(plotfile)
    return plotfile
예제 #51
0
파일: rogue.py 프로젝트: barentsen/tractor
def search(tile):

    if os.path.exists('rogue-%s-02.png' %
                      tile) and not os.path.exists('rogue-%s-03.png' % tile):
        print 'Skipping', tile
        return

    fn = os.path.join(tile[:3], tile, 'unwise-%s-w2-%%s-m.fits' % tile)

    try:
        II = [fitsio.read(os.path.join('e%i' % e, fn % 'img')) for e in [1, 2]]
        PP = [fitsio.read(os.path.join('e%i' % e, fn % 'std')) for e in [1, 2]]
        wcs = Tan(os.path.join('e%i' % 1, fn % 'img'))
    except:
        import traceback
        print
        print 'Failed to read data for tile', tile
        traceback.print_exc()
        print
        return
    H, W = II[0].shape

    ps = PlotSequence('rogue-%s' % tile)

    aa = dict(interpolation='nearest', origin='lower')
    ima = dict(interpolation='nearest', origin='lower', vmin=-100, vmax=500)

    plt.clf()
    plt.imshow(II[0], **ima)
    plt.title('Epoch 1')
    ps.savefig()
    plt.clf()
    plt.imshow(II[1], **ima)
    plt.title('Epoch 2')
    ps.savefig()

    # X = gaussian_filter(np.abs((II[0] - II[1]) / np.hypot(PP[0], PP[1])), 1.0)
    # plt.clf()
    # plt.imshow(X, interpolation='nearest', origin='lower')
    # plt.title('Blurred abs difference / per-pixel-std')
    # ps.savefig()

    # Y = (II[0] - II[1]) / reduce(np.hypot, [PP[0], PP[1], np.hypot(100,II[0]), np.hypot(100,II[1]) ])
    Y = (II[0] - II[1]) / reduce(np.hypot, [PP[0], PP[1]])
    X = gaussian_filter(np.abs(Y), 1.0)

    xthresh = 3.

    print 'Value at rogue:', X[1452, 1596]

    print 'pp at rogue:', [pp[1452, 1596] for pp in PP]

    plt.clf()
    plt.imshow(X, interpolation='nearest', origin='lower')
    plt.title('X')
    ps.savefig()

    # plt.clf()
    # plt.hist(np.minimum(100, PP[0].ravel()), 100, range=(0,100),
    #          histtype='step', color='r')
    # plt.hist(np.minimum(100, PP[1].ravel()), 100, range=(0,100),
    #          histtype='step', color='b')
    # plt.title('Per-pixel std')
    # ps.savefig()

    #Y = ((II[0] - II[1]) / np.hypot(PP[0], PP[1]))
    #Y = gaussian_filter(
    #    (II[0] - II[1]) / np.hypot(100, np.hypot(II[0], II[1]))
    #    , 1.0)

    #I = np.argsort(-X.ravel())
    #yy,xx = np.unravel_index(I[:25], X.shape)
    #print 'xx', xx
    #print 'yy', yy

    hot = (X > xthresh)
    peak = find_peaks(hot, X)
    dilate = 2
    hot = binary_dilation(hot, structure=np.ones((3, 3)), iterations=dilate)
    blobs, nblobs = label(hot, np.ones((3, 3), int))
    blobslices = find_objects(blobs)
    # Find maximum pixel within each blob.
    BX, BY = [], []
    BV = []
    for b, slc in enumerate(blobslices):
        sy, sx = slc
        y0, y1 = sy.start, sy.stop
        x0, x1 = sx.start, sx.stop
        bl = blobs[slc]
        i = np.argmax((bl == (b + 1)) * X[slc])
        iy, ix = np.unravel_index(i, dims=bl.shape)
        by = iy + y0
        bx = ix + x0
        BX.append(bx)
        BY.append(by)
        BV.append(X[by, bx])
    BX = np.array(BX)
    BY = np.array(BY)
    BV = np.array(BV)
    I = np.argsort(-BV)
    xx, yy = BX[I], BY[I]

    keep = []
    S = 15
    for i, (x, y) in enumerate(zip(xx, yy)):
        #print x,y
        if x < S or y < S or x + S >= W or y + S >= H:
            continue

        slc = slice(y - S, y + S + 1), slice(x - S, x + S + 1)
        slc2 = slice(y - 3, y + 3 + 1), slice(x - 3, x + 3 + 1)

        mx = np.max((II[0][slc] + II[1][slc]) / 2.)
        #print 'Max within slice:', mx
        #if mx > 5e3:
        if mx > 2e3:
            continue

        mx2 = np.max((II[0][slc2] + II[1][slc2]) / 2.)
        print 'Flux near object:', mx2
        if mx2 < 250:
            continue

        #miny = np.min(Y[slc2])
        #maxy = np.max(Y[slc2])
        keep.append(i)

    keep = np.array(keep)
    if len(keep) == 0:
        print 'No objects passed cuts'
        return
    xx = xx[keep]
    yy = yy[keep]

    plt.clf()
    plt.imshow(X, interpolation='nearest', origin='lower', cmap='gray')
    plt.title('X')
    ax = plt.axis()
    plt.plot(xx, yy, 'r+')
    plt.plot(1596, 1452, 'o', mec=(0, 1, 0), mfc='none')
    plt.axis(ax)
    ps.savefig()

    ylo, yhi = [], []
    for i in range(min(len(xx), 100)):
        x, y = xx[i], yy[i]
        slc2 = slice(y - 3, y + 3 + 1), slice(x - 3, x + 3 + 1)
        ylo.append(np.min(Y[slc2]))
        yhi.append(np.max(Y[slc2]))
    plt.clf()
    plt.plot(ylo, yhi, 'r.')
    plt.axis('scaled')
    ps.savefig()

    for i, (x, y) in enumerate(zip(xx, yy)[:50]):
        print x, y
        rows, cols = 2, 3
        ra, dec = wcs.pixelxy2radec(x + 1, y + 1)

        slc = slice(y - S, y + S + 1), slice(x - S, x + S + 1)
        slc2 = slice(y - 3, y + 3 + 1), slice(x - 3, x + 3 + 1)

        mx = max(np.max(II[0][slc]), np.max(II[1][slc]))
        print 'Max within slice:', mx
        miny = np.min(Y[slc2])
        maxy = np.max(Y[slc2])

        plt.clf()

        plt.subplot(rows, cols, 1)
        plt.imshow(II[0][slc], **ima)
        plt.xticks([])
        plt.yticks([])
        plt.colorbar()
        plt.title('epoch 1')

        plt.subplot(rows, cols, 2)
        plt.imshow(II[1][slc], **ima)
        plt.xticks([])
        plt.yticks([])
        plt.colorbar()
        plt.title('epoch 2')

        plt.subplot(rows, cols, 3)
        plt.imshow(PP[0][slc], **aa)
        plt.xticks([])
        plt.yticks([])
        plt.colorbar()
        plt.title('std 1')

        plt.subplot(rows, cols, 6)
        plt.imshow(PP[1][slc], **aa)
        plt.xticks([])
        plt.yticks([])
        plt.colorbar()
        plt.title('std 2')

        plt.subplot(rows, cols, 4)
        plt.imshow(X[slc], **aa)
        plt.xticks([])
        plt.yticks([])
        plt.colorbar()
        plt.title('X')

        plt.subplot(rows, cols, 5)
        plt.imshow(Y[slc], **aa)
        plt.xticks([])
        plt.yticks([])
        plt.colorbar()
        plt.title('Y')

        #plt.suptitle('Tile %s, Flux: %4.0f, Range: %.2g %.2g' % (tile,mx,miny,maxy))
        plt.suptitle('Tile %s, RA,Dec (%.4f, %.4f)' % (tile, ra, dec))

        ps.savefig()
예제 #52
0
파일: kepfilter.py 프로젝트: rodluger/PyKE
def kepfilter(infile,outfile,datacol,function,cutoff,passband,plot,plotlab,
              clobber,verbose,logfile,status,cmdLine=False): 

## startup parameters

    status = 0
    numpy.seterr(all="ignore") 
    labelsize = 24
    ticksize = 16
    xsize = 16
    ysize = 6
    lcolor = '#0000ff'
    lwidth = 1.0
    fcolor = '#ffff00'
    falpha = 0.2

## log the call 

    hashline = '----------------------------------------------------------------------------'
    kepmsg.log(logfile,hashline,verbose)
    call = 'KEPFILTER -- '
    call += 'infile='+infile+' '
    call += 'outfile='+outfile+' '
    call += 'datacol='+str(datacol)+' '
    call += 'function='+str(function)+' '
    call += 'cutoff='+str(cutoff)+' '
    call += 'passband='+str(passband)+' '
    plotit = 'n'
    if (plot): plotit = 'y'
    call += 'plot='+plotit+ ' '
    call += 'plotlab='+str(plotlab)+' '
    overwrite = 'n'
    if (clobber): overwrite = 'y'
    call += 'clobber='+overwrite+ ' '
    chatter = 'n'
    if (verbose): chatter = 'y'
    call += 'verbose='+chatter+' '
    call += 'logfile='+logfile
    kepmsg.log(logfile,call+'\n',verbose)

## start time

    kepmsg.clock('KEPFILTER started at',logfile,verbose)

## test log file

    logfile = kepmsg.test(logfile)

## clobber output file

    if clobber: status = kepio.clobber(outfile,logfile,verbose)
    if kepio.fileexists(outfile): 
	    message = 'ERROR -- KEPFILTER: ' + outfile + ' exists. Use clobber=yes'
	    status = kepmsg.err(logfile,message,verbose)

## open input file

    if status == 0:
        instr, status = kepio.openfits(infile,'readonly',logfile,verbose)
        tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr,infile,logfile,verbose,status)
    if status == 0:
        try:
            work = instr[0].header['FILEVER']
            cadenom = 1.0
        except:
            cadenom = cadence

## fudge non-compliant FITS keywords with no values

    if status == 0:
        instr = kepkey.emptykeys(instr,file,logfile,verbose)

## read table structure

    if status == 0:
	table, status = kepio.readfitstab(infile,instr[1],logfile,verbose)

# read time and flux columns

    if status == 0:
        barytime, status = kepio.readtimecol(infile,table,logfile,verbose)
        flux, status = kepio.readsapcol(infile,table,logfile,verbose)

# filter input data table

    if status == 0:
        try:
            nanclean = instr[1].header['NANCLEAN']
        except:
            naxis2 = 0
            for i in range(len(table.field(0))):
                if (numpy.isfinite(barytime[i]) and numpy.isfinite(flux[i]) and flux[i] != 0.0):
                    table[naxis2] = table[i]
                    naxis2 += 1
            instr[1].data = table[:naxis2]
            comment = 'NaN cadences removed from data'
            status = kepkey.new('NANCLEAN',True,comment,instr[1],outfile,logfile,verbose)

## read table columns

    if status == 0:
        intime, status = kepio.readtimecol(infile,instr[1].data,logfile,verbose)
    if status == 0:
	indata, status = kepio.readfitscol(infile,instr[1].data,datacol,logfile,verbose)
    if status == 0:
        intime = intime + bjdref
        indata = indata / cadenom

## define data sampling

    if status == 0:
        tr = 1.0 / (cadence / 86400)
        timescale = 1.0 / (cutoff / tr)

## define convolution function

    if status == 0:
        if function == 'boxcar':
            filtfunc = numpy.ones(numpy.ceil(timescale))
        elif function == 'gauss':
            timescale /= 2
            dx = numpy.ceil(timescale * 10 + 1)
            filtfunc = kepfunc.gauss()
            filtfunc = filtfunc([1.0,dx/2-1.0,timescale],linspace(0,dx-1,dx))
        elif function == 'sinc':
            dx = numpy.ceil(timescale * 12 + 1)
            fx = linspace(0,dx-1,dx)
            fx = fx - dx / 2 + 0.5
            fx /= timescale
            filtfunc = numpy.sinc(fx)
        filtfunc /= numpy.sum(filtfunc)

## pad time series at both ends with noise model

    if status == 0:
        ave, sigma  = kepstat.stdev(indata[:len(filtfunc)])
        padded = append(kepstat.randarray(np.ones(len(filtfunc)) * ave,
                                          np.ones(len(filtfunc)) * sigma), indata)
        ave, sigma  = kepstat.stdev(indata[-len(filtfunc):])
        padded = append(padded, kepstat.randarray(np.ones(len(filtfunc)) * ave,
                                                  np.ones(len(filtfunc)) * sigma))

## convolve data

    if status == 0:
        convolved = convolve(padded,filtfunc,'same')

## remove padding from the output array

    if status == 0:
        if function == 'boxcar':
            outdata = convolved[len(filtfunc):-len(filtfunc)]
        else:
            outdata = convolved[len(filtfunc):-len(filtfunc)]
            

## subtract low frequencies

    if status == 0 and passband == 'high':
        outmedian = median(outdata)
        outdata = indata - outdata + outmedian

## comment keyword in output file

    if status == 0:
        status = kepkey.history(call,instr[0],outfile,logfile,verbose)

## clean up x-axis unit

    if status == 0:
	intime0 = float(int(tstart / 100) * 100.0)
        if intime0 < 2.4e6: intime0 += 2.4e6
	ptime = intime - intime0
	xlab = 'BJD $-$ %d' % intime0

## clean up y-axis units

    if status == 0:
        pout = indata * 1.0
        pout2 = outdata * 1.0
	nrm = len(str(int(numpy.nanmax(pout))))-1
	pout = pout / 10**nrm
	pout2 = pout2 / 10**nrm
	ylab = '10$^%d$ %s' % (nrm, plotlab)

## data limits

	xmin = ptime.min()
	xmax = ptime.max()
	ymin = numpy.nanmin(pout)
	ymax = numpy.nanmax(pout)
	xr = xmax - xmin
	yr = ymax - ymin
        ptime = insert(ptime,[0],[ptime[0]]) 
        ptime = append(ptime,[ptime[-1]])
        pout = insert(pout,[0],[0.0]) 
        pout = append(pout,0.0)
        pout2 = insert(pout2,[0],[0.0]) 
        pout2 = append(pout2,0.0)

## plot light curve

    if status == 0 and plot:
        try:
            params = {'backend': 'png',
                      'axes.linewidth': 2.5,
                      'axes.labelsize': labelsize,
                      'axes.font': 'sans-serif',
                      'axes.fontweight' : 'bold',
                      'text.fontsize': 12,
                      'legend.fontsize': 12,
                      'xtick.labelsize': ticksize,
                      'ytick.labelsize': ticksize}
            rcParams.update(params)
        except:
            print('ERROR -- KEPFILTER: install latex for scientific plotting')
            status = 1
    if status == 0 and plot:
        pylab.figure(figsize=[xsize,ysize])
        pylab.clf()

## plot filtered data

        ax = pylab.axes([0.06,0.1,0.93,0.87])
        pylab.gca().xaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        pylab.gca().yaxis.set_major_formatter(pylab.ScalarFormatter(useOffset=False))
        labels = ax.get_yticklabels()
        setp(labels, 'rotation', 90, fontsize=12)
        pylab.plot(ptime,pout,color='#ff9900',linestyle='-',linewidth=lwidth)
        fill(ptime,pout,color=fcolor,linewidth=0.0,alpha=falpha)
        if passband == 'low':
            pylab.plot(ptime[1:-1],pout2[1:-1],color=lcolor,linestyle='-',linewidth=lwidth)
        else:
            pylab.plot(ptime,pout2,color=lcolor,linestyle='-',linewidth=lwidth)
            fill(ptime,pout2,color=lcolor,linewidth=0.0,alpha=falpha)
	xlabel(xlab, {'color' : 'k'})
	ylabel(ylab, {'color' : 'k'})
	xlim(xmin-xr*0.01,xmax+xr*0.01)
        if ymin >= 0.0: 
            ylim(ymin-yr*0.01,ymax+yr*0.01)
        else:
            ylim(1.0e-10,ymax+yr*0.01)
        pylab.grid()
        
# render plot

        if cmdLine: 
            pylab.show()
        else: 
            pylab.ion()
            pylab.plot([])
            pylab.ioff()
	
## write output file

    if status == 0:
        for i in range(len(outdata)):
            instr[1].data.field(datacol)[i] = outdata[i]
        instr.writeto(outfile)
    
## close input file

    if status == 0:
        status = kepio.closefits(instr,logfile,verbose)	    

## end time

    if (status == 0):
	    message = 'KEPFILTER completed at'
    else:
	    message = '\nKEPFILTER aborted at'
    kepmsg.clock(message,logfile,verbose)
예제 #53
0
def fit_exp(o_xdata,o_ydata,o_yerr):    

    import scipy, pylab

    a = scipy.array(o_ydata) 
    a, b, varp = pylab.hist(a,bins=scipy.arange(-2,2,0.05))
    pylab.xlabel("shear")
    pylab.ylabel("Number of Galaxies")
    #pylab.show()

    o_xdata = scipy.array(o_xdata)
    o_ydata = scipy.array(o_ydata)
    o_yerr = scipy.array(o_yerr)
    print o_yerr

    #o_xdata = o_xdata[abs(o_ydata) > 0.05]
    #o_yerr = o_yerr[abs(o_ydata) > 0.05]
    #o_ydata = o_ydata[abs(o_ydata) > 0.05]

    both = 0 
    As = []
    for z in range(25): 

        xdata = []
        ydata = []
        yerr = []
        for i in range(len(o_xdata)):
            rand = int(random.random()*len(o_xdata)) - 1
            #print rand, len(o_xdata)
            xdata.append(o_xdata[rand])
            ydata.append(o_ydata[rand]) 
            yerr.append(o_yerr[rand])
        xdata = scipy.array(xdata)
        ydata = scipy.array(ydata)
                
        ##########                                                                                                                        
        # Fitting the data -- Least Squares Method
        ##########
        
        # Power-law fitting is best done by first converting
        # to a linear equation and then fitting to a straight line.
        #
        #  y = a * x^b
        #  log(y) = log(a) + b*log(x)
        #
                                                                                                                                          
        powerlaw = lambda x, amp, index: amp * (x**index)
                                                                                                                                          
        #print xdata
        #print ydata 
                                                                                                                                          
        ydata = ydata / (scipy.ones(len(ydata)) + abs(ydata))
        
        # define our (line) fitting function
        #fitfunc = lambda p, x: p[0]*x**p[1] 
                                                                                                                                          
        if both: 
            fitfunc = lambda p, x: p[0]*x**p[1] 
            errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err
            pinit = [1.,-1.]
        else:
            fitfunc = lambda p, x: p[0]*x**-1.
            errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err
            pinit = [1.] #, -1.0]
                                                                                                                                          
        #ydataerr = scipy.ones(len(ydata))  * 0.3
                                                                                                                                          
        out = scipy.optimize.leastsq(errfunc, pinit, args=(scipy.array(xdata), scipy.array(ydata), scipy.array(yerr)), full_output=1)
                                                                                                                                          
        if both:
            pfinal = out[0]          
            covar = out[1]
            print pfinal
            print covar
            
            index = pfinal[1]
            amp = pfinal[0]
            ampErr = covar[0][0]
            indexErr = covar[1][1]
                                                                                                                                          
        else: 
            pfinal = out[0]          
            covar = out[1]
            print pfinal
            print covar
            
            index = -1. #pfinal[1]
            amp = pfinal #[0]
            ampErr = covar[0][0]**0.5
            indexErr = 0 #covar
        
        #indexErr = scipy.sqrt( covar[0][0] )
        #ampErr = scipy.sqrt( covar[1][1] ) * amp
        
        ##########
        # Plotting data
        ##########
                                                                                                                                          
        import pylab 
        pylab.clf()
        #pylab.subplot(2, 1, 1)
        pylab.errorbar(xdata, ydata, yerr=yerr, fmt='k.')  # Data
        from copy import copy
        xdataline = copy(xdata)
        xdataline.sort()
        pylab.plot(xdataline, powerlaw(xdataline, amp, index))     # Fit
        pylab.text(5, 6.5, 'Ampli = %5.2f +/- %5.2f' % (amp, ampErr))
        pylab.text(5, 5.5, 'Index = %5.2f +/- %5.2f' % (index, indexErr))
        pylab.title('Best Fit Power Law')
        pylab.xlabel('X')
        pylab.ylabel('Y')
        
        #pylab.subplot(2, 1, 2)
        #pylab.loglog(xdataline, powerlaw(xdataline, amp, index))
        #pylab.errorbar(xdata, ydata, yerr=ydataerr, fmt='k.')  # Data
        #pylab.xlabel('X (log scale)')
        #pylab.ylabel('Y (log scale)')
        #pylab.xlim(1.0, 11)
                                                                                                                                          
        print z 
        if both:
            pylab.show() 
                                                                                                                                          
                                                                                                                                          
        #pylab.show() 
                                                                                                                                          
        pylab.clf()

        As.append(amp)            

    As = scipy.array(As)

    return As.mean(), As.std()
mySamples = []
myLinear = []
myQuadratic = []
myCubic = []
myExponential = []

for i in range(0, 30):
    mySamples.append(i)
    myLinear.append(i)
    myQuadratic.append(i**2)
    myCubic.append(i**3)
    myExponential.append(1.4**i)

# Generate linear vs quadratic plot
plt.figure("lin quad")
plt.clf()  # clears the frame
plt.ylim(0, 1000)
plt.plot(mySamples, myLinear, "b-", label="linear")  # b- means blue line
plt.plot(mySamples, myQuadratic, "ko",
         label="quadratic")  # ko means black dots
plt.legend(
    loc="upper right"
)  # specifies the location of the legend instead of applying pylab default

# Generate cubic vs exponential plot
plt.figure("cube exp")
plt.clf()  # clears the frame
plt.plot(mySamples, myCubic, "r--", label="cubic")  # r-- means red dashed
plt.plot(mySamples, myExponential, "g^",
         label="exponential")  # g^ means green triangles
plt.legend()  # apply pylab default for legend location
def plot_compare_methods(offsets,
                         eoffsets,
                         dx=None,
                         dy=None,
                         fig1=1,
                         fig2=2,
                         legend=True):
    """
    plot wrapper
    """
    import pylab

    pylab.figure(fig1)
    pylab.clf()
    if dx is not None and dy is not None:
        pylab.plot([dx], [dy],
                   'kx',
                   markersize=30,
                   zorder=50,
                   markeredgewidth=3)
    pylab.errorbar(offsets[:, 0],
                   offsets[:, 1],
                   xerr=eoffsets[:, 0],
                   yerr=eoffsets[:, 1],
                   linestyle='none',
                   label='DFT')
    pylab.errorbar(offsets[:, 2],
                   offsets[:, 3],
                   xerr=eoffsets[:, 2],
                   yerr=eoffsets[:, 3],
                   linestyle='none',
                   label='Taylor')
    pylab.errorbar(offsets[:, 4],
                   offsets[:, 5],
                   xerr=eoffsets[:, 4],
                   yerr=eoffsets[:, 5],
                   linestyle='none',
                   label='Gaussian')
    pylab.errorbar(offsets[:, 6],
                   offsets[:, 7],
                   xerr=eoffsets[:, 6],
                   yerr=eoffsets[:, 7],
                   linestyle='none',
                   label='$\\chi^2$')
    if legend:
        pylab.legend(loc='best')

    means = offsets.mean(axis=0)
    stds = offsets.std(axis=0)
    emeans = eoffsets.mean(axis=0)
    estds = eoffsets.std(axis=0)

    print("Standard Deviations: ", stds)
    print("Error Means: ", emeans)
    print("emeans/stds: ", emeans / stds)

    pylab.figure(fig2)
    pylab.clf()
    if dx is not None and dy is not None:
        pylab.plot([dx], [dy],
                   'kx',
                   markersize=30,
                   zorder=50,
                   markeredgewidth=3)
    pylab.errorbar(means[0],
                   means[1],
                   xerr=emeans[0],
                   yerr=emeans[1],
                   capsize=20,
                   color='r',
                   dash_capstyle='round',
                   solid_capstyle='round',
                   label='DFT')
    pylab.errorbar(means[2],
                   means[3],
                   xerr=emeans[2],
                   yerr=emeans[3],
                   capsize=20,
                   color='g',
                   dash_capstyle='round',
                   solid_capstyle='round',
                   label='Taylor')
    pylab.errorbar(means[4],
                   means[5],
                   xerr=emeans[4],
                   yerr=emeans[5],
                   capsize=20,
                   color='b',
                   dash_capstyle='round',
                   solid_capstyle='round',
                   label='Gaussian')
    pylab.errorbar(means[6],
                   means[7],
                   xerr=emeans[6],
                   yerr=emeans[7],
                   capsize=20,
                   color='m',
                   dash_capstyle='round',
                   solid_capstyle='round',
                   label='$\\chi^2$')
    pylab.errorbar(means[0],
                   means[1],
                   xerr=stds[0],
                   yerr=stds[1],
                   capsize=10,
                   color='r',
                   linestyle='--',
                   linewidth=5)
    pylab.errorbar(means[2],
                   means[3],
                   xerr=stds[2],
                   yerr=stds[3],
                   capsize=10,
                   color='g',
                   linestyle='--',
                   linewidth=5)
    pylab.errorbar(means[4],
                   means[5],
                   xerr=stds[4],
                   yerr=stds[5],
                   capsize=10,
                   color='b',
                   linestyle='--',
                   linewidth=5)
    pylab.errorbar(means[6],
                   means[7],
                   xerr=stds[6],
                   yerr=stds[7],
                   capsize=10,
                   color='m',
                   linestyle='--',
                   linewidth=5)
    if legend:
        pylab.legend(loc='best')
예제 #56
0
파일: rogue.py 프로젝트: barentsen/tractor
def epoch_coadd_plots(tractor, ps, S, ima, yearcut, fakewcs):

    bimg = np.zeros((S, S))
    bmod = np.zeros((S, S))
    bnum = np.zeros((S, S))
    bchisq = np.zeros((S, S))
    bchi = np.zeros((S, S))
    aimg = np.zeros((S, S))
    amod = np.zeros((S, S))
    anum = np.zeros((S, S))
    achisq = np.zeros((S, S))
    achi = np.zeros((S, S))

    tims = tractor.getImages()
    for i, tim in enumerate(tims):
        mod = tractor.getModelImage(i)

        hh, ww = tim.shape
        wrap = TractorWCSWrapper(tim.wcs, ww, hh)
        #print 'Shape', tim.shape
        #print 'WCS', tim.wcs
        #print 'WCS', tim.wcs.wcs
        Yo, Xo, Yi, Xi, [rim,
                         rmod] = resample_with_wcs(fakewcs, wrap,
                                                   [tim.data, mod], 3)

        if tim.time.toYear() > yearcut:
            im, mod, num, chisq, chi = (aimg, amod, anum, achisq, achi)
        else:
            im, mod, num, chisq, chi = (bimg, bmod, bnum, bchisq, bchi)
        im[Yo, Xo] += rim
        mod[Yo, Xo] += rmod
        num[Yo, Xo] += 1.
        chi[Yo, Xo] += ((rim - rmod) * tim.getInvError()[Yi, Xi])
        chisq[Yo, Xo] += ((rim - rmod)**2 * tim.getInvvar()[Yi, Xi])

    bimg /= np.maximum(bnum, 1)
    aimg /= np.maximum(anum, 1)
    bmod /= np.maximum(bnum, 1)
    amod /= np.maximum(anum, 1)

    #print 'N', anum.max(), bnum.max()
    #print 'mean N', anum.mean(), bnum.mean()

    achi /= np.maximum(anum, 1)
    bchi /= np.maximum(bnum, 1)

    nn = np.mean([anum.mean(), bnum.mean()])

    #chimax = max(achisq.max(), bchisq.max())
    #ca = dict(interpolation='nearest', origin='lower', vmin=0, vmax=chimax)
    c2a = dict(interpolation='nearest', origin='lower', vmin=0, vmax=16 * nn)
    ca = dict(interpolation='nearest', origin='lower', vmin=-3, vmax=3)

    plt.clf()

    plt.subplot(2, 3, 1)
    plt.imshow(bimg, **ima)

    plt.subplot(2, 3, 2)
    plt.imshow(bmod, **ima)
    plt.title('First epoch')

    plt.subplot(2, 3, 3)
    #plt.imshow(bchisq, **c2a)
    plt.imshow(bchi, **ca)

    plt.subplot(2, 3, 4)
    plt.imshow(aimg, **ima)

    plt.subplot(2, 3, 5)
    plt.imshow(amod, **ima)
    plt.title('Second epoch')

    plt.subplot(2, 3, 6)
    #plt.imshow(achisq, **c2a)
    plt.imshow(achi, **ca)

    ps.savefig()
예제 #57
0
def tests():
    x = pylab.arange(0.0, 2 * pylab.pi, 0.01)
    list_y = (pylab.sin(x), pylab.sin(2 * x))
    plot_and_format((x, ), (list_y[0], ))
    exportplot('test/test1_one_curve.png')
    pylab.clf()
    list_x = (x, x)
    plot_and_format(list_x, list_y)
    exportplot('test/test2_two_curves.png')
    pylab.clf()
    list_format = ('k-', 'r--')
    plot_and_format(list_x, list_y, list_format=list_format)
    exportplot('test/test3_two_curves_formatting.png')
    pylab.clf()
    plot_and_format(list_x,
                    list_y,
                    list_format=list_format,
                    xlabel='hello x axis')
    exportplot('test/test4_two_curves_formatting_xlab.png')
    pylab.clf()
    plot_and_format(list_x,
                    list_y,
                    list_format=list_format,
                    legend=['sin($x$)', 'sin($2x$)'])
    exportplot('test/test5_two_curves_formatting_legend.png')
    pylab.clf()
    plot_and_format(list_x,
                    list_y,
                    list_format=list_format,
                    xticks={
                        'ticks': [0, pylab.pi, 2 * pylab.pi],
                        'labels': ['0', '$\pi$', '$2\pi$']
                    })
    exportplot('test/test6_two_curves_formatting_xticks.png')
    pylab.clf()
    plot_and_format(list_x,
                    list_y,
                    list_format=list_format,
                    xticks={
                        'ticks': [0, pylab.pi, 2 * pylab.pi],
                        'labels': ['0', '$\pi$', '$2\pi$']
                    },
                    xlim=[0, 2 * pylab.pi])
    exportplot('test/test7_two_curves_formatting_xticks_xlim.png')
    pylab.clf()
예제 #58
0
def perceptron(hidden_neurons=5, weightdecay=0.01, momentum=0.1):
    INPUT_FEATURES = 2
    CLASSES = 3
    HIDDEN_NEURONS = hidden_neurons
    WEIGHTDECAY = weightdecay
    MOMENTUM = momentum

    # Generate the labeled set
    g = generate_data()
    #g = generate_data2()
    alldata = g['d']
    minX, maxX, minY, maxY = g['minX'], g['maxX'], g['minY'], g['maxY']

    # Split data into test and training dataset
    tstdata, trndata = alldata.splitWithProportion(0.25)

    trndata._convertToOneOfMany()  # This is necessary, but I don't know why
    tstdata._convertToOneOfMany()  # http://stackoverflow.com/q/8154674/562769

    print("Number of training patterns: %i" % len(trndata))
    print("Input and output dimensions: %i, %i" %
          (trndata.indim, trndata.outdim))
    print("Hidden neurons: %i" % HIDDEN_NEURONS)
    print("First sample (input, target, class):")
    print(trndata['input'][0], trndata['target'][0], trndata['class'][0])

    fnn = buildNetwork(trndata.indim,
                       HIDDEN_NEURONS,
                       trndata.outdim,
                       outclass=SoftmaxLayer)

    trainer = BackpropTrainer(fnn,
                              dataset=trndata,
                              momentum=MOMENTUM,
                              verbose=True,
                              weightdecay=WEIGHTDECAY)

    # Visualization
    ticksX = arange(minX - 1, maxX + 1, 0.2)
    ticksY = arange(minY - 1, maxY + 1, 0.2)
    X, Y = meshgrid(ticksX, ticksY)

    # need column vectors in dataset, not arrays
    griddata = ClassificationDataSet(INPUT_FEATURES, 1, nb_classes=CLASSES)
    for i in range(X.size):
        griddata.addSample([X.ravel()[i], Y.ravel()[i]], [0])

    for i in range(20):
        trainer.trainEpochs(1)
        trnresult = percentError(trainer.testOnClassData(), trndata['class'])
        tstresult = percentError(trainer.testOnClassData(dataset=tstdata),
                                 tstdata['class'])

        print("epoch: %4d" % trainer.totalepochs,
              "  train error: %5.2f%%" % trnresult,
              "  test error: %5.2f%%" % tstresult)
        out = fnn.activateOnDataset(griddata)
        # the highest output activation gives the class
        out = out.argmax(axis=1)
        out = out.reshape(X.shape)

        figure(1)  # always print on the same canvas
        ioff()  # interactive graphics off
        clf()  # clear the plot
        for c in [0, 1, 2]:
            here, _ = where(tstdata['class'] == c)
            plot(tstdata['input'][here, 0], tstdata['input'][here, 1], 'o')
        if out.max() != out.min():  # safety check against flat field
            contourf(X, Y, out)  # plot the contour
        ion()  # interactive graphics on
        draw()  # update the plot

    ioff()
    show()
예제 #59
0
파일: sed.py 프로젝트: Areustle/fermi-glast
    def __call__(
        self,
        model=None,
        name=None,
        fignum=5,
        axes=None,
        axis=None,  #(1e2,1e6,1e-7,1e-2),
        data_kwargs=dict(
            linewidth=2,
            color='k',
        ),
        fit_kwargs=dict(
            lw=2,
            color='r',
        ),
        butterfly=True,
        outdir=None,
        suffix='_sed',
        galmap=None,
        annotate=None,
        grid=True,
    ):
        """Plot the SED
        ========     ===================================================
        keyword      description
        ========     ===================================================
        model        spectral model object
        name         name of the source
        fignum       [5] if set, use (and clear) this figure. If None, use current Axes object
        axes         [None] If set use this Axes object
        axis         None, (1e2, 1e5, 1e-8, 1e-2) depending on energy flux unit
        data_kwargs  a dict to pass to the data part of the display
        fit_kwargs   a dict to pass to the fit part of the display
        butterfly    [True] plot model with a butterfly outline
        outdir       [None] if set, save sed into <outdir>/<source_name>_sed.png if outdir is a directory, save into filename=<outdir> if noself.
        suffix       ['_sed'] Add to source name to form filename
        galmap       [None] if set to a SkyDir, create a little galactic map showing this position
        annotate     [None] if set, a tuple of (x, y, text), in axes coords
        grid         [True] Set False to turn off grid
        ========     ===================================================
        
        """
        if model is None: model = self.model
        if name is None: name = self.name
        energy_flux_factor = self.scale_factor
        # conversion 1.602E-19 * 1E6 eV/Mev * 1E7 erg/J * = 1.602E-6 erg/MeV
        oldlw = plt.rcParams['axes.linewidth']
        oldticksize = plt.rcParams['xtick.labelsize']
        plt.rcParams['axes.linewidth'] = 2
        plt.rcParams['xtick.labelsize'] = plt.rcParams['ytick.labelsize'] = 10
        if axes is None:
            fig = plt.figure(fignum, figsize=(4, 4))
            plt.clf()
            fig.add_axes((0.22, 0.15, 0.75, 0.72))
            axes = plt.gca()
        self.axes = axes
        axes.set_xscale('log')
        axes.set_yscale('log')
        if axis is None:
            axis = (1e2, 4e5, 0.2 * self.scale_factor, 1e4 * self.scale_factor)
        axes.axis(axis)
        axes.grid(grid)
        axes.set_autoscale_on(False)

        self.plot_data(axes, **data_kwargs)
        # and the model, perhaps with a butterfly
        self.dom = np.logspace(np.log10(self.rec.elow[0]),
                               np.log10(self.rec.ehigh[-1]), 26)
        self.plot_model(model, butterfly, **fit_kwargs)
        plt.rcParams['axes.linewidth'] = oldlw
        plt.rcParams['xtick.labelsize'] = plt.rcParams[
            'ytick.labelsize'] = oldticksize

        # the axis labels (note reduced labelpad for y)
        axes.set_ylabel(r'$\mathsf{Energy\ Flux\ (%s\ cm^{-2}\ s^{-1})}$' %
                        self.energy_flux_unit,
                        labelpad=0)
        axes.set_xlabel(r'$\mathsf{Energy\ (GeV)}$')
        if self.energy_flux_unit == 'eV':
            axes.set_yticklabels(
                ['', '1', '10', '100', r'$\mathdefault{10^{3}}$'])

        axes.set_title(name, size=14)
        set_xlabels(axes, self.gev_scale)
        # add a galactic map if requested
        if galmap is not None:
            image.galactic_map(galmap,
                               axes=self.axes,
                               color='lightblue',
                               marker='s',
                               markercolor='r',
                               markersize=20)

        if annotate is not None:
            axes.text(annotate[0],
                      annotate[1],
                      annotate[2],
                      transform=axes.transAxes,
                      fontsize=8)
        if outdir is not None:
            self.name = name
            self.savefig(outdir, suffix)
예제 #60
0
파일: liteMap.py 프로젝트: cpvargas/flipper
def addLiteMapsWithSpectralWeighting(liteMap1,
                                     liteMap2,
                                     kMask1Params=None,
                                     kMask2Params=None,
                                     signalMap=None):
    """
    @brief add two maps, weighting in Fourier space
    Maps must be the same size.
    @param kMask1Params mask params for liteMap1 (see fftTools.power2D.createKspaceMask)
    @param kMask2Params mask params for liteMap2 (see fftTools.power2D.createKspaceMask)
    @param signalMap liteMap with best estimate of signal to use when estimating noise weighting
    @return new map
    """
    #Get fourier weights
    flTrace.issue("liteMap", 0, "Computing Weights")
    #np1 = fftTools.noisePowerFromLiteMaps(liteMap1, liteMap2, applySlepianTaper = False)
    #np2 = fftTools.noisePowerFromLiteMaps(liteMap2, liteMap1, applySlepianTaper = False)
    data1 = copy.copy(liteMap1.data)
    data2 = copy.copy(liteMap2.data)
    if signalMap != None:
        liteMap1.data[:] = (liteMap1.data - signalMap.data)[:]
        liteMap2.data[:] = (liteMap2.data - signalMap.data)[:]
    np1 = fftTools.powerFromLiteMap(liteMap1)  #, applySlepianTaper = True)
    np2 = fftTools.powerFromLiteMap(liteMap2)  #), applySlepianTaper = True)
    print "testing", liteMap1.data == data1
    liteMap1.data[:] = data1[:]
    liteMap2.data[:] = data2[:]

    n1 = np1.powerMap

    n2 = np2.powerMap
    #     n1[np.where( n1<n1.max()*.002)] = n1.max()*.001
    #     n2[np.where( n2<n2.max()*.002)] = n2.max()*.001

    w1 = 1 / n1
    w2 = 1 / n2

    m1 = np.median(w1)
    m2 = np.median(w2)
    w1[np.where(abs(w1) > 4 * m1)] = 4 * m1
    w2[np.where(abs(w2) > 4 * m2)] = 4 * m2

    #w1[:] = 1.
    #w2[:] = 1.
    #pylab.hist(w1.ravel())
    #pylab.savefig("hist1.png")
    #pylab.clf()
    yrange = [4, 5000]
    np1.powerMap = w1
    #np1.plot(pngFile="w1.png", log=True, zoomUptoL=8000, yrange = yrange)
    np2.powerMap = w2
    #np2.plot(pngFile="w2.png", log=True, zoomUptoL=8000, yrange = yrange)

    if kMask1Params != None:
        np1.createKspaceMask(**kMask1Params)
        w1 *= np1.kMask

    if kMask2Params != None:
        np2.createKspaceMask(**kMask2Params)
        w2 *= np2.kMask
    pylab.clf()

    invW = 1.0 / (w1 + w2)
    invW[np.where(np.isnan(invW))] = 0.
    invW[np.where(np.isinf(invW))] = 0.

    flTrace.issue("liteMap", 3,
                  "NaNs in inverse weight: %s" % str(np.where(np.isnan(invW))))
    flTrace.issue("liteMap", 3,
                  "Infs in inverse weight: %s" % str(np.where(np.isinf(invW))))

    flTrace.issue("liteMap", 2, "Adding Maps")
    f1 = fftTools.fftFromLiteMap(liteMap1, applySlepianTaper=False)
    f2 = fftTools.fftFromLiteMap(liteMap2, applySlepianTaper=False)
    kTot = (f1.kMap * w1 + f2.kMap * w2) * invW
    flTrace.issue(
        "liteMap", 3,
        "NaNs in filtered transform: %s" % str(np.where(np.isnan(kTot))))
    f1.kMap = kTot
    finalMap = liteMap1.copy()
    finalMap.data[:] = 0.
    finalMap.data = f1.mapFromFFT()
    return finalMap