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

Add default constructor to datamodels and deprecate the maker_utils #436

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from roman_datamodels.datamodels import MODEL_REGISTRY as _MODEL_REGISTRY # Hide from public API

from ._basic_meta import * # noqa: F403
from ._common_meta import * # noqa: F403
from ._datamodels import * # noqa: F403
Expand All @@ -19,9 +17,6 @@
"WfiMosaic": "mk_level3_mosaic",
}

# This is static at runtime, so we might as well compute it once
NODE_REGISTRY = {mdl: node for node, mdl in _MODEL_REGISTRY.items()}


def _camel_case_to_snake_case(value):
"""
Expand Down Expand Up @@ -99,4 +94,4 @@ def mk_datamodel(model_class, **kwargs):
`roman_datamodels.datamodels.Datamodel`
"""

return model_class(mk_node(NODE_REGISTRY[model_class], **kwargs))
return model_class.create_default(**kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def mk_associations(*, shape=(2, 3, 1), filepath=None, **kwargs):

Returns
-------
roman_datamodels.stnode.AssociationsModel
roman_datamodels.stnode.Associations
"""

associations = stnode.Associations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def mk_epsf(*, shape=(3, 6, 9, 361, 361), filepath=None, **kwargs):
----------
shape
(optional, keyword-only) Shape of arrays in the model.
If shape is greater than 1D, the first dimension is used.
If shape is greater than 5D, the first 5 dimensions are used.

filepath
(optional, keyword-only) File name and path to write model to.
Expand Down Expand Up @@ -376,7 +376,7 @@ def mk_inverselinearity(*, shape=(2, 4096, 4096), filepath=None, **kwargs):

Returns
-------
roman_datamodels.stnode.InverseLinearityRef
roman_datamodels.stnode.InverselinearityRef
"""
if len(shape) != 3:
shape = (2, 4096, 4096)
Expand Down
19 changes: 18 additions & 1 deletion src/roman_datamodels/datamodels/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,28 @@

crds_observatory = "roman"

@abc.abstractproperty
@property
@abc.abstractmethod
def _node_type(self):
"""Define the top-level node type for this model"""
pass

@classmethod
def create_default(cls, *, filepath=None, **kwargs):
"""
Create a default instance of this model using the maker_utils
"""
from roman_datamodels._maker_utils import mk_node

# don't use the maker util save, instead save through
# the datamodel itself
default = cls(mk_node(cls._node_type, filepath=None, **kwargs))

if filepath is not None:
default.save(filepath)

Check warning on line 98 in src/roman_datamodels/datamodels/_core.py

View check run for this annotation

Codecov / codecov/patch

src/roman_datamodels/datamodels/_core.py#L98

Added line #L98 was not covered by tests

return default

def __init_subclass__(cls, **kwargs):
"""Register each subclass in the MODEL_REGISTRY"""
super().__init_subclass__(**kwargs)
Expand Down
Loading
Loading