From 142f804d6c3fbe2638f0daa5fdc7099ece8a1c20 Mon Sep 17 00:00:00 2001 From: Mihai Cara Date: Wed, 18 Dec 2024 04:40:12 -0500 Subject: [PATCH 1/2] Fix test for custom WCS for imaging --- jwst/resample/tests/test_resample_step.py | 37 +++++++++++++++-------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/jwst/resample/tests/test_resample_step.py b/jwst/resample/tests/test_resample_step.py index c9eb4ec1b5..597e6882a6 100644 --- a/jwst/resample/tests/test_resample_step.py +++ b/jwst/resample/tests/test_resample_step.py @@ -884,15 +884,17 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match, rng = np.random.default_rng(seed=77) im.data[:, :] = rng.random(im.data.shape) - crpix = (600, 550) - crval = (22.04, 11.98) + if output_shape2 is None: + crpix = None + else: + crpix = (output_shape2[-1] // 2, output_shape2[-2] // 2) + crval = tuple(np.mean(im.meta.wcs.footprint(), axis=0)) rotation = 15 ratio = 0.7 - # first pass - create a reference output WCS: result = ResampleStep.call( im, - output_shape=(1205, 1100), + output_shape=output_shape2, crpix=crpix, crval=crval, rotation=rotation, @@ -901,11 +903,15 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match, # make sure results are nontrivial data1 = result.data + weight1 = result.wht + assert not np.all(np.isnan(data1)) + if crpix is not None: + assert np.allclose(result.meta.wcs(*crpix), crval, rtol=1e-12, atol=0) + refwcs = str(tmp_path / "resample_refwcs.asdf") - result.meta.wcs.bounding_box = [(-0.5, 1204.5), (-0.5, 1099.5)] - asdf.AsdfFile({"wcs": result.meta.wcs}).write_to(refwcs) + asdf.AsdfFile({"wcs": result.meta.wcs, "array_shape": data1.shape}).write_to(refwcs) result = ResampleStep.call( im, @@ -914,6 +920,7 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match, ) data2 = result.data + weight2 = result.wht assert not np.all(np.isnan(data2)) if output_shape2 is not None: @@ -922,17 +929,21 @@ def test_custom_refwcs_resample_imaging(nircam_rate, output_shape2, match, if match: # test output image shape assert data1.shape == data2.shape - assert np.allclose(data1, data2, equal_nan=True) + assert np.allclose(data1, data2, equal_nan=True, rtol=1.0e-7, atol=1e-7) # make sure pixel values are similar, accounting for scale factor # (assuming inputs are in surface brightness units) - iscale = np.sqrt(im.meta.photometry.pixelarea_steradians - / compute_image_pixel_area(im.meta.wcs)) + iscale2 = ( + im.meta.photometry.pixelarea_steradians / + compute_image_pixel_area(im.meta.wcs) + ) + input_mean = np.nanmean(im.data) - output_mean_1 = np.nanmean(data1) - output_mean_2 = np.nanmean(data2) - assert np.isclose(input_mean * iscale**2, output_mean_1, atol=1e-4) - assert np.isclose(input_mean * iscale**2, output_mean_2, atol=1e-4) + total_weight = np.sum(weight1) + output_mean_1 = np.nansum(data1 * weight1) / total_weight + output_mean_2 = np.nansum(data2 * weight2) / total_weight + assert np.isclose(input_mean * iscale2, output_mean_1) + assert np.isclose(input_mean * iscale2, output_mean_2) im.close() result.close() From 2826d63822375208133d66284f4d3671f54c71d7 Mon Sep 17 00:00:00 2001 From: Mihai Cara Date: Wed, 18 Dec 2024 18:00:50 -0500 Subject: [PATCH 2/2] minor tweaks --- jwst/resample/tests/test_resample_step.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/jwst/resample/tests/test_resample_step.py b/jwst/resample/tests/test_resample_step.py index 597e6882a6..87dc4ec959 100644 --- a/jwst/resample/tests/test_resample_step.py +++ b/jwst/resample/tests/test_resample_step.py @@ -831,12 +831,13 @@ def test_resample_undefined_variance(nircam_rate, shape): @pytest.mark.parametrize('ratio', [0.7, 1.2]) @pytest.mark.parametrize('rotation', [0, 15, 135]) -@pytest.mark.parametrize('crpix', [(600, 550), (601, 551)]) -@pytest.mark.parametrize('crval', [(22.04, 11.98), (22.041, 11.981)]) -@pytest.mark.parametrize('shape', [(1205, 1100)]) +@pytest.mark.parametrize('crpix', [(256, 488), (700, 124)]) +@pytest.mark.parametrize('crval', [(22.04019, 11.98262), (22.0404, 11.983)]) +@pytest.mark.parametrize('shape', [(1020, 1010)]) def test_custom_wcs_resample_imaging(nircam_rate, ratio, rotation, crpix, crval, shape): im = AssignWcsStep.call(nircam_rate, sip_approx=False) im.data += 5 + result = ResampleStep.call( im, output_shape=shape, @@ -957,7 +958,7 @@ def test_custom_refwcs_pixel_shape_imaging(nircam_rate, tmp_path): im.data[:, :] = rng.random(im.data.shape) crpix = (600, 550) - crval = (22.04, 11.98) + crval = (22.04019, 11.98262) rotation = 15 ratio = 0.7