# HG changeset patch # User Francois Belisle # Date 1338919825 14400 # Node ID e841ba9981e29d6b801958aeed073452da27c00c # Parent 746d02cea65fd4cba1e20883262c1b733922b6c3# Parent 48f83ff769fd25da66b5f59d1ff7c92c48a611a2 Merged. diff -r 48f83ff769fd -r e841ba9981e2 python/storage.py --- 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 . + 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()