-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1211-fix #1213
Merged
Merged
1211-fix #1213
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
53a76b8
fix
kasyanovse 1950859
pep8 and small fix
kasyanovse 824fa69
pep8
kasyanovse 4cae937
remove old function
kasyanovse b25b18f
fix typing hints
kasyanovse 6d0784c
fix test
kasyanovse cbeb7b7
fix extract_metadata_from_pipeline
kasyanovse 0d757dc
fix atomized model type hint
kasyanovse 63c98b7
fix test
kasyanovse 27a0b52
add docstring and replace lambda with operators
kasyanovse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from copy import deepcopy | ||
from collections import Counter | ||
from datetime import timedelta | ||
from typing import Callable, Union, Optional | ||
from functools import reduce | ||
from itertools import chain | ||
from typing import Callable, Union, Optional, Set, List, Any, Dict | ||
|
||
from fedot.core.pipelines.node import PipelineNode | ||
from golem.core.tuning.simultaneous import SimultaneousTuner | ||
|
||
from fedot.core.data.data import InputData, OutputData | ||
|
@@ -11,7 +14,6 @@ | |
from fedot.core.pipelines.tuning.tuner_builder import TunerBuilder | ||
from fedot.core.repository.operation_types_repository import OperationMetaInfo, \ | ||
atomized_model_type | ||
from fedot.core.utils import make_pipeline_generator | ||
|
||
|
||
class AtomizedModel(Operation): | ||
|
@@ -23,32 +25,35 @@ def __init__(self, pipeline: 'Pipeline'): | |
|
||
super().__init__(operation_type=atomized_model_type()) | ||
self.pipeline = pipeline | ||
self.unique_id = self.pipeline.root_node.descriptive_id | ||
|
||
def fit(self, params: Optional[Union[OperationParameters, dict]], data: InputData): | ||
|
||
copied_input_data = deepcopy(data) | ||
predicted_train = self.pipeline.fit(input_data=copied_input_data) | ||
def fit(self, params: Optional[Union[OperationParameters, dict]], data: InputData) -> ('Pipeline', OutputData): | ||
predicted_train = self.pipeline.fit(input_data=data) | ||
fitted_atomized_operation = self.pipeline | ||
|
||
return fitted_atomized_operation, predicted_train | ||
|
||
def predict(self, fitted_operation, data: InputData, | ||
params: Optional[Union[OperationParameters, dict]] = None, output_mode: str = 'default'): | ||
def predict(self, | ||
fitted_operation: 'Pipeline', | ||
data: InputData, | ||
params: Optional[Union[OperationParameters, Dict[str, Any]]] = None, | ||
output_mode: str = 'default') -> OutputData: | ||
|
||
# Preprocessing applied | ||
copied_input_data = deepcopy(data) | ||
prediction = fitted_operation.predict(input_data=copied_input_data, output_mode=output_mode) | ||
prediction = fitted_operation.predict(input_data=data, output_mode=output_mode) | ||
prediction = self.assign_tabular_column_types(prediction, output_mode) | ||
return prediction | ||
|
||
def predict_for_fit(self, fitted_operation, data: InputData, params: Optional[OperationParameters] = None, | ||
output_mode: str = 'default'): | ||
def predict_for_fit(self, | ||
fitted_operation: 'Pipeline', | ||
data: InputData, | ||
params: Optional[OperationParameters] = None, | ||
output_mode: str = 'default') -> OutputData: | ||
return self.predict(fitted_operation, data, params, output_mode) | ||
|
||
def fine_tune(self, metric_function: Callable, | ||
input_data: InputData = None, iterations: int = 50, | ||
timeout: int = 5): | ||
def fine_tune(self, | ||
metric_function: Callable, | ||
input_data: Optional[InputData] = None, | ||
iterations: int = 50, | ||
timeout: int = 5) -> 'AtomizedModel': | ||
""" Method for tuning hyperparameters """ | ||
tuner = TunerBuilder(input_data.task)\ | ||
.with_tuner(SimultaneousTuner)\ | ||
|
@@ -62,40 +67,46 @@ def fine_tune(self, metric_function: Callable, | |
|
||
@property | ||
def metadata(self) -> OperationMetaInfo: | ||
generator = make_pipeline_generator(self.pipeline) | ||
tags = set() | ||
|
||
for node in generator: | ||
tags.update(node.operation_tags) | ||
|
||
root_node = self.pipeline.root_node | ||
supported_strategies = None | ||
allowed_positions = ['any'] | ||
tags = list(tags) | ||
|
||
operation_info = OperationMetaInfo(root_node.operation.supplementary_data.id, | ||
root_node.operation.supplementary_data.input_types, | ||
root_node.operation.supplementary_data.output_types, | ||
root_node.operation.supplementary_data.task_type, | ||
supported_strategies, allowed_positions, | ||
tags) | ||
|
||
def extract_metadata_from_pipeline(attr_name: str, | ||
node_filter: Optional[Callable[[PipelineNode], bool]] = None, | ||
reduce_function: Optional[Callable[[List[Set]], Set]] = None): | ||
nodes_to_extract_metadata = self.pipeline.nodes | ||
if node_filter is not None: | ||
nodes_to_extract_metadata = [node for node in nodes_to_extract_metadata if node_filter(node)] | ||
data = [set(getattr(node.operation.metadata, attr_name)) for node in nodes_to_extract_metadata] | ||
if reduce_function is None: | ||
reduce_function = lambda x, y: x | y | ||
return list(reduce(reduce_function, data)) | ||
|
||
tags = extract_metadata_from_pipeline('tags') | ||
input_types = extract_metadata_from_pipeline('input_types', | ||
node_filter=lambda node: node.is_primary, | ||
reduce_function=lambda x, y: x & y) | ||
output_types = root_node.operation.metadata.output_types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут вместо lambda можно использовать эти функции:
|
||
presets = extract_metadata_from_pipeline('presets') | ||
|
||
operation_info = OperationMetaInfo(id=root_node.operation.metadata.id, | ||
input_types=input_types, | ||
output_types=output_types, | ||
task_type=root_node.operation.metadata.task_type, | ||
supported_strategies=None, | ||
allowed_positions=['any'], | ||
tags=tags, | ||
presets=presets) | ||
return operation_info | ||
|
||
def description(self, operation_params: Optional[dict]): | ||
def description(self, operation_params: Optional[dict] = None) -> str: | ||
operation_type = self.operation_type | ||
operation_length = self.pipeline.length | ||
operation_depth = self.pipeline.depth | ||
operation_id = self.unique_id | ||
operation_types = {} | ||
|
||
for node in self.pipeline.nodes: | ||
if node.operation.operation_type in operation_types: | ||
operation_types[node.operation.operation_type] += 1 | ||
else: | ||
operation_types[node.operation.operation_type] = 1 | ||
|
||
operation_id = self.pipeline.root_node.descriptive_id | ||
operation_types = map(lambda node: node.operation.operation_type, | ||
self.pipeline.nodes) | ||
operation_types_dict = dict(Counter(operation_types)) | ||
return f'{operation_type}_length:{operation_length}_depth:{operation_depth}' \ | ||
f'_types:{operation_types}_id:{operation_id}' | ||
f'_types:{operation_types_dict}_id:{operation_id}' | ||
|
||
@staticmethod | ||
def assign_tabular_column_types(output_data: OutputData, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Очень не хватает докстринги для понимания параметров