diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/diversity_utils.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/diversity_utils.py index 98956a5a5e9a92f2dad3093a9ee7e90885aff96e..b602d77f779fa60c40598bdda26f2722bdd63cdd 100644 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/diversity_utils.py +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/diversity_utils.py @@ -6,17 +6,19 @@ import os import numpy as np from ...multiview.multiview_utils import ConfigGenerator, \ - get_monoview_classifier, get_examples_views_indices, \ - get_available_monoview_classifiers, BaseMultiviewClassifier + get_examples_views_indices, get_available_monoview_classifiers, \ + BaseMultiviewClassifier +from .fusion_utils import BaseLateFusionClassifier -class DiversityFusion(BaseMultiviewClassifier): +class DiversityFusionClassifier(BaseMultiviewClassifier, + BaseLateFusionClassifier): """This is the base class for all the diversity fusion based classifiers.""" def __init__(self, random_state=None, classifier_names=None, monoview_estimators=None, classifiers_configs=None): """Used to init the instances""" - super(DiversityFusion, self).__init__(random_state) + super(DiversityFusionClassifier, self).__init__(random_state) if classifier_names is None: classifier_names = get_available_monoview_classifiers() self.classifier_names = classifier_names @@ -33,18 +35,7 @@ class DiversityFusion(BaseMultiviewClassifier): self.estimator_pool = [] for classifier_idx, classifier_name in enumerate(self.classifier_names): self.estimator_pool.append([]) - if self.classifiers_configs is not None and classifier_name in self.classifiers_configs: - if 'random_state' in inspect.getfullargspec(get_monoview_classifier(classifier_name).__init__).args: - estimator = get_monoview_classifier(classifier_name)(random_state=self.random_state, - **self.classifiers_configs[classifier_name]) - else: - estimator = get_monoview_classifier(classifier_name)( - **self.classifiers_configs[classifier_name]) - else: - if 'random_state' in inspect.getfullargspec(get_monoview_classifier(classifier_name).__init__).args: - estimator = get_monoview_classifier(classifier_name)(random_state=self.random_state) - else: - estimator = get_monoview_classifier(classifier_name)() + estimator = self.init_monoview_estimator(classifier_name) for idx, view_idx in enumerate(views_indices): estimator.fit(X.get_v(view_idx, train_indices), y[train_indices]) self.estimator_pool[classifier_idx].append(estimator) @@ -96,7 +87,7 @@ class DiversityFusion(BaseMultiviewClassifier): return combinations, combis, div_measure, classifiers_decisions, nb_views -class GlobalDiversityFusion(DiversityFusion): +class GlobalDiversityFusionClassifier(DiversityFusionClassifier): def choose_combination(self, X, y, examples_indices, view_indices): combinations, combis, div_measure, classifiers_decisions, nb_views = self.init_combinations( @@ -114,7 +105,7 @@ class GlobalDiversityFusion(DiversityFusion): in enumerate(best_combination)] -class CoupleDiversityFusion(DiversityFusion): +class CoupleDiversityFusionClassifier(DiversityFusionClassifier): def choose_combination(self, X, y, examples_indices, view_indices): combinations, combis, div_measure, classifiers_decisions, nb_views = self.init_combinations( diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/fusion_utils.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/fusion_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..0196e70957cd24010aa8c15786dbb00c3d2a96b8 --- /dev/null +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/fusion_utils.py @@ -0,0 +1,30 @@ +import inspect + + +from ...multiview.multiview_utils import BaseMultiviewClassifier, get_monoview_classifier + + +class BaseLateFusionClassifier(BaseMultiviewClassifier): + + def init_monoview_estimator(self, classifier_name, classifier_index=None): + if classifier_index is not None: + classifier_configs = self.classifier_configs[classifier_index] + else: + classifier_configs = self.classifier_configs + if classifier_configs is not None and classifier_name in classifier_configs: + if 'random_state' in inspect.getfullargspec( + get_monoview_classifier(classifier_name).__init__).args: + estimator = get_monoview_classifier(classifier_name)( + random_state=self.random_state, + **classifier_configs[classifier_name]) + else: + estimator = get_monoview_classifier(classifier_name)( + **classifier_configs[classifier_name]) + else: + if 'random_state' in inspect.getfullargspec( + get_monoview_classifier(classifier_name).__init__).args: + estimator = get_monoview_classifier(classifier_name)( + random_state=self.random_state) + else: + estimator = get_monoview_classifier(classifier_name)() + return estimator diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/late_fusion_utils.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/late_fusion_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..c05d319c65c718381532a7e1fa85bf2b881e9ce7 --- /dev/null +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/additions/late_fusion_utils.py @@ -0,0 +1,92 @@ +import numpy as np +import warnings +from scipy.stats import uniform + + +from ...multiview.multiview_utils import BaseMultiviewClassifier, get_available_monoview_classifiers, get_monoview_classifier, get_examples_views_indices, ConfigGenerator +from .fusion_utils import BaseLateFusionClassifier + + +class ClassifierCombinator: + + def __init__(self, nb_view): + self.nb_view = nb_view + self.available_classifiers = get_available_monoview_classifiers() + + def rvs(self, random_state=None): + classifier_names = random_state.choice(self.available_classifiers, + size=self.nb_view, replace=True) + return classifier_names + + +class MultipleConfigGenerator(ConfigGenerator): + def __init__(self, nb_view): + super(MultipleConfigGenerator, self).__init__(get_available_monoview_classifiers()) + self.nb_view = nb_view + self.multiple_distribs = [self.distribs for _ in range(nb_view)] + + def rvs(self, random_state=None): + config_samples = [super(MultipleConfigGenerator, self).rvs(random_state) + for _ in range(self.nb_view)] + return config_samples + +class WeightsGenerator: + + def __init__(self, nb_view): + self.nb_view=nb_view + self.uniform = uniform(loc=0, state=1) + + def rvs(self, random_state=None): + return np.array([uniform.rvs(random_state=random_state) + for _ in range(self.nb_view)]) + + +class LateFusionClassifier(BaseMultiviewClassifier, BaseLateFusionClassifier): + + def __init__(self, random_state=None, classifier_names=None, + classifier_configs=None, nb_cores=1, nb_view=None, weights=None): + super(LateFusionClassifier, self).__init__(random_state) + self.verif_clf_views(classifier_names, nb_view) + self.nb_view = len(classifier_names) + self.classifiers_names = classifier_names + self.classifier_configs = classifier_configs + self.monoview_estimators = [self.init_monoview_estimator(classifier_name, classifier_index) + for classifier_index, classifier_name in self.classifiers_names] + self.nb_cores = nb_cores + self.accuracies = np.zeros(len(classifier_names)) + self.needProbas = False + if weights is None: + self.weights = np.ones(nb_view)/nb_view + else: + self.weights = weights + self.param_names = ["classifier_names", "classifier_configs", "weights"] + self.distribs =[ClassifierCombinator(self.nb_view), + MultipleConfigGenerator(self.nb_view), + WeightsGenerator(nb_view)] + + def fit(self, X, y, train_indices=None, views_indices=None): + train_indices, views_indices = get_examples_views_indices(X, + train_indices, + views_indices) + self.monoview_estimators = [monoview_estimator.fit(X.get_v(view_index, train_indices), y[train_indices]) for view_index, monoview_estimator in zip(views_indices, self.monoview_estimators)] + return self + + def verif_clf_views(self, classifier_names, nb_view): + if classifier_names is None: + if nb_view is None: + raise AttributeError(self.__class__.__name__+" must have either classifier_names or nb_views provided.") + else: + self.classifiers_names = self.get_classifiers(get_available_monoview_classifiers(), nb_view) + else: + if nb_view is None: + self.classifiers_names = classifier_names + else: + if len(classifier_names)==nb_view: + self.classifiers_names = classifier_names + else: + warnings.warn("nb_view and classifier_names not matching, choosing nb_view random classifiers in classifier_names.", UserWarning) + self.classifiers_names = self.get_classifiers(classifier_names, nb_view) + + + def get_classifiers(self, classifiers_names, nb_choices): + return self.random_state.choice(classifiers_names, size=nb_choices, replace=True) diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/bayesian_inference_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/bayesian_inference_fusion.py new file mode 100644 index 0000000000000000000000000000000000000000..0ebefcf51daeb315b05b61d8c110df7a1cc01f0a --- /dev/null +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/bayesian_inference_fusion.py @@ -0,0 +1,33 @@ +import numpy as np + +from ..multiview_classifiers.additions.late_fusion_utils import \ + LateFusionClassifier +from ..multiview.multiview_utils import get_examples_views_indices + +classifier_class_name = "BayesianInferenceClassifier" + + +class BayesianInferenceClassifier(LateFusionClassifier): + def __init__(self, random_state, classifier_names=None, + classifier_configs=None, nb_view=None, nb_cores=1): + super(BayesianInferenceClassifier, self).__init__(random_state=random_state, + classifier_names=classifier_names, + classifier_configs=classifier_configs, + nb_cores=nb_cores, + nb_view=nb_view) + + def predict(self, X, example_indices=None, views_indices=None): + example_indices, views_indices = get_examples_views_indices(X, example_indices, views_indices) + + if sum(self.weights) != 1.0: + self.weights = self.weights / sum(self.weights) + + view_scores = [] + for index, view_index in enumerate(views_indices): + view_scores.append(np.power( + self.monoviewClassifiers[index].predict_proba(X.get_v(view_index, + example_indices)), + self.weights[index])) + view_scores = np.array(view_scores) + predicted_labels = np.argmax(np.prod(view_scores, axis=0), axis=1) + return predicted_labels diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/difficulty_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/difficulty_fusion.py index dbdc97de4408e4954cf281bf2314e4e4ad213a94..b89fa1e4a397590c7eddf7e2dea8574f0095f681 100644 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/difficulty_fusion.py +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/difficulty_fusion.py @@ -1,12 +1,12 @@ import numpy as np -from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import GlobalDiversityFusion +from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import GlobalDiversityFusionClassifier classifier_class_name = "DifficultyFusion" -class DifficultyFusion(GlobalDiversityFusion): +class DifficultyFusion(GlobalDiversityFusionClassifier): def diversity_measure(self, classifiers_decisions, combination, y): _, nb_view, nb_examples = classifiers_decisions.shape diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/disagree_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/disagree_fusion.py index 445e4f8532e1557cd4aa9f16efc49f923c9b5342..2dec7814a5f383471685aedcf75c1286e66b8273 100644 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/disagree_fusion.py +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/disagree_fusion.py @@ -1,12 +1,12 @@ import numpy as np -from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import CoupleDiversityFusion +from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import CoupleDiversityFusionClassifier classifier_class_name = "DisagreeFusion" -class DisagreeFusion(CoupleDiversityFusion): +class DisagreeFusion(CoupleDiversityFusionClassifier): def diversity_measure(self, first_classifier_decision, second_classifier_decision, _): return np.logical_xor(first_classifier_decision, second_classifier_decision) diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/double_fault_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/double_fault_fusion.py index 15ad8b6ae073f999642d5e987ea6aff57c9327c6..12eb6b64c39a1606f950e24e3a2e30e35fee10b9 100644 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/double_fault_fusion.py +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/double_fault_fusion.py @@ -1,12 +1,12 @@ import numpy as np from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import \ - CoupleDiversityFusion + CoupleDiversityFusionClassifier classifier_class_name = "DoubleFaultFusion" -class DoubleFaultFusion(CoupleDiversityFusion): +class DoubleFaultFusion(CoupleDiversityFusionClassifier): def diversity_measure(self, first_classifier_decision, second_classifier_decision, y): diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/entropy_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/entropy_fusion.py index 54986816d569bc9ec5761dafe3dced75467fa766..6d5e846b43db9edfa5f7a3bdb06162a438e16c95 100644 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/entropy_fusion.py +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/entropy_fusion.py @@ -1,12 +1,12 @@ import numpy as np -from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import GlobalDiversityFusion +from multiview_platform.mono_multi_view_classifiers.multiview_classifiers.additions.diversity_utils import GlobalDiversityFusionClassifier classifier_class_name = "EntropyFusion" -class EntropyFusion(GlobalDiversityFusion): +class EntropyFusion(GlobalDiversityFusionClassifier): def diversity_measure(self, classifiers_decisions, combination, y): _, nb_view, nb_examples = classifiers_decisions.shape diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusion.py deleted file mode 100644 index 4bb84dc6daf14a9ee282f8f233018370b5d91c3d..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusion.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python -# -*- encoding: utf-8 - -import numpy as np -from ....utils.dataset import get_v -from .... import monoview_classifiers - -class EarlyFusionClassifier(object): - def __init__(self, randomState, monoviewClassifierName, monoviewClassifierConfig, NB_CORES=1): - self.monoviewClassifierName = monoviewClassifierName - if type(monoviewClassifierConfig) == dict: - pass - elif monoviewClassifierConfig is None: - pass - else: - monoviewClassifierConfig = dict((str(configIndex), config[0]) for configIndex, config in - enumerate(monoviewClassifierConfig - )) - monoviewClassifierModule = getattr(monoview_classifiers, self.monoviewClassifierName) - if monoviewClassifierConfig is not None: - self.monoviewClassifier = getattr(monoviewClassifierModule, self.monoviewClassifierName)( - random_state=randomState, **monoviewClassifierConfig) - else: - self.monoviewClassifier = getattr(monoviewClassifierModule, self.monoviewClassifierName)( - random_state=randomState) - self.monoviewClassifiersConfig = monoviewClassifierConfig - self.nbCores = NB_CORES - self.monoviewData = None - self.randomState = randomState - - def makeMonoviewData_hdf5(self, DATASET, weights=None, usedIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - if type(weights) == type(None): - weights = np.array([1 / nbView for i in range(nbView)]) - if sum(weights) != 1: - weights = weights / sum(weights) - self.monoviewData = np.concatenate([get_v(DATASET, viewIndex, usedIndices) - for index, viewIndex in enumerate(viewsIndices)], axis=1) diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage/WeightedLinear.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage/WeightedLinear.py deleted file mode 100644 index 79059d5ca203ccc3a2a5fcc651086431262403cb..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage/WeightedLinear.py +++ /dev/null @@ -1,121 +0,0 @@ -import numpy as np - -from ...Methods.EarlyFusion import EarlyFusionClassifier -from ..... import monoview_classifiers - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - nbView = classificationKWARGS["nbView"] - if classificationKWARGS["classifiersConfigs"] is None: - monoviewClassifierModule = getattr(monoview_classifiers, classificationKWARGS["classifiersNames"]) - paramsMonoview = monoviewClassifierModule.paramsToSet(nIter, randomState) - paramsSets = [] - for iterIndex in range(nIter): - randomWeightsArray = randomState.random_sample(nbView) - normalizedArray = randomWeightsArray / np.sum(randomWeightsArray) - paramsSets.append([normalizedArray, paramsMonoview[iterIndex]]) - return paramsSets - - -def getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, classificationIndices): - argumentsList = [] - if args.FU_E_cl_names != ['']: - pass - else: - monoviewClassifierModulesNames = benchmark["monoview"] - args.FU_E_cl_names = monoviewClassifierModulesNames - args.FU_E_cl_config = [None for _ in monoviewClassifierModulesNames] - for classifierName, classifierConfig in zip(args.FU_E_cl_names, args.FU_E_cl_config): - monoviewClassifierModule = getattr(monoview_classifiers, classifierName) - if classifierConfig is not None: - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "EarlyFusion", - "fusionMethod": "WeightedLinear", - "classifiersNames": classifierName, - "classifiersConfigs": monoviewClassifierModule.getKWARGS([arg.split(":") - for arg in - classifierConfig.split( - ",")]), - 'fusionMethodConfig': args.FU_E_method_configs, - "nbView": (len(viewsIndices))}} - else: - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "EarlyFusion", - "fusionMethod": "WeightedLinear", - "classifiersNames": classifierName, - "classifiersConfigs": None, - 'fusionMethodConfig': args.FU_E_method_configs, - "nbView": (len(viewsIndices))}} - argumentsList.append(arguments) - return argumentsList - - -class WeightedLinear(EarlyFusionClassifier): - def __init__(self, randomState, NB_CORES=1, **kwargs): - EarlyFusionClassifier.__init__(self, randomState, kwargs['classifiersNames'], kwargs['classifiersConfigs'], - NB_CORES=NB_CORES) - if kwargs['fusionMethodConfig'] is None: - self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float) - elif kwargs['fusionMethodConfig'] == ['']: - self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float) - else: - self.weights = np.array(map(float, kwargs['fusionMethodConfig'])) - self.weights /= float(max(self.weights)) - - - - def fit_hdf5(self, DATASET, labels, trainIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - if trainIndices is None: - trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - self.weights /= float(max(self.weights)) - self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=trainIndices, viewsIndices=viewsIndices) - # monoviewClassifierModule = getattr(monoview_classifiers, self.monoviewClassifierName) - self.monoviewClassifier.fit(self.monoviewData, labels[trainIndices]) - # self.randomState, - # NB_CORES=self.nbCores, - # **self.monoviewClassifiersConfig) - - def setParams(self, paramsSet): - self.weights = paramsSet[0] - self.monoviewClassifiersConfig = paramsSet[1] - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - self.weights /= float(np.sum(self.weights)) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=usedIndices, viewsIndices=viewsIndices) - predictedLabels = self.monoviewClassifier.predict(self.monoviewData) - - return predictedLabels - - def predict_proba_hdf5(self, DATASET, usedIndices=None): - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=usedIndices) - predictedLabels = self.monoviewClassifier.predict_proba(self.monoviewData) - return predictedLabels - - def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs): - configString = "with weighted concatenation, using weights : " + ", ".join(map(str, self.weights)) + \ - " with monoview classifier : " - # monoviewClassifierModule = getattr(monoview_classifiers, monoviewClassifiersNames) - configString += self.monoviewClassifier.getConfig() - return configString - - def gridSearch(self, classificationKWARGS): - - return diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage/__init__.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage/__init__.py deleted file mode 100644 index 3636ca90ab36db2f57b006402a3c6767d9395117..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -import os - -for module in os.listdir(os.path.dirname(os.path.realpath(__file__))): - if module == '__init__.py' or module[-3:] != '.py': - continue - __import__(module[:-3], locals(), globals(), [], 1) -del module -del os diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusion.py deleted file mode 100644 index 314b11bb3b85b0fb570450fc93923997c47c58e9..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusion.py +++ /dev/null @@ -1,148 +0,0 @@ -#!/usr/bin/env python -# -*- encoding: utf-8 - -import numpy as np -import itertools -from joblib import Parallel, delayed -import sys -import math - -from .... import monoview_classifiers -from .... import metrics -from ....utils.dataset import get_v - - -# def canProbasClassifier(classifierConfig): -# try: -# _ = getattr(classifierConfig, "predict_proba") -# return True -# except AttributeError: -# return False - - -def fitMonoviewClassifier(monoviewClassifier, data, labels, needProbas, randomState, nbCores=1): - if needProbas and not monoviewClassifier.canProbas(): - DTConfig = {"max_depth": 300, "criterion": "entropy", "splitter": "random"} - monoviewClassifier = getattr(monoview_classifiers, "DecisionTree")(random_state=randomState, **DTConfig) - classifier = monoviewClassifier.fit(data, labels) - return classifier - else: - # if type(classifierConfig) is dict: - # pass - # else: - # classifierConfig = dict((str(configIndex), config) - # for configIndex, config in enumerate(classifierConfig)) - - classifier = monoviewClassifier.fit(data, labels) - return classifier - - -def getScores(LateFusionClassifiers): - return "" - - -def intersect(allClassifersNames, directory, viewsIndices, resultsMonoview, classificationIndices): - wrongSets = [[] for _ in viewsIndices] - # wrongSets = [0 for _ in allClassifersNames] - classifiersNames = [[] for _ in viewsIndices] - nbViews = len(viewsIndices) - trainLabels = np.genfromtxt(directory + "train_labels.csv", delimiter=",").astype(np.int16) - length = len(trainLabels) - for resultMonoview in resultsMonoview: - if resultMonoview.classifier_name in classifiersNames[viewsIndices.index(resultMonoview.view_index)]: - classifierIndex = classifiersNames.index(resultMonoview.classifier_name) - wrongSets[resultMonoview.view_index][classifierIndex] = np.where( - trainLabels + resultMonoview.full_labels_pred[classificationIndices[0]] == 1)[0] - else: - classifiersNames[viewsIndices.index(resultMonoview.view_index)].append(resultMonoview.classifier_name) - wrongSets[viewsIndices.index(resultMonoview.view_index)].append( - np.where(trainLabels + resultMonoview.full_labels_pred[classificationIndices[0]] == 1)[0]) - - combinations = itertools.combinations_with_replacement(range(len(classifiersNames[0])), nbViews) - bestLen = length - bestCombination = None - for combination in combinations: - intersect = np.arange(length, dtype=np.int16) - for viewIndex, classifierIndex in enumerate(combination): - intersect = np.intersect1d(intersect, wrongSets[viewIndex][classifierIndex]) - if len(intersect) < bestLen: - bestLen = len(intersect) - bestCombination = combination - return [classifiersNames[viewIndex][index] for viewIndex, index in enumerate(bestCombination)] - - -def bestScore(allClassifersNames, directory, viewsIndices, resultsMonoview, classificationIndices): - nbViews = len(viewsIndices) - nbClassifiers = len(allClassifersNames) - scores = np.zeros((nbViews, nbClassifiers)) - classifiersNames = [[] for _ in viewsIndices] - metricName = resultsMonoview[0].metrics_scores.keys()[0] - metricModule = getattr(metrics, metricName) - if metricModule.getConfig()[-14] == "h": - betterHigh = True - else: - betterHigh = False - for resultMonoview in resultsMonoview: - if resultMonoview.classifier_name not in classifiersNames[resultMonoview.view_index]: - classifiersNames[resultMonoview.view_index].append(resultMonoview.classifier_name) - classifierIndex = classifiersNames[resultMonoview.view_index].index(resultMonoview.classifier_name) - scores[resultMonoview.view_index, classifierIndex] = resultMonoview.metrics_scores.values()[0][0] - - if betterHigh: - classifierIndices = np.argmax(scores, axis=1) - else: - classifierIndices = np.argmin(scores, axis=1) - return [classifiersNames[viewIndex][index] for viewIndex, index in enumerate(classifierIndices)] - - -def getClassifiers(selectionMethodName, allClassifiersNames, directory, viewsIndices, resultsMonoview, - classificationIndices): - thismodule = sys.modules[__name__] - selectionMethod = getattr(thismodule, selectionMethodName) - classifiersNames = selectionMethod(allClassifiersNames, directory, viewsIndices, resultsMonoview, - classificationIndices) - return classifiersNames - - -def getConfig(classifiersNames, resultsMonoview, viewsIndices): - classifiersConfigs = [0 for _ in range(len(classifiersNames))] - for classifierIndex, classifierName in enumerate(classifiersNames): - for resultMonoview in resultsMonoview: - if resultMonoview.view_index == viewsIndices[classifierIndex] and resultMonoview.classifier_name == classifierName: - classifiersConfigs[classifierIndex] = resultMonoview.classifier_config - return classifiersConfigs - - -class LateFusionClassifier(object): - def __init__(self, randomState, monoviewClassifiersNames, monoviewClassifiersConfigs, monoviewSelection, - NB_CORES=1): - self.monoviewClassifiersNames = monoviewClassifiersNames - monoviewClassifiersModules = [getattr(monoview_classifiers, classifierName) - for classifierName in self.monoviewClassifiersNames] - if type(monoviewClassifiersConfigs[0]) == dict: - self.monoviewClassifiers = [ - getattr(monoviewClassifiersModule, classifierName)(random_state=randomState, **config) - for monoviewClassifiersModule, config, classifierName - in zip(monoviewClassifiersModules, monoviewClassifiersConfigs, monoviewClassifiersNames)] - else: - self.monoviewClassifiers = [ - getattr(monoviewClassifiersModule, classifierName)(random_state=randomState,) - for monoviewClassifiersModule, config, classifierName - in zip(monoviewClassifiersModules, monoviewClassifiersConfigs, monoviewClassifiersNames)] - self.nbCores = NB_CORES - self.accuracies = np.zeros(len(monoviewClassifiersNames)) - self.needProbas = False - self.monoviewSelection = monoviewSelection - self.randomState = randomState - - def fit_hdf5(self, DATASET, labels, trainIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - if trainIndices is None: - trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - self.monoviewClassifiers = Parallel(n_jobs=self.nbCores)( - delayed(fitMonoviewClassifier)(self.monoviewClassifiers[index], - get_v(DATASET, viewIndex, trainIndices), - labels[trainIndices], - self.needProbas, self.randomState) - for index, viewIndex in enumerate(viewsIndices)) diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/BayesianInference.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/BayesianInference.py deleted file mode 100644 index 61ec3838f238adeafa98a02471890e58eb1ccbd2..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/BayesianInference.py +++ /dev/null @@ -1,94 +0,0 @@ -import numpy as np -from sklearn.metrics import accuracy_score -import pkgutil - -from .....utils.dataset import get_v -from ..... import monoview_classifiers -from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - nbView = classificationKWARGS["nbView"] - paramsSets = [] - for _ in range(nIter): - randomWeightsArray = randomState.random_sample(nbView) - normalizedArray = randomWeightsArray / np.sum(randomWeightsArray) - paramsSets.append([normalizedArray]) - return paramsSets - - -def getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, classificationIndices): - if args.FU_L_cl_names != ['']: - args.FU_L_select_monoview = "user_defined" - else: - monoviewClassifierModulesNames = benchmark["monoview"] - args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, - viewsIndices, resultsMonoview, classificationIndices) - monoviewClassifierModules = [getattr(monoview_classifiers, classifierName) - for classifierName in args.FU_L_cl_names] - if args.FU_L_cl_names == [""] and args.CL_type == ["multiview"]: - raise AttributeError("You must perform monoview classification or specify " - "which monoview classifier to use Late fusion") - if args.FU_L_cl_config != ['']: - classifiersConfigs = [ - monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")]) - for monoviewClassifierModule, classifierConfig - in zip(monoviewClassifierModules, args.FU_L_cl_config)] - else: - classifiersConfigs = getConfig(args.FU_L_cl_names, resultsMonoview, viewsIndices) - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "LateFusion", - "fusionMethod": "BayesianInference", - "classifiersNames": args.FU_L_cl_names, - "classifiersConfigs": classifiersConfigs, - 'fusionMethodConfig': args.FU_L_method_config, - 'monoviewSelection': args.FU_L_select_monoview, - "nbView": (len(viewsIndices))}} - return [arguments] - - -class BayesianInference(LateFusionClassifier): - def __init__(self, randomState, NB_CORES=1, **kwargs): - LateFusionClassifier.__init__(self, randomState, kwargs['classifiersNames'], kwargs['classifiersConfigs'], - kwargs["monoviewSelection"], - NB_CORES=NB_CORES) - if kwargs['fusionMethodConfig'][0] is None or kwargs['fusionMethodConfig'] == ['']: - self.weights = np.array([1.0 for _ in kwargs['classifiersNames']]) - else: - self.weights = np.array(map(float, kwargs['fusionMethodConfig'][0])) - self.needProbas = True - - def setParams(self, paramsSet): - self.weights = paramsSet[0] - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if viewsIndices is None: - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - if sum(self.weights) != 1.0: - self.weights = self.weights / sum(self.weights) - - viewScores = []#np.zeros((nbView, len(usedIndices), DATASET.get("Metadata").attrs["nbClass"])) - for index, viewIndex in enumerate(viewsIndices): - viewScores.append(np.power( - self.monoviewClassifiers[index].predict_proba(get_v(DATASET, viewIndex, usedIndices)), - self.weights[index])) - viewScores = np.array(viewScores) - predictedLabels = np.argmax(np.prod(viewScores, axis=0), axis=1) - return predictedLabels - - def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs): - configString = "with Bayesian Inference using a weight for each view : " + ", ".join(map(str, self.weights)) + \ - "\n\t-With monoview classifiers : " - for monoviewClassifier in self.monoviewClassifiers: - - configString += monoviewClassifier.getConfig() - configString += "\n\t -Method used to select monoview classifiers : " + self.monoviewSelection - return configString diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/MajorityVoting.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/MajorityVoting.py deleted file mode 100644 index bcdbfa8222d9f4aa79333dbf52132dff757da227..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/MajorityVoting.py +++ /dev/null @@ -1,106 +0,0 @@ -import numpy as np -# from sklearn.metrics import accuracy_score -# import pkgutil - -from .....utils.dataset import get_v -from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig -from ..... import monoview_classifiers - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - nbView = classificationKWARGS["nbView"] - paramsSets = [] - for _ in range(nIter): - randomWeightsArray = randomState.random_sample(nbView) - normalizedArray = randomWeightsArray / np.sum(randomWeightsArray) - paramsSets.append([normalizedArray]) - return paramsSets - - -def getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, classificationIndices): - if args.FU_L_cl_names != ['']: - pass - else: - monoviewClassifierModulesNames = benchmark["monoview"] - args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, - viewsIndices, resultsMonoview, classificationIndices) - monoviewClassifierModules = [getattr(monoview_classifiers, classifierName) - for classifierName in args.FU_L_cl_names] - if args.FU_L_cl_names == [""] and args.CL_type == ["multiview"]: - raise AttributeError("You must perform monoview classification or specify " - "which monoview classifier to use Late fusion") - if args.FU_L_cl_config != ['']: - classifiersConfigs = [ - monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")]) - for monoviewClassifierModule, classifierConfig - in zip(monoviewClassifierModules, args.FU_L_cl_config)] - else: - classifiersConfigs = getConfig(args.FU_L_cl_names, resultsMonoview, viewsIndices) - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "LateFusion", - "fusionMethod": "MajorityVoting", - "classifiersNames": args.FU_L_cl_names, - "classifiersConfigs": classifiersConfigs, - 'fusionMethodConfig': args.FU_L_method_config, - 'monoviewSelection': args.FU_L_select_monoview, - "nbView": (len(viewsIndices))}} - return [arguments] - - -class MajorityVoting(LateFusionClassifier): - def __init__(self, randomState, NB_CORES=1, **kwargs): - LateFusionClassifier.__init__(self, randomState, kwargs['classifiersNames'], kwargs['classifiersConfigs'], - kwargs["monoviewSelection"], - NB_CORES=NB_CORES) - if kwargs['fusionMethodConfig'][0] is None or kwargs['fusionMethodConfig'] == ['']: - self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float) - else: - self.weights = np.array(map(float, kwargs['fusionMethodConfig'][0])) - - def setParams(self, paramsSet): - self.weights = np.array(paramsSet[0]) - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - self.weights /= float(sum(self.weights)) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - - datasetLength = len(usedIndices) - votes = np.zeros((datasetLength, DATASET.get("Metadata").attrs["nbClass"]), dtype=float) - monoViewDecisions = np.zeros((len(usedIndices), nbView), dtype=int) - for index, viewIndex in enumerate(viewsIndices): - monoViewDecisions[:, index] = self.monoviewClassifiers[index].predict( - get_v(DATASET, viewIndex, usedIndices)) - for exampleIndex in range(datasetLength): - for viewIndex, featureClassification in enumerate(monoViewDecisions[exampleIndex, :]): - votes[exampleIndex, featureClassification] += self.weights[viewIndex] - nbMaximum = len(np.where(votes[exampleIndex] == max(votes[exampleIndex]))[0]) - try: - assert nbMaximum != nbView - except: - print("Majority voting can't decide, each classifier has voted for a different class") - raise - predictedLabels = np.argmax(votes, axis=1) - # Can be upgraded by restarting a new classification process if - # there are multiple maximums ?: - # while nbMaximum>1: - # relearn with only the classes that have a maximum number of vote - # votes = revote - # nbMaximum = len(np.where(votes==max(votes))[0]) - return predictedLabels - - def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs): - configString = "with Majority Voting \n\t-With weights : " + str( - self.weights) + "\n\t-With monoview classifiers : " - for monoviewClassifier in self.monoviewClassifiers: - - configString += monoviewClassifier.getConfig() - return configString diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/SCMForLinear.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/SCMForLinear.py deleted file mode 100644 index 739ba0233224cd84057d66ec2b9442a72cbf69b2..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/SCMForLinear.py +++ /dev/null @@ -1,186 +0,0 @@ -import numpy as np - -from pyscm.scm import SetCoveringMachineClassifier as scm -from sklearn.base import BaseEstimator, ClassifierMixin -from sklearn.externals.six import iteritems, iterkeys, itervalues - -from sklearn.metrics import accuracy_score -import itertools - -from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig -from ..... import monoview_classifiers -from .....utils.dataset import get_v - - -class DecisionStumpSCMNew(BaseEstimator, ClassifierMixin): - """docstring for SCM - A hands on class of SCM using decision stump, built with sklearn format in order to use sklearn function on SCM like - CV, gridsearch, and so on ...""" - - def __init__(self, model_type='conjunction', p=0.1, max_rules=10, random_state=42): - super(DecisionStumpSCMNew, self).__init__() - self.model_type = model_type - self.p = p - self.max_rules = max_rules - self.random_state = random_state - - def fit(self, X, y): - self.clf = scm(model_type=self.model_type, max_rules=self.max_rules, p=self.p, random_state=self.random_state) - self.clf.fit(X=X, y=y) - - def predict(self, X): - return self.clf.predict(X) - - def set_params(self, **params): - for key, value in iteritems(params): - if key == 'p': - self.p = value - if key == 'model_type': - self.model_type = value - if key == 'max_rules': - self.max_rules = value - - def get_stats(self): - return {"Binary_attributes": self.clf.model_.rules} - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - paramsSets = [] - for _ in range(nIter): - max_attributes = randomState.randint(1, 20) - p = randomState.random_sample() - model = randomState.choice(["conjunction", "disjunction"]) - order = randomState.randint(1, 10) - paramsSets.append([p, max_attributes, model, order]) - return paramsSets - - -def getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, classificationIndices): - if args.FU_L_cl_names != ['']: - pass - else: - monoviewClassifierModulesNames = benchmark["monoview"] - args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, - viewsIndices, resultsMonoview, classificationIndices) - monoviewClassifierModules = [getattr(monoview_classifiers, classifierName) - for classifierName in args.FU_L_cl_names] - if args.FU_L_cl_names == [""] and args.CL_type == ["multiview"]: - raise AttributeError("You must perform monoview classification or specify " - "which monoview classifier to use Late fusion") - if args.FU_L_cl_config != ['']: - classifiersConfigs = [ - monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")]) - for monoviewClassifierModule, classifierConfig - in zip(monoviewClassifierModules, args.FU_L_cl_config)] - else: - classifiersConfigs = getConfig(args.FU_L_cl_names, resultsMonoview, viewsIndices) - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "LateFusion", - "fusionMethod": "SCMForLinear", - "classifiersNames": args.FU_L_cl_names, - "classifiersConfigs": classifiersConfigs, - 'fusionMethodConfig': args.FU_L_method_config, - 'monoviewSelection': args.FU_L_select_monoview, - "nbView": (len(viewsIndices))}} - return [arguments] - - -class SCMForLinear(LateFusionClassifier): - def __init__(self, randomState, NB_CORES=1, **kwargs): - LateFusionClassifier.__init__(self, randomState, kwargs['classifiersNames'], kwargs['classifiersConfigs'], - kwargs["monoviewSelection"], - NB_CORES=NB_CORES) - self.SCMClassifier = None - if kwargs['fusionMethodConfig'][0] is None or kwargs['fusionMethodConfig'] == ['']: - self.p = 1 - self.maxAttributes = 5 - self.order = 1 - self.modelType = "conjunction" - else: - self.p = int(kwargs['fusionMethodConfig'][0]) - self.maxAttributes = int(kwargs['fusionMethodConfig'][1]) - self.order = int(kwargs['fusionMethodConfig'][2]) - self.modelType = kwargs['fusionMethodConfig'][3] - - def setParams(self, paramsSet): - self.p = paramsSet[0] - self.maxAttributes = paramsSet[1] - self.order = paramsSet[3] - self.modelType = paramsSet[2] - - def fit_hdf5(self, DATASET, labels, trainIndices=None, viewsIndices=None): - if viewsIndices is None: - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - if trainIndices is None: - trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - for index, viewIndex in enumerate(viewsIndices): - self.monoviewClassifiers[index].fit(get_v(DATASET, viewIndex, trainIndices), - labels[trainIndices]) - self.SCMForLinearFusionFit(DATASET, labels, usedIndices=trainIndices, viewsIndices=viewsIndices) - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if viewsIndices is None: - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - monoviewDecisions = np.zeros((len(usedIndices), nbView), dtype=int) - # accus = [] - for index, viewIndex in enumerate(viewsIndices): - monoviewDecision = self.monoviewClassifiers[index].predict( - get_v(DATASET, viewIndex, usedIndices)) - # accus.append(accuracy_score(DATASET.get("Labels").value[usedIndices], monoviewDecision)) - monoviewDecisions[:, index] = monoviewDecision - features = self.generateInteractions(monoviewDecisions) - predictedLabels = self.SCMClassifier.predict(features) - return predictedLabels - - def SCMForLinearFusionFit(self, DATASET, labels, usedIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - - nbView = len(viewsIndices) - self.SCMClassifier = DecisionStumpSCMNew(p=self.p, max_rules=self.maxAttributes, model_type=self.modelType, - random_state=self.randomState) - monoViewDecisions = np.zeros((len(usedIndices), nbView), dtype=int) - for index, viewIndex in enumerate(viewsIndices): - monoViewDecisions[:, index] = self.monoviewClassifiers[index].predict( - get_v(DATASET, viewIndex, usedIndices)) - features = self.generateInteractions(monoViewDecisions) - features = np.array([np.array([feat for feat in feature]) for feature in features]) - self.SCMClassifier.fit(features, labels[usedIndices].astype(int)) - - def generateInteractions(self, monoViewDecisions): - if type(self.order) == type(None): - self.order = monoViewDecisions.shape[1] - if self.order == 1: - return monoViewDecisions - else: - genratedIntercations = [monoViewDecisions[:, i] for i in range(monoViewDecisions.shape[1])] - for orderIndex in range(self.order - 1): - combins = itertools.combinations(range(monoViewDecisions.shape[1]), orderIndex + 2) - for combin in combins: - generatedDecision = monoViewDecisions[:, combin[0]] - for index in range(len(combin) - 1): - if self.modelType == "disjunction": - generatedDecision = np.logical_and(generatedDecision, - monoViewDecisions[:, combin[index + 1]]) - else: - generatedDecision = np.logical_or(generatedDecision, - monoViewDecisions[:, combin[index + 1]]) - genratedIntercations.append(generatedDecision) - return np.transpose(np.array(genratedIntercations)) - - def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs): - configString = "with SCM for linear with max_attributes : " + str(self.maxAttributes) + ", p : " + str(self.p) + \ - " model_type : " + str(self.modelType) + " order : " + str(self.order)+ " has chosen " + \ - str(0.1) + " rule(s) \n\t-With monoview classifiers : " - for monoviewClassifier in self.monoviewClassifiers: - - configString += monoviewClassifier.getConfig() - return configString diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/SVMForLinear.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/SVMForLinear.py deleted file mode 100644 index 509256190aaa545ae1dbae70083c183ff24f4ec8..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/SVMForLinear.py +++ /dev/null @@ -1,103 +0,0 @@ -import numpy as np -from sklearn.multiclass import OneVsOneClassifier -from sklearn.svm import SVC -import pkgutil - -from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig -from .....utils.dataset import get_v -from ..... import monoview_classifiers - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - paramsSets = [] - for _ in range(nIter): - paramsSets.append([]) - return paramsSets - - -def getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, classificationIndices): - if args.FU_L_cl_names != ['']: - pass - else: - monoviewClassifierModulesNames = benchmark["monoview"] - args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, - viewsIndices, resultsMonoview, classificationIndices) - monoviewClassifierModules = [getattr(monoview_classifiers, classifierName) - for classifierName in args.FU_L_cl_names] - if args.FU_L_cl_names == [""] and args.CL_type == ["multiview"]: - raise AttributeError("You must perform monoview classification or specify " - "which monoview classifier to use Late fusion") - if args.FU_L_cl_config != ['']: - classifiersConfigs = [ - monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")]) - for monoviewClassifierModule, classifierConfig - in zip(monoviewClassifierModules, args.FU_L_cl_config)] - else: - classifiersConfigs = getConfig(args.FU_L_cl_names, resultsMonoview, viewsIndices) - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "LateFusion", - "fusionMethod": "SVMForLinear", - "classifiersNames": args.FU_L_cl_names, - "classifiersConfigs": classifiersConfigs, - 'fusionMethodConfig': args.FU_L_method_config, - 'monoviewSelection': args.FU_L_select_monoview, - "nbView": (len(viewsIndices))}} - return [arguments] - - -class SVMForLinear(LateFusionClassifier): - def __init__(self, randomState, NB_CORES=1, **kwargs): - LateFusionClassifier.__init__(self, randomState, kwargs['classifiersNames'], kwargs['classifiersConfigs'], - kwargs["monoviewSelection"], - NB_CORES=NB_CORES) - self.SVMClassifier = None - - def fit_hdf5(self, DATASET, labels, trainIndices=None, viewsIndices=None): - if viewsIndices is None: - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - if trainIndices is None: - trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - for index, viewIndex in enumerate(viewsIndices): - self.monoviewClassifiers[index].fit(get_v(DATASET, viewIndex, trainIndices), - labels[trainIndices]) - self.SVMForLinearFusionFit(DATASET, labels, usedIndices=trainIndices, viewsIndices=viewsIndices) - - def setParams(self, paramsSet): - pass - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if viewsIndices is None: - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - monoviewDecisions = np.zeros((len(usedIndices), nbView), dtype=int) - for index, viewIndex in enumerate(viewsIndices): - monoviewDecisions[:, index] = self.monoviewClassifiers[index].predict( - get_v(DATASET, viewIndex, usedIndices)) - predictedLabels = self.SVMClassifier.predict(monoviewDecisions) - return predictedLabels - - def SVMForLinearFusionFit(self, DATASET, labels, usedIndices=None, viewsIndices=None): - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - self.SVMClassifier = OneVsOneClassifier(SVC()) - monoViewDecisions = np.zeros((len(usedIndices), nbView), dtype=int) - for index, viewIndex in enumerate(viewsIndices): - monoViewDecisions[:, index] = self.monoviewClassifiers[index].predict( - get_v(DATASET, viewIndex, usedIndices)) - - self.SVMClassifier.fit(monoViewDecisions, labels[usedIndices]) - - def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs): - configString = "with SVM for linear \n\t-With monoview classifiers : " - for monoviewClassifier in self.monoviewClassifiers: - - configString += monoviewClassifier.getConfig() - return configString diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/WeightedLinear.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/WeightedLinear.py deleted file mode 100644 index baf9c56b9fab46b7702b0754da894a4e053e044f..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/WeightedLinear.py +++ /dev/null @@ -1,91 +0,0 @@ -import numpy as np -from sklearn.metrics import accuracy_score -import pkgutil - -from ..... import monoview_classifiers -from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig -from .....utils.dataset import get_v - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - nbView = classificationKWARGS["nbView"] - paramsSets = [] - for _ in range(nIter): - randomWeightsArray = randomState.random_sample(nbView) - normalizedArray = randomWeightsArray / np.sum(randomWeightsArray) - paramsSets.append([normalizedArray]) - return paramsSets - - -def getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, classificationIndices): - if args.FU_L_cl_names != ['']: - pass - else: - monoviewClassifierModulesNames = benchmark["monoview"] - args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, - viewsIndices, resultsMonoview, classificationIndices) - monoviewClassifierModules = [getattr(monoview_classifiers, classifierName) - for classifierName in args.FU_L_cl_names] - if args.FU_L_cl_names == [""] and args.CL_type == ["multiview"]: - raise AttributeError("You must perform monoview classification or specify " - "which monoview classifier to use Late fusion") - if args.FU_L_cl_config != ['']: - classifiersConfigs = [ - monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")]) - for monoviewClassifierModule, classifierConfig - in zip(monoviewClassifierModules, args.FU_L_cl_config)] - else: - classifiersConfigs = getConfig(args.FU_L_cl_names, resultsMonoview, viewsIndices) - arguments = {"CL_type": "fusion", - "views": views, - "NB_VIEW": len(views), - "viewsIndices": viewsIndices, - "NB_CLASS": len(args.CL_classes), - "LABELS_NAMES": args.CL_classes, - "FusionKWARGS": {"fusionType": "LateFusion", - "fusionMethod": "WeightedLinear", - "classifiersNames": args.FU_L_cl_names, - "classifiersConfigs": classifiersConfigs, - 'fusionMethodConfig': args.FU_L_method_config, - 'monoviewSelection': args.FU_L_select_monoview, - "nbView": (len(viewsIndices))}} - return [arguments] - - -class WeightedLinear(LateFusionClassifier): - def __init__(self, randomState, NB_CORES=1, **kwargs): - LateFusionClassifier.__init__(self, randomState, kwargs['classifiersNames'], kwargs['classifiersConfigs'], - kwargs["monoviewSelection"], - NB_CORES=NB_CORES) - if kwargs['fusionMethodConfig'][0] is None or kwargs['fusionMethodConfig'] == ['']: - self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float) - else: - self.weights = np.array(map(float, kwargs['fusionMethodConfig'][0])) - self.needProbas = True - - def setParams(self, paramsSet): - self.weights = paramsSet[0] - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if viewsIndices is None: - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - self.weights /= float(sum(self.weights)) - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - viewScores = []#np.zeros((nbView, len(usedIndices), DATASET.get("Metadata").attrs["nbClass"])) - for index, viewIndex in enumerate(viewsIndices): - viewScores.append(np.array(self.monoviewClassifiers[index].predict_proba( - get_v(DATASET, viewIndex, usedIndices))) * self.weights[index]) - viewScores = np.array(viewScores) - predictedLabels = np.argmax(np.sum(viewScores, axis=0), axis=1) - - return predictedLabels - - def getConfig(self, fusionMethodConfig, monoviewClassifiersNames, monoviewClassifiersConfigs): - configString = "with Weighted linear using a weight for each view : " + ", ".join(map(str, self.weights)) + \ - "\n\t-With monoview classifiers : " - for monoviewClassifier in self.monoviewClassifiers: - - configString += monoviewClassifier.getConfig() - return configString diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/__init__.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/__init__.py deleted file mode 100644 index 3636ca90ab36db2f57b006402a3c6767d9395117..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -import os - -for module in os.listdir(os.path.dirname(os.path.realpath(__file__))): - if module == '__init__.py' or module[-3:] != '.py': - continue - __import__(module[:-3], locals(), globals(), [], 1) -del module -del os diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/__init__.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/__init__.py deleted file mode 100644 index a09dbec153951124f73795359bcc3653ef2fb1d7..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -# import pdb;pdb.set_trace() -from . import EarlyFusion -from . import LateFusion -from . import LateFusionPackage -from . import EarlyFusionPackage - -__all__ = ["EarlyFusionPackage", "LateFusionPackage"] diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/__init__.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/__init__.py deleted file mode 100644 index 0f8d47a0e70080f0e3db6dc2957ad10dad0079a2..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from . import fusion, analyze_results, Methods - -__all__ = ["fusion.py", "Methods"] diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/analyze_results.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/analyze_results.py deleted file mode 100644 index 70b11657f805e88568cc261a543a3a65f55abbab..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/analyze_results.py +++ /dev/null @@ -1,21 +0,0 @@ -from ...multiview import analyze_results - -# Author-Info -__author__ = "Baptiste Bauvin" -__status__ = "Prototype" # Production, Development, Prototype - - -def execute(classifier, trainLabels, - testLabels, DATASET, - classificationKWARGS, classificationIndices, - LABELS_DICTIONARY, views, nbCores, times, - name, KFolds, - hyperParamSearch, nIter, metrics, - viewsIndices, randomState, labels, classifierModule): - return analyze_results.execute(classifier, trainLabels, - testLabels, DATASET, - classificationKWARGS, classificationIndices, - LABELS_DICTIONARY, views, nbCores, times, - name, KFolds, - hyperParamSearch, nIter, metrics, - viewsIndices, randomState, labels, classifierModule) diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/fusion.py deleted file mode 100644 index c26387fb1057cb648ad97a8be10d0e54da1a5082..0000000000000000000000000000000000000000 --- a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/fusion/fusion.py +++ /dev/null @@ -1,210 +0,0 @@ -import logging -import pkgutil - -import numpy as np - -# from Methods import * - -try: - from . import Methods -except ValueError: - import pdb;pdb.set_trace() - -from ... import monoview_classifiers -from ...utils.dataset import get_v - -# Author-Info -__author__ = "Baptiste Bauvin" -__status__ = "Prototype" # Production, Development, Prototype - - -def genName(config): - if config["fusionType"] == "LateFusion": - classifierRedNames = [classifierName[:4] for classifierName in config["classifiersNames"]] - return "Late-" + str(config["fusionMethod"][:4])#+"-"+"-".join(classifierRedNames) - elif config["fusionType"] == "EarlyFusion": - monoview_short_name = getattr(getattr(monoview_classifiers, config["classifiersNames"]), - config["classifiersNames"])().get_name_for_fusion() - return "Early-" + config["fusionMethod"][:4] + "-" + monoview_short_name - - -def getBenchmark(benchmark, args=None): - """Used to generate the list of fusion classifiers for the benchmark""" - - ##### PLaceholder - # To aviod problems with the new args, as Multiview will be reworked - args = None - ########### - - fusionModulesNames = [name for _, name, isPackage - in pkgutil.iter_modules(['./mono_multi_view_classifiers/multiview_classifiers/fusion/Methods']) if not isPackage] - fusionMethods = dict((fusionModulesName, [name for _, name, isPackage in - pkgutil.iter_modules( - ["./mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/" + fusionModulesName + "Package"]) - if not isPackage]) - for fusionModulesName in fusionModulesNames) - if args is None: - allMonoviewAlgos = [name for _, name, isPackage in - pkgutil.iter_modules(['./mono_multi_view_classifiers/monoview_classifiers']) - if (not isPackage)] - fusionMonoviewClassifiers = allMonoviewAlgos - allFusionAlgos = {"Methods": fusionMethods, "Classifiers": fusionMonoviewClassifiers} - benchmark["multiview"]["fusion"] = allFusionAlgos - else: - benchmark["multiview"]["fusion"] = {} - if args.FU_types != [""]: - benchmark["multiview"]["fusion"]["Methods"] = dict( - (fusionType, []) for fusionType in args.FU_types) - else: - benchmark["multiview"]["fusion"]["Methods"] = dict( - (fusionModulesName, "_") for fusionModulesName in fusionModulesNames) - if "LateFusion" in benchmark["multiview"]["fusion"]["Methods"]: - if args.FU_late_methods == [""]: - benchmark["multiview"]["fusion"]["Methods"]["LateFusion"] = [name for _, name, isPackage in - pkgutil.iter_modules([ - "./mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/LateFusionPackage"]) - if not isPackage] - else: - benchmark["multiview"]["fusion"]["Methods"]["LateFusion"] = args.FU_late_methods - if "EarlyFusion" in benchmark["multiview"]["fusion"]["Methods"]: - if args.FU_early_methods == [""]: - benchmark["multiview"]["fusion"]["Methods"]["EarlyFusion"] = [name for _, name, isPackage in - pkgutil.iter_modules([ - "./mono_multi_view_classifiers/multiview_classifiers/fusion/Methods/EarlyFusionPackage"]) - if not isPackage] - else: - benchmark["multiview"]["fusion"]["Methods"]["EarlyFusion"] = args.FU_early_methods - if args.CL_algos_monoview == ['']: - benchmark["multiview"]["fusion"]["Classifiers"] = [name for _, name, isPackage in - pkgutil.iter_modules(['./mono_multi_view_classifiers/monoview_classifiers']) - if (not isPackage) and (name != "SGD") and ( - name[:3] != "SVM") - and (name != "SCM")] - else: - benchmark["multiview"]["fusion"]["Classifiers"] = args.CL_algos_monoview - return benchmark - - -def getArgs(args, benchmark, views, viewsIndices, randomState, directory, resultsMonoview, classificationIndices): - """Used to generate the list of arguments for each fusion experimentation""" - if not "monoview" in benchmark and not args.FU_L_select_monoview in ["randomClf", "Determined"]: - args.FU_L_select_monoview = "randomClf" - argumentsList = [] - for fusionType in benchmark["multiview"]["fusion"]["Methods"]: - fusionTypePackage = getattr(Methods, fusionType + "Package") - for fusionMethod in benchmark["multiview"]["fusion"]["Methods"][fusionType]: - fusionMethodModule = getattr(fusionTypePackage, fusionMethod) - arguments = fusionMethodModule.getArgs(benchmark, args, views, viewsIndices, directory, resultsMonoview, - classificationIndices) - argumentsList += arguments - return argumentsList - - -def makeMonoviewData_hdf5(DATASET, weights=None, usedIndices=None, viewsIndices=None): - """Used to concatenate the viewsin one big monoview dataset""" - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - if not usedIndices: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - NB_VIEW = len(viewsIndices) - if weights is None: - weights = np.array([1 / NB_VIEW for i in range(NB_VIEW)]) - if sum(weights) != 1: - weights = weights / sum(weights) - monoviewData = np.concatenate([weights[index] * get_v(DATASET, viewIndex, usedIndices) - for index, viewIndex in enumerate(viewsIndices)], axis=1) - return monoviewData - - -def genParamsSets(classificationKWARGS, randomState, nIter=1): - """Used to generate parameters sets for the random hyper parameters optimization function""" - fusionTypeName = classificationKWARGS["fusionType"] - fusionTypePackage = getattr(Methods, fusionTypeName + "Package") - fusionMethodModuleName = classificationKWARGS["fusionMethod"] - fusionMethodModule = getattr(fusionTypePackage, fusionMethodModuleName) - fusionMethodConfig = fusionMethodModule.genParamsSets(classificationKWARGS, randomState, nIter=nIter) - return fusionMethodConfig - - -# def gridSearch_hdf5(DATASET, viewsIndices, classificationKWARGS, learningIndices, metric=None, nIter=30): -# if type(viewsIndices) == type(None): -# viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) -# fusionTypeName = classificationKWARGS["fusionType"] -# fusionTypePackage = globals()[fusionTypeName + "Package"] -# fusionMethodModuleName = classificationKWARGS["fusionMethod"] -# fusionMethodModule = getattr(fusionTypePackage, fusionMethodModuleName) -# classifiersNames = classificationKWARGS["classifiersNames"] -# bestSettings = [] -# for classifierIndex, classifierName in enumerate(classifiersNames): -# logging.debug("\tStart:\t Random search for " + classifierName + " with " + str(nIter) + " iterations") -# classifierModule = getattr(monoview_classifiers, classifierName) -# classifierMethod = getattr(classifierModule, "hyperParamSearch") -# if fusionTypeName == "LateFusion": -# bestSettings.append(classifierMethod(get_v(DATASET, viewsIndices[classifierIndex], learningIndices), -# DATASET.get("Labels")[learningIndices], metric=metric, -# nIter=nIter)) -# else: -# bestSettings.append( -# classifierMethod(makeMonoviewData_hdf5(DATASET, usedIndices=learningIndices, viewsIndices=viewsIndices), -# DATASET.get("Labels")[learningIndices], metric=metric, -# nIter=nIter)) -# logging.debug("\tDone:\t Random search for " + classifierName) -# classificationKWARGS["classifiersConfigs"] = bestSettings -# logging.debug("\tStart:\t Random search for " + fusionMethodModuleName) -# fusionMethodConfig = fusionMethodModule.gridSearch(DATASET, classificationKWARGS, learningIndices, nIter=nIter, -# viewsIndices=viewsIndices) -# logging.debug("\tDone:\t Random search for " + fusionMethodModuleName) -# return bestSettings, fusionMethodConfig - - - -class FusionClass: - """The global representant of fusion""" - def __init__(self, randomState, NB_CORES=1, **kwargs): - fusionType = kwargs['fusionType'] - fusionMethod = kwargs['fusionMethod'] - fusionTypePackage = getattr(Methods, fusionType + "Package") - fusionMethodModule = getattr(fusionTypePackage, fusionMethod) - fusionMethodClass = getattr(fusionMethodModule, fusionMethod) - nbCores = NB_CORES - classifierKWARGS = dict( - (key, value) for key, value in kwargs.items() if key not in ['fusionType', 'fusionMethod']) - self.classifier = fusionMethodClass(randomState, NB_CORES=nbCores, **classifierKWARGS) - - def setParams(self, paramsSet): - self.classifier.setParams(paramsSet) - - def fit_hdf5(self, DATASET, labels, trainIndices=None, viewsIndices=None, metric=["f1_score", None]): - self.classifier.fit_hdf5(DATASET, labels, trainIndices=trainIndices, viewsIndices=viewsIndices) - - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - if type(viewsIndices) == type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - predictedLabels = self.classifier.predict_hdf5(DATASET, usedIndices=usedIndices, viewsIndices=viewsIndices) - return predictedLabels - - def predict_probas_hdf5(self, DATASET, usedIndices=None): - if usedIndices is None: - usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) - if usedIndices: - predictedLabels = self.classifier.predict_probas_hdf5(DATASET, usedIndices=usedIndices) - else: - predictedLabels = [] - return predictedLabels - - def getConfigString(self, classificationKWARGS): - monoviewClassifiersNames = classificationKWARGS["classifiersNames"] - monoviewClassifiersConfigs = classificationKWARGS["classifiersConfigs"] - fusionMethodConfig = classificationKWARGS["fusionMethodConfig"] - return self.classifier.getConfig(fusionMethodConfig, monoviewClassifiersNames, - monoviewClassifiersConfigs) - - def getSpecificAnalysis(self, classificationKWARGS): - fusionType = classificationKWARGS["fusionType"] - if fusionType == "LateFusion": - stringAnalysis = Methods.LateFusion.getScores(self) - else: - stringAnalysis = '' - return stringAnalysis diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/majority_voting_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/majority_voting_fusion.py new file mode 100644 index 0000000000000000000000000000000000000000..eed9638ee1d2aec59a80ef51150d6ad3c9f0f976 --- /dev/null +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/majority_voting_fusion.py @@ -0,0 +1,44 @@ +import numpy as np + +from ..multiview_classifiers.additions.late_fusion_utils import LateFusionClassifier +from ..multiview.multiview_utils import get_examples_views_indices + + +classifier_class_name = "MajorityVoting" + +class VotingIndecision(Exception): + pass + +class MajorityVoting(LateFusionClassifier): + def __init__(self, random_state, classifier_names=None, + classifier_configs=None, nb_view=None, nb_cores=1): + super(MajorityVoting, self).__init__(random_state=random_state, + classifier_names=classifier_names, + classifier_configs=classifier_configs, + nb_cores=nb_cores, + nb_view=nb_view) + + def predict(self, X, example_indices=None, views_indices=None): + examples_indices, views_indices = get_examples_views_indices(X, + example_indices, + views_indices) + + n_examples = len(examples_indices) + votes = np.zeros((n_examples, X.get_nb_class(example_indices)), dtype=float) + monoview_decisions = np.zeros((len(examples_indices), self.nb_view), dtype=int) + for index, view_index in enumerate(views_indices): + monoview_decisions[:, index] = self.monoviewClassifiers[index].predict( + X.get_v(view_index, examples_indices)) + for example_index in range(n_examples): + for view_index, feature_classification in enumerate(monoview_decisions[example_index, :]): + votes[example_index, feature_classification] += self.weights[view_index] + nb_maximum = len(np.where(votes[example_index] == max(votes[example_index]))[0]) + if nb_maximum == self.nb_view: + raise VotingIndecision("Majority voting can't decide, each classifier has voted for a different class") + + predicted_labels = np.argmax(votes, axis=1) + # Can be upgraded by restarting a new classification process if + # there are multiple maximums ?: + # while nbMaximum>1: + # relearn with only the classes that have a maximum number of vote + return predicted_labels diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/scm_late_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/scm_late_fusion.py new file mode 100644 index 0000000000000000000000000000000000000000..2e3fae5d703dee2dc186847dab8896fd52a21299 --- /dev/null +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/scm_late_fusion.py @@ -0,0 +1,124 @@ +import numpy as np +from sklearn.base import BaseEstimator, ClassifierMixin +from sklearn.externals.six import iteritems +import itertools +from pyscm.scm import SetCoveringMachineClassifier as scm + + +from ..multiview_classifiers.additions.late_fusion_utils import \ + LateFusionClassifier +from ..multiview.multiview_utils import get_examples_views_indices +from ..monoview.monoview_utils import CustomRandint, CustomUniform + +classifier_class_name = "SCMLateFusionClassifier" + + +class DecisionStumpSCMNew(BaseEstimator, ClassifierMixin): + """docstring for SCM + A hands on class of SCM using decision stump, built with sklearn format in order to use sklearn function on SCM like + CV, gridsearch, and so on ...""" + + def __init__(self, model_type='conjunction', p=0.1, max_rules=10, random_state=42): + super(DecisionStumpSCMNew, self).__init__() + self.model_type = model_type + self.p = p + self.max_rules = max_rules + self.random_state = random_state + + def fit(self, X, y): + self.clf = scm(model_type=self.model_type, max_rules=self.max_rules, p=self.p, random_state=self.random_state) + self.clf.fit(X=X, y=y) + + def predict(self, X): + return self.clf.predict(X) + + def set_params(self, **params): + for key, value in iteritems(params): + if key == 'p': + self.p = value + if key == 'model_type': + self.model_type = value + if key == 'max_rules': + self.max_rules = value + + def get_stats(self): + return {"Binary_attributes": self.clf.model_.rules} + + +class SCMLateFusionClassifier(LateFusionClassifier): + def __init__(self, random_state=None, classifier_names=None, + classifier_configs=None, nb_cores=1, nb_view=1, + p=1, max_attributes=5, order=1, model_type="conjunction"): + super(SCMLateFusionClassifier, self).__init__(random_state=random_state, + classifier_names=classifier_names, + classifier_configs=classifier_configs, + nb_cores=nb_cores, + nb_view=nb_view) + self.scm_classifier = None + self.p = p + self.max_attributes = max_attributes + self.order = order + self.model_type = model_type + self.param_names+=["model_type", "max_rules", "p", "order"] + self.distribs+=[["conjunction", "disjunction"], + CustomRandint(low=1, high=15), + CustomUniform(loc=0, state=1), [1,2,3]] + + def fit(self, X, y, train_indices=None, view_indices=None): + super(SCMLateFusionClassifier, self).fit(X, y, + train_indices=train_indices, + views_indices=view_indices) + self.scm_fusion_fit(X, y, train_indices=train_indices, view_indices=view_indices) + return self + + def predict(self, X, example_indices=None, view_indices=None): + example_indices, view_indices = get_examples_views_indices(X, + example_indices, + view_indices) + monoview_decisions = np.zeros((len(example_indices), self.nb_view), + dtype=int) + for index, view_index in enumerate(view_indices): + monoview_decision = self.monoview_estimators[index].predict( + X.get_v(view_index, example_indices)) + monoview_decisions[:, index] = monoview_decision + features = self.generate_interactions(monoview_decisions) + predicted_labels = self.scm_classifier.predict(features) + return predicted_labels + + def scm_fusion_fit(self, X, y, train_indices=None, view_indices=None): + train_indices, view_indices = get_examples_views_indices(X, train_indices, view_indices) + + self.scm_classifier = DecisionStumpSCMNew(p=self.p, max_rules=self.max_attributes, model_type=self.model_type, + random_state=self.randomState) + monoview_decisions = np.zeros((len(train_indices), self.nb_view), dtype=int) + for index, view_index in enumerate(view_indices): + monoview_decisions[:, index] = self.monoviewClassifiers[index].predict( + X.get_v(view_index, train_indices)) + features = self.generate_interactions(monoview_decisions) + features = np.array([np.array([feat for feat in feature]) + for feature in features]) + self.scm_classifier.fit(features, y[train_indices].astype(int)) + + def generate_interactions(self, monoview_decisions): + if self.order is None: + self.order = monoview_decisions.shape[1] + if self.order == 1: + return monoview_decisions + else: + genrated_intercations = [monoview_decisions[:, i] + for i in range(monoview_decisions.shape[1])] + for order_index in range(self.order - 1): + combins = itertools.combinations(range(monoview_decisions.shape[1]), + order_index + 2) + for combin in combins: + generated_decision = monoview_decisions[:, combin[0]] + for index in range(len(combin) - 1): + if self.model_type == "disjunction": + generated_decision = np.logical_and(generated_decision, + monoview_decisions[:, combin[index + 1]]) + else: + generated_decision = np.logical_or(generated_decision, + monoview_decisions[:, combin[index + 1]]) + genrated_intercations.append(generated_decision) + return np.transpose(np.array(genrated_intercations)) + diff --git a/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/weighted_linear_late_fusion.py b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/weighted_linear_late_fusion.py new file mode 100644 index 0000000000000000000000000000000000000000..efbd3f17cda04e2e5fdc66b2a00c018c10d21d52 --- /dev/null +++ b/multiview_platform/mono_multi_view_classifiers/multiview_classifiers/weighted_linear_late_fusion.py @@ -0,0 +1,26 @@ +import numpy as np + +from ..multiview_classifiers.additions.late_fusion_utils import LateFusionClassifier +from ..multiview.multiview_utils import get_examples_views_indices + +classifier_class_name = "WeightedLinearLateFusion" + + +class WeightedLinearLateFusion(LateFusionClassifier): + def __init__(self, random_state, classifier_names=None, + classifier_configs=None, nb_view=None, nb_cores=1): + super(WeightedLinearLateFusion, self).__init__(random_state=random_state, + classifier_names=classifier_names, + classifier_configs=classifier_configs, + nb_cores=nb_cores, + nb_view=nb_view) + + def predict(self, X, example_indices=None, views_indices=None): + example_indices, views_indices = get_examples_views_indices(X, example_indices, views_indices) + view_scores = [] + for index, viewIndex in enumerate(views_indices): + view_scores.append(np.array(self.monoviewClassifiers[index].predict_proba( + X.get_v(viewIndex, example_indices))) * self.weights[index]) + view_scores = np.array(view_scores) + predicted_labels = np.argmax(np.sum(view_scores, axis=0), axis=1) + return predicted_labels diff --git a/multiview_platform/tests/test_multiview_classifiers/test_additions/test_diversity_utils.py b/multiview_platform/tests/test_multiview_classifiers/test_additions/test_diversity_utils.py index 6c22d615d12d8640b5f96c35dfb67999381fb138..522022041d942bf908ee19671e238887906a240a 100644 --- a/multiview_platform/tests/test_multiview_classifiers/test_additions/test_diversity_utils.py +++ b/multiview_platform/tests/test_multiview_classifiers/test_additions/test_diversity_utils.py @@ -18,7 +18,7 @@ class FakeDataset(): def get_nb_class(self, example_indices): return np.unique(self.labels[example_indices]) -class FakeDivCoupleClf(diversity_utils.CoupleDiversityFusion): +class FakeDivCoupleClf(diversity_utils.CoupleDiversityFusionClassifier): def __init__(self, rs, classifier_names=None, classifiers_config=None, monoview_estimators=None): @@ -32,7 +32,7 @@ class FakeDivCoupleClf(diversity_utils.CoupleDiversityFusion): return self.rs.randint(0,100) -class FakeDivGlobalClf(diversity_utils.GlobalDiversityFusion): +class FakeDivGlobalClf(diversity_utils.GlobalDiversityFusionClassifier): def __init__(self, rs, classifier_names=None, classifiers_config=None, monoview_estimators=None):