comparison python/storage.py @ 546:6c0923f1ce68

add some functions for behaviour analysis
author MohamedGomaa
date Thu, 03 Jul 2014 14:30:20 -0400
parents 6c264b914846
children b5525249eda1
comparison
equal deleted inserted replaced
538:bd1ad468e928 546:6c0923f1ce68
59 for position in trajectory.getPositions(): 59 for position in trajectory.getPositions():
60 frame_number += 1 60 frame_number += 1
61 query = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" 61 query = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)"
62 cursor.execute(query,(trajectory_id,frame_number,position.x,position.y)) 62 cursor.execute(query,(trajectory_id,frame_number,position.x,position.y))
63 63
64 connection.commit()
65 connection.close()
66
67 def writeFeaturesToSqlite(objects, outFilename, trajectoryType, objectNumbers = -1):
68 '''write features trajectories maintain trajectory ID,velocities dataset '''
69 connection = sqlite3.connect(outFilename)
70 cursor = connection.cursor()
71
72 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))")
73 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))")
74
75 if trajectoryType == 'feature':
76 if type(objectNumbers) == int and objectNumbers == -1:
77 for trajectory in objects:
78 trajectory_id = trajectory.num
79 frame_number = trajectory.timeInterval.first
80 for position,velocity in zip(trajectory.getPositions(),trajectory.getVelocities()):
81 cursor.execute("insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)",(trajectory_id,frame_number,position.x,position.y))
82 cursor.execute("insert into velocities (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)",(trajectory_id,frame_number,velocity.x,velocity.y))
83 frame_number += 1
84
85 connection.commit()
86 connection.close()
87
88 def writePrototypesToSqlite(prototypes,nMatching, outFilename):
89 """ prototype dataset is a dictionary with keys== routes, values== prototypes Ids """
90 connection = sqlite3.connect(outFilename)
91 cursor = connection.cursor()
92
93 cursor.execute("CREATE TABLE IF NOT EXISTS \"prototypes\"(prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))")
94
95 for route in prototypes.keys():
96 if prototypes[route]!=[]:
97 for i in prototypes[route]:
98 cursor.execute("insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)",(i,route[0],route[1],nMatching[route][i]))
99
100 connection.commit()
101 connection.close()
102
103 def loadPrototypesFromSqlite(filename):
104 """
105 This function loads the prototype file in the database
106 It returns a dictionary for prototypes for each route and nMatching
107 """
108 prototypes = {}
109 nMatching={}
110
111 connection = sqlite3.connect(filename)
112 cursor = connection.cursor()
113
114 try:
115 cursor.execute('SELECT * from prototypes order by prototype_id, routeIDstart,routeIDend, nMatching')
116 except sqlite3.OperationalError as error:
117 utils.printDBError(error)
118 return []
119
120 for row in cursor:
121 route=(row[1],row[2])
122 if route not in prototypes.keys():
123 prototypes[route]=[]
124 prototypes[route].append(row[0])
125 nMatching[row[0]]=row[3]
126
127 connection.close()
128 return prototypes,nMatching
129
130 def writeLabelsToSqlite(labels, outFilename):
131 """ labels is a dictionary with keys: routes, values: prototypes Ids
132 """
133 connection = sqlite3.connect(outFilename)
134 cursor = connection.cursor()
135
136 cursor.execute("CREATE TABLE IF NOT EXISTS \"labels\"(object_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, prototype_id INTEGER, PRIMARY KEY(object_id))")
137
138 for route in labels.keys():
139 if labels[route]!=[]:
140 for i in labels[route]:
141 for j in labels[route][i]:
142 cursor.execute("insert into labels (object_id, routeIDstart,routeIDend, prototype_id) values (?,?,?,?)",(j,route[0],route[1],i))
143
144 connection.commit()
145 connection.close()
146
147 def loadLabelsFromSqlite(filename):
148 labels = {}
149
150 connection = sqlite3.connect(filename)
151 cursor = connection.cursor()
152
153 try:
154 cursor.execute('SELECT * from labels order by object_id, routeIDstart,routeIDend, prototype_id')
155 except sqlite3.OperationalError as error:
156 utils.printDBError(error)
157 return []
158
159 for row in cursor:
160 route=(row[1],row[2])
161 p=row[3]
162 if route not in labels.keys():
163 labels[route]={}
164 if p not in labels[route].keys():
165 labels[route][p]=[]
166 labels[route][p].append(row[0])
167
168 connection.close()
169 return labels
170
171 def writeRoutesToSqlite(Routes, outFilename):
172 """ This function writes the activity path define by start and end IDs"""
173 connection = sqlite3.connect(outFilename)
174 cursor = connection.cursor()
175
176 cursor.execute("CREATE TABLE IF NOT EXISTS \"routes\"(object_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, PRIMARY KEY(object_id))")
177
178 for route in Routes.keys():
179 if Routes[route]!=[]:
180 for i in Routes[route]:
181 cursor.execute("insert into routes (object_id, routeIDstart,routeIDend) values (?,?,?)",(i,route[0],route[1]))
182
183 connection.commit()
184 connection.close()
185
186 def loadRoutesFromSqlite(filename):
187 Routes = {}
188
189 connection = sqlite3.connect(filename)
190 cursor = connection.cursor()
191
192 try:
193 cursor.execute('SELECT * from routes order by object_id, routeIDstart,routeIDend')
194 except sqlite3.OperationalError as error:
195 utils.printDBError(error)
196 return []
197
198 for row in cursor:
199 route=(row[1],row[2])
200 if route not in Routes.keys():
201 Routes[route]=[]
202 Routes[route].append(row[0])
203
204 connection.close()
205 return Routes
206
207 def setRoutes(filename, objects):
208 connection = sqlite3.connect(filename)
209 cursor = connection.cursor()
210 for obj in objects:
211 cursor.execute('update objects set startRouteID = {} where object_id = {}'.format(obj.startRouteID, obj.getNum()))
212 cursor.execute('update objects set endRouteID = {} where object_id = {}'.format(obj.endRouteID, obj.getNum()))
64 connection.commit() 213 connection.commit()
65 connection.close() 214 connection.close()
66 215
67 def setRoadUserTypes(filename, objects): 216 def setRoadUserTypes(filename, objects):
68 '''Saves the user types of the objects in the sqlite database stored in filename 217 '''Saves the user types of the objects in the sqlite database stored in filename