Guest User

Untitled

a guest
Dec 17th, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. """
  2. Copyright (c) 2017 Stian Lode,
  3. stian.lode@gmail.com
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. """
  21. import numpy as np
  22.  
  23. # Let me know if you found it useful at stian.lode@gmail.com
  24. def edges(window):
  25. """Splits a window into start and end indices. Will default to have the
  26. larger padding at the end in case of an odd window.
  27. """
  28. start = window//2
  29. end = window-start
  30. return (start, end)
  31.  
  32. def fast_pad_symmetric(values, window, dtype='f8'):
  33. """A fast version of numpy n-dimensional symmetric pad.
  34.  
  35. In contrast to np.pad, this algorithm only allocates memory once, regardless
  36. of the number of axes padded. Performance for large data sets is vastly
  37. improved.
  38.  
  39. Note: if the requested padding is 0 along all axes, then this algorithm
  40. returns the original input ndarray.
  41.  
  42. Author: Stian Lode stian.lode@gmail.com
  43.  
  44. Args:
  45. values: n-dimensional ndarray
  46. window: an iterable of length n
  47.  
  48. return:
  49. a numpy ndarray containing the values with each axis padded according
  50. to the specified window. The padding is a reflection of the data in
  51. the input values.
  52. """
  53. assert len(values.shape) == len(window)
  54.  
  55. if (window <= 0).all():
  56. return values
  57.  
  58. start, end = edges(window)
  59. new = np.empty(values.shape + window, dtype=dtype)
  60.  
  61. slice_stack = []
  62. for a, b in zip(start, end):
  63. slice_stack.append(slice(a, None if b == 0 else -b))
  64.  
  65. new[tuple(slice_stack)] = values
  66.  
  67. slice_stack = []
  68. for a,b in zip(start, end):
  69. if a > 0:
  70. s_to, s_from = slice(a - 1, None, -1), slice(a, 2 * a, None)
  71. new[tuple(slice_stack + [s_to])] = new[tuple(slice_stack + [s_from])]
  72.  
  73. if b > 0:
  74. e_to, e_from = slice(-1, -b-1, -1), slice(-2 * b, -b)
  75. new[tuple(slice_stack + [e_to])] = new[tuple(slice_stack + [e_from])]
  76.  
  77. slice_stack.append(slice(None))
  78.  
  79. return new
  80.  
  81. def numpypad(values, window):
  82. s, e = edges(window)
  83. return np.pad(values, zip(s, e), mode='symmetric')
  84.  
  85. import time
  86. for N in [100, 200, 300, 400, 500, 600]:
  87. values = np.arange(N*N*N, dtype='f8').reshape(N,N,N)
  88. window = np.array((8,8,8), dtype='i')
  89.  
  90. to = time.clock()
  91. a = numpypad(values, window)
  92. print("numpypad {} {}".format(N, time.clock()-to))
  93.  
  94. to = time.clock()
  95. b = fast_pad_symmetric(values, window)
  96. print("fast_pad_symmetric {} {}".format(N, time.clock()-to))
  97.  
  98. import gc; gc.collect()
  99. assert (a==b).all()
Add Comment
Please, Sign In to add comment