Skip to content

Conversation

@aemerywatkins
Copy link
Contributor

The matchBackgrounds.py script was an initial attempt at matching multiple warped visit-level images to a given reference image. Taking the difference images between the reference and each subsequent image allows for each of these images to be modified to match the background of the reference image. These images may then be stacked (coadded), and a single background estimation made on the combined (and, importantly, deeper) coadd image.

The existing matchBackgroundsTask has not been utilized in some time. This ticket aims to explore the feasibility of resurrecting this code base, with a mind towards testing on HSC in the near-term.

@leeskelvin leeskelvin force-pushed the tickets/DM-44889 branch 2 times, most recently from a95c234 to b0463a5 Compare July 19, 2024 19:32
@aemerywatkins aemerywatkins marked this pull request as ready for review June 5, 2025 13:58
@aemerywatkins aemerywatkins requested a review from TallJimbo June 5, 2025 14:14
Copy link
Member

@TallJimbo TallJimbo left a comment

Choose a reason for hiding this comment

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

Seems like a decent cleanup of the existing code. Only serious problem is the use of a single BackgroundList to represent the backgrounds for all visits, but that should be easy to fix (and I think using more visit-ID-keyed dicts instead of lists will help in other parts of the code, too).

@aemerywatkins aemerywatkins force-pushed the tickets/DM-44889 branch 2 times, most recently from d957a3c to b7318ec Compare November 13, 2025 16:59
aemerywatkins and others added 14 commits November 21, 2025 08:08
Initial version of this task is written using old architecture, and so
needs updating.  In MatchBackgroundsTask, and its method
selectRefExposure, required parameters were equally outdated: DataId,
DatasetType, and ImageScaler.  All of these now seem consolidated under
lsst.afw.image.Exposure, so separate calls to DataId and DatasetType are
now single calls for Exposure objects.  ImageScaler calls were replaced
in-line with Exposure.getPhotoCalib() calls, to scale all image flux to
the same zeropoint (nJy).

Also, we want to process visit-level images using this, so a
MatchBackgroundsConnections class was created, MatchBackgroundsConfig
was updated to inherit from PipelineTaskConfig (and those connections),
and a rudimentary runQuantum method was added to MatchBackgroundsTask.
Code now runs without complaint through self.matchBackgrounds.
Also added a self._fluxScale method to replace repeat code blocks.
Will decide later if scaling to nJy is the best way to do this.
Code is now functional, in that it accepts images and returns
difference image background models as "psfMatchedWarpBackground_diff"
(name likely to be altered later).  Uses a fit to a blank image for
that corresponding to the reference image.
Difference background models are now formatted properly, to allow
for image creation from the spline parameters.  Also did some
adjustments to documentation for Flake8 formatting.
_defineWarps() now rejects any image with all NaNs along any image
edge, and creates the cost function using a sky-subtracted image.
This sky-subtraction fits a 1st order Chebyshev polynomial to the
masked image background.

Also fixed a bug from LSK refactor by inserting a blank sky model
into the background model list at the chosen reference image index.
Otherwise, changes are clean-up from previous refactoring to restore
functionality, plus a bug fix.  Bug fix was the restoration of two
lines of code in MatchBackgroundsTask.matchBackgrounds() which
produced a difference image to work from.
All images and background models now returned in counts, not nJy.
`matchBackgrounds` in its original form matches by warps, i.e.
single detectors.  A more sensible thing to do is to match
backgrounds across the whole focal plane.  This functionality
needed to be added to do this using warped exposures, in tract
coordinates, so this is now added to `backgrounds.py`.  This
commit also includes a partial revision to `matchBackgrounds.py`
using this new functionality, choosing a reference visit ID based
on these background models instead of on individual warps.  Full
functionality has yet to be restored in light of these changes.
All methods, including run, updated to function properly with
tract backgrounds rather than warps.  Task completes without
error when run on three full visits, and output appears roughly
correct.
@aemerywatkins aemerywatkins merged commit 9d21009 into main Nov 25, 2025
2 checks passed
@aemerywatkins aemerywatkins deleted the tickets/DM-44889 branch November 25, 2025 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants