Example #1
0
def exercise_real_to_complex_3d():
  print "real_to_complex_3d"
  for n_real,n_repeats in [((100,80,90),8),
                           ((200,160,180),2),
                           ((300,240,320),1)]:
    print "  dimensions:", n_real
    print "  repeats:", n_repeats
    fft = fftpack.real_to_complex_3d(n_real)
    m_real = fft.m_real()
    np = n_real[0]*n_real[1]*n_real[2]
    mp = m_real[0]*m_real[1]*m_real[2]
    d0 = flex.random_double(size=mp)*2-1
    d0.reshape(flex.grid(m_real).set_focus(n_real))
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
    overhead = time.time()-t0
    print "    overhead: %.2f seconds" % overhead
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      c = fftw3tbx.real_to_complex_3d_in_place(data=d)
      assert c.all() == fft.n_complex()
      assert c.focus() == fft.n_complex()
      assert c.id() == d.id()
      r = fftw3tbx.complex_to_real_3d_in_place(data=c, n=n_real)
      assert r.all() == fft.m_real()
      assert r.focus() == fft.n_real()
      assert r.id() == d.id()
    print "    fftw:     %.2f seconds" % (time.time()-t0-overhead)
    if (maptbx is not None):
      maptbx.unpad_in_place(map=d)
      rw = d / np
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      c = fftpack.real_to_complex_3d(n_real).forward(d)
      assert c.all() == fft.n_complex()
      assert c.focus() == fft.n_complex()
      assert c.id() == d.id()
      r = fftpack.real_to_complex_3d(n_real).backward(c)
      assert r.all() == fft.m_real()
      assert r.focus() == fft.n_real()
      assert r.id() == d.id()
    print "    fftpack:  %.2f seconds" % (time.time()-t0-overhead)
    sys.stdout.flush()
    if (maptbx is not None):
      maptbx.unpad_in_place(map=d)
      rp = d / np
      #
      assert flex.max(flex.abs(rw-rp)) < 1.e-6
Example #2
0
def one_series(num_threads, n_iterations, quick=False):
  if (num_threads > 0):
    omptbx.env.num_threads = num_threads
  print "working omptbx.env.num_threads:", omptbx.env.num_threads
  use_wall_clock = (omptbx.env.num_threads > 1)
  print "use_wall_clock:", use_wall_clock
  #
  if (quick):
    dims = (2*3, 3*4, 4*5)
  else:
    dims = (2*3*5*7,3*4*5*7,3*4*5*5)
  rfft = fftpack.real_to_complex_3d(dims)
  print "rfft.m_real():", rfft.m_real()
  #
  t_map = time_log(label="map", use_wall_clock=use_wall_clock)
  t_fill = time_log(label="fill", use_wall_clock=use_wall_clock)
  t_fft = time_log(label="fft", use_wall_clock=use_wall_clock)
  print t_map.legend
  sys.stdout.flush()
  for i_iteration in xrange(n_iterations):
    t_map.start()
    map = fftpack.zeros_parallel_double(flex_grid=flex.grid(rfft.m_real()))
    print t_map.log()
    sys.stdout.flush()
    t_fill.start()
    for i in xrange(0, map.size(), 97):
      map[i] = random.random()
    print t_fill.log()
    sys.stdout.flush()
    t_fft.start()
    map = rfft.forward(map)
    print t_fft.log()
    sys.stdout.flush()
def direct_space_squaring(start, selection_fixed):
    map_gridding = miller.index_span(
        miller.set.expand_to_p1(start).indices()).map_grid()
    if (selection_fixed is None):
        fixed = start
        var = start
    else:
        fixed = start.select(selection_fixed)
        var = start.select(~selection_fixed)
    rfft = fftpack.real_to_complex_3d([n * 3 // 2 for n in map_gridding])
    conjugate_flag = True
    structure_factor_map = maptbx.structure_factors.to_map(
        space_group=fixed.space_group(),
        anomalous_flag=fixed.anomalous_flag(),
        miller_indices=fixed.indices(),
        structure_factors=fixed.data(),
        n_real=rfft.n_real(),
        map_grid=flex.grid(rfft.n_complex()),
        conjugate_flag=conjugate_flag)
    real_map = rfft.backward(structure_factor_map.complex_map())
    squared_map = flex.pow2(real_map)
    squared_sf_map = rfft.forward(squared_map)
    allow_miller_indices_outside_map = False
    from_map = maptbx.structure_factors.from_map(
        anomalous_flag=var.anomalous_flag(),
        miller_indices=var.indices(),
        complex_map=squared_sf_map,
        conjugate_flag=conjugate_flag,
        allow_miller_indices_outside_map=allow_miller_indices_outside_map)
    if (selection_fixed is None):
        return from_map.data()
    result = start.data().deep_copy()
    result.set_selected(~selection_fixed, from_map.data())
    assert result.select(selection_fixed).all_eq(fixed.data())
    return result
def run(args):
  assert len(args) == 1
  timer = time_log("pdb.input").start()
  pdb_inp = iotbx.pdb.input(file_name=args[0])
  print "number of pdb atoms:", pdb_inp.atoms().size()
  print timer.log()
  crystal_symmetry = pdb_inp.crystal_symmetry()
  assert crystal_symmetry is not None
  crystal_symmetry.show_summary()
  assert crystal_symmetry.unit_cell() is not None
  assert crystal_symmetry.space_group_info() is not None
  sites_cart = pdb_inp.atoms().extract_xyz()
  site_radii = flex.double(sites_cart.size(), 2.5)
  crystal_gridding = maptbx.crystal_gridding(
    unit_cell=crystal_symmetry.unit_cell(),
    d_min=2,
    resolution_factor=1/3)
  fft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
  print "n_real:", fft.n_real()
  print "m_real:", fft.m_real()
  timer = time_log("grid_indices_around_sites").start()
  grid_indices = maptbx.grid_indices_around_sites(
    unit_cell=crystal_symmetry.unit_cell(),
    fft_n_real=fft.n_real(),
    fft_m_real=fft.m_real(),
    sites_cart=sites_cart,
    site_radii=site_radii)
  print "grid_indices.size():", grid_indices.size()
  print timer.log()
  print "grid fraction:", \
    grid_indices.size() / matrix.col(fft.n_real()).product()
def exercise_f000():
  miller_indices = flex.miller_index([(0,0,0)])
  data = flex.complex_double([1-2j])
  n_real = [1,2,3]
  conjugate_flag = True
  for hall_symbol in ["P 1", "P 3", "R 3*"]:
    for is_centric in [False, True]:
      if (not is_centric):
        space_group = sgtbx.space_group(hall_symbol)
      else:
        space_group.expand_smx("-x,-y,-z")
      for anomalous_flag in [False, True]:
        if (not anomalous_flag):
          rfft = fftpack.real_to_complex_3d(n_real)
          n_complex = rfft.n_complex()
        else:
          cfft = fftpack.complex_to_complex_3d(n_real)
          n_complex = cfft.n()
        for treat_restricted in [False, True]:
          map = maptbx.structure_factors.to_map(
            space_group=space_group,
            anomalous_flag=anomalous_flag,
            miller_indices=miller_indices,
            structure_factors=data,
            n_real=n_real,
            map_grid=flex.grid(n_complex),
            conjugate_flag=conjugate_flag,
            treat_restricted=treat_restricted)
          if (treat_restricted):
            assert approx_equal(
              map.complex_map()[0], data[0])
          else:
            assert approx_equal(
              map.complex_map()[0], data[0]*space_group.order_p())
def exercise_f000():
    miller_indices = flex.miller_index([(0, 0, 0)])
    data = flex.complex_double([1 - 2j])
    n_real = [1, 2, 3]
    conjugate_flag = True
    for hall_symbol in ["P 1", "P 3", "R 3*"]:
        for is_centric in [False, True]:
            if (not is_centric):
                space_group = sgtbx.space_group(hall_symbol)
            else:
                space_group.expand_smx("-x,-y,-z")
            for anomalous_flag in [False, True]:
                if (not anomalous_flag):
                    rfft = fftpack.real_to_complex_3d(n_real)
                    n_complex = rfft.n_complex()
                else:
                    cfft = fftpack.complex_to_complex_3d(n_real)
                    n_complex = cfft.n()
                for treat_restricted in [False, True]:
                    map = maptbx.structure_factors.to_map(
                        space_group=space_group,
                        anomalous_flag=anomalous_flag,
                        miller_indices=miller_indices,
                        structure_factors=data,
                        n_real=n_real,
                        map_grid=flex.grid(n_complex),
                        conjugate_flag=conjugate_flag,
                        treat_restricted=treat_restricted)
                    if (treat_restricted):
                        assert approx_equal(map.complex_map()[0], data[0])
                    else:
                        assert approx_equal(map.complex_map()[0],
                                            data[0] * space_group.order_p())
Example #7
0
def test_real_to_complex_3d(verbose):
  for nx in [3,4]:
    for ny in [4,5]:
      for nz in [7,8]:
        fft = fftpack.real_to_complex_3d((nx,ny,nz))
        m = fft.m_real()
        vd = flex.double(flex.grid(m))
        vc = flex.complex_double(flex.grid((m[0], m[1], m[2]//2)))
        assert vd.size() == 2 * vc.size()
        for i in xrange(vd.size()):
          vd[i] = random.random()*2-1
        vd_orig = vd.deep_copy()
        for i in xrange(vc.size()):
          vc[i] = complex(vd[2*i], vd[2*i+1])
        vdt = fft.forward(vd)
        vct = fft.forward(vc)
        for t in (vdt, vct):
          assert t.origin() == (0,0,0)
          assert t.all() == fft.n_complex()
          assert t.focus() == fft.n_complex()
        if (verbose): show_rseq_3d(vd, fft.m_real(), fft.m_real())
        assert_complex_eq_real(vc, vd)
        vdt = fft.backward(vd)
        vct = fft.backward(vc)
        for t in (vdt, vct):
          assert t.origin() == (0,0,0)
          assert t.all() == fft.m_real()
          assert t.focus() == fft.n_real()
        if (verbose): show_rseq_3d(vd, fft.m_real(), fft.n_real())
        assert_complex_eq_real(vc, vd)
        f = nx*ny*nz
        for ix in xrange(nx):
          for iy in xrange(ny):
            for iz in xrange(nz):
              assert approx_equal(vdt[(ix,iy,iz)], vd_orig[(ix,iy,iz)]*f)
Example #8
0
def run(args):
    assert len(args) == 1
    timer = time_log("pdb.input").start()
    pdb_inp = iotbx.pdb.input(file_name=args[0])
    print("number of pdb atoms:", pdb_inp.atoms().size())
    print(timer.log())
    crystal_symmetry = pdb_inp.crystal_symmetry()
    assert crystal_symmetry is not None
    crystal_symmetry.show_summary()
    assert crystal_symmetry.unit_cell() is not None
    assert crystal_symmetry.space_group_info() is not None
    sites_cart = pdb_inp.atoms().extract_xyz()
    site_radii = flex.double(sites_cart.size(), 2.5)
    crystal_gridding = maptbx.crystal_gridding(
        unit_cell=crystal_symmetry.unit_cell(),
        d_min=2,
        resolution_factor=1 / 3)
    fft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
    print("n_real:", fft.n_real())
    print("m_real:", fft.m_real())
    timer = time_log("grid_indices_around_sites").start()
    grid_indices = maptbx.grid_indices_around_sites(
        unit_cell=crystal_symmetry.unit_cell(),
        fft_n_real=fft.n_real(),
        fft_m_real=fft.m_real(),
        sites_cart=sites_cart,
        site_radii=site_radii)
    print("grid_indices.size():", grid_indices.size())
    print(timer.log())
    print("grid fraction:", \
      grid_indices.size() / matrix.col(fft.n_real()).product())
def map_of_coeff_scaled(mask_map,structure,nxyz):
     assert mask_map.is_0_based()
     assert not mask_map.is_padded()
     fft_manager = fftpack.real_to_complex_3d(mask_map.focus())
     padded_data = maptbx.copy(mask_map,
                               flex.grid(fft_manager.m_real()
                              ).set_focus(fft_manager.n_real()))
     map_of_coeff = fft_manager.forward(padded_data)
     scale = matrix.col(nxyz).product()/structure.unit_cell().volume()
     sc = matrix.col(mask_map.focus()).product()/structure.unit_cell().volume()
     assert sc == scale
     map_of_coeff /= scale
     return map_of_coeff
Example #10
0
def map_of_coeff_scaled(mask_map, structure, nxyz):
    assert mask_map.is_0_based()
    assert not mask_map.is_padded()
    fft_manager = fftpack.real_to_complex_3d(mask_map.focus())
    padded_data = maptbx.copy(
        mask_map,
        flex.grid(fft_manager.m_real()).set_focus(fft_manager.n_real()))
    map_of_coeff = fft_manager.forward(padded_data)
    scale = matrix.col(nxyz).product() / structure.unit_cell().volume()
    sc = matrix.col(
        mask_map.focus()).product() / structure.unit_cell().volume()
    assert sc == scale
    map_of_coeff /= scale
    return map_of_coeff
def direct_space_squaring(start, selection_fixed):
    map_gridding = miller.index_span(miller.set.expand_to_p1(start).indices()).map_grid()
    if selection_fixed is None:
        fixed = start
        var = start
    else:
        fixed = start.select(selection_fixed)
        var = start.select(~selection_fixed)
    rfft = fftpack.real_to_complex_3d([n * 3 // 2 for n in map_gridding])
    conjugate_flag = True
    structure_factor_map = maptbx.structure_factors.to_map(
        space_group=fixed.space_group(),
        anomalous_flag=fixed.anomalous_flag(),
        miller_indices=fixed.indices(),
        structure_factors=fixed.data(),
        n_real=rfft.n_real(),
        map_grid=flex.grid(rfft.n_complex()),
        conjugate_flag=conjugate_flag,
    )
    real_map = rfft.backward(structure_factor_map.complex_map())
    squared_map = flex.pow2(real_map)
    squared_sf_map = rfft.forward(squared_map)
    allow_miller_indices_outside_map = False
    from_map = maptbx.structure_factors.from_map(
        anomalous_flag=var.anomalous_flag(),
        miller_indices=var.indices(),
        complex_map=squared_sf_map,
        conjugate_flag=conjugate_flag,
        allow_miller_indices_outside_map=allow_miller_indices_outside_map,
    )
    if selection_fixed is None:
        return from_map.data()
    result = start.data().deep_copy()
    result.set_selected(~selection_fixed, from_map.data())
    assert result.select(selection_fixed).all_eq(fixed.data())
    return result
Example #12
0
def test_real_to_complex_3d(verbose):
    for nx in [3, 4]:
        for ny in [4, 5]:
            for nz in [7, 8]:
                fft = fftpack.real_to_complex_3d((nx, ny, nz))
                m = fft.m_real()
                vd = flex.double(flex.grid(m))
                vc = flex.complex_double(flex.grid((m[0], m[1], m[2] // 2)))
                assert vd.size() == 2 * vc.size()
                for i in range(vd.size()):
                    vd[i] = random.random() * 2 - 1
                vd_orig = vd.deep_copy()
                for i in range(vc.size()):
                    vc[i] = complex(vd[2 * i], vd[2 * i + 1])
                vdt = fft.forward(vd)
                vct = fft.forward(vc)
                for t in (vdt, vct):
                    assert t.origin() == (0, 0, 0)
                    assert t.all() == fft.n_complex()
                    assert t.focus() == fft.n_complex()
                if (verbose): show_rseq_3d(vd, fft.m_real(), fft.m_real())
                assert_complex_eq_real(vc, vd)
                vdt = fft.backward(vd)
                vct = fft.backward(vc)
                for t in (vdt, vct):
                    assert t.origin() == (0, 0, 0)
                    assert t.all() == fft.m_real()
                    assert t.focus() == fft.n_real()
                if (verbose): show_rseq_3d(vd, fft.m_real(), fft.n_real())
                assert_complex_eq_real(vc, vd)
                f = nx * ny * nz
                for ix in range(nx):
                    for iy in range(ny):
                        for iz in range(nz):
                            assert approx_equal(vdt[(ix, iy, iz)],
                                                vd_orig[(ix, iy, iz)] * f)
Example #13
0
def _exercise_real_to_complex_3d(sizes, benchmark=True):
    from cctbx import maptbx
    from scitbx.array_family import flex
    from cudatbx import cufft
    from scitbx import fftpack
    from libtbx.test_utils import approx_equal
    import time
    for n_real, n_repeats, eps in sizes:
        nx, ny, nz = n_real
        fft = fftpack.real_to_complex_3d((nx, ny, nz))
        mt = flex.mersenne_twister(seed=1)
        g = flex.grid(fft.m_real()).set_focus(fft.n_real())
        map = mt.random_double(size=g.size_1d())
        map.reshape(g)
        sfs = fft.forward(map.deep_copy())
        map2 = fft.backward(sfs)
        fft_cuda = cufft.real_to_complex_3d((nx, ny, nz))
        sfs_cuda = fft_cuda.forward(
            map)  #cufft.real_to_complex_3d_in_place(map)
        map2_cuda = fft_cuda.backward(
            sfs_cuda)  #cufft.complex_to_real_3d_in_place(sfs_cuda, n_real)
        maptbx.unpad_in_place(map=map2)
        maptbx.unpad_in_place(map=map2_cuda)
        map2_values = map2.as_1d()
        map2_cuda_values = map2_cuda.as_1d()
        mmm = map2_values.min_max_mean()
        mmm_cuda = map2_cuda_values.min_max_mean()
        assert (map2.size() == map2_cuda.size())
        assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
        assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
        assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
        if (benchmark):
            map_bak = map.deep_copy()
            r2c = [fft.forward, fft_cuda.forward]
            c2r = [fft.backward, fft_cuda.backward]
            modules = ["fftpack:", "cufft:  "]
            last_real = [None, None]
            print "  dimensions:", n_real
            print "  repeats:", n_repeats
            k = 0
            for (r2c_fn, c2r_fn, name) in zip(r2c, c2r, modules):
                t_forward = 0
                t_backward = 0
                map = map_bak.deep_copy()
                for i in range(n_repeats):
                    t1 = time.time()
                    sfs = r2c_fn(map)
                    t2 = time.time()
                    map2 = c2r_fn(sfs)
                    t3 = time.time()
                    t_forward += t2 - t1
                    t_backward += t3 - t2
                    if (i == n_repeats - 1):
                        last_real[k] = map2.deep_copy()
                k += 1
                print "    %s %7.3fs (forward)  %7.3fs (backward)" % (
                    name, t_forward / n_repeats, t_backward / n_repeats)
            last_fftpack, last_cufft = last_real
            maptbx.unpad_in_place(map=last_fftpack)
            maptbx.unpad_in_place(map=last_cufft)
            mmm = last_fftpack.as_1d().min_max_mean()
            mmm_cuda = last_cufft.as_1d().min_max_mean()
            # FIXME why doesn't this work?
            #assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
            assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
            assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
            print ""
Example #14
0
def _exercise_real_to_complex_3d (sizes, benchmark=True) :
  from cctbx import maptbx
  from scitbx.array_family import flex
  from cudatbx import cufft
  from scitbx import fftpack
  from libtbx.test_utils import approx_equal
  import time
  for n_real, n_repeats, eps in sizes :
    nx, ny, nz = n_real
    fft = fftpack.real_to_complex_3d((nx,ny,nz))
    mt = flex.mersenne_twister(seed=1)
    g = flex.grid(fft.m_real()).set_focus(fft.n_real())
    map = mt.random_double(size=g.size_1d())
    map.reshape(g)
    sfs = fft.forward(map.deep_copy())
    map2 = fft.backward(sfs)
    fft_cuda = cufft.real_to_complex_3d((nx,ny,nz))
    sfs_cuda = fft_cuda.forward(map)#cufft.real_to_complex_3d_in_place(map)
    map2_cuda = fft_cuda.backward(sfs_cuda)#cufft.complex_to_real_3d_in_place(sfs_cuda, n_real)
    maptbx.unpad_in_place(map=map2)
    maptbx.unpad_in_place(map=map2_cuda)
    map2_values = map2.as_1d()
    map2_cuda_values = map2_cuda.as_1d()
    mmm = map2_values.min_max_mean()
    mmm_cuda = map2_cuda_values.min_max_mean()
    assert (map2.size() == map2_cuda.size())
    assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
    assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
    assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
    if (benchmark) :
      map_bak = map.deep_copy()
      r2c = [ fft.forward, fft_cuda.forward ]
      c2r = [ fft.backward, fft_cuda.backward ]
      modules = ["fftpack:", "cufft:  "]
      last_real = [None, None]
      print "  dimensions:", n_real
      print "  repeats:", n_repeats
      k = 0
      for (r2c_fn, c2r_fn, name) in zip(r2c, c2r, modules) :
        t_forward = 0
        t_backward = 0
        map = map_bak.deep_copy()
        for i in range(n_repeats) :
          t1 = time.time()
          sfs = r2c_fn(map)
          t2 = time.time()
          map2 = c2r_fn(sfs)
          t3 = time.time()
          t_forward += t2 - t1
          t_backward += t3 - t2
          if (i == n_repeats - 1) :
            last_real[k] = map2.deep_copy()
        k += 1
        print "    %s %7.3fs (forward)  %7.3fs (backward)" % (name,
          t_forward / n_repeats, t_backward / n_repeats)
      last_fftpack,last_cufft = last_real
      maptbx.unpad_in_place(map=last_fftpack)
      maptbx.unpad_in_place(map=last_cufft)
      mmm = last_fftpack.as_1d().min_max_mean()
      mmm_cuda = last_cufft.as_1d().min_max_mean()
      # FIXME why doesn't this work?
      #assert approx_equal(mmm.min, mmm_cuda.min, eps=eps)
      assert approx_equal(mmm.max, mmm_cuda.max, eps=eps)
      assert approx_equal(mmm.mean, mmm_cuda.mean, eps=eps)
      print ""
 def rfft(self):
   if (self._rfft is None):
     self._rfft = fftpack.real_to_complex_3d(self.crystal_gridding().n_real())
   return self._rfft
def exercise(space_group_info, redundancy_counter=0):
    n_real = (12, 12, 12)
    miller_max = (2, 2, 2)
    gt = maptbx.grid_tags(n_real)
    uc = space_group_info.any_compatible_unit_cell(volume=1000)
    fl = sgtbx.search_symmetry_flags(use_space_group_symmetry=True)
    gt.build(space_group_info.type(), fl)
    fft = fftpack.real_to_complex_3d(n_real)
    map0 = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()), 0)
    weight_map = map0.deep_copy()
    map = map0.deep_copy()
    ta = gt.tag_array()
    order_z = space_group_info.group().order_z()
    problems_expected = (redundancy_counter != 0)
    for ijk in flex.nested_loop(n_real):
        t = ta[ijk]
        if (t < 0):
            xyz = [i / n for i, n in zip(ijk, n_real)]
            ss = sgtbx.site_symmetry(unit_cell=uc,
                                     space_group=space_group_info.group(),
                                     original_site=xyz,
                                     min_distance_sym_equiv=1e-5)
            m = space_group_info.group().multiplicity(
                site=boost.rational.vector(ijk, n_real))
            assert m == ss.multiplicity()
            w = m / order_z
            weight_map[ijk] = w
            map[ijk] = w
        elif (redundancy_counter != 0):
            redundancy_counter -= 1
            ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real)
            assert ta.accessor()(ijk_asu) == t
            map[ijk] = map[ijk_asu]
    sf_map = fft.forward(map)
    del map
    mi = miller.index_generator(space_group_info.type(), False,
                                miller_max).to_array()
    assert mi.size() != 0
    from_map = maptbx.structure_factors.from_map(
        space_group=space_group_info.group(),
        anomalous_flag=False,
        miller_indices=mi,
        complex_map=sf_map,
        conjugate_flag=True)
    sf = [iround(abs(f)) for f in from_map.data()]
    if (sf != [0] * len(sf)):
        assert problems_expected
        return
    else:
        not problems_expected
    #
    map_p1 = map0.deep_copy()
    map_sw = map0.deep_copy()
    for ijk in flex.nested_loop(n_real):
        t = ta[ijk]
        if (t < 0):
            v = random.random() * 2 - 1
            map_p1[ijk] = v
            map_sw[ijk] = v * weight_map[ijk]
        else:
            ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real)
            assert ta.accessor()(ijk_asu) == t
            assert map_p1[ijk_asu] != 0
            map_p1[ijk] = map_p1[ijk_asu]
    #
    # fft followed by symmetry summation in reciprocal space
    sf_map_sw = fft.forward(map_sw)
    del map_sw
    sf_sw = maptbx.structure_factors.from_map(
        space_group=space_group_info.group(),
        anomalous_flag=False,
        miller_indices=mi,
        complex_map=sf_map_sw,
        conjugate_flag=True).data()
    del sf_map_sw
    #
    # symmetry expansion in real space (done above already) followed fft
    sf_map_p1 = fft.forward(map_p1)
    del map_p1
    sf_p1 = maptbx.structure_factors.from_map(space_group=sgtbx.space_group(),
                                              anomalous_flag=False,
                                              miller_indices=mi,
                                              complex_map=sf_map_p1,
                                              conjugate_flag=True).data()
    del sf_map_p1
    #
    corr = flex.linear_correlation(x=flex.abs(sf_sw), y=flex.abs(sf_p1))
    assert corr.is_well_defined
    assert approx_equal(corr.coefficient(), 1)
Example #17
0
def exercise(space_group_info,
             const_gaussian,
             negative_gaussian,
             anomalous_flag,
             allow_mix,
             use_u_iso,
             use_u_aniso,
             d_min=1.,
             resolution_factor=1. / 3,
             max_prime=5,
             quality_factor=100,
             wing_cutoff=1.e-6,
             exp_table_one_over_step_size=-100,
             force_complex=False,
             verbose=0):
    if (const_gaussian):
        elements = ["const"] * 8
    elif (negative_gaussian):
        elements = ["H"] * 8
    else:
        elements = ["N", "C", "C", "O", "N", "C", "C", "O"]
    if (random.random() < 0.5):
        random_f_prime_scale = 0.6
    else:
        random_f_prime_scale = 0
    structure = random_structure.xray_structure(
        space_group_info,
        elements=elements,
        random_f_prime_d_min=1,
        random_f_prime_scale=random_f_prime_scale,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        use_u_iso=False,
        random_u_cart_scale=0.3,
        random_u_iso=True,
        random_occupancy=True)
    random_structure.random_modify_adp_and_adp_flags_2(
        scatterers=structure.scatterers(),
        use_u_iso=use_u_iso,
        use_u_aniso=use_u_aniso,
        allow_mix=allow_mix,
        random_u_iso_scale=0.3,
        random_u_iso_min=0.0)
    sampled_density_must_be_positive = True
    if (negative_gaussian):
        reg = structure.scattering_type_registry(
            custom_dict={"H": eltbx.xray_scattering.gaussian(-1)})
        assert reg.gaussian("H").n_terms() == 0
        assert reg.gaussian("H").c() == -1
        sampled_density_must_be_positive = False
    elif (not const_gaussian and random.random() < 0.5):
        if (random.random() < 0.5):
            sampled_density_must_be_positive = False
        assign_custom_gaussians(
            structure, negative_a=not sampled_density_must_be_positive)
    f_direct = structure.structure_factors(anomalous_flag=anomalous_flag,
                                           d_min=d_min,
                                           algorithm="direct").f_calc()
    crystal_gridding = f_direct.crystal_gridding(
        resolution_factor=resolution_factor, d_min=d_min, max_prime=max_prime)
    assert crystal_gridding.symmetry_flags() is None
    rfft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
    u_base = xray.calc_u_base(d_min, resolution_factor, quality_factor)
    omptbx.env.num_threads = libtbx.introspection.number_of_processors()
    sampled_density = xray.sampled_model_density(
        unit_cell=structure.unit_cell(),
        scatterers=structure.scatterers(),
        scattering_type_registry=structure.scattering_type_registry(),
        fft_n_real=rfft.n_real(),
        fft_m_real=rfft.m_real(),
        u_base=u_base,
        wing_cutoff=wing_cutoff,
        exp_table_one_over_step_size=exp_table_one_over_step_size,
        force_complex=force_complex,
        sampled_density_must_be_positive=sampled_density_must_be_positive,
        tolerance_positive_definite=1.e-5,
        use_u_base_as_u_extra=False)
    focus = sampled_density.real_map_unpadded().focus()
    all = sampled_density.real_map_unpadded().all()
    last = sampled_density.real_map_unpadded().last()
    assert approx_equal(focus, last)
    assert approx_equal(all, last)
    assert sampled_density.anomalous_flag() == (anomalous_flag
                                                or force_complex)
    if (0 or verbose):
        print("const_gaussian:", const_gaussian)
        print("negative_gaussian:", negative_gaussian)
        print("number of scatterers passed:", \
          sampled_density.n_scatterers_passed())
        print("number of contributing scatterers:", \
          sampled_density.n_contributing_scatterers())
        print("number of anomalous scatterers:", \
          sampled_density.n_anomalous_scatterers())
        print("wing_cutoff:", sampled_density.wing_cutoff())
        print("exp_table_one_over_step_size:", \
          sampled_density.exp_table_one_over_step_size())
        print("exp_table_size:", sampled_density.exp_table_size())
        print("max_sampling_box_edges:",
              sampled_density.max_sampling_box_edges(),
              end=' ')
        print("(%.4f, %.4f, %.4f)" %
              sampled_density.max_sampling_box_edges_frac())
        if (not sampled_density.anomalous_flag()):
            print("map min:", flex.min(sampled_density.real_map()))
            print("map max:", flex.max(sampled_density.real_map()))
        else:
            print("map min:",
                  flex.min(flex.real(sampled_density.complex_map())),
                  end=' ')
            print(flex.min(flex.imag(sampled_density.complex_map())))
            print("map max:",
                  flex.max(flex.real(sampled_density.complex_map())),
                  end=' ')
            print(flex.max(flex.imag(sampled_density.complex_map())))
    if (not sampled_density.anomalous_flag() and negative_gaussian):
        assert flex.min(sampled_density.real_map()) < 0
        assert flex.max(sampled_density.real_map()) == 0
    if (not sampled_density.anomalous_flag()):
        map = sampled_density.real_map()
        assert map.all() == rfft.m_real()
        assert map.focus() == rfft.n_real()
        sf_map = rfft.forward(map)
        assert sf_map.all() == rfft.n_complex()
        assert sf_map.focus() == rfft.n_complex()
        collect_conj = True
    else:
        cfft = fftpack.complex_to_complex_3d(rfft.n_real())
        map = sampled_density.complex_map()
        assert map.all() == cfft.n()
        assert map.focus() == cfft.n()
        sf_map = cfft.backward(map)
        assert sf_map.all() == cfft.n()
        assert sf_map.focus() == cfft.n()
        collect_conj = False
    f_fft_data = maptbx.structure_factors.from_map(
        space_group=f_direct.space_group(),
        anomalous_flag=sampled_density.anomalous_flag(),
        miller_indices=f_direct.indices(),
        complex_map=sf_map,
        conjugate_flag=collect_conj).data()
    sampled_density.eliminate_u_extra_and_normalize(f_direct.indices(),
                                                    f_fft_data)
    structure_factor_utils.check_correlation("direct/fft_regression",
                                             f_direct.indices(),
                                             0,
                                             f_direct.data(),
                                             f_fft_data,
                                             min_corr_ampl=1 * 0.99,
                                             max_mean_w_phase_error=1 * 3.,
                                             verbose=verbose)
    f_fft = xray.structure_factors.from_scatterers(
        miller_set=f_direct,
        grid_resolution_factor=resolution_factor,
        quality_factor=quality_factor,
        wing_cutoff=wing_cutoff,
        exp_table_one_over_step_size=exp_table_one_over_step_size,
        sampled_density_must_be_positive=sampled_density_must_be_positive,
        max_prime=max_prime)(xray_structure=structure,
                             miller_set=f_direct,
                             algorithm="fft").f_calc()
    structure_factor_utils.check_correlation("direct/fft_xray",
                                             f_direct.indices(),
                                             0,
                                             f_direct.data(),
                                             f_fft.data(),
                                             min_corr_ampl=1 * 0.99,
                                             max_mean_w_phase_error=1 * 3.,
                                             verbose=verbose)
def exercise(space_group_info, const_gaussian, negative_gaussian,
             anomalous_flag,
             allow_mix,
             use_u_iso,
             use_u_aniso,
             d_min=1., resolution_factor=1./3, max_prime=5,
             quality_factor=100, wing_cutoff=1.e-6,
             exp_table_one_over_step_size=-100,
             force_complex=False,
             verbose=0):
  if (const_gaussian):
    elements=["const"]*8
  elif (negative_gaussian):
    elements=["H"]*8
  else:
    elements=["N", "C", "C", "O", "N", "C", "C", "O"]
  if (random.random() < 0.5):
    random_f_prime_scale=0.6
  else:
    random_f_prime_scale=0
  structure = random_structure.xray_structure(
    space_group_info,
    elements=elements,
    random_f_prime_d_min=1,
    random_f_prime_scale=random_f_prime_scale,
    random_f_double_prime=anomalous_flag,
    use_u_aniso= True,
    use_u_iso= False,
    random_u_cart_scale=0.3,
    random_u_iso=True,
    random_occupancy=True)
  random_structure.random_modify_adp_and_adp_flags_2(
                                 scatterers         = structure.scatterers(),
                                 use_u_iso          = use_u_iso,
                                 use_u_aniso        = use_u_aniso,
                                 allow_mix          = allow_mix,
                                 random_u_iso_scale = 0.3,
                                 random_u_iso_min   = 0.0)
  sampled_density_must_be_positive = True
  if (negative_gaussian):
    reg = structure.scattering_type_registry(
      custom_dict={"H": eltbx.xray_scattering.gaussian(-1)})
    assert reg.gaussian("H").n_terms() == 0
    assert reg.gaussian("H").c() == -1
    sampled_density_must_be_positive = False
  elif (not const_gaussian and random.random() < 0.5):
    if (random.random() < 0.5):
      sampled_density_must_be_positive = False
    assign_custom_gaussians(
      structure,
      negative_a=not sampled_density_must_be_positive)
  f_direct = structure.structure_factors(
    anomalous_flag=anomalous_flag,
    d_min=d_min,
    algorithm="direct").f_calc()
  crystal_gridding = f_direct.crystal_gridding(
    resolution_factor=resolution_factor,
    d_min=d_min,
    max_prime=max_prime)
  assert crystal_gridding.symmetry_flags() is None
  rfft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
  u_base = xray.calc_u_base(d_min, resolution_factor, quality_factor)
  omptbx.env.num_threads = libtbx.introspection.number_of_processors()
  sampled_density = xray.sampled_model_density(
    unit_cell=structure.unit_cell(),
    scatterers=structure.scatterers(),
    scattering_type_registry=structure.scattering_type_registry(),
    fft_n_real=rfft.n_real(),
    fft_m_real=rfft.m_real(),
    u_base=u_base,
    wing_cutoff=wing_cutoff,
    exp_table_one_over_step_size=exp_table_one_over_step_size,
    force_complex=force_complex,
    sampled_density_must_be_positive=sampled_density_must_be_positive,
    tolerance_positive_definite=1.e-5,
    use_u_base_as_u_extra=False)
  focus = sampled_density.real_map_unpadded().focus()
  all   = sampled_density.real_map_unpadded().all()
  last  = sampled_density.real_map_unpadded().last()
  assert approx_equal(focus, last)
  assert approx_equal(all, last)
  assert sampled_density.anomalous_flag() == (anomalous_flag or force_complex)
  if (0 or verbose):
    print "const_gaussian:", const_gaussian
    print "negative_gaussian:", negative_gaussian
    print "number of scatterers passed:", \
      sampled_density.n_scatterers_passed()
    print "number of contributing scatterers:", \
      sampled_density.n_contributing_scatterers()
    print "number of anomalous scatterers:", \
      sampled_density.n_anomalous_scatterers()
    print "wing_cutoff:", sampled_density.wing_cutoff()
    print "exp_table_one_over_step_size:", \
      sampled_density.exp_table_one_over_step_size()
    print "exp_table_size:", sampled_density.exp_table_size()
    print "max_sampling_box_edges:", sampled_density.max_sampling_box_edges(),
    print "(%.4f, %.4f, %.4f)" % sampled_density.max_sampling_box_edges_frac()
    if (not sampled_density.anomalous_flag()):
      print "map min:", flex.min(sampled_density.real_map())
      print "map max:", flex.max(sampled_density.real_map())
    else:
      print "map min:", flex.min(flex.real(sampled_density.complex_map())),
      print             flex.min(flex.imag(sampled_density.complex_map()))
      print "map max:", flex.max(flex.real(sampled_density.complex_map())),
      print             flex.max(flex.imag(sampled_density.complex_map()))
  if (not sampled_density.anomalous_flag() and negative_gaussian):
    assert flex.min(sampled_density.real_map()) < 0
    assert flex.max(sampled_density.real_map()) == 0
  if (not sampled_density.anomalous_flag()):
    map = sampled_density.real_map()
    assert map.all() == rfft.m_real()
    assert map.focus() == rfft.n_real()
    sf_map = rfft.forward(map)
    assert sf_map.all() == rfft.n_complex()
    assert sf_map.focus() == rfft.n_complex()
    collect_conj = True
  else:
    cfft = fftpack.complex_to_complex_3d(rfft.n_real())
    map = sampled_density.complex_map()
    assert map.all() == cfft.n()
    assert map.focus() == cfft.n()
    sf_map = cfft.backward(map)
    assert sf_map.all() == cfft.n()
    assert sf_map.focus() == cfft.n()
    collect_conj = False
  f_fft_data = maptbx.structure_factors.from_map(
    space_group=f_direct.space_group(),
    anomalous_flag=sampled_density.anomalous_flag(),
    miller_indices=f_direct.indices(),
    complex_map=sf_map,
    conjugate_flag=collect_conj).data()
  sampled_density.eliminate_u_extra_and_normalize(
    f_direct.indices(),
    f_fft_data)
  structure_factor_utils.check_correlation(
    "direct/fft_regression", f_direct.indices(), 0,
    f_direct.data(), f_fft_data,
    min_corr_ampl=1*0.99, max_mean_w_phase_error=1*3.,
    verbose=verbose)
  f_fft = xray.structure_factors.from_scatterers(
    miller_set=f_direct,
    grid_resolution_factor=resolution_factor,
    quality_factor=quality_factor,
    wing_cutoff=wing_cutoff,
    exp_table_one_over_step_size=exp_table_one_over_step_size,
    sampled_density_must_be_positive=sampled_density_must_be_positive,
    max_prime=max_prime)(
      xray_structure=structure,
      miller_set=f_direct,
      algorithm="fft").f_calc()
  structure_factor_utils.check_correlation(
    "direct/fft_xray", f_direct.indices(), 0,
    f_direct.data(), f_fft.data(),
    min_corr_ampl=1*0.99, max_mean_w_phase_error=1*3.,
    verbose=verbose)
def exercise_shannon_sampled(space_group_info,
                             anomalous_flag,
                             conjugate_flag,
                             d_min=3.,
                             resolution_factor=0.5,
                             max_prime=5,
                             verbose=0):
    structure = random_structure.xray_structure(
        space_group_info,
        elements=("N", "C", "C", "O"),
        random_f_prime_d_min=1,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        random_u_iso=True,
        random_occupancy=True)
    f_calc = structure.structure_factors(anomalous_flag=anomalous_flag,
                                         d_min=d_min,
                                         algorithm="direct").f_calc()
    n_real = f_calc.crystal_gridding(resolution_factor=resolution_factor,
                                     d_min=d_min,
                                     max_prime=max_prime).n_real()
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real)
        n_complex = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real)
        n_complex = cfft.n()
    map = maptbx.structure_factors.to_map(space_group=f_calc.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc.indices(),
                                          structure_factors=f_calc.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    f_calc_p1 = f_calc.expand_to_p1()
    map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real,
        map_grid=flex.grid(n_complex),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        real_map = rfft.backward(map.complex_map())
        assert real_map.all() == rfft.m_real()
        complex_map = rfft.forward(real_map)
    else:
        real_map = cfft.backward(map.complex_map())
        assert not real_map.is_padded()
        complex_map = cfft.forward(real_map)
    complex_map /= n_real[0] * n_real[1] * n_real[2]
    assert real_map.focus() == n_real
    assert complex_map.focus() == n_complex
    from_map = maptbx.structure_factors.from_map(
        unit_cell=f_calc.unit_cell(),
        space_group_type=f_calc.space_group_info().type(),
        anomalous_flag=anomalous_flag,
        d_min=d_min,
        complex_map=complex_map,
        conjugate_flag=conjugate_flag)
    from_map = f_calc.customized_copy(indices=from_map.miller_indices(),
                                      data=from_map.data())
    lone_sets = f_calc.lone_sets(from_map)
    for lone_set in lone_sets:
        if (lone_set.indices().size() > 0):
            flex.max(lone_set.d_spacings().data() - d_min) < 1.e-5
    common_sets = f_calc.common_sets(from_map)
    assert flex.max(
        flex.abs(common_sets[0].data() - common_sets[1].data())) < 1.e-10
    from_map = maptbx.structure_factors.from_map(
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        complex_map=complex_map,
        conjugate_flag=conjugate_flag)
    assert from_map.miller_indices().size() == 0
    assert flex.max(flex.abs(f_calc.data() - from_map.data())) < 1.e-10
    structure_p1 = structure.asymmetric_unit_in_p1()
    f_calc_p1 = f_calc_p1.structure_factors_from_scatterers(
        xray_structure=structure_p1, algorithm="direct").f_calc()
    map = maptbx.structure_factors.to_map(space_group=f_calc_p1.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc_p1.indices(),
                                          structure_factors=f_calc_p1.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    from_map = maptbx.structure_factors.from_map(
        space_group=f_calc.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        complex_map=map.complex_map(),
        conjugate_flag=conjugate_flag)
    assert from_map.miller_indices().size() == 0
    assert flex.max(flex.abs(f_calc.data() - from_map.data())) < 1.e-10
def exercise_under_sampled(space_group_info, anomalous_flag, conjugate_flag,
                           under_sampling,
                           d_min=2., resolution_factor=0.5, max_prime=5,
                           verbose=0):
  structure_factors = random_structure.xray_structure(
    space_group_info,
    elements=("N", "C", "C", "O"),
    random_f_prime_d_min=1,
    random_f_double_prime=anomalous_flag,
    use_u_aniso=True,
    random_u_iso=True,
    random_occupancy=True
    ).structure_factors(
        anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct")
  f_calc = structure_factors.f_calc()
  n_real = maptbx.crystal_gridding(
    unit_cell=f_calc.unit_cell(),
    d_min=d_min,
    resolution_factor=resolution_factor,
    max_prime=max_prime,
    mandatory_factors=(under_sampling,)*3).n_real()
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real)
    n_complex = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real)
    n_complex = cfft.n()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  f_calc_p1 = f_calc.expand_to_p1()
  map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    real_map = rfft.backward(map.complex_map())
    assert real_map.all() == rfft.m_real()
  else:
    real_map = cfft.backward(map.complex_map())
    assert not real_map.is_padded()
  if (0 or verbose):
    if (not anomalous_flag):
      maptbx.statistics(real_map).show_summary()
      maptbx.statistics(real_map).show_summary()
    else:
      maptbx.statistics(flex.real(real_map)).show_summary()
      maptbx.statistics(flex.imag(real_map)).show_summary()
  n_real_under_sampled = [n//under_sampling for n in n_real]
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real_under_sampled)
    n_complex_under_sampled = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real_under_sampled)
    n_complex_under_sampled = cfft.n()
  under_sampled_map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real_under_sampled,
    map_grid=flex.grid(n_complex_under_sampled),
    conjugate_flag=conjugate_flag)
  under_sampled_map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real_under_sampled,
    map_grid=flex.grid(n_complex_under_sampled),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(under_sampled_map_p1.complex_map()
                         - under_sampled_map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    under_sampled_map_before_fft = under_sampled_map.complex_map().deep_copy()
    under_sampled_real_map = rfft.backward(under_sampled_map.complex_map())
    assert under_sampled_real_map.all() == rfft.m_real()
  else:
    under_sampled_real_map = cfft.backward(under_sampled_map.complex_map())
    assert not under_sampled_real_map.is_padded()
  if (0 or verbose):
    if (not anomalous_flag):
      maptbx.statistics(under_sampled_real_map).show_summary()
      maptbx.statistics(under_sampled_real_map).show_summary()
    else:
      maptbx.statistics(flex.real(under_sampled_real_map)).show_summary()
      maptbx.statistics(flex.imag(under_sampled_real_map)).show_summary()
  if (0 or verbose):
    print real_map.all(), n_complex
    print under_sampled_real_map.all(), n_complex_under_sampled
  if (not anomalous_flag):
    x_source = real_map
    y_source = under_sampled_real_map
  else:
    x_source = flex.real(real_map)
    y_source = flex.real(under_sampled_real_map)
  x = flex.double()
  n = x_source.focus()
  for i in xrange(0, n[0], under_sampling):
    for j in xrange(0, n[1], under_sampling):
      for k in xrange(0, n[2], under_sampling):
        x.append(x_source[(i,j,k)])
  y = maptbx.copy(y_source, flex.grid(y_source.focus())).as_1d()
  if (0 or verbose):
    print "x:", tuple(x)
    print "y:", tuple(y)
  assert flex.max(flex.abs(x-y)) \
      < (flex.max(flex.abs(x))+flex.max(flex.abs(y)))/2*1.e-6
  if (under_sampling == 1):
    x = maptbx.copy(x_source, flex.grid(x_source.focus())).as_1d()
    c = flex.linear_correlation(x, y)
    assert c.coefficient() >= 0.9999
def exercise_under_sampled(space_group_info,
                           anomalous_flag,
                           conjugate_flag,
                           under_sampling,
                           d_min=2.,
                           resolution_factor=0.5,
                           max_prime=5,
                           verbose=0):
    structure_factors = random_structure.xray_structure(
        space_group_info,
        elements=("N", "C", "C", "O"),
        random_f_prime_d_min=1,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        random_u_iso=True,
        random_occupancy=True).structure_factors(anomalous_flag=anomalous_flag,
                                                 d_min=d_min,
                                                 algorithm="direct")
    f_calc = structure_factors.f_calc()
    n_real = maptbx.crystal_gridding(unit_cell=f_calc.unit_cell(),
                                     d_min=d_min,
                                     resolution_factor=resolution_factor,
                                     max_prime=max_prime,
                                     mandatory_factors=(under_sampling, ) *
                                     3).n_real()
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real)
        n_complex = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real)
        n_complex = cfft.n()
    map = maptbx.structure_factors.to_map(space_group=f_calc.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc.indices(),
                                          structure_factors=f_calc.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    f_calc_p1 = f_calc.expand_to_p1()
    map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real,
        map_grid=flex.grid(n_complex),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        real_map = rfft.backward(map.complex_map())
        assert real_map.all() == rfft.m_real()
    else:
        real_map = cfft.backward(map.complex_map())
        assert not real_map.is_padded()
    if (0 or verbose):
        if (not anomalous_flag):
            maptbx.statistics(real_map).show_summary()
            maptbx.statistics(real_map).show_summary()
        else:
            maptbx.statistics(flex.real(real_map)).show_summary()
            maptbx.statistics(flex.imag(real_map)).show_summary()
    n_real_under_sampled = [n // under_sampling for n in n_real]
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real_under_sampled)
        n_complex_under_sampled = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real_under_sampled)
        n_complex_under_sampled = cfft.n()
    under_sampled_map = maptbx.structure_factors.to_map(
        space_group=f_calc.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        structure_factors=f_calc.data(),
        n_real=n_real_under_sampled,
        map_grid=flex.grid(n_complex_under_sampled),
        conjugate_flag=conjugate_flag)
    under_sampled_map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real_under_sampled,
        map_grid=flex.grid(n_complex_under_sampled),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(under_sampled_map_p1.complex_map() -
                 under_sampled_map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        under_sampled_map_before_fft = under_sampled_map.complex_map(
        ).deep_copy()
        under_sampled_real_map = rfft.backward(under_sampled_map.complex_map())
        assert under_sampled_real_map.all() == rfft.m_real()
    else:
        under_sampled_real_map = cfft.backward(under_sampled_map.complex_map())
        assert not under_sampled_real_map.is_padded()
    if (0 or verbose):
        if (not anomalous_flag):
            maptbx.statistics(under_sampled_real_map).show_summary()
            maptbx.statistics(under_sampled_real_map).show_summary()
        else:
            maptbx.statistics(flex.real(under_sampled_real_map)).show_summary()
            maptbx.statistics(flex.imag(under_sampled_real_map)).show_summary()
    if (0 or verbose):
        print(real_map.all(), n_complex)
        print(under_sampled_real_map.all(), n_complex_under_sampled)
    if (not anomalous_flag):
        x_source = real_map
        y_source = under_sampled_real_map
    else:
        x_source = flex.real(real_map)
        y_source = flex.real(under_sampled_real_map)
    x = flex.double()
    n = x_source.focus()
    for i in range(0, n[0], under_sampling):
        for j in range(0, n[1], under_sampling):
            for k in range(0, n[2], under_sampling):
                x.append(x_source[(i, j, k)])
    y = maptbx.copy(y_source, flex.grid(y_source.focus())).as_1d()
    if (0 or verbose):
        print("x:", tuple(x))
        print("y:", tuple(y))
    assert flex.max(flex.abs(x-y)) \
        < (flex.max(flex.abs(x))+flex.max(flex.abs(y)))/2*1.e-6
    if (under_sampling == 1):
        x = maptbx.copy(x_source, flex.grid(x_source.focus())).as_1d()
        c = flex.linear_correlation(x, y)
        assert c.coefficient() >= 0.9999
def exercise_shannon_sampled(space_group_info, anomalous_flag, conjugate_flag,
                             d_min=3., resolution_factor=0.5, max_prime=5,
                             verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=("N", "C", "C", "O"),
    random_f_prime_d_min=1,
    random_f_double_prime=anomalous_flag,
    use_u_aniso=True,
    random_u_iso=True,
    random_occupancy=True)
  f_calc = structure.structure_factors(
    anomalous_flag=anomalous_flag,
    d_min=d_min,
    algorithm="direct").f_calc()
  n_real = f_calc.crystal_gridding(
    resolution_factor=resolution_factor,
    d_min=d_min,
    max_prime=max_prime).n_real()
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real)
    n_complex = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real)
    n_complex = cfft.n()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  f_calc_p1 = f_calc.expand_to_p1()
  map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    real_map = rfft.backward(map.complex_map())
    assert real_map.all() == rfft.m_real()
    complex_map = rfft.forward(real_map)
  else:
    real_map = cfft.backward(map.complex_map())
    assert not real_map.is_padded()
    complex_map = cfft.forward(real_map)
  complex_map /= n_real[0] * n_real[1] * n_real[2]
  assert real_map.focus() == n_real
  assert complex_map.focus() == n_complex
  from_map = maptbx.structure_factors.from_map(
    unit_cell=f_calc.unit_cell(),
    space_group_type=f_calc.space_group_info().type(),
    anomalous_flag=anomalous_flag,
    d_min=d_min,
    complex_map=complex_map,
    conjugate_flag=conjugate_flag)
  from_map = f_calc.customized_copy(
    indices=from_map.miller_indices(),
    data=from_map.data())
  lone_sets = f_calc.lone_sets(from_map)
  for lone_set in lone_sets:
    if (lone_set.indices().size() > 0):
      flex.max(lone_set.d_spacings().data()-d_min) < 1.e-5
  common_sets = f_calc.common_sets(from_map)
  assert flex.max(flex.abs(common_sets[0].data()
                         - common_sets[1].data())) < 1.e-10
  from_map = maptbx.structure_factors.from_map(
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    complex_map=complex_map,
    conjugate_flag=conjugate_flag)
  assert from_map.miller_indices().size() == 0
  assert flex.max(flex.abs(f_calc.data()-from_map.data())) < 1.e-10
  structure_p1 = structure.asymmetric_unit_in_p1()
  f_calc_p1 = f_calc_p1.structure_factors_from_scatterers(
    xray_structure=structure_p1,
    algorithm="direct").f_calc()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  from_map = maptbx.structure_factors.from_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    complex_map=map.complex_map(),
    conjugate_flag=conjugate_flag)
  assert from_map.miller_indices().size() == 0
  assert flex.max(flex.abs(f_calc.data()-from_map.data())) < 1.e-10
def exercise(space_group_info, redundancy_counter=0):
  n_real = (12,12,12)
  miller_max = (2,2,2)
  gt = maptbx.grid_tags(n_real)
  uc = space_group_info.any_compatible_unit_cell(volume=1000)
  fl = sgtbx.search_symmetry_flags(use_space_group_symmetry=True)
  gt.build(space_group_info.type(), fl)
  fft = fftpack.real_to_complex_3d(n_real)
  map0 = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()), 0)
  weight_map = map0.deep_copy()
  map = map0.deep_copy()
  ta = gt.tag_array()
  order_z = space_group_info.group().order_z()
  problems_expected = (redundancy_counter != 0)
  for ijk in flex.nested_loop(n_real):
    t = ta[ijk]
    if (t < 0):
      xyz = [i/n for i,n in zip(ijk, n_real)]
      ss = sgtbx.site_symmetry(
        unit_cell=uc,
        space_group=space_group_info.group(),
        original_site=xyz,
        min_distance_sym_equiv=1e-5)
      m = space_group_info.group().multiplicity(
        site=boost.rational.vector(ijk, n_real))
      assert m == ss.multiplicity()
      w = m / order_z
      weight_map[ijk] = w
      map[ijk] = w
    elif (redundancy_counter != 0):
      redundancy_counter -= 1
      ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real)
      assert ta.accessor()(ijk_asu) == t
      map[ijk] = map[ijk_asu]
  sf_map = fft.forward(map)
  del map
  mi = miller.index_generator(
    space_group_info.type(), False, miller_max).to_array()
  assert mi.size() != 0
  from_map = maptbx.structure_factors.from_map(
    space_group=space_group_info.group(),
    anomalous_flag=False,
    miller_indices=mi,
    complex_map=sf_map,
    conjugate_flag=True)
  sf = [iround(abs(f)) for f in from_map.data()]
  if (sf != [0]*len(sf)):
    assert problems_expected
    return
  else:
    not problems_expected
  #
  map_p1 = map0.deep_copy()
  map_sw = map0.deep_copy()
  for ijk in flex.nested_loop(n_real):
    t = ta[ijk]
    if (t < 0):
      v = random.random()*2-1
      map_p1[ijk] = v
      map_sw[ijk] = v * weight_map[ijk]
    else:
      ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real)
      assert ta.accessor()(ijk_asu) == t
      assert map_p1[ijk_asu] != 0
      map_p1[ijk] = map_p1[ijk_asu]
  #
  # fft followed by symmetry summation in reciprocal space
  sf_map_sw = fft.forward(map_sw)
  del map_sw
  sf_sw = maptbx.structure_factors.from_map(
    space_group=space_group_info.group(),
    anomalous_flag=False,
    miller_indices=mi,
    complex_map=sf_map_sw,
    conjugate_flag=True).data()
  del sf_map_sw
  #
  # symmetry expansion in real space (done above already) followed fft
  sf_map_p1 = fft.forward(map_p1)
  del map_p1
  sf_p1 = maptbx.structure_factors.from_map(
    space_group=sgtbx.space_group(),
    anomalous_flag=False,
    miller_indices=mi,
    complex_map=sf_map_p1,
    conjugate_flag=True).data()
  del sf_map_p1
  #
  corr = flex.linear_correlation(x=flex.abs(sf_sw), y=flex.abs(sf_p1))
  assert corr.is_well_defined
  assert approx_equal(corr.coefficient(), 1)
Example #24
0
  # coerce to double
  from scitbx.array_family import flex
  C3D = flex.double(mrc.data.astype(np.float64))
  # in-place resize to a good multiple of 2
  C3D.resize(flex.grid(imax,imax,imax))


  from matplotlib import pyplot as plt
  from scitbx import fftpack
  #plt.imshow(S2D)
  #plt.show()
  #from IPython import embed; embed()
  print C3D.focus()
  print C3D
  import copy
  FFT = fftpack.real_to_complex_3d((C3D.focus()[0],C3D.focus()[1],C3D.focus()[2]-2))
  c = FFT.forward(C3D)
  print c.focus()
  print c

  cum = None
  for sec in xrange((imax//4)-25,(imax//4)+25):
    print sec

    D = c[:,:,sec:sec+1]
    #D = C[:,:,sec]
    parts = D.parts()
    magnitude = flex.sqrt(parts[0]*parts[0] + parts[1]*parts[1])
    mag2d = magnitude.reshape(flex.grid(128,128))
    n = magnitude.as_numpy_array()
    n.reshape(128,128)