Example #1
0
def compare_by_subject(tasks, subjects, position, sessionnum=""):
    for i in range(len(tasks)):
        print('For task ' + tasks[i] + ', position ' + str(position) + ':')
        for j in range(len(subjects)):
            for k in range(j+1, len(subjects)):
                subA = feature_vector_generator(tasks[i], subjects[j], position, sessionnum)
                subB = feature_vector_generator(tasks[i], subjects[k], position, sessionnum)
                X, y = labeler.vectorsAndLabels([subA, subB])
                comparison = 'subject' + str(subjects[j]) + " vs. subject" + str(subjects[k]) + " cross-validation is: " + str(labeler.crossValidate(X, y))
                print(comparison)
Example #2
0
def compare_by_task(tasks, subject1, position, sessionnum="", sq=""):
    print('For subject ' + str(subject1) + ', position ' + str(position) + ':')
    for i in range(len(tasks)):
        for j in range(i+1, len(tasks)):
            for k in range(j+1, len(tasks)):
                for l in range(k+1, len(tasks)):
                    taskA = feature_vector_generator(tasks[i], subject1, position, sessionnum, sq)
                    taskB = feature_vector_generator(tasks[j], subject1, position, sessionnum, sq)
                    taskC = feature_vector_generator(tasks[k], subject1, position, sessionnum, sq)
                    taskD = feature_vector_generator(tasks[l], subject1, position, sessionnum, sq)
                    X, y = labeler.vectorsAndLabels([taskA, taskB, taskC, taskD])
                    comparison = tasks[i] + " vs. " + tasks[j] + " vs. " + tasks[k] + " vs. " + tasks[l] + " cross-validation is: " + str(labeler.crossValidate(X, y))
                    print(comparison)
Example #3
0
def num_feature_vectors(subjects, t0, t1):
    def genlength(gen):
        return len([n for n in gen])

    return [{
        subject: genlength(feature_vector_generator(subject, t0, t1))
    } for subject in subjects]
Example #4
0
def compare_across_all_tasks(tasks, subject, position, sessionnum="", sq=""):
    print('For subject ' + str(subject) + ':')
    for i in range(len(tasks)):
        taskX = feature_vector_generator(tasks[i], subject, position, sessionnum, sq)

        def f():
            return
            yield
        g = f()
        for j in range(i):
            # print(tasks[j])
            taskA = feature_vector_generator(tasks[j], subject, position, sessionnum, sq)
            g = chain(g, taskA)
        for k in range(i+1, len(tasks)):
            # print(tasks[k])
            taskB = feature_vector_generator(tasks[k], subject, position, sessionnum, sq)
            g = chain(g, taskB)
        X, y = labeler.vectorsAndLabels([taskX, g])
        comparison = tasks[i] + " vs. others cross-validation is: " + str(labeler.crossValidate(X, y))
        print(comparison)
Example #5
0
def make_svm_classifier(tasks, subject, position, sessionnum="", sq=""):
    for i in range(len(tasks)):
        print('For subject ' + str(subject) + ', task ' + str(tasks[i]) + ':')
        taskX = feature_vector_generator(tasks[i], subject, position, sessionnum, sq)
        def f():
            return
            yield
        g = f()
        for j in range(i):
            # print(tasks[j])
            taskA = feature_vector_generator(tasks[j], subject, position, sessionnum, sq)
            g = chain(g, taskA)
        for k in range(i+1, len(tasks)):
            # print(tasks[k])
            taskB = feature_vector_generator(tasks[k], subject, position, sessionnum, sq)
            g = chain(g, taskB)
        X, y = labeler.vectorsAndLabels([taskX, g])
        '''Toggle the following for learner function or cross-validation.'''
        # lin_clf = LinearSVC()
        # target = lin_clf.fit(X, y)
        # return target
        comparison = tasks[i] + " vs. others cross-validation is: " + str(labeler.crossValidate(X, y))
        print(comparison)
Example #6
0
def num_feature_vectors(subjects, t0, t1):
  def genlength(gen):
    return len([n for n in gen])
  return [{subject: genlength(feature_vector_generator(subject, t0, t1))}
     for subject in subjects]
Example #7
0

def vectorsAndLabels(arrayOfGenerators):
    '''Takes an array of generators and produces two lists X and y
    where len(X) = len(y),
    X is the vectors, y is their (numerical) labels.'''
    X = []
    y = []
    currentLabel = 0
    for generator in arrayOfGenerators:
        for vector in generator:
            # NB: feature vectors are serialized as strings sometimes
            # we map them to floats
            X.append(vector)
            y.append(currentLabel)
        currentLabel += 1
    return X, y


def crossValidate(X, y):
    "7-fold cross-validation with an SVM with a set of labels and vectors"
    clf = svm.LinearSVC()
    scores = cross_validation.cross_val_score(clf, np.array(X), y, cv=7)
    return scores.mean()

personA_gen = feature_vector_generator('cube', 2, 2)
personB_gen = feature_vector_generator('cube', 1, 2)

X, y = vectorsAndLabels([personA_gen, personB_gen])
print(crossValidate(X, y))
Example #8
0
    y = []
    currentLabel = 0
    for generator in arrayOfGenerators:
        for vector in generator:
            # NB: feature vectors are serialized as strings sometimes
            # we map them to floats
            X.append(vector)
            y.append(currentLabel)
        currentLabel += 1
    return X, y


def crossValidate(X, y):
    "7-fold cross-validation with an SVM with a set of labels and vectors"
    clf = svm.LinearSVC()
    scores = cross_validation.cross_val_score(clf, np.array(X), y, cv=7)
    return scores.mean()


# let's see how well we can distinguish between two subjects based on their brainwaves.
# we'll get their data from a specific time range:
t0 = parse('2015-05-09 23:28:00+00')
t1 = parse('2015-05-09 23:30:31+00')
# and make two generators of feature vectors for the two different subjects:
personA_gen = feature_vector_generator(9, t0, t1)
personB_gen = feature_vector_generator(13, t0, t1)
# now let's feed these feature vectors into an SVM
# and do 7-fold cross-validation.
X, y = vectorsAndLabels([personA_gen, personB_gen])
print crossValidate(X, y)
  X is the vectors, y is their (numerical) labels.'''
  X = []
  y = []
  currentLabel = 0
  for generator in arrayOfGenerators:
  	for vector in generator:
  		# NB: feature vectors are serialized as strings sometimes
  		# we map them to floats
  		X.append(vector)
  		y.append(currentLabel)
  	currentLabel+=1
  return X, y

def crossValidate(X,y):
  "7-fold cross-validation with an SVM with a set of labels and vectors"
  clf = svm.LinearSVC()
  scores = cross_validation.cross_val_score(clf, np.array(X), y, cv=7)
  return scores.mean()

# let's see how well we can distinguish between two subjects based on their brainwaves.
# we'll get their data from a specific time range:
t0 = parse('2015-05-09 23:28:00+00')
t1 = parse('2015-05-09 23:30:31+00')
# and make two generators of feature vectors for the two different subjects:
personA_gen = feature_vector_generator(9, t0, t1)
personB_gen = feature_vector_generator(13, t0, t1)
# now let's feed these feature vectors into an SVM
# and do 7-fold cross-validation.
X, y = vectorsAndLabels([personA_gen, personB_gen])
print crossValidate(X, y)