Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-46693: Add ability to re-apply bgModel1 in SkyCorrectionTask #989

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

aemerywatkins
Copy link

@aemerywatkins aemerywatkins commented Oct 9, 2024

The current SkyCorrectionTask has three principal steps:

  1. bgModel1 - a large-scale sky estimation and subtraction

  2. sky - a subtraction of the sky frame

  3. bgModel2 - a small-scale sky estimation and subtraction

If one has doSky = True, then bgModel1 is implied. However, during testing of DM-44889, it became apparent that the need for a doSky = True -only dataset output was required. This necessitates that bgModel1 is generated, used, and then undone.

This ticket adds that functionality into the current SkyCorrectionTask.

Note the prior related removal ticket of doBgModel1, DM-37429, which may be relevant/of use here.

@aemerywatkins aemerywatkins marked this pull request as ready for review October 10, 2024 15:06
Copy link
Contributor

@fred3m fred3m left a comment

Choose a reason for hiding this comment

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

Just a few minor comments, otherwise LGTM.

Comment on lines 31 to 37
from lsst.pipe.base import (PipelineTask, PipelineTaskConfig,
PipelineTaskConnections, Struct)
from lsst.pipe.tasks.background import (FocalPlaneBackground,
FocalPlaneBackgroundConfig,
MaskObjectsTask, SkyMeasurementTask)
from lsst.pipe.tasks.visualizeVisit import (VisualizeMosaicExpConfig,
VisualizeMosaicExpTask)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason that this formatting changed? In general we prefer not to make changes that are merely stylistic that depend on the coders style preference, and the older version is the preferred black formatting, so I would undo this change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, this formatting change should probably be reverted. When I run this file through my black/isort formatters, it also reverts it to its original format as well.

Comment on lines 180 to 184
doBgModel1 = Field(
dtype=bool,
default=True,
doc="If False, adds back initial background model after sky",
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would change the name of this config parameter. I see the symmetry in making it similar to doBgModel2 but in that case the config option chooses whether or not to do the final cleanup and background subtraction. Here, it looks like the initial background subtraction is done in order to do the sky subtraction, then it is added back in if doBgModel1 == False. So I would recommend calling this restoreOriginalBackground or something similar that doesn't make it sound as if the model 1 background subtraction is skipped completely.

Copy link
Author

Choose a reason for hiding this comment

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

How about restoreBgModel1? To keep the symmetry but clarify the usage. And then the default value will swap to False.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I misunderstand the code, but it isn't restoring bgModel1, right? I thought that it's restoring the background before bgModel1 was made and subtracted. Maybe undoBgModel1 or removeBgModel1 or revertBgModel1?

Copy link
Contributor

Choose a reason for hiding this comment

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

The order of operations facilitated here is:

  1. subtract bgModel1
  2. subtract sky
  3. remove bgModel1 from the background list
  4. subtract bgModel2

At the end of this, the skyCorr output background list should consist of:

  1. the inverted original detector-level background elements (4 of them)
  2. no bgModel1 (this has been used to fit the sky frame, and then removed from the bg list)
  3. sky frame
  4. bgModel2

With that in mind, I do agree that naming this config doBgModel1 makes it seem like a similar operation to doBgModel2, but it does act differently. I wasn't sure which alternative to endorse; I realize that in describing the order of operations and dataset outputs above I twice referred to this as a removal. However, out of context, "remove BG Model 1" sounds like you're subtracting the model, which is the opposite of what we're doing here.

So with that said, I think undoBgModel1 is a nice compromise. I think we would need to make the usage of this config clear in the doc string though.


Parameters
----------
calExps : `list` [`lsst.afw.image.exposure.ExposureF`]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can just be:

calExps : `list` [`lsst.afw.image.ExposureF`]

Copy link
Contributor

@leeskelvin leeskelvin Oct 11, 2024

Choose a reason for hiding this comment

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

(it probably needs to be changed in the doc-strings across this file at the same time too)

----------
calExps : `list` [`lsst.afw.image.exposure.ExposureF`]
Calibrated exposures to be background subtracted.
calBkgs : `list` [`lsst.afw.math._backgroundList.BackgroundList`]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can also just be:

calBkgs : `list` [`lsst.afw.math.BackgroundList`]

Copy link
Contributor

Choose a reason for hiding this comment

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

(and in the returns below)

Comment on lines 414 to 419
Returns
-------
calExps : `list` [`lsst.afw.image.maskedImage.MaskedImageF`]
Background subtracted exposures for creating a focal plane image.
calBkgs : `list` [`lsst.afw.math._backgroundList.BackgroundList`]
Updated background lists with a visit-level model removed.
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a return section here, but this function doesn't seem to return anything? Instead, it seems to edit in-place?

calBkg._backgrounds.pop(-2)

self.log.info(
"Detector %d: FFP background restored",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this log message could do with being more verbose, e.g.:

"Detector %d: Initial background model prior to sky frame subtraction (bgModel1) has been restored",

This does however now clash with usage of "initial background" on lines 377 and 396, which refer to the "original background" at the calexp level. I would favor renaming these two instances to "original background", and preserve initial background as a reference to bgModel1.

@@ -387,6 +400,37 @@ def _restoreBackgroundRefineMask(self, calExps, calBkgs):
skyCorrBases.append(skyCorrBase)
return calExps, skyCorrBases

def _restoreVisitBackground(self, calExps, calBkgs):
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest renaming this to _restoreInitialBackground.

In tandem with this, the current method _restoreBackgroundRefineMask should probably also be renamed to _restoreOriginalBackgroundRefineMask, to avoid any potential confusion.

Copy link
Contributor

Choose a reason for hiding this comment

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

PS - if we're changing the associated config to undoBgModel1, it probably also makes sense to rename this method _undoInitialBackground as well.


Returns
-------
calExps : `list` [`lsst.afw.image.maskedImage.MaskedImageF`]
Copy link
Contributor

Choose a reason for hiding this comment

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

This worried me originally, as the input calExps are ExposureF, but the output is a MaskedImageF? However, I think this returns section is going away in any case (see other related comment). You can probably replace this section with a Notes stating that the inputs are modified in-place.

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.

3 participants