Ejemplo n.º 1
0
    def it_knows_its_scaled_slides(self, request, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        slide1 = instance_mock(request, Slide)
        slide2 = instance_mock(request, Slide)

        slideset = SlideSet(tmp_path_, os.path.join(tmp_path_, "processed"),
                            [])
        slides = method_mock(request, SlideSet, "__iter__")
        slides.return_value = [slide1, slide2]
        slideset.scaled_images(32, 2)

        slide1.scaled_image.assert_called_once_with(32)
        slide2.scaled_image.assert_called_once_with(32)
Ejemplo n.º 2
0
    def it_knows_its_thumbnails(self, request, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        thumbnail_ = property_mock(request, Slide, "thumbnail")
        slide1 = Slide("foo/bar", "proc")
        slide2 = Slide("foo/bar", "proc")

        slideset = SlideSet(tmp_path_, os.path.join(tmp_path_, "processed"),
                            [])
        slides = method_mock(request, SlideSet, "__iter__")
        slides.return_value = [slide1, slide2]

        slideset.thumbnails()

        assert thumbnail_.call_args_list == [call(), call()]
Ejemplo n.º 3
0
    def it_knows_the_slides_dimensions(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        image2 = PILIMG.RGBA_COLOR_50X50_155_0_0
        image2.save(os.path.join(tmp_path_, "mywsi2.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, "proc", [".svs"])

        slides_dimensions = slideset._slides_dimensions

        expected_value = [
            {
                "slide": "mywsi",
                "width": 500,
                "height": 500,
                "size": 250000
            },
            {
                "slide": "mywsi2",
                "width": 50,
                "height": 50,
                "size": 2500
            },
        ]
        assert dict_list_eq(slides_dimensions, expected_value) is True
Ejemplo n.º 4
0
    def it_knows_its_total_slides(self, request, Slide_):
        slides = property_mock(request, SlideSet, "slides")
        slides.return_value = [Slide_ for _ in range(4)]
        slideset = SlideSet("the/path", "proc", [".svs"])

        total_slides = slideset.total_slides

        assert total_slides == 4
Ejemplo n.º 5
0
    def it_constructs_its_sequence_of_slides_to_help(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        image2 = PILIMG.RGBA_COLOR_50X50_155_0_0
        image2.save(os.path.join(tmp_path_, "mywsi2.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, "proc", [".svs"])
        expected_slides = [
            Slide(os.path.join(tmp_path_, _path), "proc")
            for _path in os.listdir(tmp_path_)
        ]

        slides = slideset.__iter__()

        for i, slide in enumerate(slides):
            np.testing.assert_array_almost_equal(
                slide.resampled_array(), expected_slides[i].resampled_array())
Ejemplo n.º 6
0
    def it_knows_its_total_slides(self, request, Slide_):
        slides = method_mock(request, SlideSet, "__iter__")
        slides.return_value = [Slide_ for _ in range(4)]
        slideset = SlideSet("the/path", "proc", [".svs"])

        total_slides = len(slideset)

        assert total_slides == 4
Ejemplo n.º 7
0
    def it_knows_its_slides(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, "proc", [".svs"])

        assert len(slideset) == 1

        slideset = SlideSet(None, "proc", [".svs"])

        assert len(slideset) == 0

        with pytest.raises(FileNotFoundError) as err:
            slideset = SlideSet("fake/path", "proc", [".svs"])
            list(slideset)

        assert isinstance(err.value, FileNotFoundError)
        assert err.value.errno == errno.ENOENT
Ejemplo n.º 8
0
    def and_it_is_exaclty_what_expected(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, "proc", [".svs"])

        slide = slideset[0]

        np.testing.assert_array_almost_equal(slide.resampled_array(),
                                             slideset[0].resampled_array())
Ejemplo n.º 9
0
    def it_can_constructs_slides(self, request, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        slides_ = tuple(instance_mock(request, Slide) for _ in range(10))
        _slides_ = property_mock(request, SlideSet, "slides")
        _slides_.return_value = slides_
        slideset = SlideSet(tmp_path_, os.path.join(tmp_path_, "b"), [".svs"])

        slides = slideset.slides

        _slides_.assert_called_once_with()
        assert len(slides) == 10
Ejemplo n.º 10
0
    def it_constructs_from_args(self, request):
        _init_ = initializer_mock(request, SlideSet)
        _slides_path = "/foo/bar/"
        _processed_path = "/foo/bar/wsislides/processed"
        _valid_extensions = [".svs", ".tiff"]

        slideset = SlideSet(_slides_path, _processed_path, _valid_extensions)

        _init_.assert_called_once_with(ANY, _slides_path, _processed_path,
                                       _valid_extensions)
        assert isinstance(slideset, SlideSet)
Ejemplo n.º 11
0
    def it_knows_its_slides_dimensions_list(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        image2 = PILIMG.RGBA_COLOR_50X50_155_0_0
        image2.save(os.path.join(tmp_path_, "mywsi2.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, "proc", [".svs"])

        _slides_dimensions_list = slideset._slides_dimensions_list

        assert sorted(_slides_dimensions_list) == sorted([(500, 500),
                                                          (50, 50)])
Ejemplo n.º 12
0
    def it_knows_its_slides(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILImageMock.DIMS_500X500_RGBA_COLOR_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, "proc", [".svs"])

        slides = slideset.slides

        assert len(slides) == 1

        slideset = SlideSet(None, "proc", [".svs"])

        slides = slideset.slides

        assert len(slides) == 0

        with pytest.raises(FileNotFoundError) as err:
            slideset = SlideSet("fake/path", "proc", [".svs"])
            slides = slideset.slides

        assert isinstance(err.value, FileNotFoundError)
        assert str(
            err.value) == "[Errno 2] No such file or directory: 'fake/path'"
Ejemplo n.º 13
0
def extract_random_tiles(
    dataset_dir: str,
    processed_path: str,
    tile_size: Tuple[int, int],
    n_tiles: int,
    level: int,
    seed: int,
    check_tissue: bool,
) -> None:
    """Save random tiles extracted from WSIs in `dataset_dir` into
    `processed_path`/tiles/

    Parameters
    ----------
    dataset_dir : str
        Path were the WSIs are saved
    processed_path : str
        Path where to store the tiles (will be concatenated with /tiles)
    tile_size : Tuple[int, int]
        width and height of the cropped tiles
    n_tiles : int
        Maximum number of tiles to extract
    level : int
        Magnification level from which extract the tiles
    seed : int
        Seed for RandomState
    check_tissue : bool
        Whether to check if the tile has enough tissue to be saved
    """
    slideset = SlideSet(dataset_dir, processed_path, valid_extensions=[".svs"])

    for slide in tqdm(slideset.slides):
        prefix = f"{slide.name}_"
        random_tiles_extractor = RandomTiler(
            tile_size=tile_size,
            n_tiles=n_tiles,
            level=level,
            seed=seed,
            check_tissue=check_tissue,
            prefix=prefix,
        )

        random_tiles_extractor.extract(slide)
Ejemplo n.º 14
0
    def it_knows_its_min_size_slide(self, _slides_dimensions_prop):
        _slides_dimensions_prop.return_value = [
            {
                "slide": "mywsi",
                "width": 500,
                "height": 100,
                "size": 250000
            },
            {
                "slide": "mywsi2",
                "width": 600,
                "height": 50,
                "size": 2500
            },
        ]
        slideset = SlideSet("fake/path", "proc", [".svs"])

        _min_size_slide = slideset._min_size_slide

        assert _min_size_slide == {"slide": "mywsi2", "size": 2500}
Ejemplo n.º 15
0
    def it_generates_slides_stats(self, total_slides_prop, tmpdir):
        total_slides_prop.return_value = 2
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGBA_COLOR_500X500_155_249_240
        image.save(os.path.join(tmp_path_, "mywsi.svs"), "TIFF")
        image2 = PILIMG.RGBA_COLOR_50X50_155_0_0
        image2.save(os.path.join(tmp_path_, "mywsi2.svs"), "TIFF")
        slideset = SlideSet(tmp_path_, os.path.join("proc"), [".svs"])

        slides_stats = slideset.slides_stats

        assert slides_stats == {
            "no_of_slides": 2,
            "max_width": {
                "slide": "mywsi",
                "width": 500
            },
            "max_height": {
                "slide": "mywsi",
                "height": 500
            },
            "max_size": {
                "slide": "mywsi",
                "size": 250000
            },
            "min_width": {
                "slide": "mywsi2",
                "width": 50
            },
            "min_height": {
                "slide": "mywsi2",
                "height": 50
            },
            "min_size": {
                "slide": "mywsi2",
                "size": 2500
            },
            "avg_width": 275.0,
            "avg_height": 275.0,
            "avg_size": 126250.0,
        }
Ejemplo n.º 16
0
    def it_constructs_its_sequence_of_slides_to_help(self, request, Slide_,
                                                     tmpdir):
        slides_path = tmpdir.mkdir("mypath")
        for i in range(4):
            open(os.path.join(slides_path, f"myfile{i}.svs"), "a")
        slides_ = tuple(instance_mock(request, Slide) for _ in range(4))
        Slide_.side_effect = iter(slides_)
        slide_set = SlideSet(
            slides_path=slides_path,
            processed_path=os.path.join(slides_path, "processed"),
            valid_extensions=[".svs"],
        )
        slides = tuple(slide_set.slides)

        assert sorted(Slide_.call_args_list) == sorted([
            call(
                os.path.join(slides_path, f"myfile{i}.svs"),
                os.path.join(slides_path, "processed"),
            ) for i in range(4)
        ])
        assert slides == slides_
Ejemplo n.º 17
0
    def it_knows_its_avg_size_slide(self, _slides_dimensions_prop,
                                    total_slides_prop):
        total_slides_prop.return_value = 2
        _slides_dimensions_prop.return_value = [
            {
                "slide": "mywsi",
                "width": 500,
                "height": 100,
                "size": 250000
            },
            {
                "slide": "mywsi2",
                "width": 50,
                "height": 50,
                "size": 2500
            },
        ]
        slideset = SlideSet("fake/path", "proc", [".svs"])

        _avg_size_slide = slideset._avg_size_slide

        assert _avg_size_slide == 126250.0
        assert (_avg_size_slide == sum(
            d["size"] for d in _slides_dimensions_prop.return_value) / 2)