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

Ignore the bounding box ordering warning when reading files #526

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.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

- Force ``bounding_box`` to always be returned as a ``F`` ordered box. [#522]

- Add warning filter to asdf extension to prevent the ``bounding_box`` order warning for gwcs
objects originating from a file. [#526]

0.21.0 (2024-03-10)
-------------------

Expand Down
11 changes: 10 additions & 1 deletion gwcs/converters/wcs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

import warnings
from contextlib import suppress
from asdf.extension import Converter


Expand All @@ -14,10 +16,17 @@ class WCSConverter(Converter):
types = ["gwcs.wcs.WCS"]

def from_yaml_tree(self, node, tag, ctx):
from ..wcs import WCS
from ..wcs import WCS, GwcsBoundingBoxWarning
gwcsobj = WCS(node['steps'], name=node['name'])
if 'pixel_shape' in node:
gwcsobj.pixel_shape = node['pixel_shape']

# Ignore the warning about the bounding box order for data read from a
# file. This is causing issues with files from MAST.
with suppress(AttributeError), warnings.catch_warnings():
warnings.filterwarnings('ignore', category=GwcsBoundingBoxWarning)
_ = gwcsobj.bounding_box

return gwcsobj

def to_yaml_tree(self, gwcsobj, tag, ctx):
Expand Down
11 changes: 11 additions & 0 deletions gwcs/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def gwcs_2d_shift_scale():
pipe = [(DETECTOR_2D_FRAME, m3), (ICRC_SKY_FRAME, None)]
return wcs.WCS(pipe)

def gwcs_2d_bad_bounding_box_order():
m1 = models.Shift(1) & models.Shift(2)
m2 = models.Scale(5) & models.Scale(10)
m3 = m1 | m2

# Purposefully set the bounding box in the wrong order
m3.bounding_box = ((1, 2), (3, 4))

pipe = [(DETECTOR_2D_FRAME, m3), (ICRC_SKY_FRAME, None)]
return wcs.WCS(pipe)


def gwcs_1d_freq_quantity():

Expand Down
27 changes: 27 additions & 0 deletions gwcs/tests/test_wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..utils import CoordinateFrameError
from .utils import _gwcs_from_hst_fits_wcs
from . import data
from ..examples import gwcs_2d_bad_bounding_box_order


data_path = os.path.split(os.path.abspath(data.__file__))[0]
Expand Down Expand Up @@ -1443,3 +1444,29 @@ def test_bounding_box_is_returned_F():

# Show the the bounding box is different between the two WCS objects
assert gwcs_object_after.bounding_box != gwcs_object_before.bounding_box


def test_no_bounding_box_if_read_from_file(tmp_path):
bad_wcs = gwcs_2d_bad_bounding_box_order()

# Check the waring is issued for the bounding box of this WCS object
with pytest.warns(wcs.GwcsBoundingBoxWarning):
bad_wcs.bounding_box

# Check that the warning is not issued again the second time
with warnings.catch_warnings():
warnings.simplefilter("error")
bad_wcs.bounding_box

# Write a bad wcs bounding box to an asdf file
asdf_file = tmp_path / "bad_wcs.asdf"
af = asdf.AsdfFile({"wcs": gwcs_2d_bad_bounding_box_order()}) # re-create the bad wcs object
af.write_to(asdf_file)

with asdf.open(asdf_file) as af:
wcs_from_file = af["wcs"]

# Check that no warning is issued for the bounding box of this WCS object
with warnings.catch_warnings():
warnings.simplefilter("error")
Copy link
Contributor

Choose a reason for hiding this comment

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

This filter is probably unnecessary if you already turn all warnings in pytest into exception but probably also does not hurt.

Copy link
Contributor

@pllim pllim Nov 14, 2024

Choose a reason for hiding this comment

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

OK nvm... looks like you are not doing that. #527

gwcs/pyproject.toml

Lines 87 to 90 in 7f92615

filterwarnings = [
"ignore:Models in math_functions:astropy.utils.exceptions.AstropyUserWarning",
"ignore:numpy.ndarray size changed:RuntimeWarning",
]

wcs_from_file.bounding_box
Loading