Skip to content

Commit 6c88302

Browse files
Merge pull request #64 from andreped/fix/multitarget
Fixed bug in multitarget macenko during import
2 parents 5ccc187 + a52c85c commit 6c88302

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# torchstain
22

33
[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
4-
[![tests](https://github.com/EIDOSLAB/torchstain/workflows/tests/badge.svg)](https://github.com/EIDOSLAB/torchstain/actions)
4+
[![Full Tests](https://github.com/EIDOSLAB/torchstain/actions/workflows/tests_full.yml/badge.svg)](https://github.com/EIDOSLAB/torchstain/actions/workflows/tests_full.yml)
55
[![Pip Downloads](https://img.shields.io/pypi/dm/torchstain?label=pip%20downloads&logo=python)](https://pypi.org/project/torchstain/)
66
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.7692014.svg)](https://doi.org/10.5281/zenodo.7692014)
77

@@ -79,7 +79,7 @@ Runtimes using the Macenko algorithm using different backends. Metrics were calc
7979
- [1] Macenko, Marc et al. "A method for normalizing histology slides for quantitative analysis." 2009 IEEE International Symposium on Biomedical Imaging: From Nano to Macro. IEEE, 2009.
8080
- [2] Reinhard, Erik et al. "Color transfer between images." IEEE Computer Graphics and Applications. IEEE, 2001.
8181
- [3] Roy, Santanu et al. "Modified Reinhard Algorithm for Color Normalization of Colorectal Cancer Histopathology Images". 2021 29th European Signal Processing Conference (EUSIPCO), IEEE, 2021.
82-
- [4] Ivanov, Desislav et al. "Multi-target stain normalization for histology slides". arXiv (preprint). 2024.
82+
- [4] Ivanov, Desislav et al. "Multi-target stain normalization for histology slides". 2nd International Workshop on Medical Optical Imaging and Virtual Microscopy Image Analysis (MOVI 2024), MICCAI. 2024.
8383

8484
## Citing
8585

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name='torchstain',
9-
version='1.3.0',
9+
version='1.4.0',
1010
description='Stain normalization tools for histological analysis and computational pathology',
1111
long_description=README,
1212
long_description_content_type='text/markdown',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .he_normalizer import HENormalizer
22
from .macenko import MacenkoNormalizer
3+
from .multitarget import MultiMacenkoNormalizer
34
from .reinhard import ReinhardNormalizer
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
def MultiMacenkoNormalizer(backend='torch', **kwargs):
2-
if backend == 'torch':
3-
from torchstain.torch.normalizers.multitarget import MultiMacenkoNormalizer
4-
return MultiMacenkoNormalizer(**kwargs)
1+
def MultiMacenkoNormalizer(backend="torch", **kwargs):
2+
if backend == "numpy":
3+
raise NotImplementedError("MultiMacenkoNormalizer is not implemented for NumPy backend")
4+
elif backend == "torch":
5+
from torchstain.torch.normalizers import TorchMultiMacenkoNormalizer
6+
return TorchMultiMacenkoNormalizer(**kwargs)
7+
elif backend == "tensorflow":
8+
raise NotImplementedError("MultiMacenkoNormalizer is not implemented for TensorFlow backend")
59
else:
6-
raise Exception(f'Unsupported backend {backend}')
10+
raise Exception(f"Unsupported backend {backend}")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from torchstain.torch.normalizers.macenko import TorchMacenkoNormalizer
2-
from torchstain.torch.normalizers.multitarget import MultiMacenkoNormalizer
2+
from torchstain.torch.normalizers.multitarget import TorchMultiMacenkoNormalizer
33
from torchstain.torch.normalizers.reinhard import TorchReinhardNormalizer

torchstain/torch/normalizers/multitarget.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import torch
22
from torchstain.torch.utils import cov, percentile
3+
34
"""
45
Implementation of the multi-target normalizer from the paper: https://arxiv.org/pdf/2406.02077
56
"""
6-
class MultiMacenkoNormalizer:
7-
def __init__(self, norm_mode='avg-post'):
7+
class TorchMultiMacenkoNormalizer:
8+
def __init__(self, norm_mode="avg-post"):
89
self.norm_mode = norm_mode
910
self.HERef = torch.tensor([[0.5626, 0.2159],
1011
[0.7201, 0.8012],
1112
[0.4062, 0.5581]])
1213
self.maxCRef = torch.tensor([1.9705, 1.0308])
13-
self.updated_lstsq = hasattr(torch.linalg, 'lstsq')
14+
self.updated_lstsq = hasattr(torch.linalg, "lstsq")
1415

1516
def __convert_rgb2od(self, I, Io, beta):
1617
I = I.permute(1, 2, 0)
@@ -59,15 +60,15 @@ def __compute_matrices_single(self, I, Io, alpha, beta):
5960
return HE, C, maxC
6061

6162
def fit(self, Is, Io=240, alpha=1, beta=0.15):
62-
if self.norm_mode == 'avg-post':
63+
if self.norm_mode == "avg-post":
6364
HEs, _, maxCs = zip(*(
6465
self.__compute_matrices_single(I, Io, alpha, beta)
6566
for I in Is
6667
))
6768

6869
self.HERef = torch.stack(HEs).mean(dim=0)
6970
self.maxCRef = torch.stack(maxCs).mean(dim=0)
70-
elif self.norm_mode == 'concat':
71+
elif self.norm_mode == "concat":
7172
ODs, ODhats = zip(*(
7273
self.__convert_rgb2od(I, Io, beta)
7374
for I in Is
@@ -83,7 +84,7 @@ def fit(self, Is, Io=240, alpha=1, beta=0.15):
8384
maxCs = torch.stack([percentile(C[0, :], 99), percentile(C[1, :], 99)])
8485
self.HERef = HE
8586
self.maxCRef = maxCs
86-
elif self.norm_mode == 'avg-pre':
87+
elif self.norm_mode == "avg-pre":
8788
ODs, ODhats = zip(*(
8889
self.__convert_rgb2od(I, Io, beta)
8990
for I in Is
@@ -100,7 +101,7 @@ def fit(self, Is, Io=240, alpha=1, beta=0.15):
100101
maxCs = torch.stack([percentile(C[0, :], 99), percentile(C[1, :], 99)])
101102
self.HERef = HE
102103
self.maxCRef = maxCs
103-
elif self.norm_mode == 'fixed-single' or self.norm_mode == 'stochastic-single':
104+
elif self.norm_mode == "fixed-single" or self.norm_mode == "stochastic-single":
104105
# single img
105106
self.HERef, _, self.maxCRef = self.__compute_matrices_single(Is[0], Io, alpha, beta)
106107
else:
@@ -127,4 +128,4 @@ def normalize(self, I, Io=240, alpha=1, beta=0.15, stains=True):
127128
E[E > 255] = 255
128129
E = E.T.reshape(h, w, c).int()
129130

130-
return Inorm, H, E
131+
return Inorm, H, E

0 commit comments

Comments
 (0)