Skip to content

Commit 8685151

Browse files
committed
Add errors to be imported with __all__
1 parent 98a1a72 commit 8685151

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

python/lsst/meas/algorithms/dynamicDetection.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
__all__ = ["DynamicDetectionConfig", "DynamicDetectionTask"]
2+
__all__ = ["DynamicDetectionConfig", "DynamicDetectionTask", "InsufficientSourcesError"]
33

44
import numpy as np
55

66
from lsst.pex.config import Field, ConfigurableField
7-
from lsst.pipe.base import Struct, NoWorkFound
7+
from lsst.pipe.base import Struct
88

99
from .detection import SourceDetectionConfig, SourceDetectionTask
1010
from .skyObjects import SkyObjectsTask
@@ -19,6 +19,40 @@
1919
import lsst.geom as geom
2020

2121

22+
class InsufficientSourcesError(Exception):
23+
"""Raised if an insufficient number of sky sources are found for
24+
dynamic detection.
25+
26+
Parameters
27+
----------
28+
msg : `str`
29+
Error message.
30+
nGoodPix : `int`
31+
Number of good pixels (i.e. not NO_DATA or BAD).
32+
nPix : `int`
33+
Total number of pixels.
34+
**kwargs : `dict`, optional
35+
Additional keyword arguments to initialize the Exception base class.
36+
"""
37+
def __init__(self, msg, nGoodPix, nPix, **kwargs):
38+
self.msg = msg
39+
self._metadata = kwargs
40+
super().__init__(msg, **kwargs)
41+
self._metadata["nGoodPix"] = int(nGoodPix)
42+
self._metadata["nPix"] = int(nPix)
43+
44+
def __str__(self):
45+
# Exception doesn't handle **kwargs, so we need a custom str.
46+
return f"{self.msg}: {self.metadata}"
47+
48+
@property
49+
def metadata(self):
50+
for key, value in self._metadata.items():
51+
if not (isinstance(value, int) or isinstance(value, float) or isinstance(value, str)):
52+
raise TypeError(f"{key} is of type {type(value)}, but only (int, float, str) are allowed.")
53+
return self._metadata
54+
55+
2256
class DynamicDetectionConfig(SourceDetectionConfig):
2357
"""Configuration for DynamicDetectionTask
2458
"""
@@ -259,8 +293,8 @@ def calculateThreshold(self, exposure, seed, sigma=None, minFractionSourcesFacto
259293
"so there should be sufficient area to locate suitable sky sources. "
260294
f"Note that {nDetectedPix} of {nGoodPix} \"good\" pixels were marked "
261295
"as DETECTED or DETECTED_NEGATIVE.")
262-
raise RuntimeError(msg)
263-
raise NoWorkFound(msg)
296+
raise InsufficientSourcesError(msg, nGoodPix, nPix)
297+
raise InsufficientSourcesError(msg, nGoodPix, nPix)
264298

265299
if not isBgTweak:
266300
self.log.info("Number of good sky sources used for dynamic detection: %d (of %d requested).",

python/lsst/meas/algorithms/subtractBackground.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
# the GNU General Public License along with this program. If not,
2121
# see <https://www.lsstcorp.org/LegalNotices/>.
2222
#
23-
__all__ = ("SubtractBackgroundConfig", "SubtractBackgroundTask", "backgroundFlatContext")
23+
__all__ = (
24+
"SubtractBackgroundConfig",
25+
"SubtractBackgroundTask",
26+
"backgroundFlatContext",
27+
"TooManyMaskedPixelsError",
28+
)
2429

2530
import itertools
2631
import math

0 commit comments

Comments
 (0)