def test_nested_newaxes(self): # Currently it is possible to nest NewAxesArrays. It would be # nice if this weren't the case. orig = np.empty([3]) array1 = NewAxesArray(orig, [1, 0]) array2 = NewAxesArray(array1, [0, 0, 2]) self.assertEqual(array2.shape, (1, 3, 1, 1)) self.assertIs(array2.array, array1)
def test_0d(self): array = NewAxesArray(np.array(0), [3]) self.assertEqual(array.shape, (1, 1, 1))
def test_right_only_new_axes(self): in_arr = ConstantArray([3, 2]) array = NewAxesArray(in_arr, [0, 0, 2]) self.assertEqual(array.shape, (3, 2, 1, 1))
def test_left_only_new_axes(self): in_arr = ConstantArray([3, 2]) array = NewAxesArray(in_arr, [1, 0, 0]) self.assertEqual(array.shape, (1, 3, 2))
def test_many_new_axes(self): in_arr = ConstantArray([3, 2]) array = NewAxesArray(in_arr, [3, 1, 2]) self.assertEqual(array.shape, (1, 1, 1, 3, 1, 2, 1, 1))
def test_no_new_axes(self): in_arr = ConstantArray([3, 1]) array = NewAxesArray(in_arr, [0, 0, 0]) self.assertEqual(array.shape, (3, 1))
def test_successful_attribute(self): in_arr = ConstantArray([3, 1]) array = NewAxesArray(in_arr, [2, 0, 4]) self.assertIs(array.array, in_arr) self.assertIsInstance(array._new_axes, np.ndarray) self.assertEqual(list(array._new_axes), [2, 0, 4])
def test_0d(self): array = NewAxesArray(np.array(0), [3]) self.assertEqual(list(array._new_axes), [3])
def test_too_many_axes(self): in_arr = ConstantArray([3, 2]) msg = 'must have length 3 but was actually length 4' with self.assertRaisesRegexp(ValueError, msg): array = NewAxesArray(in_arr, [1, 2, 0, 1])
def test_new_axes_negative(self): in_arr = ConstantArray([1, 2]) msg = 'Only positive integer types may be used for new_axes.' with self.assertRaisesRegexp(ValueError, msg): array = NewAxesArray(in_arr, [-1, 0, 1])
def test_numpy_array(self): array = NewAxesArray(np.arange(24), [1, 2]) self.assertIsInstance(array.masked_array(), np.ma.MaskedArray) assert_array_equal(array.masked_array(), np.arange(24).reshape(1, 24, 1, 1))
def setUp(self): self.array = NewAxesArray(np.arange(24).reshape(4, 3, 2), [1, 2, 0, 1]) self.array_3d = NewAxesArray(np.arange(3), [1, 1])
def assert_newaxis_keys(self, shape, new_axes, expected): in_arr = ConstantArray(shape) array = NewAxesArray(in_arr, new_axes) keys = array._newaxis_keys() self.assertEqual(keys, expected)