comparison python/moving.py @ 65:75cf537b8d88

moved and generalized map making functions to the library
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 07 Nov 2010 01:09:50 -0500
parents c75bcdaed00f
children 56fe4ef1377e
comparison
equal deleted inserted replaced
64:c75bcdaed00f 65:75cf537b8d88
385 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values 385 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values
386 return mean(values[:minNInstants]) 386 return mean(values[:minNInstants])
387 else: 387 else:
388 return None 388 return None
389 389
390 def indicatorMap(indicatorValues, trajectory, squareSize):
391 '''Returns a dictionary
392 with keys for the indices of the cells (squares)
393 in which the trajectory positions are located
394 at which the indicator values are attached'''
395 from numpy import floor, mean
396 assert len(indicatorValues) == trajectory.length()
397 indicatorMap = {}
398 for k in xrange(trajectory.length()):
399 p = trajectory[k]
400 i = floor(p.x/squareSize)
401 j = floor(p.y/squareSize)
402 if indicatorMap.has_key((i,j)):
403 indicatorMap[(i,j)].append(indicatorValues[k])
404 else:
405 indicatorMap[(i,j)] = [indicatorValues[k]]
406 for k in indicatorMap.keys():
407 indicatorMap[k] = mean(indicatorMap[k])
408 return indicatorMap
409
410 def combineIndicatorMaps(maps, squareSize):
411 '''Puts many indicator maps together
412 (averaging the values in each cell
413 if more than one maps has a value)'''
414 from numpy import mean
415 indicatorMap = {}
416 for m in maps:
417 for k,v in m.iteritems():
418 if indicatorMap.has_key(k):
419 indicatorMap[k].append(v)
420 else:
421 indicatorMap[k] = [v]
422 for k in indicatorMap.keys():
423 indicatorMap[k] = mean(indicatorMap[k])
424 return indicatorMap
425
390 if __name__ == "__main__": 426 if __name__ == "__main__":
391 import doctest 427 import doctest
392 import unittest 428 import unittest
393 suite = doctest.DocFileSuite('tests/moving.txt') 429 suite = doctest.DocFileSuite('tests/moving.txt')
394 #suite = doctest.DocTestSuite() 430 #suite = doctest.DocTestSuite()