def __init__(self, training_subjects=['a1','a2','c1','c2'], test_subjects=['b'], db=None, sliding_window_size=3): if not db: db = gyroWalkingData() self.db = db self.trainingSet = self.datasetFromSubjects(training_subjects, sliding_window_size=sliding_window_size) self.testingSet = self.datasetFromSubjects(test_subjects, sliding_window_size=sliding_window_size)
def classificationDataSet(subjects=['a2','b','c1','c2'], segClass=0, db=None, seg_width=10, usePCA=True, n_components=5, isTrainingData=False): if not db: db = gyroWalkingData() if usePCA: DS = ClassificationDataSet(n_components*3, nb_classes=2) else: DS = ClassificationDataSet(21*3, nb_classes=2) for subject in subjects: # Initialise data if usePCA: raw = db.pca_dict(n_components=n_components, whiten=False)[subject] else: raw = db.data[subject][:,2:] gradients, standardDeviations = summaryStatistics(raw, std_window=seg_width) # Initialise segments if 0 <= segClass < 4: segs = [s for s,c in db.manual_gait_segments[subject] if c == segClass] else: segs = db.segments[subject] # Add data for i in range(0,len(raw)): """ # Look for segments in window, including those of other classes hasSeg = 0 hasOtherSeg = False for j in range(seg_width): if i+j in segs: hasSeg = 1 else: if i+j in zip(*db.manual_gait_segments[subject])[0]: hasOtherSeg = True if hasOtherSeg: hasSeg = 0 # Add segments to classifier, duplicating rare classes if it is training data for j in range(seg_width): if i+j < len(raw): DS.appendLinked( np.concatenate( [raw[i+j],gradients[i+j],standardDeviations[i+j]] ), [hasSeg] ) if isTrainingData and (hasSeg or hasOtherSeg): for i in range(0): DS.appendLinked( np.concatenate( [raw[i+j],gradients[i+j],standardDeviations[i+j]] ), [hasSeg] ) """ hasSeg = 0 if i in segs: hasSeg = 1 DS.appendLinked( np.concatenate( [raw[i],gradients[i],standardDeviations[i]] ), [hasSeg] ) DS._convertToOneOfMany() if isTrainingData: DS = balanceClassRatios(DS) return DS
def demo(subject='a1', segClass=0, plot_stages=True, saveGraph=False, n_most_likely=50, windowSize=5, clusterThresh=20): db = gyroWalkingData() print "Building neural network" net,trainer=networkAndTrainer(db, segClass=segClass, subjects=list( set(db.segments) - set([subject])), windowSize=windowSize) print "Training network..." train(trainer, epochs=25) print "Finding segments" testingData = classificationDataSet(subjects=[subject], segClass=segClass, seg_width=windowSize, db=db) top_segments, likelihoods = getSegments(net, db, testingData, subject=subject, n_segments=n_most_likely, plot=plot_stages) print "Clustering segments" clusters = getClusters(top_segments, db, subject=subject, thresh=clusterThresh, plot=plot_stages) print "Showing most likely segment from each cluster" segments = mostLikelySegsInClusters(top_segments, clusters, likelihoods) db.plotSegments(db.data[subject], segments, name=("%s - segClass %i"%(subject, segClass)), save=saveGraph)
def demo(subject='a1', plot_stages=True): db = gyroWalkingData() print "Building neural network" net,trainer=networkAndTrainer(db, holdouts=[subject]) print "Training network..." train(trainer, epochs=25) print "Finding segments" testingData = classificationDataSet(subject=subject, db=db) top_segments, likelihoods = getSegments(net, db, testingData, subject=subject, n_segments=140, plot=plot_stages) print "Clustering segments" clusters = getClusters(top_segments, db, subject=subject, plot=plot_stages) print "Showing most likely segment from each cluster" segments = mostLikelySegsInClusters(top_segments, clusters, likelihoods) db.plotSegments(db.data[subject], segments)
def classificationDataSet(subject='a1', db=None): if not db: db = gyroWalkingData() raw = db.data[subject][:,2:] segs = db.segments[subject] DS = ClassificationDataSet(21, nb_classes=2) for i in range(0,len(raw),5): hasSeg = 0 for j in range(5): if i+j in segs: hasSeg = 1 for j in range(5): if i+j < len(raw): DS.appendLinked(raw[i+j],[hasSeg]) DS._convertToOneOfMany() return DS
def sequenceClassificationDataSet(subject='a1', db=None): """Don't know if this is set up right or how to use it""" if not db: db = gyroWalkingData() raw = db.data[subject][:,2:] segs = db.segments[subject] DS = SequenceClassificationDataSet(21, 1) for i in range(0,len(raw),10): DS.newSequence() isSeg = 0 for j in range(10): if i+j in segs: isSeg = 1 else: isSeg = 0 DS.appendLinked(raw[i+j],[isSeg]) DS._convertToOneOfMany() return DS
def classificationTrainingSet(holdouts=['a1'], db=None): if not db: db = gyroWalkingData() DS = ClassificationDataSet(21, nb_classes=2) for subject in db.data: if subject not in holdouts: raw = db.data[subject][:,2:] segs = db.segments[subject] seg_width = 2 for i in range(0,len(raw),seg_width): hasSeg = 0 for j in range(seg_width): if i+j in segs: hasSeg = 1 for j in range(seg_width): if i+j < len(raw): DS.appendLinked(raw[i+j],[hasSeg]) DS._convertToOneOfMany() return DS