diff python/storage.py @ 849:a414a7d58483

corrected issue with prototypes storage
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 22 Jul 2016 17:23:49 -0400
parents 0cb69238e6f5
children c724a51d4f5f
line wrap: on
line diff
--- a/python/storage.py	Thu Jul 21 17:51:11 2016 -0400
+++ b/python/storage.py	Fri Jul 22 17:23:49 2016 -0400
@@ -366,12 +366,13 @@
 
 def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None, dbFilenames = None):
     '''save the prototype indices
-    nMatchings, if not None, is a dictionnary between indices and number of matches'''
+    nMatchings, if not None, is a list of the number of matches
+    dbFilenames, if not None, is a list of the DB filenames'''
     connection = sqlite3.connect(filename)
     cursor = connection.cursor()
     try:
         cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER, dbfilename VARCHAR, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER, PRIMARY KEY (id, dbfilename))')
-        for i in prototypeIndices:
+        for i, protoId in enumerate(prototypeIndices):
             if nMatchings is not None:
                 n = nMatchings[i]
             else:
@@ -380,7 +381,7 @@
                 dbfn = dbFilenames[i]
             else:
                 dbfn = filename
-            cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nMatchings) VALUES ({},\"{}\",\"{}\",{})'.format(i, dbfn, trajectoryType, n))
+            cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nMatchings) VALUES ({},\"{}\",\"{}\",{})'.format(protoId, dbfn, trajectoryType, n))
     except sqlite3.OperationalError as error:
         printDBError(error)
     connection.commit()
@@ -391,23 +392,23 @@
     connection = sqlite3.connect(filename)
     cursor = connection.cursor()
     prototypeIndices = []
-    dbFilenames = {}
+    dbFilenames = []
     trajectoryTypes = []
-    nMatchings = {}
+    nMatchings = []
     try:
         cursor.execute('SELECT * FROM prototypes')
         for row in cursor:
             prototypeIndices.append(row[0])
-            dbFilenames[row[0]] = row[1]
+            dbFilenames.append(row[1])
             trajectoryTypes.append(row[2])
-            if row[2] is not None:
-                nMatchings[row[0]] = row[3]
+            if row[3] is not None:
+                nMatchings.append(row[3])
     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, dbFilenames, trajectoryTypes[0], nMatchings
+    return prototypeIndices, dbFilenames, trajectoryTypes, nMatchings
 
 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None):
     '''Loads bounding box moving object from an SQLite