Skip to content

Commit

Permalink
Merge 865c766 into e673dd4
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Sep 19, 2020
2 parents e673dd4 + 865c766 commit 0fdbe22
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 670 deletions.
19 changes: 19 additions & 0 deletions src/histolab/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# encoding: utf-8

# ------------------------------------------------------------------------
# Copyright 2020 All Histolab Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------


class HistolabException(Exception):
"""Histolab custom exception main class"""

Expand Down
36 changes: 31 additions & 5 deletions src/histolab/scorer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# encoding: utf-8

# ------------------------------------------------------------------------
# Copyright 2020 All Histolab Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

import operator
from abc import abstractmethod

Expand All @@ -15,16 +33,22 @@


@runtime_checkable
class Scorer(Protocol):
"""General scorer object"""
class _Scorer(Protocol):
"""General scorer object
.. automethod:: __call__
"""

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


class RandomScorer(Scorer):
"""Implement a Scorer that returns a random float score between 0 and 1."""
class RandomScorer(_Scorer):
"""Implement a Scorer that returns a random float score between 0 and 1.
.. automethod:: __call__
"""

def __call__(self, tile: Tile) -> float:
"""Return the random score associated with the tile.
Expand All @@ -42,7 +66,7 @@ def __call__(self, tile: Tile) -> float:
return np.random.random()


class NucleiScorer(Scorer):
class NucleiScorer(_Scorer):
r"""Implement a Scorer that estimates the presence of nuclei in an H&E-stained tile.
A higher presence of nuclei is associated with a higher scorer, following this
Expand All @@ -51,6 +75,8 @@ class NucleiScorer(Scorer):
.. math::
score = nuclei\_ratio \cdot tanh(tissue\_ratio)
.. automethod:: __call__
"""

def __call__(self, tile: Tile) -> float:
Expand Down
18 changes: 18 additions & 0 deletions src/histolab/tile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# encoding: utf-8

# ------------------------------------------------------------------------
# Copyright 2020 All Histolab Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

import os
from pathlib import Path
from typing import Callable, Union
Expand Down
26 changes: 22 additions & 4 deletions src/histolab/tiler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
# encoding: utf-8

# ------------------------------------------------------------------------
# Copyright 2020 All Histolab Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

import csv
import os
from abc import abstractmethod
from typing import List, Tuple
from typing import List, Tuple, Union

import numpy as np

from .exceptions import LevelError
from .scorer import Scorer
from .scorer import RandomScorer, NucleiScorer
from .slide import Slide
from .tile import Tile
from .types import CoordinatePair
Expand Down Expand Up @@ -337,7 +355,7 @@ def __init__(
check_tissue: bool = True,
prefix: str = "",
suffix: str = ".png",
max_iter: int = 1e4,
max_iter: float = 1e4,
):

super().__init__()
Expand Down Expand Up @@ -519,7 +537,7 @@ class ScoreTiler(GridTiler):

def __init__(
self,
scorer: Scorer,
scorer: Union[NucleiScorer, RandomScorer],
tile_size: Tuple[int, int],
n_tiles: int = 0,
level: int = 0,
Expand Down
2 changes: 2 additions & 0 deletions src/histolab/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@

CoordinatePair = namedtuple("CoordinatePair", ("x_ul", "y_ul", "x_br", "y_br"))
Region = namedtuple("Region", ("index", "area", "bbox", "center"))

CP = CoordinatePair
1 change: 1 addition & 0 deletions src/histolab/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------

import functools
import warnings
from collections import deque
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_tile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from histolab.tile import Tile
from histolab.types import CoordinatePair
from histolab.types import CP

from ..fixtures import TILES

Expand All @@ -21,7 +21,7 @@ class Describe_Tile:
),
)
def it_knows_if_is_is_almost_white(self, tile_img, expected_result):
coords = CoordinatePair(0, 512, 0, 512)
coords = CP(0, 512, 0, 512)
tile = Tile(tile_img, coords)

is_almost_white = tile._is_almost_white
Expand Down

0 comments on commit 0fdbe22

Please sign in to comment.