def redshiftfunctions(self):
        D = self.D
        zbins = self.zbins
        z2bins = self.z2bins
        Dabins = zbins * 0.0
        Dmodbins = zbins * 0.0
        Da2bins = numpy.zeros((z2bins.size, z2bins.size))
        volumebins = zbins * 0.0
        for i in range(zbins.size):
            Dabins[i] = D.Da(zbins[i])
            Dmodbins[i] = D.distance_modulus(zbins[i])
            volumebins[i] = D.volume(zbins[i])
        for i in range(z2bins.size):
            for j in range(z2bins.size):
                if j > i:
                    Da2bins[i, j] = D.Da(z2bins[i], z2bins[j])

        self.Da_spline = interpolate.splrep(zbins, Dabins)
        self.Dmod_spline = interpolate.splrep(zbins, Dmodbins)

        self.volume_spline = interpolate.splrep(zbins, volumebins)

        z2d = iT.coords((z2bins.size, z2bins.size)) * self.dz2
        self.Da_bispline = interpolate.RectBivariateSpline(
            z2bins, z2bins, Da2bins)

        #pickle the splines
        splinedump = open("redshiftsplines.pkl", "wb")
        cPickle.dump([
            self.Da_spline, self.Dmod_spline, self.volume_spline,
            self.Da_bispline
        ], splinedump, 2)
Example #2
0
def newAxes(x, y, srcxsize, srcysize):
    import numpy
    import indexTricks as iT

    xdif = x.max() - x.min()
    ydif = y.max() - y.min()

    difmax = numpy.max([xdif, ydif])

    scentre = ((x.max() + x.min()) / 2., (y.max() + y.min()) / 2.)
    sshape = ((srcysize, srcxsize))

    pscale = 1.2 * (difmax / ((srcysize - 3)))

    srcy, srcx = iT.coords(sshape) * pscale
    srcx -= srcx.mean()
    srcy -= srcy.mean()
    srcy += scentre[1]
    srcx += scentre[0]

    srcxaxis = srcx[0]
    srcyaxis = srcy[:, 0]

    srcx = srcx.flatten()
    srcy = srcy.flatten()

    return srcxaxis, srcyaxis, srcx, srcy
Example #3
0
    def lensASource(self,sourcenumber,bands):
        src=self.src[sourcenumber]
        lens=massmodel.PowerLaw('lens',{},{'x':self.xl+self.deltaxl,'y':self.yl+self.deltayl,'q':self.ql,'pa':90+self.deltap,'b':self.bl[sourcenumber],'eta':1})
        es=massmodel.ExtShear('lens',{},{'x':self.xl+self.deltaxl,'y':self.yl+self.deltayl,'pa':self.xp,'b':self.xb})
        lenses=[lens,es]

        a=51
        ox,oy=iT.coords((a,a))
        ps=(self.rs[sourcenumber]*(10./a))
        ox=(ox-(a-1)/2.)*ps+(self.xs[sourcenumber])
        oy=(oy-(a-1)/2.)*ps+(self.ys[sourcenumber])

        unlensedsrcmodel=(src.pixeval(ox,oy,csub=5)*(ps**2)).sum()
        srcnorm=unlensedsrcmodel.sum()
        unlensedsrcmodel/=srcnorm

        srcmodel=pylens.lens_images(lenses,src,[self.x,self.y],getPix=True,csub=5)[0]
        srcmodel[srcmodel<0]=0
        srcmodel/=srcnorm

        self.magnification[sourcenumber]=(numpy.sum(numpy.ravel(srcmodel))/numpy.sum(numpy.ravel(unlensedsrcmodel)))
        sm={}
        for band in bands:
            unlensedtotalsrcflux=10**(-(self.ms[sourcenumber][band]-self.zeropoints[band])/2.5)
            sm[band]=srcmodel*unlensedtotalsrcflux

            if sm[band].max()>0:
                self.totallensedsrcmag[sourcenumber][band]=-2.5*numpy.log10(sm[band].sum())+self.zeropoints[band]
            else:
                self.totallensedsrcmag[sourcenumber][band]=99
        return sm
Example #4
0
    def lensASource(self,sourcenumber,bands):
        src=self.src[sourcenumber]
        lens=massmodel.PowerLaw('lens',{},{'x':self.xl+self.deltaxl,'y':self.yl+self.deltayl,'q':self.ql,'pa':90+self.deltap,'b':self.bl[sourcenumber],'eta':1})
        es=massmodel.ExtShear('lens',{},{'x':self.xl+self.deltaxl,'y':self.yl+self.deltayl,'pa':self.xp,'b':self.xb})
        lenses=[lens,es]

        a=51
        ox,oy=iT.coords((a,a))
        ps=(self.rs[sourcenumber]*(10./a))
        ox=(ox-(a-1)/2.)*ps+(self.xs[sourcenumber])
        oy=(oy-(a-1)/2.)*ps+(self.ys[sourcenumber])

        unlensedsrcmodel=(src.pixeval(ox,oy,csub=5)*(ps**2)).sum()
        srcnorm=unlensedsrcmodel.sum()
        unlensedsrcmodel/=srcnorm

        srcmodel=pylens.lens_images(lenses,src,[self.x,self.y],getPix=True,csub=5)[0]
        srcmodel[srcmodel<0]=0
        srcmodel/=srcnorm

        self.magnification[sourcenumber]=(numpy.sum(numpy.ravel(srcmodel))/numpy.sum(numpy.ravel(unlensedsrcmodel)))
        sm={}
        for band in bands:
            unlensedtotalsrcflux=10**(-(self.ms[sourcenumber][band]-self.zeropoints[band])/2.5)
            sm[band]=srcmodel*unlensedtotalsrcflux
        
            if sm[band].max()>0:
                self.totallensedsrcmag[sourcenumber][band]=-2.5*numpy.log10(sm[band].sum())+self.zeropoints[band]
            else:
                self.totallensedsrcmag[sourcenumber][band]=99
        return sm
    def MomentChecks(self,cmax=2.7,cmin=0.5,vb=False):
        import moments
        import indexTricks as iT
        x,y=iT.coords(self.D.shape)
        x-=self.xl
        y-=self.yl
        x*=self.pixelsize
        y*=self.pixelsize

        goodlabels=[]
        unalignedlabels=[]

        for label in self.labels:
            D2=self.D*1
            D2[self.regions!=label]=0
            

            com,flux,mean=moments.comflux(D2) # we check this bit first since evaluating the shape is the slowest part
            ca=(com[1]-self.xl,com[0]-self.yl)
            com_dist=((ca[0]**2+ca[1]**2)**0.5)*self.pixelsize
            if com_dist < cmin:#arcseconds 
                if vb:print "too close"
                continue 
            if com_dist > cmax:#arcseconds 
                if vb:print "too far"
                continue 

            if self.FluxCheck(flux,mean)==False:
                if vb:print "too faint"
                continue

            com,(a,b,q), eigvals,eigvecs, flux,mean= moments.main(D2)

            ca_hat=ca/((ca[0]**2+ca[1]**2)**0.5)
            va=eigvecs[:,1]#normalized
            dp=ca_hat[0]*va[0]+ca_hat[1]*va[1]
            theta=(numpy.arccos(dp)*180./3.14159) #degrees

#this bit will let you visualize what's going on in each detected residual:
            if self.visualize:
                plt.imshow(D2,interpolation="None")
                plt.scatter(self.xl,self.yl,c="r",s=25)
                plt.plot([self.xl,self.xl+ca[0]],[self.yl,self.yl+ca[1]],c="r",lw=2)
                plt.scatter(com[1],com[0],c="k",s=25)
                plt.plot([com[1]-5*va[0],com[1]+5*va[0]],[com[0]-5*va[1],com[0]+5*va[1]],c="k",lw=3)
                plt.show(block=True)

            if b*self.pixelsize<0.2:#arcseconds 
                if vb:print "too thin"
                continue

            if numpy.abs(theta) >30 and (theta<60 or theta >120):
                unalignedlabels.append(label)
            else:
                goodlabels.append(label)

            self.q=q

        self.goodlabels=goodlabels
        self.unalignedlabels=unalignedlabels
def showRes(x,y,src,psf,img,sig,mask,iflt,vflt,cmat,reg,niter,npix):
    oy,ox = iT.coords((npix,npix))
    oy -= oy.mean()
    ox -= ox.mean()
    span = max(x.max()-x.min(),y.max()-y.min())
    oy *= span/npix
    ox *= span/npix
    ox += x.mean()
    oy += y.mean()
    lmat = psf*src.lmat
    rmat = src.rmat
    res,fit,model,rhs,regg = aT.getModelG(iflt,vflt,lmat,cmat,rmat,reg,niter=niter)

    osrc = src.eval(ox.ravel(),oy.ravel(),fit).reshape(ox.shape)

    oimg = img*numpy.nan
    oimg[mask] = (lmat*fit)

    ext = [0,img.shape[1],0,img.shape[0]]
    ext2 = [x.mean()-span/2.,x.mean()+span/2.,y.mean()-span/2.,y.mean()+span/2.]
    pylab.figure()
    pylab.subplot(221)
    img[~mask] = numpy.nan
    pylab.imshow(img,origin='lower',interpolation='nearest',extent=ext)
    pylab.colorbar()
    pylab.subplot(222)
    pylab.imshow(oimg,origin='lower',interpolation='nearest',extent=ext)
    pylab.colorbar()
    pylab.subplot(223)
    pylab.imshow((img-oimg)/sig,origin='lower',interpolation='nearest',extent=ext)
    pylab.colorbar()
    pylab.subplot(224)
    pylab.imshow(osrc,origin='lower',interpolation='nearest',extent=ext2)
    pylab.colorbar()
    return osrc
Example #7
0
def fullSolution(shape,ysoln,orders,wideorders,wsoln):
    import indexTricks as iT
    coords = iT.coords(shape)
    y = coords[0].copy()
    x = coords[1].copy()
    soln = []
    if shape[1]>3000:
        disp = 1.65e-5
    else:
        disp = 2*1.65e-5
    owave = numpy.arange(3.585,4.038,disp)
    for i in range(len(ysoln)):
        low,high = orders[i]
        wlow,whigh = wideorders[i]
        ytrue,ymap = ysoln[i]
        win,wout = wsoln[i]
        tmpw = genfunc(x[0],0.,win)
        ow = owave[(owave>=tmpw[0])&(owave<=tmpw[-1])].copy()
        if ow.size>x.shape[1]:
            diff = ow.size-x.shape[1]
            if diff%2==0:
                ow = ow[int(diff/2):int(diff/-2)]
            else:
                ow = ow[int(diff/2):int((diff+1)/-2)]
        xc = genfunc(ow,0.,wout)
        xc = xc.repeat(high-low).reshape((xc.size,high-low)).T
        yc = genfunc(xc.ravel(),y[low:high,:xc.shape[1]].ravel(),ytrue)
        yc = yc.reshape((high-low,xc.shape[1]))-wlow

        corr = numpy.linspace(-0.2,0.2,high-low)
        xc = (xc.T+corr).T
        #xc = xc.repeat(high-low).reshape(yc.shape[::-1]).T
        soln.append([numpy.array([yc,xc]),ow[0],ow[-1],disp])
    return soln
Example #8
0
def matrixAppend(mat,vec):
    from scipy.sparse import coo_matrix
    nr,nc = mat.shape
    mat = mat.tocoo()
    r = mat.row.copy()
    c = mat.col.copy()
    d = mat.data.copy()
    if vec.ndim==1:
        if vec.size!=nr:
            print 'Array is not aligned with matrix'
            df
        r = numpy.concatenate((r,numpy.arange(vec.size)))
        c = numpy.concatenate((c,numpy.ones(vec.size)*nc))
        d = numpy.concatenate((d,vec))
        return coo_matrix((d,(r,c)),(nr,nc+1))
    if vec.shape[1]!=nr:
        print 'Array is not aligned with matrix'
        df
    import indexTricks as iT
    vr,vc = iT.coords(vec.shape)
    vc += nc
    r = numpy.concatenate((r,vr.ravel()))
    c = numpy.concatenate((c,vc.ravel()))
    d = numpy.concatenate((d,vec.ravel()))
    return coo_matrix((d,(r,c)),(nr,nc+vec.shape[0]))
Example #9
0
    def __init__(self,B,R,sB,sR,pixelsize,zerofluxB,zerofluxR,xl=None,yl=None):
        self.pixelsize=pixelsize
        self.zerofluxB=zerofluxB
        self.zerofluxR=zerofluxR
        self.zeroflux=self.zerofluxB

        if xl==None:
            xl=B.shape[0]/2.
        if yl==None:
            yl=B.shape[1]/2.

        self.xl=xl
        self.yl=yl

        x,y=iT.coords(B.shape)
        x-=self.xl
        y-=self.yl
        r=(x**2+y**2)**0.5
        mask=((r<2.7) & (r>0.5))
        
        alpha=B[mask].sum()*1./R[mask].sum()


        self.D=B-alpha*R

        self.S=(sB**2+(alpha*sR)**2)**.5

        
        self.SN=self.D/self.S
Example #10
0
    def redshiftfunctions(self):   
        D=self.D
        zbins=self.zbins
        z2bins=self.z2bins
        Dabins=zbins*0.0
        Dmodbins=zbins*0.0
        Da2bins=numpy.zeros((z2bins.size,z2bins.size))
        volumebins=zbins*0.0
        for i in range(zbins.size):
            Dabins[i]=D.Da(zbins[i])
            Dmodbins[i]=D.distance_modulus(zbins[i])
            volumebins[i]=D.volume(zbins[i])
        for i in range(z2bins.size):
            for j in range(z2bins.size):
                if j>i:
                    Da2bins[i,j]=D.Da(z2bins[i],z2bins[j])

        self.Da_spline=interpolate.splrep(zbins,Dabins)
        self.Dmod_spline=interpolate.splrep(zbins,Dmodbins)

        self.volume_spline=interpolate.splrep(zbins,volumebins)

        z2d=iT.coords((z2bins.size,z2bins.size))*self.dz2
        self.Da_bispline=interpolate.RectBivariateSpline(z2bins,z2bins,Da2bins)

        #pickle the splines
        splinedump=open("redshiftsplines.pkl","wb")
        cPickle.dump([self.Da_spline,self.Dmod_spline,self.volume_spline,self.Da_bispline],splinedump,2)
 def GetFits(self):
     bands = dict([('J0837','F606W'),('J0901','F606W'),('J0913','F555W'),('J1125','F606W'),('J1144','F606W'),('J1218','F606W'),('J1248','F555W'),('J1323','F555W'),('J1347','F606W'),('J1446','F606W'),('J1605','F555W'),('J1606','F606W'),('J1619','F606W'),('J2228','F606W')])
     #  not working yet - this will need us to have all the images somewhere, and all the xc,yc offsets!
     # get nnls values! Ideally, these should be already saved
     print "why didn't you save these before?!?"
     yc,xc = iT.coords(self.img1.shape)
     OVRS=1
     yo,xo=iT.overSample(self.img1.shape,OVRS)
     colours = [bands[self.name], 'F814W']
     models = []
     fits = []
     for i in range(len(self.imgs)):
         if i == 0:
             dx,dy = 0,0
         else:
             dx = self.Ddic['xoffset']
             dy = self.Ddic['yoffset']
         xp,yp = xc+dx+self.Dx,yc+dy+self.Dx
         xop,yop = xo+dy+self.Dy,yo+dy+self.Dy
         image = self.imgs[i]
         sigma = self.sigs[i]
         psf = self.PSFs[i]
         imin,sigin,xin,yin = image.flatten(), sigma.flatten(),xp.flatten(),yp.flatten()
         n = 0
         model = np.empty(((len(self.gals) + len(self.srcs)+1),imin.size))
         for gal in self.gals:
             gal.setPars()
             tmp = xc*0.
             tmp = gal.pixeval(xp,yp,1./OVRS,csub=11) # evaulate on the oversampled grid. OVRS = number of new pixels per old pixel.
             tmp = iT.resamp(tmp,OVRS,True) # convert it back to original size
             tmp = convolve.convolve(tmp,psf,False)[0]
             model[n] = tmp.ravel()
             n +=1
         for lens in self.lenses:
             lens.setPars()
             x0,y0 = pylens.lens_images(self.lenses,self.srcs,[xp,yp],1./OVRS,getPix=True)
         for src in self.srcs:
             src.setPars()
             tmp = xc*0.
             tmp = src.pixeval(x0,y0,1./OVRS,csub=11)
             tmp = iT.resamp(tmp,OVRS,True)
             tmp = convolve.convolve(tmp,psf,False)[0]
             model[n] = tmp.ravel()
             n +=1
         model[n] = np.ones(model[n].shape)
         n +=1
         rhs = (imin/sigin) # data
         op = (model/sigin).T # model matrix
         fit, chi = optimize.nnls(op,rhs)
         components = (model.T*fit).T.reshape((n,image.shape[0],image.shape[1]))
         model = components.sum(0)
         models.append(model)
         #SotPleparately(image,model,sigma,colours[i])
         #NotPlicely(image,model,sigma)
         comps = False
         if comps == True:
             CotSomponents(components,colours[i])
         fits.append(fit)
         self.fits = fits
def setupImageCoords(image,sig,psf,mask=None,regularizationmode='curvature'):
    import indexTricks as iT
    from imageSim import convolve
    from scipy.sparse import coo_matrix,diags
    import pixellatedTools
    import numpy
    inshape = image.shape
    image = image.flatten()
    sig = sig.flatten()
    ysize,xsize = inshape

    # There are two sets of coordinates in the image plane, one for the
    #  centers of pixels and one for the corners. This defines a mapping that
    #  describes which corners are associated with a center.
    yc,xc = iT.coords((ysize+1,xsize+1)) # Corners
    yc = yc.flatten()-0.5
    xc = xc.flatten()-0.5
    y0,x0 = iT.coords((ysize,xsize))     # Centers
    y0 = y0.flatten()
    x0 = x0.flatten()
    
    i = numpy.arange(x0.size)
    res = i%xsize
    p = i/xsize
    i = p*(xsize+1)+res
    indx = numpy.array([i,i+1,i+xsize+1,i+xsize+2]).T


    P = pixellatedTools.getPSFMatrix(psf,inshape)
    

    if mask!=None:
        image=image[mask]
        indx=None
        sig=sig[mask]
        yc=yc[mask]
        xc=xc[mask]
        y0=y0[mask]
        x0=x0[mask]
        P= P.tocsc()[:,list(mask)[0]]
        P= P[list(mask)[0],:]

    C = diags(1./sig**2,0)
    c= diags(sig**2,0)

    return image,sig,yc,xc,y0,x0,P,c,C,indx,inshape,mask
Example #13
0
def getPSFMatrix(psf, imshape, mask=None):
    """
    Create a PSF matrix given the PSF model and image dimensions
    """
    import numpy
    from scipy.sparse import coo_matrix
    import indexTricks as iT

    imsize = imshape[0] * imshape[1]

    y, x = iT.coords(psf.shape)
    x -= int(x.mean())
    y -= int(y.mean())
    c = psf.ravel() != 0
    x = x.ravel().astype(numpy.int32)[c]
    y = y.ravel().astype(numpy.int32)[c]
    Y, X = iT.coords(imshape)
    X = X.ravel().astype(numpy.int32)
    Y = Y.ravel().astype(numpy.int32)

    cols = X.repeat(x.size) + numpy.tile(x, X.size)
    rows = Y.repeat(y.size) + numpy.tile(y, Y.size)

    C = (cols >= 0) & (cols < imshape[1]) & (rows >= 0) & (rows < imshape[0])
    cols = cols[C]
    rows = rows[C]

    pvals = numpy.tile(psf.ravel()[c], imsize)[C]
    col = cols + rows * imshape[1]
    row = numpy.arange(imsize).repeat(c.sum())[C]

    cols = cols[pvals != 0]
    rows = rows[pvals != 0]
    pvals = pvals[pvals != 0]

    pmat = coo_matrix((pvals, (col, row)), shape=(imsize, imsize))
    if mask is not None:
        npnts = mask.sum()
        c = numpy.arange(imsize)[mask.ravel()]
        r = numpy.arange(npnts)
        smat = coo_matrix((numpy.ones(npnts), (c, r)), shape=(imsize, npnts))
        pmat = smat.T * (pmat * smat)
    return pmat
Example #14
0
def getStraight(img, ysoln, low, resamp=True, mode='constant'):
    yforw = ysoln[2]
    tmp = img.repeat(2, 0).repeat(2, 1)
    x = iT.coords(yforw.shape)[1]
    c = numpy.array([yforw + low * 2, x])
    X = ndimage.map_coordinates(rotcoords2[1], c)
    Y = ndimage.map_coordinates(rotcoords2[0], c)
    c = numpy.array([Y, X])
    tmp = ndimage.map_coordinates(tmp, c, mode=mode)
    if resamp == True:
        return iT.resamp(tmp, 2)
    return tmp
Example #15
0
def getPSFMatrix(psf,imshape,mask=None):
    """
    Create a PSF matrix given the PSF model and image dimensions
    """
    import numpy
    from scipy.sparse import coo_matrix
    import indexTricks as iT


    imsize = imshape[0]*imshape[1]

    y,x = iT.coords(psf.shape)
    x -= int(x.mean())
    y -= int(y.mean())
    c = psf.ravel()!=0
    x = x.ravel().astype(numpy.int32)[c]
    y = y.ravel().astype(numpy.int32)[c]
    Y,X = iT.coords(imshape)
    X = X.ravel().astype(numpy.int32)
    Y = Y.ravel().astype(numpy.int32)

    cols = X.repeat(x.size)+numpy.tile(x,X.size)
    rows = Y.repeat(y.size)+numpy.tile(y,Y.size)

    C = (cols>=0)&(cols<imshape[1])&(rows>=0)&(rows<imshape[0])
    cols = cols[C]
    rows = rows[C]

    pvals = numpy.tile(psf.ravel()[c],imsize)[C]
    col = cols+rows*imshape[1]
    row = numpy.arange(imsize).repeat(c.sum())[C]

    pmat = coo_matrix((pvals,(col,row)),shape=(imsize,imsize))
    if mask is not None:
        npnts = mask.sum()
        c = numpy.arange(imsize)[mask.ravel()]
        r = numpy.arange(npnts)
        smat = coo_matrix((numpy.ones(npnts),(c,r)),shape=(imsize,npnts))
        pmat = smat.T*(pmat*smat)
    return pmat
Example #16
0
def resampleData(data, owgrid, low, high, ysoln, xsoln, rwsoln):
    dy = (high - low) * 2
    ygrid = iT.coords((dy, owgrid.size))[0]
    xgrid = sf.genfunc(owgrid, 0., rwsoln).repeat(dy)
    xgrid = xgrid.reshape((owgrid.size, dy)).T
    xgrid = sf.genfunc(xgrid.flatten(), ygrid.flatten(), xsoln['forw'])
    xgrid = xgrid.reshape(ygrid.shape)
    c = numpy.array([ygrid, xgrid])
    ygrid = ndimage.map_coordinates(ysoln[2], c)
    c = numpy.array([ygrid + low * 2, xgrid])
    X = ndimage.map_coordinates(rotcoords2[1], c)
    Y = ndimage.map_coordinates(rotcoords2[0], c)
    c = numpy.array([Y, X])
    d = data.repeat(2, 0).repeat(2, 1)
    img = ndimage.map_coordinates(d, c, order=5)
    return iT.resamp(img, 2)
Example #17
0
    def __init__(self, surveyname, fractionofseeing=1):
        #-----------------------------------------------------
        ### Read in survey
        self.surveyname = surveyname
        survey = Survey(
            surveyname)  #This stores typical survey  in Surveys.Survey
        self.survey = survey
        self.pixelsize = survey.pixelsize
        self.side = survey.side
        self.readnoise = survey.readnoise
        self.nexposures = survey.nexposures
        self.f_sky = survey.f_sky

        self.bands = survey.bands
        self.strategy = survey.strategy
        self.strategyx = survey.strategyx

        self.exposuretimes = {}
        self.zeropoints = {}
        self.stochasticobservingdata = {}
        self.gains = {}
        self.seeing = {}
        self.psfscale = {}
        self.psf = {}
        self.psfFFT = {}

        self.ET = {}
        self.SB = {}

        for i in range(len(survey.bands)):
            self.exposuretimes[survey.bands[i]] = survey.exposuretimes[i]
            self.zeropoints[survey.bands[i]] = survey.zeropoints[i]
            self.gains[survey.bands[i]] = survey.gains[i]
            self.stochasticobservingdata[
                survey.bands[i]] = survey.stochasticobservingdata[i]
        self.zeroexposuretime = survey.zeroexposuretime
        #-----------------------------------------------------
        ###do some setup
        self.xl = (self.side - 1.) / 2.
        self.yl = (self.side - 1.) / 2.
        self.x, self.y = iT.coords((self.side, self.side))
        self.r2 = (self.x - self.xl)**2 + (self.y - self.yl)**2

        self.pixelunits = False

        #-----------------------------------------------------
        self.Reset()
Example #18
0
    def __init__(self, surveyname, fractionofseeing=1):
        #setting surveyname to be the surveyname and declaring the parameters to be the same
        ### Read in survey
        self.surveyName = surveyname
        survey = Survey(
            surveyname)  #This stores typical survey  in Surveys.Survey
        self.survey = Survey(surveyname)
        self.pixelsize = self.survey.pixelsize  #let self.pixelsixe be equal to the pixelsize of survey
        self.side = self.survey.side
        self.readnoise = self.survey.readnoise
        self.nexposures = self.survey.nexposures
        self.f_sky = self.survey.f_sky
        self.bands = self.survey.bands
        self.strategy = self.survey.strategy
        self.strategyx = self.survey.strategyx
        self.exposuretimes = {}
        self.zeropoints = {}
        self.stochasticobservingdata = {}
        self.gains = {}
        self.seeing = {}
        self.psfscale = {}
        self.psf = {}
        self.psfFFT = {}
        self.ET = {}
        self.SB = {}

        #for i in range of the length of bands in survey, set the parameters to self.parameter
        for i in range(len(survey.bands)):
            self.exposuretimes[survey.bands[i]] = survey.exposuretimes[i]
            self.zeropoints[survey.bands[i]] = survey.zeropoints[i]
            self.gains[survey.bands[i]] = survey.gains[i]
            self.stochasticobservingdata[
                survey.bands[i]] = survey.stochasticobservingdata[i]
        self.zeroexposuretime = survey.zeroexposuretime

        ###do some setup
        self.xl = (self.side - 1.) / 2.
        self.yl = (self.side - 1.) / 2.
        self.x, self.y = iT.coords(
            (self.side,
             self.side))  #creating an array of the sides coordinates
        self.r2 = (self.x - self.xl)**2 + (
            self.y -
            self.yl)**2  # r squared (r2) = distance between (x,y) and (xl, yl)
        self.pixelunits = False

        self.Reset()  #resets all parameters as seen in definition below
Example #19
0
    def __init__(self,surveyname,fractionofseeing=1):
        #-----------------------------------------------------
        ### Read in survey
        self.surveyname=surveyname
        survey=Survey(surveyname)#This stores typical survey  in Surveys.Survey
        self.survey=survey
        self.pixelsize=survey.pixelsize
        self.side=survey.side
        self.readnoise=survey.readnoise
        self.nexposures=survey.nexposures
        self.f_sky=survey.f_sky

        self.bands=survey.bands
        self.strategy=survey.strategy
        self.strategyx=survey.strategyx

        self.exposuretimes={}
        self.zeropoints={}
        self.stochasticobservingdata={}
        self.gains={}
        self.seeing={}
        self.psfscale={}
        self.psf={}
        self.psfFFT={}

        self.ET={}
        self.SB={}


        for i in range(len(survey.bands)):
            self.exposuretimes[survey.bands[i]]=survey.exposuretimes[i]
            self.zeropoints[survey.bands[i]]=survey.zeropoints[i]
            self.gains[survey.bands[i]]=survey.gains[i]
            self.stochasticobservingdata[survey.bands[i]]=survey.stochasticobservingdata[i]
        self.zeroexposuretime=survey.zeroexposuretime
        #-----------------------------------------------------
        ###do some setup
	self.xl=(self.side-1.)/2.
	self.yl=(self.side-1.)/2.
	self.x,self.y = iT.coords((self.side,self.side))
        self.r2 = (self.x-self.xl)**2+(self.y-self.yl)**2

        self.pixelunits=False

        #-----------------------------------------------------
        self.Reset()
def setupSourceGrid(sshape,pscale,scentre,mask=None,regularizationmode='curvature',R=None):
    import pixellatedTools
    import indexTricks as iT

    srcy,srcx = iT.coords(sshape)*pscale
    srcx -= srcx.mean()
    srcy -= srcy.mean()
    srcy += scentre[1]
    srcx += scentre[0]

    srcxaxis=srcx[0]
    srcyaxis=srcy[:,0]

    srcx = srcx.flatten()

    srcy = srcy.flatten()

    if R==None:
        R= pixellatedTools.getRegularizationMatrix(srcxaxis,srcyaxis,mode=regularizationmode)

    return srcx,srcy,srcxaxis,srcyaxis,R,pscale,sshape
    pl.title('model')
    pl.subplot(223)
    pl.imshow(image-im,origin='lower',interpolation='nearest',extent=ext,vmin=-0.25,vmax=0.25,cmap='afmhot',aspect='auto')
    pl.colorbar()
    pl.title('data-model')
    pl.subplot(224)
    pl.imshow((image-im)/sigma,origin='lower',interpolation='nearest',extent=ext,vmin=-5,vmax=5,cmap='afmhot',aspect='auto')
    pl.title('signal-to-noise residuals')
    pl.colorbar()
    pl.show()
    #pl.suptitle(str(V))
    #pl.savefig('/data/ljo31/Lens/TeXstuff/plotrun'+str(X)+'.png')

image = pyfits.open('/data/ljo31/Lens/J1347/J1347_med_cutout.fits')[0].data.copy()
sigma = pyfits.open('/data/ljo31/Lens/J1347/J137_med_sigma.fits')[0].data.copy()
yc,xc = iT.coords(image.shape)*0.8 # wide camera: 0.04 arcsec/pixel

# Model the PSF as a Moffat
xp,yp = iT.coords((100,100))-50
OVRS = 1

# load up model
det = np.load('/data/ljo31/Lens/J1347/emcee_FINAL_uncertainties_TWO')[2]
lp = np.load('/data/ljo31/Lens/J1347/emcee_FINAL_uncertainties_TWO')[0]
ii = np.where(lp==np.amax(lp))

g1,l1,s1,s2,sh = {},{},{},{},{}

srcs = []
gals = []
lenses = []
Example #22
0
    def lensASource(self, sourcenumber, bands):

        # src is created as a sourcenumber
        src = self.src[sourcenumber]

        # lens is a powerlaw instance of massmodel which includes x, y, q, pa, b, eta
        lens = massmodel.PowerLaw('lens', {}, {
            'x': self.xl + self.deltaxl,
            'y': self.yl + self.deltayl,
            'q': self.ql,
            'pa': 90 + self.deltap,
            'b': self.bl[sourcenumber],
            'eta': 1
        })

        # es is a ExtShear instance of massmodel. Not sure what ExtShear is
        es = massmodel.ExtShear('lens', {}, {
            'x': self.xl + self.deltaxl,
            'y': self.yl + self.deltayl,
            'pa': self.xp,
            'b': self.xb
        })

        # list lenses created including lens and es
        lenses = [lens, es]

        a = 51

        # makes array of ox, and oy which shows the shape of the array as a float
        ox, oy = iT.coords((a, a))

        # ps is a float which is calculated by taking the rs value and multiplying it by (10.0/a)
        ps = (self.rs[sourcenumber] * (10.0 / a))

        ox = (ox - (a - 1) / 2.0) * ps + (
            self.xSum[sourcenumber]
        )  # ox are arrays in which values are calculates as ox-(a-1)/2 + the xSum
        oy = (oy - (a - 1) / 2.0) * ps + (
            self.ySum[sourcenumber]
        )  # oy are arrays in which values are calculates as oy-(a-1)/2 + the ySum

        unlensedSourceModel = (src.pixeval(ox, oy, csub=5) *
                               (ps**2)).sum()  #sum of pixelvalues of souces
        sourceNorm = unlensedSourceModel.sum()  # sum of unlensedSourceModel
        unlensedSourceModel /= sourceNorm  # unlensedSourceModel = unlensedSourceModel / sourceNorm

        #creating a model for the lenses
        sourceModel = pylens.lens_images(lenses,
                                         src, [self.x, self.y],
                                         getPix=True,
                                         csub=5)[0]
        sourceModel[
            sourceModel < 0] = 0  # if the sourceModel is < 0,set it to be 0
        sourceModel /= sourceNorm  #sourceModel = sourceModel / sourceNorm

        #creating a magnification for each source
        self.magnification[sourcenumber] = (
            numpy.sum(numpy.ravel(sourceModel)) /
            numpy.sum(numpy.ravel(unlensedSourceModel)))
        sm = {}

        #for each band that is seen in the bands list:
        # unlensedTotalSourceFlux is calculated as flux of the a band with regards to the sourcenumber,
        # and that same band at the zeropoint
        # important to note:  flux = 10 ** (0.4 *( m2 - m1)), where m2 is the zeropoints
        #                  or fluw = 10 ** (-(m1 - m2) / 2.5)

        for band in bands:
            unlensedtotalsrcflux = 10**(
                -(self.ms[sourcenumber][band] - self.zeropoints[band]) / 2.5)
            sm[band] = sourceModel * unlensedtotalsrcflux

            if sm[band].max() > 0:

                # if the maximum value in sm[band] > 0, then the magnitude calculated
                # magnitude = m1/m2 ((m1/m2) from calculations of flux in comments above)

                self.totallensedsrcmag[sourcenumber][
                    band] = -2.5 * numpy.log10(
                        sm[band].sum()) + self.zeropoints[band]
            else:
                # else magnitude = 99
                self.totallensedsrcmag[sourcenumber][band] = 99
        return sm
Example #23
0
    pl.imshow(image-im,origin='lower',interpolation='nearest',extent=ext,vmin=-0.25,vmax=0.25,cmap='afmhot',aspect='auto')
    pl.colorbar()
    #pl.title('data-model')
    pl.subplot(224)
    pl.imshow((image-im)/sigma,origin='lower',interpolation='nearest',extent=ext,vmin=-5,vmax=5,cmap='afmhot',aspect='auto')
    #pl.title('signal-to-noise residuals')
    pl.colorbar()
    #pl.subplots_adjust(left=0.05,bottom=0.05,top=0.95,right=0.95)
    #pl.subplots_adjust(wspace=-0.1,hspace=0.05)
    #pl.suptitle(str(V))
    #pl.savefig('/data/ljo31/Lens/TeXstuff/plotrun'+str(X)+'.png')

image = pyfits.open('/data/ljo31/Lens/J1605/J1605_Kp_narrow_med_cutout.fits')[0].data.copy()
sigma = pyfits.open('/data/ljo31/Lens/J1605/J1605_Kp_narrow_med_sigma.fits')[0].data.copy()
#sigma = np.ones(sigma.shape)*sigma[0,0]
yc,xc = iT.coords(image.shape)*0.2

# Model the PSF as a Gaussian to start with. We'll do this over a grid of sigmas, and then maybe also ellitpicity and position anlge (will get kompliziert!!)
xp,yp = iT.coords((170,170))-85
OVRS = 1
guiFile = '/data/ljo31/Lens/J1605/terminal_iterated_4'
G,L,S,offsets,_ = numpy.load(guiFile)


pars = []
cov = []
srcs = []
for name in 'Source 1', 'Source 2':
    s = S[name]
    p = {}
    if name == 'Source 1':
# psf6
pars.append(pymc.Uniform('sigma 6',0.1,400,value= 8 )) 
pars.append(pymc.Uniform('q 6',0,1,value=0.9))
pars.append(pymc.Uniform('pa 6',-180,180,value= 90 ))
pars.append(pymc.Uniform('amp 6',0,1,value=0.15))
cov += [1,0.5,50,0.2]

psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':pars[2],'q':pars[3],'pa':pars[4],'amp':pars[5]})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':pars[6],'q':pars[7],'pa':pars[8],'amp':pars[9]})
psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':pars[10],'q':pars[11],'pa':pars[12],'amp':pars[13]})
psfObj4 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':pars[14],'q':pars[15],'pa':pars[16],'amp':pars[17]})
psfObj5 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':pars[18],'q':pars[19],'pa':pars[20],'amp':pars[21]})
psfObj6 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':pars[22],'q':pars[23],'pa':pars[24],'amp':pars[25]})
psfObjs = [psfObj1,psfObj2,psfObj3,psfObj4,psfObj5,psfObj6]

xpsf,ypsf = iT.coords((200,200))-100

gals = []
for name in ['Galaxy 1', 'Galaxy 2']:
    p = {}
    if name == 'Galaxy 1':
        for key in 'x','y','q','pa','re','n':
            p[key] = dic[name+' '+key][a1,a2,a3]
    elif name == 'Galaxy 2':
        for key in 'q','pa','re','n':
            p[key] = dic[name+' '+key][a1,a2,a3]
        for key in 'x','y':
            p[key] = gals[0].pars[key]
    gals.append(SBModels.Sersic(name,p))

lenses = []
Example #25
0
    def MomentChecks(self,cmax=2.7,cmin=0.5,vb=False):
        import moments
        import indexTricks as iT
        x,y=iT.coords(self.D.shape)
        x-=self.xl
        y-=self.yl
        x*=self.pixelsize
        y*=self.pixelsize


        goodlabels=[]
        unalignedlabels=[]

        for label in self.labels:
            D2=self.D*1
            D2[self.regions!=label]=0

            #plt.imshow(D2,interpolation="None")
            #plt.show(block=True)



            com,(a,b,q), eigvecs, flux,mean= moments.main(D2)

            va=eigvecs[:,1]

            ca=(com[0]-self.xl,com[1]-self.yl)
            com_dist=((ca[0]**2+ca[1]**2)**0.5)*self.pixelsize
            ca=ca/((ca[0]**2+ca[1]**2)**0.5)
            

            dp=ca[0]*va[0]+ca[1]*va[1]
            theta=(numpy.arccos(dp)*180/3.14159)#-90

            #print theta,self.thetain
            
            #plt.imshow(D2,interpolation="none")
            #plt.show(block=True)

            if com_dist < cmin:#arcseconds 
                if vb:print 21
                continue 
            if com_dist > cmax:#arcseconds 
                if vb:print 22
                continue 
            if b*self.pixelsize<0.2:#arcseconds 
                if vb:print 23
                continue
            if self.FluxCheck(flux,mean)==False:
                if vb:print 24
                continue

            if numpy.abs(theta) >30 and (theta<150 or theta >210):#degrees
                unalignedlabels.append(label)
            else:
                goodlabels.append(label)

            self.q=q

        self.goodlabels=goodlabels
        self.unalignedlabels=unalignedlabels
    pylab.subplot(222)
    pylab.imshow(oimg,origin='lower',interpolation='nearest',extent=ext,cmap='jet')
    pylab.colorbar()
    pylab.subplot(223)
    pylab.imshow((img-oimg)/sig,origin='lower',interpolation='nearest',extent=ext,cmap='jet')
    pylab.colorbar()
    pylab.subplot(224)
    pylab.imshow(osrc,origin='lower',interpolation='nearest',extent=ext2,cmap='jet')
    pylab.colorbar()
    return osrc

# Setup data for adaptive source modelling
img,sig,psf = galsub, sig2,psf2
#pl.figure()
#pl.imshow(img)
y,x = iT.coords(img.shape)
y,x=y+10,x+15
cpsf = convolve.convolve(img,psf)[1]
ifltm = img[mask]
sfltm = sig[mask]
vfltm = sfltm**2
cmatm = diags(1./sfltm,0)
xm = x[mask]
ym = y[mask]
coords = [xm,ym]

PSF = pT.getPSFMatrix(psf,img.shape)
PSFm = pT.maskPSFMatrix(PSF,mask)

iflt = img.flatten()
sflt = sig.flatten()
Example #27
0
flnprob = np.load('/data/ljo31/Lens/J1605/flnprob_PixFit_2src.npy')
fit = np.load('/data/ljo31/Lens/J1605/fit_PixFit_2src.npy')
ii = np.argmax(flnprob)
XX = fchain[ii]
x,y,pa,q,re,n,pa2,q2,re2,n2 = fchain[ii]
srcs = []
src1 = SBModels.Sersic('Source 1', {'x':0.1196461*x+10.91,'y':0.11966*y + 5.877,'pa':pa,'q':q,'re':re*0.1196461,'n':n,'amp':fit[0]})
src2 = SBModels.Sersic('Source 2', {'x':0.1196461*x+10.91,'y':0.11966*y + 5.877,'pa':pa2,'q':q2,'re':re2*0.1196461,'n':n2,'amp':fit[1]})
srcs = [src1,src2]


guiFile = '/data/ljo31/Lens/J1605/terminal_iterated_4'
G,L,S,offsets,shear = numpy.load(guiFile)
x0 = offsets[0][3]
y0 = offsets[1][3]
yc,xc = iT.coords(img2.shape)-15
yc,xc = yc+y0,xc+x0

cov = []
pars = []
gals = []
for name in 'Galaxy 1', 'Galaxy 2':
    s = G[name]
    p = {}
    if name == 'Galaxy 1':
        for key in 'x','y','q','pa','re','n':
            if s[key]['type']=='constant':
                p[key] = s[key]['value']
            else:
                lo,hi,val = s[key]['lower'],s[key]['upper'],s[key]['value']
                pars.append(pymc.Uniform('%s %s'%(name,key),lo,hi,value=val))
Example #28
0
    def GetPDFs(self,kpc=False):
        self.muPDF = []
        self.murestPDF = []
        self.RePDF = []
        self.magrestPDF = []
        self.LumPDF = []
        self.magPDF = []
        self.fitPDF = []
        self.bbandPDF = []
        OVRS=self.OVRS
        yo,xo = iT.coords(self.img1.shape)
        yc,xc=iT.overSample(self.img1.shape,OVRS)
        colours = [bands[self.name], 'F814W']
        for b1 in range(0,len(self.dictionaries),100):
            srcs,gals,lenses = [],[],[]
            for number in range(1,1+self.srcno):
                name = 'Source '+str(number)
                p = {}
                for key in 'q','re','n','pa':
                    p[key] = self.dictionaries[b1][name+' '+key]
                for key in 'x','y': # subtract lens potition - to be added back on later in each likelihood iteration!
                    p[key] = self.dictionaries[b1][name+' '+key]+self.dictionaries[b1]['Lens 1 '+key]
                srcs.append(SBBModels.Sersic(name,p))
            for number in range(1,1+self.galno):
                name = 'Galaxy '+str(number)
                p = {}
                for key in 'x','y','q','re','n','pa':
                    p[key] = self.dictionaries[b1][name+' '+key]
                gals.append(SBBModels.Sersic(name,p))
            p = {}
            for key in 'x','y','q','pa','b','eta':
                p[key] = self.dictionaries[b1]['Lens 1 '+key]
            lenses.append(MassModels.PowerLaw('Lens 1',p))
            p = {}
            p['x'] = lenses[0].pars['x']
            p['y'] = lenses[0].pars['y']
            p['b'] = self.dictionaries[b1]['extShear']
            p['pa'] = self.dictionaries[b1]['extShear PA']
            lenses.append(MassModels.ExtShear('shear',p))
            # fits
            fits = []
            for i in range(len(self.imgs)):
                if i == 0:
                    dx,dy = 0,0
                else:
                    dx = self.dictionaries[b1]['xoffset']
                    dy = self.dictionaries[b1]['yoffset']
                xp,yp = xc+dx+self.Dx,yc+dy+self.Dy
                xop,yop = xo+dy+self.Dx,yo+dy+self.Dy
                image = self.imgs[i]
                sigma = self.sigs[i]
                psf = self.PSFs[i]
                imin,sigin,xin,yin = image.flatten(), sigma.flatten(),xp.flatten(),yp.flatten()
                n = 0
                model = np.empty(((len(gals) + len(srcs)+1),imin.size))
                for gal in gals:
                    gal.setPars()
                    tmp = xc*0.
                    tmp = gal.pixeval(xp,yp,1./OVRS,csub=11) # evaulate on the oversampled grid. OVRS = number of new pixels per old pixel.
                    tmp = iT.resamp(tmp,OVRS,True) # convert it back to original size
                    tmp = convolve.convolve(tmp,psf,False)[0]
                    model[n] = tmp.ravel()
                    n +=1
                for lens in lenses:
                    lens.setPars()
                    x0,y0 = pylens.lens_images(lenses,srcs,[xp,yp],1./OVRS,getPix=True)
                for src in srcs:
                    src.setPars()
                    tmp = xc*0.
                    if 'boxiness' in self.dic.keys():
                        if src.name == 'Source 2':
                            #print 'this source has a boxy/disky component with c = ', '%.2f'%self.Ddic['boxiness'], '. I hope this is J1606!'
                            tmp = src.boxypixeval(x0,y0,1./OVRS,csub=11,c=self.Ddic['boxiness'])
                        else:
                            tmp = src.pixeval(x0,y0,1./OVRS,csub=11)
                    else:
                        tmp = src.pixeval(x0,y0,1./OVRS,csub=11)
                    tmp = iT.resamp(tmp,OVRS,True)
                    tmp = convolve.convolve(tmp,psf,False)[0]
                    model[n] = tmp.ravel()
                    if self.name == 'J0837':
                        #print "making J0837's dust lane..."
                        if src.name == 'Source 2':
                            model[n] *= -1
                    n +=1
                model[n] = np.ones(model[n].shape)
                n +=1
                rhs = (imin/sigin) # data
                op = (model/sigin).T # model matrix
                fit, chi = optimize.nnls(op,rhs)
                fits.append(fit)
            # source plane (observed) magnitudes
            if len(self.srcs)==1 or self.name == 'J0837':
                mag_v = srcs[0].getMag(fits[0][-2],self.ZPs[0])
                mag_i = srcs[0].getMag(fits[1][-2],self.ZPs[1])
            elif len(self.srcs)==2 and self.name != 'J0837':
                mv1,mv2 = srcs[0].getMag(fits[0][-3],self.ZPs[0]), srcs[1].getMag(fits[0][-2],self.ZPs[0])
                mi1,mi2 = srcs[0].getMag(fits[1][-3],self.ZPs[1]),srcs[1].getMag(fits[1][-2],self.ZPs[1]) 
                Fv = 10**(0.4*(self.ZPs[0]-mv1)) + 10**(0.4*(self.ZPs[0]-mv2))
                Fi = 10**(0.4*(self.ZPs[0]-mi1)) + 10**(0.4*(self.ZPs[0]-mi2))
                mag_v, mag_i = -2.5*np.log10(Fv) + self.ZPs[0], -2.5*np.log10(Fi) + self.ZPs[1]
            else:
                print 'how many sources do you want from me?'
            # intrinsic magnitudes
            if bands[self.name] == 'F555W':
                vband = MassB2
                bband = MassB2toB
            elif bands[self.name] == 'F606W':
                vband = MassB1
                bband = MassB1toB
            iband, kband = MassR, MassK
            Lv,mag_v_rest = vband(mag_v, self.z)
            Li,mag_i_rest = iband(mag_i, self.z)
            Lb,mag_b_rest = bband(mag_v,self.z)
            # sizes
            if self.srcno == 1 or self.name == 'J0837':
                Re_v, Re_i = srcs[0].pars['re']*0.05, srcs[0].pars['re']*0.05
            elif self.srcno == 2 and self.name != 'J0837':
                Xgrid = np.logspace(-4,5,1501)
                Ygrid = np.logspace(-4,5,1501)
                bandRes = []
                for i in range(len(self.imgs)):
                    source = fits[i][-3]*srcs[0].eval(Xgrid) + fits[i][-2]*srcs[1].eval(Xgrid)
                    R = Xgrid.copy()
                    light = source*2.*np.pi*R
                    mod = splrep(R,light,t=np.logspace(-3.8,4.8,1301))
                    intlight = np.zeros(len(R))
                    for i in range(len(R)):
                        intlight[i] = splint(0,R[i],mod)
                    model = splrep(intlight[:-300],R[:-300])
                    if len(model[1][np.where(np.isnan(model[1])==True)]>0):
                        #print 'here'
                        model = splrep(intlight[:-600],R[:-600])
                    reff = splev(0.5*intlight[-1],model)
                    bandRes.append(reff*0.05)
                Re_v,Re_i = bandRes
            # surface brightnesses
            mu_v_rest = mag_v_rest +  2.5*np.log10(2.*np.pi*Re_v**2.)
            mu_i_rest = mag_i_rest + 2.5*np.log10(2.*np.pi*Re_i**2.)
            mu_b_rest = mag_b_rest + 2.5*np.log10(2.*np.pi*Re_v**2.)
            mu_v = mag_v +  2.5*np.log10(2.*np.pi*Re_v**2.)
            mu_i = mag_i + 2.5*np.log10(2.*np.pi*Re_i**2.)
            self.muPDF.append([mu_v,mu_i])
            self.murestPDF.append([mu_v_rest,mu_i_rest,mu_b_rest])
	    if kpc:
		self.RePDF.append([Re_v*self.scale,Re_i*self.scale])
	    else:
		self.RePDF.append([Re_v,Re_i])
            self.magrestPDF.append([mag_v_rest,mag_i_rest,mag_b_rest])
            self.LumPDF.append([Lv,Li,Lb])
            self.magPDF.append([mag_v,mag_i])
            self.fitPDF.append(fits)
Example #29
0
   src = SBModels.Sersic('src',{'x':para[i,7],'y':para[i,8],'re':para[i,9],'q':para[i,10],'pa':para[i,11],'n':para[i,12]})
   mag_src = para[i,13]
   srcMag = src.Mag(zp)
   src.amp = 10.**(-0.4*(mag_src-srcMag))


# Create mass (or lensing) objects
   lensMass = MassModels.PowerLaw('lens',{'x':para[i,14],'y':para[i,15],'b':para[i,16],'q':para[i,17],'pa':para[i,18],'eta':para[i,19]})

   shear = MassModels.ExtShear('shear',{'x':para[i,20],'y':para[i,21],'b':para[i,22],'pa':para[i,23]})

   lenses = [lensMass,shear]

#create the coordinate grids that the image will be evaluated on
   y,x = iT.coords((240,240))   #with higher resolution 60*6

# create a PSF   
   import pyfits
   psf = pyfits.open('../fits/sub400.fits')[0].data.copy()
   psf /= psf.sum()

# Form the image and convolve (convolution returns the image and FFT'd PSF)
   img,psf = convolve.convolve(lensGal.pixeval(x,y),psf)
   
   xl,yl = pylens.getDeflections(lenses,[x,y])
   img +=convolve.convolve(src.pixeval(xl,yl),psf,False)[0]

   #pylab.imshow(img,origin='lower',interpolation='nearest')
   #pylab.colorbar()
   #pylab.show()
OVRS = 1
PVRS=2
yc,xc = iT.overSample(image.shape,OVRS)
yo,xo = iT.overSample(image.shape,1)
xc,xo,yc,yo=xc*0.2,xo*0.2,yc*0.2,yo*0.2
xc,xo = xc+12 , xo+12 
yc,yo = yc+20 , yo+20 
mask = np.zeros(image.shape)
tck = RectBivariateSpline(yo[:,0],xo[0],mask)
mask2 = tck.ev(xc,yc)
mask2[mask2<0.5] = 0
mask2[mask2>0.5] = 1
mask2 = mask2==0
mask = mask==0
xpsf,ypsf = iT.coords((340,340))-170
xpsf,ypsf = iT.overSample((340,340),PVRS) # oversampling
xpsf,ypsf=xpsf-170.,ypsf-170.
dx,dy,sig1,q1,pa1,amp1,sig2,q2,pa2 = trace[a1,a2,a3]
amp2=1.-amp1
gals = []
for name in G.keys():
    s = G[name]
    p = {}
    if name == 'Galaxy 1':
        for key in 'x','y','q','pa','re','n':
            p[key] =s[key]['value']
    elif name == 'Galaxy 2':
        for key in 'q','pa','re','n':
            p[key] =s[key]['value']
        for key in 'x','y':
Example #31
0

def lnprob(X, xc, yc, rhs):
    if np.any(X < 0):
        return -np.inf
    x, y, pa, q, re, n = X
    src = SBModels.Sersic("Source 1", {"x": x, "y": y, "pa": pa, "q": q, "re": re, "n": n})
    model = src.pixeval(xc, yc)
    fit = optimize.leastsq(func, 0.1, args=(model, rhs))[0].item()
    lnL = (-0.5 * (model * fit - rhs) ** 2.0).sum()
    # print lnL
    return lnL


osrc = np.load("/data/ljo31/Lens/J1605/osrc_F814W.npy")
yc, xc = iT.coords(osrc.shape)
ii = np.where(np.isnan(osrc) == False)
yc, xc = yc[ii], xc[ii]
rhs = osrc[ii].flatten()

# set up emcee
ndim, nwalkers = 6, 40  # for instance
p0 = np.zeros((nwalkers, ndim))

p0[:, 0] = 200 + np.random.randn(nwalkers) * 20  # x
p0[:, 1] = 200.0 + np.random.randn(nwalkers) * 20  # y
p0[:, 2] = 170.0 + np.random.randn(nwalkers) * 20  # pa
p0[:, 3] = 0.8 + np.random.randn(nwalkers) * 0.5  # q
p0[:, 4] = 25 + np.random.randn(nwalkers) * 3  # Re
p0[:, 5] = 4.0 + np.random.randn(nwalkers) * 1  # n


kresult = np.load('/data/ljo31/Lens/J2228/KeckPSF_final')
klp= kresult[0]
ka2=0
ka1,ka3 = numpy.unravel_index(klp[:,0].argmax(),klp[:,0].shape)
ktrace = kresult[1]
kdic = kresult[2]

imgs = [img]
sigs = [sig]

PSFs = []

xpsf,ypsf = iT.coords((201,201))-100.
xoffset,yoffset,sig1,q1,pa1,amp1,sig2,q2,pa2,amp2,sig3,q3,pa3,amp3,sig4,q4,pa4,amp4,sig5,q5,pa5,amp5 = ktrace[ka1,ka2,ka3]
psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':sig1,'q':q1,'pa':pa1,'amp':amp1})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':sig2,'q':q2,'pa':pa2,'amp':amp2})
psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':sig3,'q':q3,'pa':pa3,'amp':amp3})
psfObj4 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':sig4,'q':q4,'pa':pa4,'amp':amp4})
psfObj5 = SBObjects.Gauss('psf 5',{'x':0,'y':0,'sigma':sig5,'q':q5,'pa':pa5,'amp':amp5})

psf1 = psfObj1.pixeval(xpsf,ypsf)  / (np.pi*2.*sig1**2.)
psf2 = psfObj2.pixeval(xpsf,ypsf) / (np.pi*2.*sig2**2.)
psf3 = psfObj3.pixeval(xpsf,ypsf)  / (np.pi*2.*sig3**2.)
psf4 = psfObj4.pixeval(xpsf,ypsf)  / (np.pi*2.*sig4**2.)
psf5 = psfObj5.pixeval(xpsf,ypsf)  / (np.pi*2.*sig5**2.)

psf = psf1 + psf2 + psf3 + psf4 + psf5
#pl.figure()
def lnprob(X,xc,yc,rhs):
    x,y,pa,q,re,n,pa2,q2,re2,n2 = X
    if np.any(np.array([x,y,q,q2,n,n2,re,re2])<0) or n<0.1 or n2<0.1 or n>6 or n2>6 or re>300 or re2>300 or q>1 or q2>1 or pa>200 or pa2>200 or pa<-180 or pa2<-180:
        return -np.inf
    src = SBModels.Sersic('Source 1',{'x':x,'y':y,'pa':pa,'q':q,'re':re,'n':n})
    src2 = SBModels.Sersic('Source 2',{'x':x,'y':y,'pa':pa2,'q':q2,'re':re2,'n':n2})
    model1 = src.pixeval(xc,yc) 
    model2 = src2.pixeval(xc,yc)
    op = np.column_stack((model1,model2))
    fit = optimize.nnls(op,rhs)[0]
    lnL = (-0.5*(model1*fit[0] + model2*fit[1] - rhs)**2.).sum()
    #lnL = (-0.5*(np.dot(op,fit) -rhs)**2.).sum()
    return lnL

osrc = np.load('/data/ljo31/Lens/J1605/osrc_F814W.npy')
yc,xc = iT.coords(osrc.shape)
ii = np.where(np.isnan(osrc)==False)
yc,xc = yc[ii],xc[ii]
rhs = osrc[ii].flatten()

# set up emcee
ndim,nwalkers = 10,60 # for instance
p0 = np.zeros((nwalkers, ndim))

p0[:,0] = 220 +np.random.randn(nwalkers)*20 # x
p0[:,1] = 200. +np.random.randn(nwalkers)*20 # y
p0[:,2] = 0. + np.random.randn(nwalkers)*20 # pa
p0[:,3] = 0.7 + np.random.randn(nwalkers)*0.1 # q
p0[:,4] = 220 + np.random.randn(nwalkers)*20 # Re # 120
p0[:,5] = 4. + np.random.randn(nwalkers)*1 # n
p0[:,6] = 150. + np.random.randn(nwalkers)*20 # pa
Example #34
0
    return mod


def clip(arr, nsig=3.5):
    a = arr.flatten()
    a.sort()
    a = a[a.size / 20:a.size * 0.95]
    m, s, l = a.mean(), a.std(), a.size
    while 1:
        a = a[abs(a - m) < s * nsig]
        if a.size == l:
            return m, s
        m, s, l = a.mean(), a.std(), a.size


y, x = iT.coords((1200, 1200))
c = cos(-84.9 * pi / 180.)
s = sin(-84.9 * pi / 180.)
X = (x - 599.5) * c + (y - 599.5) * s + 511.5
Y = (599.5 - x) * s + (y - 599.5) * c + 511.5
rotcoords = numpy.array([Y, X])
y, x = iT.coords((1024, 1024))
c = cos(84.9 * pi / 180.)
s = sin(84.9 * pi / 180.)
X = (x - 511.5) * c + (y - 511.5) * s + 599.5
Y = (511.5 - x) * s + (y - 511.5) * c + 599.5
derotcoords = numpy.array([Y, X])
y, x = iT.coords((2400, 2400))
c = cos(-84.9 * pi / 180.)
s = sin(-84.9 * pi / 180.)
X = (x - 1199.5) * c + (y - 1199.5) * s + 1023.5
Example #35
0
    pl.colorbar()
    pl.title('model')
    pl.subplot(223)
    pl.imshow(image-im,origin='lower',interpolation='nearest',extent=ext,vmin=-0.25,vmax=0.25)
    pl.colorbar()
    pl.title('data-model')
    pl.subplot(224)
    pl.imshow((image-im)/sigma,origin='lower',interpolation='nearest',extent=ext,vmin=-5,vmax=5)
    pl.title('signal-to-noise residuals')
    pl.colorbar()
    #pl.suptitle(str(V))
    #pl.savefig('/data/ljo31/Lens/TeXstuff/plotrun'+str(X)+'.png')

image = pyfits.open('/data/ljo31/Lens/J1605/J1605_Kp_narrow_med_cutout.fits')[0].data.copy()
sigma = pyfits.open('/data/ljo31/Lens/J1605/J1605_Kp_narrow_med_sigma.fits')[0].data.copy()
yc,xc = iT.coords(image.shape)

# Model the PSF as a Gaussian to start with. We'll do this over a grid of sigmas, and then maybe also ellitpicity and position anlge (will get kompliziert!!)
xp,yp = iT.coords((50,50))-25
OVRS = 1

# we should get these from the iterated terminal version.
det = np.load('/data/ljo31/Lens/J1605/det8.npy')[()]
g1,g2,l1,s1,s2,sh = {},{},{},{},{},{}

coeff=[]
lenses = []
for name in det.keys():
    s = det[name]
    coeff.append(s[-1])
    if name[:8] == 'Source 1':
dic = result[2]

OVRS = 1
yc, xc = iT.overSample(image.shape, OVRS)
yo, xo = iT.overSample(image.shape, 1)
xc, xo, yc, yo = xc * 0.2, xo * 0.2, yc * 0.2, yo * 0.2
xc, xo = xc + 12, xo + 12
yc, yo = yc + 20, yo + 20
mask = np.zeros(image.shape)
tck = RectBivariateSpline(yo[:, 0], xo[0], mask)
mask2 = tck.ev(xc, yc)
mask2[mask2 < 0.5] = 0
mask2[mask2 > 0.5] = 1
mask2 = mask2 == 0
mask = mask == 0
xpsf, ypsf = iT.coords((310, 310)) - 165

dx, dy, sig1, q1, pa1, amp1, sig2 = trace[a1, a2, a3]
amp2 = 1.0 - amp1
pa2 = 0
gals = []
for name in G.keys():
    s = G[name]
    p = {}
    if name == "Galaxy 1":
        for key in "x", "y", "q", "pa", "re", "n":
            p[key] = s[key]["value"]
    elif name == "Galaxy 2":
        for key in "q", "pa", "re", "n":
            p[key] = s[key]["value"]
        for key in "x", "y":
Example #37
0
   src = SBModels.Sersic('src',{'x':para[i,7],'y':para[i,8],'re':para[i,9],'q':para[i,10],'pa':para[i,11],'n':para[i,12]})
   mag_src = para[i,13]
   srcMag = src.Mag(zp)
   src.amp = 10.**(-0.4*(mag_src-srcMag))


# Create mass (or lensing) objects
   lensMass = MassModels.PowerLaw('lens',{'x':para[i,14],'y':para[i,15],'b':para[i,16],'q':para[i,17],'pa':para[i,18],'eta':para[i,19]})

   shear = MassModels.ExtShear('shear',{'x':para[i,20],'y':para[i,21],'b':para[i,22],'pa':para[i,23]})

   lenses = [lensMass,shear]

#create the coordinate grids that the image will be evaluated on
   y,x = iT.coords((360,360))   #with higher resolution 60*6

# create a PSF   
   import pyfits
   psf = pyfits.open('sub600.fits')[0].data.copy()
   psf /= psf.sum()

# Form the image and convolve (convolution returns the image and FFT'd PSF)
   img,psf = convolve.convolve(lensGal.pixeval(x,y),psf)
   
   xl,yl = pylens.getDeflections(lenses,[x,y])
   img +=convolve.convolve(src.pixeval(xl,yl),psf,False)[0]

   #pylab.imshow(img,origin='lower',interpolation='nearest')
   #pylab.colorbar()
   #pylab.show()
# psf4
pars.append(pymc.Uniform('sigma 4',0.1,400,value= sig4 )) 
pars.append(pymc.Uniform('q 4',0,1,value=q4))
pars.append(pymc.Uniform('pa 4',-180,180,value= pa4 ))
pars.append(pymc.Uniform('amp 4',0,1,value=amp4))
cov += [1,0.5,50,0.5]


psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':pars[2],'q':pars[3],'pa':pars[4],'amp':pars[5]})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':pars[6],'q':pars[7],'pa':pars[8],'amp':pars[9]})
psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':pars[10],'q':pars[11],'pa':pars[12],'amp':pars[13]})
psfObj4 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':pars[14],'q':pars[15],'pa':pars[16],'amp':pars[17]})
psfObjs = [psfObj1,psfObj2,psfObj3,psfObj4]

xpsf,ypsf = iT.coords((101,101))-50

gals = []
for name in ['Galaxy 1', 'Galaxy 2']:
    p = {}
    if name == 'Galaxy 1':
        for key in 'x','y','q','pa','re','n':
            p[key] = dic[name+' '+key][a1,a2,a3]
    elif name == 'Galaxy 2':
        for key in 'q','pa','re','n':
            p[key] = dic[name+' '+key][a1,a2,a3]
        for key in 'x','y':
            p[key] = gals[0].pars[key]
    gals.append(SBModels.Sersic(name,p))

lenses = []
Example #39
0
    pl.colorbar()
    pl.title('model')
    pl.subplot(223)
    pl.imshow(image-im,origin='lower',interpolation='nearest',extent=ext,vmin=-0.25,vmax=0.25)
    pl.colorbar()
    pl.title('data-model')
    pl.subplot(224)
    pl.imshow((image-im)/sigma,origin='lower',interpolation='nearest',extent=ext,vmin=-5,vmax=5)
    pl.title('signal-to-noise residuals')
    pl.colorbar()
    #pl.suptitle(str(V))
    #pl.savefig('/data/ljo31/Lens/TeXstuff/plotrun'+str(X)+'.png')

image = pyfits.open('/data/ljo31/Lens/J1605/J1605_Kp_narrow_med_cutout.fits')[0].data.copy()
sigma = pyfits.open('/data/ljo31/Lens/J1605/J1605_Kp_narrow_med_sigma.fits')[0].data.copy()
yc,xc = iT.coords(image.shape)

# Model the PSF as a Gaussian to start with. We'll do this over a grid of sigmas, and then maybe also ellitpicity and position anlge (will get kompliziert!!)
xp,yp = iT.coords((150,150))-75
#print yp.shape,xp.shape
#sig=1 #4.
#psfObj = SBObjects.Gauss('psf',{'x':0,'y':0,'sigma':sig,'q':1,'pa':0,'amp':1})
#psf = psfObj.pixeval(xp,yp)
#psf /= psf.sum()
#psf = convolve.convolve(image,psf)[1]

OVRS = 1

# we should get these from the iterated terminal version.
det = np.load('/data/ljo31/Lens/J1605/det8.npy')[()]
g1,g2,l1,s1,s2,sh = {},{},{},{},{},{}
# psf4
pars.append(pymc.Uniform('sigma 4',0.1,400,value= 10 )) 
pars.append(pymc.Uniform('q 4',0,1,value=0.9))
pars.append(pymc.Uniform('pa 4',-180,180,value= 90 ))
pars.append(pymc.Uniform('amp 4',0,1,value=0.25))
cov += [1,0.5,50,0.5]


psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':pars[2],'q':pars[3],'pa':pars[4],'amp':pars[5]})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':pars[6],'q':pars[7],'pa':pars[8],'amp':pars[9]})
psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':pars[10],'q':pars[11],'pa':pars[12],'amp':pars[13]})
psfObj4 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':pars[14],'q':pars[15],'pa':pars[16],'amp':pars[17]})
psfObjs = [psfObj1,psfObj2,psfObj3,psfObj4]


xpsf,ypsf = iT.coords((221,221))-110

gals = []
for name in ['Galaxy 1', 'Galaxy 2']:
    p = {}
    if name == 'Galaxy 1':
        for key in 'q','pa','n':
            p[key] = dic[name+' '+key][a1,a2,a3]
        for key in 'x','y','re':
            p[key] = dic[name+' '+key][a1,a2,a3]*5.
    elif name == 'Galaxy 2':
        for key in 'q','pa','re','n':
            if key == 're':
                p[key] = dic[name+' '+key][a1,a2,a3]*5.
            else:
                p[key] = dic[name+' '+key][a1,a2,a3]
cov += [1,0.5,50,0.2]
# psf4
#pars.append(pymc.Uniform('sigma 4',0.1,400,value=kdic['sigma 4'][ka1,ka2,ka3]))
#pars.append(pymc.Uniform('q 4',0,1,value=kdic['q 4'][ka1,ka2,ka3]))
#pars.append(pymc.Uniform('pa 4',-180,180,value= kdic['pa 4'][ka1,ka2,ka3] ))
#pars.append(pymc.Uniform('amp 4',0,1,value=kdic['amp 4'][ka1,ka2,ka3]))
#cov += [1,0.5,50,0.2]

psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':pars[4],'q':pars[5],'pa':pars[6],'amp':pars[7]})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':pars[8],'q':pars[9],'pa':pars[10],'amp':pars[11]})
psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':pars[12],'q':pars[13],'pa':pars[14],'amp':pars[15]})
#psfObj4 = SBObjects.Gauss('psf 4',{'x':0,'y':0,'sigma':pars[16],'q':pars[17],'pa':pars[18],'amp':pars[19]})

psfObjs = [psfObj1,psfObj2,psfObj3]#,psfObj4]

xpsf,ypsf = iT.coords((81,81))-40
los = dict([('q',0.05),('pa',-180.),('re',0.1),('n',0.5)])
his = dict([('q',1.00),('pa',180.),('re',100.),('n',10.)])
covs = dict([('x',0.1),('y',0.1),('q',0.05),('pa',10.),('re',3.),('n',0.2)])
covlens = dict([('x',0.1),('y',0.1),('q',0.05),('pa',10.),('b',0.2),('eta',0.1)])
lenslos, lenshis = dict([('q',0.05),('pa',-180.),('b',0.5),('eta',0.5)]), dict([('q',1.00),('pa',180.),('b',100.),('eta',1.5)])
gals = []
for name in ['Galaxy 1', 'Galaxy 2']:
    p = {}
    if name == 'Galaxy 1':
        for key in 'x','y','q','pa','re','n':
            val = dic[name+' '+key][a1,a2,a3]
            if key == 'x' or key == 'y':
                lo,hi=val-10,val+10
            else:
                lo,hi= los[key],his[key]