From 983ab7392b6f9ebbcbada688ee9cadb409afc577 Mon Sep 17 00:00:00 2001 From: Brett Graham Date: Tue, 29 Nov 2022 13:43:54 -0500 Subject: [PATCH] Add test for duplicate data when resaving model to fits (#7358) * 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: https://github.com/asdf-format/asdf/issues/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 use context manager in test_fits Co-authored-by: James Davies improve test docstring in test_fits * bump asdf version to 2.14.1 to fix hdu data unskip test for issue #7354 fixes #7354 --- jwst/datamodels/tests/test_fits.py | 25 ++++++++++++++++++++++++- setup.cfg | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/jwst/datamodels/tests/test_fits.py b/jwst/datamodels/tests/test_fits.py index 2dd07fbea4..cb70e45dd4 100644 --- a/jwst/datamodels/tests/test_fits.py +++ b/jwst/datamodels/tests/test_fits.py @@ -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 @@ -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 diff --git a/setup.cfg b/setup.cfg index 759381c69a..0bed9a5466 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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