Advertisement
Guest User

Untitled

a guest
Apr 20th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. import pyximport; pyximport.install()
  2. import cmap
  3.  
  4. import numpy
  5.  
  6. def test_getHeatMap():
  7.     mapStrs = ["""000
  8. 000
  9. 000""",
  10. """11111
  11. 10101
  12. 10101
  13. 10001
  14. 11111""",
  15. """00000000
  16. 01111110
  17. 00000010
  18. 01111110
  19. 01000000
  20. 01110110
  21. 00000000""",
  22. """00000
  23. 00000
  24. 00000"""]
  25.     goals = [[(0, 0)], [(1, 1)], [(5, 2)], [(0, 1), (4, 1)]]
  26.     expected = map(numpy.array, [
  27.             [[0, 1, 2],
  28.              [1, 1, 2],
  29.              [2, 2, 2]],
  30.             [[-1, -1, -1, -1, -1],
  31.              [-1,  0,  1,  2, -1],
  32.              [-1, -1, -1,  2, -1],
  33.              [-1,  4,  3,  3, -1],
  34.              [-1, -1, -1, -1, -1]],
  35.             [[6,   5,  5,  5,  6,  7,  8],
  36.              [6,  -1,  4, -1, -1, -1,  8],
  37.              [7,  -1,  3, -1, 13, -1,  9],
  38.              [8,  -1,  2, -1, 12, -1, 10],
  39.              [9,  -1,  1, -1, 12, 11, 11],
  40.              [10, -1,  0, -1, 12, -1, 12],
  41.              [11, -1, -1, -1, 13, -1, 13],
  42.              [12, 12, 13, 14, 14, 14, 14]],
  43.             [[1, 0, 1],
  44.              [1, 1, 1],
  45.              [2, 2, 2],
  46.              [1, 1, 1],
  47.              [1, 0, 1]],
  48.     ])
  49.  
  50.     for i in xrange(len(mapStrs)):        
  51.         # Conversion to array could be simpler but I want to use X/Y ordering
  52.         # instead of Y/X.
  53.         lines = mapStrs[i].split('\n')
  54.         mapGrid = numpy.zeros((len(lines[0]), len(lines)))
  55.         for j, line in enumerate(lines):
  56.             mapGrid[:, j] = map(int, line)
  57.         result = cmap.getHeatMap(mapGrid, goals[i])
  58.         if numpy.any(result != expected[i]):
  59.             print "Test %d failed" % i
  60.             print "Expected:\n", expected[i]
  61.             print "Got:\n", result
  62.         assert numpy.all(result == expected[i])
  63.  
  64. def speedTest_getHeatMap():
  65.     for i in xrange(10):
  66.         grid = numpy.zeros((360, 240))
  67.         cmap.getHeatMap(grid, [(0, 0)])
  68.  
  69. if __name__ == '__main__':
  70.     test_getHeatMap()
  71.     import cProfile
  72.     cProfile.runctx('speedTest_getHeatMap()', locals(), globals(), 'profiling.txt')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement