Skip to content

Commit

Permalink
Fix inplace modification of data during data definition (resolves #943)…
Browse files Browse the repository at this point in the history
… (#950)

* some name/move refactorings

* Fix inplace modification of input data during data define

* fixup! some name/move refactorings
  • Loading branch information
gkirgizov authored Oct 20, 2022
1 parent c66d718 commit 3381f4b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
7 changes: 3 additions & 4 deletions fedot/api/api_utils/api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from typing import Optional

import numpy as np
import pandas as pd

from fedot.api.api_utils.data_definition import data_strategy_selector
from fedot.api.api_utils.data_definition import data_strategy_selector, FeaturesType, TargetType
from fedot.core.data.data import InputData, OutputData, data_type_is_table
from fedot.core.data.data_preprocessing import convert_into_column
from fedot.core.data.multi_modal import MultiModalData
Expand Down Expand Up @@ -35,8 +34,8 @@ def __init__(self, task: Task):
'label_encoded': self.preprocessor.label_encoding_for_fit}

def define_data(self,
features: Union[str, np.ndarray, pd.DataFrame, InputData, dict],
target: Union[str, np.ndarray, pd.Series] = None,
features: FeaturesType,
target: Optional[TargetType] = None,
is_predict=False):
""" Prepare data for FEDOT pipeline composing.
Obligatory preprocessing steps are applying also. If features is dictionary
Expand Down
37 changes: 21 additions & 16 deletions fedot/api/api_utils/data_definition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from copy import deepcopy
from typing import Union
from typing import Union, Optional

import numpy as np
import pandas as pd
Expand All @@ -10,6 +10,9 @@
from fedot.core.repository.dataset_types import DataTypesEnum
from fedot.core.repository.tasks import Task, TaskTypesEnum

FeaturesType = Union[str, np.ndarray, pd.DataFrame, InputData, dict, tuple]
TargetType = Union[str, np.ndarray, pd.Series, dict]


class DataDefiner:

Expand All @@ -24,9 +27,9 @@ def strategy(self):
def strategy(self, strategy) -> None:
self._strategy = strategy

def define_data(self, features: Union[tuple, str, np.ndarray, pd.DataFrame, InputData],
def define_data(self, features: FeaturesType,
ml_task: Task,
target: str = None,
target: Optional[str] = None,
is_predict: bool = False) -> None:
return self._strategy.define_data(features,
ml_task,
Expand Down Expand Up @@ -77,7 +80,7 @@ def define_data(self, features: pd.DataFrame,

if isinstance(target, str) and target in features.columns:
target_array = features[target]
del features[target]
features = features.drop(columns=[target])
else:
target_array = target

Expand All @@ -90,15 +93,15 @@ def define_data(self, features: pd.DataFrame,
class NumpyStrategy(StrategyDefineData):
def define_data(self, features: np.ndarray,
ml_task: Task,
target: str = None,
target: Optional[int] = None,
is_predict: bool = False) -> InputData:
# numpy format for input data
if target is None:
target = np.array([])

if isinstance(target, str):
if isinstance(target, int):
target_array = features[target]
del features[target]
features = np.delete(features, target, axis=1)
else:
target_array = target

Expand Down Expand Up @@ -163,13 +166,15 @@ def define_data(self, features: dict,

def data_strategy_selector(features, target, ml_task: Task = None, is_predict: bool = None):
data_type = type(features)
strategy_dict = {InputData: FedotStrategy(),
MultiModalData: FedotStrategy(),
tuple: TupleStrategy(),
pd.DataFrame: PandasStrategy(),
np.ndarray: NumpyStrategy(),
str: CsvStrategy(),
dict: MultimodalStrategy()}

data = DataDefiner(strategy_dict[data_type])

data = DataDefiner(_strategy_dispatch[data_type]())
return data.define_data(features, ml_task, target, is_predict)


_strategy_dispatch = {InputData: FedotStrategy,
MultiModalData: FedotStrategy,
tuple: TupleStrategy,
pd.DataFrame: PandasStrategy,
np.ndarray: NumpyStrategy,
str: CsvStrategy,
dict: MultimodalStrategy}
6 changes: 2 additions & 4 deletions fedot/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from fedot.api.api_utils.api_composer import ApiComposer
from fedot.api.api_utils.api_data import ApiDataProcessor
from fedot.api.api_utils.data_definition import FeaturesType, TargetType
from fedot.api.api_utils.api_data_analyser import DataAnalyser
from fedot.api.api_utils.metrics import ApiMetrics
from fedot.api.api_utils.params import ApiParams
Expand All @@ -30,9 +31,6 @@

NOT_FITTED_ERR_MSG = 'Model not fitted yet'

FeaturesType = Union[str, np.ndarray, pd.DataFrame, InputData, dict]
TargetType = Union[str, np.ndarray, pd.Series, dict]


class Fedot:
"""Main class for FEDOT API.
Expand Down Expand Up @@ -142,7 +140,7 @@ def __init__(self,
self.history: Optional[OptHistory] = None

def fit(self,
features: Union[str, np.ndarray, pd.DataFrame, InputData, dict],
features: FeaturesType,
target: TargetType = 'target',
predefined_model: Union[str, Pipeline] = None) -> Pipeline:
"""Fits the graph with a predefined structure or compose and fit the new graph
Expand Down

0 comments on commit 3381f4b

Please sign in to comment.