|
17 | 17 | # Private utility functions. |
18 | 18 |
|
19 | 19 |
|
20 | | -def _linear_ramp(ndim, axis, start, stop, size, reverse=False): |
21 | | - """ |
22 | | - Create a linear ramp of `size` in `axis` with `ndim`. |
23 | | -
|
24 | | - This algorithm behaves like a vectorized version of `numpy.linspace`. |
25 | | - The resulting linear ramp is broadcastable to any array that matches the |
26 | | - ramp in `shape[axis]` and `ndim`. |
27 | | -
|
28 | | - Parameters |
29 | | - ---------- |
30 | | - ndim : int |
31 | | - Number of dimensions of the resulting array. All dimensions except |
32 | | - the one specified by `axis` will have the size 1. |
33 | | - axis : int |
34 | | - The dimension that contains the linear ramp of `size`. |
35 | | - start : int or ndarray |
36 | | - The starting value(s) of the linear ramp. If given as an array, its |
37 | | - size must match `size`. |
38 | | - stop : int or ndarray |
39 | | - The stop value(s) (not included!) of the linear ramp. If given as an |
40 | | - array, its size must match `size`. |
41 | | - size : int |
42 | | - The number of elements in the linear ramp. If this argument is 0 the |
43 | | - dimensions of `ramp` will all be of length 1 except for the one given |
44 | | - by `axis` which will be 0. |
45 | | - reverse : bool |
46 | | - If False, increment in a positive fashion, otherwise decrement. |
47 | | -
|
48 | | - Returns |
49 | | - ------- |
50 | | - ramp : ndarray |
51 | | - Output array of dtype np.float64 that in- or decrements along the given |
52 | | - `axis`. |
53 | | -
|
54 | | - Examples |
55 | | - -------- |
56 | | - >>> _linear_ramp(ndim=2, axis=0, start=np.arange(3), stop=10, size=2) |
57 | | - array([[0. , 1. , 2. ], |
58 | | - [5. , 5.5, 6. ]]) |
59 | | - >>> _linear_ramp(ndim=3, axis=0, start=2, stop=0, size=0) |
60 | | - array([], shape=(0, 1, 1), dtype=float64) |
61 | | - """ |
62 | | - # Create initial ramp |
63 | | - ramp = np.arange(size, dtype=np.float64) |
64 | | - if reverse: |
65 | | - ramp = ramp[::-1] |
66 | | - |
67 | | - # Make sure, that ramp is broadcastable |
68 | | - init_shape = (1,) * axis + (size,) + (1,) * (ndim - axis - 1) |
69 | | - ramp = ramp.reshape(init_shape) |
70 | | - |
71 | | - if size != 0: |
72 | | - # And scale to given start and stop values |
73 | | - gain = (stop - start) / float(size) |
74 | | - ramp = ramp * gain |
75 | | - ramp += start |
76 | | - |
77 | | - return ramp |
78 | | - |
79 | | - |
80 | 20 | def _round_if_needed(arr, dtype): |
81 | 21 | """ |
82 | 22 | Rounds arr inplace if destination dtype is integer. |
@@ -269,17 +209,25 @@ def _get_linear_ramps(padded, axis, width_pair, end_value_pair): |
269 | 209 | """ |
270 | 210 | edge_pair = _get_edges(padded, axis, width_pair) |
271 | 211 |
|
272 | | - left_ramp = _linear_ramp( |
273 | | - padded.ndim, axis, start=end_value_pair[0], stop=edge_pair[0], |
274 | | - size=width_pair[0], reverse=False |
| 212 | + left_ramp = np.linspace( |
| 213 | + start=end_value_pair[0], |
| 214 | + stop=edge_pair[0].squeeze(axis), # Dimensions is replaced by linspace |
| 215 | + num=width_pair[0], |
| 216 | + endpoint=False, |
| 217 | + dtype=padded.dtype, |
| 218 | + axis=axis, |
275 | 219 | ) |
276 | | - _round_if_needed(left_ramp, padded.dtype) |
277 | 220 |
|
278 | | - right_ramp = _linear_ramp( |
279 | | - padded.ndim, axis, start=end_value_pair[1], stop=edge_pair[1], |
280 | | - size=width_pair[1], reverse=True |
| 221 | + right_ramp = np.linspace( |
| 222 | + start=end_value_pair[1], |
| 223 | + stop=edge_pair[1].squeeze(axis), # Dimension is replaced by linspace |
| 224 | + num=width_pair[1], |
| 225 | + endpoint=False, |
| 226 | + dtype=padded.dtype, |
| 227 | + axis=axis, |
281 | 228 | ) |
282 | | - _round_if_needed(right_ramp, padded.dtype) |
| 229 | + # Reverse linear space in appropriate dimension |
| 230 | + right_ramp = right_ramp[_slice_at_axis(slice(None, None, -1), axis)] |
283 | 231 |
|
284 | 232 | return left_ramp, right_ramp |
285 | 233 |
|
|
0 commit comments