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

Investigate using a singleton in place of None to trigger _make_default #310

Open
braingram opened this issue Jul 24, 2024 · 0 comments
Open

Comments

@braingram
Copy link
Collaborator

Currently setting a DataModel attribute to None (which is a common pattern for freeing up memory) results in creation of a default value for the attribute:

def __setattr__(self, attr, val):
if attr.startswith('_'):
self.__dict__[attr] = val
else:
schema = _get_schema_for_property(self._schema, attr)
if val is None:
val = _make_default(attr, schema, self._ctx)

For large arrays (which are likely candidates for removal to free up memory) this results in creation of a large new array with default values.

>>> import stdatamodels.jwst.datamodels as dm
>>> m = dm.ImageModel((1000, 1000))
>>> d0 = m._instance['data']  # avoid the getattr for data
>>> d0.shape
(1000, 1000)
>>> m.data = None
>>> d0 is m._instance['data'] 
False
>>> m._instance['data'].shape
(1000, 1000)

It may be helpful to use a singleton like MAKE_DEFAULT to signify that a default should be made.

>>> import stdatamodels
>>> import stdatamodels.jwst.datamodels as dm
>>> m = dm.ImageModel((1000, 1000))
>>> m.data = stdatamodels.MAKE_DEFAULT  # to trigger a new default for data

This could help to make it more explicit when a default is requested. However it would require changes to code like the following:

self.data = init.data
self.dq = init.dq
self.err = init.err
self.area = init.area

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