def test_hidden(self): shape = (2, ) a = self.array(shape, intent.hide, None) assert_(a.arr.shape == shape) assert_(a.arr_equal(a.arr, zeros(shape, dtype=self.type.dtype))) shape = (2, 3) a = self.array(shape, intent.hide, None) assert_(a.arr.shape == shape) assert_(a.arr_equal(a.arr, zeros(shape, dtype=self.type.dtype))) assert_(a.arr.flags['FORTRAN'] and not a.arr.flags['CONTIGUOUS']) shape = (2, 3) a = self.array(shape, intent.c.hide, None) assert_(a.arr.shape == shape) assert_(a.arr_equal(a.arr, zeros(shape, dtype=self.type.dtype))) assert_(not a.arr.flags['FORTRAN'] and a.arr.flags['CONTIGUOUS']) shape = (-1, 3) try: a = self.array(shape, intent.hide, None) except ValueError as msg: if not str(msg).startswith('failed to create intent' '(cache|hide)|optional array'): raise else: raise SystemError('intent(hide) should have failed' ' on undefined dimensions')
def test_mem_masked_where(self): # Ticket #62 from numpy1.ma import masked_where, MaskType a = np.zeros((1, 1)) b = np.zeros(a.shape, MaskType) c = masked_where(b, a) a - c
def test_scalar_return_type(self): # Full scalar indices should return scalars and object # arrays should not call PyArray_Return on their items class Zero(object): # The most basic valid indexing def __index__(self): return 0 z = Zero() class ArrayLike(object): # Simple array, should behave like the array def __array__(self): return np.array(0) a = np.zeros(()) assert_(isinstance(a[()], np.float_)) a = np.zeros(1) assert_(isinstance(a[z], np.float_)) a = np.zeros((1, 1)) assert_(isinstance(a[z, np.array(0)], np.float_)) assert_(isinstance(a[z, ArrayLike()], np.float_)) # And object arrays do not call it too often: b = np.array(0) a = np.array(0, dtype=object) a[()] = b assert_(isinstance(a[()], np.ndarray)) a = np.array([b, None]) assert_(isinstance(a[z], np.ndarray)) a = np.array([[b, None]]) assert_(isinstance(a[z, np.array(0)], np.ndarray)) assert_(isinstance(a[z, ArrayLike()], np.ndarray))
def test_dtype(self): dt = np.intc p = ndpointer(dtype=dt) assert_(p.from_param(np.array([1], dt))) dt = '<i4' p = ndpointer(dtype=dt) assert_(p.from_param(np.array([1], dt))) dt = np.dtype('>i4') p = ndpointer(dtype=dt) p.from_param(np.array([1], dt)) assert_raises(TypeError, p.from_param, np.array([1], dt.newbyteorder('swap'))) dtnames = ['x', 'y'] dtformats = [np.intc, np.float64] dtdescr = {'names': dtnames, 'formats': dtformats} dt = np.dtype(dtdescr) p = ndpointer(dtype=dt) assert_(p.from_param(np.zeros((10,), dt))) samedt = np.dtype(dtdescr) p = ndpointer(dtype=samedt) assert_(p.from_param(np.zeros((10,), dt))) dt2 = np.dtype(dtdescr, align=True) if dt.itemsize != dt2.itemsize: assert_raises(TypeError, p.from_param, np.zeros((10,), dt2)) else: assert_(p.from_param(np.zeros((10,), dt2)))
def test_broadcast_to_succeeds(): data = [ [np.array(0), (0, ), np.array(0)], [np.array(0), (1, ), np.zeros(1)], [np.array(0), (3, ), np.zeros(3)], [np.ones(1), (1, ), np.ones(1)], [np.ones(1), (2, ), np.ones(2)], [np.ones(1), (1, 2, 3), np.ones((1, 2, 3))], [np.arange(3), (3, ), np.arange(3)], [np.arange(3), (1, 3), np.arange(3).reshape(1, -1)], [np.arange(3), (2, 3), np.array([[0, 1, 2], [0, 1, 2]])], # test if shape is not a tuple [np.ones(0), 0, np.ones(0)], [np.ones(1), 1, np.ones(1)], [np.ones(1), 2, np.ones(2)], # these cases with size 0 are strange, but they reproduce the behavior # of broadcasting with ufuncs (see test_same_as_ufunc above) [np.ones(1), (0, ), np.ones(0)], [np.ones((1, 2)), (0, 2), np.ones((0, 2))], [np.ones((2, 1)), (2, 0), np.ones((2, 0))], ] for input_array, shape, expected in data: actual = broadcast_to(input_array, shape) assert_array_equal(expected, actual)
def test_empty(self): mat = np.zeros((0, 3)) for f in self.nanfuncs: for axis in [0, None]: assert_raises(ValueError, f, mat, axis=axis) for axis in [1]: res = f(mat, axis=axis) assert_equal(res, np.zeros(0))
def test_methods(self): "cases from methods.c" a = np.zeros((3, 3)) assert_raises(IndexError, lambda: a.item(100)) assert_raises(IndexError, lambda: a.itemset(100, 1)) a = np.zeros((0, 3)) assert_raises(IndexError, lambda: a.item(100)) assert_raises(IndexError, lambda: a.itemset(100, 1))
def test_put_exceptions(self): a = np.zeros((5, 5)) assert_raises(IndexError, a.put, 100, 0) a = np.zeros((5, 5), dtype=object) assert_raises(IndexError, a.put, 100, 0) a = np.zeros((5, 5, 0)) assert_raises(IndexError, a.put, 100, 0) a = np.zeros((5, 5, 0), dtype=object) assert_raises(IndexError, a.put, 100, 0)
def test_zeros0D(self): """Check creation of 0-dimensional objects""" h = np.zeros((), dtype=self._descr) assert_(normalize_descr(self._descr) == h.dtype.descr) assert_(h.dtype.fields['x'][0].name[:4] == 'void') assert_(h.dtype.fields['x'][0].char == 'V') assert_(h.dtype.fields['x'][0].type == np.void) # A small check that data is ok assert_equal(h['z'], np.zeros((), dtype='u1'))
def test_zerosMD(self): """Check creation of multi-dimensional objects""" h = np.zeros((2, 3), dtype=self._descr) assert_(normalize_descr(self._descr) == h.dtype.descr) assert_(h.dtype['z'].name == 'uint8') assert_(h.dtype['z'].char == 'B') assert_(h.dtype['z'].type == np.uint8) # A small check that data is ok assert_equal(h['z'], np.zeros((2, 3), dtype='u1'))
def test_lower_align(self): # check data that is not aligned to element size # i.e doubles are aligned to 4 bytes on i386 d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64) o = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64) assert_almost_equal(d + d, d * 2) np.add(d, d, out=o) np.add(np.ones_like(d), d, out=o) np.add(d, np.ones_like(d), out=o) np.add(np.ones_like(d), d) np.add(d, np.ones_like(d))
def test_take_from_object(self): # Check exception taking from object array d = np.zeros(5, dtype=object) assert_raises(IndexError, d.take, [6]) # Check exception taking from 0-d array d = np.zeros((5, 0), dtype=object) assert_raises(IndexError, d.take, [1], axis=1) assert_raises(IndexError, d.take, [0], axis=1) assert_raises(IndexError, d.take, [0]) assert_raises(IndexError, d.take, [0], mode='wrap') assert_raises(IndexError, d.take, [0], mode='clip')
def test_empty(self): for f, tgt_value in zip(self.nanfuncs, [0, 1]): mat = np.zeros((0, 3)) tgt = tgt_value*np.ones((0, 3)) res = f(mat, axis=0) assert_equal(res, tgt) tgt = mat res = f(mat, axis=1) assert_equal(res, tgt) tgt = np.zeros((0)) res = f(mat, axis=None) assert_equal(res, tgt)
def test_ptp(self): (x, X, XX, m, mx, mX, mXX,) = self.d (n, m) = X.shape assert_equal(mx.ptp(), mx.compressed().ptp()) rows = np.zeros(n, np.float_) cols = np.zeros(m, np.float_) for k in range(m): cols[k] = mX[:, k].compressed().ptp() for k in range(n): rows[k] = mX[k].compressed().ptp() assert_(eq(mX.ptp(0), cols)) assert_(eq(mX.ptp(1), rows))
def test_normal_types(self): for op in (operator.eq, operator.ne): # Broadcasting errors: self.assert_deprecated(op, args=(np.zeros(3), [])) a = np.zeros(3, dtype='i,i') # (warning is issued a couple of times here) self.assert_deprecated(op, args=(a, a[:-1]), num=None) # Element comparison error (numpy array can't be compared). a = np.array([1, np.array([1, 2, 3])], dtype=object) b = np.array([1, np.array([1, 2, 3])], dtype=object) self.assert_deprecated(op, args=(a, b), num=None)
def test_empty(self): mat = np.zeros((0, 3)) for axis in [0, None]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') assert_(np.isnan(np.nanpercentile(mat, 40, axis=axis)).all()) assert_(len(w) == 1) assert_(issubclass(w[0].category, RuntimeWarning)) for axis in [1]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') assert_equal(np.nanpercentile(mat, 40, axis=axis), np.zeros([])) assert_(len(w) == 0)
def test_boolean_assignment_needs_api(self): # See also gh-7666 # This caused a segfault on Python 2 due to the GIL not being # held when the iterator does not need it, but the transfer function # does arr = np.zeros(1000) indx = np.zeros(1000, dtype=bool) indx[:100] = True arr[indx] = np.ones(100, dtype=object) expected = np.zeros(1000) expected[:100] = 1 assert_array_equal(arr, expected)
def test_vector(self): vals = (100 * arange(5)).astype('l') b = zeros((5, 5)) for k in range(5): b[k, k] = vals[k] assert_equal(diag(vals), b) b = zeros((7, 7)) c = b.copy() for k in range(5): b[k, k + 2] = vals[k] c[k + 2, k] = vals[k] assert_equal(diag(vals, k=2), b) assert_equal(diag(vals, k=-2), c)
def test_may_share_memory_manual(): # Manual test cases for may_share_memory # Base arrays xs0 = [ np.zeros([13, 21, 23, 22], dtype=np.int8), np.zeros([13, 21, 23*2, 22], dtype=np.int8)[:,:,::2,:] ] # Generate all negative stride combinations xs = [] for x in xs0: for ss in itertools.product(*(([slice(None), slice(None, None, -1)],)*4)): xp = x[ss] xs.append(xp) for x in xs: # The default is a simple extent check assert_(np.may_share_memory(x[:,0,:], x[:,1,:])) assert_(np.may_share_memory(x[:,0,:], x[:,1,:], max_work=None)) # Exact checks check_may_share_memory_exact(x[:,0,:], x[:,1,:]) check_may_share_memory_exact(x[:,::7], x[:,3::3]) try: xp = x.ravel() if xp.flags.owndata: continue xp = xp.view(np.int16) except ValueError: continue # 0-size arrays cannot overlap check_may_share_memory_exact(x.ravel()[6:6], xp.reshape(13, 21, 23, 11)[:,::7]) # Test itemsize is dealt with check_may_share_memory_exact(x[:,::7], xp.reshape(13, 21, 23, 11)) check_may_share_memory_exact(x[:,::7], xp.reshape(13, 21, 23, 11)[:,3::3]) check_may_share_memory_exact(x.ravel()[6:7], xp.reshape(13, 21, 23, 11)[:,::7]) # Check unit size x = np.zeros([1], dtype=np.int8) check_may_share_memory_exact(x, x) check_may_share_memory_exact(x, x.copy())
def test_stack(): # non-iterable input assert_raises(TypeError, stack, 1) # 0d input for input_ in [(1, 2, 3), [np.int32(1), np.int32(2), np.int32(3)], [np.array(1), np.array(2), np.array(3)]]: assert_array_equal(stack(input_), [1, 2, 3]) # 1d input examples a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) r1 = array([[1, 2, 3], [4, 5, 6]]) assert_array_equal(np.stack((a, b)), r1) assert_array_equal(np.stack((a, b), axis=1), r1.T) # all input types assert_array_equal(np.stack(list([a, b])), r1) assert_array_equal(np.stack(array([a, b])), r1) # all shapes for 1d input arrays = [np.random.randn(3) for _ in range(10)] axes = [0, 1, -1, -2] expected_shapes = [(10, 3), (3, 10), (3, 10), (10, 3)] for axis, expected_shape in zip(axes, expected_shapes): assert_equal(np.stack(arrays, axis).shape, expected_shape) assert_raises_regex(np.AxisError, 'out of bounds', stack, arrays, axis=2) assert_raises_regex(np.AxisError, 'out of bounds', stack, arrays, axis=-3) # all shapes for 2d input arrays = [np.random.randn(3, 4) for _ in range(10)] axes = [0, 1, 2, -1, -2, -3] expected_shapes = [(10, 3, 4), (3, 10, 4), (3, 4, 10), (3, 4, 10), (3, 10, 4), (10, 3, 4)] for axis, expected_shape in zip(axes, expected_shapes): assert_equal(np.stack(arrays, axis).shape, expected_shape) # empty arrays assert_(stack([[], [], []]).shape == (3, 0)) assert_(stack([[], [], []], axis=1).shape == (0, 3)) # edge cases assert_raises_regex(ValueError, 'need at least one array', stack, []) assert_raises_regex(ValueError, 'must have the same shape', stack, [1, np.arange(3)]) assert_raises_regex(ValueError, 'must have the same shape', stack, [np.arange(3), 1]) assert_raises_regex(ValueError, 'must have the same shape', stack, [np.arange(3), 1], axis=1) assert_raises_regex(ValueError, 'must have the same shape', stack, [np.zeros((3, 3)), np.zeros(3)], axis=1) assert_raises_regex(ValueError, 'must have the same shape', stack, [np.arange(2), np.arange(3)])
def test_diag_indices(): di = diag_indices(4) a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) a[di] = 100 assert_array_equal( a, np.array([[100, 2, 3, 4], [5, 100, 7, 8], [9, 10, 100, 12], [13, 14, 15, 100]]) ) # Now, we create indices to manipulate a 3-d array: d3 = diag_indices(2, 3) # And use it to set the diagonal of a zeros array to 1: a = np.zeros((2, 2, 2), int) a[d3] = 1 assert_array_equal( a, np.array([[[1, 0], [0, 0]], [[0, 0], [0, 1]]]) )
def test_internal_overlap_slices(): # Slicing an array never generates internal overlap x = np.zeros([17,34,71,97], dtype=np.int16) rng = np.random.RandomState(1234) def random_slice(n, step): start = rng.randint(0, n+1, dtype=np.intp) stop = rng.randint(start, n+1, dtype=np.intp) if rng.randint(0, 2, dtype=np.intp) == 0: stop, start = start, stop step *= -1 return slice(start, stop, step) cases = 0 min_count = 5000 while cases < min_count: steps = tuple(rng.randint(1, 11, dtype=np.intp) if rng.randint(0, 5, dtype=np.intp) == 0 else 1 for j in range(x.ndim)) t1 = np.arange(x.ndim) rng.shuffle(t1) s1 = tuple(random_slice(p, s) for p, s in zip(x.shape, steps)) a = x[s1].transpose(t1) assert_(not internal_overlap(a)) cases += 1
def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count): # Check that overlap problems with common strides are solved with # little work. x = np.zeros([17,34,71,97], dtype=np.int16) feasible = 0 infeasible = 0 pair_iter = iter_random_view_pairs(x, same_steps) while min(feasible, infeasible) < min_count: a, b = next(pair_iter) bounds_overlap = np.may_share_memory(a, b) may_share_answer = np.may_share_memory(a, b) easy_answer = np.may_share_memory(a, b, max_work=get_max_work(a, b)) exact_answer = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT) if easy_answer != exact_answer: # assert_equal is slow... assert_equal(easy_answer, exact_answer) if may_share_answer != bounds_overlap: assert_equal(may_share_answer, bounds_overlap) if bounds_overlap: if exact_answer: feasible += 1 else: infeasible += 1
def test_different_field_order(self): # gh-8940 a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u1')]) b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')]) # this should not give a FutureWarning: j = join_by(['c', 'b'], a, b, jointype='inner', usemask=False) assert_equal(j.dtype.names, ['b', 'c', 'a1', 'a2'])
def test_weights(self): v = np.random.rand(100) w = np.ones(100) * 5 a, b = histogram(v) na, nb = histogram(v, density=True) wa, wb = histogram(v, weights=w) nwa, nwb = histogram(v, weights=w, density=True) assert_array_almost_equal(a * 5, wa) assert_array_almost_equal(na, nwa) # Check weights are properly applied. v = np.linspace(0, 10, 10) w = np.concatenate((np.zeros(5), np.ones(5))) wa, wb = histogram(v, bins=np.arange(11), weights=w) assert_array_almost_equal(wa, w) # Check with integer weights wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1]) assert_array_equal(wa, [4, 5, 0, 1]) wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], density=True) assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4) # Check weights with non-uniform bin widths a, b = histogram(np.arange(9), [0, 1, 3, 6, 10], weights=[2, 1, 1, 1, 1, 1, 1, 1, 1], density=True) assert_almost_equal(a, [.2, .1, .1, .075])
def test_simple(self): x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5], [.5, .5, 1.5], [.5, 1.5, 2.5], [.5, 2.5, 2.5]]) H, edges = histogramdd(x, (2, 3, 3), range=[[-1, 1], [0, 3], [0, 3]]) answer = np.array([[[0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, 1, 0], [0, 0, 1], [0, 0, 1]]]) assert_array_equal(H, answer) # Check normalization ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]] H, edges = histogramdd(x, bins=ed, density=True) assert_(np.all(H == answer / 12.)) # Check that H has the correct shape. H, edges = histogramdd(x, (2, 3, 4), range=[[-1, 1], [0, 3], [0, 4]], density=True) answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]], [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]]) assert_array_almost_equal(H, answer / 6., 4) # Check that a sequence of arrays is accepted and H has the correct # shape. z = [np.squeeze(y) for y in np.split(x, 3, axis=1)] H, edges = histogramdd(z, bins=(4, 3, 2), range=[[-2, 2], [0, 3], [0, 2]]) answer = np.array([[[0, 0], [0, 0], [0, 0]], [[0, 1], [0, 0], [1, 0]], [[0, 1], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]) assert_array_equal(H, answer) Z = np.zeros((5, 5, 5)) Z[list(range(5)), list(range(5)), list(range(5))] = 1. H, edges = histogramdd([np.arange(5), np.arange(5), np.arange(5)], 5) assert_array_equal(H, Z)
def test_valuesSD(self): # Check assignment of single-dimensional objects with values ua = np.zeros((2,), dtype='U%s' % self.ulen) ua[0] = self.ucs_value*self.ulen self.content_check(ua, ua[0], 4*self.ulen*2) ua[1] = self.ucs_value*self.ulen self.content_check(ua, ua[1], 4*self.ulen*2)
def test_valuesMD(self): # Check assignment of multi-dimensional objects with values ua = np.zeros((2, 3, 4), dtype='U%s' % self.ulen) ua[0, 0, 0] = self.ucs_value*self.ulen self.content_check(ua, ua[0, 0, 0], 4*self.ulen*2*3*4) ua[-1, -1, -1] = self.ucs_value*self.ulen self.content_check(ua, ua[-1, -1, -1], 4*self.ulen*2*3*4)
def setup(self): self.a = np.arange(np.prod([3, 1, 5, 6])).reshape(3, 1, 5, 6) self.b = np.empty((3, 0, 5, 6)) self.complex_indices = ['skip', Ellipsis, 0, # Boolean indices, up to 3-d for some special cases of eating up # dimensions, also need to test all False np.array([True, False, False]), np.array([[True, False], [False, True]]), np.array([[[False, False], [False, False]]]), # Some slices: slice(-5, 5, 2), slice(1, 1, 100), slice(4, -1, -2), slice(None, None, -3), # Some Fancy indexes: np.empty((0, 1, 1), dtype=np.intp), # empty and can be broadcast np.array([0, 1, -2]), np.array([[2], [0], [1]]), np.array([[0, -1], [0, 1]], dtype=np.dtype('intp').newbyteorder()), np.array([2, -1], dtype=np.int8), np.zeros([1]*31, dtype=int), # trigger too large array. np.array([0., 1.])] # invalid datatype # Some simpler indices that still cover a bit more self.simple_indices = [Ellipsis, None, -1, [1], np.array([True]), 'skip'] # Very simple ones to fill the rest: self.fill_indices = [slice(None, None), 0]
def test_nested(self): one = np.array([1, 1, 1]) two = np.array([[2, 2, 2], [2, 2, 2], [2, 2, 2]]) three = np.array([3, 3, 3]) four = np.array([4, 4, 4]) five = np.array(5) six = np.array([6, 6, 6, 6, 6]) zero = np.zeros((2, 6)) result = np.block([ [ np.block([ [one], [three], [four] ]), two ], [five, six], [zero] ]) expected = np.array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 2, 2, 2], [4, 4, 4, 2, 2, 2], [5, 6, 6, 6, 6, 6], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) assert_equal(result, expected)