Skip to content

Commit aaedd68

Browse files
authored
refactor: corrected for inconsistencies in code
1 parent 1076efe commit aaedd68

File tree

6 files changed

+23
-26
lines changed

6 files changed

+23
-26
lines changed

compare.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import torch
22
from torchvision import transforms
33
import torchstain
4-
54
import cv2
65
import matplotlib.pyplot as plt
76
import time

example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from torchvision import transforms
66
import time
77

8-
98
size = 1024
109
target = cv2.resize(cv2.cvtColor(cv2.imread("./data/target.png"), cv2.COLOR_BGR2RGB), (size, size))
1110
to_transform = cv2.resize(cv2.cvtColor(cv2.imread("./data/source.png"), cv2.COLOR_BGR2RGB), (size, size))

torchstain/numpy/normalizers/macenko.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def __init__(self):
1010
super().__init__()
1111

1212
self.HERef = np.array([[0.5626, 0.2159],
13-
[0.7201, 0.8012],
14-
[0.4062, 0.5581]])
13+
[0.7201, 0.8012],
14+
[0.4062, 0.5581]])
1515
self.maxCRef = np.array([1.9705, 1.0308])
1616

1717
def __convert_rgb2od(self, I, Io=240, beta=0.15):
1818
# calculate optical density
19-
OD = -np.log((I.astype(float)+1)/Io)
19+
OD = -np.log((I.astype(float) + 1) / Io)
2020

2121
# remove transparent pixels
2222
ODhat = OD[~np.any(OD < beta, axis=1)]
@@ -26,22 +26,22 @@ def __convert_rgb2od(self, I, Io=240, beta=0.15):
2626
def __find_HE(self, ODhat, eigvecs, alpha):
2727
#project on the plane spanned by the eigenvectors corresponding to the two
2828
# largest eigenvalues
29-
That = ODhat.dot(eigvecs[:,1:3])
29+
That = ODhat.dot(eigvecs[:, 1:3])
3030

31-
phi = np.arctan2(That[:,1],That[:,0])
31+
phi = np.arctan2(That[:, 1], That[:, 0])
3232

3333
minPhi = np.percentile(phi, alpha)
34-
maxPhi = np.percentile(phi, 100-alpha)
34+
maxPhi = np.percentile(phi, 100 - alpha)
3535

36-
vMin = eigvecs[:,1:3].dot(np.array([(np.cos(minPhi), np.sin(minPhi))]).T)
37-
vMax = eigvecs[:,1:3].dot(np.array([(np.cos(maxPhi), np.sin(maxPhi))]).T)
36+
vMin = eigvecs[:, 1:3].dot(np.array([(np.cos(minPhi), np.sin(minPhi))]).T)
37+
vMax = eigvecs[:, 1:3].dot(np.array([(np.cos(maxPhi), np.sin(maxPhi))]).T)
3838

3939
# a heuristic to make the vector corresponding to hematoxylin first and the
4040
# one corresponding to eosin second
4141
if vMin[0] > vMax[0]:
42-
HE = np.array((vMin[:,0], vMax[:,0])).T
42+
HE = np.array((vMin[:, 0], vMax[:, 0])).T
4343
else:
44-
HE = np.array((vMax[:,0], vMin[:,0])).T
44+
HE = np.array((vMax[:, 0], vMin[:, 0])).T
4545

4646
return HE
4747

@@ -55,7 +55,7 @@ def __find_concentration(self, OD, HE):
5555
return C
5656

5757
def __compute_matrices(self, I, Io, alpha, beta):
58-
I = I.reshape((-1,3))
58+
I = I.reshape((-1, 3))
5959

6060
OD, ODhat = self.__convert_rgb2od(I, Io=Io, beta=beta)
6161

@@ -67,7 +67,7 @@ def __compute_matrices(self, I, Io, alpha, beta):
6767
C = self.__find_concentration(OD, HE)
6868

6969
# normalize stain concentrations
70-
maxC = np.array([np.percentile(C[0,:], 99), np.percentile(C[1,:],99)])
70+
maxC = np.array([np.percentile(C[0, :], 99), np.percentile(C[1, :],99)])
7171

7272
return HE, C, maxC
7373

@@ -81,7 +81,7 @@ def normalize(self, I, Io=240, alpha=1, beta=0.15, stains=True):
8181
''' Normalize staining appearence of H&E stained images
8282
8383
Example use:
84-
see test.py
84+
see example.py
8585
8686
Input:
8787
I: RGB input image
@@ -97,7 +97,7 @@ def normalize(self, I, Io=240, alpha=1, beta=0.15, stains=True):
9797
Macenko et al., ISBI 2009
9898
'''
9999
h, w, c = I.shape
100-
I = I.reshape((-1,3))
100+
I = I.reshape((-1, 3))
101101

102102
HE, C, maxC = self.__compute_matrices(I, Io, alpha, beta)
103103

@@ -114,11 +114,11 @@ def normalize(self, I, Io=240, alpha=1, beta=0.15, stains=True):
114114

115115
if stains:
116116
# unmix hematoxylin and eosin
117-
H = np.multiply(Io, np.exp(np.expand_dims(-self.HERef[:,0], axis=1).dot(np.expand_dims(C2[0,:], axis=0))))
117+
H = np.multiply(Io, np.exp(np.expand_dims(-self.HERef[:, 0], axis=1).dot(np.expand_dims(C2[0, :], axis=0))))
118118
H[H > 255] = 255
119119
H = np.reshape(H.T, (h, w, c)).astype(np.uint8)
120120

121-
E = np.multiply(Io, np.exp(np.expand_dims(-self.HERef[:,1], axis=1).dot(np.expand_dims(C2[1,:], axis=0))))
121+
E = np.multiply(Io, np.exp(np.expand_dims(-self.HERef[:, 1], axis=1).dot(np.expand_dims(C2[1, :], axis=0))))
122122
E[E > 255] = 255
123123
E = np.reshape(E.T, (h, w, c)).astype(np.uint8)
124124

torchstain/tf/normalizers/macenko.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import numpy as np
55
import tensorflow.keras.backend as K
66

7-
87
"""
98
Source code ported from: https://github.com/schaugf/HEnorm_python
109
Original implementation: https://github.com/mitkovetta/staining-normalization
@@ -14,8 +13,8 @@ def __init__(self):
1413
super().__init__()
1514

1615
self.HERef = tf.constant([[0.5626, 0.2159],
17-
[0.7201, 0.8012],
18-
[0.4062, 0.5581]])
16+
[0.7201, 0.8012],
17+
[0.4062, 0.5581]])
1918
self.maxCRef = tf.constant([1.9705, 1.0308])
2019

2120
def __convert_rgb2od(self, I, Io, beta):
@@ -78,7 +77,7 @@ def normalize(self, I, Io=240, alpha=1, beta=0.15, stains=True):
7877
''' Normalize staining appearence of H&E stained images
7978
8079
Example use:
81-
see test.py
80+
see example.py
8281
8382
Input:
8483
I: RGB input image: tensor of shape [C, H, W] and type uint8

torchstain/torch/normalizers/macenko.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __convert_rgb2od(self, I, Io, beta):
2020
I = I.permute(1, 2, 0)
2121

2222
# calculate optical density
23-
OD = -torch.log((I.reshape((-1, I.shape[-1])).float() + 1)/Io)
23+
OD = -torch.log((I.reshape((-1, I.shape[-1])).float() + 1) / Io)
2424

2525
# remove transparent pixels
2626
ODhat = OD[~torch.any(OD < beta, dim=1)]
@@ -79,7 +79,7 @@ def normalize(self, I, Io=240, alpha=1, beta=0.15, stains=True):
7979
''' Normalize staining appearence of H&E stained images
8080
8181
Example use:
82-
see test.py
82+
see example.py
8383
8484
Input:
8585
I: RGB input image: tensor of shape [C, H, W] and type uint8

torchstain/torch/utils/rgb2lab.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# constant conversion matrices between color spaces: https://gist.github.com/bikz05/6fd21c812ef6ebac66e1
44
_rgb2xyz = torch.tensor([[0.412453, 0.357580, 0.180423],
5-
[0.212671, 0.715160, 0.072169],
6-
[0.019334, 0.119193, 0.950227]])
5+
[0.212671, 0.715160, 0.072169],
6+
[0.019334, 0.119193, 0.950227]])
77

88
_white = torch.tensor([0.95047, 1., 1.08883])
99

0 commit comments

Comments
 (0)