diff --git a/galsim/chromatic.py b/galsim/chromatic.py index 581aa23bc81..5dfe165b234 100644 --- a/galsim/chromatic.py +++ b/galsim/chromatic.py @@ -124,6 +124,15 @@ class ChromaticObject: (though note that the new object's `SED` may not be equal to the SED being multiplied by since the original ChromaticObject may not have had unit normalization.) + .. note:: + The ``sed`` property of a ChromaticObject always includes the full normalization so that + ``obj.sed(wave)`` gives the flux of the object at that wavelength (where wave is in nm). + We allow objects to have zero flux without being trivially zero though. In such cases, + it may be useful to separate the underlying wavelength dependence from the flux + normalization. For such cases, you may use ``obj.ref_sed`` and ``obj.ref_flux`` + where ``obj.sed = obj.ref_sed * obj.ref_flux``. The latter is a wavelength-independent + scalar, which is allowed to be zero. + **Methods**: `evaluateAtWavelength` returns the monochromatic surface brightness profile (as a `GSObject`) @@ -164,6 +173,8 @@ class ChromaticObject: # InterpolatedChromaticObject. # - .spectral indicates obj.sed.spectral # - .dimensionless indicates obj.sed.dimensionless + # - obj.sed(lam) == obj.ref_sed(lam) * obj.ref_flux, where ref_flux is wavelength independent. + # Unlike sed, ref_sed can remain nonzero for a signed profile with zero spatial integral. def __init__(self, obj): self._obj = obj @@ -178,6 +189,28 @@ def __init__(self, obj): def sed(self): return self._obj.sed + @property + def ref_sed(self): + """A reference `SED` with ``sed = ref_sed * ref_flux``. + """ + if type(self) is ChromaticObject: + # For directly construction ChromaticObject(obj) + return self._obj.ref_sed + else: + # The base class default whne not overridden by a subclass + return self.sed + + @property + def ref_flux(self): + """A reference flux value scaling ref_sed to give the total sed. + """ + if type(self) is ChromaticObject: + # For directly construction ChromaticObject(obj) + return self._obj.ref_flux + else: + # The base class default whne not overridden by a subclass + return 1. + @property def SED(self): from .deprecated import depr @@ -241,7 +274,7 @@ def _fiducial_profile(self, bandpass): """ bpwave = bandpass.effective_wavelength bpwave, prof0 = self._approxWavelength(bpwave) - if prof0.flux != 0: + if self.ref_sed(bpwave) != 0: return bpwave, prof0 candidate_waves = np.concatenate( @@ -253,7 +286,7 @@ def _fiducial_profile(self, bandpass): for w in candidate_waves: if bandpass.blue_limit <= w <= bandpass.red_limit: prof0 = self.evaluateAtWavelength(w) - if prof0.flux != 0: + if self.ref_sed(w) != 0: return w, prof0 raise GalSimError("Could not locate fiducial wavelength where SED * Bandpass is nonzero.") @@ -383,8 +416,7 @@ def dimensionless(self): """ return self.sed.dimensionless - @staticmethod - def _get_integrator(integrator, wave_list): + def _get_integrator(self, integrator, wave_list, bandpass): # Decide on integrator. If the user passed one of the integrators from galsim.integ, that's # fine. Otherwise we decide based on the adopted integration rule and the presence/absence # of `wave_list`. @@ -404,7 +436,15 @@ def _get_integrator(integrator, wave_list): integrator = integ.ContinuousIntegrator(rule) if not isinstance(integrator, integ.ImageIntegrator): raise TypeError("Invalid type passed in for integrator!") - return integrator + + # Merge self.wave_list into bandpass.wave_list if using a sampling integrator + if isinstance(integrator, integ.SampleIntegrator): + if len(wave_list) < 2: + raise GalSimIncompatibleValuesError( + "Cannot use SampleIntegrator when Bandpass and SED are both analytic.", + integrator=integrator, bandpass=bandpass, sed=self.sed) + bandpass = Bandpass(_LookupTable(wave_list, bandpass(wave_list), 'linear'), 'nm') + return integrator, bandpass def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): """Base implementation for drawing an image of a `ChromaticObject`. @@ -465,12 +505,12 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): if self.sed.dimensionless: raise GalSimSEDError("Can only draw ChromaticObjects with spectral SEDs.", self.sed) - # setup output image using fiducial profile + # Setup output image using fiducial profile wave0, prof0 = self._fiducial_profile(bandpass) image = prof0.drawImage(image=image, setup_only=True, **kwargs) _remove_setup_kwargs(kwargs) - # determine combined self.wave_list and bandpass.wave_list + # Determine combined self.wave_list and bandpass.wave_list wave_list, _, _ = utilities.combine_wave_list(self, bandpass) # If there are photon ops, they'll probably need valid wavelengths, so add @@ -481,8 +521,8 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): kwargs['photon_ops'] = [wave_sampler] + kwargs['photon_ops'] if self.separable: - multiplier = ChromaticObject._multiplier_cache(self.sed, bandpass, tuple(wave_list)) - prof0 *= multiplier/self.sed(wave0) + multiplier = ChromaticObject._multiplier_cache(self.ref_sed, bandpass, tuple(wave_list)) + prof0 *= multiplier/self.ref_sed(wave0) image = prof0.drawImage(image=image, **kwargs) return image @@ -494,15 +534,8 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): # specialize something in the subclass's drawImage method. assert kwargs.get('method', None) != 'phot' - integrator = self._get_integrator(integrator, wave_list) - - # merge self.wave_list into bandpass.wave_list if using a sampling integrator - if isinstance(integrator, integ.SampleIntegrator): - if len(wave_list) < 2: - raise GalSimIncompatibleValuesError( - "Cannot use SampleIntegrator when Bandpass and SED are both analytic.", - integrator=integrator, bandpass=bandpass, sed=self.sed) - bandpass = Bandpass(_LookupTable(wave_list, bandpass(wave_list), 'linear'), 'nm') + # Get the actual integrator object, and maybe adjust the bandpass if necessary. + integrator, bandpass = self._get_integrator(integrator, wave_list, bandpass) add_to_image = kwargs.pop('add_to_image', False) integral = integrator(self.evaluateAtWavelength, bandpass, image, kwargs) @@ -554,25 +587,22 @@ def drawKImage(self, bandpass, image=None, integrator='quadratic', **kwargs): if self.sed.dimensionless: raise GalSimSEDError("Can only drawK ChromaticObjects with spectral SEDs.", self.sed) - # setup output image (semi-arbitrarily using the bandpass effective wavelength) - prof0 = self.evaluateAtWavelength(bandpass.effective_wavelength) + # Setup output image using fiducial profile + wave0, prof0 = self._fiducial_profile(bandpass) image = prof0.drawKImage(image=image, setup_only=True, **kwargs) _remove_setup_kwargs(kwargs) - # determine combined self.wave_list and bandpass.wave_list + # Determine combined self.wave_list and bandpass.wave_list wave_list, _ , _ = utilities.combine_wave_list(self, bandpass) if self.separable: - multiplier = ChromaticObject._multiplier_cache(self.sed, bandpass, tuple(wave_list)) - prof0 *= multiplier/self.sed(bandpass.effective_wavelength) + multiplier = ChromaticObject._multiplier_cache(self.ref_sed, bandpass, tuple(wave_list)) + prof0 *= multiplier/self.ref_sed(wave0) image = prof0.drawKImage(image=image, **kwargs) return image - integrator = self._get_integrator(integrator, wave_list) - - # merge self.wave_list into bandpass.wave_list if using a sampling integrator - if isinstance(integrator, integ.SampleIntegrator): - bandpass = Bandpass(_LookupTable(wave_list, bandpass(wave_list), 'linear'), 'nm') + # Get the actual integrator object, and maybe adjust the bandpass if necessary. + integrator, bandpass = self._get_integrator(integrator, wave_list, bandpass) add_to_image = kwargs.pop('add_to_image', False) image_int = integrator(self.evaluateAtWavelength, bandpass, image, kwargs, doK=True) @@ -1332,6 +1362,14 @@ def _from_images(cls, images, waves, _force_stepk = None, _force_maxk = None, ** def sed(self): return self.deinterpolated.sed + @property + def ref_sed(self): + return self.deinterpolated.ref_sed + + @property + def ref_flux(self): + return self.deinterpolated.ref_flux + # Note: We don't always need all of these, so only calculate them if needed. # E.g. if photon shooting, pretty much only need objs. @lazy_property @@ -1362,10 +1400,6 @@ def ims(self): return [ obj.drawImage(scale=scale, nx=im_size, ny=im_size, method='no_pixel') for obj in self.objs ] - @lazy_property - def fluxes(self): - return [ obj.flux for obj in self.objs ] - @property def gsparams(self): """The `GSParams` for this object. @@ -1431,8 +1465,8 @@ def _imageAtWavelength(self, wave): # Rescale to use the exact flux or normalization if requested. if self.use_exact_sed: - interp_norm = _linearInterp(self.fluxes, frac, lower_idx) - exact_norm = self.sed(wave) + interp_norm = _linearInterp(self.ref_sed(self.waves), frac, lower_idx) + exact_norm = self.ref_sed(wave) im *= exact_norm/interp_norm return im, stepk, maxk @@ -1455,7 +1489,8 @@ def evaluateAtWavelength(self, wave): the monochromatic object at the given wavelength, as a `GSObject`. """ im, stepk, maxk = self._imageAtWavelength(wave) - return InterpolatedImage(im, _force_stepk=stepk, _force_maxk=maxk) + return InterpolatedImage(im, gsparams=self.gsparams, + _force_stepk=stepk, _force_maxk=maxk) def _shoot(self, photons, rng): w = photons.wavelength @@ -1501,16 +1536,6 @@ def _shoot(self, photons, rng): def _get_interp_image(self, bandpass, image=None, integrator='quadratic', _flux_ratio=None, **kwargs): - if integrator == 'quadratic': - rule = integ.quadRule - elif integrator == 'trapezoidal': - rule = integ.trapzRule - elif integrator == 'midpoint': - rule = integ.midptRule - else: - raise GalSimValueError("Unrecognized integration rule", integrator, - ('trapezoidal', 'midpoint', 'quadratic')) - if _flux_ratio is None: _flux_ratio = lambda w: np.ones_like(w) # _flux_ratio might be a constant float. For simplicity below, turn it into a function. @@ -1518,7 +1543,7 @@ def _get_interp_image(self, bandpass, image=None, integrator='quadratic', val = _flux_ratio _flux_ratio = lambda w: np.full_like(w, val) - # setup output image (semi-arbitrarily using the bandpass effective wavelength). + # Setup output image using fiducial profile # Note: we cannot just use self._imageAtWavelength, because that routine returns an image # with whatever pixel scale was required to sample all the images properly. We want to set # up an output image that has the requested pixel scale, which might change the image size @@ -1532,6 +1557,8 @@ def _get_interp_image(self, bandpass, image=None, integrator='quadratic', if isinstance(_flux_ratio, SED): wave_objs += [_flux_ratio] wave_list, _, _ = utilities.combine_wave_list(wave_objs) + integrator, bandpass = self._get_integrator(integrator, wave_list, bandpass) + wave_list = integrator.calculateWaves(bandpass) if np.any((wave_list < self.waves[0]) | (wave_list > self.waves[-1])): # pragma: no cover # MJ: I'm pretty sure it's impossible to hit this. @@ -1541,9 +1568,11 @@ def _get_interp_image(self, bandpass, image=None, integrator='quadratic', bad_waves, self.waves[0], self.waves[-1]) # weights are the weights to use at each of the given wavelengths for the integration. - weights = rule.calculateWeights(wave_list, bandpass) + weights = integrator.rule.calculateWeights(wave_list, bandpass) # im_weights are the weights for the stored images. im_weights = np.zeros(len(self.waves)) + if self.use_exact_sed: + ref_sed_vals = self.ref_sed(self.waves) for w, wt in zip(wave_list, weights): # Find where this is with respect to the wavelengths on which images are stored. lower_idx, frac = _findWave(self.waves, w) @@ -1551,8 +1580,8 @@ def _get_interp_image(self, bandpass, image=None, integrator='quadratic', # Rescale to use the exact flux or normalization if requested. if self.use_exact_sed: - interp_norm = _linearInterp(self.fluxes, frac, lower_idx) - exact_norm = self.sed(w) + interp_norm = _linearInterp(ref_sed_vals, frac, lower_idx) + exact_norm = self.ref_sed(w) wt *= exact_norm/interp_norm im_weights[lower_idx] += (1.-frac) * wt * _flux_ratio(w) @@ -1565,8 +1594,9 @@ def _get_interp_image(self, bandpass, image=None, integrator='quadratic', stepk = np.average(self.stepk_vals, weights=im_weights) maxk = np.average(self.maxk_vals, weights=im_weights) - # Instantiate the InterpolatedImage, using these conservative stepk and maxk choices. - return InterpolatedImage(integral, _force_stepk=stepk, _force_maxk=maxk) + # Instantiate the InterpolatedImage, using these stepk and maxk choices. + return InterpolatedImage(integral, gsparams=self.gsparams, + _force_stepk=stepk, _force_maxk=maxk) def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): """Draw an image as seen through a particular bandpass using the stored interpolated @@ -1580,8 +1610,7 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): integrate. image: Optionally, the `Image` to draw onto. (See `GSObject.drawImage` for details.) [default: None] - integrator: The integration algorithm to use, given as a string. Either - 'midpoint', 'trapezoidal', or 'quadratic' is allowed. + integrator: The integration algorithm to use, as for `ChromaticObject.drawImage`. [default: 'quadratic'] **kwargs: For all other kwarg options, see `GSObject.drawImage`. @@ -1686,6 +1715,14 @@ def __init__(self, base_obj, base_wavelength, scale_unit=None, **kwargs): def sed(self): return self.base_obj.sed + @property + def ref_sed(self): + return self.base_obj.ref_sed + + @property + def ref_flux(self): + return self.base_obj.ref_flux + @property def gsparams(self): """The `GSParams` for this object. @@ -1893,7 +1930,15 @@ def __init__(self, obj, jac=None, offset=(0.,0.), flux_ratio=1., redshift=None, @lazy_property def sed(self): - sed = self._original.sed * self._flux_ratio + return self.ref_sed * self.ref_flux + + @property + def ref_flux(self): + return self._original.ref_flux + + @lazy_property + def ref_sed(self): + sed = self._original.ref_sed * self._flux_ratio if self._redshift is not None: sed = sed.atRedshift(self._redshift) @@ -2017,7 +2062,8 @@ def __str__(self): def __getstate__(self): d = self.__dict__.copy() - d.pop('sed',None) + d.pop('sed', None) + d.pop('ref_sed', None) return d def __setstate__(self, d): @@ -2103,6 +2149,16 @@ def applyTo(self, photons, local_wcs=None, rng=None): return ChromaticTransformationPhotonOp(self) + def _get_interp_image(self, bandpass, image, integrator, kwargs): + # This path is used only when original is an InterpolatedChromaticObject. + int_im = self.original._get_interp_image( + bandpass, image=image, integrator=integrator, _flux_ratio=self._flux_ratio, **kwargs) + # Shape transformations are achromatic, so their wavelength of evaluation is immaterial. + jac, offset, _ = self._getTransformations(bandpass.red_limit) + return transform.Transform(int_im, jac=jac, offset=_PositionD(*offset), + gsparams=self._gsparams, + propagate_gsparams=self._propagate_gsparams) + def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): """ See `ChromaticObject.drawImage` for a full description. @@ -2123,9 +2179,6 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): or not the object has a ``wave_list``. [default: 'quadratic', which will try to select an appropriate integrator using the quadratic integration rule automatically.] - If the object being transformed is an `InterpolatedChromaticObject`, - then ``integrator`` can only be a string, either 'midpoint', - 'trapezoidal', or 'quadratic'. **kwargs: For all other kwarg options, see `GSObject.drawImage`. Returns: @@ -2136,16 +2189,7 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', **kwargs): if self.sed.dimensionless: raise GalSimSEDError("Can only draw ChromaticObjects with spectral SEDs.", self.sed) if isinstance(self.original, InterpolatedChromaticObject): - # Pass self._flux_ratio, which *could* depend on wavelength, to _get_interp_image, - # where it will be used to reweight the stored images. - int_im = self.original._get_interp_image(bandpass, image=image, integrator=integrator, - _flux_ratio=self._flux_ratio, **kwargs) - # Get shape transformations at bandpass.red_limit (they are achromatic so it doesn't - # matter where you get them). - jac, offset, _ = self._getTransformations(bandpass.red_limit) - offset = _PositionD(*offset) - int_im = transform.Transform(int_im, jac=jac, offset=offset, gsparams=self._gsparams, - propagate_gsparams=self._propagate_gsparams) + int_im = self._get_interp_image(bandpass, image, integrator, kwargs) image = int_im.drawImage(image=image, **kwargs) elif kwargs.get('method',None) == 'phot': # Need to calculate n_photons now using the fiducial profile, not self.original, @@ -2242,6 +2286,14 @@ def __init__(self, obj, sed=1., gsparams=None, propagate_gsparams=True): def sed(self): return self._flux_ratio * self.original.flux + @property + def ref_sed(self): + return self._flux_ratio + + @property + def ref_flux(self): + return self.original.flux + def _atRedshift(self, redshift): return SimpleChromaticTransformation(self.original, self._flux_ratio.atRedshift(redshift), self._gsparams, self._propagate_gsparams) @@ -2263,7 +2315,7 @@ def __str__(self): def __getstate__(self): d = self.__dict__.copy() - d.pop('sed',None) + d.pop('sed', None) return d def __setstate__(self, d): @@ -2409,6 +2461,25 @@ def sed(self): sed += obj.sed return sed + @lazy_property + def _all_ref_sed_equal(self): + sed = self.obj_list[0].ref_sed + return all(obj.ref_sed == sed for obj in self.obj_list[1:]) + + @lazy_property + def ref_sed(self): + if self._all_ref_sed_equal: + return self.obj_list[0].ref_sed + else: + return self.sed + + @lazy_property + def ref_flux(self): + if self._all_ref_sed_equal: + return sum(obj.ref_flux for obj in self.obj_list) + else: + return 1. + @property def gsparams(self): """The `GSParams` for this object. @@ -2455,7 +2526,8 @@ def __str__(self): def __getstate__(self): d = self.__dict__.copy() - d.pop('sed',None) + d.pop('sed', None) + d.pop('ref_sed', None) return d def __setstate__(self, d): @@ -2727,6 +2799,17 @@ def sed(self): sed *= obj.sed return sed + @lazy_property + def ref_sed(self): + sed = self.obj_list[0].ref_sed + for obj in self.obj_list[1:]: + sed *= obj.ref_sed + return sed + + @lazy_property + def ref_flux(self): + return np.prod([obj.ref_flux for obj in self.obj_list]) + @property def gsparams(self): """The `GSParams` for this object. @@ -2750,12 +2833,33 @@ def withGSParams(self, gsparams=None, **kwargs): @staticmethod def _get_effective_prof(insep_obj, bandpass, iimult, integrator, gsparams): - # Find scale at which to draw effective profile - # Use smallest nyquist scale among the fiducial profile and at the two limits of the bp. - _, prof0 = insep_obj._fiducial_profile(bandpass) - prof1 = insep_obj.evaluateAtWavelength(bandpass.red_limit) - prof2 = insep_obj.evaluateAtWavelength(bandpass.blue_limit) - iiscale = min(prof0.nyquist_scale, prof1.nyquist_scale, prof2.nyquist_scale) + # Early return for types that already have a straightforward effective profile. + # This avoids an extra unnecessary resampling step. + if isinstance(insep_obj, InterpolatedChromaticObject): + return insep_obj._get_interp_image(bandpass, integrator=integrator) + elif (isinstance(insep_obj, ChromaticTransformation) and + isinstance(insep_obj.original, InterpolatedChromaticObject)): + return insep_obj._get_interp_image(bandpass, None, integrator, {}) + + # Get the actual integrator object, and maybe adjust the bandpass if necessary. + wave_list, _, _ = utilities.combine_wave_list(insep_obj, bandpass) + integrator, bandpass = insep_obj._get_integrator(integrator, wave_list, bandpass) + + # Evaluate the profiles at the wave values where the integrator will need them. + # We save them here in a dict, so we can use this in the integrator, but also + # get the stepk/maxk values to set the image size and scale appropriately. + profile_dict = {wave: insep_obj.evaluateAtWavelength(wave) + for wave in integrator.calculateWaves(bandpass)} + profiles = profile_dict.values() + + # Determine stepk, maxk the normal way we do this for sums so we can force + # them when making the InterpolatedImage. In addition to the efficiency benefit of + # saving the calls to calculateStepK and calculateMaxK, this is required if the + # insep_obj has zero flux, since those cannot calculate either value from the image. + prof0 = min(profiles, key=lambda prof: prof.stepk) # We use prof0 again below. + stepk = prof0.stepk + maxk = max(prof.maxk for prof in profiles) + iiscale = math.pi / maxk iiscale /= 2 # This seems to be required to make test_monochromatic_sed to pass. # Not sure why, since I thought straight nyquist should be good enough. # But if it's needed there, it's probably worth always doing, rather than @@ -2765,18 +2869,17 @@ def _get_effective_prof(insep_obj, bandpass, iimult, integrator, gsparams): if iimult is not None: iiscale /= iimult - # Prevent infinite recursive loop by using ChromaticObject.drawImage() on a - # ChromaticConvolution. - if isinstance(insep_obj, ChromaticConvolution): - effective_prof_image = ChromaticObject.drawImage( - insep_obj, bandpass, scale=iiscale, - integrator=integrator, method='no_pixel') - else: - effective_prof_image = insep_obj.drawImage( - bandpass, scale=iiscale, integrator=integrator, - method='no_pixel') + # Set up the image using the one with the smallest stepk (needing the largest image). + image = prof0.drawImage(scale=iiscale, method='no_pixel', setup_only=True) - return InterpolatedImage(effective_prof_image, gsparams=gsparams) + # The dict __getitem__ function acts like a function of wavelength returning profiles, + # which is what the integrator needs. + effective_prof_image = integrator(profile_dict.__getitem__, bandpass, image, + {'method': 'no_pixel'}) + + # Use the calculated stepk, maxk directly here. + return InterpolatedImage(effective_prof_image, gsparams=gsparams, + _force_stepk=stepk, _force_maxk=maxk) @staticmethod def resize_effective_prof_cache(maxsize): @@ -2811,7 +2914,8 @@ def __str__(self): def __getstate__(self): d = self.__dict__.copy() - d.pop('sed',None) + d.pop('sed', None) + d.pop('ref_sed', None) return d def __setstate__(self, d): @@ -2968,7 +3072,7 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', iimult=None, * # If program gets this far, the objects in obj_list should be atomic (non-ChromaticSum # and non-ChromaticConvolution). (The latter case was dealt with in the constructor.) - # setup output image (semi-arbitrarily using the bandpass effective wavelength) + # Setup output image using fiducial profile wave0, prof0 = self._fiducial_profile(bandpass) image = prof0.drawImage(image=image, setup_only=True, **kwargs) _remove_setup_kwargs(kwargs) @@ -3016,8 +3120,8 @@ def drawImage(self, bandpass, image=None, integrator='quadratic', iimult=None, * if not obj.separable: continue wave0, prof0 = obj._fiducial_profile(bandpass) - sep_profs.append(prof0 / obj.sed(wave0)) - insep_obj *= obj.sed + sep_profs.append(prof0 / obj.ref_sed(wave0)) + insep_obj *= obj.ref_sed # Collapse inseparable profiles and chromatic normalizations into one effective profile # Note that at this point, insep_obj.sed should *not* be None. @@ -3187,6 +3291,14 @@ def __init__(self, obj, real_space=None, gsparams=None, propagate_gsparams=True) def sed(self): return self._obj.sed * self._obj.sed + @lazy_property + def ref_sed(self): + return self._obj.ref_sed * self._obj.ref_sed + + @lazy_property + def ref_flux(self): + return self._obj.ref_flux**2 + @property def gsparams(self): """The `GSParams` for this object. @@ -3224,7 +3336,8 @@ def __str__(self): def __getstate__(self): d = self.__dict__.copy() - d.pop('sed',None) + d.pop('sed', None) + d.pop('ref_sed', None) return d def __setstate__(self, d): @@ -3292,6 +3405,14 @@ def __init__(self, obj, real_space=None, gsparams=None, propagate_gsparams=True) def sed(self): return self._obj.sed * self._obj.sed + @lazy_property + def ref_sed(self): + return self._obj.ref_sed * self._obj.ref_sed + + @lazy_property + def ref_flux(self): + return self._obj.ref_flux**2 + @property def gsparams(self): """The `GSParams` for this object. @@ -3329,7 +3450,8 @@ def __str__(self): def __getstate__(self): d = self.__dict__.copy() - d.pop('sed',None) + d.pop('sed', None) + d.pop('ref_sed', None) return d def __setstate__(self, d): diff --git a/galsim/gsobject.py b/galsim/gsobject.py index 84c935bc5b2..f276fb6b7bb 100644 --- a/galsim/gsobject.py +++ b/galsim/gsobject.py @@ -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 diff --git a/galsim/interpolatedimage.py b/galsim/interpolatedimage.py index 9ed6d853f0e..4be730fdf79 100644 --- a/galsim/interpolatedimage.py +++ b/galsim/interpolatedimage.py @@ -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.: diff --git a/tests/chromatic_reference_images/.gitignore b/tests/chromatic_reference_images/.gitignore new file mode 100644 index 00000000000..7f6adb0b4e7 --- /dev/null +++ b/tests/chromatic_reference_images/.gitignore @@ -0,0 +1 @@ +r_exact.fits diff --git a/tests/chromatic_reference_images/r_exact.fits b/tests/chromatic_reference_images/r_exact.fits deleted file mode 100644 index d7c521afe7b..00000000000 Binary files a/tests/chromatic_reference_images/r_exact.fits and /dev/null differ diff --git a/tests/test_chromatic.py b/tests/test_chromatic.py index 66877ad7fc8..b4ab106d945 100644 --- a/tests/test_chromatic.py +++ b/tests/test_chromatic.py @@ -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 @@ -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). @@ -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) @@ -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) @@ -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)) @@ -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(): @@ -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) diff --git a/tests/test_interpolatedimage.py b/tests/test_interpolatedimage.py index 6d8c6dbbd4a..a119fe777f6 100644 --- a/tests/test_interpolatedimage.py +++ b/tests/test_interpolatedimage.py @@ -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',