Skip to content

Commit

Permalink
Merge 965f7f3 into d5945e1
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiamarcolini committed Aug 5, 2020
2 parents d5945e1 + 965f7f3 commit 0d83ab8
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 172 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def ascii_bytes_from(path, *paths):
"Pillow",
"scikit-image",
"scipy",
"sparse",
"openslide-wrapper",
"typing_extensions",
]
Expand Down
14 changes: 14 additions & 0 deletions src/histolab/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class LevelError(Exception):
"""Raised when a requested level is not available"""

def __init__(self, *args) -> None:
if args:
self.message = args[0]
else:
self.message = None

def __str__(self):
if self.message:
return self.message
else:
return ""
22 changes: 22 additions & 0 deletions src/histolab/scorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from abc import abstractmethod

import numpy as np

from histolab.tile import Tile

try:
from typing import Protocol, runtime_checkable
except ImportError:
from typing_extensions import Protocol, runtime_checkable


@runtime_checkable
class Scorer(Protocol):
@abstractmethod
def __call__(self, tile: Tile) -> float:
raise NotImplementedError


class RandomScorer(Scorer):
def __call__(self, tile: Tile) -> float:
return np.random.random()
28 changes: 19 additions & 9 deletions src/histolab/slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import numpy as np
import openslide
import PIL
import sparse

from .exceptions import LevelError
from .filters import image_filters as imf
from .filters import morphological_filters as mof
from .tile import Tile
Expand All @@ -42,7 +42,6 @@
polygon_to_mask_array,
region_coordinates,
regions_from_binary_mask,
resize_mask,
scale_coordinates,
)

Expand Down Expand Up @@ -97,20 +96,32 @@ def level_dimensions(self, level: int = 0) -> Tuple[int, int]:
try:
return self._wsi.level_dimensions[level]
except IndexError:
raise ValueError(
raise LevelError(
f"Level {level} not available. Number of available levels: "
f"{len(self._wsi.level_dimensions)}"
)

@lazyproperty
def levels(self) -> List[int]:
"""Return the slide's available levels
Returns
-------
List[int]
The levels available
"""
return list(range(len(self._wsi.level_dimensions)))

@lazyproperty
@lru_cache(maxsize=100)
def biggest_tissue_box_mask(self) -> sparse._coo.core.COO:
"""Return the binary mask of the box containing the max area of tissue.
def biggest_tissue_box_mask(self) -> np.ndarray:
"""Return the thumbnail binary mask of the box containing the max tissue area.
Returns
-------
mask: sparse._coo.core.COO
Binary mask of the box containing the max area of tissue.
mask: np.ndarray
Binary mask of the box containing the max area of tissue. The dimensions are
those of the thumbnail.
"""
thumb = self._wsi.get_thumbnail(self._thumbnail_size)
Expand All @@ -123,8 +134,7 @@ def biggest_tissue_box_mask(self) -> sparse._coo.core.COO:
thumb_bbox_mask = polygon_to_mask_array(
self._thumbnail_size, biggest_region_coordinates
)
thumb_bbox_mask_sparse = sparse.COO(thumb_bbox_mask)
return resize_mask(thumb_bbox_mask_sparse, self.dimensions)
return thumb_bbox_mask

def extract_tile(self, coords: CoordinatePair, level: int) -> Tile:
"""Extract a tile of the image at the selected level.
Expand Down

0 comments on commit 0d83ab8

Please sign in to comment.