Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make Serializer extendable for dependent projects #969

Merged
merged 20 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions fedot/core/operations/operation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from abc import abstractmethod
from typing import Optional, Union
from typing import Optional, Union, Dict, Any

from fedot.core.data.data import InputData, OutputData
from fedot.core.log import default_log
from fedot.core.operations.hyperparameters_preprocessing import HyperparametersPreprocessor
from fedot.core.operations.operation_parameters import OperationParameters
from fedot.core.repository.operation_types_repository import OperationMetaInfo
from fedot.core.repository.tasks import Task, TaskTypesEnum, compatible_task_types
from fedot.core.utils import DEFAULT_PARAMS_STUB
from fedot.core.serializers.serializer import register_serializable


@register_serializable
class Operation:
"""Base class for operations in nodes. Operations could be machine learning
(or statistical) models or data operations
Expand Down Expand Up @@ -151,6 +152,14 @@ def assign_tabular_column_types(output_data: OutputData, output_mode: str) -> Ou
def __str__(self):
return f'{self.operation_type}'

def to_json(self) -> Dict[str, Any]:
"""Serializes object and ignores unrelevant fields."""
return {
k: v
for k, v in sorted(vars(self).items())
if k not in ['log', 'operations_repo', '_eval_strategy', 'fitted_operation']
}


def _eval_strategy_for_task(operation_type: str, current_task_type: TaskTypesEnum,
operations_repo):
Expand Down
2 changes: 2 additions & 0 deletions fedot/core/pipelines/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from fedot.core.operations.operation import Operation
from fedot.core.optimisers.timer import Timer
from fedot.core.repository.operation_types_repository import OperationTypesRepository
from fedot.core.serializers.serializer import register_serializable
from fedot.core.utils import DEFAULT_PARAMS_STUB


@register_serializable
@dataclass
class NodeMetadata:
"""Dataclass. :class:`Node` metadata
Expand Down
1 change: 1 addition & 0 deletions fedot/core/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .serializer import CLASS_PATH_KEY, INSTANCE_OR_CALLABLE, MODULE_X_NAME_DELIMITER, Serializer
from .any_serialization import any_to_json, any_from_json
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from copy import deepcopy
from inspect import signature
from typing import Any, Dict, Type
from typing import Any, Dict, Type, TypeVar, Callable

from .. import INSTANCE_OR_CALLABLE, Serializer

INSTANCE_OR_CALLABLE = TypeVar('INSTANCE_OR_CALLABLE', object, Callable)


def any_to_json(obj: INSTANCE_OR_CALLABLE) -> Dict[str, Any]:
return {
**{k: v for k, v in sorted(vars(obj).items()) if not _is_log_var(k)},
**Serializer.dump_path_to_obj(obj)
}
return {k: v for k, v in sorted(vars(obj).items()) if not _is_log_var(k)}


def any_from_json(cls: Type[INSTANCE_OR_CALLABLE], json_obj: Dict[str, Any]) -> INSTANCE_OR_CALLABLE:
Expand Down
2 changes: 0 additions & 2 deletions fedot/core/serializers/coders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# flake8: noqa
from .any_serialization import any_from_json, any_to_json
from .enum_serialization import enum_from_json, enum_to_json
from .graph_node_serialization import graph_node_to_json
from .graph_serialization import graph_from_json
from .operation_serialization import operation_to_json
from .opt_history_serialization import opt_history_from_json, opt_history_to_json
from .parent_operator_serialization import parent_operator_from_json, parent_operator_to_json
from .uuid_serialization import uuid_from_json, uuid_to_json
Expand Down
7 changes: 1 addition & 6 deletions fedot/core/serializers/coders/enum_serialization.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from enum import Enum
from typing import Any, Dict, Type

from .. import Serializer


def enum_to_json(obj: Enum) -> Dict[str, Any]:
return {
'value': obj.value,
**Serializer.dump_path_to_obj(obj)
}
return { 'value': obj.value }


def enum_from_json(cls: Type[Enum], json_obj: Dict[str, Any]) -> Enum:
Expand Down
2 changes: 1 addition & 1 deletion fedot/core/serializers/coders/graph_node_serialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict

from fedot.core.dag.linked_graph_node import LinkedGraphNode
from . import any_to_json
from .. import any_to_json


def graph_node_to_json(obj: LinkedGraphNode) -> Dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion fedot/core/serializers/coders/objective_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fedot.core.optimisers.objective import Objective
from fedot.core.optimisers.objective.metrics_objective import MetricsObjective
from fedot.core.serializers.coders import any_from_json
from fedot.core.serializers.any_serialization import any_from_json


def objective_from_json(cls: Type[Objective], json_obj: Dict[str, Any]) -> Objective:
Expand Down
16 changes: 0 additions & 16 deletions fedot/core/serializers/coders/operation_serialization.py

This file was deleted.

2 changes: 1 addition & 1 deletion fedot/core/serializers/coders/opt_history_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fedot.core.optimisers.graph import OptGraph
from fedot.core.optimisers.opt_history_objects.individual import Individual
from fedot.core.optimisers.opt_history_objects.opt_history import OptHistory
from . import any_from_json, any_to_json
from .. import any_from_json, any_to_json

MISSING_INDIVIDUAL_ARGS = {
'metadata': {'MISSING_INDIVIDUAL': 'This individual could not be restored during `OptHistory.load()`'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict, Type

from fedot.core.optimisers.opt_history_objects.parent_operator import ParentOperator
from . import any_from_json, any_to_json
from .. import any_from_json, any_to_json


def parent_operator_to_json(obj: ParentOperator) -> Dict[str, Any]:
Expand Down
7 changes: 1 addition & 6 deletions fedot/core/serializers/coders/uuid_serialization.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from typing import Any, Dict, Type
from uuid import UUID

from .. import Serializer


def uuid_to_json(obj: UUID) -> Dict[str, Any]:
return {
'hex': obj.hex,
**Serializer.dump_path_to_obj(obj)
}
return { 'hex': obj.hex }


def uuid_from_json(cls: Type[UUID], json_obj: Dict[str, Any]) -> UUID:
Expand Down
Loading