Source code for pycif.plugins.obsoperators.standard.transforms.init_mainpipe
import itertools
import copy
from .utils import add_default
from .....utils.classes.transforms import Transform
[docs]
def init_mainpipe(self, all_transforms,
backup_comps, mapper):
"""Initialize the core of the transform_pipe depending on the list of
transformations specified in obsoper.transform_pipe.
.. warning::
If no transform_pipe is specified, the CTM model specified in the Yaml is run by
default.
On the opposite, if ``transform_pipe`` is specified in the observation operator,
only transforms explicitly specified will be used.
Thus, if custom transforms need to be run on top of the model, one should not
forget to include the transform ``run_model`` in the ``transform_pipe``.
Another option (recommended for most applications) is to use the ``controlvect``
and ``obsvect`` transform_pipes to define transforms related to the control
vector and to the observation vector respectively.
"""
# Initializes the overall transform pipe
# If no pipe is specified, assume that the model should be run
if not hasattr(self, "transform_pipe"):
if not self.ignore_model:
yml_dict = {
"run_model": {
"plugin": {
"name": "run_model",
"version": "std",
"type": "transform",
}
}
}
else:
yml_dict = {}
self.transform_pipe = Transform.load_from_dict(yml_dict)
main_transforms = self.transform_pipe
transfs_ids = copy.deepcopy(getattr(main_transforms, "attributes", []))
# Loops backwards on available transformations and update inputs/out
# According to model outputs
ref_index = 0
if self.mainpipe != []:
ref_index = all_transforms.attributes.index(self.mainpipe[0])
for transform in transfs_ids[::-1]:
# Initializes mapper if not already done
if transform in mapper:
continue
# Replacing the transform by a transform class
# if not already initialized
transf = getattr(main_transforms, transform)
if transf is None:
transf = Transform.from_dict({}, orig_name=transform)
yml_dict = transf.to_dict(transf, full_output=True)
_, new_id = add_default.add_default(
self,
all_transforms,
yml_dict,
position="index",
index=ref_index,
mapper=mapper,
init=True,
backup_comps=backup_comps,
transform_id=transform
)
ref_index = all_transforms.attributes.index(new_id)
self.mainpipe.insert(0, new_id)
# # Keep in memory the transformations in the main pipe
# all_transforms.mainpipe = \
# [tr for tr in all_transforms.attributes
# if tr not in all_transforms.obsvectpipe]