From 7e582b5e8653e7dbed8c23c174c9067358c73ebe Mon Sep 17 00:00:00 2001 From: Carlos Sevilla Salcedo <22427519+sevisal@users.noreply.github.com> Date: Tue, 24 Oct 2023 13:14:50 +0200 Subject: [PATCH] Change plugin templates (#116) * Change plugin templates * Remove test * Fix issue with environment modules * Add datatype and remove innecesary variables * Add default type * Fix warning * Fixed minor warning issues --- src/vai_lab/Core/vai_lab_core.py | 8 +- .../DataProcessing/DataProcessing_core.py | 22 ++--- .../DataProcessing/plugins/binarizer.py | 15 ++- .../plugins/kbinsdiscretizer.py | 15 ++- .../DataProcessing/plugins/labelbinarizer.py | 15 ++- .../DataProcessing/plugins/labelencoder.py | 15 ++- .../DataProcessing/plugins/maxabsscaler.py | 15 ++- .../DataProcessing/plugins/minmaxscaler.py | 15 ++- .../plugins/multilabelbinarizer.py | 15 ++- .../DataProcessing/plugins/normalizer.py | 15 ++- .../DataProcessing/plugins/onehotencoder.py | 15 ++- .../DataProcessing/plugins/ordinalencoder.py | 15 ++- .../plugins/polynomialfeatures.py | 15 ++- .../plugins/quantiletransformer.py | 15 ++- .../DataProcessing/plugins/standardscaler.py | 15 ++- .../DecisionMaking/DecisionMaking_core.py | 28 +++--- .../plugins/BayesianOptimisation(GPy).py | 15 ++- .../BayesianOptimisation(bayes_opt).py | 15 ++- src/vai_lab/Environment/Environment_core.py | 13 +-- .../Environment/plugins/PyBulletEnv.py | 8 +- src/vai_lab/InputData/InputData_core.py | 9 +- src/vai_lab/Modelling/Modelling_core.py | 25 +++-- .../Modelling/plugins/affinitypropagation.py | 15 ++- .../Modelling/plugins/bayesianridge.py | 17 +++- src/vai_lab/Modelling/plugins/birch.py | 15 ++- .../plugins/decisiontreeclassifier.py | 17 +++- .../plugins/decisiontreeregressor.py | 16 ++- src/vai_lab/Modelling/plugins/elasticnet.py | 16 ++- src/vai_lab/Modelling/plugins/gpclassifier.py | 17 +++- src/vai_lab/Modelling/plugins/gpregressor.py | 16 ++- src/vai_lab/Modelling/plugins/kernelridge.py | 16 ++- src/vai_lab/Modelling/plugins/kmeans.py | 16 ++- .../Modelling/plugins/knnclassifier.py | 17 +++- src/vai_lab/Modelling/plugins/knnregressor.py | 16 ++- src/vai_lab/Modelling/plugins/lasso.py | 16 ++- .../Modelling/plugins/linearregression.py | 16 ++- .../Modelling/plugins/logisticregression.py | 17 +++- src/vai_lab/Modelling/plugins/meanshift.py | 15 ++- .../plugins/passiveaggressiveclassifier.py | 17 +++- src/vai_lab/Modelling/plugins/perceptron.py | 17 +++- .../plugins/randomforestclassifier.py | 17 +++- .../plugins/randomforestregressor.py | 16 ++- .../Modelling/plugins/ridgeregression.py | 16 ++- src/vai_lab/Modelling/plugins/svc.py | 17 +++- src/vai_lab/Modelling/plugins/svr.py | 16 ++- src/vai_lab/_plugin_templates.py | 92 ++++++++---------- src/vai_lab/examples/results/output.pkl | Bin 5769 -> 7042 bytes .../xml_files/k-mean_clustering_demo.xml | 7 ++ src/vai_lab/tests/test_examples.py | 5 +- src/vai_lab/utils/plugins/progressTracker.py | 8 +- 50 files changed, 634 insertions(+), 190 deletions(-) diff --git a/src/vai_lab/Core/vai_lab_core.py b/src/vai_lab/Core/vai_lab_core.py index ebe9ca5d..6edb6114 100644 --- a/src/vai_lab/Core/vai_lab_core.py +++ b/src/vai_lab/Core/vai_lab_core.py @@ -67,8 +67,12 @@ def _execute_module(self, specs): mod._debug = self._debug mod.set_avail_plugins(self._avail_plugins) self._load_data(specs, specs["name"]) - mod.set_data_in(self.data[specs["name"]]) mod.set_options(specs) + if specs["name"] == 'User Interaction': + mod._load_plugin(specs["plugin"]["plugin_name"]) + mod.set_data_in(self.data[specs["name"]]) + else: + mod._load_plugin(self.data[specs["name"]]) print("\t"*self.loop_level + specs["module_type"] + " module: \"{}\" ".format(specs["name"]) @@ -189,4 +193,4 @@ def run(self): self._init_status(self._xml_handler.loaded_modules) self._execute(self._xml_handler.loaded_modules) - print("Pipeline Complete") + print("Pipeline Complete") \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/DataProcessing_core.py b/src/vai_lab/DataProcessing/DataProcessing_core.py index ba3716d6..19d2c576 100644 --- a/src/vai_lab/DataProcessing/DataProcessing_core.py +++ b/src/vai_lab/DataProcessing/DataProcessing_core.py @@ -1,6 +1,7 @@ from vai_lab._import_helper import import_plugin_absolute from vai_lab._types import PluginSpecsInterface, DataInterface, DataProcessingPluginInterface - +from pandas import DataFrame +from numpy import array class DataProcessing(object): def __init__(self) -> None: self.output_data: DataInterface @@ -11,14 +12,14 @@ def set_avail_plugins(self, avail_plugins: PluginSpecsInterface) -> None: def set_data_in(self, data_in: DataInterface) -> None: self._data_in = data_in - def _load_plugin(self, plugin_name: str) -> None: + def _load_plugin(self, data_in: DataInterface) -> None: avail_plugins = self._avail_plugins.find_from_readable_name( - plugin_name) - self._plugin_name = plugin_name + self._module_config["plugin"]["plugin_name"]) + self.set_data_in(data_in) self._plugin: DataProcessingPluginInterface = import_plugin_absolute(globals(), avail_plugins["_PLUGIN_PACKAGE"], avail_plugins["_PLUGIN_CLASS_NAME"])\ - .__call__() + .__call__(self._module_config["plugin"], data_in) def set_options(self, module_config: dict) -> None: """Send configuration arguments to plugin @@ -26,19 +27,18 @@ def set_options(self, module_config: dict) -> None: :param module_config: dict of settings to configure the plugin """ self._module_config = module_config - self._load_plugin(self._module_config["plugin"]["plugin_name"]) def launch(self) -> None: - self._plugin.set_data_in(self._data_in) - self._plugin.configure(self._module_config["plugin"]) - self._plugin.init() + for method in self._module_config["plugin"]["methods"]["_order"]: if "options" in self._module_config["plugin"]["methods"][method].keys(): - getattr(self._plugin, "{}".format(method))(self._plugin._parse_options_dict(self._module_config["plugin"]["methods"][method]["options"])) + out = getattr(self._plugin, "{}".format(method))(self._plugin._parse_options_dict(self._module_config["plugin"]["methods"][method]["options"])) else: - getattr(self._plugin, "{}".format(method))() + out = getattr(self._plugin, "{}".format(method))() self.output_data = self._data_in.copy() + if len(out) > 0 and (isinstance(out[0], DataFrame) or isinstance(out[0], array)): + self.output_data.data[list(out[1])[0]] = out[0] def get_result(self) -> DataInterface: return self.output_data \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/binarizer.py b/src/vai_lab/DataProcessing/plugins/binarizer.py index 71e334ed..4dab8267 100644 --- a/src/vai_lab/DataProcessing/plugins/binarizer.py +++ b/src/vai_lab/DataProcessing/plugins/binarizer.py @@ -15,9 +15,20 @@ class Binarizer(DataProcessingT): Binarize data (set feature values to 0 or 1) according to a threshold """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/kbinsdiscretizer.py b/src/vai_lab/DataProcessing/plugins/kbinsdiscretizer.py index 4fc7023e..99aa302f 100644 --- a/src/vai_lab/DataProcessing/plugins/kbinsdiscretizer.py +++ b/src/vai_lab/DataProcessing/plugins/kbinsdiscretizer.py @@ -15,9 +15,20 @@ class KBinsDiscretizer(DataProcessingT): Bin continuous data into interval """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/labelbinarizer.py b/src/vai_lab/DataProcessing/plugins/labelbinarizer.py index 811e1774..f5c76520 100644 --- a/src/vai_lab/DataProcessing/plugins/labelbinarizer.py +++ b/src/vai_lab/DataProcessing/plugins/labelbinarizer.py @@ -13,9 +13,20 @@ class LabelBinarizer(DataProcessingT): """ Binarize labels in a one-vs-all fashion """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/labelencoder.py b/src/vai_lab/DataProcessing/plugins/labelencoder.py index a7166062..a1dd209f 100644 --- a/src/vai_lab/DataProcessing/plugins/labelencoder.py +++ b/src/vai_lab/DataProcessing/plugins/labelencoder.py @@ -14,9 +14,20 @@ class LabelEncoder(DataProcessingT): Encode target labels with value between 0 and n_classes-1 """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/maxabsscaler.py b/src/vai_lab/DataProcessing/plugins/maxabsscaler.py index 58def4b4..c7664835 100644 --- a/src/vai_lab/DataProcessing/plugins/maxabsscaler.py +++ b/src/vai_lab/DataProcessing/plugins/maxabsscaler.py @@ -14,9 +14,20 @@ class MaxAbsScaler(DataProcessingT): Scale each feature by its maximum absolute value """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/minmaxscaler.py b/src/vai_lab/DataProcessing/plugins/minmaxscaler.py index c7e2d170..1cdb349d 100644 --- a/src/vai_lab/DataProcessing/plugins/minmaxscaler.py +++ b/src/vai_lab/DataProcessing/plugins/minmaxscaler.py @@ -15,9 +15,20 @@ class MinMaxScaler(DataProcessingT): is in the given range on the training set, e.g. between zero and one """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/multilabelbinarizer.py b/src/vai_lab/DataProcessing/plugins/multilabelbinarizer.py index 4be65da8..5bbeaa06 100644 --- a/src/vai_lab/DataProcessing/plugins/multilabelbinarizer.py +++ b/src/vai_lab/DataProcessing/plugins/multilabelbinarizer.py @@ -14,9 +14,20 @@ class MultiLabelBinarizer(DataProcessingT): Transform between iterable of iterables and a multilabel format """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/normalizer.py b/src/vai_lab/DataProcessing/plugins/normalizer.py index 6c0cbb8c..05812b33 100644 --- a/src/vai_lab/DataProcessing/plugins/normalizer.py +++ b/src/vai_lab/DataProcessing/plugins/normalizer.py @@ -18,9 +18,20 @@ class Normalizer(DataProcessingT): Normalize samples individually to unit norm """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/onehotencoder.py b/src/vai_lab/DataProcessing/plugins/onehotencoder.py index 73d8eb2c..44edd661 100644 --- a/src/vai_lab/DataProcessing/plugins/onehotencoder.py +++ b/src/vai_lab/DataProcessing/plugins/onehotencoder.py @@ -14,9 +14,20 @@ class OneHotEncoder(DataProcessingT): Encode categorical features as a one-hot numeric array """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/ordinalencoder.py b/src/vai_lab/DataProcessing/plugins/ordinalencoder.py index a9095ccf..7e48d568 100644 --- a/src/vai_lab/DataProcessing/plugins/ordinalencoder.py +++ b/src/vai_lab/DataProcessing/plugins/ordinalencoder.py @@ -15,9 +15,20 @@ class OrdinalEncoder(DataProcessingT): Encode categorical features as an integer array """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/polynomialfeatures.py b/src/vai_lab/DataProcessing/plugins/polynomialfeatures.py index c40c5ea7..b1bcd423 100644 --- a/src/vai_lab/DataProcessing/plugins/polynomialfeatures.py +++ b/src/vai_lab/DataProcessing/plugins/polynomialfeatures.py @@ -18,9 +18,20 @@ class PolynomialFeatures(DataProcessingT): Generate polynomial and interaction features """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/quantiletransformer.py b/src/vai_lab/DataProcessing/plugins/quantiletransformer.py index 91085593..306d4724 100644 --- a/src/vai_lab/DataProcessing/plugins/quantiletransformer.py +++ b/src/vai_lab/DataProcessing/plugins/quantiletransformer.py @@ -16,9 +16,20 @@ class QuantileTransformer(DataProcessingT): Transform features using quantiles information """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DataProcessing/plugins/standardscaler.py b/src/vai_lab/DataProcessing/plugins/standardscaler.py index cda34552..029f49c5 100644 --- a/src/vai_lab/DataProcessing/plugins/standardscaler.py +++ b/src/vai_lab/DataProcessing/plugins/standardscaler.py @@ -16,9 +16,20 @@ class StandardScaler(DataProcessingT): Standardize features by removing the mean and scaling to unit variance """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.transform_plugin = self.model.transform \ No newline at end of file diff --git a/src/vai_lab/DecisionMaking/DecisionMaking_core.py b/src/vai_lab/DecisionMaking/DecisionMaking_core.py index 88c8ea44..a38ad013 100644 --- a/src/vai_lab/DecisionMaking/DecisionMaking_core.py +++ b/src/vai_lab/DecisionMaking/DecisionMaking_core.py @@ -1,22 +1,24 @@ # -*- coding: utf-8 -*- from vai_lab._import_helper import import_plugin_absolute +from vai_lab._types import PluginSpecsInterface, DataInterface class DecisionMaking(object): def __init__(self): - self.output_data = None + self.output_data: DataInterface - def set_avail_plugins(self,avail_plugins): + def set_avail_plugins(self,avail_plugins: PluginSpecsInterface): self._avail_plugins = avail_plugins - def set_data_in(self,data_in): + def set_data_in(self,data_in: DataInterface): self._data_in = data_in - def _load_plugin(self, plugin_name:str): - avail_plugins = self._avail_plugins.find_from_readable_name(plugin_name) - self._plugin_name = plugin_name + def _load_plugin(self, data_in: DataInterface): + avail_plugins = self._avail_plugins.find_from_readable_name(self._module_config["plugin"]["plugin_name"]) + self._plugin_name = self._module_config["plugin"]["plugin_name"] + self.set_data_in(data_in) self._plugin = import_plugin_absolute(globals(),\ avail_plugins["_PLUGIN_PACKAGE"],\ avail_plugins["_PLUGIN_CLASS_NAME"])\ - .__call__() + .__call__(self._module_config["plugin"], data_in) def set_options(self, module_config: dict): """Send configuration arguments to plugin @@ -24,12 +26,16 @@ def set_options(self, module_config: dict): :param module_config: dict of settings to configure the plugin """ self._module_config = module_config - self._load_plugin(self._module_config["plugin"]["plugin_name"]) def launch(self): - self._plugin.set_data_in(self._data_in) - self._plugin.configure(self._module_config["plugin"]) - # self._plugin.optimise() + + for method in self._module_config["plugin"]["methods"]["_order"]: + if "options" in self._module_config["plugin"]["methods"][method].keys(): + out = getattr(self._plugin, "{}".format(method))(self._plugin._parse_options_dict(self._module_config["plugin"]["methods"][method]["options"])) + else: + out = getattr(self._plugin, "{}".format(method))() + + self.output_data = self._data_in.copy() self.output_data = self._plugin.suggest_locations() def get_result(self): diff --git a/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(GPy).py b/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(GPy).py index bf9ff291..28cde0ef 100644 --- a/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(GPy).py +++ b/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(GPy).py @@ -23,12 +23,23 @@ class GPyOpt(DecisionMakingPluginT): Bayesian optimisation model using GPyOpt. Compatible with no objective function using tabular data. """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + # self.fit_plugin = self.model.fit + # self.transform_plugin = self.model.transform def _parse_options_dict(self, options_dict:Dict): super()._parse_options_dict(options_dict) diff --git a/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(bayes_opt).py b/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(bayes_opt).py index 544bcdc9..105c8e9d 100644 --- a/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(bayes_opt).py +++ b/src/vai_lab/DecisionMaking/plugins/BayesianOptimisation(bayes_opt).py @@ -20,12 +20,23 @@ class bayes_opt(DecisionMakingPluginT): Bayesian optimisation model using bayes_opt. """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + # self.fit_plugin = self.model.fit + # self.transform_plugin = self.model.transform def optimise(self): """Probes the target space to find the parameters that yield the maximum value for the given function.""" diff --git a/src/vai_lab/Environment/Environment_core.py b/src/vai_lab/Environment/Environment_core.py index 70bcf13c..c64fe48e 100644 --- a/src/vai_lab/Environment/Environment_core.py +++ b/src/vai_lab/Environment/Environment_core.py @@ -11,14 +11,14 @@ def set_avail_plugins(self, avail_plugins: PluginSpecsInterface) -> None: def set_data_in(self, data_in: DataInterface) -> None: self._data_in = data_in - def _load_plugin(self, plugin_name: str) -> None: + def _load_plugin(self, data_in: DataInterface) -> None: avail_plugins = self._avail_plugins.find_from_readable_name( - plugin_name) - self._plugin_name = plugin_name + self._module_config["plugin"]["plugin_name"]) + self.set_data_in(data_in) self._plugin: EnvironmentPluginInterface = import_plugin_absolute(globals(), avail_plugins["_PLUGIN_PACKAGE"], avail_plugins["_PLUGIN_CLASS_NAME"])\ - .__call__() + .__call__(self._module_config["plugin"], data_in) def set_options(self, module_config: dict) -> None: """Send configuration arguments to plugin @@ -26,11 +26,8 @@ def set_options(self, module_config: dict) -> None: :param module_config: dict of settings to configure the plugin """ self._module_config = module_config - self._load_plugin(self._module_config["plugin"]["plugin_name"]) def launch(self) -> None: - self._plugin.set_data_in(self._data_in) - self._plugin.configure(self._module_config["plugin"]) self._plugin.connect() self._plugin.load_model() self._plugin.run_simulation() @@ -38,4 +35,4 @@ def launch(self) -> None: def get_result(self) -> DataInterface: # return self.output_data - return self._data_in + return self._data_in \ No newline at end of file diff --git a/src/vai_lab/Environment/plugins/PyBulletEnv.py b/src/vai_lab/Environment/plugins/PyBulletEnv.py index 727f2d79..43b4f221 100644 --- a/src/vai_lab/Environment/plugins/PyBulletEnv.py +++ b/src/vai_lab/Environment/plugins/PyBulletEnv.py @@ -20,9 +20,11 @@ class PyBulletEnv(EnvironmentPluginT): Loads the pybullet library as wildcard and exposes all functions """ - def __init__(self) -> None: + def __init__(self, config = {}, data_in = [None]) -> None: super().__init__(globals()) self.model_ids: Dict = {} + self.set_data_in(data_in) + self.configure(config) def set_gui(self, use_gui: bool = True): if type(use_gui) == str: @@ -96,6 +98,4 @@ def __getattr__(self, attr: str) -> Any: if __name__ == "__main__": pb = PyBulletEnv() - print(pb.ACTIVATION_STATE_DISABLE_SLEEPING) - # pb.set_gui(True) - # pb.connect() + print(pb.ACTIVATION_STATE_DISABLE_SLEEPING) \ No newline at end of file diff --git a/src/vai_lab/InputData/InputData_core.py b/src/vai_lab/InputData/InputData_core.py index f9f4d165..883d7c99 100644 --- a/src/vai_lab/InputData/InputData_core.py +++ b/src/vai_lab/InputData/InputData_core.py @@ -23,19 +23,18 @@ def set_options(self, module_config: dict) -> None: :param module_config: dict of settings to configure the plugin """ self._module_config = module_config - self._load_plugin(self._module_config["plugin"]["plugin_name"]) def set_avail_plugins(self, avail_plugins: PluginSpecsInterface) -> None: self._avail_plugins = avail_plugins - def _load_plugin(self, plugin_name: str) -> None: + def _load_plugin(self, data_in: DataInterface) -> None: avail_plugins = self._avail_plugins.find_from_readable_name( - plugin_name) - self._plugin_name = plugin_name + self._module_config["plugin"]["plugin_name"]) + self.set_data_in(data_in) self._plugin: PluginSpecsInterface = import_plugin_absolute(globals(), avail_plugins["_PLUGIN_PACKAGE"], avail_plugins["_PLUGIN_CLASS_NAME"])\ - .__call__() + .__call__(self._module_config["plugin"], data_in) def get_result(self): return self._data_in diff --git a/src/vai_lab/Modelling/Modelling_core.py b/src/vai_lab/Modelling/Modelling_core.py index fa19e436..9e27d3fd 100644 --- a/src/vai_lab/Modelling/Modelling_core.py +++ b/src/vai_lab/Modelling/Modelling_core.py @@ -1,22 +1,24 @@ # -*- coding: utf-8 -*- from vai_lab._import_helper import import_plugin_absolute +from vai_lab._types import PluginSpecsInterface, DataInterface + class Modelling(object): def __init__(self): - self.output_data = None + self.output_data: DataInterface - def set_avail_plugins(self,avail_plugins): + def set_avail_plugins(self, avail_plugins: PluginSpecsInterface): self._avail_plugins = avail_plugins - def set_data_in(self,data_in): + def set_data_in(self,data_in: DataInterface): self._data_in = data_in - def _load_plugin(self, plugin_name:str): - avail_plugins = self._avail_plugins.find_from_readable_name(plugin_name) - self._plugin_name = plugin_name + def _load_plugin(self, data_in: DataInterface): + avail_plugins = self._avail_plugins.find_from_readable_name(self._module_config["plugin"]["plugin_name"]) + self.set_data_in(data_in) self._plugin = import_plugin_absolute(globals(),\ avail_plugins["_PLUGIN_PACKAGE"],\ avail_plugins["_PLUGIN_CLASS_NAME"])\ - .__call__() + .__call__(self._module_config["plugin"], data_in) def set_options(self, module_config: dict): """Send configuration arguments to plugin @@ -24,17 +26,14 @@ def set_options(self, module_config: dict): :param module_config: dict of settings to configure the plugin """ self._module_config = module_config - self._load_plugin(self._module_config["plugin"]["plugin_name"]) def launch(self): - self._plugin.set_data_in(self._data_in) - self._plugin.configure(self._module_config["plugin"]) - self._plugin.init() + for method in self._module_config["plugin"]["methods"]["_order"]: if "options" in self._module_config["plugin"]["methods"][method].keys(): - getattr(self._plugin, "{}".format(method))(self._plugin._parse_options_dict(self._module_config["plugin"]["methods"][method]["options"])) + out = getattr(self._plugin, "{}".format(method))(self._plugin._parse_options_dict(self._module_config["plugin"]["methods"][method]["options"])) else: - getattr(self._plugin, "{}".format(method))() + out = getattr(self._plugin, "{}".format(method))() self.output_data = self._data_in.copy() self.output_data = self._plugin._test(self.output_data) diff --git a/src/vai_lab/Modelling/plugins/affinitypropagation.py b/src/vai_lab/Modelling/plugins/affinitypropagation.py index 648a2091..d1c0d115 100644 --- a/src/vai_lab/Modelling/plugins/affinitypropagation.py +++ b/src/vai_lab/Modelling/plugins/affinitypropagation.py @@ -14,9 +14,20 @@ class AffinityPropagation(ModellingPluginT): Perform Affinity Propagation Clustering of data """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/bayesianridge.py b/src/vai_lab/Modelling/plugins/bayesianridge.py index c8c034f4..7853e1c6 100644 --- a/src/vai_lab/Modelling/plugins/bayesianridge.py +++ b/src/vai_lab/Modelling/plugins/bayesianridge.py @@ -13,10 +13,21 @@ class BayesianRidge(ModellingPluginT): """ Bayesian ridge regression """ - - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/birch.py b/src/vai_lab/Modelling/plugins/birch.py index 86325a30..dc0ac4f8 100644 --- a/src/vai_lab/Modelling/plugins/birch.py +++ b/src/vai_lab/Modelling/plugins/birch.py @@ -15,9 +15,20 @@ class Birch(ModellingPluginT): Implements the BIRCH clustering algorithm """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/decisiontreeclassifier.py b/src/vai_lab/Modelling/plugins/decisiontreeclassifier.py index 797de431..15c1f284 100644 --- a/src/vai_lab/Modelling/plugins/decisiontreeclassifier.py +++ b/src/vai_lab/Modelling/plugins/decisiontreeclassifier.py @@ -15,9 +15,22 @@ class DecisionTreeClassifier(ModellingPluginTClass): A decision tree classifier """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/decisiontreeregressor.py b/src/vai_lab/Modelling/plugins/decisiontreeregressor.py index 4665cd54..f5f8b705 100644 --- a/src/vai_lab/Modelling/plugins/decisiontreeregressor.py +++ b/src/vai_lab/Modelling/plugins/decisiontreeregressor.py @@ -15,9 +15,21 @@ class DecisionTreeRegressor(ModellingPluginT): A decision tree regressor """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/elasticnet.py b/src/vai_lab/Modelling/plugins/elasticnet.py index eabd33bf..c986b309 100644 --- a/src/vai_lab/Modelling/plugins/elasticnet.py +++ b/src/vai_lab/Modelling/plugins/elasticnet.py @@ -14,9 +14,21 @@ class ElasticNet(ModellingPluginT): Linear regression with combined L1 and L2 priors as regularizer """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/gpclassifier.py b/src/vai_lab/Modelling/plugins/gpclassifier.py index cefcf61a..a8b25126 100644 --- a/src/vai_lab/Modelling/plugins/gpclassifier.py +++ b/src/vai_lab/Modelling/plugins/gpclassifier.py @@ -17,9 +17,22 @@ class GPClassifier(ModellingPluginTClass): Gaussian process Classifier (GPC) based on Laplace approximation """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/gpregressor.py b/src/vai_lab/Modelling/plugins/gpregressor.py index add31a24..ee83e8a5 100644 --- a/src/vai_lab/Modelling/plugins/gpregressor.py +++ b/src/vai_lab/Modelling/plugins/gpregressor.py @@ -17,9 +17,21 @@ class GPRegressor(ModellingPluginT): Gaussian process regressor """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/kernelridge.py b/src/vai_lab/Modelling/plugins/kernelridge.py index fcac937a..864c6c39 100644 --- a/src/vai_lab/Modelling/plugins/kernelridge.py +++ b/src/vai_lab/Modelling/plugins/kernelridge.py @@ -17,9 +17,21 @@ class KernelRidge(ModellingPluginT): Kernel ridge regression. """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/kmeans.py b/src/vai_lab/Modelling/plugins/kmeans.py index 11e3e71e..0fa2a9f4 100644 --- a/src/vai_lab/Modelling/plugins/kmeans.py +++ b/src/vai_lab/Modelling/plugins/kmeans.py @@ -14,9 +14,21 @@ class KMeans(ModellingPluginT): K-Means clustering """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/knnclassifier.py b/src/vai_lab/Modelling/plugins/knnclassifier.py index 2c880e95..25fe692c 100644 --- a/src/vai_lab/Modelling/plugins/knnclassifier.py +++ b/src/vai_lab/Modelling/plugins/knnclassifier.py @@ -15,9 +15,22 @@ class KNNClassifier(ModellingPluginTClass): Classifier implementing the k-nearest neighbors vote """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/knnregressor.py b/src/vai_lab/Modelling/plugins/knnregressor.py index 64982701..7b9a5fc3 100644 --- a/src/vai_lab/Modelling/plugins/knnregressor.py +++ b/src/vai_lab/Modelling/plugins/knnregressor.py @@ -15,9 +15,21 @@ class KNNRegressor(ModellingPluginT): Regression based on k-nearest neighbors """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/lasso.py b/src/vai_lab/Modelling/plugins/lasso.py index bfb391b4..081f6d88 100644 --- a/src/vai_lab/Modelling/plugins/lasso.py +++ b/src/vai_lab/Modelling/plugins/lasso.py @@ -13,9 +13,21 @@ class Lasso(ModellingPluginT): Linear Model trained with L1 prior as regularizer """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/linearregression.py b/src/vai_lab/Modelling/plugins/linearregression.py index 25facd00..faee6278 100644 --- a/src/vai_lab/Modelling/plugins/linearregression.py +++ b/src/vai_lab/Modelling/plugins/linearregression.py @@ -15,9 +15,21 @@ class LinearRegression(ModellingPluginT): Ordinary least squares Linear Regression """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/logisticregression.py b/src/vai_lab/Modelling/plugins/logisticregression.py index 25eed6c2..6e4c0a17 100644 --- a/src/vai_lab/Modelling/plugins/logisticregression.py +++ b/src/vai_lab/Modelling/plugins/logisticregression.py @@ -16,9 +16,22 @@ class LogisticRegression(ModellingPluginTClass): Logistic regression classification. """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/meanshift.py b/src/vai_lab/Modelling/plugins/meanshift.py index 28d1327b..38f0bd9a 100644 --- a/src/vai_lab/Modelling/plugins/meanshift.py +++ b/src/vai_lab/Modelling/plugins/meanshift.py @@ -13,9 +13,20 @@ class MeanShift(ModellingPluginT): Mean shift clustering using a flat kernel. """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/passiveaggressiveclassifier.py b/src/vai_lab/Modelling/plugins/passiveaggressiveclassifier.py index dc01447a..64531891 100644 --- a/src/vai_lab/Modelling/plugins/passiveaggressiveclassifier.py +++ b/src/vai_lab/Modelling/plugins/passiveaggressiveclassifier.py @@ -15,9 +15,22 @@ class PassiveAggressiveClassifier(ModellingPluginTClass): Passive aggressive classifier """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/perceptron.py b/src/vai_lab/Modelling/plugins/perceptron.py index 93f35f40..69799653 100644 --- a/src/vai_lab/Modelling/plugins/perceptron.py +++ b/src/vai_lab/Modelling/plugins/perceptron.py @@ -16,9 +16,22 @@ class Perceptron(ModellingPluginTClass): Linear perceptron classification """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/randomforestclassifier.py b/src/vai_lab/Modelling/plugins/randomforestclassifier.py index a8dc60cd..4ac41571 100644 --- a/src/vai_lab/Modelling/plugins/randomforestclassifier.py +++ b/src/vai_lab/Modelling/plugins/randomforestclassifier.py @@ -16,9 +16,22 @@ class RandomForestClassifier(ModellingPluginTClass): A random forest classifier """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/randomforestregressor.py b/src/vai_lab/Modelling/plugins/randomforestregressor.py index 85cf9c10..116a80e5 100644 --- a/src/vai_lab/Modelling/plugins/randomforestregressor.py +++ b/src/vai_lab/Modelling/plugins/randomforestregressor.py @@ -16,9 +16,21 @@ class RandomForestRegressor(ModellingPluginT): A random forest regression """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/ridgeregression.py b/src/vai_lab/Modelling/plugins/ridgeregression.py index 0ee2f965..610d87ea 100644 --- a/src/vai_lab/Modelling/plugins/ridgeregression.py +++ b/src/vai_lab/Modelling/plugins/ridgeregression.py @@ -15,9 +15,21 @@ class RidgeRegression(ModellingPluginT): Linear least squares with l2 regularization """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/svc.py b/src/vai_lab/Modelling/plugins/svc.py index 7fd4d700..1b8f19a9 100644 --- a/src/vai_lab/Modelling/plugins/svc.py +++ b/src/vai_lab/Modelling/plugins/svc.py @@ -18,9 +18,22 @@ class SVC(ModellingPluginTClass): C-Support Vector Classification """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.predict_proba_plugin = self.model.predict_proba + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/Modelling/plugins/svr.py b/src/vai_lab/Modelling/plugins/svr.py index 282fb622..f01ef1c9 100644 --- a/src/vai_lab/Modelling/plugins/svr.py +++ b/src/vai_lab/Modelling/plugins/svr.py @@ -18,9 +18,21 @@ class SVR(ModellingPluginT): Epsilon-Support Vector Regression """ - def __init__(self): + def __init__(self, config = {}, data_in = [None]): """Initialises parent class. Passes `globals` dict of all current variables """ super().__init__(globals()) - self.model = model() \ No newline at end of file + self.set_data_in(data_in) + self.configure(config) + + try: + self.model = model(**self._config["options"]) + except Exception as exc: + print('The plugin encountered an error on the parameters of ' + +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') + raise + + self.fit_plugin = self.model.fit + self.predict_plugin = self.model.predict + self.score_plugin = self.model.score \ No newline at end of file diff --git a/src/vai_lab/_plugin_templates.py b/src/vai_lab/_plugin_templates.py index 437ff74c..d30b87f9 100644 --- a/src/vai_lab/_plugin_templates.py +++ b/src/vai_lab/_plugin_templates.py @@ -76,7 +76,7 @@ def _data_missing_error(self, req): def _parse_config(self): """Parse incoming data and args, sets them as class variables""" - self.X = np.array(self._get_data_if_exist(self._data_in, "X")) + self.X = self._get_data_if_exist(self._data_in, "X") self.Y = np.array(self._get_data_if_exist(self._data_in, "Y")).ravel() self.X_test = self._get_data_if_exist(self._data_in, "X_test") self.Y_test = np.array(self._get_data_if_exist(self._data_in, "Y_test")).ravel() @@ -151,7 +151,7 @@ def _test(self, data: DataInterface) -> DataInterface: print('Test accuracy: %.2f%%' % (self.score([self.X_test, self.Y_test])*100)) if self.X_test is not None: - data.append_data_column("Y_pred", self.predict([self.X_test])) + data.append_data_column("Y_pred", self.predict(self.X_test)) return data elif self._PLUGIN_MODULE_OPTIONS['Type'] == 'regression': print('Training R2 score: %.3f' % @@ -160,12 +160,12 @@ def _test(self, data: DataInterface) -> DataInterface: print('Test R2 score: %.3f' % (self.score([self.X_test, self.Y_test]))) if self.X_test is not None: - data.append_data_column("Y_pred", self.predict([self.X_test])) + data.append_data_column("Y_pred", self.predict_plugin(self.X_test)) return data elif self._PLUGIN_MODULE_OPTIONS['Type'] == 'clustering': print('Clustering completed') if self.X_test is not None: - data.append_data_column("Y_pred", self.predict([self.X_test])) + data.append_data_column("Y_pred", self.predict(self.X_test)) return data else: return data @@ -209,30 +209,14 @@ def _clean_solver_options(self): _cleaned[key] = self.Y return _cleaned - def init(self): - """Sends params to model""" - try: - self.model.set_params(**self._config["options"]) - except Exception as exc: - print('The plugin encountered an error on the parameters of ' - +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') - raise - def fit(self, options={}): try: - if type(self._clean_solver_options()) == list: - self.model.set_params(*self._clean_solver_options()) - else: - self.model.set_params(**self._clean_solver_options()) - except Exception as exc: - print('The plugin encountered an error on the parameters of ' - +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') - raise - try: - if type(options) == list: - return self.model.fit(*options) + if isinstance(options, list): + return self.fit_plugin(*options) + if isinstance(options, dict): + return self.fit_plugin(**options) else: - return self.model.fit(**options) + return self.fit_plugin(options) except Exception as exc: print('The plugin encountered an error when fitting ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') @@ -240,37 +224,30 @@ def fit(self, options={}): def transform(self, options={}) -> DataInterface: try: - if type(options) == list: - return pd.DataFrame(self.model.transform(*options)) + if isinstance(options, list): + return pd.DataFrame(self.transform_plugin(*options)) + elif isinstance(options, dict): + return pd.DataFrame(self.transform_plugin(**options)), options.keys() else: - return pd.DataFrame(self.model.transform(**options)) + return pd.DataFrame(self.transform_plugin(options)) except Exception as exc: print('The plugin encountered an error when transforming the data with ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') raise - return data - class ModellingPluginT(PluginTemplate, ABC): def __init__(self, plugin_globals: dict) -> None: super().__init__(plugin_globals) - - def init(self): - """Sends params to model""" - try: - self.model.set_params(**self._config["options"]) - except Exception as exc: - print('The plugin encountered an error on the parameters of ' - +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') - raise def fit(self, options={}): """Sends params to fit, then runs fit""" try: - if type(options) == list: - return self.model.fit(*options) + if isinstance(options, list): + return self.fit_plugin(*options) + if isinstance(options, dict): + return self.fit_plugin(**options) else: - return self.model.fit(**options) + return self.fit_plugin(options) except Exception as exc: print('The plugin encountered an error when fitting ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') @@ -285,10 +262,13 @@ def predict(self, options={}): Returns predicted values. """ try: - if type(options) == list: - return self.model.predict(*options) + if isinstance(options, list): + return self.predict_plugin(*options) + elif isinstance(options, dict): + return self.predict_plugin(**options) else: - return self.model.predict(**options) + return self.predict_plugin(options) + except Exception as exc: print('The plugin encountered an error when predicting with ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+': '+str(exc)+'.') @@ -302,10 +282,12 @@ def score(self, options={}): :returns: score : float of ``self.predict(X)`` wrt. `y`. """ try: - if type(options) == list: - return self.model.score(*options) + if isinstance(options, list): + return self.score_plugin(*options) + elif isinstance(options, dict): + return self.score_plugin(**options) else: - return self.model.score(**options) + return self.score_plugin(options) except Exception as exc: print('The plugin encountered an error when calculating the score with ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+'.') @@ -324,10 +306,12 @@ def predict_proba(self, options={}): Returns predicted values. """ try: - if type(options) == list: - return self.model.predict_proba(*options) + if isinstance(options, list): + return self.predict_proba_plugin(*options) + if isinstance(options, dict): + return self.predict_proba_plugin(**options) else: - return self.model.predict_proba(**options) + return self.predict_proba_plugin(options) except Exception as exc: print('The plugin encountered an error when predicting the probability with ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+'.') @@ -341,10 +325,12 @@ def configure(self, config: dict): """Extended from PluginTemplate.configure""" super().configure(config) try: - if type(self._clean_options()) == list: + if isinstance(self._clean_options(), list): self.BO = self.model(*self._clean_options()) - else: + if isinstance(self._clean_options(), list): self.BO = self.model(**self._clean_options()) + else: + self.BO = self.model(self._clean_options()) except Exception as exc: print('The plugin encountered an error on the parameters of ' +str(list(self._PLUGIN_READABLE_NAMES.keys())[list(self._PLUGIN_READABLE_NAMES.values()).index('default')])+'.') diff --git a/src/vai_lab/examples/results/output.pkl b/src/vai_lab/examples/results/output.pkl index 37f85b484926d421d1e5b3e3af1eec68b9db0c9a..bcb9c7294b5682175c4e0dceaf9c0f55e692dd6e 100644 GIT binary patch delta 1333 zcmZvb`!|$%9LMJwMnuE77jts3nab^CQ7NBq8PynaD-tD>sfSj3!4NsqayFN0HJOSW zt<1Jzaa^`iv|rmUI&pMzX+>H`n&-(a_grGO)9(JTd-nY&ykDkR91f>{+y z!Ilysa?)}A*YXrOk8~nk$SveH-j#lW@o}Q$W?9oIBjj`x_%bC0&nSz)*~&@0srZ5! z^Nt}}7CVF)T-8N&)ZLayZY#uoSGM2L>PI}0b*Eh-RzY#=?+3?}yPw+KG_2bMc-%9HU2 zvF;H&%|Xrtm>Hi58>(WW0uw!+TUjL>6kRV{cT26IGA3ke!uNCFZ+*?xt6UTLIr}Zn zN6^^5=Gnls&M+=g8TqGt`U=#EFqJVjMy|R-n+mUSxFHSd%H;?Ml)L^w;m*`?*kCH# zGji-b-eYG{nv$xIrcW3aC7fU(URK_D@#_(6u?rt-zRGAgkn(yq+*1z)??pX%Yv*z9 z>GKO;=)J}*)}Pf^98^OF+?FhGkwlhwjdM#FfT^iavA1#EWt!b1kq02tc2^dcA%;hCTg1lsn zjEj_T<+AJe@E9B2r6YsO?B)P2eg7oy~=w_%>X=ZbBWHXAdse@&y*z9(15w{)m ziuOl_rzRojMqXNvQi-L}iu|MAX)IJJ(>T?i(S@x?H(o607{|Zy^Am+zma$(zP#KFd zKnpVAzPe^zG*oAK&f%~&x>7KI-ywv7Ub;S5M2R#6m5zEPuVEr$Rb`!%!T`-oy(&XC zEOgI9|D2$`12VKM^?h^*l$f{Dr9XZi7hEg6otr-^g@-S~cApHN1&&^(zG!e7T-O!% zMkKaly_F_yYVI^nfBqn__AL{oxtA_kawlNEI!b-4n}yWHLq3uY0ttf2QNxHaOi9kq z>}X;(2x%(dt|FFFcj{!nfeqY?ex*fGJFHtRerYCLp0O0bRfFYrz>=)hRHrc*^^76%8dn&F6OxD4~&wB1Q_8pz=*psPCenJn!T& zM-~nJ{7b=YqhS;*Id8qs^`v1wrf`SkCIzAX_L4>g1*OfJx7_S$ShI0J<;|gC)wkv? rr>$tnwA&CUT2R6}ovILW7Y#qRowHr3PJc{-_d))TLBz+4V(tF~q7G>5 delta 245 zcmZoN@6=^&V4Yegwvlx)-)3XMKWvlbr8w&sFhBq^kYWO25Df#2Kn4hd#6bY21||-o zVPYT}#0LQw8>SAVAKh%IzF4qgm?0oV$cDoBAbLuMLl0A?$&}8H_9;PAG`yKJT>Sj} dy#50LnDAyOnUdtK(K;o=y#%PBv^1$$4*+zUA&39~ diff --git a/src/vai_lab/examples/xml_files/k-mean_clustering_demo.xml b/src/vai_lab/examples/xml_files/k-mean_clustering_demo.xml index 997202a7..1c7ff0b8 100644 --- a/src/vai_lab/examples/xml_files/k-mean_clustering_demo.xml +++ b/src/vai_lab/examples/xml_files/k-mean_clustering_demo.xml @@ -35,6 +35,13 @@ + + + + X + + + diff --git a/src/vai_lab/tests/test_examples.py b/src/vai_lab/tests/test_examples.py index b23eece1..56fa1185 100644 --- a/src/vai_lab/tests/test_examples.py +++ b/src/vai_lab/tests/test_examples.py @@ -15,4 +15,7 @@ def test_examples(): core = ai.Core() core.load_config_file(file) core._debug = True - core.run() \ No newline at end of file + core.run() + +if __name__ == "__main__": + test_examples() \ No newline at end of file diff --git a/src/vai_lab/utils/plugins/progressTracker.py b/src/vai_lab/utils/plugins/progressTracker.py index 22fab3fe..152fe1db 100644 --- a/src/vai_lab/utils/plugins/progressTracker.py +++ b/src/vai_lab/utils/plugins/progressTracker.py @@ -838,9 +838,8 @@ def upload(self): arrow=tk.LAST, tags=('o'+str(parent_id), 'o'+str(1), modout['coordinates'][2][connect[p]])) - self.out_data.iloc[int(parent_id)][1] = 1 - self.connections[1][ - int(parent_id)] = out[0]+str(parent_id) + '-' + ins[0]+str(1) + self.out_data.iloc[int(parent_id)].iloc[1] = 1 + self.connections[1][int(parent_id)] = out[0]+str(parent_id) + '-' + ins[0]+str(1) self.m = self.id_mod[2] else: # There are no coordinates for some modules. @@ -987,8 +986,7 @@ def place_modules(self, modules: dict): arrow=tk.LAST, tags=('o'+str(parent_id), 'o'+str(self.id_mod[-1]), modules[key]['coordinates'][2][connect[p]])) - self.out_data.iloc[int(parent_id)][int( - self.id_mod[-1])] = 1 + self.out_data.iloc[int(parent_id)].iloc[int(self.id_mod[-1])] = 1 self.connections[int(self.id_mod[-1])][ int(parent_id)] = out[0]+str(parent_id) + '-' + ins[0]+str(self.id_mod[-1]) else: