changeset 344:14a2405f54f8

slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 21 Jun 2013 17:32:57 -0400
parents 74e437ab5f11
children fa64b2e3a64f
files python/storage.py python/utils.py scripts/delete-object-tables.py scripts/delete-tables.py scripts/safety-analysis.py
diffstat 5 files changed, 38 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Fri Jun 21 15:28:59 2013 -0400
+++ b/python/storage.py	Fri Jun 21 17:32:57 2013 -0400
@@ -56,9 +56,6 @@
     connection.commit()
     connection.close()
 
-def printDBError(error):
-    print('DB Error: {0}'.format(error))
-
 def loadPrototypeMatchIndexesFromSqlite(filename):
     """
     This function loads the prototypes table in the database of name <filename>.
@@ -72,7 +69,7 @@
     try:
         cursor.execute('SELECT * from prototypes order by prototype_id, trajectory_id_matched')
     except sqlite3.OperationalError as error:
-        printDBError(error)
+        utils.printDBError(error)
         return []
 
     for row in cursor:
@@ -115,7 +112,7 @@
         else:
             print('no trajectory type was chosen')
     except sqlite3.OperationalError as error:
-        printDBError(error)
+        utils.printDBError(error)
         return []
 
     objId = -1
@@ -185,22 +182,23 @@
                 obj.userType = userTypes[obj.getNum()]
              
         except sqlite3.OperationalError as error:
-            printDBError(error)
+            utils.printDBError(error)
             return []
 
     connection.close()
     return objects
 
-def removeObjectsFromSqlite(filename):
-    'Removes the objects and object_features tables in the filename'
+def removeFromSqlite(filename, dataType):
+    'Removes some tables in the filename depending on type of data'
     connection = sqlite3.connect(filename)
-    utils.dropTables(connection, ['objects', 'objects_features'])
+    if dataType == 'object':
+        utils.dropTables(connection, ['objects', 'objects_features'])
+    elif dataType == 'interaction':
+        utils.dropTables(connection, ['interactions', 'indicators'])
+    else:
+        print('Unknown data type {} to delete from database'.format(dataType))
     connection.close()
 
-def deleteIndicators(filename):
-    'Deletes all indicator data in db'
-    pass
-
 def createInteractionTable(cursor):
     cursor.execute('CREATE TABLE IF NOT EXISTS interactions (id INTEGER PRIMARY KEY, object_id1 INTEGER, object_id2 INTEGER, first_frame_number INTEGER, last_frame_number INTEGER, FOREIGN KEY(object_id1) REFERENCES objects(id), FOREIGN KEY(object_id2) REFERENCES objects(id))')
 
@@ -222,7 +220,7 @@
         for inter in interactions:
             saveInteraction(cursor, inter)
     except sqlite3.OperationalError as error:
-        printDBError(error)
+        utils.printDBError(error)
     connection.commit()
     connection.close()
 
@@ -245,7 +243,7 @@
                 if indicator != None:
                     saveIndicator(cursor, inter.getNum(), indicator)
     except sqlite3.OperationalError as error:
-        printDBError(error)
+        utils.printDBError(error)
     connection.commit()
     connection.close()
 
@@ -284,7 +282,7 @@
             interactions.append(events.Interaction(interactionNum, moving.TimeInterval(row[3],row[4]), roadUserNumbers[0], roadUserNumbers[1]))
             interactions[-1].indicators = tmpIndicators
     except sqlite3.OperationalError as error:
-        printDBError(error)
+        utils.printDBError(error)
         return []
     connection.close()
     return interactions
--- a/python/utils.py	Fri Jun 21 15:28:59 2013 -0400
+++ b/python/utils.py	Fri Jun 21 17:32:57 2013 -0400
@@ -455,11 +455,17 @@
 # sqlite
 #########################
 
+def printDBError(error):
+    print('DB Error: {}'.format(error))
+
 def dropTables(connection, tableNames):
     'deletes the table with names in tableNames'
-    cursor = connection.cursor()
-    for tableName in tableNames:
-        cursor.execute('DROP TABLE '+tableName)
+    try:
+        cursor = connection.cursor()
+        for tableName in tableNames:
+            cursor.execute('DROP TABLE '+tableName)
+    except sqlite3.OperationalError as error:
+        printDBError(error)
 
 #########################
 # running tests
--- a/scripts/delete-object-tables.py	Fri Jun 21 15:28:59 2013 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-#! /usr/bin/env python
-
-import sys,getopt
-
-import utils
-import storage
-
-options, args = getopt.getopt(sys.argv[1:], 'h',['help'])
-options = dict(options)
-
-if '--help' in options.keys() or '-h' in options.keys() or len(args) == 0:
-    print('Usage: {0} --help|-h <database-filename.sqlite>'.format(sys.argv[0]))
-    sys.exit()
-
-storage.removeObjectsFromSqlite(args[0])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/delete-tables.py	Fri Jun 21 17:32:57 2013 -0400
@@ -0,0 +1,14 @@
+#! /usr/bin/env python
+
+import sys, argparse
+
+import utils
+import storage
+
+parser = argparse.ArgumentParser(description='The program deletes the tables in the database before saving new results (for objects, tables object_features and objects are dropped; for interactions, the tables interactions and indicators are dropped')
+#parser.add_argument('configFilename', help = 'name of the configuration file')
+parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database', required = True)
+parser.add_argument('-t', dest = 'dataType', help = 'type of the data to remove', required = True, choices = ['object','interaction'])
+args = parser.parse_args()
+
+storage.removeFromSqlite(args.databaseFilename, args.dataType)
--- a/scripts/safety-analysis.py	Fri Jun 21 15:28:59 2013 -0400
+++ b/scripts/safety-analysis.py	Fri Jun 21 17:32:57 2013 -0400
@@ -7,8 +7,6 @@
 import matplotlib.pyplot as plt
 import numpy as np
 
-from ConfigParser import ConfigParser
-
 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene')
 parser.add_argument('configFilename', help = 'name of the configuration file')
 # parser.add_argument('-c', help = 'name of the configuration file') # 
@@ -53,7 +51,7 @@
 # features = storage.loadTrajectoriesFromSqlite('amherst-10.sqlite','feature') # needed if normal adaptation
 
 interactions = events.createInteractions(objects)
-for inter in interactions[:2]:
+for inter in interactions:
     inter.computeIndicators()
     # inter.computeCrossingsCollisions(constantVelocityPredictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ)