Skip to content

Commit

Permalink
add _model_to_exptype method to AbstractModelLibrary (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram authored Oct 23, 2024
2 parents 03aa64f + 558c952 commit 799be65
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
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

0 comments on commit 799be65

Please sign in to comment.