Example #1
0
    def it_can_extract_grid_tiles(self, request, tmpdir):
        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, os.path.join(tmp_path_, "processed"))
        _tiles_generator = method_mock(request, GridTiler, "_tiles_generator")
        coords = CP(0, 10, 0, 10)
        tile = Tile(image, coords)
        _tiles_generator.return_value = [(tile, coords), (tile, coords)]
        _tile_filename = method_mock(request, GridTiler, "_tile_filename")
        _tile_filename.side_effect = [
            os.path.join(tmp_path_, "processed", "tiles",
                         f"tile_{i}_level2_0-10-0-10.png") for i in range(2)
        ]
        _has_valid_tile_size = method_mock(request, GridTiler,
                                           "_has_valid_tile_size")
        _has_valid_tile_size.return_value = True
        grid_tiler = GridTiler((10, 10), level=0)

        grid_tiler.extract(slide)

        assert _tile_filename.call_args_list == [
            call(grid_tiler, coords, 0),
            call(grid_tiler, coords, 1),
        ]
        assert os.path.exists(
            os.path.join(tmp_path_, "processed", "tiles",
                         "tile_0_level2_0-10-0-10.png"))
        assert os.path.exists(
            os.path.join(tmp_path_, "processed", "tiles",
                         "tile_1_level2_0-10-0-10.png"))
        _has_valid_tile_size.assert_called_once_with(grid_tiler, slide)
Example #2
0
    def or_it_has_not_available_level_value(self, tmpdir):
        slide, _ = base_test_slide(tmpdir, PILIMG.RGB_RANDOM_COLOR_500X500)
        binary_mask = BiggestTissueBoxMask()
        grid_tiler = GridTiler((128, 128), 3)

        with pytest.raises(LevelError) as err:
            grid_tiler.extract(slide, binary_mask)

        assert isinstance(err.value, LevelError)
        assert str(err.value
                   ) == "Level 3 not available. Number of available levels: 1"
Example #3
0
    def or_it_has_not_available_level_value(self, tmpdir):
        tmp_path_ = tmpdir.mkdir("myslide")
        image = PILIMG.RGB_RANDOM_COLOR_500X500
        image.save(os.path.join(tmp_path_, "mywsi.png"), "PNG")
        slide_path = os.path.join(tmp_path_, "mywsi.png")
        slide = Slide(slide_path, "processed")
        grid_tiler = GridTiler((128, 128), 3)

        with pytest.raises(LevelError) as err:
            grid_tiler.extract(slide)

        assert isinstance(err.value, LevelError)
        assert str(err.value) == "Level 3 not available. Number of available levels: 1"
Example #4
0
    def but_it_raises_tilesizeerror_if_tilesize_larger_than_slidesize(
            self, request, tmpdir, image, size):
        tmp_path_ = tmpdir.mkdir("myslide")
        image.save(os.path.join(tmp_path_, "mywsi.png"), "PNG")
        slide_path = os.path.join(tmp_path_, "mywsi.png")
        slide = Slide(slide_path, os.path.join(tmp_path_, "processed"))
        _has_valid_tile_size = method_mock(request, GridTiler,
                                           "_has_valid_tile_size")
        _has_valid_tile_size.return_value = False
        grid_tiler = GridTiler((50, 52), level=0)

        with pytest.raises(TileSizeError) as err:
            grid_tiler.extract(slide)

        assert isinstance(err.value, TileSizeError)
        assert (str(
            err.value
        ) == f"Tile size (50, 52) is larger than slide size {size} at level 0")
        _has_valid_tile_size.assert_called_once_with(grid_tiler, slide)
    check_tissue=
    check_tissue,  # Whether to check if the tile has enough tissue to be saved. Default is True.
    pixel_overlap=
    0,  # Number of overlapping pixels (for both height and width) between two adjacent tiles.
    prefix=
    '',  # Prefix to be added to the tile filename. Default is an empty string.
    suffix=".png"  # default
)

# Find the smallest slide
idx = meta_df['openslide.level[0].height'].isin(
    [meta_df['openslide.level[0].height'].min()])
img_name = meta_df.loc[idx, 'aperio.ImageID'].values[0]
fname = imgpath / f'{img_name}.svs'

# Slide instance
# path: path to WSI file
# processed_path: path to save thumbnails and scaled images
img_inpath = str(fname)
sfx = 'tissue' if check_tissue else 'all'
img_outpath = os.path.join(str(datapath), 'tiles', sfx, f'{tile_sz}px',
                           fname.with_suffix('').name)
# os.makedirs(grid_tiles_path, exist_ok=True)
pdx_slide = Slide(path=img_inpath, processed_path=img_outpath)
print(pdx_slide.dimensions)

# Extract
import ipdb
ipdb.set_trace(context=11)
grid_tiler.extract(pdx_slide)