From f582e2d1b3d3a883fd6441afc670f789b34f1c51 Mon Sep 17 00:00:00 2001 From: "tristan.lazard" Date: Fri, 13 Mar 2020 18:28:22 +0100 Subject: [PATCH] Patch_sampling possible w/ mask_level > max_level When the level of the mask is set to be greater than the number of levels of the slide (max_level), then get_whole_image immediately adapt the level to slide.level_count - 1. This commit updates level for the rest of the procedure in Patch_sampling, otherwise we would get an error. --- useful_wsi/patch_sampling.py | 3 ++- useful_wsi/utils.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/useful_wsi/patch_sampling.py b/useful_wsi/patch_sampling.py index 1c6b6c6..ccaf027 100644 --- a/useful_wsi/patch_sampling.py +++ b/useful_wsi/patch_sampling.py @@ -8,7 +8,7 @@ import numpy as np from .tissue_segmentation import roi_binary_mask -from .utils import (find_square, get_size, get_whole_image, pj_slice, +from .utils import (find_square, get_size, get_whole_image, pj_slice, true_level, get_x_y, get_x_y_from_0, mask_percentage, open_image) @@ -307,6 +307,7 @@ def patch_sampling(slide, seed=None, mask_level=None, wsi_tissue = get_whole_image(slide, level=mask_level, numpy=True) wsi_mask = mask_function(wsi_tissue) + mask_level = true_level(slide, level = mask_level) if sampling_method == 'grid': # grid is just grid_etienne with marge = 0 min_row, min_col, max_row, max_col = 0, 0, *wsi_mask.shape diff --git a/useful_wsi/utils.py b/useful_wsi/utils.py index 8b1d1b6..68dfaae 100644 --- a/useful_wsi/utils.py +++ b/useful_wsi/utils.py @@ -54,6 +54,28 @@ def get_image(slide, para, numpy=True): slide = np.array(slide)[:, :, 0:3] return slide +def true_level(slide, level): + """" Finds the true level to which the mask has been done. + In the case where the image does not have enough levels, get_whole_images() takes + the level ${slide.level_count - 1} as default level. This function is there to actualize the value of level + so that the following treatement of the WSI is coherent with level = slide.level_count - 1. + + Parameters + ---------- + slide : openslide image + WSI + level : int + asked level. + + Returns + ------- + int + true level + """ + max_level = slide.level_count - 1 + level = min(max_level, level) + return level + def get_whole_image(slide, level=None, numpy=True): """ Return whole image at a certain level.