Mercurial Hosting > traffic-intelligence
changeset 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 | a52653dca25d |
files | python/moving.py python/tests/moving.txt |
diffstat | 2 files changed, 58 insertions(+), 2 deletions(-) [+] |
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 = {}
--- a/python/tests/moving.txt Mon Nov 08 12:20:52 2010 -0500 +++ b/python/tests/moving.txt Wed Nov 10 23:41:49 2010 -0500 @@ -25,3 +25,28 @@ >>> Point.distanceNorm2(Point(3,4),Point(1,7)) 3.6055512754639891 +>>> t1 = Trajectory([[0.5,1.5,2.5],[0.5,3.5,6.5]]) +>>> t1.length() +3 +>>> t1[1] +(1.500000,3.500000) + +>>> indic1 = TemporalIndicator('bla', [0,3,-4], TimeInterval(4,6)) +>>> indic1.empty() +False +>>> indic1[5] +3 +>>> indic1[3] +>>> [v for v in indic1] +[0, 3, -4] +>>> indic1 = TemporalIndicator('bla', {2:0,4:3,5:-5}) +>>> indic1[4] +3 +>>> indic1[3] +>>> [v for v in indic1] +[0, 3, -5] + +>>> indicatorMap([1,2,3], t1, 1) +{(1.0, 3.0): 2.0, (2.0, 6.0): 3.0, (0.0, 0.0): 1.0} +>>> indicatorMap([1,2,3], t1, 4) +{(0.0, 1.0): 3.0, (0.0, 0.0): 1.5}