Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def newchoose(a, choices, out=None, mode='raise'):
  4. """
  5. Construct an array from an index array and a set of arrays to choose from.
  6.  
  7. First of all, if confused or uncertain, definitely look at the Examples -
  8. in its full generality, this function is less simple than it might
  9. seem from the following code description (below ndi =
  10. `numpy.lib.index_tricks`):
  11.  
  12. ``np.choose(a,c) == np.array([c[a[I]][I] for I in ndi.ndindex(a.shape)])``.
  13.  
  14. But this omits some subtleties. Here is a fully general summary:
  15.  
  16. Given an "index" array (`a`) of integers and a sequence of `n` arrays
  17. (`choices`), `a` and each choice array are first broadcast, as necessary,
  18. to arrays of a common shape; calling these *Ba* and *Bchoices[i], i =
  19. 0,...,n-1* we have that, necessarily, ``Ba.shape == Bchoices[i].shape``
  20. for each `i`. Then, a new array with shape ``Ba.shape`` is created as
  21. follows:
  22.  
  23. * if ``mode=raise`` (the default), then, first of all, each element of
  24. `a` (and thus `Ba`) must be in the range `[0, n-1]`; now, suppose that
  25. `i` (in that range) is the value at the `(j0, j1, ..., jm)` position
  26. in `Ba` - then the value at the same position in the new array is the
  27. value in `Bchoices[i]` at that same position;
  28.  
  29. * if ``mode=wrap``, values in `a` (and thus `Ba`) may be any (signed)
  30. integer; modular arithmetic is used to map integers outside the range
  31. `[0, n-1]` back into that range; and then the new array is constructed
  32. as above;
  33.  
  34. * if ``mode=clip``, values in `a` (and thus `Ba`) may be any (signed)
  35. integer; negative integers are mapped to 0; values greater than `n-1`
  36. are mapped to `n-1`; and then the new array is constructed as above.
  37.  
  38. Parameters
  39. ----------
  40. a : int array
  41. This array must contain integers in `[0, n-1]`, where `n` is the number
  42. of choices, unless ``mode=wrap`` or ``mode=clip``, in which cases any
  43. integers are permissible.
  44. choices : sequence of arrays
  45. Choice arrays. `a` and all of the choices must be broadcastable to the
  46. same shape. If `choices` is itself an array (not recommended), then
  47. its outermost dimension (i.e., the one corresponding to
  48. ``choices.shape[0]``) is taken as defining the "sequence".
  49. out : array, optional
  50. If provided, the result will be inserted into this array. It should
  51. be of the appropriate shape and dtype.
  52. mode : {'raise' (default), 'wrap', 'clip'}, optional
  53. Specifies how indices outside `[0, n-1]` will be treated:
  54.  
  55. * 'raise' : an exception is raised
  56. * 'wrap' : value becomes value mod `n`
  57. * 'clip' : values < 0 are mapped to 0, values > n-1 are mapped to n-1
  58.  
  59. Returns
  60. -------
  61. merged_array : array
  62. The merged result.
  63.  
  64. Raises
  65. ------
  66. ValueError: shape mismatch
  67. If `a` and each choice array are not all broadcastable to the same
  68. shape.
  69.  
  70. See Also
  71. --------
  72. ndarray.choose : equivalent method
  73.  
  74. Notes
  75. -----
  76. To reduce the chance of misinterpretation, even though the following
  77. "abuse" is nominally supported, `choices` should neither be, nor be
  78. thought of as, a single array, i.e., the outermost sequence-like container
  79. should be either a list or a tuple.
  80.  
  81. Examples
  82. --------
  83.  
  84. >>> choices = [[0, 1, 2, 3], [10, 11, 12, 13],
  85. ... [20, 21, 22, 23], [30, 31, 32, 33]]
  86. >>> np.choose([2, 3, 1, 0], choices
  87. ... # the first element of the result will be the first element of the
  88. ... # third (2+1) "array" in choices, namely, 20; the second element
  89. ... # will be the second element of the fourth (3+1) choice array, i.e.,
  90. ... # 31, etc.
  91. ... )
  92. array([20, 31, 12, 3])
  93. >>> np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1)
  94. array([20, 31, 12, 3])
  95. >>> # because there are 4 choice arrays
  96. >>> np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4)
  97. array([20, 1, 12, 3])
  98. >>> # i.e., 0
  99.  
  100. A couple examples illustrating how choose broadcasts:
  101.  
  102. >>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
  103. >>> choices = [-10, 10]
  104. >>> np.choose(a, choices)
  105. array([[ 10, -10, 10],
  106. [-10, 10, -10],
  107. [ 10, -10, 10]])
  108.  
  109. >>> # With thanks to Anne Archibald
  110. >>> a = np.array([0, 1]).reshape((2,1,1))
  111. >>> c1 = np.array([1, 2, 3]).reshape((1,3,1))
  112. >>> c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5))
  113. >>> np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2
  114. array([[[ 1, 1, 1, 1, 1],
  115. [ 2, 2, 2, 2, 2],
  116. [ 3, 3, 3, 3, 3]],
  117. [[-1, -2, -3, -4, -5],
  118. [-1, -2, -3, -4, -5],
  119. [-1, -2, -3, -4, -5]]])
  120.  
  121. """
  122.  
  123. # First ensure everything is an array
  124. choices = [np.asarray(choice) for choice in choices]
  125. a = np.asarray(a)
  126.  
  127. # Store the number of choices. FIXME: is this actually a good idea?
  128. nchoices = len(choices)
  129.  
  130. # Broadcast arrays together
  131. ars = np.broadcast_arrays(a,*choices)
  132. a = ars[0]; choices = ars[1:]
  133.  
  134. # Configure output if it isn't provided
  135. if out is None:
  136. dtype = np.result_type(*choices)
  137. out = np.empty(a.shape,dtype=dtype)
  138.  
  139. # Raise mode
  140. if mode[0]=='r' or mode[0]=='R':
  141. if not np.all( (a>=0) & (a<nchoices) ):
  142. raise ValueError("invalid choices in entry array")
  143. for choice, i in zip( choices, range(0,nchoices) ):
  144. np.copyto(out, choice, where=(a==i))
  145.  
  146. # Wrap mode
  147. elif mode[0]=='w' or mode[0]=='W':
  148. a = a % nchoices
  149. for choice, i in zip( choices, range(0,nchoices) ):
  150. np.copyto(out, choice, where=(a==i))
  151.  
  152. # Clip mode
  153. elif mode[0]=='c' or mode[0]=='C':
  154. for choice, i in zip( choices, range(0,nchoices) ):
  155. if i==0:
  156. np.copyto(out, choice, where=(a<=i))
  157. elif i==nchoices-1:
  158. np.copyto(out, choice, where=(a>=i))
  159. else:
  160. np.copyto(out, choice, where=(a==i))
  161.  
  162. # Invalid mode
  163. else:
  164. raise TypeError("clipmode not understood")
  165.  
  166. return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement