Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions python/lsst/meas/algorithms/dynamicDetection.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

__all__ = ["DynamicDetectionConfig", "DynamicDetectionTask"]
__all__ = ["DynamicDetectionConfig", "DynamicDetectionTask", "InsufficientSourcesError"]

import numpy as np

from lsst.pex.config import Field, ConfigurableField
from lsst.pipe.base import Struct, NoWorkFound
from lsst.pipe.base import Struct

from .detection import SourceDetectionConfig, SourceDetectionTask
from .skyObjects import SkyObjectsTask
Expand All @@ -19,6 +19,40 @@
import lsst.geom as geom


class InsufficientSourcesError(Exception):
"""Raised if an insufficient number of sky sources are found for
dynamic detection.

Parameters
----------
msg : `str`
Error message.
nGoodPix : `int`
Number of good pixels (i.e. not NO_DATA or BAD).
nPix : `int`
Total number of pixels.
**kwargs : `dict`, optional
Additional keyword arguments to initialize the Exception base class.
"""
def __init__(self, msg, nGoodPix, nPix, **kwargs):
self.msg = msg
self._metadata = kwargs
super().__init__(msg, **kwargs)
self._metadata["nGoodPix"] = int(nGoodPix)
self._metadata["nPix"] = int(nPix)

def __str__(self):
# Exception doesn't handle **kwargs, so we need a custom str.
return f"{self.msg}: {self.metadata}"

@property
def metadata(self):
for key, value in self._metadata.items():
if not isinstance(value, (int, float, str)):
raise TypeError(f"{key} is of type {type(value)}, but only (int, float, str) are allowed.")
return self._metadata


class DynamicDetectionConfig(SourceDetectionConfig):
"""Configuration for DynamicDetectionTask
"""
Expand Down Expand Up @@ -259,8 +293,8 @@ def calculateThreshold(self, exposure, seed, sigma=None, minFractionSourcesFacto
"so there should be sufficient area to locate suitable sky sources. "
f"Note that {nDetectedPix} of {nGoodPix} \"good\" pixels were marked "
"as DETECTED or DETECTED_NEGATIVE.")
raise RuntimeError(msg)
raise NoWorkFound(msg)
raise InsufficientSourcesError(msg, nGoodPix, nPix)
raise InsufficientSourcesError(msg, nGoodPix, nPix)

if not isBgTweak:
self.log.info("Number of good sky sources used for dynamic detection: %d (of %d requested).",
Expand Down
7 changes: 6 additions & 1 deletion python/lsst/meas/algorithms/subtractBackground.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
__all__ = ("SubtractBackgroundConfig", "SubtractBackgroundTask", "backgroundFlatContext")
__all__ = (
"SubtractBackgroundConfig",
"SubtractBackgroundTask",
"backgroundFlatContext",
"TooManyMaskedPixelsError",
)

import itertools
import math
Expand Down