Skip to content

Commit

Permalink
add opt histopy vis
Browse files Browse the repository at this point in the history
  • Loading branch information
v1docq committed Apr 16, 2024
1 parent fc49e56 commit 79f75bb
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 80 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from fedot_ind.api.utils.path_lib import PROJECT_PATH
from fedot_ind.tools.example_utils import industrial_common_modelling_loop

if __name__ == "__main__":
return_history = True
opt_hist = PROJECT_PATH + '/examples/data/forecasting/D1679_opt_history/'
dataset_name = 'Lightning7'
finetune = False
metric_names = ('f1', 'accuracy', 'precision', 'roc_auc')
api_config = dict(problem='classification',
metric='f1',
timeout=5,
pop_size=10,
with_tuning=False,
n_jobs=2,
logging_level=10)

industrial, labels, metrics = industrial_common_modelling_loop(api_config=api_config,
dataset_name=dataset_name,
finetune=finetune)
if return_history:
opt_hist = industrial.save_optimization_history(return_history=True)
else:
# tutorial sample of opt history
opt_hist = PROJECT_PATH + '/examples/data/forecasting/D1679_opt_history/'
opt_hist = industrial.vis_optimisation_history(opt_history_path=opt_hist, return_history=True)

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from fedot_ind.api.main import FedotIndustrial
from fedot_ind.tools.loader import DataLoader

dataset_name = 'Lightning7'
metric_names = ('f1', 'accuracy', 'precision', 'roc_auc')
api_config = dict(problem='classification',
metric='f1',
timeout=5,
n_jobs=2,
industrial_strategy='federated_automl',
industrial_strategy_params={},
logging_level=20)
train_data, test_data = DataLoader(dataset_name).load_data()
industrial = FedotIndustrial(**api_config)
industrial.fit(train_data)
predict = industrial.predict(test_data)
_ = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from fedot_ind.api.main import FedotIndustrial
from fedot_ind.tools.loader import DataLoader

dataset_name = 'Lightning7'
metric_names = ('f1', 'accuracy', 'precision', 'roc_auc')
api_config = dict(problem='ts_forecasting',
metric='rmse',
timeout=15,
with_tuning=False,
industrial_strategy='forecasting_assumptions',
industrial_strategy_params={},
logging_level=20)
train_data, test_data = DataLoader(dataset_name).load_data()
industrial = FedotIndustrial(**api_config)
industrial.fit(train_data)
predict = industrial.predict(test_data)
_ = 1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
if __name__ == "__main__":
dataset_name = 'Handwriting'
finetune = True
initial_assumption = PipelineBuilder().add_node('channel_filtration'). \
add_node('quantile_extractor').add_node('rf')
initial_assumption = PipelineBuilder().add_node('channel_filtration').add_node('quantile_extractor').add_node('rf')
metric_names = ('f1', 'accuracy', 'precision', 'roc_auc')
api_config = dict(problem='classification',
metric='f1',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

import pandas as pd
from fedot.core.pipelines.pipeline_builder import PipelineBuilder

from fedot_ind.api.utils.path_lib import PROJECT_PATH
from fedot_ind.core.repository.constanst_repository import M4_FORECASTING_BENCH, M4_FORECASTING_LENGTH
from fedot_ind.tools.example_utils import industrial_forecasting_modelling_loop, compare_forecast_with_sota, \
Expand Down Expand Up @@ -37,7 +35,7 @@
print('Already evaluated, but with bad metrics')
horizon = M4_FORECASTING_LENGTH[dataset_name[0]]
api_config.update(task_params={'forecast_length': horizon})
api_config.update(output_folder=os.path.join(PROJECT_PATH, 'results_of_experiments',dataset_name))
api_config.update(output_folder=os.path.join(PROJECT_PATH, 'results_of_experiments', dataset_name))
n_beats_forecast, n_beats_metrics, \
autogluon_forecast, autogluon_metrics = compare_forecast_with_sota(dataset_name=dataset_name,
horizon=horizon)
Expand Down
53 changes: 36 additions & 17 deletions fedot_ind/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
from pathlib import Path
from typing import Union

import matplotlib
import numpy as np
import pandas as pd
from fedot.api.main import Fedot
from fedot.core.pipelines.pipeline import Pipeline

from golem.core.optimisers.opt_history_objects.opt_history import OptHistory

from fedot.core.pipelines.adapters import PipelineAdapter
from fedot.core.utils import fedot_project_root
from fedot.core.visualisation.pipeline_specific_visuals import PipelineHistoryVisualizer
from fedot_ind.api.utils.checkers_collections import DataCheck
from fedot_ind.api.utils.industrial_strategy import IndustrialStrategy
from fedot_ind.api.utils.path_lib import DEFAULT_PATH_RESULTS as default_path_to_save_results
Expand All @@ -19,7 +25,7 @@
from fedot_ind.core.operation.transformation.splitter import TSTransformer
from fedot_ind.core.optimizer.IndustrialEvoOptimizer import IndustrialEvoOptimizer
from fedot_ind.core.repository.constanst_repository import \
FEDOT_WORKER_TIMEOUT_PARTITION, FEDOT_GET_METRICS, FEDOT_TUNING_METRICS,\
FEDOT_WORKER_TIMEOUT_PARTITION, FEDOT_GET_METRICS, FEDOT_TUNING_METRICS, \
FEDOT_API_PARAMS, FEDOT_ASSUMPTIONS, FEDOT_TUNER_STRATEGY
from fedot_ind.core.repository.industrial_implementations.abstract import build_tuner
from fedot_ind.core.repository.initializer_industrial_models import IndustrialModels
Expand Down Expand Up @@ -366,9 +372,11 @@ def load(self, path):
self.solver.append(Pipeline().load(
f'{path}/{p}/0_pipeline_saved'))

def save_optimization_history(self, **kwargs):
def save_optimization_history(self, return_history: bool = False):
"""Plot prediction of the model"""
self.solver.history.save(f"{self.output_folder}/optimization_history.json")
if return_history:
return self.solver.history

def save_best_model(self):
if self.condition_check.solver_is_fedot_class(self.solver):
Expand All @@ -383,21 +391,6 @@ def save_best_model(self):
Pipeline(self.solver.ensemble_head).save(f'./raf_ensemble/ensemble_head', create_subdir=True)
self.solver.current_pipeline.save(f'./raf_ensemble/ensemble_composed', create_subdir=True)

def plot_fitness_by_generation(self, **kwargs):
"""Plot prediction of the model"""
self.solver.history.show.fitness_box(save_path=f'{self.output_folder}/fitness_by_gen.png',
best_fraction=0.5,
dpi=100)

def plot_operation_distribution(self, mode: str = 'total'):
"""Plot prediction of the model"""
if mode == 'total':
self.solver.history.show.operations_kde(
save_path=f'{self.output_folder}/operation_kde.png', dpi=100)
else:
self.solver.history.show.operations_animated_bar(
save_path=f'{self.output_folder}/history_animated_bars.gif', show_fitness=True, dpi=100)

def explain(self, **kwargs):
""" Explain model's prediction via time series points perturbation
Expand Down Expand Up @@ -429,6 +422,32 @@ def return_report(self) -> pd.DataFrame:
if isinstance(self.solver, Fedot):
return self.solver.return_report()

def vis_optimisation_history(self, opt_history_path: str = None,
mode: str = 'all',
return_history: bool = False):
""" The function runs visualization of the composing history and the best pipeline. """
# Gather pipeline and history.
matplotlib.use('TkAgg')
if isinstance(opt_history_path, str):
history = OptHistory.load(opt_history_path + 'optimization_history.json')
else:
history = opt_history_path
history_visualizer = PipelineHistoryVisualizer(history)
vis_func = {
'fitness': (history_visualizer.fitness_box, dict(save_path='fitness_by_generation.png',
best_fraction=1)),
'models': (history_visualizer.operations_animated_bar,
dict(save_path='operations_animated_bar.gif', show_fitness=True)),
'diversity': (history_visualizer.diversity_population, dict(save_path='diversity_population.gif', fps=1))}
if mode == 'all':
for func, params in vis_func.values():
func(**params)
else:
func, params = vis_func[mode]
func(**params)
if return_history:
return history_visualizer.history

@staticmethod
def generate_ts(ts_config: dict):
"""
Expand Down
5 changes: 5 additions & 0 deletions fedot_ind/core/architecture/preprocessing/data_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def __init__(self, data):
self.tensor_data = self.convert_to_tensor(data)

def convert_to_tensor(self, data):
if isinstance(data, tuple):
data = data[0]

if isinstance(data, torch.Tensor):
return data
elif isinstance(data, np.ndarray):
Expand Down Expand Up @@ -258,6 +261,8 @@ def __init__(self, data):
def convert_to_array(self, data):
if isinstance(data, np.ndarray):
return data
elif isinstance(data, tuple):
return data[0]
elif isinstance(data, torch.Tensor):
return data.detach().numpy()
elif isinstance(data, pd.DataFrame):
Expand Down
8 changes: 4 additions & 4 deletions fedot_ind/tools/example_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ def industrial_common_modelling_loop(dataset_name: str = None,

train_data, test_data = DataLoader(dataset_name=dataset_name).load_data()
if finetune:
model = industrial.finetune(train_data)
industrial.finetune(train_data)
else:
model = industrial.fit(train_data)
industrial.fit(train_data)

labels = industrial.predict(test_data)
metrics = industrial.get_metrics(target=test_data[1],
rounding_order=3,
metric_names=metric_names)
return model, labels, metrics
return industrial, labels, metrics


def read_results(forecast_result_path):
Expand All @@ -120,7 +120,7 @@ def create_comprasion_df(df, metric: str = 'rmse'):
df_full = pd.concat(df)
df_full = df_full[df_full['Unnamed: 0'] == metric]
df_full = df_full.drop('Unnamed: 0', axis=1)
df_full['Difference_industrial'] = (df_full.iloc[:, 1:2].min(axis=1) - df_full['industrial'])
df_full['Difference_industrial'] = (df_full.iloc[:, 1:3].min(axis=1) - df_full['industrial'])
df_full['industrial_Wins'] = df_full.apply(lambda row: 'Win' if row.loc['Difference_industrial'] > 0 else 'Loose',
axis=1)
return df_full

0 comments on commit 79f75bb

Please sign in to comment.