Skip to content

Commit

Permalink
Add test for duplicate data when resaving model to fits (#7358)
Browse files Browse the repository at this point in the history
* Add test for duplicate data when resaving model to fits

When resaving a model to fits, asdf loses track of array-hdu
links and saves arrays in both hdu and as internal blocks in the
asdf tree. This can make an asdf tree that is larger than what
can be stored in a fits hdu.

See asdf issue: asdf-format/asdf#1232

This test is skipped as it fails with the current asdf version.

modify imports and use context manager for test_fits

Co-authored-by: James Davies <[email protected]>

use context manager in test_fits

Co-authored-by: James Davies <[email protected]>

improve test docstring in test_fits

* bump asdf version to 2.14.1 to fix hdu data

unskip test for issue #7354

fixes #7354
  • Loading branch information
braingram authored Nov 29, 2022
1 parent 478ced4 commit 983ab73
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion jwst/datamodels/tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import numpy as np
import pytest

from jwst.datamodels import JwstDataModel, RampModel
from jwst import datamodels
from jwst.datamodels import ImageModel, JwstDataModel, RampModel


@pytest.fixture
Expand Down Expand Up @@ -68,3 +69,25 @@ def test_casting():
total = dm.data.sum()
dm.data = dm.data + 2
assert dm.data.sum() > total


def test_resave_duplication_bug(tmp_path):
"""
An issue in asdf (https://github.com/asdf-format/asdf/issues/1232)
resulted in duplication of data when a model was read from and then
written to a fits file. A resave results in array data being written to
both hdus and as internal blocks within the asdf tree (which is then
stored in the asdf hdu).
"""
fn1 = tmp_path / "test1.fits"
fn2 = tmp_path / "test2.fits"

arr = np.zeros((1000, 100), dtype='f4')
m = ImageModel(arr)
m.save(fn1)

with datamodels.open(fn1) as m2:
m2.save(fn2)

with fits.open(fn1) as ff1, fits.open(fn2) as ff2:
assert ff1['ASDF'].size == ff2['ASDF'].size
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ python_requires = >=3.8
setup_requires =
setuptools_scm
install_requires =
asdf>=2.13.0
asdf>=2.14.1
asdf-transform-schemas>=0.3.0
astropy>=5.1
BayesicFitting>=3.0.1
Expand Down

0 comments on commit 983ab73

Please sign in to comment.