Mercurial Hosting > traffic-intelligence
diff python/storage.py @ 844:5a68779d7777
added capability to save prototypes
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 14 Jul 2016 00:34:59 -0400 |
parents | 119c4efe6398 |
children | 0cb69238e6f5 |
line wrap: on
line diff
--- a/python/storage.py Wed Jul 13 23:45:47 2016 -0400 +++ b/python/storage.py Thu Jul 14 00:34:59 2016 -0400 @@ -364,21 +364,44 @@ printDBError(error) connection.close() -def savePrototypesToSqlite(filename, prototypes, trajectoryType = 'feature'): - 'Work in progress, do not use' +def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None): + '''save the prototype indices + nMatchings, if not None, is a dictionnary between indices and number of matches''' connection = sqlite3.connect(filename) cursor = connection.cursor() try: - 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))') - #for inter in interactions: - # saveInteraction(cursor, inter) + cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER)') + for i in prototypeIndices: + if nMatchings is not None: + n = nMatchings[i] + else: + n = 'NULL' + cursor.execute('INSERT INTO prototypes (id, trajectory_type, nMatchings) VALUES ({},\"{}\",{})'.format(i, trajectoryType, n)) except sqlite3.OperationalError as error: printDBError(error) connection.commit() connection.close() def loadPrototypesFromSqlite(filename): - pass + 'Loads prototype ids and matchings (if stored)' + connection = sqlite3.connect(filename) + cursor = connection.cursor() + prototypeIndices = [] + trajectoryTypes = [] + nMatchings = {} + try: + cursor.execute('SELECT * FROM prototypes') + for row in cursor: + prototypeIndices.append(row[0]) + trajectoryTypes.append(row[1]) + if row[2] is not None: + nMatchings[row[0]] = row[2] + except sqlite3.OperationalError as error: + printDBError(error) + connection.close() + if len(set(trajectoryTypes)) > 1: + print('Different types of prototypes in database ({}).'.format(set(trajectoryTypes))) + return prototypeIndices, trajectoryTypes[0], nMatchings def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): '''Loads bounding box moving object from an SQLite @@ -418,6 +441,8 @@ dropTables(connection, ['bounding_boxes']) elif dataType == 'pois': dropTables(connection, ['gaussians2d']) + elif dataType == 'prototype': + dropTables(connection, ['prototypes']) else: print('Unknown data type {} to delete from database'.format(dataType)) connection.close()