|
| 1 | +# Copyright 2020 - 2021 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import unittest |
| 13 | +from typing import TYPE_CHECKING |
| 14 | + |
| 15 | +from parameterized import parameterized |
| 16 | + |
| 17 | +from monai.apps.pathology.transforms import ExtractStainsMacenko |
| 18 | +from monai.utils import exact_version, optional_import |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + import cupy as cp |
| 22 | +else: |
| 23 | + cp, _ = optional_import("cupy", "8.6.0", exact_version) |
| 24 | + |
| 25 | +# input pixels are all transparent and below the beta absorbance threshold |
| 26 | +EXTRACT_STAINS_TEST_CASE_1 = [ |
| 27 | + cp.zeros((3, 2, 3)), |
| 28 | + cp.array([[0.0, 0.0], [0.70710678, 0.70710678], [0.70710678, 0.70710678]]), |
| 29 | +] |
| 30 | + |
| 31 | +# input pixels are all the same, but above beta absorbance threshold |
| 32 | +EXTRACT_STAINS_TEST_CASE_2 = [ |
| 33 | + cp.full((3, 2, 3), 200), |
| 34 | + cp.array([[0.57735027, 0.57735027], [0.57735027, 0.57735027], [0.57735027, 0.57735027]]), |
| 35 | +] |
| 36 | + |
| 37 | +# input pixels are all transparent and below the beta absorbance threshold |
| 38 | +NORMALIZE_STAINS_TEST_CASE_1 = [ |
| 39 | + {}, |
| 40 | + cp.zeros((3, 2, 3)), |
| 41 | + cp.array([[[63, 25, 60], [63, 25, 60]], [[63, 25, 60], [63, 25, 60]], [[63, 25, 60], [63, 25, 60]]]), |
| 42 | +] |
| 43 | + |
| 44 | +# input pixels are all the same, but above beta absorbance threshold |
| 45 | +NORMALIZE_STAINS_TEST_CASE_2 = [ |
| 46 | + {}, |
| 47 | + cp.full((3, 2, 3), 200), |
| 48 | + cp.array([[[63, 25, 60], [63, 25, 60]], [[63, 25, 60], [63, 25, 60]], [[63, 25, 60], [63, 25, 60]]]), |
| 49 | +] |
| 50 | + |
| 51 | +# with a custom target_he, which is the same as the image's stain matrix |
| 52 | +NORMALIZE_STAINS_TEST_CASE_3 = [ |
| 53 | + {"target_he": cp.full((3, 2), 0.57735027)}, |
| 54 | + cp.full((3, 2, 3), 200), |
| 55 | + cp.full((3, 2, 3), 42), |
| 56 | +] |
| 57 | + |
| 58 | + |
| 59 | +class TestExtractStainsMacenko(unittest.TestCase): |
| 60 | + @parameterized.expand([EXTRACT_STAINS_TEST_CASE_1, EXTRACT_STAINS_TEST_CASE_2]) |
| 61 | + def test_value(self, image, expected_data): |
| 62 | + result = ExtractStainsMacenko()(image) |
| 63 | + cp.testing.assert_allclose(result, expected_data) |
| 64 | + |
| 65 | + |
| 66 | +class TestNormalizeStainsMacenko(unittest.TestCase): |
| 67 | + @parameterized.expand([NORMALIZE_STAINS_TEST_CASE_1, NORMALIZE_STAINS_TEST_CASE_2, NORMALIZE_STAINS_TEST_CASE_3]) |
| 68 | + def test_value(self, argments, image, expected_data): |
| 69 | + result = NormalizeStainsMacenko(**argments)(image) |
| 70 | + cp.testing.assert_allclose(result, expected_data) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + unittest.main() |
0 commit comments