Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test for custom WCS for imaging #9017

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions jwst/resample/tests/test_resample_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -884,15 +885,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,
Expand All @@ -901,11 +904,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,
Expand All @@ -914,6 +921,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:
Expand All @@ -922,17 +930,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()
Expand All @@ -946,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

Expand Down
Loading