changeset 382:ba813f148ade

development for clustering
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 21 Jul 2013 10:23:15 -0400
parents 387cc0142211
children 0ce2210790b1
files python/events.py python/ml.py python/storage.py scripts/replay-event-annotation.py
diffstat 4 files changed, 17 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/python/events.py	Fri Jul 19 11:58:35 2013 -0400
+++ b/python/events.py	Sun Jul 21 10:23:15 2013 -0400
@@ -99,7 +99,7 @@
         if self.roadUser1.features != None and self.roadUser2.features != None:
             minDistance={}
             for instant in self.timeInterval:
-                minDistance[instant] = MovingObject.minDistance(self.roadUser1, self.roadUser2, instant)
+                minDistance[instant] = moving.MovingObject.minDistance(self.roadUser1, self.roadUser2, instant)
             self.addIndicator(indicators.SeverityIndicator('Minimum Distance', minDistance))
 
     def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None):
--- a/python/ml.py	Fri Jul 19 11:58:35 2013 -0400
+++ b/python/ml.py	Sun Jul 21 10:23:15 2013 -0400
@@ -54,7 +54,7 @@
         text(self.instance.position.x+1, self.instance.position.y+1, str(self.nInstances))
 
 
-def clustering(data, similar, initialCentroids = []):
+def assignCluster(data, similarFunc, initialCentroids = [], shuffleData = True):
     '''k-means algorithm with similarity function
     Two instances should be in the same cluster if the sameCluster function returns true for two instances. It is supposed that the average centroid of a set of instances can be computed, using the function. 
     The number of clusters will be determined accordingly
@@ -65,14 +65,15 @@
     from random import shuffle
     from copy import copy, deepcopy
     localdata = copy(data) # shallow copy to avoid modifying data
-    shuffle(localdata)
+    if shuffleData:
+        shuffle(localdata)
     if initialCentroids:
         centroids = deepcopy(initialCentroids)
     else:
         centroids = [Centroid(localdata[0])]
     for instance in localdata[1:]:
         i = 0
-        while i<len(centroids) and not similar(centroids[i].instance, instance):
+        while i<len(centroids) and not similarFunc(centroids[i].instance, instance):
             i += 1
         if i == len(centroids):
             centroids.append(Centroid(instance))
@@ -81,6 +82,8 @@
 
     return centroids
 
+# TODO recompute centroids for each cluster: instance that minimizes some measure to all other elements
+
 def spectralClustering(similarityMatrix, k, iter=20):
 	'''Spectral Clustering algorithm'''
 	n = len(similarityMatrix)
--- a/python/storage.py	Fri Jul 19 11:58:35 2013 -0400
+++ b/python/storage.py	Sun Jul 21 10:23:15 2013 -0400
@@ -17,7 +17,7 @@
 # Sqlite
 #########################
 
-def saveTrajectoriesToSqlite(objects, outFilename, trajectoryType, objectNumbers = -1):
+def writeTrajectoriesToSqlite(objects, outFilename, trajectoryType, objectNumbers = -1):
     """
     This function writers trajectories to a specified sqlite file
     @param[in] objects -> a list of trajectories
@@ -402,6 +402,13 @@
         f.write('{}\n'.format(x))
     f.close()
 
+def loadListStrings(filename):
+    f = utils.openCheck(filename, 'r')
+    result = [l.strip() for l in f]
+    f.close()
+    return result
+        
+
 if __name__ == "__main__":
     import doctest
     import unittest
--- a/scripts/replay-event-annotation.py	Fri Jul 19 11:58:35 2013 -0400
+++ b/scripts/replay-event-annotation.py	Sun Jul 21 10:23:15 2013 -0400
@@ -1,6 +1,6 @@
 #! /usr/bin/env python
 
-import sys, argparse
+import sys, argparse, datetime
 
 import storage, cvutils, utils
 
@@ -23,4 +23,4 @@
     print('{} {}'.format(annotation['conflict_start_time'], annotation['conflict_end_time']))
     print(annotation['road_user_1']+' '+annotation['road_user_2']+' '+annotation['conflict_quality'])
     print(annotation['comments'])
-    cvutils.playVideo(dirname+videoDirnames[video]+video+'-{}.avi'.format(annotation['video_start_time']), utils.timeToFrames(annotation['conflict_start_time'], frameRate), frameRate, True, False, annotation['road_user_1']+' '+annotation['road_user_2']+' '+annotation['conflict_quality'])
+    cvutils.playVideo(dirname+videoDirnames[video]+video+'-{}.avi'.format(annotation['video_start_time']), utils.timeToFrames(annotation['conflict_start_time']+datetime.timedelta(seconds=-40), frameRate), frameRate, True, False, annotation['road_user_1']+' '+annotation['road_user_2']+' '+annotation['conflict_quality'])