Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 68:846fa9dc47de | 69:cc192d0450b3 |
|---|---|
| 149 @staticmethod | 149 @staticmethod |
| 150 def plotAll(points, color='r'): | 150 def plotAll(points, color='r'): |
| 151 from matplotlib.pyplot import scatter | 151 from matplotlib.pyplot import scatter |
| 152 scatter([p.x for p in points],[p.y for p in points], c=color) | 152 scatter([p.x for p in points],[p.y for p in points], c=color) |
| 153 | 153 |
| 154 | |
| 155 class Trajectory: | 154 class Trajectory: |
| 156 '''Class for trajectories | 155 '''Class for trajectories |
| 157 i.e. a temporal sequence of positions | 156 i.e. a temporal sequence of positions |
| 158 | 157 |
| 159 the class is iterable.''' | 158 the class is iterable.''' |
| 166 return Trajectory([[float(n) for n in line1.split(' ')], | 165 return Trajectory([[float(n) for n in line1.split(' ')], |
| 167 [float(n) for n in line2.split(' ')]]) | 166 [float(n) for n in line2.split(' ')]]) |
| 168 | 167 |
| 169 def __str__(self): | 168 def __str__(self): |
| 170 return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())]) | 169 return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())]) |
| 170 | |
| 171 def __repr__(self): | |
| 172 return str(self) | |
| 171 | 173 |
| 172 def __getitem__(self, i): | 174 def __getitem__(self, i): |
| 173 return Point(self.positions[0][i], self.positions[1][i]) | 175 return Point(self.positions[0][i], self.positions[1][i]) |
| 174 | 176 |
| 175 def __iter__(self): | 177 def __iter__(self): |
| 380 | 382 |
| 381 def __init__(self, name, values, timeInterval=None): | 383 def __init__(self, name, values, timeInterval=None): |
| 382 self.name = name | 384 self.name = name |
| 383 self.values = values | 385 self.values = values |
| 384 self.timeInterval = timeInterval | 386 self.timeInterval = timeInterval |
| 387 if timeInterval: | |
| 388 assert len(values) == timeInterval.length() | |
| 385 | 389 |
| 386 def empty(self): | 390 def empty(self): |
| 387 return len(self.values) == 0 | 391 return len(self.values) == 0 |
| 392 | |
| 393 def __getitem__(self, i): | |
| 394 if self.timeInterval: | |
| 395 if self.timeInterval.contains(i): | |
| 396 return self.values[i-self.timeInterval.first] | |
| 397 else: | |
| 398 if i in self.values.keys(): | |
| 399 return self.values[i] | |
| 400 return None # default | |
| 401 | |
| 402 def __iter__(self): | |
| 403 self.iterInstantNum = 0 # index in the interval or keys of the dict | |
| 404 return self | |
| 405 | |
| 406 def next(self): | |
| 407 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\ | |
| 408 # or (self.iterInstantNum >= self.values) | |
| 409 raise StopIteration | |
| 410 else: | |
| 411 self.iterInstantNum += 1 | |
| 412 if self.timeInterval: | |
| 413 return self.values[self.iterInstantNum-1] | |
| 414 else: | |
| 415 return self.values.values()[self.iterInstantNum-1] | |
| 388 | 416 |
| 389 class SeverityIndicator(TemporalIndicator): | 417 class SeverityIndicator(TemporalIndicator): |
| 390 '''Class for severity indicators | 418 '''Class for severity indicators |
| 391 field mostSevereIsMax is True | 419 field mostSevereIsMax is True |
| 392 if the most severe value taken by the indicator is the maximum''' | 420 if the most severe value taken by the indicator is the maximum''' |
| 413 | 441 |
| 414 def indicatorMap(indicatorValues, trajectory, squareSize): | 442 def indicatorMap(indicatorValues, trajectory, squareSize): |
| 415 '''Returns a dictionary | 443 '''Returns a dictionary |
| 416 with keys for the indices of the cells (squares) | 444 with keys for the indices of the cells (squares) |
| 417 in which the trajectory positions are located | 445 in which the trajectory positions are located |
| 418 at which the indicator values are attached''' | 446 at which the indicator values are attached |
| 447 | |
| 448 ex: speeds and trajectory''' | |
| 449 | |
| 419 from numpy import floor, mean | 450 from numpy import floor, mean |
| 420 assert len(indicatorValues) == trajectory.length() | 451 assert len(indicatorValues) == trajectory.length() |
| 421 indicatorMap = {} | 452 indicatorMap = {} |
| 422 for k in xrange(trajectory.length()): | 453 for k in xrange(trajectory.length()): |
| 423 p = trajectory[k] | 454 p = trajectory[k] |
