Skip to content
Closed
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
52 changes: 52 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Rust
on:
push:
branches: [ develop, main, master ]
pull_request:
branches: [ develop, main, master ]
jobs:
rust:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.12"
- name: Install dependencies
run: |
sudo apt update
sudo apt-get install -y libopenjp2-7 libopenjp2-tools
python -m pip install --upgrade pip
python -m pip install ruff==0.16.1 pytest pytest-cov pytest-runner
pip install uv
uv pip install --system torch torchvision --index-url https://download.pytorch.org/whl/cpu
uv pip install --system -e .
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Check formatting
run: cargo fmt --all -- --check
- name: Run Clippy
run: cargo clippy --all-targets --all-features -- -D warnings -A clippy::too_many_arguments
- name: Run Rust coverage
shell: bash
run: |
source <(cargo llvm-cov show-env --sh)
cargo llvm-cov clean --workspace
python -m pip install --force-reinstall --no-deps -e .
python -m pytest tests/rust -v
cargo test --all-features
cargo llvm-cov report --lcov --output-path lcov.info
- name: Upload Rust coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: lcov.info
flags: rust
fail_ci_if_error: false
1 change: 1 addition & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build:
os: ubuntu-24.04
tools:
python: "3.12"
rust: "latest"
apt_packages:
- libopenjp2-7-dev
- libopenjp2-tools
Expand Down
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[workspace]
resolver = "2"
members = [
"tiatoolbox/rust/utils/misc",
]

[workspace.package]
version = "0.1.0"
edition = "2024"

[workspace.dependencies]
pyo3 = "0.29.2"
49,437 changes: 49,437 additions & 0 deletions benchmarks/comparing_misc_functions_in_rust_v_python.ipynb

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ torchvision = [
omit = ['tests/*', 'tiatoolbox/__main__.py', '*/utils/env_detection.py', 'tiatoolbox/typing.py']

[build-system]
requires = ["setuptools"]
requires = [
"setuptools",
"setuptools-rust",
]
build-backend = "setuptools.build_meta"

[tool.distutils.bdist_wheel]
Expand Down Expand Up @@ -298,3 +301,10 @@ ignore_missing_imports = true
# Default local target: minimum supported Python (requires-python lower bound).
# CI overrides this per job with --python-version for 3.12-3.14.
python_version = "3.12"

[[tool.setuptools-rust.ext-modules]]
target = "tiatoolbox.rust.utils.misc"
path = "tiatoolbox/rust/utils/misc/Cargo.toml"
binding = "PyO3"
#Uncomment below for release
#debug = false
1 change: 1 addition & 0 deletions tests/rust/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Unit test package for rust code in tiatoolbox."""
63 changes: 63 additions & 0 deletions tests/rust/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Test for rust functionality."""

import numpy as np

from tiatoolbox import utils


def test_contrast_enhancer() -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like contents of this file are duplicated in the other PR #1142. Is that intentional?

"""Test contrast enhancement functionality."""
input_array = np.array(
[
[
[37, 244, 193, 106, 235, 128, 71, 140, 47],
[103, 184, 72, 20, 188, 238, 126, 7, 0],
[137, 195, 204, 32, 203, 170, 101, 77, 133],
]
],
dtype=np.uint8,
)

# expected output of the contrast_enhancer
result_array = np.array(
[
[
[35, 255, 203, 110, 248, 133, 72, 146, 46],
[106, 193, 73, 17, 198, 251, 131, 3, 0],
[143, 205, 215, 30, 214, 178, 104, 78, 139],
]
],
dtype=np.uint8,
)

# Calculating the contrast enhanced version of input_array
output_array = utils.misc.contrast_enhancer(input_array, low_p=2, high_p=98)
# The out_put array should be equal to expected result_array
assert np.all(result_array == output_array)
input_array = np.array(
[
[
[0],
[0],
[0],
]
],
dtype=np.uint8,
)

# expected output of the contrast_enhancer
result_array = np.array(
[
[
[0],
[0],
[0],
]
],
dtype=np.uint8,
)

# Calculating the contrast enhanced version of input_array
output_array = utils.misc.contrast_enhancer(input_array, low_p=2, high_p=98)
# The out_put array should be equal to expected result_array
assert np.all(result_array == output_array)
35 changes: 35 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,41 @@ def test_contrast_enhancer() -> None:
output_array = utils.misc.contrast_enhancer(input_array, low_p=2, high_p=98)
# The out_put array should be equal to expected result_array
assert np.all(result_array == output_array)
# Calculating the contrast enhanced version of input_array

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the comment above, lines 918-952 are duplicated in #1142.

input_array = np.array([0, 255], dtype=np.uint8)
result_array = np.array([0, 255], dtype=np.uint8)
output_array = utils.misc.contrast_enhancer(input_array, low_p=98, high_p=2)
# The out_put array should be equal to expected result_array
assert np.all(result_array == output_array)
input_array = np.array([0, 0], dtype=np.uint8)
result_array = np.array([0, 0], dtype=np.uint8)
output_array = utils.misc.contrast_enhancer(input_array, low_p=98, high_p=2)
# The out_put array should be equal to expected result_array
assert np.all(result_array == output_array)

input_array = np.array(
[
[37, 244, 193, 106, 235, 128, 71, 140, 47],
[103, 184, 72, 20, 188, 238, 126, 7, 0],
[137, 195, 204, 32, 203, 170, 101, 77, 133],
],
dtype=np.uint8,
)

# expected output of the contrast_enhancer
result_array = np.array(
[
[35, 255, 203, 110, 248, 133, 72, 146, 46],
[106, 193, 73, 17, 198, 251, 131, 3, 0],
[143, 205, 215, 30, 214, 178, 104, 78, 139],
],
dtype=np.uint8,
)

# Calculating the contrast enhanced version of input_array
output_array = utils.misc.contrast_enhancer(input_array, low_p=2, high_p=98)
# The out_put array should be equal to expected result_array
assert np.all(result_array == output_array)


def test_load_stain_matrix(track_tmp_path: Path) -> None:
Expand Down
13 changes: 13 additions & 0 deletions tiatoolbox/rust/utils/misc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "misc"
version.workspace = true
edition.workspace = true

[lib]
name = "misc"
crate-type = ["cdylib", "rlib"]

[dependencies]
ndarray = "0.17.2"
numpy = "0.29.0"
pyo3 = { workspace = true, features = ["extension-module"] }
10 changes: 10 additions & 0 deletions tiatoolbox/rust/utils/misc/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# tiatoolbox/rust/utils/misc.pyi

import numpy as np
from numpy.typing import NDArray

def contrast_enhancer(
img: NDArray[np.uint8],
low_p: int,
high_p: int,
) -> NDArray[np.uint8]: ...
80 changes: 80 additions & 0 deletions tiatoolbox/rust/utils/misc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use ndarray::{Array1, Array3};
use numpy::{IntoPyArray, PyArray3, PyReadonlyArray3};
use pyo3::prelude::*;

fn rescale_intensity(x: f32, in_range_low: f32, in_range_high: f32, range: f32) -> u8 {
//asssumes out_min = 0 and out_max = 255
if x <= in_range_low {
0
} else if x >= in_range_high {
255
} else {
(255.0 * ((x - in_range_low) / range)) as u8
}
}

fn rust_contrast_enhancer(img: Array3<u8>, low_p: u8, high_p: u8) -> Array3<u8> {
/*Get tissue mask based on the luminosity of the input image.

Args:
img: Array
Input image used to obtain tissue mask.
threshold (float):
Luminosity threshold used to determine tissue area.

Returns:
tissue_mask
Binary tissue mask.

*/
let img_out = img.to_owned();
let len = img.len();
let mut flat_img_out: Array1<u8> = img_out.into_shape_with_order(len).unwrap();

if let Some(slice) = flat_img_out.as_slice_mut() {
slice.sort_unstable();
}

let lenf32: f32 = len as f32;

let p_low_index: f32 = (lenf32 - 1.0) * low_p as f32 / 100.0;
let p_low_index_difference = p_low_index - p_low_index.floor();
let mut p_low = (1.0 - p_low_index_difference)
* flat_img_out[p_low_index.floor() as usize] as f32
+ p_low_index_difference * flat_img_out[p_low_index.ceil() as usize] as f32;

let p_high_index: f32 = (lenf32 - 1.0) * high_p as f32 / 100.0;
let p_high_index_difference = p_high_index - p_high_index.floor();
let mut p_high = (1.0 - p_high_index_difference)
* flat_img_out[p_high_index.floor() as usize] as f32
+ p_high_index_difference * flat_img_out[p_high_index.ceil() as usize] as f32;

if p_low >= p_high {
p_low = flat_img_out[0].into();
p_high = flat_img_out[len - 1].into();
}

if p_high > p_low {
let range = p_high - p_low;
return img.mapv(|x| rescale_intensity(x.into(), p_low, p_high, range));
}

img
}

#[pyfunction]
fn contrast_enhancer<'py>(
py: Python<'py>,
img: PyReadonlyArray3<'py, u8>,
low_p: u8,
high_p: u8,
) -> Bound<'py, PyArray3<u8>> {
let data = img.as_array().to_owned();
rust_contrast_enhancer(data, low_p, high_p).into_pyarray(py)
}

#[pymodule]
fn misc(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(contrast_enhancer, m)?)?;
Ok(())
}
4 changes: 4 additions & 0 deletions tiatoolbox/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from tqdm.auto import tqdm, trange
from tqdm.dask import TqdmCallback

import tiatoolbox.rust.utils.misc as rmisc
from tiatoolbox import logger
from tiatoolbox.annotation.storage import Annotation, AnnotationStore, SQLiteStore
from tiatoolbox.utils.exceptions import FileNotSupportedError
Expand Down Expand Up @@ -430,6 +431,9 @@ def contrast_enhancer(img: np.ndarray, low_p: int = 2, high_p: int = 98) -> np.n
if img.dtype != np.uint8:
msg = "Image should be uint8."
raise AssertionError(msg)
dimension_for_rust = 3
if img.ndim == dimension_for_rust:
return rmisc.contrast_enhancer(img, low_p, high_p)
img_out = img.copy()
percentiles = np.array(np.percentile(img_out, (low_p, high_p)))
p_low, p_high = percentiles[0], percentiles[1]
Expand Down
Loading