From 2d548ad56cc3b206a439e1e5584e061f63ad9fed Mon Sep 17 00:00:00 2001 From: Ned Molter Date: Wed, 16 Oct 2024 11:06:50 -0400 Subject: [PATCH] added unit test and fixed mypy problems --- src/stcal/alignment/util.py | 37 ++++++++++++++++++++----------------- tests/test_alignment.py | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/stcal/alignment/util.py b/src/stcal/alignment/util.py index bc328be5..174884ab 100644 --- a/src/stcal/alignment/util.py +++ b/src/stcal/alignment/util.py @@ -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). @@ -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: @@ -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 @@ -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: @@ -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 @@ -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 @@ -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 @@ -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 ---------- @@ -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, @@ -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 @@ -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( diff --git a/tests/test_alignment.py b/tests/test_alignment.py index 9a4aef40..68ab766b 100644 --- a/tests/test_alignment.py +++ b/tests/test_alignment.py @@ -15,6 +15,7 @@ compute_s_region_keyword, compute_scale, reproject, + _sregion_to_footprint, wcs_bbox_from_shape, wcs_from_footprints, ) @@ -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.