This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_subsample.py
171 lines (145 loc) · 5.92 KB
/
test_subsample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# encoding: utf-8
__author__ = "Dimitrios Karkalousos"
import numpy as np
import pytest
from mridc.data.subsample import (
create_mask_for_mask_type,
EquispacedMaskFunc,
Gaussian1DMaskFunc,
Gaussian2DMaskFunc,
RandomMaskFunc,
)
@pytest.mark.parametrize(
"mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage",
[("random", [0.08, 0.04], [4, 8], RandomMaskFunc, np.array([1, 320, 320]), None, 0)],
)
def test_create_mask_for_random_type(
mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage
):
"""
Test that the function returns random 1D masks
Args:
mask_type: The type of mask to be created
center_fractions: The center fractions of the mask
accelerations: The accelerations of the mask
expected_mask_func: The expected mask function
x: The shape of the mask
seed: The seed of the mask
half_scan_percentage: The half scan percentage of the mask
Returns:
None
"""
mask_func = create_mask_for_mask_type(mask_type, center_fractions, accelerations)
mask, acc = mask_func(x, seed, half_scan_percentage)
mask = mask.squeeze(0).numpy()
if not isinstance(mask_func, expected_mask_func):
raise AssertionError
if not accelerations[0] <= mask_func.choose_acceleration()[1] <= accelerations[1]:
raise AssertionError
if mask.shape != (x[1], 1):
raise AssertionError
if mask.dtype != np.float32:
raise AssertionError
if not accelerations[0] <= acc <= accelerations[1]:
raise AssertionError
@pytest.mark.parametrize(
"mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage",
[("equispaced", [0.08, 0.04], [4, 8], EquispacedMaskFunc, np.array([1, 320, 320]), None, 0)],
)
def test_create_mask_for_equispaced_type(
mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage
):
"""
Test that the function returns equispaced 1D masks
Args:
mask_type: The type of mask to be created
center_fractions: The center fractions of the mask
accelerations: The accelerations of the mask
expected_mask_func: The expected mask function
x: The shape of the mask
seed: The seed of the mask
half_scan_percentage: The half scan percentage of the mask
Returns:
None
"""
mask_func = create_mask_for_mask_type(mask_type, center_fractions, accelerations)
mask, acc = mask_func(x, seed, half_scan_percentage)
mask = mask.squeeze(0).numpy()
if not isinstance(mask_func, expected_mask_func):
raise AssertionError
if not accelerations[0] <= mask_func.choose_acceleration()[1] <= accelerations[1]:
raise AssertionError
if mask.shape != (x[1], 1):
raise AssertionError
if mask.dtype != np.float32:
raise AssertionError
if not accelerations[0] <= acc <= accelerations[1]:
raise AssertionError
@pytest.mark.parametrize(
"mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage, scale",
[("gaussian1d", [0.7, 0.7], [4, 10], Gaussian1DMaskFunc, np.array([1, 320, 320, 1]), None, 0, 0.02)],
)
def test_create_mask_for_gaussian1d_type(
mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage, scale
):
"""
Test that the function returns gaussian 1D masks
Args:
mask_type: The type of mask to be created
center_fractions: The center fractions of the mask
accelerations: The accelerations of the mask
expected_mask_func: The expected mask function
x: The shape of the mask
seed: The seed of the mask
half_scan_percentage: The half scan percentage of the mask
scale: The scale of the mask
Returns:
None
"""
mask_func = create_mask_for_mask_type(mask_type, center_fractions, accelerations)
mask, acc = mask_func(x, seed, half_scan_percentage, scale)
mask = mask.squeeze(0).numpy()
if not isinstance(mask_func, expected_mask_func):
raise AssertionError
if not accelerations[0] <= mask_func.choose_acceleration()[1] <= accelerations[1]:
raise AssertionError
if mask.shape != (x[1], 1):
raise AssertionError
if mask.dtype != np.float32:
raise AssertionError
if not accelerations[0] <= acc <= accelerations[1]:
raise AssertionError
@pytest.mark.parametrize(
"mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage, scale",
[("gaussian2d", [0.7, 0.7], [4, 10], Gaussian2DMaskFunc, np.array([1, 320, 320, 1]), None, 0, 0.02)],
)
def test_create_mask_for_gaussian2d_type(
mask_type, center_fractions, accelerations, expected_mask_func, x, seed, half_scan_percentage, scale
):
"""
Test that the function returns gaussian 2D masks
Args:
mask_type: The type of mask to be created
center_fractions: The center fractions of the mask
accelerations: The accelerations of the mask
expected_mask_func: The expected mask function
x: The shape of the mask
seed: The seed of the mask
half_scan_percentage: The half scan percentage of the mask
scale: The scale of the mask
Returns:
None
"""
mask_func = create_mask_for_mask_type(mask_type, center_fractions, accelerations)
mask, acc = mask_func(x, seed, half_scan_percentage, scale)
mask = mask.squeeze(0).squeeze(-1).numpy()
if not isinstance(mask_func, expected_mask_func):
raise AssertionError
if not accelerations[0] <= mask_func.choose_acceleration()[1] <= accelerations[1]:
raise AssertionError
if mask.shape != tuple(x[1:-1]):
raise AssertionError
if mask.dtype != np.float32:
raise AssertionError
if not accelerations[0] <= acc <= accelerations[1]:
raise AssertionError