-
Notifications
You must be signed in to change notification settings - Fork 6
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-42981: Convert AstrometryTask to new exception handling system #190
Changes from all commits
6a49253
d2f6ad5
a124fbe
8c8c87c
a619f85
9bbef5c
b830d46
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# This file is part of meas_astrom. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
__all__ = ["AstrometryError", "AstrometryFitFailure", "BadAstrometryFit", "MatcherFailure"] | ||
|
||
import lsst.pipe.base | ||
|
||
|
||
class AstrometryError(lsst.pipe.base.AlgorithmError): | ||
"""Parent class for failures in astrometric fitting. | ||
|
||
Parameters | ||
---------- | ||
msg : `str` | ||
Informative message about the nature of the error. | ||
**kwargs | ||
All other arguments are added to a ``_metadata`` attribute, which is | ||
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. Feels a little strange to have the private attribute's name included in the docs like this. Is the name not actually an implementation detail? 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'm torn on that: I directly modify 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 I see that it's effectively "protected" rather than just "private", and those are always in a nebulous state in Python already. Fine to leave it as-is. |
||
used to generate the metadata property for Task annotation. | ||
""" | ||
def __init__(self, msg, **kwargs): | ||
self.msg = msg | ||
self._metadata = kwargs | ||
super().__init__(msg, kwargs) | ||
|
||
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) or isinstance(value, float) or isinstance(value, str)): | ||
raise TypeError(f"{key} is of type {type(value)}, but only (int, float, str) are allowed.") | ||
return self._metadata | ||
|
||
|
||
class BadAstrometryFit(AstrometryError): | ||
"""Raised if the quality of the astrometric fit is worse than some | ||
threshold. | ||
|
||
Parameters | ||
---------- | ||
distMean : `float` | ||
Mean on-sky separation of matched sources, in arcseconds. | ||
distMedian : `float` | ||
Median on-sky separation of matched sources, in arcseconds. | ||
""" | ||
def __init__(self, distMean, maxMeanDist, distMedian, **kwargs): | ||
msg = f'Poor quality astrometric fit, {distMean}" > {maxMeanDist}"' | ||
super().__init__(msg, **kwargs) | ||
self._metadata["distMean"] = distMean | ||
self._metadata["maxMeanDist"] = distMean | ||
self._metadata["distMedian"] = distMedian | ||
|
||
|
||
class AstrometryFitFailure(AstrometryError): | ||
"""Raised if the astrometry fitter fails.""" | ||
|
||
|
||
class MatcherFailure(AstrometryError): | ||
"""Raised if the matcher fails.""" |
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'm surprised we don't consider it an error (or at least some sort of qualified success) if we hit the maximum number of iterations without satisfying the max distance criteria. If we add that, then I think the old form of the loop may be closer to what we want; it's a very natural
for-else
:Of course,
for-else
isn't exactly super familiar to people, and that makes me wonder if wrapping the whole loop up in a separate method that can return or raise an already-updated exception would be even better.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 like
for-else
in general, but I'm not sure it's right here. I readminMatchDistanceArcSec
as "definitely stop here, that's plenty good", and not a quality threshold. But also, on DM-44012, I'm going to look at whether this outer loop is even necessary at all: I believe that with the pessimistic matcher and the affine fitter, there's no need for this outer loop (and the ticket it was added on, DM-2755, has an argument between Paul and Russell about whether we should even do that loop. We now have a much better matcher, so the original rational mostly doesn't exist.AstrometryTask has grown organically without an overarching vision, and I think there are too many different kinds of stop/quality criteria. That's a bigger question than this ticket, though.
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.
Ok, fine to leave this as-is.