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

Clean up bounding box assignment #1527

Merged
Merged
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
1 change: 1 addition & 0 deletions changes/1527.general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Perform bounding box assignment inline with the ordering that GWCS prefers.
12 changes: 7 additions & 5 deletions romancal/assign_wcs/assign_wcs_step.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import gwcs.coordinate_frames as cf
from astropy import coordinates as coord
from astropy import units as u
from astropy.modeling import bind_bounding_box
from gwcs.wcs import WCS, Step
from roman_datamodels import datamodels as rdm

@@ -141,17 +142,18 @@ def wfi_distortion(model, reference_files):
transform = dist.coordinate_distortion_transform

try:
bbox = transform.bounding_box
bbox = transform.bounding_box.bounding_box(order="F")
except NotImplementedError:
# Check if the transform in the reference file has a ``bounding_box``.
# If not set a ``bounding_box`` equal to the size of the image after
# assembling all distortion corrections.
bbox = None
dist.close()

if bbox is None:
transform.bounding_box = wcs_bbox_from_shape(model.data.shape)
else:
transform.bounding_box = bbox
bind_bounding_box(
transform,
wcs_bbox_from_shape(model.data.shape) if bbox is None else bbox,
order="F",
)
Comment on lines +153 to +157
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a case where the GWCS warning identified a bug. It just did not get picked up by any tests...

Note

def wcs_bbox_from_shape(shape):
"""Create a bounding box from the shape of the data.
This is appropriate to attach to a wcs object.
Parameters
----------
shape : tuple
The shape attribute from a `numpy.ndarray` array.
Returns
-------
bbox : tuple
Bounding box in x, y order.
"""
bbox = ((-0.5, shape[-1] - 0.5), (-0.5, shape[-2] - 0.5))
return bbox

Reports to create a bounding box in the x, y ordering but the transform.bounding_box = was making the assumption this was a y, x box.

With this change it is now being assigned correctly with an x, y ordering.


return transform
2 changes: 1 addition & 1 deletion romancal/conftest.py
Original file line number Diff line number Diff line change
@@ -198,7 +198,6 @@ def _create_wcs(input_dm, shift_1=0, shift_2=0):

# create necessary transformations
distortion = Shift(-shift_1) & Shift(-shift_2)
distortion.bounding_box = ((-0.5, shape[-1] + 0.5), (-0.5, shape[-2] + 0.5))
tel2sky = pointing.v23tosky(input_dm)

# create required frames
@@ -219,6 +218,7 @@ def _create_wcs(input_dm, shift_1=0, shift_2=0):
]

wcs_obj = wcs.WCS(pipeline)
wcs_obj.bounding_box = ((-0.5, shape[-2] + 0.5), (-0.5, shape[-1] + 0.5))

input_dm.meta["wcs"] = wcs_obj

2 changes: 1 addition & 1 deletion romancal/tweakreg/tests/test_astrometric_utils.py
Original file line number Diff line number Diff line change
@@ -254,7 +254,6 @@ def create_wcs_for_tweakreg_pipeline(input_dm, shift_1=0, shift_2=0):

# create necessary transformations
distortion = Shift(-shift_1) & Shift(-shift_2)
distortion.bounding_box = ((-0.5, shape[-1] + 0.5), (-0.5, shape[-2] + 0.5))
tel2sky = _create_tel2sky_model(input_dm)

# create required frames
@@ -276,6 +275,7 @@ def create_wcs_for_tweakreg_pipeline(input_dm, shift_1=0, shift_2=0):
]

wcs_obj = wcs.WCS(pipeline)
wcs_obj.bounding_box = ((-0.5, shape[-2] + 0.5), (-0.5, shape[-1] + 0.5))

input_dm.meta["wcs"] = wcs_obj

2 changes: 1 addition & 1 deletion romancal/tweakreg/tests/test_tweakreg.py
Original file line number Diff line number Diff line change
@@ -316,7 +316,6 @@ def create_wcs_for_tweakreg_pipeline(input_dm, shift_1=0, shift_2=0):

# create necessary transformations
distortion = Shift(-shift_1) & Shift(-shift_2)
distortion.bounding_box = ((-0.5, shape[-1] + 0.5), (-0.5, shape[-2] + 0.5))
tel2sky = _create_tel2sky_model(input_dm)

# create required frames
@@ -337,6 +336,7 @@ def create_wcs_for_tweakreg_pipeline(input_dm, shift_1=0, shift_2=0):
]

wcs_obj = wcs.WCS(pipeline)
wcs_obj.bounding_box = ((-0.5, shape[-2] + 0.5), (-0.5, shape[-1] + 0.5))

input_dm.meta["wcs"] = wcs_obj