Skip to content

Commit

Permalink
move 'evaluate_from_data'-parameter to ml-class only
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBlanke committed May 26, 2024
1 parent dd1340b commit 828436e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
36 changes: 1 addition & 35 deletions src/surfaces/test_functions/_base_test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import numpy as np
import pandas as pd

from ..data_collector import SurfacesDataCollector


class BaseTestFunction:
explanation = """ """
Expand All @@ -20,18 +18,12 @@ class BaseTestFunction:
objective_function: callable
pure_objective_function: callable

def __init__(self, metric, sleep, evaluate_from_data):
def __init__(self, metric, sleep):
self.sleep = sleep
self.metric = metric

self.create_objective_function()

if evaluate_from_data:
self.sdc = SurfacesDataCollector()
self._objective_function_ = self.objective_function_loaded
else:
self._objective_function_ = self.pure_objective_function

def create_objective_function(self):
e_msg = "'create_objective_function'-method is not implemented"
raise NotImplementedError(e_msg)
Expand Down Expand Up @@ -61,30 +53,4 @@ def objective_function(self, *input):
"""

metric = self.pure_objective_function(*input)
print("\n metric ", metric, "\n")
return self.return_metric(metric)

def objective_function_loaded(self, params):
try:
parameter_d = params.para_dict
except AttributeError:
parameter_d = params

for para_names, dim_value in parameter_d.items():
try:
parameter_d[para_names] = dim_value.__name__
except AttributeError:
pass

search_data = self.sdc.load(self.__name__)
if search_data is None:
msg = "Search Data is empty"
raise TypeError(msg)

params_df = pd.DataFrame(parameter_d, index=[0])

para_df_row = search_data[
np.all(search_data[self.para_names].values == params_df.values, axis=1)
]
score = para_df_row["score"].values[0]
return score
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,40 @@


from .._base_test_function import BaseTestFunction
from ...data_collector import SurfacesDataCollector


class MachineLearningFunction(BaseTestFunction):
def __init__(self, metric, sleep, evaluate_from_data):
super().__init__(metric, sleep, evaluate_from_data)
super().__init__(metric, sleep)

if evaluate_from_data:
self.sdc = SurfacesDataCollector()
self._objective_function_ = self.objective_function_loaded
else:
self._objective_function_ = self.pure_objective_function

def objective_function_loaded(self, params):
try:
parameter_d = params.para_dict
except AttributeError:
parameter_d = params

for para_names, dim_value in parameter_d.items():
try:
parameter_d[para_names] = dim_value.__name__
except AttributeError:
pass

search_data = self.sdc.load(self.__name__)
if search_data is None:
msg = "Search Data is empty"
raise TypeError(msg)

params_df = pd.DataFrame(parameter_d, index=[0])

para_df_row = search_data[
np.all(search_data[self.para_names].values == params_df.values, axis=1)
]
score = para_df_row["score"].values[0]
return score
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ def __init__(
self,
metric="loss",
sleep=0,
evaluate_from_data=False,
):
super().__init__(metric, sleep, evaluate_from_data)
super().__init__(metric, sleep)

self.metric = metric
self.sleep = sleep

self._objective_function_ = self.pure_objective_function

def return_metric(self, loss):
if self.metric == "score":
return -loss
Expand Down

0 comments on commit 828436e

Please sign in to comment.