Example #1
0
def get_gaussian_psf_template_3_5_20(pixel_fwhm=3., nbin=5):
    nc = nbin**2
    psfnew = Gaussian2DKernel((pixel_fwhm / 2.355) * nbin,
                              x_size=125,
                              y_size=125).array.astype(np.float32)
    cf = psf_poly_fit(psfnew, nbin=nbin)
    return psfnew, cf, nc, nbin
Example #2
0
def get_psf_and_vals(path):
    f = open(path)
    psf = np.loadtxt(path, skiprows=1).astype(np.float32)
    nc, nbin = [np.int32(i) for i in f.readline().split()]
    f.close()
    cf = psf_poly_fit(psf, nbin=nbin)
    return psf, nc, cf
def get_gaussian_psf_template_3_5_20(pixel_fwhm=3., nbin=5):
    ''' 
	Computes Gaussian PSF kernel for fast model evaluation with lion

	Parameters
	----------
	pixel_fwhm : float
		Full width at half-maximum (FWHM) of PSF in units of pixels.
		Default is 3 (for SPIRE analysis).

	nbin : int
		Upsampling factor for sub-pixel interpolation method in lion
		Default is 5.

	Returns
	-------

	psfnew : '~numpy.ndarray'
		Sum normalized PSF template up sampled by factor nbin in both x and y

	cf : '~numpy.ndarray'
		Coefficients of polynomial fit to upsampled PSF. These are used by lion

	nbin : int
		Upsampling factor, same as before.. 

	'''
    nc = nbin**2
    psfnew = Gaussian2DKernel((pixel_fwhm / 2.355) * nbin,
                              x_size=125,
                              y_size=125).array.astype(np.float32)
    cf = psf_poly_fit(psfnew, nbin=nbin)
    return psfnew, cf, nc, nbin
    def mock_pix_psf_files(self, two_star_blend, two_star_mode):
        f = open(self.base_path + '/Data/sdss.0921/sdss.0921_psf.txt')
        nc, nbin = [np.int32(i) for i in f.readline().split()]
        f.close()
        psf = np.loadtxt(self.base_path + '/Data/sdss.0921/sdss.0921_psf.txt',
                         skiprows=1).astype(np.float32)
        cf = psf_poly_fit(psf, nbin=nbin)

        nb = self.nbands
        if two_star_blend:
            if two_star_mode == 'rx3':
                nb = 1
        for b in xrange(nb):
            f = open(
                self.data_path + '/pixs/' + self.dir_name + '-pix' +
                self.bands[b] + '.txt', 'w')
            if two_star_blend:
                f.write('%1d\t%1d\t1\n0.\t%0.3f\n%0.8f\t%1d' %
                        (self.imsz[0], self.imsz[1], self.gain,
                         self.nmgy_to_cts, self.truebacks[b]))
            else:
                f.write('%1d\t%1d\t1\n0.\t%0.3f\n%0.8f\t%1d' %
                        (self.imsz[0], self.imsz[1], self.gain,
                         self.nmgy_to_cts, self.normal_backs[b]))
            np.savetxt(self.data_path + '/psfs/' + self.dir_name + '-psf' +
                       self.bands[b] + '.txt',
                       psf,
                       header='%1d\t%1d' % (nc, nbin),
                       comments='')

        return nc, nbin, psf, cf
Example #5
0
def get_gaussian_psf_template(fwhm, pixel_fwhm=3., nbin=5):
    nc = nbin**2
    psfnew = Gaussian2DKernel(pixel_fwhm / 2.355 * nbin,
                              x_size=125,
                              y_size=125).array.astype(np.float32)
    psfnew2 = psfnew / np.max(psfnew) * nc
    cf = psf_poly_fit(psfnew, nbin=nbin)
    return psfnew2, cf, nc, nbin
Example #6
0
def get_gaussian_psf_template(pixel_fwhm=3., nbin=5, normalization='max'):
    nc = 25
    psfnew = Gaussian2DKernel((pixel_fwhm / 2.355) * nbin,
                              x_size=125,
                              y_size=125).array.astype(np.float32)

    if normalization == 'max':
        print('Normalizing PSF by kernel maximum')
        psfnew /= np.max(psfnew)
        psfnew /= 4 * np.pi * (pixel_fwhm / 2.355)**2
    else:
        print('Normalizing PSF by kernel sum')
        psfnew *= nc
    cf = psf_poly_fit(psfnew, nbin=nbin)
    return psfnew, cf, nc, nbin
Example #7
0
    def get_psf(self,
                ifield=4,
                band=0,
                nx=1024,
                ny=1024,
                multfac=7.0,
                nbin=0.,
                poly_fit=True,
                nwide=17,
                width_psf_temp=50):

        beta, rc, norm = find_psf_params(self.ciberdir + '/data/psfparams.txt',
                                         tm=band + 1,
                                         field=self.ciber_field_dict[ifield])

        Nlarge = nx + 30 + 30

        radmap = make_radius_map(
            2 * Nlarge + nbin, 2 * Nlarge + nbin, Nlarge + nbin, Nlarge + nbin,
            rc) * multfac  # is the input supposed to be 2d?

        self.psf_full = norm * np.power(1 + radmap, -3 * beta / 2.)
        self.psf_full /= np.sum(self.psf_full)
        self.psf_template = self.psf_full[Nlarge - nwide:Nlarge + nwide + 1,
                                          Nlarge - nwide:Nlarge + nwide + 1]
        # self.psf_template = self.psf_full[Nlarge-nwide:Nlarge+nwide, Nlarge-nwide:Nlarge+nwide]

        print('imap center has shape', self.psf_template.shape)

        if poly_fit:

            psf = np.zeros((70, 70))
            psf[0:35, 0:35] = self.psf_template

            psf = np.array(
                Image.fromarray(psf).resize((350, 350),
                                            resample=Image.LANCZOS))
            # psf = scipy.misc.imresize(psf, (350, 350), interp='lanczos', mode='F') # deprecated for Python 3
            psfnew = np.array(psf[0:175, 0:175])

            psfnew[0:173, 0:173] = psf[2:175,
                                       2:175]  # shift due to lanczos kernel
            psfnew[0:173, 0:173] = psf[2:175,
                                       2:175]  # shift due to lanczos kernel
            self.cf = psf_poly_fit(psfnew, nbin=5)

        return beta, rc, norm
Example #8
0
import os
from image_eval import image_model_eval, psf_poly_fit
from galaxy import retr_sers, retr_tranphon, to_moments

pixel_scale = 0.2  # arcsec
sigma_psf = 0.297  # arcsec, fwhm=0.7/2.355
sigma_in_pix = sigma_psf / pixel_scale
nc = 25  # PSF postage stamp size
nbin = 5  # upsampling

x = np.arange(-(nc / 2), (nc / 2) + 1, 1 / float(nbin))
r2 = x[:, None] * x[:, None] + x[None, :] * x[None, :]
psf = np.exp(-r2 /
             (2. * sigma_in_pix * sigma_in_pix)) / (2 * np.pi * sigma_in_pix *
                                                    sigma_in_pix)
cf = psf_poly_fit(psf, nbin=nbin)
npar = cf.shape[0]

types = np.loadtxt('Data/phoSim_example.txt',
                   skiprows=9,
                   usecols=12,
                   dtype='string')
stars = (types == 'point')
galxs = (types == 'sersic2d')
mags = np.loadtxt('Data/phoSim_example.txt',
                  skiprows=9,
                  usecols=4,
                  dtype=np.float32)
star_mags = mags[stars]
galx_mags = mags[galxs]
rads = np.loadtxt('Data/phoSim_example.txt',
Example #9
0
def writ_truedata():

    pathlion = os.environ["LION_PATH"]
    sys.path.insert(0, pathlion)
    from image_eval import image_model_eval, psf_poly_fit

    dictglob = dict()

    sersindx = 2.
    numbsidegrid = 20

    pathliondata = os.environ["LION_DATA_PATH"] + '/data/'
    pathlionimag = os.environ["LION_DATA_PATH"] + '/imag/'
    fileobjt = open(pathliondata + 'sdss.0921_psf.txt')
    numbsidepsfn, factsamp = [np.int32(i) for i in fileobjt.readline().split()]
    fileobjt.close()

    psfn = np.loadtxt(pathliondata + 'sdss.0921_psf.txt',
                      skiprows=1).astype(np.float32)
    cpsf = psf_poly_fit(psfn, factsamp)
    cpsf = cpsf.reshape((-1, cpsf.shape[2]))

    np.random.seed(0)
    numbside = [100, 100]

    # generate stars
    numbstar = 1
    fluxdistslop = np.float32(2.0)
    minmflux = np.float32(250.)
    logtflux = np.random.exponential(scale=1. / (fluxdistslop - 1.),
                                     size=numbstar).astype(np.float32)

    dictglob['numbstar'] = numbstar
    dictglob['xposstar'] = (np.random.uniform(size=numbstar) *
                            (numbside[0] - 1)).astype(np.float32)
    dictglob['yposstar'] = (np.random.uniform(size=numbstar) *
                            (numbside[1] - 1)).astype(np.float32)
    dictglob['fluxdistslop'] = fluxdistslop
    dictglob['minmflux'] = minmflux
    dictglob['fluxstar'] = minmflux * np.exp(logtflux)
    dictglob['specstar'] = dictglob['fluxstar'][None, :]

    dictglob['back'] = np.float32(179.)
    dictglob['gain'] = np.float32(4.62)

    # generate galaxies
    numbgalx = 100
    dictglob['numbgalx'] = numbgalx

    dictglob['xposgalx'] = (np.random.uniform(size=numbgalx) *
                            (numbside[0] - 1)).astype(np.float32)
    dictglob['yposgalx'] = (np.random.uniform(size=numbgalx) *
                            (numbside[1] - 1)).astype(np.float32)

    dictglob['sizegalx'] = samp_powr(2., 10., 1.5, size=numbgalx)
    dictglob['avecfrst'] = (2. * np.random.uniform(size=numbgalx) - 1.).astype(
        np.float32)
    dictglob['avecseco'] = (2. * np.random.uniform(size=numbgalx) - 1.).astype(
        np.float32)
    dictglob['avecthrd'] = (2. * np.random.uniform(size=numbgalx) - 1.).astype(
        np.float32)
    mgtd = np.sqrt(dictglob['avecfrst']**2 + dictglob['avecseco']**2 +
                   dictglob['avecthrd']**2)
    dictglob['avecfrst'] /= mgtd
    dictglob['avecseco'] /= mgtd
    dictglob['avecthrd'] /= mgtd
    dictglob['specgalx'] = samp_powr(2500., 25000., 2., size=numbgalx)[None, :]
    dictglob['sbrtgalx'] = retr_sbrt(dictglob['specgalx'],
                                     dictglob['sizegalx'], sersindx)

    gdat = gdatstrt()

    listsersindx = [0.5, 1., 2., 4., 6., 8., 10.]
    for sersindx in listsersindx:
        gdat.sersindx = sersindx
        gridphon, amplphon = retr_sers(numbsidegrid=numbsidegrid,
                                       sersindx=sersindx,
                                       pathplot=pathlionimag)

        gridphon, amplphon = retr_sers(numbsidegrid=numbsidegrid,
                                       sersindx=sersindx,
                                       pathplot=pathlionimag)
        numbphongalx = (numbsidegrid + 1)**2
        numbener = 1
        numbphon = numbgalx * numbphongalx
        xposphon = np.empty(numbphon)
        yposphon = np.empty(numbphon)
        specphon = np.empty((numbener, numbphon))
        for k in range(dictglob['numbgalx']):
            indx = np.arange(k * numbphongalx, (k + 1) * numbphongalx)
            gridphontemp = gridphon * dictglob['sizegalx'][k]
            xposphon[indx], yposphon[indx], specphon[:, indx] = retr_tranphon(gridphontemp, amplphon, dictglob['xposgalx'][k], dictglob['yposgalx'][k], dictglob['specgalx'][:, k], \
                                                                                                 dictglob['avecfrst'][k], dictglob['avecseco'][k], dictglob['avecthrd'][k], 'fris')

        xposcand = np.concatenate(
            (xposphon, dictglob['xposstar'])).astype(np.float32)
        yposcand = np.concatenate(
            (yposphon, dictglob['yposstar'])).astype(np.float32)
        speccand = np.concatenate((specphon, dictglob['specstar']),
                                  axis=1).astype(np.float32)

        indx = np.where((xposcand < 100.) & (xposcand > 0.) & (yposcand < 100.)
                        & (yposcand > 0.))[0]
        xposcand = xposcand[indx]
        yposcand = yposcand[indx]
        speccand = speccand[:, indx]

        # generate data
        datacnts = image_model_eval(xposcand, yposcand, speccand[0, :],
                                    dictglob['back'], numbside, numbsidepsfn,
                                    cpsf)
        #datacnts[datacnts < 1] = 1. # maybe some negative pixels
        variance = datacnts / dictglob['gain']
        datacnts += (np.sqrt(variance) *
                     np.random.normal(size=(numbside[1], numbside[0]))).astype(
                         np.float32)
        dictglob['datacnts'] = datacnts
        # auxiliary data
        dictglob['numbside'] = numbside
        dictglob['psfn'] = psfn

        # write data to disk
        path = pathliondata + 'true.h5'
        filearry = h5py.File(path, 'w')
        for attr, valu in dictglob.iteritems():
            filearry.create_dataset(attr, data=valu)
        filearry.close()

        gdat.pathlionimag = pathlionimag
        plot_cntsmaps(gdat, datacnts, 'truedatacnts')
        plot_cntsmaps(gdat,
                      datacnts,
                      'truedatacntsphon',
                      xposcand=xposcand,
                      yposcand=yposcand,
                      speccand=speccand)