1
|
|
|
# coding=utf-8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
Tests for deepreg/dataset/loader/util.py in pytest style |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
from test.unit.util import is_equal_np |
8
|
|
|
|
9
|
|
|
import numpy as np |
10
|
|
|
import pytest |
11
|
|
|
|
12
|
|
|
import deepreg.dataset.loader.util as util |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class TestNormalizeArray: |
16
|
|
|
@pytest.mark.parametrize( |
17
|
|
|
"arr,expected", |
18
|
|
|
[ |
19
|
|
|
[np.array([0, 1, 2]), np.array([0, 0.5, 1])], |
20
|
|
|
[np.array([-2, 0, 1, 2]), np.array([0, 0.5, 0.75, 1])], |
21
|
|
|
[np.array([1, 1, 1, 1]), np.array([0, 0, 0, 0])], |
22
|
|
|
], |
23
|
|
|
) |
24
|
|
|
def test_no_min_no_max(self, arr, expected): |
25
|
|
|
got = util.normalize_array(arr=arr) |
26
|
|
|
assert is_equal_np(got, expected) |
27
|
|
|
|
28
|
|
|
@pytest.mark.parametrize( |
29
|
|
|
"arr,v_max,expected", |
30
|
|
|
[ |
31
|
|
|
[np.array([0, 1, 2]), 1, np.array([0, 1, 1])], |
32
|
|
|
[np.array([-2, 0, 1, 2]), 3, np.array([0, 0.4, 0.6, 0.8])], |
33
|
|
|
], |
34
|
|
|
) |
35
|
|
|
def test_no_min(self, arr, v_max, expected): |
36
|
|
|
got = util.normalize_array(arr=arr, v_max=v_max) |
37
|
|
|
assert is_equal_np(got, expected) |
38
|
|
|
|
39
|
|
|
@pytest.mark.parametrize( |
40
|
|
|
"arr,v_min,expected", |
41
|
|
|
[ |
42
|
|
|
[np.array([0, 1, 2]), 1, np.array([0, 0, 1])], |
43
|
|
|
[np.array([-2, 0, 1, 2]), -3, np.array([0.2, 0.6, 0.8, 1])], |
44
|
|
|
], |
45
|
|
|
) |
46
|
|
|
def test_no_max(self, arr, v_min, expected): |
47
|
|
|
got = util.normalize_array(arr=arr, v_min=v_min) |
48
|
|
|
assert is_equal_np(got, expected) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_remove_prefix_suffix(): |
52
|
|
|
""" |
53
|
|
|
Test remove_prefix_suffix by verifying outputs |
54
|
|
|
""" |
55
|
|
|
x = "sample000.nii" |
56
|
|
|
|
57
|
|
|
# single prefix, suffix |
58
|
|
|
got = util.remove_prefix_suffix(x=x, prefix="sample", suffix=".nii") |
59
|
|
|
expected = "000" |
60
|
|
|
assert got == expected |
61
|
|
|
|
62
|
|
|
# multiple prefixes, suffixes |
63
|
|
|
got = util.remove_prefix_suffix(x=x, prefix=["sample"], suffix=[".nii.gz", ".nii"]) |
64
|
|
|
expected = "000" |
65
|
|
|
assert got == expected |
66
|
|
|
|