Given a np.npdarray with nDims > 2, reduce it to 2d. It does this by selecting the zeroth coordinate for every dimension greater than two. Args: arr: a numpy ndarray of dimension at least 2. Returns: A two-dimensional subarray from the input array. Raises: V
(arr)
| 126 | |
| 127 | |
| 128 | def reduce_to_2d(arr): |
| 129 | """Given a np.npdarray with nDims > 2, reduce it to 2d. |
| 130 | |
| 131 | It does this by selecting the zeroth coordinate for every dimension greater |
| 132 | than two. |
| 133 | |
| 134 | Args: |
| 135 | arr: a numpy ndarray of dimension at least 2. |
| 136 | |
| 137 | Returns: |
| 138 | A two-dimensional subarray from the input array. |
| 139 | |
| 140 | Raises: |
| 141 | ValueError: If the argument is not a numpy ndarray, or the dimensionality |
| 142 | is too low. |
| 143 | """ |
| 144 | if not isinstance(arr, np.ndarray): |
| 145 | raise ValueError("reduce_to_2d requires a numpy.ndarray") |
| 146 | |
| 147 | ndims = len(arr.shape) |
| 148 | if ndims < 2: |
| 149 | raise ValueError("reduce_to_2d requires an array of dimensionality >=2") |
| 150 | # slice(None) is equivalent to `:`, so we take arr[0,0,...0,:,:] |
| 151 | slices = ([0] * (ndims - 2)) + [slice(None), slice(None)] |
| 152 | return arr[tuple(slices)] |
| 153 | |
| 154 | |
| 155 | def text_array_to_html(text_arr, enable_markdown): |
no outgoing calls
no test coverage detected
searching dependent graphs…