-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
4 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
31 changes: 31 additions & 0 deletions
31
tests/transforms/preprocessing/test_ensure_shape_multiple.py
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,31 @@ | ||
import torchio as tio | ||
from ...utils import TorchioTestCase | ||
|
||
|
||
class TestEnsureShapeMultiple(TorchioTestCase): | ||
|
||
def test_bad_method(self): | ||
with self.assertRaises(ValueError): | ||
tio.EnsureShapeMultiple(1, method='bad') | ||
|
||
def test_pad(self): | ||
sample_t1 = self.sample_subject.t1 | ||
assert sample_t1.shape == (1, 10, 20, 30) | ||
transform = tio.EnsureShapeMultiple(4, method='pad') | ||
transformed = transform(sample_t1) | ||
assert transformed.shape == (1, 12, 20, 32) | ||
|
||
def test_crop(self): | ||
sample_t1 = self.sample_subject.t1 | ||
assert sample_t1.shape == (1, 10, 20, 30) | ||
transform = tio.EnsureShapeMultiple(4, method='crop') | ||
transformed = transform(sample_t1) | ||
assert transformed.shape == (1, 8, 20, 28) | ||
|
||
def test_2d(self): | ||
sample_t1 = self.sample_subject.t1 | ||
sample_2d = sample_t1.data[..., :1] | ||
assert sample_2d.shape == (1, 10, 20, 1) | ||
transform = tio.EnsureShapeMultiple(4, method='crop') | ||
transformed = transform(sample_2d) | ||
assert transformed.shape == (1, 8, 20, 1) |
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
62 changes: 62 additions & 0 deletions
62
torchio/transforms/preprocessing/spatial/ensure_shape_multiple.py
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,62 @@ | ||
from typing import Union, Optional | ||
|
||
import numpy as np | ||
|
||
from ... import SpatialTransform | ||
from ....utils import to_tuple | ||
from ....data.subject import Subject | ||
from ....typing import TypeTripletInt | ||
from .crop_or_pad import CropOrPad | ||
|
||
|
||
class EnsureShapeMultiple(SpatialTransform): | ||
"""Crop or pad an image to a shape that is a multiple of :math:`N`. | ||
Args: | ||
target_multiple: Tuple :math:`(w, h, d)`. If a single value :math:`n` is | ||
provided, then :math:`w = h = d = n`. | ||
method: Either ``'crop'`` or ``'pad'``. | ||
**kwargs: See :class:`~torchio.transforms.Transform` for additional | ||
keyword arguments. | ||
Example: | ||
>>> import torchio as tio | ||
>>> image = tio.datasets.Colin27().t1 | ||
>>> image.shape | ||
(1, 181, 217, 181) | ||
>>> transform = tio.EnsureShapeMultiple(8, method='pad') | ||
>>> transformed = transform(image) | ||
>>> transformed.shape | ||
(1, 184, 224, 184) | ||
>>> transform = tio.EnsureShapeMultiple(8, method='crop') | ||
>>> transformed = transform(image) | ||
>>> transformed.shape | ||
(1, 176, 216, 176) | ||
>>> image_2d = image.data[..., :1] | ||
>>> image_2d.shape | ||
torch.Size([1, 181, 217, 1]) | ||
>>> transformed = transform(image_2d) | ||
>>> transformed.shape | ||
torch.Size([1, 176, 216, 1]) | ||
""" | ||
def __init__( | ||
self, | ||
target_multiple: Union[int, TypeTripletInt], | ||
*, | ||
method: Optional[str] = 'pad', | ||
**kwargs | ||
): | ||
super().__init__(**kwargs) | ||
self.target_multiple = np.array(to_tuple(target_multiple, 3)) | ||
if method not in ('crop', 'pad'): | ||
raise ValueError('Method must be "crop" or "pad"') | ||
self.method = method | ||
|
||
def apply_transform(self, subject: Subject) -> Subject: | ||
source_shape = np.array(subject.spatial_shape, np.uint16) | ||
function = np.floor if self.method == 'crop' else np.ceil | ||
integer_ratio = function(source_shape / self.target_multiple) | ||
target_shape = integer_ratio * self.target_multiple | ||
target_shape = np.maximum(target_shape, 1) | ||
return CropOrPad(target_shape.astype(int))(subject) |
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