Advertisement
Dmitrey15

tests for hstack/vstack/dstack

Jan 26th, 2012
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. def test_hstack(self):
  2.     import numpypy as np
  3.     a = np.array((1,2,3))
  4.     b = np.array((2,3,4))
  5.     assert all(np.hstack((a,b))== np.array([1, 2, 3, 2, 3, 4]))
  6.     a = np.array([[1],[2],[3]])
  7.     b = np.array([[2],[3],[4]])
  8.     assert np.all(np.hstack((a,b)) == np.array([
  9.                                                 [1, 2],
  10.                                                 [2, 3],
  11.                                                 [3, 4]
  12.                                                 ]))
  13.     for shape1, shape2 in [[(1, 2), (1, 3)], [(4, 2), [4, 3]]]:
  14.         a, b = np.ones(shape1), np.ones(shape2)
  15.         assert np.all(np.hstack((a, b)) == np.ones((a.shape[0], a.shape[1]+b.shape[1])))
  16.     for shape1, shape2 in [[(2, 3, 4), (2, 7, 4)], [(1, 4, 7), (1, 10, 7)], [(1, 4, 7), (1, 0, 7)], [(1, 0, 7), (1, 0, 7)]]:
  17.         a, b = np.ones(shape1), np.ones(shape2)
  18.         assert np.all(np.hstack((a, b)) == np.ones((a.shape[0], a.shape[1]+b.shape[1], a.shape[2])))
  19.  
  20. def test_vstack(self):
  21.     import numpypy as np
  22.     a = np.array([1, 2, 3])
  23.     b = np.array([2, 3, 4])
  24.     assert np.all(np.vstack((a,b)) == np.array([
  25.                                           [1, 2, 3],
  26.                                           [2, 3, 4]
  27.                                           ]))
  28.                                          
  29.     a = np.array([[1], [2], [3]])
  30.     b = np.array([[2], [3], [4]])
  31.     assert np.all(np.vstack((a,b)) == np.array([
  32.                                              [1],
  33.                                              [2],
  34.                                              [3],
  35.                                              [2],
  36.                                              [3],
  37.                                              [4]
  38.                                              ]))
  39.  
  40.     for shape1, shape2 in [[(2, 1), (3, 1)], [(2, 4), [3, 4]]]:
  41.         a, b = np.ones(shape1), np.ones(shape2)
  42.         assert np.all(np.vstack((a, b)) == np.ones((a.shape[0]+b.shape[0], a.shape[1])))
  43.     for shape1, shape2 in [[(3, 2, 4), (7, 2, 4)], [(0, 2, 7), (10, 2, 7)], [(0, 2, 7), (0, 2, 7)]]:
  44.         a, b = np.ones(shape1), np.ones(shape2)
  45.         assert np.all(np.vstack((a, b)) == np.ones((a.shape[0]+b.shape[0], a.shape[1], a.shape[2])))
  46.  
  47. def test_dstack(self):
  48.     import numpypy as np
  49.     a = np.array((1,2,3))
  50.     b = np.array((2,3,4))
  51.     assert np.all(np.dstack((a,b)) == np.array([[
  52.                                            [1, 2],
  53.                                            [2, 3],
  54.                                            [3, 4]
  55.                                            ]]))
  56.     a = np.array([[1],[2],[3]])
  57.     b = np.array([[2],[3],[4]])
  58.     assert np.all(np.dstack((a,b)) == np.array([
  59.                                           [[1, 2]],
  60.                                           [[2, 3]],
  61.                                           [[3, 4]]
  62.                                           ]))
  63.     for shape1, shape2 in [[(4, 2, 3), (4, 2, 7)], [(7, 2, 0), (7, 2, 10)], [(7, 2, 0), (7, 2, 0)]]:
  64.         a, b = np.ones(shape1), np.ones(shape2)
  65.         assert np.all(np.dstack((a, b)) == np.ones((a.shape[0], a.shape[1], a.shape[2]+b.shape[2])))
  66.        
  67.     for shape1, shape2 in [[(4, 2, 3, 5), (4, 2, 7, 5)], [(7, 2, 0, 5), (7, 2, 10, 5)], [(7, 2, 0, 5), (7, 2, 0, 5)]]:
  68.         a, b = np.ones(shape1), np.ones(shape2)
  69.         assert np.all(np.dstack((a, b)) == np.ones((a.shape[0], a.shape[1], a.shape[2]+b.shape[2], a.shape[3])))
  70.        
  71.  
  72.  
  73. test_hstack(1)
  74. test_vstack(1)
  75. test_dstack(1)
  76. print('passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement