Example #1
0
def test_nufft1d1(seed=42, iflag=1):
    np.random.seed(seed)

    ms = int(1e3)
    n = int(2e3)
    tol = 1.0e-9

    x = np.random.uniform(-np.pi, np.pi, n)
    c = np.random.uniform(-1.0, 1.0,
                          n) + 1.0j * np.random.uniform(-1.0, 1.0, n)
    f = finufft.nufft1d1(x, c, ms, eps=tol, iflag=iflag)

    # Make sure that this also works with other values of 'fftw'
    f = finufft.nufft1d1(x,
                         c,
                         ms,
                         eps=tol,
                         iflag=iflag,
                         fftw=interface.FFTWOptions.measure)

    with pytest.raises(TypeError):
        f = finufft.nufft1d1(x, c, ms, eps=tol, iflag=iflag, fftw=100)

    f0 = interface.dirft1d1(x, c, ms, iflag=iflag)
    assert np.all(np.abs((f - f0) / f0) < 1e-6)
Example #2
0
def test_nufft1d2(seed=42, iflag=1):
    np.random.seed(seed)

    ms = int(1e3)
    n = int(2e3)
    tol = 1.0e-9

    x = np.random.uniform(-np.pi, np.pi, n)
    c = np.random.uniform(-1.0, 1.0,
                          n) + 1.0j * np.random.uniform(-1.0, 1.0, n)
    f = finufft.nufft1d1(x, c, ms, eps=tol, iflag=iflag)

    c = finufft.nufft1d2(x, f, eps=tol, iflag=iflag)
    c0 = interface.dirft1d2(x, f, iflag=iflag)
    assert np.all(np.abs((c - c0) / c0) < 1e-6)
Example #3
0
import time
import finufft
import numpy as np

np.random.seed(42)

acc = 1.e-9
N = int(1e6)
M = int(1e6)
x = np.random.uniform(-np.pi, np.pi, N)
c = np.random.randn(N) + 1.j * np.random.randn(N)

strt = time.time()
F = finufft.nufft1d1(x, c, M, eps=acc, iflag=1)
print("Finished nufft in {0:.2e} seconds. Checking..."
      .format(time.time()-strt))

n = 142519
Ftest = 0.0
for j in range(M):
    Ftest += c[j] * np.exp(n * x[j] * 1.j)
Fmax = np.max(np.abs(F))
err = np.abs((F[n + N // 2] - Ftest) / Fmax)
print("Error relative to max of F: {0}".format(err))
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 = finufft.nufft1d1(xj, cj, ms, fk, eps, iflag)
    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 = finufft.nufft1d2(xj, fk, cj, eps, iflag)
    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 = finufft.nufft1d3(x, c, s, f, eps, iflag)
    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)
    timer = time.time()
    ret = finufft.nufft2d1(xj, yj, cj, (ms, mt), fk, eps, iflag)
    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 = 8  # how many vectors to do
    cj = np.array(np.random.rand(ndata, nj) + 1j * np.random.rand(ndata, nj))
    fk = np.zeros([ndata, ms, mt], dtype=np.complex128)
    timer = time.time()
    ret = finufft.nufft2d1(xj, yj, cj, ms, fk, eps, iflag)
    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()[ii] * xj + Kt.ravel()[ii] * yj))
        )  # note fortran-ravel-order needed throughout - mess.
        Xtrue[ii] = fk.ravel()[
            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 = finufft.nufft2d2(xj, yj, fk, cj, eps, iflag)
    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([ndata, nj], dtype=np.complex128)
    fk = np.array(
        np.random.rand(ndata, ms, mt) + 1j * np.random.rand(ndata, ms, mt))
    timer = time.time()
    ret = finufft.nufft2d2(xj, yj, fk, cj, eps, iflag)
    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[dtest, ii]
    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 = finufft.nufft2d3(x, y, c, s, t, f, eps, iflag)
    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)
    timer = time.time()
    ret = finufft.nufft3d1(xj, yj, zj, cj, fk.shape, fk, eps, iflag)
    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 = finufft.nufft3d2(xj, yj, zj, fk, cj, eps, iflag)
    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 = finufft.nufft3d3(x, y, z, c, s, t, u, f, eps, iflag)
    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)
Example #5
0
import numpy as np

# print finufft.nufft1d1.__doc__

np.random.seed(42)

acc = 1.e-9
iflag = 1
N = int(1e6)
M = int(1e5)
x = np.random.uniform(-np.pi, np.pi, M)
c = np.random.randn(M) + 1.j * np.random.randn(M)
F = np.zeros([N], dtype=np.complex128)  # allocate F (modes out)

strt = time.time()
F = finufft.nufft1d1(x, c, N, eps=acc, isign=iflag, debug=1, spread_debug=1)
print("Finished nufft in {0:.2g} seconds. Checking...".format(time.time() -
                                                              strt))

n = 142519  # mode to check
Ftest = 0.0
# this is so slow...
for j in range(M):
    Ftest += c[j] * np.exp(n * x[j] * 1.j)
Fmax = np.max(np.abs(F))
err = np.abs((F[n + N // 2] - Ftest) / Fmax)
print("Error relative to max of F: {0:.2e}".format(err))

# now test FFT mode output version, overwriting F...
strt = time.time()
finufft.nufft1d1(x, c, out=F, eps=acc, isign=iflag, modeord=1)
Example #6
0
# Barnett 8/19/20

import numpy as np
import finufft
import time
np.random.seed(42)

# number of nonuniform points
M = 100000

# input nonuniform points
x = 2 * np.pi * np.random.uniform(size=M)

# their complex strengths
c = (np.random.standard_normal(size=M) +
     1J * np.random.standard_normal(size=M))

# desired number of output Fourier modes
N = 1000000

# calculate the transform
t0 = time.time()
f = finufft.nufft1d1(x, c, N, eps=1e-9)
print("finufft1d1 done in {0:.2g} s.".format(time.time() - t0))

n = 142519  # do a math check, for a single output mode index n
assert ((n >= -N / 2.) & (n < N / 2.))
ftest = sum(c * np.exp(1.j * n * x))
err = np.abs(f[n + N // 2] - ftest) / np.max(np.abs(f))
print("Error relative to max: {0:.2e}".format(err))