def test_astype_strides_negative(self, xp, src_dtype, dst_dtype): data = xp.empty((2, 3), dtype=src_dtype) mask = testing.shaped_random((2, 3), xp, dtype=numpy.bool_) fill_value = testing.shaped_arange((2, 3), nlcpy) + 1 src = xp.ma.array(data, mask=mask, fill_value=fill_value)[::-1, :] return numpy.array( astype_without_warning(src, dst_dtype, order='K').strides)
def check_binary(self, name, xp, dtype, no_complex=False): if no_complex: if numpy.dtype(dtype).kind == 'c': return xp.array(True) a = testing.shaped_arange(self.shape, xp, dtype) b = testing.shaped_reverse_arange(self.shape, xp, dtype) return getattr(xp, name)(a, b)
def test_setitem_different_type(self, xp, src_type, dst_type): a = xp.zeros((2, 3, 4), dtype=dst_type) b = testing.shaped_arange((2, 3, 4), xp, src_type) with warnings.catch_warnings(): warnings.simplefilter('ignore', numpy.ComplexWarning) a[:] = b return a
def test_view_ndarray(self, xp): data = testing.shaped_arange((4, ), xp, dtype=numpy.int32) mask = [0, 1, 0, 1] fill_value = 5 a = xp.ma.array(data, mask=mask, fill_value=fill_value) b = a.view(dtype=numpy.float64, type=xp.ndarray) return b
def test_clip_with_out(self, xp, dt_a, dt_amin, dt_amax, dt_out): a = testing.shaped_arange([3, 4], xp, dtype=dt_a) amin = xp.array(3, dtype=dt_amin) amax = xp.array(10, dtype=dt_amax) out = xp.zeros(a.shape, dtype=dt_out) xp.clip(a, amin, amax, out=out, casting=self.casting) raise Exception
def test_invalid_getitem(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) if self.transpose: a = a.transpose(self.transpose) print("a:\n", a) print("self.indexes:\n", self.indexes) a[self.indexes]
def test_fill_diagonal_strided(self, xp, dtype_a, dtype_val): a = testing.shaped_arange(self.shape_a, xp, dtype=dtype_a) a = a[[slice(0, None, 2) for _ in range(len(self.shape_a))]] val = testing.shaped_random(self.shape_val, xp, dtype_val) val = val[[slice(0, None, 2) for _ in range(len(self.shape_val))]] xp.fill_diagonal(a, val=val, wrap=self.wrap) return a
def test_invalid_getitem(self, xp, dtype): data = testing.shaped_arange(self.shape, xp, dtype) mask = testing.shaped_random(self.shape, xp, numpy.bool_) fill_value = testing.shaped_random(self.shape, xp) a = xp.ma.array(data, mask=mask, fill_value=fill_value) if self.transpose: a = a.transpose(self.transpose) a[self.indexes]
def test_take_by_masked_array(self, xp): data = testing.shaped_random([3, 4, 3], xp) mask = testing.shaped_arange([3, 4, 3], xp) % 2 a = xp.ma.array(data, mask=mask) data = testing.shaped_random([2, 2], xp) % 3 mask = testing.shaped_random([2, 2], xp) % 3 idx = xp.ma.array(data, mask=mask, dtype='l') return a.take(idx, axis=2)
def test_clip_with_dtype(self, xp, dt_a, dt_amin, dt_amax, dtype): a = testing.shaped_arange([3, 4], xp, dtype=dt_a) amin = xp.array(3, dtype=dt_amin) amax = xp.array(10, dtype=dt_amax) try: return xp.clip(a, amin, amax, dtype=dtype, casting=self.casting) except UFuncTypeError: return 0
def test_take_with_ndarray_out(self, xp): data = testing.shaped_random([3, 4, 3], xp) mask = testing.shaped_arange([3, 4, 3], xp) % 2 a = xp.ma.array(data, mask=mask) idx = xp.array([[1, 3], [2, 0]]) out = xp.empty([3, 2, 2, 3], dtype=numpy.float32) a.take(idx, axis=1, out=out) return out
def test_nextafter_float(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) - 3 b = testing.shaped_reverse_arange(self.shape, xp, dtype) - 3 b[0] = -xp.inf b[-1] = xp.inf a = a[:, None] b = b[None, :] return xp.nextafter(a, b)
def test_view_same_itemsize(self, xp): data = testing.shaped_arange((4, ), xp, dtype=numpy.int32) mask = [0, 1, 0, 1] fill_value = 5 a = xp.ma.array(data, mask=mask, fill_value=fill_value) b = a.view(dtype=numpy.float32) b.fill_value = 10 return b
def test_clip_order(self, xp, order1, order2, order3, order4, order5, order6): a = xp.asarray(testing.shaped_arange([2, 3, 4], xp), order=order1) amin = xp.asarray(testing.shaped_random(a.shape, xp), order=order2) amax = xp.asarray(testing.shaped_random(a.shape, xp), order=order3) out = xp.zeros(a.shape, order=order4) where = xp.asarray(testing.shaped_random(a.shape, xp, dtype=bool), order=order5) return xp.clip(a, amin, amax, out=out, order=order6, where=where)
def test_fill_diagonal(self, xp, dtype_a, dtype_val, order_a, order_val): a = testing.shaped_arange(self.shape_a, xp, dtype=dtype_a, order=order_a) val = xp.asarray(testing.shaped_random(self.shape_val, xp, dtype_val), order=order_val) xp.fill_diagonal(a, val=val, wrap=self.wrap) return a
def test_is_contiguous(self): a = testing.shaped_arange((2, 3, 4)) self.assertTrue(a.flags.c_contiguous) b = a.transpose(2, 0, 1) self.assertFalse(b.flags.c_contiguous) c = a[::-1] self.assertFalse(c.flags.c_contiguous) d = a[:, :, ::2] self.assertFalse(d.flags.c_contiguous)
def test_broadcast_to_numpy19(self, xp, dtype): # Note that broadcast_to is only supported on numpy>=1.10 a = testing.shaped_arange((3, 1, 4), xp, dtype) if xp is nlcpy: b = xp.broadcast_to(a, (2, 3, 3, 4)) else: dummy = xp.empty((2, 3, 3, 4)) b, _ = xp.broadcast_arrays(a, dummy) return b
def test_load_pickle_failure(self, xp): a = testing.shaped_arange((2, 3, 4), numpy, dtype='float32') with io.BytesIO() as f: a.dump(f) val = f.getvalue() with io.BytesIO(val) as f: xp.load(f, allow_pickle=False)
def test_empty_like_contiguity(self, xp, dtype, order): a = testing.shaped_arange((2, 3, 4), xp, dtype) b = xp.empty_like(a, order=order) b.fill(0) if order in ['f', 'F']: self.assertTrue(b.flags.f_contiguous) else: self.assertTrue(b.flags.c_contiguous) return b
def test_save_load(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), xp, dtype=dtype) with io.BytesIO() as f: xp.save(f, a) val = f.getvalue() with io.BytesIO(val) as f: b = xp.load(f) return b
def test_dump(self, xp, dtype): a = testing.shaped_arange((2, 3, 4), numpy, dtype=dtype) with io.BytesIO() as f: a.dump(f) val = f.getvalue() with io.BytesIO(val) as f: b = xp.load(f, allow_pickle=True) return b
def test_empty_like_reshape_contiguity_nlcpy_only(self, dtype, order): a = testing.shaped_arange((2, 3, 4), nlcpy, dtype) b = nlcpy.empty_like(a, order=order, shape=self.shape) b.fill(0) c = nlcpy.empty(self.shape) c.fill(0) if order in ['f', 'F']: self.assertTrue(b.flags.f_contiguous) else: self.assertTrue(b.flags.c_contiguous) testing.assert_array_equal(b, c)
def test_expand_dims_tuple_axis(self, xp): a = testing.shaped_arange((2, 2, 2), xp) return [ xp.expand_dims(a, axis) for axis in [ (0, 1, 2), (0, -1, -2), (0, 3, 5), (0, -3, -5), (), (1, ), ] ]
def test_3d_volumetric_with_factor_and_coef(self): xin = testing.shaped_arange(self.shape, nlcpy).astype(self.dtype) * 0.1 n_elem = get_n_stencil_elem(self.stencil_scale) nlcpy.random.seed(0) # to avoid loss of digits, create from arenge factor = ((nlcpy.arange(n_elem) - 0.5) * 10).tolist() if self.coef_array: coef_shape = list(xin.shape) for an in get_axis_numbers_from_strtype(self.type): if abs(an) <= xin.ndim: coef_shape[an] -= 2 * self.stencil_scale coef = testing.shaped_arange( [n_elem, ] + coef_shape, nlcpy, dtype=self.dtype ) * 0.01 else: coef = (nlcpy.arange(n_elem) - 0.5) * 10 coef = coef.astype(dtype=self.dtype) rtol = TOL_SINGLE if self.dtype == numpy.float32 else TOL_DOUBLE sca_res, sca_out = compute_with_sca( self.type, xin, self.stencil_scale, factor=factor, coef=coef, is_out=self.is_out, prefix=self.prefix, optimize=self.optimize, change_coef=self.change_coef ) naive_res = compute_with_naive( self.type, xin, self.stencil_scale, factor=factor, coef=coef ) if self.is_out: assert id(sca_res) == id(sca_out) testing.assert_allclose(sca_res, naive_res, rtol=rtol)
def test_setitem_partial_copy(self, xp, dtype): shape_a = (2, 3, 4) data = xp.zeros(shape_a, dtype=dtype) mask = testing.shaped_random(shape_a, xp, numpy.bool_) fill_value = testing.shaped_random(shape_a, xp) a = xp.ma.array(data, mask=mask, fill_value=fill_value) shape_b = (3, 2) data = testing.shaped_arange(shape_b, xp, dtype=dtype) mask = testing.shaped_random(shape_b, xp, numpy.bool_) fill_value = testing.shaped_random(shape_b, xp) b = xp.ma.array(data, mask=mask, fill_value=fill_value) a[1, ::-1, 1:4:2] = b return a
def test_setitem_copy(self, xp, dtype): shape = (2, 3, 4) data = xp.zeros(shape, dtype=dtype) mask = testing.shaped_random(shape, xp, numpy.bool_) fill_value = testing.shaped_random(shape, xp) a = xp.ma.array(data, mask=mask, fill_value=fill_value) data = testing.shaped_arange(shape, xp, dtype=dtype) mask = testing.shaped_random(shape, xp, numpy.bool_) fill_value = testing.shaped_random(shape, xp) b = xp.ma.array(data, mask=mask, fill_value=fill_value) a[:] = b return a
def test_setitem_different_type(self, xp, src_type, dst_type): shape = (2, 3, 4) data = xp.zeros(shape, dtype=dst_type) mask = testing.shaped_random(shape, xp, numpy.bool_) fill_value = testing.shaped_random(shape, xp) a = xp.ma.array(data, mask=mask, fill_value=fill_value) data = testing.shaped_arange(shape, xp, dtype=src_type) mask = testing.shaped_random(shape, xp, numpy.bool_) fill_value = testing.shaped_random(shape, xp) b = xp.ma.array(data, mask=mask, fill_value=fill_value) with warnings.catch_warnings(): warnings.simplefilter('ignore', numpy.ComplexWarning) a[:] = b return a
def test_3d_volumetric_with_factor(self): xin = testing.shaped_arange(self.shape, nlcpy).astype(self.dtype) * 0.1 n_elem = get_n_stencil_elem(self.stencil_scale) factor = (nlcpy.arange(n_elem) * 0.1).tolist() rtol = TOL_SINGLE if self.dtype == numpy.float32 else TOL_DOUBLE sca_res, sca_out = compute_with_sca( self.type, xin, self.stencil_scale, factor=factor, is_out=self.is_out, prefix=self.prefix, optimize=self.optimize ) naive_res = compute_with_naive(self.type, xin, self.stencil_scale, factor=factor) if self.is_out: assert id(sca_res) == id(sca_out) testing.assert_allclose(sca_res, naive_res, rtol=rtol)
def test_1d_axial_with_factor_and_coef_div_sub(self): if self.prefix and self.div: return True if self.change_coef and self.div: return True xin = testing.shaped_arange(self.shape, nlcpy).astype(self.dtype) * 0.1 n_elem = get_n_stencil_elem(self.stencil_scale) nlcpy.random.seed(0) factor = ((nlcpy.random.rand(n_elem) - 0.5) * 10).tolist() coef = (nlcpy.random.rand(n_elem) - 0.5) * 10 coef = coef.astype(dtype=self.dtype) rtol = TOL_SINGLE if self.dtype == numpy.float32 else TOL_DOUBLE sca_res, sca_out = compute_with_sca( self.type, xin, self.stencil_scale, factor=factor, coef=coef, is_out=self.is_out, prefix=self.prefix, optimize=self.optimize, change_coef=self.change_coef, div=self.div, sub=self.sub ) naive_res = compute_with_naive( self.type, xin, self.stencil_scale, factor=factor, coef=coef, div=self.div, sub=self.sub ) if self.is_out: assert id(sca_res) == id(sca_out) testing.assert_allclose(sca_res, naive_res, rtol=rtol)
def test_astype_type(self, src_dtype, dst_dtype, order): data = testing.shaped_arange((2, 3, 4), nlcpy) mask = testing.shaped_arange((2, 3, 4), nlcpy) % 2 fill_value = testing.shaped_arange((2, 3, 4), nlcpy) + 1 a = nlcpy.ma.array(data, mask=mask, fill_value=fill_value, dtype=src_dtype) b = astype_without_warning(a, dst_dtype, order=order) data = testing.shaped_arange((2, 3, 4), numpy) mask = testing.shaped_arange((2, 3, 4), numpy) % 2 fill_value = testing.shaped_arange((2, 3, 4), numpy) + 1 a_cpu = numpy.ma.array(data, mask=mask, fill_value=fill_value, dtype=src_dtype) b_cpu = astype_without_warning(a_cpu, dst_dtype, order=order) self.assertEqual(b.dtype.type, b_cpu.dtype.type) self.assertEqual(b._data.dtype.type, b_cpu._data.dtype.type)