Skip to content

Allow chromatic profiles with zero-flux components. - #1379

Merged
rmjarvis merged 12 commits into
mainfrom
chromatic_zero_flux
Dec 3, 2026
Merged

rmjarvis merged 12 commits into
mainfrom
chromatic_zero_flux

Conversation

@rmjarvis

Copy link
Copy Markdown
Member

In Piff, one of my recent changes (rmjarvis/Piff#213) involves drawing an analytic derivative of a profile to get the gradient for the fit. This works fine when it's achromatic, but fails when I do this in a chromatic drawing context. It turns out there were two separate problems.

  1. The first was easy to fix. An InterpolatedImage (used for intermediate images during chromatic drawing) cannot determine stepk and maxk automatically when the total flux is zero, so you need to supply them with _force_stepk and _force_maxk. I was doing that, but it still failed because of an erroneous check. I fixed it so InterpolatedImage only checks for zero flux if these values are not present and the calculate directives are not set to False.
  2. A more serious problem is determining the fiducial profile to set up the image. It looks for a wavelength at which the profile doesn't vanish. It had been doing that using the flux evaluated at various wavelengths. But for these profiles, the flux is always 0 at every wavelength (but the images are not zero). To fix this I separate our SED into two components: ref_sed and ref_flux (ref for reference). The full SED is the reference SED times the reference flux. But keeping this separate allows these profiles to have ref_flux=0, but let _fiducial_profile look at ref_sed to find a good wavelength. This also fixed some places where normalization would involve division by zero when the flux is zero.
  3. Those two were enough for my use case, since my kernels are separable (monochromatic * sed) but if a zero-flux component is inseparable and part of a convolution, then we have to calculate stepk and maxk from the monochromatic profiles at the wavelengths used in the integral for the InterpolatedImage in _get_effective_prof.

@rmjarvis
rmjarvis requested a review from jmeyers314 November 16, 2026 23:43
@rmjarvis rmjarvis added this to the v2.9 milestone Nov 19, 2026
@rmjarvis rmjarvis added bug report Bug report chromatic Related to the Chromatic classes, SEDs, bandpasses, etc. labels Nov 19, 2026
@rmjarvis
rmjarvis force-pushed the chromatic_zero_flux branch from 1dde8a0 to 27ecf93 Compare November 19, 2026 15:36
@rmjarvis
rmjarvis changed the base branch from releases/2.8 to main November 19, 2026 15:36

@jmeyers314 jmeyers314 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.
I left a couple typos/questions for you.

Comment thread galsim/chromatic.py Resolved
Comment thread galsim/chromatic.py Resolved
Comment thread tests/test_chromatic.py
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'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's fast now, should we just always keep in memory? Or do you think it's useful as a regression test as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. It's still faster to read it rather than remake it. But mostly it didn't seem worth keeping in the repo download space just to shave a couple seconds off the first time you run the test suite.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a similar zero-flux guard in the re-thresholding bits of withGSParams()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch. These probably need some thought:

        if ret._gsparams.folding_threshold != self._gsparams.folding_threshold:
            ret._stepk = ret._getStepK(self._calculate_stepk, 0.)
        if ret._gsparams.maxk_threshold != self._gsparams.maxk_threshold:
            ret._maxk = ret._getMaxK(self._calculate_maxk, 0.)

I think you're right that these should include a check that flux != 0.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. 44c0f41

@rmjarvis
rmjarvis merged commit a1cd513 into main Dec 3, 2026
10 checks passed
@rmjarvis
rmjarvis deleted the chromatic_zero_flux branch December 3, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug report Bug report chromatic Related to the Chromatic classes, SEDs, bandpasses, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants