Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions trident/wsi_objects/WSI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion trident/wsi_objects/WSIPatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down