changeset 210:e841ba9981e2

Merged.
author Francois Belisle <belisle.francois@gmail.com>
date Tue, 05 Jun 2012 14:10:25 -0400
parents 746d02cea65f (diff) 48f83ff769fd (current diff)
children ada6e8fbe4c6
files
diffstat 1 files changed, 71 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Wed May 16 02:23:49 2012 -0400
+++ b/python/storage.py	Tue Jun 05 14:10:25 2012 -0400
@@ -1,4 +1,5 @@
 #! /usr/bin/env python
+# -*- coding: utf-8 -*-
 '''Various utilities to save and load data'''
 
 import utils
@@ -11,13 +12,82 @@
                   'car':2,
                   'truck':3}
 
+def writeTrajectoriesToSqlite(objects, outFile, trajectoryType, objectNumbers = -1):
+    """
+    This function writers trajectories to a specified sqlite file
+    @param[in] objects -> a list of trajectories
+    @param[in] trajectoryType -
+    @param[out] outFile -> the .sqlite file containting the written objects
+    @param[in] objectNumber : number of objects loaded
+    """
+
+    import sqlite3
+    connection = sqlite3.connect(outFile)
+    cursor = connection.cursor()
+
+    #creation de la table
+    schema = "CREATE TABLE \"trajectories\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))"
+    cursor.execute(schema)
+    trajectory_id = 0
+    frame_number = 0
+    if trajectoryType == 'feature':
+        if type(objectNumbers) == int and objectNumbers == -1:
+            for trajectory in objects:
+                trajectory_id += 1
+                frame_number = 0
+                for position in trajectory.getPositions():
+                    frame_number += 1
+                    requete = "insert into trajectories(trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)"
+                    cursor.execute(requete,(trajectory_id,frame_number,position.x,position.y))
+                    
+    connection.commit()            
+    connection.close()
+
+def testWrite():
+    features = loadTrajectoriesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/Data/prototypes.sqlite",'feature',-1)
+    writeTrajectoriesToSqlite(features, "/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/testWrite.sqlite", 'feature')
+
+def loadPrototypeMatchIndexesFromSqlite(filename):
+    """
+    This function loads the prototypes table in the database of name <filename>.
+    It returns a list of tuples representing matching ids : [(prototype_id, matched_trajectory_id),...]
+    """
+    matched_indexes = []
+
+    import sqlite3    
+    connection = sqlite3.connect(filename) # add test if it open
+    cursor = connection.cursor()
+
+    try:
+        cursor.execute('SELECT * from prototypes order by prototype_id, trajectory_id_matched')
+    except sqlite3.OperationalError:
+        return matched_indexes
+
+    for row in cursor:
+        matched_indexes.append((row[0],row[1]))
+    return matched_indexes
+
+def testloadPrototypeMatchIndexesFromSqlite():
+    empty_list = loadPrototypeMatchIndexesFromSqlite("bidon")
+    if empty_list == []:
+        print "Empty list test Ok"
+    
+    matches=loadPrototypeMatchIndexesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/prototypes-with-matches.sqlite")
+    if len(matches) == 66:
+        print "Matches test Ok"
+    return matches
+    
+    
+
+
+
 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1):
     '''Loads nObjects or the indices in objectNumbers from the database 
     TODO: load velocities (replace table name 'positions' by 'velocities'
     TODO: load features as well, other ways of averaging trajectories
     '''
     import sqlite3
-
+    
     connection = sqlite3.connect(filename) # add test if it open
     cursor = connection.cursor()