Skip to content
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
24 changes: 24 additions & 0 deletions docs/source/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,18 @@ Utility
:members:
:special-members: __call__

`ConvertToMultiChannelBasedOnBratsClasses`
""""""""""""""""""""""""""""""""""""""""""
.. autoclass:: ConvertToMultiChannelBasedOnBratsClasses
:members:
:special-members: __call__

`AddExtremePointsChannel`
"""""""""""""""""""""""""
.. autoclass:: AddExtremePointsChannel
:members:
:special-members: __call__

`TorchVision`
"""""""""""""
.. autoclass:: TorchVision
Expand Down Expand Up @@ -975,6 +987,18 @@ Utility (Dict)
:members:
:special-members: __call__

`ConvertToMultiChannelBasedOnBratsClassesd`
"""""""""""""""""""""""""""""""""""""""""""
.. autoclass:: ConvertToMultiChannelBasedOnBratsClassesd
:members:
:special-members: __call__

`AddExtremePointsChanneld`
""""""""""""""""""""""""""
.. autoclass:: AddExtremePointsChanneld
:members:
:special-members: __call__

`TorchVisiond`
""""""""""""""
.. autoclass:: TorchVisiond
Expand Down
1 change: 1 addition & 0 deletions monai/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
AsChannelFirst,
AsChannelLast,
CastToType,
ConvertToMultiChannelBasedOnBratsClasses,
DataStats,
FgBgToIndices,
Identity,
Expand Down
22 changes: 22 additions & 0 deletions monai/transforms/utility/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"Lambda",
"LabelToMask",
"FgBgToIndices",
"ConvertToMultiChannelBasedOnBratsClasses",
"AddExtremePointsChannel",
"TorchVision",
]
Expand Down Expand Up @@ -556,6 +557,27 @@ def __call__(
return fg_indices, bg_indices


class ConvertToMultiChannelBasedOnBratsClasses(Transform):
"""
Convert labels to multi channels based on brats18 classes:
label 1 is the necrotic and non-enhancing tumor core
label 2 is the the peritumoral edema
label 4 is the GD-enhancing tumor
The possible classes are TC (Tumor core), WT (Whole tumor)
and ET (Enhancing tumor).
"""

def __call__(self, img: np.ndarray) -> np.ndarray:
result = []
# merge labels 1 (tumor non-enh) and 4 (tumor enh) to TC
result.append(np.logical_or(img == 1, img == 4))
# merge labels 1 (tumor non-enh) and 4 (tumor enh) and 2 (large edema) to WT
result.append(np.logical_or(np.logical_or(img == 1, img == 4), img == 2))
# label 4 is ET
result.append(img == 4)
return np.stack(result, axis=0).astype(np.float32)


class AddExtremePointsChannel(Transform, Randomizable):
"""
Add extreme points of label to the image as a new channel. This transform generates extreme
Expand Down
15 changes: 7 additions & 8 deletions monai/transforms/utility/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
AsChannelFirst,
AsChannelLast,
CastToType,
ConvertToMultiChannelBasedOnBratsClasses,
DataStats,
FgBgToIndices,
Identity,
Expand Down Expand Up @@ -649,6 +650,7 @@ def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.nda

class ConvertToMultiChannelBasedOnBratsClassesd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.ConvertToMultiChannelBasedOnBratsClasses`.
Convert labels to multi channels based on brats18 classes:
label 1 is the necrotic and non-enhancing tumor core
label 2 is the the peritumoral edema
Expand All @@ -657,17 +659,14 @@ class ConvertToMultiChannelBasedOnBratsClassesd(MapTransform):
and ET (Enhancing tumor).
"""

def __init__(self, keys: KeysCollection):
super().__init__(keys)
self.converter = ConvertToMultiChannelBasedOnBratsClasses()

def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
result = []
# merge labels 1 (tumor non-enh) and 4 (tumor enh) to TC
result.append(np.logical_or(d[key] == 1, d[key] == 4))
# merge labels 1 (tumor non-enh) and 4 (tumor enh) and 2 (large edema) to WT
result.append(np.logical_or(np.logical_or(d[key] == 1, d[key] == 4), d[key] == 2))
# label 4 is ET
result.append(d[key] == 4)
d[key] = np.stack(result, axis=0).astype(np.float32)
d[key] = self.converter(d[key])
return d


Expand Down
33 changes: 33 additions & 0 deletions tests/test_convert_to_multi_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2020 - 2021 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np
from parameterized import parameterized

from monai.transforms import ConvertToMultiChannelBasedOnBratsClasses

TEST_CASE = [
np.array([[0, 1, 2], [1, 2, 4], [0, 1, 4]]),
np.array([[[0, 1, 0], [1, 0, 1], [0, 1, 1]], [[0, 1, 1], [1, 1, 1], [0, 1, 1]], [[0, 0, 0], [0, 0, 1], [0, 0, 1]]]),
]


class TestConvertToMultiChannel(unittest.TestCase):
@parameterized.expand([TEST_CASE])
def test_type_shape(self, data, expected_result):
result = ConvertToMultiChannelBasedOnBratsClasses()(data)
np.testing.assert_equal(result, expected_result)


if __name__ == "__main__":
unittest.main()