Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 215 additions & 93 deletions galsim/chromatic.py

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions galsim/gsobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ def deinterpolated(self): return self
@property
def sed(self):
return sed.SED(self.flux, 'nm', '1')
@lazy_property
def ref_sed(self):
return sed.SED(1., 'nm', '1')
@property
def ref_flux(self):
return self.flux
@property
def SED(self):
from .deprecated import depr
Expand Down
4 changes: 4 additions & 0 deletions galsim/interpolatedimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ def __init__(self, image, x_interpolant=None, k_interpolant=None, normalization=
self._buildRealImage(pad_factor, pad_image, noise_pad_size, noise_pad, rng, use_cache)
self._image_flux = np.sum(self._image.array, dtype=np.float64)

# If forcing stepk/maxk, we don't calculate them.
if _force_stepk > 0: calculate_stepk = False
if _force_maxk > 0: calculate_maxk = False

# I think the only things that will mess up if flux == 0 are the
# calculateStepK and calculateMaxK functions, and rescaling the flux to some value.
if (calculate_stepk or calculate_maxk or flux is not None) and self._image_flux == 0.:
Expand Down
1 change: 1 addition & 0 deletions tests/chromatic_reference_images/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
r_exact.fits
Binary file removed tests/chromatic_reference_images/r_exact.fits
Binary file not shown.
154 changes: 139 additions & 15 deletions tests/test_chromatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def test_chromatic_flux():
err_msg="Drawn ChromaticConvolve flux (interpolated) doesn't match analytic prediction")
# As an aside, check for appropriate tests of 'integrator' argument.
assert_raises(ValueError, final_int.drawImage, bandpass, integrator='midp') # minor misspelling
assert_raises(ValueError, final_int.drawImage, bandpass, integrator=galsim.integ.midptRule)
assert_raises(TypeError, final_int.drawImage, bandpass, integrator=galsim.integ.midptRule)
check_pickle(PSF)

# Check option to not use exact SED
Expand All @@ -622,7 +622,7 @@ def test_chromatic_flux():
err_msg="Drawn ChromaticConvolve flux (interpolated) doesn't match analytic prediction")
# As an aside, check for appropriate tests of 'integrator' argument.
assert_raises(ValueError, final_int.drawImage, bandpass, integrator='midp') # minor misspelling
assert_raises(ValueError, final_int.drawImage, bandpass, integrator=galsim.integ.midptRule)
assert_raises(TypeError, final_int.drawImage, bandpass, integrator=galsim.integ.midptRule)
check_pickle(PSF)

# Go back to no interpolation (this will effect the PSFs that are used below).
Expand Down Expand Up @@ -1806,6 +1806,15 @@ def __repr__(self):
kim_interp.array, kim_exact.array, rtol=0, atol=1e-4*kim_exact.array.real.max(),
err_msg='Interpolated ChromaticObject results differ for exact vs. interpolated (trapz)')

# Can also use a ContinuousIntegrator if you want rather than a SampleIntegrator with
# one of the above rules. (Sample is implicit when using the names as above.)
source = interp_psf * bulge_SED
integrator = galsim.integ.ContinuousIntegrator(galsim.integ.quadRule, N=4)
drawn = source.drawImage(bandpass, image=im_exact.copy(), integrator=integrator)
reference = galsim.ChromaticObject.drawImage(
source, bandpass, image=im_exact.copy(), integrator=integrator)
np.testing.assert_allclose(drawn.array, reference.array, atol=1.e-4, rtol=0.)

# Check that we can turn interpolation off and on at will.
other_psf = interp_psf.deinterpolated
other_obj = galsim.Convolve(star, other_psf)
Expand Down Expand Up @@ -2108,6 +2117,7 @@ def test_ChromaticOpticalPSF(run_slow):
# this unit test, we use an interpolated version only. The tests of
# ChromaticObject in the previous unit test should be enough to ensure that exact
# and interpolated evaluation match in general (given reasonable settings).
# Edit: This isn't really true anymore, but continue to use the interpolated version anyway.

# First, compare the interpolated result with saved, exact results.
# Note that if the saved results file cannot be found, then this function will (slowly)
Expand Down Expand Up @@ -2159,22 +2169,14 @@ def test_ChromaticOpticalPSF(run_slow):
galsim.ChromaticOpticalPSF(lam=lam, diam=diam, aper=psf2._aper, obscuration=0.3)

# We will compare to this reference image below. Make it if it doesn't exist yet.
# This used to take a long time to make, but now it's pretty fast (<2 seconds to make), so
# just make it on demand if it doesn't exist rather than store it in the repo.
# The interpolated version is still a lot faster, especially for repeated usage.
if not os.path.isfile(os.path.join(refdir, 'r_exact.fits')):
import warnings
warnings.warn("Could not find file r_exact.fits, so generating it from scratch. This "
"should only happen if you intentionally deleted the file in order to "
"regenerate it!")

# Generate exact results inside this if-block.
# This block took ~30 seconds to run on a ~2016 Macbook Pro, nearly all in the image
# rendering process. In contrast, the ChromaticOpticalPSF with interpolation that is used
# for this unit test takes about ~9 seconds to initialize, and ~1s for the image rendering
# process. Obviously, if many images are to be rendered after incurring the overhead of
# initializing this object, the interpolated calculation leads to a huge savings compared to
# doing the exact calculation each time.
t2 = time.time()
obj = galsim.Convolve(star, psf)
im_r_ref = obj.drawImage(bandpass, scale=scale)
# Use iimult=2 for better accuracy on this "exact" version.
im_r_ref = obj.drawImage(bandpass, scale=scale, iimult=2)
im_r_ref.write(os.path.join(refdir, 'r_exact.fits'))
t3 = time.time()
print("Time to draw ChromaticOpticalPSF: {0}s".format(t3-t2))
Expand Down Expand Up @@ -2524,6 +2526,125 @@ def test_low_flux_phot():
image = observed.drawImage(nx=53, ny=53, bandpass=bandpass, method="phot", rng=rng)
np.testing.assert_array_equal(image.array, 0.)

@timer
def test_chromatic_zero_flux():
"""Test drawing a chromatic object with zero total flux, but not a zero image.

This shows up in practice in Piff when we make images that are the derivative of a
profile. They have + and - parts, but the spatial integral is 0.
"""
# A difference of normalized Gaussians has zero flux and a nonzero spatial profile.
bp = bandpass # LSST r-band throughput loaded from LSST_r.dat above.
waves = bp.wave_list
sed = galsim.SED('(wave/600)**2', 'nm', 'fphotons').withFlux(1., bp)
psf = galsim.ChromaticObject(galsim.Gaussian(sigma=0.4)).dilate(lambda w: w/600.)

# Use two different compensated kernels.
# One is a Mexican hat pattern of a wide Gaussian minus a narrow one.
plus1 = galsim.Gaussian(sigma=0.25)
minus1 = galsim.Gaussian(sigma=0.20)

# The other uses two identical Gaussians, shifted in opposite directions.
# This one renders with exactly zero flux to make sure that doesn't fail.
plus2 = plus1.shift(0.1,0)
minus2 = plus1.shift(-0.1,0)

for plus, minus in ((plus1,minus1), (plus2,minus2)):
kernel = plus - minus
assert kernel.flux == 0.

# Check that drawing the combined kernel is equivalent to the difference of the images
# you get from drawing plus and minus separately.
# Include putting a shift in, since zero-flux components don't have a measurable centroid,
# so make sure the code is insensitive to that.
for shift in [(0., 0.), (0.17, -0.23)]:
# First the simple separable case kernel * sed.
image = (kernel * sed).shift(shift).drawImage(bp, nx=96, ny=96, scale=0.1, dtype=float)
reference = (plus * sed).shift(shift).drawImage(bp, image=image.copy())
reference -= (minus * sed).shift(shift).drawImage(bp, image=image.copy())
assert np.max(np.abs(image.array)) > 5.e-4
np.testing.assert_allclose(image.array, reference.array, atol=1.e-10, rtol=0.)
assert abs(image.array.sum()) < 1.e-10

# Also check as a sum of chromatic objects. I.e. apply sed to plus and minus separately.
# This is still separable, since the seds are equal.
(plus*sed - minus*sed).shift(shift).drawImage(bp, image=image)
assert np.max(np.abs(image.array)) > 1.e-3
np.testing.assert_allclose(image.array, reference.array, atol=1.e-10, rtol=0.)
assert abs(image.array.sum()) < 1.e-10

# Check convolving the kernel by an inseparable component.
obj = (galsim.Convolve(psf, kernel) * sed).shift(*shift)
obj.drawImage(bp, image=image)
reference = (galsim.Convolve(psf, plus) * sed).shift(*shift).drawImage(
bp, image=image.copy())
reference -= (galsim.Convolve(psf, minus) * sed).shift(*shift).drawImage(
bp, image=image.copy())
assert np.max(np.abs(image.array)) > 1.e-4
np.testing.assert_allclose(image.array, reference.array, atol=1.e-10, rtol=0.)
assert abs(image.array.sum()) < 1.e-8

# Check drawKImage similarly.
image = (kernel * sed).shift(*shift).drawKImage(bp, nx=33, ny=33, scale=0.1)
reference = (plus * sed).shift(*shift).drawKImage(bp, image=image.copy())
reference -= (minus * sed).shift(*shift).drawKImage(bp, image=image.copy())
np.testing.assert_allclose(image.array, reference.array, atol=1.e-10, rtol=0.)
assert abs(image(0, 0)) < 1.e-10

# And with the psf convolution.
image = (galsim.Convolve(psf, kernel) * sed).shift(*shift).drawKImage(
bp, image=image.copy())
reference = (galsim.Convolve(psf, plus) * sed).shift(*shift).drawKImage(
bp, image=image.copy())
reference -= (galsim.Convolve(psf, minus) * sed).shift(*shift).drawKImage(
bp, image=image.copy())
np.testing.assert_allclose(image.array, reference.array, atol=1.e-10, rtol=0.)
assert abs(image(0, 0)) < 1.e-10

# Check that drawing a convolution where the inseparable part has zero flux also works.
# A wavelength-dependent dilation makes the signed profile and its two parts inseparable.
kernel2 = galsim.ChromaticObject(kernel).dilate(lambda w: w/600.) * sed
plus2 = galsim.ChromaticObject(plus).dilate(lambda w: w/600.) * sed
minus2 = galsim.ChromaticObject(minus).dilate(lambda w: w/600.) * sed
blur = galsim.Gaussian(sigma=0.2)
image = galsim.Convolve(kernel2, blur).drawImage(bp, nx=96, ny=96, scale=0.1, dtype=float)
reference = galsim.Convolve(plus2, blur).drawImage(bp, image=image.copy())
reference -= galsim.Convolve(minus2, blur).drawImage(bp, image=image.copy())
assert np.max(np.abs(image.array)) > 1.e-4
assert abs(image.array.sum()) < 1.e-7
np.testing.assert_allclose(image.array, reference.array, atol=1.e-6, rtol=0.)

# Interpolating the signed profile also agrees with interpolating and drawing its parts.
kernel3 = kernel2.interpolate(waves)
plus3 = plus2.interpolate(waves)
minus3 = minus2.interpolate(waves)
image = galsim.Convolve(kernel3, blur).drawImage(bp, image=image.copy())
reference = galsim.Convolve(plus3, blur).drawImage(bp, image=image.copy())
reference -= galsim.Convolve(minus3, blur).drawImage(bp, image=image.copy())
assert np.max(np.abs(image.array)) > 1.e-4
assert abs(image.array.sum()) < 2.e-7
# The intermediate images use slightly different sizes and scales in the two cases,
# so the tolerance here isn't quite as low as other tests.
np.testing.assert_allclose(image.array, reference.array, atol=2.e-5, rtol=0.)

# Shearing and shifting the interpolated profiles preserve the same equivalence.
kernel4 = kernel3.shear(g1=0.1, g2=0.05).shift(0.13, -0.19)
plus4 = plus3.shear(g1=0.1, g2=0.05).shift(0.13, -0.19)
minus4 = minus3.shear(g1=0.1, g2=0.05).shift(0.13, -0.19)
image = galsim.Convolve(kernel4, blur).drawImage(bp, image=image.copy())
reference = galsim.Convolve(plus4, blur).drawImage(bp, image=image.copy())
reference -= galsim.Convolve(minus4, blur).drawImage(bp, image=image.copy())
assert np.max(np.abs(image.array)) > 1.e-4
assert abs(image.array.sum()) < 2.e-7
np.testing.assert_allclose(image.array, reference.array, atol=2.e-5, rtol=0.)

# If the sed itself is completely zero, that doesn't work.
zero_sed = galsim.SED('0', 'nm', 'fphotons')
for obj in [kernel * zero_sed, galsim.Convolve(psf, kernel) * zero_sed]:
with assert_raises(galsim.GalSimError):
obj.drawImage(bp, nx=32, ny=32, scale=0.1)
with assert_raises(galsim.GalSimError):
obj.drawKImage(bp, nx=33, ny=33, scale=0.2)

@timer
def test_chromatic_fiducial_wavelength():
Expand Down Expand Up @@ -2673,6 +2794,9 @@ def check_chromatic_invariant(obj, bps=None, waves=None):
desired,
rtol=1e-2)

# Check invariant that sed = ref_sed * ref_flux.
np.testing.assert_allclose(obj.ref_sed(wave)*obj.ref_flux, desired)

if obj.sed.spectral:
for bp in bps:
calc_flux = obj.calculateFlux(bp)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_interpolatedimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,16 @@ def test_exceptions():
with assert_raises(galsim.GalSimValueError):
galsim.InterpolatedImage(image=galsim.ImageI(5, 5, scale=1))

# Image must have non-zero flux
# Image must have non-zero flux if not providing stepk, maxk.
with assert_raises(galsim.GalSimValueError):
galsim.InterpolatedImage(image=galsim.ImageF(5, 5, scale=1, init_value=0.))

# It's fine if we don't ask InterpolatedImage to figure out stepk, maxk though.
galsim.InterpolatedImage(image=galsim.ImageF(5, 5, scale=1, init_value=0.),
calculate_stepk=False, calculate_maxk=False)
galsim.InterpolatedImage(image=galsim.ImageF(5, 5, scale=1, init_value=0.),
_force_stepk=0.05, _force_maxk=10)

# Can't shoot II with SincInterpolant
ii = galsim.InterpolatedImage(image=galsim.ImageF(5, 5, scale=1, init_value=1.),
x_interpolant='sinc',
Expand Down
Loading