Mercurial Hosting > traffic-intelligence
comparison scripts/process.py @ 986:3be8aaa47651
work in progress
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Mar 2018 23:54:10 -0500 |
parents | 668a85c963c3 |
children | f026ce2af637 |
comparison
equal
deleted
inserted
replaced
985:668a85c963c3 | 986:3be8aaa47651 |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 | 2 |
3 import sys, argparse | 3 import sys, argparse |
4 from pathlib2 import Path | 4 from pathlib2 import Path |
5 | |
6 import matplotlib | |
7 matplotlib.use('Agg') | |
8 import matplotlib.pyplot as plt | |
5 | 9 |
6 import storage, events, prediction | 10 import storage, events, prediction |
7 from metadata import * | 11 from metadata import * |
8 | 12 |
9 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.') | 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.') |
34 vs = session.query(VideoSequence).get(videoId) | 38 vs = session.query(VideoSequence).get(videoId) |
35 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) | 39 storage.deleteFromSqlite(str(parentDir/vs.getDatabaseFilename()), args.delete) |
36 | 40 |
37 if args.process == 'interaction': | 41 if args.process == 'interaction': |
38 # safety analysis TODO make function in safety analysis script | 42 # safety analysis TODO make function in safety analysis script |
39 predictionParameters = prediction.CVExactPredictionParameters() | 43 predictionParameters = prediction.CVDirectPredictionParameters()#prediction.CVExactPredictionParameters() |
40 for videoId in args.videoIds: | 44 for videoId in args.videoIds: |
41 vs = session.query(VideoSequence).get(videoId) | 45 vs = session.query(VideoSequence).get(videoId) |
42 print('Processing '+vs.getDatabaseFilename()) | 46 print('Processing '+vs.getDatabaseFilename()) |
43 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object')#, args.nObjects, withFeatures = (params.useFeaturesForPrediction or predictionMethod == 'ps' or predictionMethod == 'mp')) | 47 objects = storage.loadTrajectoriesFromSqlite(str(parentDir/vs.getDatabaseFilename()), 'object')#, args.nObjects, withFeatures = (params.useFeaturesForPrediction or predictionMethod == 'ps' or predictionMethod == 'mp')) |
44 interactions = events.createInteractions(objects) | 48 interactions = events.createInteractions(objects) |
57 # processed += job.get() | 61 # processed += job.get() |
58 # pool.close() | 62 # pool.close() |
59 | 63 |
60 if args.analyze == 'interaction': | 64 if args.analyze == 'interaction': |
61 indicatorIds = [2,5,7,10] | 65 indicatorIds = [2,5,7,10] |
66 conversionFactors = {2: 1., 5: 30.*3.6, 7:1./30, 10:1./30} | |
62 indicators = {} | 67 indicators = {} |
63 interactions = {} | 68 interactions = {} |
64 for videoId in args.videoIds: | 69 for videoId in args.videoIds: |
65 vs = session.query(VideoSequence).get(videoId) | 70 vs = session.query(VideoSequence).get(videoId) |
66 if not vs.cameraView.siteIdx in interactions: | 71 if not vs.cameraView.siteIdx in interactions: |
67 interactions[vs.cameraView.siteIdx] = [] | 72 interactions[vs.cameraView.siteIdx] = [] |
73 indicators[vs.cameraView.siteIdx] = {} | |
68 for i in indicatorIds: | 74 for i in indicatorIds: |
69 indicators[events.Interaction.indicatorNames[i]][vs.cameraView.siteIdx] = [] | 75 indicators[vs.cameraView.siteIdx][i] = [] |
70 interactions[vs.cameraView.siteIdx].append(storage.loadInteractionsFromSqlite(str(parentDir/vs.getDatabaseFilename()))) | 76 interactions[vs.cameraView.siteIdx] += storage.loadInteractionsFromSqlite(str(parentDir/vs.getDatabaseFilename())) |
71 print(vs.getDatabaseFilename(), len(interactions)) | 77 print(vs.getDatabaseFilename(), len(interactions[vs.cameraView.siteIdx])) |
72 for inter in interactions[videoId]: | 78 for inter in interactions[vs.cameraView.siteIdx]: |
73 for i in indicatorIds: | 79 for i in indicatorIds: |
74 indic = inter.getIndicator(events.Interaction.indicatorNames[i]) | 80 indic = inter.getIndicator(events.Interaction.indicatorNames[i]) |
75 if indic is not None: | 81 if indic is not None: |
76 indicators[events.Interaction.indicatorNames[i]][vs.cameraView.siteIdx].append(indic.getMostSevereValue()) | 82 indicators[vs.cameraView.siteIdx][i].append(indic.getMostSevereValue()*conversionFactors[i]) |
83 | |
84 for i in indicatorIds: | |
85 tmp = [indicators[siteId][i] for siteId in indicators] | |
86 plt.ioff() | |
87 plt.figure() | |
88 plt.boxplot(tmp, labels = [session.query(Site).get(siteId).name for siteId in indicators]) | |
89 plt.title(events.Interaction.indicatorNames[i]) | |
90 plt.savefig(events.Interaction.indicatorNames[i]+'.png') | |
91 plt.close() |