def interpolate_to_points(self, ff, x, y, fix_r=False, dzl=None, gil=None): key = self.get_interpolation_key(x, y, fix_r, dzl, gil) p = self.registered_partitions[key] # get the category numbers c1n, c2n, c3n = p.get_Ns() # initialize output vector output = np.empty(x.size) # interpolate appropriate portion with grid (polynomial, for now...) if c1n > 0: if False: zone1 = p.zone1 f = ff.get_grid_value() grid = self.grid lbds = [self.grid.x_bounds[0], self.grid.y_bounds[0]] ubds = [self.grid.x_bounds[1], self.grid.y_bounds[1]] hs = [self.grid.xh, self.grid.yh] interp = fast_interp.interp2d(lbds, ubds, hs, f, k=7, p=[True, True]) output[zone1] = interp(x[zone1], y[zone1]) else: # HERE zone1 = p.zone1 funch = np.fft.fft2(ff.get_smoothed_grid_value()) out = np.zeros(p.zone1_N, dtype=complex) if old_nufft: diagnostic = finufftpy.nufft2d2(p.x_transf, p.y_transf, out, 1, 1e-14, funch, modeord=1) else: diagnostic = finufft.nufft2d2(p.x_transf, p.y_transf, funch, out, isign=1, eps=1e-14, modeord=1) out.real / np.prod(funch.shape) output[zone1] = out.real / np.prod(funch.shape) if c2n > 0: for ind in range(self.N): ebdy = self[ind] z2l = p.zone2l[ind] z2transfr = p.zone2_transfr[ind] z2t = p.zone2t[ind] fr = ff[ind] output[z2l] = ebdy.interpolate_radial_to_points( fr, z2transfr, z2t) # fill in those that are exterior with nan if c3n > 0: for z3 in p.zone3l: output[z3] = np.nan return output
def interpolate_radial_to_points(self, fr, transf_r, t): """ Interpolate the radial function fr defined on this annulus to points with coordinates (r, t) given by transf_r, t """ self.interpolation_hold[:self.M, :] = fr self.interpolation_hold[self.M:, :] = fr[::-1] funch = np.fft.fft2( self.interpolation_hold) * self.interpolation_modifier funch[self.M] = 0.0 out = np.empty(t.size, dtype=complex) if old_nufft: diagnostic = finufftpy.nufft2d2(transf_r, t, out, 1, 1e-14, funch, modeord=1) else: diagnostic = finufft.nufft2d2(transf_r, t, funch, out, isign=1, eps=1e-14, modeord=1) vals = out.real / np.prod(funch.shape) return vals
def cartesian_to_pft(templates, T, pf_grid): xnodesr = pf_grid['xnodesr'] n_psi = pf_grid['n_psi'] ngridr = xnodesr.shape[0] n_templates = templates.shape[0] N = templates.shape[1] dx = T / N dy = T / N wx = pf_grid['wx'] wy = pf_grid['wy'] Shat = np.zeros((n_templates, ngridr * n_psi), dtype=np.complex128) upsampfac = 1.25 fcc = np.empty(len(wx), dtype=np.complex128) for k in range(n_templates): template = templates[k, :, :] # Need to force Fortran ordering because that's what the FINUFFT # interface expects. gg = np.asfortranarray(template.transpose((1, 0))) isign = -1 eps = 1e-6 # Note: Crashes if gg is a 1D vector (raveled). Why? finufftpy.nufft2d2(wx * dx, wy * dy, fcc, isign, eps, gg, upsampfac=upsampfac) Shat[k, :] = fcc return Shat
def visibilities_from_image(self, image_in_2d): visibilities = np.zeros(shape=self.uv_wavelengths.shape[0], dtype=np.complex) ret = finufftpy.nufft2d2(self.uv_wavelengths_normalized[:, 1] * np.pi, self.uv_wavelengths_normalized[:, 0] * np.pi, visibilities, 1, self.eps, image_in_2d[:, ::-1]) visibilities *= self.shift return visibilities
def forward(self, img): """ :param img: 2D array with shape (ms, mt) :return: c[j] = SUM img[k1,k2] exp(+/-i (k1 x[j] + k2 y[j])), for j = 0,...,nj-1 k1,k2 where sum is over -ms/2 <= k1 <= (ms-1)/2, -mt/2 <= k2 <= (mt-1)/2 and x and y are u & v. """ vis = np.empty((self.size_nu, ), dtype=np.complex128, order='F') img = np.array(img, dtype=np.complex128, order='F') code = finufftpy.nufft2d2(self.u, self.v, vis, isign=-1, eps=1e-8, f=img, fftw=1, upsampfac=1.25) return vis
def nufft_interpolate_grid_to_interface(self, f): """ Call through interpolate_grid_to_interface """ funch = np.fft.fft2(f) out = np.zeros(self.interfaces_x_transf.size, dtype=complex) if old_nufft: diagnostic = finufftpy.nufft2d2(self.interfaces_x_transf, self.interfaces_y_transf, out, 1, 1e-14, funch, modeord=1) else: diagnostic = finufft.nufft2d2(self.interfaces_x_transf, self.interfaces_y_transf, funch, out, isign=1, eps=1e-14, modeord=1) return out.real / np.prod(funch.shape)
def __call__(self, x, y): sh = x.shape x, y = x.ravel(), y.ravel() work = np.empty(x.size, dtype=complex) info = finufftpy.nufft2d2(x, y, work, 1, 1e-14, self.uh, modeord=1) return (work.real*self.scale).reshape(sh)
def accuracy_speed_tests(num_nonuniform_points,num_uniform_points,eps): nj,nk = int(num_nonuniform_points),int(num_nonuniform_points) iflag=1 num_samples=int(np.minimum(5,num_uniform_points*0.5+1)) # number of outputs used for estimating accuracy; is small for speed print('Accuracy and speed tests for %d nonuniform points and eps=%g (error estimates use %d samples per run)' % (num_nonuniform_points,eps,num_samples)) # for doing the error estimates Xest=np.zeros(num_samples,dtype=np.complex128) Xtrue=np.zeros(num_samples,dtype=np.complex128) ###### 1-d cases ........................................................ ms=int(num_uniform_points) xj=np.random.rand(nj)*2*math.pi-math.pi cj=np.random.rand(nj)+1j*np.random.rand(nj); fk=np.zeros([ms],dtype=np.complex128) timer=time.time() ret=finufftpy.nufft1d1(xj,cj,iflag,eps,ms,fk) elapsed=time.time()-timer k=np.arange(-np.floor(ms/2),np.floor((ms-1)/2+1)) for ii in np.arange(0,num_samples): Xest[ii]=np.sum(cj * np.exp(1j*k[ii]*xj)) Xtrue[ii]=fk[ii] print_report('finufft1d1',elapsed,Xest,Xtrue,nj) xj=np.random.rand(nj)*2*math.pi-math.pi cj=np.zeros([nj],dtype=np.complex128); fk=np.random.rand(ms)+1j*np.random.rand(ms); timer=time.time() ret=finufftpy.nufft1d2(xj,cj,iflag,eps,fk) elapsed=time.time()-timer k=np.arange(-np.floor(ms/2),np.floor((ms-1)/2+1)) for ii in np.arange(0,num_samples): Xest[ii]=np.sum(fk * np.exp(1j*k*xj[ii])) Xtrue[ii]=cj[ii] print_report('finufft1d2',elapsed,Xest,Xtrue,nj) x=np.random.rand(nj)*2*math.pi-math.pi c=np.random.rand(nj)+1j*np.random.rand(nj); s=np.random.rand(nk)*2*math.pi-math.pi f=np.zeros([nk],dtype=np.complex128) timer=time.time() ret=finufftpy.nufft1d3(x,c,iflag,eps,s,f) elapsed=time.time()-timer for ii in np.arange(0,num_samples): Xest[ii]=np.sum(c * np.exp(1j*s[ii]*x)) Xtrue[ii]=f[ii] print_report('finufft1d3',elapsed,Xest,Xtrue,nj+nk) ###### 2-d cases .................................................... ms=int(np.ceil(np.sqrt(num_uniform_points))) mt=ms xj=np.random.rand(nj)*2*math.pi-math.pi yj=np.random.rand(nj)*2*math.pi-math.pi cj=np.random.rand(nj)+1j*np.random.rand(nj) fk=np.zeros([ms,mt],dtype=np.complex128,order='F') timer=time.time() ret=finufftpy.nufft2d1(xj,yj,cj,iflag,eps,ms,mt,fk) elapsed=time.time()-timer Ks,Kt=np.mgrid[-np.floor(ms/2):np.floor((ms-1)/2+1),-np.floor(mt/2):np.floor((mt-1)/2+1)] for ii in np.arange(0,num_samples): Xest[ii]=np.sum(cj * np.exp(1j*(Ks.ravel()[ii]*xj+Kt.ravel()[ii]*yj))) Xtrue[ii]=fk.ravel()[ii] print_report('finufft2d1',elapsed,Xest,Xtrue,nj) ## 2d1many: ndata = 5 # how many vectors to do cj=np.array(np.random.rand(nj,ndata)+1j*np.random.rand(nj,ndata),order='F') fk=np.zeros([ms,mt,ndata],dtype=np.complex128,order='F') timer=time.time() ret=finufftpy.nufft2d1many(xj,yj,cj,iflag,eps,ms,mt,fk) elapsed=time.time()-timer dtest = ndata-1 # which of the ndata to test (in 0,..,ndata-1) for ii in np.arange(0,num_samples): Xest[ii]=np.sum(cj[:,dtest] * np.exp(1j*(Ks.ravel(order='F')[ii]*xj+Kt.ravel(order='F')[ii]*yj))) # note fortran-ravel-order needed throughout - mess. Xtrue[ii]=fk.ravel(order='F')[ii + dtest*ms*mt] # hack the offset in fk array - has to be better way print_report('finufft2d1many',elapsed,Xest,Xtrue,ndata*nj) # 2d2 xj=np.random.rand(nj)*2*math.pi-math.pi yj=np.random.rand(nj)*2*math.pi-math.pi cj=np.zeros([nj],dtype=np.complex128); fk=np.random.rand(ms,mt)+1j*np.random.rand(ms,mt); timer=time.time() ret=finufftpy.nufft2d2(xj,yj,cj,iflag,eps,fk) elapsed=time.time()-timer Ks,Kt=np.mgrid[-np.floor(ms/2):np.floor((ms-1)/2+1),-np.floor(mt/2):np.floor((mt-1)/2+1)] for ii in np.arange(0,num_samples): Xest[ii]=np.sum(fk * np.exp(1j*(Ks*xj[ii]+Kt*yj[ii]))) Xtrue[ii]=cj[ii] print_report('finufft2d2',elapsed,Xest,Xtrue,nj) # 2d2many (using same ndata and dtest as 2d1many; see above) cj=np.zeros([nj,ndata],order='F',dtype=np.complex128); fk=np.array(np.random.rand(ms,mt,ndata)+1j*np.random.rand(ms,mt,ndata),order='F') timer=time.time() ret=finufftpy.nufft2d2many(xj,yj,cj,iflag,eps,fk) elapsed=time.time()-timer for ii in np.arange(0,num_samples): Xest[ii]=np.sum(fk[:,:,dtest] * np.exp(1j*(Ks*xj[ii]+Kt*yj[ii]))) Xtrue[ii]=cj[ii,dtest] print_report('finufft2d2many',elapsed,Xest,Xtrue,ndata*nj) # 2d3 x=np.random.rand(nj)*2*math.pi-math.pi y=np.random.rand(nj)*2*math.pi-math.pi c=np.random.rand(nj)+1j*np.random.rand(nj); s=np.random.rand(nk)*2*math.pi-math.pi t=np.random.rand(nk)*2*math.pi-math.pi f=np.zeros([nk],dtype=np.complex128) timer=time.time() ret=finufftpy.nufft2d3(x,y,c,iflag,eps,s,t,f) elapsed=time.time()-timer for ii in np.arange(0,num_samples): Xest[ii]=np.sum(c * np.exp(1j*(s[ii]*x+t[ii]*y))) Xtrue[ii]=f[ii] print_report('finufft2d3',elapsed,Xest,Xtrue,nj+nk) ###### 3-d cases ............................................................ ms=int(np.ceil(num_uniform_points**(1.0/3))) mt=ms mu=ms xj=np.random.rand(nj)*2*math.pi-math.pi yj=np.random.rand(nj)*2*math.pi-math.pi zj=np.random.rand(nj)*2*math.pi-math.pi cj=np.random.rand(nj)+1j*np.random.rand(nj); fk=np.zeros([ms,mt,mu],dtype=np.complex128,order='F') timer=time.time() ret=finufftpy.nufft3d1(xj,yj,zj,cj,iflag,eps,ms,mt,mu,fk) elapsed=time.time()-timer Ks,Kt,Ku=np.mgrid[-np.floor(ms/2):np.floor((ms-1)/2+1),-np.floor(mt/2):np.floor((mt-1)/2+1),-np.floor(mu/2):np.floor((mu-1)/2+1)] for ii in np.arange(0,num_samples): Xest[ii]=np.sum(cj * np.exp(1j*(Ks.ravel()[ii]*xj+Kt.ravel()[ii]*yj+Ku.ravel()[ii]*zj))) Xtrue[ii]=fk.ravel()[ii] print_report('finufft3d1',elapsed,Xest,Xtrue,nj) xj=np.random.rand(nj)*2*math.pi-math.pi yj=np.random.rand(nj)*2*math.pi-math.pi zj=np.random.rand(nj)*2*math.pi-math.pi cj=np.zeros([nj],dtype=np.complex128); fk=np.random.rand(ms,mt,mu)+1j*np.random.rand(ms,mt,mu); timer=time.time() ret=finufftpy.nufft3d2(xj,yj,zj,cj,iflag,eps,fk) elapsed=time.time()-timer Ks,Kt,Ku=np.mgrid[-np.floor(ms/2):np.floor((ms-1)/2+1),-np.floor(mt/2):np.floor((mt-1)/2+1),-np.floor(mu/2):np.floor((mu-1)/2+1)] for ii in np.arange(0,num_samples): Xest[ii]=np.sum(fk * np.exp(1j*(Ks*xj[ii]+Kt*yj[ii]+Ku*zj[ii]))) Xtrue[ii]=cj[ii] print_report('finufft3d2',elapsed,Xest,Xtrue,nj) x=np.random.rand(nj)*2*math.pi-math.pi y=np.random.rand(nj)*2*math.pi-math.pi z=np.random.rand(nj)*2*math.pi-math.pi c=np.random.rand(nj)+1j*np.random.rand(nj); s=np.random.rand(nk)*2*math.pi-math.pi t=np.random.rand(nk)*2*math.pi-math.pi u=np.random.rand(nk)*2*math.pi-math.pi f=np.zeros([nk],dtype=np.complex128) timer=time.time() ret=finufftpy.nufft3d3(x,y,z,c,iflag,eps,s,t,u,f) elapsed=time.time()-timer for ii in np.arange(0,num_samples): Xest[ii]=np.sum(c * np.exp(1j*(s[ii]*x+t[ii]*y+u[ii]*z))) Xtrue[ii]=f[ii] print_report('finufft3d3',elapsed,Xest,Xtrue,nj+nk)
def accuracy_speed_tests(num_nonuniform_points, num_uniform_points, eps): nj, nk = int(num_nonuniform_points), int(num_nonuniform_points) iflag = 1 num_samples = int(np.minimum(20, num_uniform_points * 0.5 + 1)) #for estimating accuracy print( 'Accuracy and speed tests for %d nonuniform points and eps=%g (error estimates use %d samples per run)' % (num_nonuniform_points, eps, num_samples)) # for doing the error estimates Xest = np.zeros(num_samples, dtype=np.complex128) Xtrue = np.zeros(num_samples, dtype=np.complex128) ###### 1-d ms = int(num_uniform_points) xj = np.random.rand(nj) * 2 * math.pi - math.pi cj = np.random.rand(nj) + 1j * np.random.rand(nj) fk = np.zeros([ms], dtype=np.complex128) timer = time.time() ret = finufftpy.nufft1d1(xj, cj, iflag, eps, ms, fk) elapsed = time.time() - timer k = np.arange(-np.floor(ms / 2), np.floor((ms - 1) / 2 + 1)) for ii in np.arange(0, num_samples): Xest[ii] = np.sum(cj * np.exp(1j * k[ii] * xj)) Xtrue[ii] = fk[ii] print_report('finufft1d1', elapsed, Xest, Xtrue, nj) xj = np.random.rand(nj) * 2 * math.pi - math.pi cj = np.zeros([nj], dtype=np.complex128) fk = np.random.rand(ms) + 1j * np.random.rand(ms) timer = time.time() ret = finufftpy.nufft1d2(xj, cj, iflag, eps, fk) elapsed = time.time() - timer k = np.arange(-np.floor(ms / 2), np.floor((ms - 1) / 2 + 1)) for ii in np.arange(0, num_samples): Xest[ii] = np.sum(fk * np.exp(1j * k * xj[ii])) Xtrue[ii] = cj[ii] print_report('finufft1d2', elapsed, Xest, Xtrue, nj) x = np.random.rand(nj) * 2 * math.pi - math.pi c = np.random.rand(nj) + 1j * np.random.rand(nj) s = np.random.rand(nk) * 2 * math.pi - math.pi f = np.zeros([nk], dtype=np.complex128) timer = time.time() ret = finufftpy.nufft1d3(x, c, iflag, eps, s, f) elapsed = time.time() - timer for ii in np.arange(0, num_samples): Xest[ii] = np.sum(c * np.exp(1j * s[ii] * x)) Xtrue[ii] = f[ii] print_report('finufft1d3', elapsed, Xest, Xtrue, nj + nk) ###### 2-d ms = int(np.ceil(np.sqrt(num_uniform_points))) mt = ms xj = np.random.rand(nj) * 2 * math.pi - math.pi yj = np.random.rand(nj) * 2 * math.pi - math.pi cj = np.random.rand(nj) + 1j * np.random.rand(nj) fk = np.zeros([ms, mt], dtype=np.complex128, order='F') timer = time.time() ret = finufftpy.nufft2d1(xj, yj, cj, iflag, eps, ms, mt, fk) elapsed = time.time() - timer Ks, Kt = np.mgrid[-np.floor(ms / 2):np.floor((ms - 1) / 2 + 1), -np.floor(mt / 2):np.floor((mt - 1) / 2 + 1)] for ii in np.arange(0, num_samples): Xest[ii] = np.sum( cj * np.exp(1j * (Ks.ravel()[ii] * xj + Kt.ravel()[ii] * yj))) Xtrue[ii] = fk.ravel()[ii] print_report('finufft2d1', elapsed, Xest, Xtrue, nj) xj = np.random.rand(nj) * 2 * math.pi - math.pi yj = np.random.rand(nj) * 2 * math.pi - math.pi cj = np.zeros([nj], dtype=np.complex128) fk = np.random.rand(ms, mt) + 1j * np.random.rand(ms, mt) timer = time.time() ret = finufftpy.nufft2d2(xj, yj, cj, iflag, eps, fk) elapsed = time.time() - timer Ks, Kt = np.mgrid[-np.floor(ms / 2):np.floor((ms - 1) / 2 + 1), -np.floor(mt / 2):np.floor((mt - 1) / 2 + 1)] for ii in np.arange(0, num_samples): Xest[ii] = np.sum(fk * np.exp(1j * (Ks * xj[ii] + Kt * yj[ii]))) Xtrue[ii] = cj[ii] print_report('finufft2d2', elapsed, Xest, Xtrue, nj) x = np.random.rand(nj) * 2 * math.pi - math.pi y = np.random.rand(nj) * 2 * math.pi - math.pi c = np.random.rand(nj) + 1j * np.random.rand(nj) s = np.random.rand(nk) * 2 * math.pi - math.pi t = np.random.rand(nk) * 2 * math.pi - math.pi f = np.zeros([nk], dtype=np.complex128) timer = time.time() ret = finufftpy.nufft2d3(x, y, c, iflag, eps, s, t, f) elapsed = time.time() - timer for ii in np.arange(0, num_samples): Xest[ii] = np.sum(c * np.exp(1j * (s[ii] * x + t[ii] * y))) Xtrue[ii] = f[ii] print_report('finufft2d3', elapsed, Xest, Xtrue, nj + nk) ###### 3-d ms = int(np.ceil(num_uniform_points**(1.0 / 3))) mt = ms mu = ms xj = np.random.rand(nj) * 2 * math.pi - math.pi yj = np.random.rand(nj) * 2 * math.pi - math.pi zj = np.random.rand(nj) * 2 * math.pi - math.pi cj = np.random.rand(nj) + 1j * np.random.rand(nj) fk = np.zeros([ms, mt, mu], dtype=np.complex128, order='F') timer = time.time() ret = finufftpy.nufft3d1(xj, yj, zj, cj, iflag, eps, ms, mt, mu, fk) elapsed = time.time() - timer Ks, Kt, Ku = np.mgrid[-np.floor(ms / 2):np.floor((ms - 1) / 2 + 1), -np.floor(mt / 2):np.floor((mt - 1) / 2 + 1), -np.floor(mu / 2):np.floor((mu - 1) / 2 + 1)] for ii in np.arange(0, num_samples): Xest[ii] = np.sum(cj * np.exp( 1j * (Ks.ravel()[ii] * xj + Kt.ravel()[ii] * yj + Ku.ravel()[ii] * zj))) Xtrue[ii] = fk.ravel()[ii] print_report('finufft3d1', elapsed, Xest, Xtrue, nj) xj = np.random.rand(nj) * 2 * math.pi - math.pi yj = np.random.rand(nj) * 2 * math.pi - math.pi zj = np.random.rand(nj) * 2 * math.pi - math.pi cj = np.zeros([nj], dtype=np.complex128) fk = np.random.rand(ms, mt, mu) + 1j * np.random.rand(ms, mt, mu) timer = time.time() ret = finufftpy.nufft3d2(xj, yj, zj, cj, iflag, eps, fk) elapsed = time.time() - timer Ks, Kt, Ku = np.mgrid[-np.floor(ms / 2):np.floor((ms - 1) / 2 + 1), -np.floor(mt / 2):np.floor((mt - 1) / 2 + 1), -np.floor(mu / 2):np.floor((mu - 1) / 2 + 1)] for ii in np.arange(0, num_samples): Xest[ii] = np.sum( fk * np.exp(1j * (Ks * xj[ii] + Kt * yj[ii] + Ku * zj[ii]))) Xtrue[ii] = cj[ii] print_report('finufft3d2', elapsed, Xest, Xtrue, nj) x = np.random.rand(nj) * 2 * math.pi - math.pi y = np.random.rand(nj) * 2 * math.pi - math.pi z = np.random.rand(nj) * 2 * math.pi - math.pi c = np.random.rand(nj) + 1j * np.random.rand(nj) s = np.random.rand(nk) * 2 * math.pi - math.pi t = np.random.rand(nk) * 2 * math.pi - math.pi u = np.random.rand(nk) * 2 * math.pi - math.pi f = np.zeros([nk], dtype=np.complex128) timer = time.time() ret = finufftpy.nufft3d3(x, y, z, c, iflag, eps, s, t, u, f) elapsed = time.time() - timer for ii in np.arange(0, num_samples): Xest[ii] = np.sum(c * np.exp(1j * (s[ii] * x + t[ii] * y + u[ii] * z))) Xtrue[ii] = f[ii] print_report('finufft3d3', elapsed, Xest, Xtrue, nj + nk)