tvaLib
stat.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # tvaLib Copyright (c) 2012-2016 Paul G. St-Aubin
3 # Ecole Polytechnique de Montreal, McGill University
4 # Python 2.7; (dt) Spyder Windows 10 64-bit; ipython Ubuntu 15.04 64-bit
5 
6 
10 import os, sys
11 import math as m
12 from inspect import getfile as inspect_getfile
13 from inspect import currentframe as inspect_currentframe
14 
15 if __name__ == "__main__":
16  print('Statistics library loaded directly.')
17  sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(inspect_getfile(inspect_currentframe())))))
18 import lib.tools as tvaLib
19 
21 try: import numpy as np
22 except Exception: raise Exception, [0101, 'Numpy is not installed.']
23 
24 
25 
26 
29 def echoGeneralStats(objects, site_analyses, camIx, fileIx, commands, indent=4, verbose=1):
30  ''' Echo general statistics '''
31 
32  if(commands.time): duration = commands.time
33  else: duration = site_analyses.site[camIx][fileIx].duration
34 
35  print(''.rjust(indent,' ')+'-------------')
36  print(''.rjust(indent,' ')+'General information:')
37  print(''.rjust(indent,' ')+'{0} at {1} for {2} minutes'.format(site_analyses.site[camIx][fileIx].startTime, site_analyses.site.name, duration))
38  print(''.rjust(indent,' ')+str(site_analyses.site[camIx][fileIx].countObjects)+' objects detected; '+str(site_analyses.site[camIx].getCountObjects())+' total for this camera')
39  print(''.rjust(indent,' ')+'Estimated object rate for this sequence is '+str(round((site_analyses.site[camIx][fileIx].countObjects*3600)/duration,0))+' per hour; '+str(round(site_analyses.getADT(),0))+' ADT (expansion factors); '+str(round(site_analyses.getAADT(),0))+' AADT (expansion factors)')
40  try:
41  print(''.rjust(indent,' ')+'Road user classification:')
42  import moving as TrafIntMoving
43  for i in range(len(TrafIntMoving.userTypeNames)):
44  print(''.rjust(indent+4,' ')+TrafIntMoving.userTypeNames[i]+': '+str([x.getUserType() for x in objects].count(i))+' ('+str(round([x.getUserType() for x in objects].count(i)/float(len(objects))*100.0,1))+'%)')
45  except: tvaLib.printWarning('No road user classification info available.')
46  return True
47 
48 
49 def echoFeatures(objects, indent=4, verbose=1):
50  ''' Echo statistics on number of features '''
51  feature_count = []
52  objects_count = len(objects)
53  for i in range(objects_count):
54  feature_count += [len(objects[i].featureNumbers)]
55 
56  a1 = len([i for i in feature_count if i <= 1])
57  a3 = len([i for i in feature_count if i <= 3])
58  a5 = len([i for i in feature_count if i <= 5])
59  a10 = len([i for i in feature_count if i <= 10])
60  a20 = len([i for i in feature_count if i <= 20])
61  a40 = len([i for i in feature_count if i <= 40])
62  aplus = len(feature_count)
63 
64  print(''.rjust(indent,' ')+'-------------')
65  print(''.rjust(indent,' ')+'Returning object-feature statistics:')
66  print(''.rjust(indent,' ')+'Number of objects: {0}'.format(objects_count))
67  print(''.rjust(indent,' ')+'features <= (1) (3) (5) (10) (20) (40) (Tot)')
68  print(''.rjust(indent,' ')+'============================================================')
69  print(''.rjust(indent,' ')+'observations '+str(a1).zfill(3)+' '+str(a3).zfill(3)+' '+str(a5).zfill(3)+' '+str(a10).zfill(3)+' '+str(a20).zfill(3)+' '+str(a40).zfill(3)+' '+str(aplus).zfill(3))
70  print(''.rjust(indent,' ')+'rate '+str(int(float(a1)/objects_count*100)).zfill(2)+'% '+str(int(float(a3)/objects_count*100)).zfill(2)+'% '+str(int(float(a5)/objects_count*100)).zfill(2)+'% '+str(int(float(a10)/objects_count*100)).zfill(2)+'% '+str(int(float(a20)/objects_count*100)).zfill(2)+'% '+str(int(float(a40)/objects_count*100)).zfill(2)+'% 100%')
71  return True
72 
73 
74 def echoScreenTime(objects, fps=15, indent=4, verbose=1):
75  ''' Echo screen time '''
76  time_count = []
77  objects_count = len(objects)
78  for i in range(objects_count):
79  time_count += [float(objects[i].timeInterval.last-objects[i].timeInterval.first)/fps]
80 
81  a1 = len([i for i in time_count if i <= 0.1])
82  a3 = len([i for i in time_count if i <= 0.2])
83  a5 = len([i for i in time_count if i <= 0.5])
84  a10 = len([i for i in time_count if i <= 1.0])
85  a20 = len([i for i in time_count if i <= 2.0])
86  a40 = len([i for i in time_count if i <= 4.0])
87  aplus = len(time_count)
88 
89  print(''.rjust(indent,' ')+'-------------')
90  print(''.rjust(indent,' ')+'Returning object screen time statistics:')
91  print(''.rjust(indent,' ')+'Number of objects: {0}'.format(objects_count))
92  print(''.rjust(indent,' ')+'time (s) <= (0.1) (0.2) (0.5) (1.0) (2.0) (4.0) (Tot)')
93  print(''.rjust(indent,' ')+'============================================================')
94  print(''.rjust(indent,' ')+'observations '+str(a1).zfill(3)+' '+str(a3).zfill(3)+' '+str(a5).zfill(3)+' '+str(a10).zfill(3)+' '+str(a20).zfill(3)+' '+str(a40).zfill(3)+' '+str(aplus).zfill(3))
95  print(''.rjust(indent,' ')+'rate '+str(int(float(a1)/objects_count*100)).zfill(2)+'% '+str(int(float(a3)/objects_count*100)).zfill(2)+'% '+str(int(float(a5)/objects_count*100)).zfill(2)+'% '+str(int(float(a10)/objects_count*100)).zfill(2)+'% '+str(int(float(a20)/objects_count*100)).zfill(2)+'% '+str(int(float(a40)/objects_count*100)).zfill(2)+'% 100%')
96  return True
97 
98 def echoSpeedDistributionsByType(objects, fps=15, mps_to_kmh=3.6, local=None, indent=4, verbose=1):
99  ''' Dump statistics about speed distribution by type. '''
100  speedsByType = {}
101  for obj in objects:
102  if(obj.userType in speedsByType): speedsByType[obj.userType].append(sum([m.sqrt(x**2+y**2) for x,y in zip(obj.velocities.getXCoordinates(),obj.velocities.getYCoordinates())])/float(len(obj.velocities.getXCoordinates()))*fps*mps_to_kmh)
103  else: speedsByType[obj.userType] = [sum([m.sqrt(x**2+y**2) for x,y in zip(obj.velocities.getXCoordinates(),obj.velocities.getYCoordinates())])/float(len(obj.velocities.getXCoordinates()))*fps*mps_to_kmh]
104 
105  if(local): header = ['User type:']+[local['userTypeNames'][x] for x in speedsByType]
106  else: header = ['User type:']+[str(x) for x in speedsByType]
107 
108  tvaLib.printTable([header,
109  '==',
110  ['Mean speed:']+ [round(np.mean(speedsByType[x]),2) for x in speedsByType],
111  ['Standard deviation speed:']+ [round(np.std(speedsByType[x]),2) for x in speedsByType]], indent=indent, padding=' | ')
112 
113 
114 
115 
116 
117 
def echoGeneralStats(objects, site_analyses, camIx, fileIx, commands, indent=4, verbose=1)
Statistics declaring functions.
Definition: stat.py:29
def echoScreenTime(objects, fps=15, indent=4, verbose=1)
Definition: stat.py:74
def echoSpeedDistributionsByType(objects, fps=15, mps_to_kmh=3.6, local=None, indent=4, verbose=1)
Definition: stat.py:98
def echoFeatures(objects, indent=4, verbose=1)
Definition: stat.py:49