Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_precond_LinearLeastSquares(self):
n = 5
_A = np.eye(n) + 0.01 * util.randn([n, n])
A = linop.MatMul([n, 1], _A)
x = util.randn([n, 1])
y = A(x)
x_lstsq = np.linalg.lstsq(_A, y, rcond=-1)[0]
p = 1 / (np.sum(abs(_A)**2, axis=0).reshape([n, 1]))
P = linop.Multiply([n, 1], p)
x_rec = app.LinearLeastSquares(A, y, show_pbar=False).run()
npt.assert_allclose(x_rec, x_lstsq, atol=1e-3)
alpha = 1 / app.MaxEig(P * A.H * A, show_pbar=False).run()
x_rec = app.LinearLeastSquares(
A,
y,
solver='GradientMethod',
alpha=alpha,
max_power_iter=100,
max_iter=1000, show_pbar=False).run()
npt.assert_allclose(x_rec, x_lstsq, atol=1e-3)
tau = p
x_rec = app.LinearLeastSquares(
A,
Nt = coord.shape[0]
device = backend.get_device(target)
xp = device.xp
with device:
pulses = xp.zeros((Nc, Nt), xp.complex)
# set up the system matrix
if explicit:
A = rf.linop.PtxSpatialExplicit(sens, coord, dt,
target.shape, b0)
else:
A = sp.mri.linop.Sense(sens, coord, weights=None, tseg=tseg,
ishape=target.shape).H
# handle the Ns * Ns error weighting ROI matrix
W = sp.linop.Multiply(A.oshape, xp.ones(target.shape))
if roi is not None:
W = sp.linop.Multiply(A.oshape, roi)
# apply ROI
A = W * A
# Unconstrained, use conjugate gradient
if st is None:
I = sp.linop.Identity((Nc, coord.shape[0]))
b = A.H * W * target
alg_method = sp.alg.ConjugateGradient(A.H * A + alpha * I,
b, pulses, P=None,
max_iter=max_iter, tol=tol)
# Constrained case, use SDMM
if self.G is None:
proxfc = prox.L2Reg(y.shape, 1, y=y)
gamma_dual = 1
else:
A = linop.Vstack([A, self.G])
proxf1c = prox.L2Reg(self.y.shape, 1, y=y)
proxf2c = prox.Conj(self.proxg)
proxfc = prox.Stack([proxf1c, proxf2c])
proxg = prox.NoOp(self.x.shape)
gamma_dual = 0
if self.tau is None:
if self.sigma is None:
self.sigma = 1
S = linop.Multiply(A.oshape, self.sigma)
AHA = A.H * S * A
max_eig = MaxEig(
AHA,
dtype=self.x.dtype,
device=self.x_device,
max_iter=self.max_power_iter,
show_pbar=self.show_pbar).run()
self.tau = 1 / (max_eig + self.lamda + self.mu)
else:
T = linop.Multiply(A.ishape, self.tau)
AAH = A * T * A.H
max_eig = MaxEig(
AAH,
dtype=self.x.dtype,
A = A * C
return A
# Create Sense linear operator
S = sp.linop.Multiply(ishape, mps)
if coord is None:
F = sp.linop.FFT(S.oshape, axes=range(-img_ndim, 0))
else:
F = sp.linop.NUFFT(S.oshape, coord)
A = F * S
if weights is not None:
with sp.get_device(weights):
P = sp.linop.Multiply(F.oshape, weights**0.5)
A = P * A
if comm is not None:
C = sp.linop.AllReduceAdjoint(ishape, comm, in_place=True)
A = A * C
A.repr_str = 'Sense'
return A
def __mul__(self, input):
if isinstance(input, Linop):
return Compose([self, input])
elif np.isscalar(input):
M = Multiply(self.ishape, input)
return Compose([self, M])
elif isinstance(input, backend.get_array_module(input).ndarray):
return self.apply(input)
return NotImplemented
R = sp.linop.Reshape((num_coils, 1) + tuple(mps_ker_shape[1:]),
mps_ker_shape)
C = sp.linop.ConvolveFilter(R.oshape, img_ker,
mode='valid', multi_channel=True)
A = C * R
if coord is not None:
num_coils = mps_ker_shape[0]
grd_shape = [num_coils] + sp.estimate_shape(coord)
iF = sp.linop.IFFT(grd_shape, axes=range(-ndim, 0))
N = sp.linop.NUFFT(grd_shape, coord)
A = N * iF * A
if weights is not None:
with sp.get_device(weights):
P = sp.linop.Multiply(A.oshape, weights**0.5)
A = P * A
return A
def Amult(self, x, A, mu, lam):
M = sp.linop.Multiply(x.shape, np.ones(x.shape) * np.sqrt(1 / mu))
L = sp.linop.Multiply(x.shape, np.ones(x.shape) * np.sqrt(lam))
Y = sp.linop.Vstack((A, M, L))
Ry = sp.linop.Reshape((Y.oshape[0], 1), Y.oshape)
Y = Ry * Y
return Y
if self.tau is None:
if self.sigma is None:
self.sigma = 1
S = linop.Multiply(A.oshape, self.sigma)
AHA = A.H * S * A
max_eig = MaxEig(
AHA,
dtype=self.x.dtype,
device=self.x_device,
max_iter=self.max_power_iter,
show_pbar=self.show_pbar).run()
self.tau = 1 / (max_eig + self.lamda + self.mu)
else:
T = linop.Multiply(A.ishape, self.tau)
AAH = A * T * A.H
max_eig = MaxEig(
AAH,
dtype=self.x.dtype,
device=self.x_device,
max_iter=self.max_power_iter,
show_pbar=self.show_pbar).run()
self.sigma = 1 / max_eig
with self.y_device:
u = self.y_device.xp.zeros(A.oshape, dtype=self.y.dtype)
self.alg = PrimalDualHybridGradient(
proxfc,