-
Notifications
You must be signed in to change notification settings - Fork 26
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
datamodels doesn't validate arrays in some cases #307
Comments
I understand (well, maybe) why one would want different models for 2-D and 3-D arrays, but your example brought to mind a possible snag. We often reshape an array to add an axis of length 1, for the purpose of broadcasting with another array. Explicitly specifying where the axis of length 1 falls within the shape may be necessary if it isn't the last axis; the irs2 code in refpix (translated from IDL) does this a lot. If an array was reshaped without first making a copy, and a validation check was done while a (10, 10) array temporarily had shape (1, 10, 10), that would not be helpful. |
I think as long as you're not assigning it, it should be fine:
|
The following fails as expected, too:
That is, the problem does not occur when using an array to instantiate the model, only when the model attempts to create its own default array from an input shape. In the latter case, there is no check to ensure |
Nice catch! The other case where it doesn't validate is when one instantiates a 2D model from a 3D model. One sometimes does this in pipelines to have a shortcut for pulling all the metadata from one model to the other, if supported. And the data too, if supported by the schemas. In [1]: from jwst import datamodels
In [2]: import numpy as np
In [6]: cube = datamodels.CubeModel((2, 3, 4))
In [7]: cube.data
Out[7]:
array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]], dtype=float32)
In [8]: cube.data.shape
Out[8]: (2, 3, 4)
In [9]: image = datamodels.ImageModel(cube)
In [10]: image.data.shape
Out[10]: (2, 3, 4) |
Gotcha, ok I will see if that is easily fixable as well. But if it's actively used in the pipelines, it's potentially more challenging to fix non-disruptively |
#395 attempts to resolve the initial issue, where the dimensions are mismatched when instantiating from a shape. I think it makes sense for that to be its own PR. I'll investigate the other case and leave this issue open until we decide what to do about it |
Sounds good. I think both are actively used in the pipelines, so it would be good to check that these fixes to |
This is only half-complete. The unexpected behavior where initializing an |
jwst.datamodels
validates array sizes on reading from a FITS file or on assignment. SoImageModel
requires the data array to be 2D, and so if I try to read or assign a 3D array, it fails validation:That's good.
But if I instantiate an
ImageModel
with a 3D array or from an already existing datamodel with a 3D array, it doesn't do any validation and allows it.This is incorrect. It should be giving the same validation error as above.
The text was updated successfully, but these errors were encountered: