Generate cartesian product of strides for all axes
(x)
| 274 | # Generate stride combination variations of the above |
| 275 | # |
| 276 | def _stride_comb_iter(x): |
| 277 | """ |
| 278 | Generate cartesian product of strides for all axes |
| 279 | """ |
| 280 | |
| 281 | if not isinstance(x, np.ndarray): |
| 282 | yield x, "nop" |
| 283 | return |
| 284 | |
| 285 | stride_set = [(1,)] * x.ndim |
| 286 | stride_set[-1] = (1, 3, -4) |
| 287 | if x.ndim > 1: |
| 288 | stride_set[-2] = (1, 3, -4) |
| 289 | if x.ndim > 2: |
| 290 | stride_set[-3] = (1, -4) |
| 291 | |
| 292 | for repeats in itertools.product(*tuple(stride_set)): |
| 293 | new_shape = [abs(a * b) for a, b in zip(x.shape, repeats)] |
| 294 | slices = tuple([slice(None, None, repeat) for repeat in repeats]) |
| 295 | |
| 296 | # new array with different strides, but same data |
| 297 | xi = np.empty(new_shape, dtype=x.dtype) |
| 298 | xi.view(np.uint32).fill(0xdeadbeef) |
| 299 | xi = xi[slices] |
| 300 | xi[...] = x |
| 301 | xi = xi.view(x.__class__) |
| 302 | assert_(np.all(xi == x)) |
| 303 | yield xi, "stride_" + "_".join(["%+d" % j for j in repeats]) |
| 304 | |
| 305 | # generate also zero strides if possible |
| 306 | if x.ndim >= 1 and x.shape[-1] == 1: |
| 307 | s = list(x.strides) |
| 308 | s[-1] = 0 |
| 309 | xi = np.lib.stride_tricks.as_strided(x, strides=s) |
| 310 | yield xi, "stride_xxx_0" |
| 311 | if x.ndim >= 2 and x.shape[-2] == 1: |
| 312 | s = list(x.strides) |
| 313 | s[-2] = 0 |
| 314 | xi = np.lib.stride_tricks.as_strided(x, strides=s) |
| 315 | yield xi, "stride_xxx_0_x" |
| 316 | if x.ndim >= 2 and x.shape[:-2] == (1, 1): |
| 317 | s = list(x.strides) |
| 318 | s[-1] = 0 |
| 319 | s[-2] = 0 |
| 320 | xi = np.lib.stride_tricks.as_strided(x, strides=s) |
| 321 | yield xi, "stride_xxx_0_0" |
| 322 | |
| 323 | |
| 324 | def _make_strided_cases(): |