Exemplo n.º 1
0
def _GenerateFromRandomBinomial(param, param_name, base, value_type):
    """@brief Return a random value drawn from a Binomial distribution
    """
    if 'rng' not in base:
        raise ValueError("No base['rng'] available for %s.type = RandomBinomial"%param_name)
    rng = base['rng']

    req = {}
    opt = { 'p' : float }

    # Let N be optional for bool, since N=1 is the only value that makes sense.
    if value_type is bool:
        opt['N'] = int
    else:
        req['N'] = int
    kwargs, safe = GetAllParams(param, param_name, base, req=req, opt=opt)

    N = kwargs.get('N',1)
    p = kwargs.get('p',0.5)
    if value_type is bool and N != 1:
        raise ValueError("N must = 1 for %s.type = RandomBinomial used in bool context"%param_name)

    dev = galsim.BinomialDeviate(rng,N=N,p=p)
    val = dev()

    #print base['obj_num'],'RandomBinomial: ',val
    return val, False
Exemplo n.º 2
0
def _GenerateFromRandomBinomial(config, base, value_type):
    """@brief Return a random value drawn from a Binomial distribution
    """
    rng = galsim.config.GetRNG(config, base)

    req = {}
    opt = { 'p' : float }

    # Let N be optional for bool, since N=1 is the only value that makes sense.
    if value_type is bool:
        opt['N'] = int
    else:
        req['N'] = int
    kwargs, safe = galsim.config.GetAllParams(config, base, req=req, opt=opt)

    N = kwargs.get('N',1)
    p = kwargs.get('p',0.5)
    if value_type is bool and N != 1:
        raise ValueError("N must = 1 for type = RandomBinomial used in bool context")

    dev = galsim.BinomialDeviate(rng,N=N,p=p)
    val = dev()

    #print(base['obj_num'],'RandomBinomial: ',val)
    return val, False
Exemplo n.º 3
0
def make_a_galaxy(ud, wcs, psf, affine):
    """
    Method to make a single galaxy object and return stamp for 
    injecting into larger GalSim image
    """
    # Choose a random RA, Dec around the sky_center.
    # Note that for this to come out close to a square shape, we need to account for the
    # cos(dec) part of the metric: ds^2 = dr^2 + r^2 d(dec)^2 + r^2 cos^2(dec) d(ra)^2
    # So need to calculate dec first.
    dec = center_dec + (ud() - 0.5) * image_ysize_arcsec * galsim.arcsec
    ra = center_ra + (
        ud() - 0.5) * image_xsize_arcsec / numpy.cos(dec) * galsim.arcsec
    world_pos = galsim.CelestialCoord(ra, dec)

    # We will need the image position as well, so use the wcs to get that
    image_pos = wcs.toImage(world_pos)

    # We also need this in the tangent plane, which we call "world coordinates" here,
    # since the PowerSpectrum class is really defined on that plane, not in (ra,dec).
    # This is still an x/y corrdinate
    uv_pos = affine.toWorld(image_pos)

    # Draw the redshift from a power law distribution: N(f) ~ f^-2
    # TAKEN FROM DEMO9.PY
    redshift_dist = galsim.DistDeviate(ud,
                                       function=lambda x: x**-2,
                                       x_min=0.3,
                                       x_max=1.0)
    gal_z = redshift_dist()

    # Get the reduced shears and magnification at this point
    nfw_shear, mu = nfw_lensing(nfw, uv_pos, gal_z)
    g1 = nfw_shear.g1
    g2 = nfw_shear.g2

    binom = galsim.BinomialDeviate(ud, N=1, p=0.6)
    real = binom()

    if real:
        # For real galaxies, we will want to whiten the noise in the image (below).
        gal = cosmos_cat.makeGalaxy(gal_type='real',
                                    rng=ud,
                                    noise_pad_size=14.3)
    else:
        gal = cosmos_cat.makeGalaxy(gal_type='parametric', rng=ud)

    # Apply a random rotation
    theta = ud() * 2.0 * numpy.pi * galsim.radians
    gal = gal.rotate(theta)

    # Rescale the flux to match our telescope configuration.
    # This automatically scales up the noise variance by flux_scaling**2.
    gal *= flux_scaling

    # Apply the cosmological (reduced) shear and magnification at this position using a single
    # GSObject method.
    try:
        gal = gal.lens(g1, g2, mu)
    except:
        print("could not lens galaxy, setting default values...")
        g1 = 0.0
        g2 = 0.0
        mu = 1.0

    # Generate PSF at location of galaxy
    # Convolve galaxy image with the PSF.
    this_psf = psf.getPSF(image_pos)
    #final_psf=galsim.Convolve(this_psf,optics)
    gsp = galsim.GSParams(maximum_fft_size=16384)
    final = galsim.Convolve([this_psf, gal], gsparams=gsp)

    # Account for the fractional part of the position
    # cf. demo9.py for an explanation of this nominal position stuff.
    x_nominal = image_pos.x + 0.5
    y_nominal = image_pos.y + 0.5
    ix_nominal = int(math.floor(x_nominal + 0.5))
    iy_nominal = int(math.floor(y_nominal + 0.5))
    dx = x_nominal - ix_nominal
    dy = y_nominal - iy_nominal
    offset = galsim.PositionD(dx, dy)
    position = [ix_nominal, iy_nominal, ra.deg, dec.deg]

    # We use method='no_pixel' here because the SDSS PSF image that we are using includes the
    # pixel response already.
    stamp = final.drawImage(scale=2.06, offset=offset, method='no_pixel')

    # If desired, one can also draw the PSF and output its moments too, as:
    #  psf_stamp = psf.drawImage(wcs=wcs.local(image_pos), offset=offset, method='no_pixel')

    # Recenter the stamp at the desired position:
    stamp.setCenter(ix_nominal, iy_nominal)

    new_variance = 0.0

    if real:
        if True:
            # We use the symmetrizing option here.
            new_variance = stamp.symmetrizeNoise(final.noise, 8)
        else:
            # Here is how you would do it if you wanted to fully whiten the image.
            new_variance = stamp.whitenNoise(final.noise)

    galaxy_truth = truth()
    galaxy_truth.ra = ra.deg
    galaxy_truth.dec = dec.deg
    galaxy_truth.x = ix_nominal
    galaxy_truth.y = iy_nominal
    galaxy_truth.g1 = g1
    galaxy_truth.g2 = g2
    galaxy_truth.mu = mu
    galaxy_truth.z = gal_z
    galaxy_truth.variance = new_variance

    return stamp, galaxy_truth
Exemplo n.º 4
0
def main(argv):
    """
    Make images using constant PSF and variable shear:
      - The main image is 2048 x 2048 pixels.
      - Pixel scale is 0.2 arcsec/pixel, hence the image is about 0.11 degrees on a side.
      - Applied shear is from a cosmological power spectrum read in from file.
      - The PSF is a real one from SDSS, and corresponds to a convolution of atmospheric PSF,
        optical PSF, and pixel response, which has been sampled at pixel centers.  We used a PSF
        from SDSS in order to have a PSF profile that could correspond to what you see with a real
        telescope. However, in order that the galaxy resolution not be too poor, we tell GalSim that
        the pixel scale for that PSF image is 0.2" rather than 0.396".  We are simultaneously lying
        about the intrinsic size of the PSF and about the pixel scale when we do this.
      - The galaxies come from COSMOSCatalog, which can produce either RealGalaxy profiles
        (like in demo10) and parametric fits to those profiles.  We choose 30% of the galaxies
        to use the images, and the other 60% to use the parametric fits
      - The real galaxy images include some initial correlated noise from the original HST
        observation.  However, we whiten the noise of the final image so the final image has
        stationary Gaussian noise, rather than correlated noise.
    """
    logging.basicConfig(format="%(message)s",
                        level=logging.INFO,
                        stream=sys.stdout)
    logger = logging.getLogger("demo11")

    # Define some parameters we'll use below.
    # Normally these would be read in from some parameter file.

    pixel_scale = 0.2  # arcsec/pixel
    image_size = 2048  # size of image in pixels
    image_size_arcsec = image_size * pixel_scale  # size of big image in each dimension (arcsec)
    noise_variance = 5.e4  # ADU^2  (Just use simple Gaussian noise here.)
    nobj = 288  # number of galaxies in entire field
    # (This corresponds to 8 galaxies / arcmin^2)
    grid_spacing = 90.0  # The spacing between the samples for the power spectrum
    # realization (arcsec)
    tel_diam = 4  # Let's figure out the flux for a 4 m class telescope
    exp_time = 300  # exposing for 300 seconds.
    center_ra = 19.3 * galsim.hours  # The RA, Dec of the center of the image on the sky
    center_dec = -33.1 * galsim.degrees

    # The catalog returns objects that are appropriate for HST in 1 second exposures.  So for our
    # telescope we scale up by the relative area and exposure time.  Note that what is important is
    # the *effective* area after taking into account obscuration.  For HST, the telescope diameter
    # is 2.4 but there is obscuration (a linear factor of 0.33).  Here, we assume that the telescope
    # we're simulating effectively has no obscuration factor.  We're also ignoring the pi/4 factor
    # since it appears in the numerator and denominator, so we use area = diam^2.
    hst_eff_area = 2.4**2 * (1. - 0.33**2)
    flux_scaling = (tel_diam**2 / hst_eff_area) * exp_time

    # random_seed is used for both the power spectrum realization and the random properties
    # of the galaxies.
    random_seed = 24783923

    file_name = os.path.join('output', 'tabulated_power_spectrum.fits.fz')

    logger.info('Starting demo script 11')

    # Read in galaxy catalog
    # The COSMOSCatalog uses the same input file as we have been using for RealGalaxyCatalogs
    # along with a second file called real_galaxy_catalog_23.5_examples_fits.fits, which stores
    # the information about the parameteric fits.  There is no need to specify the second file
    # name, since the name is derivable from the name of the main catalog.
    if True:
        # The catalog we distribute with the GalSim code only has 100 galaxies.
        # The galaxies will typically be reused several times here.
        cat_file_name = 'real_galaxy_catalog_23.5_example.fits'
        dir = 'data'
        cosmos_cat = galsim.COSMOSCatalog(cat_file_name, dir=dir)
    else:
        # If you've run galsim_download_cosmos, you can leave out the cat_file_name and dir
        # to use the full COSMOS catalog with 56,000 galaxies in it.
        cosmos_cat = galsim.COSMOSCatalog()
    logger.info('Read in %d galaxies from catalog', cosmos_cat.nobjects)

    # Setup the PowerSpectrum object we'll be using:
    # To do this, we first have to read in the tabulated shear power spectrum, often denoted
    # C_ell(ell), where ell has units of inverse angle and C_ell has units of angle^2.  However,
    # GalSim works in the flat-sky approximation, so we use this notation interchangeably with
    # P(k).  GalSim does not calculate shear power spectra for users, who must be able to provide
    # their own (or use the examples in the repository).
    #
    # Here we use a tabulated power spectrum from iCosmo (http://icosmo.org), with the following
    # cosmological parameters and survey design:
    # H_0 = 70 km/s/Mpc
    # Omega_m = 0.25
    # Omega_Lambda = 0.75
    # w_0 = -1.0
    # w_a = 0.0
    # n_s = 0.96
    # sigma_8 = 0.8
    # Smith et al. prescription for the non-linear power spectrum.
    # Eisenstein & Hu transfer function with wiggles.
    # Default dN/dz with z_med = 1.0
    # The file has, as required, just two columns which are k and P(k).  However, iCosmo works in
    # terms of ell and C_ell; ell is inverse radians and C_ell in radians^2.  Since GalSim tends to
    # work in terms of arcsec, we have to tell it that the inputs are radians^-1 so it can convert
    # to store in terms of arcsec^-1.
    pk_file = os.path.join('data', 'cosmo-fid.zmed1.00.out')
    ps = galsim.PowerSpectrum(pk_file, units=galsim.radians)
    # The argument here is "e_power_function" which defines the E-mode power to use.
    logger.info('Set up power spectrum from tabulated P(k)')

    # Now let's read in the PSF.  It's a real SDSS PSF, which means pixel scale of 0.396".  However,
    # the typical seeing is 1.2" and we want to simulate better seeing, so we will just tell GalSim
    # that the pixel scale is 0.2".  We have to be careful with SDSS PSF images, as they have an
    # added 'soft bias' of 1000 which has been removed before creation of this file, so that the sky
    # level is properly zero.  Also, the file is bzipped, to demonstrate the ability of GalSim
    # handle this kind of compressed file (among others).  We read the image directly into an
    # InterpolatedImage GSObject, so we can manipulate it as needed (here, the only manipulation
    # needed is convolution).  The flux is 1 as needed for a PSF.
    psf_file = os.path.join('data', 'example_sdss_psf_sky0.fits.bz2')
    psf = galsim.InterpolatedImage(psf_file, scale=pixel_scale, flux=1.)
    logger.info('Read in PSF image from bzipped FITS file')

    # Setup the image:
    full_image = galsim.ImageF(image_size, image_size)

    # The default convention for indexing an image is to follow the FITS standard where the
    # lower-left pixel is called (1,1).  However, this can be counter-intuitive to people more
    # used to C or python indexing, where indices start at 0.  It is possible to change the
    # coordinates of the lower-left pixel with the methods `setOrigin`.  For this demo, we
    # switch to 0-based indexing, so the lower-left pixel will be called (0,0).
    full_image.setOrigin(0, 0)

    # As for demo10, we use random_seed for the random numbers required for the
    # whole image.  In this case, both the power spectrum realization and the noise on the
    # full image we apply later.
    rng = galsim.BaseDeviate(random_seed)

    # We want to make random positions within our image.  However, currently for shears from a power
    # spectrum we first have to get shears on a grid of positions, and then we can choose random
    # positions within that.  So, let's make the grid.  We're going to make it as large as the
    # image, with grid points spaced by 90 arcsec (hence interpolation only happens below 90"
    # scales, below the interesting scales on which we want the shear power spectrum to be
    # represented exactly).  The lensing engine wants positions in arcsec, so calculate that:
    ps.buildGrid(grid_spacing=grid_spacing,
                 ngrid=int(math.ceil(image_size_arcsec / grid_spacing)),
                 rng=rng)
    logger.info('Made gridded shears')

    # We keep track of how much noise is already in the image from the RealGalaxies.
    # The default initial value is all pixels = 0.
    noise_image = galsim.ImageF(image_size, image_size)
    noise_image.setOrigin(0, 0)

    # Make a slightly non-trivial WCS.  We'll use a slightly rotated coordinate system
    # and center it at the image center.
    theta = 0.17 * galsim.degrees
    # ( dudx  dudy ) = ( cos(theta)  -sin(theta) ) * pixel_scale
    # ( dvdx  dvdy )   ( sin(theta)   cos(theta) )
    # Aside: You can call numpy trig functions on Angle objects directly, rather than getting
    #        their values in radians first.  Or, if you prefer, you can write things like
    #        theta.sin() or theta.cos(), which are equivalent.
    dudx = numpy.cos(theta) * pixel_scale
    dudy = -numpy.sin(theta) * pixel_scale
    dvdx = numpy.sin(theta) * pixel_scale
    dvdy = numpy.cos(theta) * pixel_scale
    image_center = full_image.true_center
    affine = galsim.AffineTransform(dudx,
                                    dudy,
                                    dvdx,
                                    dvdy,
                                    origin=full_image.true_center)

    # We can also put it on the celestial sphere to give it a bit more realism.
    # The TAN projection takes a (u,v) coordinate system on a tangent plane and projects
    # that plane onto the sky using a given point as the tangent point.  The tangent
    # point should be given as a CelestialCoord.
    sky_center = galsim.CelestialCoord(ra=center_ra, dec=center_dec)

    # The third parameter, units, defaults to arcsec, but we make it explicit here.
    # It sets the angular units of the (u,v) intermediate coordinate system.
    wcs = galsim.TanWCS(affine, sky_center, units=galsim.arcsec)
    full_image.wcs = wcs

    # Now we need to loop over our objects:
    for k in range(nobj):
        time1 = time.time()
        # The usual random number generator using a different seed for each galaxy.
        ud = galsim.UniformDeviate(random_seed + k + 1)

        # Choose a random RA, Dec around the sky_center.
        # Note that for this to come out close to a square shape, we need to account for the
        # cos(dec) part of the metric: ds^2 = dr^2 + r^2 d(dec)^2 + r^2 cos^2(dec) d(ra)^2
        # So need to calculate dec first.
        dec = center_dec + (ud() - 0.5) * image_size_arcsec * galsim.arcsec
        ra = center_ra + (
            ud() - 0.5) * image_size_arcsec / numpy.cos(dec) * galsim.arcsec
        world_pos = galsim.CelestialCoord(ra, dec)

        # We will need the image position as well, so use the wcs to get that
        image_pos = wcs.toImage(world_pos)

        # We also need this in the tangent plane, which we call "world coordinates" here,
        # since the PowerSpectrum class is really defined on that plane, not in (ra,dec).
        uv_pos = affine.toWorld(image_pos)

        # Get the reduced shears and magnification at this point
        g1, g2, mu = ps.getLensing(pos=uv_pos)

        # Now we will have the COSMOSCatalog make a galaxy profile for us.  It can make either
        # a RealGalaxy using the original HST image and PSF, or a parametric model based on
        # parametric fits to the light distribution of the HST observation.  The parametric
        # models are either a Sersic fit to the data or a bulge + disk fit according to which
        # one gave the better chisq value.  We will select a galaxy at random from the catalog.
        # One could easily do this by choosing an index = int(ud() * cosmos_cat.nobjects), but
        # we will instead allow the catalog to choose a random galaxy for us.  It will remove any
        # selection effects involved in postage stamp creation using weights that are stored in
        # the catalog.  (If for some reason you prefer not to do that, you can always choose a
        # purely random index yourself using int(ud() * cosmos_cat.nobjects).)  We employ this
        # random selection by simply failing to specify an index or identifier for a galaxy, in
        # which case it chooses a random one.

        # First determine whether we will make a real galaxy (`gal_type = 'real'`) or a parametric
        # galaxy (`gal_type = 'parametric'`).  The real galaxies take longer to render, so for this
        # script, we just use them 30% of the time and use parametric galaxies the other 70%.

        # We could just use `ud()<0.3` for this, but instead we introduce another Deviate type
        # available in GalSim that we haven't used yet: BinomialDeviate.
        # It takes an N and p value and returns integers according to a binomial distribution.
        # i.e. How many heads you get after N flips if each flip has a chance, p, of being heads.
        binom = galsim.BinomialDeviate(ud, N=1, p=0.3)
        real = binom()

        if real:
            # For real galaxies, we will want to whiten the noise in the image (below).
            # When whitening the image, we need to make sure the original correlated noise is
            # present throughout the whole image, otherwise the whitening will do the wrong thing
            # to the parts of the image that don't include the original image.  The RealGalaxy
            # stores the correct noise profile to use as the gal.noise attribute.  This noise
            # profile is automatically updated as we shear, dilate, convolve, etc.  But we need to
            # tell it how large to pad with this noise by hand.  This is a bit complicated for the
            # code to figure out on its own, so we have to supply the size for noise padding
            # with the noise_pad_size parameter.

            # The large galaxies will render fine without any noise padding, but the postage stamp
            # for the smaller galaxies will be sized appropriately for the PSF, which may make the
            # stamp larger than the original galaxy image.  The psf image is 40 x 40, although
            # the bright part is much more concentrated than that.  If we pad out the galaxy image
            # to at least 40 x sqrt(2), we should be safe even if the galaxy image is rotated
            # with respect to the psf image.
            #     noise_pad_size = 40 * sqrt(2) * 0.2 arcsec/pixel = 11.3 arcsec
            gal = cosmos_cat.makeGalaxy(gal_type='real',
                                        rng=ud,
                                        noise_pad_size=11.3)
        else:
            gal = cosmos_cat.makeGalaxy(gal_type='parametric', rng=ud)

        # Apply a random rotation
        theta = ud() * 2.0 * numpy.pi * galsim.radians
        gal = gal.rotate(theta)

        # Rescale the flux to match our telescope configuration.
        # This automatically scales up the noise variance by flux_scaling**2.
        gal *= flux_scaling

        # Apply the cosmological (reduced) shear and magnification at this position using a single
        # GSObject method.
        gal = gal.lens(g1, g2, mu)

        # Convolve with the PSF.
        final = galsim.Convolve(psf, gal)

        # Account for the fractional part of the position
        # cf. demo9.py for an explanation of this nominal position stuff.
        x_nominal = image_pos.x + 0.5
        y_nominal = image_pos.y + 0.5
        ix_nominal = int(math.floor(x_nominal + 0.5))
        iy_nominal = int(math.floor(y_nominal + 0.5))
        dx = x_nominal - ix_nominal
        dy = y_nominal - iy_nominal
        offset = galsim.PositionD(dx, dy)

        # We use method='no_pixel' here because the SDSS PSF image that we are using includes the
        # pixel response already.
        stamp = final.drawImage(wcs=wcs.local(image_pos),
                                offset=offset,
                                method='no_pixel')

        # Recenter the stamp at the desired position:
        stamp.setCenter(ix_nominal, iy_nominal)

        # Find the overlapping bounds:
        bounds = stamp.bounds & full_image.bounds

        # Now, if we are using a real galaxy, we want to ether whiten or at least symmetrize the
        # noise on the postage stamp to avoid having to deal with correlated noise in any kind of
        # image processing you would want to do on the final image.  (Like measure galaxy shapes.)

        # Galsim automatically propagates the noise correctly from the initial RealGalaxy object
        # through the applied shear, distortion, rotation, and convolution into the final object's
        # noise attribute.  To make the noise fully white, use the image.whitenNoise() method.
        # The returned value is the variance of the Gaussian noise that is present after the
        # whitening process.

        # However, this is often overkill for many applications.  If it is acceptable to merely end
        # up with noise with some degree of symmetry (say 4-fold or 8-fold symmetry), then you can
        # instead have GalSim just add enough noise to make the resulting noise have this kind of
        # symmetry.  Usually this requires adding significantly less additional noise, which means
        # you can have the resulting total variance be somewhat smaller.  The returned variance
        # corresponds to the zero-lag value of the noise correlation function, which will still have
        # off-diagonal elements.  We can do this step using the image.symmetrizeNoise() method.
        if real:
            if True:
                # We use the symmetrizing option here.
                new_variance = stamp.symmetrizeNoise(final.noise, 8)
            else:
                # Here is how you would do it if you wanted to fully whiten the image.
                new_variance = stamp.whitenNoise(final.noise)

            # We need to keep track of how much variance we have currently in the image, so when
            # we add more noise, we can omit what is already there.
            noise_image[bounds] += new_variance

        # Finally, add the stamp to the full image.
        full_image[bounds] += stamp[bounds]

        time2 = time.time()
        tot_time = time2 - time1
        logger.info('Galaxy %d: position relative to center = %s, t=%f s', k,
                    str(uv_pos), tot_time)

    # We already have some noise in the image, but it isn't uniform.  So the first thing to do is
    # to make the Gaussian noise uniform across the whole image.  We have a special noise class
    # that can do this.  VariableGaussianNoise takes an image of variance values and applies
    # Gaussian noise with the corresponding variance to each pixel.
    # So all we need to do is build an image with how much noise to add to each pixel to get us
    # up to the maximum value that we already have in the image.
    max_current_variance = numpy.max(noise_image.array)
    noise_image = max_current_variance - noise_image
    vn = galsim.VariableGaussianNoise(rng, noise_image)
    full_image.addNoise(vn)

    # Now max_current_variance is the noise level across the full image.  We don't want to add that
    # twice, so subtract off this much from the intended noise that we want to end up in the image.
    noise_variance -= max_current_variance

    # Now add Gaussian noise with this variance to the final image.  We have to do this step
    # at the end, rather than adding to individual postage stamps, in order to get the noise
    # level right in the overlap regions between postage stamps.
    noise = galsim.GaussianNoise(rng, sigma=math.sqrt(noise_variance))
    full_image.addNoise(noise)
    logger.info('Added noise to final large image')

    # Now write the image to disk.  It is automatically compressed with Rice compression,
    # since the filename we provide ends in .fz.
    full_image.write(file_name)
    logger.info('Wrote image to %r', file_name)

    # Compute some sky positions of some of the pixels to compare with the values of RA, Dec
    # that ds9 reports.  ds9 always uses (1,1) for the lower left pixel, so the pixel coordinates
    # of these pixels are different by 1, but you can check that the RA and Dec values are
    # the same as what GalSim calculates.
    ra_str = center_ra.hms()
    dec_str = center_dec.dms()
    logger.info('Center of image    is at RA %sh %sm %ss, DEC %sd %sm %ss',
                ra_str[0:3], ra_str[3:5], ra_str[5:], dec_str[0:3],
                dec_str[3:5], dec_str[5:])
    for (x, y) in [(0, 0), (0, image_size - 1), (image_size - 1, 0),
                   (image_size - 1, image_size - 1)]:
        world_pos = wcs.toWorld(galsim.PositionD(x, y))
        ra_str = world_pos.ra.hms()
        dec_str = world_pos.dec.dms()
        logger.info('Pixel (%4d, %4d) is at RA %sh %sm %ss, DEC %sd %sm %ss',
                    x, y, ra_str[0:3], ra_str[3:5], ra_str[5:], dec_str[0:3],
                    dec_str[3:5], dec_str[5:])
    logger.info(
        'ds9 reports these pixels as (1,1), (1,2048), etc. with the same RA, Dec.'
    )
Exemplo n.º 5
0
def test_bool_value():
    """Test various ways to generate a bool value
    """
    import time
    t1 = time.time()

    config = {
        'input': {
            'catalog': [{
                'dir': 'config_input',
                'file_name': 'catalog.txt'
            }, {
                'dir': 'config_input',
                'file_name': 'catalog.fits'
            }],
            'dict': [{
                'dir': 'config_input',
                'file_name': 'dict.p'
            }, {
                'dir': 'config_input',
                'file_name': 'dict.json'
            }, {
                'dir': 'config_input',
                'file_name': 'dict.yaml'
            }]
        },
        'val1': True,
        'val2': 1,
        'val3': 0.0,
        'str1': 'true',
        'str2': '0',
        'str3': 'yes',
        'str4': 'No',
        'cat1': {
            'type': 'Catalog',
            'col': 4
        },
        'cat2': {
            'type': 'Catalog',
            'col': 5
        },
        'cat3': {
            'type': 'Catalog',
            'num': 1,
            'col': 'bool1'
        },
        'cat4': {
            'type': 'Catalog',
            'num': 1,
            'col': 'bool2'
        },
        'ran1': {
            'type': 'Random'
        },
        'dev1': {
            'type': 'RandomBinomial',
            'N': 1
        },
        'dev2': {
            'type': 'RandomBinomial',
            'N': 1,
            'p': 0.5
        },
        'dev3': {
            'type': 'RandomBinomial',
            'p': 0.2
        },
        'seq1': {
            'type': 'Sequence'
        },
        'seq2': {
            'type': 'Sequence',
            'first': True,
            'repeat': 2
        },
        'list1': {
            'type': 'List',
            'items': ['yes', 'no', 'no']
        },
        'list2': {
            'type': 'List',
            'items': [0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0],
            'index': {
                'type': 'Sequence',
                'first': 10,
                'step': -3
            }
        },
        'dict1': {
            'type': 'Dict',
            'key': 'b'
        },
        'dict2': {
            'type': 'Dict',
            'num': 1,
            'key': 'b'
        },
        'dict3': {
            'type': 'Dict',
            'num': 2,
            'key': 'b'
        }
    }

    test_yaml = True
    try:
        galsim.config.ProcessInput(config)
    except:
        # We don't require PyYAML as a dependency, so if this fails, just remove the YAML dict.
        del config['input']['dict'][2]
        galsim.config.ProcessInput(config)
        test_yaml = False

    # Test direct values
    val1 = galsim.config.ParseValue(config, 'val1', config, bool)[0]
    np.testing.assert_equal(val1, True)

    val2 = galsim.config.ParseValue(config, 'val2', config, bool)[0]
    np.testing.assert_equal(val2, True)

    val3 = galsim.config.ParseValue(config, 'val3', config, bool)[0]
    np.testing.assert_equal(val3, False)

    # Test conversions from strings
    str1 = galsim.config.ParseValue(config, 'str1', config, bool)[0]
    np.testing.assert_equal(str1, True)

    str2 = galsim.config.ParseValue(config, 'str2', config, bool)[0]
    np.testing.assert_equal(str2, False)

    str3 = galsim.config.ParseValue(config, 'str3', config, bool)[0]
    np.testing.assert_equal(str3, True)

    str4 = galsim.config.ParseValue(config, 'str4', config, bool)[0]
    np.testing.assert_equal(str4, False)

    # Test values read from a Catalog
    cat1 = []
    cat2 = []
    cat3 = []
    cat4 = []
    config['index_key'] = 'obj_num'
    for k in range(5):
        config['obj_num'] = k
        cat1.append(galsim.config.ParseValue(config, 'cat1', config, bool)[0])
        cat2.append(galsim.config.ParseValue(config, 'cat2', config, bool)[0])
        cat3.append(galsim.config.ParseValue(config, 'cat3', config, bool)[0])
        cat4.append(galsim.config.ParseValue(config, 'cat4', config, bool)[0])

    np.testing.assert_array_equal(cat1, [1, 0, 1, 1, 0])
    np.testing.assert_array_equal(cat2, [1, 0, 0, 1, 0])
    np.testing.assert_array_equal(cat3, [1, 0, 1, 1, 0])
    np.testing.assert_array_equal(cat4, [1, 0, 0, 1, 0])

    # Test values generated from a uniform deviate
    rng = galsim.UniformDeviate(1234)
    config['rng'] = galsim.UniformDeviate(
        1234)  # A second copy starting with the same seed.
    for k in range(6):
        config['obj_num'] = k
        ran1 = galsim.config.ParseValue(config, 'ran1', config, bool)[0]
        np.testing.assert_equal(ran1, rng() < 0.5)

    # Test values generated from binomial deviate
    for k in range(6):
        config['obj_num'] = k
        dev = galsim.BinomialDeviate(rng, N=1)
        dev1 = galsim.config.ParseValue(config, 'dev1', config, bool)[0]
        np.testing.assert_almost_equal(dev1, dev())

        dev = galsim.BinomialDeviate(rng, N=1, p=0.5)
        dev2 = galsim.config.ParseValue(config, 'dev2', config, bool)[0]
        np.testing.assert_almost_equal(dev2, dev())

        dev = galsim.BinomialDeviate(rng, N=1, p=0.2)
        dev3 = galsim.config.ParseValue(config, 'dev3', config, bool)[0]
        np.testing.assert_almost_equal(dev3, dev())

    # Test values generated from a Sequence
    seq1 = []
    seq2 = []
    config['index_key'] = 'obj_num'
    for k in range(6):
        config['obj_num'] = k
        seq1.append(galsim.config.ParseValue(config, 'seq1', config, bool)[0])
        seq2.append(galsim.config.ParseValue(config, 'seq2', config, bool)[0])

    np.testing.assert_array_equal(seq1, [0, 1, 0, 1, 0, 1])
    np.testing.assert_array_equal(seq2, [1, 1, 0, 0, 1, 1])

    # Test values taken from a List
    list1 = []
    list2 = []
    config['index_key'] = 'file_num'
    for k in range(5):
        config['file_num'] = k
        list1.append(
            galsim.config.ParseValue(config, 'list1', config, bool)[0])
        list2.append(
            galsim.config.ParseValue(config, 'list2', config, bool)[0])

    np.testing.assert_array_equal(list1, [1, 0, 0, 1, 0])
    np.testing.assert_array_equal(list2, [0, 1, 1, 1, 0])

    # Test values read from a Dict
    dict = []
    dict.append(galsim.config.ParseValue(config, 'dict1', config, bool)[0])
    dict.append(galsim.config.ParseValue(config, 'dict2', config, bool)[0])
    if test_yaml:
        dict.append(galsim.config.ParseValue(config, 'dict3', config, bool)[0])
    else:
        dict.append(False)
    np.testing.assert_array_equal(dict, [True, False, False])

    t2 = time.time()
    print 'time for %s = %.2f' % (funcname(), t2 - t1)
Exemplo n.º 6
0
def test_int_value():
    """Test various ways to generate an int value
    """
    import time
    t1 = time.time()

    config = {
        'input': {
            'catalog': [{
                'dir': 'config_input',
                'file_name': 'catalog.txt'
            }, {
                'dir': 'config_input',
                'file_name': 'catalog.fits'
            }],
            'dict': [{
                'dir': 'config_input',
                'file_name': 'dict.p'
            }, {
                'dir': 'config_input',
                'file_name': 'dict.json'
            }, {
                'dir': 'config_input',
                'file_name': 'dict.yaml'
            }]
        },
        'val1': 9,
        'val2': float(8.7),  # Reading as int will drop the fraction.
        'val3': -400.8,  # Not floor - negatives will round up.
        'str1': '8',
        'str2': '-2',
        'cat1': {
            'type': 'Catalog',
            'col': 2
        },
        'cat2': {
            'type': 'Catalog',
            'col': 3
        },
        'cat3': {
            'type': 'Catalog',
            'num': 1,
            'col': 'int1'
        },
        'cat4': {
            'type': 'Catalog',
            'num': 1,
            'col': 'int2'
        },
        'ran1': {
            'type': 'Random',
            'min': 0,
            'max': 3
        },
        'ran2': {
            'type': 'Random',
            'min': -5,
            'max': 10
        },
        'dev1': {
            'type': 'RandomPoisson',
            'mean': 137
        },
        'dev2': {
            'type': 'RandomBinomial',
            'N': 17
        },
        'dev3': {
            'type': 'RandomBinomial',
            'N': 17,
            'p': 0.2
        },
        'seq1': {
            'type': 'Sequence'
        },
        'seq2': {
            'type': 'Sequence',
            'step': 3
        },
        'seq3': {
            'type': 'Sequence',
            'first': 1,
            'step': 5
        },
        'seq4': {
            'type': 'Sequence',
            'first': 10,
            'step': -2
        },
        'seq5': {
            'type': 'Sequence',
            'first': 1,
            'last': 2,
            'repeat': 2
        },
        'seq_file': {
            'type': 'Sequence',
            'index_key': 'file_num'
        },
        'seq_image': {
            'type': 'Sequence',
            'index_key': 'image_num'
        },
        'seq_obj': {
            'type': 'Sequence',
            'index_key': 'obj_num'
        },
        'seq_obj2': {
            'type': 'Sequence',
            'index_key': 'obj_num_in_file'
        },
        'list1': {
            'type': 'List',
            'items': [73, 8, 3]
        },
        'list2': {
            'type': 'List',
            'items': [6, 8, 1, 7, 3, 5, 1, 0, 6, 3, 8, 2],
            'index': {
                'type': 'Sequence',
                'first': 10,
                'step': -3
            }
        },
        'dict1': {
            'type': 'Dict',
            'key': 'i'
        },
        'dict2': {
            'type': 'Dict',
            'num': 1,
            'key': 'i'
        },
        'dict3': {
            'type': 'Dict',
            'num': 2,
            'key': 'i'
        },
        'sum1': {
            'type': 'Sum',
            'items': [72.3, '2', {
                'type': 'Dict',
                'key': 'i'
            }]
        }
    }

    test_yaml = True
    try:
        galsim.config.ProcessInput(config)
    except:
        # We don't require PyYAML as a dependency, so if this fails, just remove the YAML dict.
        del config['input']['dict'][2]
        galsim.config.ProcessInput(config)
        test_yaml = False

    # Test direct values
    val1 = galsim.config.ParseValue(config, 'val1', config, int)[0]
    np.testing.assert_equal(val1, 9)

    val2 = galsim.config.ParseValue(config, 'val2', config, int)[0]
    np.testing.assert_equal(val2, 8)

    val3 = galsim.config.ParseValue(config, 'val3', config, int)[0]
    np.testing.assert_equal(val3, -400)

    # Test conversions from strings
    str1 = galsim.config.ParseValue(config, 'str1', config, int)[0]
    np.testing.assert_equal(str1, 8)

    str2 = galsim.config.ParseValue(config, 'str2', config, int)[0]
    np.testing.assert_equal(str2, -2)

    # Test values read from a Catalog
    cat1 = []
    cat2 = []
    cat3 = []
    cat4 = []
    config['index_key'] = 'image_num'
    for k in range(5):
        config['image_num'] = k
        cat1.append(galsim.config.ParseValue(config, 'cat1', config, int)[0])
        cat2.append(galsim.config.ParseValue(config, 'cat2', config, int)[0])
        cat3.append(galsim.config.ParseValue(config, 'cat3', config, int)[0])
        cat4.append(galsim.config.ParseValue(config, 'cat4', config, int)[0])

    np.testing.assert_array_equal(cat1, [9, 0, -4, 9, 0])
    np.testing.assert_array_equal(cat2, [-3, 8, 17, -3, 8])
    np.testing.assert_array_equal(cat3, [9, 0, -4, 9, 0])
    np.testing.assert_array_equal(cat4, [-3, 8, 17, -3, 8])

    # Test values generated from a uniform deviate
    rng = galsim.UniformDeviate(1234)
    config['rng'] = galsim.UniformDeviate(
        1234)  # A second copy starting with the same seed.
    for k in range(6):
        config['obj_num'] = k
        ran1 = galsim.config.ParseValue(config, 'ran1', config, int)[0]
        np.testing.assert_equal(ran1, int(math.floor(rng() * 4)))

        ran2 = galsim.config.ParseValue(config, 'ran2', config, int)[0]
        np.testing.assert_equal(ran2, int(math.floor(rng() * 16)) - 5)

    # Test values generated from various other deviates
    for k in range(6):
        config['obj_num'] = k
        dev = galsim.PoissonDeviate(rng, mean=137)
        dev1 = galsim.config.ParseValue(config, 'dev1', config, int)[0]
        np.testing.assert_almost_equal(dev1, dev())

        dev = galsim.BinomialDeviate(rng, N=17)
        dev2 = galsim.config.ParseValue(config, 'dev2', config, int)[0]
        np.testing.assert_almost_equal(dev2, dev())

        dev = galsim.BinomialDeviate(rng, N=17, p=0.2)
        dev3 = galsim.config.ParseValue(config, 'dev3', config, int)[0]
        np.testing.assert_almost_equal(dev3, dev())

    # Test values generated from a Sequence
    seq1 = []
    seq2 = []
    seq3 = []
    seq4 = []
    seq5 = []
    config['index_key'] = 'obj_num'
    for k in range(6):
        config['obj_num'] = k
        seq1.append(galsim.config.ParseValue(config, 'seq1', config, int)[0])
        seq2.append(galsim.config.ParseValue(config, 'seq2', config, int)[0])
        seq3.append(galsim.config.ParseValue(config, 'seq3', config, int)[0])
        seq4.append(galsim.config.ParseValue(config, 'seq4', config, int)[0])
        seq5.append(galsim.config.ParseValue(config, 'seq5', config, int)[0])

    np.testing.assert_array_equal(seq1, [0, 1, 2, 3, 4, 5])
    np.testing.assert_array_equal(seq2, [0, 3, 6, 9, 12, 15])
    np.testing.assert_array_equal(seq3, [1, 6, 11, 16, 21, 26])
    np.testing.assert_array_equal(seq4, [10, 8, 6, 4, 2, 0])
    np.testing.assert_array_equal(seq5, [1, 1, 2, 2, 1, 1])

    # This is more like how the indexing actually happens in a regular config run:
    seq_file = []
    seq_image = []
    seq_obj = []
    seq_obj2 = []
    config['file_num'] = 0
    config['image_num'] = 0
    config['obj_num'] = 0
    for file_num in range(3):
        config['start_obj_num'] = config['obj_num']
        for image_num in range(2):
            for obj_num in range(5):
                seq_file.append(
                    galsim.config.ParseValue(config, 'seq_file', config,
                                             int)[0])
                seq_image.append(
                    galsim.config.ParseValue(config, 'seq_image', config,
                                             int)[0])
                seq_obj.append(
                    galsim.config.ParseValue(config, 'seq_obj', config,
                                             int)[0])
                seq_obj2.append(
                    galsim.config.ParseValue(config, 'seq_obj2', config,
                                             int)[0])
                config['obj_num'] += 1
            config['image_num'] += 1
        config['file_num'] += 1

    np.testing.assert_array_equal(seq_file, [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2
    ])
    np.testing.assert_array_equal(seq_image, [
        0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4,
        4, 5, 5, 5, 5, 5
    ])
    np.testing.assert_array_equal(seq_obj, [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
        20, 21, 22, 23, 24, 25, 26, 27, 28, 29
    ])
    np.testing.assert_array_equal(seq_obj2, [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
        4, 5, 6, 7, 8, 9
    ])

    # Test values taken from a List
    list1 = []
    list2 = []
    config['index_key'] = 'obj_num'
    for k in range(5):
        config['obj_num'] = k
        list1.append(galsim.config.ParseValue(config, 'list1', config, int)[0])
        list2.append(galsim.config.ParseValue(config, 'list2', config, int)[0])

    np.testing.assert_array_equal(list1, [73, 8, 3, 73, 8])
    np.testing.assert_array_equal(list2, [8, 0, 3, 8, 8])

    # Test values read from a Dict
    dict = []
    dict.append(galsim.config.ParseValue(config, 'dict1', config, int)[0])
    dict.append(galsim.config.ParseValue(config, 'dict2', config, int)[0])
    if test_yaml:
        dict.append(galsim.config.ParseValue(config, 'dict3', config, int)[0])
    else:
        dict.append(1)
    np.testing.assert_array_equal(dict, [17, -23, 1])

    sum1 = galsim.config.ParseValue(config, 'sum1', config, int)[0]
    np.testing.assert_almost_equal(sum1, 72 + 2 + 17)

    t2 = time.time()
    print 'time for %s = %.2f' % (funcname(), t2 - t1)
Exemplo n.º 7
0
def test_float_value():
    """Test various ways to generate a float value
    """
    import time
    t1 = time.time()

    config = {
        'input': {
            'catalog': [{
                'dir': 'config_input',
                'file_name': 'catalog.txt'
            }, {
                'dir': 'config_input',
                'file_name': 'catalog.fits'
            }],
            'dict': [{
                'dir': 'config_input',
                'file_name': 'dict.p'
            }, {
                'dir': 'config_input',
                'file_name': 'dict.json'
            }, {
                'dir': 'config_input',
                'file_name': 'dict.yaml'
            }]
        },
        'val1': 9.9,
        'val2': int(400),
        'str1': '8.73',
        'str2': '2.33e-9',
        'str3': '6.e-9',
        'cat1': {
            'type': 'Catalog',
            'col': 0
        },
        'cat2': {
            'type': 'Catalog',
            'col': 1
        },
        'cat3': {
            'type': 'Catalog',
            'num': 1,
            'col': 'float1'
        },
        'cat4': {
            'type': 'Catalog',
            'num': 1,
            'col': 'float2'
        },
        'ran1': {
            'type': 'Random',
            'min': 0.5,
            'max': 3
        },
        'ran2': {
            'type': 'Random',
            'min': -5,
            'max': 0
        },
        'gauss1': {
            'type': 'RandomGaussian',
            'sigma': 1
        },
        'gauss2': {
            'type': 'RandomGaussian',
            'sigma': 3,
            'mean': 4
        },
        'gauss3': {
            'type': 'RandomGaussian',
            'sigma': 1.5,
            'min': -2,
            'max': 2
        },
        'gauss4': {
            'type': 'RandomGaussian',
            'sigma': 0.5,
            'min': 0,
            'max': 0.8
        },
        'gauss5': {
            'type': 'RandomGaussian',
            'sigma': 0.3,
            'mean': 0.5,
            'min': 0,
            'max': 0.5
        },
        'dist1': {
            'type': 'RandomDistribution',
            'function': 'config_input/distribution.txt',
            'interpolant': 'linear'
        },
        'dist2': {
            'type': 'RandomDistribution',
            'function': 'config_input/distribution2.txt',
            'interpolant': 'linear'
        },
        'dist3': {
            'type': 'RandomDistribution',
            'function': 'x*x',
            'x_min': 0.,
            'x_max': 2.0
        },
        'dev1': {
            'type': 'RandomPoisson',
            'mean': 137
        },
        'dev2': {
            'type': 'RandomBinomial',
            'N': 17
        },
        'dev3': {
            'type': 'RandomBinomial',
            'N': 17,
            'p': 0.2
        },
        'dev4': {
            'type': 'RandomWeibull',
            'a': 1.7,
            'b': 4.3
        },
        'dev5': {
            'type': 'RandomGamma',
            'k': 1,
            'theta': 4
        },
        'dev6': {
            'type': 'RandomGamma',
            'k': 1.9,
            'theta': 4.1
        },
        'dev7': {
            'type': 'RandomChi2',
            'n': 17
        },
        'seq1': {
            'type': 'Sequence'
        },
        'seq2': {
            'type': 'Sequence',
            'step': 0.1
        },
        'seq3': {
            'type': 'Sequence',
            'first': 1.5,
            'step': 0.5
        },
        'seq4': {
            'type': 'Sequence',
            'first': 10,
            'step': -2
        },
        'seq5': {
            'type': 'Sequence',
            'first': 1,
            'last': 2.1,
            'repeat': 2
        },
        'list1': {
            'type': 'List',
            'items': [73, 8.9, 3.14]
        },
        'list2': {
            'type': 'List',
            'items':
            [0.6, 1.8, 2.1, 3.7, 4.3, 5.5, 6.1, 7.0, 8.6, 9.3, 10.8, 11.2],
            'index': {
                'type': 'Sequence',
                'first': 10,
                'step': -3
            }
        },
        'dict1': {
            'type': 'Dict',
            'key': 'f'
        },
        'dict2': {
            'type': 'Dict',
            'num': 1,
            'key': 'f'
        },
        'dict3': {
            'type': 'Dict',
            'num': 2,
            'key': 'f'
        },
        'dict4': {
            'type': 'Dict',
            'num': 2,
            'key': 'noise.models.1.gain'
        },
        'sum1': {
            'type': 'Sum',
            'items': [72, '2.33', {
                'type': 'Dict',
                'key': 'f'
            }]
        }
    }

    test_yaml = True
    try:
        galsim.config.ProcessInput(config)
    except:
        # We don't require PyYAML as a dependency, so if this fails, just remove the YAML dict.
        del config['input']['dict'][2]
        galsim.config.ProcessInput(config)
        test_yaml = False

    # Test direct values
    val1 = galsim.config.ParseValue(config, 'val1', config, float)[0]
    np.testing.assert_almost_equal(val1, 9.9)

    val2 = galsim.config.ParseValue(config, 'val2', config, float)[0]
    np.testing.assert_almost_equal(val2, 400)

    # Test conversions from strings
    str1 = galsim.config.ParseValue(config, 'str1', config, float)[0]
    np.testing.assert_almost_equal(str1, 8.73)

    str2 = galsim.config.ParseValue(config, 'str2', config, float)[0]
    np.testing.assert_almost_equal(str2, 2.33e-9)

    str3 = galsim.config.ParseValue(config, 'str3', config, float)[0]
    np.testing.assert_almost_equal(str3, 6.0e-9)

    # Test values read from a Catalog
    cat1 = []
    cat2 = []
    cat3 = []
    cat4 = []
    config['index_key'] = 'file_num'
    for k in range(5):
        config['file_num'] = k
        cat1.append(galsim.config.ParseValue(config, 'cat1', config, float)[0])
        cat2.append(galsim.config.ParseValue(config, 'cat2', config, float)[0])
        cat3.append(galsim.config.ParseValue(config, 'cat3', config, float)[0])
        cat4.append(galsim.config.ParseValue(config, 'cat4', config, float)[0])

    np.testing.assert_array_almost_equal(cat1,
                                         [1.234, 2.345, 3.456, 1.234, 2.345])
    np.testing.assert_array_almost_equal(cat2,
                                         [4.131, -900, 8000, 4.131, -900])
    np.testing.assert_array_almost_equal(cat3,
                                         [1.234, 2.345, 3.456, 1.234, 2.345])
    np.testing.assert_array_almost_equal(cat4,
                                         [4.131, -900, 8000, 4.131, -900])

    # Test values generated from a uniform deviate
    rng = galsim.UniformDeviate(1234)
    config['rng'] = galsim.UniformDeviate(
        1234)  # A second copy starting with the same seed.
    for k in range(6):
        config[
            'obj_num'] = k  # The Random type doesn't use obj_num, but this keeps it
        # from thinking current_val is still current.
        ran1 = galsim.config.ParseValue(config, 'ran1', config, float)[0]
        np.testing.assert_almost_equal(ran1, rng() * 2.5 + 0.5)

        ran2 = galsim.config.ParseValue(config, 'ran2', config, float)[0]
        np.testing.assert_almost_equal(ran2, rng() * 5 - 5)

    # Test values generated from a Gaussian deviate
    for k in range(6):
        config['obj_num'] = k
        gauss1 = galsim.config.ParseValue(config, 'gauss1', config, float)[0]
        gd = galsim.GaussianDeviate(rng, mean=0, sigma=1)
        np.testing.assert_almost_equal(gauss1, gd())

        gauss2 = galsim.config.ParseValue(config, 'gauss2', config, float)[0]
        gd = galsim.GaussianDeviate(rng, mean=4, sigma=3)
        np.testing.assert_almost_equal(gauss2, gd())

        gauss3 = galsim.config.ParseValue(config, 'gauss3', config, float)[0]
        gd = galsim.GaussianDeviate(rng, mean=0, sigma=1.5)
        gd_val = gd()
        while math.fabs(gd_val) > 2:
            gd_val = gd()
        np.testing.assert_almost_equal(gauss3, gd_val)

        gauss4 = galsim.config.ParseValue(config, 'gauss4', config, float)[0]
        gd = galsim.GaussianDeviate(rng, mean=0, sigma=0.5)
        gd_val = math.fabs(gd())
        while gd_val > 0.8:
            gd_val = math.fabs(gd())
        np.testing.assert_almost_equal(gauss4, gd_val)

        gauss5 = galsim.config.ParseValue(config, 'gauss5', config, float)[0]
        gd = galsim.GaussianDeviate(rng, mean=0.5, sigma=0.3)
        gd_val = gd()
        if gd_val > 0.5:
            gd_val = 1 - gd_val
        while gd_val < 0:
            gd_val = gd()
            if gd_val > 0.5:
                gd_val = 1 - gd_val
        np.testing.assert_almost_equal(gauss5, gd_val)

    # Test values generated from a distribution in a file
    dd = galsim.DistDeviate(rng,
                            function='config_input/distribution.txt',
                            interpolant='linear')
    for k in range(6):
        config['obj_num'] = k
        dist1 = galsim.config.ParseValue(config, 'dist1', config, float)[0]
        np.testing.assert_almost_equal(dist1, dd())
    dd = galsim.DistDeviate(rng,
                            function='config_input/distribution2.txt',
                            interpolant='linear')
    for k in range(6):
        config['obj_num'] = k
        dist2 = galsim.config.ParseValue(config, 'dist2', config, float)[0]
        np.testing.assert_almost_equal(dist2, dd())
    dd = galsim.DistDeviate(rng, function=lambda x: x * x, x_min=0., x_max=2.)
    for k in range(6):
        config['obj_num'] = k
        dist3 = galsim.config.ParseValue(config, 'dist3', config, float)[0]
        np.testing.assert_almost_equal(dist3, dd())

    # Test values generated from various other deviates
    for k in range(6):
        config['obj_num'] = k
        dev = galsim.PoissonDeviate(rng, mean=137)
        dev1 = galsim.config.ParseValue(config, 'dev1', config, float)[0]
        np.testing.assert_almost_equal(dev1, dev())

        dev = galsim.BinomialDeviate(rng, N=17)
        dev2 = galsim.config.ParseValue(config, 'dev2', config, float)[0]
        np.testing.assert_almost_equal(dev2, dev())

        dev = galsim.BinomialDeviate(rng, N=17, p=0.2)
        dev3 = galsim.config.ParseValue(config, 'dev3', config, float)[0]
        np.testing.assert_almost_equal(dev3, dev())

        dev = galsim.WeibullDeviate(rng, a=1.7, b=4.3)
        dev4 = galsim.config.ParseValue(config, 'dev4', config, float)[0]
        np.testing.assert_almost_equal(dev4, dev())

        dev = galsim.GammaDeviate(rng, k=1, theta=4)
        dev5 = galsim.config.ParseValue(config, 'dev5', config, float)[0]
        np.testing.assert_almost_equal(dev5, dev())

        dev = galsim.GammaDeviate(rng, k=1.9, theta=4.1)
        dev6 = galsim.config.ParseValue(config, 'dev6', config, float)[0]
        np.testing.assert_almost_equal(dev6, dev())

        dev = galsim.Chi2Deviate(rng, n=17)
        dev7 = galsim.config.ParseValue(config, 'dev7', config, float)[0]
        np.testing.assert_almost_equal(dev7, dev())

    # Test values generated from a Sequence
    seq1 = []
    seq2 = []
    seq3 = []
    seq4 = []
    seq5 = []
    config['index_key'] = 'file_num'
    for k in range(6):
        config['file_num'] = k
        seq1.append(galsim.config.ParseValue(config, 'seq1', config, float)[0])
    config['index_key'] = 'image_num'
    for k in range(6):
        config['image_num'] = k
        seq2.append(galsim.config.ParseValue(config, 'seq2', config, float)[0])
    config['index_key'] = 'obj_num'
    for k in range(6):
        config['obj_num'] = k
        seq3.append(galsim.config.ParseValue(config, 'seq3', config, float)[0])
    config['index_key'] = 'obj_num_in_file'
    config['start_obj_num'] = 10
    for k in range(6):
        config['obj_num'] = k + 10
        seq4.append(galsim.config.ParseValue(config, 'seq4', config, float)[0])
        seq5.append(galsim.config.ParseValue(config, 'seq5', config, float)[0])

    np.testing.assert_array_almost_equal(seq1, [0, 1, 2, 3, 4, 5])
    np.testing.assert_array_almost_equal(seq2, [0, 0.1, 0.2, 0.3, 0.4, 0.5])
    np.testing.assert_array_almost_equal(seq3, [1.5, 2, 2.5, 3, 3.5, 4])
    np.testing.assert_array_almost_equal(seq4, [10, 8, 6, 4, 2, 0])
    np.testing.assert_array_almost_equal(seq5, [1, 1, 2, 2, 1, 1])

    # Test values taken from a List
    list1 = []
    list2 = []
    config['index_key'] = 'obj_num'
    for k in range(5):
        config['obj_num'] = k
        list1.append(
            galsim.config.ParseValue(config, 'list1', config, float)[0])
        list2.append(
            galsim.config.ParseValue(config, 'list2', config, float)[0])

    np.testing.assert_array_almost_equal(list1, [73, 8.9, 3.14, 73, 8.9])
    np.testing.assert_array_almost_equal(list2, [10.8, 7.0, 4.3, 1.8, 10.8])

    # Test values read from a Dict
    dict = []
    dict.append(galsim.config.ParseValue(config, 'dict1', config, float)[0])
    dict.append(galsim.config.ParseValue(config, 'dict2', config, float)[0])
    if test_yaml:
        dict.append(
            galsim.config.ParseValue(config, 'dict3', config, float)[0])
        dict.append(
            galsim.config.ParseValue(config, 'dict4', config, float)[0])
    else:
        dict.append(0.1)
        dict.append(1.9)
    np.testing.assert_array_almost_equal(dict, [23.17, -17.23, 0.1, 1.9])

    sum1 = galsim.config.ParseValue(config, 'sum1', config, float)[0]
    np.testing.assert_almost_equal(sum1, 72 + 2.33 + 23.17)

    t2 = time.time()
    print 'time for %s = %.2f' % (funcname(), t2 - t1)
Exemplo n.º 8
0
def test_dep_random():
    """Test the deprecated methods in galsim/deprecated/random.py
    """
    rng = galsim.BaseDeviate(123)

    gd = galsim.GaussianDeviate(rng, mean=0.5, sigma=1.7)
    np.testing.assert_almost_equal(gd.getMean(), 0.5)
    np.testing.assert_almost_equal(gd.getSigma(), 1.7)

    check_dep(gd.setMean, 0.9)
    np.testing.assert_almost_equal(gd.getMean(), 0.9)
    np.testing.assert_almost_equal(gd.getSigma(), 1.7)

    check_dep(gd.setSigma, 2.3)
    np.testing.assert_almost_equal(gd.getMean(), 0.9)
    np.testing.assert_almost_equal(gd.getSigma(), 2.3)

    bd = galsim.BinomialDeviate(rng, N=7, p=0.7)
    np.testing.assert_almost_equal(bd.getN(), 7)
    np.testing.assert_almost_equal(bd.getP(), 0.7)

    check_dep(bd.setN, 9)
    np.testing.assert_almost_equal(bd.getN(), 9)
    np.testing.assert_almost_equal(bd.getP(), 0.7)

    check_dep(bd.setP, 0.3)
    np.testing.assert_almost_equal(bd.getN(), 9)
    np.testing.assert_almost_equal(bd.getP(), 0.3)

    pd = galsim.PoissonDeviate(rng, mean=0.5)
    np.testing.assert_almost_equal(pd.getMean(), 0.5)

    check_dep(pd.setMean, 0.9)
    np.testing.assert_almost_equal(pd.getMean(), 0.9)

    wd = galsim.WeibullDeviate(rng, a=0.5, b=1.7)
    np.testing.assert_almost_equal(wd.getA(), 0.5)
    np.testing.assert_almost_equal(wd.getB(), 1.7)

    check_dep(wd.setA, 0.9)
    np.testing.assert_almost_equal(wd.getA(), 0.9)
    np.testing.assert_almost_equal(wd.getB(), 1.7)

    check_dep(wd.setB, 2.3)
    np.testing.assert_almost_equal(wd.getA(), 0.9)
    np.testing.assert_almost_equal(wd.getB(), 2.3)

    gd = galsim.GammaDeviate(rng, k=0.5, theta=1.7)
    np.testing.assert_almost_equal(gd.getK(), 0.5)
    np.testing.assert_almost_equal(gd.getTheta(), 1.7)

    check_dep(gd.setK, 0.9)
    np.testing.assert_almost_equal(gd.getK(), 0.9)
    np.testing.assert_almost_equal(gd.getTheta(), 1.7)

    check_dep(gd.setTheta, 2.3)
    np.testing.assert_almost_equal(gd.getK(), 0.9)
    np.testing.assert_almost_equal(gd.getTheta(), 2.3)

    cd = galsim.Chi2Deviate(rng, n=5)
    np.testing.assert_almost_equal(cd.getN(), 5)

    check_dep(cd.setN, 9)
    np.testing.assert_almost_equal(cd.getN(), 9)
Exemplo n.º 9
0
def test_scattered_whiten():
    """Test whitening with the image type Scattered.  In particular getting the noise flattened
    across overlapping stamps and stamps that are partially off the image.
    """
    real_gal_dir = os.path.join('..', 'examples', 'data')
    real_gal_cat = 'real_galaxy_catalog_23.5_example.fits'
    scale = 0.05
    index = 79
    flux = 10000
    variance = 10
    skip_prob = 0.2
    nobjects = 30
    config = {
        'image': {
            'type': 'Scattered',
            'random_seed': 12345,
            'pixel_scale': scale,
            'size': 100,
            'image_pos': {
                'type': 'XY',
                # Some of these will be completely off the main image.
                # They will be ignored.
                'x': {
                    'type': 'Random',
                    'min': -50,
                    'max': 150
                },
                'y': {
                    'type': 'Random',
                    'min': -50,
                    'max': 150
                },
            },
            'nobjects': nobjects,
            'noise': {
                'type': 'Gaussian',
                'variance': variance,
                'whiten': True,
            },
        },
        'gal': {
            'type': 'RealGalaxy',
            'index':
            index,  # It's a bit faster if they all use the same index.
            'flux': flux,

            # This tests a special case in FlattenNoiseVariance
            'skip': {
                'type': 'RandomBinomial',
                'p': skip_prob
            }
        },
        'psf': {
            'type': 'Gaussian',
            'sigma': 0.1,
        },
        'input': {
            'real_catalog': {
                'dir': real_gal_dir,
                'file_name': real_gal_cat,
            }
        }
    }

    # First build by hand
    rgc = galsim.RealGalaxyCatalog(os.path.join(real_gal_dir, real_gal_cat))
    gal = galsim.RealGalaxy(rgc, index=index, flux=flux)
    psf = galsim.Gaussian(sigma=0.1)
    final = galsim.Convolve(gal, psf)
    im1 = galsim.Image(100, 100, scale=scale)
    cv_im = galsim.Image(100, 100)

    for k in range(nobjects):
        ud = galsim.UniformDeviate(12345 + k + 1)

        x = ud() * 200. - 50.
        y = ud() * 200. - 50.

        skip_dev = galsim.BinomialDeviate(ud, N=1, p=skip_prob)
        if skip_dev() > 0: continue

        ix = int(math.floor(x + 1))
        iy = int(math.floor(y + 1))
        dx = x - ix + 0.5
        dy = y - iy + 0.5
        stamp = final.drawImage(offset=(dx, dy), scale=scale)
        stamp.setCenter(ix, iy)

        final.noise.rng.reset(ud)
        cv = final.noise.whitenImage(stamp)

        b = im1.bounds & stamp.bounds
        if not b.isDefined(): continue

        im1[b] += stamp[b]
        cv_im[b] += cv

    print('max cv = ', cv_im.array.max())
    print('min cv = ', cv_im.array.min())
    max_cv = cv_im.array.max()
    noise_im = max_cv - cv_im
    rng = galsim.BaseDeviate(12345)
    im1.addNoise(galsim.VariableGaussianNoise(rng, noise_im))
    im1.addNoise(galsim.GaussianNoise(rng, sigma=math.sqrt(variance - max_cv)))

    # Compare to what config builds
    im2 = galsim.config.BuildImage(config)
    np.testing.assert_almost_equal(im2.array, im1.array)

    # Should give a warning for the objects that fall off the edge
    with CaptureLog() as cl:
        im3 = galsim.config.BuildImage(config, logger=cl.logger)
    #print(cl.output)
    assert "Object centered at (-24,-43) is entirely off the main image" in cl.output
    im2 = galsim.config.BuildImage(config)
Exemplo n.º 10
0
def test_tiled():
    """Test image type = Tiled
    """
    nx = 5
    ny = 7
    xsize = 32
    ysize = 25
    xborder = 2
    yborder = 3
    scale = 0.3
    config = {
        'image': {
            'type': 'Tiled',
            'nx_tiles': nx,
            'ny_tiles': ny,
            'stamp_xsize': xsize,
            'stamp_ysize': ysize,
            'pixel_scale': scale,
            'xborder': xborder,
            'yborder': yborder,
            'random_seed': 1234,
            'noise': {
                'type': 'Gaussian',
                'sigma': 0.5
            }
        },
        'gal': {
            'type': 'Gaussian',
            'sigma': {
                'type': 'Random',
                'min': 1,
                'max': 2
            },
            'flux': '$image_pos.x + image_pos.y',
        }
    }

    seed = 1234
    im1a = galsim.Image(nx * (xsize + xborder) - xborder,
                        ny * (ysize + yborder) - yborder,
                        scale=scale)
    for j in range(ny):
        for i in range(nx):
            seed += 1
            ud = galsim.UniformDeviate(seed)
            xorigin = i * (xsize + xborder) + 1
            yorigin = j * (ysize + yborder) + 1
            x = xorigin + (xsize - 1) / 2.
            y = yorigin + (ysize - 1) / 2.
            stamp = galsim.Image(xsize, ysize, scale=scale)
            stamp.setOrigin(xorigin, yorigin)

            sigma = ud() + 1
            flux = x + y
            gal = galsim.Gaussian(sigma=sigma, flux=flux)
            gal.drawImage(stamp)
            stamp.addNoise(galsim.GaussianNoise(sigma=0.5, rng=ud))
            im1a[stamp.bounds] = stamp

    # Compare to what config builds
    im1b = galsim.config.BuildImage(config)
    np.testing.assert_array_equal(im1b.array, im1a.array)

    # Switch to column ordering.  Also make the stamps overlap slightly, which changes how the
    # noise is applied.
    galsim.config.RemoveCurrent(config)
    config['image']['order'] = 'col'
    config['image']['xborder'] = -xborder
    config['image']['yborder'] = -yborder
    im2a = galsim.Image(nx * (xsize - xborder) + xborder,
                        ny * (ysize - yborder) + yborder,
                        scale=scale)
    seed = 1234
    for i in range(nx):
        for j in range(ny):
            seed += 1
            ud = galsim.UniformDeviate(seed)
            xorigin = i * (xsize - xborder) + 1
            yorigin = j * (ysize - yborder) + 1
            x = xorigin + (xsize - 1) / 2.
            y = yorigin + (ysize - 1) / 2.
            stamp = galsim.Image(xsize, ysize, scale=scale)
            stamp.setOrigin(xorigin, yorigin)

            sigma = ud() + 1
            flux = x + y
            gal = galsim.Gaussian(sigma=sigma, flux=flux)
            gal.drawImage(stamp)
            im2a[stamp.bounds] += stamp
    im2a.addNoise(galsim.GaussianNoise(sigma=0.5,
                                       rng=galsim.BaseDeviate(1234)))

    # Compare to what config builds
    im2b = galsim.config.BuildImage(config)
    np.testing.assert_array_equal(im2b.array, im2a.array)

    # Finally, random ordering.  And also add some skips, so some of the stamps come back as None.
    # Switch to column ordering.  Also make the stamps overlap slightly, which changes how the
    # noise is applied.
    galsim.config.RemoveCurrent(config)
    config['image']['order'] = 'rand'
    config['gal']['skip'] = {'type': 'RandomBinomial', 'p': 0.2}
    im3a = galsim.Image(nx * (xsize - xborder) + xborder,
                        ny * (ysize - yborder) + yborder,
                        scale=scale)
    seed = 1234
    i_list = []
    j_list = []
    for i in range(nx):
        for j in range(ny):
            i_list.append(i)
            j_list.append(j)
    rng = galsim.BaseDeviate(seed)
    galsim.random.permute(rng, i_list, j_list)
    for i, j in zip(i_list, j_list):
        seed += 1
        ud = galsim.UniformDeviate(seed)

        skip_dev = galsim.BinomialDeviate(ud, N=1, p=0.2)
        if skip_dev() > 0: continue

        xorigin = i * (xsize - xborder) + 1
        yorigin = j * (ysize - yborder) + 1
        x = xorigin + (xsize - 1) / 2.
        y = yorigin + (ysize - 1) / 2.
        stamp = galsim.Image(xsize, ysize, scale=scale)
        stamp.setOrigin(xorigin, yorigin)

        sigma = ud() + 1
        flux = x + y
        gal = galsim.Gaussian(sigma=sigma, flux=flux)
        gal.drawImage(stamp)
        im3a[stamp.bounds] += stamp
    im3a.addNoise(galsim.GaussianNoise(sigma=0.5, rng=rng))

    # Compare to what config builds
    im3b = galsim.config.BuildImage(config)
    np.testing.assert_array_equal(im3b.array, im3a.array)