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

Force array validation when initializing a datamodel from another datamodel of different type #403

Merged
merged 2 commits into from
Feb 18, 2025
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
1 change: 1 addition & 0 deletions changes/403.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Force array validation when initializing a datamodel from another datamodel of different type
9 changes: 9 additions & 0 deletions src/stdatamodels/jwst/datamodels/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
IFUImageModel,
ABVegaOffsetModel,
Level1bModel,
CubeModel,
)
from stdatamodels.jwst import datamodels
from stdatamodels.jwst.datamodels import _defined_models as defined_models
from stdatamodels.schema import walk_schema
from stdatamodels.validate import ValidationWarning

ROOT_DIR = Path(__file__).parent / "data"
FITS_FILE = ROOT_DIR / "test.fits"
Expand Down Expand Up @@ -314,6 +316,13 @@ def test_ifuimage():
assert type(im) is ImageModel


def test_init_incompatible_model():
data = np.arange(24).reshape((6, 4))
im = ImageModel(data=data, err=data / 2, dq=data)
with pytest.raises(ValidationWarning):
CubeModel(im)


def test_abvega_offset_model():
path = ROOT_DIR / "nircam_abvega_offset.asdf"
with ABVegaOffsetModel(path) as model:
Expand Down
3 changes: 3 additions & 0 deletions src/stdatamodels/model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ def __init__(
asdffile = None
self.clone(self, init)
if not isinstance(init, self.__class__):
current_validate_arrays = self._validate_arrays
self._validate_arrays = True
self.validate()
self._validate_arrays = current_validate_arrays
return

elif isinstance(init, AsdfFile):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from stdatamodels import DataModel

from models import BasicModel, AnyOfModel, TableModel, TransformModel
from models import BasicModel, AnyOfModel, TableModel, TransformModel, FitsModel
from stdatamodels.validate import ValidationWarning


def test_init_from_pathlib(tmp_path):
Expand Down Expand Up @@ -116,6 +117,15 @@ def test_init_invalid_shape2():
BasicModel((50, 50), schema=schema)


def test_init_incompatible_datamodel():
"""Initialized a model with a different model type that has invalid dimensions"""
input_model = FitsModel((50, 50))
schema = input_model._schema
schema["properties"]["data"]["ndim"] = 3
with pytest.raises(ValidationWarning):
BasicModel(input_model, schema=schema)


def test_set_array():
with pytest.raises(ValueError):
with BasicModel() as dm:
Expand Down