def roundtrip(self, dtype, x): f = NamedTemporaryFile(suffix='.tif') fname = f.name f.close() imsave(fname, x) y = imread(fname) assert_array_equal(x, y)
def test_all_negative_image(): im = np.array([-128, -1], dtype=np.int8) frequencies, bin_centers = exposure.histogram(im) assert_array_equal(bin_centers, np.arange(-128, 0)) assert frequencies[0] == 1 assert frequencies[-1] == 1 assert_array_equal(frequencies[1:-1], 0)
def test_negative_overflow(): im = np.array([-1, 127], dtype=np.int8) frequencies, bin_centers = exposure.histogram(im) assert_array_equal(bin_centers, np.arange(-1, 128)) assert frequencies[0] == 1 assert frequencies[-1] == 1 assert_array_equal(frequencies[1:-1], 0)
def test_img_as_gray_flatten(): img = imread(os.path.join(data_dir, 'color.png'), as_gray=True) with expected_warnings(['deprecated']): img_flat = imread(os.path.join(data_dir, 'color.png'), flatten=True) assert_array_equal(img, img_flat)
def test_skeletonize_no_foreground(self): im = np.zeros((5, 5)) result = skeletonize(im) assert_array_equal(result, np.zeros((5, 5)))
def test_ellipse_generic(): img = np.zeros((4, 4), 'uint8') rr, cc = ellipse(1.5, 1.5, 1.1, 1.7) img[rr, cc] = 1 img_ = np.array([ [0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0], ]) assert_array_equal(img, img_) img = np.zeros((5, 5), 'uint8') rr, cc = ellipse(2, 2, 1.7, 1.7) img[rr, cc] = 1 img_ = np.array([ [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], ]) assert_array_equal(img, img_) img = np.zeros((10, 10), 'uint8') rr, cc = ellipse(5, 5, 3, 4) img[rr, cc] = 1 img_ = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]) assert_array_equal(img, img_) img = np.zeros((10, 10), 'uint8') rr, cc = ellipse(4.5, 5, 3.5, 4) img[rr, cc] = 1 img_ = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]) assert_array_equal(img, img_) img = np.zeros((15, 15), 'uint8') rr, cc = ellipse(7, 7, 3, 7) img[rr, cc] = 1 img_ = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1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 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]]) assert_array_equal(img, img_)
def test_filled_image(): img = regionprops(SAMPLE)[0].filled_image assert_array_equal(img, SAMPLE)
def test_image(): img = regionprops(SAMPLE)[0].image assert_array_equal(img, SAMPLE) img = regionprops(SAMPLE_3D)[0].image assert_array_equal(img, SAMPLE_3D[1:4, 1:3, 1:3])
def test_flat_int_range_dtype(): im = np.linspace(-128, 128, 256, dtype=np.int8) frequencies, bin_centers = exposure.histogram(im, source_range='dtype') assert_array_equal(bin_centers, np.arange(-128, 128)) assert_equal(frequencies.shape, (256,))
def test_basic(): x = np.array([[1, 1, 3], [0, 2, 0], [4, 3, 1]]) path, cost = spath.shortest_path(x) assert_array_equal(path, [0, 0, 1]) assert_equal(cost, 1)
def test_non_square(): x = np.array([[1, 1, 1, 1, 5, 5, 5], [5, 0, 0, 5, 9, 1, 1], [0, 5, 1, 0, 5, 5, 0], [6, 1, 1, 5, 0, 0, 1]]) path, cost = spath.shortest_path(x, reach=2) assert_array_equal(path, [2, 1, 1, 2, 3, 3, 2]) assert_equal(cost, 0)
def test_reach(): x = np.array([[1, 1, 3], [0, 2, 0], [4, 3, 1]]) path, cost = spath.shortest_path(x, reach=2) assert_array_equal(path, [0, 0, 2]) assert_equal(cost, 0)
def test_1d_erosion(): image = np.array([1, 2, 3, 2, 1]) expected = np.array([1, 1, 2, 1, 1]) eroded = gray.erosion(image) testing.assert_array_equal(eroded, expected)
def _check_maps(ar, ar_relab, fw, inv): assert_array_equal(fw[ar], ar_relab) assert_array_equal(inv[ar_relab], ar)
def test_arraymap_call(): ar = np.array([1, 1, 5, 5, 8, 99, 42, 0], dtype=np.intp) relabeled, fw, inv = relabel_sequential(ar) testing.assert_array_equal(relabeled, fw(ar)) testing.assert_array_equal(ar, inv(relabeled))
def test_adjust_gamma_one(): """Same image should be returned for gamma equal to one""" image = np.random.uniform(0, 255, (8, 8)) result = exposure.adjust_gamma(image, 1) assert_array_equal(result, image)
def test_adjust_gamma_zero(): """White image should be returned for gamma equal to zero""" image = np.random.uniform(0, 255, (8, 8)) result = exposure.adjust_gamma(image, 0) dtype = image.dtype.type assert_array_equal(result, dtype_range[dtype][1])
def test_consistent_2d_3d_hulls(images2d3d): image, image3d = images2d3d chimage = convex_hull_image(image) chimage[8, 0] = True # correct for single point exactly on hull edge chimage3d = convex_hull_image(image3d) assert_array_equal(chimage3d[1], chimage)
def test_peak_float_out_of_range_image(): im = np.array([10, 100], dtype=np.float16) frequencies, bin_centers = exposure.histogram(im, nbins=90) # offset values by 0.5 for float... assert_array_equal(bin_centers, np.arange(10, 100) + 0.5)
def test_empty_image(): image = np.zeros((6, 6), dtype=bool) with expected_warnings(['entirely zero']): assert_array_equal(convex_hull_image(image), image)
def test_label(): label = regionprops(SAMPLE)[0].label assert_array_equal(label, 1) label = regionprops(SAMPLE_3D)[0].label assert_array_equal(label, 1)
def test_intensity_range_uint8(test_input, expected): image = np.array([0, 1], dtype=np.uint8) out = intensity_range(image, range_values=test_input) assert_array_equal(out, expected)
def test_bilevel(): expected = np.zeros((10, 10)) expected[::2] = 255 img = imread(os.path.join(data_dir, 'checker_bilevel.png')) assert_array_equal(img, expected)
def test_intensity_range_float(test_input, expected): image = np.array([0.1, 0.2], dtype=np.float64) out = intensity_range(image, range_values=test_input) assert_array_equal(out, expected)
def test_bezier_segment_curved(): img = np.zeros((25, 25), 'uint8') r0, c0 = 20, 20 r1, c1 = 20, 2 r2, c2 = 2, 2 rr, cc = _bezier_segment(r0, c0, r1, c1, r2, c2, 1) img[rr, cc] = 1 img_ = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0, 0, 0,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 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]]) assert_equal(img[r0, c0], 1) assert_equal(img[r2, c2], 1) assert_array_equal(img, img_)
def test_intensity_range_clipped_float(): image = np.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_stack_basic(): x = np.arange(12).reshape(3, 4) io.push(x) assert_array_equal(io.pop(), x)
def test_lut_generation(self): g123, g123p = _generate_thin_luts() assert_array_equal(g123, G123_LUT) assert_array_equal(g123p, G123P_LUT)
def test_rescale_no_overflow(): image = np.array([-128, 0, 127], dtype=np.int8) output_image = exposure.rescale_intensity(image, out_range=np.uint8) testing.assert_array_equal(output_image, [0, 128, 255]) assert output_image.dtype == np.uint8
def test_skeletonize_single_point(self): im = np.zeros((5, 5), np.uint8) im[3, 3] = 1 result = skeletonize(im) assert_array_equal(result, im)
def test_rescale_float_output(): image = np.array([-128, 0, 127], dtype=np.int8) output_image = exposure.rescale_intensity(image, out_range=(0, 255)) testing.assert_array_equal(output_image, [0, 128, 255]) assert output_image.dtype == float
def test_bilevel(): expected = np.zeros((10, 10), bool) expected[::2] = 1 img = imread(fetch('data/checker_bilevel.png')) assert_array_equal(img.astype(bool), expected)