def simMatrixAverageClassDistancesHighDimArmEx(db=None): if db is None: db = armExercisesDatabase.db(True) classes = {key:value[1] for (key,value) in zip(db.data,db.segs.values())} classes = {key[:-1]:classes[key] for key in classes.keys() if 'l' in key} # Left hand motions only weights = {key:[[1]*np.shape(segments[0])[1]]*len(segments) for (key,segments) in zip(classes.keys(),classes.values())} m.averageSimilarityMatrix(classes, weights, "Distances between clusters of exercises with left arm", savePlot=True)
def __init__(self, split=0.5): data = armExercisesDatabase.db(True) # High dimensional data LRclasses = {key:value for (key,value) in zip(data.HDsegs.keys(),[sum(l,[]) for l in data.HDsegs.values()])} classes = {} for key,value in LRclasses.iteritems(): for segment in value: classes.setdefault(key[:-1], []).append(segment) for segments in classes.values(): random.shuffle(segments) self.HDtraining = {'10 degrees':classes['10'][:(int(len(classes['10'])*split))], '20 degrees':classes['20'][:(int(len(classes['20'])*split))], '30 degrees':classes['30'][:(int(len(classes['30'])*split))], '40 degrees':classes['40'][:(int(len(classes['40'])*split))], '50 degrees':classes['50'][:(int(len(classes['50'])*split))], '60 degrees':classes['60'][:(int(len(classes['60'])*split))], '70 degrees':classes['70'][:(int(len(classes['70'])*split))], '80 degrees':classes['80'][:(int(len(classes['80'])*split))], '90 degrees':classes['90'][:(int(len(classes['90'])*split))]} self.HDtest = {'10 degrees':classes['10'][(int(len(classes['10'])*split)):], '20 degrees':classes['20'][(int(len(classes['20'])*split)):], '30 degrees':classes['30'][(int(len(classes['30'])*split)):], '40 degrees':classes['40'][(int(len(classes['40'])*split)):], '50 degrees':classes['50'][(int(len(classes['50'])*split)):], '60 degrees':classes['60'][(int(len(classes['60'])*split)):], '70 degrees':classes['70'][(int(len(classes['70'])*split)):], '80 degrees':classes['80'][(int(len(classes['80'])*split)):], '90 degrees':classes['90'][(int(len(classes['90'])*split)):]} # Low dimensional data LRclasses = {key:value for (key,value) in zip(data.LDsegs.keys(),[sum(l,[]) for l in data.LDsegs.values()])} classes = {} for key,value in LRclasses.iteritems(): for segment in value: classes.setdefault(key[:-1], []).append(segment) for segments in classes.values(): random.shuffle(segments) self.LDtraining = {'10 degrees':classes['10'][:(int(len(classes['10'])*split))], '20 degrees':classes['20'][:(int(len(classes['20'])*split))], '30 degrees':classes['30'][:(int(len(classes['30'])*split))], '40 degrees':classes['40'][:(int(len(classes['40'])*split))], '50 degrees':classes['50'][:(int(len(classes['50'])*split))], '60 degrees':classes['60'][:(int(len(classes['60'])*split))], '70 degrees':classes['70'][:(int(len(classes['70'])*split))], '80 degrees':classes['80'][:(int(len(classes['80'])*split))], '90 degrees':classes['90'][:(int(len(classes['90'])*split))]} self.LDtest = {'10 degrees':classes['10'][(int(len(classes['10'])*split)):], '20 degrees':classes['20'][(int(len(classes['20'])*split)):], '30 degrees':classes['30'][(int(len(classes['30'])*split)):], '40 degrees':classes['40'][(int(len(classes['40'])*split)):], '50 degrees':classes['50'][(int(len(classes['50'])*split)):], '60 degrees':classes['60'][(int(len(classes['60'])*split)):], '70 degrees':classes['70'][(int(len(classes['70'])*split)):], '80 degrees':classes['80'][(int(len(classes['80'])*split)):], '90 degrees':classes['90'][(int(len(classes['90'])*split)):]} self.averageWeights = data.averageExplainedVariance
def armExercisesHDsimMatrix(db=None): if db is None: db = armExercisesDatabase.db(True) LRclasses = {key:random.sample(value,1) for (key,value) in zip(db.HDsegs.keys(),[sum(l,[]) for l in db.HDsegs.values()])} classes = {} for key,value in LRclasses.iteritems(): for segment in value: classes.setdefault(key[:-1], []).append(segment) # Merge left and right hand motions together weights = {key:[[1]*np.shape(segments[0])[1]]*len(segments) for (key,segments) in zip(classes.keys(),classes.values())} m.averageSimilarityMatrix(classes, weights, "Distances between clusters of exercises", savePlot=True)
def armExercisesHDParallelsimMatrix(db=None): if db is None: db = armExercisesDatabase.db(True) LRclasses = {key:value for (key,value) in zip(db.HDsegs.keys(),[sum(l,[]) for l in db.HDsegs.values()])} classes = {} for key,value in LRclasses.iteritems(): for segment in value: classes.setdefault(key[:-1], []).append(segment) # Merge left and right hand motions together weights = {key:[[1]*np.shape(segments[0])[1]]*len(segments) for (key,segments) in zip(classes.keys(),classes.values())} parallelSimilarityMatrix.averageSimilarityMatrix(classes, weights, "High Dimensional Arm Cluster Comparisons", savePlot=True)
def armExercisesLDParallelsimMatrix(db=None): if db is None: db = armExercisesDatabase.db(True) LRclasses = {key:value for (key,value) in zip(db.LDsegs.keys(),[sum(l,[]) for l in db.LDsegs.values()])} classes = {} for key,value in LRclasses.iteritems(): for segment in value: classes.setdefault(key[:-1], []).append(segment) # Merge left and right hand motions together averageWeight = [float(sum(t))/len(t) for t in zip(*[[float(sum(t))/len(t) for t in zip(*l)] for l in db.explainedVariances.values()])] weights = {key:[averageWeight]*len(value) for key,value in classes.iteritems()} parallelSimilarityMatrix.averageSimilarityMatrix(classes, weights, "Arm Exercise Clusters - Low Dimensional Distances", savePlot=True)
def armExercisesIndividualSimMatrix(db=None, subjectNumber=2): if db is None: db = armExercisesDatabase.db(True) LRclasses = {key:value for (key,value) in zip(db.LDsegs.keys(),[l[subjectNumber] for l in db.LDsegs.values()])} classes = {} for key,value in LRclasses.iteritems(): for segment in value: classes.setdefault(key[:-1], []).append(segment) # Merge left and right hand motions together averageWeight = [float(sum(t))/len(t) for t in zip(*[[float(sum(t))/len(t) for t in zip(*l)] for l in db.explainedVariances.values()])] weights = {key:[averageWeight]*len(value) for key,value in classes.iteritems()} parallelSimilarityMatrix.averageSimilarityMatrix(classes, weights, "Subject C: PCA data distances", savePlot=True)
def similarityMatrixForLowDimArmExercisesLeftHand(segmentsFromEach=1, database=None): if database is None: data=armExercisesDatabase.db(True) else: data = database segs10l=random.sample(data.segs['10l'][1],segmentsFromEach) segs20l=random.sample(data.segs['20l'][1],segmentsFromEach) segs30l=random.sample(data.segs['30l'][1],segmentsFromEach) segs40l=random.sample(data.segs['40l'][1],segmentsFromEach) segs50l=random.sample(data.segs['50l'][1],segmentsFromEach) segs60l=random.sample(data.segs['60l'][1],segmentsFromEach) segs70l=random.sample(data.segs['70l'][1],segmentsFromEach) segs80l=random.sample(data.segs['80l'][1],segmentsFromEach) segs90l=random.sample(data.segs['90l'][1],segmentsFromEach) names = ["10"]*segmentsFromEach+["20"]*segmentsFromEach+["30"]*segmentsFromEach+["40"]*segmentsFromEach+["50"]*segmentsFromEach+["60"]*segmentsFromEach+["70"]*segmentsFromEach+["80"]*segmentsFromEach+["90"]*segmentsFromEach weights = [float(sum(t))/float(len(t)) for t in zip(data.pca['10l'][1][1],data.pca['20l'][1][1],data.pca['30l'][1][1],data.pca['50l'][1][1],data.pca['60l'][1][1],data.pca['70l'][1][1],data.pca['80l'][1][1],data.pca['90l'][1][1])] print "Calculating similarities..." m.similarityMatrix(segs10l+segs20l+segs30l+segs40l+segs50l+segs60l+segs70l+segs80l+segs90l,names,weights,"Arm exercises, Low Dim",savePlot=True)
import segment import armExercisesDatabase db = armExercisesDatabase.db(computeSegments=False) classes = ['10l','10r','20l','20r','30l','30r','40l','40r','50l','50r','60l','60r','70l','70r','80l','80r','90l','90r'] subjects = ['dan','rob','laura'] db.accelData_pca sIndex = -1 for s in subjects: sIndex += 1 for c in classes: segment.segmentAndPlot(db.accelData_pca[c][sIndex][0][:,0], smoothingWindow=300, save=True, title=s+': '+c)