diff python/moving.py @ 69:cc192d0450b3

added full support for two implementations of indicators, with tests
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 10 Nov 2010 23:41:49 -0500
parents 846fa9dc47de
children 45e958ccd9bd
line wrap: on
line diff
--- a/python/moving.py	Mon Nov 08 12:20:52 2010 -0500
+++ b/python/moving.py	Wed Nov 10 23:41:49 2010 -0500
@@ -151,7 +151,6 @@
         from matplotlib.pyplot import scatter
         scatter([p.x for p in points],[p.y for p in points], c=color)
 
-
 class Trajectory:
     '''Class for trajectories
     i.e. a temporal sequence of positions
@@ -169,6 +168,9 @@
     def __str__(self):
         return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())])
 
+    def __repr__(self):
+        return str(self)
+
     def __getitem__(self, i):
         return Point(self.positions[0][i], self.positions[1][i])
 
@@ -382,10 +384,36 @@
         self.name = name
         self.values = values
         self.timeInterval = timeInterval
+        if timeInterval:
+            assert len(values) == timeInterval.length()
 
     def empty(self):
         return len(self.values) == 0
 
+    def __getitem__(self, i):
+        if self.timeInterval:
+            if self.timeInterval.contains(i):
+                return self.values[i-self.timeInterval.first]
+        else:
+            if i in self.values.keys():
+                return self.values[i]
+        return None # default
+
+    def __iter__(self):
+        self.iterInstantNum = 0 # index in the interval or keys of the dict
+        return self
+
+    def next(self):
+        if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\
+           #     or (self.iterInstantNum >= self.values)
+            raise StopIteration
+        else:
+            self.iterInstantNum += 1
+            if self.timeInterval:
+                return self.values[self.iterInstantNum-1]
+            else:
+                return self.values.values()[self.iterInstantNum-1]
+
 class SeverityIndicator(TemporalIndicator):
     '''Class for severity indicators 
     field mostSevereIsMax is True 
@@ -415,7 +443,10 @@
     '''Returns a dictionary 
     with keys for the indices of the cells (squares)
     in which the trajectory positions are located
-    at which the indicator values are attached'''
+    at which the indicator values are attached
+
+    ex: speeds and trajectory'''
+
     from numpy import floor, mean
     assert len(indicatorValues) == trajectory.length()
     indicatorMap = {}