def testCNN1DKeras(mult_donnees,mesDonnees_test): print("debut test conv1D") def f1(y_true, y_pred): def recall(y_true, y_pred): """Recall metric. Only computes a batch-wise average of recall. Computes the recall, a metric for multi-label classification of how many relevant items are selected. """ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) recall = true_positives / (possible_positives + K.epsilon()) return recall def precision(y_true, y_pred): """Precision metric. Only computes a batch-wise average of precision. Computes the precision, a metric for multi-label classification of how many selected items are relevant. """ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision = true_positives / (predicted_positives + K.epsilon()) return precision precision = precision(y_true, y_pred) recall = recall(y_true, y_pred) return 2*((precision*recall)/(precision+recall+K.epsilon())) def create_model(): # create model model = Sequential() model.add(Conv1D(4, 10, strides=2, padding='same', activation='relu')) model.add(Dropout(0.4)) model.add(MaxPooling1D(3)) model.add(Conv1D(40, 5, strides=2, padding='same', activation='relu')) model.add(Flatten()) model.add(Dense(2, activation='softmax')) # Compile model model.compile(loss='binary_crossentropy', optimizer='SGD', metrics=['binary_accuracy']) return model result=[] for r,donnees in mult_donnees.items(): print(donnees.data.shape) print("r = "+str(r)) model = KerasClassifier(build_fn=create_model, epochs=1, batch_size=1000) m = GenericModele(model,donnees) s = m.f1Score(mode="proba") result.append([r,s]) print("reactionTime,f1score : "+str(result)) return result
def testRiemannMDM(mult_donnees): print("debut test riemann MDM") result = [] for r, donnees in mult_donnees.items(): print("r = " + str(r)) m = GenericModele(pyriemann.classification.MDM(), donnees) m.dataToCov() s = m.f1Score() result.append([r, s]) print("reactionTime,f1score : " + str(result)) return result
def testKNNBrut(mult_donnees): print("debut test knn brut") result = [] for r, donnees in mult_donnees.items(): print("r = " + str(r)) neigh = KNeighborsClassifier(n_neighbors=3) m = GenericModele(neigh, donnees) m.vectorize() s = m.f1Score() result.append([r, s]) print("reactionTime,f1score : " + str(result)) return result
def testSVMBrut(mult_donnees): print("debut test SVM brut") result = [] for r, donnees in mult_donnees.items(): print("r = " + str(r)) clf = SVC(gamma='auto', probability=True, max_iter=100, verbose=1) m = GenericModele(clf, donnees) m.vectorize() s = m.f1Score() result.append([r, s]) print("reactionTime,f1score : " + str(result)) return result
def testCovSVM(mult_donnees,mesDonnees_test): print("debut test cov SVM") result=[] for r,donnees in mult_donnees.items(): print("r = "+str(r)) clf = SVC(gamma='auto',probability=True,max_iter=100,verbose=1) m = GenericModele(clf,donnees) m.dataToCov() m.vectorize() m.fit() s = m.score(mesDonnees_test[r].data,mesDonnees_test[r].labels) result.append([r,s]) print("reactionTime,f1score : "+str(result)) return result
def testKNNTF(mult_donnees,mesDonnees_test): print("debut test knn tf") result=[] for r,donnees in mult_donnees.items(): print("r = "+str(r)) neigh = KNeighborsClassifier(n_neighbors=3) m = GenericModele(neigh,donnees) m.dataToTf() m.vectorize(tf=True) m.fit() s = m.score(mesDonnees_test[r].data,mesDonnees_test[r].labels) result.append([r,s]) print("reactionTime,f1score : "+str(result)) return result
def testKNNPaseBas(nbPoint,slider,mult_donnees,mesDonnees_test): print("debut test passe bas knn") result=[] for r,donnees in mult_donnees.items(): print("r = "+str(r)) neigh = KNeighborsClassifier(n_neighbors=3) m = GenericModele(neigh,donnees) m.vectorize() m.dataToMoy(nbPoint,slider) m.fit() s = m.score(mesDonnees_test[r].data,mesDonnees_test[r].labels) result.append([r,s]) print("reactionTime,f1score : "+str(result)) return result
def testRiemannKNN(mult_donnees,mesDonnees_test): print("debut test riemann KNN") result=[] for r,donnees in mult_donnees.items(): print("r = "+str(r)) m = GenericModele(pyriemann.classification.KNearestNeighbor(n_neighbors=3),donnees) m.dataToCov() m.fit() s = m.score(mesDonnees_test[r].data,mesDonnees_test[r].labels) result.append([r,s]) print("reactionTime,f1score : "+str(result)) return result
def testRiemannMDMPlusXdawn(mult_donnees,mesDonnees_test): print("debut test riemann MDM") result=[] for r,donnees in mult_donnees.items(): print("r = "+str(r)) m = GenericModele(pyriemann.classification.MDM(),donnees) m.dataToXdawnCov() m.fit() s = m.score(mesDonnees_test[r].data,mesDonnees_test[r].labels) result.append([r,s]) print("reactionTime,f1score : "+str(result)) return result
def testSVMPaseBas(nbPoint, slider, mult_donnees): print("debut test passe bas SVM") result = [] for r, donnees in mult_donnees.items(): print("r = " + str(r)) clf = SVC(gamma='auto', probability=True, max_iter=100, verbose=1) m = GenericModele(clf, donnees) m.vectorize() m.dataToMoy(nbPoint, slider) s = m.f1Score() result.append([r, s]) print("reactionTime,f1score : " + str(result)) return result
myFileList = ["data/subject1/Session1/1.gdf"] reactTimeToTest = [1, 0.1, 0.04] mesDonnees = dict() for r in reactTimeToTest: mesDonnees[r] = dataLoader.DataLoader(myFileList, r) os.mkdir("resultats/TFKNN_valeurs_k") for r, donnees in mesDonnees.items(): print(r) result = [] for k in range(1, 20, 2): neigh = KNeighborsClassifier(n_neighbors=k) m = GenericModele(neigh, donnees) m.dataToTf() m.vectorize(tf=True) m.dataToMoy(nbPoint, slider) s = m.f1Score() result.append([k, s]) plt.clf() f = plt.figure() plt.title("F1 score en fonction de la valeur de k") plt.xlabel("Valeur de k") plt.ylabel("F1 score") plt.plot([re[0] for re in result], [re[1] for re in result]) name = "resultats/TFKNN_valeurs_k/KNN_tf_valeur_de_k_pour_time_" + str(r) f.savefig(name + ".pdf", bbox_inches='tight') saveResult(name + ".csv", result)
import pyriemann import csv def saveResult(name, result): myfile = open(name, 'w') wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(["Time(second)", "F1 Score"]) for r in result: wr.writerow(r) reactTimeToTest = [1, 0.1, 0.04] for r in reactTimeToTest: print(r) result = [] for k in range(1, 50, 2): m = GenericModele( r, pyriemann.classification.KNearestNeighbor(n_neighbors=k)) m.load_data_from_file("data/subject1/Session1/1.gdf") m.dataToCov() s = m.f1Score() result.append([k, s]) plt.clf() f = plt.figure() plt.plot([re[0] for re in result], [re[1] for re in result]) name = "resultats/riemannKNN_valeur_de_k_pour_time_" + str(r) f.savefig(name + ".pdf", bbox_inches='tight') saveResult(name + ".csv", result)
wr.writerow(r) myFileList = ["data/subject1/Session1/1.gdf"] reactTimeToTest = [1, 0.1, 0.04] mesDonnees = dict() for r in reactTimeToTest: mesDonnees[r] = dataLoader.DataLoader(myFileList, r) os.mkdir("resultats/rieamannKNN_valeurs_k") for r, donnees in mesDonnees.items(): print(r) result = [] for k in range(1, 20, 2): m = GenericModele( pyriemann.classification.KNearestNeighbor(n_neighbors=k), donnees) m.dataToCov() s = m.f1Score() print([k, s]) result.append([k, s]) plt.clf() f = plt.figure() plt.title("F1 score en fonction de la valeur de k") plt.xlabel("Valeur de k") plt.ylabel("F1 score") plt.plot([re[0] for re in result], [re[1] for re in result]) name = "resultats/rieamannKNN_valeurs_k/riemannKNN_valeur_de_k_pour_time_" + str( r) f.savefig(name + ".pdf", bbox_inches='tight') saveResult(name + ".csv", result)
wr.writerow(r) myFileList = ["data/subject1/Session1/1.gdf"] reactTimeToTest = [1, 0.1, 0.04] mesDonnees = dict() for r in reactTimeToTest: mesDonnees[r] = dataLoader.DataLoader(myFileList, r) os.mkdir("resultats/BrutKNN_valeurs_k") for r, donnees in mesDonnees.items(): print(r) result = [] for k in range(1, 20, 2): neigh = KNeighborsClassifier(n_neighbors=k) m = GenericModele(neigh, donnees) m.vectorize() s = m.f1Score() result.append([k, s]) plt.clf() f = plt.figure() plt.title("F1 score en fonction de la valeur de k") plt.xlabel("Valeur de k") plt.ylabel("F1 score") plt.plot([re[0] for re in result], [re[1] for re in result]) name = "resultats/BrutKNN_valeurs_k/KNN_brut_valeur_de_k_pour_time_" + str( r) f.savefig(name + ".pdf", bbox_inches='tight') saveResult(name + ".csv", result)