From aded2570a19c7dd14eb60137e93bb5b42a36f611 Mon Sep 17 00:00:00 2001 From: Sarah Krebs Date: Fri, 8 Sep 2023 16:35:11 +0200 Subject: [PATCH] Format --- deepcave/config.py | 4 +++- .../hyperparameter/symbolic_explanations.py | 18 +++++++--------- deepcave/utils/symbolic_regression.py | 21 +++++-------------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/deepcave/config.py b/deepcave/config.py index 674a338a..4aefb8b8 100644 --- a/deepcave/config.py +++ b/deepcave/config.py @@ -49,7 +49,9 @@ def PLUGINS(self) -> Dict[str, List["Plugin"]]: from deepcave.plugins.budget.budget_correlation import BudgetCorrelation from deepcave.plugins.hyperparameter.importances import Importances from deepcave.plugins.hyperparameter.pdp import PartialDependencies - from deepcave.plugins.hyperparameter.symbolic_explanations import SymbolicExplanations + from deepcave.plugins.hyperparameter.symbolic_explanations import ( + SymbolicExplanations, + ) from deepcave.plugins.objective.configuration_cube import ConfigurationCube from deepcave.plugins.objective.cost_over_time import CostOverTime from deepcave.plugins.objective.parallel_coordinates import ParallelCoordinates diff --git a/deepcave/plugins/hyperparameter/symbolic_explanations.py b/deepcave/plugins/hyperparameter/symbolic_explanations.py index 7097f52c..9f094ed9 100644 --- a/deepcave/plugins/hyperparameter/symbolic_explanations.py +++ b/deepcave/plugins/hyperparameter/symbolic_explanations.py @@ -2,8 +2,8 @@ import numpy as np import plotly.graph_objs as go from dash import dcc, html -from pyPDP.algorithms.pdp import PDP from gplearn.genetic import SymbolicRegressor +from pyPDP.algorithms.pdp import PDP from deepcave import config from deepcave.evaluators.epm.random_forest_surrogate import RandomForestSurrogate @@ -11,7 +11,7 @@ from deepcave.runs import Status from deepcave.utils.layout import get_checklist_options, get_select_options, help_button from deepcave.utils.styled_plotty import get_color, get_hyperparameter_ticks, save_image -from deepcave.utils.symbolic_regression import get_function_set, convert_symb +from deepcave.utils.symbolic_regression import convert_symb, get_function_set GRID_POINTS_PER_AXIS = 20 SAMPLES_PER_HP = 10 @@ -24,7 +24,7 @@ class SymbolicExplanations(StaticPlugin): id = "symbolic_explanations" name = "Symbolic Explanations" icon = "fas fa-subscript" - help = "docs/plugins/partial_dependencies.rst" # TODO + help = "docs/plugins/partial_dependencies.rst" # TODO activate_run_selection = True @staticmethod @@ -129,8 +129,8 @@ def get_filter_layout(register): def load_inputs(self): return { - #"show_confidence": {"options": get_select_options(binary=True), "value": "true"}, - #"show_ice": {"options": get_select_options(binary=True), "value": "true"}, + # "show_confidence": {"options": get_select_options(binary=True), "value": "true"}, + # "show_ice": {"options": get_select_options(binary=True), "value": "true"}, } def load_dependency_inputs(self, run, previous_inputs, inputs): @@ -234,11 +234,7 @@ def process(run, inputs): y_symbolic = symb_model.predict(x).tolist() - return { - "x": x, - "y": y_symbolic, - "expr": str(conv_expr) - } + return {"x": x, "y": y_symbolic, "expr": str(conv_expr)} @staticmethod def get_output_layout(register): @@ -313,7 +309,7 @@ def load_outputs(run, inputs, outputs): xaxis=dict(tickvals=x_tickvals, ticktext=x_ticktext, title=hp1_name), yaxis=dict(tickvals=y_tickvals, ticktext=y_ticktext, title=hp2_name), margin=config.FIGURE_MARGIN, - title=expr + title=expr, ) ) diff --git a/deepcave/utils/symbolic_regression.py b/deepcave/utils/symbolic_regression.py index 6f999cb2..c726914f 100644 --- a/deepcave/utils/symbolic_regression.py +++ b/deepcave/utils/symbolic_regression.py @@ -1,5 +1,5 @@ -import sympy import numpy as np +import sympy from gplearn import functions from gplearn.functions import make_function @@ -14,21 +14,11 @@ def exp(x): def get_function_set(): exp_func = make_function(function=exp, arity=1, name="exp") - function_set = [ - "add", - "sub", - "mul", - "div", - "sqrt", - "log", - "sin", - "cos", - "abs", - exp_func - ] + function_set = ["add", "sub", "mul", "div", "sqrt", "log", "sin", "cos", "abs", exp_func] return function_set + def convert_symb(symb, n_dim: int = None, n_decimals: int = None) -> sympy.core.expr: """ Convert a fitted symbolic regression to a simplified and potentially rounded mathematical expression. @@ -68,14 +58,13 @@ def convert_symb(symb, n_dim: int = None, n_decimals: int = None) -> sympy.core. symb_str = str(symb._program) - converter = { "sub": lambda x, y: x - y, "div": lambda x, y: x / y, "mul": lambda x, y: x * y, "add": lambda x, y: x + y, "neg": lambda x: -x, - "pow": lambda x, y: x**y + "pow": lambda x, y: x**y, } if symb._program.length_ > 300: @@ -99,4 +88,4 @@ def convert_symb(symb, n_dim: int = None, n_decimals: int = None) -> sympy.core. if isinstance(a, sympy.core.numbers.Float): symb_simpl = symb_simpl.subs(a, round(a, n_decimals)) - return symb_simpl \ No newline at end of file + return symb_simpl