Skip to content

Commit

Permalink
Remove unphysical diaSources from the output of detectAndMeasure
Browse files Browse the repository at this point in the history
  • Loading branch information
isullivan committed Dec 1, 2023
1 parent 8b71add commit 4ce7458
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions python/lsst/ip/diffim/detectAndMeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from deprecated.sphinx import deprecated
import numpy as np

import lsst.afw.table as afwTable
import lsst.daf.base as dafBase
Expand Down Expand Up @@ -140,6 +141,18 @@ class DetectAndMeasureConfig(pipeBase.PipelineTaskConfig,
target=SkyObjectsTask,
doc="Generate sky sources",
)
badSourceFlags = lsst.pex.config.ListField(
dtype=str,
doc="Flags that, if set, the associated source should not "
"be included in the output diaSource catalog.",
default=("base_PixelFlags_flag_offimage",
"base_PixelFlags_flag_interpolatedCenterAll",
"base_PixelFlags_flag_saturatedCenterAll",
"base_PixelFlags_flag_crCenterAll",
"base_PixelFlags_flag_badCenterAll",
"base_PixelFlags_flag_suspectCenterAll",
),
)
idGenerator = DetectorVisitIdGeneratorConfig.make_field()

def setDefaults(self):
Expand Down Expand Up @@ -361,6 +374,7 @@ def processResults(self, science, matchedTemplate, difference, sources, table,
self.addSkySources(diaSources, difference.mask, difference.info.id)

self.measureDiaSources(diaSources, science, difference, matchedTemplate)
self.purgeSources(diaSources)

if self.config.doForcedMeasurement:
self.measureForcedSources(diaSources, science, difference.getWcs())
Expand All @@ -372,6 +386,27 @@ def processResults(self, science, matchedTemplate, difference, sources, table,

return measurementResults

def purgeSources(self, diaSources):
"""Remove bad diaSources from the catalog.
Parameters
----------
diaSources : `lsst.afw.table.SourceCatalog`
The catalog of detected sources. Modified in place if any sources
have a flag in `badSourceFlags` set.
"""
flags = np.ones(len(diaSources), dtype=bool)
for flag in self.config.badSourceFlags:
try:
flags *= ~diaSources[flag]
except Exception as e:
self.log.warning("Could not apply source flag: %s", e)
nPurged = np.sum(~flags)
if nPurged > 0:
self.log.warning(f"Found and purged {nPurged} unphysical sources.")
diaSources = diaSources[flags].copy(deep=True)
self.metadata.add("nPurgedSources", nPurged)

def addSkySources(self, diaSources, mask, seed):
"""Add sources in empty regions of the difference image
for measuring the background.
Expand Down

0 comments on commit 4ce7458

Please sign in to comment.