Ejemplo n.º 1
0
    def it_knows_its_biggest_regions(self):
        regions = [
            Region(index=0,
                   area=14,
                   bbox=(0, 0, 2, 2),
                   center=(0.5, 0.5),
                   coords=None),
            Region(index=1,
                   area=2,
                   bbox=(0, 0, 2, 2),
                   center=(0.5, 0.5),
                   coords=None),
            Region(index=2,
                   area=5,
                   bbox=(0, 0, 2, 2),
                   center=(0.5, 0.5),
                   coords=None),
            Region(index=3,
                   area=10,
                   bbox=(0, 0, 2, 2),
                   center=(0.5, 0.5),
                   coords=None),
        ]
        binary_mask = BiggestTissueBoxMask

        biggest_regions = binary_mask._regions(regions, 2)
        assert biggest_regions == [regions[0], regions[3]]
Ejemplo n.º 2
0
def test_regions_to_binary_mask():
    regions = [
        Region(
            index=None,
            area=None,
            bbox=None,
            center=None,
            coords=np.array([[1, 3], [2, 3], [3, 2], [3, 3]]),
        ),
        Region(
            index=None,
            area=None,
            bbox=None,
            center=None,
            coords=np.array([[3, 7], [4, 7], [4, 8]]),
        ),
    ]

    binary_mask_regions = regions_to_binary_mask(regions, dims=(10, 10))

    assert type(binary_mask_regions) == np.ndarray
    assert binary_mask_regions.dtype == bool
    np.testing.assert_array_almost_equal(
        binary_mask_regions,
        load_expectation("mask-arrays/regions-to-binary-mask", type_="npy"),
    )
Ejemplo n.º 3
0
    def it_knows_its_biggest_regions(self):
        regions = [
            Region(index=0, area=14, bbox=(0, 0, 2, 2), center=(0.5, 0.5)),
            Region(index=1, area=2, bbox=(0, 0, 2, 2), center=(0.5, 0.5)),
            Region(index=2, area=5, bbox=(0, 0, 2, 2), center=(0.5, 0.5)),
            Region(index=3, area=10, bbox=(0, 0, 2, 2), center=(0.5, 0.5)),
        ]
        slide = Slide("a/b", "c/d")

        biggest_regions = slide._biggest_regions(regions, 2)

        assert biggest_regions == [regions[0], regions[3]]
Ejemplo n.º 4
0
    def it_knows_regions_from_binary_mask(self, request):
        binary_mask = np.array([[True, False], [True, True]])
        label = function_mock(request, "histolab.util.label")
        regionprops = function_mock(request, "histolab.util.regionprops")
        RegionProps = namedtuple("RegionProps", ("area", "bbox", "centroid"))
        regions_props = [
            RegionProps(3, (0, 0, 2, 2),
                        (0.6666666666666666, 0.3333333333333333))
        ]
        regionprops.return_value = regions_props
        label(binary_mask).return_value = [[0, 1], [1, 1]]

        regions_from_binary_mask_ = regions_from_binary_mask(binary_mask)

        regionprops.assert_called_once_with(label(binary_mask))
        assert type(regions_from_binary_mask_) == list
        assert len(regions_from_binary_mask_) == 1
        assert type(regions_from_binary_mask_[0]) == Region
        assert regions_from_binary_mask_ == [
            Region(
                index=0,
                area=regions_props[0].area,
                bbox=regions_props[0].bbox,
                center=regions_props[0].centroid,
            )
        ]
Ejemplo n.º 5
0
    def it_knows_its_region_coordinates(self):
        region = Region(index=0, area=14, bbox=(0, 1, 1, 2), center=(0.5, 0.5))
        slide = Slide("a/b", "c/d")

        region_coords_ = slide._region_coordinates(region)

        assert region_coords_ == CoordinatePair(x_ul=1, y_ul=0, x_br=2, y_br=1)
Ejemplo n.º 6
0
def test_region_coordinates():
    region = Region(index=0,
                    area=14,
                    bbox=(0, 1, 1, 2),
                    center=(0.5, 0.5),
                    coords=None)
    region_coords_ = region_coordinates(region)

    assert region_coords_ == CP(x_ul=1, y_ul=0, x_br=2, y_br=1)
Ejemplo n.º 7
0
    def it_knows_its_mask(
        self,
        request,
        tmpdir,
        RgbToGrayscale_,
        OtsuThreshold_,
        BinaryDilation_,
        RemoveSmallHoles_,
        RemoveSmallObjects_,
    ):
        slide, _ = base_test_slide(tmpdir,
                                   PILIMG.RGBA_COLOR_500X500_155_249_240)
        regions = [
            Region(index=0,
                   area=33,
                   bbox=(0, 0, 2, 2),
                   center=(0.5, 0.5),
                   coords=None)
        ]
        main_tissue_areas_mask_filters_ = property_mock(
            request, _SlideFiltersComposition, "tissue_mask_filters")
        main_tissue_areas_mask_filters_.return_value = Compose([
            RgbToGrayscale_,
            OtsuThreshold_,
            BinaryDilation_,
            RemoveSmallHoles_,
            RemoveSmallObjects_,
        ])
        regions_from_binary_mask = function_mock(
            request, "histolab.masks.regions_from_binary_mask")
        regions_from_binary_mask.return_value = regions
        biggest_regions_ = method_mock(request,
                                       BiggestTissueBoxMask,
                                       "_regions",
                                       autospec=False)
        biggest_regions_.return_value = regions
        region_coordinates_ = function_mock(
            request, "histolab.masks.region_coordinates")
        region_coordinates_.return_values = CP(0, 0, 2, 2)
        rectangle_to_mask_ = function_mock(request,
                                           "histolab.util.rectangle_to_mask")
        rectangle_to_mask_((1000, 1000), CP(0, 0, 2, 2)).return_value = [
            [True, True],
            [False, True],
        ]
        biggest_mask_tissue_box = BiggestTissueBoxMask()

        binary_mask = biggest_mask_tissue_box(slide)

        np.testing.assert_almost_equal(binary_mask, np.zeros((500, 500)))
        region_coordinates_.assert_called_once_with(regions[0])
        biggest_regions_.assert_called_once_with(regions, n=1)
        rectangle_to_mask_.assert_called_once_with((1000, 1000),
                                                   CP(x_ul=0,
                                                      y_ul=0,
                                                      x_br=2,
                                                      y_br=2))
Ejemplo n.º 8
0
    def it_knows_its_biggest_tissue_box_mask(
        self,
        request,
        tmpdir,
        RgbToGrayscale_,
        OtsuThreshold_,
        BinaryDilation_,
        RemoveSmallHoles_,
        RemoveSmallObjects_,
    ):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.png"), "PNG")
        slide_path = os.path.join(tmp_path_, "mywsi.png")
        slide = Slide(slide_path, "processed")
        regions = [
            Region(index=0, area=33, bbox=(0, 0, 2, 2), center=(0.5, 0.5))
        ]
        main_tissue_areas_mask_filters_ = property_mock(
            request, _SlideFiltersComposition, "tissue_mask_filters")
        main_tissue_areas_mask_filters_.return_value = Compose([
            RgbToGrayscale_,
            OtsuThreshold_,
            BinaryDilation_,
            RemoveSmallHoles_,
            RemoveSmallObjects_,
        ])
        regions_from_binary_mask = function_mock(
            request, "histolab.slide.regions_from_binary_mask")
        regions_from_binary_mask.return_value = regions
        biggest_regions_ = method_mock(request,
                                       Slide,
                                       "_biggest_regions",
                                       autospec=False)
        biggest_regions_.return_value = regions
        region_coordinates_ = function_mock(
            request, "histolab.slide.region_coordinates")
        region_coordinates_.return_values = CP(0, 0, 2, 2)
        polygon_to_mask_array_ = function_mock(
            request, "histolab.util.polygon_to_mask_array")
        polygon_to_mask_array_((1000, 1000), CP(0, 0, 2, 2)).return_value = [
            [True, True],
            [False, True],
        ]

        biggest_mask_tissue_box = slide.biggest_tissue_box_mask

        region_coordinates_.assert_called_once_with(regions[0])
        biggest_regions_.assert_called_once_with(regions, n=1)
        polygon_to_mask_array_.assert_called_once_with((1000, 1000),
                                                       CP(x_ul=0,
                                                          y_ul=0,
                                                          x_br=2,
                                                          y_br=2))
        np.testing.assert_almost_equal(biggest_mask_tissue_box,
                                       np.zeros((500, 500)))
Ejemplo n.º 9
0
    def but_it_raises_an_error_when_n_is_not_between_1_and_number_of_regions(
            self, n):
        regions = [
            Region(i, i + 1, (0, 0, 2, 2), (0.5, 0.5)) for i in range(4)
        ]
        slide = Slide("a/b", "c/d")
        with pytest.raises(ValueError) as err:
            slide._biggest_regions(regions, n)

        assert str(
            err.value) == f"n should be between 1 and {len(regions)}, got {n}"