diff --git a/hyppopy/settingsparticle.py b/hyppopy/settingsparticle.py index b083300..60e9ce4 100644 --- a/hyppopy/settingsparticle.py +++ b/hyppopy/settingsparticle.py @@ -1,88 +1,86 @@ -# -*- 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 abc import logging from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) class SettingsParticle(object): domains = ["uniform", "loguniform", "normal", "categorical"] _name = None _domain = None _dtype = None _data = None def __init__(self, name=None, domain=None, dtype=None, data=None): if name is not None: self.name = name if domain is not None: self.domain = domain if dtype is not None: self.dtype = dtype if data is not None: self.data = data @abc.abstractmethod def convert(self): raise NotImplementedError("the user has to implement this function") def get(self): msg = None if self.name is None: msg = "cannot convert unnamed parameter" if self.domain is None: msg = "cannot convert parameter of empty domain" if self.dtype is None: msg = "cannot convert parameter with unknown dtype" if self.data is None: msg = "cannot convert parameter having no data" if msg is not None: LOG.error(msg) raise LookupError(msg) return self.convert() @property def name(self): return self._name @name.setter def name(self, value): self._name = value @property def domain(self): return self._domain @domain.setter def domain(self, value): self._domain = value @property def dtype(self): return self._dtype @dtype.setter def dtype(self, value): self._dtype = value @property def data(self): return self._data @data.setter def data(self, value): self._data = value diff --git a/hyppopy/settingspluginbase.py b/hyppopy/settingspluginbase.py index f732e58..94cc169 100644 --- a/hyppopy/settingspluginbase.py +++ b/hyppopy/settingspluginbase.py @@ -1,78 +1,76 @@ # 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 abc import os import copy import logging from hyppopy.globals import DEBUGLEVEL LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) -from hyppopy.globals import SETTINGSSOLVERPATH, SETTINGSCUSTOMPATH - from hyppopy.deepdict import DeepDict class SettingsPluginBase(object): _data = None _name = None def __init__(self): self._data = {} @abc.abstractmethod def convert_parameter(self): raise NotImplementedError('users must define convert_parameter to use this base class') def get_hyperparameter(self): return self.convert_parameter(self.data) def set_hyperparameter(self, input_data): self.data.clear() self.data = copy.deepcopy(input_data) def read(self, fname): self.data.clear() self.data.from_file(fname) def write(self, fname): self.data.to_file(fname) @property def data(self): return self._data @data.setter def data(self, value): if isinstance(value, dict): self._data = value elif isinstance(value, DeepDict): self._data = value.data else: raise IOError("unexpected input type({}) for data, needs to be of type dict or DeepDict!".format(type(value))) @property def name(self): return self._name @name.setter def name(self, value): if not isinstance(value, str): LOG.error("Invalid input, str type expected for value, got {} instead".format(type(value))) raise IOError("Invalid input, str type expected for value, got {} instead".format(type(value))) self._name = value diff --git a/hyppopy/solverpluginbase.py b/hyppopy/solverpluginbase.py index a58dd94..97485a5 100644 --- a/hyppopy/solverpluginbase.py +++ b/hyppopy/solverpluginbase.py @@ -1,85 +1,93 @@ # 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 abc import os import time import logging from hyppopy.globals import DEBUGLEVEL from hyppopy.settingspluginbase import SettingsPluginBase LOG = logging.getLogger(os.path.basename(__file__)) LOG.setLevel(DEBUGLEVEL) class SolverPluginBase(object): - data = None - loss = None + _data = None + _loss = None _settings = None _name = None def __init__(self): pass @abc.abstractmethod def loss_function(self, params): raise NotImplementedError('users must define loss_func to use this base class') @abc.abstractmethod def execute_solver(self): raise NotImplementedError('users must define execute_solver to use this base class') @abc.abstractmethod def convert_results(self): raise NotImplementedError('users must define convert_results to use this base class') def set_data(self, data): - self.data = data + self._data = data def set_loss_function(self, func): - self.loss = func + self._loss = func def get_results(self): return self.convert_results() def run(self): self.execute_solver(self.settings.get_hyperparameter()) + @property + def data(self): + return self._data + + @property + def loss(self): + return self._loss + @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 @property def settings(self): return self._settings @settings.setter def settings(self, value): if not isinstance(value, SettingsPluginBase): msg = "Invalid input, SettingsPluginBase type expected for value, got {} instead".format(type(value)) LOG.error(msg) raise IOError(msg) self._settings = value