-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,13 @@ | |
import lsst.pipe.base.connectionTypes as cT | ||
import numpy as np | ||
from lsst.pex.config import Config, ConfigField, ConfigurableField, Field | ||
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 | ||
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) | ||
|
||
|
||
def _skyFrameLookup(datasetType, registry, quantumDataId, collections): | ||
|
@@ -178,6 +177,11 @@ class SkyCorrectionConfig(PipelineTaskConfig, pipelineConnections=SkyCorrectionC | |
dtype=FocalPlaneBackgroundConfig, | ||
doc="Initial background model, prior to sky frame subtraction", | ||
) | ||
doBgModel1 = Field( | ||
dtype=bool, | ||
default=True, | ||
doc="If False, adds back initial background model after sky", | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of operations facilitated here is:
At the end of this, the
With that in mind, I do agree that naming this config So with that said, I think |
||
sky = ConfigurableField( | ||
target=SkyMeasurementTask, | ||
doc="Sky measurement", | ||
|
@@ -210,6 +214,11 @@ def setDefaults(self): | |
self.bgModel2.ySize = 256 | ||
self.bgModel2.smoothScale = 1.0 | ||
|
||
def validate(self): | ||
super().validate() | ||
if not self.doBgModel1 and not self.doSky and not self.doBgModel2: | ||
raise ValueError("Task requires at least one of doBgModel1, doSky or doBgModel2 to be True.") | ||
|
||
|
||
class SkyCorrectionTask(PipelineTask): | ||
"""Perform a full focal plane sky correction.""" | ||
|
@@ -310,6 +319,10 @@ def run(self, calExps, calBkgs, skyFrames, camera): | |
if self.config.doSky: | ||
self._subtractSkyFrame(calExps, skyFrames, calBkgs) | ||
|
||
# Adds full-fp bg back onto exposures, removes it from list | ||
if not self.config.doBgModel1: | ||
_ = self._restoreVisitBackground(calExps, calBkgs) | ||
|
||
# Bin exposures, generate full-fp bg, map to CCDs and subtract in-place | ||
if self.config.doBgModel2: | ||
_ = self._subtractVisitBackground(calExps, calBkgs, camera, self.config.bgModel2) | ||
|
@@ -387,6 +400,37 @@ def _restoreBackgroundRefineMask(self, calExps, calBkgs): | |
skyCorrBases.append(skyCorrBase) | ||
return calExps, skyCorrBases | ||
|
||
def _restoreVisitBackground(self, calExps, calBkgs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest renaming this to In tandem with this, the current method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS - if we're changing the associated config to |
||
"""Restores the full focal-plane background to a visit. | ||
Runs after _subtractSkyFrame() if doBgModel1=False. | ||
|
||
Parameters | ||
---------- | ||
calExps : `list` [`lsst.afw.image.exposure.ExposureF`] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can just be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
Calibrated exposures to be background subtracted. | ||
calBkgs : `list` [`lsst.afw.math._backgroundList.BackgroundList`] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also just be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (and in the returns below) |
||
Background lists associated with the input calibrated exposures. | ||
|
||
Returns | ||
------- | ||
calExps : `list` [`lsst.afw.image.maskedImage.MaskedImageF`] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This worried me originally, as the input |
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
""" | ||
for calExp, calBkg in zip(calExps, calBkgs): | ||
image = calExp.getMaskedImage() | ||
|
||
# Restore full focal-plane background in calexp; remove from BGList | ||
skyCorrBase = calBkg[-2][0].getImageF() | ||
image += skyCorrBase | ||
calBkg._backgrounds.pop(-2) | ||
|
||
self.log.info( | ||
"Detector %d: FFP background restored", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.:
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 |
||
calExp.getDetector().getId(), | ||
) | ||
|
||
def _subtractVisitBackground(self, calExps, calBkgs, camera, config): | ||
"""Perform a full focal-plane background subtraction for a visit. | ||
|
||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.