def test_09(self): fg, ax = plot.imview(self.z, title='Imview Test', fltscl=True, cbar=None) ax.format_coord(0, 0) plot.close(fg)
def test_11(self): fig = plot.figure() plot.imview((100.0 * self.z).astype(np.uint16), title='Imview Test', fltscl=True, fgrf=fig) plot.close()
def test_06(self): x = np.linspace(-1, 1, 20)[np.newaxis, :] y = np.linspace(-1, 1, 20)[:, np.newaxis] z = np.sqrt(x**2 + y**2) fig = plot.figure() plot.imview(z, title='Imview Test', fgrf=fig) plot.close()
def test_03(self): x = np.linspace(-1, 1, 20)[np.newaxis, :] y = np.linspace(-1, 1, 20)[:, np.newaxis] z = np.sqrt(x**2 + y**2) fig = plot.figure() plot.surf(z, title='Surf Test', xlbl='x', ylbl='y', zlbl='z') plot.close()
def saveXimg(cri, Xr, filename): # print(Xr.shape) X = np.sum(abs(Xr), axis=cri.axisM).squeeze() fig = plot.figure(figsize=(7, 7)) plot.imview(X, cmap=plot.cm.Blues, fig=fig) fig.savefig(filename) plot.close() mplot.close()
def test_07(self): x = np.linspace(-1, 1, 20)[np.newaxis, :] y = np.linspace(-1, 1, 20)[:, np.newaxis] z = np.sqrt(x**2 + y**2) z3 = np.dstack((z, 2 * z, 3 * z)) fig = plot.figure() plot.imview(z3, title='Imview Test', fgrf=fig) plot.close()
def saveXhist(Xr, filename): Xr_ = abs(Xr.flatten()) fig = plot.figure(figsize=(7 * 10, 7)) ax = fig.add_subplot(1, 1, 1) ax.hist(Xr_, bins=500, density=True) fig.savefig(filename) plot.close() mplot.close()
def test_06(self): fig = plot.figure() plot.contour(self.z, title='Contour Test', xlbl='x', ylbl='y', fgrf=fig) plot.close()
def test_05(self): plot.contour(self.z, x=self.x, y=self.y, title='Contour Test', xlbl='x', ylbl='y') plot.close()
def test_04(self): fig = plot.figure() plot.surf(self.z, x=self.x, y=self.y, title='Surf Test', xlbl='x', ylbl='y', zlbl='z', fgrf=fig) plot.close()
def test_03(self): fig, ax = plot.subplots(nrows=1, ncols=1) plot.surf(self.z, title='Surf Test', xlbl='x', ylbl='y', zlbl='z', elev=0.0, fgrf=fig, axrf=ax) plot.close()
def saveimg2D(imgs, filename, titles=None): if imgs.ndim == 3: imgs = np.array([imgs]) if titles is not None and titles.ndim == 3: titles = np.array([titles]) R = imgs.shape[0] C = imgs.shape[1] fig = plot.figure(figsize=(7 * C, 7 * R)) for r in range(R): for c in range(C): ax = fig.add_subplot(R, C, r * C + c + 1) s = None if titles is not None: s = titles[r][c] plot.imview(imgs[r][c], title=s, fig=fig, ax=ax) plot.savefig(filename) plot.close() mplot.close()
def test_02(self): x = np.linspace(-1, 1, 20) y = x**2 fig = plot.figure() plot.plot(y, x=x, title='Plot Test', xlbl='x', ylbl='y', fgrf=fig) plot.close()
def test_01(self): x = np.linspace(-1, 1, 20) y = x**2 plot.plot(y, title='Plot Test', xlbl='x', ylbl='y', lgnd=('Legend')) plot.close()
def test_12(self): z3 = np.dstack((self.z, 2 * self.z, 3 * self.z)) fig = plot.figure() plot.imview(z3, title='Imview Test', fgrf=fig) plot.close()
def nakashizuka_solve( cri, Dr0, Xr, Sr, final_sigma, maxitr = 40, param_mu = 1, param_lambda = 1e-2, debug_dir = None ): param_rho = 0.5 Xr = Xr.copy() Sr = Sr.copy() Dr = Dr0.copy() Hr = np.zeros_like(cnvrep.zpad(Dr, cri.Nv)) Sf = to_frequency(cri, Sr) # sigma set # sigma_list = [] # sigma_list.append(Xr.max()*4) # for i in range(7): # sigma_list.append(sigma_list[i]*0.5) first_sigma = Xr.max()*4 c = (final_sigma / first_sigma) ** (1/(maxitr - 1)) print("c = %.8f" % c) sigma_list = [] sigma_list.append(first_sigma) for i in range(maxitr - 1): sigma_list.append(sigma_list[i]*c) crop_op = [] for l in Dr.shape: crop_op.append(slice(0, l)) crop_op = tuple(crop_op) Pcn = cnvrep.getPcn(Dr.shape, cri.Nv, cri.dimN, cri.dimCd, zm=False) updcnt = 0 dictcnt = 0 for sigma in sigma_list: print("sigma = %.8f" % sigma) # Xf_old = sl.rfftn(Xr, cri.Nv, cri.axisN) for l in range(1): # print("l0norm: %f" % l0norm(Xr, sigma_list[-1])) # print('error1: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) # print("l2(Xr): %.6f, l2(delta): %.6f" % (l2norm(Xr), l2norm(delta))) delta = Xr * np.exp(-(Xr*Xr) / (2*sigma*sigma)) Xr = Xr - param_mu*delta# + np.random.randn(*Xr.shape)*sigma*1e-1 Xf = to_frequency(cri, Xr) # print('error2: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) Df = to_frequency(cri, Dr) b = Xf / param_lambda + np.conj(Df) * Sf Xf = sl.solvedbi_sm(Df, 1/param_lambda, b, axis=cri.axisM) Xr = to_spatial(cri, Xf).astype(np.float32) # print('error3: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) # save_reconstructed(cri, Dr, Xr, Sr, "./rec/%da.png" % reccnt) # saveXhist(Xr, "./hist/%da.png" % reccnt) Dr, Hr = update_dict(cri, Pcn, crop_op, Xr, Dr, Hr, Sf, param_rho) Df = to_frequency(cri, Dr) # print('error4: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) # # project X to solution space # b = sl.inner(Df, Xf, axis=cri.axisM) - Sf # c = sl.inner(Df, np.conj(Df), axis=cri.axisM) # Xf = Xf - np.conj(Df) / c * b # Xr = sl.irfftn(Xf, s=cri.Nv, axes=cri.axisN) # print('error5: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) if debug_dir is not None: saveimg(util.tiledict(Dr.squeeze()), debug_dir + "/dict/%d.png" % updcnt) updcnt += 1 # saveXhist(Xr, "Xhist_sigma=" + str(sigma) + ".png") # print("l0 norm of final X: %d" % smoothedl0norm(Xr, 0.00001)) plot.close() mplot.close() return Dr
def mysolve( cri, Dr0, Xr, Sr, final_sigma, maxitr = 40, param_mu = 1, debug_dir = None ): Dr = Dr0.copy() Xr = Xr.copy() Sr = Sr.copy() Df = sl.rfftn(Dr, s=cri.Nv, axes=cri.axisN) Sf = sl.rfftn(Sr, s=cri.Nv, axes=cri.axisN) Xf = sl.rfftn(Xr, s=cri.Nv, axes=cri.axisN) alpha = 1e0 # sigma set first_sigma = Xr.max()*4 c = (final_sigma / first_sigma) ** (1/(maxitr - 1)) print("c = %.8f" % c) sigma_list = [] sigma_list.append(first_sigma) for i in range(maxitr - 1): sigma_list.append(sigma_list[i]*c) print(sigma_list[-1]) crop_op = [] for l in Dr.shape: crop_op.append(slice(0, l)) crop_op = tuple(crop_op) Pcn = cnvrep.getPcn(Dr.shape, cri.Nv, cri.dimN, cri.dimCd, zm=False) updcnt = 0 for sigma in sigma_list: print("sigma = %.8f" % sigma) # Xf_old = sl.rfftn(Xr, cri.Nv, cri.axisN) # print("l0norm: %f" % l0norm(Xr, sigma_list[-1])) # print('error1: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) delta = Xr * np.exp(-(Xr*Xr) / (2*sigma*sigma)) # print("l2(Xr): %.6f, l2(delta): %.6f" % (l2norm(Xr), l2norm(delta))) Xr = Xr - param_mu*delta# + np.random.randn(*Xr.shape)*sigma*1e-1 Xf = sl.rfftn(Xr, cri.Nv, cri.axisN) # saveXhist(Xr, "./hist/%db.png" % reccnt) # print('error2: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) # if debug_dir is not None: # save_reconstructed(cri, Dr, Xr, Sr, debug_dir + '/%drecA.png' % updcnt) # DXf = sl.inner(Df, Xf, axis=cri.axisM) # gamma = (np.sum(np.conj(DXf) * Sf, axis=cri.axisN, keepdims=True) + np.sum(DXf * np.conj(Sf), axis=cri.axisN, keepdims=True)) / 2 / np.sum(np.conj(DXf) * DXf, axis=cri.axisN, keepdims=True) # print(gamma) # print(gamma.shape, ' * ', Xr.shape) # gamma = np.real(gamma) # Xr = Xr * gamma # Xf = to_frequency(cri, Xr) # if debug_dir is not None: # save_reconstructed(cri, Dr, Xr, Sr, debug_dir + '/%drecB.png' % updcnt) # print('error3: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) # print("max: ", np.max(Xr)) B = sl.inner(Xf, Df, axis=cri.axisM) - Sf derivDf = sl.inner(np.conj(Xf), B, axis=cri.axisK) # derivDr = sl.irfftn(derivDf, s=cri.Nv, axes=cri.axisN)[crop_op] def func(alpha): Df_ = Df - alpha * derivDf Dr_ = sl.irfftn(Df_, s=cri.Nv, axes=cri.axisN)[crop_op] Df_ = sl.rfftn(Dr_, s=cri.Nv, axes=cri.axisN) Sf_ = sl.inner(Df_, Xf, axis=cri.axisM) return l2norm(Sr - sl.irfftn(Sf_, s=cri.Nv, axes=cri.axisN)) choice = np.array([func(alpha / 2), func(alpha), func(alpha * 2)]).argmin() alpha *= [0.5, 1, 2][choice] print("alpha: ", alpha) Df = Df - alpha * derivDf Dr = Pcn(sl.irfftn(Df, s=cri.Nv, axes=cri.axisN))[crop_op] # print(l2norm(Dr.T[0])) # Dr = normalize(Dr, axis=cri.axisN) print(l2norm(Dr.T[0])) Df = sl.rfftn(Dr, s=cri.Nv, axes=cri.axisN) if debug_dir is not None: saveimg(util.tiledict(Dr.squeeze()), debug_dir + "/dict/%d.png" % updcnt) # if debug_dir is not None: # save_reconstructed(cri, Dr, Xr, Sr, debug_dir + '/%drecC.png' % updcnt) # dictcnt += 1 # print('error4: ', l2norm(Sr - reconstruct(cri, Dr, Xr))) # save_reconstructed(cri, Dr, Xr, Sr, debug_dir + "/rec/%dc.png" % updcnt) # project X to solution space b = sl.inner(Df, Xf, axis=cri.axisM) - Sf c = sl.inner(Df, np.conj(Df), axis=cri.axisM) Xf = Xf - np.conj(Df) / c * b Xr = sl.irfftn(Xf, s=cri.Nv, axes=cri.axisN) # save_reconstructed(cri, Dr, Xr, Sr, debug_dir + "rec/%dd.png" % updcnt) # saveXhist(Xr, debug_dir + "hist/%db.png" % updcnt) updcnt += 1 # print("l0 norm of final X: %d" % smoothedl0norm(Xr, 0.00001)) plot.close() mplot.close() return Dr
def test_08(self): fig = plot.figure() plot.imview(self.z, title='Imview Test', fltscl=True, fgrf=fig) plot.close()
def test_07(self): plot.imview(self.z.astype(np.float16), title='Imview Test', cbar=True) plot.close()
def test_05(self): x = np.linspace(-1, 1, 20)[np.newaxis, :] y = np.linspace(-1, 1, 20)[:, np.newaxis] z = np.sqrt(x**2 + y**2) plot.imview(np.asarray(z, np.float16), title='Imview Test', cbar=True) plot.close()
def test_03(self): fig = plot.figure() plot.surf(self.z, title='Surf Test', xlbl='x', ylbl='y', zlbl='z') plot.close()
def saveimg(img, filename, title=None): fig = plot.figure(figsize=(7, 7)) plot.imview(img, fig=fig) fig.savefig(filename) plot.close() mplot.close()