From 8dbaaf2b94fe4d2cf5dfba9dcc91cb1881154510 Mon Sep 17 00:00:00 2001 From: guillaumejaume Date: Tue, 18 Feb 2025 11:50:49 -0500 Subject: [PATCH] mem: gdf contours not in self od wsi --- trident/wsi_objects/WSI.py | 24 ++++++++++++++++-------- trident/wsi_objects/WSIPatcher.py | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/trident/wsi_objects/WSI.py b/trident/wsi_objects/WSI.py index 79186a2..d289d34 100644 --- a/trident/wsi_objects/WSI.py +++ b/trident/wsi_objects/WSI.py @@ -127,12 +127,6 @@ def _lazy_initialize(self) -> None: self.mag = self._fetch_magnification(self.custom_mpp_keys) self.lazy_init = True - if self.tissue_seg_path is not None: - try: - # self.mask = cv2.imread(self.tissue_seg_path, cv2.IMREAD_GRAYSCALE) - self.gdf_contours = gpd.read_file(self.tissue_seg_path) - except FileNotFoundError: - raise FileNotFoundError(f"Tissue segmentation file not found: {self.tissue_seg_path}") except Exception as e: raise Exception(f"Error initializing WSI: {e}") @@ -513,7 +507,6 @@ def segment_tissue( ) gdf_contours.set_crs("EPSG:3857", inplace=True) # used to silent warning // Web Mercator gdf_contours.to_file(gdf_saveto, driver="GeoJSON") - self.gdf_contours = gdf_contours self.tissue_seg_path = gdf_saveto # Draw the contours on the thumbnail image @@ -586,6 +579,20 @@ def get_best_level_and_custom_downsample( # If no suitable level is found, raise an error raise ValueError(f"No suitable level found for downsample {downsample}.") + def load_gdf_contours(self): + """ + Load geodataframe with tissue vs. background contours. + + Returns: + -------- + gpd: Geodataframe. Each entry is a contour. + """ + try: + gdf_contours = gpd.read_file(self.tissue_seg_path) + except FileNotFoundError: + raise FileNotFoundError(f"Tissue segmentation file not found: {self.tissue_seg_path}") + return gdf_contours + def extract_tissue_coords( self, target_mag: int, @@ -625,12 +632,13 @@ def extract_tissue_coords( """ self._lazy_initialize() + gdf_contours = self.load_gdf_contours() patcher = self.create_patcher( patch_size=patch_size, src_mag=self.mag, dst_mag=target_mag, - mask=self.gdf_contours, + mask=gdf_contours, coords_only=True, overlap=overlap, threshold=min_tissue_proportion, diff --git a/trident/wsi_objects/WSIPatcher.py b/trident/wsi_objects/WSIPatcher.py index 02e7224..9e5bfda 100644 --- a/trident/wsi_objects/WSIPatcher.py +++ b/trident/wsi_objects/WSIPatcher.py @@ -27,7 +27,7 @@ def __init__( """ Initialize patcher, compute number of (masked) rows, columns. Args: - wsi (WSI): wsi to patch + wsi (OpenSlideWSI): wsi to patch patch_size (int): patch width/height in pixel on the slide after rescaling src_pixel_size (float, optional): pixel size in um/px of the slide before rescaling. Defaults to None. dst_pixel_size (float, optional): pixel size in um/px of the slide after rescaling. Defaults to None.