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

Function test_as_strided

numpy/lib/tests/test_stride_tricks.py:354–397  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

352
353
354def test_as_strided():
355 a = np.array([None])
356 a_view = as_strided(a)
357 expected = np.array([None])
358 assert_array_equal(a_view, np.array([None]))
359
360 a = np.array([1, 2, 3, 4])
361 a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,))
362 expected = np.array([1, 3])
363 assert_array_equal(a_view, expected)
364
365 a = np.array([1, 2, 3, 4])
366 a_view = as_strided(a, shape=(3, 4), strides=(0, 1 * a.itemsize))
367 expected = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
368 assert_array_equal(a_view, expected)
369
370 # Regression test for gh-5081
371 dt = np.dtype([('num', 'i4'), ('obj', 'O')])
372 a = np.empty((4,), dtype=dt)
373 a['num'] = np.arange(1, 5)
374 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
375 expected_num = [[1, 2, 3, 4]] * 3
376 expected_obj = [[None]*4]*3
377 assert_equal(a_view.dtype, dt)
378 assert_array_equal(expected_num, a_view['num'])
379 assert_array_equal(expected_obj, a_view['obj'])
380
381 # Make sure that void types without fields are kept unchanged
382 a = np.empty((4,), dtype='V4')
383 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
384 assert_equal(a.dtype, a_view.dtype)
385
386 # Make sure that the only type that could fail is properly handled
387 dt = np.dtype({'names': [''], 'formats': ['V4']})
388 a = np.empty((4,), dtype=dt)
389 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
390 assert_equal(a.dtype, a_view.dtype)
391
392 # Custom dtypes should not be lost (gh-9161)
393 r = [rational(i) for i in range(4)]
394 a = np.array(r, dtype=rational)
395 a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
396 assert_equal(a.dtype, a_view.dtype)
397 assert_array_equal([r] * 3, a_view)
398
399
400class TestSlidingWindowView:

Callers

nothing calls this directly

Calls 4

as_stridedFunction · 0.90
assert_array_equalFunction · 0.90
assert_equalFunction · 0.90
dtypeMethod · 0.45

Tested by

no test coverage detected