MCPcopy Index your code
hub / github.com/numpy/numpy / test_stack

Function test_stack

numpy/_core/tests/test_shape_base.py:463–528  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

461
462
463def test_stack():
464 # non-iterable input
465 assert_raises(TypeError, stack, 1)
466
467 # 0d input
468 for input_ in [(1, 2, 3),
469 [np.int32(1), np.int32(2), np.int32(3)],
470 [np.array(1), np.array(2), np.array(3)]]:
471 assert_array_equal(stack(input_), [1, 2, 3])
472 # 1d input examples
473 a = np.array([1, 2, 3])
474 b = np.array([4, 5, 6])
475 r1 = array([[1, 2, 3], [4, 5, 6]])
476 assert_array_equal(np.stack((a, b)), r1)
477 assert_array_equal(np.stack((a, b), axis=1), r1.T)
478 # all input types
479 assert_array_equal(np.stack([a, b]), r1)
480 assert_array_equal(np.stack(array([a, b])), r1)
481 # all shapes for 1d input
482 arrays = [np.random.randn(3) for _ in range(10)]
483 axes = [0, 1, -1, -2]
484 expected_shapes = [(10, 3), (3, 10), (3, 10), (10, 3)]
485 for axis, expected_shape in zip(axes, expected_shapes):
486 assert_equal(np.stack(arrays, axis).shape, expected_shape)
487 assert_raises_regex(AxisError, 'out of bounds', stack, arrays, axis=2)
488 assert_raises_regex(AxisError, 'out of bounds', stack, arrays, axis=-3)
489 # all shapes for 2d input
490 arrays = [np.random.randn(3, 4) for _ in range(10)]
491 axes = [0, 1, 2, -1, -2, -3]
492 expected_shapes = [(10, 3, 4), (3, 10, 4), (3, 4, 10),
493 (3, 4, 10), (3, 10, 4), (10, 3, 4)]
494 for axis, expected_shape in zip(axes, expected_shapes):
495 assert_equal(np.stack(arrays, axis).shape, expected_shape)
496 # empty arrays
497 assert_(stack([[], [], []]).shape == (3, 0))
498 assert_(stack([[], [], []], axis=1).shape == (0, 3))
499 # out
500 out = np.zeros_like(r1)
501 np.stack((a, b), out=out)
502 assert_array_equal(out, r1)
503 # edge cases
504 assert_raises_regex(ValueError, 'need at least one array', stack, [])
505 assert_raises_regex(ValueError, 'must have the same shape',
506 stack, [1, np.arange(3)])
507 assert_raises_regex(ValueError, 'must have the same shape',
508 stack, [np.arange(3), 1])
509 assert_raises_regex(ValueError, 'must have the same shape',
510 stack, [np.arange(3), 1], axis=1)
511 assert_raises_regex(ValueError, 'must have the same shape',
512 stack, [np.zeros((3, 3)), np.zeros(3)], axis=1)
513 assert_raises_regex(ValueError, 'must have the same shape',
514 stack, [np.arange(2), np.arange(3)])
515
516 # do not accept generators
517 with pytest.raises(TypeError, match="arrays to stack must be"):
518 stack(x for x in range(3))
519
520 # casting and dtype test

Callers

nothing calls this directly

Calls 8

assert_raisesFunction · 0.90
assert_array_equalFunction · 0.90
stackFunction · 0.90
arrayFunction · 0.90
assert_equalFunction · 0.90
assert_raises_regexFunction · 0.90
assert_Function · 0.90
zeros_likeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…