changeset 878:8e8ec4ece66e

minor + bug corrected in motion pattern learning
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 10 Mar 2017 15:31:26 -0500
parents d1ff6917d082
children f9ea5083588e
files python/ml.py python/tests/utils.txt scripts/learn-motion-patterns.py
diffstat 3 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/python/ml.py	Wed Mar 08 17:46:28 2017 -0500
+++ b/python/ml.py	Fri Mar 10 15:31:26 2017 -0500
@@ -150,6 +150,10 @@
     TODO: at each step, optimize the prototype as the most similar in its current cluster (can be done easily if similarities are already computed)'''
 
     # sort instances based on length
+    if len(instances) == 0:
+        print('no instances to cluster (empty list)')
+        return None
+
     indices = range(len(instances))
     if randomInitialization:
         indices = np.random.permutation(indices)
--- a/python/tests/utils.txt	Wed Mar 08 17:46:28 2017 -0500
+++ b/python/tests/utils.txt	Fri Mar 10 15:31:26 2017 -0500
@@ -10,7 +10,7 @@
 True
 
 >>> computeChi2([],[])
-0.0
+0
 >>> computeChi2(range(1,10),range(1,10))
 0.0
 >>> computeChi2(range(1,9),range(1,10))
--- a/scripts/learn-motion-patterns.py	Wed Mar 08 17:46:28 2017 -0500
+++ b/scripts/learn-motion-patterns.py	Fri Mar 10 15:31:26 2017 -0500
@@ -10,7 +10,7 @@
 parser = argparse.ArgumentParser(description='The program learns prototypes for the motion patterns') #, epilog = ''
 #parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file')
 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True)
-parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures')
+parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeatures', 'feature', 'object'], default = 'objectfeatures')
 parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 3)
 parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None)
 parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True)
@@ -28,8 +28,10 @@
 # TODO add possibility to cluter with velocities
 
 trajectoryType = args.trajectoryType
+prototypeType = args.trajectoryType
 if args.trajectoryType == 'objectfeatures':
     trajectoryType = 'object'
+    prototypeType = 'feature'
 
 #features = storage.loadTrajectoriesFromSqlite(databaseFilename, args.trajectoryType)
 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate)
@@ -37,7 +39,7 @@
 if args.trajectoryType == 'objectfeatures':
     features = []
     for o in objects:
-        o.getNLongestFeatures(args.maxNObjectFeatures)
+        features += o.getNLongestFeatures(args.maxNObjectFeatures)
     objects = features
 
 trajectories = [o.getPositions().asArray().T for o in objects]
@@ -52,7 +54,7 @@
 clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1)
 print(clusterSizes)
 
-storage.savePrototypesToSqlite(args.databaseFilename, [objects[i].getNum() for i in prototypeIndices], args.trajectoryType, [clusterSizes[i] for i in prototypeIndices]) # if saving filenames, add for example [objects[i].dbFilename for i in prototypeIndices]
+storage.savePrototypesToSqlite(args.databaseFilename, [objects[i].getNum() for i in prototypeIndices], prototypeType, [clusterSizes[i] for i in prototypeIndices]) # if saving filenames, add for example [objects[i].dbFilename for i in prototypeIndices]
 
 if args.saveSimilarities:
     np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f')