Ejemplo n.º 1
0
def compare_fftpack_with_hermft_1d():
  mt = flex.mersenne_twister(seed=0)
  for n_cmpl in xrange(1, 101):
    primes = prime_factors_of(n_cmpl)
    if (n_cmpl != 1 and max(primes) > 19): continue
    n_real = n_cmpl * 2
    m_real = n_real + 2
    z = (mt.random_double(size=m_real)*2-1).as_float()
      # The imaginary parts of the first and last complex values should
      # be zero, but z has random values in these places, to prove that
      # they don't change the result.
    hermft_xy = z.deep_copy()
    hermft_xy[1] = hermft_xy[-2]
      # real part of last complex value stored in imaginary part of first
      # (see ccp4/doc/libfft.doc)
    hermft_xy[-2] = 253
      # random value, for consistency check below
    d = flex.int((m_real,2,m_real,m_real,m_real))
    ccp4io_dev_ext.fftlib_hermft(xy=hermft_xy, n=n_cmpl, d=d)
    # consistency check first
    assert hermft_xy[-2] == 253
    assert hermft_xy[-1] == z[-1]
    # reset to zero for comparision with fftpack result further down
    hermft_xy[-2] = 0
    hermft_xy[-1] = 0
    fft = scitbx.fftpack.real_to_complex(n_real)
    assert fft.m_real() == m_real
    fftpack_xy = z.as_double()
    for i in xrange(n_cmpl): fftpack_xy[i*2+1] *= -1 # conjugate
    fft.backward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float()
    if (flex.max_absolute(hermft_xy-fftpack_xy) > 1e-5):
      assert approx_equal(hermft_xy, fftpack_xy, eps=1e-5)
Ejemplo n.º 2
0
def compare_fftpack_with_hermft_1d():
    mt = flex.mersenne_twister(seed=0)
    for n_cmpl in xrange(1, 101):
        primes = prime_factors_of(n_cmpl)
        if (n_cmpl != 1 and max(primes) > 19): continue
        n_real = n_cmpl * 2
        m_real = n_real + 2
        z = (mt.random_double(size=m_real) * 2 - 1).as_float()
        # The imaginary parts of the first and last complex values should
        # be zero, but z has random values in these places, to prove that
        # they don't change the result.
        hermft_xy = z.deep_copy()
        hermft_xy[1] = hermft_xy[-2]
        # real part of last complex value stored in imaginary part of first
        # (see ccp4/doc/libfft.doc)
        hermft_xy[-2] = 253
        # random value, for consistency check below
        d = flex.int((m_real, 2, m_real, m_real, m_real))
        ccp4io_dev_ext.fftlib_hermft(xy=hermft_xy, n=n_cmpl, d=d)
        # consistency check first
        assert hermft_xy[-2] == 253
        assert hermft_xy[-1] == z[-1]
        # reset to zero for comparision with fftpack result further down
        hermft_xy[-2] = 0
        hermft_xy[-1] = 0
        fft = scitbx.fftpack.real_to_complex(n_real)
        assert fft.m_real() == m_real
        fftpack_xy = z.as_double()
        for i in xrange(n_cmpl):
            fftpack_xy[i * 2 + 1] *= -1  # conjugate
        fft.backward(fftpack_xy)
        fftpack_xy = fftpack_xy.as_float()
        if (flex.max_absolute(hermft_xy - fftpack_xy) > 1e-5):
            assert approx_equal(hermft_xy, fftpack_xy, eps=1e-5)
Ejemplo n.º 3
0
def exercise_fftlib_real_complex_3d_real_imag_w_given_dims(
        mt, nu, nv, nw, verbose):
    map_size = nu * nv * (nw + 2)
    map = (mt.random_double(size=map_size) * 2 - 1).as_float()
    x = map.deep_copy()
    ccp4io_dev_ext.fftlib_real_to_complex_3d_real_imag_w(x=x,
                                                         nu=nu,
                                                         nv=nv,
                                                         nw=nw,
                                                         complete=True)
    f = x.deep_copy()
    ccp4io_dev_ext.hermitian_conjugate_3d_real_imag_w(x=x, nu=nu, nv=nv, nw=nw)
    ccp4io_dev_ext.fftlib_complex_to_real_3d_real_imag_w(x=x,
                                                         nu=nu,
                                                         nv=nv,
                                                         nw=nw,
                                                         complete=True)
    x *= 1 / (nu * nv * nw)
    xfocus = x[:nu * nv * nw]
    mfocus = map[:nu * nv * nw]
    if (flex.max_absolute(xfocus - mfocus) > 1e-5):
        assert approx_equal(xfocus, mfocus, eps=1e-5)
    #
    fft = scitbx.fftpack.real_to_complex_3d((nu, nv, nw))
    g = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()))
    assert g.size() == f.size()
    for u in xrange(nu):
        for v in xrange(nv):
            for w in xrange(nw + 2):
                g[(u * nv + v) * (nw + 2) + w] = map[(w * nv + v) * nu + u]
    fft.forward(g)
    for u in xrange(nu):
        for v in xrange(nv):
            for w in xrange(nw // 2 + 1):
                wr = w * 2
                wi = wr + 1
                for wj in [wr, wi]:
                    ge = g[(u * nv + v) * (nw + 2) + wj]
                    fe = f[(wj * nv + v) * nu + u]
                    assert approx_equal(fe, ge, eps=1e-4)
    #
    y = map.deep_copy()
    ccp4io_dev_ext.fftlib_real_to_complex_3d_real_imag_w(x=y,
                                                         nu=nu,
                                                         nv=nv,
                                                         nw=nw,
                                                         complete=False)
    ccp4io_dev_ext.hermitian_conjugate_3d_real_imag_w(x=y, nu=nu, nv=nv, nw=nw)
    ccp4io_dev_ext.fftlib_complex_to_real_3d_real_imag_w(x=y,
                                                         nu=nu,
                                                         nv=nv,
                                                         nw=nw,
                                                         complete=False)
    y *= 1 / (nu * nv * nw)
    show_complete_true_false_cc(nu, nv, nw, (x, y), verbose)
Ejemplo n.º 4
0
def exercise_savitzky_golay_smoothing():

    plot = False

    def rms(flex_double):
        return math.sqrt(flex.mean(flex.pow2(flex_double)))

    for sigma_frac in (0.005, 0.01, 0.05, 0.1):
        mean = random.randint(-5, 5)
        scale = flex.random_double() * 10
        sigma = flex.random_double() * 5 + 1
        gaussian = scitbx.math.curve_fitting.gaussian(scale, mean, sigma)

        x = flex.double(frange(-20, 20, 0.1))
        y = gaussian(x)
        rand_norm = scitbx.random.normal_distribution(mean=0,
                                                      sigma=sigma_frac *
                                                      flex.max_absolute(y))
        g = scitbx.random.variate(rand_norm)
        noise = g(y.size())
        y_noisy = y + noise
        # according to numerical recipes the best results are obtained where the
        # full window width is between 1 and 2 times the number of points at fwhm
        # for polynomials of degree 4
        half_window = int(round(0.5 * 2.355 * sigma * 10))
        y_filtered = savitzky_golay_filter(x,
                                           y_noisy,
                                           half_window=half_window,
                                           degree=4)[1]
        extracted_noise = y_noisy - y_filtered
        rms_noise = rms(noise)
        rms_extracted_noise = rms(extracted_noise)

        assert is_below_limit(value=abs(rand_norm.sigma - rms_noise) /
                              rand_norm.sigma,
                              limit=0.15)
        assert is_below_limit(
            value=abs(rand_norm.sigma - rms_extracted_noise) / rand_norm.sigma,
            limit=0.15)

        diff = y_filtered - y
        assert is_below_limit(value=(rms(diff) / rand_norm.sigma), limit=0.4)

        if plot:
            from matplotlib import pyplot
            pyplot.plot(x, y)
            pyplot.plot(x, noise)
            pyplot.scatter(x, y_noisy, marker="x")
            pyplot.plot(x, y_filtered)
            pyplot.show()
            pyplot.plot(x, extracted_noise)
            pyplot.plot(x, noise)
            pyplot.show()

    return
Ejemplo n.º 5
0
def exercise_savitzky_golay_smoothing():

  plot = False

  def rms(flex_double):
    return math.sqrt(flex.mean(flex.pow2(flex_double)))

  for sigma_frac in (0.005, 0.01, 0.05, 0.1):
    mean = random.randint(-5,5)
    scale = flex.random_double() * 10
    sigma = flex.random_double() * 5 + 1
    gaussian = curve_fitting.gaussian(scale, mean, sigma)

    x = flex.double(frange(-20,20,0.1))
    y = gaussian(x)
    rand_norm = scitbx.random.normal_distribution(
      mean=0, sigma=sigma_frac*flex.max_absolute(y))
    g = scitbx.random.variate(rand_norm)
    noise = g(y.size())
    y_noisy = y + noise
    # according to numerical recipes the best results are obtained where the
    # full window width is between 1 and 2 times the number of points at fwhm
    # for polynomials of degree 4
    half_window = int(round(0.5 * 2.355 * sigma * 10))
    y_filtered = savitzky_golay_filter(x, y_noisy, half_window=half_window, degree=4)[1]
    extracted_noise = y_noisy - y_filtered
    rms_noise = rms(noise)
    rms_extracted_noise = rms(extracted_noise)

    assert is_below_limit(
      value=abs(rand_norm.sigma - rms_noise)/rand_norm.sigma,
      limit=0.15)
    assert is_below_limit(
      value=abs(rand_norm.sigma - rms_extracted_noise)/rand_norm.sigma,
      limit=0.15)

    diff = y_filtered - y
    assert is_below_limit(
      value=(rms(diff)/ rand_norm.sigma),
      limit=0.4)

    if plot:
      from matplotlib import pyplot
      pyplot.plot(x, y)
      pyplot.plot(x, noise)
      pyplot.scatter(x, y_noisy, marker="x")
      pyplot.plot(x, y_filtered)
      pyplot.show()
      pyplot.plot(x, extracted_noise)
      pyplot.plot(x, noise)
      pyplot.show()

  return
Ejemplo n.º 6
0
def compare_fftpack_with_cmplft_1d():
  mt = flex.mersenne_twister(seed=0)
  for n in xrange(1, 101):
    primes = prime_factors_of(n)
    if (n != 1 and max(primes) > 19): continue
    z = (mt.random_double(size=n*2)*2-1).as_float()
    cmplft_xy = z.deep_copy()
    d = flex.int((2*n,2,2*n,2*n,2*n))
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=n, d=d)
    fft = scitbx.fftpack.complex_to_complex(n)
    fftpack_xy = z.as_double()
    fft.forward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float()
    if (flex.max_absolute(cmplft_xy-fftpack_xy) > 1e-5):
      assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-5)
Ejemplo n.º 7
0
def compare_fftpack_with_cmplft_1d():
    mt = flex.mersenne_twister(seed=0)
    for n in xrange(1, 101):
        primes = prime_factors_of(n)
        if (n != 1 and max(primes) > 19): continue
        z = (mt.random_double(size=n * 2) * 2 - 1).as_float()
        cmplft_xy = z.deep_copy()
        d = flex.int((2 * n, 2, 2 * n, 2 * n, 2 * n))
        ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=n, d=d)
        fft = scitbx.fftpack.complex_to_complex(n)
        fftpack_xy = z.as_double()
        fft.forward(fftpack_xy)
        fftpack_xy = fftpack_xy.as_float()
        if (flex.max_absolute(cmplft_xy - fftpack_xy) > 1e-5):
            assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-5)
Ejemplo n.º 8
0
def compare_fftpack_with_realft_1d():
  mt = flex.mersenne_twister(seed=0)
  for n_cmpl in xrange(1, 101):
    primes = prime_factors_of(n_cmpl)
    if (n_cmpl != 1 and max(primes) > 19): continue
    n_real = n_cmpl * 2
    m_real = n_real + 2
    z = (mt.random_double(size=m_real)*2-1).as_float()
    realft_xy = z.deep_copy()
    d = flex.int((m_real,2,m_real,m_real,m_real))
    ccp4io_dev_ext.fftlib_realft(xy=realft_xy, n=n_cmpl, d=d)
    fft = scitbx.fftpack.real_to_complex(n_real)
    assert fft.m_real() == m_real
    fftpack_xy = z.as_double()
    fft.forward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float()
    if (flex.max_absolute(realft_xy-fftpack_xy) > 1e-5):
      assert approx_equal(realft_xy, fftpack_xy, eps=1e-5)
Ejemplo n.º 9
0
def compare_fftpack_with_cmplft_3d():
  mt = flex.mersenne_twister(seed=0)
  for nx,ny,nz in [(30,20,40), (7,19,13), (5,11,4)]:
    z = (mt.random_double(size=2*nx*ny*nz)*2-1).as_float()
    cmplft_xy = z.deep_copy()
    d = flex.int([2*nx*ny*nz, 2*nx*ny, 2*nx*ny*nz, 2*nx*ny, 2])
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=nz, d=d)
    d = flex.int([2*nx*ny*nz, 2*nx, 2*nx*ny, 2*nx, 2])
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=ny, d=d)
    d = flex.int([2*nx*ny*nz, 2, 2*nx*ny*nz, 2*nx*ny*nz, 2*nx])
    ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=nx, d=d)
    fft = scitbx.fftpack.complex_to_complex_3d((nz,ny,nx))
    fftpack_xy = z.as_double()
    fftpack_xy.reshape(flex.grid(nz,ny,2*nx))
    fft.forward(fftpack_xy)
    fftpack_xy = fftpack_xy.as_float().as_1d()
    if (flex.max_absolute(cmplft_xy-fftpack_xy) > 1e-4):
      assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-4)
Ejemplo n.º 10
0
def compare_fftpack_with_realft_1d():
    mt = flex.mersenne_twister(seed=0)
    for n_cmpl in xrange(1, 101):
        primes = prime_factors_of(n_cmpl)
        if (n_cmpl != 1 and max(primes) > 19): continue
        n_real = n_cmpl * 2
        m_real = n_real + 2
        z = (mt.random_double(size=m_real) * 2 - 1).as_float()
        realft_xy = z.deep_copy()
        d = flex.int((m_real, 2, m_real, m_real, m_real))
        ccp4io_dev_ext.fftlib_realft(xy=realft_xy, n=n_cmpl, d=d)
        fft = scitbx.fftpack.real_to_complex(n_real)
        assert fft.m_real() == m_real
        fftpack_xy = z.as_double()
        fft.forward(fftpack_xy)
        fftpack_xy = fftpack_xy.as_float()
        if (flex.max_absolute(realft_xy - fftpack_xy) > 1e-5):
            assert approx_equal(realft_xy, fftpack_xy, eps=1e-5)
Ejemplo n.º 11
0
def exercise_fftlib_real_complex_3d_real_imag_w_given_dims(
      mt, nu, nv, nw, verbose):
  map_size = nu * nv * (nw+2)
  map = (mt.random_double(size=map_size)*2-1).as_float()
  x = map.deep_copy()
  ccp4io_dev_ext.fftlib_real_to_complex_3d_real_imag_w(
    x=x, nu=nu, nv=nv, nw=nw, complete=True)
  f = x.deep_copy()
  ccp4io_dev_ext.hermitian_conjugate_3d_real_imag_w(x=x, nu=nu, nv=nv, nw=nw)
  ccp4io_dev_ext.fftlib_complex_to_real_3d_real_imag_w(
    x=x, nu=nu, nv=nv, nw=nw, complete=True)
  x *= 1/(nu*nv*nw)
  xfocus = x[:nu*nv*nw]
  mfocus = map[:nu*nv*nw]
  if (flex.max_absolute(xfocus-mfocus) > 1e-5):
    assert approx_equal(xfocus, mfocus, eps=1e-5)
  #
  fft = scitbx.fftpack.real_to_complex_3d((nu, nv, nw))
  g = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()))
  assert g.size() == f.size()
  for u in xrange(nu):
    for v in xrange(nv):
      for w in xrange(nw+2):
        g[(u*nv+v)*(nw+2)+w] = map[(w*nv+v)*nu+u]
  fft.forward(g)
  for u in xrange(nu):
    for v in xrange(nv):
      for w in xrange(nw//2+1):
        wr = w*2
        wi = wr+1
        for wj in [wr, wi]:
          ge = g[(u*nv+v)*(nw+2)+wj]
          fe = f[(wj*nv+v)*nu+u]
          assert approx_equal(fe, ge, eps=1e-4)
  #
  y = map.deep_copy()
  ccp4io_dev_ext.fftlib_real_to_complex_3d_real_imag_w(
    x=y, nu=nu, nv=nv, nw=nw, complete=False)
  ccp4io_dev_ext.hermitian_conjugate_3d_real_imag_w(x=y, nu=nu, nv=nv, nw=nw)
  ccp4io_dev_ext.fftlib_complex_to_real_3d_real_imag_w(
    x=y, nu=nu, nv=nv, nw=nw, complete=False)
  y *= 1/(nu*nv*nw)
  show_complete_true_false_cc(nu, nv, nw, (x, y), verbose)
Ejemplo n.º 12
0
def compare_fftpack_with_cmplft_3d():
    mt = flex.mersenne_twister(seed=0)
    for nx, ny, nz in [(30, 20, 40), (7, 19, 13), (5, 11, 4)]:
        z = (mt.random_double(size=2 * nx * ny * nz) * 2 - 1).as_float()
        cmplft_xy = z.deep_copy()
        d = flex.int(
            [2 * nx * ny * nz, 2 * nx * ny, 2 * nx * ny * nz, 2 * nx * ny, 2])
        ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=nz, d=d)
        d = flex.int([2 * nx * ny * nz, 2 * nx, 2 * nx * ny, 2 * nx, 2])
        ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=ny, d=d)
        d = flex.int(
            [2 * nx * ny * nz, 2, 2 * nx * ny * nz, 2 * nx * ny * nz, 2 * nx])
        ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=nx, d=d)
        fft = scitbx.fftpack.complex_to_complex_3d((nz, ny, nx))
        fftpack_xy = z.as_double()
        fftpack_xy.reshape(flex.grid(nz, ny, 2 * nx))
        fft.forward(fftpack_xy)
        fftpack_xy = fftpack_xy.as_float().as_1d()
        if (flex.max_absolute(cmplft_xy - fftpack_xy) > 1e-4):
            assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-4)