-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transform to ensure shape multiple of N (#401)
* Add transform to ensure shape multiple of N * Add transform to ensure shape multiple of N * Ensure single dimensions do not disappear * Add tests for EnsureShapeMultiple * Stop making 2D images 3D
- v0.20.4
- v0.20.3
- v0.20.2
- v0.20.1
- v0.20.0
- v0.19.9
- v0.19.8
- v0.19.7
- v0.19.6
- v0.19.5
- v0.19.4
- v0.19.3
- v0.19.2
- v0.19.1
- v0.19.0
- v0.18.92
- v0.18.91
- v0.18.90
- v0.18.89
- v0.18.88
- v0.18.87
- v0.18.86
- v0.18.85
- v0.18.84
- v0.18.83
- v0.18.82
- v0.18.81
- v0.18.80
- v0.18.79
- v0.18.78
- v0.18.77
- v0.18.76
- v0.18.75
- v0.18.74
- v0.18.73
- v0.18.72
- v0.18.71
- v0.18.70
- v0.18.69
- v0.18.68
- v0.18.67
- v0.18.66
- v0.18.65
- v0.18.64
- v0.18.63
- v0.18.62
- v0.18.61
- v0.18.60
- v0.18.59
- v0.18.58
- v0.18.57
- v0.18.56
- v0.18.55
- v0.18.54
- v0.18.53
- v0.18.52
- v0.18.51
- v0.18.50
- v0.18.49
- v0.18.48
- v0.18.47
- v0.18.46
- v0.18.45
- v0.18.44
- v0.18.43
- v0.18.42
- v0.18.41
- v0.18.40
- v0.18.39
- v0.18.38
- v0.18.37
- v0.18.36
- v0.18.35
- v0.18.34
- v0.18.33
- v0.18.32
- v0.18.31
- v0.18.30
- v0.18.29
- v0.18.28
- v0.18.27
- v0.18.26
- v0.18.25
- v0.18.24
- v0.18.23
- v0.18.22
- v0.18.21
- v0.18.20
- v0.18.19
- v0.18.18
- v0.18.17
- v0.18.16
- v0.18.15
- v0.18.14
- test
Showing
7 changed files
with
108 additions
and
2 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