Repository navigation
🆕 Convert contrast_enhancer to rust
#1141
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6201c0e
Made contrast enhancer in rust
hannah275 3668241
Merge branch 'TissueImageAnalytics:develop' into rcontrast
hannah275 4412d4a
Update viewer.js to remove stashed changes lines
hannah275 1dcf874
Corrected errors
hannah275 b58e4a1
Removed unused imports
hannah275 32bfc4b
Removed unused imports
hannah275 443f802
Removed unused code
hannah275 3ea28cd
Merge branch 'develop' into rcontrast
shaneahmed 983314d
Merge branch 'develop' into rcontrast
abdol 1baf6a1
Merge branch 'develop' into rcontrast
shaneahmed 2aea21c
Merge branch 'develop' into rcontrast
abdol ceccc92
Merge branch 'develop' into rcontrast
shaneahmed dc9027e
Merge branch 'develop' into rcontrast
shaneahmed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
49,437
benchmarks/comparing_misc_functions_in_rust_v_python.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Unit test package for rust code in tiatoolbox.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
| """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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?