def test_check_convert_shape(self): #TODO: check if shape is correct everywhere. #Correct input --------------------------------------------- #Recognize correct shape #Input is array, shape (3,), single legal shape arr = _check_convert_array(array([1., 2, 3]), [(3, )], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is array, shape (3,), two legal shapes arr = _check_convert_array(array([1., 2, 3]), [(3, ), (1, 3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is array, 2D, shape (1,3) arr = _check_convert_array(array([[1., 2, 3]]), [(3, ), (1, 3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Test special value any #Input is array, 2D, shape (1,3) arr = _check_convert_array(array([[1., 2, 3]]), [(4, ), (1, "any")], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is array, 2D, shape (3,1) arr = _check_convert_array(array([[1.], [2], [3]]), [(4, ), ("any", 1)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Convert array-like objects to arrays #Input is matrix, shape (1,3), must convert to array arr = _check_convert_array(matrix("1. 2 3"), [(3, ), (1, 3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is list, shape (1,3), must convert to array arr = _check_convert_array([[1., 2, 3]], [(3, ), (1, 3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Special treatment of scalars and zero dimensional arrays: #They are converted to an array of a legal shape, filled with the scalar #value arr = _check_convert_array(5, [(3, ), (1, 3)], 'Test: ') assert isinstance(arr, np.ndarray) assert arr.shape == (3, ) assert_array_almost_equal(arr, [5, 5, 5]) #Squeeze shape #Input is array, 2D, shape (1,3) arr = _check_convert_array(array([[1., 2, 3]]), [(3, ), (1, 3)], 'Test: ', squeeze=True) assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) assert arr.shape == (3, ) #Shape must be squeezed. (1,3) -> (3,) #Erroneous input ----------------------------------------------------- #test wrong element data types #Input is array of functions, 2D, shape (1,3) self.assertRaises( TypeError, _check_convert_array(array([[min, max, all]]), [(3, ), (1, 3)], 'Test: ', squeeze=True)) #Test wrong shapes #Input has shape (4,) but (3,) or (1,3) are legal shapes self.assertRaises( ValueError, _check_convert_array(array([1., 2, 3, 4]), [(3, ), (1, 3)], 'Test: '))
def test_check_convert_shape(self): #TODO: check if shape is correct everywhere. #Correct input --------------------------------------------- #Recognize correct shape #Input is array, shape (3,), single legal shape arr = _check_convert_array(array([1., 2, 3]), [(3,)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is array, shape (3,), two legal shapes arr = _check_convert_array(array([1., 2, 3]), [(3,), (1,3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is array, 2D, shape (1,3) arr = _check_convert_array(array([[1., 2, 3]]), [(3,), (1,3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Test special value any #Input is array, 2D, shape (1,3) arr = _check_convert_array(array([[1., 2, 3]]), [(4,), (1,"any")], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is array, 2D, shape (3,1) arr = _check_convert_array(array([[1.], [2], [3]]), [(4,), ("any", 1)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Convert array-like objects to arrays #Input is matrix, shape (1,3), must convert to array arr = _check_convert_array(matrix("1. 2 3"), [(3,), (1,3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Input is list, shape (1,3), must convert to array arr = _check_convert_array([[1., 2, 3]], [(3,), (1,3)], 'Test: ') assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) #Special treatment of scalars and zero dimensional arrays: #They are converted to an array of a legal shape, filled with the scalar #value arr = _check_convert_array(5, [(3,), (1,3)], 'Test: ') assert isinstance(arr, np.ndarray) assert arr.shape == (3,) assert_array_almost_equal(arr, [5, 5, 5]) #Squeeze shape #Input is array, 2D, shape (1,3) arr = _check_convert_array(array([[1., 2, 3]]), [(3,), (1,3)], 'Test: ', squeeze=True) assert isinstance(arr, np.ndarray) assert not isinstance(arr, matrix) assert arr.shape == (3,) #Shape must be squeezed. (1,3) -> (3,) #Erroneous input ----------------------------------------------------- #test wrong element data types #Input is array of functions, 2D, shape (1,3) self.assertRaises(TypeError, _check_convert_array(array([[min, max, all]]), [(3,), (1,3)], 'Test: ', squeeze=True)) #Test wrong shapes #Input has shape (4,) but (3,) or (1,3) are legal shapes self.assertRaises(ValueError, _check_convert_array(array([1., 2, 3, 4]), [(3,), (1,3)], 'Test: '))