-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmpdenoise.py
227 lines (182 loc) · 7.78 KB
/
mpdenoise.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""A Python class containing an implimentation of MPPCA denoising.
By default we denoise using a 5x5x5 box kernel.
Inputs are a 4D image with dimentions (X x Y x Z x N)
Usage:
import mpdenoise as mp
imgdn, sigma, nparameters = mp.denoise(img, kernel='5,5,5')
LICENCE
Authors: Benjamin Ades-Aron ([email protected])
Copyright (c) 2016 New York University
Permission is hereby granted, free of charge, to any non-commercial entity
('Recipient') obtaining a copy of this software and associated
documentation files (the 'Software'), to the Software solely for
non-commercial research, including the rights to use, copy and modify the
Software, subject to the following conditions:
1. The above copyright notice and this permission notice shall be
included by Recipient in all copies or substantial portions of the
Software.
2. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIESOF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BELIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF ORIN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
3. In no event shall NYU be liable for direct, indirect, special,
incidental or consequential damages in connection with the Software.
Recipient will defend, indemnify and hold NYU harmless from any claims or
liability resulting from the use of the Software by recipient.
4. Neither anything contained herein nor the delivery of the Software to
recipient shall be deemed to grant the Recipient any right or licenses
under any patents or patent application owned by NYU.
5. The Software may only be used for non-commercial research and may not
be used for clinical care.
6. Any publication by Recipient of research involving the Software shall
cite the references listed below.
REFERENCES
Veraart, J.; Fieremans, E. & Novikov, D.S. Diffusion MRI noise mapping
using random matrix theory Magn. Res. Med., 2016, early view, doi:
10.1002/mrm.26059
"""
import os, sys
import numpy as np
import multiprocessing
from joblib import Parallel, delayed
from tqdm import tqdm
class MP(object):
def __init__(self, dwi, kernel=None):
self.dwi = dwi
sx, sy, sz, N = self.dwi.shape
if kernel is None:
kernel = np.array([[5,5,5]])
else:
kernel = np.array([np.fromstring(kernel, sep=',')])
kernel = kernel+np.mod(kernel,2)-1
self.kernel = kernel.astype(int)
k = self.kernel // 2
# Jelle's padding
# Nduplicates = (kernel[0,-1]-1)/2
# slicesA = np.arange(kernel[0,2], kernel[0,2]-Nduplicates, -1) - 1
# slicesB = np.arange(0, self.dwi.shape[2], 1)
# slicesC = self.dwi.shape[2] - slicesA - 1
# sliceselection = np.concatenate((slicesA, slicesB, slicesC)).astype(int)
# self.dwi_tmp = self.dwi[:,:,sliceselection,:]
# mask = np.ones((sx,sy,sz))
# self.mask = np.concatenate((np.zeros((sx, sy, len(slicesA))), mask, np.zeros((sx, sy, len(slicesC)))), axis=2)
# self.origsz = sz
pwidth = (k[0,0], k[0,0]), (k[0,1], k[0,1]), (k[0,2],k[0,2]), (0,0)
self.pwidth = pwidth
self.dwi_tmp = np.pad(self.dwi, pad_width=pwidth, mode='wrap')
def boxpatch(self, coords):
# extracts a patch of size kx x ky x kz from the padded input at specified coords
k = ((self.kernel-1)/2).astype(int)
kx = k[0,0]
ky = k[0,1]
kz = k[0,2]
X = self.dwi_tmp[coords[0]-kx:coords[0]+kx+1, coords[1]-ky:coords[1]+ky+1, coords[2]-kz:coords[2]+kz+1, :]
return X
def unpad(self, x, pad_width):
slices = []
for c in pad_width:
e = None if c[1] == 0 else -c[1]
slices.append(slice(c[0], e))
return x[tuple(slices)]
def sample(self, mask):
# outputs a grid x, y, z of which coordinates to loop over when processing
k = ((self.kernel-1)/2).astype(int)
kx = k[0,0];
ky = k[0,1];
kz = k[0,2];
sx, sy, sz = mask.shape
mask[:kx,:,:] = 0
mask[sx-kx:,:,:] = 0
mask[:,:ky,:] = 0
mask[:,sy-ky:,:] = 0
mask[:,:,:kz] = 0
mask[:,:,sz-kz:] = 0
self.mask = mask
x, y, z = np.where(mask==1)
return x.astype(int), y.astype(int), z.astype(int)
def denoise(self, coords, M, N, centering=0):
X = self.boxpatch(coords)
X = X.reshape((M, N))
R = np.min((M, N)).astype(int)
flip = False
if M < N:
flip = True
X = X.T
M = X.shape[0]
N = X.shape[1]
if centering:
colmean = np.mean(X, axis=0)
X = X - np.tile(colmean, (M, 1))
try:
u,vals,v = np.linalg.svd(X, full_matrices=False)
vals_orig = vals
vals = (vals**2)/N
csum = np.cumsum(vals[R-centering-1:None:-1])
sigmasq_1 = csum[R-centering-1:None:-1]/range(R-centering, 0, -1)
gamma = (M-np.array(range(0, R-centering)))/N
rangeMP = 4*np.sqrt(gamma[:])
rangeData = vals[0:R-centering]-vals[R-centering-1]
sigmasq_2 = rangeData/rangeMP
t = np.where(sigmasq_2 < sigmasq_1)
t = t[0][0]
sigma = np.sqrt(sigmasq_1[t])
vals[t:R] = 0
s = np.matrix(u) * np.diag(np.sqrt(N*vals)) * np.matrix(v)
if flip:
s = s.T
M = X.shape[1]
N = X.shape[0]
if centering:
s = s + np.tile(colmean, (M, 1))
signal = np.squeeze(s[M//2, :])
except:
sigma = np.nan
if flip:
X = X.T
M = X.shape[1]
N = X.shape[0]
signal = np.squeeze(X[M//2, :])
t = R
npars = t
return signal, sigma, npars
def process(self):
sx, sy, sz, N = self.dwi_tmp.shape
mask = np.ones((sx,sy,sz))
x, y, z = self.sample(mask)
xsize = int(x.size)
coords = np.vstack((x,y,z))
centering = 0
M = np.prod(self.kernel).astype(int)
#print('...denoising')
inputs = tqdm(range(0, xsize))
num_cores = multiprocessing.cpu_count()
# parallel
signal, sigma, npars = zip(*Parallel(n_jobs=num_cores, prefer='processes')\
(delayed(self.denoise)(coords[:,i], M, N, centering=centering) for i in inputs))
# serial
# for t in inputs:
# a, b, c = self.denoise(coords[:,t], M, N, centering=centering)
# reconstruct original data matrix
Sigma = np.zeros((sx, sy, sz))
Npars = np.zeros((sx, sy, sz))
Signal = np.zeros((sx, sy, sz, N))
for nn in range(0, xsize):
Sigma[x[nn], y[nn], z[nn]] = sigma[nn]
Npars[x[nn], y[nn], z[nn]] = npars[nn]
Signal[x[nn], y[nn], z[nn], :] = signal[nn]
# Signal = Signal[:,:, 2: 2+self.origsz, :]
# Sigma = Sigma[:,:, 2: 2+self.origsz]
# Npars = Npars[:,:, 2: 2+self.origsz]
Signal = self.unpad(Signal, self.pwidth)
Npars = self.unpad(Npars, self.pwidth[:][:-1])
Sigma = self.unpad(Sigma, self.pwidth[:][:-1])
return Signal, Sigma, Npars
def denoise(img, kernel='5,5,5'):
mp = MP(img, kernel)
Signal, Sigma, Npars = mp.process()
return Signal, Sigma, Npars
if __name__ == "__main__":
denoise()