1
|
|
|
import torch |
2
|
|
|
import torchio as tio |
3
|
|
|
from ...utils import TorchioTestCase |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class TestClamp(TorchioTestCase): |
7
|
|
|
"""Tests for :class:`tio.Clamp` class.""" |
8
|
|
|
|
9
|
|
|
def test_out_min_max(self): |
10
|
|
|
transform = tio.Clamp(out_min=0, out_max=1) |
11
|
|
|
transformed = transform(self.sample_subject) |
12
|
|
|
self.assertEqual(transformed.t1.data.min(), 0) |
13
|
|
|
self.assertEqual(transformed.t1.data.max(), 1) |
14
|
|
|
|
15
|
|
|
def test_ct(self): |
16
|
|
|
ct_max = 1500 |
17
|
|
|
ct_min = -2000 |
18
|
|
|
ct_range = ct_max - ct_min |
19
|
|
|
tensor = torch.rand(1, 30, 30, 30) * ct_range + ct_min |
20
|
|
|
ct = tio.ScalarImage(tensor=tensor) |
21
|
|
|
ct_air = -1000 |
22
|
|
|
ct_bone = 1000 |
23
|
|
|
clamp = tio.Clamp(ct_air, ct_bone) |
24
|
|
|
clamped = clamp(ct) |
25
|
|
|
assert clamped.data.min() == ct_air |
26
|
|
|
assert clamped.data.max() == ct_bone |
27
|
|
|
|
28
|
|
|
def test_too_many_values_for_out_min(self): |
29
|
|
|
with self.assertRaises(TypeError): |
30
|
|
|
clamp = tio.Clamp(out_min=(1, 2)) |
31
|
|
|
clamp(self.sample_subject) |
32
|
|
|
|
33
|
|
|
def test_too_many_values_for_out_max(self): |
34
|
|
|
with self.assertRaises(TypeError): |
35
|
|
|
clamp = tio.Clamp(out_max=(1, 2)) |
36
|
|
|
clamp(self.sample_subject) |
37
|
|
|
|
38
|
|
|
def test_wrong_out_min_type(self): |
39
|
|
|
with self.assertRaises(TypeError): |
40
|
|
|
clamp = tio.Clamp(out_min='foo') |
41
|
|
|
clamp(self.sample_subject) |
42
|
|
|
|
43
|
|
|
def test_wrong_out_max_type(self): |
44
|
|
|
with self.assertRaises(TypeError): |
45
|
|
|
clamp = tio.Clamp(out_max='foo') |
46
|
|
|
clamp(self.sample_subject) |
47
|
|
|
|