Beispiel #1
0
def test_read_rect_jp2_levels(_sample_jp2):
    """Test jp2 read rect with resolution in levels.

    Location coordinate is in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    location = (0, 0)
    size = JP2_TEST_TISSUE_SIZE
    width, height = size
    for level in range(wsi.info.level_count):
        level_width, level_height = wsi.info.level_dimensions[level]
        im_region = wsi.read_rect(location,
                                  size,
                                  resolution=level,
                                  units="level")

        assert isinstance(im_region, np.ndarray)
        assert im_region.dtype == "uint8"
        assert approx(
            im_region.shape,
            (
                min(height, level_height),
                min(width, level_width),
                3,
            ),
            abs=1,
        )
Beispiel #2
0
def test__relative_level_scales_jp2_mpp(_sample_jp2):
    """Test jp2 calculation of relative level scales for mpp."""
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    level_scales = wsi._relative_level_scales(0.5, "mpp")
    level_scales = np.array(level_scales)
    assert strictly_increasing(level_scales[:, 0])
    assert strictly_increasing(level_scales[:, 1])
    assert all(level_scales[0] == wsi.info.mpp / 0.5)
Beispiel #3
0
def test_read_rect_jp2_mpp(_sample_jp2):
    """Test jp2 read rect with resolution in microns per pixel.

    Location coordinate is in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    location = JP2_TEST_TISSUE_LOCATION
    size = JP2_TEST_TISSUE_SIZE
    read_rect_mpp(wsi, location, size)
Beispiel #4
0
def test_find_optimal_level_and_downsample_jp2_interpolation_warning(
        _sample_jp2):
    """Test finding optimal level for mpp read with scale > 1.

    This tests the case where the scale is found to be > 1 and interpolation
    will be applied to the output. A UserWarning should be raised in this case.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    with pytest.warns(UserWarning):
        _, _ = wsi._find_optimal_level_and_downsample(0.1, "mpp")
Beispiel #5
0
def test_read_bounds_jp2_baseline(_sample_jp2):
    """Test jp2 read bounds at baseline.

    Coordinates in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    bounds = JP2_TEST_TISSUE_BOUNDS
    size = JP2_TEST_TISSUE_SIZE
    im_region = wsi.read_bounds(bounds, resolution=0, units="level")

    assert isinstance(im_region, np.ndarray)
    assert im_region.dtype == "uint8"
    assert im_region.shape == (*size[::-1], 3)
Beispiel #6
0
def test_read_rect_jp2_baseline(_sample_jp2):
    """Test jp2 read rect at baseline.

    Location coordinate is in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    location = JP2_TEST_TISSUE_LOCATION
    size = JP2_TEST_TISSUE_SIZE
    im_region = wsi.read_rect(location, size, resolution=0, units="level")

    assert isinstance(im_region, np.ndarray)
    assert im_region.dtype == "uint8"
    assert im_region.shape == (*size[::-1], 3)
Beispiel #7
0
def test_wsireader_jp2_save_tiles(_sample_jp2, tmp_path):
    """Test for save_tiles in wsireader as a python function."""
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    wsi.save_tiles(
        output_dir=str(
            pathlib.Path(tmp_path).joinpath("test_wsireader_jp2_save_tiles")),
        tile_objective_value=5,
        tile_read_size=(5000, 5000),
        verbose=True,
    )
    assert (pathlib.Path(tmp_path).joinpath("test_wsireader_jp2_save_tiles").
            joinpath("test1.jp2").joinpath("Output.csv").exists())
    assert (pathlib.Path(tmp_path).joinpath("test_wsireader_jp2_save_tiles").
            joinpath("test1.jp2").joinpath("slide_thumbnail.jpg").exists())
    assert (pathlib.Path(tmp_path).joinpath("test_wsireader_jp2_save_tiles").
            joinpath("test1.jp2").joinpath("Tile_5_0_0.jpg").exists())
Beispiel #8
0
def test_read_bounds_level_consistency_jp2(_sample_jp2):
    """Test read_bounds produces the same visual field across resolution levels."""
    bounds = JP2_TEST_TISSUE_BOUNDS
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    imgs = [
        wsi.read_bounds(bounds, power, "power") for power in [60, 40, 20, 10]
    ]
    smallest_size = imgs[-1].shape[:2][::-1]
    resized = [
        cv2.GaussianBlur(cv2.resize(img, smallest_size), (5, 5),
                         cv2.BORDER_REFLECT) for img in imgs
    ]
    # Pair-wise check resolutions for mean squared error
    for a in resized:
        for b in resized:
            assert np.sum((a - b)**2) / np.prod(a.shape) < 16
Beispiel #9
0
def test_read_bounds_jp2_levels(_sample_jp2):
    """Test jp2 read bounds with resolution in levels.

    Coordinates in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    bounds = JP2_TEST_TISSUE_BOUNDS
    width, height = JP2_TEST_TISSUE_SIZE
    for level, downsample in enumerate(wsi.info.level_downsamples):
        im_region = wsi.read_bounds(bounds, resolution=level, units="level")

        assert isinstance(im_region, np.ndarray)
        assert im_region.dtype == "uint8"
        expected_output_shape = tuple(
            np.round([height / downsample, width / downsample]))
        assert im_region.shape[:2] == approx(expected_output_shape, abs=1)
        assert im_region.shape[2] == 3
Beispiel #10
0
def test_read_rect_jp2_objective_power(_sample_jp2):
    """Test jp2 read rect with resolution in objective power.

    Location coordinate is in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    location = JP2_TEST_TISSUE_LOCATION
    size = JP2_TEST_TISSUE_SIZE
    for objective_power in [20, 10, 5, 2.5, 1.25]:
        im_region = wsi.read_rect(location,
                                  size,
                                  resolution=objective_power,
                                  units="mpp")

        assert isinstance(im_region, np.ndarray)
        assert im_region.dtype == "uint8"
        assert im_region.shape == (*size[::-1], 3)
Beispiel #11
0
def test_read_bounds_jp2_mpp(_sample_jp2):
    """Test jp2 read bounds with resolution in microns per pixel.

    Coordinates in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    bounds = JP2_TEST_TISSUE_BOUNDS
    size = JP2_TEST_TISSUE_SIZE
    slide_mpp = wsi.info.mpp
    for factor in range(1, 10):
        mpp = slide_mpp * factor
        downsample = mpp / slide_mpp

        im_region = wsi.read_bounds(bounds, resolution=mpp, units="mpp")

        assert isinstance(im_region, np.ndarray)
        assert im_region.dtype == "uint8"
        expected_output_shape = tuple(
            np.round((np.array(size[::-1]) / downsample)).astype(int))
        assert im_region.shape[:2] == approx(expected_output_shape, abs=1)
        assert im_region.shape[2] == 3
Beispiel #12
0
def test_read_bounds_jp2_objective_power(_sample_jp2):
    """Test jp2 read bounds with resolution in objective power.

    Coordinates in baseline (level 0) reference frame.
    """
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    bounds = JP2_TEST_TISSUE_BOUNDS
    size = JP2_TEST_TISSUE_SIZE
    slide_power = wsi.info.objective_power
    for objective_power in [20, 10, 5, 2.5, 1.25]:
        downsample = slide_power / objective_power

        im_region = wsi.read_bounds(
            bounds,
            resolution=objective_power,
            units="power",
        )

        assert isinstance(im_region, np.ndarray)
        assert im_region.dtype == "uint8"
        expected_output_shape = tuple(
            np.round((np.array(size[::-1]) / downsample)).astype(int))
        assert im_region.shape[:2] == approx(expected_output_shape[:2], abs=1)
        assert im_region.shape[2] == 3
Beispiel #13
0
def test_wsireader_get_thumbnail_jp2(_sample_jp2):
    """Test for get_thumbnail as a python function."""
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    slide_thumbnail = wsi.slide_thumbnail()
    assert isinstance(slide_thumbnail, np.ndarray)
    assert slide_thumbnail.dtype == "uint8"
Beispiel #14
0
def test__relative_level_scales_jp2_level_float(_sample_jp2):
    """Test jp2 calculation of relative level scales for fractional level."""
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    relative_level_scales_float(wsi)
Beispiel #15
0
def test__relative_level_scales_jp2_power(_sample_jp2):
    """Test jp2 calculation of relative level scales for objective power."""
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    relative_level_scales_power(wsi)
Beispiel #16
0
def test__relative_level_scales_jp2_baseline(_sample_jp2):
    """Test jp2 relative level scales for pixels per baseline pixel."""
    wsi = wsireader.OmnyxJP2WSIReader(_sample_jp2)
    relative_level_scales_baseline(wsi)