Skip to content

Change ModelBlock signature to avoid passing config dict around #222

@lucianopaz

Description

@lucianopaz

At the moment, the main methods of every ModelBlock that are called by the enclosing Model instance take no positional arguments and expect to receive data and config keyword arguments (preprocess, make_latent_rvs and make_observations). This makes documenting the potential key, value and defaults for the configuration dictionary cumbersome and difficult.

We could change the signature of the ModelBlock methods to take 1 positional argument (data) and then explicitly put all keys of the config as optional keyword arguments.

Example

The old signature:

class WithinBlockGP(ModelBlock):
    ...
    make_latent_rvs(
        self, *, data: Optional[DataFrame] = None, config: Optional[Dict[str, Any]] = None
    ) -> TensorVariable:
        conf = self.read_config(config)
        return self.make_within_block_component(data, **conf)
    ...

Proposed new signature

class WithinBlockGP(ModelBlock):
    ...
    make_latent_rvs(
        self,
        data: DataFrame,
        *,
        nested_hierarchies: Optional[Sequence[str]] = None,
        name_prefix: str = "gp",
        level_scale_kwargs: Optional[Dict[str, Any]] = None,
        level_kernel_kwargs: Optional[Dict[int, Dict[str, Any]]] = None,
        parametrization: ParametrizationType = None,
        parametrized_by_level: bool = False,
        coef_dims: Optional[Sequence[str]] = None,
        time_column: Optional[str] = None,
        model: Optional[Model] = None,
        **kwargs,
    ) -> TensorVariable:
        # Do whatever `make_within_block_component` used to do
    ...

Comparison

Pros of the new signature:

  • Clear potential configuration arguments
  • Clear default values
  • Easier to write docstrings for

Cons of the new signature:

  • Different method signatures between parent and child classes
  • Model needs to unpack the configuration values and feed them to the corresponding ModelBlock

In my opinion the pros greatly outweigh the cons.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions