Skip to content

Commit

Permalink
Added test for multimacenko tf vs numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
andreped committed Jan 14, 2025
1 parent 17127a5 commit 8fbbe79
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/test_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import tensorflow as tf
import numpy as np


def test_cov():
x = np.random.randn(10, 10)
cov_np = np.cov(x)
cov_t = torchstain.tf.utils.cov(x)

np.testing.assert_almost_equal(cov_np, cov_t.numpy())


def test_percentile():
x = np.random.randn(10, 10)
p = 20
Expand All @@ -20,6 +22,7 @@ def test_percentile():

np.testing.assert_almost_equal(p_np, p_t)


def test_macenko_tf():
size = 1024
curr_file_path = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -48,6 +51,7 @@ def test_macenko_tf():
# assess whether the normalized images are identical across backends
np.testing.assert_almost_equal(result_numpy.flatten(), result_tf.flatten(), decimal=2, verbose=True)


def test_reinhard_tf():
size = 1024
curr_file_path = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -75,3 +79,37 @@ def test_reinhard_tf():

# assess whether the normalized images are identical across backends
np.testing.assert_almost_equal(result_numpy.flatten(), result_tf.flatten(), decimal=2, verbose=True)


def test_multistain_tf():
size = 1024
curr_file_path = os.path.dirname(os.path.realpath(__file__))
target = cv2.resize(cv2.cvtColor(cv2.imread(os.path.join(curr_file_path, "../data/target.png")), cv2.COLOR_BGR2RGB), (size, size))
to_transform = cv2.resize(cv2.cvtColor(cv2.imread(os.path.join(curr_file_path, "../data/source.png")), cv2.COLOR_BGR2RGB), (size, size))

# setup preprocessing and preprocess image to be normalized
T = lambda x: tf.convert_to_tensor(np.moveaxis(x, -1, 0).astype("float32")) # * 255
t_to_transform = T(to_transform)
target_transformed = T(target)

# move channel to first
target_numpy = np.moveaxis(target, -1, 0)
to_transform_numpy = np.moveaxis(to_transform, -1, 0)

# initialize normalizers for each backend and fit to target image
normalizer = torchstain.normalizers.MultiMacenkoNormalizer(backend='numpy')
normalizer.fit([target_numpy, target_numpy, target_numpy])

tf_normalizer = torchstain.normalizers.MultiMacenkoNormalizer(backend='tensorflow')
tf_normalizer.fit([target_transformed, target_transformed, target_transformed])

# transform
result_numpy, _, _ = normalizer.normalize(I=to_transform_numpy, stains=True)
result_tf, _, _ = tf_normalizer.normalize(I=t_to_transform, stains=True)

# convert to numpy and set dtype
result_numpy = result_numpy.astype("float32") / 255.
result_tf = result_tf.numpy().astype("float32") / 255.

# assess whether the normalized images are identical across backends
np.testing.assert_almost_equal(result_numpy.flatten(), result_tf.flatten(), decimal=2, verbose=True)

0 comments on commit 8fbbe79

Please sign in to comment.