comparison python/storage.py @ 607:84690dfe5560

add some functions for behaviour analysis
author MohamedGomaa
date Tue, 25 Nov 2014 22:49:47 -0500
parents 36605d843be5
children 0954aaf28231
comparison
equal deleted inserted replaced
606:75ad9c0d6cc3 607:84690dfe5560
165 labels[route][p]=[] 165 labels[route][p]=[]
166 labels[route][p].append(row[0]) 166 labels[route][p].append(row[0])
167 167
168 connection.close() 168 connection.close()
169 return labels 169 return labels
170 def writeSpeedPrototypeToSqlite(prototypes,nmatching, outFilename):
171 """ to match the format of second layer prototypes"""
172 connection = sqlite3.connect(outFilename)
173 cursor = connection.cursor()
174
175 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))")
176
177 for route in prototypes.keys():
178 if prototypes[route]!={}:
179 for i in prototypes[route]:
180 if prototypes[route][i]!= []:
181 for j in prototypes[route][i]:
182 cursor.execute("insert into speedprototypes (spdprototype_id,prototype_id, routeID_start, routeID_end, nMatching) values (?,?,?,?,?)",(j,i,route[0],route[1],nmatching[j]))
183
184 connection.commit()
185 connection.close()
186
187 def loadSpeedPrototypeFromSqlite(filename):
188 """
189 This function loads the prototypes table in the database of name <filename>.
190 """
191 prototypes = {}
192 nMatching={}
193 connection = sqlite3.connect(filename)
194 cursor = connection.cursor()
195
196 try:
197 cursor.execute('SELECT * from speedprototypes order by spdprototype_id,prototype_id, routeID_start, routeID_end, nMatching')
198 except sqlite3.OperationalError as error:
199 utils.printDBError(error)
200 return []
201
202 for row in cursor:
203 route=(row[2],row[3])
204 if route not in prototypes.keys():
205 prototypes[route]={}
206 if row[1] not in prototypes[route].keys():
207 prototypes[route][row[1]]=[]
208 prototypes[route][row[1]].append(row[0])
209 nMatching[row[0]]=row[4]
210
211 connection.close()
212 return prototypes,nMatching
213
170 214
171 def writeRoutesToSqlite(Routes, outputFilename): 215 def writeRoutesToSqlite(Routes, outputFilename):
172 """ This function writes the activity path define by start and end IDs""" 216 """ This function writes the activity path define by start and end IDs"""
173 connection = sqlite3.connect(outputFilename) 217 connection = sqlite3.connect(outputFilename)
174 cursor = connection.cursor() 218 cursor = connection.cursor()
792 for sectionName in config.sections(): 836 for sectionName in config.sections():
793 configDict[sectionName] = SceneParameters(config, sectionName) 837 configDict[sectionName] = SceneParameters(config, sectionName)
794 return configDict 838 return configDict
795 839
796 840
841
797 if __name__ == "__main__": 842 if __name__ == "__main__":
798 import doctest 843 import doctest
799 import unittest 844 import unittest
800 suite = doctest.DocFileSuite('tests/storage.txt') 845 suite = doctest.DocFileSuite('tests/storage.txt')
801 unittest.TextTestRunner().run(suite) 846 unittest.TextTestRunner().run(suite)