Skip to content

Commit

Permalink
deprecate cast_fits_array and cast_arrays (#214)
Browse files Browse the repository at this point in the history
Co-authored-by: Howard Bushouse <bushouse@stsci.edu>
  • Loading branch information
braingram and hbushouse authored Dec 6, 2023
1 parent 12a4c3a commit b7eada9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Bug Fixes
Changes to API
--------------

-
- Deprecate ``cast_arrays`` argument to ``from_fits_hdu`` and
``cast_fits_arrays`` argument to ``Datamodel.__init__`` [#214]

Other
-----
Expand All @@ -29,6 +30,9 @@ Bug Fixes
- Fix ``rebuild_fits_rec_dtype`` handling of unsigned integer columns
with shapes [#213]

Changes to API
--------------

- Sort keyword files used for schema_editor to make output non-arbitrary
copy schema before merging to avoid schema modification [#227]

Expand Down
6 changes: 5 additions & 1 deletion src/stdatamodels/fits_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,14 @@ def callback(node, json_id):
af._tree = treeutil.walk_and_modify(af.tree, callback)


def from_fits_hdu(hdu, schema, cast_arrays=True):
def from_fits_hdu(hdu, schema, cast_arrays=None):
"""
Read the data from a fits hdu into a numpy ndarray
"""
if cast_arrays is not None:
warnings.warn("cast_arrays is deprecated and will be removed")
else:
cast_arrays = True
data = hdu.data

if cast_arrays:
Expand Down
5 changes: 0 additions & 5 deletions src/stdatamodels/jwst/datamodels/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ def open(init=None, guess=True, memmap=False, **kwargs):
- FITS
cast_fits_arrays : bool
If `True`, arrays will be cast to the dtype described by the schema
when read from a FITS file.
If `False`, arrays will be read without casting.
skip_fits_update : bool or None
`True` to skip updating the ASDF tree from the FITS headers, if possible.
If `None`, value will be taken from the environmental SKIP_FITS_UPDATE.
Expand Down
8 changes: 6 additions & 2 deletions src/stdatamodels/model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DataModel(properties.ObjectNode):

def __init__(self, init=None, schema=None, memmap=False,
pass_invalid_values=None, strict_validation=None,
validate_on_assignment=None, cast_fits_arrays=True,
validate_on_assignment=None, cast_fits_arrays=None,
validate_arrays=False, ignore_missing_extensions=True, **kwargs):
"""
Parameters
Expand Down Expand Up @@ -158,7 +158,11 @@ def __init__(self, init=None, schema=None, memmap=False,
self._strict_validation = strict_validation
self._ignore_missing_extensions = ignore_missing_extensions
self._validate_on_assignment = validate_on_assignment
self._cast_fits_arrays = cast_fits_arrays
if cast_fits_arrays is not None:
warnings.warn("cast_fits_array is deprecated and will be removed", DeprecationWarning)
self._cast_fits_arrays = cast_fits_arrays
else:
self._cast_fits_arrays = None
self._validate_arrays = validate_arrays

kwargs.update({'ignore_missing_extensions': ignore_missing_extensions})
Expand Down
17 changes: 13 additions & 4 deletions tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,15 @@ def test_ndarray_validation(tmp_path):

# But raise an error when casting is disabled
with pytest.raises(ValidationError, match="Array datatype 'float64' is not compatible with 'float32'"):
with FitsModel(file_path, strict_validation=True, cast_fits_arrays=False, validate_arrays=True) as model:
model.validate()
# also warn due to the cast_fits_array deprecation
with pytest.warns(DeprecationWarning, match="cast_fits_array"):
with FitsModel(
file_path,
strict_validation=True,
cast_fits_arrays=False,
validate_arrays=True,
) as model:
model.validate()

# Wrong dimensions
hdu = fits.ImageHDU(data=np.ones((4,), dtype=np.float64), name="SCI")
Expand All @@ -607,8 +614,10 @@ def test_ndarray_validation(tmp_path):

# Should also be caught by validation
with pytest.raises(ValidationError, match="Wrong number of dimensions: Expected 2, got 1"):
with FitsModel(file_path, strict_validation=True, cast_fits_arrays=False, validate_arrays=True) as model:
model.validate()
# also warn due to the cast_fits_array deprecation
with pytest.warns(DeprecationWarning, match="cast_fits_array"):
with FitsModel(file_path, strict_validation=True, cast_fits_arrays=False, validate_arrays=True) as model:
model.validate()


def test_resave_duplication_bug(tmp_path):
Expand Down

0 comments on commit b7eada9

Please sign in to comment.