Skip to content

Commit

Permalink
Refactor convert to op (#1176)
Browse files Browse the repository at this point in the history
Closes #299
  • Loading branch information
valer1435 authored Sep 28, 2023
1 parent 92cee16 commit 2ecbc7e
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 147 deletions.
32 changes: 4 additions & 28 deletions fedot/core/operations/evaluation/automl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class H2OAutoMLRegressionStrategy(EvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'h2o_regr': H2OAutoML
}

Expand Down Expand Up @@ -62,12 +62,6 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
out = self._convert_to_output(res, predict_data)
return out

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain H2O AutoML Regression Strategy for {operation_type}')

def _data_transform(self, data: InputData) -> H2OFrame:
if len(data.target.shape) == 1:
concat_data = np.concatenate((data.features, data.target.reshape(-1, 1)), 1)
Expand All @@ -83,7 +77,7 @@ def _get_h2o_connect_config(self):


class H2OAutoMLClassificationStrategy(EvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'h2o_class': H2OAutoML
}

Expand Down Expand Up @@ -129,12 +123,6 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
out = self._convert_to_output(prediction, predict_data)
return out

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain H2O AutoML Classification Strategy for {operation_type}')

def _data_transform(self, data: InputData) -> H2OFrame:
concat_data = np.concatenate((data.features, data.target.reshape(-1, 1)), 1)
frame = H2OFrame(python_obj=concat_data)
Expand All @@ -147,7 +135,7 @@ def _get_h2o_connect_config(self):


class TPOTAutoMLRegressionStrategy(EvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'tpot_regr': TPOTRegressor
}

Expand Down Expand Up @@ -189,15 +177,9 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
out = self._convert_to_output(res, predict_data)
return out

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain H2O AutoML Regression Strategy for {operation_type}')


class TPOTAutoMLClassificationStrategy(EvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'tpot_class': TPOTClassifier
}

Expand Down Expand Up @@ -231,9 +213,3 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
raise ValueError(f'Output model {self.output_mode} is not supported')
out = self._convert_to_output(prediction, predict_data)
return out

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain H2O AutoML Classification Strategy for {operation_type}')
16 changes: 2 additions & 14 deletions fedot/core/operations/evaluation/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:


class FedotClassificationStrategy(EvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'lda': LDAImplementation,
'qda': QDAImplementation,
'svc': FedotSVCImplementation,
Expand Down Expand Up @@ -93,19 +93,13 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
converted = self._convert_to_output(prediction, predict_data)
return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain Fedot Classification Strategy for {operation_type}')


class FedotClassificationPreprocessingStrategy(EvaluationStrategy):
""" Strategy for applying custom algorithms from FEDOT to preprocess data
for classification task
"""

__operations_by_types = {
_operations_by_types = {
'rfe_lin_class': LinearClassFSImplementation,
'rfe_non_lin_class': NonLinearClassFSImplementation,
'class_decompose': DecomposerClassImplementation,
Expand Down Expand Up @@ -153,9 +147,3 @@ def predict_for_fit(self, trained_operation, predict_data: InputData) -> OutputD
prediction = trained_operation.transform_for_fit(predict_data)
converted = self._convert_to_output(prediction, predict_data)
return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain custom classification preprocessing strategy for {operation_type}')
8 changes: 1 addition & 7 deletions fedot/core/operations/evaluation/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class SkLearnClusteringStrategy(SkLearnEvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'kmeans': SklearnKmeans
}

Expand Down Expand Up @@ -44,9 +44,3 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
prediction = trained_operation.predict(predict_data.features)
converted = self._convert_to_output(prediction, predict_data)
return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain SkLearn clustering strategy for {operation_type}')
8 changes: 1 addition & 7 deletions fedot/core/operations/evaluation/common_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FedotPreprocessingStrategy(EvaluationStrategy):
"""

__operations_by_types = {
_operations_by_types = {
'scaling': ScalingImplementation,
'normalization': NormalizationImplementation,
'simple_imputation': ImputationImplementation,
Expand Down Expand Up @@ -95,9 +95,3 @@ def predict_for_fit(self, trained_operation, predict_data: InputData) -> OutputD
prediction = trained_operation.transform_for_fit(predict_data)
converted = self._convert_to_output(prediction, predict_data)
return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain custom preprocessing strategy for {operation_type}')
17 changes: 6 additions & 11 deletions fedot/core/operations/evaluation/evaluation_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class EvaluationStrategy:
def __init__(self, operation_type: str, params: Optional[OperationParameters] = None):
self.params_for_fit = params or OperationParameters()
self.operation_id = operation_type

self.output_mode = False

self.log = default_log(self)
Expand Down Expand Up @@ -97,9 +96,11 @@ def predict_for_fit(self, trained_operation, predict_data: InputData) -> OutputD
"""
return self.predict(trained_operation, predict_data)

@abstractmethod
def _convert_to_operation(self, operation_type: str):
raise NotImplementedError()
if operation_type in self._operations_by_types:
return self._operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain {self.__class__} strategy for {operation_type}')

@property
def implementation_info(self) -> str:
Expand Down Expand Up @@ -170,7 +171,7 @@ class SkLearnEvaluationStrategy(EvaluationStrategy):
params: hyperparameters to fit the operation with
"""

__operations_by_types = {
_operations_by_types = {
'xgbreg': XGBRegressor,
'adareg': AdaBoostRegressor,
'gbr': GradientBoostingRegressor,
Expand Down Expand Up @@ -246,14 +247,8 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
"""
raise NotImplementedError()

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain SKlearn strategy for {operation_type}')

def _find_operation_by_impl(self, impl):
for operation, operation_impl in self.__operations_by_types.items():
for operation, operation_impl in self._operations_by_types.items():
if operation_impl == impl:
return operation

Expand Down
8 changes: 1 addition & 7 deletions fedot/core/operations/evaluation/gpu/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class CumlClusteringStrategy(CuMLEvaluationStrategy):
__operations_by_types = {
_operations_by_types = {
'kmeans': KMeans
}

Expand Down Expand Up @@ -61,9 +61,3 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
converted = self._convert_to_output(prediction, predict_data)

return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain SkLearn clustering strategy for {operation_type}')
10 changes: 2 additions & 8 deletions fedot/core/operations/evaluation/gpu/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CuMLEvaluationStrategy(SkLearnEvaluationStrategy):
:param dict params: hyperparameters to fit the operation with
"""
try:
__operations_by_types = {
_operations_by_types = {
'ridge': Ridge,
'lasso': Lasso,
'logit': LogisticRegression,
Expand All @@ -54,7 +54,7 @@ class CuMLEvaluationStrategy(SkLearnEvaluationStrategy):
}
except NameError:
# if cuML not installed
__operations_by_types = {}
_operations_by_types = {}

def __init__(self, operation_type: str, params: Optional[dict] = None):
super().__init__(operation_type, params)
Expand Down Expand Up @@ -95,9 +95,3 @@ def predict(self, trained_operation, predict_data: InputData) -> OutputData:
:return OutputData: passed data with new predicted target
"""
raise NotImplementedError()

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain cuML strategy for {operation_type}')
18 changes: 2 additions & 16 deletions fedot/core/operations/evaluation/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FedotRegressionPreprocessingStrategy(EvaluationStrategy):
for regression task
"""

__operations_by_types = {
_operations_by_types = {
'ransac_lin_reg': LinearRegRANSACImplementation,
'ransac_non_lin_reg': NonLinearRegRANSACImplementation,
'rfe_lin_reg': LinearRegFSImplementation,
Expand Down Expand Up @@ -87,19 +87,13 @@ def predict_for_fit(self, trained_operation, predict_data: InputData) -> OutputD
converted = self._convert_to_output(prediction, predict_data)
return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain Custom Regression Preprocessing Strategy for {operation_type}')


class FedotRegressionStrategy(EvaluationStrategy):
"""
Strategy for applying custom regression models from FEDOT make predictions
"""

__operations_by_types = {
_operations_by_types = {
'knnreg': FedotKnnRegImplementation
}

Expand All @@ -117,19 +111,11 @@ def fit(self, train_data: InputData):
return operation_implementation

def predict(self, trained_operation, predict_data: InputData) -> OutputData:

prediction = trained_operation.predict(predict_data)
converted = self._convert_to_output(prediction, predict_data)
return converted

def predict_for_fit(self, trained_operation, predict_data: InputData) -> OutputData:

prediction = trained_operation.predict_for_fit(predict_data)
converted = self._convert_to_output(prediction, predict_data)
return converted

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain Fedot Regression Strategy for {operation_type}')
Loading

0 comments on commit 2ecbc7e

Please sign in to comment.