def test_weighted_moments(): wm = regionprops(SAMPLE, intensity_image=INTENSITY_SAMPLE)[0].weighted_moments ref = cp.asarray([ [7.4000000e01, 6.9900000e02, 7.8630000e03, 9.7317000e04], [4.1000000e02, 3.7850000e03, 4.4063000e04, 5.7256700e05], [2.7500000e03, 2.4855000e04, 2.9347700e05, 3.9007170e06], [1.9778000e04, 1.7500100e05, 2.0810510e06, 2.8078871e07], ]) assert_array_almost_equal(wm, ref)
def test_standard_deviation07(): labels = cp.asarray([1]) olderr = np.seterr(all="ignore") try: for type in types: input = cp.asarray([-0.00619519], type) output = ndimage.standard_deviation(input, labels, cp.asarray([1])) assert_array_almost_equal(output, [0]) finally: np.seterr(**olderr)
def test_entropy_2d_zero(self): pk = cupy.asarray([[0.1, 0.2], [0.6, 0.3], [0.3, 0.5]]) qk = cupy.asarray([[0.0, 0.1], [0.3, 0.6], [0.5, 0.3]]) testing.assert_array_almost_equal(stats.entropy(pk, qk), [cupy.inf, 0.18609809]) pk[0][0] = 0.0 testing.assert_array_almost_equal( stats.entropy(pk, qk), [0.17403988, 0.18609809] )
def test_color_names(): image = cp.ones((1, 3)) label = cp.arange(3).reshape(1, -1) cnames = ["red", "lime", "blue"] colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # Set alphas just in case the defaults change rgb = label2rgb( label, image=image, colors=cnames, alpha=1, image_alpha=1, bg_label=-1 ) assert_array_almost_equal(rgb, [colors])
def test_variance06(): labels = cp.asarray([2, 2, 3, 3, 4]) olderr = np.seterr(all="ignore") try: for type in types: input = cp.asarray([1, 3, 8, 10, 8], type) output = ndimage.variance(input, labels, cp.asarray([2, 3, 4])) assert_array_almost_equal(output, [1.0, 1.0, 0.0]) finally: np.seterr(**olderr)
def test_bg_and_color_cycle(): image = cp.zeros((1, 10)) # dummy image label = cp.arange(10).reshape(1, -1) colors = [(1, 0, 0), (0, 0, 1)] bg_color = (0, 0, 0) rgb = label2rgb(label, image=image, bg_label=0, bg_color=bg_color, colors=colors, alpha=1) assert_array_almost_equal(rgb[0, 0], bg_color) for pixel, color in zip(rgb[0, 1:], itertools.cycle(colors)): assert_array_almost_equal(pixel, color)
def test_hsv2rgb_conversion(self): rgb = self.img_rgb.astype("float32")[::16, ::16] # create HSV image with colorsys hsv = cp.asarray([ colorsys.rgb_to_hsv(pt[0], pt[1], pt[2]) for pt in rgb.reshape(-1, 3).get() ]).reshape(rgb.shape) # convert back to RGB and compare with original. # relative precision for RGB -> HSV roundtrip is about 1e-6 assert_array_almost_equal(rgb, hsv2rgb(hsv), decimal=4)
def test_moments_normalized_3d(): image = cp.asarray(draw.ellipsoid(1, 1, 10)) mu_image = moments_central(image) nu = moments_normalized(mu_image) assert nu[0, 0, 2] > nu[0, 2, 0] assert_almost_equal(nu[0, 2, 0], nu[2, 0, 0]) coords = cp.where(image) mu_coords = moments_coords_central(coords) assert_array_almost_equal(mu_coords, mu_image)
def test_csrmm2_with_c(self): a = sparse.csr_matrix(self.a) b = cupy.array(self.b, order='f') c = cupy.array(self.c, order='f') y = cupy.cusparse.csrmm2( a, b, c=c, alpha=self.alpha, beta=self.beta, transa=self.transa, transb=self.transb) expect = self.alpha * self.op_a.dot(self.op_b) + self.beta * self.c self.assertIs(y, c) testing.assert_array_almost_equal(y, expect)
def test_histogram_int_weights_nonuniform_bins(self, xp, dtype): # Check weights with non-uniform bin widths a, b = xp.histogram( xp.arange(9, dtype=dtype), xp.asarray([0, 1, 3, 6, 10], dtype=dtype), weights=xp.asarray([2, 1, 1, 1, 1, 1, 1, 1, 1], dtype=dtype), density=True, ) testing.assert_array_almost_equal(a, [0.2, 0.1, 0.1, 0.075]) return a, b
def test_csrmm2(self): a = sparse.csr_matrix(self.a) b = cupy.array(self.b, order='f') y = cupy.cusparse.csrmm2(a, b, alpha=self.alpha, transa=self.transa, transb=self.transb) expect = self.alpha * self.op_a.dot(self.op_b) testing.assert_array_almost_equal(y, expect)
def test_resize2d(): x = cp.zeros((5, 5), dtype=np.double) x[1, 1] = 1 resized = resize(x, (10, 10), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_moments_hu(): hu = regionprops(SAMPLE)[0].moments_hu # fmt: off ref = cp.array([ 3.27117627e-01, 2.63869194e-02, 2.35390060e-02, 1.23151193e-03, 1.38882330e-06, -2.72586158e-05, -6.48350653e-06 ]) # fmt: on # bug in OpenCV caused in Central Moments calculation? assert_array_almost_equal(hu, ref)
def test_csrgemm2_abpd(self): if not cupy.cusparse.check_availability('csrgemm2'): pytest.skip('csrgemm2 is not available.') a = sparse.csr_matrix(self.a) b = sparse.csr_matrix(self.b) d = sparse.csr_matrix(self.d) c = cupy.cusparse.csrgemm2(a, b, d=d, alpha=self.alpha, beta=self.beta) expect = self.alpha * self.a.dot(self.b) + self.beta * self.d testing.assert_array_almost_equal(c.toarray(), expect.toarray())
def test_csrmvEx(self): if self.transa: # no support for transa return a = sparse.csr_matrix(self.a) x = cupy.array(self.x, order='f') y = cupy.cusparse.csrmvEx(a, x, alpha=self.alpha) expect = self.alpha * self.op_a.dot(self.x) testing.assert_array_almost_equal(y, expect)
def test_spmv(self): if not cupy.cusparse.check_availability('spmv'): pytest.skip('spmv is not available') a = self.sparse_matrix(self.a) if not a.has_canonical_format: a.sum_duplicates() x = cupy.array(self.x) y = cupy.cusparse.spmv(a, x, alpha=self.alpha, transa=self.transa) expect = self.alpha * self.op_a.dot(self.x) testing.assert_array_almost_equal(y, expect)
def test_weighted_moments_hu(): whu = regionprops(SAMPLE, intensity_image=INTENSITY_SAMPLE)[0].weighted_moments_hu # fmt: off ref = cp.array([ 3.1750587329e-01, 2.1417517159e-02, 2.3609322038e-02, 1.2565683360e-03, 8.3014209421e-07, -3.5073773473e-05, -6.7936409056e-06 ]) # fmt: on assert_array_almost_equal(whu, ref)
def test_fundamental_matrix_estimation(xp): # fmt: off src = xp.array([ 1.839035, 1.924743, 0.543582, 0.375221, # noqa 0.473240, 0.142522, 0.964910, 0.598376, # noqa 0.102388, 0.140092, 15.994343, 9.622164, # noqa 0.285901, 0.430055, 0.091150, 0.254594 ]).reshape(-1, 2) # noqa dst = xp.array([ 1.002114, 1.129644, 1.521742, 1.846002, # noqa 1.084332, 0.275134, 0.293328, 0.588992, # noqa 0.839509, 0.087290, 1.779735, 1.116857, # noqa 0.878616, 0.602447, 0.642616, 1.028681 ]).reshape(-1, 2) # noqa # fmt: on tform = estimate_transform('fundamental', src, dst) # Reference values obtained using COLMAP SfM library. # fmt: off tform_ref = xp.array([ [-0.217859, 0.419282, -0.0343075], # noqa [-0.0717941, 0.0451643, 0.0216073], # noqa [0.248062, -0.429478, 0.0221019] ]) # noqa # fmt: on if xp == cp: # TODO: grlee77: why is there a sign difference here for CuPy? tform_ref = -tform_ref assert_array_almost_equal(tform.params, tform_ref, 6)
def test_weighted_moments_normalized(): wnu = regionprops( SAMPLE, intensity_image=INTENSITY_SAMPLE)[0].weighted_moments_normalized ref = cp.asarray([ [cp.nan, cp.nan, 0.2301467830, -0.0162529732], [cp.nan, -0.0160405109, 0.0457932622, -0.0104598869], [0.0873590903, -0.0031421072, 0.0165315478, -0.0028544152], [-0.0161217406, -0.0031376984, 0.0043903193, -0.0011057191], ]) assert_array_almost_equal(wnu, ref)
def test_spmm(self): if not cupy.cusparse.check_availability('spmm'): pytest.skip('spmm is not available') a = self.sparse_matrix(self.a) if not a.has_canonical_format: a.sum_duplicates() b = cupy.array(self.b, order='f') c = cupy.cusparse.spmm( a, b, alpha=self.alpha, transa=self.transa, transb=self.transb) expect = self.alpha * self.op_a.dot(self.op_b) testing.assert_array_almost_equal(c, expect)
def test_csrmm_with_c(self): if not cusparse.check_availability('csrmm'): pytest.skip('csrmm is not available') a = sparse.csr_matrix(self.a) b = cupy.array(self.b, order='f') c = cupy.array(self.c, order='f') y = cupy.cusparse.csrmm( a, b, c=c, alpha=self.alpha, beta=self.beta, transa=self.transa) expect = self.alpha * self.op_a.dot(self.b) + self.beta * self.c assert y is c testing.assert_array_almost_equal(y, expect)
def test_csrmv_with_y(self): if not cusparse.check_availability('csrmv'): pytest.skip('csrmv is not available') a = sparse.csr_matrix(self.a) x = cupy.array(self.x, order='f') y = cupy.array(self.y, order='f') z = cupy.cusparse.csrmv( a, x, y=y, alpha=self.alpha, beta=self.beta, transa=self.transa) expect = self.alpha * self.op_a.dot(self.x) + self.beta * self.y assert y is z testing.assert_array_almost_equal(y, expect)
def test_fundamental_matrix_residuals(xp): essential_matrix_tform = EssentialMatrixTransform(rotation=xp.eye(3), translation=xp.array( [1, 0, 0]), xp=xp) tform = FundamentalMatrixTransform() tform.params = essential_matrix_tform.params src = xp.array([[0, 0], [0, 0], [0, 0]]) dst = xp.array([[2, 0], [2, 1], [2, 2]]) assert_array_almost_equal( tform.residuals(src, dst)**2, xp.array([0, 0.5, 2]))
def test_warp_callable(): x = cp.zeros((5, 5), dtype=np.double) x[2, 2] = 1 refx = cp.zeros((5, 5), dtype=np.double) refx[1, 1] = 1 def shift(xy): return xy + 1 outx = warp(x, shift, order=1) assert_array_almost_equal(outx, refx)
def test_isodata_moon_image_negative_float(): moon = util.img_as_ubyte(moond).astype(cp.float64) moon -= 100 assert -14 < threshold_isodata(moon) < -13 thresholds = threshold_isodata(moon, return_all=True) # fmt: off assert_array_almost_equal(thresholds, [-13.83789062, -12.84179688, -11.84570312, 22.02148438, # noqa 23.01757812, 24.01367188, 38.95507812, 39.95117188]) # noqa
def test_csrmvEx_with_y(self): if self.transa: # no support for transa return a = sparse.csr_matrix(self.a) x = cupy.array(self.x, order='f') y = cupy.array(self.y, order='f') z = cupy.cusparse.csrmvEx(a, x, y=y, alpha=self.alpha, beta=self.beta) expect = self.alpha * self.op_a.dot(self.x) + self.beta * self.y self.assertIs(y, z) testing.assert_array_almost_equal(y, expect)
def test_gesvdj_no_uv(self): a = cupy.array(self.a, order=self.order) s = cusolver.gesvdj(a, full_matrices=self.full_matrices, compute_uv=False, overwrite_a=self.overwrite_a) expect = numpy.linalg.svd(self.a, full_matrices=self.full_matrices, compute_uv=False) if self.dtype in (numpy.float32, numpy.complex64): decimal = 5 else: decimal = 10 testing.assert_array_almost_equal(s, expect, decimal=decimal)
def test_resize3d_resize(): # resize 3rd dimension x = cp.zeros((5, 5, 3), dtype=np.double) x[1, 1, :] = 1 resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10, 1)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_multiple_modes_uniform(): # Test uniform filter for multiple extrapolation modes arr = cp.array([[1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 0.0, 0.0]]) expected = cp.array([[0.32, 0.40, 0.48], [0.20, 0.28, 0.32], [0.28, 0.32, 0.40]]) modes = ["reflect", "wrap"] assert_array_almost_equal(expected, sndi.uniform_filter(arr, 5, mode=modes))
def test_resize3d_2din_3dout(): # 3D output with 2D input x = cp.zeros((5, 5), dtype=np.double) x[1, 1] = 1 resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10, 1)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)