Mercurial Hosting > traffic-intelligence
annotate python/storage.py @ 740:867bab9f317a dev
function to check the existence of tables
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 27 Aug 2015 23:51:18 -0400 |
parents | 967d244968a4 |
children | ed6ff2ec0aeb |
rev | line source |
---|---|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
2 # -*- coding: utf-8 -*- |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
3 '''Various utilities to save and load data''' |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
4 |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
5 import utils, moving, events, indicators |
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
664
diff
changeset
|
6 from base import VideoFilenameAddable |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
7 |
417
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
8 import sqlite3, logging |
704
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
9 from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
10 |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
11 |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
12 commentChar = '#' |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
13 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
14 delimiterChar = '%'; |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
15 |
50 | 16 ngsimUserTypes = {'twowheels':1, |
204
966c2cd2bd9f
added code to load object trajectories (average of features)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
203
diff
changeset
|
17 'car':2, |
966c2cd2bd9f
added code to load object trajectories (average of features)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
203
diff
changeset
|
18 'truck':3} |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
19 |
259
8ab76b95ee72
added code to save collision points and crossing zones in txt files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
246
diff
changeset
|
20 ######################### |
8ab76b95ee72
added code to save collision points and crossing zones in txt files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
246
diff
changeset
|
21 # Sqlite |
8ab76b95ee72
added code to save collision points and crossing zones in txt files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
246
diff
changeset
|
22 ######################### |
8ab76b95ee72
added code to save collision points and crossing zones in txt files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
246
diff
changeset
|
23 |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
24 # utils |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
25 def printDBError(error): |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
26 print('DB Error: {}'.format(error)) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
27 |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
28 def dropTables(connection, tableNames): |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
29 'deletes the table with names in tableNames' |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
30 try: |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
31 cursor = connection.cursor() |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
32 for tableName in tableNames: |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
33 cursor.execute('DROP TABLE IF EXISTS '+tableName) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
34 except sqlite3.OperationalError as error: |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
35 printDBError(error) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
36 |
740
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
37 def tableExists(filename, tableName): |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
38 'indicates if the table exists in the database' |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
39 try: |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
40 connection = sqlite3.connect(filename) |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
41 cursor = connection.cursor() |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
42 cursor.execute('SELECT COUNT(*) FROM SQLITE_MASTER WHERE type = \'table\' AND name = \''+tableName+'\'') |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
43 return cursor.fetchone()[0] == 1 |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
44 except sqlite3.OperationalError as error: |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
45 printDBError(error) |
867bab9f317a
function to check the existence of tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
736
diff
changeset
|
46 |
714
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
47 def createIndex(connection, tableName, columnName, unique = False): |
711
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
48 '''Creates an index for the column in the table |
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
49 I will make querying with a condition on this column faster''' |
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
50 try: |
714
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
51 #connection = sqlite3.connect(filename) |
711
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
52 cursor = connection.cursor() |
714
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
53 s = "CREATE " |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
54 if unique: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
55 s += "UNIQUE " |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
56 cursor.execute(s+"INDEX IF NOT EXISTS "+tableName+"_"+columnName+"_index ON "+tableName+"("+columnName+")") |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
57 connection.commit() |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
58 #connection.close() |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
59 except sqlite3.OperationalError as error: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
60 printDBError(error) |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
61 |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
62 def getNumberRowsTable(connection, tableName, columnName = None): |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
63 '''Returns the number of rows for the table |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
64 If columnName is not None, means we want the number of distinct values for that column |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
65 (otherwise, we can just count(*))''' |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
66 try: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
67 cursor = connection.cursor() |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
68 if columnName is None: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
69 cursor.execute("SELECT COUNT(*) from "+tableName) |
711
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
70 else: |
714
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
71 cursor.execute("SELECT COUNT(DISTINCT "+columnName+") from "+tableName) |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
72 return cursor.fetchone()[0] |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
73 except sqlite3.OperationalError as error: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
74 printDBError(error) |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
75 |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
76 def getMinMax(connection, tableName, columnName, minmax): |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
77 '''Returns max/min or both for given column in table |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
78 minmax must be string max, min or minmax''' |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
79 try: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
80 cursor = connection.cursor() |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
81 if minmax == 'min' or minmax == 'max': |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
82 cursor.execute("SELECT "+minmax+"("+columnName+") from "+tableName) |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
83 elif minmax == 'minmax': |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
84 cursor.execute("SELECT MIN("+columnName+"), MAX("+columnName+") from "+tableName) |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
85 else: |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
86 print("Argument minmax unknown: {}".format(minmax)) |
d6c69d3d09e5
developing python performance tests for SQLite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
712
diff
changeset
|
87 return cursor.fetchone()[0] |
711
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
88 except sqlite3.OperationalError as error: |
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
89 printDBError(error) |
523eda2fafd4
added function to create index
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
704
diff
changeset
|
90 |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
91 # TODO: add test if database connection is open |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
92 # IO to sqlite |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
93 def writeTrajectoriesToSqlite(objects, outputFilename, trajectoryType, objectNumbers = -1): |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
94 """ |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
95 This function writers trajectories to a specified sqlite file |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
96 @param[in] objects -> a list of trajectories |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
97 @param[in] trajectoryType - |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
98 @param[out] outputFilename -> the .sqlite file containting the written objects |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
99 @param[in] objectNumber : number of objects loaded |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
100 """ |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
101 connection = sqlite3.connect(outputFilename) |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
102 cursor = connection.cursor() |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
103 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
104 schema = "CREATE TABLE IF NOT EXISTS \"positions\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))" |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
105 cursor.execute(schema) |
212
ce44605f888a
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
211
diff
changeset
|
106 |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
107 trajectory_id = 0 |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
108 frame_number = 0 |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
109 if trajectoryType == 'feature': |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
110 if type(objectNumbers) == int and objectNumbers == -1: |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
111 for trajectory in objects: |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
112 trajectory_id += 1 |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
113 frame_number = 0 |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
114 for position in trajectory.getPositions(): |
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
115 frame_number += 1 |
212
ce44605f888a
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
211
diff
changeset
|
116 query = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" |
ce44605f888a
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
211
diff
changeset
|
117 cursor.execute(query,(trajectory_id,frame_number,position.x,position.y)) |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
118 |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
119 connection.commit() |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
120 connection.close() |
619
dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
diff
changeset
|
121 |
208
d9855499fc88
Added functions to write trajectories through sqlite
Francois Belisle <belisle.francois@gmail.com>
parents:
204
diff
changeset
|
122 |
209
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
123 def loadPrototypeMatchIndexesFromSqlite(filename): |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
124 """ |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
125 This function loads the prototypes table in the database of name <filename>. |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
126 It returns a list of tuples representing matching ids : [(prototype_id, matched_trajectory_id),...] |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
127 """ |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
128 matched_indexes = [] |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
129 |
211 | 130 connection = sqlite3.connect(filename) |
209
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
131 cursor = connection.cursor() |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
132 |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
133 try: |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
134 cursor.execute('SELECT * from prototypes order by prototype_id, trajectory_id_matched') |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
135 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
136 printDBError(error) |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
137 return [] |
209
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
138 |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
139 for row in cursor: |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
140 matched_indexes.append((row[0],row[1])) |
211 | 141 |
142 connection.close() | |
209
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
143 return matched_indexes |
746d02cea65f
Added function to read Prototype indexes matches.
Francois Belisle <belisle.francois@gmail.com>
parents:
208
diff
changeset
|
144 |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
145 def getObjectCriteria(objectNumbers): |
585
aab2242ea88c
minor modification of objectNumbers to None
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
584
diff
changeset
|
146 if objectNumbers is None: |
aab2242ea88c
minor modification of objectNumbers to None
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
584
diff
changeset
|
147 query = '' |
aab2242ea88c
minor modification of objectNumbers to None
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
584
diff
changeset
|
148 elif type(objectNumbers) == int: |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
149 query = 'between 0 and {0}'.format(objectNumbers-1) |
263
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
150 elif type(objectNumbers) == list: |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
151 query = 'in ('+', '.join([str(n) for n in objectNumbers])+')' |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
152 else: |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
153 print('objectNumbers {} are not a known type ({})'.format(objectNumbers, type(objectNumbers))) |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
154 query = '' |
263
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
155 return query |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
156 |
585
aab2242ea88c
minor modification of objectNumbers to None
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
584
diff
changeset
|
157 def loadTrajectoriesFromTable(connection, tableName, trajectoryType, objectNumbers = None): |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
158 '''Loads trajectories (in the general sense) from the given table |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
159 can be positions or velocities |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
160 |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
161 returns a moving object''' |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
162 cursor = connection.cursor() |
204
966c2cd2bd9f
added code to load object trajectories (average of features)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
203
diff
changeset
|
163 |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
164 try: |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
165 objectCriteria = getObjectCriteria(objectNumbers) |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
166 if trajectoryType == 'feature': |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
167 queryStatement = 'SELECT * from '+tableName |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
168 if objectNumbers is not None: |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
169 queryStatement += ' where trajectory_id '+objectCriteria |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
170 queryStatement += ' ORDER BY trajectory_id, frame_number' |
417
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
171 cursor.execute(queryStatement) |
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
172 logging.debug(queryStatement) |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
173 elif trajectoryType == 'object': |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
174 queryStatement = 'SELECT OF.object_id, P.frame_number, avg(P.x_coordinate), avg(P.y_coordinate) from '+tableName+' P, objects_features OF where P.trajectory_id = OF.trajectory_id' |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
175 if objectNumbers is not None: |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
176 queryStatement += ' and OF.object_id '+objectCriteria |
712
21aeadcfbabb
added script to test SQLite performance and impact of indices
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
711
diff
changeset
|
177 queryStatement += ' GROUP BY OF.object_id, P.frame_number ORDER BY OF.object_id, P.frame_number' |
587
cf578ba866da
added code to load bounding box corners as trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
586
diff
changeset
|
178 cursor.execute(queryStatement) |
cf578ba866da
added code to load bounding box corners as trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
586
diff
changeset
|
179 logging.debug(queryStatement) |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
180 elif trajectoryType in ['bbtop', 'bbbottom']: |
587
cf578ba866da
added code to load bounding box corners as trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
586
diff
changeset
|
181 if trajectoryType == 'bbtop': |
cf578ba866da
added code to load bounding box corners as trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
586
diff
changeset
|
182 corner = 'top_left' |
cf578ba866da
added code to load bounding box corners as trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
586
diff
changeset
|
183 elif trajectoryType == 'bbbottom': |
cf578ba866da
added code to load bounding box corners as trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
586
diff
changeset
|
184 corner = 'bottom_right' |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
185 queryStatement = 'SELECT object_id, frame_number, x_'+corner+', y_'+corner+' FROM '+tableName |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
186 if objectNumbers is not None: |
712
21aeadcfbabb
added script to test SQLite performance and impact of indices
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
711
diff
changeset
|
187 queryStatement += ' WHERE object_id '+objectCriteria |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
188 queryStatement += ' ORDER BY object_id, frame_number' |
417
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
189 cursor.execute(queryStatement) |
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
190 logging.debug(queryStatement) |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
191 else: |
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
192 print('no trajectory type was chosen') |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
193 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
194 printDBError(error) |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
195 return [] |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
196 |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
197 objId = -1 |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
198 obj = None |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
199 objects = [] |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
200 for row in cursor: |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
201 if row[0] != objId: |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
202 objId = row[0] |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
203 if obj is not None and obj.length() == obj.positions.length(): |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
204 objects.append(obj) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
205 elif obj is not None: |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
206 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length())) |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
207 obj = moving.MovingObject(row[0], timeInterval = moving.TimeInterval(row[1], row[1]), positions = moving.Trajectory([[row[2]],[row[3]]])) |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
208 else: |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
209 obj.timeInterval.last = row[1] |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
210 obj.positions.addPositionXY(row[2],row[3]) |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
211 |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
212 if obj is not None and obj.length() == obj.positions.length(): |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
213 objects.append(obj) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
214 elif obj is not None: |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
215 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length())) |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
216 |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
217 return objects |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
218 |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
219 def loadUserTypesFromTable(cursor, trajectoryType, objectNumbers): |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
220 objectCriteria = getObjectCriteria(objectNumbers) |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
221 queryStatement = 'SELECT object_id, road_user_type from objects' |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
222 if objectNumbers is not None: |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
223 queryStatement += ' where object_id '+objectCriteria |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
224 cursor.execute(queryStatement) |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
225 userTypes = {} |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
226 for row in cursor: |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
227 userTypes[row[0]] = row[1] |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
228 return userTypes |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
229 |
685
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
230 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False): |
585
aab2242ea88c
minor modification of objectNumbers to None
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
584
diff
changeset
|
231 '''Loads the first objectNumbers objects or the indices in objectNumbers from the database''' |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
232 connection = sqlite3.connect(filename) |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
233 |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
234 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers) |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
235 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers) |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
236 |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
237 if len(objectVelocities) > 0: |
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
238 for o,v in zip(objects, objectVelocities): |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
239 if o.getNum() == v.getNum(): |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
240 o.velocities = v.positions |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
241 o.velocities.duplicateLastPosition() # avoid having velocity shorter by one position than positions |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
242 else: |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
243 print('Could not match positions {0} with velocities {1}'.format(o.getNum(), v.getNum())) |
239
93c26e45efd8
modified functions to read velocities from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
244 |
263
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
245 if trajectoryType == 'object': |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
246 cursor = connection.cursor() |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
247 try: |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
248 # attribute feature numbers to objects |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
249 objectCriteria = getObjectCriteria(objectNumbers) |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
250 queryStatement = 'SELECT P.trajectory_id, OF.object_id from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id' |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
251 if objectNumbers is not None: |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
252 queryStatement += ' and OF.object_id '+objectCriteria |
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
253 queryStatement += ' group by P.trajectory_id order by OF.object_id' # order is important to group all features per object |
417
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
254 cursor.execute(queryStatement) |
a2ff03a52b73
added basic logging capability for debugging
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
394
diff
changeset
|
255 logging.debug(queryStatement) |
263
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
256 |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
257 featureNumbers = {} |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
258 for row in cursor: |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
259 objId = row[1] |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
260 if objId not in featureNumbers: |
263
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
261 featureNumbers[objId] = [row[0]] |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
262 else: |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
263 featureNumbers[objId].append(row[0]) |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
264 |
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
265 for obj in objects: |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
266 obj.featureNumbers = featureNumbers[obj.getNum()] |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
267 |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
268 # load userType |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
269 userTypes = loadUserTypesFromTable(cursor, trajectoryType, objectNumbers) |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
270 for obj in objects: |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
271 obj.userType = userTypes[obj.getNum()] |
685
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
272 |
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
273 if withFeatures: |
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
274 nFeatures = 0 |
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
275 for obj in objects: |
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
276 nFeatures = max(nFeatures, max(obj.featureNumbers)) |
702
f0a897d7f3a5
corrected bug when loading features with objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
695
diff
changeset
|
277 features = loadTrajectoriesFromSqlite(filename, 'feature', nFeatures+1) |
685
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
278 for obj in objects: |
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
279 obj.setFeatures(features) |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
280 |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
281 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
282 printDBError(error) |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
283 objects = [] |
263
c71540470057
reorganized loading trajectories and objects, added loading feature numbers for objects (the set of features grouped as one moving object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
259
diff
changeset
|
284 |
143
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
285 connection.close() |
436b87d4b992
wrote the code to load positions from sqlite database
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
78
diff
changeset
|
286 return objects |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
287 |
736
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
288 def savePrototypesToSqlite(filename, prototypes, trajectoryType = 'feature'): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
289 'Work in progress, do not use' |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
290 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
291 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
292 try: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
293 cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, object_id INTEGER, trajectory_id INTEGER, nMatchings INTEGER, FOREIGN KEY(object_id) REFERENCES objects(id), FOREIGN KEY(trajectory_id) REFERENCES positions(trajectory_id))') |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
294 #for inter in interactions: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
295 # saveInteraction(cursor, inter) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
296 except sqlite3.OperationalError as error: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
297 printDBError(error) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
298 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
299 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
300 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
301 def loadPrototypesFromSqlite(filename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
302 pass |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
303 |
589
5800a87f11ae
corrected one bug and changed attribute names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
588
diff
changeset
|
304 def loadGroundTruthFromSqlite(filename, gtType = 'bb', gtNumbers = None): |
588
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
305 'Loads bounding box annotations (ground truth) from an SQLite ' |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
306 connection = sqlite3.connect(filename) |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
307 gt = [] |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
308 |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
309 if gtType == 'bb': |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
310 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', gtNumbers) |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
311 bottomCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbbottom', gtNumbers) |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
312 userTypes = loadUserTypesFromTable(connection.cursor(), 'object', gtNumbers) # string format is same as object |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
313 |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
314 for t, b in zip(topCorners, bottomCorners): |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
315 num = t.getNum() |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
316 if t.getNum() == b.getNum(): |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
317 annotation = moving.BBAnnotation(num, t.getTimeInterval(), t, b, userTypes[num]) |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
318 gt.append(annotation) |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
319 else: |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
320 print ('Unknown type of annotation {}'.format(gtType)) |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
321 |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
322 connection.close() |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
323 return gt |
c5406edbcf12
added loading ground truth annotations (ground truth) from polytrack format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
587
diff
changeset
|
324 |
584
5bda87ac0a69
renames removeFromSqlite to deleteFromSqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
564
diff
changeset
|
325 def deleteFromSqlite(filename, dataType): |
5bda87ac0a69
renames removeFromSqlite to deleteFromSqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
564
diff
changeset
|
326 'Deletes (drops) some tables in the filename depending on type of data' |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
327 import os |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
328 if os.path.isfile(filename): |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
329 connection = sqlite3.connect(filename) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
330 if dataType == 'object': |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
331 dropTables(connection, ['objects', 'objects_features']) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
332 elif dataType == 'interaction': |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
333 dropTables(connection, ['interactions', 'indicators']) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
334 elif dataType == 'bb': |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
335 dropTables(connection, ['bounding_boxes']) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
336 else: |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
337 print('Unknown data type {} to delete from database'.format(dataType)) |
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
338 connection.close() |
344
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
343
diff
changeset
|
339 else: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
340 print('{} does not exist'.format(filename)) |
235
584613399513
added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
218
diff
changeset
|
341 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
342 def createInteractionTable(cursor): |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
343 cursor.execute('CREATE TABLE IF NOT EXISTS interactions (id INTEGER PRIMARY KEY, object_id1 INTEGER, object_id2 INTEGER, first_frame_number INTEGER, last_frame_number INTEGER, FOREIGN KEY(object_id1) REFERENCES objects(id), FOREIGN KEY(object_id2) REFERENCES objects(id))') |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
344 |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
345 def createIndicatorTables(cursor): |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
346 # cursor.execute('CREATE TABLE IF NOT EXISTS indicators (id INTEGER PRIMARY KEY, interaction_id INTEGER, indicator_type INTEGER, FOREIGN KEY(interaction_id) REFERENCES interactions(id))') |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
347 # cursor.execute('CREATE TABLE IF NOT EXISTS indicator_values (indicator_id INTEGER, frame_number INTEGER, value REAL, FOREIGN KEY(indicator_id) REFERENCES indicators(id), PRIMARY KEY(indicator_id, frame_number))') |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
348 cursor.execute('CREATE TABLE IF NOT EXISTS indicators (interaction_id INTEGER, indicator_type INTEGER, frame_number INTEGER, value REAL, FOREIGN KEY(interaction_id) REFERENCES interactions(id), PRIMARY KEY(interaction_id, indicator_type, frame_number))') |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
349 |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
350 def saveInteraction(cursor, interaction): |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
351 roadUserNumbers = list(interaction.getRoadUserNumbers()) |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
352 cursor.execute('INSERT INTO interactions VALUES({}, {}, {}, {}, {})'.format(interaction.getNum(), roadUserNumbers[0], roadUserNumbers[1], interaction.getFirstInstant(), interaction.getLastInstant())) |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
353 |
340
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
354 def saveInteractions(filename, interactions): |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
355 'Saves the interactions in the table' |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
356 connection = sqlite3.connect(filename) |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
357 cursor = connection.cursor() |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
358 try: |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
359 createInteractionTable(cursor) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
360 for inter in interactions: |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
361 saveInteraction(cursor, inter) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
362 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
363 printDBError(error) |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
364 connection.commit() |
340
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
365 connection.close() |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
366 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
367 def saveIndicator(cursor, interactionNum, indicator): |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
368 for instant in indicator.getTimeInterval(): |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
369 if indicator[instant]: |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
370 cursor.execute('INSERT INTO indicators VALUES({}, {}, {}, {})'.format(interactionNum, events.Interaction.indicatorNameToIndices[indicator.getName()], instant, indicator[instant])) |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
371 |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
372 def saveIndicators(filename, interactions, indicatorNames = events.Interaction.indicatorNames): |
340
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
373 'Saves the indicator values in the table' |
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
374 connection = sqlite3.connect(filename) |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
375 cursor = connection.cursor() |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
376 try: |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
377 createInteractionTable(cursor) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
378 createIndicatorTables(cursor) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
379 for inter in interactions: |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
380 saveInteraction(cursor, inter) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
381 for indicatorName in indicatorNames: |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
382 indicator = inter.getIndicator(indicatorName) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
383 if indicator is not None: |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
384 saveIndicator(cursor, inter.getNum(), indicator) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
385 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
386 printDBError(error) |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
387 connection.commit() |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
388 connection.close() |
340
1046b7346886
work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
335
diff
changeset
|
389 |
482
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
417
diff
changeset
|
390 def loadInteractions(filename): |
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
417
diff
changeset
|
391 '''Loads interaction and their indicators |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
392 |
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
393 TODO choose the interactions to load''' |
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
394 interactions = [] |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
395 connection = sqlite3.connect(filename) |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
396 cursor = connection.cursor() |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
397 try: |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
398 cursor.execute('select INT.id, INT.object_id1, INT.object_id2, INT.first_frame_number, INT.last_frame_number, IND.indicator_type, IND.frame_number, IND.value from interactions INT, indicators IND where INT.id = IND.interaction_id ORDER BY INT.id, IND.indicator_type, IND.frame_number') |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
399 interactionNum = -1 |
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
400 indicatorTypeNum = -1 |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
401 tmpIndicators = {} |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
402 for row in cursor: |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
619
diff
changeset
|
403 if row[0] != interactionNum: |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
404 interactionNum = row[0] |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
619
diff
changeset
|
405 interactions.append(events.Interaction(interactionNum, moving.TimeInterval(row[3],row[4]), row[1], row[2])) |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
619
diff
changeset
|
406 interactions[-1].indicators = {} |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
407 if indicatorTypeNum != row[5] or row[0] != interactionNum: |
695
957126bfb456
corrected bug with indicator loading(also now correctly loading mostsevereismax)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
693
diff
changeset
|
408 indicatorTypeNum = row[5] |
628
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
619
diff
changeset
|
409 indicatorName = events.Interaction.indicatorNames[indicatorTypeNum] |
977407c9f815
corrected bugs in loading interactions (index shifted) and added functionalities to play/plot interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
619
diff
changeset
|
410 indicatorValues = {row[6]:row[7]} |
695
957126bfb456
corrected bug with indicator loading(also now correctly loading mostsevereismax)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
693
diff
changeset
|
411 interactions[-1].indicators[indicatorName] = indicators.SeverityIndicator(indicatorName, indicatorValues, mostSevereIsMax = not indicatorName in events.Interaction.timeIndicators) |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
412 else: |
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
413 indicatorValues[row[6]] = row[7] |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
690
diff
changeset
|
414 interactions[-1].indicators[indicatorName].timeInterval.last = row[6] |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
415 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
416 printDBError(error) |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
417 return [] |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
418 connection.close() |
343
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
419 return interactions |
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
420 # load first and last object instants |
74e437ab5f11
first version of indicator loading code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
342
diff
changeset
|
421 # CREATE TEMP TABLE IF NOT EXISTS object_instants AS SELECT OF.object_id, min(frame_number) as first_instant, max(frame_number) as last_instant from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id group by OF.object_id order by OF.object_id |
342
4d69486869a5
work on loading indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
341
diff
changeset
|
422 |
390
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
423 def createBoundingBoxTable(filename, invHomography = None): |
482
f6415f012640
adding functionalities (save images directly to display trajectories to create movies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
417
diff
changeset
|
424 '''Create the table to store the object bounding boxes in image space |
390
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
425 ''' |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
426 connection = sqlite3.connect(filename) |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
427 cursor = connection.cursor() |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
428 try: |
394
6567fee37c16
renamed table for bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
429 cursor.execute('CREATE TABLE IF NOT EXISTS bounding_boxes (object_id INTEGER, frame_number INTEGER, x_top_left REAL, y_top_left REAL, x_bottom_right REAL, y_bottom_right REAL, PRIMARY KEY(object_id, frame_number))') |
6567fee37c16
renamed table for bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
430 cursor.execute('INSERT INTO bounding_boxes SELECT object_id, frame_number, min(x), min(y), max(x), max(y) from ' |
390
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
431 '(SELECT object_id, frame_number, (x*{}+y*{}+{})/w as x, (x*{}+y*{}+{})/w as y from ' |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
432 '(SELECT OF.object_id, P.frame_number, P.x_coordinate as x, P.y_coordinate as y, P.x_coordinate*{}+P.y_coordinate*{}+{} as w from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id)) '.format(invHomography[0,0], invHomography[0,1], invHomography[0,2], invHomography[1,0], invHomography[1,1], invHomography[1,2], invHomography[2,0], invHomography[2,1], invHomography[2,2])+ |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
433 'GROUP BY object_id, frame_number') |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
434 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
435 printDBError(error) |
390
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
436 connection.commit() |
12be4a0cb9aa
sql code to create bounding boxes in image space
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
382
diff
changeset
|
437 connection.close() |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
438 |
586
ff4f0ce46ca6
modified name for loading bounding boxes (only for display)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
585
diff
changeset
|
439 def loadBoundingBoxTableForDisplay(filename): |
647
458890c0437c
cleaned functions to lead bounding boxes (for display)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
646
diff
changeset
|
440 '''Loads bounding boxes from bounding_boxes table for display over trajectories''' |
393
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
441 connection = sqlite3.connect(filename) |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
442 cursor = connection.cursor() |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
443 boundingBoxes = {} # list of bounding boxes for each instant |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
444 try: |
394
6567fee37c16
renamed table for bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
445 cursor.execute('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'bounding_boxes\'') |
393
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
446 result = [row for row in cursor] |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
447 if len(result) > 0: |
394
6567fee37c16
renamed table for bounding boxes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
393
diff
changeset
|
448 cursor.execute('SELECT * FROM bounding_boxes') |
393
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
449 for row in cursor: |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
450 boundingBoxes.setdefault(row[1], []).append([moving.Point(row[2], row[3]), moving.Point(row[4], row[5])]) |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
451 except sqlite3.OperationalError as error: |
491
343cfd185ca6
minor changes and reaarrangements
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
482
diff
changeset
|
452 printDBError(error) |
393
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
453 return boundingBoxes |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
454 connection.close() |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
455 return boundingBoxes |
eaf7765221d9
added code to create bounding boxes and display them (inc scripts)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
390
diff
changeset
|
456 |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
457 ######################### |
736
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
458 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
459 ######################### |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
460 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
461 def writeFeaturesToSqlite(objects, outputFilename, trajectoryType, objectNumbers = -1): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
462 '''write features trajectories maintain trajectory ID,velocities dataset ''' |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
463 connection = sqlite3.connect(outputFilename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
464 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
465 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
466 cursor.execute("CREATE TABLE IF NOT EXISTS \"positions\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
467 cursor.execute("CREATE TABLE IF NOT EXISTS \"velocities\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
468 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
469 if trajectoryType == 'feature': |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
470 if type(objectNumbers) == int and objectNumbers == -1: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
471 for trajectory in objects: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
472 trajectory_id = trajectory.num |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
473 frame_number = trajectory.timeInterval.first |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
474 for position,velocity in zip(trajectory.getPositions(),trajectory.getVelocities()): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
475 cursor.execute("insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)",(trajectory_id,frame_number,position.x,position.y)) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
476 cursor.execute("insert into velocities (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)",(trajectory_id,frame_number,velocity.x,velocity.y)) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
477 frame_number += 1 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
478 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
479 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
480 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
481 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
482 def writePrototypesToSqlite(prototypes,nMatching, outputFilename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
483 """ prototype dataset is a dictionary with keys== routes, values== prototypes Ids """ |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
484 connection = sqlite3.connect(outputFilename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
485 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
486 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
487 cursor.execute("CREATE TABLE IF NOT EXISTS \"prototypes\"(prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))") |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
488 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
489 for route in prototypes.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
490 if prototypes[route]!=[]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
491 for i in prototypes[route]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
492 cursor.execute("insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)",(i,route[0],route[1],nMatching[route][i])) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
493 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
494 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
495 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
496 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
497 def readPrototypesFromSqlite(filename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
498 """ |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
499 This function loads the prototype file in the database |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
500 It returns a dictionary for prototypes for each route and nMatching |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
501 """ |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
502 prototypes = {} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
503 nMatching={} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
504 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
505 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
506 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
507 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
508 try: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
509 cursor.execute('SELECT * from prototypes order by prototype_id, routeIDstart,routeIDend, nMatching') |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
510 except sqlite3.OperationalError as error: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
511 utils.printDBError(error) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
512 return [] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
513 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
514 for row in cursor: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
515 route=(row[1],row[2]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
516 if route not in prototypes.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
517 prototypes[route]=[] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
518 prototypes[route].append(row[0]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
519 nMatching[row[0]]=row[3] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
520 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
521 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
522 return prototypes,nMatching |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
523 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
524 def writeLabelsToSqlite(labels, outputFilename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
525 """ labels is a dictionary with keys: routes, values: prototypes Ids |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
526 """ |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
527 connection = sqlite3.connect(outputFilename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
528 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
529 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
530 cursor.execute("CREATE TABLE IF NOT EXISTS \"labels\"(object_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, prototype_id INTEGER, PRIMARY KEY(object_id))") |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
531 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
532 for route in labels.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
533 if labels[route]!=[]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
534 for i in labels[route]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
535 for j in labels[route][i]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
536 cursor.execute("insert into labels (object_id, routeIDstart,routeIDend, prototype_id) values (?,?,?,?)",(j,route[0],route[1],i)) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
537 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
538 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
539 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
540 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
541 def loadLabelsFromSqlite(filename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
542 labels = {} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
543 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
544 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
545 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
546 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
547 try: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
548 cursor.execute('SELECT * from labels order by object_id, routeIDstart,routeIDend, prototype_id') |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
549 except sqlite3.OperationalError as error: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
550 utils.printDBError(error) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
551 return [] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
552 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
553 for row in cursor: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
554 route=(row[1],row[2]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
555 p=row[3] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
556 if route not in labels.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
557 labels[route]={} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
558 if p not in labels[route].keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
559 labels[route][p]=[] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
560 labels[route][p].append(row[0]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
561 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
562 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
563 return labels |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
564 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
565 def writeSpeedPrototypeToSqlite(prototypes,nmatching, outFilename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
566 """ to match the format of second layer prototypes""" |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
567 connection = sqlite3.connect(outFilename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
568 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
569 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
570 cursor.execute("CREATE TABLE IF NOT EXISTS \"speedprototypes\"(spdprototype_id INTEGER,prototype_id INTEGER,routeID_start INTEGER, routeID_end INTEGER, nMatching INTEGER, PRIMARY KEY(spdprototype_id))") |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
571 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
572 for route in prototypes.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
573 if prototypes[route]!={}: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
574 for i in prototypes[route]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
575 if prototypes[route][i]!= []: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
576 for j in prototypes[route][i]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
577 cursor.execute("insert into speedprototypes (spdprototype_id,prototype_id, routeID_start, routeID_end, nMatching) values (?,?,?,?,?)",(j,i,route[0],route[1],nmatching[j])) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
578 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
579 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
580 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
581 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
582 def loadSpeedPrototypeFromSqlite(filename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
583 """ |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
584 This function loads the prototypes table in the database of name <filename>. |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
585 """ |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
586 prototypes = {} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
587 nMatching={} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
588 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
589 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
590 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
591 try: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
592 cursor.execute('SELECT * from speedprototypes order by spdprototype_id,prototype_id, routeID_start, routeID_end, nMatching') |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
593 except sqlite3.OperationalError as error: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
594 utils.printDBError(error) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
595 return [] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
596 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
597 for row in cursor: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
598 route=(row[2],row[3]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
599 if route not in prototypes.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
600 prototypes[route]={} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
601 if row[1] not in prototypes[route].keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
602 prototypes[route][row[1]]=[] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
603 prototypes[route][row[1]].append(row[0]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
604 nMatching[row[0]]=row[4] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
605 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
606 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
607 return prototypes,nMatching |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
608 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
609 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
610 def writeRoutesToSqlite(Routes, outputFilename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
611 """ This function writes the activity path define by start and end IDs""" |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
612 connection = sqlite3.connect(outputFilename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
613 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
614 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
615 cursor.execute("CREATE TABLE IF NOT EXISTS \"routes\"(object_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, PRIMARY KEY(object_id))") |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
616 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
617 for route in Routes.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
618 if Routes[route]!=[]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
619 for i in Routes[route]: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
620 cursor.execute("insert into routes (object_id, routeIDstart,routeIDend) values (?,?,?)",(i,route[0],route[1])) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
621 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
622 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
623 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
624 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
625 def loadRoutesFromSqlite(filename): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
626 Routes = {} |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
627 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
628 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
629 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
630 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
631 try: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
632 cursor.execute('SELECT * from routes order by object_id, routeIDstart,routeIDend') |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
633 except sqlite3.OperationalError as error: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
634 utils.printDBError(error) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
635 return [] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
636 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
637 for row in cursor: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
638 route=(row[1],row[2]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
639 if route not in Routes.keys(): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
640 Routes[route]=[] |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
641 Routes[route].append(row[0]) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
642 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
643 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
644 return Routes |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
645 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
646 def setRoutes(filename, objects): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
647 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
648 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
649 for obj in objects: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
650 cursor.execute('update objects set startRouteID = {} where object_id = {}'.format(obj.startRouteID, obj.getNum())) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
651 cursor.execute('update objects set endRouteID = {} where object_id = {}'.format(obj.endRouteID, obj.getNum())) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
652 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
653 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
654 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
655 def setRoadUserTypes(filename, objects): |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
656 '''Saves the user types of the objects in the sqlite database stored in filename |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
657 The objects should exist in the objects table''' |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
658 connection = sqlite3.connect(filename) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
659 cursor = connection.cursor() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
660 for obj in objects: |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
661 cursor.execute('update objects set road_user_type = {} where object_id = {}'.format(obj.getUserType(), obj.getNum())) |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
662 connection.commit() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
663 connection.close() |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
664 |
967d244968a4
work in progress on saving/loading prototypes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
722
diff
changeset
|
665 ######################### |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
666 # txt files |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
667 ######################### |
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
668 |
525
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
669 def openCheck(filename, option = 'r', quitting = False): |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
670 '''Open file filename in read mode by default |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
671 and checks it is open''' |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
672 try: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
673 return open(filename, option) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
674 except IOError: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
675 print 'File %s could not be opened.' % filename |
525
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
676 if quitting: |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
677 from sys import exit |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
678 exit() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
679 return None |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
680 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
681 def readline(f, commentCharacters = commentChar): |
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
682 '''Modified readline function to skip comments |
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
683 Can take a list of characters or a string (in will work in both)''' |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
684 s = f.readline() |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
685 while (len(s) > 0) and s[0] in commentCharacters: |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
686 s = f.readline() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
687 return s.strip() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
688 |
722
49e99ca34a7d
corrected bugs in old ubc code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
718
diff
changeset
|
689 def getLines(f, delimiterChar = delimiterChar, commentCharacters = commentChar): |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
690 '''Gets a complete entry (all the lines) in between delimiterChar.''' |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
691 dataStrings = [] |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
692 s = readline(f, commentCharacters) |
722
49e99ca34a7d
corrected bugs in old ubc code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
718
diff
changeset
|
693 while len(s) > 0 and s[0] != delimiterChar: |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
694 dataStrings += [s.strip()] |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
695 s = readline(f, commentCharacters) |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
696 return dataStrings |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
697 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
698 def writeList(filename, l): |
514
1ba618fb0f70
corrected bug from merging and argument issue in display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
699 f = openCheck(filename, 'w') |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
700 for x in l: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
701 f.write('{}\n'.format(x)) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
702 f.close() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
703 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
704 def loadListStrings(filename, commentCharacters = commentChar): |
514
1ba618fb0f70
corrected bug from merging and argument issue in display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
705 f = openCheck(filename, 'r') |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
706 result = getLines(f, commentCharacters) |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
707 f.close() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
708 return result |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
709 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
710 def getValuesFromINIFile(filename, option, delimiterChar = '=', commentCharacters = commentChar): |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
711 values = [] |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
712 for l in loadListStrings(filename, commentCharacters): |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
713 if l.startswith(option): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
714 values.append(l.split(delimiterChar)[1].strip()) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
715 return values |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
716 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
717 class FakeSecHead(object): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
718 '''Add fake section header [asection] |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
719 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
720 from http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
721 use read_file in Python 3.2+ |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
722 ''' |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
723 def __init__(self, fp): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
724 self.fp = fp |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
725 self.sechead = '[main]\n' |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
726 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
727 def readline(self): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
728 if self.sechead: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
729 try: return self.sechead |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
730 finally: self.sechead = None |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
731 else: return self.fp.readline() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
732 |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
733 def generatePDLaneColumn(data): |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
734 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str) |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
735 |
718
2cd245cb780d
added option to set low_memory = False for pandas.read_csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
714
diff
changeset
|
736 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, warmUpLastInstant = None, usePandas = False, nDecimals = 2, lowMemory = True): |
525
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
737 '''Reads data from VISSIM .fzp trajectory file |
527
37830a831818
loading from VISSIM trajectory data works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
526
diff
changeset
|
738 simulationStepsPerTimeUnit is the number of simulation steps per unit of time used by VISSIM |
37830a831818
loading from VISSIM trajectory data works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
526
diff
changeset
|
739 for example, there seems to be 5 simulation steps per simulated second in VISSIM, |
37830a831818
loading from VISSIM trajectory data works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
526
diff
changeset
|
740 so simulationStepsPerTimeUnit should be 5, |
37830a831818
loading from VISSIM trajectory data works
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
526
diff
changeset
|
741 so that all times correspond to the number of the simulation step (and can be stored as integers) |
660
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
742 |
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
743 Objects positions will be considered only after warmUpLastInstant |
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
744 (if the object has no such position, it won't be loaded) |
525
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
745 |
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
746 Assumed to be sorted over time''' |
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
747 objects = {} # dictionary of objects index by their id |
7124c7d2a663
first draft of loading from VISSIM file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
524
diff
changeset
|
748 |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
749 if usePandas: |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
750 from pandas import read_csv |
718
2cd245cb780d
added option to set low_memory = False for pandas.read_csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
714
diff
changeset
|
751 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, low_memory = lowMemory) |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
752 generatePDLaneColumn(data) |
642
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
753 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit |
660
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
754 if warmUpLastInstant is not None: |
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
755 data = data[data['TIME']>=warmUpLastInstant] |
642
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
756 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) |
704
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
757 instants = grouped['TIME'].agg({'first': npmin, 'last': npmax}) |
642
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
758 for row_index, row in instants.iterrows(): |
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
759 objNum = int(row['NO']) |
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
760 tmp = data[data['NO'] == objNum] |
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
761 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last'])) |
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
762 # positions should be rounded to nDecimals decimals only |
704
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
763 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = npround(tmp['POS'].tolist(), nDecimals), Y = npround(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist()) |
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
764 if nObjects > 0 and len(objects) >= nObjects: |
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
765 break |
642
932f96c89212
added pandas to read vissim fzp (more robust with respect to column names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
636
diff
changeset
|
766 return objects.values() |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
767 else: |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
768 inputfile = openCheck(filename, quitting = True) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
769 # data = pd.read_csv(filename, skiprows=15, delimiter=';') |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
770 # skip header: 15 lines + 1 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
771 line = readline(inputfile, '*$') |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
772 while len(line) > 0:#for line in inputfile: |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
773 data = line.strip().split(';') |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
774 objNum = int(data[1]) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
775 instant = int(float(data[0])*simulationStepsPerTimeUnit) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
776 s = float(data[4]) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
777 y = float(data[5]) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
778 lane = data[2]+'_'+data[3] |
660
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
779 if objNum not in objects: |
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
780 if warmUpLastInstant is None or instant >= warmUpLastInstant: |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
781 if nObjects < 0 or len(objects) < nObjects: |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
782 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant)) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
783 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory() |
660
994dd644f6ab
corrected impact of warmUpLastInstant
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
649
diff
changeset
|
784 if (warmUpLastInstant is None or instant >= warmUpLastInstant) and objNum in objects: |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
785 objects[objNum].timeInterval.last = instant |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
786 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) |
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
787 line = readline(inputfile, '*$') |
524
1dced8932b08
corrected bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
514
diff
changeset
|
788 |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
789 return objects.values() |
645
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
790 |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
791 def selectPDLanes(data, lanes = None): |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
792 '''Selects the subset of data for the right lanes |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
793 |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
794 Lane format is a string 'x_y' where x is link index and y is lane index''' |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
795 if lanes is not None: |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
796 if 'LANE' not in data.columns: |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
797 generatePDLaneColumn(data) |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
798 indices = (data['LANE'] == lanes[0]) |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
799 for l in lanes[1:]: |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
800 indices = indices | (data['LANE'] == l) |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
801 return data[indices] |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
802 else: |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
803 return data |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
804 |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
805 def countStoppedVehiclesVissim(filename, lanes = None, proportionStationaryTime = 0.7): |
646
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
806 '''Counts the number of vehicles stopped for a long time in a VISSIM trajectory file |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
807 and the total number of vehicles |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
808 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
809 Vehicles are considered finally stationary |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
810 if more than proportionStationaryTime of their total time |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
811 If lanes is not None, only the data for the selected lanes will be provided |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
812 (format as string x_y where x is link index and y is lane index)''' |
646
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
813 from pandas import read_csv |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
814 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
815 if lanes is not None: |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
816 columns += ['LANE\LINK\NO', 'LANE\INDEX'] |
718
2cd245cb780d
added option to set low_memory = False for pandas.read_csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
714
diff
changeset
|
817 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns, low_memory = lowMemory) |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
818 data = selectPDLanes(data, lanes) |
646
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
819 data.sort(['$VEHICLE:SIMSEC'], inplace = True) |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
820 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
821 nStationary = 0 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
822 nVehicles = 0 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
823 for name, group in data.groupby(['NO'], sort = False): |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
824 nVehicles += 1 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
825 positions = array(group['POS']) |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
826 diff = positions[1:]-positions[:-1] |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
827 if npsum(diff == 0.) >= proportionStationaryTime*len(positions): |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
828 nStationary += 1 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
829 |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
830 return nStationary, nVehicles |
6680a69d5bf3
added fast function to detect VISSIM simulations with errors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
645
diff
changeset
|
831 |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
832 def countCollisionsVissim(filename, lanes = None, collisionTimeDifference = 0.2): |
645
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
833 '''Counts the number of collisions per lane in a VISSIM trajectory file |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
834 |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
835 To distinguish between cars passing and collision, |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
836 one checks when the sign of the position difference inverts |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
837 (if the time are closer than collisionTimeDifference) |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
838 If lanes is not None, only the data for the selected lanes will be provided |
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
839 (format as string x_y where x is link index and y is lane index)''' |
645
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
840 from pandas import read_csv, merge |
718
2cd245cb780d
added option to set low_memory = False for pandas.read_csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
714
diff
changeset
|
841 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS'], low_memory = lowMemory) |
649
df9ddeaee4a6
added ability to select lanes to count collisions or stationary vehicles in VISSIM files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
647
diff
changeset
|
842 data = selectPDLanes(data, lanes) |
645
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
843 merged = merge(data, data, how='inner', left_on=['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC'], right_on=['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC'], sort = False) |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
844 merged = merged[merged['NO_x']>merged['NO_y']] |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
845 |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
846 nCollisions = 0 |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
847 for name, group in merged.groupby(['LANE\LINK\NO', 'LANE\INDEX', 'NO_x', 'NO_y']): |
704
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
848 diff = group['POS_x'].convert_objects(convert_numeric=True)-group['POS_y'].convert_objects(convert_numeric=True) |
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
849 # diff = group['POS_x']-group['POS_y'] # to check the impact of convert_objects and the possibility of using type conversion in read_csv or function to convert strings if any |
f83d125d0c55
cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
702
diff
changeset
|
850 if len(diff) >= 2 and npmin(diff) < 0 and npmax(diff) > 0: |
645
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
851 xidx = diff[diff < 0].argmax() |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
852 yidx = diff[diff > 0].argmin() |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
853 if abs(group.loc[xidx, '$VEHICLE:SIMSEC'] - group.loc[yidx, '$VEHICLE:SIMSEC']) <= collisionTimeDifference: |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
854 nCollisions += 1 |
5ed2118c959d
added function to count collisions in vissim
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
643
diff
changeset
|
855 return nCollisions |
524
1dced8932b08
corrected bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
514
diff
changeset
|
856 |
173
319a04ba9033
function name change
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
143
diff
changeset
|
857 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1): |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
858 '''Reads data from the trajectory data provided by NGSIM project |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
859 and returns the list of Feature objects''' |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
860 objects = [] |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
861 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
862 inputfile = openCheck(filename, quitting = True) |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
863 |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
864 def createObject(numbers): |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
865 firstFrameNum = int(numbers[1]) |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
866 # do the geometry and usertype |
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
867 |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
868 firstFrameNum = int(numbers[1]) |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
869 lastFrameNum = firstFrameNum+int(numbers[2])-1 |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
870 #time = moving.TimeInterval(firstFrameNum, firstFrameNum+int(numbers[2])-1) |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
871 obj = moving.MovingObject(num = int(numbers[0]), |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
872 timeInterval = moving.TimeInterval(firstFrameNum, lastFrameNum), |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
873 positions = moving.Trajectory([[float(numbers[6])],[float(numbers[7])]]), |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
874 userType = int(numbers[10])) |
78
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
875 obj.userType = int(numbers[10]) |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
876 obj.laneNums = [int(numbers[13])] |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
877 obj.precedingVehicles = [int(numbers[14])] # lead vehicle (before) |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
878 obj.followingVehicles = [int(numbers[15])] # following vehicle (after) |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
879 obj.spaceHeadways = [float(numbers[16])] # feet |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
880 obj.timeHeadways = [float(numbers[17])] # seconds |
327
42f2b46ec210
added class for trajectories in curvilinear coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
326
diff
changeset
|
881 obj.curvilinearPositions = moving.CurvilinearTrajectory([float(numbers[5])],[float(numbers[4])], obj.laneNums) # X is the longitudinal coordinate |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
882 obj.speeds = [float(numbers[11])] |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
883 obj.size = [float(numbers[8]), float(numbers[9])] # 8 lengh, 9 width # TODO: temporary, should use a geometry object |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
884 return obj |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
885 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
886 numbers = readline(inputfile).strip().split() |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
887 if (len(numbers) > 0): |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
888 obj = createObject(numbers) |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
889 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
890 for line in inputfile: |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
891 numbers = line.strip().split() |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
892 if obj.getNum() != int(numbers[0]): |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
893 # check and adapt the length to deal with issues in NGSIM data |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
894 if (obj.length() != obj.positions.length()): |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
895 print 'length pb with object %s (%d,%d)' % (obj.getNum(),obj.length(),obj.positions.length()) |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
896 obj.last = obj.getFirstInstant()+obj.positions.length()-1 |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
897 #obj.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ? |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
898 objects.append(obj) |
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
899 if (nObjects>0) and (len(objects)>=nObjects): |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
900 break |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
901 obj = createObject(numbers) |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
902 else: |
327
42f2b46ec210
added class for trajectories in curvilinear coordinates
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
326
diff
changeset
|
903 obj.laneNums.append(int(numbers[13])) |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
904 obj.positions.addPositionXY(float(numbers[6]), float(numbers[7])) |
542
a3add9f751ef
added differentiate function for curvilinear trajectories and modified the addPosition functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
541
diff
changeset
|
905 obj.curvilinearPositions.addPositionSYL(float(numbers[5]), float(numbers[4]), obj.laneNums[-1]) |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
906 obj.speeds.append(float(numbers[11])) |
78
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
907 obj.precedingVehicles.append(int(numbers[14])) |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
908 obj.followingVehicles.append(int(numbers[15])) |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
909 obj.spaceHeadways.append(float(numbers[16])) |
99e807c29753
added loading other information from NGSIM
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
72
diff
changeset
|
910 obj.timeHeadways.append(float(numbers[17])) |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
911 |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
912 if (obj.size[0] != float(numbers[8])): |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
913 print 'changed length obj %d' % (obj.getNum()) |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
914 if (obj.size[1] != float(numbers[9])): |
329
a70c205ebdd9
added sqlite code, in particular to load and save road user type
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
327
diff
changeset
|
915 print 'changed width obj %d' % (obj.getNum()) |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
916 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
917 inputfile.close() |
7
ffddccfab7f9
loading shell objects from NGSIM works
Nicolas Saunier <nico@confins.net>
parents:
0
diff
changeset
|
918 return objects |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
919 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
920 def convertNgsimFile(inputfile, outputfile, append = False, nObjects = -1, sequenceNum = 0): |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
921 '''Reads data from the trajectory data provided by NGSIM project |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
922 and converts to our current format.''' |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
923 if append: |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
924 out = openCheck(outputfile,'a') |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
925 else: |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
926 out = openCheck(outputfile,'w') |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
927 nObjectsPerType = [0,0,0] |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
928 |
564
36605d843be5
modified bug for reading vissim files, cleaned use of readline with multiple type of characters for comments (to ignore)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
552
diff
changeset
|
929 features = loadNgsimFile(inputfile, sequenceNum) |
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
930 for f in features: |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
931 nObjectsPerType[f.userType-1] += 1 |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
932 f.write(out) |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
933 |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
934 print nObjectsPerType |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
935 |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
936 out.close() |
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
937 |
335
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
938 def writePositionsToCsv(f, obj): |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
939 timeInterval = obj.getTimeInterval() |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
940 positions = obj.getPositions() |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
941 curvilinearPositions = obj.getCurvilinearPositions() |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
942 for i in xrange(int(obj.length())): |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
943 p1 = positions[i] |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
944 s = '{},{},{},{}'.format(obj.num,timeInterval[i],p1.x,p1.y) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
945 if curvilinearPositions is not None: |
335
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
946 p2 = curvilinearPositions[i] |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
947 s += ',{},{}'.format(p2[0],p2[1]) |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
948 f.write(s+'\n') |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
949 |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
950 def writeTrajectoriesToCsv(filename, objects): |
514
1ba618fb0f70
corrected bug from merging and argument issue in display-trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
509
diff
changeset
|
951 f = openCheck(filename, 'w') |
335
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
952 for i,obj in enumerate(objects): |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
953 writePositionsToCsv(f, obj) |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
954 f.close() |
3950bfe22768
added functions to export trajectories to csv
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
330
diff
changeset
|
955 |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
956 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
957 ######################### |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
958 # Utils to read .ini type text files for configuration, meta data... |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
959 ######################### |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
960 |
664
455f9b93819c
added capability to set a videofilename to movingobject and interaction, renames interactiontype to collision in interaction
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
660
diff
changeset
|
961 class ProcessParameters(VideoFilenameAddable): |
537
6c264b914846
work on classification parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
536
diff
changeset
|
962 '''Class for all parameters controlling data processing: input, |
6c264b914846
work on classification parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
536
diff
changeset
|
963 method parameters, etc. for tracking, classification and safety |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
964 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
965 Note: framerate is already taken into account''' |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
966 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
967 def loadConfigFile(self, filename): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
968 from ConfigParser import ConfigParser |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
969 from os import path |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
970 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
971 config = ConfigParser() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
972 config.readfp(FakeSecHead(openCheck(filename))) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
973 self.sectionHeader = config.sections()[0] |
537
6c264b914846
work on classification parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
536
diff
changeset
|
974 # Tracking/display parameters |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
975 self.videoFilename = config.get(self.sectionHeader, 'video-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
976 self.databaseFilename = config.get(self.sectionHeader, 'database-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
977 self.homographyFilename = config.get(self.sectionHeader, 'homography-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
978 if (path.exists(self.homographyFilename)): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
979 self.homography = loadtxt(self.homographyFilename) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
980 else: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
981 self.homography = None |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
982 self.intrinsicCameraFilename = config.get(self.sectionHeader, 'intrinsic-camera-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
983 if (path.exists(self.intrinsicCameraFilename)): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
984 self.intrinsicCameraMatrix = loadtxt(self.intrinsicCameraFilename) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
985 else: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
986 self.intrinsicCameraMatrix = None |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
987 distortionCoefficients = getValuesFromINIFile(filename, 'distortion-coefficients', '=') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
988 self.distortionCoefficients = [float(x) for x in distortionCoefficients] |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
989 self.undistortedImageMultiplication = config.getfloat(self.sectionHeader, 'undistorted-size-multiplication') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
990 self.undistort = config.getboolean(self.sectionHeader, 'undistort') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
991 self.firstFrameNum = config.getint(self.sectionHeader, 'frame1') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
992 self.videoFrameRate = config.getfloat(self.sectionHeader, 'video-fps') |
377 | 993 |
537
6c264b914846
work on classification parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
536
diff
changeset
|
994 # Classification parameters |
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
995 self.speedAggregationMethod = config.get(self.sectionHeader, 'speed-aggregation-method') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
996 self.nFramesIgnoreAtEnds = config.getint(self.sectionHeader, 'nframes-ignore-at-ends') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
997 self.speedAggregationQuantile = config.getint(self.sectionHeader, 'speed-aggregation-quantile') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
998 self.minSpeedEquiprobable = config.getfloat(self.sectionHeader, 'min-speed-equiprobable') |
685
94b291a5f933
several updates for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
680
diff
changeset
|
999 self.minNPixels = config.getint(self.sectionHeader, 'min-npixels-crop') |
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1000 self.pedBikeCarSVMFilename = config.get(self.sectionHeader, 'pbv-svm-filename') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1001 self.bikeCarSVMFilename = config.get(self.sectionHeader, 'bv-svm-filename') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1002 self.maxPedestrianSpeed = config.getfloat(self.sectionHeader, 'max-ped-speed') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1003 self.maxCyclistSpeed = config.getfloat(self.sectionHeader, 'max-cyc-speed') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1004 self.meanPedestrianSpeed = config.getfloat(self.sectionHeader, 'mean-ped-speed') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1005 self.stdPedestrianSpeed = config.getfloat(self.sectionHeader, 'std-ped-speed') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1006 self.locationCyclistSpeed = config.getfloat(self.sectionHeader, 'cyc-speed-loc') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1007 self.scaleCyclistSpeed = config.getfloat(self.sectionHeader, 'cyc-speed-scale') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1008 self.meanVehicleSpeed = config.getfloat(self.sectionHeader, 'mean-veh-speed') |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1009 self.stdVehicleSpeed = config.getfloat(self.sectionHeader, 'std-veh-speed') |
537
6c264b914846
work on classification parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
536
diff
changeset
|
1010 |
6c264b914846
work on classification parameters
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
536
diff
changeset
|
1011 # Safety parameters |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1012 self.maxPredictedSpeed = config.getfloat(self.sectionHeader, 'max-predicted-speed')/3.6/self.videoFrameRate |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1013 self.predictionTimeHorizon = config.getfloat(self.sectionHeader, 'prediction-time-horizon')*self.videoFrameRate |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1014 self.collisionDistance = config.getfloat(self.sectionHeader, 'collision-distance') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1015 self.crossingZones = config.getboolean(self.sectionHeader, 'crossing-zones') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1016 self.predictionMethod = config.get(self.sectionHeader, 'prediction-method') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1017 self.nPredictedTrajectories = config.getint(self.sectionHeader, 'npredicted-trajectories') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1018 self.maxNormalAcceleration = config.getfloat(self.sectionHeader, 'max-normal-acceleration')/self.videoFrameRate**2 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1019 self.maxNormalSteering = config.getfloat(self.sectionHeader, 'max-normal-steering')/self.videoFrameRate |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1020 self.minExtremeAcceleration = config.getfloat(self.sectionHeader, 'min-extreme-acceleration')/self.videoFrameRate**2 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1021 self.maxExtremeAcceleration = config.getfloat(self.sectionHeader, 'max-extreme-acceleration')/self.videoFrameRate**2 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1022 self.maxExtremeSteering = config.getfloat(self.sectionHeader, 'max-extreme-steering')/self.videoFrameRate |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1023 self.useFeaturesForPrediction = config.getboolean(self.sectionHeader, 'use-features-prediction') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1024 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1025 def __init__(self, filename = None): |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
628
diff
changeset
|
1026 if filename is not None: |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1027 self.loadConfigFile(filename) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1028 |
680
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1029 def convertToFrames(self, speedRatio = 3.6): |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1030 '''Converts parameters with a relationship to time in 'native' frame time |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1031 speedRatio is the conversion from the speed unit in the config file |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1032 to the distance per second |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1033 |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1034 ie param(config file) = speedRatio x fps x param(used in program) |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1035 eg km/h = 3.6 (m/s to km/h) x frame/s x m/frame''' |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1036 denominator = self.videoFrameRate*speedRatio |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1037 denominator2 = denominator**2 |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1038 self.minSpeedEquiprobable = self.minSpeedEquiprobable/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1039 self.maxPedestrianSpeed = self.maxPedestrianSpeed/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1040 self.maxCyclistSpeed = self.maxCyclistSpeed/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1041 self.meanPedestrianSpeed = self.meanPedestrianSpeed/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1042 self.stdPedestrianSpeed = self.stdPedestrianSpeed/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1043 self.meanVehicleSpeed = self.meanVehicleSpeed/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1044 self.stdVehicleSpeed = self.stdVehicleSpeed/denominator |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1045 # special case for the lognormal distribution |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1046 self.locationCyclistSpeed = self.locationCyclistSpeed-log(denominator) |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1047 #self.scaleCyclistSpeed = self.scaleCyclistSpeed |
da1352b89d02
classification is working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
1048 |
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
664
diff
changeset
|
1049 class SceneParameters(object): |
509
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1050 def __init__(self, config, sectionName): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1051 from ConfigParser import NoOptionError |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1052 from ast import literal_eval |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1053 try: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1054 self.sitename = config.get(sectionName, 'sitename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1055 self.databaseFilename = config.get(sectionName, 'data-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1056 self.homographyFilename = config.get(sectionName, 'homography-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1057 self.calibrationFilename = config.get(sectionName, 'calibration-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1058 self.videoFilename = config.get(sectionName, 'video-filename') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1059 self.frameRate = config.getfloat(sectionName, 'framerate') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1060 self.date = datetime.strptime(config.get(sectionName, 'date'), datetimeFormat) # 2011-06-22 11:00:39 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1061 self.translation = literal_eval(config.get(sectionName, 'translation')) # = [0.0, 0.0] |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1062 self.rotation = config.getfloat(sectionName, 'rotation') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1063 self.duration = config.getint(sectionName, 'duration') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1064 except NoOptionError as e: |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1065 print(e) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1066 print('Not a section for scene meta-data') |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1067 |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1068 @staticmethod |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1069 def loadConfigFile(filename): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1070 from ConfigParser import ConfigParser |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1071 config = ConfigParser() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1072 config.readfp(openCheck(filename)) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1073 configDict = dict() |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1074 for sectionName in config.sections(): |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1075 configDict[sectionName] = SceneParameters(config, sectionName) |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1076 return configDict |
935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
491
diff
changeset
|
1077 |
382
ba813f148ade
development for clustering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
377
diff
changeset
|
1078 |
215
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
1079 if __name__ == "__main__": |
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
1080 import doctest |
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
1081 import unittest |
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
1082 suite = doctest.DocFileSuite('tests/storage.txt') |
5e2983b05d4e
created first doctest tests for storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
212
diff
changeset
|
1083 unittest.TextTestRunner().run(suite) |
72
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
1084 # #doctest.testmod() |
575340e6fce3
corrected most of the method to load NGSIM data (adapted to the current MovingObject class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
50
diff
changeset
|
1085 # #doctest.testfile("example.txt") |