diff --git a/hyppopy/plugins/optunity_settings_plugin.py b/hyppopy/plugins/optunity_settings_plugin.py index 6cce92b..ca7bb99 100644 --- a/hyppopy/plugins/optunity_settings_plugin.py +++ b/hyppopy/plugins/optunity_settings_plugin.py @@ -1,104 +1,106 @@ # -*- coding: utf-8 -*- # # DKFZ # # # Copyright (c) German Cancer Research Center, # Division of Medical and Biological Informatics. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. # # See LICENSE.txt or http://www.mitk.org for details. # # Author: Sven Wanner (s.wanner@dkfz.de) import os import logging from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) from pprint import pformat try: import optunity from yapsy.IPlugin import IPlugin except: LOG.warning("optunity package not installed, will ignore this plugin!") print("optunity package not installed, will ignore this plugin!") from hyppopy.settingspluginbase import SettingsPluginBase from hyppopy.settingsparticle import split_categorical class optunity_Settings(SettingsPluginBase, IPlugin): def __init__(self): SettingsPluginBase.__init__(self) LOG.debug("initialized") def convert_parameter(self, input_dict): LOG.debug("convert input parameter\n\n\t{}\n".format(pformat(input_dict))) solution_space = {} # split input in categorical and non-categorical data cat, uni = split_categorical(input_dict) # build up dictionary keeping all non-categorical data uniforms = {} for key, value in uni.items(): for key2, value2 in value.items(): if key2 == 'data': uniforms[key] = value2 + if len(cat) == 0: + return uniforms # build nested categorical structure inner_level = uniforms for key, value in cat.items(): tmp = {} tmp2 = {} for key2, value2 in value.items(): if key2 == 'data': for elem in value2: tmp[elem] = inner_level tmp2[key] = tmp inner_level = tmp2 solution_space = tmp2 return solution_space # class optunity_SettingsParticle(SettingsParticle): # # def __init__(self, name=None, domain=None, dtype=None, data=None): # SettingsParticle.__init__(self, name, domain, dtype, data) # # def convert(self): # if self.domain == "uniform": # if self.dtype == "float" or self.dtype == "double": # pass # elif self.dtype == "int": # pass # else: # msg = f"cannot convert the type {self.dtype} in domain {self.domain}" # LOG.error(msg) # raise LookupError(msg) # elif self.domain == "loguniform": # if self.dtype == "float" or self.dtype == "double": # pass # else: # msg = f"cannot convert the type {self.dtype} in domain {self.domain}" # LOG.error(msg) # raise LookupError(msg) # elif self.domain == "normal": # if self.dtype == "float" or self.dtype == "double": # pass # else: # msg = f"cannot convert the type {self.dtype} in domain {self.domain}" # LOG.error(msg) # raise LookupError(msg) # elif self.domain == "categorical": # if self.dtype == 'str': # pass # elif self.dtype == 'bool': # pass diff --git a/hyppopy/plugins/randomsearch_solver_plugin.py b/hyppopy/plugins/randomsearch_solver_plugin.py index 9b389ce..cfa51a2 100644 --- a/hyppopy/plugins/randomsearch_solver_plugin.py +++ b/hyppopy/plugins/randomsearch_solver_plugin.py @@ -1,134 +1,135 @@ # DKFZ # # # Copyright (c) German Cancer Research Center, # Division of Medical and Biological Informatics. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. # # See LICENSE.txt or http://www.mitk.org for details. # # Author: Sven Wanner (s.wanner@dkfz.de) import os import copy import random import logging import numpy as np from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) from pprint import pformat from yapsy.IPlugin import IPlugin from hyppopy.helpers import Trials from hyppopy.globals import DEFAULTITERATIONS from hyppopy.projectmanager import ProjectManager from hyppopy.solverpluginbase import SolverPluginBase def drawUniformSample(param): assert param['type'] != 'str', "Cannot sample a string list uniformly!" assert param['data'][0] < param['data'][1], "Precondition violation: data[0] > data[1]!" s = random.random() s *= np.abs(param['data'][1]-param['data'][0]) s += param['data'][0] if param['type'] == 'int': s = int(np.round(s)) if s < param['data'][0]: s = int(param['data'][0]) if s > param['data'][1]: s = int(param['data'][1]) return s def drawNormalSample(param): mu = (param['data'][1]-param['data'][0])/2 sigma = mu/3 - s = np.random.normal(loc=mu, scale=sigma) + s = np.random.normal(loc=param['data'][0] + mu, scale=sigma) return s def drawLoguniformSample(param): p = copy.deepcopy(param) p['data'][0] = np.log(param['data'][0]) p['data'][1] = np.log(param['data'][1]) assert p['data'][0] is not np.nan, "Precondition violation, left bound input error, results in nan!" assert p['data'][1] is not np.nan, "Precondition violation, right bound input error, results in nan!" x = drawUniformSample(p) s = np.exp(x) return s def drawCategoricalSample(param): return random.sample(param['data'], 1)[0] def drawSample(param): if param['domain'] == "uniform": return drawUniformSample(param) elif param['domain'] == "normal": return drawNormalSample(param) elif param['domain'] == "loguniform": return drawLoguniformSample(param) elif param['domain'] == "categorical": return drawCategoricalSample(param) else: raise LookupError("Unknown domain {}".format(param['domain'])) class randomsearch_Solver(SolverPluginBase, IPlugin): trials = None best = None def __init__(self): SolverPluginBase.__init__(self) LOG.debug("initialized") def blackbox_function(self, params): loss = None self.trials.set_parameter(params) try: self.trials.start_iteration() loss = self.blackbox_function_template(self.data, params) self.trials.stop_iteration() if loss is None: self.trials.set_status(False) + self.trials.stop_iteration() except Exception as e: LOG.error("execution of self.loss(self.data, params) failed due to:\n {}".format(e)) self.trials.set_status(False) self.trials.stop_iteration() self.trials.set_status(True) self.trials.set_loss(loss) return def execute_solver(self, parameter): LOG.debug("execute_solver using solution space:\n\n\t{}\n".format(pformat(parameter))) self.trials = Trials() if 'max_iterations' not in ProjectManager.__dict__: msg = "Missing max_iteration entry in config, used default {}!".format(DEFAULTITERATIONS) LOG.warning(msg) print("WARNING: {}".format(msg)) setattr(ProjectManager, 'max_iterations', DEFAULTITERATIONS) N = ProjectManager.max_iterations - print("") + #print("") try: for n in range(N): params = {} for name, p in parameter.items(): params[name] = drawSample(p) self.blackbox_function(params) - print("\r{}% done".format(int(round(100.0 / N * n))), end="") + #print("\r{}% done".format(int(round(100.0 / N * n))), end="") except Exception as e: msg = "internal error in randomsearch execute_solver occured. {}".format(e) LOG.error(msg) raise BrokenPipeError(msg) - print("\r{}% done".format(100), end="") - print("") + #print("\r{}% done".format(100), end="") + #print("") def convert_results(self): return self.trials.get() diff --git a/setup.py b/setup.py index 0757283..2d78c24 100644 --- a/setup.py +++ b/setup.py @@ -1,63 +1,63 @@ # -*- coding: utf-8 -*- import os from setuptools import setup, find_packages with open('README.rst') as f: readme = f.read() with open('LICENSE') as f: license = f.read() -VERSION = "0.3.2" +VERSION = "0.3.3" ROOT = os.path.dirname(os.path.realpath(__file__)) new_init = [] with open(os.path.join(ROOT, *("hyppopy", "__init__.py")), "r") as infile: for line in infile: new_init.append(line) for n in range(len(new_init)): if new_init[n].startswith("__version__"): split = line.split("=") new_init[n] = "__version__ = '" + VERSION + "'\n" with open(os.path.join(ROOT, *("hyppopy", "__init__.py")), "w") as outfile: outfile.writelines(new_init) setup( name='hyppopy', version=VERSION, description='Hyper-Parameter Optimization Toolbox for Blackboxfunction Optimization', long_description=readme, # if you want, put your own name here # (this would likely result in people sending you emails) author='Sven Wanner', author_email='s.wanner@dkfz.de', url='', license=license, packages=find_packages(exclude=('*test*', 'doc')), package_data={ 'hyppopy.plugins': ['*.yapsy-plugin'] }, # the requirements to install this project. # Since this one is so simple this is empty. install_requires=[ 'dicttoxml>=1.7.4', 'xmltodict>=0.11.0', 'hyperopt>=0.1.1', 'Optunity>=1.1.1', 'numpy>=1.16.0', 'matplotlib>=3.0.2', 'scikit-learn>=0.20.2', 'scipy>=1.2.0', 'Sphinx>=1.8.3', 'xmlrunner>=1.7.7', 'Yapsy>=1.11.223', 'pandas>=0.24.1', 'seaborn>=0.9.0', 'deap>=1.2.2', 'bayesian-optimization>=1.0.1' ], )