def test_padding_top_bottom(self, small_cont_1c: ImageContainer): dim1, dim2, _ = small_cont_1c["image"].data.shape crop = small_cont_1c.crop_center(dim1, dim2 // 2, dim1 // 2) data = crop["image"].data np.testing.assert_array_equal(data[dim1 // 2:, :], 0) crop = small_cont_1c.crop_center(0, dim2 // 2, dim1 // 2) data = crop["image"].data np.testing.assert_array_equal(data[:dim2 // 2, :], 0)
def test_padding_left_right(self, small_cont_1c: ImageContainer): dim1, dim2, _ = small_cont_1c["image"].data.shape crop = small_cont_1c.crop_center(dim1 // 2, 0, dim1 // 2) data = crop["image"].data np.testing.assert_array_equal(data[:, :dim2 // 2], 0) crop = small_cont_1c.crop_center(dim1 // 2, dim2, dim1 // 2) data = crop["image"].data np.testing.assert_array_equal(data[:, dim2 // 2:], 0)
def test_padding_bottom_left(self, small_cont_1c: ImageContainer): crop = small_cont_1c.crop_center(small_cont_1c.shape[1], 0, 10) data = crop["image"].data assert crop.shape == (21, 21) np.testing.assert_array_equal(data[10:, :10], 0) np.testing.assert_array_equal(data[:10, 10:] != 0, True)
def test_crop_center_radius(self, small_cont_1c: ImageContainer, ry: Optional[Union[int, float]], rx: Optional[Union[int, float]]): crop = small_cont_1c.crop_center(0, 0, radius=(ry, rx)) sy = int(ry * small_cont_1c.shape[0]) if isinstance(ry, float) else ry sx = int(rx * small_cont_1c.shape[1]) if isinstance(rx, float) else rx assert crop.shape == (2 * sy + 1, 2 * sx + 1)
def test_plot_crop_corner(self, qtbot, adata: AnnData, napari_cont: ImageContainer): viewer = napari_cont.crop_center(500, 500, radius=250).interactive(adata) bdata = viewer.adata cnt = viewer._controller cnt.add_points(bdata.obs_vector(bdata.var_names[42]), layer_name="foo") viewer.screenshot(dpi=DPI)
def test_crop_smapp(self, cont_dot: ImageContainer): crop = cont_dot.crop_center( x=50, y=20, radius=0, cval=5, ) np.testing.assert_array_equal(crop.data["image_0"].shape, (1, 1, 10)) np.testing.assert_array_equal(crop.data["image_0"][0, 0, :3], [10, 11, 12]) assert crop.data["image_0"].dtype == np.uint8
def test_crop_scale(self, cont_dot: ImageContainer): # crop with scaling mask = np.random.randint(low=0, high=10, size=cont_dot.shape) cont_dot.add_img(mask, layer="image_1", channel_dim="mask") crop = cont_dot.crop_center(y=50, x=20, radius=10, cval=5, scale=0.5) assert "image_0" in crop assert "image_1" in crop np.testing.assert_array_equal(crop.data["image_0"].shape, (21 // 2, 21 // 2, 10)) np.testing.assert_array_equal(crop.data["image_1"].shape, (21 // 2, 21 // 2, 1))
def test_crop_mask_circle(self, cont_dot: ImageContainer): # crop with mask_circle crop = cont_dot.crop_center( y=20, x=50, radius=5, cval=5, mask_circle=True, ) np.testing.assert_array_equal(crop.data["image_0"][1, 0, :], 5) np.testing.assert_array_equal(crop.data["image_0"][2, 2, :], 0) np.testing.assert_array_equal(crop.data["image_0"][7, 7, :], 0) np.testing.assert_array_equal(crop.data["image_0"][9, 9, :], 5)
def test_crop_multiple_images(self, cont_dot: ImageContainer): mask = np.random.randint(low=0, high=10, size=cont_dot.shape) cont_dot.add_img(mask, layer="image_1", channel_dim="mask") crop = cont_dot.crop_center( y=50, x=20, radius=0, cval=5, ) assert "image_0" in crop assert "image_1" in crop np.testing.assert_array_equal(crop.data["image_0"].shape, (1, 1, 10)) np.testing.assert_array_equal(crop.data["image_1"].shape, (1, 1, 1))
def test_big_crop(self, cont_dot: ImageContainer): crop = cont_dot.crop_center( y=50, x=20, radius=150, cval=5, ) np.testing.assert_array_equal(crop.data["image_0"].shape, (301, 301, 10)) # check that values outside of img are padded with 5 np.testing.assert_array_equal(crop.data["image_0"][0, 0, 0], 5) np.testing.assert_array_equal(crop.data["image_0"][-1, -1, 0], 5) assert crop.data["image_0"].dtype == np.uint8 # compare with crop_corner crop2 = cont_dot.crop_corner(y=-100, x=-130, size=301, cval=5) np.testing.assert_array_equal(crop2.data["image_0"], crop.data["image_0"])