-
Notifications
You must be signed in to change notification settings - Fork 83
Function to transform etna objects to dict
#818
Copy link
Copy link
Closed
Description
🚀 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 toBaseMixin
, 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 sklearn BaseEstimator we can get parameters with
- For inspiration you can look at
__repr__
implementation or atto_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
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Type
Projects
Status
Done