Skip to content

Improve FactorAdaptor in the submodel context #199

Open

Description

@tyralla

While working on #188, I need to adjust the example explaining how to use FactorAdaptor. This is not possible without changing the implementation of FactorAdaptor, because it currently only works if the reference and the target parameter are members of the same model instance. However, in the current state of snow submodularisation, GLMelt is still a member of HydPy-H, but CFMax has been moved from HydPy-H to HydPy-Snow (and renamed DegreeDayFactor).

I will introduce a quick change that allows using a reference parameter from another (sub)model handled by the same element:

    def __call__(self, target: parametertools.Parameter) -> None:
        model = target.subpars.pars.model.element.model
        controls = []
        for submodel in model.find_submodels(include_mainmodel=True).values():
            control = submodel.parameters.control
            if hasattr(control, self._reference):
                controls.append(control)
        assert len(controls) == 1  # ToDo: Support to select the desired reference
                                   #       parameter more explicitly (see Issue #199).
        ref = controls[0][self._reference]
        if self._mask:
            mask = target.get_submask(self._mask)
            values = ref.values[mask] if ref.NDIM else ref.value
            target.values[mask] = self._rule.value * values
        else:
            target.value = self._rule.value * ref.value

Note that after this change, the mask comes from the target parameter instead of the reference parameter. I think this is an improvement, but using a mask from the reference parameter might be preferable in some contexts. See the related discussion in #72.

I hope this quick improvement is enough for this (less commonly used feature) of the calibtools module. However, we should check later if related features show similar limitations and address the left ToDo remark.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions