Skip to content

Function to transform etna objects to dict #818

Closed
martins0n opened this issue Jul 26, 2022 · 0 comments · Fixed by #834
Closed

Function to transform etna objects to dict #818

martins0n opened this issue Jul 26, 2022 · 0 comments · Fixed by #834
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@martins0n
Copy link
Contributor

martins0n commented Jul 26, 2022

🚀 Feature Request

We need function to serialize and deserialize etna objects to dict for experiments, cli and platform.
For now we can generate etna objects with hydra_slayer.get_from_params but we can't reverse that transformation.

Proposal

  • Add to_dict method to BaseMixin, which will save etna object in hydra-core, hydra-slayer format.
  • In some cases for example ChangePointsTrendTransform we have external objects as input parameters:
    • For sklearn BaseEstimator we can get parameters with BaseEstimator.get_parameters
    • For other cases: we should leave knowledge of _target_ class only and raise warning with the name of field.
  • For inspiration you can look at __repr__ implementation or at to_json:
    def to_json(self):
        """Library objects to json representation.

        Returns
        -------
        Dict with object allenai-common representation.
        """
        class_to_name = {}
        self.reversed_index(Registrable._registry, class_to_name)
        json_config = {}
        init_args = inspect.signature(self.__init__).parameters
        json_config["type"] = class_to_name[self.__class__]
        for arg, param in init_args.items():
            if param.kind == param.VAR_POSITIONAL:
                continue
            value = self.__dict__[arg]
            if param.kind == param.VAR_KEYWORD:
                json_config.update(value)
            elif getattr(value, "to_json", None) is not None:
                json_config[arg] = value.to_json()
            elif value.__class__ in class_to_name:
                json_config[arg] = {"type": class_to_name[value.__class__]}
            elif isinstance(value, tuple) or isinstance(value, list):
                json_config[arg] = [item if not isinstance(item, BaseMixin) else item.to_json() for item in value]
            else:
                json_config[arg] = value
        return json_config

Test cases

  • Pipeline, model, metrics, transforms (choose some not trivial cases: ChangePointsTrendTransform for example) with to_dict and hydra_slayer.get_from_params initialize the same object
  • check that ensembles are working with to_dict correctly
  • check that warning is arised in case of non etna-sklearn models.

Additional context

Example of Pipeline:

_target_: etna.pipeline.Pipeline
horizon: 14
model:
  _target_: etna.models.CatBoostPerSegmentModel
transforms:
- _target_: etna.transforms.TimeSeriesImputerTransform
  in_column: target
  strategy: mean
- _target_: etna.transforms.AddConstTransform
  in_column: target
  value: 100
- _target_: etna.transforms.LogTransform
  in_column: target
- _target_: etna.transforms.LagTransform
  in_column: target
  lags: ${shift:${pipeline.horizon},[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
    17, 18, 19, 20]}
- _target_: etna.transforms.DateFlagsTransform
  day_number_in_month: true
  day_number_in_week: true
  is_weekend: true
  month_number_in_year: true
  week_number_in_month: true
  week_number_in_year: true
  year_number: true
@martins0n martins0n added the enhancement New feature or request label Jul 26, 2022
@martins0n martins0n added this to the AutoML milestone Jul 26, 2022
@martins0n martins0n moved this to Specification in etna board Jul 26, 2022
@martins0n martins0n moved this from Specification to Todo in etna board Jul 28, 2022
@scanhex12 scanhex12 self-assigned this Aug 2, 2022
@alex-hse-repository alex-hse-repository moved this from Todo to In Progress in etna board Aug 3, 2022
@martins0n martins0n moved this from In Progress to In Review in etna board Aug 4, 2022
Repository owner moved this from In Review to Done in etna board Aug 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants