-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
207 lines (139 loc) · 4.42 KB
/
utils.py
File metadata and controls
207 lines (139 loc) · 4.42 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import numpy as np
def uniform_selection(input_data, input_mask, rho=0.2, small_acs_block=(4, 4), seed=None):
np.random.seed(seed)
nrow, ncol = input_data.shape[0], input_data.shape[1]
center_kx = int(find_center_ind(input_data, axes=(1, 2)))
center_ky = int(find_center_ind(input_data, axes=(0, 2)))
temp_mask = np.copy(input_mask)
temp_mask[center_kx - small_acs_block[0] // 2: center_kx + small_acs_block[0] // 2,
center_ky - small_acs_block[1] // 2: center_ky + small_acs_block[1] // 2] = 0
pr = np.ndarray.flatten(temp_mask)
ind = np.random.choice(np.arange(nrow * ncol),
size=np.int(np.count_nonzero(pr) * rho), replace=False, p=pr / np.sum(pr))
[ind_x, ind_y] = index_flatten2nd(ind, (nrow, ncol))
loss_mask = np.zeros_like(input_mask)
loss_mask[ind_x, ind_y] = 1
trn_mask = input_mask - loss_mask
return trn_mask, loss_mask
def getPSNR(ref, recon):
"""
Measures PSNR between the reference and the reconstructed images
"""
mse = np.sum(np.square(np.abs(ref - recon))) / ref.size
psnr = 20 * np.log10(np.abs(ref.max()) / (np.sqrt(mse) + 1e-10))
return psnr
def fft(ispace, axes=(0, 1), norm=None, unitary_opt=True):
"""
Parameters
----------
ispace : coil images of size nrow x ncol x ncoil.
axes : The default is (0, 1).
norm : The default is None.
unitary_opt : The default is True.
Returns
-------
transform image space to k-space.
"""
kspace = np.fft.fftshift(np.fft.fftn(np.fft.ifftshift(ispace, axes=axes), axes=axes, norm=norm), axes=axes)
if unitary_opt:
fact = 1
for axis in axes:
fact = fact * kspace.shape[axis]
kspace = kspace / np.sqrt(fact)
return kspace
def ifft(kspace, axes=(0, 1), norm=None, unitary_opt=True):
"""
Parameters
----------
ispace : image space of size nrow x ncol x ncoil.
axes : The default is (0, 1).
norm : The default is None.
unitary_opt : The default is True.
Returns
-------
transform k-space to image space.
"""
ispace = np.fft.ifftshift(np.fft.ifftn(np.fft.fftshift(kspace, axes=axes), axes=axes, norm=norm), axes=axes)
if unitary_opt:
fact = 1
for axis in axes:
fact = fact * ispace.shape[axis]
ispace = ispace * np.sqrt(fact)
return ispace
def norm(tensor, axes=(0, 1, 2), keepdims=True):
"""
Parameters
----------
tensor : It can be in image space or k-space.
axes : The default is (0, 1, 2).
keepdims : The default is True.
Returns
-------
tensor : applies l2-norm .
"""
for axis in axes:
tensor = np.linalg.norm(tensor, axis=axis, keepdims=True)
if not keepdims: return tensor.squeeze()
return tensor
def find_center_ind(kspace, axes=(1, 2, 3)):
"""
Parameters
----------
kspace : nrow x ncol x ncoil.
axes : The default is (1, 2, 3).
Returns
-------
the center of the k-space
"""
center_locs = norm(kspace, axes=axes).squeeze()
return np.argsort(center_locs)[-1:]
def index_flatten2nd(ind, shape):
"""
Parameters
----------
ind : 1D vector containing chosen locations.
shape : shape of the matrix/tensor for mapping ind.
Returns
-------
list of >=2D indices containing non-zero locations
"""
array = np.zeros(np.prod(shape))
array[ind] = 1
ind_nd = np.nonzero(np.reshape(array, shape))
return [list(ind_nd_ii) for ind_nd_ii in ind_nd]
def sense1(input_kspace, sens_maps, axes=(0, 1)):
"""
Parameters
----------
input_kspace : nrow x ncol x ncoil
sens_maps : nrow x ncol x ncoil
axes : The default is (0,1).
Returns
-------
sense1 image
"""
image_space = ifft(input_kspace, axes=axes, norm=None, unitary_opt=True)
Eh_op = np.conj(sens_maps) * image_space
sense1_image = np.sum(Eh_op, axis=axes[-1] + 1)
return sense1_image
def complex2real(input_data):
"""
Parameters
----------
input_data : row x col
dtype :The default is np.float32.
Returns
-------
output : row x col x 2
"""
return np.stack((input_data.real, input_data.imag), axis=-1)
def real2complex(input_data):
"""
Parameters
----------
input_data : row x col x 2
Returns
-------
output : row x col
"""
return input_data[..., 0] + 1j * input_data[..., 1]