Skip to content

Commit

Permalink
Merge pull request #203 from mcara/add-custom-exception
Browse files Browse the repository at this point in the history
align_wcs to raise custom exception when not enough inputs
  • Loading branch information
mcara committed Apr 19, 2024
2 parents fb46242 + ac5cefd commit adbb5d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Release Notes
.. 0.8.8 (unreleased)
==================
0.8.8 (unreleased)
==================

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


0.8.7 (29-March-2024)
=====================

Expand Down
24 changes: 21 additions & 3 deletions tweakwcs/imalign.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@

__author__ = 'Mihai Cara'

__all__ = ['fit_wcs', 'align_wcs']
__all__ = ['fit_wcs', 'align_wcs', 'NotEnoughCatalogs']

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


class NotEnoughCatalogs(ValueError):
"""
An exception class used to notify that an alignment routine does not have
enough input catalogs to perform alignment.
"""
pass


@deprecated_renamed_argument('tpwcs', 'corrector', since='0.8.0')
def fit_wcs(refcat, imcat, corrector, ref_tpwcs=None, fitgeom='general',
nclip=3, sigma=(3.0, 'rmse'), clip_accum=False,
Expand Down Expand Up @@ -465,6 +473,14 @@ def align_wcs(wcscat, refcat=None, ref_tpwcs=None, enforce_user_order=True,
``refcat`` catalog, an expanded ``refcat`` with a combination of
source positions from all input images.
Raises
------
NotEnoughCatalogs
This exception is raised when there are not enough input catalogs
to perform alignment. For example, ``wcscat`` must be a list of at least
two correctors when ``refcat`` is `None`, or be a single corrector
when a refernce catalog is provided via ``refcat``.
Notes
-----
**1. Weights:**
Expand Down Expand Up @@ -649,8 +665,10 @@ def align_wcs(wcscat, refcat=None, ref_tpwcs=None, enforce_user_order=True,

# check that we have enough input images:
if (refcat is None and len(wcs_gcat) < 2) or len(wcs_gcat) == 0:
raise ValueError("Too few input images (or groups of images) with "
"non-empty catalogs.")
raise NotEnoughCatalogs(
"Too few input images (or groups of images) with "
"non-empty catalogs."
)

# get the first image to be aligned and
# create reference catalog if needed:
Expand Down
11 changes: 8 additions & 3 deletions tweakwcs/tests/test_imalign.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
from astropy.table import Table

from tweakwcs.linearfit import build_fit_matrix
from tweakwcs.imalign import fit_wcs, align_wcs, max_overlap_pair
from tweakwcs.imalign import (
fit_wcs,
align_wcs,
max_overlap_pair,
NotEnoughCatalogs,
)
from tweakwcs import FITSWCSCorrector


Expand All @@ -26,7 +31,7 @@ def test_fit_wcs_empty_cat(empty_refcat, empty_imcat, mock_fits_wcs):
meta={'catalog': Table([[], []], names=('x', 'y')), 'group_id': 1}
)

with pytest.raises(ValueError) as e:
with pytest.raises(NotEnoughCatalogs) as e:
align_wcs([tpwcs, tpwcs, tpwcs])
assert e.value.args[0] == ("Too few input images (or groups of images) "
"with non-empty catalogs.")
Expand Down Expand Up @@ -472,7 +477,7 @@ def test_align_wcs_1im_no_ref(mock_fits_wcs):
imcat = Table(xy, names=('x', 'y'))
tpwcs = FITSWCSCorrector(mock_fits_wcs, meta={'catalog': imcat})

with pytest.raises(ValueError) as e:
with pytest.raises(NotEnoughCatalogs) as e:
align_wcs(tpwcs, refcat=None, fitgeom='shift', match=None)
assert e.value.args[0] == ("Too few input images (or groups of images) "
"with non-empty catalogs.")

0 comments on commit adbb5d9

Please sign in to comment.