Skip to content
Snippets Groups Projects
Commit 91cbd8d3 authored by bbauvin's avatar bbauvin
Browse files

Everything seems to be working with kfolds, randomstate and learning rate defined in execlassif

parent e5423d4f
No related branches found
No related tags found
No related merge requests found
Showing
with 480 additions and 292 deletions
......@@ -216,14 +216,13 @@ def lateFusionSetArgs(views, viewsIndices, classes, method,
return arguments
def initMultiviewArguments(args, benchmark, views, viewsIndices, scores, classifiersConfigs, classifiersNames,
NB_VIEW, metrics, argumentDictionaries, randomState, directory):
def initMultiviewArguments(args, benchmark, views, viewsIndices, argumentDictionaries, randomState, directory, resultsMonoview):
multiviewArguments = []
if "Multiview" in benchmark:
for multiviewAlgoName in benchmark["Multiview"]:
multiviewPackage = getattr(Multiview, multiviewAlgoName)
mutliviewModule = getattr(multiviewPackage, multiviewAlgoName)
multiviewArguments += mutliviewModule.getArgs(args, benchmark, views, viewsIndices, randomState, directory)
multiviewArguments += mutliviewModule.getArgs(args, benchmark, views, viewsIndices, randomState, directory, resultsMonoview)
argumentDictionaries["Multiview"] = multiviewArguments
return argumentDictionaries
......@@ -503,9 +502,7 @@ else:
viewsIndices]
monoviewTime = time.time() - dataBaseTime - start
argumentDictionaries = initMultiviewArguments(args, benchmark, views, viewsIndices, scores, classifiersConfigs,
classifiersNames, NB_VIEW, metrics[0], argumentDictionaries, randomState,
directory)
argumentDictionaries = initMultiviewArguments(args, benchmark, views, viewsIndices, argumentDictionaries, randomState, directory, resultsMonoview)
if nbCores > 1:
resultsMultiview = []
......
......@@ -55,12 +55,12 @@ def ExecMonoview(directory, X, Y, name, labelsNames, classificationIndices, KFol
nbClass = kwargs["nbClass"]
X = getValue(X)
datasetLength = X.shape[0]
learningRate = len(classificationIndices[0])/(len(classificationIndices[0])+len(classificationIndices[1]))
logging.debug("Done:\t Loading data")
# Determine the Database to extract features
print KFolds
logging.debug("Info:\t Classification - Database:" + str(name) + " Feature:" + str(feat) + " train_size:"
+ str(len(classificationIndices[0])) + ", CrossValidation k-folds: " + str(KFolds.n_splits) + ", cores:"
logging.debug("Info:\t Classification - Database:" + str(name) + " Feature:" + str(feat) + " train ratio:"
+ str(learningRate) + ", CrossValidation k-folds: " + str(KFolds.n_splits) + ", cores:"
+ str(nbCores) + ", algorithm : " + CL_type)
# y_trains = []
......@@ -123,7 +123,7 @@ def ExecMonoview(directory, X, Y, name, labelsNames, classificationIndices, KFol
timestr = time.strftime("%Y%m%d-%H%M%S")
CL_type_string = CL_type
outputFileName = directory + "/"+CL_type_string+"/"+"/"+feat+"/"+timestr +"Results-" + CL_type_string + "-" + labelsString + \
'-learnRate' + str(len(classificationIndices)) + '-' + name + "-" + feat + "-"
'-learnRate' + str(learningRate) + '-' + name + "-" + feat + "-"
if not os.path.exists(os.path.dirname(outputFileName)):
try:
os.makedirs(os.path.dirname(outputFileName))
......@@ -151,7 +151,7 @@ def ExecMonoview(directory, X, Y, name, labelsNames, classificationIndices, KFol
logging.info("Done:\t Result Analysis")
viewIndex = args["viewIndex"]
return viewIndex, [CL_type, cl_desc+[feat], metricsScores, full_labels, y_train_pred]
return viewIndex, [CL_type, cl_desc+[feat], metricsScores, full_labels, cl_res]
# # Classification Report with Precision, Recall, F1 , Support
# logging.debug("Info:\t Classification report:")
......
......@@ -22,6 +22,13 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 15), DecisionTreeClassifier()])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -52,6 +59,9 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, metric=["accuracy_
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- Adaboost with num_esimators : "+str(config.n_estimators)+", base_estimators : "+str(config.base_estimator)
else:
try:
return "\n\t\t- Adaboost with num_esimators : "+str(config[0])+", base_estimators : "+str(config[1])
except:
......
......@@ -23,6 +23,13 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 300), randomState.choice(["gini", "entropy"]), randomState.choice(["best", "random"])])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -37,7 +44,7 @@ def getKWARGS(kwargsList):
def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=["accuracy_score", None], nIter=30):
pipeline_DT = Pipeline([('classifier', DecisionTreeClassifier())])
param_DT = {"classifier__max_depth": randint(1, 30),
param_DT = {"classifier__max_depth": randint(1, 300),
"classifier__criterion": ["gini", "entropy"],
"classifier__splitter": ["best", "random"]}
metricModule = getattr(Metrics, metric[0])
......@@ -55,6 +62,9 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- Decision Tree with max_depth : "+str(config.max_depth) + ", criterion : "+config.criterion+", splitter : "+config.splitter
else:
try:
return "\n\t\t- Decision Tree with max_depth : "+str(config[0]) + ", criterion : "+config[1]+", splitter : "+config[2]
except:
......
......@@ -25,6 +25,14 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 50), randomState.choice(["uniform", "distance"]),
randomState.choice(["auto", "ball_tree", "kd_tree", "brute"]), randomState.choice([1,2])])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -64,6 +72,9 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- K nearest Neighbors with n_neighbors : "+str(config.n_neighbors)+", weights : "+config.weights+", algorithm : "+config.algorithm+", p : "+str(config.p)
else:
try:
return "\n\t\t- K nearest Neighbors with n_neighbors : "+str(config[0])+", weights : "+config[1]+", algorithm : "+config[2]+", p : "+str(config[3])
except:
......
......@@ -23,6 +23,14 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 300), randomState.randint(1, 300),
randomState.choice(["gini", "entropy"])])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -37,8 +45,8 @@ def getKWARGS(kwargsList):
def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=["accuracy_score", None], nIter=30):
pipeline_rf = Pipeline([('classifier', RandomForestClassifier())])
param_rf = {"classifier__n_estimators": randint(1, 30),
"classifier__max_depth": randint(1, 30),
param_rf = {"classifier__n_estimators": randint(1, 300),
"classifier__max_depth": randint(1, 300),
"classifier__criterion": ["gini", "entropy"]}
metricModule = getattr(Metrics, metric[0])
if metric[1]!=None:
......@@ -57,6 +65,9 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- Random Forest with num_esimators : "+str(config.n_estimators)+", max_depth : "+str(config.max_depth)+ ", criterion : "+config.criterion
else:
try:
return "\n\t\t- Random Forest with num_esimators : "+str(config[0])+", max_depth : "+str(config[1])+ ", criterion : "+config[2]
except:
......
......@@ -13,10 +13,10 @@ __author__ = "Baptiste Bauvin"
__status__ = "Prototype" # Production, Development, Prototype
def canProbas():
return False
def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
max_attrtibutes = kwargs['0']
try:
......@@ -42,6 +42,15 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
pass
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 20), randomState.random_sample(),
randomState.choice(["conjunction", "disjunction"])])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -78,8 +87,8 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=None, metric=["accura
# else:
# kFolds = [[], range(len(y_train))]
scores = []
KFolds = KFolds.split(X_train, y_train)
for foldIdx, (trainIndices, testIndices) in enumerate(KFolds):
kFolds = KFolds.split(X_train, y_train)
for foldIdx, (trainIndices, testIndices) in enumerate(kFolds):
# if fold != range(len(y_train)):
# fold.sort()
# trainIndices = [index for index in range(len(y_train)) if (index not in fold)]
......@@ -112,10 +121,13 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=None, metric=["accura
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SCM with max_attributes : "+str(config.max_attributes)+", model type : "+config.model_type+", p : "+str(config.p)
else:
try :
return "\n\t\t- SCM with max_attributes : "+str(config[0])#+", c : "+str(config[1])+", p : "+str(config[2])
return "\n\t\t- SCM with max_attributes : "+str(config[0])+", p : "+str(config[1])+", model type : "+str(config[2])
except:
return "\n\t\t- SCM with max_attributes : "+str(config["0"])#+", c : "+str(config["1"])+", p : "+str(config["2"])
return "\n\t\t- SCM with max_attributes : "+str(config["0"])+", p : "+str(config["1"])+", model type : "+str(config["2"])
def transformData(dataArray):
......
......@@ -27,6 +27,14 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.choice(['log', 'modified_huber']),
randomState.choice(["l1", "l2", "elasticnet"]), randomState.random_sample()])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -59,8 +67,12 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
SGD_detector.best_params_["classifier__alpha"]]
return desc_params
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SGDClassifier with loss : "+config.loss+", penalty : "+config.penalty+", alpha : "+str(config.alpha)
else:
try:
return "\n\t\t- SGDClassifier with loss : "+config[0]+", penalty : "+config[1]
return "\n\t\t- SGDClassifier with loss : "+config[0]+", penalty : "+config[1]+", alpha : "+str(config[2])
except:
return "\n\t\t- SGDClassifier with loss : "+config["0"]+", penalty : "+config["1"]
\ No newline at end of file
return "\n\t\t- SGDClassifier with loss : "+config["0"]+", penalty : "+config["1"]+", alpha : "+str(config["2"])
\ No newline at end of file
......@@ -21,6 +21,13 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 10000),])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -48,6 +55,9 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SVM Linear with C : "+str(config.C)
else:
try:
return "\n\t\t- SVM Linear with C : "+str(config[0])
except:
......
......@@ -23,6 +23,13 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 10000), randomState.randint(1, 30)])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -51,7 +58,10 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SVM Poly with C : "+str(config.C)+", degree : "+str(config.degree)
else:
try:
return "\n\t\t- SVM Poly with C : "+str(config[0])
return "\n\t\t- SVM Poly with C : "+str(config[0])+", degree : "+str(config[1])
except:
return "\n\t\t- SVM Poly with C : "+str(config["0"])
\ No newline at end of file
return "\n\t\t- SVM Poly with C : "+str(config["0"])+", degree : "+str(config["1"])
\ No newline at end of file
......@@ -21,6 +21,13 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1,**kwargs):
return classifier
def paramsToSet(nIter, randomState):
paramsSet = []
for _ in range(nIter):
paramsSet.append([randomState.randint(1, 10000),])
return paramsSet
def getKWARGS(kwargsList):
kwargsDict = {}
for (kwargName, kwargValue) in kwargsList:
......@@ -46,6 +53,9 @@ def randomizedSearch(X_train, y_train, randomState, KFolds=4, nbCores=1, metric=
def getConfig(config):
if type(config) not in [list, dict]:
return "\n\t\t- SVM RBF with C : "+str(config.C)
else:
try:
return "\n\t\t- SVM RBF with C : "+str(config[0])
except:
......
import sys
import os.path
import errno
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
from Multiview import *
# from Multiview import *
import Multiview
import GetMultiviewDb as DB
import os
......@@ -26,7 +28,7 @@ def ExecMultiview_multicore(directory, coreIndex, name, learningRate, nbFolds, d
hyperParamSearch=hyperParamSearch, metrics=metrics, nIter=nIter, **arguments)
def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, databaseType, path, LABELS_DICTIONARY, statsIter, randomState,
def ExecMultiview(directory, DATASET, name, classificationIndices, KFolds, nbCores, databaseType, path, LABELS_DICTIONARY, statsIter, randomState,
hyperParamSearch=False, metrics=None, nIter=30, **kwargs):
datasetLength = DATASET.get("Metadata").attrs["datasetLength"]
......@@ -40,11 +42,12 @@ def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, data
CL_type = kwargs["CL_type"]
LABELS_NAMES = kwargs["LABELS_NAMES"]
classificationKWARGS = kwargs[CL_type+"KWARGS"]
learningRate = len(classificationIndices[0])/(len(classificationIndices[0])+len(classificationIndices[1]))
t_start = time.time()
logging.info("### Main Programm for Multiview Classification")
logging.info("### Classification - Database : " + str(name) + " ; Views : " + ", ".join(views) +
" ; Algorithm : " + CL_type + " ; Cores : " + str(nbCores))
" ; Algorithm : " + CL_type + " ; Cores : " + str(nbCores)+", Train ratio : " + str(learningRate)+
", CV on " + str(KFolds.n_splits) + " folds")
for viewIndex, viewName in zip(viewsIndices, views):
logging.info("Info:\t Shape of " + str(viewName) + " :" + str(
......@@ -52,27 +55,27 @@ def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, data
logging.info("Done:\t Read Database Files")
extractionTime = time.time() - t_start
ivalidationIndices = []
trainLabelsIterations = []
testLabelsIterations = []
classifiersIterations = []
classifierPackage = globals()[CL_type] # Permet d'appeler un module avec une string
# ivalidationIndices = []
learningIndices, validationIndices = classificationIndices
# trainLabelsIterations = []
# testLabelsIterations = []
# classifiersIterations = []
classifierPackage = getattr(Multiview, CL_type) # Permet d'appeler un module avec une string
classifierModule = getattr(classifierPackage, CL_type)
classifierClass = getattr(classifierModule, CL_type)
analysisModule = getattr(classifierPackage, "analyzeResults")
logging.info("Start:\t Determine validation split for ratio " + str(learningRate))
iValidationIndices = [DB.splitDataset(DATASET, learningRate, datasetLength, randomState) for _ in range(statsIter)]
iLearningIndices = [[index for index in range(datasetLength) if index not in validationIndices] for validationIndices in iValidationIndices]
iClassificationSetLength = [len(learningIndices) for learningIndices in iLearningIndices]
logging.info("Done:\t Determine validation split")
logging.info("Train ratio : " + str(learningRate))
# iValidationIndices = [DB.splitDataset(DATASET, classificationIndices, datasetLength, randomState) for _ in range(statsIter)]
# iLearningIndices = [[index for index in range(datasetLength) if index not in validationIndices] for validationIndices in iValidationIndices]
# iClassificationSetLength = [len(learningIndices) for learningIndices in iLearningIndices]
# logging.info("Done:\t Determine validation split")
logging.info("Start:\t Determine "+str(nbFolds)+" folds")
if nbFolds != 1:
iKFolds = [DB.getKFoldIndices(nbFolds, DATASET.get("Labels")[...], NB_CLASS, learningIndices, randomState) for learningIndices in iLearningIndices]
else:
iKFolds = [[[], range(classificationSetLength)] for classificationSetLength in iClassificationSetLength]
logging.info("CV On " + str(KFolds.n_splits) + " folds")
# if KFolds != 1:
# iKFolds = [DB.getKFoldIndices(KFolds, DATASET.get("Labels")[...], NB_CLASS, learningIndices, randomState) for learningIndices in iLearningIndices]
# else:
# iKFolds = [[[], range(classificationSetLength)] for classificationSetLength in iClassificationSetLength]
# logging.info("Info:\t Length of Learning Sets: " + str(classificationSetLength - len(kFolds[0])))
# logging.info("Info:\t Length of Testing Sets: " + str(len(kFolds[0])))
......@@ -84,19 +87,18 @@ def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, data
# logging.info("Start:\t Classification")
# Begin Classification
if hyperParamSearch != "None":
classifier = searchBestSettings(DATASET, CL_type, metrics, iLearningIndices, iKFolds, randomState, viewsIndices=viewsIndices, searchingTool=hyperParamSearch, nIter=nIter, **classificationKWARGS)
classifier = searchBestSettings(DATASET, CL_type, metrics, learningIndices, KFolds, randomState, viewsIndices=viewsIndices, searchingTool=hyperParamSearch, nIter=nIter, **classificationKWARGS)
else:
classifier = classifierClass(NB_CORES=nbCores, **classificationKWARGS)
for _ in range(statsIter):
learningIndices, validationIndices = learningRate
classifier.fit_hdf5(DATASET, trainIndices=learningIndices, viewsIndices=viewsIndices)
trainLabels = classifier.predict_hdf5(DATASET, usedIndices=learningIndices, viewsIndices=viewsIndices)
testLabels = classifier.predict_hdf5(DATASET, usedIndices=validationIndices, viewsIndices=viewsIndices)
fullLabels = classifier.predict_hdf5(DATASET, viewsIndices=viewsIndices)
trainLabelsIterations.append(trainLabels)
testLabelsIterations.append(testLabels)
ivalidationIndices.append(validationIndices)
classifiersIterations.append(classifier)
# trainLabelsIterations.append(trainLabels)
# testLabelsIterations.append(testLabels)
# ivalidationIndices.append(validationIndices)
# classifiersIterations.append(classifier)
logging.info("Done:\t Classification")
classificationTime = time.time() - t_start
......@@ -106,11 +108,11 @@ def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, data
times = (extractionTime, classificationTime)
stringAnalysis, imagesAnalysis, metricsScores = analysisModule.execute(classifiersIterations, trainLabelsIterations,
testLabelsIterations, DATASET,
classificationKWARGS, learningRate,
stringAnalysis, imagesAnalysis, metricsScores = analysisModule.execute(classifier, trainLabels,
testLabels, DATASET,
classificationKWARGS, classificationIndices,
LABELS_DICTIONARY, views, nbCores, times,
name, nbFolds, ivalidationIndices,
name, KFolds,
hyperParamSearch, nIter, metrics, statsIter,
viewsIndices, randomState)
labelsSet = set(LABELS_DICTIONARY.values())
......@@ -118,14 +120,19 @@ def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, data
featureString = "-".join(views)
labelsString = "-".join(labelsSet)
timestr = time.strftime("%Y%m%d-%H%M%S")
CL_type_string = CL_type
if CL_type=="Fusion":
CL_type_string += "-"+classificationKWARGS["fusionType"]+"-"+classificationKWARGS["fusionMethod"]+"-"+"-".join(classificationKWARGS["classifiersNames"])
elif CL_type=="Mumbo":
CL_type_string += "-"+"-".join(classificationKWARGS["classifiersNames"])
outputFileName = directory + timestr + "Results-" + CL_type_string + "-" + featureString + '-' + labelsString + \
CL_type_string = classifierModule.getCLString(classificationKWARGS)
# if CL_type=="Fusion":
# CL_type_string += "-"+classificationKWARGS["fusionType"]+"-"+classificationKWARGS["fusionMethod"]+"-"+"-".join(classificationKWARGS["classifiersNames"])
# elif CL_type=="Mumbo":
# CL_type_string += "-"+"-".join(classificationKWARGS["classifiersNames"])
outputFileName = directory + "/" + CL_type_string + "/" + timestr + "Results-" + CL_type_string + "-" + featureString + '-' + labelsString + \
'-learnRate' + str(learningRate) + '-' + name
if not os.path.exists(os.path.dirname(outputFileName)):
try:
os.makedirs(os.path.dirname(outputFileName))
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
outputTextFile = open(outputFileName + '.txt', 'w')
outputTextFile.write(stringAnalysis)
outputTextFile.close()
......
......@@ -5,6 +5,7 @@ import pkgutil
# from Methods import *
import Methods
import MonoviewClassifiers
from utils.Dataset import getV
......@@ -47,7 +48,7 @@ def getBenchmark(benchmark, args=None):
if not isPackage]
else:
benchmark["Multiview"]["Fusion"]["Methods"]["LateFusion"] = args.FU_late_methods
if "EarlyFusion" in args.FU_types:
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([
......@@ -66,16 +67,15 @@ def getBenchmark(benchmark, args=None):
return benchmark
def getArgs(args, benchmark, views, viewsIndices, randomState, directory):
def getArgs(args, benchmark, views, viewsIndices, randomState, directory, resultsMonoview):
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"]:
# import pdb;pdb.set_trace()
fusionTypePackage = getattr(Methods, fusionType+"Package")
for fusionMethod in benchmark["Multiview"]["Fusion"]["Methods"][fusionType]:
fusionMethodModule = getattr(fusionTypePackage, fusionMethod)
arguments = fusionMethodModule.getArgs(args, views, viewsIndices, directory)
arguments = fusionMethodModule.getArgs(args, views, viewsIndices, directory, resultsMonoview)
argumentsList+= arguments
return argumentsList
......@@ -84,7 +84,7 @@ def makeMonoviewData_hdf5(DATASET, weights=None, usedIndices=None, viewsIndices=
if type(viewsIndices)==type(None):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if not usedIndices:
uesdIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
NB_VIEW = len(viewsIndices)
if weights==None:
weights = np.array([1/NB_VIEW for i in range(NB_VIEW)])
......@@ -97,7 +97,7 @@ def makeMonoviewData_hdf5(DATASET, weights=None, usedIndices=None, viewsIndices=
def genParamsSets(classificationKWARGS, randomState, nIter=1):
fusionTypeName = classificationKWARGS["fusionType"]
fusionTypePackage = globals()[fusionTypeName+"Package"]
fusionTypePackage = getattr(Methods, fusionTypeName+"Package")
fusionMethodModuleName = classificationKWARGS["fusionMethod"]
fusionMethodModule = getattr(fusionTypePackage, fusionMethodModuleName)
fusionMethodConfig = fusionMethodModule.genParamsSets(classificationKWARGS, randomState, nIter=nIter)
......@@ -133,11 +133,19 @@ def gridSearch_hdf5(DATASET, viewsIndices, classificationKWARGS, learningIndices
return bestSettings, fusionMethodConfig
def getCLString(classificationKWARGS):
if classificationKWARGS["fusionType"] == "LateFusion":
return "Fusion-"+classificationKWARGS["fusionType"]+"-"+classificationKWARGS["fusionMethod"]+"-"+\
"-".join(classificationKWARGS["classifiersNames"])
elif classificationKWARGS["fusionType"] == "EarlyFusion":
return "Fusion-"+classificationKWARGS["fusionType"]+"-"+classificationKWARGS["fusionMethod"]+"-"+ \
classificationKWARGS["classifiersNames"]
class Fusion:
def __init__(self, randomState, NB_CORES=1, **kwargs):
fusionType = kwargs['fusionType']
fusionMethod = kwargs['fusionMethod']
fusionTypePackage = globals()[fusionType+"Package"]
fusionTypePackage = getattr(Methods, fusionType+"Package")
fusionMethodModule = getattr(fusionTypePackage, fusionMethod)
fusionMethodClass = getattr(fusionMethodModule, fusionMethod)
nbCores = NB_CORES
......@@ -164,10 +172,7 @@ class Fusion:
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if type(viewsIndices)==type(None):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if usedIndices:
predictedLabels = self.classifier.predict_hdf5(DATASET, usedIndices=usedIndices, viewsIndices=viewsIndices)
else:
predictedLabels = []
return predictedLabels
def predict_probas_hdf5(self, DATASET, usedIndices=None):
......
......@@ -10,6 +10,8 @@ class EarlyFusionClassifier(object):
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
......@@ -24,7 +26,7 @@ class EarlyFusionClassifier(object):
if type(viewsIndices)==type(None):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
nbView = len(viewsIndices)
if not usedIndices:
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)])
......
from ...Methods.EarlyFusion import EarlyFusionClassifier
import MonoviewClassifiers
import numpy as np
import pkgutil
from sklearn.metrics import accuracy_score
def genParamsSets(classificationKWARGS, randomState, nIter=1):
nbView = classificationKWARGS["nbView"]
if classificationKWARGS["classifiersConfigs"] is None:
monoviewClassifierModule = getattr(MonoviewClassifiers, classificationKWARGS["classifiersNames"])
paramsMonoview = monoviewClassifierModule.paramsToSet(nIter, randomState)
paramsSets = []
for _ in range(nIter):
for iterIndex in range(nIter):
randomWeightsArray = randomState.random_sample(nbView)
normalizedArray = randomWeightsArray/np.sum(randomWeightsArray)
paramsSets.append([normalizedArray])
paramsSets.append([normalizedArray, paramsMonoview[iterIndex]])
return paramsSets
def getArgs(args, views, viewsIndices, directory):
def getArgs(args, views, viewsIndices, directory, resultsMonoview):
argumentsList = []
if args.FU_E_cl_names != ['']:
pass
else:
monoviewClassifierModulesNames = [name for _, name, isPackage in pkgutil.iter_modules(['MonoviewClassifiers'])
if (not isPackage)]
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(MonoviewClassifiers, classifierName)
if classifierConfig is not None:
arguments = {"CL_type": "Fusion",
"views": views,
"NB_VIEW": len(views),
......@@ -32,6 +44,19 @@ def getArgs(args, views, viewsIndices, directory):
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
......@@ -60,9 +85,9 @@ 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'][0]==None:
if kwargs['fusionMethodConfig']==None:
self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float)
elif kwargs['fusionMethodConfig'][0]==['']:
elif kwargs['fusionMethodConfig']==['']:
self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float)
else:
self.weights = np.array(map(float, kwargs['fusionMethodConfig']))
......@@ -70,17 +95,18 @@ class WeightedLinear(EarlyFusionClassifier):
def fit_hdf5(self, DATASET, trainIndices=None, viewsIndices=None):
if type(viewsIndices)==type(None):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if not trainIndices:
if trainIndices is None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
self.weights = self.weights/float(max(self.weights))
self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=trainIndices, viewsIndices=viewsIndices)
monoviewClassifierModule = getattr(MonoviewClassifiers, self.monoviewClassifierName)
self.monoviewClassifier = monoviewClassifierModule.fit(self.monoviewData, DATASET.get("Labels")[trainIndices],
self.monoviewClassifier = monoviewClassifierModule.fit(self.monoviewData, DATASET.get("Labels").value[trainIndices], self.randomState,
NB_CORES=self.nbCores, #**self.monoviewClassifiersConfig)
**self.monoviewClassifiersConfig)
def setParams(self, paramsSet):
self.weights = paramsSet[0]
self.monoviewClassifiersConfig = dict((str(index), param) for index, param in enumerate(paramsSet[1]))
def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None):
if type(viewsIndices)==type(None):
......@@ -88,28 +114,23 @@ class WeightedLinear(EarlyFusionClassifier):
self.weights = self.weights/float(np.sum(self.weights))
if usedIndices == None:
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if usedIndices:
self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=usedIndices, viewsIndices=viewsIndices)
predictedLabels = self.monoviewClassifier.predict(self.monoviewData)
else:
predictedLabels=[]
return predictedLabels
def predict_proba_hdf5(self, DATASET, usedIndices=None):
if usedIndices == None:
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if usedIndices:
self.makeMonoviewData_hdf5(DATASET, weights=self.weights, usedIndices=usedIndices)
predictedLabels = self.monoviewClassifier.predict_proba(self.monoviewData)
else:
predictedLabels=[]
return predictedLabels
def getConfig(self, fusionMethodConfig ,monoviewClassifiersNames, monoviewClassifiersConfigs):
configString = "with weighted concatenation, using weights : "+", ".join(map(str, self.weights))+ \
" with monoview classifier : "
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifiersNames)
configString += monoviewClassifierModule.getConfig(monoviewClassifiersConfigs)
configString += monoviewClassifierModule.getConfig(self.monoviewClassifiersConfig)
return configString
def gridSearch(self, classificationKWARGS):
......
......@@ -7,44 +7,68 @@ from joblib import Parallel, delayed
# from sklearn.multiclass import OneVsOneClassifier
# from sklearn.svm import SVC
import os
import sys
import MonoviewClassifiers
import Metrics
from utils.Dataset import getV
def fitMonoviewClassifier(classifierName, data, labels, classifierConfig, needProbas):
def canProbasClassifier(classifierConfig):
try:
_ = getattr(classifierConfig, "predict_proba")
return True
except AttributeError:
return False
def fitMonoviewClassifier(classifierName, data, labels, classifierConfig, needProbas, randomState):
if type(classifierConfig[0])==dict:
monoviewClassifier = getattr(MonoviewClassifiers, classifierName)
if needProbas and not monoviewClassifier.canProbas():
monoviewClassifier = getattr(MonoviewClassifiers, "DecisionTree")
classifier = monoviewClassifier.fit(data,labels,**dict((str(configIndex), config) for configIndex, config in
DTConfig = {"0":300, "1":"entropy", "2":"random"}
classifier = monoviewClassifier.fit(data,labels, randomState,DTConfig)
return classifier
else:
classifier = monoviewClassifier.fit(data,labels, randomState,**dict((str(configIndex), config) for configIndex, config in
enumerate(classifierConfig
)))
return classifier
else:
if needProbas and not canProbasClassifier(classifierConfig):
monoviewClassifier = getattr(MonoviewClassifiers, "DecisionTree")
DTConfig = {"0":300, "1":"entropy", "2":"random"}
classifier = monoviewClassifier.fit(data,labels, randomState,DTConfig)
return classifier
else:
return classifierConfig
def getAccuracies(LateFusionClassifiers):
def getScores(LateFusionClassifiers):
return ""
def intersect(allClassifersNames, directory, viewsIndices):
wrongSets = []
wrongSets = [0 for _ in allClassifersNames]
nbViews = len(viewsIndices)
for classifierIndex, classifierName in enumerate(allClassifersNames):
wrongSets[classifierIndex]=[]
classifierDirectory = directory+"/"+classifierName+"/"
for viewIndex, viewDirectory in enumerate(os.listdir(classifierDirectory)):
for resultFileName in os.listdir(classifierDirectory+"/"+viewDirectory+"/"):
viewDirectoryNames = os.listdir(classifierDirectory)
wrongSets[classifierIndex]=[0 for _ in viewDirectoryNames]
for viewIndex, viewDirectoryName in enumerate(viewDirectoryNames):
for resultFileName in os.listdir(classifierDirectory+"/"+viewDirectoryName+"/"):
if resultFileName.endswith("train_labels.csv"):
yTrainFileName = classifierDirectory+"/"+viewDirectory+"/"+resultFileName
yTrainFileName = classifierDirectory+"/"+viewDirectoryName+"/"+resultFileName
elif resultFileName.endswith("train_pred.csv"):
yTrainPredFileName = classifierDirectory+"/"+viewDirectory+"/"+resultFileName
yTrainPredFileName = classifierDirectory+"/"+viewDirectoryName+"/"+resultFileName
train = np.genfromtxt(yTrainFileName, delimiter=",").astype(np.int16)
pred = np.genfromtxt(yTrainPredFileName, delimiter=",").astype(np.int16)
length = len(train)
wrongLabelsIndices = np.where(train+pred == 1)
wrongSets[classifierIndex][viewIndex]=wrongLabelsIndices
combinations = itertools.combinations_with_replacement(range(nbViews), len(allClassifersNames))
combinations = itertools.combinations_with_replacement(range(len(allClassifersNames)), nbViews)
bestLen = length
bestCombination = None
for combination in combinations:
......@@ -91,26 +115,31 @@ def bestScore(allClassifersNames, directory, viewsIndices):
def getClassifiers(selectionMethodName, allClassifiersNames, directory, viewsIndices):
selectionMethod = locals()[selectionMethodName]
thismodule = sys.modules[__name__]
selectionMethod = getattr(thismodule, selectionMethodName)
classifiersNames = selectionMethod(allClassifiersNames, directory, viewsIndices)
return classifiersNames
# def getConfig(classifiersNames, directory):
# for classifierIndex, classifierName in classifiersNames:
# classifierDirectory = directory+"/"+classifierName+"/"
# viewName = os.listdir(classifierDirectory)[classifierIndex]
# viewDirectory = classifierDirectory+"/"+viewName+"/"
# for resultFileName in os.listdir(classifierDirectory+"/"+viewDirectory+"/"):
# if resultFileName.endswith(".txt"):
# pass
def getConfig(classifiersNames, resultsMonoview):
classifiers = [0 for _ in range(len(classifiersNames))]
for viewIndex, classifierName in enumerate(classifiersNames):
for resultMonoview in resultsMonoview:
if resultMonoview[0]==viewIndex and resultMonoview[1][0]==classifierName:
classifiers[viewIndex]=resultMonoview[1][4]
return classifiers
def jambon(fromage):
pass
class LateFusionClassifier(object):
def __init__(self, randomState, monoviewClassifiersNames, monoviewClassifiersConfigs, monoviewSelection, NB_CORES=1):
self.monoviewClassifiersNames = monoviewClassifiersNames
if type(monoviewClassifiersConfigs[0])==dict:
self.monoviewClassifiersConfigs = monoviewClassifiersConfigs
self.monoviewClassifiers = []
else:
self.monoviewClassifiersConfigs = monoviewClassifiersConfigs
self.nbCores = NB_CORES
self.accuracies = np.zeros(len(monoviewClassifiersNames))
self.needProbas = False
......@@ -122,11 +151,17 @@ class LateFusionClassifier(object):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if trainIndices == None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
monoviewSelectionMethod = locals()[self.monoviewSelection]
self.monoviewClassifiers = monoviewSelectionMethod()
# monoviewSelectionMethod = locals()[self.monoviewSelection]
# self.monoviewClassifiers = monoviewSelectionMethod()
# a = Parallel(n_jobs=self.nbCores)(
# delayed(jambon)(DATASET.get("Labels").value[trainIndices],
# )
# for index, viewIndex in enumerate(viewsIndices))
# import pdb;pdb.set_trace()
self.monoviewClassifiers = Parallel(n_jobs=self.nbCores)(
delayed(fitMonoviewClassifier)(self.monoviewClassifiersNames[index],
getV(DATASET, viewIndex, trainIndices),
DATASET.get("Labels")[trainIndices],
self.monoviewClassifiersConfigs[index], self.needProbas)
DATASET.get("Labels").value[trainIndices],
self.monoviewClassifiersConfigs[index], self.needProbas, self.randomState)
for index, viewIndex in enumerate(viewsIndices))
\ No newline at end of file
......@@ -4,7 +4,7 @@ import pkgutil
from utils.Dataset import getV
import MonoviewClassifiers
from ..LateFusion import LateFusionClassifier, getClassifiers#, getConfig
from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig
def genParamsSets(classificationKWARGS, randomState, nIter=1):
......@@ -25,9 +25,9 @@ def genParamsSets(classificationKWARGS, randomState, nIter=1):
# fusionMethodConfig = args.FU_method_config
# return classifiersNames, classifiersConfig, fusionMethodConfig
def getArgs(args, views, viewsIndices, directory):
def getArgs(args, views, viewsIndices, directory, resultsMonoview):
if args.FU_L_cl_names!=['']:
pass
args.FU_L_select_monoview = "user_defined"
else:
monoviewClassifierModulesNames = [name for _, name, isPackage in pkgutil.iter_modules(['MonoviewClassifiers'])
if (not isPackage)]
......@@ -35,11 +35,14 @@ def getArgs(args, views, viewsIndices, directory):
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName)
for classifierName in args.FU_L_cl_names]
if args.FU_L_cl_config != ['']:
classifierConfig = [monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")])
classifiersConfigs = [monoviewClassifierModule.getKWARGS([arg.split(":") for arg in classifierConfig.split(",")])
for monoviewClassifierModule,classifierConfig
in zip(monoviewClassifierModules,args.FU_L_cl_config)]
else:
# args.FU_L_cl_config = getConfig(args.FU_L_cl_names, directory)
classifiersConfigs = getConfig(args.FU_L_cl_names, resultsMonoview)
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")
arguments = {"CL_type": "Fusion",
"views": views,
"NB_VIEW": len(views),
......@@ -49,11 +52,7 @@ def getArgs(args, views, viewsIndices, directory):
"FusionKWARGS": {"fusionType": "LateFusion",
"fusionMethod": "BayesianInference",
"classifiersNames": args.FU_L_cl_names,
"classifiersConfigs": [monoviewClassifierModule.getKWARGS([arg.split(":")
for arg in
classifierConfig.split(",")])
for monoviewClassifierModule,classifierConfig
in zip(monoviewClassifierModules,args.FU_L_cl_config)],
"classifiersConfigs": classifiersConfigs,
'fusionMethodConfig': args.FU_L_method_config,
'monoviewSelection': args.FU_L_select_monoview,
"nbView": (len(viewsIndices))}}
......@@ -89,7 +88,7 @@ class BayesianInference(LateFusionClassifier):
if kwargs['fusionMethodConfig'][0]==None or kwargs['fusionMethodConfig']==['']:
self.weights = [1.0 for classifier in kwargs['classifiersNames']]
else:
self.weights = np.array(map(float, kwargs['fusionMethodConfig']))
self.weights = np.array(map(float, kwargs['fusionMethodConfig'][0]))
self.needProbas = True
def setParams(self, paramsSet):
......@@ -104,15 +103,12 @@ class BayesianInference(LateFusionClassifier):
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if sum(self.weights)!=1.0:
self.weights = self.weights/sum(self.weights)
if usedIndices:
viewScores = np.zeros((nbView, len(usedIndices), DATASET.get("Metadata").attrs["nbClass"]))
for index, viewIndex in enumerate(viewsIndices):
viewScores[index] = np.power(self.monoviewClassifiers[index].predict_proba(getV(DATASET, viewIndex, usedIndices)),
self.weights[index])
predictedLabels = np.argmax(np.prod(viewScores, axis=0), axis=1)
else:
predictedLabels = []
return predictedLabels
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames,monoviewClassifiersConfigs):
......@@ -121,4 +117,5 @@ class BayesianInference(LateFusionClassifier):
for monoviewClassifierConfig, monoviewClassifierName in zip(monoviewClassifiersConfigs, monoviewClassifiersNames):
monoviewClassifierModule = getattr(MonoviewClassifiers, monoviewClassifierName)
configString += monoviewClassifierModule.getConfig(monoviewClassifierConfig)
configString+="\n\t -Method used to select monoview classifiers : "+self.monoviewSelection
return configString
\ No newline at end of file
from ...Methods.LateFusion import LateFusionClassifier
from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig
import MonoviewClassifiers
import numpy as np
from sklearn.metrics import accuracy_score
from utils.Dataset import getV
import pkgutil
def genParamsSets(classificationKWARGS, randomState, nIter=1):
......@@ -15,8 +16,21 @@ def genParamsSets(classificationKWARGS, randomState, nIter=1):
return paramsSets
def getArgs(args, views, viewsIndices, directory):
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName) for classifierName in args.FU_L_cl_names]
def getArgs(args, views, viewsIndices, directory, resultsMonoview):
if args.FU_L_cl_names!=['']:
pass
else:
monoviewClassifierModulesNames = [name for _, name, isPackage in pkgutil.iter_modules(['MonoviewClassifiers'])
if (not isPackage)]
args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, viewsIndices)
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName)
for classifierName in args.FU_L_cl_names]
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)
arguments = {"CL_type": "Fusion",
"views": views,
"NB_VIEW": len(views),
......@@ -24,14 +38,10 @@ def getArgs(args, views, viewsIndices, directory):
"NB_CLASS": len(args.CL_classes),
"LABELS_NAMES": args.CL_classes,
"FusionKWARGS": {"fusionType": "LateFusion",
"fusionMethod": "BayesianInference",
"fusionMethod": "MajorityVoting",
"classifiersNames": args.FU_L_cl_names,
"classifiersConfigs": [monoviewClassifierModule.getKWARGS([arg.split(":")
for arg in
classifierConfig.split(";")])
for monoviewClassifierModule,classifierConfig
in zip(args.FU_L_cl_config,monoviewClassifierModules)],
'fusionMethodConfig': args.FU_L_method_config[0],
"classifiersConfigs": classifiersConfigs,
'fusionMethodConfig': args.FU_L_method_config,
'monoviewSelection': args.FU_L_select_monoview,
"nbView": (len(viewsIndices))}}
return [arguments]
......@@ -61,7 +71,7 @@ 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]==None or kwargs['fusionMethodConfig'][0]==['']:
if kwargs['fusionMethodConfig'][0]==None or kwargs['fusionMethodConfig']==['']:
self.weights = np.ones(len(kwargs["classifiersNames"]), dtype=float)
else:
self.weights = np.array(map(float, kwargs['fusionMethodConfig'][0]))
......@@ -76,7 +86,7 @@ class MajorityVoting(LateFusionClassifier):
self.weights = self.weights/float(max(self.weights))
if usedIndices == None:
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if usedIndices:
datasetLength = len(usedIndices)
votes = np.zeros((datasetLength, DATASET.get("Metadata").attrs["nbClass"]), dtype=int)
monoViewDecisions = np.zeros((len(usedIndices),nbView), dtype=int)
......@@ -99,8 +109,6 @@ class MajorityVoting(LateFusionClassifier):
# relearn with only the classes that have a maximum number of vote
# votes = revote
# nbMaximum = len(np.where(votes==max(votes))[0])
else:
predictedLabels = []
return predictedLabels
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames,monoviewClassifiersConfigs):
......
from ...Methods.LateFusion import LateFusionClassifier
from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig
import MonoviewClassifiers
import numpy as np
import pyscm
......@@ -12,6 +12,7 @@ from math import ceil
import random
from sklearn.metrics import accuracy_score
import itertools
import pkgutil
def genParamsSets(classificationKWARGS, randomState, nIter=1):
......@@ -19,15 +20,28 @@ def genParamsSets(classificationKWARGS, randomState, nIter=1):
paramsSets = []
for _ in range(nIter):
max_attributes = randomState.randint(1, 20)
p = randomState.random()
p = randomState.random_sample()
model = randomState.choice(["conjunction", "disjunction"])
order = randomState.randint(1,nbView)
paramsSets.append([p, max_attributes, model, order])
return paramsSets
def getArgs(args, views, viewsIndices, directory):
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName) for classifierName in args.FU_L_cl_names]
def getArgs(args, views, viewsIndices, directory, resultsMonoview):
if args.FU_L_cl_names!=['']:
pass
else:
monoviewClassifierModulesNames = [name for _, name, isPackage in pkgutil.iter_modules(['MonoviewClassifiers'])
if (not isPackage)]
args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, viewsIndices)
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName)
for classifierName in args.FU_L_cl_names]
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)
arguments = {"CL_type": "Fusion",
"views": views,
"NB_VIEW": len(views),
......@@ -35,14 +49,10 @@ def getArgs(args, views, viewsIndices, directory):
"NB_CLASS": len(args.CL_classes),
"LABELS_NAMES": args.CL_classes,
"FusionKWARGS": {"fusionType": "LateFusion",
"fusionMethod": "BayesianInference",
"fusionMethod": "SCMForLinear",
"classifiersNames": args.FU_L_cl_names,
"classifiersConfigs": [monoviewClassifierModule.getKWARGS([arg.split(":")
for arg in
classifierConfig.split(";")])
for monoviewClassifierModule,classifierConfig
in zip(args.FU_L_cl_config,monoviewClassifierModules)],
'fusionMethodConfig': args.FU_L_method_config[0],
"classifiersConfigs": classifiersConfigs,
'fusionMethodConfig': args.FU_L_method_config,
'monoviewSelection': args.FU_L_select_monoview,
"nbView": (len(viewsIndices))}}
return [arguments]
......@@ -77,15 +87,15 @@ class SCMForLinear(LateFusionClassifier):
NB_CORES=NB_CORES)
self.SCMClassifier = None
# self.config = kwargs['fusionMethodConfig'][0]
if kwargs['fusionMethodConfig'][0]==None or kwargs['fusionMethodConfig'][0]==['']:
if kwargs['fusionMethodConfig'][0]==None or kwargs['fusionMethodConfig']==['']:
self.p = 1
self.maxAttributes = 5
self.order = 1
self.modelType = "conjunction"
else:
self.p = kwargs['fusionMethodConfig'][0]
self.maxAttributes = kwargs['fusionMethodConfig'][1]
self.order = kwargs['fusionMethodConfig'][2]
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):
......@@ -99,6 +109,7 @@ class SCMForLinear(LateFusionClassifier):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if trainIndices == None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if type(self.monoviewClassifiersConfigs[0])==dict:
for index, viewIndex in enumerate(viewsIndices):
monoviewClassifier = getattr(MonoviewClassifiers, self.monoviewClassifiersNames[index])
self.monoviewClassifiers.append(
......@@ -107,6 +118,8 @@ class SCMForLinear(LateFusionClassifier):
NB_CORES=self.nbCores,
**dict((str(configIndex), config) for configIndex, config in
enumerate(self.monoviewClassifiersConfigs[index]))))
else:
self.monoviewClassifiers = self.monoviewClassifiersConfigs
self.SCMForLinearFusionFit(DATASET, usedIndices=trainIndices, viewsIndices=viewsIndices)
def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None):
......@@ -115,7 +128,6 @@ class SCMForLinear(LateFusionClassifier):
nbView = len(viewsIndices)
if usedIndices == None:
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if usedIndices:
monoviewDecisions = np.zeros((len(usedIndices), nbView), dtype=int)
accus=[]
for index, viewIndex in enumerate(viewsIndices):
......@@ -125,8 +137,6 @@ class SCMForLinear(LateFusionClassifier):
monoviewDecisions[:, index] = monoviewDecision
features = self.generateInteractions(monoviewDecisions)
predictedLabels = self.SCMClassifier.predict(features)
else:
predictedLabels = []
return predictedLabels
def SCMForLinearFusionFit(self, DATASET, usedIndices=None, viewsIndices=None):
......@@ -175,7 +185,7 @@ class SCMForLinear(LateFusionClassifier):
dsetFile = h5py.File(name, "r")
packedDataset = dsetFile.get("temp_scm")
attributeClassification = BaptisteRuleClassifications(packedDataset, features.shape[0])
self.SCMClassifier.fit(binaryAttributes, DATASET.get("Labels")[usedIndices], attribute_classifications=attributeClassification)
self.SCMClassifier.fit(binaryAttributes, DATASET.get("Labels").value[usedIndices], attribute_classifications=attributeClassification)
try:
dsetFile.close()
os.remove(name)
......
from ...Methods.LateFusion import LateFusionClassifier
from ..LateFusion import LateFusionClassifier, getClassifiers, getConfig
import MonoviewClassifiers
import numpy as np
from sklearn.multiclass import OneVsOneClassifier
from sklearn.svm import SVC
from utils.Dataset import getV
import pkgutil
def genParamsSets(classificationKWARGS, randomState, nIter=1):
......@@ -16,8 +17,21 @@ def genParamsSets(classificationKWARGS, randomState, nIter=1):
# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None):
# return None
def getArgs(args, views, viewsIndices, directory):
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName) for classifierName in args.FU_L_cl_names]
def getArgs(args, views, viewsIndices, directory, resultsMonoview):
if args.FU_L_cl_names!=['']:
pass
else:
monoviewClassifierModulesNames = [name for _, name, isPackage in pkgutil.iter_modules(['MonoviewClassifiers'])
if (not isPackage)]
args.FU_L_cl_names = getClassifiers(args.FU_L_select_monoview, monoviewClassifierModulesNames, directory, viewsIndices)
monoviewClassifierModules = [getattr(MonoviewClassifiers, classifierName)
for classifierName in args.FU_L_cl_names]
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)
arguments = {"CL_type": "Fusion",
"views": views,
"NB_VIEW": len(views),
......@@ -25,14 +39,10 @@ def getArgs(args, views, viewsIndices, directory):
"NB_CLASS": len(args.CL_classes),
"LABELS_NAMES": args.CL_classes,
"FusionKWARGS": {"fusionType": "LateFusion",
"fusionMethod": "BayesianInference",
"fusionMethod": "SVMForLinear",
"classifiersNames": args.FU_L_cl_names,
"classifiersConfigs": [monoviewClassifierModule.getKWARGS([arg.split(":")
for arg in
classifierConfig.split(";")])
for monoviewClassifierModule,classifierConfig
in zip(args.FU_L_cl_config,monoviewClassifierModules)],
'fusionMethodConfig': args.FU_L_method_config[0],
"classifiersConfigs": classifiersConfigs,
'fusionMethodConfig': args.FU_L_method_config,
'monoviewSelection': args.FU_L_select_monoview,
"nbView": (len(viewsIndices))}}
return [arguments]
......@@ -48,6 +58,7 @@ class SVMForLinear(LateFusionClassifier):
viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"])
if trainIndices == None:
trainIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if type(self.monoviewClassifiersConfigs[0])==dict:
for index, viewIndex in enumerate(viewsIndices):
monoviewClassifier = getattr(MonoviewClassifiers, self.monoviewClassifiersNames[index])
self.monoviewClassifiers.append(
......@@ -56,6 +67,8 @@ class SVMForLinear(LateFusionClassifier):
NB_CORES=self.nbCores,
**dict((str(configIndex), config) for configIndex, config in
enumerate(self.monoviewClassifiersConfigs[index]))))
else:
self.monoviewClassifiers = self.monoviewClassifiersConfigs
self.SVMForLinearFusionFit(DATASET, usedIndices=trainIndices, viewsIndices=viewsIndices)
def setParams(self, paramsSet):
......@@ -67,14 +80,11 @@ class SVMForLinear(LateFusionClassifier):
nbView = len(viewsIndices)
if usedIndices == None:
usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"])
if usedIndices:
monoviewDecisions = np.zeros((len(usedIndices), nbView), dtype=int)
for index, viewIndex in enumerate(viewsIndices):
monoviewDecisions[:, index] = self.monoviewClassifiers[index].predict(
getV(DATASET, viewIndex, usedIndices))
predictedLabels = self.SVMClassifier.predict(monoviewDecisions)
else:
predictedLabels = []
return predictedLabels
def SVMForLinearFusionFit(self, DATASET, usedIndices=None, viewsIndices=None):
......@@ -87,7 +97,7 @@ class SVMForLinear(LateFusionClassifier):
monoViewDecisions[:, index] = self.monoviewClassifiers[index].predict(
getV(DATASET, viewIndex, usedIndices))
self.SVMClassifier.fit(monoViewDecisions, DATASET.get("Labels")[usedIndices])
self.SVMClassifier.fit(monoViewDecisions, DATASET.get("Labels").value[usedIndices])
def getConfig(self, fusionMethodConfig, monoviewClassifiersNames,monoviewClassifiersConfigs):
configString = "with SVM for linear \n\t-With monoview classifiers : "
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment