Skip to content

Commit

Permalink
Formatting with Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Milk committed Dec 10, 2023
1 parent 8df1934 commit ca05cf4
Show file tree
Hide file tree
Showing 16 changed files with 834 additions and 495 deletions.
16 changes: 11 additions & 5 deletions lazyslide/cv_mods/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from .mods import (ConvertColorspace,
MedianBlur, GaussianBlur, BoxBlur,
BinaryThreshold, MorphOpen, MorphClose,
ForegroundDetection, TissueDetectionHE
)
from .mods import (
ConvertColorspace,
MedianBlur,
GaussianBlur,
BoxBlur,
BinaryThreshold,
MorphOpen,
MorphClose,
ForegroundDetection,
TissueDetectionHE,
)
88 changes: 47 additions & 41 deletions lazyslide/cv_mods/mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@


class ConvertColorspace(Transform):

def __init__(self, code=None, old=None, new=None, ):
def __init__(
self,
code=None,
old=None,
new=None,
):
if code is None:
self.old = old.upper()
self.new = new.upper()
Expand Down Expand Up @@ -123,7 +127,7 @@ def __repr__(self):
def apply(self, image):
assert image.dtype == np.uint8, f"image dtype {image.dtype} must be np.uint8"
assert (
image.ndim == 2
image.ndim == 2
), f"input image has shape {image.shape}. Must convert to 1-channel image (H, W)."
_, out = cv2.threshold(
src=image,
Expand Down Expand Up @@ -219,11 +223,11 @@ class ForegroundDetection(Transform):
"""

def __init__(
self,
mask_name=None,
min_region_size=5000,
max_hole_size=1500,
outer_contours_only=False,
self,
mask_name=None,
min_region_size=5000,
max_hole_size=1500,
outer_contours_only=False,
):
self.min_region_size = min_region_size
self.max_hole_size = max_hole_size
Expand Down Expand Up @@ -286,12 +290,12 @@ def apply(self, mask):

# loop thru contours
for (
cnt,
outside,
size_thresh,
hole,
hole_size_thresh,
hole_parent_thresh,
cnt,
outside,
size_thresh,
hole,
hole_size_thresh,
hole_parent_thresh,
) in zip(
contours,
outside_contours,
Expand Down Expand Up @@ -331,11 +335,11 @@ class ForegroundContourDetection(Transform):
"""

def __init__(
self,
mask_name=None,
min_region_size=5000,
max_hole_size=1500,
outer_contours_only=False,
self,
mask_name=None,
min_region_size=5000,
max_hole_size=1500,
outer_contours_only=False,
):
self.min_region_size = min_region_size
self.max_hole_size = max_hole_size
Expand Down Expand Up @@ -374,16 +378,22 @@ def apply(self, mask):

# outside contours must be above min_tissue_region_size threshold
tissue_contours = outmost[
contours_areas[outmost_slice] > self.min_region_size]
contours_areas[outmost_slice] > self.min_region_size
]

tissue_holes = holes[
# hole contours must be above area threshold
contours_areas[hole_slice] < self.max_hole_size & \
contours_areas[hole_slice]
< self.max_hole_size
&
# holes must have parents above area threshold
(contours_areas[hierarchy[hole_slice, 3]] > self.min_region_size)]
(contours_areas[hierarchy[hole_slice, 3]] > self.min_region_size)
]

return ([np.squeeze(contours[ix], axis=1) for ix in tissue_contours],
[np.squeeze(contours[ix], axis=1) for ix in tissue_holes])
return (
[np.squeeze(contours[ix], axis=1) for ix in tissue_contours],
[np.squeeze(contours[ix], axis=1) for ix in tissue_holes],
)


class TissueDetectionHE(Transform):
Expand All @@ -406,16 +416,16 @@ class TissueDetectionHE(Transform):
"""

def __init__(
self,
use_saturation=True,
blur_ksize=17,
threshold=7,
morph_n_iter=3,
morph_k_size=7,
min_region_size=2500,
max_hole_size=100,
outer_contours_only=False,
return_contours=False,
self,
use_saturation=True,
blur_ksize=17,
threshold=7,
morph_n_iter=3,
morph_k_size=7,
min_region_size=2500,
max_hole_size=100,
outer_contours_only=False,
return_contours=False,
):
self.use_sat = use_saturation
self.blur_ksize = blur_ksize
Expand Down Expand Up @@ -447,12 +457,8 @@ def __init__(
self.pipeline = [
MedianBlur(kernel_size=self.blur_ksize),
thresholder,
MorphOpen(
kernel_size=self.morph_k_size,
n_iterations=self.morph_n_iter),
MorphClose(
kernel_size=self.morph_k_size,
n_iterations=self.morph_n_iter),
MorphOpen(kernel_size=self.morph_k_size, n_iterations=self.morph_n_iter),
MorphClose(kernel_size=self.morph_k_size, n_iterations=self.morph_n_iter),
foreground,
]

Expand All @@ -466,7 +472,7 @@ def __repr__(self):

def apply(self, image):
assert (
image.dtype == np.uint8
image.dtype == np.uint8
), f"Input image dtype {image.dtype} must be np.uint8"
# first get single channel image_ref
if self.use_sat:
Expand Down
11 changes: 6 additions & 5 deletions lazyslide/h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def load(self):
for mask_name in masks_group.keys():
ds = masks_group.get(mask_name)
masks[mask_name] = ds[:]
masks_level[mask_name] = ds.attrs['level']
masks_level[mask_name] = ds.attrs["level"]
self.masks = masks
self.masks_level = masks_level

Expand Down Expand Up @@ -115,8 +115,7 @@ def save(self):
if self.COORDS_KEY in h5:
del h5[self.COORDS_KEY]

ds = h5.create_dataset(self.COORDS_KEY, data=self.coords,
chunks=True)
ds = h5.create_dataset(self.COORDS_KEY, data=self.coords, chunks=True)
attrs = ds.attrs
for k, v in asdict(self.tile_ops).items():
if v is None:
Expand All @@ -130,9 +129,11 @@ def save(self):

masks_group = h5.create_group(self.MASKS_KEY)
for mask_name, mask_array in self.masks.items():
ds = masks_group.create_dataset(mask_name, data=mask_array, chunks=True)
ds = masks_group.create_dataset(
mask_name, data=mask_array, chunks=True
)
attrs = ds.attrs
attrs['level'] = self.masks_level[mask_name]
attrs["level"] = self.masks_level[mask_name]

if self._rewrite_contours:
if self.CONTOURS_KEY in h5:
Expand Down
57 changes: 30 additions & 27 deletions lazyslide/loader/dataset.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import torch
from torch.utils.data import Dataset
from torchvision.transforms.v2 import (ToImage, ToDtype, Normalize,
Compose, Resize)
from torchvision.transforms.v2 import ToImage, ToDtype, Normalize, Compose, Resize

from lazyslide.normalizer import ColorNormalizer


def compose_transform(resize=None,
antialias=False,
color_normalize=None,
feature_extraction=False,

):
def compose_transform(
resize=None,
antialias=False,
color_normalize=None,
feature_extraction=False,
):
if feature_extraction:
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
else:
mean = (0.5, 0.5, 0.5)
std = (0.5, 0.5, 0.5)
pre = []
after = [ToImage(), ToDtype(dtype=torch.float32, scale=True), Normalize(mean=mean, std=std)]
after = [
ToImage(),
ToDtype(dtype=torch.float32, scale=True),
Normalize(mean=mean, std=std),
]
if resize is not None:
pre += [ToImage(), Resize(size=resize, antialias=antialias)]
if color_normalize is not None:
Expand All @@ -29,18 +32,17 @@ def compose_transform(resize=None,


class FeatureExtractionDataset(Dataset):

def __init__(self,
wsi,
transform=None,
resize=None,
antialias=False,
color_normalize=None,
):
def __init__(
self,
wsi,
transform=None,
resize=None,
antialias=False,
color_normalize=None,
):
self.wsi = wsi
if not wsi.has_tiles:
raise ValueError("WSI does not have tiles. "
"Please create tiles first.")
raise ValueError("WSI does not have tiles. " "Please create tiles first.")
self.tiles_coords = self.wsi.h5_file.get_coords()
self.tile_ops = self.wsi.h5_file.get_tile_ops()
if transform is not None:
Expand All @@ -52,19 +54,20 @@ def __init__(self,
resize_to = (int(self.tile_ops.height), int(self.tile_ops.width))
else:
resize_to = None
self.transform = compose_transform(resize=resize_to,
antialias=antialias,
color_normalize=color_normalize,
feature_extraction=True)
self.transform = compose_transform(
resize=resize_to,
antialias=antialias,
color_normalize=color_normalize,
feature_extraction=True,
)

def __len__(self):
return len(self.tiles_coords)

def __getitem__(self, idx):
coords = self.tiles_coords[idx]
x, y = coords
image = self.wsi.get_patch(y, x,
self.tile_ops.ops_width,
self.tile_ops.ops_height,
self.tile_ops.level)
image = self.wsi.get_patch(
y, x, self.tile_ops.ops_width, self.tile_ops.ops_height, self.tile_ops.level
)
return self.transform(image)
Loading

0 comments on commit ca05cf4

Please sign in to comment.