MCPcopy Create free account
hub / github.com/numpy/numpy / iter_random_view_pairs

Function iter_random_view_pairs

numpy/core/tests/test_mem_overlap.py:234–319  ·  view source on GitHub ↗
(x, same_steps=True, equal_size=False)

Source from the content-addressed store, hash-verified

232
233
234def iter_random_view_pairs(x, same_steps=True, equal_size=False):
235 rng = np.random.RandomState(1234)
236
237 if equal_size and same_steps:
238 raise ValueError()
239
240 def random_slice(n, step):
241 start = rng.randint(0, n+1, dtype=np.intp)
242 stop = rng.randint(start, n+1, dtype=np.intp)
243 if rng.randint(0, 2, dtype=np.intp) == 0:
244 stop, start = start, stop
245 step *= -1
246 return slice(start, stop, step)
247
248 def random_slice_fixed_size(n, step, size):
249 start = rng.randint(0, n+1 - size*step)
250 stop = start + (size-1)*step + 1
251 if rng.randint(0, 2) == 0:
252 stop, start = start-1, stop-1
253 if stop < 0:
254 stop = None
255 step *= -1
256 return slice(start, stop, step)
257
258 # First a few regular views
259 yield x, x
260 for j in range(1, 7, 3):
261 yield x[j:], x[:-j]
262 yield x[...,j:], x[...,:-j]
263
264 # An array with zero stride internal overlap
265 strides = list(x.strides)
266 strides[0] = 0
267 xp = as_strided(x, shape=x.shape, strides=strides)
268 yield x, xp
269 yield xp, xp
270
271 # An array with non-zero stride internal overlap
272 strides = list(x.strides)
273 if strides[0] > 1:
274 strides[0] = 1
275 xp = as_strided(x, shape=x.shape, strides=strides)
276 yield x, xp
277 yield xp, xp
278
279 # Then discontiguous views
280 while True:
281 steps = tuple(rng.randint(1, 11, dtype=np.intp)
282 if rng.randint(0, 5, dtype=np.intp) == 0 else 1
283 for j in range(x.ndim))
284 s1 = tuple(random_slice(p, s) for p, s in zip(x.shape, steps))
285
286 t1 = np.arange(x.ndim)
287 rng.shuffle(t1)
288
289 if equal_size:
290 t2 = t1
291 else:

Callers 3

check_unary_fuzzMethod · 0.85

Calls 4

as_stridedFunction · 0.90
random_sliceFunction · 0.85
random_slice_fixed_sizeFunction · 0.85
maxFunction · 0.50

Tested by

no test coverage detected