Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalizer unit tests, bug fixes, and badges #8

Merged
merged 17 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: tests

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

jobs:
build:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- name: Set up Python 3.6
uses: actions/setup-python@v2
with:
python-version: 3.6

- name: Install dependencies
run: pip install wheel setuptools torch tensorflow numpy

- name: Build wheel
run: python setup.py bdist_wheel

- name: Upload Python wheel
uses: actions/upload-artifact@v2
with:
name: Python wheel
path: ${{github.workspace}}/dist/torchstain-*.whl
if-no-files-found: error

test:
needs: build
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-2019, ubuntu-18.04, macos-11]
python-version: [3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pip install wheel setuptools torch tensorflow numpy

- name: Install test dependencies
run: pip install opencv-python matplotlib torchvision scikit-image pytest

- name: Download artifact
uses: actions/download-artifact@master
with:
name: "Python wheel"

- name: Install wheel
run: pip install --find-links=${{github.workspace}} torchstain

- name: Run tests
run: |
pytest -v tests
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ ENV/
env.bak/
venv.bak/

# IDE
.vs/
.idea/

# Spyder project settings
.spyderproject
.spyproject
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# torchstain

[![License](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![tests](https://github.com/andreped/torchstain/workflows/tests/badge.svg)](https://github.com/andreped/torchstain/actions)
[![Pip Downloads](https://img.shields.io/pypi/dm/torchstain?label=pip%20downloads&logo=python)](https://pypi.org/project/torchstain/)

Pytorch-compatible normalization tools for histopathological images.
Normalization algorithms currently implemented:

Expand Down
11 changes: 11 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@
'numpy',
'tensorflow'
],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
],
python_requires='>=3.6'
)
46 changes: 46 additions & 0 deletions tests/test_normalizers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import cv2
import matplotlib.pyplot as plt
import torchstain
import torch
from torchvision import transforms
import time
from skimage.metrics import structural_similarity as ssim
import numpy as np


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 = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x*255)
])
t_to_transform = T(to_transform)

# initialize normalizers for each backend and fit to target image
normalizer = torchstain.MacenkoNormalizer(backend='numpy')
normalizer.fit(target)

torch_normalizer = torchstain.MacenkoNormalizer(backend='torch')
torch_normalizer.fit(T(target))

tf_normalizer = torchstain.MacenkoNormalizer(backend='tensorflow')
tf_normalizer.fit(T(target))

# transform
result_numpy, _, _ = normalizer.normalize(I=to_transform, stains=True)
result_torch, _, _ = torch_normalizer.normalize(I=t_to_transform, 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")
result_torch = result_torch.numpy().astype("float32")
result_tf = result_tf.numpy().astype("float32")

# assess whether the normalized images are identical across backends
np.testing.assert_almost_equal(ssim(result_numpy.flatten(), result_torch.flatten()), 1.0, decimal=4, verbose=True)
np.testing.assert_almost_equal(ssim(result_numpy.flatten(), result_tf.flatten()), 1.0, decimal=4, verbose=True)
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def test_percentile():
p_np = np.percentile(x, p, interpolation='nearest')
p_t = torchstain.utils.percentile(torch.tensor(x), p)

np.testing.assert_equal(p_np, p_t)
np.testing.assert_almost_equal(p_np, p_t)
1 change: 0 additions & 1 deletion torchstain/normalizers/torch_macenko_normalizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import torch
import cv2
from torchstain.normalizers.he_normalizer import HENormalizer
from torchstain.utils import cov, percentile

Expand Down