def test_crop_corner_scale(self, scale: float): shape_img = (50, 50) img = ImageContainer(np.zeros(shape_img)) if scale <= 0: with pytest.raises( ValueError, match=r"Expected `scale` to be positive, found `0`."): img.crop_corner(10, 10, size=20, scale=scale) else: crop = img.crop_corner(10, 10, size=20, scale=scale) assert crop.shape == tuple(int(i * scale) for i in (20, 20))
def test_plot_crop_center(self, qtbot, adata: AnnData, napari_cont: ImageContainer): viewer = napari_cont.crop_corner(0, 0, size=500).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_metadata(self, small_cont_1c: ImageContainer, dy: int): crop = small_cont_1c.crop_corner(dy, 0, 50, mask_circle=True) assert small_cont_1c.data.attrs[Key.img.coords] is _NULL_COORDS assert crop.data.attrs[Key.img.coords] == CropCoords(0, 0, 50, 50 + dy) assert crop.data.attrs[Key.img.padding] == CropPadding(x_pre=0, y_pre=abs(dy), x_post=0, y_post=0) assert crop.data.attrs[Key.img.mask_circle]
def test_crop_corner_mask_circle(self, small_cont_1c: ImageContainer, size: Tuple[int, int]): if size[0] != size[1]: with pytest.raises(ValueError, match=r"Masking circle is only"): small_cont_1c.crop_corner(0, 0, size=size, mask_circle=True, cval=np.nan) else: crop = small_cont_1c.crop_corner(0, 0, size=20, mask_circle=True, cval=np.nan) mask = (crop.data.x - 10)**2 + (crop.data.y - 10)**2 <= 10**2 assert crop.shape == (20, 20) np.testing.assert_array_equal( crop["image"].values[..., 0][~mask.values], np.nan)
def test_preserve_dtypes(self, cont: ImageContainer, preserve: bool): assert np.issubdtype(cont["image"].dtype, np.uint8) crop = cont.crop_corner(-10, -10, 20, cval=-5, preserve_dtypes=preserve) if preserve: assert np.issubdtype(crop["image"].dtype, np.uint8) # we specifically use 0, otherwise overflow would happend and the value would be 256 - 5 np.testing.assert_array_equal(crop["image"][:10, :10], 0) else: assert np.issubdtype(crop["image"].dtype, np.signedinteger) np.testing.assert_array_equal(crop["image"][:10, :10], -5)
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"])
def test_crop_corner_size(self, small_cont_1c: ImageContainer, dy: Optional[Union[int, float]], dx: Optional[Union[int, float]]): crop = small_cont_1c.crop_corner(dy, dx, size=20) # original coordinates ody, odx = max(dy, 0), max(dx, 0) ody = int(ody * small_cont_1c.shape[0]) if isinstance(ody, float) else ody odx = int(odx * small_cont_1c.shape[1]) if isinstance(odx, float) else odx # crop coordinates cdy = 0 if isinstance(dy, float) or dy > 0 else dy cdx = 0 if isinstance(dx, float) or dx > 0 else dx cdy, cdx = abs(cdy), abs(cdx) assert crop.shape == (20, 20) cdata, odata = crop["image"].data, small_cont_1c["image"].data cdata = cdata[cdy:, cdx:] np.testing.assert_array_equal( cdata, odata[ody:ody + cdata.shape[0], odx:odx + cdata.shape[1]])
def test_test_crop_corner_cval(self, cval: float): shape_img = (50, 50) img = ImageContainer(np.zeros(shape_img)) crop = img.crop_corner(10, 10, cval=cval) np.testing.assert_array_equal(crop["image"].data[-10:, -10:], cval)