Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 209:746d02cea65f
Added function to read Prototype indexes matches.
| author | Francois Belisle <belisle.francois@gmail.com> |
|---|---|
| date | Tue, 05 Jun 2012 14:08:46 -0400 |
| parents | d9855499fc88 |
| children | ada6e8fbe4c6 |
comparison
equal
deleted
inserted
replaced
| 208:d9855499fc88 | 209:746d02cea65f |
|---|---|
| 26 cursor = connection.cursor() | 26 cursor = connection.cursor() |
| 27 | 27 |
| 28 #creation de la table | 28 #creation de la table |
| 29 schema = "CREATE TABLE \"trajectories\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))" | 29 schema = "CREATE TABLE \"trajectories\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))" |
| 30 cursor.execute(schema) | 30 cursor.execute(schema) |
| 31 | |
| 32 trajectory_id = 0 | 31 trajectory_id = 0 |
| 33 frame_number = 0 | 32 frame_number = 0 |
| 34 if trajectoryType == 'feature': | 33 if trajectoryType == 'feature': |
| 35 if type(objectNumbers) == int and objectNumbers == -1: | 34 if type(objectNumbers) == int and objectNumbers == -1: |
| 36 for trajectory in objects: | 35 for trajectory in objects: |
| 45 connection.close() | 44 connection.close() |
| 46 | 45 |
| 47 def testWrite(): | 46 def testWrite(): |
| 48 features = loadTrajectoriesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/Data/prototypes.sqlite",'feature',-1) | 47 features = loadTrajectoriesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/Data/prototypes.sqlite",'feature',-1) |
| 49 writeTrajectoriesToSqlite(features, "/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/testWrite.sqlite", 'feature') | 48 writeTrajectoriesToSqlite(features, "/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/testWrite.sqlite", 'feature') |
| 49 | |
| 50 def loadPrototypeMatchIndexesFromSqlite(filename): | |
| 51 """ | |
| 52 This function loads the prototypes table in the database of name <filename>. | |
| 53 It returns a list of tuples representing matching ids : [(prototype_id, matched_trajectory_id),...] | |
| 54 """ | |
| 55 matched_indexes = [] | |
| 56 | |
| 57 import sqlite3 | |
| 58 connection = sqlite3.connect(filename) # add test if it open | |
| 59 cursor = connection.cursor() | |
| 60 | |
| 61 try: | |
| 62 cursor.execute('SELECT * from prototypes order by prototype_id, trajectory_id_matched') | |
| 63 except sqlite3.OperationalError: | |
| 64 return matched_indexes | |
| 65 | |
| 66 for row in cursor: | |
| 67 matched_indexes.append((row[0],row[1])) | |
| 68 return matched_indexes | |
| 69 | |
| 70 def testloadPrototypeMatchIndexesFromSqlite(): | |
| 71 empty_list = loadPrototypeMatchIndexesFromSqlite("bidon") | |
| 72 if empty_list == []: | |
| 73 print "Empty list test Ok" | |
| 74 | |
| 75 matches=loadPrototypeMatchIndexesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/prototypes-with-matches.sqlite") | |
| 76 if len(matches) == 66: | |
| 77 print "Matches test Ok" | |
| 78 return matches | |
| 79 | |
| 80 | |
| 81 | |
| 50 | 82 |
| 51 | 83 |
| 52 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1): | 84 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1): |
| 53 '''Loads nObjects or the indices in objectNumbers from the database | 85 '''Loads nObjects or the indices in objectNumbers from the database |
| 54 TODO: load velocities (replace table name 'positions' by 'velocities' | 86 TODO: load velocities (replace table name 'positions' by 'velocities' |
