Skip to content

Commit

Permalink
Merge 4de6d99 into 7b55a25
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Sep 6, 2020
2 parents 7b55a25 + 4de6d99 commit d41fadb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 29 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -47,6 +47,7 @@ jobs:
install:
- pip3 install -e .[testing]
- pip3 install flake8
- pip3 install pooch

services:
- xvfb
Expand Down
17 changes: 0 additions & 17 deletions src/histolab/filters/util.py
@@ -1,5 +1,4 @@
import numpy as np
from PIL import Image


def mask_percent(mask: np.ndarray) -> float:
Expand All @@ -18,19 +17,3 @@ def mask_percent(mask: np.ndarray) -> float:

mask_percentage = 100 - np.count_nonzero(mask) / mask.size * 100
return mask_percentage


def tissue_percent(img: Image.Image) -> float:
"""Compute percentage of tissue in an image.
Parameters
----------
img : Image.Image
Input image
Returns
-------
float
Percentage of image that is not masked
"""
return 100 - mask_percent(img)
18 changes: 8 additions & 10 deletions src/histolab/tiler.py
Expand Up @@ -192,16 +192,13 @@ def _grid_coordinates_from_bbox_coordinates(
n_tiles_row = self._n_tiles_row(bbox_coordinates)
n_tiles_column = self._n_tiles_column(bbox_coordinates)

x_ul_lvl_offset = bbox_coordinates.x_ul
y_ul_lvl_offset = bbox_coordinates.y_ul

for i in range(n_tiles_row):
for j in range(n_tiles_column):
x_ul_lvl = x_ul_lvl_offset + tile_w_lvl * j - self.pixel_overlap
y_ul_lvl = y_ul_lvl_offset + tile_h_lvl * i - self.pixel_overlap
x_ul_lvl = bbox_coordinates.x_ul + tile_w_lvl * j - self.pixel_overlap
y_ul_lvl = bbox_coordinates.y_ul + tile_h_lvl * i - self.pixel_overlap

x_ul_lvl = np.clip(x_ul_lvl, x_ul_lvl_offset, None)
y_ul_lvl = np.clip(y_ul_lvl, y_ul_lvl_offset, None)
x_ul_lvl = np.clip(x_ul_lvl, bbox_coordinates.x_ul, None)
y_ul_lvl = np.clip(y_ul_lvl, bbox_coordinates.y_ul, None)

x_br_lvl = x_ul_lvl + tile_w_lvl
y_br_lvl = y_ul_lvl + tile_h_lvl
Expand Down Expand Up @@ -232,7 +229,8 @@ def _grid_coordinates_generator(self, slide: Slide) -> CoordinatePair:
box_mask = self.box_mask(slide)

regions = regions_from_binary_mask(box_mask)
for region in regions: # at the moment there is only one region
# ----at the moment there is only one region----
for region in regions:
bbox_coordinates_thumb = region_coordinates(region)
bbox_coordinates = scale_coordinates(
bbox_coordinates_thumb,
Expand Down Expand Up @@ -431,12 +429,12 @@ def _random_tile_coordinates(self, slide: Slide) -> CoordinatePair:
tile_w_thumb = (
tile_w_lvl * box_mask.shape[1] / slide.level_dimensions(self.level)[0]
)
tile_h_thumn = (
tile_h_thumb = (
tile_h_lvl * box_mask.shape[0] / slide.level_dimensions(self.level)[1]
)

x_br_lvl = x_ul_lvl + tile_w_thumb
y_br_lvl = y_ul_lvl + tile_h_thumn
y_br_lvl = y_ul_lvl + tile_h_thumb

tile_wsi_coords = scale_coordinates(
reference_coords=CoordinatePair(x_ul_lvl, y_ul_lvl, x_br_lvl, y_br_lvl),
Expand Down
75 changes: 73 additions & 2 deletions tests/unit/data/test_data.py
@@ -1,15 +1,26 @@
# coding: utf-8

import copy

import os
import sys
from unittest.mock import patch
from importlib import reload

import pytest

import openslide
from histolab.data import _has_hash, _load_svs, cmu_small_region, data_dir
from histolab.data import (
_fetch,
_has_hash,
_load_svs,
cmu_small_region,
data_dir,
registry,
)

from ...fixtures import SVS
from ...unitutil import function_mock
from ...unitutil import function_mock, ANY


def test_data_dir():
Expand Down Expand Up @@ -47,3 +58,63 @@ def it_knows_its_hash(request, file, hash, expected_value):
has_hash = _has_hash(file, hash)

assert has_hash is expected_value


@patch.dict(registry, {"data/cmu_small_region.svs": "bar"}, clear=True)
@patch("histolab.data.image_fetcher", None)
def it_raises_error_on_fetch_if_image_fetcher_is_None():
with pytest.raises(ModuleNotFoundError) as err:
_fetch("data/cmu_small_region.svs")

assert (
str(err.value)
== "The requested file is part of the histolab distribution, but requires the "
"installation of an optional dependency, pooch. To install pooch, use your "
"preferred python package manager. Follow installation instruction found at "
"https://www.fatiando.org/pooch/latest/install.html"
)


def test_pooch_missing(monkeypatch):
from histolab import data

fakesysmodules = copy.copy(sys.modules)
fakesysmodules["pooch.utils"] = None
monkeypatch.delitem(sys.modules, "pooch.utils")
monkeypatch.setattr("sys.modules", fakesysmodules)
file = SVS.CMU_1_SMALL_REGION
reload(data)

data.file_hash(file)

assert data.file_hash.__module__ == "histolab.data"


def test_file_hash_with_wrong_algorithm(monkeypatch):
from histolab import data

fakesysmodules = copy.copy(sys.modules)
fakesysmodules["pooch.utils"] = None
monkeypatch.delitem(sys.modules, "pooch.utils")
monkeypatch.setattr("sys.modules", fakesysmodules)
file = SVS.CMU_1_SMALL_REGION
reload(data)

with pytest.raises(ValueError) as err:
data.file_hash(file, "fakesha")
assert str(err.value) == "Algorithm 'fakesha' not available in hashlib"
assert data.file_hash.__module__ == "histolab.data"


def test_create_image_fetcher_without_pooch(monkeypatch):
from histolab import data

fakesysmodules = copy.copy(sys.modules)
fakesysmodules["pooch"] = None
monkeypatch.delitem(sys.modules, "pooch")
monkeypatch.setattr("sys.modules", fakesysmodules)
reload(data)

create_image_fetcher = data._create_image_fetcher()

assert create_image_fetcher == (None, ANY)

0 comments on commit d41fadb

Please sign in to comment.