def nlbayes2(im, t, sigma, knn='sklearn', flat=True, borde=8, oracle=np.zeros((1, ))): '''Restore image 'im' using NLBayes with noise sigma and patch size txt If oracle is provided (in second step), it's used for build the gaussian model''' im = aumentar_imagen(im, borde) # Parameters: N1, N2 = im.shape # Dimensions of im # List of patches of im: listpat = gen_listpat2(im, t) if not oracle.any(): # No oracle provided k = 90 # Fijo para comparar con otros metodos use_oracle = False listsearch = listpat else: # An oracle is provided k = 90 # Fijo para comparar con otros metodos use_oracle = True oracle = aumentar_imagen(oracle, borde) listsearch = gen_listpat2(oracle, t) # Restore image: imres = np.zeros([N1, N2]) for i in xrange(N1 - t + 1): for j in xrange(N2 - t + 1): p = listpat[i, j, :] vecinos = find_neighbors2([i, j], listsearch, t, k, knn) if not use_oracle: # No provided oracle pres = restore_patch_nlbayes1(p, t, sigma, vecinos, flat) else: # If oracle is provided, it's used for the gaussian patch model: pres = restore_patch_nlbayes2(p, t, sigma, vecinos, flat) imres[i:i + t, j:j + t] += np.reshape(pres, (t, t)) # Aggregation # Normalizing aggregation u = conc(([range(1, t) ], t * np.ones([1, N1 - 2 * t + 2]), [range(t - 1, 0, -1)]), 1) v = conc(([range(1, t) ], t * np.ones([1, N2 - 2 * t + 2]), [range(t - 1, 0, -1)]), 1) mask = u.T * v # 'mask' contains number of estimations of each pixel imres = np.divide(imres, mask) if borde: imres = recortar_imagen(imres, borde) return imres
def find_neighbors3(indpat, listsearch, listpatches, t, k, tau=0): '''Find the k nearest neighbors of the patch with indices indpat between the rows of listpat. It uses weighted band coefficients in wnlbayes2 (if provided)''' n = 4 * t**2 # Lenght of the patches in wavelet domain (n = 4xtxt) N1, N2 = listpatches.shape[0:2] Nt = 7 sW = Nt * t # The size of the LOCAL search window i, j = indpat pat = listsearch[i, j, :, 0] # Top-left index of search window: if i < N1 - (Nt + 1) / 2 * t: fil = max(0, i - (Nt - 1) / 2 * t) # Beginning of search window else: fil = N1 - Nt * t if j < N2 - (Nt + 1) / 2 * t: col = max(0, j - (Nt - 1) / 2 * t) # Beginning of search window else: col = N2 - Nt * t searchlist = conc([ listsearch[fil:fil + (Nt - 1) * t + 1, col:col + (Nt - 1) * t + 1, :, dim].reshape(((sW - t + 1)**2, n)) for dim in xrange(4) ]) # kNN search: Using scikit-learn kNN nbrs = NearestNeighbors(n_neighbors=k, algorithm='brute').fit(searchlist) distances, indices = nbrs.kneighbors(pat) indices = indices[0] distances = distances[0] if tau: # Second step: keep variable number of neighbors kmin = 60 # Minimum number of neighbors to be used distances /= n aux = (distances < tau) # Keep the neighbors closer to tau if aux.sum() > kmin: indices = indices[aux] else: indices = indices[:kmin] listpat = conc([ listpatches[fil:fil + (Nt - 1) * t + 1, col:col + (Nt - 1) * t + 1, :, dim].reshape(((sW - t + 1)**2, n)) for dim in xrange(4) ]) return listpat[indices, :]
def wav_analysis(im, wavelet, niv): '''Analisis de la imagen im en niv niveles size(im) debe ser multiplo de 2**niv en cada dimension''' n, m = im.shape W = im.copy() for i in range(niv): LL, (HL, LH, HH) = pywt.dwt2(W[:n, :m], wavelet, mode='per') #size(LL), size(HL), size(LH), size(HH) W[:n, :m] = conc((conc((LL, HL), 1), conc((LH, HH), 1))) n /= 2 m /= 2 return W
def gen_listpat_3D2(Wblock, t): ''' Genera bloque de patches (como filas de listpat3D) de la imagen W Wblock contiene W, Wh, Wv, Wd apiladas (en 3ra dimension) W son los coef wavelets originales Wh, Wv, Wd son los coef de las traslaciones horizontal, vertical y diagonal respectivamente Filas de listpat3D(:,:,i) son los patches de la traslacion i i=1 -> Imagen original, i=2 -> Wh, i=3 -> Wv, i=4 -> Wd Se usa en: WNLBayes2''' N1, N2 = Wblock[:, :, 0].shape # Shape of the image N1 /= 2 N2 /= 2 # Size of each band ncol = N1 - t + 1 # Cantidad de patches en cada columna de c/banda nfil = N2 - t + 1 # Cantidad de patches en cada fila de c/banda listpat3D = np.zeros([ncol, nfil, 4 * t**2, 4]) # Bloque de patches (filas) for dim in xrange(4): Wx = Wblock[:, :, dim] # Traslacion x in {0, h, v, d} for i in xrange(ncol): for j in xrange(nfil): pat0 = np.reshape(Wx[i:i + t, j:j + t], t**2) # Patch del resumen patH = np.reshape(Wx[i:i + t, N2 + j:N2 + j + t], t**2) # Patch de la banda H patV = np.reshape(Wx[N1 + i:N1 + i + t, j:j + t], t**2) # Patch de la banda V patD = np.reshape(Wx[N1 + i:N1 + i + t, N2 + j:N2 + j + t], t**2) # Patch de la banda D listpat3D[i, j, :, dim] = conc((pat0, patH, patV, patD)) return listpat3D
def traslaciones_wav(W, n): '''Coeficientes de las 3 traslaciones (de n pixeles c/u) W tiene que tener solo 1 nivel Wh: traslacion horizontal Wv: traslacion vertical Wd: traslacion diagonal''' #% Reconstruimos imagen (1 nivel): imagen = wav_synthesis(W, 'db8', 1) # Trasladamos imagen espacial: imH = pad(imagen, ((0, 0), (n, 0)), mode='reflect')[:, :-n] imV = pad(imagen, ((n, 0), (0, 0)), mode='reflect')[:-n, :] imD = pad(imagen, ((n, 0), (n, 0)), mode='reflect')[:-n, :-n] # Coeficientes de las traslaciones: Wh = wav_analysis(imH, 'db8', 1) Wv = wav_analysis(imV, 'db8', 1) Wd = wav_analysis(imD, 'db8', 1) Wblock = np.zeros(conc((W.shape, (4, )))) Wblock[:, :, 0] = W Wblock[:, :, 1] = Wh Wblock[:, :, 2] = Wv Wblock[:, :, 3] = Wd return Wblock
def mk_SwitchSpline(level1,switch1,delay1,level2,switch2=None,delay2=None,level3=None,maxT = 100, debug = False, smord = 3, smooth = 0): ''' General two switch rate defined by spline interpolation of 5 points in the t-rate(t) plane given by: (0,level1),(switch1,level1),(switch1+delay1,level2),(switch2,level2),(switch2+delay2,level3) Caution with too short delays (boxes), induces spline oscillations at the borders! Returns a spline function. ''' # only one switch if switch2 == None: # if delay is too small, then do first order spline if delay1 < 0.15: smord = 1 smooth = 0.5 # raise Exception('delay smaller than 0.15!') if switch1 > maxT: yvec = level1*ones(100) xvec = linspace(0, maxT, 100) elif switch1 + delay1 > maxT: n1 = 100 yvec = conc( [level1*ones(100),level2*ones(n1)] ) xvec = conc( [linspace(0,switch1,100),linspace(switch1+delay1, switch1+delay1+10,n1)] ) else: n1 = (maxT-switch1)/(0.1*delay1) n1 = n1 if n1>100 else 100 yvec = conc( [level1*ones(100),level2*ones(n1)] ) xvec = conc( [linspace(0,switch1,100),linspace(switch1+delay1,maxT,n1)] ) else: if delay1 < 0.15 or delay2 < 0.15: smord = 1 smooth = 0.5 # raise Exception('delay smaller than 0.15!') if maxT < switch2: print switch1,delay1,switch2 raise Exception('maxT smaller than 2nd switching time!') if switch1 + delay1 > switch2: print 'switch1', switch1, 'delay1', delay1, 'switch2', switch2 raise Exception('First switch plus delay bigger than 2nd switch!') # adjust interpolation point by given delays, minimum 100 points! n1 = (switch2-switch1)/(0.1*delay1) n1 = n1 if n1>100 else 100 n2 = (maxT-switch2)/(0.1*delay2) n2 = n2 if n2>100 else 100 yvec = conc( [level1*ones(100),level2*ones(n1),level3*ones(n2)] ) xvec = conc( [linspace(0,switch1,100),linspace(switch1+delay1,switch2,n1),linspace(switch2+delay2,maxT,n2)] ) s = spline(xvec,yvec,k = smord, s = smooth) if debug: return s,xvec,yvec # to check the xvec and yvec generated from the parameters return s
def get_aoneo(self): """Packs the data into one array for a later transfer to the library """ import numpy as np from numpy import require, float64, concatenate as conc nr = self.nr nsp = self.nspecies nmt = sum(self.sp2nmult) nrt = nr*nmt nms = nmt+nsp nsvn = 200 + 2*nr + 4*nsp + 2*nmt + nrt + nms svn = require(np.zeros(nsvn), dtype=float64, requirements='CW') # Simple parameters i = 0 svn[i] = nsp; i+=1; svn[i] = nr; i+=1; svn[i] = self.rmin; i+=1; svn[i] = self.rmax; i+=1; svn[i] = self.kmax; i+=1; svn[i] = self.jmx; i+=1; svn[i] = conc(self.psi_log).sum(); i+=1; # Pointers to data i = 99 s = 199 svn[i] = s+1; i+=1; f=s+nr; svn[s:f] = self.rr; s=f; # pointer to rr svn[i] = s+1; i+=1; f=s+nr; svn[s:f] = self.pp; s=f; # pointer to pp svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2nmult; s=f; # pointer to sp2nmult svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2rcut; s=f; # pointer to sp2rcut svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2norbs; s=f; # pointer to sp2norbs svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2charge; s=f; # pointer to sp2charge svn[i] = s+1; i+=1; f=s+nmt; svn[s:f] = conc(self.sp_mu2j); s=f; # pointer to sp_mu2j svn[i] = s+1; i+=1; f=s+nmt; svn[s:f] = conc(self.sp_mu2rcut); s=f; # pointer to sp_mu2rcut svn[i] = s+1; i+=1; f=s+nrt; svn[s:f] = conc(self.psi_log).reshape(nrt); s=f; # pointer to psi_log svn[i] = s+1; i+=1; f=s+nms; svn[s:f] = conc(self.sp_mu2s); s=f; # pointer to sp_mu2s svn[i] = s+1; # this is a terminator to simple operation return svn
def get_result(source): # data format: # 1-3: real1Sup 1-3 col # 4-6: real1Sup 4-6 col # 7-9: real1DP 1-3 col # 10-12: real1DP 4-6 col # 13-15: real2Sup 1-3 col # 16-18: real2Sup 4-6 col # ... # 93-95: apr2DP 1-3 col # 94-96: apr2DP 4-6 col for i in np.arange(paralelEX): real1Sup[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[0], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[1]), axis=1) real1DP[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[2], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[3]), axis=1) real2Sup[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[4], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[5]), axis=1) real2DP[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[6], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[7]), axis=1) approx1Sup[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[8], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[9]), axis=1) approx1DP[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[10], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[11]), axis=1) approx2Sup[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[12], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[13]), axis=1) approx2DP[i] = conc((np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[14], np.vsplit(np.loadtxt('out/NF' + str(source) + '_' + str(i+1) + '.txt'), 16)[15]), axis=1) global avgR1Sup global avgR1DP global avgR2Sup global avgR2DP global avgA1Sup global avgA1DP global avgA2Sup global avgA2DP avgR1Sup = np.mean(real1Sup, axis=0) avgR1DP = np.mean(real1DP, axis=0) avgR2Sup = np.mean(real2Sup, axis=0) avgR2DP = np.mean(real2DP, axis=0) avgA1Sup = np.mean(approx1Sup, axis=0) avgA1DP = np.mean(approx1DP, axis=0) avgA2Sup = np.mean(approx2Sup, axis=0) avgA2DP = np.mean(approx2DP, axis=0)
def get_aoneo(self): """Packs the data into one array for a later transfer to the library """ import numpy as np from numpy import require, float64, concatenate as conc nr = self.nr nsp = self.nspecies nmt = sum(self.sp2nmult) nrt = nr*nmt nms = nmt+nsp nsvn = 200 + 2*nr + 4*nsp + 2*nmt + nrt + nms svn = require(np.zeros(nsvn), dtype=float64, requirements='CW') # Simple parameters i = 0 svn[i] = nsp; i+=1; svn[i] = nr; i+=1; svn[i] = self.rmin; i+=1; svn[i] = self.rmax; i+=1; svn[i] = self.kmax; i+=1; svn[i] = self.jmx; i+=1; svn[i] = conc(self.psi_log).sum(); i+=1; # Pointers to data i = 99 s = 199 svn[i] = s+1; i+=1; f=s+nr; svn[s:f] = self.rr; s=f; # pointer to rr svn[i] = s+1; i+=1; f=s+nr; svn[s:f] = self.pp; s=f; # pointer to pp svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2nmult; s=f; # pointer to sp2nmult svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2rcut; s=f; # pointer to sp2rcut svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2norbs; s=f; # pointer to sp2norbs svn[i] = s+1; i+=1; f=s+nsp; svn[s:f] = self.sp2charge; s=f; # pointer to sp2charge svn[i] = s+1; i+=1; f=s+nmt; svn[s:f] = conc(self.sp_mu2j); s=f; # pointer to sp_mu2j svn[i] = s+1; i+=1; f=s+nmt; svn[s:f] = conc(self.sp_mu2rcut); s=f; # pointer to sp_mu2rcut svn[i] = s+1; i+=1; f=s+nrt; svn[s:f] = conc(self.psi_log).reshape(nrt); s=f; # pointer to psi_log svn[i] = s+1; i+=1; f=s+nms; svn[s:f] = conc(self.sp_mu2s); s=f; # pointer to sp_mu2s svn[i] = s+1; # this is a terminator to simple operation return svn
def sv_chain_data(sv): """ This subroutine creates a buffer of information to communicate the system variables to libnao.""" from numpy import zeros, concatenate as conc aos,sv = sv.ao_log, sv nr,nsp,nmt,nrt = aos.nr,aos.nspecies, sum(aos.sp2nmult),aos.nr*sum(aos.sp2nmult) nat,na1,tna,nms = sv.natoms,sv.natoms+1,3*sv.natoms,sum(aos.sp2nmult)+aos.nspecies ndat = 200 + 2*nr + 4*nsp + 2*nmt + nrt + nms + 3*3 + nat + 2*na1 + tna + 4*nsp dat = zeros(ndat) # Simple parameters i = 0 dat[i] = -999.0; i+=1 # pointer to the empty space in simple parameter dat[i] = aos.nspecies; i+=1 dat[i] = aos.nr; i+=1 dat[i] = aos.rmin; i+=1; dat[i] = aos.rmax; i+=1; dat[i] = aos.kmax; i+=1; dat[i] = aos.jmx; i+=1; dat[i] = conc(aos.psi_log).sum(); i+=1; dat[i] = sv.natoms; i+=1 dat[i] = sv.norbs; i+=1 dat[i] = sv.norbs_sc; i+=1 dat[i] = sv.nspin; i+=1 dat[0] = i # Pointers to data i = 99 s = 199 dat[i] = s+1; i+=1; f=s+nr; dat[s:f] = aos.rr; s=f; # pointer to rr dat[i] = s+1; i+=1; f=s+nr; dat[s:f] = aos.pp; s=f; # pointer to pp dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2nmult; s=f; # pointer to sp2nmult dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2rcut; s=f; # pointer to sp2rcut dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2norbs; s=f; # pointer to sp2norbs dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2charge; s=f; # pointer to sp2charge dat[i] = s+1; i+=1; f=s+nmt; dat[s:f] = conc(aos.sp_mu2j); s=f; # pointer to sp_mu2j dat[i] = s+1; i+=1; f=s+nmt; dat[s:f] = conc(aos.sp_mu2rcut); s=f; # pointer to sp_mu2rcut dat[i] = s+1; i+=1; f=s+nrt; dat[s:f] = conc(aos.psi_log).reshape(nrt); s=f; # pointer to psi_log dat[i] = s+1; i+=1; f=s+nms; dat[s:f] = conc(aos.sp_mu2s); s=f; # pointer to sp_mu2s dat[i] = s+1; i+=1; f=s+3*3; dat[s:f] = conc(sv.ucell); s=f; # pointer to ucell (123,xyz) ? dat[i] = s+1; i+=1; f=s+nat; dat[s:f] = sv.atom2sp; s=f; # pointer to atom2sp dat[i] = s+1; i+=1; f=s+na1; dat[s:f] = sv.atom2s; s=f; # pointer to atom2s dat[i] = s+1; i+=1; f=s+na1; dat[s:f] = sv.atom2mu_s; s=f; # pointer to atom2mu_s dat[i] = s+1; i+=1; f=s+tna; dat[s:f] = conc(sv.atom2coord); s=f; # pointer to atom2coord dat[i] = s+1; # this is a terminator to simplify operation return dat
def chain_data(self): """ This subroutine creates a buffer of information to communicate the system variables and the local product vertex to libnao. Later, one will be able to generate the bilocal vertex and conversion coefficient for a given pair of atom species and their coordinates .""" from numpy import zeros, concatenate as conc aos, sv, pl = self.sv.ao_log, self.sv, self.prod_log assert aos.nr == pl.nr assert aos.nspecies == pl.nspecies nr, nsp, nmt, nrt = aos.nr, aos.nspecies, sum( aos.sp2nmult), aos.nr * sum(aos.sp2nmult) nat, na1, tna, nms = sv.natoms, sv.natoms + 1, 3 * sv.natoms, sum( aos.sp2nmult) + aos.nspecies nmtp, nrtp, nmsp = sum(pl.sp2nmult), pl.nr * sum(pl.sp2nmult), sum( pl.sp2nmult) + pl.nspecies nvrt = sum(aos.sp2norbs * aos.sp2norbs * pl.sp2norbs) ndat = 200 + 2*nr + 4*nsp + 2*nmt + nrt + nms + 3*3 + nat + 2*na1 + tna + \ 4*nsp + 2*nmtp + nrtp + nmsp + nvrt dat = zeros(ndat) # Simple parameters i = 0 dat[i] = -999.0 i += 1 # pointer to the empty space in simple parameter dat[i] = aos.nspecies i += 1 dat[i] = aos.nr i += 1 dat[i] = aos.rmin i += 1 dat[i] = aos.rmax i += 1 dat[i] = aos.kmax i += 1 dat[i] = aos.jmx i += 1 dat[i] = conc(aos.psi_log).sum() i += 1 dat[i] = conc(pl.psi_log).sum() i += 1 dat[i] = sv.natoms i += 1 dat[i] = sv.norbs i += 1 dat[i] = sv.norbs_sc i += 1 dat[i] = sv.nspin i += 1 dat[i] = self.tol_loc i += 1 dat[i] = self.tol_biloc i += 1 dat[i] = self.ac_rcut_ratio i += 1 dat[i] = self.ac_npc_max i += 1 dat[i] = self.jcutoff i += 1 dat[i] = self.metric_type i += 1 dat[i] = self.optimize_centers i += 1 dat[i] = self.ngl i += 1 dat[0] = i # Pointers to data i = 99 s = 199 dat[i] = s + 1 i += 1 f = s + nr dat[s:f] = aos.rr s = f # pointer to rr dat[i] = s + 1 i += 1 f = s + nr dat[s:f] = aos.pp s = f # pointer to pp dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2nmult s = f # pointer to sp2nmult dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2rcut s = f # pointer to sp2rcut dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2norbs s = f # pointer to sp2norbs dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2charge s = f # pointer to sp2charge dat[i] = s + 1 i += 1 f = s + nmt dat[s:f] = conc(aos.sp_mu2j) s = f # pointer to sp_mu2j dat[i] = s + 1 i += 1 f = s + nmt dat[s:f] = conc(aos.sp_mu2rcut) s = f # pointer to sp_mu2rcut dat[i] = s + 1 i += 1 f = s + nrt dat[s:f] = conc(aos.psi_log).reshape(nrt) s = f # pointer to psi_log dat[i] = s + 1 i += 1 f = s + nms dat[s:f] = conc(aos.sp_mu2s) s = f # pointer to sp_mu2s dat[i] = s + 1 i += 1 f = s + 3 * 3 dat[s:f] = conc(sv.ucell) s = f # pointer to ucell (123,xyz) ? dat[i] = s + 1 i += 1 f = s + nat dat[s:f] = sv.atom2sp s = f # pointer to atom2sp dat[i] = s + 1 i += 1 f = s + na1 dat[s:f] = sv.atom2s s = f # pointer to atom2s dat[i] = s + 1 i += 1 f = s + na1 dat[s:f] = sv.atom2mu_s s = f # pointer to atom2mu_s dat[i] = s + 1 i += 1 f = s + tna dat[s:f] = conc(sv.atom2coord) s = f # pointer to atom2coord dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = pl.sp2nmult s = f # sp2nmult of product basis dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = pl.sp2rcut s = f # sp2nmult of product basis dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = pl.sp2norbs s = f # sp2norbs of product basis dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = pl.sp2charge s = f # sp2norbs of product basis dat[i] = s + 1 i += 1 f = s + nmtp dat[s:f] = conc(pl.sp_mu2j) s = f # pointer to sp_mu2j dat[i] = s + 1 i += 1 f = s + nmtp dat[s:f] = conc(pl.sp_mu2rcut) s = f # pointer to sp_mu2rcut dat[i] = s + 1 i += 1 f = s + nrtp dat[s:f] = conc(pl.psi_log).reshape(nrtp) s = f # pointer to psi_log dat[i] = s + 1 i += 1 f = s + nmsp dat[s:f] = conc(pl.sp_mu2s) s = f # pointer to sp_mu2s dat[i] = s + 1 i += 1 f = s + nvrt dat[s:f] = conc([v.flatten() for v in pl.sp2vertex]) s = f # pointer to sp2vertex dat[i] = s + 1 # this is a terminator to simplify operation return dat
def wnlbayes_1nivel2(W, sigma, t, l, denoise_sum, flat, weights, Woracle=np.zeros((1, ))): '''Restaura la imagen correspondiente a W (que tiene solo 1 nivel) usando nlbayes en dominio wavelet con ruido sigma y tamanho t de cada patch l es el largo de la traslacion Si denoise_sum == True, se filtra resumen, en caso contrario se deja el original''' N1, N2 = W.shape # Dimensiones de la imagen Wres = np.zeros((N1, N2)) # Coeficientes restaurados N1 /= 2 N2 /= 2 # Dimensiones de cada banda tau = 15 # Generamos traslaciones de la imagen original: Wblock = traslaciones_wav(W, l) listpat3D = gen_listpat_3D2(Wblock, t) if not Woracle.any(): # No oracle provided k = 90 # Fijo para comparar con otros metodos use_oracle = False listpatches = listpat3D listsearch = softthreshold_bandas(listpatches, weights, t) else: # An oracle is provided k = 90 # Fijo para comparar con otros metodos use_oracle = True WblockOR = traslaciones_wav(Woracle, l) listpatches = gen_listpat_3D2(WblockOR, t) listsearch = listpatches #listsearch = softthreshold_bandas(listpatches, weights, t) #listsearch = ponderar_bandas(listpatches, weights, t) for i in xrange(N1 - t + 1): for j in xrange(N2 - t + 1): p = listpat3D[i, j, :, 0] # Patch a restaurar if not use_oracle: vecinos = find_neighbors3([i, j], listsearch, listpatches, t, k) pres, conf = restore_patch_wnlbayes1(p, t, sigma, vecinos, flat) # WNLBayes else: vecinos = find_neighbors3([i, j], listsearch, listpatches, t, k) #, tau=tau) pres, conf = restore_patch_wnlbayes2(p, t, sigma, vecinos, flat) # WNLBayes # Aggregation f1 = range( i, i + t ) # Intevalo de filas de la posicion de los patches en resumen y banda H f2 = range( N1 + i, N1 + i + t ) # Intevalo de filas de la posicion de los patches en banda V y banda D c1 = range( j, j + t ) # Intevalo de columnas de la posicion de los patches en resumen y banda V c2 = range( N2 + j, N2 + j + t ) # Intevalo de columnas de la posicion de los patches en resumen y banda V if denoise_sum: Wres[np.ix_(f1, c1)] += np.reshape( pres[:t**2], (t, t)) # Agregamos patch restaurado al resumen Wres[np.ix_(f1, c2)] += np.reshape( pres[t**2:2 * t**2], (t, t)) # Agregamos patch restaurado a banda H Wres[np.ix_(f2, c1)] += np.reshape( pres[2 * t**2:3 * t**2], (t, t)) # Agregamos patch restaurado a banda V Wres[np.ix_(f2, c2)] += np.reshape( pres[3 * t**2:], (t, t)) # Agregamos patch restaurado a banda D # Normalizing aggregation u = conc(([range(1, t) ], t * np.ones([1, N1 - 2 * t + 2]), [range(t - 1, 0, -1)]), 1) v = conc(([range(1, t) ], t * np.ones([1, N2 - 2 * t + 2]), [range(t - 1, 0, -1)]), 1) mask = repmat(u.T * v, 2, 2) # Mask contiene cant de estimaciones de cada pixel Wres = np.divide(Wres, mask) if not denoise_sum: Wres[:N1, :N2] = W[:N1, :N2] return Wres
def chain_data(self): """ This subroutine creates a buffer of information to communicate the system variables and the local product vertex to libnao. Later, one will be able to generate the bilocal vertex and conversion coefficient for a given pair of atom species and their coordinates .""" from numpy import zeros, concatenate as conc aos,sv,pl = self.sv.ao_log, self.sv, self.prod_log assert aos.nr==pl.nr assert aos.nspecies==pl.nspecies nr,nsp,nmt,nrt = aos.nr,aos.nspecies, sum(aos.sp2nmult),aos.nr*sum(aos.sp2nmult) nat,na1,tna,nms = sv.natoms,sv.natoms+1,3*sv.natoms,sum(aos.sp2nmult)+aos.nspecies nmtp,nrtp,nmsp = sum(pl.sp2nmult),pl.nr*sum(pl.sp2nmult),sum(pl.sp2nmult)+pl.nspecies nvrt = sum(aos.sp2norbs*aos.sp2norbs*pl.sp2norbs) ndat = 200 + 2*nr + 4*nsp + 2*nmt + nrt + nms + 3*3 + nat + 2*na1 + tna + \ 4*nsp + 2*nmtp + nrtp + nmsp + nvrt dat = zeros(ndat) # Simple parameters i = 0 dat[i] = -999.0; i+=1 # pointer to the empty space in simple parameter dat[i] = aos.nspecies; i+=1 dat[i] = aos.nr; i+=1 dat[i] = aos.rmin; i+=1; dat[i] = aos.rmax; i+=1; dat[i] = aos.kmax; i+=1; dat[i] = aos.jmx; i+=1; dat[i] = conc(aos.psi_log).sum(); i+=1; dat[i] = conc(pl.psi_log).sum(); i+=1; dat[i] = sv.natoms; i+=1 dat[i] = sv.norbs; i+=1 dat[i] = sv.norbs_sc; i+=1 dat[i] = sv.nspin; i+=1 dat[i] = self.tol_loc; i+=1 dat[i] = self.tol_biloc; i+=1 dat[i] = self.ac_rcut_ratio; i+=1 dat[i] = self.ac_npc_max; i+=1 dat[i] = self.jcutoff; i+=1 dat[i] = self.metric_type; i+=1 dat[i] = self.optimize_centers; i+=1 dat[i] = self.ngl; i+=1 dat[0] = i # Pointers to data i = 99 s = 199 dat[i] = s+1; i+=1; f=s+nr; dat[s:f] = aos.rr; s=f; # pointer to rr dat[i] = s+1; i+=1; f=s+nr; dat[s:f] = aos.pp; s=f; # pointer to pp dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2nmult; s=f; # pointer to sp2nmult dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2rcut; s=f; # pointer to sp2rcut dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2norbs; s=f; # pointer to sp2norbs dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = aos.sp2charge; s=f; # pointer to sp2charge dat[i] = s+1; i+=1; f=s+nmt; dat[s:f] = conc(aos.sp_mu2j); s=f; # pointer to sp_mu2j dat[i] = s+1; i+=1; f=s+nmt; dat[s:f] = conc(aos.sp_mu2rcut); s=f; # pointer to sp_mu2rcut dat[i] = s+1; i+=1; f=s+nrt; dat[s:f] = conc(aos.psi_log).reshape(nrt); s=f; # pointer to psi_log dat[i] = s+1; i+=1; f=s+nms; dat[s:f] = conc(aos.sp_mu2s); s=f; # pointer to sp_mu2s dat[i] = s+1; i+=1; f=s+3*3; dat[s:f] = conc(sv.ucell); s=f; # pointer to ucell (123,xyz) ? dat[i] = s+1; i+=1; f=s+nat; dat[s:f] = sv.atom2sp; s=f; # pointer to atom2sp dat[i] = s+1; i+=1; f=s+na1; dat[s:f] = sv.atom2s; s=f; # pointer to atom2s dat[i] = s+1; i+=1; f=s+na1; dat[s:f] = sv.atom2mu_s; s=f; # pointer to atom2mu_s dat[i] = s+1; i+=1; f=s+tna; dat[s:f] = conc(sv.atom2coord); s=f; # pointer to atom2coord dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = pl.sp2nmult; s=f; # sp2nmult of product basis dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = pl.sp2rcut; s=f; # sp2nmult of product basis dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = pl.sp2norbs; s=f; # sp2norbs of product basis dat[i] = s+1; i+=1; f=s+nsp; dat[s:f] = pl.sp2charge; s=f; # sp2norbs of product basis dat[i] = s+1; i+=1; f=s+nmtp; dat[s:f] = conc(pl.sp_mu2j); s=f; # pointer to sp_mu2j dat[i] = s+1; i+=1; f=s+nmtp; dat[s:f] = conc(pl.sp_mu2rcut); s=f; # pointer to sp_mu2rcut dat[i] = s+1; i+=1; f=s+nrtp; dat[s:f] = conc(pl.psi_log).reshape(nrtp); s=f; # pointer to psi_log dat[i] = s+1; i+=1; f=s+nmsp; dat[s:f] = conc(pl.sp_mu2s); s=f; # pointer to sp_mu2s dat[i] = s+1; i+=1; f=s+nvrt; dat[s:f] = conc([v.flatten() for v in pl.sp2vertex]); s=f; # pointer to sp2vertex dat[i] = s+1; # this is a terminator to simplify operation return dat
def sv_chain_data(sv): """ This subroutine creates a buffer of information to communicate the system variables to libnao.""" from numpy import zeros, concatenate as conc aos, sv = sv.ao_log, sv nr, nsp, nmt, nrt = aos.nr, aos.nspecies, sum( aos.sp2nmult), aos.nr * sum(aos.sp2nmult) nat, na1, tna, nms = sv.natoms, sv.natoms + 1, 3 * sv.natoms, sum( aos.sp2nmult) + aos.nspecies ndat = 200 + 2 * nr + 4 * nsp + 2 * nmt + nrt + nms + 3 * 3 + nat + 2 * na1 + tna + 4 * nsp dat = zeros(ndat) # Simple parameters i = 0 dat[i] = -999.0 i += 1 # pointer to the empty space in simple parameter dat[i] = aos.nspecies i += 1 dat[i] = aos.nr i += 1 dat[i] = aos.rmin i += 1 dat[i] = aos.rmax i += 1 dat[i] = aos.kmax i += 1 dat[i] = aos.jmx i += 1 dat[i] = conc(aos.psi_log).sum() i += 1 dat[i] = sv.natoms i += 1 dat[i] = sv.norbs i += 1 dat[i] = sv.norbs_sc i += 1 dat[i] = sv.nspin i += 1 dat[0] = i # Pointers to data i = 99 s = 199 dat[i] = s + 1 i += 1 f = s + nr dat[s:f] = aos.rr s = f # pointer to rr dat[i] = s + 1 i += 1 f = s + nr dat[s:f] = aos.pp s = f # pointer to pp dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2nmult s = f # pointer to sp2nmult dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2rcut s = f # pointer to sp2rcut dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2norbs s = f # pointer to sp2norbs dat[i] = s + 1 i += 1 f = s + nsp dat[s:f] = aos.sp2charge s = f # pointer to sp2charge dat[i] = s + 1 i += 1 f = s + nmt dat[s:f] = conc(aos.sp_mu2j) s = f # pointer to sp_mu2j dat[i] = s + 1 i += 1 f = s + nmt dat[s:f] = conc(aos.sp_mu2rcut) s = f # pointer to sp_mu2rcut dat[i] = s + 1 i += 1 f = s + nrt dat[s:f] = conc(aos.psi_log).reshape(nrt) s = f # pointer to psi_log dat[i] = s + 1 i += 1 f = s + nms dat[s:f] = conc(aos.sp_mu2s) s = f # pointer to sp_mu2s dat[i] = s + 1 i += 1 f = s + 3 * 3 dat[s:f] = conc(sv.ucell) s = f # pointer to ucell (123,xyz) ? dat[i] = s + 1 i += 1 f = s + nat dat[s:f] = sv.atom2sp s = f # pointer to atom2sp dat[i] = s + 1 i += 1 f = s + na1 dat[s:f] = sv.atom2s s = f # pointer to atom2s dat[i] = s + 1 i += 1 f = s + na1 dat[s:f] = sv.atom2mu_s s = f # pointer to atom2mu_s dat[i] = s + 1 i += 1 f = s + tna dat[s:f] = conc(sv.atom2coord) s = f # pointer to atom2coord dat[i] = s + 1 # this is a terminator to simplify operation return dat
def generatemat(incidence, constraints, copper): K = np.genfromtxt(incidence, delimiter=",", dtype="d") Nnodes = np.size(K, 0) Nlinks = np.size(K, 1) # These numbers include the dummy node and link # With this info, we create the P matrix, sized P1 = np.eye(Nlinks + 2 * Nnodes) # because a row is needed for each flow, and two for each node P = conc((P1[:Nlinks], P1[-2 * Nnodes :] * 1e-6)) # and the bal/cur part has dif. coeffs # Then we make the q vector, whose values will be changed all the time q = np.zeros(Nlinks + 2 * Nnodes) # q has the same size and structure as the solution 'x' # Then we build the equality constraint matrix A # The stucture is more or less [ K | -I | I ] A1 = conc((K, -np.eye(Nnodes)), axis=1) A = conc((A1, np.eye(Nnodes)), axis=1) # See documentation for why first row is cut A = np.delete(A, np.s_[0], axis=0) # b vector will be defined by the mismatches, in MAIN # Finally, the inequality matrix and vector, G and h. # Refer to doc to understand what the hell I'm doing, as I build G... g1 = np.eye(Nlinks) G1 = g1 for i in range(Nlinks - 1): i += i G1 = np.insert(G1, i + 1, -G1[i], axis=0) G1 = conc((G1, -G1[-1:])) # to model copper plate, we forget about the effect of G matrix on the flows if copper == 1: G1 *= 0 # G1 is ready, now we make G2 G2 = np.zeros((2 * Nlinks, 2 * Nnodes)) # G3 is built as [ 0 | -I | 0 ] g3 = conc((np.zeros((Nnodes, Nlinks)), -np.eye(Nnodes)), axis=1) G3 = conc((g3, np.zeros((Nnodes, Nnodes))), axis=1) g4 = np.eye(Nnodes) G4 = g4 for i in range(Nnodes - 1): i += i G4 = np.insert(G4, i + 1, -G4[i], axis=0) G4 = conc((G4, -G4[-1:])) G5 = conc((np.zeros((2 * Nnodes, Nlinks + Nnodes)), G4), axis=1) G = conc((G1, G2), axis=1) G = conc((G, G3)) G = conc((G, G5)) # That was crazy! Now, the h vector is partly made from the constraints.txt file h = np.genfromtxt(constraints, dtype="d") # but also added some extra zeros for the rest of the constraints. h = np.append(h, np.zeros(3 * Nnodes)) # And that's it! return P, q, G, h, A