def just_denoise(arr, k=3, level=5, noise_std=None, coefs=None, supp=None, min_nscales=2): if np.iterable(k): level = len(k) if coefs is None: coefs = atrous.decompose(arr, level) if noise_std is None: if arr.ndim > 2: noise_std = atrous.estimate_sigma_mad(coefs[0], True) else: noise_std = atrous.estimate_sigma(arr, coefs) ## calculate support taking only positive coefficients (light sources) if supp is None: supp = atrous.get_support(coefs, np.array(k, _dtype_) * noise_std, modulus=False) structures = get_structures(coefs, supp) g = connectivity_graph(structures, min_nscales) #labels = reduce(lambda a,b:a+b, (n.labels for n in lib.flatten(g))) new_supp = supp_from_connectivity(g, level) return atrous.rec_with_support(coefs, new_supp)
def conj_grad_rec(obj, coefs, thresh=0.1, verbose=True): adjoint = _atilda1 N = len(coefs) - 1 supp = [x for x in supp_from_obj(obj)] while len(supp) < len(coefs): supp.append(np.zeros(coefs[0].shape)) WW = _project(coefs, supp) F0 = atrous.rec_atrous(WW) x = atrous.decompose(F0, N) AF0 = _project(x, supp) Wr = [c1 - c2 for c1, c2 in zip(WW, AF0)] Fr0 = adjoint(Wr) AF, Fp, Fr, niter = AF0, F0, Fr0, 0 conv = [] meas_prev = -1e-5 while niter < 1000: Fr_norm = np.sum(Fr**2) AF_norm = np.sum([x**2 for x in AF[:-1]]) alpha = Fr_norm / AF_norm _Fn = Fp + alpha * Fr Fnext = _Fn * (_Fn >= 0) AF = _project(atrous.decompose(Fnext, N), supp) Wrn = [c1 - c2 for c1, c2 in zip(WW, AF)] meas_next = np.sum([c**2 for c in Wrn[:-1]]) if niter > 0: meas = 1 - meas_next / meas_prev else: meas = 1 conv.append(meas) if verbose: print meas, meas_next if meas < thresh or meas_next < thresh: return Fnext, conv meas_prev = meas_next Frnext = adjoint(Wrn) beta = np.sum(Frnext**2) / Fr_norm Frnext = Frnext + beta * Fr Fr, Wr, Fp = Frnext, Wrn, Fnext niter += 1 return Fnext