From fbddd04ab7599723f0ab16c5bd37f58716713a3c Mon Sep 17 00:00:00 2001 From: Mr-Milk Date: Mon, 18 Mar 2024 12:58:27 +0100 Subject: [PATCH] Update main API --- lazyslide/__init__.py | 1 + lazyslide/wsi.py | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lazyslide/__init__.py b/lazyslide/__init__.py index f77b8de..3d8931c 100644 --- a/lazyslide/__init__.py +++ b/lazyslide/__init__.py @@ -3,6 +3,7 @@ from .wsi import WSI from .utils import get_reader +from .filter import SobelFilter, BlurFilter, FocusLiteFilter def about(): diff --git a/lazyslide/wsi.py b/lazyslide/wsi.py index 549373a..2e4901c 100644 --- a/lazyslide/wsi.py +++ b/lazyslide/wsi.py @@ -1,6 +1,7 @@ from __future__ import annotations import warnings +from concurrent.futures import ProcessPoolExecutor from numbers import Integral from pathlib import Path from typing import Sequence, Mapping @@ -255,6 +256,7 @@ def create_tiles( background_fraction=0.8, tile_pts=3, errors="ignore", + tile_filter=None, save=True, ): """ @@ -404,8 +406,6 @@ def create_tiles( points = rect_coords is_in = [] for c in self._contours: - # Coerce the point to python int and let the opencv decide the type - # Flip x, y because it's different in opencv polytest = [ cv2.pointPolygonTest(c, (float(x), float(y)), measureDist=False) for x, y in points @@ -428,6 +428,16 @@ def create_tiles( # Select only the top_left corner tiles_coords = rect_coords[rect_indices[good_tiles, 0]].copy() + # Run tile filter + if tile_filter is not None: + masks = [] + + for c in tiles_coords: + left, top = c + patch = self.get_patch(left, top, tile_w, tile_h, level=0) + masks.append(tile_filter.filter(patch)) + tiles_coords = tiles_coords[masks].copy() + self.new_tiles( tiles_coords, width=tile_w, @@ -589,13 +599,16 @@ def plot_tissue( ) if contours: - viewer.add_contours_holes( - self.contours, - self.holes, - contour_color=contour_color, - hole_color=hole_color, - linewidth=linewidth, - ) + if self.contours is None: + warnings.warn("No contours is created") + else: + viewer.add_contours_holes( + self.contours, + self.holes, + contour_color=contour_color, + hole_color=hole_color, + linewidth=linewidth, + ) if save is not None: savefig_kws = {} if savefig_kws is None else savefig_kws