Skip to content
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

Pipeline save&load path refactoring #1066

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions examples/advanced/automl/h2o_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from sklearn.metrics import roc_auc_score as roc_auc, mean_squared_error, mean_absolute_error

from examples.advanced.time_series_forecasting.composing_pipelines import visualise
from examples.simple.pipeline_import_export import create_correct_path
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.pipelines.node import PipelineNode
from fedot.core.pipelines.pipeline import Pipeline
Expand Down Expand Up @@ -36,11 +35,10 @@ def pipeline_h2o_ts(window_size: int = 20):

def export_h2o(pipeline, pipeline_path, test_data):
# Export it
pipeline.save(path=pipeline_path)
pipeline.save(path=pipeline_path, create_subdir=False)

# Import pipeline
json_path_load = create_correct_path(pipeline_path)
new_pipeline = Pipeline.from_serialized(json_path_load)
new_pipeline = Pipeline().load(pipeline_path)

results = new_pipeline.predict(input_data=test_data, output_mode="full_probs")
prediction_after_export = results.predict[:, 0]
Expand Down
16 changes: 6 additions & 10 deletions examples/advanced/automl/tpot_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np

from examples.advanced.time_series_forecasting.composing_pipelines import visualise
from examples.simple.pipeline_import_export import create_correct_path
from fedot.core.data.data_split import train_test_data_setup
from fedot.core.pipelines.node import PipelineNode
from fedot.core.pipelines.pipeline import Pipeline
Expand Down Expand Up @@ -49,11 +48,10 @@ def tpot_classification_pipeline_evaluation():
print(f'Before export {prediction_before_export[:4]}')

# Export it
pipeline.save(path=pipeline_path)
pipeline.save(path=pipeline_path, create_subdir=False)

# Import pipeline
json_path_load = create_correct_path(pipeline_path)
new_pipeline = Pipeline.from_serialized(json_path_load)
new_pipeline = Pipeline().load(pipeline_path)

predicted_output_after_export = new_pipeline.predict(test_data, output_mode="full_probs")
prediction_after_export = predicted_output_after_export.predict[:, 0]
Expand All @@ -80,11 +78,10 @@ def tpot_regression_pipeline_evaluation():
print(f'Before export {results.predict[:4]}')

# Export it
pipeline.save(path=pipeline_path)
pipeline.save(path=pipeline_path, create_subdir=False)

# Import pipeline
json_path_load = create_correct_path(pipeline_path)
new_pipeline = Pipeline.from_serialized(json_path_load)
new_pipeline = Pipeline().load(pipeline_path)

predicted_output_after_export = new_pipeline.predict(test_data)
prediction_after_export = predicted_output_after_export.predict[:4]
Expand All @@ -106,11 +103,10 @@ def tpot_ts_pipeline_evaluation():
print(f'Before export {test_pred.predict[:4]}')

# Export it
pipeline.save(path=pipeline_path)
pipeline.save(path=pipeline_path, create_subdir=False)

# Import pipeline
json_path_load = create_correct_path(pipeline_path)
new_pipeline = Pipeline.from_serialized(json_path_load)
new_pipeline = Pipeline().load(pipeline_path)

predicted_output_after_export = new_pipeline.predict(test_data)
prediction_after_export = predicted_output_after_export.predict[:4]
Expand Down
22 changes: 2 additions & 20 deletions examples/advanced/sensitivity_analysis/pipeline_export_with_sa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

import numpy as np

from examples.advanced.sensitivity_analysis.dataset_access import get_scoring_data
Expand All @@ -9,21 +7,6 @@
from fedot.sensitivity.nodes_sensitivity import NodesAnalysis


def create_correct_path(path: str, dirname_flag: bool = False):
"""
Create path with time which was created during the testing process.
"""

for dirname in next(os.walk(os.path.curdir))[1]:
if dirname.endswith(path):
if dirname_flag:
return dirname
else:
file = os.path.join(dirname, path + '.json')
return file
return None


def run_import_export_example(pipeline_path):
# Prepare data to train the model
train_data, test_data = get_scoring_data()
Expand All @@ -41,11 +24,10 @@ def run_import_export_example(pipeline_path):
NodeReplaceOperationAnalyze]).analyze()

# Export it
pipeline.save(path=pipeline_path)
pipeline.save(path=pipeline_path, create_subdir=False)

# Import pipeline
json_path_load = create_correct_path(pipeline_path)
new_pipeline = Pipeline.from_serialized(json_path_load)
new_pipeline = Pipeline().load(pipeline_path)

predicted_output_after_export = new_pipeline.predict(test_data)
prediction_after_export = np.array(predicted_output_after_export.predict)
Expand Down
17 changes: 0 additions & 17 deletions examples/simple/pipeline_import_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import os

import numpy as np

Expand All @@ -12,22 +11,6 @@
from fedot.core.utils import fedot_project_root


def create_correct_path(path: str, dirname_flag: bool = False):
"""
Create path with time which was created during the testing process.
"""
# TODO: this function is used in many places, but now is not really needed
last_el = None
for dirname in next(os.walk(os.path.curdir))[1]:
if dirname.endswith(path):
if dirname_flag:
last_el = dirname
else:
file = os.path.join(dirname, path + '.json')
last_el = file
return last_el


def run_import_export_example(pipeline_path, pipeline):
features_options = {'informative': 1, 'bias': 0.0}
samples_amount = 100
Expand Down
18 changes: 8 additions & 10 deletions test/unit/models/test_atomized_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from fedot.core.pipelines.node import PipelineNode
from fedot.core.pipelines.pipeline import Pipeline
from fedot.core.utils import fedot_project_root
from test.unit.utilities.test_pipeline_import_export import create_correct_path, create_func_delete_files
from test.unit.utilities.test_pipeline_import_export import create_func_delete_files


@pytest.fixture(scope='session', autouse=True)
Expand Down Expand Up @@ -109,31 +109,29 @@ def create_input_data():
def test_save_load_atomized_pipeline_correctly():
pipeline = create_pipeline_with_several_nested_atomized_model()

json_actual, _ = pipeline.save('test_save_load_atomized_pipeline_correctly', create_subdir=False)
path = 'test_save_load_atomized_pipeline_correctly'
json_actual, _ = pipeline.save(path, create_subdir=False)

json_path_load = create_correct_path('test_save_load_atomized_pipeline_correctly')

with open(json_path_load, 'r') as json_file:
with open(os.path.join(path, path + '.json'), 'r') as json_file:
json_expected = json.load(json_file)

pipeline_loaded = Pipeline.from_serialized(json_path_load)
pipeline_loaded = Pipeline().load(path)

assert pipeline.length == pipeline_loaded.length
assert json_actual == json.dumps(json_expected, indent=4)


def test_save_load_fitted_atomized_pipeline_correctly():
train_data, test_data = create_input_data()
path = 'test_save_load_fitted_atomized_pipeline_correctly'

pipeline = create_pipeline_with_several_nested_atomized_model()

pipeline.fit(train_data)
before_save_predicted = pipeline.predict(test_data)
json_actual, _ = pipeline.save('test_save_load_fitted_atomized_pipeline_correctly', create_subdir=False)

json_path_load = create_correct_path('test_save_load_fitted_atomized_pipeline_correctly')
json_actual, _ = pipeline.save(path, create_subdir=False)

pipeline_loaded = Pipeline.from_serialized(json_path_load)
pipeline_loaded = Pipeline().load(path)
json_expected, _ = pipeline_loaded.save('test_save_load_fitted_atomized_pipeline_correctly_loaded',
create_subdir=False)

Expand Down
10 changes: 4 additions & 6 deletions test/unit/models/test_custom_model_introduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pandas as pd

from examples.advanced.time_series_forecasting.custom_model_tuning import get_fitting_custom_pipeline
from examples.simple.pipeline_import_export import create_correct_path
from fedot.api.main import Fedot
from fedot.core.data.data import InputData
from fedot.core.data.data_split import train_test_data_setup
Expand Down Expand Up @@ -194,15 +193,14 @@ def test_save_pipeline_with_custom():
pipeline = get_centered_pipeline()
pipeline.fit_from_scratch(train_input)

pipeline.save(path='test_pipeline', create_subdir=False)
json_path_load = create_correct_path('test_pipeline')
new_pipeline = Pipeline.from_serialized(json_path_load)
path = 'test_pipeline'
pipeline.save(path=path, create_subdir=False)
new_pipeline = Pipeline().load(path)
predicted_output_after_export = new_pipeline.predict(predict_input)
prediction_after_export = np.array(predicted_output_after_export.predict)

# recursive deleting
dir_ = os.path.dirname(json_path_load)
shutil.rmtree(dir_)
shutil.rmtree(path)

assert prediction_after_export is not None

Expand Down
Loading