-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
scorer.py
109 lines (81 loc) · 3.03 KB
/
scorer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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
import numpy as np
from .filters import image_filters as imf
from .filters import morphological_filters as mof
from .filters.util import mask_difference
from .tile import Tile
try:
from typing import Protocol, runtime_checkable
except ImportError:
from typing_extensions import Protocol, runtime_checkable
@runtime_checkable
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.
.. automethod:: __call__
"""
def __call__(self, tile: Tile) -> float:
"""Return the random score associated with the tile.
Parameters
----------
tile : Tile
The tile to calculate the score from.
Returns
-------
float
Random score ranging between 0 and 1.
"""
return np.random.random()
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
formula:
.. math::
score = nuclei\_ratio \cdot tanh(tissue\_ratio)
.. automethod:: __call__
"""
def __call__(self, tile: Tile) -> float:
"""Return the nuclei score associated with the tile.
Parameters
----------
tile : Tile
The tile to calculate the score from.
Returns
-------
float
Nuclei score
"""
filters_raw_nuclei = imf.Compose(
[imf.HematoxylinChannel(), imf.YenThreshold(operator.gt)]
)
filters_nuclei_cleaner = imf.Compose(
[imf.HematoxylinChannel(), imf.YenThreshold(operator.gt), mof.WhiteTopHat()]
)
mask_raw_nuclei = np.array(tile.apply_filters(filters_raw_nuclei).image)
mask_nuclei_clean = np.array(tile.apply_filters(filters_nuclei_cleaner).image)
mask_nuclei = mask_difference(mask_raw_nuclei, mask_nuclei_clean)
nuclei_ratio = np.count_nonzero(mask_nuclei) / mask_nuclei.size
return nuclei_ratio * np.tanh(tile.tissue_ratio)