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)
Beispiel #2
0
def predictGUI(y, clf, scaler, featuremask, windowLength,
               shift):  ### Trenger testing og implementasjon i gui.py
    global yTestGUI, predictionsGUI
    MergeDict = {0: 0, 1: 8, 2: 8, 3: 6, 4: 6, 5: 5, 6: 4, 7: 4, 8: 2, 9: 2}
    y = MergeDict[y]
    yTest = [y]
    #print("Starting to predict with GUI")
    start = time.time()
    X = dataset.shapeArray(windowLength, y)
    if X == -1:
        return

    Xtest = features.extractFeaturesWithMask(X,
                                             featuremask=featuremask,
                                             printTime=False)
    Xtest = classifier.realTimeScale(Xtest, scaler)
    predictions = clf.predict(Xtest)
    #print("Time taken to predict with given examples in GUI:")
    #print(time.time() - start)
    #Print the test data to see how well it performs.
    if yTest == predictions:
        print("Correct prediction of %d!" % predictions[0])
    else:
        print("Should have predicted: %d Actually predicted: %d" %
              (y, predictions[0]))
        #print("Actually predicted: %d" %predictions[0])

    yTestGUI.append(y)
    predictionsGUI.append(predictions[0])

    yfile = open("y.txt", 'a+')
    yfile.write(str(y))
    yfile.write(",")
    yfile.close()

    pfile = open("p.txt", 'a+')
    pfile.write(str(predictions[0]))
    pfile.write(",")
    pfile.close()
Beispiel #3
0
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])
Beispiel #4
0
def predictRealTime(clf,
                    scaler,
                    featuremask,
                    windowLength,
                    shift,
                    debug=False):  ### Trenger testing og implementasjon i main
    #print(X)
    start = datetime.now()
    oldStart = datetime.now()
    print(clf)
    print(featuremask)
    print(windowLength)
    while True:
        tme.sleep(0.010)
        try:
            with glb.rtLock:
                #print("Predict Rt lock")
                start = glb.rtQueue.get(block=False, timeout=None)
        except:
            start = None

        if start != None:

            #start = time.time()
            #start = datetime.now()
            #print(start-oldStart)
            #oldStart = start
            X = dataset.shapeArray(windowLength, checkTimestamp=False)
            #print(len(X[0][0]))
            if (X == -1) or ((len(X[0][0]) < windowLength) and
                             (len(glb.data[0][filterdata]) > 1500)):
                print("Error from shape array")
            else:
                #Xtest = features.extractFeatures(X, 0)
                #L = np.arange(0, len(X[0][0])/glb.fs, 1/glb.fs)

                Xtest = features.extractFeaturesWithMask(
                    X, featuremask=featuremask, printTime=False)
                Xtest = classifier.realTimeScale(Xtest, scaler)
                #print Xtest[0]
                if debug:
                    L = np.arange(0, len(Xtest[0]))
                    plt.ion()
                    plt.show()
                    plt.clf()
                    #for i in range(numCh):
                    plt.plot(L, Xtest[0])
                    plt.ylabel('uV')
                    plt.xlabel('Seconds')
                    plt.draw()
                    plt.pause(0.001)
                    #print(len(Xtest[0]))
                    print("Starting to predict")

                prediction = clf.predict(Xtest)
                #if not(prediction == 5 or prediction == 0):
                #print("The prediction is: %d" %prediction[0])
                #pass
                #print(prediction)
                #with glb.predictionslock:
                #glb.predictions.append(prediction[0])

                with glb.predictionslock:
                    #print("Prediction lock")
                    if not glb.predictionsQueue.full():
                        glb.predictionsQueue.put(prediction[0])
                    else:
                        popped = glb.predictionsQueue.get()
                        glb.predictionsQueue.put(prediction[0])
                        #print("Prediction buffer is full")
                #print("Release prediction lock")
                if debug:
                    timeStop = time.time()

                    #print()
                    print("Time taken to predict with given examples:")
                    print(timeStop - start)
    print("Exiting rtPredict")
Beispiel #5
0
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])