예제 #1
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.
            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:
                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.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
예제 #2
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'])
예제 #3
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