diff --git a/__main__.py b/__main__.py index f9af847..a720673 100644 --- a/__main__.py +++ b/__main__.py @@ -1,89 +1,95 @@ #!/usr/bin/env python # # 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 sys ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) sys.path.append(ROOT) from hyppopy.projectmanager import ProjectManager -from hyppopy.workflows.unet_usecase.unet_usecase import unet_usecase from hyppopy.workflows.svc_usecase.svc_usecase import svc_usecase +from hyppopy.workflows.knc_usecase.knc_usecase import knc_usecase +from hyppopy.workflows.lda_usecase.lda_usecase import lda_usecase +from hyppopy.workflows.unet_usecase.unet_usecase import unet_usecase from hyppopy.workflows.randomforest_usecase.randomforest_usecase import randomforest_usecase from hyppopy.workflows.imageregistration_usecase.imageregistration_usecase import imageregistration_usecase import os import sys import time import argparse def print_warning(msg): print("\n!!!!! WARNING !!!!!") print(msg) sys.exit() def args_check(args): if not args.workflow: print_warning("No workflow specified, check --help") if not args.config: print_warning("Missing config parameter, check --help") if not os.path.isfile(args.config): print_warning(f"Couldn't find configfile ({args.config}), please check your input --config") if __name__ == "__main__": parser = argparse.ArgumentParser(description='UNet Hyppopy UseCase Example Optimization.') parser.add_argument('-w', '--workflow', type=str, help='workflow to be executed') parser.add_argument('-o', '--output', type=str, default=None, help='output path to store result') parser.add_argument('-c', '--config', type=str, help='config filename, .xml or .json formats are supported.' 'pass a full path filename or the filename only if the' 'configfile is in the data folder') args = parser.parse_args() args_check(args) ProjectManager.read_config(args.config) if args.output is not None: ProjectManager.output_dir = args.output if args.workflow == "svc_usecase": uc = svc_usecase() elif args.workflow == "randomforest_usecase": uc = randomforest_usecase() + elif args.workflow == "knc_usecase": + uc = knc_usecase() + elif args.workflow == "lda_usecase": + uc = lda_usecase() elif args.workflow == "unet_usecase": uc = unet_usecase() elif args.workflow == "imageregistration_usecase": uc = imageregistration_usecase() else: print("No workflow called {} found!".format(args.workflow)) sys.exit() print("\nStart optimization...") start = time.process_time() - uc.run() + uc.run(save=True) end = time.process_time() print("Finished optimization!\n") print("Total Time: {}s\n".format(end-start)) res, best = uc.get_results() print("---- Optimal Parameter -----\n") for p in best.items(): print(" - {}\t:\t{}".format(p[0], p[1])) diff --git a/bin/hyppopy_exe.py b/bin/hyppopy_exe.py deleted file mode 100644 index f9af847..0000000 --- a/bin/hyppopy_exe.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python -# -# 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 sys -ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -sys.path.append(ROOT) - -from hyppopy.projectmanager import ProjectManager -from hyppopy.workflows.unet_usecase.unet_usecase import unet_usecase -from hyppopy.workflows.svc_usecase.svc_usecase import svc_usecase -from hyppopy.workflows.randomforest_usecase.randomforest_usecase import randomforest_usecase -from hyppopy.workflows.imageregistration_usecase.imageregistration_usecase import imageregistration_usecase - - -import os -import sys -import time -import argparse - - -def print_warning(msg): - print("\n!!!!! WARNING !!!!!") - print(msg) - sys.exit() - - -def args_check(args): - if not args.workflow: - print_warning("No workflow specified, check --help") - if not args.config: - print_warning("Missing config parameter, check --help") - if not os.path.isfile(args.config): - print_warning(f"Couldn't find configfile ({args.config}), please check your input --config") - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description='UNet Hyppopy UseCase Example Optimization.') - parser.add_argument('-w', '--workflow', type=str, help='workflow to be executed') - parser.add_argument('-o', '--output', type=str, default=None, help='output path to store result') - parser.add_argument('-c', '--config', type=str, help='config filename, .xml or .json formats are supported.' - 'pass a full path filename or the filename only if the' - 'configfile is in the data folder') - - args = parser.parse_args() - args_check(args) - - ProjectManager.read_config(args.config) - - if args.output is not None: - ProjectManager.output_dir = args.output - - if args.workflow == "svc_usecase": - uc = svc_usecase() - elif args.workflow == "randomforest_usecase": - uc = randomforest_usecase() - elif args.workflow == "unet_usecase": - uc = unet_usecase() - elif args.workflow == "imageregistration_usecase": - uc = imageregistration_usecase() - else: - print("No workflow called {} found!".format(args.workflow)) - sys.exit() - - print("\nStart optimization...") - start = time.process_time() - uc.run() - end = time.process_time() - - print("Finished optimization!\n") - print("Total Time: {}s\n".format(end-start)) - res, best = uc.get_results() - print("---- Optimal Parameter -----\n") - for p in best.items(): - print(" - {}\t:\t{}".format(p[0], p[1])) diff --git a/hyppopy/__init__.py b/hyppopy/__init__.py index e69de29..5f03f22 100644 --- a/hyppopy/__init__.py +++ b/hyppopy/__init__.py @@ -0,0 +1,3 @@ +__version__ = '0.1.1dev' +from hyppopy.solverfactory import SolverFactory +from hyppopy.projectmanager import ProjectManager \ No newline at end of file diff --git a/hyppopy/globals.py b/hyppopy/globals.py index 3b2a6cd..99491ce 100644 --- a/hyppopy/globals.py +++ b/hyppopy/globals.py @@ -1,31 +1,31 @@ # 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. import os import sys import logging ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) sys.path.insert(0, ROOT) LIBNAME = "hyppopy" PLUGIN_DEFAULT_DIR = os.path.join(ROOT, *(LIBNAME, "plugins")) TESTDATA_DIR = os.path.join(ROOT, *(LIBNAME, "tests", "data")) -SETTINGSSOLVERPATH = "settings/solver" +SETTINGSSOLVERPATH = "settings/solver_plugin" SETTINGSCUSTOMPATH = "settings/custom" DEEPDICT_XML_ROOT = LIBNAME -LOGFILENAME = os.path.join(ROOT, 'logfile2.log') +LOGFILENAME = os.path.join(ROOT, '{}_log.log'.format(LIBNAME)) DEBUGLEVEL = logging.DEBUG logging.basicConfig(filename=LOGFILENAME, filemode='w', format='%(levelname)s: %(name)s - %(message)s') diff --git a/hyppopy/__init__.py b/hyppopy/plugins/__init__.py similarity index 100% copy from hyppopy/__init__.py copy to hyppopy/plugins/__init__.py diff --git a/hyppopy/projectmanager.py b/hyppopy/projectmanager.py index 3eab4e3..efae813 100644 --- a/hyppopy/projectmanager.py +++ b/hyppopy/projectmanager.py @@ -1,111 +1,147 @@ # 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) from hyppopy.singleton import * from hyppopy.deepdict import DeepDict from hyppopy.globals import SETTINGSCUSTOMPATH, SETTINGSSOLVERPATH import os import logging +import datetime from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) @singleton_object class ProjectManager(metaclass=Singleton): def __init__(self): self.configfilename = None self.config = None self._extmembers = [] + self._identifier = None def clear(self): self.configfilename = None self.config = None self.remove_externals() def is_ready(self): return self.config is not None def remove_externals(self): for added in self._extmembers: if added in self.__dict__.keys(): del self.__dict__[added] self._extmembers = [] def get_hyperparameter(self): return self.config["hyperparameter"] def test_config(self): if not isinstance(self.config, DeepDict): msg = "test_config failed, config is not of type DeepDict" LOG.error(msg) - return False + raise IOError(msg) sections = ["hyperparameter"] - sections += SETTINGSSOLVERPATH.split("/") - sections += SETTINGSCUSTOMPATH.split("/") - for sec in sections: + sections += [SETTINGSSOLVERPATH.split("/")[-1]] + sections += [SETTINGSCUSTOMPATH.split("/")[-1]] + sections_available = [True, True, True] + for n, sec in enumerate(sections): if not self.config.has_section(sec): - msg = "test_config failed, config has no section {}".format(sec) - LOG.error(msg) - return False - return True + msg = "WARNING: config has no section {}".format(sec) + LOG.warning(msg) + sections_available[n] = False + return sections_available + def set_config(self, config): self.clear() if isinstance(config, dict): self.config = DeepDict() self.config.data = config elif isinstance(config, DeepDict): self.config = config else: msg = "unknown type ({}) for config passed, expected dict or DeepDict".format(type(config)) LOG.error(msg) raise IOError(msg) - if not self.test_config(): - self.clear() - return False - - try: - self._extmembers += self.config.transfer_attrs(self, SETTINGSCUSTOMPATH.split("/")[-1]) - self._extmembers += self.config.transfer_attrs(self, SETTINGSSOLVERPATH.split("/")[-1]) - except Exception as e: - msg = "transfering custom section as class attributes failed, " \ - "is the config path to your custom section correct? {}. Exception {}".format(SETTINGSCUSTOMPATH, e) + sections_available = self.test_config() + if not sections_available[0]: + msg = "Missing section {}".format("hyperparameter") LOG.error(msg) raise LookupError(msg) - + if not sections_available[1]: + msg = "Missing section {}".format(SETTINGSSOLVERPATH) + LOG.error(msg) + raise LookupError(msg) + else: + try: + self._extmembers += self.config.transfer_attrs(self, SETTINGSCUSTOMPATH.split("/")[-1]) + except Exception as e: + msg = "transfering custom section as class attributes failed, " \ + "is the config path to your custom section correct? {}. Exception {}".format(SETTINGSCUSTOMPATH, + e) + LOG.error(msg) + raise LookupError(msg) + if sections_available[2]: + try: + self._extmembers += self.config.transfer_attrs(self, SETTINGSSOLVERPATH.split("/")[-1]) + except Exception as e: + msg = "transfering custom section as class attributes failed, " \ + "is the config path to your custom section correct? {}. Exception {}".format(SETTINGSCUSTOMPATH, + e) + LOG.error(msg) + raise LookupError(msg) return True def read_config(self, configfile): self.clear() self.configfilename = configfile self.config = DeepDict(configfile) - if not self.test_config(): - self.clear() - return False - - try: - self._extmembers += self.config.transfer_attrs(self, SETTINGSCUSTOMPATH.split("/")[-1]) - self._extmembers += self.config.transfer_attrs(self, SETTINGSSOLVERPATH.split("/")[-1]) - except Exception as e: - msg = "transfering custom section as class attributes failed, " \ - "is the config path to your custom section correct? {}. Exception {e}".format(SETTINGSCUSTOMPATH, e) + sections_available = self.test_config() + if not sections_available[0]: + msg = "Missing section {}".format("hyperparameter") + LOG.error(msg) + raise LookupError(msg) + if not sections_available[1]: + msg = "Missing section {}".format(SETTINGSSOLVERPATH) LOG.error(msg) raise LookupError(msg) + else: + try: + self._extmembers += self.config.transfer_attrs(self, SETTINGSSOLVERPATH.split("/")[-1]) + except Exception as e: + msg = "transfering custom section as class attributes failed, " \ + "is the config path to your custom section correct? {}. Exception {}".format(SETTINGSSOLVERPATH, e) + LOG.error(msg) + raise LookupError(msg) + if sections_available[2]: + try: + self._extmembers += self.config.transfer_attrs(self, SETTINGSCUSTOMPATH.split("/")[-1]) + except Exception as e: + msg = "transfering custom section as class attributes failed, " \ + "is the config path to your custom section correct? {}. Exception {}".format(SETTINGSCUSTOMPATH, e) + LOG.error(msg) + raise LookupError(msg) return True + + def identifier(self, force=False): + if self._identifier is None or force: + self._identifier = datetime.datetime.now().strftime("%Y.%m.%d.%H.%M.%S") + return self._identifier diff --git a/hyppopy/resultviewer.py b/hyppopy/resultviewer.py index 096fe91..52da45a 100644 --- a/hyppopy/resultviewer.py +++ b/hyppopy/resultviewer.py @@ -1,83 +1,83 @@ # 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 pandas as pd import seaborn as sns import matplotlib.pyplot as plt import logging from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) sns.set(style="darkgrid") class ResultViewer(object): def __init__(self, fname=None, save_only=False): self.df = None self.has_duration = False self.hyperparameter = None self.save_only = save_only self.path = None self.appendix = None if fname is not None: self.read(fname) def read(self, fname): self.path = os.path.dirname(fname) split = os.path.basename(fname).split("_") - self.appendix = split[-2]+"_"+split[-1] + self.appendix = split[-1] self.appendix = self.appendix[:-4] self.df = pd.read_csv(fname, index_col=0) const_data = ["duration", "losses"] hyperparameter_columns = [item for item in self.df.columns if item not in const_data] self.hyperparameter = pd.DataFrame() for key in hyperparameter_columns: self.hyperparameter[key] = self.df[key] self.has_duration = "duration" in self.df.columns def show(self, save=True): if self.has_duration: sns_plot = sns.jointplot(y="duration", x="losses", data=self.df, kind="kde") if not self.save_only: plt.show() if save: save_name = os.path.join(self.path, "t_vs_loss_"+self.appendix+".png") try: sns_plot.savefig(save_name) except Exception as e: msg = "failed to save file {}, reason {}".format(save_name, e) LOG.error(msg) raise IOError(msg) sns_plot = sns.pairplot(self.df, height=1.8, aspect=1.8, plot_kws=dict(edgecolor="k", linewidth=0.5), diag_kind="kde", diag_kws=dict(shade=True)) fig = sns_plot.fig fig.subplots_adjust(top=0.93, wspace=0.3) t = fig.suptitle('Pairwise Plots', fontsize=14) if not self.save_only: plt.show() if save: save_name = os.path.join(self.path, "matrixview_"+self.appendix+".png") try: sns_plot.savefig(save_name) except Exception as e: msg = "failed to save file {}, reason {}".format(save_name, e) LOG.error(msg) raise IOError(msg) diff --git a/hyppopy/solver.py b/hyppopy/solver.py index 4d4a183..47d3762 100644 --- a/hyppopy/solver.py +++ b/hyppopy/solver.py @@ -1,126 +1,125 @@ # 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) from hyppopy.projectmanager import ProjectManager from hyppopy.resultviewer import ResultViewer import os -import datetime import logging import pandas as pd from hyppopy.globals import LIBNAME from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) class Solver(object): _name = None _solver_plugin = None _settings_plugin = None def __init__(self): pass def set_data(self, data): self._solver_plugin.set_data(data) def set_hyperparameters(self, params): self.settings_plugin.set_hyperparameter(params) def set_loss_function(self, func): self._solver_plugin.set_blackbox_function(func) def run(self): if not ProjectManager.is_ready(): LOG.error("No config data found to initialize PluginSetting object") raise IOError("No config data found to initialize PluginSetting object") self.settings_plugin.set_hyperparameter(ProjectManager.get_hyperparameter()) self._solver_plugin.settings = self.settings_plugin self._solver_plugin.run() def save_results(self, savedir=None, savename=None, show=False): df, best = self.get_results() dir = None if savename is None: savename = LIBNAME if savedir is None: if 'output_dir' in ProjectManager.__dict__.keys(): if not os.path.isdir(ProjectManager.output_dir): os.mkdir(ProjectManager.output_dir) dir = ProjectManager.output_dir else: print("WARNING: No solver option output_dir found, cannot save results!") LOG.warning("WARNING: No solver option output_dir found, cannot save results!") else: dir = savedir if not os.path.isdir(savedir): os.mkdir(savedir) - tstr = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") - name = savename + "_all_" + tstr + ".csv" + appendix = ProjectManager.identifier(True) + name = savename + "_all_" + appendix + ".csv" fname_all = os.path.join(dir, name) df.to_csv(fname_all) - name = savename + "_best_" + tstr + ".txt" + name = savename + "_best_" + appendix + ".txt" fname_best = os.path.join(dir, name) with open(fname_best, "w") as text_file: for item in best.items(): text_file.write("{}\t:\t{}\n".format(item[0], item[1])) if show: viewer = ResultViewer(fname_all) viewer.show() else: viewer = ResultViewer(fname_all, save_only=True) viewer.show() def get_results(self): results, best = self._solver_plugin.get_results() df = pd.DataFrame.from_dict(results) return df, best @property def is_ready(self): return self._solver_plugin is not None and self.settings_plugin is not None @property def solver_plugin(self): return self._solver_plugin @solver_plugin.setter def solver_plugin(self, value): self._solver_plugin = value @property def settings_plugin(self): return self._settings_plugin @settings_plugin.setter def settings_plugin(self, value): self._settings_plugin = value @property def name(self): return self._name @name.setter def name(self, value): if not isinstance(value, str): msg = "Invalid input, str type expected for value, got {} instead".format(type(value)) LOG.error(msg) raise IOError(msg) self._name = value diff --git a/hyppopy/solverfactory.py b/hyppopy/solverfactory.py index fb053b6..80ce478 100644 --- a/hyppopy/solverfactory.py +++ b/hyppopy/solverfactory.py @@ -1,165 +1,166 @@ # 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) from yapsy.PluginManager import PluginManager from hyppopy.projectmanager import ProjectManager from hyppopy.globals import PLUGIN_DEFAULT_DIR from hyppopy.deepdict import DeepDict from hyppopy.solver import Solver from hyppopy.singleton import * import os import logging from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) @singleton_object class SolverFactory(metaclass=Singleton): """ This class is responsible for grabbing all plugins from the plugin folder arranging them into a Solver class instances. These Solver class instances can be requested from the factory via the get_solver method. The SolverFactory class is a Singleton class, so try not to instantiate it using SolverFactory(), the consequences will be horrific. Instead use is like a class having static functions only, SolverFactory.method(). """ _plugin_dirs = [] _plugins = {} def __init__(self): self.reset() self.load_plugins() LOG.debug("Solverfactory initialized") def load_plugins(self): """ Load plugin modules from plugin paths """ LOG.debug("load_plugins()") manager = PluginManager() LOG.debug("setPluginPlaces(" + " ".join(map(str, self._plugin_dirs))) manager.setPluginPlaces(self._plugin_dirs) manager.collectPlugins() + print("") for plugin in manager.getAllPlugins(): name_elements = plugin.plugin_object.__class__.__name__.split("_") LOG.debug("found plugin " + " ".join(map(str, name_elements))) - print("Solverfactory: found plugins " + " ".join(map(str, name_elements))) + print("Hyppopy: found plugin " + " ".join(map(str, name_elements))) if len(name_elements) != 2 or ("Solver" not in name_elements and "Settings" not in name_elements): msg = "invalid plugin class naming for class {}, the convention is libname_Solver or libname_Settings.".format(plugin.plugin_object.__class__.__name__) LOG.error(msg) raise NameError(msg) if name_elements[0] not in self._plugins.keys(): self._plugins[name_elements[0]] = Solver() self._plugins[name_elements[0]].name = name_elements[0] if name_elements[1] == "Solver": try: obj = plugin.plugin_object.__class__() obj.name = name_elements[0] self._plugins[name_elements[0]].solver_plugin = obj LOG.info("plugin: {} Solver loaded".format(name_elements[0])) except Exception as e: msg = "failed to instanciate class {}".format(plugin.plugin_object.__class__.__name__) LOG.error(msg) raise ImportError(msg) elif name_elements[1] == "Settings": try: obj = plugin.plugin_object.__class__() obj.name = name_elements[0] self._plugins[name_elements[0]].settings_plugin = obj LOG.info("plugin: {} ParameterSpace loaded".format(name_elements[0])) except Exception as e: msg = "failed to instanciate class {}".format(plugin.plugin_object.__class__.__name__) LOG.error(msg) raise ImportError(msg) else: msg = "failed loading plugin {}, please check if naming conventions are kept!".format(name_elements[0]) LOG.error(msg) raise IOError(msg) if len(self._plugins) == 0: msg = "no plugins found, please check your plugin folder names or your plugin scripts for errors!" LOG.error(msg) raise IOError(msg) def reset(self): """ Reset solver factory """ LOG.debug("reset()") self._plugins = {} self._plugin_dirs = [] self.add_plugin_dir(os.path.abspath(PLUGIN_DEFAULT_DIR)) def add_plugin_dir(self, dir): """ Add plugin directory """ LOG.debug("add_plugin_dir({})".format(dir)) self._plugin_dirs.append(dir) def list_solver(self): """ list all solvers available :return: [list(str)] """ return list(self._plugins.keys()) def from_settings(self, settings): if isinstance(settings, str): if not os.path.isfile(settings): LOG.error("input error, file {} not found!".format(settings)) if not ProjectManager.read_config(settings): LOG.error("failed to read config in ProjectManager!") return None else: if not ProjectManager.set_config(settings): LOG.error("failed to set config in ProjectManager!") return None if not ProjectManager.is_ready(): LOG.error("failed to set config in ProjectManager!") return None try: solver = self.get_solver(ProjectManager.use_plugin) except Exception as e: msg = "failed to create solver, reason {}".format(e) LOG.error(msg) return None return solver def get_solver(self, name=None): """ returns a solver by name tag :param name: [str] solver name :return: [Solver] instance """ if name is None: try: name = ProjectManager.use_plugin except Exception as e: - msg = "failed to setup solver, no solver specified, check your ProjectManager for the use_plugin value!" + msg = "failed to setup solver, no solver specified, check your ProjectManager for the use_plugin value! Error {}".format(e) LOG.error(msg) raise LookupError(msg) if not isinstance(name, str): msg = "Invalid input, str type expected for name, got {} instead".format(type(name)) LOG.error(msg) raise IOError(msg) if name not in self.list_solver(): msg = "failed solver request, a solver called {} is not available, check for typo or if your plugin failed while loading!".format(name) LOG.error(msg) raise LookupError(msg) LOG.debug("get_solver({})".format(name)) return self._plugins[name] diff --git a/hyppopy/tests/data/Iris/svc_config.xml b/hyppopy/tests/data/Iris/knc_config.xml similarity index 62% copy from hyppopy/tests/data/Iris/svc_config.xml copy to hyppopy/tests/data/Iris/knc_config.xml index 2495f60..eb253bb 100644 --- a/hyppopy/tests/data/Iris/svc_config.xml +++ b/hyppopy/tests/data/Iris/knc_config.xml @@ -1,36 +1,36 @@ - + uniform - [0,20] - float - - + [1,50] + int + + uniform - [0.0001,20.0] - float - - + [1,100] + int + + categorical - [linear,sigmoid,poly,rbf] + [uniform,distance] str - - + + categorical - [ovo,ovr] + [auto,ball_tree,kd_tree,brute] str - + - + 3 hyperopt D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris train_data.npy train_labels.npy \ No newline at end of file diff --git a/hyppopy/tests/data/Iris/svc_config.xml b/hyppopy/tests/data/Iris/lda_config.xml similarity index 50% copy from hyppopy/tests/data/Iris/svc_config.xml copy to hyppopy/tests/data/Iris/lda_config.xml index 2495f60..b6b2fc4 100644 --- a/hyppopy/tests/data/Iris/svc_config.xml +++ b/hyppopy/tests/data/Iris/lda_config.xml @@ -1,36 +1,26 @@ - - uniform - [0,20] - float - - - uniform - [0.0001,20.0] - float - - - categorical - [linear,sigmoid,poly,rbf] - str - - + categorical - [ovo,ovr] + [svd,lsqr,eigen] str - + + + uniform + [0.0,1.0] + float + - + 3 hyperopt D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris - train_data.npy - train_labels.npy + train_cleaned.csv + Survived \ No newline at end of file diff --git a/hyppopy/tests/data/Iris/rf_config.json b/hyppopy/tests/data/Iris/rf_config.json index 59ecd73..baa11c3 100644 --- a/hyppopy/tests/data/Iris/rf_config.json +++ b/hyppopy/tests/data/Iris/rf_config.json @@ -1,44 +1,44 @@ {"hyperparameter": { "n_estimators": { "domain": "uniform", "data": "[3,500]", "type": "int" }, "criterion": { "domain": "categorical", "data": "[gini,entropy]", "type": "str" }, "max_depth": { "domain": "uniform", "data": "[3, 50]", "type": "int" }, "min_samples_split": { "domain": "uniform", "data": "[0.0001,1]", "type": "float" }, "min_samples_leaf": { "domain": "uniform", "data": "[0.0001,0.5]", "type": "float" }, "max_features": { "domain": "categorical", "data": "[auto,sqrt,log2]", "type": "str" } }, "settings": { - "solver": { + "solver_plugin": { "max_iterations": "3", "use_plugin" : "optunity", "output_dir": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris" }, "custom": { "data_path": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris", "data_name": "train_data.npy", "labels_name": "train_labels.npy" } }} \ No newline at end of file diff --git a/hyppopy/tests/data/Iris/rf_config.xml b/hyppopy/tests/data/Iris/rf_config.xml index 516eb43..23c7747 100644 --- a/hyppopy/tests/data/Iris/rf_config.xml +++ b/hyppopy/tests/data/Iris/rf_config.xml @@ -1,46 +1,46 @@ uniform [3,200] int categorical [gini,entropy] str uniform [3, 50] int uniform [0.0001,1] float uniform [0.0001,0.5] float categorical [auto,sqrt,log2] str - + 3 optunity D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris train_data.npy train_labels.npy \ No newline at end of file diff --git a/hyppopy/tests/data/Iris/svc_config.json b/hyppopy/tests/data/Iris/svc_config.json index 2ed1c8d..02c4fd4 100644 --- a/hyppopy/tests/data/Iris/svc_config.json +++ b/hyppopy/tests/data/Iris/svc_config.json @@ -1,34 +1,34 @@ {"hyperparameter": { "C": { "domain": "uniform", "data": "[0,20]", "type": "float" }, "gamma": { "domain": "uniform", "data": "[0.0001,20.0]", "type": "float" }, "kernel": { "domain": "categorical", "data": "[linear, sigmoid, poly, rbf]", "type": "str" }, "decision_function_shape": { "domain": "categorical", "data": "[ovo,ovr]", "type": "str" } }, "settings": { - "solver": { + "solver_plugin": { "max_iterations": "3", "use_plugin" : "optunity", "output_dir": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris" }, "custom": { "data_path": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris", "data_name": "train_data.npy", "labels_name": "train_labels.npy" } }} \ No newline at end of file diff --git a/hyppopy/tests/data/Iris/svc_config.xml b/hyppopy/tests/data/Iris/svc_config.xml index 2495f60..cc3bbca 100644 --- a/hyppopy/tests/data/Iris/svc_config.xml +++ b/hyppopy/tests/data/Iris/svc_config.xml @@ -1,36 +1,36 @@ uniform [0,20] float uniform [0.0001,20.0] float categorical [linear,sigmoid,poly,rbf] str categorical [ovo,ovr] str - + 3 hyperopt D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Iris train_data.npy train_labels.npy \ No newline at end of file diff --git a/hyppopy/tests/data/Titanic/svc_config.xml b/hyppopy/tests/data/Titanic/knc_config.xml similarity index 58% copy from hyppopy/tests/data/Titanic/svc_config.xml copy to hyppopy/tests/data/Titanic/knc_config.xml index 60ed08e..641beb6 100644 --- a/hyppopy/tests/data/Titanic/svc_config.xml +++ b/hyppopy/tests/data/Titanic/knc_config.xml @@ -1,36 +1,36 @@ - + uniform - [0,20] - float - - + [1,50] + int + + uniform - [0.0001,20.0] - float - - + [1,100] + int + + categorical - [linear,sigmoid,poly,rbf] + [uniform,distance] str - - + + categorical - [ovo,ovr] + [auto,ball_tree,kd_tree,brute] str - + - + 3 - optunity + hyperopt D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic train_cleaned.csv Survived \ No newline at end of file diff --git a/hyppopy/tests/data/Titanic/rf_config.xml b/hyppopy/tests/data/Titanic/lda_config.xml similarity index 62% copy from hyppopy/tests/data/Titanic/rf_config.xml copy to hyppopy/tests/data/Titanic/lda_config.xml index a812834..556ff45 100644 --- a/hyppopy/tests/data/Titanic/rf_config.xml +++ b/hyppopy/tests/data/Titanic/lda_config.xml @@ -1,31 +1,26 @@ - - uniform - [3,200] - int - - + categorical - [gini,entropy] + [svd,lsqr,eigen] str - - + + uniform - [3, 50] - int - + [0.0,1.0] + float + - + 3 - optunity + hyperopt D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic train_cleaned.csv Survived \ No newline at end of file diff --git a/hyppopy/tests/data/Titanic/rf_config.json b/hyppopy/tests/data/Titanic/rf_config.json index af66f3a..1d77e50 100644 --- a/hyppopy/tests/data/Titanic/rf_config.json +++ b/hyppopy/tests/data/Titanic/rf_config.json @@ -1,29 +1,29 @@ {"hyperparameter": { "n_estimators": { "domain": "uniform", "data": "[3,500]", "type": "int" }, "criterion": { "domain": "categorical", "data": "[gini,entropy]", "type": "str" }, "max_depth": { "domain": "uniform", "data": "[3, 50]", "type": "int" } }, "settings": { - "solver": { + "solver_plugin": { "max_iterations": "3", "use_plugin" : "optunity", "output_dir": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic" }, "custom": { "data_path": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic", "data_name": "train_cleaned.csv", "labels_name": "Survived" } }} \ No newline at end of file diff --git a/hyppopy/tests/data/Titanic/rf_config.xml b/hyppopy/tests/data/Titanic/rf_config.xml index a812834..3585336 100644 --- a/hyppopy/tests/data/Titanic/rf_config.xml +++ b/hyppopy/tests/data/Titanic/rf_config.xml @@ -1,31 +1,31 @@ uniform [3,200] int categorical [gini,entropy] str uniform [3, 50] int - + 3 optunity D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic train_cleaned.csv Survived \ No newline at end of file diff --git a/hyppopy/tests/data/Titanic/svc_config.json b/hyppopy/tests/data/Titanic/svc_config.json index 98eced5..947ed37 100644 --- a/hyppopy/tests/data/Titanic/svc_config.json +++ b/hyppopy/tests/data/Titanic/svc_config.json @@ -1,34 +1,34 @@ {"hyperparameter": { "C": { "domain": "uniform", "data": "[0,20]", "type": "float" }, "gamma": { "domain": "uniform", "data": "[0.0001,20.0]", "type": "float" }, "kernel": { "domain": "categorical", "data": "[linear, sigmoid, poly, rbf]", "type": "str" }, "decision_function_shape": { "domain": "categorical", "data": "[ovo,ovr]", "type": "str" } }, "settings": { - "solver": { + "solver_plugin": { "max_iterations": "3", "use_plugin" : "hyperopt", "output_dir": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic" }, "custom": { "data_path": "D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic", "data_name": "train_cleaned.csv", "labels_name": "Survived" } }} \ No newline at end of file diff --git a/hyppopy/tests/data/Titanic/svc_config.xml b/hyppopy/tests/data/Titanic/svc_config.xml index 60ed08e..094fcd1 100644 --- a/hyppopy/tests/data/Titanic/svc_config.xml +++ b/hyppopy/tests/data/Titanic/svc_config.xml @@ -1,36 +1,36 @@ uniform [0,20] float uniform [0.0001,20.0] float categorical [linear,sigmoid,poly,rbf] str categorical [ovo,ovr] str - + 3 optunity D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic - + D:/Projects/Python/hyppopy/hyppopy/tests/data/Titanic train_cleaned.csv Survived \ No newline at end of file diff --git a/hyppopy/tests/data/iris_svc_parameter.json b/hyppopy/tests/data/iris_svc_parameter.json index eb60183..df34b09 100644 --- a/hyppopy/tests/data/iris_svc_parameter.json +++ b/hyppopy/tests/data/iris_svc_parameter.json @@ -1,26 +1,26 @@ {"hyperparameter": { "C": { "domain": "uniform", "data": "[0,20]", "type": "float" }, "gamma": { "domain": "uniform", "data": "[0.0001,20.0]", "type": "float" }, "kernel": { "domain": "categorical", "data": "[linear, sigmoid, poly, rbf]", "type": "str" } }, "settings": { - "solver": { + "solver_plugin": { "max_iterations": "50", "use_plugin" : "hyperopt" }, "custom": { "data_path": "C:/path/to/my/data" } }} \ No newline at end of file diff --git a/hyppopy/tests/data/iris_svc_parameter.xml b/hyppopy/tests/data/iris_svc_parameter.xml index 88cbc8c..e01bae0 100644 --- a/hyppopy/tests/data/iris_svc_parameter.xml +++ b/hyppopy/tests/data/iris_svc_parameter.xml @@ -1,28 +1,28 @@ uniform [0,20] float uniform [0.0001,20.0] float categorical [linear,sigmoid,poly,rbf] str - + 50 optunity - + C:/path/to/my/data \ No newline at end of file diff --git a/hyppopy/tests/test_deepdict.py b/hyppopy/tests/test_deepdict.py index fc5efe8..29e5de7 100644 --- a/hyppopy/tests/test_deepdict.py +++ b/hyppopy/tests/test_deepdict.py @@ -1,163 +1,163 @@ # 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 unittest from hyppopy.deepdict import DeepDict DATA_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data") class DeepDictTestSuite(unittest.TestCase): def setUp(self): self.test_data = { 'widget': { 'debug': 'on', 'image': {'alignment': 'center', 'hOffset': 250, 'name': 'sun1', 'src': 'Images/Sun.png', 'vOffset': 250}, 'text': {'alignment': 'center', 'data': 'Click Here', 'hOffset': 250, 'name': 'text1', 'onMouseUp': 'sun1.opacity = (sun1.opacity / 100) * 90;', 'size': 36, 'style': 'bold', 'vOffset': 100}, 'window': {'height': 500, 'name': 'main_window', 'title': 'Sample Konfabulator Widget', 'width': 500} } } self.test_data2 = {"test": { "section": { "var1": 100, "var2": 200 } }} def test_fileIO(self): dd_json = DeepDict(os.path.join(DATA_PATH, 'test_json.json')) dd_xml = DeepDict(os.path.join(DATA_PATH, 'test_xml.xml')) dd_dict = DeepDict(self.test_data) self.assertTrue(list(self.test_data.keys())[0] == list(dd_json.data.keys())[0]) self.assertTrue(list(self.test_data.keys())[0] == list(dd_xml.data.keys())[0]) self.assertTrue(list(self.test_data.keys())[0] == list(dd_dict.data.keys())[0]) for key in self.test_data['widget'].keys(): self.assertTrue(self.test_data['widget'][key] == dd_json.data['widget'][key]) self.assertTrue(self.test_data['widget'][key] == dd_xml.data['widget'][key]) self.assertTrue(self.test_data['widget'][key] == dd_dict.data['widget'][key]) for key in self.test_data['widget'].keys(): if key == 'debug': self.assertTrue(dd_json.data['widget']["debug"] == "on") self.assertTrue(dd_xml.data['widget']["debug"] == "on") self.assertTrue(dd_dict.data['widget']["debug"] == "on") else: for key2, value2 in self.test_data['widget'][key].items(): self.assertTrue(value2 == dd_json.data['widget'][key][key2]) self.assertTrue(value2 == dd_xml.data['widget'][key][key2]) self.assertTrue(value2 == dd_dict.data['widget'][key][key2]) dd_dict.to_file(os.path.join(DATA_PATH, 'write_to_json_test.json')) dd_dict.to_file(os.path.join(DATA_PATH, 'write_to_xml_test.xml')) self.assertTrue(os.path.isfile(os.path.join(DATA_PATH, 'write_to_json_test.json'))) self.assertTrue(os.path.isfile(os.path.join(DATA_PATH, 'write_to_xml_test.xml'))) dd_json = DeepDict(os.path.join(DATA_PATH, 'write_to_json_test.json')) dd_xml = DeepDict(os.path.join(DATA_PATH, 'write_to_xml_test.xml')) self.assertTrue(dd_json == dd_dict) self.assertTrue(dd_xml == dd_dict) try: os.remove(os.path.join(DATA_PATH, 'write_to_json_test.json')) os.remove(os.path.join(DATA_PATH, 'write_to_xml_test.xml')) except Exception as e: print(e) print("Warning: Failed to delete temporary data during tests!") def test_has_section(self): dd = DeepDict(self.test_data) self.assertTrue(dd.has_section('hOffset')) self.assertTrue(dd.has_section('window')) self.assertTrue(dd.has_section('widget')) self.assertTrue(dd.has_section('style')) self.assertTrue(dd.has_section('window')) self.assertTrue(dd.has_section('title')) self.assertFalse(dd.has_section('notasection')) def test_data_access(self): dd = DeepDict(self.test_data) self.assertEqual(dd['widget/window/height'], 500) self.assertEqual(dd['widget/image/name'], 'sun1') self.assertTrue(isinstance(dd['widget/window'], dict)) self.assertEqual(len(dd['widget/window']), 4) dd = DeepDict(path_sep=".") dd.data = self.test_data self.assertEqual(dd['widget.window.height'], 500) self.assertEqual(dd['widget.image.name'], 'sun1') self.assertTrue(isinstance(dd['widget.window'], dict)) self.assertEqual(len(dd['widget.window']), 4) def test_data_adding(self): dd = DeepDict() dd["test/section/var1"] = 100 dd["test/section/var2"] = 200 self.assertTrue(dd.data == self.test_data2) dd = DeepDict() dd["test"] = {} dd["test/section"] = {} dd["test/section/var1"] = 100 dd["test/section/var2"] = 200 self.assertTrue(dd.data == self.test_data2) def test_sample_space(self): dd = DeepDict(os.path.join(DATA_PATH, 'test_paramset.json')) self.assertEqual(len(dd[['parameter', 'activation', 'data']]), 4) self.assertEqual(dd['parameter/activation/data'], ['ReLU', 'tanh', 'sigm', 'ELU']) self.assertTrue(isinstance(dd['parameter/activation/data'], list)) self.assertTrue(isinstance(dd['parameter/activation/data'][0], str)) self.assertEqual(dd['parameter/layerdepth/data'], [3, 20]) self.assertTrue(isinstance(dd['parameter/layerdepth/data'], list)) self.assertTrue(isinstance(dd['parameter/layerdepth/data'][0], int)) self.assertTrue(isinstance(dd['parameter/learningrate/data'][0], float)) self.assertEqual(dd['parameter/learningrate/data'][0], 1e-5) self.assertEqual(dd['parameter/learningrate/data'][1], 10.0) def test_len(self): dd = DeepDict(os.path.join(DATA_PATH, 'test_paramset.json')) self.assertEqual(len(dd), 1) def test_setattr(self): dd = DeepDict(os.path.join(DATA_PATH, 'iris_svc_parameter.xml')) class Foo(object): def __init__(self): pass foo = Foo - dd.transfer_attrs(foo, 'solver') + dd.transfer_attrs(foo, 'solver_plugin') self.assertEqual(foo.max_iterations, 50) self.assertEqual(foo.use_plugin, 'optunity') if __name__ == '__main__': unittest.main() diff --git a/hyppopy/__init__.py b/hyppopy/workflows/knc_usecase/__init__.py similarity index 100% copy from hyppopy/__init__.py copy to hyppopy/workflows/knc_usecase/__init__.py diff --git a/hyppopy/workflows/svc_usecase/svc_usecase.py b/hyppopy/workflows/knc_usecase/knc_usecase.py similarity index 87% copy from hyppopy/workflows/svc_usecase/svc_usecase.py copy to hyppopy/workflows/knc_usecase/knc_usecase.py index 9f7800a..d59db98 100644 --- a/hyppopy/workflows/svc_usecase/svc_usecase.py +++ b/hyppopy/workflows/knc_usecase/knc_usecase.py @@ -1,38 +1,35 @@ # 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 numpy as np -import pandas as pd -from sklearn.svm import SVC +from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import cross_val_score from hyppopy.projectmanager import ProjectManager from hyppopy.workflows.workflowbase import WorkflowBase from hyppopy.workflows.dataloader.simpleloader import SimpleDataLoader -class svc_usecase(WorkflowBase): +class knc_usecase(WorkflowBase): def setup(self, **kwargs): dl = SimpleDataLoader() dl.start(path=ProjectManager.data_path, data_name=ProjectManager.data_name, labels_name=ProjectManager.labels_name) self.solver.set_data(dl.data) def blackbox_function(self, data, params): - clf = SVC(**params) + clf = KNeighborsClassifier(**params) return -cross_val_score(estimator=clf, X=data[0], y=data[1], cv=3).mean() diff --git a/hyppopy/__init__.py b/hyppopy/workflows/lda_usecase/__init__.py similarity index 100% copy from hyppopy/__init__.py copy to hyppopy/workflows/lda_usecase/__init__.py diff --git a/hyppopy/workflows/svc_usecase/svc_usecase.py b/hyppopy/workflows/lda_usecase/lda_usecase.py similarity index 86% copy from hyppopy/workflows/svc_usecase/svc_usecase.py copy to hyppopy/workflows/lda_usecase/lda_usecase.py index 9f7800a..2d8e646 100644 --- a/hyppopy/workflows/svc_usecase/svc_usecase.py +++ b/hyppopy/workflows/lda_usecase/lda_usecase.py @@ -1,38 +1,35 @@ # 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 numpy as np -import pandas as pd -from sklearn.svm import SVC +from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.model_selection import cross_val_score from hyppopy.projectmanager import ProjectManager from hyppopy.workflows.workflowbase import WorkflowBase from hyppopy.workflows.dataloader.simpleloader import SimpleDataLoader -class svc_usecase(WorkflowBase): +class lda_usecase(WorkflowBase): def setup(self, **kwargs): dl = SimpleDataLoader() dl.start(path=ProjectManager.data_path, data_name=ProjectManager.data_name, labels_name=ProjectManager.labels_name) self.solver.set_data(dl.data) def blackbox_function(self, data, params): - clf = SVC(**params) + clf = LinearDiscriminantAnalysis(**params) return -cross_val_score(estimator=clf, X=data[0], y=data[1], cv=3).mean() diff --git a/hyppopy/workflows/svc_usecase/svc_usecase.py b/hyppopy/workflows/svc_usecase/svc_usecase.py index 9f7800a..0e63f3f 100644 --- a/hyppopy/workflows/svc_usecase/svc_usecase.py +++ b/hyppopy/workflows/svc_usecase/svc_usecase.py @@ -1,38 +1,35 @@ # 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 numpy as np -import pandas as pd from sklearn.svm import SVC from sklearn.model_selection import cross_val_score from hyppopy.projectmanager import ProjectManager from hyppopy.workflows.workflowbase import WorkflowBase from hyppopy.workflows.dataloader.simpleloader import SimpleDataLoader class svc_usecase(WorkflowBase): def setup(self, **kwargs): dl = SimpleDataLoader() dl.start(path=ProjectManager.data_path, data_name=ProjectManager.data_name, labels_name=ProjectManager.labels_name) self.solver.set_data(dl.data) def blackbox_function(self, data, params): clf = SVC(**params) return -cross_val_score(estimator=clf, X=data[0], y=data[1], cv=3).mean() diff --git a/hyppopy/workflows/workflowbase.py b/hyppopy/workflows/workflowbase.py index 6b14ada..f991036 100644 --- a/hyppopy/workflows/workflowbase.py +++ b/hyppopy/workflows/workflowbase.py @@ -1,62 +1,67 @@ # -*- 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) from hyppopy.deepdict import DeepDict from hyppopy.solverfactory import SolverFactory from hyppopy.projectmanager import ProjectManager from hyppopy.globals import SETTINGSCUSTOMPATH, SETTINGSSOLVERPATH import os import abc import logging from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) class WorkflowBase(object): def __init__(self): self._solver = SolverFactory.get_solver() def run(self, save=True): self.setup() self.solver.set_loss_function(self.blackbox_function) self.solver.run() if save: self.solver.save_results() self.test() def get_results(self): return self.solver.get_results() + def save_results(self, savedir=None, show=False): + if savedir is None: + savedir = ProjectManager.output_dir + return self.solver.save_results(savedir=savedir, show=show) + @abc.abstractmethod def setup(self, **kwargs): raise NotImplementedError('the user has to implement this function') @abc.abstractmethod def blackbox_function(self): raise NotImplementedError('the user has to implement this function') @abc.abstractmethod def test(self): pass @property def solver(self): return self._solver diff --git a/requirements.txt b/requirements.txt index 7688b4f..444d9f3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,16 +1,13 @@ -dicttoxml==1.7.4 -hyperopt==0.1.1 -matplotlib==3.0.2 -numpy==1.16.0 -Optunity==1.1.1 -pytest==4.1.1 -scikit-learn==0.20.2 -scipy==1.2.0 -sklearn==0.0 -Sphinx==1.8.3 -xmlrunner==1.7.7 -xmltodict==0.11.0 -Yapsy==1.11.223 -pandas==0.24.1 -medpy==0.3.0 -batchgenerators==0.18.1 \ No newline at end of file +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 \ No newline at end of file diff --git a/setup.py b/setup.py index 9221d46..3d270a8 100644 --- a/setup.py +++ b/setup.py @@ -1,39 +1,61 @@ # -*- 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.1.1dev" + +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='0.0.1', + 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=('bin', '*test*', 'doc', 'hyppopy')), + 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', 'hyperopt>=0.1.1', 'matplotlib>=3.0.2', 'numpy>=1.16.0', - 'Optunity>=1.1.1', 'pytest>=4.1.1', 'scikit-learn>=0.20.2', 'scipy>=1.2.0', 'sklearn>=0.0', 'Sphinx>=1.8.3', - 'xmlrunner>=1.7.7', 'xmltodict>=0.11.0', 'Yapsy>=1.11.223', 'visdom>=0.1.8.8'], - # a more sophisticated project might have something like: - #install_requires=['numpy>=1.11.0', 'scipy>=0.17', 'scikit-learn'] - - # after running setup.py, you will be able to call hypopy_exe - # from the console as if it was a normal binary. It will call the function - # main in bin/hypopy_exe.py - entry_points={ - 'console_scripts': ['hyppopy_exe=bin.hypopy_exe:main'], - } + 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' + ], )