def test_uncropped_mask(): """Test that BinaryMaskCollection.uncropped_mask() works correctly. """ label_image_array, physical_ticks = label_array_2d() label_image = LabelImage.from_label_array_and_ticks( label_image_array, None, physical_ticks, None, ) mask_collection = BinaryMaskCollection.from_label_image(label_image) assert len(mask_collection) == 2 region_0 = mask_collection.uncropped_mask(0) assert region_0.shape == label_image_array.shape assert region_0.dtype == np.bool assert np.all(region_0[0] == 1) assert np.all(region_0[1:5] == 0) region_1 = mask_collection.uncropped_mask(1) assert region_1.shape == label_image_array.shape assert region_1.dtype == np.bool assert np.all(region_1[0:3, :] == 0) assert np.all(region_1[:, 0:3] == 0) assert np.all(region_1[3:5, 3:6] == [[1, 1, 1], [1, 1, 0]])
def test_from_empty_label_image(tmp_path): label_array, physical_ticks = label_array_2d() label_array.fill(0) label_image = LabelImage.from_label_array_and_ticks( label_array, None, physical_ticks, None, ) binary_mask_collection = BinaryMaskCollection.from_label_image(label_image) masks = list(binary_mask_collection.masks()) assert len(masks) == 0 path = tmp_path / "data.tgz" binary_mask_collection.to_targz(path) masks2 = BinaryMaskCollection.open_targz(path) for m, m2 in zip(binary_mask_collection.masks(), masks2.masks()): assert np.array_equal(m, m2) # ensure that the regionprops are equal for ix in range(len(binary_mask_collection)): original_props = binary_mask_collection.mask_regionprops(ix) recalculated_props = binary_mask_collection.mask_regionprops(ix) assert original_props == recalculated_props
def test_to_label_image(): # test via roundtrip label_image_array, physical_ticks = label_array_2d() label_image = LabelImage.from_label_array_and_ticks( label_image_array, None, physical_ticks, None, ) masks = BinaryMaskCollection.from_label_image(label_image) assert np.array_equal(masks.to_label_image().xarray, label_image.xarray)
def test_uncropped_mask_no_uncropping(): """If the mask doesn't need to be uncropped, it should still work. This is an optimized code path, so it is separately validated. """ label_image_array, physical_ticks = label_array_2d() label_image_array.fill(1) label_image = LabelImage.from_label_array_and_ticks( label_image_array, None, physical_ticks, None, ) mask_collection = BinaryMaskCollection.from_label_image(label_image) assert len(mask_collection) == 1 region = mask_collection.uncropped_mask(0) assert region.shape == label_image_array.shape assert np.all(region == 1)
def to_label_image(self) -> LabelImage: shape = tuple( len(self._pixel_ticks[axis]) for axis in (Axes.ZPLANE, Axes.Y, Axes.X) if axis in self._pixel_ticks) label_image_array = np.zeros(shape=shape, dtype=np.uint16) for ix, mask_data in self._masks.items(): fill_from_mask( mask_data.binary_mask, mask_data.offsets, ix + 1, label_image_array, ) return LabelImage.from_label_array_and_ticks( label_image_array, self._pixel_ticks, self._physical_ticks, self._log, )
def from_external_labeled_image(cls, path_to_labeled_image: Union[str, Path], original_image: ImageStack): """ Construct a BinaryMaskCollection from an external label image. Ex. the output from ilastik object classification Parameters ---------- path_to_labeled_image : Union[str, Path] Path to an external label image file original_image : ImageStack Dapi image used in external segmentation workflow Returns ------- BinaryMaskCollection """ # Load the label image generated from another program label_image = io.imread(path_to_labeled_image) # Get the physical ticks from the original dapi image physical_ticks = { Coordinates.Y: original_image.xarray.yc.values, Coordinates.X: original_image.xarray.xc.values } # Get the pixel values from the original dapi image pixel_coords = { Axes.Y: original_image.xarray.y.values, Axes.X: original_image.xarray.x.values } # Create the label image label_im = LabelImage.from_label_array_and_ticks( label_image, pixel_ticks=pixel_coords, physical_ticks=physical_ticks, log=original_image.log) # Create the mask collection return BinaryMaskCollection.from_label_image(label_im)
def test_from_label_image(): label_image_array, physical_ticks = label_array_2d() label_image = LabelImage.from_label_array_and_ticks( label_image_array, None, physical_ticks, None, ) mask_collection = BinaryMaskCollection.from_label_image(label_image) assert len(mask_collection) == 2 region_0, region_1 = mask_collection.masks() assert region_0.name == '0' assert region_1.name == '1' assert np.array_equal(region_0, np.ones((1, 6), dtype=np.bool)) temp = np.ones((2, 3), dtype=np.bool) temp[-1, -1] = False assert np.array_equal(region_1, temp) assert np.array_equal(region_0[Axes.Y.value], [0]) assert np.array_equal(region_0[Axes.X.value], [0, 1, 2, 3, 4, 5]) assert np.array_equal(region_1[Axes.Y.value], [3, 4]) assert np.array_equal(region_1[Axes.X.value], [3, 4, 5]) assert np.array_equal(region_0[Coordinates.Y.value], physical_ticks[Coordinates.Y][0:1]) assert np.array_equal(region_0[Coordinates.X.value], physical_ticks[Coordinates.X][0:6]) assert np.array_equal(region_1[Coordinates.Y.value], physical_ticks[Coordinates.Y][3:5]) assert np.array_equal(region_1[Coordinates.X.value], physical_ticks[Coordinates.X][3:6])