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

Calling datamodels.open on a SlitModel clears wht #296

Open
braingram opened this issue Mar 29, 2024 · 3 comments
Open

Calling datamodels.open on a SlitModel clears wht #296

braingram opened this issue Mar 29, 2024 · 3 comments

Comments

@braingram
Copy link
Collaborator

Minimal example:

import numpy as np
import stdatamodels.jwst.datamodels as dm

m0 = dm.SlitModel((27, 1299))
m0.wht[:] = 1

m1 = dm.open(m0)
np.testing.assert_equal(m1.wht, 0)
@braingram
Copy link
Collaborator Author

@nden @perrygreenfield This is the bug I mentioned.

@braingram
Copy link
Collaborator Author

There appears to be other issues with dm.open on a model. Here's an example with ImageModel:

import numpy as np
import stdatamodels.jwst.datamodels as dm

m0 = dm.ImageModel((256, 256))

np.testing.assert_allclose(m0.data, 0.0)  # data starts as all 0s

m1 = dm.open(m0)  # what should we expect this to do?

assert m1 is not m0   # the models are not the same
assert m1.data is m0.data  # however both use the same data array
m1.data[:] = 42  # modifying m1.data now modifies m0.data
np.testing.assert_allclose(m0.data, 42.0)

Passing a model to open is undocumented:
https://stdatamodels.readthedocs.io/en/latest/api/stdatamodels.jwst.datamodels.open.html#stdatamodels.jwst.datamodels.open
but is supported and the commented line says this should produce a "copy"

elif isinstance(init, model_base.JwstDataModel):
# Copy the object so it knows not to close here
return init.__class__(init, **kwargs)

Passing a model to open is commonly used in jwst with many steps starting with a call to open(input).

@braingram
Copy link
Collaborator Author

A similar "shared" issue can be seen with the metadata and can persist across model type changes. See the following:

img = dm.ImageModel((256, 256))
assert img.meta.observation.visit_number is None

clone = dm.open(img)
assert clone.meta.observation.visit_number is None
clone.meta.observation.visit_number = '42'

# changing `visit_number` in clone also modified img
assert img.meta.observation.visit_number == '42'

# we can convert img to a CubeModel by doing the following
cube = dm.CubeModel(img)
cube.meta.observation.visit_number = '26'

assert img.meta.observation.visit_number == '26'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant