From a7ee2fca959fe991b0a689405e16ab5c81ef3508 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 23 Oct 2024 14:18:52 -0400 Subject: [PATCH 1/3] add hook to override exptype --- src/stpipe/library.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/stpipe/library.py b/src/stpipe/library.py index 4011a44a..65ea804e 100644 --- a/src/stpipe/library.py +++ b/src/stpipe/library.py @@ -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 @@ -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) @@ -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`` @@ -781,6 +781,9 @@ def _to_group_id(self, model_or_filename, index): except NoGroupID: return f"exposure{index + 1:04d}" + def _model_to_exptype(self, model): + return getattr(model.meta, "exptype", "SCIENCE") + @property @abc.abstractmethod def crds_observatory(self): From 8aed87844bb50c742395919618ea33004b76914c Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 23 Oct 2024 14:39:34 -0400 Subject: [PATCH 2/3] add changelog fragment --- changes/201.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/201.bugfix.rst diff --git a/changes/201.bugfix.rst b/changes/201.bugfix.rst new file mode 100644 index 00000000..bc7efd90 --- /dev/null +++ b/changes/201.bugfix.rst @@ -0,0 +1 @@ +Add hook to allow ModelLibrary subclasses to override exptype. From 558c952b08eeb10680dfd6885036bfd7a0a74aee Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 23 Oct 2024 14:43:54 -0400 Subject: [PATCH 3/3] update docs --- docs/source/model_library.rst | 1 + src/stpipe/library.py | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/docs/source/model_library.rst b/docs/source/model_library.rst index 66d72e18..c065b74c 100644 --- a/docs/source/model_library.rst +++ b/docs/source/model_library.rst @@ -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. diff --git a/src/stpipe/library.py b/src/stpipe/library.py index 65ea804e..79f295af 100644 --- a/src/stpipe/library.py +++ b/src/stpipe/library.py @@ -782,6 +782,25 @@ def _to_group_id(self, model_or_filename, index): 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 @@ -854,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").