Skip to content

Commit

Permalink
catch RuntimeError and raise custom exception for xyxymatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed May 16, 2024
1 parent adbb5d9 commit b2d2723
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Release Notes

- ``align_wcs`` now will raise a custom exception of type ``NotEnoughCatalogs``
when there are not enough input catalogs to perform alignment. [#203]

- ``XYXYMatch`` now will raise a custom exception of type ``MultiMatchError``
when multipe reference sources match a single input source. [#204]


0.8.7 (29-March-2024)
Expand Down
29 changes: 21 additions & 8 deletions tweakwcs/matchutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@

__author__ = 'Mihai Cara'

__all__ = ['MatchCatalogs', 'XYXYMatch']
__all__ = ['MatchCatalogs', 'XYXYMatch', 'MultiMatchError']

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class MultiMatchError(RuntimeError):
"""
Error indicating that multiple sources matched to a single reference
source.
"""


class MatchCatalogs(ABC):
""" A class that provides common interface for matching catalogs. """

Expand Down Expand Up @@ -266,13 +273,19 @@ def __call__(self, refcat, imcat, tp_pscale=1.0, tp_units=None, **kwargs):
else:
xyoff = (self._xoffset, self._yoffset)

matches = xyxymatch(
imxy,
refxy,
origin=xyoff,
tolerance=self._tolerance,
separation=self._separation
)
try:
matches = xyxymatch(
imxy,
refxy,
origin=xyoff,
tolerance=self._tolerance,
separation=self._separation
)
except RuntimeError as e:
msg = e.args[0]
if msg.startswith("Number of output coordinates exceeded allocation"):
raise MultiMatchError(msg)
raise e

Check warning on line 288 in tweakwcs/matchutils.py

View check run for this annotation

Codecov / codecov/patch

tweakwcs/matchutils.py#L288

Added line #L288 was not covered by tests

return matches['ref_idx'], matches['input_idx']

Expand Down
11 changes: 10 additions & 1 deletion tweakwcs/tests/test_matchutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import tweakwcs
from tweakwcs.matchutils import (_xy_2dhist, _estimate_2dhist_shift,
_find_peak, XYXYMatch, MatchCatalogs)
_find_peak, XYXYMatch, MatchCatalogs,
MultiMatchError)
from .helper_correctors import DummyWCSCorrector


Expand Down Expand Up @@ -293,6 +294,14 @@ def test_tpmatch(tp_wcs, use2dhist):
)


def test_multi_match_error():
tpmatch = XYXYMatch(tolerance=1.0, separation=0.01)
refcat = Table([[0.0, 0.1], [0.1, 0.0]], names=('TPx', 'TPy'), meta={'name': None})
imcat = Table([[0.0], [0.0]], names=('TPx', 'TPy'), meta={'name': None})
with pytest.raises(MultiMatchError):
tpmatch(refcat, imcat)


def test_match_catalogs_abc():
class DummyMatchCatalogs(MatchCatalogs):
def __call__(self, refcat, imcat):
Expand Down

0 comments on commit b2d2723

Please sign in to comment.