Skip to content

add _model_to_exptype method to AbstractModelLibrary #201

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

Merged
merged 3 commits into from
Oct 23, 2024
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/201.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add hook to allow ModelLibrary subclasses to override exptype.
1 change: 1 addition & 0 deletions docs/source/model_library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ Several methods are abstract and will need implementations:
It's likely that a few other methods might require overriding:

- ``_model_to_filename``
- ``_model_to_exptype``
- ``_assign_member_to_model``

Consult the docstrings (and base implementations) for more details.
Expand Down
40 changes: 32 additions & 8 deletions src/stpipe/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __init__(
else:
model = model_or_filename

exptype = getattr(model.meta, "exptype", "SCIENCE")
exptype = self._model_to_exptype(model)

if asn_exptypes is not None and exptype.lower() not in asn_exptypes:
continue
Expand Down Expand Up @@ -612,8 +612,8 @@ def _save(self, path, **kwargs):
members.append(
{
"expname": str(mfn),
"exptype": model.meta.exptype,
"group_id": model.meta.group_id,
"exptype": self._model_to_exptype(model),
"group_id": self._model_to_group_id(model),
}
)
self.shelve(model, i, modify=False)
Expand All @@ -626,7 +626,7 @@ def _save(self, path, **kwargs):
def get_crds_parameters(self):
"""
Get the "crds_parameters" from either:
- the first "science" member (based on model.meta.exptype)
- the first "science" member (based on exptype)
- the first model (if no "science" member is found)

If no "science" members are found in the library a ``UserWarning``
Expand Down Expand Up @@ -781,6 +781,28 @@ def _to_group_id(self, model_or_filename, index):
except NoGroupID:
return f"exposure{index + 1:04d}"

def _model_to_exptype(self, model):
"""
Compute "exptype" from a model using the DataModel interface.

This will be called for every model in the library:
- when the library is created from a list of models
- when _save is called
In both cases the models are all in memory and this method
can use the in memory DataModel to determine the "exptype"
(likely ``model.meta.exptype``).

Parameters
----------
model : DataModel

Returns
-------
exptype : str
Exposure type (for example "SCIENCE").
"""
return getattr(model.meta, "exptype", "SCIENCE")

@property
@abc.abstractmethod
def crds_observatory(self):
Expand Down Expand Up @@ -851,10 +873,12 @@ def _model_to_group_id(self, model):
"""
Compute a "group_id" from a model using the DataModel interface.

This will be called for every model in the library ONLY when
the library is created from a list of models. In this case the
models are all in memory and this method can use the in memory
DataModel to determine the "group_id" (likely `model.meta.group_id`).
This will be called for every model in the library:
- when the library is created from a list of models
- when _save is called
In both cases the models are all in memory and this method
can use the in memory DataModel to determine the "group_id"
(likely ``model.meta.group_id``).

If no "group_id" can be determined `NoGroupID` should be
raised (to allow the library to assign a unique "group_id").
Expand Down