Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/process.py @ 988:dc0be55e2bf5
new process functionalities
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 08 Mar 2018 16:44:20 -0500 |
| parents | f026ce2af637 |
| children | 933670761a57 |
comparison
equal
deleted
inserted
replaced
| 987:f026ce2af637 | 988:dc0be55e2bf5 |
|---|---|
| 4 from pathlib2 import Path | 4 from pathlib2 import Path |
| 5 | 5 |
| 6 import matplotlib | 6 import matplotlib |
| 7 matplotlib.use('Agg') | 7 matplotlib.use('Agg') |
| 8 import matplotlib.pyplot as plt | 8 import matplotlib.pyplot as plt |
| 9 from numpy import percentile | |
| 9 | 10 |
| 10 import storage, events, prediction | 11 import storage, events, prediction |
| 11 from metadata import * | 12 from metadata import * |
| 12 | 13 |
| 13 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.') | 14 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.') |
| 14 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True) | 15 parser.add_argument('--db', dest = 'metadataFilename', help = 'name of the metadata file', required = True) |
| 15 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int) | 16 parser.add_argument('--videos', dest = 'videoIds', help = 'indices of the video sequences', nargs = '*', type = int) |
| 17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects/interactions to process', type = int) | |
| 16 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']) | 18 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']) |
| 17 parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true') | 19 parser.add_argument('--pet', dest = 'computePET', help = 'computes PET', action = 'store_true') |
| 18 parser.add_argument('--delete', dest = 'delete', help = 'data to delete', choices = ['feature', 'object', 'classification', 'interaction']) | 20 parser.add_argument('--delete', dest = 'delete', help = 'data to delete', choices = ['feature', 'object', 'classification', 'interaction']) |
| 19 parser.add_argument('--process', dest = 'process', help = 'data to process', choices = ['feature', 'object', 'classification', 'interaction']) | 21 parser.add_argument('--process', dest = 'process', help = 'data to process', choices = ['feature', 'object', 'classification', 'interaction']) |
| 20 parser.add_argument('--analyze', dest = 'analyze', help = 'data to analyze (results)', choices = ['feature', 'object', 'classification', 'interaction']) | 22 parser.add_argument('--analyze', dest = 'analyze', help = 'data to analyze (results)', choices = ['feature', 'object', 'classification', 'interaction']) |
| 63 # processed = [] | 65 # processed = [] |
| 64 # for job in jobs: | 66 # for job in jobs: |
| 65 # processed += job.get() | 67 # processed += job.get() |
| 66 # pool.close() | 68 # pool.close() |
| 67 | 69 |
| 70 if args.analyze == 'object': # user speed for now | |
| 71 medianSpeeds = {} | |
| 72 speeds85 = {} | |
| 73 minLength = 2*30 | |
| 74 for videoId in args.videoIds: | |
| 75 vs = session.query(VideoSequence).get(videoId) | |
| 76 if not vs.cameraView.siteIdx in medianSpeeds: | |
| 77 medianSpeeds[vs.cameraView.siteIdx] = [] | |
| 78 speeds85[vs.cameraView.siteIdx] = [] | |
| 79 print('Extracting speed from '+vs.getDatabaseFilename()) | |
| 80 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object') | |
| 81 for o in objects: | |
| 82 if o.length() > minLength: | |
| 83 speeds = 30*3.6*percentile(o.getSpeeds(), [50, 85]) | |
| 84 medianSpeeds[vs.cameraView.siteIdx].append(speeds[0]) | |
| 85 speeds85[vs.cameraView.siteIdx].append(speeds[1]) | |
| 86 for speeds, name in zip([medianSpeeds, speeds85], ['Median', '85th Centile']): | |
| 87 plt.ioff() | |
| 88 plt.figure() | |
| 89 plt.boxplot(speeds.values(), labels = [session.query(Site).get(siteId).name for siteId in speeds]) | |
| 90 plt.ylabel(name+' Speeds (km/h)') | |
| 91 plt.savefig(name.lower()+'-speeds.png', dpi=150) | |
| 92 plt.close() | |
| 93 | |
| 68 if args.analyze == 'interaction': | 94 if args.analyze == 'interaction': |
| 69 indicatorIds = [2,5,7,10] | 95 indicatorIds = [2,5,7,10] |
| 70 conversionFactors = {2: 1., 5: 30.*3.6, 7:1./30, 10:1./30} | 96 conversionFactors = {2: 1., 5: 30.*3.6, 7:1./30, 10:1./30} |
| 97 maxIndicatorValue = {2: float('inf'), 5: float('inf'), 7:10., 10:10.} | |
| 71 indicators = {} | 98 indicators = {} |
| 72 interactions = {} | 99 interactions = {} |
| 73 for videoId in args.videoIds: | 100 for videoId in args.videoIds: |
| 74 vs = session.query(VideoSequence).get(videoId) | 101 vs = session.query(VideoSequence).get(videoId) |
| 75 if not vs.cameraView.siteIdx in interactions: | 102 if not vs.cameraView.siteIdx in interactions: |
| 81 print(vs.getDatabaseFilename(), len(interactions[vs.cameraView.siteIdx])) | 108 print(vs.getDatabaseFilename(), len(interactions[vs.cameraView.siteIdx])) |
| 82 for inter in interactions[vs.cameraView.siteIdx]: | 109 for inter in interactions[vs.cameraView.siteIdx]: |
| 83 for i in indicatorIds: | 110 for i in indicatorIds: |
| 84 indic = inter.getIndicator(events.Interaction.indicatorNames[i]) | 111 indic = inter.getIndicator(events.Interaction.indicatorNames[i]) |
| 85 if indic is not None: | 112 if indic is not None: |
| 86 indicators[vs.cameraView.siteIdx][i].append(indic.getMostSevereValue()*conversionFactors[i]) | 113 v = indic.getMostSevereValue()*conversionFactors[i] |
| 114 if v < maxIndicatorValue[i]: | |
| 115 indicators[vs.cameraView.siteIdx][i].append(v) | |
| 87 | 116 |
| 88 for i in indicatorIds: | 117 for i in indicatorIds: |
| 89 tmp = [indicators[siteId][i] for siteId in indicators] | 118 tmp = [indicators[siteId][i] for siteId in indicators] |
| 90 plt.ioff() | 119 plt.ioff() |
| 91 plt.figure() | 120 plt.figure() |
| 92 plt.boxplot(tmp, labels = [session.query(Site).get(siteId).name for siteId in indicators]) | 121 plt.boxplot(tmp, labels = [session.query(Site).get(siteId).name for siteId in indicators]) |
| 93 plt.title(events.Interaction.indicatorNames[i]) | 122 plt.ylabel(events.Interaction.indicatorNames[i]+' ('+events.Interaction.indicatorUnits[i]+')') |
| 94 plt.savefig(events.Interaction.indicatorNames[i]+'.png') | 123 plt.savefig(events.Interaction.indicatorNames[i]+'.png', dpi=150) |
| 95 plt.close() | 124 plt.close() |
