def test_scatter_add_cupy_arguments_mask(self, dtype): shape = (2, 3) a = cupy.zeros(shape, dtype) slices = (cupy.array([True, False]), slice(None)) a.scatter_add(slices, cupy.array(1.)) testing.assert_array_equal( a, cupy.array([[1., 1., 1.], [0., 0., 0.]], dtype))
def test_strides(self): x = cupy.arange(6).reshape((2, 3)).astype('i') y = cupy.ElementwiseKernel( 'raw int32 x', 'int32 y', 'y = x.strides()[i]', 'test_carray_strides', )(x, size=2) testing.assert_array_equal(y, (12, 4))
def test_adv_getitem_cupy_indices3(self): shape = (2, 3, 4) a = cupy.zeros(shape) index = cupy.array([True, False]) b = a[index] b_cpu = a.get()[index.get()] testing.assert_array_equal(b, b_cpu)
def test_adv_getitem_cupy_indices2(self): shape = (2, 3, 4) a = cupy.zeros(shape) index = cupy.array([1, 0]) b = a[(slice(None), index)] b_cpu = a.get()[(slice(None), index.get())] testing.assert_array_equal(b, b_cpu)
def test_cupy_indices_integer_array(self): shape = (2, 3) a = cupy.zeros(shape) indexes = cupy.array([0, 1]) a[:, indexes] = cupy.array(1.) testing.assert_array_equal( a, cupy.array([[1., 1., 0.], [1., 1., 0.]]))
def test_cupy_indices_boolean_array(self): shape = (2, 3) a = cupy.zeros(shape) indexes = cupy.array([True, False]) a[indexes] = cupy.array(1.) testing.assert_array_equal( a, cupy.array([[1., 1., 1.], [0., 0., 0.]]))
def test_scatter_add_cupy_arguments(self, dtype): shape = (2, 3) a = cupy.zeros(shape, dtype) slices = (cupy.array([1, 1]), slice(None)) a.scatter_add(slices, cupy.array(1.)) testing.assert_array_equal( a, cupy.array([[0., 0., 0.], [2., 2., 2.]], dtype))
def test_take(self, xp, dtype): a = testing.shaped_arange(self.shape, xp, dtype) r1 = wrap_take(a, self.indices, self.axis) r2 = xp.zeros_like(r1) wrap_take(a, self.indices, self.axis, out=r2) testing.assert_array_equal(r1, r2) return r2
def test_getitem_int(self): x = cupy.arange(24).reshape((2, 3, 4)).astype('i') y = cupy.empty_like(x) y = cupy.ElementwiseKernel( 'raw T x', 'int32 y', 'y = x[i]', 'test_carray_getitem_int', )(x, y) testing.assert_array_equal(y, x)
def test_getitem_idx(self): x = cupy.arange(24).reshape((2, 3, 4)).astype('i') y = cupy.empty_like(x) y = cupy.ElementwiseKernel( 'raw T x', 'int32 y', 'int idx[] = {i / 12, i / 4 % 3, i % 4}; y = x[idx]', 'test_carray_getitem_idx', )(x, y) testing.assert_array_equal(y, x)
def test_frexp(self, dtype): numpy_a = numpy.array([-300, -20, -10, -1, 0, 1, 10, 20, 300], dtype=dtype) numpy_b, numpy_c = numpy.frexp(numpy_a) cupy_a = cupy.array(numpy_a) cupy_b, cupy_c = cupy.frexp(cupy_a) testing.assert_allclose(cupy_b, numpy_b) testing.assert_array_equal(cupy_c, numpy_c)
def test_reset_seed(self, dtype): rs = random.get_random_state() rs.seed(0) l1 = rs.rand(10, dtype=dtype) rs = random.get_random_state() rs.seed(0) l2 = rs.rand(10, dtype=dtype) testing.assert_array_equal(l1, l2)
def test_scan(self, dtype): element_num = 10000 if dtype in {cupy.int8, cupy.uint8}: element_num = 100 a = cupy.ones((element_num,), dtype=dtype) prefix_sum = cupy.core.core.scan(a) expect = cupy.arange(start=1, stop=element_num + 1).astype(dtype) testing.assert_array_equal(prefix_sum, expect)
def test_save_load(self, dtype): a = testing.shaped_arange((2, 3, 4), dtype=dtype) sio = six.BytesIO() cupy.save(sio, a) s = sio.getvalue() sio.close() sio = six.BytesIO(s) b = cupy.load(sio) sio.close() testing.assert_array_equal(a, b)
def test_frexp(self, dtype): numpy_a = numpy.array([-300, -20, -10, -1, 0, 1, 10, 20, 300], dtype=dtype) @cupy.fuse() def g(x): return cupy.frexp(x) numpy_b, numpy_c = g(numpy_a) cupy_a = cupy.array(numpy_a) cupy_b, cupy_c = g(cupy_a) testing.assert_allclose(cupy_b, numpy_b) testing.assert_array_equal(cupy_c, numpy_c)
def test_scan_out(self, dtype): element_num = 10000 if dtype in {cupy.int8, cupy.uint8, cupy.float16}: element_num = 100 a = cupy.ones((element_num,), dtype=dtype) b = cupy.zeros_like(a) cupy.core.core.scan(a, b) expect = cupy.arange(start=1, stop=element_num + 1).astype(dtype) testing.assert_array_equal(b, expect) cupy.core.core.scan(a, a) testing.assert_array_equal(a, expect)
def check_savez(self, savez, dtype): a1 = testing.shaped_arange((2, 3, 4), dtype=dtype) a2 = testing.shaped_arange((3, 4, 5), dtype=dtype) sio = six.BytesIO() savez(sio, a1, a2) s = sio.getvalue() sio.close() sio = six.BytesIO(s) with cupy.load(sio) as d: b1 = d['arr_0'] b2 = d['arr_1'] sio.close() testing.assert_array_equal(a1, b1) testing.assert_array_equal(a2, b2)
def test_manual_indexing(self, n=100): in1 = cupy.random.uniform(-1, 1, n).astype(cupy.float32) in2 = cupy.random.uniform(-1, 1, n).astype(cupy.float32) uesr_kernel_1 = cupy.ElementwiseKernel( 'T x, T y', 'T z', ''' z = x + y; ''', 'uesr_kernel_1') out1 = uesr_kernel_1(in1, in2) uesr_kernel_2 = cupy.ElementwiseKernel( 'raw T x, raw T y', 'raw T z', ''' z[i] = x[i] + y[i]; ''', 'uesr_kernel_2') out2 = uesr_kernel_2(in1, in2, size=n) testing.assert_array_equal(out1, out2)
def test_ifft(self, xp, dtype): x = testing.shaped_random(self.shape, xp, dtype) x_orig = x.copy() out = _fft_module(xp).ifft(x, n=self.n, axis=self.axis, norm=self.norm) testing.assert_array_equal(x, x_orig) return _correct_np_dtype(xp, dtype, out)
def test_ascontiguousarray_on_noncontiguous_array(self): a = testing.shaped_arange((2, 3, 4)) b = a.transpose(2, 0, 1) c = cupy.ascontiguousarray(b) self.assertTrue(c.flags.c_contiguous) testing.assert_array_equal(b, c)
def test_cube_selem(self): """Test cube structuring elements""" for k in range(0, 5): actual_mask = selem.cube(k) expected_mask = cp.ones((k, k, k), dtype="uint8") assert_array_equal(expected_mask, actual_mask)
def test_col(self): self.assertEqual(self.m.col.dtype, numpy.int32) testing.assert_array_equal(self.m.col, cupy.array([0, 1, 3, 2], self.dtype))
def test_equality_numpy(self): testing.assert_array_equal(self.x, self.y, strides_check=True)
def test_inverse(): tform = SimilarityTransform(scale=0.5, rotation=0.1) inverse_tform = SimilarityTransform(matrix=cp.linalg.inv(tform.params)) image = cp.arange(10 * 10).reshape(10, 10).astype(cp.double) assert_array_equal(warp(image, inverse_tform), warp(image, tform.inverse))
def test_inequality_numpy(self): self.y = self.array_module_y.asfortranarray(self.y) with self.assertRaises(AssertionError): testing.assert_array_equal(self.x, self.y, strides_check=True)
def test_intensity_range_clipped_float(): image = cp.array([0.1, 0.2], dtype=np.float64) out = intensity_range(image, range_values="dtype", clip_negative=True) assert_array_equal(out, (0, 1))
def test_sum01(): for type in types: input = cp.asarray([], type) output = ndimage.sum(input) assert_array_equal(output, 0.0)
def test_not_copied(self, dtype): a = testing.shaped_arange((2, 3, 4), cupy, dtype) b = cupy.asarray( DummyObjectWithCudaArrayInterface(a, self.ver, self.strides)) a.fill(0) testing.assert_array_equal(a, b)
def test_basic(self): x = cp.asarray([0, 1, 6, 2]) cases = [ ([0, 0, 1, 1], [0, 1]), # "Small" integer labels ([0, 0, 9, 9], [0, 9]), # A label larger than len(labels) ([0.0, 0.0, 7.0, 7.0], [0.0, 7.0]), # Non-integer labels ] for labels, index in cases: labels = cp.asarray(labels) index = cp.asarray(index) result = ndimage.measurements._select(x, labels=labels, index=index) assert_(len(result) == 0) result = ndimage.measurements._select(x, labels=labels, index=index, find_max=True) assert_(len(result) == 1) assert_array_equal(result[0], [1, 6]) result = ndimage.measurements._select(x, labels=labels, index=index, find_min=True) assert_(len(result) == 1) assert_array_equal(result[0], [0, 2]) result = ndimage.measurements._select( x, labels=labels, index=index, find_min=True, find_min_positions=True, ) assert_(len(result) == 2) assert_array_equal(result[0], [0, 2]) assert_array_equal(result[1], [0, 3]) assert_equal(result[1].dtype.kind, "i") result = ndimage.measurements._select( x, labels=labels, index=index, find_max=True, find_max_positions=True, ) assert_(len(result) == 2) assert_array_equal(result[0], [1, 6]) assert_array_equal(result[1], [1, 2]) assert_equal(result[1].dtype.kind, "i")
def test_ifft(self, xp, scp, dtype): x = testing.shaped_random(self.shape, xp, dtype) x_orig = x.copy() out = scp.fftpack.ifft(x, n=self.n, axis=self.axis) testing.assert_array_equal(x, x_orig) return out
def test_fftn(self, xp, scp, dtype): x = testing.shaped_random(self.shape, xp, dtype) x_orig = x.copy() out = scp.fftpack.fftn(x, shape=self.s, axes=self.axes) testing.assert_array_equal(x, x_orig) return out
def test_intensity_range_uint8(test_input, expected): image = cp.array([0, 1], dtype=np.uint8) out = intensity_range(image, range_values=test_input) assert_array_equal(out, cp.array(expected))
def test_pythagorean_triangle_right_downward(): prof = profile_line(image, (1, 1), (7, 9), order=0, mode="constant") expected_prof = cp.array([11, 22, 23, 33, 34, 45, 56, 57, 67, 68, 79]) assert_array_equal(prof, expected_prof)
def test_intensity_range_float(test_input, expected): image = cp.array([0.1, 0.2], dtype=np.float64) out = intensity_range(image, range_values=test_input) assert_array_equal(out, expected)
def test_pickle(self, dtype): a = testing.shaped_arange((2, 3, 4), dtype=dtype) s = six.moves.cPickle.dumps(a) b = six.moves.cPickle.loads(s) testing.assert_array_equal(a, b)
def test_indptr(self): self.assertEqual(self.m.indptr.dtype, numpy.int32) testing.assert_array_equal(self.m.indptr, cupy.array([0, 1, 2, 3, 4], self.dtype))
def test_indices(self): self.assertEqual(self.m.indices.dtype, numpy.int32) testing.assert_array_equal(self.m.indices, cupy.array([0, 0, 2, 1], self.dtype))
def test_cupy_indices_boolean_array(self): shape = (2, 3) a = cupy.zeros(shape) indexes = cupy.array([True, False]) a[indexes] = cupy.array(1.) testing.assert_array_equal(a, cupy.array([[1., 1., 1.], [0., 0., 0.]]))
def test_shape(self): x = cupy.arange(6).reshape((2, 3)).astype('i') y = cupy.ElementwiseKernel( 'raw int32 x', 'int32 y', 'y = x.shape()[i]', 'test_carray_shape', )(x, size=2) testing.assert_array_equal(y, (2, 3))
def test_scatter_add(self): a = cupy.zeros((3,), dtype=numpy.float32) i = cupy.array([1, 1], numpy.int32) v = cupy.array([2., 1.], dtype=numpy.float32) cupy.scatter_add(a, i, v) testing.assert_array_equal(a, cupy.array([0, 3, 0]))
def test_asnumpy(self, dtype): a = testing.shaped_arange((2, 3, 4), cupy, dtype) b = DummyObjectWithCudaArrayInterface(a, self.ver, self.strides) a_cpu = cupy.asnumpy(a) b_cpu = cupy.asnumpy(b) testing.assert_array_equal(a_cpu, b_cpu)
def test_horizontal_rightward(): prof = profile_line(image, (0, 2), (0, 8), order=0, mode="constant") expected_prof = cp.arange(2, 9) assert_array_equal(prof, expected_prof)
def test_sum08(): labels = cp.asarray([1, 0], bool) for type in types: input = cp.asarray([1, 2], type) output = ndimage.sum(input, labels=labels) assert_array_equal(output, 1.0)
def test_vertical_upward(): prof = profile_line(image, (8, 5), (2, 5), order=0, mode="constant") expected_prof = cp.arange(85, 15, -10) assert_array_equal(prof, expected_prof)
def test_maximum05(): # Regression test for ticket #501 (Trac) x = cp.asarray([-3, -2, -1]) assert_array_equal(ndimage.maximum(x), -1)
def test_data(self): self.assertEqual(self.m.data.dtype, self.dtype) testing.assert_array_equal(self.m.data, cupy.array([0, 1, 2, 3], self.dtype))
def test_row(self): self.assertEqual(self.m.row.dtype, numpy.int32) testing.assert_array_equal(self.m.row, cupy.array([0, 0, 1, 2], self.dtype))
def test_sum02(): for type in types: input = cp.zeros([0, 4], type) output = ndimage.sum(input) assert_array_equal(output, 0.0)
def test_asarray_cuda_array_interface(self, dtype): a = testing.shaped_arange((2, 3, 4), cupy, dtype) b = cupy.asarray(DummyObjectWithCudaArrayInterface(a)) testing.assert_array_equal(a, b)
def test_sum07(): labels = cp.ones([0, 4], bool) for type in types: input = cp.zeros([0, 4], type) output = ndimage.sum(input, labels=labels) assert_array_equal(output, 0.0)