Skip to content

Commit

Permalink
added unit test and fixed mypy problems
Browse files Browse the repository at this point in the history
  • Loading branch information
emolter committed Oct 16, 2024
1 parent b873279 commit 2d548ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/stcal/alignment/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _get_axis_min_and_bounding_box(footprints: list[np.ndarray],


def _calculate_fiducial(footprints: list[np.ndarray],
crval: Sequence | None = None) -> np.ndarray:
crval: Sequence | None = None) -> tuple:
"""
Calculates the coordinates of the fiducial point and, if necessary, updates it with
the values in CRVAL (the update is applied to spatial axes only).
Expand All @@ -197,15 +197,15 @@ def _calculate_fiducial(footprints: list[np.ndarray],
Returns
-------
fiducial : np.ndarray
A two-elements array containing the world coordinate of the fiducial point.
fiducial : tuple
A tuple containing the world coordinate of the fiducial point.
"""
if crval is not None:
return crval
return tuple(crval)
return compute_fiducial(footprints)


def _calculate_offsets(fiducial: np.ndarray,
def _calculate_offsets(fiducial: tuple,
wcs: gwcs.wcs.WCS | None,
axis_min_values: np.ndarray | None,
crpix: Sequence | None) -> astmodels.Model:
Expand All @@ -214,8 +214,8 @@ def _calculate_offsets(fiducial: np.ndarray,
Parameters
----------
fiducial : np.ndarray
A two-elements containing the world coordinates of the fiducial point.
fiducial : tuple
A tuple containing the world coordinates of the fiducial point.
wcs : ~gwcs.wcs.WCS
A WCS object. It will be used to determine the
Expand Down Expand Up @@ -254,7 +254,7 @@ def _calculate_offsets(fiducial: np.ndarray,
def _calculate_new_wcs(wcs: gwcs.wcs.WCS,
shape: Sequence | None,
footprints: list[np.ndarray],
fiducial: np.ndarray,
fiducial: tuple,
crpix: Sequence | None = None,
transform: astmodels.Model | None = None,
) -> gwcs.wcs.WCS:
Expand All @@ -274,8 +274,8 @@ def _calculate_new_wcs(wcs: gwcs.wcs.WCS,
A list of numpy arrays each of shape (N, 2) containing the
(RA, Dec) vertices demarcating the footprint of the input WCSs.
fiducial : np.ndarray
A two-elements array containing the location on the sky in some standard
fiducial : tuple
A tuple containing the location on the sky in some standard
coordinate system.
crpix : tuple, optional
Expand Down Expand Up @@ -382,7 +382,7 @@ def compute_scale(
return float(np.sqrt(xscale * yscale))


def compute_fiducial(footprints: list[np.ndarray]) -> np.ndarray:
def compute_fiducial(footprints: list[np.ndarray]) -> tuple:
"""
Calculates the world coordinates of the fiducial point of a list of WCS objects.
For a celestial footprint this is the center. For a spectral footprint, it is the
Expand All @@ -403,8 +403,8 @@ def compute_fiducial(footprints: list[np.ndarray]) -> np.ndarray:
Returns
-------
fiducial : np.ndarray
A two-elements array containing the world coordinates of the fiducial point
fiducial : tuple
A tuple containing the world coordinates of the fiducial point
in the combined output coordinate frame.
Notes
Expand Down Expand Up @@ -459,7 +459,7 @@ def calc_rotation_matrix(roll_ref: float, v3i_yangle: float, vparity: int = 1) -
return [pc1_1, pc1_2, pc2_1, pc2_2]


def sregion_to_footprint(s_region):
def _sregion_to_footprint(s_region: str) -> np.ndarray:
"""
Parameters
----------
Expand All @@ -476,7 +476,7 @@ def sregion_to_footprint(s_region):


def wcs_from_footprints(
footprints: list[np.ndarray],
footprints: list[np.ndarray] | list[str],
ref_wcs: gwcs.wcs.WCS,
ref_wcsinfo: dict,
transform: astropy.modeling.models.Model | None = None,
Expand Down Expand Up @@ -504,9 +504,11 @@ def wcs_from_footprints(
Parameters
----------
footprints : list
A list of numpy arrays each of shape (N, 2) containing the
footprints : list of np.ndarray or list of str
If list elements are numpy arrays, each should have shape (N, 2) and contain
(RA, Dec) vertices demarcating the footprint of the input WCSs.
If list elements are strings, each should be the S_REGION header keyword
containing (RA, Dec) vertices demarcating the footprint of the input WCSs.
ref_wcs :
A valid datamodel whose WCS is used as reference for the creation of the output
Expand Down Expand Up @@ -558,6 +560,7 @@ def wcs_from_footprints(
The WCS object corresponding to the combined input footprints.
"""
footprints = [_sregion_to_footprint(s_region) for s_region in footprints if isinstance(s_region, str)]
fiducial = _calculate_fiducial(footprints, crval=crval)

transform = _generate_tranform(
Expand Down
14 changes: 14 additions & 0 deletions tests/test_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
compute_s_region_keyword,
compute_scale,
reproject,
_sregion_to_footprint,
wcs_bbox_from_shape,
wcs_from_footprints,
)
Expand Down Expand Up @@ -154,6 +155,19 @@ def test_compute_scale(pscales):
assert np.isclose(expected_scale, computed_scale)


def test_sregion_to_footprint():
"""Test that util._sregion_to_footprint can properly convert an S_REGION
string to a list of vertices.
"""
s_region = "POLYGON ICRS 1.000000000 2.000000000 3.000000000 4.000000000 5.000000000 6.000000000 7.000000000 8.000000000"
expected_footprint = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]])

footprint = _sregion_to_footprint(s_region)

assert footprint.shape == (4,2)
assert np.allclose(footprint, expected_footprint)


def test_wcs_from_footprints():
"""
Test that the WCS created from wcs_from_footprints has correct vertice coordinates.
Expand Down

0 comments on commit 2d548ad

Please sign in to comment.