From 8154e24427083c04821acc193e050e909ca6755c Mon Sep 17 00:00:00 2001 From: Ned Molter Date: Tue, 22 Oct 2024 11:24:10 -0400 Subject: [PATCH 1/2] JP-3782: Fix improper setting of AREA extension in coron3 pipeline --- jwst/pipeline/calwebb_coron3.py | 9 ++++++++- jwst/pipeline/tests/test_calwebb_coron3.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/jwst/pipeline/calwebb_coron3.py b/jwst/pipeline/calwebb_coron3.py index b35cceecf8..0516d66f59 100644 --- a/jwst/pipeline/calwebb_coron3.py +++ b/jwst/pipeline/calwebb_coron3.py @@ -30,7 +30,14 @@ def to_container(model): 'var_poisson', 'var_rnoise', 'var_flat' ]: try: - setattr(image, attribute, model.getarray_noinit(attribute)[plane]) + arr = model.getarray_noinit(attribute) + if arr.ndim == 3: + setattr(image, attribute, arr[plane]) + elif arr.ndim == 2: + setattr(image, attribute, arr) + else: + msg = f"Unexpected array shape for extension {attribute}: {arr.shape}" + raise ValueError(msg) except AttributeError: pass image.update(model) diff --git a/jwst/pipeline/tests/test_calwebb_coron3.py b/jwst/pipeline/tests/test_calwebb_coron3.py index 3a8c4b34c4..9dc9756fe0 100644 --- a/jwst/pipeline/tests/test_calwebb_coron3.py +++ b/jwst/pipeline/tests/test_calwebb_coron3.py @@ -3,6 +3,7 @@ from stdatamodels.jwst.datamodels import CubeModel from jwst.pipeline.calwebb_coron3 import to_container +import numpy as np # Generate data @@ -22,6 +23,26 @@ def make_container(): ] +def test_to_container(): + """Cover bug where IndexError would be raised when area extension of CubeModel + has shape (x,y) instead of (nints,x,y). In this case area extension should + be copied to each ImageModel in the ModelContainer. + """ + shp = (10,5,5) + cube = CubeModel(shp) + extensions_3d = ['data', 'dq', 'err',] + for extension in extensions_3d: + setattr(cube, extension, np.random.rand(*shp)) + cube.area = np.random.rand(5,5) + + container = to_container(cube) + for i, model in enumerate(container): + for extension in extensions_3d: + assert np.all(getattr(model, extension) == getattr(cube, extension)[i]) + assert hasattr(model, 'area') + assert np.all(model.area == cube.area) + + @pytest.mark.parametrize('cube, container', [(cube, container)]) def test_container_shape(cube, container): """Test container shape""" From 01d3f6718898428c3c4b2d4e6f73330d214b099f Mon Sep 17 00:00:00 2001 From: Ned Molter Date: Tue, 22 Oct 2024 11:32:45 -0400 Subject: [PATCH 2/2] Added changelog entry --- changes/8907.pipeline.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/8907.pipeline.rst diff --git a/changes/8907.pipeline.rst b/changes/8907.pipeline.rst new file mode 100644 index 0000000000..c6d1759f93 --- /dev/null +++ b/changes/8907.pipeline.rst @@ -0,0 +1 @@ +Fixed a bug leading to incorrect area extensions, and sometimes crashes, in the coron3 pipeline