def calculateAndWriteExecutionTime(classifierstring = 'AllFeatures', shift = False, windowLength = 250, Sort = False): featuremask = features.readFeatureMask(classifierstring) nrOfFeatures = len(featuremask) dictionary = {} featureName = None InputData = [[]] executionTimeList = [] X1, y1 = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=shift, windowLength=windowLength) X1, y1 = dataset.sortDataset(X1, y1, length=130, classes=[0,1,2,3,4,5,6,7,8,9], merge = True) #,6,4,2,8 #Just append some movement into the input data from each channel for i in range(8): InputData.append(X1[i][0]) #Pop the first list because it is empty InputData.pop(0) #just iterate through all the features for i in range(len(featuremask)): wrapped = wrapper(calculateFeature, i, InputData) featureString = str(features.FUNC_MAP.get(i)) featureString = featureString.split(" ") featureName = featureString [1] + " " dictionary[featureName] = min(timeit.repeat(wrapped, repeat = 10000, number = 1)) print("finished with feature %d" %i) executionTimeList.append(dictionary[featureName]) writeToFile(executionTimeList, windowLength) #Should the list be sorted and printed? if Sort: sortedValues = sorted(list(dictionary.values())) sortedKeys = sorted(list(dictionary), key = dictionary.__getitem__) printSortedValues(sortedKeys, sortedValues)
def startLearning(): bestParams = [] accuracyScore = [] f1Score = [] precision = [] classificationReport = [] classifierstring = "learning260RBFsvm22Features" #Make sure that the result can be reproduced seed = 7 np.random.seed(seed) X, y = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=False, windowLength=250) X, y = dataset.sortDataset(X, y, length=10000, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], merge=True) #numClasses = 6 channelIndex = 0 featuremask = features.readFeatureMask() #Use number of features as input layer #numFeatures = len(featuremask) XL = features.extractFeaturesWithMask(X, channelIndex, featuremask=featuremask, printTime=False) XLtrain, XLtest, yTrain, yTest, XL, scaler = classifier.scaleAndSplit( XL, y[0]) #One hot encoding of the classes #yTrain = np_utils.to_categorical(yTrain) #yTest = np_utils.to_categorical(yTest) #Define variable with number of classes clf = KerasClassifier(build_fn=createModel, epochs=10, batch_size=50, verbose=0) #clf.fit(XLtrain, yTrain, validation_data = (XLtest, yTest), epochs = 10, batch_size = 200, verbose = 2) clf.fit(XLtrain, yTrain) #clf.fit(XLtrain, yTrain, validation_data = (XLtest, yTest), epochs = 10, batch_size = 50) #scores = model.evaluate(Xtest, yTest, verbose = 0) #print('Baseline Error: %.2f%%' %(100 - scores[1]*100)) scores = cross_val_score(clf, XLtrain, yTrain, cv=50, scoring='accuracy') print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) print() print("Scores") print(scores)
def main(status): if status == 'generate': generateDatasets(dataset_name, n_train, n_valid, n_test) main('train') elif status == 'train': model_name = md.train(dataset_name, batch_size=512, debug=False) main('evaluate') pass elif status == 'evaluate': model = md.loadModel(model_path) dataset = ds.loadDataset(dataset_path + dataset_name + '/' + test_file) for path, y in zip(dataset['path'], dataset['y']): img = utils.loadImage(path) p = model.predict(img[None, :, :, :], batch_size=1) print(p, y) p = p[0] y_pred = [[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]] img_dbg = drawPoints(img, y_pred, colors) utils.showImage(img_dbg) pass
import dataset from classifier import Classifier print("Loading dataset...") trainSet, validationSet, testSet = dataset.loadDataset() print("Dataset loaded!") class1 = Classifier(leaky_RELU=True) class1.fit(trainSet["data"], trainSet["one_hot"], validationSet=validationSet, graphics=False) #class1.grid_search(trainSet["data"], trainSet["one_hot"], validationSet, [0.1, 0.01, 0.001, 0.0001, 0.9, 0.00001, 0.5], [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1], [0.95]) #class1.random_search(trainSet["data"][:, :5000], trainSet["one_hot"][:, :5000], validationSet, 100, [0.001, 0.02], [5.0e-05, .001]) print("Final accuracy:") print(" " + str(class1.computeAccuracy(testSet["data"], testSet["labels"])))
def createPredictor(name, windowLength, datasetnum=1, shift=None, bruteForcemask=None, zeroClassMultiplier=2, datasetLength=130, kernel='rbf'): ##### Parameters if shift == None: if windowLength < 250: shift = True print "Shift is true" else: shift = False print "Shift is false" else: print("Shift is: %r" % shift) ##### Save parameters parameters = { 'windowLength': windowLength, 'shift': shift, 'dataset': datasetnum, 'datasetLength': datasetLength, 'kernel': kernel } pickle.dump(parameters, open("Parameters" + slash + name + ".pkl", "wb")) ##### Declarations bestParams = [] accuracyScore = [] f1Score = [] precision = [] classificationReport = [] XL = [[], [], [], [], [], [], [], []] y = [[], [], [], [], [], [], [], []] XLlist = [] ylist = [] XLtrain = None XLtest = None yTrain = None yTest = None ##### Code if isinstance(datasetnum, int): var = datasetnum datasetnum = [] datasetnum.append(var) print(datasetnum) for i in datasetnum: print i dataset.setDatasetFolder(i) X, Y = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=shift, windowLength=windowLength) Xl, Y = dataset.sortDataset(X, Y, length=datasetLength, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], merge=True, zeroClassMultiplier=zeroClassMultiplier) XLlist.append(Xl) ylist.append(Y) XL, y = dataset.mergeDatasets(XL, Xl, y, Y) #XL, y = dataset.mergeDatasets(XLlist, XLtest, yTrain, yTest) if bruteForcemask == None: features.compareFeatures2(name, shift, windowLength, X=XL, y=y, plot=False) featuremask = features.readFeatureMask(name) else: featuremask = features.readFeatureMask(bruteForcemask) features.writeFeatureMask(featuremask, name) for i in range(len(XLlist)): XLlist[i] = features.extractFeaturesWithMask(XLlist[i], featuremask=featuremask, printTime=False) print("XL list featureextraction finished") XL = features.extractFeaturesWithMask(XL, featuremask=featuremask, printTime=False) print("XL featureextraction finished") scaler = classifier.makeScaler(XL) for i in range(len(XLlist)): XLtrain1, XLtest1, yTrain1, yTest1, XLlist[ i] = classifier.scaleAndSplit(XLlist[i], ylist[i][0], scaler) if i == 0: XLtrain = XLtrain1 yTrain = yTrain1 XLtest = XLtest1 yTest = yTest1 else: XLtrain, yTrain = dataset.mergeDatasets(XLtrain, XLtrain1, yTrain, yTrain1) XLtest, yTest = dataset.mergeDatasets(XLtest, XLtest1, yTest, yTest1) print("Split fininshed, starting training") if kernel == 'rbf': clf = svm.SVC(kernel='rbf', gamma=0.01, C=10, decision_function_shape='ovr') elif kernel == 'linear': clf = svm.SVC(kernel='linear', gamma=0.01, C=10, decision_function_shape='ovr') elif kernel == 'linearSVC': clf = svm.LinearSVC(penalty='l2', dual=False, C=10, random_state=42) clf.fit(XLtrain, yTrain) classifier.saveMachinestate(clf, name) classifier.saveScaler(scaler, name) scores = cross_val_score(clf, XLtrain, yTrain, cv=10, scoring='recall_macro') print("Recall: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) print() print("Scores") print(scores) tempAccuracyScore, tempPrecision, tempClassificationReport\ , tempf1Score = classifier.predict(XLtest, clf, yTest) accuracyScore.append(tempAccuracyScore) f1Score.append(tempf1Score) precision.append(tempPrecision) classificationReport.append(tempClassificationReport) print() print("The best parameters for the different channels are:") print() print(bestParams) print() print("The prediction accuracy for the different channels is:") print(accuracyScore) print("The f1 score which include false negatives etc is:") print( f1Score ) #This score says something about the correctness of the prediction. print("The precision score:") print( precision ) #This score says something about the correctness of the prediction. print("Classification Report:") print(classificationReport[0])
def multiplottestHHT(c): x, y = dataset.loadDataset("longdata.txt", filterCondition=True, filterType="DC") #print(y[0]) x, y = dataset.sortDataset(x, y, length=10, classes=[c]) channelsToPlot = 4 N_imfs = 6 for i in range(len(x[0])): #Iternate over number of elements in dataset title = movements[y[0][i]] #print title title = title[1:] #print title fig = plt.figure(figsize=(20, 10)) #fig = plt.figure() plt.suptitle(title) outer = gridspec.GridSpec(1, channelsToPlot, wspace=0.1, hspace=0.2, right=0.98, left=0.02, bottom=0.02, top=0.95) for j in range(channelsToPlot): #Iterate over channels inner = gridspec.GridSpecFromSubplotSpec(N_imfs + 1, 1, subplot_spec=outer[j], wspace=0.1, hspace=0.1) xt = x[j][i] imfs = fixedIterationHht(xt) n_imfs = imfs.shape[0] length = len(xt) t = np.arange(0, length / glb.fs, 1.0 / glb.fs) time_samples = t signal = xt ax = plt.Subplot(fig, inner[0]) #ax = plt.subplot(n_imfs + 1, 1, 1) ax.plot(time_samples, signal) ax.axis([ time_samples[0], time_samples[-1], signal.min(), signal.max() ]) ax.tick_params(which='both', left=False, bottom=False, labelleft=False, labelbottom=False) ax.grid(False) ax.set_ylabel('Signal') ax.set_title(channels[j]) fig.add_subplot(ax) axis_extent = max(np.max(np.abs(imfs[:-1, :]), axis=0)) for k in range(n_imfs - 1): #print(i + 2) ax = plt.Subplot(fig, inner[k + 1]) #ax = plt.subplot(n_imfs + 1, 1, i + 2) #ax.plot(time_samples, imfs[k, :]) ax.plot(time_samples, imfs[k, :]) ax.axis([ time_samples[0], time_samples[-1], -axis_extent, axis_extent ]) ax.tick_params(which='both', left=False, bottom=False, labelleft=False, labelbottom=False) ax.grid(False) ax.set_ylabel('imf' + str(k + 1)) fig.add_subplot(ax) ax = plt.Subplot(fig, inner[n_imfs]) #ax = plt.subplot(n_imfs + 1, 1, n_imfs + 1) ax.plot(time_samples, imfs[-1, :], 'r') ax.axis('tight') ax.tick_params(which='both', left=False, bottom=False, labelleft=False, labelbottom=False) ax.grid(False) ax.set_ylabel('res.') fig.add_subplot(ax) plt.show()
def startLearning(): bestParams = [] accuracyScore = [] f1Score = [] precision = [] classificationReport = [] #classifierstring = "learning260RBFsvm22Features" classifierstring = 'AllFeatures' #X, y = dataset.loadDataset("longdata.txt") ''' X, y = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=False, windowLength=250) ''' dataset.setDatasetFolder(1) X1, y1 = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=False, windowLength=250) X1, y1 = dataset.sortDataset(X1, y1, length=130, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], merge=True) #,6,4,2,8 ''' X1T, y1T = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=False, windowLength=250) X1T, y1T = dataset.sortDataset(X1T, y1T, length=130, classes=[0,1,2,3,4,5,6,7,8,9], merge = True) #,6,4,2,8 ''' dataset.setDatasetFolder(2) X2, y2 = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=True, windowLength=2) X2, y2 = dataset.sortDataset(X2, y2, length=130, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], merge=True, zeroClassMultiplier=1) #,6,4,2,8 ''' X2T, y2T = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=False, windowLength=250) X2T, y2T = dataset.sortDataset(X2T, y2T, length=130, classes=[0,1,2,3,4,5,6,7,8,9], merge = True) #,6,4,2,8 ''' #X, y = dataset.sortDataset(X, y, length=10000, classes=[6,8], merge = False) #def sortDataset(x=None, y=None, length=10, classes=[0,5,4,2,6,8]) #if x or y is undefined, data.txt will be loaded channelIndex = 0 ''' FUNC_MAP = {0: hfd, 1: minDiff, 2: maxDiff, 3: specEntropy, 4: pearsonCoeff14, 5: stdDeviation, 6: slope, 7: thetaBeta1, 8: extrema 9: pearsonCoeff13} ''' #XL = features.extractFeatures(X, channelIndex) featuremask = features.readFeatureMask(classifierstring) XL1 = features.extractFeaturesWithMask(X1, featuremask=featuremask, printTime=False) XL2 = features.extractFeaturesWithMask(X2, featuremask=featuremask, printTime=False) #If a test set is needed for the combined subject model ''' XL1T = features.extractFeaturesWithMask( X1T, featuremask=featuremask, printTime=False) XL2T = features.extractFeaturesWithMask( X2T, featuremask=featuremask, printTime=False) ''' #uncomment for using samples as features ''' XL2 = X2[0] print(len(X2[0])) for i in range(len(X2[0])): #XL2[i] = np.concatenate((XL2[i], X2[1][i], X2[3][i])) #np.append(XL[i], X[1][i]) #np.append(XL[i], X[2][i]) #np.append(XL[i], X[3][i]) print(len(XL2[0])) ''' #Scale the data if needed and split dataset into training and testing #proposed solution, change input to makeScaler when creating for one subject. See classifier.py #XLjoined = np.append(XL1, XL2, axis = 0) scaler = classifier.makeScaler(XL2) XLtrain1, XLtest1, yTrain1, yTest1, XL1 = classifier.scaleAndSplit( XL1, y1[0], scaler) XLtrain2, XLtest2, yTrain2, yTest2, XL2 = classifier.scaleAndSplit( XL2, y2[0], scaler) #XLtrain1T, XLtest1T, yTrain1T, yTest1T, XL1T = classifier.scaleAndSplit(XL1T, y1T[0], scaler) #XLtrain2T, XLtest2T, yTrain2T, yTest2T, XL2T = classifier.scaleAndSplit(XL2T, y2T[0], scaler) #This is to combine the two subjects #yTrain = np.append(yTrain1, yTrain2, axis = 0) #XLtrain = np.append(XLtrain1, XLtrain2, axis = 0) #yTest = np.append(yTest1, yTest2, axis = 0) #XLtest = np.append(XLtest1, XLtest2, axis = 0) #bestParams.append(classifier.tuneSvmParameters(XLtrain, yTrain, XLtest, yTest, n_jobs = -1)) #bestParams.append(tuneDecisionTreeParameters(XLtrain, yTrain, XLtest, yTest, n_jobs = -1)) #try this with tuning of parameters later today. #clf, clfPlot = createAndTrain(XLtrain, yTrain, bestParams[0]) #Use this if predictor other than SVM is used. clf, clfPlot = createAndTrain(XLtrain2, yTrain2, None) #plot.trainingPredictions(clf, XL, y[0]) ###TO PLOT LEARNING CURVE UNCOMMENT THIS. #title = "Learning Curves (SVM, RBF kernel, C = 50, $\gamma=0.001$)" #estimator = svm.SVC(kernel = 'rbf', gamma = 0.01, C = 50, decision_function_shape = 'ovr') #plot.learningCurve(estimator, title, XL, y[0], (0.7, 1.01), cv=20, n_jobs=-1) #plt.show() #clf = classifier.loadMachineState(classifierstring) classifier.saveMachinestate( clf, classifierstring) #Uncomment this to save the machine state classifier.saveScaler(scaler, classifierstring) #clf = CalibratedClassifierCV(svm.SVC(kernel = 'linear', C = C, decision_function_shape = 'ovr'), cv=5, method='sigmoid') #Use this if it is important to see the overall prediction, and not for only the test set scores = cross_val_score(clf, XLtrain2, yTrain2, cv=50, scoring='accuracy') print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) print() print("Scores") print(scores) tempAccuracyScore, tempPrecision, tempClassificationReport, tempf1Score = classifier.predictTimer( XLtest2, clf, yTest2) accuracyScore.append(tempAccuracyScore) f1Score.append(tempf1Score) precision.append(tempPrecision) classificationReport.append(tempClassificationReport) #crossValScore.append(tempCrossValScore) #accuracyScore, classificationReport = compareFeatures(XL, XLtrain, yTrain, XLtest, yTest, bestParams) print() print("The best parameters for the different channels are:") print() print(bestParams) print() print("The prediction accuracy for the different channels is:") print(accuracyScore) print("The f1 score which include false negatives etc is:") print( f1Score ) #This score says something about the correctness of the prediction. print("The precision score:") print( precision ) #This score says something about the correctness of the prediction. print( "Classification Report of channel %d:" % channelIndex ) #String, weird if you print whole array with string, and predicting over several channels. print(classificationReport[0])
def compareFeatures(n_jobs=1, datasetnum=1, shift=False, windowLength=250, zeroClassMultiplier=1): #array declaration trainings = 0 allPermutations = [] allParams = [] allPavg = [] allP = [] allR = [] allF1 = [] allS = [] #Constants and configuration maxNumFeatures = 8 minNumFeatures = 6 #Must be bigger than 1 #datasetfile = "longdata.txt" datasetfile = "data.txt" merge = True skip = False logging = False print( "Setup for brute force testing of all feature combination, features in list: %d" % len(FUNC_MAP)) print("Enter maximum number of features: (\"all\" for all combinations)") inputString = raw_input() if inputString.isdigit(): inputval = int(inputString) if inputval > len(FUNC_MAP): print("Invalid input, exiting") return if inputval >= 1: maxNumFeatures = inputval else: print("Invalid input, exiting") return else: if inputString == "all": maxNumFeatures = len(FUNC_MAP) minNumFeatures = 1 skip = True else: print("Invalid input, exiting") return if not skip: print("Enter minimum number of features: ") inputString = raw_input() if inputString.isdigit(): inputval = int(inputString) if inputval <= 0: print("Invalid input, exiting") return else: print("Invalid input, exiting") return if inputval >= 1: minNumFeatures = inputval else: print("Invalid input, exiting") return print("Is this a debug session?(Will leak memory) [Y/n]") inputString = raw_input() if inputString == "Y": debug = True print( "Debug session activated, results will not be valid and memory will explode." ) sendMail = False logging = False else: debug = False print("Normal session activated, results will be valid. ") print("Do you want to send a mail notification when finished? [Y/n]") inputString = raw_input() if inputString == "Y": sendMail = True print("Sending mail when script is finished") else: print("Do not send mail when script is finished") sendMail = False print("Do you want to save logs? [Y/n]") inputString = raw_input() if inputString == "Y": logging = True print("Saving logs") else: print("Do not save logs") logging = False #Load dataset #print("Before load") ''' X, y = dataset.loadDataset(filename="data.txt", filterCondition=True, filterType="DcNotch", removePadding=True, shift=False, windowLength=200) #print("After load") #print X if datasetfile == "longdata.txt": classes = [0,5,6,4,2,8] else: classes = [0,1,2,3,4,5,6,7,8,9] #classes = [9,7,3,1,0,5] X, y = dataset.sortDataset(X, y, length=1000, classes=classes, merge=merge, zeroClassMultiplier=1.2) if merge: classes = [0,5,6,4,2,8] #y = dataset.mergeLabels(y) else: classes = [0,1,2,3,4,5,6,7,8,9] #Calculate features #XL = extractAllFeatures(X, channel=0) XL = extractFeaturesWithMask(X, featuremask=range(len(FUNC_MAP))) #XLtrain, XLtest, yTrain, yTest = classifier.scaleAndSplit(XL, y[0]) scaler = classifier.makeScaler(XL) XLtrain, XLtest, yTrain, yTest, XL = classifier.scaleAndSplit(XL, y[0], scaler) ''' XL = [[], [], [], [], [], [], [], []] y = [[], [], [], [], [], [], [], []] XLlist = [] ylist = [] XLtrain = None XLtest = None yTrain = None yTest = None ##### Code if isinstance(datasetnum, int): var = datasetnum datasetnum = [] datasetnum.append(var) print(datasetnum) for i in datasetnum: print i dataset.setDatasetFolder(i) X, Y = dataset.loadDataset(filename=datasetfile, filterCondition=True, filterType="DcNotch", removePadding=True, shift=shift, windowLength=windowLength) Xl, Y = dataset.sortDataset(X, Y, length=130, classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], merge=True, zeroClassMultiplier=zeroClassMultiplier) XLlist.append(Xl) ylist.append(Y) XL, y = dataset.mergeDatasets(XL, Xl, y, Y) for i in range(len(XLlist)): XLlist[i] = extractFeaturesWithMask(XLlist[i], featuremask=range(len(FUNC_MAP)), printTime=False) print("XL list featureextraction finished") XL = extractFeaturesWithMask(XL, featuremask=range(len(FUNC_MAP)), printTime=False) print("XL featureextraction finished") scaler = classifier.makeScaler(XL) for i in range(len(XLlist)): XLtrain1, XLtest1, yTrain1, yTest1, XLlist[ i] = classifier.scaleAndSplit(XLlist[i], ylist[i][0], scaler) if i == 0: XLtrain = XLtrain1 yTrain = yTrain1 XLtest = XLtest1 yTest = yTest1 else: XLtrain, yTrain = dataset.mergeDatasets(XLtrain, XLtrain1, yTrain, yTrain1) XLtest, yTest = dataset.mergeDatasets(XLtest, XLtest1, yTest, yTest1) print("Split fininshed, starting training") features = range(len(XL[0])) print("Featureextraction finished, number of features to check: %d" % len(XL[0])) if len(features) < maxNumFeatures: maxNumFeatures = len(features) elif maxNumFeatures < minNumFeatures: maxNumFeatures = minNumFeatures if minNumFeatures < 1: minNumFeatures = 1 elif minNumFeatures > maxNumFeatures: minNumFeatures = maxNumFeatures print("Testing with combinations of %d to %d" % (minNumFeatures, maxNumFeatures)) numberOfCombinations = 0 for i in range(minNumFeatures, maxNumFeatures + 1): #numberOfCombinations += len(list(combinations(features, i))) #Dette sprenger minnet :/ comb = nCr(len(features), i) print("Number of iterations for combinations of length %d: %d" % (i, comb)) numberOfCombinations += comb print("Number of combinations to test %d" % numberOfCombinations) #for i in range(minNumFeatures, maxNumFeatures+1): for i in range(maxNumFeatures, minNumFeatures - 1, -1): print("Starting to read PermutationLog") try: permfile = open( "Logs" + slash + "PermutationLog" + str(i) + ".txt", 'r') #permfile = open("Logs"+slash+"PermutationLog.txt", 'r') except IOError: print("PermutationLog file does not exist") skip = False else: print("Performing operations on PermutationLog buffer") PermutationsString = permfile.read() permfile.close() PermutationsList = PermutationsString.split(':') PermutationsList.pop(0) #PermutationsList = tuple(PermutationsList) #Might need some more processing, now returns a list of tuples #print PermutationsList[28] for q in range(len(PermutationsList)): #print(eval(PermutationsList[i])) #PermutationsList[i] = tuple(map(int, PermutationsList[i][1:-1].split(','))) PermutationsList[q] = tuple(eval(PermutationsList[q])) print("Finished with operations") skip = True start = datetime.now() print("Finished reading permutations file") lastTrainings = 1000 for p in combinations(features, i): #If order matters use permutations if skip == True: if p in PermutationsList: #print("Combination exists") numberOfCombinations -= 1 trainings += 1 if trainings == lastTrainings: lastTrainings = trainings + 1000 print("Training number: %d" % trainings) print("Remaining combinations: %d" % numberOfCombinations) print( "Elapsed time for checking that this combination exists: " + str(elapsedTime)) stop = datetime.now() elapsedTime = (stop - start) start = stop else: print("Found Starting point") skip = False if skip == False: start = datetime.now() XLtrainPerm = np.empty([len(XLtrain), i]) XLtestPerm = np.empty([len(XLtest), i]) for j in range(len(XLtrain)): #print j #print([XLtrain[j][k] for k in p]) XLtrainPerm[j] = [XLtrain[j][k] for k in p] for j in range(len(XLtest)): #print j #print([XLtest[j][k] for k in p]) XLtestPerm[j] = [XLtest[j][k] for k in p] #print(XLtrainPerm[0]) #print(XLtestPerm[0]) #print("Starting to train with combination: "+convertPermutationToFeatureString(p)) bestParams, presc, r, f1, s, report = classifier.tuneSvmParameters( XLtrainPerm, yTrain, XLtestPerm, yTest, debug=False, fast=True, n_jobs=n_jobs) if logging: permfile = open( dir_path + slash + "Logs" + slash + "PermutationLog" + str(i) + ".txt", 'a+') permfile.write(":") permfile.write(str(p)) permfile.close() permfile = open( dir_path + slash + "Logs" + slash + "PrecisionLog" + str(i) + ".txt", 'a+') permfile.write(":") for k in range(len(presc)): permfile.write(',' + str(presc[k])) permfile.close() permfile = open( dir_path + slash + "Logs" + slash + "RecallLog" + str(i) + ".txt", 'a+') permfile.write(":") for k in range(len(r)): permfile.write(',' + str(r[k])) permfile.close() permfile = open( dir_path + slash + "Logs" + slash + "F1Log" + str(i) + ".txt", 'a+') permfile.write(":") for k in range(len(f1)): permfile.write(',' + str(f1[k])) permfile.close() if debug: #Append scores allPermutations.append(p) allParams.append(bestParams) allP.append(presc) allPavg.append(np.average(presc, weights=s)) allR.append(r) allF1.append(f1) winner = allPavg.index( max(allPavg)) #Check for max average precision print(report) print( "Best features so far are: " + convertPermutationToFeatureString( allPermutations[winner])) print("Best result so far are: ", allPavg[winner]) #print("Best parameters for this feature combination: " + str(bestParams)) stop = datetime.now() numberOfCombinations -= 1 trainings += 1 remainingTime = (stop - start) * numberOfCombinations elapsedTime = (stop - start) print("Training number: %d" % trainings) print("Remaining combinations: %d" % numberOfCombinations) print("Elapsed time for training with this combination: " + str(elapsedTime)) print("Estimated remaining time: " + str(remainingTime)) ''' #Evaluate score if len(allPavg) > 1: winner = allPavg.index(max(allPavg)) #Check for max average precision p = allPermutations[winner] XLtrainPerm = np.empty([len(XLtrain), len(p)]) XLtestPerm = np.empty([len(XLtest), len(p)]) p = allPermutations[winner] for j in range(len(XLtrain)): XLtrainPerm[j] = [XLtrain[j][k] for k in p] for j in range(len(XLtest)): XLtestPerm[j] = [XLtest[j][k] for k in p] print("Best features for max average precision are:") print allPermutations[winner] #Test bestParams = allParams[winner] print("Best parameters for max average precision are: ") print(bestParams) if bestParams['kernel'] == 'linear': clf = svm.SVC(kernel =bestParams['kernel'], C = bestParams['C'], decision_function_shape = 'ovr') else: clf = svm.SVC(kernel = bestParams['kernel'], gamma=bestParams['gamma'], C= bestParams['C'], decision_function_shape='ovr') clf.fit(XLtrainPerm, yTrain) saveMachinestate(clf, "BruteForceClassifier") featuremask = open("featuremask.txt", 'w+') featuremask.write(str(allPermutations[winner])) #featuremask.write(",") featuremask.close() yPred = clf.predict(XLtestPerm) print(classification_report(yTest, yPred)) ''' if sendMail: mail.sendemail( from_addr='*****@*****.**', to_addr_list=['*****@*****.**', '*****@*****.**'], cc_addr_list=[], subject="Training finished with combinations of %d to %d features" % (minNumFeatures, maxNumFeatures), message="Logs are ready for download ", login='******', password='******')
def compareFeatures2(name, shift, windowLength, n_jobs=-1, X=None, y=None, plot=True): #datasetfile = "longdata.txt" datasetfile = "data.txt" merge = True if (X == None) or (y == None): X, y = dataset.loadDataset(filename=datasetfile, filterCondition=True, filterType="DcNotch", removePadding=True, shift=shift, windowLength=windowLength) #print("After load") #print X if datasetfile == "longdata.txt": classes = [0, 5, 6, 4, 2, 8] else: classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #classes = [9,7,3,1,0,5] X, y = dataset.sortDataset(X, y, length=1000, classes=classes, merge=merge) #,6,4,2,8 if merge: classes = [0, 5, 6, 4, 2, 8] #y = dataset.mergeLabels(y) else: classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #Calculate features #XL = extractAllFeatures(X, channel=0) XL = extractFeaturesWithMask(X, featuremask=range(len(FUNC_MAP))) scaler = classifier.makeScaler(XL) XLtrain, XLtest, yTrain, yTest, XL = classifier.scaleAndSplit( XL, y[0], scaler) #scaler = StandardScaler() #XL = scaler.fit_transform(XL, y[0]) #XLtest = scaler.fit_transform(XLtest, yTest) clf = svm.SVC(kernel="linear", C=10, decision_function_shape='ovr') #clf = svm.LinearSVC(penalty = 'l2', loss='squared_hinge', dual = False, C = 10, random_state = 42) #clf = RandomForestClassifier(n_estimators = 45, max_depth = 10, min_samples_leaf = 1, random_state = 40) #clf = svm.LinearSVC(penalty = 'l2', dual = False, C = 10, random_state = 42) #clf = linear_model.SGDClassifier(penalty = 'l2', random_state = 42) rfecv = RFECV(estimator=clf, step=1, cv=10, n_jobs=n_jobs, scoring='accuracy') rfecv.fit(XL, y[0]) print("Optimal number of features : %d" % rfecv.n_features_) print("Optimal features: ") print(rfecv.support_) writeFeatureMask(rfecv.support_, name) print("The ranking of the features: ") print(rfecv.ranking_) print("The scores for each feature combination:") print(rfecv.grid_scores_) if plot: # Plot number of features VS. cross-validation scores plt.figure() plt.xlabel("Number of features selected") plt.ylabel("Cross validation score (nb of correct classifications)") plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_) plt.show() print("After feature selection: ") scores = cross_val_score(rfecv.estimator_, XLtrain, yTrain, cv=10, scoring='accuracy') print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) print() print("Scores") print(scores)
def keys(): global board, bandstopFilter, filtering, lowpassFilter, bandpassFilter, graphVar global guiVar global predictionParameters, predictioncondition while not killkeys: inputString = raw_input() if "=" in inputString: stringArr = inputString.split('=') string = stringArr[0] if stringArr[1].isdigit(): inputval = int(stringArr[1]) else: inputval = None else: string = inputString inputval = None ''' if string == "notch=true": bandstopFilter = True elif string == "notch=false": bandstopFilter = False elif string == "filter=true": filtering = True print(filtering) elif string == "filter=false": filtering = False print(filtering) elif string == "lowpass=true": lowpassFilter = True elif string == "lowpass=false": lowpassFilter = False elif string == "bandpass=true": bandpassFilter = True elif string == "bandpass=false": bandpassFilter = False ''' if string == "exit": print("Initiating exit sequence") exit = True #if root != None: #root.destroy() #print("Quit gui") if QtGui != None: QtGui.QApplication.quit() #print("Quit Graph") if board != None: print("Closing board") board.stop() board.disconnect() #print("Quit board") os._exit(0) elif string == "plot": plotlib.plot() #plotThread = threading.Thread(target=plot,args=()) #plotThread.start() #plotthread.join() elif string == "plotall": plotlib.plotAll() #plotAllThread = threading.Thread(target=plotAll,args=()) #plotAllThread.start() #plotAllThread.join() #elif string == "save": #save() elif string == "start": threadDataCatcher = threading.Thread(target=dataCatcher, args=()) threadDataCatcher.setDaemon(True) threadDataCatcher.start() elif string == "graph": #graphVar = True thread2 = threading.Thread(target=graph, args=()) thread2.start() elif string == "gui": #app = ttk.App() guiVar = True #threadGui = threading.Thread(target=ttk.guiloop, args=()) #threadGui.setDaemon(True) #threadGui.start() elif string == "makedata": threadDataCatcher = threading.Thread(target=dataCatcher, args=()) #threadDataCatcher.setDaemon(True) threadDataCatcher.start() guiVar = True thread2 = threading.Thread(target=graph, args=()) thread2.start() #graphVar = True #threadGui = threading.Thread(target=gui, args=()) #threadGui.setDaemon(True) #threadGui.start() elif string == "analyzefilter": if inputval != None: filterlib.analyze_filter(inputval) else: filterlib.analyze_filter() elif string == "setfilter": if inputval != None: filterlib.set_filter_q(inputval) else: print("Invalid input") elif string == "exportdataplots": exportThread = threading.Thread(target=dataset.exportPlots, args=("data", "time")) exportThread.start() #dataset.exportPlots("data") elif string == "exporttempplots": exportThread = threading.Thread(target=dataset.exportPlots, args=("temp", "time", "fast")) exportThread.start() #dataset.exportPlots("temp") elif string == "exportallplots": exportThread = threading.Thread(target=dataset.exportPlots, args=("all", "time")) exportThread.start() #dataset.exportPlots("all") elif string == "saveshortdata": dataset.saveShortData() elif string == "savelongdata": dataset.saveLongData() elif string == "saveall": dataset.saveShortData() dataset.saveLongData() elif string == "deleteshortdata": dataset.clear("shortdata") elif string == "deletelongdata": dataset.clear("longdata") elif string == "deleteshorttemp": dataset.clear("shorttemp") elif string == "deletelongtemp": dataset.clear("longtemp") elif string == "deletetemp": dataset.clear("shorttemp") dataset.clear("longtemp") elif string == "savefilter": filterlib.savefiltercoeff() elif string == "deleteshortdataelement": if inputval != None: dataset.deleteelement(inputval, "data.txt") else: print("Invalid input") elif string == "deleteshorttempelement": if inputval != None: dataset.deleteelement(inputval, "temp.txt") else: print("Invalid input") elif string == "deletelongdataelement": if inputval != None: dataset.deleteelement(inputval, "longdata.txt") else: print("Invalid input") elif string == "deletelongtempelement": if inputval != None: dataset.deleteelement(inputval, "longtemp.txt") else: print("Invalid input") elif string == "deleteappendedtemp": dataset.deletesystem("shorttemp") dataset.deletesystem("longtemp") elif string == "deleteappendeddata": dataset.deletesystem("shortdata") dataset.deletesystem("longdata") elif string == "appenddelshorttemp": if inputval != None: dataset.appenddelete(inputval, "shorttemp") else: print("Invalid input") elif string == "appenddellongtemp": if inputval != None: dataset.appenddelete(inputval, "longtemp") else: print("Invalid input") elif string == "appenddelshortdata": if inputval != None: dataset.appenddelete(inputval, "shortdata") else: print("Invalid input") elif string == "appenddellongdata": if inputval != None: dataset.appenddelete(inputval, "longdata") else: print("Invalid input") elif string == "printappenddelshorttemp": dataset.print_appenddelete("shorttemp") elif string == "printappenddelshortdata": dataset.print_appenddelete("shortdata") elif string == "printappenddellongtemp": dataset.print_appenddelete("longtemp") elif string == "printappenddellongdata": dataset.print_appenddelete("longdata") elif string == "removeappenddelshorttemp": if inputval != None: dataset.remove_appenddelete(inputval, "shorttemp") else: print("Invalid input") elif string == "removeappenddelshortdata": if inputval != None: dataset.remove_appenddelete(inputval, "shortdata") else: print("Invalid input") elif string == "removeappenddellongtemp": if inputval != None: dataset.remove_appenddelete(inputval, "longtemp") else: print("Invalid input") elif string == "removeappenddellongdata": if inputval != None: dataset.remove_appenddelete(inputval, "longdata") else: print("Invalid input") #elif string == "viewdataelement": #if inputval != None: #dataset.viewdataelement(inputval) #else: #print("Invalid input") #elif string == "viewtempelement": #if inputval != None: #dataset.viewtempelement(inputval) #else: #print("Invalid input") elif string == "loadpredictor": predictionParameters = predict.setPredictor() elif string == "setdatasetfolder": if inputval != None: dataset.setDatasetFolder(inputval) else: print("Invalid input") elif string == "printdatasetfolder": dataset.printDatasetFolder() elif string == "exportfft": exportThread = threading.Thread(target=dataset.exportPlots, args=("temp", "fft", "fast")) exportThread.start() #dataset.exportPlots("temp", "fft") elif string == "exportraw": exportThread = threading.Thread(target=dataset.exportPlots, args=("data", "raw")) exportThread.start() #dataset.exportPlots("temp", "raw") elif string == "fftplot": if inputval != None: plotlib.fftplot(inputval) plt.show() else: plotlib.fftplot(0) plt.show() elif string == "loaddataset": x, y = dataset.loadDataset("data.txt") #x,y = dataset.sortDataset(x, y, classes=[0,5,2,4,6,8]) print(x[0][0]) elif string == "stats": dataset.datasetStats("data.txt") elif string == "testsave": dataset.saveLongTemp(0) elif string == "learn": learnThread = threading.Thread(target=learning.startLearning, args=()) learnThread.setDaemon(True) learnThread.start() elif string == "predict": print("Starting to load predictor") #predictionParameters = predict.loadPredictor(machinestate) predictioncondition = True #print featuremask print("Predictionsetup complete") elif string == "notpredict": predictioncondition = False elif string == "report": predict.classificationReportGUI() elif string == "clearreport": predict.resetClassificationReport() elif string == "hht": if inputval != None and inputval < 10: hht.multiplottestHHT(inputval) elif string == "drone": predictioncondition = True predictionThread = threading.Thread(target=predict.predictRealTime, kwargs=(predictionParameters)) predictionThread.setDaemon(True) predictionThread.start() #housekeepThread = threading.Thread(target=controller.housekeeper, args=()) #housekeepThread.start() #controller.droneController() controller.originalDroneController() elif string == "speak": threadSpeak = threading.Thread(target=speak.speakSystem, args=()) threadSpeak.setDaemon(True) threadSpeak.start() elif string == "online": predictioncondition = True predictionThread = threading.Thread(target=predict.predictRealTime, kwargs=(predictionParameters)) predictionThread.setDaemon(True) predictionThread.start() #housekeepThread = threading.Thread(target=controller.housekeeper, args=()) #housekeepThread.start() online = threading.Thread( target=controller.onlineVerificationController, args=()) online.start() elif string == "dronesimulator": housekeepThread = threading.Thread(target=controller.housekeeper, args=()) housekeepThread.start() droneSimulatorThread = threading.Thread( target=controller.droneSimulatorController, args=()) droneSimulatorThread.start() elif string == "mujaffa": webbrowser.open( 'http://www.123spill.no/spill/spill-spillet/448/Mujaffa-1.6', new=2) housekeepThread = threading.Thread(target=controller.housekeeper, args=()) housekeepThread.start() mujaffaThread = threading.Thread( target=controller.mujaffaController, args=()) mujaffaThread.start() elif string == "supermario": webbrowser.open( 'http://nrksuper.no/super/spill/super-mario-flash/', new=2) housekeepThread = threading.Thread(target=controller.housekeeper, args=()) housekeepThread.start() superMarioThread = threading.Thread( target=controller.superMarioController, args=()) superMarioThread.start() elif string == "tetris": webbrowser.open('https://tetris.com/play-tetris/', new=2) housekeepThread = threading.Thread(target=controller.housekeeper, args=()) housekeepThread.start() superMarioThread = threading.Thread( target=controller.tetrisController, args=()) superMarioThread.start() elif string == "help": print( "This is a list over essential commands, those ending with = need input values" ) print("exit - exits the system") print( "start - starts the serial communication with the EEG helmet") print("graph - realtime graph with filtered data") print("gui - start the training gui") print("learn - train the system") print( "analyzefilter - makes a plot of amplitude, phase, impule and step response for the current filter" ) print( "exportdataplots - exports time domain plots for dataset, files can be found in Dataset_exports folder" ) print( "exporttempplots - export timedomain plots for temp dataset, files can be found in Dataset_exports folder" ) print( "exportraw - export unfiltered timedomain plots for both temp set and data set" ) print( "exportfft - export fft plots for both temp set and data set") print("helpdataset - print out the dataset handling commands") print( "+ many more, look in the code in the keys() function in main.py file for more..." ) elif string == "helpdataset": print("This is a list over essential dataset commands") else: print("Unknown command") tme.sleep(0.5)