コード例 #1
0
    def ObserveLens(self, noisy=True, bands=[]):
        if bands == []:
            bands = self.bands  # if bands exists, then bands is set to be the bands

        for band in bands:
            if self.seeing[band] == 0:
                # convolvedGalaxy and the psfForwardFourierTransform is the convolution of the galaxyModel and the psf from convolve.py and edgeCheck is True
                convolvedGalaxy, self.psfFFT[band] = convolve.convolve(
                    self.galModel[band], self.psf[band], True)

                convolvedGalaxy[convolvedGalaxy < 0] = 0
                self.convolvedGal[band] = convolvedGalaxy

                convolvedmodel = convolvedGalaxy * 1
                convolvedsrc = {}

                for sourcenumber in self.sourcenumbers:
                    convolvedsrc[sourcenumber] = convolve.convolve(
                        self.sourceModel[sourcenumber][band],
                        self.psfFFT[band], False)[0]
                    convolvedsrc[sourcenumber][
                        convolvedsrc[sourcenumber] < 0] = 0
                    self.convolvedsrc[sourcenumber][band] = convolvedsrc[
                        sourcenumber]
                    convolvedmodel += convolvedsrc[sourcenumber]

                self.zeroMagCounts[band] = (10**(-(0 - self.zeropoints[band]) /
                                                 2.5))

                exposurecorrection = (
                    (self.ET[band] * 1. /
                     self.zeroexposuretime)) * self.gains[band]
                convolvedmodel *= exposurecorrection

                #skybackground per second per square arcsecond
                background = (10**(-(self.SB[band] - self.zeropoints[band]) /
                                   2.5)) * (self.pixelsize**2)
                tot_bg = background * exposurecorrection

                sigma = ((convolvedmodel + tot_bg) + self.nexposures *
                         (self.readnoise**0.5)**2)**.5

                fakeLens = convolvedmodel * 1.
                if noisy:
                    fakeLens += (numpy.random.randn(self.side, self.side) *
                                 (sigma))

                #convert back to ADU/second:
                fakeLens /= exposurecorrection
                sigma /= exposurecorrection

                self.image[band] = fakeLens * 1
                self.fakeLens[band] = fakeLens * 1
                self.sigma[band] = sigma * 1
                self.fakeResidual[0][band] = fakeLens - convolvedGalaxy
                for sourcenumber in self.sourcenumbers:
                    self.SN[sourcenumber][band]=self.SNfunc(\
                        convolvedsrc[sourcenumber],sigma)
                    self.fakeResidual[sourcenumber][band]=\
                        fakeLens-convolvedmodel+convolvedsrc[sourcenumber]
コード例 #2
0
ファイル: pyplz_offsetpos.py プロジェクト: langeroodi/pyplz
    def optimize_amp(self):

        chi2sum = 0.

        for band in self.bands:

            lens_models = []
            for lens_model in self.lens_models:
                lens_models.append(lens_model[band])
            xl, yl = pylens.getDeflections(lens_models, (self.X, self.Y))

            lpix = []
            spix = []

            for light in self.light_sb_models:
                light[band].amp = 1.
                lpix.append(light[band].pixeval(self.X, self.Y))

            for source in self.source_sb_models:
                source[band].amp = 1.
                spix.append(source[band].pixeval(xl, yl))

            modlist = []
            for l in lpix:
                modlist.append(
                    (convolve.convolve(l, self.convol_matrix[band], False)[0] /
                     self.err[band]).ravel()[self.mask_r])
            for s in spix:
                modlist.append(
                    (convolve.convolve(s, self.convol_matrix[band], False)[0] /
                     self.err[band]).ravel()[self.mask_r])

            modarr = np.array(modlist).T
            if np.isnan(modarr).any() or not np.isfinite(modarr).any():
                amps = np.ones(self.nlight + self.nsource)
                chi = 1e300
            else:
                amps, chi = nnls(modarr, (self.sci[band] /
                                          self.err[band]).ravel()[self.mask_r])
            chi2sum += chi**2

            i = 0
            for light, mags in zip(self.light_sb_models, self.light_mags):
                if amps[i] > 0.:
                    light[band].amp = amps[i]
                    mags[band] = light[band].Mag(self.zp[band])
                else:
                    mags[band] = 99.
                i += 1

            for source, mags in zip(self.source_sb_models, self.source_mags):
                if amps[i] > 0.:
                    source[band].amp = amps[i]
                    mags[band] = source[band].Mag(self.zp[band])
                else:
                    mags[band] = 99.
                i += 1

        self.logp = -0.5 * chi2sum
        return chi2sum
コード例 #3
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    for i in range(len(imgs)):
        image, sigma,scale = imgs[i],sigs[i],scales[i]
        if i == 0:
            dx,dy = 0,0
            xp,yp = xc+dx,yc+dy
            psf = PSFs[i]
        elif i ==1:
            dx = pars[0].value 
            dy = pars[1].value 
            xp,yp = xc+dx,yc+dy
            psf = PSFs[i]
        elif i ==2:
            dx = pars[2].value 
            dy = pars[3].value
            xp,yp = xck+dx,yck+dy
            psf = xpsf*0.
            for obj in psfObjs:
                obj.setPars()
                psf += obj.pixeval(xpsf,ypsf) / (np.pi*2.*obj.pars['sigma'].value**2.)
            psf = psf/psf.sum()
            psf = convolve.convolve(image,psf)[1]
        mask,mask2=np.ones(image.shape),np.ones(image.shape)
        mask,mask2=mask==1,mask2==1
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(gals) + len(srcs)+1),imin.size))
        for gal in gals:
            gal.setPars()
            #print gal.pars['x'].value, gal.pars['y'].value,gal.pars['q'].value, gal.pars['pa'].value,gal.pars['re'].value, gal.pars['n'].value
            tmp = xp*0.
            tmp[mask2] = gal.pixeval(xin,yin,scale/OVRS,csub=1) 
            tmp = iT.resamp(tmp,OVRS,True) 
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        for lens in lenses:
            lens.setPars()
        x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
        for src in srcs:
            src.setPars()
            #print src.pars['x'].value, src.pars['y'].value,src.pars['q'].value, src.pars['pa'].value,src.pars['re'].value, src.pars['n'].value
            tmp = xp*0.
            tmp[mask2] = src.pixeval(x0,y0,scale/OVRS,csub=1)
            tmp = iT.resamp(tmp,OVRS,True)
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        model[n] = np.ones(model[n-1].shape)
        rhs = (imin/sigin) 
        op = (model/sigin).T 
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp 
コード例 #4
0
 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
コード例 #5
0
ファイル: FastLensSim.py プロジェクト: peterrwi/LensPop
    def ObserveLens(self, noisy=True, bands=[]):
        if bands == []: bands = self.bands
        for band in bands:
            if self.seeing[band] != 0:
                convolvedgal, self.psfFFT[band] = convolve.convolve(
                    self.galmodel[band], self.psf[band], True)
                convolvedgal[convolvedgal < 0] = 0
                self.convolvedgal[band] = convolvedgal

                convolvedmodel = convolvedgal * 1

                convolvedsrc = {}

                for sourcenumber in self.sourcenumbers:
                    convolvedsrc[sourcenumber] = convolve.convolve(
                        self.sourcemodel[sourcenumber][band],
                        self.psfFFT[band], False)[0]
                    convolvedsrc[sourcenumber][
                        convolvedsrc[sourcenumber] < 0] = 0
                    self.convolvedsrc[sourcenumber][band] = convolvedsrc[
                        sourcenumber]
                    convolvedmodel += convolvedsrc[sourcenumber]

                self.zeromagcounts[band] = (10**(-(0 - self.zeropoints[band]) /
                                                 2.5))

                exposurecorrection = (
                    (self.ET[band] * 1. /
                     self.zeroexposuretime)) * self.gains[band]
                convolvedmodel *= exposurecorrection

                #skybackground per second per square arcsecond
                background = (10**(-(self.SB[band] - self.zeropoints[band]) /
                                   2.5)) * (self.pixelsize**2)
                tot_bg = background * exposurecorrection

                sigma = ((convolvedmodel + tot_bg) + self.nexposures *
                         (self.readnoise**0.5)**2)**.5

                fakeLens = convolvedmodel * 1.
                if noisy:
                    fakeLens += (numpy.random.randn(self.side, self.side) *
                                 (sigma))

                #convert back to ADU/second:
                fakeLens /= exposurecorrection
                sigma /= exposurecorrection

                self.image[band] = fakeLens * 1
                self.fakeLens[band] = fakeLens * 1
                self.sigma[band] = sigma * 1
                self.fakeResidual[0][band] = fakeLens - convolvedgal
                for sourcenumber in self.sourcenumbers:
                    self.SN[sourcenumber][band]=self.SNfunc(\
                        convolvedsrc[sourcenumber],sigma)
                    self.fakeResidual[sourcenumber][band]=\
                        fakeLens-convolvedmodel+convolvedsrc[sourcenumber]
コード例 #6
0
ファイル: Keck_J1144_X3.py プロジェクト: lindzlebean/pylathon
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    dx = pars[0].value
    dy = pars[1].value 
    xp,yp = xc+dx,yc+dy
    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 = 1.#pars[7].value.item()
    pa2 = 0#pars[7].value.item()
    amp2 = pars[7].value.item()
    sig3 = pars[8].value.item()
    q3,pa3 = 1.,0.
    amp3 = 1.-amp1-amp2
    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(xpsf,ypsf) * amp1 / (np.pi*2.*sig1**2.)
    psf2 = psfObj2.pixeval(xpsf,ypsf) * amp2 / (np.pi*2.*sig2**2.)
    psf3 = psfObj3.pixeval(xpsf,ypsf) * amp3 / (np.pi*2.*sig3**2.)
    psf = psf1 + psf2 + psf3
    psf /= psf.sum()
    psf = convolve.convolve(image,psf)[1]
    imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
    n = 0
    model = np.empty(((len(gals) + len(srcs)+1),imin.size))
    for gal in gals:
        gal.setPars()
        tmp = xc*0.
        tmp[mask2] = gal.pixeval(xin,yin,1./OVRS,csub=1) # 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[mask].ravel()
        n +=1
    for lens in lenses:
        lens.setPars()
    x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
    for src in srcs:
        src.setPars()
        tmp = xc*0.
        tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=1)
        tmp = iT.resamp(tmp,OVRS,True)
        tmp = convolve.convolve(tmp,psf,False)[0]
        model[n] = tmp[mask].ravel()
        n +=1
    model[n] = np.ones(model[n-1].shape)
    rhs = (imin/sigin) # data
    op = (model/sigin).T # model matrix
    fit, chi = optimize.nnls(op,rhs)
    model = (model.T*fit).sum(1)
    resid = (model-imin)/sigin
    lp = -0.5*(resid**2.).sum()
    return lp 
コード例 #7
0
    def get_chi2(self):

        light_ind_model = []
        source_ind_model = []

        xl, yl = pylens.getDeflections(self.lens_models, (self.X, self.Y))

        model_array = (0. * self.scistack).ravel()[self.maskstack_r]

        for light, sed in zip(self.light_sb_models, self.light_sed_models):
            lmodel = 0. * self.scistack
            light.setAmpFromMag(sed.mags[self.main_band],
                                self.zp[self.main_band])
            if light.__class__.__name__ == 'PointSource':
                for i in range(self.nbands):
                    scale = sed.scale(self.bands[i], self.main_band)
                    lmodel[i * self.ny:(i + 1) * self.ny, :] = light.pixeval(
                        self.X, self.Y, self.bands[i])
            else:
                lpix = light.pixeval(self.X, self.Y)
                for i in range(self.nbands):
                    scale = sed.scale(self.bands[i], self.main_band)
                    lmodel[i * self.ny:(i + 1) *
                           self.ny, :] = scale * convolve.convolve(
                               lpix, self.convol_matrix[self.bands[i]],
                               False)[0]
            model_array += lmodel.ravel()[self.maskstack_r]

        for source, sed in zip(self.source_sb_models, self.source_sed_models):
            smodel = 0. * self.scistack
            source.setAmpFromMag(sed.mags[self.main_band],
                                 self.zp[self.main_band])
            spix = source.pixeval(xl, yl)
            for i in range(self.nbands):
                scale = sed.scale(self.bands[i], self.main_band)
                smodel[i * self.ny:(i + 1) *
                       self.ny, :] = scale * convolve.convolve(
                           spix, self.convol_matrix[self.bands[i]], False)[0]
            model_array += smodel.ravel()[self.maskstack_r]

        if self.sky is not None:
            skymodel = 0. * self.scistack
            for i in range(self.nbands):
                skymodel[i * self.ny:(i + 1) *
                         self.ny, :] = self.sky['amp_%s' % self.bands[i]].value
            model_array += skymodel.ravel()[self.maskstack_r]

        if np.isnan(model_array).any() or not np.isfinite(model_array).any():
            chi2 = 1e300
        else:
            chi2 = (
                (model_array - self.scistack.ravel()[self.maskstack_r])**2 /
                (self.errstack.ravel()[self.maskstack_r])**2).sum()

        self.logp = -0.5 * chi2
コード例 #8
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
            xp,yp = xc+dx,yc+dy
            OVRS=3
        elif i ==1:
            dx = pars[0].value 
            dy = pars[1].value 
            xp,yp = xc+dx,yc+dy
            OVRS=3
        elif i ==2:
            dx = pars[0].value+pars[2].value 
            dy = pars[1].value +pars[3].value
            xp,yp = xck+dx,yck+dy
            OVRS=1
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        mask,mask2=np.ones(image.shape),np.ones(image.shape)
        mask,mask2=mask==1,mask2==1
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(gals) + len(srcs)+1),imin.size))
        for gal in gals:
            gal.setPars()
            tmp = xp*0.
            tmp[mask2] = gal.pixeval(xin,yin,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[mask].ravel()
            n +=1
        for lens in lenses:
            lens.setPars()
        x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
        for src in srcs:
            src.setPars()
            tmp = xp*0.
            tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=11)
            tmp = iT.resamp(tmp,OVRS,True)
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].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)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp #,models
コード例 #9
0
def logP(value=0.,p=pars):
    # read in sources - I don't think this is the best way, but...
    xll,yll = pylens.getDeflections(lenses,[xind,yind])
    xpeak = np.sum(xll*imgind)/np.sum(imgind)
    ypeak = np.sum(yll*imgind)/np.sum(imgind)
    #print 'xpeak,ypeak',xpeak, ypeak
    if len(srcs)==2:
        srcs[0] = SBModels.Sersic('Source 2',{'x':dics[0]['x']+xpeak,'y':dics[0]['y']+ypeak,'pa':dics[0]['pa'],'q':dics[0]['q'],'re':dics[0]['re'],'n':dics[0]['n']})
        srcs[1] = SBModels.Sersic('Source 1',{'x':dics[0]['x']+xpeak,'y':dics[0]['y']+ypeak,'pa':dics[1]['pa'],'q':dics[1]['q'],'re':dics[1]['re'],'n':dics[1]['n']})
    else:
        srcs[0] = SBModels.Sersic('Source 1',{'x':dics[0]['x']+xpeak,'y':dics[0]['y']+ypeak,'pa':dics[0]['pa'],'q':dics[0]['q'],'re':dics[0]['re'],'n':dics[0]['n']})
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
        else:
            dx = pars[0].value 
            dy = pars[1].value 
        xp,yp = xc+dx,yc+dy
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(gals) + len(srcs)),imin.size))
        for gal in gals:
            gal.setPars()
            tmp = xc*0.
            tmp[mask2] = gal.pixeval(xin,yin,1./OVRS,csub=1) # 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[mask].ravel()
            n +=1
        for lens in lenses:
            lens.setPars()
        x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
        for src in srcs:
            src.setPars()
            tmp = xc*0.
            tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=1)
            tmp = iT.resamp(tmp,OVRS,True)
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        rhs = (imin/sigin) # data
        op = (model/sigin).T # model matrix
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp #,models
コード例 #10
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
        else:
            dx = pars[0].value 
            dy = pars[1].value 
        xp,yp = xc+dx,yc+dy
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(gals) + len(srcs)+1),imin.size))
        galaxy = np.zeros(imin.size)
        for gal in gals:
            gal.setPars()
            tmp = xc*0.
            tmp[mask2] = gal.pixeval(xin,yin,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[mask].ravel()
            if gal.pars['q'].value<0.35:
                galaxy += gal.pixeval(xin,yin,1,csub=11)
            n +=1
        for lens in lenses:
            lens.setPars()
        x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
        for src in srcs:
            src.setPars()
            tmp = xc*0.
            tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=11)
            tmp = iT.resamp(tmp,OVRS,True)
            b,std,xmid,ymid = lenses[0].pars['b'].value, lenses[0].pars['b'].value*0.5,lenses[0].pars['x'].value,lenses[0].pars['y'].value
            r = np.sqrt((xin-xmid)**2. + (yin-ymid)**2.)
            mask3 = np.where((r>b-std) & (r<b+std) & (galaxy>=0.05*np.amax(galaxy)))
            tmp.ravel()[mask3] -= pars[-1].value#*galaxy[mask3]
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        model[n] = -1*np.ones(model[n-1].shape)
        n+=1
        ### if galaxy light > X, 
        rhs = (imin/sigin) # data
        op = (model/sigin).T # model matrix
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp #,models
コード例 #11
0
ファイル: lensModel.py プロジェクト: tcollett/pylens
def lensModel(inpars,
              image,
              sig,
              gals,
              lenses,
              sources,
              xc,
              yc,
              OVRS=1,
              csub=11,
              psf=None,
              noResid=False,
              verbose=False):
    import pylens, numpy
    import indexTricks as iT

    model = xc * 0.
    for gal in gals:
        gal.setPars(inpars)
        model += gal.pixeval(xc, yc, 1. / OVRS, csub=csub)

    for src in sources:
        src.setPars(inpars)

    for lens in lenses:
        lens.setPars(inpars)

    model = model + pylens.lens_images(lenses, sources, [xc, yc], 1. / OVRS)
    if numpy.isnan(model.sum()):
        if verbose == True:
            print 'nan model'
        return -1e300

    if OVRS > 1:
        model = iT.resamp(model, OVRS, True)

    if psf is not None:
        from imageSim import convolve
        global psfFFT
        if psfFFT is None:
            psf /= psf.sum()
            model, psfFFT = convolve.convolve(model, psf)
        else:
            model, psfFFT = convolve.convolve(model, psfFFT, False)

    if noResid is True:
        return model
    resid = ((model - image) / sig).ravel()
    if verbose == True:
        print "%f  %5.2f %d %dx%d" % (
            (resid**2).sum(), (resid**2).sum() / resid.size, resid.size,
            image.shape[1], image.shape[0])
    return -0.5 * (resid**2).sum()
コード例 #12
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
            xp,yp = xc,yc
        else:
            dx = pars[0].value 
            dy = pars[1].value 
            xp,yp = (xck+dx)*0.2,(yck+dy)*0.2
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        mask = np.ones(image.shape)
        mask = mask==1
        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.
            if gal.pars['q'].value<0.3:
                tmp = gal.boxypixeval(xp,yp,1./OVRS,csub=11,c=pars[-1].value)
            else:
                tmp = gal.boxypixeval(xp,yp,1./OVRS,csub=11)
            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 = xp*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-1].shape)
        n+=1
        rhs = (imin/sigin) # data
        op = (model/sigin).T # model matrix
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp #,models
コード例 #13
0
def logP(value=0.0, p=pars):
    lp = 0.0
    models = []
    dx = pars[0].value
    dy = pars[1].value
    xp, yp = xc + dx, yc + dy
    psf = xpsf * 0.0
    for obj in psfObjs:
        obj.setPars()
        psf += obj.pixeval(xpsf, ypsf) / (np.pi * 2.0 * obj.pars["sigma"].value ** 2.0)
    if obj.pars["amp"].value < 0:
        return -1e10
    psf = psf / np.sum(psf)
    # print obj.pars['q'].value, obj.pars['amp'].value
    psf = convolve.convolve(image, psf)[1]
    imin, sigin, xin, yin = image[mask], sigma[mask], xp[mask2], yp[mask2]
    n = 0
    model = np.empty(((len(gals) + len(srcs) + 1), imin.size))
    for gal in gals:
        gal.setPars()
        tmp = xc * 0.0
        tmp[mask2] = gal.pixeval(
            xin, yin, 0.2, 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[mask].ravel()
        n += 1
    for lens in lenses:
        lens.setPars()
    x0, y0 = pylens.lens_images(lenses, srcs, [xin, yin], 1.0 / OVRS, getPix=True)
    kk = 0
    for src in srcs:
        src.setPars()
        tmp = xc * 0.0
        tmp[mask2] = src.pixeval(x0, y0, 0.2, csub=11)
        tmp = iT.resamp(tmp, OVRS, True)
        tmp = convolve.convolve(tmp, psf, False)[0]
        model[n] = tmp[mask].ravel()
        n += 1
    model[n] = np.ones(model[n - 1].shape)
    rhs = imin / sigin  # data
    op = (model / sigin).T  # model matrix
    fit, chi = optimize.nnls(op, rhs)
    model = (model.T * fit).sum(1)
    resid = (model - imin) / sigin
    lp = -0.5 * (resid ** 2.0).sum()
    return lp
コード例 #14
0
def logP(value=0.,p=pars):
    #print 'calculating likelihood'
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
        else:
            dx = pars[0].value 
            dy = pars[1].value 
        xp,yp = xc+dx,yc+dy
        image = imgs[i]
        poisson = poissons[i]
        noise = noises[i]
        X = pars[-1].value
        sigma = (poisson + noise**X)**0.5
        psf = PSFs[i]
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(gals) + len(srcs)),imin.size))
        for gal in gals:
            gal.setPars()
            tmp = xc*0.
            tmp[mask2] = gal.pixeval(xin,yin,1./OVRS,csub=1) # 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[mask].ravel()
            n +=1
        for lens in lenses:
            lens.setPars()
        x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
        for src in srcs:
            src.setPars()
            tmp = xc*0.
            tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=1)
            tmp = iT.resamp(tmp,OVRS,True)
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        rhs = (imin/sigin) # data
        op = (model/sigin).T # model matrix
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
        #print 'calculated likelihood', lp
    return lp #,models
コード例 #15
0
ファイル: J1605_keck.py プロジェクト: lindzlebean/pylathon
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)
コード例 #16
0
ファイル: modelSB.py プロジェクト: tcollett/pylens
def modelSB(inpars,
            image,
            sigma,
            mask,
            models,
            xc,
            yc,
            OVRS=1,
            csub=11,
            noResid=False):
    output = image * 0
    for model in models:
        model.setPars(inpars)
        img = model.pixeval(xc, yc)
        if numpy.isnan(img).any() and noResid == False:
            return -1e300
        if model.convolve is not None:
            img = convolve.convolve(img, model.convolve, False)[0]
        output += img

    if noResid == True:
        return output

    logp = (-0.5 * ((output - image) / sigma)[mask]**2).sum()
    logp -= numpy.log(sigma[mask]).sum()

    return logp
コード例 #17
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)
コード例 #18
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
        else:
            dx = pars[0].value 
            dy = pars[1].value 
        xp,yp = xc+dx,yc+dy
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(srcs)),imin.size))
        for lens in lenses:
            lens.setPars()
        x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
        for src in srcs:
            src.setPars()
            tmp = xc*0.
            tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=1)
            tmp = iT.resamp(tmp,OVRS,True)
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        rhs = (imin/sigin) # data
        op = (model/sigin).T # model matrix
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp #,models
コード例 #19
0
ファイル: FastLensSim.py プロジェクト: bnord/LensPop
    def ObserveLens(self,noisy=True,bands=[]):
      if bands==[]:bands=self.bands
      for band in bands:
        if self.seeing[band]!=0:
          convolvedgal,self.psfFFT[band] = convolve.convolve(self.galmodel[band],self.psf[band],True)
          convolvedgal[convolvedgal<0]=0
          self.convolvedgal[band]=convolvedgal


          convolvedmodel=convolvedgal*1

          convolvedsrc={}
            
          for sourcenumber in self.sourcenumbers:
            convolvedsrc[sourcenumber]=convolve.convolve(self.sourcemodel[sourcenumber][band],self.psfFFT[band],False)[0]
            convolvedsrc[sourcenumber][convolvedsrc[sourcenumber]<0]=0
            self.convolvedsrc[sourcenumber][band]=convolvedsrc[sourcenumber]
            convolvedmodel+=convolvedsrc[sourcenumber]

          self.zeromagcounts[band]=(10**(-(0-self.zeropoints[band])/2.5))

          exposurecorrection=((self.ET[band]*1./self.zeroexposuretime))*self.gains[band]
          convolvedmodel*=exposurecorrection

          #skybackground per second per square arcsecond
          background=(10**(-(self.SB[band]-self.zeropoints[band])/2.5))*(self.pixelsize**2)
          tot_bg=background*exposurecorrection
          

          sigma=((convolvedmodel+tot_bg)+self.nexposures*(self.readnoise**0.5)**2)**.5

          fakeLens=convolvedmodel*1.
          if noisy:fakeLens+=(numpy.random.randn(self.side,self.side)*(sigma))

          #convert back to ADU/second:
          fakeLens/=exposurecorrection
          sigma/=exposurecorrection

          self.image[band]=fakeLens*1 
          self.fakeLens[band]=fakeLens*1
          self.sigma[band]=sigma*1
          self.fakeResidual[0][band]=fakeLens-convolvedgal
          for sourcenumber in self.sourcenumbers:
              self.SN[sourcenumber][band]=self.SNfunc(\
                  convolvedsrc[sourcenumber],sigma)
              self.fakeResidual[sourcenumber][band]=\
                  fakeLens-convolvedmodel+convolvedsrc[sourcenumber]
コード例 #20
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    for i in range(len(imgs)):
        if i == 0:
            dx,dy = 0,0
        else:
            dx = pars[0].value 
            dy = pars[1].value 
        xp,yp = xc+dx,yc+dy
        image = imgs[i]
        sigma = sigs[i]
        psf = PSFs[i]
        mask = masks[i]
        mask2 = mask2s[i]
        x0,y0=coords[i]
        imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
        n = 0
        model = np.empty(((len(gals) + len(srcs)),imin.size))
        for gal in gals:
            gal.setPars()
            tmp = xc*0.
            tmp[mask2] = gal.pixeval(xin,yin,1./OVRS,csub=1) # 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[mask].ravel()
            n +=1
        for src in srcs:
            src.setPars()
            tmp = xc*0.
            tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=1)
            tmp = iT.resamp(tmp,OVRS,True)
            tmp = convolve.convolve(tmp,psf,False)[0]
            model[n] = tmp[mask].ravel()
            n +=1
        rhs = (imin/sigin) # data
        op = (model/sigin).T # model matrix
        fit, chi = optimize.nnls(op,rhs)
        model = (model.T*fit).sum(1)
        resid = (model-imin)/sigin
        lp += -0.5*(resid**2.).sum()
        models.append(model)
    return lp #,models
コード例 #21
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)
コード例 #22
0
def lensModel(inpars,image,sig,gals,lenses,sources,xc,yc,OVRS=1,csub=11,psf=None,noResid=False,verbose=False):
    import pylens,numpy
    import indexTricks as iT

    model = xc*0.
    for gal in gals:
        gal.setPars(inpars)
        model += gal.pixeval(xc,yc,1./OVRS,csub=csub)

    for src in sources:
        src.setPars(inpars)

    for lens in lenses:
        lens.setPars(inpars)

    model = model + pylens.lens_images(lenses,sources,[xc,yc],1./OVRS)
    if numpy.isnan(model.sum()):
        if verbose==True:
            print 'nan model'
        return -1e300

    if OVRS>1:
        model = iT.resamp(model,OVRS,True)

    if psf is not None:
        from imageSim import convolve
        global psfFFT
        if psfFFT is None:
            psf /= psf.sum()
            model,psfFFT = convolve.convolve(model,psf)
        else:
            model,psfFFT = convolve.convolve(model,psfFFT,False)

    if noResid is True:
        return model
    resid = ((model-image)/sig).ravel()
    if verbose==True:
        print "%f  %5.2f %d %dx%d"%((resid**2).sum(),(resid**2).sum()/resid.size,resid.size,image.shape[1],image.shape[0])
    return -0.5*(resid**2).sum()
コード例 #23
0
def logP(value=0.,p=pars):
    lp = 0.
    models = []
    dx = pars[0].value
    dy = pars[1].value 
    xp,yp = xc+dx,yc+dy
    imin,sigin,xin,yin = image[mask], sigma[mask],xp[mask2],yp[mask2]
    n = 0
    model = np.empty(((len(gals) + len(srcs)+1),imin.size))
    for gal in gals:
        gal.setPars()
        tmp = xc*0.
        tmp[mask2] = gal.pixeval(xin,yin,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[mask].ravel()
        n +=1
    for lens in lenses:
        lens.setPars()
    x0,y0 = pylens.lens_images(lenses,srcs,[xin,yin],1./OVRS,getPix=True)
    kk=0
    for src in srcs:
        src.setPars()
        tmp = xc*0.
        tmp[mask2] = src.pixeval(x0,y0,1./OVRS,csub=11)
        tmp = iT.resamp(tmp,OVRS,True)
        tmp = convolve.convolve(tmp,psf,False)[0]
        model[n] = tmp[mask].ravel()
        n +=1
    model[n] = np.ones(model[n-1].shape)
    rhs = (imin/sigin) # data
    op = (model/sigin).T # model matrix
    fit, chi = optimize.nnls(op,rhs)
    model = (model.T*fit).sum(1)
    resid = (model-imin)/sigin
    lp = -0.5*(resid**2.).sum()
    return lp 
コード例 #24
0
ファイル: lensInference.py プロジェクト: lindzlebean/pylathon
 def addImg(self,name,img,sig,psf=None,scale=1.):
     self.imgs.append(img)
     self.sigs.append(sig)
     if psf is not None:
         PSF = psf/psf.sum()
         self.psfImgs.append(PSF)
         PSF = convolve.convolve(img,PSF)[1]
         self.psfs.append(PSF)
     else:
         self.psfs.append(None)
         self.psfImgs.append(None)
     yc,xc = iT.overSample(img.shape,1.)
     self.xc.append(xc*scale)
     self.yc.append(yc*scale)
     if len(self.imgs)>1:
         self.offsets.append(pymc.Uniform('xoff_%s'%name,-5.*scale,5.*scale,value=0.))
         self.offsets.append(pymc.Uniform('yoff_%s'%name,-5.*scale,5.*scale,value=0.))
         self.cov += [0.1,0.1]
コード例 #25
0
ファイル: EELsModels.py プロジェクト: lindzlebean/EELs
 def AddImages(self,img1,sig1,psf1,img2,sig2,psf2,Dx=None,Dy=None):
     self.img1=img1
     self.sig1=sig1
     self.psf1=psf1
     self.img2=img2
     self.sig2=sig2
     self.psf2=psf2
     self.Dx=Dx
     self.Dy=Dy
     self.imgs = [self.img1,self.img2]
     self.sigs = [self.sig1,self.sig2]
     self.psfs = [self.psf1,self.psf2]
     self.PSFs = []
     for i in range(len(self.imgs)):
         psf = self.psfs[i]
         image = self.imgs[i]
         psf /= psf.sum()
         psf = convolve.convolve(image,psf)[1]
         self.PSFs.append(psf)
コード例 #26
0
ファイル: EELsModels.py プロジェクト: lindzlebean/EELs
 def EasyAddImages(self):
     if self.name == 'J0837':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J0837()
     elif self.name == 'J0901':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J0901()
     elif self.name == 'J0913':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J0913(self.srcno)
     elif self.name == 'J1125':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1125()
     elif self.name == 'J1144':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS  = EELsImages.J1144()
     elif self.name == 'J1218':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1218()
     elif self.name == 'J1248':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1248()
     elif self.name == 'J1323':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1323(self.srcno)
     elif self.name == 'J1347':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1347()
     elif self.name == 'J1446':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1446()
     elif self.name == 'J1605':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1605(self.srcno)
     elif self.name == 'J1606':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1606()
     elif self.name == 'J1619':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J1619()
     elif self.name == 'J2228':
         self.img1,self.sig1,self.psf1,self.img2,self.sig2,self.psf2,self.Dx,self.Dy,self.OVRS = EELsImages.J2228()
     else:
         print 'are you sure this is an EEL?'
         return
     self.imgs = [self.img1,self.img2]
     self.sigs = [self.sig1,self.sig2]
     self.psfs = [self.psf1,self.psf2]
     self.PSFs = []
     for i in range(len(self.imgs)):
         psf = self.psfs[i]
         image = self.imgs[i]
         psf /= psf.sum()
         psf = convolve.convolve(image,psf)[1]
         self.PSFs.append(psf)
コード例 #27
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)
コード例 #28
0
ファイル: GrandKeckInf.py プロジェクト: lindzlebean/pylathon
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
コード例 #29
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
    )
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)
コード例 #31
0
        pars[i].value = trace[a1,a2,a3,i]
    print jj
    jj+=1



dx = pars[0].value
dy = pars[1].value 
xp,yp = xc+dx,yc+dy
psf = xpsf*0.
for obj in psfObjs:
    obj.setPars()
    psf += obj.pixeval(xpsf,ypsf) / (np.pi*2.*obj.pars['sigma'].value**2.)
psf = psf/np.sum(psf)
print 'ici',obj.pars['q'].value
psf = convolve.convolve(image,psf)[1]
xp,yp = xc+dx,yc+dy
xop,yop = xo+dy,yo+dy
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()
コード例 #32
0
ファイル: pyplz_offsetpos.py プロジェクト: langeroodi/pyplz
    def save(self, outname, make_rgb=True):

        fitsname = outname + '.fits'
        rgbname = outname + '_rgb.png'

        hdr = pyfits.Header()
        hdr['logp'] = self.logp

        light_ind_model = []
        source_ind_model = []

        xl, yl = pylens.getDeflections(self.lens_models, (self.X, self.Y))

        n = 0
        for light, mags in zip(self.light_sb_models, self.light_mags):

            light_ind_dic = {}
            for band in self.bands:
                light[band].amp = 1.
                lpix = light[band].pixeval(self.X, self.Y)
                hdr['%s.mag_%s' % (light[band].name, band)] = mags[band]
                scale = 10.**(-2. / 5. *
                              (mags[band] - light[band].Mag(self.zp[band])))

                light_ind_dic[band] = scale * convolve.convolve(
                    lpix, self.convol_matrix[band], False)[0]

            light_ind_model.append(light_ind_dic)

            n += 1

        xl = {}
        yl = {}
        for band in self.bands:
            lens_models = []
            for lens_model in self.lens_models:
                lens_models.append(lens_model[band])
            xl_here, yl_here = pylens.getDeflections(lens_models,
                                                     (self.X, self.Y))
            xl[band] = xl_here
            yl[band] = yl_here

        for source, mags in zip(self.source_sb_models, self.source_mags):

            source_ind_dic = {}

            for band in self.bands:
                source[band].amp = 1.
                spix = source[band].pixeval(xl[band], yl[band])

                hdr['%s.mag_%s' % (source[band].name, band)] = mags[band]
                scale = 10.**(-2. / 5. *
                              (mags[band] - source[band].Mag(self.zp[band])))
                source_ind_here = scale * convolve.convolve(
                    spix, self.convol_matrix[band], False)[0]

                source_ind_dic[band] = source_ind_here

            source_ind_model.append(source_ind_dic)

            n += 1

        phdu = pyfits.PrimaryHDU(header=hdr)

        hdulist = pyfits.HDUList([phdu])

        for light, light_ind_dic in zip(self.light_sb_models, light_ind_model):

            for band in self.bands:
                hdu_here = pyfits.ImageHDU(data=light_ind_dic[band])
                hdu_here.header['EXTNAME'] = '%s_%s' % (light[band].name, band)

                hdulist.append(hdu_here)

        for source, source_ind_dic in zip(self.source_sb_models,
                                          source_ind_model):
            for band in self.bands:
                hdu_here = pyfits.ImageHDU(data=source_ind_dic[band])
                hdu_here.header['EXTNAME'] = '%s_%s' % (source[band].name,
                                                        band)

                hdulist.append(hdu_here)

        hdulist.writeto(fitsname, overwrite=True)

        if make_rgb:

            # makes model rgb
            sci_list = []
            light_list = []
            source_list = []
            for band in self.bands:
                sci_list.append(self.sci[band])
                lmodel = 0. * self.sci[band]
                smodel = 0. * self.sci[band]
                for light in light_ind_model:
                    lmodel += light[band]
                light_list.append(lmodel)
                for source in source_ind_model:
                    smodel += source[band]
                source_list.append(smodel)

            pyplz_rgbtools.make_full_rgb(sci_list,
                                         light_list,
                                         source_list,
                                         outname=rgbname)

print 'schon aus Terminal'

imgs = [img1,img2]
sigs = [sig1,sig2]
psfs = [psf1,psf2]

PSFs = []
yc,xc = iT.overSample(img1.shape,1.)
yc,xc = yc-15.,xc-15.
for i in range(len(imgs)):
    psf = psfs[i]
    image = imgs[i]
    psf /= psf.sum()
    psf = convolve.convolve(image,psf)[1]
    PSFs.append(psf)


OVRS = 1

G,L,S,offsets,_ = numpy.load(guiFile)

pars = []
cov = []
### first parameters need to be the offsets
xoffset = offsets[0][3]
yoffset = offsets[1][3]
print xoffset,yoffset
print offsets
pars.append(pymc.Uniform('xoffset',-5.,5.,value=offsets[0][3]))
コード例 #34
0
    def optimize_amp(self):

        light_ind_model = []
        source_ind_model = []

        xl, yl = pylens.getDeflections(self.lens_models, (self.X, self.Y))

        lpix = []
        spix = []

        for light in self.light_sb_models:
            light.amp = 1.
            if type(light) == SBModels.PointSource:
                lpix.append(light)
            else:
                lpix.append(light.pixeval(self.X, self.Y))

        for source in self.source_sb_models:
            source.amp = 1.
            spix.append(source.pixeval(xl, yl))

        chi2sum = 0.

        for band in self.bands:
            modlist = []
            for l in lpix:
                if type(l) == SBModels.PointSource:
                    modlist.append((l.pixeval(self.X, self.Y, band) /
                                    self.err[band]).ravel()[self.mask_r])
                else:
                    modlist.append((convolve.convolve(
                        l, self.convol_matrix[band], False)[0] /
                                    self.err[band]).ravel()[self.mask_r])
            for s in spix:
                modlist.append(
                    (convolve.convolve(s, self.convol_matrix[band], False)[0] /
                     self.err[band]).ravel()[self.mask_r])

            modarr = np.array(modlist).T
            if np.isnan(modarr).any() or not np.isfinite(modarr).any():
                self.logp = -0.5e300
                return 1e300
            else:
                amps, chi = nnls(modarr, (self.sci[band] /
                                          self.err[band]).ravel()[self.mask_r])
                print(amps)
                df
                chi2sum += chi**2

            i = 0
            for light, mags in zip(self.light_sb_models, self.light_mags):
                if amps[i] > 0.:
                    light.amp = amps[i]
                    mags[band] = light.Mag(self.zp[band])
                else:
                    mags[band] = 99.
                i += 1

            for source, mags in zip(self.source_sb_models, self.source_mags):
                if amps[i] > 0.:
                    source.amp = amps[i]
                    mags[band] = source.Mag(self.zp[band])
                else:
                    mags[band] = 99.
                i += 1

        self.logp = -0.5 * chi2sum
        return chi2sum
コード例 #35
0
ファイル: modelSB.py プロジェクト: tcollett/pylens
def linearmodelSB(inpars,
                  simage,
                  sigma,
                  mask,
                  models,
                  xc,
                  yc,
                  OVRS=1,
                  csub=11,
                  noResid=False,
                  levMar=False):
    def objf(x, lhs, rhs):
        return ((numpy.dot(lhs, x) - rhs)**2).sum()

    def objdf(x, lhs, rhs):
        return numpy.dot(lhs.T, numpy.dot(lhs, x) - rhs)

    nmod = len(models)
    model = numpy.zeros((nmod, mask.sum()))
    norm = numpy.zeros(nmod)
    for n in range(nmod):
        M = models[n]
        M.setPars(inpars)
        if M.convolve is not None:
            img = M.pixeval(xc, yc, scale=1. / abs(OVRS))
            img = convolve.convolve(img, M.convolve, False)[0]
            if OVRS > 1:
                img = iT.resamp(img, OVRS, True)
        else:
            img = M.pixeval(xc, yc)
            if OVRS > 1:
                img = iT.resamp(img, OVRS, True)
        if numpy.isnan(img).any() and noResid == False:
            return -1e300
        model[n] = img[mask].ravel()
        norm[n] = model[n].max()
        model[n] /= norm[n]

    op = (model / sigma).T
    fit, chi = numpy.linalg.lstsq(op, simage)[:2]
    fit = numpy.array(fit)
    if (fit < 0).any():
        sol = fit
        sol[sol < 0] = 1e-11
        bounds = [(1e-11, 1e11)] * nmod
        result = fmin_slsqp(objf,
                            sol,
                            bounds=bounds,
                            full_output=1,
                            fprime=objdf,
                            acc=1e-19,
                            iter=2000,
                            args=[op.copy(), simage.copy()],
                            iprint=0)
        fit, chi = result[:2]
        fit = numpy.asarray(fit)
        if (fit < 1e-11).any():
            fit[fit < 1e-11] = 1e-11

    for i in range(nmod):
        models[i].amp = fit[i] / norm[i]

    if levMar == True:
        return (op * fit).sum(1) - simage
        lhs = (op * fit)
        print lhs.shape, simage.shape
        return lhs - simage

    if noResid == True:
        output = []
        for M in models:
            if M.convolve is not None:
                img = M.pixeval(xc, yc, scale=1. / abs(OVRS))
                img = convolve.convolve(img, M.convolve, False)[0]
                if OVRS > 1:
                    img = iT.resamp(img, OVRS, True)
            else:
                img = M.pixeval(xc, yc)
                if OVRS > 1:
                    img = iT.resamp(img, OVRS, True)
            output.append(img)
        return output

    logp = -0.5 * chi - numpy.log(sigma).sum()
    return logp
コード例 #36
0
def logP(value=0.0, p=pars):
    lp = 0.0
    models = []
    dx = pars[0].value
    dy = pars[1].value
    xp, yp = xc + dx, yc + dy
    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 = pars[9].value.item()
    sig3 = pars[10].value.item()
    q3 = pars[11].value.item()
    pa3 = pars[12].value.item()
    amp3 = 1.0 - amp1 - amp2
    if amp3 < 0:
        return -1e10
    # print dx,dy,sig1,sig2,sig3,q1,q2,q3,pa1,pa2,pa3,amp1,amp2,amp3
    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(xpsf, ypsf) * amp1 / (np.pi * 2.0 * sig1 ** 2.0)
    psf2 = psfObj2.pixeval(xpsf, ypsf) * amp2 / (np.pi * 2.0 * sig2 ** 2.0)
    psf3 = psfObj3.pixeval(xpsf, ypsf) * amp3 / (np.pi * 2.0 * sig3 ** 2.0)
    psf = psf1 + psf2 + psf3
    psf /= psf.sum()
    psf = convolve.convolve(image, psf)[1]
    imin, sigin, xin, yin = image[mask], sigma[mask], xp[mask2], yp[mask2]
    n = 0
    model = np.empty(((len(gals) + len(srcs) + 1), imin.size))
    for gal in gals:
        gal.setPars()
        tmp = xc * 0.0
        tmp[mask2] = gal.pixeval(
            xin, yin, 1.0 / OVRS, csub=1
        )  # 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[mask].ravel()
        n += 1
    for lens in lenses:
        lens.setPars()
    x0, y0 = pylens.lens_images(lenses, srcs, [xin, yin], 1.0 / OVRS, getPix=True)
    kk = 0
    for src in srcs:
        src.setPars()
        tmp = xc * 0.0
        tmp[mask2] = src.pixeval(x0, y0, 1.0 / OVRS, csub=1)
        tmp = iT.resamp(tmp, OVRS, True)
        tmp = convolve.convolve(tmp, psf, False)[0]
        model[n] = tmp[mask].ravel()
        n += 1
    model[n] = np.ones(model[n - 1].shape)
    rhs = imin / sigin  # data
    op = (model / sigin).T  # model matrix
    fit, chi = optimize.nnls(op, rhs)
    model = (model.T * fit).sum(1)
    resid = (model - imin) / sigin
    lp = -0.5 * (resid ** 2.0).sum()
    return lp
コード例 #37
0
ファイル: lensInference.py プロジェクト: lindzlebean/pylathon
    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()
コード例 #38
0
    def optimize_amp(self):

        light_ind_model = []
        source_ind_model = []

        xl, yl = pylens.getDeflections(self.lens_models, (self.X, self.Y))

        modlist = []

        for light, sed, mags in zip(self.light_sb_models,
                                    self.light_sed_models, self.light_mags):
            lmodel = 0. * self.scistack
            light.amp = 1.
            if light.__class__.__name__ == 'PointSource':
                for i in range(self.nbands):
                    scale = sed.scale(self.bands[i], self.reference_band)
                    lmodel[i * self.ny:(i + 1) * self.ny, :] = light.pixeval(
                        self.X, self.Y, self.bands[i])
                    mags[self.bands[i]] = -2.5 * np.log10(scale) + self.zp[
                        self.bands[i]] - self.zp[self.reference_band]
            else:
                lpix = light.pixeval(self.X, self.Y)
                for i in range(self.nbands):
                    scale = sed.scale(self.bands[i], self.reference_band)
                    lmodel[i * self.ny:(i + 1) *
                           self.ny, :] = scale * convolve.convolve(
                               lpix, self.convol_matrix[self.bands[i]],
                               False)[0]
                    mags[self.bands[i]] = -2.5 * np.log10(scale) + self.zp[
                        self.bands[i]] - self.zp[self.reference_band]
            modlist.append((lmodel / self.errstack).ravel()[self.maskstack_r])

        for source, sed, mags in zip(self.source_sb_models,
                                     self.source_sed_models, self.source_mags):
            smodel = 0. * self.scistack
            source.amp = 1.
            spix = source.pixeval(xl, yl)
            for i in range(self.nbands):
                scale = sed.scale(self.bands[i], self.reference_band)
                smodel[i * self.ny:(i + 1) *
                       self.ny, :] = scale * convolve.convolve(
                           spix, self.convol_matrix[self.bands[i]], False)[0]
                mags[self.bands[i]] = -2.5 * np.log10(scale) + self.zp[
                    self.bands[i]] - self.zp[self.reference_band]
            modlist.append((smodel / self.errstack).ravel()[self.maskstack_r])

        modarr = np.array(modlist).T
        if np.isnan(modarr).any() or not np.isfinite(modarr).any():
            amps = np.ones(self.nlight + self.nsource)
            chi = 1e300
        else:
            amps, chi = nnls(modarr, (self.scistack /
                                      self.errstack).ravel()[self.maskstack_r])

        i = 0
        for light, sed, mags in zip(self.light_sb_models,
                                    self.light_sed_models, self.light_mags):

            if amps[i] > 0.:
                light.amp *= amps[i]
                mainmag = light.Mag(self.zp[self.reference_band])
                for band in self.bands:
                    mags[band] += mainmag
                if sed.__class__.__name__ == 'SPS':
                    mags['mstar'] = 10.**(
                        -2. / 5. * (mainmag - sed.mags[self.reference_band]))
            else:
                for band in self.bands:
                    mags[band] = 99.
                if sed.__class__.__name__ == 'SPS':
                    mags['mstar'] = 0.
            i += 1

        for source, mags in zip(self.source_sb_models, self.source_mags):
            if amps[i] > 0.:
                source.amp *= amps[i]
                mainmag = source.Mag(self.zp[self.reference_band])
                for band in self.bands:
                    mags[band] = mags[band] + mainmag
            else:
                for band in self.bands:
                    mags[band] = 99.
            i += 1

        self.logp = -0.5 * chi**2
        return chi**2
コード例 #39
0
    def save(self, outname, config, make_rgb=True):

        fitsname = outname + '.fits'
        rgbname = outname + '_rgb.png'

        hdr = pyfits.Header()
        hdr['logp'] = self.logp

        light_ind_model = []
        source_ind_model = []

        xl, yl = pylens.getDeflections(self.lens_models, (self.X, self.Y))

        n = 0
        for light, sed, mags in zip(self.light_sb_models,
                                    self.light_sed_models, self.light_mags):

            light_ind_dic = {}

            if light.__class__.__name__ == 'PointSource':
                for band in self.bands:
                    hdr['%s.mag_%s' % (light.name, band)] = mags[band]
                    scale = sed.scale(band, self.reference_band)
                    light_ind_dic[band] = scale * light.pixeval(
                        self.X, self.Y, band)
            else:
                lpix = light.pixeval(self.X, self.Y)
                for band in self.bands:
                    hdr['%s.mag_%s' % (light.name, band)] = mags[band]
                    scale = sed.scale(band, self.reference_band)
                    light_ind_dic[band] = scale * convolve.convolve(
                        lpix, self.convol_matrix[band], False)[0]

            light_ind_model.append(light_ind_dic)

            n += 1

        for source, sed, mags in zip(self.source_sb_models,
                                     self.source_sed_models, self.source_mags):

            source_ind_dic = {}

            spix = source.pixeval(xl, yl)

            for band in self.bands:
                hdr['%s.mag_%s' % (source.name, band)] = mags[band]
                scale = sed.scale(band, self.reference_band)
                source_ind_here = scale * convolve.convolve(
                    spix, self.convol_matrix[band], False)[0]

                source_ind_dic[band] = source_ind_here

            source_ind_model.append(source_ind_dic)

            n += 1

        phdu = pyfits.PrimaryHDU(header=hdr)

        hdulist = pyfits.HDUList([phdu])

        for light, light_ind_dic in zip(self.light_sb_models, light_ind_model):

            for band in self.bands:
                hdu_here = pyfits.ImageHDU(data=light_ind_dic[band])
                hdu_here.header['EXTNAME'] = '%s_%s' % (light.name, band)

                hdulist.append(hdu_here)

        for source, source_ind_dic in zip(self.source_sb_models,
                                          source_ind_model):
            for band in self.bands:
                hdu_here = pyfits.ImageHDU(data=source_ind_dic[band])
                hdu_here.header['EXTNAME'] = '%s_%s' % (source.name, band)

                hdulist.append(hdu_here)

        hdulist.writeto(fitsname, overwrite=True)

        if make_rgb:

            # makes model rgb
            sci_list = []
            model_list = []
            for band in self.bands:
                sci_list.append(self.sci[band])
                comp_list = []
                for light in light_ind_model:
                    comp_list.append(light[band])
                for source in source_ind_model:
                    comp_list.append(source[band])
                model_list.append(comp_list)

            pyplz_rgbtools.make_full_rgb(sci_list,
                                         model_list,
                                         outname=rgbname,
                                         scheme=config['rgbscheme'],
                                         cuts=config['rgbcuts'],
                                         scales=config['rgbscales'])
コード例 #40
0
    def __init__(self, config):

        self.pars = []
        self.par2index = {}
        self.index2par = {}

        self.sci = {}
        self.err = {}
        self.convol_matrix = {}
        self.zp = {}

        self.light_sb_models = []
        self.source_sb_models = []
        self.light_sed_models = []
        self.source_sed_models = []

        self.light_mags = []
        self.source_mags = []

        self.lens_models = []

        self.logp = None

        # defines bands

        self.bands = []
        self.reference_band = config['reference_band']

        for band in config['filters']:
            self.bands.append(band)

        self.nbands = len(self.bands)

        # reads in data

        filtnames = {}
        psfdic = {}
        for i in range(self.nbands):
            band = self.bands[i]

            self.zp[band] = config['zeropoints'][i]

            filtname = config['filter_prefix'] + band + config['filter_suffix']

            filtnames[band] = filtname

            hdu = pyfits.open(
                os.path.expandvars(config['data_dir']) + '/' +
                config['filename'] + '_%s' % band + config['science_tag'])[0]

            img = hdu.data.copy()
            subimg = img.copy()
            self.sci[band] = subimg
            suberr = pyfits.open(
                os.path.expandvars(config['data_dir']) + '/' +
                config['filename'] + '_%s' % band +
                config['err_tag'])[0].data.copy()

            if config['err_type'] == 'VAR':
                self.err[band] = suberr**0.5
            elif config['err_type'] == 'SIGMA':
                self.err[band] = suberr.copy()
            else:
                df

            psf_file = pyfits.open(
                os.path.expandvars(config['data_dir']) + '/' +
                config['filename'] + '_%s' % band + config['psf_tag'])
            nhdu = len(psf_file)
            found = False
            n = 0
            while not found and n < nhdu:
                if psf_file[n].data is not None:
                    psf = psf_file[n].data.copy()
                    found = True
                else:
                    n += 1
            if not found:
                df

            m = (psf[:2].mean() + psf[-2:].mean() + psf[:, :2].mean() +
                 psf[:, -2:].mean()) / 4.
            psf -= m
            psf /= psf.sum()

            psfdic[band] = psf
            self.convol_matrix[band] = convolve.convolve(self.sci[band],
                                                         psf)[1]

        # prepares mask and data array

        ny, nx = self.sci[self.bands[0]].shape
        X, Y = np.meshgrid(np.arange(1. * nx), np.arange(1. * ny))

        self.ny = ny
        self.nx = nx
        self.X = X
        self.Y = Y
        self.R = ((X - nx / 2)**2 + (Y - ny / 2)**2)**0.5

        if config['mask_dir'] is None:
            mask_dir = './'
        else:
            mask_dir = os.path.expandvars(config['mask_dir'])

        if config['maskname'] is not None:
            MASK = pyfits.open(mask_dir + config['maskname'])[0].data.copy()
        else:
            MASK = np.ones(X.shape, dtype=int)

        if config['rmax'] is not None:
            MASK[self.R > config['rmax']] = 0

        mask = MASK > 0
        mask_r = mask.ravel()

        self.modelstack = np.zeros((self.nbands * ny, nx))
        self.scistack = 0. * self.modelstack
        self.errstack = 0. * self.modelstack
        self.maskstack = np.zeros((self.nbands * ny, nx), dtype=bool)

        for i in range(self.nbands):
            self.scistack[i * ny:(i + 1) * ny, :] = self.sci[self.bands[i]]
            self.errstack[i * ny:(i + 1) * ny, :] = self.err[self.bands[i]]
            self.maskstack[i * ny:(i + 1) * ny, :] = mask

        self.maskstack_r = self.maskstack.ravel()

        #defines model parameters

        ncomp = 0
        npar = 0
        for comp in config['light_components']:
            ncomp += 1
            for par in comp['pars']:
                parpar = comp['pars'][par]
                if parpar['link'] is None and parpar['var'] == 1:
                    self.pars.append(
                        Par(par + str(ncomp),
                            lower=parpar['low'],
                            upper=parpar['up'],
                            value=parpar['value'],
                            step=parpar['step']))
                    self.par2index['light' + str(ncomp) + '.' + par] = npar
                    self.index2par[npar] = 'light' + str(ncomp) + '.' + par
                    npar += 1

        ncomp = 0
        for comp in config['source_components']:
            ncomp += 1
            for par in comp['pars']:
                parpar = comp['pars'][par]
                if parpar['link'] is None and parpar['var'] == 1:
                    self.pars.append(
                        Par(par + str(ncomp),
                            lower=parpar['low'],
                            upper=parpar['up'],
                            value=parpar['value'],
                            step=parpar['step']))
                    self.par2index['source' + str(ncomp) + '.' + par] = npar
                    self.index2par[npar] = 'source' + str(ncomp) + '.' + par
                    npar += 1

        ncomp = 0
        for comp in config['lens_components']:
            ncomp += 1
            for par in comp['pars']:
                parpar = comp['pars'][par]
                if parpar['link'] is None and parpar['var'] == 1:
                    self.pars.append(
                        Par(par + str(ncomp),
                            lower=parpar['low'],
                            upper=parpar['up'],
                            value=parpar['value'],
                            step=parpar['step']))
                    self.par2index['lens' + str(ncomp) + '.' + par] = npar
                    self.index2par[npar] = 'lens' + str(ncomp) + '.' + par
                    npar += 1

        i = 0

        filtnames = {}
        for band in config['filters']:

            filtname = config['filter_prefix'] + band + config['filter_suffix']

            filtnames[band] = filtname

        ncomp = 1
        for comp in config['lens_components']:

            name = 'lens%d' % ncomp

            pars_here = {}
            for par in comp['pars']:
                if comp['pars'][par]['link'] is None:
                    if comp['pars'][par]['var'] == 1:
                        pars_here[par] = self.pars[self.par2index[name + '.' +
                                                                  par]]
                    elif comp['pars'][par]['var'] == 0:
                        pars_here[par] = Par(par,
                                             lower=comp['pars'][par]['value'],
                                             upper=comp['pars'][par]['value'],
                                             value=comp['pars'][par]['value'])
                    else:
                        df
                else:
                    pars_here[par] = self.pars[self.par2index[comp['pars'][par]
                                                              ['link']]]

            ncomp += 1

            lens = MassModels.PowerLaw(name, pars_here)
            self.lens_models.append(lens)

        self.nlens = len(self.lens_models)

        ncomp = 1

        for comp in config['light_components']:

            name = 'light%d' % ncomp

            pars_here = {}
            sed_here = {}
            for par in comp['pars']:
                if par in SBModels.parlists[comp['class']]:
                    if comp['pars'][par]['link'] is None:
                        if comp['pars'][par]['var'] == 1:
                            pars_here[par] = self.pars[self.par2index[name +
                                                                      '.' +
                                                                      par]]
                        else:
                            pars_here[par] = Par(
                                par,
                                lower=comp['pars'][par]['value'],
                                upper=comp['pars'][par]['value'],
                                value=comp['pars'][par]['value'])
                    else:
                        pars_here[par] = self.pars[self.par2index[
                            comp['pars'][par]['link']]]
                else:
                    if comp['pars'][par]['link'] is None:
                        if comp['pars'][par]['var'] == 1:
                            sed_here[par] = self.pars[self.par2index[name +
                                                                     '.' +
                                                                     par]]
                        else:
                            sed_here[par] = Par(
                                par,
                                lower=comp['pars'][par]['value'],
                                upper=comp['pars'][par]['value'],
                                value=comp['pars'][par]['value'])
                    else:
                        sed_here[par] = self.pars[self.par2index[
                            comp['pars'][par]['link']]]

            if comp['class'] == 'Sersic':
                light = SBModels.Sersic('light%d' % ncomp, pars_here)
            elif comp['class'] == 'PointSource':
                light = SBModels.PointSource('light%d' % ncomp, psfdic,
                                             pars_here)
            else:
                df

            self.light_sb_models.append(light)

            if comp['sed'] == 'colorpars':
                sed = SEDModels.Colors('light_sed%d' % ncomp, sed_here,
                                       self.bands, self.reference_band,
                                       self.zp)
            elif comp['sed'] == 'template':
                sed = SEDModels.Template('light_sed%d' % ncomp, sed_here,
                                         filtnames, self.zp)
            elif comp['sed'] == 'sps':
                modelname = config['sps_model_dir'] + comp['sps_model']
                sed = SEDModels.SPS('light_sed%d' % ncomp, sed_here,
                                    self.bands, self.zp, modelname)
            else:
                df
            self.light_sed_models.append(sed)
            self.light_mags.append({})

            ncomp += 1

        self.nlight = len(self.light_sb_models)

        ncomp = 1
        for comp in config['source_components']:

            name = 'source%d' % ncomp

            pars_here = {}
            sed_here = {}
            for par in comp['pars']:
                if par in SBModels.parlists['Sersic']:
                    if comp['pars'][par]['link'] is None:
                        if comp['pars'][par]['var'] == 1:
                            pars_here[par] = self.pars[self.par2index[name +
                                                                      '.' +
                                                                      par]]
                        else:
                            pars_here[par] = comp['pars'][par]['value']
                    else:
                        pars_here[par] = self.pars[self.par2index[
                            comp['pars'][par]['link']]]
                else:
                    if comp['pars'][par]['link'] is None:
                        if comp['pars'][par]['var'] == 1:
                            sed_here[par] = self.pars[self.par2index[name +
                                                                     '.' +
                                                                     par]]
                        else:
                            sed_here[par] = Par(
                                par,
                                lower=comp['pars'][par]['value'],
                                upper=comp['pars'][par]['value'],
                                value=comp['pars'][par]['value'])
                    else:
                        sed_here[par] = self.pars[self.par2index[
                            comp['pars'][par]['link']]]

            if comp['class'] == 'Sersic':
                source = SBModels.Sersic('source%d' % ncomp, pars_here)
            else:
                df

            self.source_sb_models.append(source)
            if comp['sed'] == 'colorpars':
                sed = SEDModels.Colors('light_sed%d' % ncomp,
                                       sed_here,
                                       self.bands,
                                       self.reference_band,
                                       zp=self.zp)
            elif comp['sed'] == 'template':
                sed = SEDModels.Template('light_sed%d' % ncomp,
                                         sed_here,
                                         filtnames,
                                         zp=self.zp)
            self.source_sed_models.append(sed)
            self.source_mags.append({})

            ncomp += 1

        self.nsource = len(self.source_sb_models)
コード例 #41
0
ファイル: FittedPix.py プロジェクト: lindzlebean/pylathon
    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()


img2 = pyfits.open('/data/ljo31/Lens/J1605/SDSSJ1605+3811_F814W_sci_cutout2.fits')[0].data.copy()
sig2 = pyfits.open('/data/ljo31/Lens/J1605/SDSSJ1605+3811_F814W_noisemap2_masked.fits')[0].data.copy()
psf2 = pyfits.open('/data/ljo31/Lens/J1605/F814W_psf_#2.fits')[0].data.copy()  
psf2= psf2[15:-16,14:-16]
psf2 /= psf2.sum()
psf = convolve.convolve(img2,psf2)[1]
OVRS=1.


fchain = np.load('/data/ljo31/Lens/J1605/fchain_PixFit_2src.npy')
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]

コード例 #42
0
    def save(self, outname, make_rgb=True):

        fitsname = outname + '.fits'
        rgbname = outname + '_rgb.png'

        hdr = pyfits.Header()
        hdr['logp'] = self.logp

        light_ind_model = []
        source_ind_model = []

        xl, yl = pylens.getDeflections(self.lens_models, (self.X, self.Y))

        n = 0
        for light, sed in zip(self.light_sb_models, self.light_sed_models):

            light_ind_dic = {}

            if light.__class__.__name__ == 'PointSource':
                for band in self.bands:
                    hdr['%s.mag_%s' % (light.name, band)] = sed.mags[band]
                    scale = sed.scale(band, self.main_band)
                    light_ind_dic[band] = scale * light.pixeval(
                        self.X, self.Y, band)
            else:
                lpix = light.pixeval(self.X, self.Y)
                for band in self.bands:
                    hdr['%s.mag_%s' % (light.name, band)] = sed.mags[band]
                    scale = sed.scale(band, self.main_band)
                    light_ind_dic[band] = scale * convolve.convolve(
                        lpix, self.convol_matrix[band], False)[0]

            light_ind_model.append(light_ind_dic)

            n += 1

        for source, sed in zip(self.source_sb_models, self.source_sed_models):

            source_ind_dic = {}

            spix = source.pixeval(xl, yl)

            for band in self.bands:
                hdr['%s.mag_%s' % (source.name, band)] = sed.mags[band]
                scale = sed.scale(band, self.main_band)
                source_ind_here = scale * convolve.convolve(
                    spix, self.convol_matrix[band], False)[0]

                source_ind_dic[band] = source_ind_here

            source_ind_model.append(source_ind_dic)

            n += 1

        phdu = pyfits.PrimaryHDU(header=hdr)

        hdulist = pyfits.HDUList([phdu])

        for light, light_ind_dic in zip(self.light_sb_models, light_ind_model):

            for band in self.bands:
                hdu_here = pyfits.ImageHDU(data=light_ind_dic[band])
                hdu_here.header['EXTNAME'] = '%s_%s' % (light.name, band)

                hdulist.append(hdu_here)

        for source, source_ind_dic in zip(self.source_sb_models,
                                          source_ind_model):
            for band in self.bands:
                hdu_here = pyfits.ImageHDU(data=source_ind_dic[band])
                hdu_here.header['EXTNAME'] = '%s_%s' % (source.name, band)

                hdulist.append(hdu_here)

        if self.sky is not None:
            for band in self.bands:
                hdu_here = pyfits.ImageHDU(
                    data=self.sky['amp_%s' % band].value + 0. * self.sci[band])
                hdu_here.header['EXTNAME'] = 'sky_%s' % band
                hdulist.append(hdu_here)

        hdulist.writeto(fitsname, overwrite=True)

        if make_rgb:

            # makes model rgb
            sci_list = []
            light_list = []
            source_list = []
            for band in self.bands:
                sci_list.append(self.sci[band])
                lmodel = 0. * self.sci[band]
                smodel = 0. * self.sci[band]
                for light in light_ind_model:
                    lmodel += light[band]
                if self.sky is not None:
                    lmodel += self.sky['amp_%s' % band].value

                light_list.append(lmodel)
                for source in source_ind_model:
                    smodel += source[band]
                source_list.append(smodel)

            pyplz_rgbtools.make_full_rgb(sci_list,
                                         light_list,
                                         source_list,
                                         outname=rgbname)
コード例 #43
0
ファイル: lensModel.py プロジェクト: tcollett/pylens
def lensFit(inpars,
            image,
            sig,
            gals,
            lenses,
            sources,
            xc,
            yc,
            OVRS=1,
            csub=5,
            psf=None,
            mask=None,
            noResid=False,
            verbose=False,
            getModel=False,
            showAmps=False,
            allowNeg=False):
    import pylens, numpy
    import indexTricks as iT
    from imageSim import convolve
    from scipy import optimize
    """
    if psf is not None:
        from imageSim import convolve
        global psfFFT
        if psfFFT is None:
            psf /= psf.sum()
            #if OVRS>1:
            #    from scipy import ndimage
            #    psf = ndimage.zoom(psf,OVRS)
            tmp,psfFFT = convolve.convolve(image,psf)
    """

    if noResid == True or getModel == True:
        mask = None
    if mask is None:
        xin = xc.copy()
        yin = yc.copy()
        imin = image.flatten()
        sigin = sig.flatten()
    else:
        xin = xc[mask].copy()
        yin = yc[mask].copy()
        imin = image[mask].flatten()
        sigin = sig[mask].flatten()

    n = 0
    model = numpy.empty((len(gals) + len(sources), imin.size))
    for gal in gals:
        gal.setPars()
        gal.amp = 1
        if mask is None:
            tmp = gal.pixeval(xin, yin, 1. / OVRS, csub=csub)
        else:
            tmp = xc * 0.
            tmp[mask] = gal.pixeval(xin, yin, 1. / OVRS, csub=csub)
        if numpy.isnan(tmp).any():
            if verbose == True:
                print 'nan model'
            return -1e300

#        if psf is not None:
#            tmp = convolve.convolve(tmp,psfFFT,False)[0]
        if OVRS > 1:
            tmp = iT.resamp(tmp, OVRS, True)
        if psf is not None and gal.convolve is not None:
            tmp = convolve.convolve(tmp, psf, False)[0]
        if mask is None:
            model[n] = tmp.ravel()
        else:
            model[n] = tmp[mask].ravel()
        n += 1

    for lens in lenses:
        lens.setPars()

    x0, y0 = pylens.lens_images(lenses,
                                sources, [xin, yin],
                                1. / OVRS,
                                getPix=True)
    for src in sources:
        src.setPars()
        src.amp = 1
        if mask is None:
            tmp = src.pixeval(x0, y0, 1. / OVRS, csub=csub)
        else:
            tmp = xc * 0.
            tmp[mask] = src.pixeval(x0, y0, 1. / OVRS, csub=csub)
        if numpy.isnan(tmp).any():
            if verbose == True:
                print 'nan model'
            return -1e300

#        if psf is not None:
#            tmp = convolve.convolve(tmp,psfFFT,False)[0]
        if OVRS > 1:
            tmp = iT.resamp(tmp, OVRS, True)
        if psf is not None:
            tmp = convolve.convolve(tmp, psf, False)[0]
        if mask is None:
            model[n] = tmp.ravel()
        else:
            model[n] = tmp[mask].ravel()
        n += 1

    rhs = (imin / sigin)
    op = (model / sigin).T

    fit, chi = optimize.nnls(op, rhs)
    #print fit
    #exit()

    if getModel is True:
        j = 0
        for m in gals + sources:
            m.amp = fit[j]
            j += 1
        return (model.T * fit).T.reshape((n, image.shape[0], image.shape[1]))
    elif noResid is True:
        model = (model.T * fit).sum(1).reshape(image.shape)
        j = 0
        for m in gals + sources:
            m.amp = fit[j]
            j += 1
        return model
    model = (model.T * fit).sum(1)

    if mask is None:
        model = model.reshape(image.shape)
        resid = ((model - image) / sig).ravel()
    else:
        resid = (model - imin) / sigin

    if verbose == True:
        print "%f  %5.2f %d %dx%d" % (
            (resid**2).sum(), (resid**2).sum() / resid.size, resid.size,
            image.shape[1], image.shape[0])
    return -0.5 * (resid**2).sum()
コード例 #44
0
def lensFit(inpars,image,sig,gals,lenses,sources,xc,yc,OVRS=1,csub=5,psf=None,mask=None,noResid=False,verbose=False,getModel=False,showAmps=False,allowNeg=False):
    import pylens,numpy
    import indexTricks as iT
    from imageSim import convolve
    from scipy import optimize


    if noResid==True or getModel==True:
        mask = None
    if mask is None:
        xin = xc.copy()
        yin = yc.copy()
        imin = image.flatten()
        sigin = sig.flatten()
    else:
        xin = xc[mask].copy()
        yin = yc[mask].copy()
        imin = image[mask].flatten()
        sigin = sig[mask].flatten()

    n = 0
    model = numpy.empty((len(gals)+len(sources),imin.size))
    for gal in gals:
        gal.setPars()
        gal.amp = 1
        if mask is None:
            tmp = gal.pixeval(xin,yin,1./OVRS,csub=csub)
        else:
            tmp = xc*0.
            tmp[mask] = gal.pixeval(xin,yin,1./OVRS,csub=csub)
        if numpy.isnan(tmp).any():
            if verbose==True:
                print 'nan model'
            return -1e300

#        if psf is not None:
#            tmp = convolve.convolve(tmp,psfFFT,False)[0]
        if OVRS>1:
            tmp = iT.resamp(tmp,OVRS,True)
        if psf is not None and gal.convolve is not None:
            tmp = convolve.convolve(tmp,psf,False)[0]
        if mask is None:
            model[n] = tmp.ravel()
        else:
            model[n] = tmp[mask].ravel()
        n += 1

    for lens in lenses:
        lens.setPars()

    x0,y0 = pylens.lens_images(lenses,sources,[xin,yin],1./OVRS,getPix=True)
    for src in sources:
        src.setPars()
        src.amp = 1
        if mask is None:
            tmp = src.pixeval(x0,y0,1./OVRS,csub=csub)
        else:
            tmp = xc*0.
            tmp[mask] = src.pixeval(x0,y0,1./OVRS,csub=csub)
        if numpy.isnan(tmp).any():
            if verbose==True:
                print 'nan model'
            return -1e300

#        if psf is not None:
#            tmp = convolve.convolve(tmp,psfFFT,False)[0]
        if OVRS>1:
            tmp = iT.resamp(tmp,OVRS,True)
        if psf is not None:
            tmp = convolve.convolve(tmp,psf,False)[0]
        if mask is None:
            model[n] = tmp.ravel()
        else:
            model[n] = tmp[mask].ravel()
        n += 1

    rhs = (imin/sigin)
    op = (model/sigin).T

    fit,chi = optimize.nnls(op,rhs)

    if getModel is True:
        j = 0
        for m in gals+sources:
            m.amp = fit[j]
            j += 1
        return (model.T*fit).T.reshape((n,image.shape[0],image.shape[1]))
    elif noResid is True:
        model = (model.T*fit).sum(1).reshape(image.shape)
        j = 0
        for m in gals+sources:
            m.amp = fit[j]
            j += 1
        return model
    model = (model.T*fit).sum(1)

    if mask is None:
        model = model.reshape(image.shape)
        resid = ((model-image)/sig).ravel()
    else:
        resid = (model-imin)/sigin

    if verbose==True:
        print "%f  %5.2f %d %dx%d"%((resid**2).sum(),(resid**2).sum()/resid.size,resid.size,image.shape[1],image.shape[0])
    return -0.5*(resid**2).sum()