|
| 1 | +# Copyright (c) 2012-2026 by the GalSim developers team on GitHub |
| 2 | +# https://github.com/GalSim-developers |
| 3 | +# |
| 4 | +# This file is part of GalSim: The modular galaxy image simulation toolkit. |
| 5 | +# https://github.com/GalSim-developers/GalSim |
| 6 | +# |
| 7 | +# GalSim is free software: redistribution and use in source and binary forms, |
| 8 | +# with or without modification, are permitted provided that the following |
| 9 | +# conditions are met: |
| 10 | +# |
| 11 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 12 | +# list of conditions, and the disclaimer given in the accompanying LICENSE |
| 13 | +# file. |
| 14 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +# this list of conditions, and the disclaimer given in the documentation |
| 16 | +# and/or other materials provided with the distribution. |
| 17 | +# |
| 18 | +""" |
| 19 | +Demo #2 |
| 20 | +
|
| 21 | +The second script in our tutorial about using JAX-GalSim in python scripts: examples/demo*.py. |
| 22 | +(This file is designed to be viewed in a window 100 characters wide.) |
| 23 | +
|
| 24 | +This script is a bit more sophisticated, but still pretty basic. We're still only making |
| 25 | +a single image, but now the galaxy has an exponential radial profile and is sheared. |
| 26 | +The PSF is a circular Moffat profile. The noise is drawn from a Poisson distribution |
| 27 | +using the flux from both the object and a background sky level to determine the |
| 28 | +variance in each pixel. |
| 29 | +
|
| 30 | +New features introduced in this demo: |
| 31 | +
|
| 32 | +- obj = jax_galsim.Exponential(flux, scale_radius) |
| 33 | +- obj = jax_galsim.Moffat(beta, flux, half_light_radius) |
| 34 | +- obj = obj.shear(g1, g2) -- with explanation of other ways to specify shear |
| 35 | +- rng = jax_galsim.BaseDeviate(seed) |
| 36 | +- noise = jax_galsim.PoissonNoise(rng, sky_level) |
| 37 | +- galsim.hsm.EstimateShear(image, image_epsf) |
| 38 | +""" |
| 39 | + |
| 40 | +import logging |
| 41 | +import os |
| 42 | +import sys |
| 43 | + |
| 44 | +import galsim |
| 45 | + |
| 46 | +import jax_galsim |
| 47 | + |
| 48 | + |
| 49 | +def main(argv): |
| 50 | + """ |
| 51 | + A little bit more sophisticated, but still pretty basic: |
| 52 | + - Use a sheared, exponential profile for the galaxy. |
| 53 | + - Convolve it by a circular Moffat PSF. |
| 54 | + - Add Poisson noise to the image. |
| 55 | + """ |
| 56 | + # In non-script code, use getLogger(__name__) at module scope instead. |
| 57 | + logging.basicConfig(format="%(message)s", level=logging.INFO, stream=sys.stdout) |
| 58 | + logger = logging.getLogger("demo2") |
| 59 | + |
| 60 | + gal_flux = 1.0e5 # counts |
| 61 | + gal_r0 = 2.7 # arcsec |
| 62 | + g1 = 0.1 # |
| 63 | + g2 = 0.2 # |
| 64 | + psf_beta = 5 # |
| 65 | + psf_re = 1.0 # arcsec |
| 66 | + pixel_scale = 0.2 # arcsec / pixel |
| 67 | + sky_level = 2.5e3 # counts / arcsec^2 |
| 68 | + |
| 69 | + # This time use a particular seed, so the image is deterministic. |
| 70 | + # This is the same seed that is used in demo2.yaml, which means the images |
| 71 | + # produced by the two methods will be precisely identical. |
| 72 | + random_seed = 1534225 |
| 73 | + |
| 74 | + # The first thing the config layer does with the random seed is to scramble |
| 75 | + # it a bit. Specifically, it makes a random number generator (BaseDeviate) |
| 76 | + # using that seed and asks for a raw value. This becomes the seed that |
| 77 | + # actually gets used. |
| 78 | + # The reason for this extra step is that eventually (cf. demo4) the config |
| 79 | + # layer will want to increment these seed values when building multiple |
| 80 | + # objects or images. If the user is likewise incrementing seed values for |
| 81 | + # multiple runs of a given config file, these can interfere leading to |
| 82 | + # surprising (and typically bad) results. |
| 83 | + random_seed = jax_galsim.BaseDeviate(random_seed).raw() |
| 84 | + |
| 85 | + logger.info("Starting demo script 2 using:") |
| 86 | + logger.info( |
| 87 | + " - sheared (%.2f,%.2f) exponential galaxy (flux = %.1e, scale radius = %.2f),", |
| 88 | + g1, |
| 89 | + g2, |
| 90 | + gal_flux, |
| 91 | + gal_r0, |
| 92 | + ) |
| 93 | + logger.info(" - circular Moffat PSF (beta = %.1f, re = %.2f),", psf_beta, psf_re) |
| 94 | + logger.info(" - pixel scale = %.2f,", pixel_scale) |
| 95 | + logger.info(" - Poisson noise (sky level = %.1e).", sky_level) |
| 96 | + |
| 97 | + # Initialize the (pseudo-)random number generator that we will be using below. |
| 98 | + # For a technical reason that will be explained later (demo9.py), we add 1 to the |
| 99 | + # given random seed here. |
| 100 | + rng = jax_galsim.BaseDeviate(random_seed + 1) |
| 101 | + |
| 102 | + # Define the galaxy profile. |
| 103 | + gal = jax_galsim.Exponential(flux=gal_flux, scale_radius=gal_r0) |
| 104 | + |
| 105 | + # Shear the galaxy by some value. |
| 106 | + # There are quite a few ways you can use to specify a shape. |
| 107 | + # q, beta Axis ratio and position angle: q = b/a, 0 < q < 1 |
| 108 | + # e, beta Ellipticity and position angle: |e| = (1-q^2)/(1+q^2) |
| 109 | + # g, beta ("Reduced") Shear and position angle: |g| = (1-q)/(1+q) |
| 110 | + # eta, beta Conformal shear and position angle: eta = ln(1/q) |
| 111 | + # e1,e2 Ellipticity components: e1 = e cos(2 beta), e2 = e sin(2 beta) |
| 112 | + # g1,g2 ("Reduced") shear components: g1 = g cos(2 beta), g2 = g sin(2 beta) |
| 113 | + # eta1,eta2 Conformal shear components: eta1 = eta cos(2 beta), eta2 = eta sin(2 beta) |
| 114 | + gal = gal.shear(g1=g1, g2=g2) |
| 115 | + logger.debug("Made galaxy profile") |
| 116 | + |
| 117 | + # Define the PSF profile. |
| 118 | + psf = jax_galsim.Moffat(beta=psf_beta, flux=1.0, half_light_radius=psf_re) |
| 119 | + logger.debug("Made PSF profile") |
| 120 | + |
| 121 | + # Final profile is the convolution of these. |
| 122 | + final = jax_galsim.Convolve([gal, psf]) |
| 123 | + logger.debug("Convolved components into final profile") |
| 124 | + |
| 125 | + # Draw the image with a particular pixel scale. |
| 126 | + image = final.drawImage(scale=pixel_scale) |
| 127 | + # The "effective PSF" is the PSF as drawn on an image, which includes the convolution |
| 128 | + # by the pixel response. We label it epsf here. |
| 129 | + image_epsf = psf.drawImage(scale=pixel_scale) |
| 130 | + logger.debug("Made image of the profile") |
| 131 | + |
| 132 | + # To get Poisson noise on the image, we will use a class called PoissonNoise. |
| 133 | + # However, we want the noise to correspond to what you would get with a significant |
| 134 | + # flux from tke sky. This is done by telling PoissonNoise to add noise from a |
| 135 | + # sky level in addition to the counts currently in the image. |
| 136 | + # |
| 137 | + # One wrinkle here is that the PoissonNoise class needs the sky level in each pixel, |
| 138 | + # while we have a sky_level in counts per arcsec^2. So we need to convert: |
| 139 | + sky_level_pixel = sky_level * pixel_scale**2 |
| 140 | + noise = jax_galsim.PoissonNoise(rng, sky_level=sky_level_pixel) |
| 141 | + image.addNoise(noise) |
| 142 | + logger.debug("Added Poisson noise") |
| 143 | + |
| 144 | + # Write the image to a file. |
| 145 | + if not os.path.isdir("output"): |
| 146 | + os.mkdir("output") |
| 147 | + file_name = os.path.join("output", "demo2.fits") |
| 148 | + file_name_epsf = os.path.join("output", "demo2_epsf.fits") |
| 149 | + image.write(file_name) |
| 150 | + image_epsf.write(file_name_epsf) |
| 151 | + logger.info("Wrote image to %r", file_name) |
| 152 | + logger.info("Wrote effective PSF image to %r", file_name_epsf) |
| 153 | + |
| 154 | + results = galsim.hsm.EstimateShear(image.to_galsim(), image_epsf.to_galsim()) |
| 155 | + |
| 156 | + logger.info("HSM reports that the image has observed shape and size:") |
| 157 | + logger.info( |
| 158 | + " e1 = %.3f, e2 = %.3f, sigma = %.3f (pixels)", |
| 159 | + results.observed_shape.e1, |
| 160 | + results.observed_shape.e2, |
| 161 | + results.moments_sigma, |
| 162 | + ) |
| 163 | + logger.info( |
| 164 | + "When carrying out Regaussianization PSF correction, HSM reports distortions" |
| 165 | + ) |
| 166 | + logger.info(" e1, e2 = %.3f, %.3f", results.corrected_e1, results.corrected_e2) |
| 167 | + logger.info( |
| 168 | + "Expected values in the limit that noise and non-Gaussianity are negligible:" |
| 169 | + ) |
| 170 | + exp_shear = galsim.Shear(g1=g1, g2=g2) |
| 171 | + logger.info(" g1, g2 = %.3f, %.3f", exp_shear.e1, exp_shear.e2) |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + main(sys.argv) |
0 commit comments