changeset 988:dc0be55e2bf5

new process functionalities
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 08 Mar 2018 16:44:20 -0500
parents f026ce2af637
children 132d84ce9f0c 94bee7b604eb
files scripts/process.py
diffstat 1 files changed, 32 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/process.py	Wed Mar 07 23:37:00 2018 -0500
+++ b/scripts/process.py	Thu Mar 08 16:44:20 2018 -0500
@@ -6,6 +6,7 @@
 import matplotlib
 matplotlib.use('Agg')
 import matplotlib.pyplot as plt
+from numpy import percentile
 
 import storage, events, prediction
 from metadata import *
@@ -13,6 +14,7 @@
 parser = argparse.ArgumentParser(description='This program manages the processing of several files based on a description of the sites and video data in an SQLite database following the metadata module.')
 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True)
 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int)
+parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int)
 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')
 parser.add_argument('--delete', dest = 'delete', help = 'data to delete', choices = ['feature', 'object', 'classification', 'interaction'])
@@ -65,9 +67,34 @@
     #         processed += job.get()
     #     pool.close()
 
+if args.analyze == 'object': # user speed for now
+    medianSpeeds = {}
+    speeds85 = {}
+    minLength = 2*30
+    for videoId in args.videoIds:
+        vs = session.query(VideoSequence).get(videoId)
+        if not vs.cameraView.siteIdx in medianSpeeds:
+            medianSpeeds[vs.cameraView.siteIdx] = []
+            speeds85[vs.cameraView.siteIdx] = []
+        print('Extracting speed from '+vs.getDatabaseFilename())
+        objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object')
+        for o in objects:
+            if o.length() > minLength:
+                speeds = 30*3.6*percentile(o.getSpeeds(), [50, 85])
+                medianSpeeds[vs.cameraView.siteIdx].append(speeds[0])
+                speeds85[vs.cameraView.siteIdx].append(speeds[1])
+    for speeds, name in zip([medianSpeeds, speeds85], ['Median', '85th Centile']):
+        plt.ioff()
+        plt.figure()
+        plt.boxplot(speeds.values(), labels = [session.query(Site).get(siteId).name for siteId in speeds])
+        plt.ylabel(name+' Speeds (km/h)')
+        plt.savefig(name.lower()+'-speeds.png', dpi=150)
+        plt.close()
+
 if args.analyze == 'interaction':
     indicatorIds = [2,5,7,10]
     conversionFactors = {2: 1., 5: 30.*3.6, 7:1./30, 10:1./30}
+    maxIndicatorValue = {2: float('inf'), 5: float('inf'), 7:10., 10:10.}
     indicators = {}
     interactions = {}
     for videoId in args.videoIds:
@@ -83,13 +110,15 @@
             for i in indicatorIds:
                 indic = inter.getIndicator(events.Interaction.indicatorNames[i])
                 if indic is not None:
-                    indicators[vs.cameraView.siteIdx][i].append(indic.getMostSevereValue()*conversionFactors[i])
+                    v = indic.getMostSevereValue()*conversionFactors[i]
+                    if v < maxIndicatorValue[i]:
+                        indicators[vs.cameraView.siteIdx][i].append(v)
 
     for i in indicatorIds:
         tmp = [indicators[siteId][i] for siteId in indicators]
         plt.ioff()
         plt.figure()
         plt.boxplot(tmp, labels = [session.query(Site).get(siteId).name for siteId in indicators])
-        plt.title(events.Interaction.indicatorNames[i])
-        plt.savefig(events.Interaction.indicatorNames[i]+'.png')
+        plt.ylabel(events.Interaction.indicatorNames[i]+' ('+events.Interaction.indicatorUnits[i]+')')
+        plt.savefig(events.Interaction.indicatorNames[i]+'.png', dpi=150)
         plt.close()