From b2d2723af7d51cb4be32c4ce4d2966cd04c23f2c Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 16 May 2024 12:29:56 -0400 Subject: [PATCH 1/5] catch RuntimeError and raise custom exception for xyxymatch error --- CHANGELOG.rst | 3 +++ tweakwcs/matchutils.py | 29 +++++++++++++++++++++-------- tweakwcs/tests/test_matchutils.py | 11 ++++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f539b99..b059ee1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) diff --git a/tweakwcs/matchutils.py b/tweakwcs/matchutils.py index d846494..5f7f3a2 100644 --- a/tweakwcs/matchutils.py +++ b/tweakwcs/matchutils.py @@ -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. """ @@ -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 return matches['ref_idx'], matches['input_idx'] diff --git a/tweakwcs/tests/test_matchutils.py b/tweakwcs/tests/test_matchutils.py index 2a3216d..b4a98de 100644 --- a/tweakwcs/tests/test_matchutils.py +++ b/tweakwcs/tests/test_matchutils.py @@ -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 @@ -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): From dcaed934769f20b63172a051eebdc445c04ad987 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 17 May 2024 11:45:43 -0400 Subject: [PATCH 2/5] rename MultiMatchError to MatchSourceConfusionError --- CHANGELOG.rst | 2 +- tweakwcs/matchutils.py | 6 +++--- tweakwcs/tests/test_matchutils.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b059ee1..ea8ebdf 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,7 +13,7 @@ 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`` +- ``XYXYMatch`` now will raise a custom exception of type ``MatchSourceConfusionError`` when multipe reference sources match a single input source. [#204] diff --git a/tweakwcs/matchutils.py b/tweakwcs/matchutils.py index 5f7f3a2..22aa7d9 100644 --- a/tweakwcs/matchutils.py +++ b/tweakwcs/matchutils.py @@ -22,13 +22,13 @@ __author__ = 'Mihai Cara' -__all__ = ['MatchCatalogs', 'XYXYMatch', 'MultiMatchError'] +__all__ = ['MatchCatalogs', 'XYXYMatch', 'MatchSourceConfusionError'] log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) -class MultiMatchError(RuntimeError): +class MatchSourceConfusionError(RuntimeError): """ Error indicating that multiple sources matched to a single reference source. @@ -284,7 +284,7 @@ def __call__(self, refcat, imcat, tp_pscale=1.0, tp_units=None, **kwargs): except RuntimeError as e: msg = e.args[0] if msg.startswith("Number of output coordinates exceeded allocation"): - raise MultiMatchError(msg) + raise MatchSourceConfusionError(msg) raise e return matches['ref_idx'], matches['input_idx'] diff --git a/tweakwcs/tests/test_matchutils.py b/tweakwcs/tests/test_matchutils.py index b4a98de..04b4bda 100644 --- a/tweakwcs/tests/test_matchutils.py +++ b/tweakwcs/tests/test_matchutils.py @@ -18,7 +18,7 @@ import tweakwcs from tweakwcs.matchutils import (_xy_2dhist, _estimate_2dhist_shift, _find_peak, XYXYMatch, MatchCatalogs, - MultiMatchError) + MatchSourceConfusionError) from .helper_correctors import DummyWCSCorrector @@ -298,7 +298,7 @@ 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): + with pytest.raises(MatchSourceConfusionError): tpmatch(refcat, imcat) From 358975801b4f7dfbf20cf6514c9315a37ca7f4ca Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 17 May 2024 11:48:15 -0400 Subject: [PATCH 3/5] add Raises to XYXYMatch.__call__ --- tweakwcs/matchutils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tweakwcs/matchutils.py b/tweakwcs/matchutils.py index 22aa7d9..c81139e 100644 --- a/tweakwcs/matchutils.py +++ b/tweakwcs/matchutils.py @@ -187,6 +187,11 @@ def __call__(self, refcat, imcat, tp_pscale=1.0, tp_units=None, **kwargs): A tuple of two 1D `numpy.ndarray` containing indices of matched sources in the ``refcat`` and ``imcat`` catalogs accordingly. + Raises + ------ + MatchSourceConfusionError + Multiple sources matched a single reference source. Try different + values for ``tolerance`` and ``separation`` to fix this error. """ # Check catalogs: if not isinstance(refcat, astropy.table.Table): From bc6018c68b81040e7aae05c21e7ccadfb9c43529 Mon Sep 17 00:00:00 2001 From: Brett Graham Date: Fri, 17 May 2024 11:50:05 -0400 Subject: [PATCH 4/5] Update docstring for exception Co-authored-by: Mihai Cara --- tweakwcs/matchutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tweakwcs/matchutils.py b/tweakwcs/matchutils.py index c81139e..7dddd3e 100644 --- a/tweakwcs/matchutils.py +++ b/tweakwcs/matchutils.py @@ -31,7 +31,7 @@ class MatchSourceConfusionError(RuntimeError): """ Error indicating that multiple sources matched to a single reference - source. + source. Try different values for ``tolerance`` and ``separation``to fix this error. """ From f91c66b3bccfd8f3f4db87368a8ab548a8ab35f4 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 17 May 2024 11:52:37 -0400 Subject: [PATCH 5/5] fix docs --- tweakwcs/matchutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tweakwcs/matchutils.py b/tweakwcs/matchutils.py index 7dddd3e..367fd40 100644 --- a/tweakwcs/matchutils.py +++ b/tweakwcs/matchutils.py @@ -31,7 +31,7 @@ class MatchSourceConfusionError(RuntimeError): """ Error indicating that multiple sources matched to a single reference - source. Try different values for ``tolerance`` and ``separation``to fix this error. + source. Try different values for ``tolerance`` and ``separation`` to fix this error. """