예제 #1
0
파일: PCA.py 프로젝트: rearmstr/PSF_PCA
class PCAResult:
    def __init__(self,filename):
        self.filename=filename
        self.exps=[]
        self.bounds=[]
        self.skip61=True
        self.patches=[]
        for i,corner in corners.items():
            self.patches.append(Rectangle(corner,xsize,ysize))
        self.outline = PatchCollection(self.patches,facecolor='None')
        self.readFits()
        self.grid = PatchCollection(self.bounds, cmap=matplotlib.cm.jet,
                                    alpha=1,edgecolors=None,linewidths=0)

    def readFits(self):
        pyfile=pyfits.open(self.filename)
        self.exps=pyfile['exps'].data['exposure']
        self.ccd=pyfile['exps'].header['ccd']
        self.nvar=pyfile['exps'].header['nvar']
        self.nx=pyfile['exps'].header['nx']
        self.ny=pyfile['exps'].header['ny']
        self.type=pyfile['exps'].header['type']
        self.grid=[]
        self.xcell=[]
        self.ycell=[]
        for lx,ly,ux,uy in zip(pyfile['grid'].data['lower_x'],
                               pyfile['grid'].data['lower_y'],
                               pyfile['grid'].data['upper_x'],
                               pyfile['grid'].data['upper_y']):
            self.grid.append((lx,ly,ux,uy))

        if self.skip61:self.ccd-=1

        # build the bounding box of ccds
        for i in range(0,self.ccd*len(self.grid)):
            iccd=i/len(self.grid)+1
            icell=i%len(self.grid)
            xl=self.grid[icell][0]
            yl=self.grid[icell][1]
            xu=self.grid[icell][2]
            yu=self.grid[icell][3]
            

            if self.skip61:
                if iccd==61:iccd=62
            #convert to focal plane coordinates
            # for center of grid
            xc=(xu-xl)/2.+xl
            yc=(yu-yl)/2.+yl
            bx,by=toFocal(iccd,xc,yc)
            self.xcell.append(bx)
            self.ycell.append(by)

            # for grid boudaries
            bcx,bcy=toFocal(iccd,xl,yl)
            bcx2,bcy2=toFocal(iccd,xu,yu)

            corner=[]
            corner.append(bcx)
            corner.append(bcy)
            self.bounds.append(Rectangle(corner,bcx2-bcx,bcy2-bcy))

        self.vec=pyfile['vec'].data
        self.data=pyfile['data_mr'].data
        self.coeff=pyfile['coeff'].data
        self.singular=pyfile['singular'].data

    def getPC(self,var,pc):
        p=copy.deepcopy(self.grid)
        if var>=self.nvar:
            print "too high"
            return False
        #p.set_array(self.vec[var::self.nvar][pc])
        p.set_array(self.vec[var*self.nx*self.ny*self.ccd:
                           (var+1)*self.nx*self.ny*self.ccd][pc])
        
        return p

    def getPC2(self,ax,var1,var2,pc,scale=-np.sqrt(2)):
        #p=copy.deepcopy(self.grid)
        if var1>=self.nvar or var2>=self.nvar:
            print "too high"
            return False
        
        p1a=scale*self.vec[var1::self.nvar,pc]
        p2a=scale*self.vec[var2::self.nvar,pc]
        
        v1=[]
        v2=[]
        for p1,p2 in zip(p1a,p2a):
            theta=np.math.atan2(p2,p1)/2
            e=np.sqrt(p1**2+p2**2)
            ce1=e*np.cos(theta)
            ce2=e*np.sin(theta)
            v1.append(ce1)
            v2.append(ce2)
        
        return  ax.quiver(self.xcell,self.ycell,v1,v2,
                          angles='uv',scale=1./500,
                          units='xy',pivot='middle',width=1,headwidth=0.,
                          headlength=0., headaxislength=0.,color='blue')
예제 #2
0
for i, x in enumerate(X3[::2]):
    X2.append(np.mean([X3[2*i], X3[2*i + 1]]))
X2 = np.arange(4)

xshift = [.2, -.2, .2, -.2]
X2 = [x + xshift[x] for x in X2]
print X2
ax2.set_ylabel('cov ' + betastr)
ax2.set_xticks(X2)
ax2data = [data_dict[reg][0] for reg in regs2]
ax2dots = [data_dict[reg][1] for reg in regs2]
colors2 = ['magenta', 'orange', 'steelblue', 'green']
patches = []
for i, dot in enumerate(ax2dots):
    circ = mpatches.Ellipse(xy = (X2[i] + .075, ax2dots[i]), width = .05, height = .05, color = 'black')
    patches.append(circ)
patches = PatchCollection(patches, match_original=True, zorder=10)
patches.set_zorder(20)
ax2.set_xticks([x + .075 for x in X2])
for i in range(4):
    ax2.bar(X2[i], ax2data[i], color=colors2[i], width=0.125, alpha = .8, zorder=2)
ax2.add_line(lines.Line2D(line_xs, line_ys, linewidth=1, alpha=.5, color='black'))
ax2.add_collection(patches)
ax2.set_xticklabels(regs2)
ax2.set_xlim(X2[0] - .125, X2[-1] + .4)

X1 = []
for i, x in enumerate(X2[::2]):
    X1.append(np.mean([X2[2*i], X2[2*i + 1]]))
ax1.set_ylabel('cov ' + betastr)
ax1.set_xticks(X1)