Ejemplo n.º 1
0
def logP(value=0,p=pars):
    x0 = pars[0].value
    y0 = pars[1].value
    sig = pars[2].value
    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]
    
    g1a,g2a,s1a,s2a,l1a,sha = g1.copy(),g2.copy(),s1.copy(),s2.copy(),l1.copy(),sh.copy()
    g1a['x'],g2a['x'] = g1a['x']+x0,g2a['x']+x0
    g1a['y'],g2a['y'] = g1a['y']+y0,g2a['y']+y0
    s1a['x'] = g1a['x'] + 5*(s1a['x']-(g1a['x']-x0))
    s2a['x'] = g1a['x'] + 5*(s2a['x']-(g1a['x']-x0))
    s1a['y'] = g1a['y'] + 5*(s1a['y']-(g1a['y']-y0))
    s2a['y'] = g1a['y'] + 5*(s2a['y']-(g1a['y']-y0))
    l1a['x'] = g1a['x'] + 5*(l1a['x']-(g1a['x']-x0))
    l1a['y'] = g1a['y'] + 5*(l1a['y']-(g1a['y']-y0))
    s1a['re'],s2a['re'],g1a['re'],g2a['re'],l1a['b'],sha['b'] = s1a['re']*5,s2a['re']*5,g1a['re']*5,g2a['re']*5,l1a['b']*5,sha['b']*5 
    srcs = []
    gals = []
    lenses = []
    srcs.append(SBModels.Sersic('Source 1',s1a))
    srcs.append(SBModels.Sersic('Source 2',s2a))
    gals.append(SBModels.Sersic('Galaxy 1',g1a))
    gals.append(SBModels.Sersic('Galaxy 2',g2a))
    lenses.append(MassModels.PowerLaw('Lens 1',l1a))

    sha['x'] = lenses[0].pars['x']
    sha['y'] = lenses[0].pars['y']
    lenses.append(MassModels.ExtShear('shear',sha))
    return lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,1,
                                verbose=False,psf=psf,csub=1)
Ejemplo n.º 2
0
def logP(value=0,p=pars):
    x0 = pars[0].value
    y0 = pars[1].value
    sig1 = pars[2].value.item()
    #q1 = pars[3].value.item()
    #pa1 = pars[4].value.item()
    amp1 = pars[3].value.item()
    sig2 = pars[4].value.item()
    #q2 = pars[7].value.item()
    #pa2 = pars[8].value.item()
    amp2 = pars[5].value.item()
    sig3 = pars[6].value.item()
    #q3 = pars[11].value.item()
    #pa3 = pars[12].value.item()
    amp3 = pars[7].value.item()
    tamp = amp1+amp2+amp3
    q1,q2,q3 = 1,1,1
    pa1,pa2,pa3 = 0,0,0
    amp1,amp2,amp3 = amp1/tamp, amp2/tamp, amp3/tamp
    psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':sig1,'q':q1,'pa':pa1,'amp':10})
    psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':sig2,'q':q2,'pa':pa2,'amp':10})
    psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':sig3,'q':q3,'pa':pa3,'amp':10})
    psf1 = psfObj1.pixeval(xp,yp) * amp1 / (np.pi*2.*sig1**2.)
    psf2 = psfObj2.pixeval(xp,yp) * amp2 / (np.pi*2.*sig2**2.)
    psf3 = psfObj3.pixeval(xp,yp) * amp3 / (np.pi*2.*sig3**2.)
    psf = psf1 + psf2 + psf3
    psf /= psf.sum()
    psf = convolve.convolve(image,psf)[1]
    return lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,1,
                                verbose=False,psf=psf,csub=1)
Ejemplo n.º 3
0
def logP(value=0,p=pars):
    x0 = pars[0].value
    y0 = pars[1].value
    sig = pars[2].value.item()
    q = pars[3].value.item()
    pa = pars[4].value.item()
    psfObj = SBObjects.Gauss('psf',{'x':0,'y':0,'sigma':sig,'q':q,'pa':pa,'amp':10})
    psf = psfObj.pixeval(xp,yp)
    psf /= psf.sum()
    psf = convolve.convolve(image,psf)[1]
    return lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,1,
                                verbose=False,psf=psf,csub=1)
Ejemplo n.º 4
0
 def logP(value=0.,p=pars):
     lp = 0.
     for i in range(len(imgs)):
         if i==0:
             x0 = 0.
             y0 = 0.
         else:
             x0 = pars[i*2-2].value
             y0 = pars[i*2-1].value
         img = imgs[i]
         sig = sigs[i]
         psf = psfs[i]
         lp += lensModel.lensFit(None,img,sig,gals,lenses,srcs,xc[i]+x0,
                                 yc[i]+y0,1,verbose=False,psf=psf,
                                 mask=mask,csub=1)
     return lp
Ejemplo n.º 5
0
def logP(value=0,p=pars):
    x0 = pars[0].value
    y0 = pars[1].value
    sig1 = pars[2].value.item()
    q1 = pars[3].value.item()
    pa1 = pars[4].value.item()
    amp1 = pars[5].value.item()
    sig2 = pars[6].value.item()
    q2 = pars[7].value.item()
    pa2 = pars[8].value.item()
    amp2 = 1.-amp1
    psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':sig1,'q':q1,'pa':pa1,'amp':10})
    psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':sig2,'q':q2,'pa':pa2,'amp':10})
    psf1 = psfObj1.pixeval(xp,yp) * amp1 / (np.pi*2.*sig1**2.)
    psf2 = psfObj2.pixeval(xp,yp) * amp2 / (np.pi*2.*sig2**2.)
    psf = psf1 + psf2
    psf /= psf.sum()
    psf = convolve.convolve(image,psf)[1]
    return lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,1,
                                verbose=False,psf=psf,csub=1)
def logP(value=0,p=pars):
    x0 = pars[0].value
    y0 = pars[1].value
    fwhm1 = pars[2].value.item()
    q1 = pars[3].value.item()
    pa1 = pars[4].value.item()
    amp1 = pars[5].value.item()
    fwhm2 = pars[6].value.item()
    q2 = pars[7].value.item()
    pa2 = pars[8].value.item()
    index2 = pars[9].value.item()
    index1 = pars[10].value.item()
    amp2 = 1.-amp1
    psfObj1 = SBObjects.Moffat('psf 1',{'x':0,'y':0,'fwhm':fwhm1,'q':q1,'pa':pa1,'amp':10,'index':index1})
    psfObj2 = SBObjects.Moffat('psf 2',{'x':0,'y':0,'fwhm':fwhm2,'q':q2,'pa':pa2,'amp':10,'index':index2})
    psf1 = psfObj1.pixeval(xp,yp) * amp1 
    psf2 = psfObj2.pixeval(xp,yp) * amp2 
    psf = psf1 + psf2
    psf /= psf.sum()
    psf = convolve.convolve(image,psf)[1]
    return lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,1,
                                verbose=False,psf=psf,csub=1)
Ejemplo n.º 7
0
def logP(value=0, p=pars):
    x0 = pars[0].value
    y0 = pars[1].value
    sig1 = pars[2].value.item()
    q1 = pars[3].value.item()
    pa1 = pars[4].value.item()
    amp1 = pars[5].value.item()
    sig2 = pars[6].value.item()
    q2 = pars[7].value.item()
    pa2 = pars[8].value.item()
    # q2,pa2 = 1,0
    amp2 = 1.0 - amp1
    psfObj1 = SBObjects.Gauss("psf 1", {"x": 0, "y": 0, "sigma": sig1, "q": q1, "pa": pa1, "amp": 10})
    psfObj2 = SBObjects.Gauss("psf 2", {"x": 0, "y": 0, "sigma": sig2, "q": q2, "pa": pa2, "amp": 10})
    psf1 = psfObj1.pixeval(xp, yp) * amp1 / (np.pi * 2.0 * sig1 ** 2.0)
    psf2 = psfObj2.pixeval(xp, yp) * amp2 / (np.pi * 2.0 * sig2 ** 2.0)
    psf = psf1 + psf2
    psf /= psf.sum()
    psf = convolve.convolve(image, psf)[1]
    return lensModel.lensFit(
        None, image, sigma, gals, lenses, srcs, xc + x0, yc + y0, 1, verbose=False, psf=psf, csub=1
    )
Ejemplo n.º 8
0
def logP(value=0,p=pars):
    lp = 0.
    for i in range(3):
        #print i
        image = imgs[i]
        sigma = sigs[i]
        if i == 0:
            psf = PSFs[i]
            x0,y0 = 0,0
            xm,ym = xc,yc
            #print i, x0,y0
        if i == 1:
            psf = PSFs[i]
            x0,y0 = pars[9].value,pars[10].value
            #print i, x0,y0
            xm,ym = xc,yc
        elif i ==2:
            x0,y0 = pars[0].value,pars[1].value
            #print i,x0,y0
            sigma1 = pars[2].value.item()
            q1 = pars[3].value.item()
            pa1 = pars[4].value.item()
            amp1 = pars[5].value.item()
            sigma2 = pars[6].value.item()
            q2 = pars[7].value.item()
            pa2 = pars[8].value.item()
            amp2 = 1.-amp1
            psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':sigma1,'q':q1,'pa':pa1,'amp':10})
            psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':sigma2,'q':q2,'pa':pa2,'amp':10})
            psf1 = psfObj1.pixeval(xp,yp) * amp1 / (np.pi*2.*sigma1**2.)
            psf2 = psfObj2.pixeval(xp,yp) * amp2 / (np.pi*2.*sigma2**2.)
            psf = psf1 + psf2
            psf /= psf.sum()
            psf = convolve.convolve(image,psf)[1]
            xm,ym = xc3,yc3
        lp += lensModel.lensFit(None,image,sigma,gals,lenses,srcs,xm+x0,ym+y0,1,
                                verbose=False,psf=psf,csub=1)
       
    return lp
Ejemplo n.º 9
0
    def runInference(self,optCov=None,getModel=False):
        import numpy
        from SampleOpt import AMAOpt,levMar
        cov = [c for c in self.cov]
        pars = [o for o in self.offsets]
        gals = []
        srcs = []
        lenses = []

        self.gals = self.parent.galaxyManager.objs
        self.lenses = self.parent.lensManager.objs
        self.srcs = self.parent.srcManager.objs
        mask = self.parent.mask

        if len(self.gals)+len(self.srcs)==0:
            return None
#        if len(self.gals)+len(self.srcs)+len(self.lenses)==0:
#            return None
        for g in self.gals.keys():
            gal = self.gals[g]
            gal.makeModel()
            gal,gpars,gcov = gal.model,gal.modelPars,gal.cov
            gals.append(gal)
            pars += gpars
            cov += gcov
        for s in self.srcs.keys():
            src = self.srcs[s]
            src.makeModel()
            src,spars,scov = src.model,src.modelPars,src.cov
            srcs.append(src)
            pars += spars
            cov += scov
        for l in self.lenses.keys():
            lens = self.lenses[l]
            lens.makeModel()
            lens,lpars,lcov = lens.model,lens.modelPars,lens.cov
            lenses.append(lens)
            pars += lpars
            cov += lcov
        if self.parent.shearFlag==True:
            shear = self.parent.shear
            shear.makeModel()
            lenses.append(shear.model)
            pars += shear.modelPars
            cov += shear.cov

        if getModel==True or len(pars)==0:
            if len(pars)==0:
                self.outPars = []
            models = []
            for i in range(len(self.imgs)):
                if i==0:
                    x0 = 0.
                    y0 = 0.
                else:
                    x0 = pars[i*2-2].value
                    y0 = pars[i*2-1].value
                    print 'x0,y0',x0,y0
                img = self.imgs[i]
                sig = self.sigs[i]
                psf = self.psfs[i]
                xc = self.xc[i]
                yc = self.yc[i]
                model = lensModel.lensFit(None,img,sig,gals,lenses,srcs,xc+x0,
                                        yc+y0,1,verbose=False,psf=psf,
                                        noResid=True,csub=1)
                models.append(model)
            return models

        # Trim images for faster convolution if masking
        xc = []
        yc = []
        imgs = []
        sigs = []
        psfs = []
        if mask is not None:
            Y,X = numpy.where(mask)
            ylo,yhi,xlo,xhi = Y.min(),Y.max()+1,X.min(),X.max()+1
            mask = mask[ylo:yhi,xlo:xhi]
            for i in range(len(self.imgs)):
                xc.append(self.xc[i][ylo:yhi,xlo:xhi].copy())
                yc.append(self.yc[i][ylo:yhi,xlo:xhi].copy())
                imgs.append(self.imgs[i][ylo:yhi,xlo:xhi].copy())
                sigs.append(self.sigs[i][ylo:yhi,xlo:xhi].copy())
                if self.psfs[i] is not None:
                    PSF = self.psfImgs[i]
                    psfs.append(convolve.convolve(imgs[-1],PSF)[1])
        else:
            xc = [i for i in self.xc]
            yc = [i for i in self.yc]
            imgs = [i for i in self.imgs]
            sigs = [i for i in self.sigs]
            psfs = [i for i in self.psfs]

        @pymc.deterministic
        def logP(value=0.,p=pars):
            lp = 0.
            for i in range(len(imgs)):
                if i==0:
                    x0 = 0.
                    y0 = 0.
                else:
                    x0 = pars[i*2-2].value
                    y0 = pars[i*2-1].value
                img = imgs[i]
                sig = sigs[i]
                psf = psfs[i]
                lp += lensModel.lensFit(None,img,sig,gals,lenses,srcs,xc[i]+x0,
                                        yc[i]+y0,1,verbose=False,psf=psf,
                                        mask=mask,csub=1)
            return lp

        @pymc.observed
        def likelihood(value=0.,lp=logP):
            return lp

        def resid(p):
            lp = -2*logP.value
            return self.imgs[0].ravel()*0 + lp

        if optCov is None:
            optCov = numpy.array(cov)

        #S = levMar(pars,resid)
        #self.outPars = pars
        #return
        niter = 2*len(pars)**2
        if niter<20:
            niter = 20
        S = AMAOpt(pars,[likelihood],[logP],cov=optCov)
        S.set_minprop(10*len(pars))
        S.sample(niter)
        self.Sampler = S
        self.outPars = pars
        return self.getModel()
Ejemplo n.º 10
0
    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


# Setup data for adaptive source modelling
cpsf = convolve.convolve(img,psf)[1]
y,x = iT.coords(img.shape)-15
model = lensModel.lensFit(None,img,sig,gals,lenses,srcs,x,y,1,noResid=True,psf=cpsf,verbose=True,getModel=True,showAmps=True)
galaxy = model[0] + model[1]
img = img - galaxy
pl.figure()
pl.imshow(img,origin='lower',interpolation='nearest')
#pyfits.writeto('/data/ljo31/Lens/J1605/F555W_galsub.fits',img)
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)
Ejemplo n.º 11
0
amp3 = det['amp 3'][-1]
tamp = amp1+amp2+amp3
amp1,amp2,amp3 = amp1/tamp,amp2/tamp,amp3/tamp
q1,q2,q3 = 1,1,1
pa1,pa2,pa3 = 0,0,0
psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':sig1,'q':q1,'pa':pa1,'amp':10})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':sig2,'q':q2,'pa':pa2,'amp':10})
psfObj3 = SBObjects.Gauss('psf 3',{'x':0,'y':0,'sigma':sig3,'q':q3,'pa':pa3,'amp':10})

psf1 = psfObj1.pixeval(xp,yp) * amp1 / (2.*np.pi*sig1**2)
psf2 = psfObj2.pixeval(xp,yp) * amp2 / (2.*np.pi*sig2**2)
psf3 = psfObj3.pixeval(xp,yp) * amp3 / (2.*np.pi*sig3**2)

psf = psf1 + psf2 + psf3
psf /= psf.sum()
psf = convolve.convolve(image,psf)[1]
im = lensModel.lensFit(coeff,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,OVRS,noResid=True,psf=psf,verbose=True) # return model
model = lensModel.lensFit(coeff,image,sigma,gals,lenses,srcs,xc+x0,yc+y0,OVRS,noResid=True,psf=psf,verbose=True,getModel=True,showAmps=True)

NotPlicely(image,im,sigma)
pl.savefig('/data/ljo31/Lens/J1605/Keckpsf.eps')

for key in det.keys():
    print key, det[key][-1]

#print 'KECK - x & y & sigma 1 & sigma 2 & sigma 3 & pa 1 & pa 2 & pa 3 & q 1 & q 2 & q 3 & amp 1 & amp2 & amp3 \\'
#print '%.1f'%det['xoffset'][-1], '&', '%.1f'%det['yoffset'][-1], '&', '%.1f'%det['sigma 1'][-1], '&', '%.1f'%det['sigma 2'][-1], '&', '%.1f'%det['sigma 3'][-1], '&','%.1f'%det['pa 1'][-1], '&', '%.1f'%det['pa 2'][-1], '&','%.1f'%det['pa 3'][-1], '&', '%.1f'%det['q 1'][-1], '&', '%.1f'%det['q 2'][-1], '&', '%.1f'%det['q 3'][-1], '&','%.1f'%det['amp 1'][-1], '&', '%.1f'%det['amp 2'][-1], '&', '%.1f'%det['amp 3'][-1],'\\'

print 'KECK - x & y & sigma 1 & sigma 2 & sigma 3 & pa 1 & pa 2 & pa 3 & q 1 & q 2 & q 3 & amp 1 & amp2 & amp3 \\'
print '%.1f'%det['xoffset'][-1], '&', '%.1f'%det['yoffset'][-1], '&', '%.1f'%det['sigma 1'][-1], '&', '%.1f'%det['sigma 2'][-1], '&', '%.1f'%det['sigma 3'][-1], '&','%.1f'%det['amp 1'][-1], '&', '%.1f'%det['amp 2'][-1], '&', '%.1f'%det['amp 3'][-1],'\\'
Ejemplo n.º 12
0
sig1 = det['sigma 1'][-1]
q1 = det['q 1'][-1]
pa1 = det['pa 1'][-1]
amp1 = det['amp 1'][-1]
sig2 = det['sigma 2'][-1]
q2 = det['q 2'][-1]
pa2 = det['pa 2'][-1]
amp2 = 1.-amp1
psfObj1 = SBObjects.Gauss('psf 1',{'x':0,'y':0,'sigma':sig1,'q':q1,'pa':pa1,'amp':10})
psfObj2 = SBObjects.Gauss('psf 2',{'x':0,'y':0,'sigma':sig2,'q':q2,'pa':pa2,'amp':10})
psf1 = psfObj1.pixeval(xp,yp) * amp1 / (2.*np.pi*sig1**2)
psf2 = psfObj2.pixeval(xp,yp) * amp2 / (2.*np.pi*sig2**2)
psf = psf1 + psf2
psf /= psf.sum()
psf = convolve.convolve(img3,psf)[1]
im = lensModel.lensFit(None,img3,sigma,gals,lenses,srcs,xc3+x0,yc3+y0,OVRS,noResid=True,psf=psf,verbose=True) # return model
model = lensModel.lensFit(None,img3,sigma,gals,lenses,srcs,xc3+x0,yc3+y0,OVRS,noResid=True,psf=psf,verbose=True,getModel=True,showAmps=True)

NotPlicely(img3,im,sigma,8)

ims = []
models = []
for i in range(len(PSFs)):
    image = imgs[i]
    sigma = sigs[i]
    psf = PSFs[i]
    if i == 0:
        x0,y0 = 0,0
    else:
        x0,y0 = det['xoffset'][-1], det['yoffset'][-1] # xoffset, yoffset #
        print x0,y0