Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add label remapping transforms (#402)
* Fix KeyError when retrieving history for a custom Transform. * Preserve include and exclude lists for inverse transforms. * Fix KeyError when retrieving history for a custom Transform. Preserve include and exclude lists for inverse transforms. * Add label transformations. * Add minor edits Co-authored-by: Fernando <fepegar@gmail.com>
- Loading branch information
Showing
20 changed files
with
447 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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,17 @@ | ||
from torchio.transforms import RemapLabels | ||
from ...utils import TorchioTestCase | ||
|
||
|
||
class TestRemapLabels(TorchioTestCase): | ||
"""Tests for `RemapLabels`.""" | ||
def test_remap(self): | ||
remapping = {1: 2, 2: 1, 5: 10, 6: 11} | ||
remap_labels = RemapLabels(remapping=remapping) | ||
|
||
subject = self.get_subject_with_labels(labels=remapping.keys()) | ||
transformed = remap_labels(subject) | ||
inverse_transformed = transformed.apply_inverse_transform() | ||
|
||
self.assertEqual(self.get_unique_labels(subject.label), set(remapping.keys())) | ||
self.assertEqual(self.get_unique_labels(transformed.label), set(remapping.values())) | ||
self.assertEqual(self.get_unique_labels(inverse_transformed.label), set(remapping.keys())) |
This file contains 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,28 @@ | ||
from torchio.transforms import RemoveLabels | ||
from ...utils import TorchioTestCase | ||
|
||
|
||
class TestRemoveLabels(TorchioTestCase): | ||
"""Tests for `RemoveLabels`.""" | ||
def test_remove(self): | ||
initial_labels = (1, 2, 3, 4, 5, 6, 7) | ||
labels_to_remove = (1, 2, 5, 6) | ||
remaining_labels = (3, 4, 7) | ||
|
||
remove_labels = RemoveLabels(labels_to_remove) | ||
|
||
subject = self.get_subject_with_labels(labels=initial_labels) | ||
transformed = remove_labels(subject) | ||
inverse_transformed = transformed.apply_inverse_transform(warn=False) | ||
self.assertEqual( | ||
self.get_unique_labels(subject.label), | ||
set(initial_labels), | ||
) | ||
self.assertEqual( | ||
self.get_unique_labels(transformed.label), | ||
set(remaining_labels), | ||
) | ||
self.assertEqual( | ||
self.get_unique_labels(inverse_transformed.label), | ||
set(remaining_labels), | ||
) |
This file contains 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,19 @@ | ||
from torchio.transforms import SequentialLabels | ||
from ...utils import TorchioTestCase | ||
|
||
|
||
class TestSequentialLabels(TorchioTestCase): | ||
"""Tests for `SequentialLabels`.""" | ||
def test_sequential(self): | ||
initial_labels = (2, 8, 9, 10, 15, 20, 100) | ||
transformed_labels = (1, 2, 3, 4, 5, 6, 7) | ||
|
||
sequential_labels = SequentialLabels() | ||
|
||
subject = self.get_subject_with_labels(labels=initial_labels) | ||
transformed = sequential_labels(subject) | ||
inverse_transformed = transformed.apply_inverse_transform() | ||
|
||
self.assertEqual(self.get_unique_labels(subject.label), set(initial_labels)) | ||
self.assertEqual(self.get_unique_labels(transformed.label), set(transformed_labels)) | ||
self.assertEqual(self.get_unique_labels(inverse_transformed.label), set(initial_labels)) |
This file contains 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 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 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 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 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 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 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 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 @@ | ||
from .remap_labels import RemapLabels | ||
from .sequential_labels import SequentialLabels | ||
from .remove_labels import RemoveLabels | ||
|
||
|
||
__all__ = [ | ||
'RemapLabels', | ||
'SequentialLabels', | ||
'RemoveLabels', | ||
] |
Oops, something went wrong.