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

Make sure bounding box is attached as order F #8963

Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions changes/8963.assign_wcs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix all bounding box assignments to wcs objects so that they are correctly and
specifically assigned as order ``F`` boxes. This ensures consistency with the
assumptions made by GWCS for bounding boxes.
9 changes: 7 additions & 2 deletions jwst/assign_wcs/fgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from astropy import units as u
from astropy import coordinates as coord
from astropy.modeling import bind_bounding_box
from gwcs import coordinate_frames as cf

from stdatamodels.jwst.datamodels import DistortionModel
Expand Down Expand Up @@ -78,7 +79,10 @@
if subarray2full is not None:
# Assign a bounding_box based on subarray's xsize and ysize
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

Check warning on line 85 in jwst/assign_wcs/fgs.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/fgs.py#L85

Added line #L85 was not covered by tests

# Compute differential velocity aberration (DVA) correction:
va_corr = pointing.dva_corr_model(
Expand Down Expand Up @@ -106,7 +110,8 @@
try:
transform.bounding_box
except NotImplementedError:
transform.bounding_box = transform_bbox_from_shape(input_model.data.shape)
bind_bounding_box(transform, transform_bbox_from_shape(input_model.data.shape, order="F"), order="F")

Check warning on line 113 in jwst/assign_wcs/fgs.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/fgs.py#L113

Added line #L113 was not covered by tests

dist.close()
return transform

Expand Down
25 changes: 14 additions & 11 deletions jwst/assign_wcs/miri.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os.path
import logging
import numpy as np
from astropy.modeling import bind_bounding_box
from astropy.modeling import models
from astropy import coordinates as coord
from astropy import units as u
Expand Down Expand Up @@ -79,16 +80,17 @@
subarray2full = subarray_transform(input_model)
if subarray2full is not None:
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

Check warning on line 86 in jwst/assign_wcs/miri.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/miri.py#L86

Added line #L86 was not covered by tests
else:
# TODO: remove setting the bounding box if it is set in the new ref file.
try:
bb = distortion.bounding_box
distortion.bounding_box

Check warning on line 90 in jwst/assign_wcs/miri.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/miri.py#L90

Added line #L90 was not covered by tests
except NotImplementedError:
shape = input_model.data.shape
# Note: Since bounding_box is attached to the model here it's in reverse order.
bb = ((-0.5, shape[0] - 0.5), (3.5, shape[1] - 4.5))
distortion.bounding_box = bb
bind_bounding_box(distortion, ((3.5, shape[1] - 4.5), (-0.5, shape[0] - 0.5)), order="F")

Check warning on line 93 in jwst/assign_wcs/miri.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/miri.py#L93

Added line #L93 was not covered by tests

# Compute differential velocity aberration (DVA) correction:
va_corr = pointing.dva_corr_model(
Expand Down Expand Up @@ -147,10 +149,11 @@

if col_offset is not None and row_offset is not None:
distortion = models.Shift(col_offset) & models.Shift(row_offset) | distortion
if bbox is None:
distortion.bounding_box = transform_bbox_from_shape(input_model.data.shape)
else:
distortion.bounding_box = bbox

# Bind the bounding box to the distortion model using the bounding box ordering used by GWCS.
# This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, transform_bbox_from_shape(input_model.data.shape, order="F") if bbox is None else bbox, order="F")

Check warning on line 155 in jwst/assign_wcs/miri.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/miri.py#L155

Added line #L155 was not covered by tests

return distortion


Expand Down Expand Up @@ -365,7 +368,7 @@
dettoabl.inverse = models.Mapping((0, 1, 2, 0, 1, 2)) | aa & bb

# Bounding box is the subarray bounding box, because we're assuming subarray coordinates passed in
dettoabl.bounding_box = bb_sub[::-1]
bind_bounding_box(dettoabl, bb_sub, order="F")

return dettoabl

Expand Down Expand Up @@ -478,7 +481,7 @@
tel2sky = pointing.v23tosky(input_model) & models.Identity(1)

# Put the transforms together into a single transform
det2abl.bounding_box = transform_bbox_from_shape(input_model.data.shape)
bind_bounding_box(det2abl, transform_bbox_from_shape(input_model.data.shape, order="F"), order="F")
pipeline = [(detector, det2abl),
(miri_focal, abl2v2v3l),
(v2v3, va_corr),
Expand Down
19 changes: 13 additions & 6 deletions jwst/assign_wcs/nircam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from astropy import coordinates as coord
from astropy import units as u
from astropy.modeling import bind_bounding_box
from astropy.modeling.models import Identity, Const1D, Mapping, Shift
import gwcs.coordinate_frames as cf

Expand Down Expand Up @@ -84,7 +85,10 @@
subarray2full = subarray_transform(input_model)
if subarray2full is not None:
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

Check warning on line 91 in jwst/assign_wcs/nircam.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/nircam.py#L91

Added line #L91 was not covered by tests

# Compute differential velocity aberration (DVA) correction:
va_corr = pointing.dva_corr_model(
Expand Down Expand Up @@ -121,7 +125,9 @@
transform = dist.model

try:
bbox = transform.bounding_box
# Purposefully grab the bounding box tuple from the transform model in the
# GWCS ordering
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
Expand All @@ -146,10 +152,11 @@
transform = Shift(col_offset) & Shift(row_offset) | transform
else:
log.debug("No match in fitleroffset file.")
if bbox is None:
transform.bounding_box = transform_bbox_from_shape(input_model.data.shape)
else:
transform.bounding_box = bbox

# Bind the bounding box to the distortion model using the bounding box ordering used by GWCS.
# This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(transform, transform_bbox_from_shape(input_model.data.shape, order="F") if bbox is None else bbox, order="F")

return transform


Expand Down
27 changes: 16 additions & 11 deletions jwst/assign_wcs/niriss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asdf
from astropy import coordinates as coord
from astropy import units as u
from astropy.modeling import bind_bounding_box
from astropy.modeling.models import Const1D, Mapping, Identity, Shift
from astropy.modeling.bounding_box import CompoundBoundingBox
import gwcs.coordinate_frames as cf
Expand Down Expand Up @@ -191,11 +192,11 @@
cm_order2 = subarray2full | cm_order2
cm_order3 = subarray2full | cm_order3

bbox = ((-0.5, input_model.meta.subarray.ysize - 0.5),
(-0.5, input_model.meta.subarray.xsize - 0.5))
cm_order1.bounding_box = bbox
cm_order2.bounding_box = bbox
cm_order3.bounding_box = bbox
bbox = ((-0.5, input_model.meta.subarray.xsize - 0.5),

Check warning on line 195 in jwst/assign_wcs/niriss.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/niriss.py#L195

Added line #L195 was not covered by tests
(-0.5, input_model.meta.subarray.ysize - 0.5))
bind_bounding_box(cm_order1, bbox, order='F')
bind_bounding_box(cm_order2, bbox, order='F')
bind_bounding_box(cm_order3, bbox, order='F')

Check warning on line 199 in jwst/assign_wcs/niriss.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/niriss.py#L197-L199

Added lines #L197 - L199 were not covered by tests

# Define the transforms, they should accept (x,y) and return (ra, dec, lambda)
soss_model = NirissSOSSModel([1, 2, 3],
Expand Down Expand Up @@ -253,7 +254,10 @@
subarray2full = subarray_transform(input_model)
if subarray2full is not None:
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

Check warning on line 260 in jwst/assign_wcs/niriss.py

View check run for this annotation

Codecov / codecov/patch

jwst/assign_wcs/niriss.py#L260

Added line #L260 was not covered by tests

tel2sky = pointing.v23tosky(input_model)
pipeline = [(detector, distortion),
Expand Down Expand Up @@ -282,7 +286,7 @@
distortion = dist.model

try:
bbox = distortion.bounding_box
bbox = distortion.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
Expand All @@ -308,10 +312,11 @@
distortion = Shift(col_offset) & Shift(row_offset) | distortion
else:
log.debug("No match in fitleroffset file.")
if bbox is None:
distortion.bounding_box = transform_bbox_from_shape(input_model.data.shape)
else:
distortion.bounding_box = bbox

# Bind the bounding box to the distortion model using the bounding box ordering used by GWCS.
# This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, transform_bbox_from_shape(input_model.data.shape, order="F") if bbox is None else bbox, order="F")

return distortion


Expand Down
19 changes: 13 additions & 6 deletions jwst/assign_wcs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ def get_num_msa_open_shutters(shutter_state):
return num


def transform_bbox_from_shape(shape):
def transform_bbox_from_shape(shape, order="C"):
"""Create a bounding box from the shape of the data.

This is appropriate to attached to a transform.
Expand All @@ -730,15 +730,19 @@ def transform_bbox_from_shape(shape):
----------
shape : tuple
The shape attribute from a `numpy.ndarray` array
order : str
The order of the array. Either "C" or "F".

Returns
-------
bbox : tuple
Bounding box in y, x order.
Bounding box in y, x order if order is "C" (default)
Boundsing box in x, y order if order is "F"
"""
bbox = ((-0.5, shape[-2] - 0.5),
(-0.5, shape[-1] - 0.5))
return bbox

return bbox if order == "C" else bbox[::-1]


def wcs_bbox_from_shape(shape):
Expand All @@ -760,7 +764,7 @@ def wcs_bbox_from_shape(shape):
return bbox


def bounding_box_from_subarray(input_model):
def bounding_box_from_subarray(input_model, order='C'):
"""Create a bounding box from the subarray size.

Note: The bounding_box assumes full frame coordinates.
Expand All @@ -771,11 +775,14 @@ def bounding_box_from_subarray(input_model):
----------
input_model : `~jwst.datamodels.JwstDataModel`
The data model.
order : str
The order of the array. Either "C" or "F".

Returns
-------
bbox : tuple
Bounding box in y, x order.
Bounding box in y, x order if order is "C" (default)
Boundsing box in x, y order if order is "F"
"""
bb_xstart = -0.5
bb_xend = -0.5
Expand All @@ -788,7 +795,7 @@ def bounding_box_from_subarray(input_model):
bb_yend = input_model.meta.subarray.ysize - 0.5

bbox = ((bb_ystart, bb_yend), (bb_xstart, bb_xend))
return bbox
return bbox if order == 'C' else bbox[::-1]


def update_s_region_imaging(model):
Expand Down
2 changes: 1 addition & 1 deletion jwst/barshadow/bar_shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def create_empty_shadow_array(nshutters):

nshutters: int
The length of the slit in shutters

https://github.com/spacetelescope/gwcs/actions/runs/11661031952/job/32464576263
Returns:

empty_shadow: nddata_array
Expand Down
3 changes: 2 additions & 1 deletion jwst/extract_2d/grisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np

from astropy.modeling import bind_bounding_box
from astropy.modeling.models import Shift, Const1D, Mapping
from gwcs.wcstools import grid_from_bounding_box
from gwcs.utils import _toindex
Expand Down Expand Up @@ -470,7 +471,7 @@ def extract_grism_objects(input_model,
else:
var_flat = None

tr.bounding_box = util.transform_bbox_from_shape(ext_data.shape)
bind_bounding_box(tr, util.transform_bbox_from_shape(ext_data.shape, order="F"), order='F')
subwcs.set_transform('grism_detector', 'detector', tr)

new_slit = datamodels.SlitModel(data=ext_data,
Expand Down
Loading