diff scripts/process.py @ 1043:b735895c8815

work in progress on process (learn motion patterns)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 04 Jul 2018 17:39:39 -0400
parents 5621e4ad2428
children 75a6ad604cc5
line wrap: on
line diff
--- a/scripts/process.py	Wed Jul 04 16:21:09 2018 -0400
+++ b/scripts/process.py	Wed Jul 04 17:39:39 2018 -0400
@@ -28,8 +28,34 @@
 # common options
 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file')
 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int)
+parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories', choices = ['objectfeature', 'feature', 'object'], default = 'objectfeature')
 parser.add_argument('--dry', dest = 'dryRun', help = 'dry run of processing', action = 'store_true')
 parser.add_argument('--nthreads', dest = 'nProcesses', help = 'number of processes to run in parallel', type = int, default = 1)
+parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int)
+parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true')
+
+### process options
+# motion pattern learning and assignment
+parser.add_argument('--prototype-filename', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes')
+#parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with')
+parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1)
+parser.add_argument('--maxdist', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True)
+parser.add_argument('--metric', dest = 'metric', help = 'metric for the similarity of trajectory points', default = 'cityblock') # default is manhattan distance
+parser.add_argument('-minsimil', dest = 'minSimilarity', help = 'minimum similarity to put a trajectory in a cluster', type = float, required = True)
+parser.add_argument('-min-cluster-size', dest = 'minClusterSize', help = 'minimum cluster size', type = int, default = 0)
+parser.add_argument('--learn', dest = 'learn', help = 'learn', action = 'store_true')
+parser.add_argument('--optimize', dest = 'optimizeCentroid', help = 'recompute centroid at each assignment', action = 'store_true')
+parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true')
+#parser.add_argument('--similarities-filename', dest = 'similaritiesFilename', help = 'filename of the similarities')
+parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true')
+parser.add_argument('--save-assignments', dest = 'saveAssignments', help = 'saves the assignments of the objects to the prototypes', action = 'store_true')
+parser.add_argument('--assign', dest = 'assign', help = 'assigns the objects to the prototypes and saves the assignments', action = 'store_true')
+
+# safety analysis
+parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (cvd: vector computation (approximate); cve: equation solving; cv: discrete time (approximate)), normal adaptation, point set prediction)', choices = ['cvd', 'cve', 'cv', 'na', 'ps', 'mp'])
+parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true')
+# override other tracking config, erase sqlite?
+
 
 # analysis options
 parser.add_argument('--output', dest = 'output', help = 'kind of output to produce (interval means)', choices = ['figure', 'interval', 'event'])
@@ -40,11 +66,6 @@
 dpi = 150
 # unit of analysis: site or video sequence?
 
-# safety analysis
-parser.add_argument('--prediction-method', dest = 'predictionMethod', help = 'prediction method (constant velocity (cvd: vector computation (approximate); cve: equation solving; cv: discrete time (approximate)), normal adaptation, point set prediction)', choices = ['cvd', 'cve', 'cv', 'na', 'ps', 'mp'])
-parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true')
-# override other tracking config, erase sqlite?
-
 # need way of selecting sites as similar as possible to sql alchemy syntax
 # override tracking.cfg from db
 # manage cfg files, overwrite them (or a subset of parameters)
@@ -61,8 +82,10 @@
 videoSequences = []
 if args.videoIds is not None:
     videoSequences = [session.query(VideoSequence).get(videoId) for videoId in args.videoIds]
+    siteIds = set([vs.cameraView.siteIdx for vs in videoSequences])
 elif args.siteIds is not None:
-    for siteId in args.siteIds:
+    siteIds = set(args.siteIds)
+    for siteId in siteIds:
         for site in getSite(session, siteId):
             for cv in site.cameraViews:
                 videoSequences += cv.videoSequences
@@ -121,7 +144,13 @@
         pool.join()
 
 elif args.process == 'prototype': # motion pattern learning
-    pass
+    # learn by site by default -> group videos by site (or by camera view? TODO add cameraviews)
+    # by default, load all objects, learn and then assign
+    objects = {siteId: [] for siteId in siteIds}
+    for vs in videoSequences:
+        print('Loading '+vs.getDatabaseFilename())
+        objects[vs.cameraView.siteIdx] += storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), args.trajectoryType, args.nTrajectories, timeStep = args.positionSubsamplingRate)
+
 
 elif args.process == 'interaction':
     # safety analysis TODO make function in safety analysis script
@@ -183,10 +212,6 @@
                         row.append(aggSpeeds)
             data.append(row)
     data = DataFrame(data, columns = headers)
-    if args.siteIds is None:
-        siteIds = set([vs.cameraView.siteIdx for vs in videoSequences])
-    else:
-        siteIds = set(args.siteIds)
     if args.output == 'figure':
         for name in headers[4:]:
             plt.ioff()