def test_values(self): shape = (3, 4, 5) value = 81 array = ConstantArray(shape, value) result = array.ndarray() expected = np.empty(shape) expected[()] = value np.testing.assert_array_equal(result, expected)
def test_repr(self): self.zeros = ConstantArray([10, 20, 40, 50]) self.a = TransposedArray(self.zeros, [1, 3, 0, 2]) expected = ("TransposedArray(<ConstantArray shape=(10, 20, 40, 50) " "dtype=dtype('float64')>, [1, 3, 0, 2])") self.assertEqual(repr(self.a), expected)
def test_multidim_stack_multidim(self): # Multidim stack of arrays shape (4, 6) arrays = np.array([[ConstantArray((), i) for i in range(6)] for i in range(4)]) msg = 'multidimensional stacks not yet supported' with self.assertRaisesRegexp(ValueError, msg): ArrayStack.multidim_array_stack(arrays, (3, 2, 4))
def test_have_enough_memory(self): # Using np.broadcast results in the actual data needing to be # realised. The code gets around this by using strides = 0. # Pick an array size which isn't realistic to realise in a # full array. a = ConstantArray([int(10 ** i) for i in range(4, 12)]) self.assertEqual(BroadcastArray._compute_broadcast_kwargs(a.shape, a.shape)[0], tuple(int(10 ** i) for i in range(4, 12)), )
def test_value(self): value = 6 array = ConstantArray(3, value) self.assertEqual(array.value, value)
def test_shape_invalid_scalar(self): with self.assertRaises(ValueError): array = ConstantArray('foo')
def test_shape_scalar_like_int(self): array = ConstantArray('34') self.assertEqual(array.shape, (34,))
def test_shape_scalar(self): shape = 5 array = ConstantArray(shape) self.assertEqual(array.shape, (shape,))
def test_dtype(self): shape = (3, 4, 5) array = ConstantArray(shape, dtype='i4') result = array.ndarray() self.assertEqual(result.dtype, np.dtype('i4'))
def test_masked(self): shape = (3, 4, 5) array = ConstantArray(shape) result = array.masked_array() self.assertTrue(np.ma.isMaskedArray(result))
def test_values_default(self): shape = (3, 4, 5) array = ConstantArray(shape) result = array.ndarray() np.testing.assert_array_equal(result, np.zeros(shape))
def test_values(self): shape = (3, 4, 5) array = ConstantArray(shape) result = array.masked_array() np.testing.assert_array_equal(result, np.ma.zeros(shape))
def transposed_shape(self, shape, axes): return TransposedArray(ConstantArray(shape), axes).shape
def test_dtype_default(self): array = ConstantArray(()) self.assertEqual(array.dtype, np.dtype('f8'))
def test_indexing_slice(self): shape = (30, 10, 20) array = ConstantArray(shape) result = array[:5] data = np.zeros(shape)[:5] self.assertEqual(result.shape, data.shape)
def test_dtype(self): shape = (3, 4, 5) array = ConstantArray(shape, dtype="i4") result = array.ndarray() self.assertEqual(result.dtype, np.dtype("i4"))
def test_dtype(self): shape = (3, 4, 5) array = ConstantArray(shape, dtype='i4') result = array.masked_array() self.assertEqual(result.dtype, np.dtype('i4'))
def test_dtype_default(self): shape = (3, 4, 5) array = ConstantArray(shape) result = array.ndarray() self.assertEqual(result.dtype, np.dtype("f8"))
def test_dtype_default(self): shape = (3, 4, 5) array = ConstantArray(shape) result = array.ndarray() self.assertEqual(result.dtype, np.dtype('f8'))
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)
def test_shape_tuple(self): shape = (30, 10, 20) array = ConstantArray(shape) self.assertEqual(array.shape, shape)
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_shape_tuple_like_int(self): array = ConstantArray(('34', '93')) self.assertEqual(array.shape, (34, 93))
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_shape_invalid_tuple(self): with self.assertRaises(ValueError): array = ConstantArray(('foo', 'bar'))
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_dtype(self): dtype = 'i2' array = ConstantArray((), dtype=dtype) self.assertIs(array.dtype, np.dtype(dtype))
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_dtype_default_integer(self): array = ConstantArray((), 42) self.assertEqual(array.dtype, np.dtype(np.int_))
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_newaxis(self): array = ConstantArray([2, 3]) result = array[:5, np.newaxis] self.assertEqual(result.shape, (2, 1, 3))
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_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))