def d2_sites(self):
   hkl_outer = self.hkl.outer_product()
   for js,site in enumerate(self.sites):
     d2_site = flex.complex_double(flex.grid(3,3), 0)
     for op in self.ops:
       op_site = op * site
       hkl_op_site = self.hkl.dot(op_site)
       d2_op_site = cmath.exp(1j*self.hkl.dot(op_site)) * (-1) * hkl_outer
       d2_site += flex.complex_double(op.transpose() * d2_op_site * op)
     yield d2_site
 def d2_sites(self):
   hkl_outer = self.hkl.outer_product()
   for js,site in enumerate(self.sites):
     d2_site = flex.complex_double(flex.grid(3,3), 0)
     for op in self.ops:
       op_site = op * site
       hkl_op_site = self.hkl.dot(op_site)
       d2_op_site = cmath.exp(1j*self.hkl.dot(op_site)) * (-1) * hkl_outer
       d2_site += flex.complex_double(op.transpose() * d2_op_site * op)
     yield d2_site
def tst():
    nmax = 20
    max_indx = math.nlm_array(nmax).nlm().size()
    a = flex.complex_double(flex.random_double(max_indx),
                            flex.random_double(max_indx))
    b = flex.complex_double(flex.random_double(max_indx),
                            flex.random_double(max_indx))
    c_list = [a]

    good_n = good2n(nmax, c_list, b, threshold=0.8)
    print good_n
 def d_sites(self):
   result = []
   for site in self.sites:
     d_site = flex.complex_double(3, 0)
     for op in self.ops:
       op_site = op * site
       hkl_op_site = self.hkl.dot(op_site)
       d_op_site = cmath.exp(1j*self.hkl.dot(op_site)) * 1j * self.hkl
       gtmx = op.transpose()
       d_site += flex.complex_double(gtmx * d_op_site.transpose())
     result.append(d_site)
   return result
 def d_sites(self):
   result = []
   for site in self.sites:
     d_site = flex.complex_double(3, 0)
     for op in self.ops:
       op_site = op * site
       hkl_op_site = self.hkl.dot(op_site)
       d_op_site = cmath.exp(1j*self.hkl.dot(op_site)) * 1j * self.hkl
       gtmx = op.transpose()
       d_site += flex.complex_double(gtmx * d_op_site.transpose())
     result.append(d_site)
   return result
Example #6
0
def test_real_to_complex(verbose):
  fft = fftpack.real_to_complex(6)
  vd = flex.double(fft.m_real())
  vc = flex.complex_double(fft.n_complex())
  for i in xrange(fft.n_real()):
    vd[i] = 1.*i
  for i in xrange(fft.n_complex()):
    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,)
    assert t.all()[0] == fft.n_complex()
    assert t.focus()[0] == fft.n_complex()
  if (verbose): show_rseq(vd, 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,)
    assert t.all()[0] == fft.m_real()
    assert t.focus()[0] == fft.n_real()
  if (verbose): show_rseq(vd, fft.n_real())
  assert_complex_eq_real(vc, vd)

  """ The backward fft computes
  y_k = sum_{n=0}^15 x_n e^{i 2pi n/16 k}
      = sum_{n=0}^7 2 Re( x_n e^{i 2pi n/16 k} ) + x_8 cos{i k pi}
  because of the assumed Hermitian condition x_{16-i} = x_i^* (=> x_8 real).
  Only x_0, ..., x_8 need to be stored.
  """
  fft = fftpack.real_to_complex(16)
  assert fft.n_complex() == 9
  x = flex.complex_double(fft.n_complex(), 0)
  x[4] = 1 + 1.j
  y = fft.backward(x)
  for i in xrange(0, fft.n_real(), 4):
    assert tuple(y[i:i+4]) == (2, -2, -2, 2)

  x = flex.complex_double(fft.n_complex(), 0)
  x[2] = 1 + 1.j
  y = fft.backward(x)
  for i in xrange(0, fft.n_real(), 8):
    assert tuple(y[i:i+3]) == (2, 0, -2)
    assert approx_equal(y[i+3], -2*math.sqrt(2))
    assert tuple(y[i+4:i+7]) == (-2, 0, 2)
    assert approx_equal(y[i+7], 2*math.sqrt(2))

  x = flex.complex_double(fft.n_complex(), 0)
  x[8] = 1.
  y = fft.backward(x)
  for i in xrange(0, fft.n_real(), 2):
    assert tuple(y[i:i+2]) == (1, -1)
Example #7
0
def test_real_to_complex(verbose):
    fft = fftpack.real_to_complex(6)
    vd = flex.double(fft.m_real())
    vc = flex.complex_double(fft.n_complex())
    for i in range(fft.n_real()):
        vd[i] = 1. * i
    for i in range(fft.n_complex()):
        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, )
        assert t.all()[0] == fft.n_complex()
        assert t.focus()[0] == fft.n_complex()
    if (verbose): show_rseq(vd, 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, )
        assert t.all()[0] == fft.m_real()
        assert t.focus()[0] == fft.n_real()
    if (verbose): show_rseq(vd, fft.n_real())
    assert_complex_eq_real(vc, vd)
    """ The backward fft computes
  y_k = sum_{n=0}^15 x_n e^{i 2pi n/16 k}
      = sum_{n=0}^7 2 Re( x_n e^{i 2pi n/16 k} ) + x_8 cos{i k pi}
  because of the assumed Hermitian condition x_{16-i} = x_i^* (=> x_8 real).
  Only x_0, ..., x_8 need to be stored.
  """
    fft = fftpack.real_to_complex(16)
    assert fft.n_complex() == 9
    x = flex.complex_double(fft.n_complex(), 0)
    x[4] = 1 + 1.j
    y = fft.backward(x)
    for i in range(0, fft.n_real(), 4):
        assert tuple(y[i:i + 4]) == (2, -2, -2, 2)

    x = flex.complex_double(fft.n_complex(), 0)
    x[2] = 1 + 1.j
    y = fft.backward(x)
    for i in range(0, fft.n_real(), 8):
        assert tuple(y[i:i + 3]) == (2, 0, -2)
        assert approx_equal(y[i + 3], -2 * math.sqrt(2))
        assert tuple(y[i + 4:i + 7]) == (-2, 0, 2)
        assert approx_equal(y[i + 7], 2 * math.sqrt(2))

    x = flex.complex_double(fft.n_complex(), 0)
    x[8] = 1.
    y = fft.backward(x)
    for i in range(0, fft.n_real(), 2):
        assert tuple(y[i:i + 2]) == (1, -1)
Example #8
0
def test_complex_to_complex_2d(verbose):
    fft = fftpack.complex_to_complex_2d((6, 10))
    n = fft.n()
    vc = flex.complex_double(flex.grid(n))
    vd = flex.double(flex.grid((n[0], 2 * n[1])))
    for i in range(vc.size()):
        vc[i] = complex(2 * i, 2 * i + 1)
    for i in range(vd.size()):
        vd[i] = i
    vct = fft.forward(vc)
    vdt = fft.forward(vd)
    for t in (vct, vdt):
        assert t.origin() == (0, 0)
        assert t.all() == fft.n()
        assert t.focus() == fft.n()
    if (verbose): show_cseq(vc)
    assert_complex_eq_real(vc, vd)
    vct = fft.backward(vc)
    vdt = fft.backward(vd)
    for t in (vct, vdt):
        assert t.origin() == (0, 0)
        assert t.all() == fft.n()
        assert t.focus() == fft.n()
    if (verbose): show_cseq(vc)
    assert_complex_eq_real(vc, vd)
    s = vd.size() // 2
    for i in range(vd.size()):
        assert approx_equal(vd[i], s * i)
Example #9
0
def test_complex_to_complex_3d(verbose):
  fft = fftpack.complex_to_complex_3d((3,4,5))
  n = fft.n()
  vc = flex.complex_double(flex.grid(n))
  vd = flex.double(flex.grid((n[0], n[1], 2 * n[2])))
  for i in xrange(vc.size()):
    vc[i] = complex(2*i, 2*i+1)
  for i in xrange(vd.size()):
    vd[i] = i
  vct = fft.forward(vc)
  vdt = fft.forward(vd)
  for t in (vct, vdt):
    assert t.origin() == (0,0,0)
    assert t.all() == fft.n()
    assert t.focus() == fft.n()
  if (verbose): show_cseq(vc)
  assert_complex_eq_real(vc, vd)
  vct = fft.backward(vc)
  vdt = fft.backward(vd)
  for t in (vct, vdt):
    assert t.origin() == (0,0,0)
    assert t.all() == fft.n()
    assert t.focus() == fft.n()
  if (verbose): show_cseq(vc)
  assert_complex_eq_real(vc, vd)
Example #10
0
 def check_f_calc_derivs():
   eps = 1e-6
   g_fin = flex.complex_double()
   c_fin = flex.vec3_double()
   for ih in xrange(f_calc.size()):
     c_orig = f_calc[ih]
     g_fin_ab = []
     c_fin_ab = []
     for iab in [0,1]:
       fs = []
       gs = []
       for signed_eps in [eps, -eps]:
         if (iab == 0):
           f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
         else:
           f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
         trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
         fs.append(trg_eps.t)
         gs.append(trg_eps.f_calc_gradients[ih])
       g_fin_ab.append((fs[0]-fs[1])/(2*eps))
       c_fin_ab.append((gs[0]-gs[1])/(2*eps))
     g_fin.append(complex(*g_fin_ab))
     assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
     c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
     f_calc[ih] = c_orig
   for pn,f,a in zip(
         g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
     print >> log, "g fin %s:" % pn, numstr(f)
     print >> log, "  ana %s:" % pn, numstr(a)
   assert approx_equal(trg.f_calc_gradients, g_fin)
   for pn,f,a in zip(
         ["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
     print >> log, "c fin %s:" % pn, numstr(f)
     print >> log, "  ana %s:" % pn, numstr(a)
   assert approx_equal(trg.f_calc_hessians, c_fin)
Example #11
0
 def check_f_calc_derivs():
     eps = 1e-6
     g_fin = flex.complex_double()
     c_fin = flex.vec3_double()
     for ih in xrange(f_calc.size()):
         c_orig = f_calc[ih]
         g_fin_ab = []
         c_fin_ab = []
         for iab in [0, 1]:
             fs = []
             gs = []
             for signed_eps in [eps, -eps]:
                 if iab == 0:
                     f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
                 else:
                     f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
                 trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
                 fs.append(trg_eps.t)
                 gs.append(trg_eps.f_calc_gradients[ih])
             g_fin_ab.append((fs[0] - fs[1]) / (2 * eps))
             c_fin_ab.append((gs[0] - gs[1]) / (2 * eps))
         g_fin.append(complex(*g_fin_ab))
         assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
         c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
         f_calc[ih] = c_orig
     for pn, f, a in zip(g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
         print >> log, "g fin %s:" % pn, numstr(f)
         print >> log, "  ana %s:" % pn, numstr(a)
     assert approx_equal(trg.f_calc_gradients, g_fin)
     for pn, f, a in zip(["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
         print >> log, "c fin %s:" % pn, numstr(f)
         print >> log, "  ana %s:" % pn, numstr(a)
     assert approx_equal(trg.f_calc_hessians, c_fin)
Example #12
0
def get_3D_transform():
    #with mrcfile.open("cropout_bin4.mrc") as mrc: # small example
    with mrcfile.open("cropout.mrc") as mrc:
        imax = 512  #take small subset, optimized for prime factorization

        # coerce to double
        from scitbx.array_family import flex
        realpart = flex.double(mrc.data.astype(np.float64))
        # in-place resize to a good multiple of 2
        realpart.resize(flex.grid(imax, imax, imax))
        complpart = flex.double(flex.grid(imax, imax, imax))
        #from IPython import embed; embed()
        C3D = flex.complex_double(realpart, complpart)

        from matplotlib import pyplot as plt
        from scitbx import fftpack
        #plt.imshow(S2D)
        #plt.show()
        #from IPython import embed; embed()
        print "C3Dfocus", C3D.focus()
        print C3D
        FFT = fftpack.complex_to_complex_3d(
            (C3D.focus()[0], C3D.focus()[1], C3D.focus()[2]))
        c = FFT.forward(C3D)
        print c.focus()
        print c
        return c
Example #13
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)
    def sum_structure_factors_cpu(self):

        self.structure_factors = flex.complex_double(len(self.cached_h),
                                                     complex(0.0, 0.0))

        # sum structure factors
        for i_structure in xrange(len(self.structures.species)):
            for j_orientation in xrange(
                    self.structures.species[i_structure].n_copies):
                tmp_xyz = self.structures.species[i_structure].xyz.deep_copy()
                translation = flex.double(
                    self.structures.translations[i_structure][j_orientation])
                rotation = matrix.sqr(
                    self.structures.rotations[i_structure][j_orientation])
                for i in xrange(len(tmp_xyz)):
                    xyz = flex.double(rotation * tmp_xyz[i])
                    tmp_xyz[i] = tuple(xyz + translation)
                self.structure_factors += direct_sum_structure_factors\
                                          (self.structures.species[i_structure].
                                           scattering_types,
                                           tmp_xyz,
                                           self.structures.species[i_structure].
                                           boundary_layer_scaling_factors,
                                           self.cached_h,
                                           self.structures.species[i_structure].
                                           scattering_type_registry)
def tst_rotation_fft(args):
    filename = args[0]
    filename2 = args[1]
    beta = float(args[2])
    nmax = 20
    nlm_array = math.nlm_array(nmax)
    coefs = easy_pickle.load(filename)
    nlm_array.load_coefs(nlm_array.nlm(), coefs)

    this_nlm_array = math.nlm_array(nmax)
    coefs = easy_pickle.load(filename2)
    this_nlm_array.load_coefs(nlm_array.nlm(), coefs)

    beta = smath.pi * beta
    cc_obj = correlation(nlm_array, this_nlm_array, nmax, beta)
    mm = cc_obj.mm_coef(0)
    fft_input = flex.complex_double(flex.grid(2 * nmax + 1, 2 * nmax + 1))
    fft_r = tst_rotation(args)
    for ii in range(mm.size()):
        fft_input[ii] = mm[ii]
    #  print ii, mm[ii], fft_r[ii]/1681.0

    fft = fftpack.complex_to_complex_2d(2 * nmax + 1, 2 * nmax + 1)

    result = fft.backward(fft_input)

    out = open("fft_" + str(beta) + ".dat", 'w')
    for ii in range(2 * nmax + 1):
        for jj in range(2 * nmax + 1):
            print >> out, ii * 9, jj * 9, abs(result[(ii, jj)])
        print >> out
    out.close()
def exercise_value(f_c_in_p1, f_o, flags, n_sampled):
    """ where we check against the trusted correlation map implemented
  in cctbx.translation_search """
    crystal_gridding = f_o.crystal_gridding(
        symmetry_flags=translation_search.symmetry_flags(
            is_isotropic_search_model=False, have_f_part=False),
        resolution_factor=1 / 3)
    correlation_map = translation_search.fast_nv1995(
        gridding=crystal_gridding.n_real(),
        space_group=f_o.space_group(),
        anomalous_flag=f_o.anomalous_flag(),
        miller_indices_f_obs=f_o.indices(),
        f_obs=f_o.data(),
        f_part=flex.complex_double(),  ## no sub-structure is already fixed
        miller_indices_p1_f_calc=f_c_in_p1.indices(),
        p1_f_calc=f_c_in_p1.data()).target_map()
    nx, ny, nz = n = crystal_gridding.n_real()

    def sampled_indices():
        from random import randrange
        yield (0, 0, 0)
        for i in xrange(n_sampled - 1):
            yield randrange(0, n[0]), randrange(0, n[1]), randrange(0, n[2])

    f_o_sq = f_o.as_intensity_array()
    for i, j, k in sampled_indices():
        x = (i / nx, j / ny, k / nz)
        if random.random() > 0.5: f_o_or_f_o_sq = f_o
        else: f_o_or_f_o_sq = f_o_sq
        gos = symmetrised_shifted_structure_factors(f_o_or_f_o_sq, f_c_in_p1,
                                                    x).misfit(f_o_or_f_o_sq)
        assert approx_equal(gos.correlation, correlation_map[i, j,
                                                             k]), (i, j, k)
def exercise_value(f_c_in_p1, f_o, flags, n_sampled):
  """ where we check against the trusted correlation map implemented
  in cctbx.translation_search """
  crystal_gridding = f_o.crystal_gridding(
    symmetry_flags=translation_search.symmetry_flags(
      is_isotropic_search_model=False,
      have_f_part=False),
    resolution_factor=1/3)
  correlation_map = translation_search.fast_nv1995(
    gridding=crystal_gridding.n_real(),
    space_group=f_o.space_group(),
    anomalous_flag=f_o.anomalous_flag(),
    miller_indices_f_obs=f_o.indices(),
    f_obs=f_o.data(),
    f_part=flex.complex_double(), ## no sub-structure is already fixed
    miller_indices_p1_f_calc=f_c_in_p1.indices(),
    p1_f_calc=f_c_in_p1.data()).target_map()
  nx, ny, nz = n = crystal_gridding.n_real()
  def sampled_indices():
    from random import randrange
    yield (0,0,0)
    for i in xrange(n_sampled - 1):
      yield randrange(0, n[0]), randrange(0, n[1]), randrange(0, n[2])
  f_o_sq = f_o.as_intensity_array()
  for i,j,k in sampled_indices():
    x = (i/nx, j/ny, k/nz)
    if random.random() > 0.5: f_o_or_f_o_sq = f_o
    else: f_o_or_f_o_sq = f_o_sq
    gos = symmetrised_shifted_structure_factors(
      f_o_or_f_o_sq, f_c_in_p1, x).misfit(f_o_or_f_o_sq)
    assert approx_equal(gos.correlation, correlation_map[i,j,k]), (i,j,k)
Example #18
0
    def _fft(self, reciprocal_lattice_vectors, d_min):

        reciprocal_space_grid, used_in_indexing = self._map_centroids_to_reciprocal_space_grid(
            reciprocal_lattice_vectors, d_min)

        logger.info("Number of centroids used: %i" %
                    ((reciprocal_space_grid > 0).count(True)))

        # gb_to_bytes = 1073741824
        # bytes_to_gb = 1/gb_to_bytes
        # (128**3)*8*2*bytes_to_gb
        # 0.03125
        # (256**3)*8*2*bytes_to_gb
        # 0.25
        # (512**3)*8*2*bytes_to_gb
        # 2.0

        fft = fftpack.complex_to_complex_3d(self._gridding)
        grid_complex = flex.complex_double(
            reals=reciprocal_space_grid,
            imags=flex.double(reciprocal_space_grid.size(), 0),
        )
        grid_transformed = fft.forward(grid_complex)
        grid_real = flex.pow2(flex.real(grid_transformed))
        del grid_transformed

        return grid_real, used_in_indexing
Example #19
0
 def fs(self):
     result = flex.complex_double()
     for hkl in self.miller_indices:
         result.append(
             structure_factor(xray_structure=self.xray_structure,
                              hkl=hkl).f())
     return result
def get_static_fcalcs():
    C2_structures = get_C2_structures()

    energy = 7070.0
    GF_whole7070 = gen_fmodel_with_complex.from_structure(C2_structures[0],energy
                   ).from_parameters(algorithm="fft")
    GF_whole7070 = GF_whole7070.as_P1_primitive()
    f_container7070 = GF_whole7070.get_fmodel()
    Fmodel_whole7070 = f_container7070.f_model
    Fmodel_indices = Fmodel_whole7070.indices() # common structure defines the indices
    F_bulk = f_container7070.fmodel.arrays.core.data.f_bulk
    F_bulk.reshape(flex.grid((Fmodel_indices.size(),1))) # in-place reshape, non-standard

    result = flex.complex_double(flex.grid((Fmodel_indices.size(),100)))

    # common structure to represent the wavelength-dependent non-Fe diffraction (bulk+atoms)
    for incr in range(100):
      energy = 7070.5 + incr

      GF_non_Fe = gen_fmodel_with_complex.from_structure(C2_structures[1],energy
                  ).from_parameters(algorithm="fft")
      GF_non_Fe = GF_non_Fe.as_P1_primitive()
      f_container = GF_non_Fe.get_fmodel()
      Fcalc_non_Fe = f_container.fmodel.f_calc().data()
      Fcalc_non_Fe.reshape(flex.grid((Fmodel_indices.size(),1))) # in-place reshape, non-standard

      result.matrix_paste_block_in_place((F_bulk + Fcalc_non_Fe),0,incr)

    # result holds a table of complex double structure factors.  Rows are Miller indices H.
    # columns are F_H(energy, 100 channels) for F(bulk) + F(non-Fe atoms). Thus this is
    # the energy-dependent portion of the calculation that is not dependent on the iron model.
    return result
Example #21
0
def tst_Inm_array(nmax):
    Inm = math.nl_c_array(nmax)
    nls = Inm.nl()
    moments = flex.double(range(nls.size()))
    moments = flex.complex_double(moments, moments)
    Inm.load_coefs(nls, moments)
    for ii, nl in zip(range(nls.size()), nls):
        assert (Inm.get_coef(nl[0], nl[1]).real == ii)
Example #22
0
    def __init__(self,
                 crystal_gridding,
                 fmodel,
                 map_type,
                 max_boxes,
                 box_size_as_fraction=None):
        sgt = fmodel.f_obs().space_group().type()
        assert sgt.number() == 1

        def get_map(fmodel, map_type, external_complete_set=None):
            f_map = fmodel.electron_density_map().map_coefficients(
                map_type=map_type, isotropize=True, fill_missing=False)
            fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                     fourier_coefficients=f_map)
            return fft_map.real_map_unpadded()

        f_model = fmodel.f_model_scaled_with_k1()
        fft_map = miller.fft_map(crystal_gridding=crystal_gridding,
                                 fourier_coefficients=f_model)
        f_model_map_data = fft_map.real_map_unpadded()
        zero_complex_ma = f_model.customized_copy(
            data=flex.complex_double(f_model.data().size(), 0))
        b = maptbx.boxes(fraction=0.3,
                         n_real=f_model_map_data.focus(),
                         max_boxes=max_boxes,
                         log=sys.stdout)
        self.map_result = flex.double(flex.grid(b.n_real))
        self.r = flex.double()
        for s, e in zip(b.starts, b.ends):
            f_model_map_data_omit = maptbx.set_box_copy(
                value=0, map_data_to=f_model_map_data, start=s, end=e)
            f_model_omit = f_model.structure_factors_from_map(
                map=f_model_map_data_omit,
                use_scale=True,
                anomalous_flag=False,
                use_sg=False)
            fmodel_ = mmtbx.f_model.manager(f_obs=fmodel.f_obs(),
                                            r_free_flags=fmodel.r_free_flags(),
                                            f_calc=f_model_omit,
                                            f_mask=zero_complex_ma)
            self.r.append(fmodel_.r_work())
            f_map_data = get_map(fmodel=fmodel_, map_type=map_type)
            etmp = [e[0] - 1, e[1] - 1,
                    e[2] - 1]  # because .copy() includes right edge
            box = maptbx.copy(f_map_data, s, etmp)
            box.reshape(flex.grid(box.all()))
            maptbx.set_box(map_data_from=box,
                           map_data_to=self.map_result,
                           start=s,
                           end=e)
        sd = self.map_result.sample_standard_deviation()
        self.map_result = self.map_result / sd
        self.map_coefficients = fmodel.f_obs().structure_factors_from_map(
            map=self.map_result,
            use_scale=True,
            anomalous_flag=False,
            use_sg=False)
Example #23
0
def test_flex_loop_nesting():
    fo = flex.int(10)
    npo = flumpy.to_numpy(fo)
    assert fo is flumpy.from_numpy(npo)

    # Now try vec
    fo = flex.complex_double(5)
    npo = flumpy.to_numpy(fo)
    assert flumpy.from_numpy(npo) is fo
Example #24
0
def tst_2d_zernike_mom(n, l, filename, h5file, flag):
    rebuilt = open(filename, 'w')
    # image = generate_image()
    # image=ImageToDat("/Users/wyf/Desktop/test11.png")
    image = preprocess_image(h5file, flag)
    # image = ImageToDat('cha.png')

    NP = int(smath.sqrt(image.size()))
    N = int(NP / 2)
    print "=====", NP, N
    grid_2d = math.two_d_grid(N, nmax)
    grid_2d.clean_space(image)
    grid_2d.construct_space_sum()
    zernike_2d_mom = math.two_d_zernike_moments(grid_2d, nmax)
    moments = zernike_2d_mom.moments()
    coefs = flex.real(moments)
    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()
    nl_array.load_coefs(nls, coefs)
    lfg = math.log_factorial_generator(nmax)
    # print nl_array.get_coef(n,l)*2

    for nl, c in zip(nls, moments):
        if (abs(c) < 1e-3):
            c = 0
        print nl, c

    reconst = flex.complex_double(NP**2, 0)
    for nl, c in zip(nls, moments):
        n = nl[0]
        l = nl[1]
        if (l > 0):
            c = c * 2
        #rzfa = math.zernike_2d_radial(n,l,lfg)
        rap = math.zernike_2d_polynome(n, l)  #,rzfa)
        i = 0
        for x in range(0, NP):
            x = x - N
            for y in range(0, NP):
                y = y - N
                rr = smath.sqrt(x * x + y * y) / N
                if rr > 1.0:
                    value = 0.0
                else:
                    tt = smath.atan2(y, x)
                    value = rap.f(rr, tt)
                reconst[i] = reconst[i] + value * c
                i = i + 1

    i = 0
    for x in range(0, NP):
        for y in range(0, NP):
            value = reconst[i].real
            print >> rebuilt, x, y, value
            i = i + 1
    rebuilt.close()
 def d2_target_d_params_diag_cpp(self, f_obs, target_type):
   da_db = flex.complex_double()
   daa_dbb_dab = flex.vec3_double()
   for hkl,obs in zip(self.miller_indices, f_obs.data()):
     sf = structure_factor(xray_structure=self.xray_structure, hkl=hkl)
     target = target_type(obs=obs, calc=sf.f())
     da_db.append(complex(target.da(), target.db()))
     daa_dbb_dab.append((target.daa(), target.dbb(), target.dab()))
   return self.xray_structure.grads_and_curvs_target_simple(
     miller_indices=f_obs.indices(), da_db=da_db, daa_dbb_dab=daa_dbb_dab)
Example #26
0
def map_calculate_rings(mp):

  # set up parameters
  mp.structure_generator.random.setstate(mp.random_state)

  es = ewald_sphere()
  es.set_wavelength(mp.beam_properties.wavelength)
  es.set_distance(mp.detector_properties.distance)

  ic = image_composer()
  ic.set_detector_size(mp.detector_properties.detector_size)
  ic.set_beam_center(mp.detector_properties.beam_center)
  ic.set_pixel_size(mp.detector_properties.pixel_size)
  ic.set_ewald_sphere(es)

  im = image_simulator()
  im.structures = mp.structure_generator
  im.image_composer = ic
  im.cached_h = mp.h

  # calculate structure factors
  for i in xrange(mp.n_images):
    im.structures.randomize()
    im.sum_structure_factors()

    # copy intensities to full image
    sf = im.structure_factors
    all_sf = flex.complex_double((mp.detector_properties.detector_size[0]+1)*\
                                 (mp.detector_properties.detector_size[1]+1),\
                                 complex(0.0,0.0))
    k = 0
    for j in xrange(len(mp.use_index)):
      if (mp.use_index[j]):
        all_sf[j] = sf[k]
        k += 1
    im.structure_factors = all_sf

    image_data = im.build_image(n_photons=bp.flux*bp.t)

    if (True):
      for q_i in [0.01, 0.1, 0.2]:
        print q_i, I_q(ic.get_q(),image_data,q_i,mp.dq)
      write_image(file_name='ring.png',
                  detector_size=mp.detector_properties.detector_size,
                  image_data=image_data)

    if (False):
      file_name = './test_rings/' + str(os.getpid()) + '_' + str(i)
      f = open(file_name,'wb')
      pickle.dump(image_data,f,2)
      f.close()

  return mp.n_images
Example #27
0
 def d2_target_d_params_diag_cpp(self, f_obs, target_type):
     da_db = flex.complex_double()
     daa_dbb_dab = flex.vec3_double()
     for hkl, obs in zip(self.miller_indices, f_obs.data()):
         sf = structure_factor(xray_structure=self.xray_structure, hkl=hkl)
         target = target_type(obs=obs, calc=sf.f())
         da_db.append(complex(target.da(), target.db()))
         daa_dbb_dab.append((target.daa(), target.dbb(), target.dab()))
     return self.xray_structure.grads_and_curvs_target_simple(
         miller_indices=f_obs.indices(),
         da_db=da_db,
         daa_dbb_dab=daa_dbb_dab)
Example #28
0
def build_scat_pat(data):
  print "DO FFT"
  np,np = data.focus()
  flex_grid=flex.grid(np,np)
  fft_input=flex.complex_double(flex_grid)
  for ii in range( fft_input.size() ):
    fft_input[ii] = complex( data[ii],0 )
  fft_obj = fftpack.complex_to_complex_2d( (np,np) )
  result = fft_obj.forward( fft_input )
  result = flex.abs( result )
  result = result*result
  result = reorder_and_cut_data(result,np,np/10)
  return result
Example #29
0
def kick_fmodel(
      fmodel,
      map_type,
      crystal_gridding,
      number_of_kicks,
      macro_cycles,
      missing           = None,
      kick_completeness = 0.95):
  f_model = fmodel.f_model_no_scales()
  zero = fmodel.f_calc().customized_copy(data =
    flex.complex_double(fmodel.f_calc().data().size(), 0))
  fmodel_dc  = mmtbx.f_model.manager(
    f_obs         = fmodel.f_obs(),
    r_free_flags  = fmodel.r_free_flags(),
    k_isotropic   = fmodel.k_isotropic(),
    k_anisotropic = fmodel.k_anisotropic(),
    f_calc        = fmodel.f_model_no_scales(),
    f_part1       = fmodel.f_part1(),
    f_part2       = fmodel.f_part2(),
    f_mask        = zero)
  r1 = fmodel.r_work()
  r2 = fmodel_dc.r_work()
  assert approx_equal(r1, r2, 1.e-4), [r1, r2]
  def get_mc(fm):
   return fm.electron_density_map().map_coefficients(
       map_type     = map_type,
       isotropize   = True,
       fill_missing = False)
  def recreate_r_free_flags(fmodel):
    rc = random.choice([0.05, 0.9])
    r_free_flags = flex.random_bool(fmodel.f_obs().indices().size(), rc)
    fmodel._r_free_flags._data = r_free_flags
    return fmodel
  map_data = None
  for it in xrange(macro_cycles):
    print "  %d"%it
    f_model_kick = randomize_struture_factors(map_coeffs=f_model,
      number_of_kicks=number_of_kicks)
    fmodel_dc = recreate_r_free_flags(fmodel = fmodel_dc)
    fmodel_dc.update(f_calc = f_model_kick)
    mc = get_mc(fm=fmodel_dc)
    if(missing is not None):
      mc = mc.complete_with(missing, scale=True)
    if(kick_completeness):
      mc = mc.select(flex.random_bool(mc.size(), kick_completeness))
    map_data = compute_map_and_combine(
      map_coeffs       = mc,
      crystal_gridding = crystal_gridding,
      map_data         = map_data)
  return map_data
Example #30
0
def generate_image(n, moments, N=100):
    # Generate images from the zernike moments, N is the number of points from 0 to 1
    nmax = n

    image = flex.vec3_double()
    NP = 2 * N + 1
    reconst = flex.complex_double(NP**2, 0)
    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()

    r_theta = flex.vec2_double()

    for x in range(0, NP):
        x = x - N
        for y in range(0, NP):
            y = y - N
            rr = smath.sqrt(x * x + y * y) / N
            if rr > 1.0:
                tt = 0.0
            else:
                tt = smath.atan2(y, x)
            r_theta.append([rr, tt])

    for nl, c in zip(nls, moments):
        n = nl[0]
        l = nl[1]
        #if(l/2*2 != l): continue
        if (l > 0): c = c * 2.0

        rap = math.zernike_2d_polynome(n, l)
        i = 0
        for pt in r_theta:
            rr = pt[0]
            if rr > 1.0:
                value = 0.0
            else:
                tt = pt[1]
                value = rap.f(rr, tt)
            reconst[i] = reconst[i] + value * c
            i = i + 1

    return reconst

    i = 0
    for x in range(0, NP):
        for y in range(0, NP):
            value = reconst[i].real
            image.append([x, y, value])
            i = i + 1
    return image
Example #31
0
def main(filenames,
         map_file,
         npoints=192,
         max_resolution=6,
         reverse_phi=False):
    rec_range = 1 / max_resolution

    image = ImageFactory(filenames[0])
    panel = image.get_detector()[0]
    beam = image.get_beam()
    s0 = beam.get_s0()
    pixel_size = panel.get_pixel_size()

    xlim, ylim = image.get_raw_data().all()

    xy = recviewer.get_target_pixels(panel, s0, xlim, ylim, max_resolution)

    s1 = panel.get_lab_coord(xy * pixel_size[0])  # FIXME: assumed square pixel
    s1 = s1 / s1.norms() * (1 / beam.get_wavelength())  # / is not supported...
    S = s1 - s0

    grid = flex.double(flex.grid(npoints, npoints, npoints), 0)
    cnts = flex.int(flex.grid(npoints, npoints, npoints), 0)

    for filename in filenames:
        print "Processing image", filename
        try:
            fill_voxels(ImageFactory(filename), grid, cnts, S, xy, reverse_phi,
                        rec_range)
        except:
            print " Failed to process. Skipped this."

    recviewer.normalize_voxels(grid, cnts)

    uc = uctbx.unit_cell((npoints, npoints, npoints, 90, 90, 90))
    ccp4_map.write_ccp4_map(map_file, uc, sgtbx.space_group("P1"), (0, 0, 0),
                            grid.all(), grid,
                            flex.std_string(["cctbx.miller.fft_map"]))
    return
    from scitbx import fftpack
    fft = fftpack.complex_to_complex_3d(grid.all())
    grid_complex = flex.complex_double(reals=flex.pow2(grid),
                                       imags=flex.double(grid.size(), 0))
    grid_transformed = flex.abs(fft.backward(grid_complex))
    print flex.max(grid_transformed), flex.min(
        grid_transformed), grid_transformed.all()
    ccp4_map.write_ccp4_map(map_file, uc, sgtbx.space_group("P1"), (0, 0, 0),
                            grid.all(), grid_transformed,
                            flex.std_string(["cctbx.miller.fft_map"]))
Example #32
0
def kick_fmodel(fmodel,
                map_type,
                crystal_gridding,
                number_of_kicks,
                macro_cycles,
                missing=None,
                kick_completeness=0.95):
    f_model = fmodel.f_model_no_scales()
    zero = fmodel.f_calc().customized_copy(
        data=flex.complex_double(fmodel.f_calc().data().size(), 0))
    fmodel_dc = mmtbx.f_model.manager(f_obs=fmodel.f_obs(),
                                      r_free_flags=fmodel.r_free_flags(),
                                      k_isotropic=fmodel.k_isotropic(),
                                      k_anisotropic=fmodel.k_anisotropic(),
                                      f_calc=fmodel.f_model_no_scales(),
                                      f_part1=fmodel.f_part1(),
                                      f_part2=fmodel.f_part2(),
                                      f_mask=zero)
    r1 = fmodel.r_work()
    r2 = fmodel_dc.r_work()
    assert approx_equal(r1, r2, 1.e-4), [r1, r2]

    def get_mc(fm):
        return fm.electron_density_map().map_coefficients(map_type=map_type,
                                                          isotropize=True,
                                                          fill_missing=False)

    def recreate_r_free_flags(fmodel):
        rc = random.choice([0.05, 0.9])
        r_free_flags = flex.random_bool(fmodel.f_obs().indices().size(), rc)
        fmodel._r_free_flags._data = r_free_flags
        return fmodel

    map_data = None
    for it in xrange(macro_cycles):
        print "  %d" % it
        f_model_kick = randomize_struture_factors(
            map_coeffs=f_model, number_of_kicks=number_of_kicks)
        fmodel_dc = recreate_r_free_flags(fmodel=fmodel_dc)
        fmodel_dc.update(f_calc=f_model_kick)
        mc = get_mc(fm=fmodel_dc)
        if (missing is not None):
            mc = mc.complete_with(missing, scale=True)
        if (kick_completeness):
            mc = mc.select(flex.random_bool(mc.size(), kick_completeness))
        map_data = compute_map_and_combine(map_coeffs=mc,
                                           crystal_gridding=crystal_gridding,
                                           map_data=map_data)
    return map_data
Example #33
0
def load_fft(filename):
    with mrcfile.open(filename) as mrc:
        # coerce to double
        realpart = flex.double(mrc.data.astype(np.float64))
        complpart = flex.double(flex.grid(realpart.focus()))
        C3D = flex.complex_double(realpart, complpart)

        print "C3Dfocus", C3D.focus()
        print C3D
        FFT = fftpack.complex_to_complex_3d(
            (C3D.focus()[0], C3D.focus()[1], C3D.focus()[2]))
        complex_flex = FFT.forward(C3D)
        print complex_flex.focus()
        print complex_flex

    return complex_flex
Example #34
0
def main(filenames, map_file, npoints=192, max_resolution=6, reverse_phi=False):
    rec_range = 1 / max_resolution

    image = ImageFactory(filenames[0])
    panel = image.get_detector()[0]
    beam = image.get_beam()
    s0 = beam.get_s0()
    pixel_size = panel.get_pixel_size()
    
    xlim, ylim = image.get_raw_data().all()
    
    xy = recviewer.get_target_pixels(panel, s0, xlim, ylim, max_resolution)
    
    s1 = panel.get_lab_coord(xy * pixel_size[0]) # FIXME: assumed square pixel
    s1 = s1 / s1.norms() * (1 / beam.get_wavelength()) # / is not supported...
    S = s1 - s0
    
    grid = flex.double(flex.grid(npoints, npoints, npoints), 0)
    cnts = flex.int(flex.grid(npoints, npoints, npoints), 0)
    
    for filename in filenames:
        print "Processing image", filename
        try:
            fill_voxels(ImageFactory(filename), grid, cnts, S, xy, reverse_phi, rec_range)
        except:
            print " Failed to process. Skipped this."
        
    recviewer.normalize_voxels(grid, cnts)
    
    uc = uctbx.unit_cell((npoints, npoints, npoints, 90, 90, 90))
    ccp4_map.write_ccp4_map(map_file, uc, sgtbx.space_group("P1"), 
                            (0, 0, 0), grid.all(), grid, 
                            flex.std_string(["cctbx.miller.fft_map"]))
    return
    from scitbx import fftpack
    fft = fftpack.complex_to_complex_3d(grid.all())
    grid_complex = flex.complex_double(
                            reals=flex.pow2(grid),
                            imags=flex.double(grid.size(), 0))
    grid_transformed = flex.abs(fft.backward(grid_complex))
    print flex.max(grid_transformed), flex.min(grid_transformed), grid_transformed.all()
    ccp4_map.write_ccp4_map(map_file, uc, sgtbx.space_group("P1"), 
                            (0, 0, 0), grid.all(), grid_transformed,
                            flex.std_string(["cctbx.miller.fft_map"]))
def tst_rotation(args):
    filename = args[0]
    filename2 = args[1]
    beta = float(args[2])
    ngrid = 40
    nmax = 20
    nlm_array = math.nlm_array(nmax)
    coefs = easy_pickle.load(filename)
    nlm_array.load_coefs(nlm_array.nlm(), coefs)

    this_nlm_array = math.nlm_array(nmax)
    coefs = easy_pickle.load(filename2)
    this_nlm_array.load_coefs(nlm_array.nlm(), coefs)

    beta = smath.pi * beta
    cc_obj = correlation(nlm_array, this_nlm_array, nmax, beta)

    fft_input = flex.complex_double(flex.grid(2 * nmax + 1, 2 * nmax + 1))
    count = 0
    radian = 180.0 / smath.pi
    out = open("scan_" + str(beta) + ".dat", 'w')
    for ii in range(ngrid + 1):
        for jj in range(ngrid + 1):
            alpha = ii * smath.pi * 2.0 / ngrid
            gama = jj * smath.pi * 2.0 / ngrid
            cc = cc_obj.calc_correlation(alpha, beta, gama)
            fft_input[count] = cc
            count = count + 1
            print >> out, alpha * radian, gama * radian, abs(cc)
        print >> out
    out.close()
    fft = fftpack.complex_to_complex_2d(2 * nmax + 1, 2 * nmax + 1)

    result = fft.forward(fft_input)
    #return result

    result = fft.backward(result)

    out = open("fft_fake_" + str(beta) + ".dat", 'w')
    for ii in range(2 * nmax + 1):
        for jj in range(2 * nmax + 1):
            print >> out, ii * 9, jj * 9, abs(result[(jj, ii)])
        print >> out
    out.close()
def map_random_image(p=None):

    result = [None for i in xrange(p.n_images)]

    # set random state for current thread
    p.structure_generator.random.setstate(p.random_state)

    es = ewald_sphere()
    es.set_wavelength(p.beam_properties.wavelength)
    es.set_distance(p.detector_properties.distance)

    ic = image_composer()
    ic.set_detector_size(p.detector_properties.detector_size)
    ic.set_beam_center(p.detector_properties.beam_center)
    ic.set_pixel_size(p.detector_properties.pixel_size)
    ic.set_ewald_sphere(es)

    im = image_simulator()
    im.structures = p.structure_generator
    im.image_composer = ic

    h = ic.cache_h()
    sf_length = (p.detector_properties.detector_size[0] + 1) *\
                (p.detector_properties.detector_size[1] + 1)
    n_photons = p.beam_properties.flux * p.beam_properties.t

    for i in xrange(p.n_images):
        sf = flex.complex_double(sf_length, 0.0)
        im.structures.species[0].n_copies = p.n_particles[i]
        im.structures.randomize()
        for j in xrange(p.n_particles[i]):
            image_name = p.base_image_directory + im.structures.random.choice(
                image_list)
            t = im.structures.translations[0][j]
            f = open(image_name, 'rb')
            sf_tmp = pickle.load(f)
            f.close()
            sf_tmp = apply_translation(sf_tmp, h, t)
            sf += sf_tmp
        im.structure_factors = sf
        image_data = im.build_image(n_photons=n_photons)
        result[i] = image_data.deep_copy()

    return result
Example #37
0
def generate_image2(n, moments, template_image, np):
    nmax = n

    image = flex.vec3_double()
    np_tot = template_image.size()
    reconst = flex.complex_double(np_tot, 0)
    nl_array = math.nl_array(nmax)
    nls = nl_array.nl()

    r_theta = flex.vec2_double()
    for pt in template_image:
        x = pt[0] - np
        y = pt[1] - np
        rr = smath.sqrt(x * x + y * y) / np
        if rr > 1.0:
            tt = 0.0
        else:
            tt = smath.atan2(y, x)
        r_theta.append([rr, tt])

    for nl, c in zip(nls, moments):
        n = nl[0]
        l = nl[1]
        #if(l/2*2 != l): continue
        if (l > 0): c = c * 2.0
        rap = math.zernike_2d_polynome(n, l)
        i = 0
        for pt in r_theta:
            rr = pt[0]
            if rr > 1.0:
                value = 0.0
            else:
                tt = pt[1]
                value = rap.f(rr, tt)
            reconst[i] = reconst[i] + value * c
            i = i + 1

    for i in range(np_tot):
        x = template_image[i][0]
        y = template_image[i][1]
        value = reconst[i].real
        image.append([x, y, value])
    return image
Example #38
0
def tst_nlm():
  nlm_array = math.nlm_array(10)
  nlm_array.set_coef(10,2,2, 3.0)
  a = nlm_array.get_coef(10,2,2)
  assert ( abs(a-3.0) ) <= 1e-5
  nlm_ind = shared.tiny_int_3( [(10,2,2),(8,2,2)]  )
  nlm_coef = flex.complex_double( [15.0+0j,15.0+0j] )
  assert nlm_array.load_coefs( nlm_ind , nlm_coef)
  a = nlm_array.get_coef(10, 2, 2)
  b = nlm_array.get_coef(8, 2, 2)
  assert ( abs(a-15.0) ) <= 1e-5
  assert ( abs(b-15.0) ) <= 1e-5
  nlm = nlm_array.nlm()
  cnlm = nlm_array.coefs()
  sel = nlm_array.select_on_nl(2,2)
  assert len(sel)==5
  assert 5 in sel
  assert 6 in sel
  assert 7 in sel
  assert 8 in sel
  assert 9 in sel
Example #39
0
def tst_nlm():
  nlm_array = math.nlm_array(10)
  nlm_array.set_coef(10,2,2, 3.0)
  a = nlm_array.get_coef(10,2,2)
  assert ( abs(a-3.0) ) <= 1e-5
  nlm_ind = shared.tiny_int_3( [(10,2,2),(8,2,2)]  )
  nlm_coef = flex.complex_double( [15.0+0j,15.0+0j] )
  assert nlm_array.load_coefs( nlm_ind , nlm_coef)
  a = nlm_array.get_coef(10, 2, 2)
  b = nlm_array.get_coef(8, 2, 2)
  assert ( abs(a-15.0) ) <= 1e-5
  assert ( abs(b-15.0) ) <= 1e-5
  nlm = nlm_array.nlm()
  cnlm = nlm_array.coefs()
  sel = nlm_array.select_on_nl(2,2)
  assert len(sel)==5
  assert 5 in sel
  assert 6 in sel
  assert 7 in sel
  assert 8 in sel
  assert 9 in sel
Example #40
0
 def __init__(self,
              fmodel,
              crystal_gridding,
              box_size_as_fraction=0.1,
              max_boxes=2000,
              neutral_volume_box_cushion_width=2,
              full_resolution_map=True,
              log=sys.stdout):
     adopt_init_args(self, locals())
     self.map_type = "mFo-DFc"  # using other map types is much worse
     self.sd = self.get_p1_map_unscaled(
         fmodel=self.fmodel,
         map_type=self.map_type).sample_standard_deviation()
     xrs_p1 = fmodel.xray_structure.expand_to_p1(sites_mod_positive=True)
     self.sgt = fmodel.f_obs().space_group().type()
     self.r = flex.double()  # for tests only
     self.acc_asu = None
     # bulk-solvent
     mp = mmtbx.masks.mask_master_params.extract()
     mp.n_real = crystal_gridding.n_real()
     mp.step = None
     mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask(xray_structure=xrs_p1,
                                                     mask_params=mp)
     self.bulk_solvent_mask = mmtbx_masks_asu_mask_obj.mask_data_whole_uc()
     self.bulk_solvent_mask_asu = self.to_asu_map(
         map_data=self.bulk_solvent_mask)
     # atom map
     self.atom_map = mmtbx.real_space.sampled_model_density(
         xray_structure=xrs_p1, n_real=crystal_gridding.n_real()).data()
     self.n_real = self.atom_map.focus()
     self.atom_map_asu = self.to_asu_map(map_data=self.atom_map)
     # extras
     self.zero_cmpl_ma = fmodel.f_calc().customized_copy(
         data=flex.complex_double(fmodel.f_calc().size(), 0))
     self.r_factor_omit = flex.double()  # for regression test only
     # result map
     self.map_result_asu = flex.double(flex.grid(self.atom_map_asu.focus()))
     # iterate over boxes
     self.box_iterator()
Example #41
0
def load_fft(filename, Tukey):
    print "Opening file"
    with mrcfile.open(filename) as mrc:
        print "Converting to flex"
        # coerce to double
        if Tukey is True:
            mod_vol = apply_tukey_3d(mrc.data.astype(np.float64))
            realpart = flex.double(mod_vol)
        else:
            realpart = flex.double(mrc.data.astype(np.float64))
        complpart = flex.double(flex.grid(realpart.focus()))
        C3D = flex.complex_double(realpart, complpart)

        print "C3Dfocus", C3D.focus()
        print C3D
        FFT = fftpack.complex_to_complex_3d(
            (C3D.focus()[0], C3D.focus()[1], C3D.focus()[2]))
        complex_flex = FFT.forward(C3D)
        print complex_flex.focus()
        print complex_flex

    return complex_flex
 def __init__(
       self,
       fmodel,
       crystal_gridding,
       box_size_as_fraction=0.1,
       max_boxes=2000,
       neutral_volume_box_cushion_width=2,
       full_resolution_map=True,
       log=sys.stdout):
   adopt_init_args(self, locals())
   self.map_type="mFo-DFc" # using other map types is much worse
   self.sd = self.get_p1_map_unscaled(fmodel = self.fmodel,
     map_type=self.map_type).sample_standard_deviation()
   xrs_p1 = fmodel.xray_structure.expand_to_p1(sites_mod_positive=True)
   self.sgt = fmodel.f_obs().space_group().type()
   self.r = flex.double() # for tests only
   self.acc_asu = None
   # bulk-solvent
   mmtbx_masks_asu_mask_obj = mmtbx.masks.asu_mask(
     xray_structure = xrs_p1,
     n_real         = crystal_gridding.n_real())
   self.bulk_solvent_mask = mmtbx_masks_asu_mask_obj.mask_data_whole_uc()
   self.bulk_solvent_mask_asu=self.to_asu_map(map_data=self.bulk_solvent_mask)
   # atom map
   self.atom_map = mmtbx.real_space.sampled_model_density(
     xray_structure = xrs_p1,
     n_real         = crystal_gridding.n_real()).data()
   self.n_real = self.atom_map.focus()
   self.atom_map_asu=self.to_asu_map(map_data=self.atom_map)
   # extras
   self.zero_cmpl_ma = fmodel.f_calc().customized_copy(
     data = flex.complex_double(fmodel.f_calc().size(), 0))
   self.r_factor_omit = flex.double() # for regression test only
   # result map
   self.map_result_asu = flex.double(flex.grid(self.atom_map_asu.focus()))
   # iterate over boxes
   self.box_iterator()
Example #43
0
def test_complex_to_complex(verbose):
  fft = fftpack.complex_to_complex(5)
  vc = flex.complex_double(fft.n())
  vd = flex.double(fft.n() * 2)
  for i in xrange(fft.n()):
    vc[i] = complex(2.*i, 2.*i+1.)
    vd[2*i] = 2.*i
    vd[2*i+1] = 2.*i+1.
  vct = fft.forward(vc)
  vdt = fft.forward(vd)
  for t in (vct, vdt):
    assert t.origin() == (0,)
    assert t.all()[0] == fft.n()
    assert t.focus()[0] == fft.n()
  if (verbose): show_cseq(vc)
  assert_complex_eq_real(vc, vd)
  vct = fft.backward(vc)
  vdt = fft.backward(vd)
  for t in (vct, vdt):
    assert t.origin() == (0,)
    assert t.all()[0] == fft.n()
    assert t.focus()[0] == fft.n()
  if (verbose): show_cseq(vc)
  assert_complex_eq_real(vc, vd)
Example #44
0
 def structure_factors(self, d_min):
   print "WARNING: RESULTS NOT VERIFIED" # XXX
   miller_set = miller.build_set(
     crystal_symmetry=self,
     anomalous_flag=True, # XXX always True?
     d_min=d_min)
   f_calc = flex.complex_double()
   for h in miller_set.indices():
     fc = 0j
     for scatterer in self.scatterers():
       site_symmetry = self.site_symmetry(scatterer.site)
       equiv_sites = sgtbx.sym_equiv_sites(site_symmetry)
       sum_exp_j_two_pi_hx = 0j
       for i_symop,x in enumerate(equiv_sites.coordinates()):
         sum_hx = 0
         for i in xrange(3):
           sum_hx += h[i] * x[i]
         phase = 2 * math.pi * sum_hx
         exp_j_two_pi_hx = complex(math.cos(phase), math.sin(phase))
         if (scatterer.anisotropic_flag):
           r = self.space_group()(i_symop).r()
           hr = h
           dw = adptbx.debye_waller_factor_u_star(hr, scatterer.u_star)
           exp_j_two_pi_hx *= dw
         sum_exp_j_two_pi_hx += exp_j_two_pi_hx
       b_j = scatterer.scattering_info.bound_coh_scatt_length()
       fc_site = scatterer.weight() * b_j * sum_exp_j_two_pi_hx
       if (not scatterer.anisotropic_flag):
         d_star_sq = self.unit_cell().d_star_sq(h)
         dw = adptbx.debye_waller_factor_u_iso(d_star_sq/4, scatterer.u_iso)
         fc_site *= dw
       fc += fc_site
     f_calc.append(fc)
   return miller.array(
     miller_set=miller_set,
     data=f_calc)
 def __init__(
       self,
       crystal_gridding,
       fmodel,
       map_type,
       max_boxes,
       box_size_as_fraction=None):
   sgt = fmodel.f_obs().space_group().type()
   assert sgt.number() == 1
   def get_map(fmodel, map_type, external_complete_set=None):
     f_map = fmodel.electron_density_map().map_coefficients(
         map_type     = map_type,
         isotropize   = True,
         fill_missing = False)
     fft_map = miller.fft_map(
       crystal_gridding     = crystal_gridding,
       fourier_coefficients = f_map)
     return fft_map.real_map_unpadded()
   f_model = fmodel.f_model_scaled_with_k1()
   fft_map = miller.fft_map(
     crystal_gridding     = crystal_gridding,
     fourier_coefficients = f_model)
   f_model_map_data = fft_map.real_map_unpadded()
   zero_complex_ma = f_model.customized_copy(
     data = flex.complex_double(f_model.data().size(), 0))
   b = maptbx.boxes(
     fraction = 0.3,
     n_real   = f_model_map_data.focus(),
     max_boxes=max_boxes,
     log=sys.stdout)
   self.map_result = flex.double(flex.grid(b.n_real))
   self.r = flex.double()
   for s,e in zip(b.starts, b.ends):
     f_model_map_data_omit = maptbx.set_box_copy(
       value         = 0,
       map_data_to   = f_model_map_data,
       start         = s,
       end           = e)
     f_model_omit = f_model.structure_factors_from_map(
       map            = f_model_map_data_omit,
       use_scale      = True,
       anomalous_flag = False,
       use_sg         = False)
     fmodel_ = mmtbx.f_model.manager(
       f_obs        = fmodel.f_obs(),
       r_free_flags = fmodel.r_free_flags(),
       f_calc       = f_model_omit,
       f_mask       = zero_complex_ma)
     self.r.append(fmodel_.r_work())
     f_map_data = get_map(fmodel=fmodel_, map_type=map_type)
     etmp = [e[0]-1, e[1]-1, e[2]-1] # because .copy() includes right edge
     box = maptbx.copy(f_map_data, s, etmp)
     box.reshape(flex.grid(box.all()))
     maptbx.set_box(
       map_data_from = box,
       map_data_to   = self.map_result,
       start         = s,
       end           = e)
   sd = self.map_result.sample_standard_deviation()
   self.map_result = self.map_result/sd
   self.map_coefficients = fmodel.f_obs().structure_factors_from_map(
     map            = self.map_result,
     use_scale      = True,
     anomalous_flag = False,
     use_sg         = False)
 def df_d_params(self):
   tphkl = 2 * math.pi * matrix.col(self.hkl)
   h,k,l = self.hkl
   d_exp_huh_d_u_star = matrix.col([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l])
   for i_scatterer,scatterer in enumerate(self.scatterers):
     site_symmetry_ops = None
     if (self.site_symmetry_table.is_special_position(i_scatterer)):
       site_symmetry_ops = self.site_symmetry_table.get(i_scatterer)
       site_constraints = site_symmetry_ops.site_constraints()
       if (scatterer.flags.use_u_aniso()):
         adp_constraints = site_symmetry_ops.adp_constraints()
     w = scatterer.weight()
     wwo = scatterer.weight_without_occupancy()
     if (not scatterer.flags.use_u_aniso()):
       huh = scatterer.u_iso * self.d_star_sq
       dw = math.exp(mtps * huh)
     gaussian = self.scattering_type_registry.gaussian_not_optional(
       scattering_type=scatterer.scattering_type)
     f0 = gaussian.at_d_star_sq(self.d_star_sq)
     ffp = f0 + scatterer.fp
     fdp = scatterer.fdp
     ff = ffp + 1j * fdp
     d_site = matrix.col([0,0,0])
     if (not scatterer.flags.use_u_aniso()):
       d_u_iso = 0
       d_u_star = None
     else:
       d_u_iso = None
       d_u_star = matrix.col([0,0,0,0,0,0])
     d_occ = 0j
     d_fp = 0j
     d_fdp = 0j
     for s in self.space_group:
       r = s.r().as_rational().as_float()
       s_site = s * scatterer.site
       alpha = matrix.col(s_site).dot(tphkl)
       if (scatterer.flags.use_u_aniso()):
         s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose()
         huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl))
         dw = math.exp(mtps * huh)
       e = cmath.exp(1j*alpha)
       site_gtmx = r.transpose()
       d_site += site_gtmx * (
         w * dw * ff * e * 1j * tphkl)
       if (not scatterer.flags.use_u_aniso()):
         d_u_iso += w * dw * ff * e * mtps * self.d_star_sq
       else:
         u_star_gtmx = matrix.sqr(tensor_rank_2_gradient_transform_matrix(r))
         d_u_star += u_star_gtmx * (
           w * dw * ff * e * mtps * d_exp_huh_d_u_star)
       d_occ += wwo * dw * ff * e
       d_fp += w * dw * e
       d_fdp += w * dw * e * 1j
     if (site_symmetry_ops is not None):
       gsm = site_constraints.gradient_sum_matrix()
       gsm = matrix.rec(elems=gsm, n=gsm.focus())
       d_site = gsm * d_site
       if (scatterer.flags.use_u_aniso()):
         gsm = adp_constraints.gradient_sum_matrix()
         gsm = matrix.rec(elems=gsm, n=gsm.focus())
         d_u_star = gsm * d_u_star
     result = flex.complex_double(d_site)
     if (not scatterer.flags.use_u_aniso()):
       result.append(d_u_iso)
     else:
       result.extend(flex.complex_double(d_u_star))
     result.extend(flex.complex_double([d_occ, d_fp, d_fdp]))
     yield result
  def __init__(self,instance,algorithm,verbose=False):
    """Initializes a direct parallel worker

    :param instance: an instance of class from_scatterers_direct(\
cctbx.xray.structure_factors.manager.managed_calculation_base)
    :type instance: cctbx.xray.structure_factors.from_scatterers_direct
    :param algorithm: an instance of class direct_summation_cuda_platform(\
direct_summation_simple) with algorithm set to "simple" or "pycuda"
    :type algorithm: cctbx.xray.structure_factors.direct_summation_cuda_platform
    """
    self.scatterers = instance._xray_structure.scatterers()
    self.registry = instance._xray_structure.scattering_type_registry()
    self.miller_indices = instance._miller_set.indices()
    self.unit_cell = instance._miller_set.unit_cell()
    self.space_group = instance._miller_set.space_group().make_tidy()

    if verbose: self.print_diagnostics() # some diagnostics used for development

    if hasattr(algorithm,"simple"):
      instance._results = ext.structure_factors_simple(
      self.unit_cell,
      instance._miller_set.space_group(),
      self.miller_indices,
      self.scatterers,
      self.registry); return

    if hasattr(algorithm,"pycuda"):
      import pycuda.driver as cuda
      from pycuda.compiler import SourceModule

      self.validate_the_inputs(instance,cuda,algorithm)

      self.prepare_miller_arrays_for_cuda(algorithm)

      self.prepare_scattering_sites_for_cuda(algorithm)

      self.prepare_gaussians_symmetries_cell(algorithm)

      assert cuda.Device.count() >= 1

      device = cuda.Device(0)
      WARPSIZE=device.get_attribute(cuda.device_attribute.WARP_SIZE) # 32
      MULTIPROCESSOR_COUNT=device.get_attribute(cuda.device_attribute.MULTIPROCESSOR_COUNT)

      sort_mod = SourceModule((mod_fhkl_sorted%(self.gaussians.shape[0],
                         self.symmetry.shape[0],
                         self.sym_stride,
                         self.g_stride,
                         int(self.use_debye_waller),
                         self.order_z,self.order_p)).replace("floating_point_t",algorithm.float_t)
                         )

      r_m_m_address = sort_mod.get_global("reciprocal_metrical_matrix")[0]
      cuda.memcpy_htod(r_m_m_address, self.reciprocal_metrical_matrix)

      gaussian_address = sort_mod.get_global("gaussians")[0]
      cuda.memcpy_htod(gaussian_address, self.gaussians)

      symmetry_address = sort_mod.get_global("symmetry")[0]
      cuda.memcpy_htod(symmetry_address, self.symmetry)

      CUDA_fhkl = sort_mod.get_function("CUDA_fhkl")

      intermediate_real = algorithm.numpy.zeros((self.n_flat_hkl,),algorithm.numpy_t)
      intermediate_imag = algorithm.numpy.zeros((self.n_flat_hkl,),algorithm.numpy_t)
      for x in xrange(self.scatterers.number_of_types):
        fhkl_real = algorithm.numpy.zeros((self.n_flat_hkl,),algorithm.numpy_t)
        fhkl_imag = algorithm.numpy.zeros((self.n_flat_hkl,),algorithm.numpy_t)

        CUDA_fhkl(cuda.InOut(fhkl_real),
                 cuda.InOut(fhkl_imag),
                 cuda.In(self.flat_sites),
                 cuda.In(self.weights),
                 cuda.In(self.u_iso),
                 algorithm.numpy.uint32(self.scatterers.increasing_order[x]),
                 algorithm.numpy.uint32(self.scatterers.sorted_ranges[x][0]),
                 algorithm.numpy.uint32(self.scatterers.sorted_ranges[x][1]),
                 cuda.In(self.flat_mix),
                 block=(FHKL_BLOCKSIZE,1,1),
                 grid=((self.n_flat_hkl//FHKL_BLOCKSIZE,1)))

        intermediate_real += fhkl_real
        intermediate_imag += fhkl_imag

      flex_fhkl_real = flex.double(intermediate_real[0:len(self.miller_indices)].astype(algorithm.numpy.float64))
      flex_fhkl_imag = flex.double(intermediate_imag[0:len(self.miller_indices)].astype(algorithm.numpy.float64))

      instance._results = fcalc_container(flex.complex_double(flex_fhkl_real,flex_fhkl_imag))

      return
Example #48
0
  def build_up(self, objective_only=False):
    if self.f_mask is not None:
      f_mask = self.f_mask.data()
    else:
      f_mask = flex.complex_double()

    extinction_correction = self.reparametrisation.extinction
    if extinction_correction is None:
      extinction_correction = xray.dummy_extinction_correction()

    def args(scale_factor, weighting_scheme, objective_only):
      args = (self,
              self.observations,
              f_mask,
              weighting_scheme,
              scale_factor,
              self.one_h_linearisation,
              self.reparametrisation.jacobian_transpose_matching_grad_fc(),
              extinction_correction
              )
      if objective_only:
        args += (True,)
      return args

    if not self.finalised: #i.e. never been called
      self.reparametrisation.linearise()
      self.reparametrisation.store()
      scale_factor = self.initial_scale_factor
      if scale_factor is None: # we haven't got one from previous refinement
        result = ext.build_normal_equations(
          *args(scale_factor=None, weighting_scheme=sigma_weighting(),
                objective_only=True))
        scale_factor = self.scale_factor()
    else: # use scale factor from the previous step
      scale_factor = self.scale_factor()

    self.reset()
    result = ext.build_normal_equations(*args(scale_factor,
                                              self.weighting_scheme,
                                              objective_only))
    self.f_calc = self.observations.fo_sq.array(
      data=result.f_calc(), sigmas=None)
    self.fc_sq = self.observations.fo_sq.array(
      data=result.observables(), sigmas=None)\
        .set_observation_type_xray_intensity()
    self.weights = result.weights()
    self.objective_data_only = self.objective()
    self.chi_sq_data_only = self.chi_sq()
    if self.restraints_manager is not None:
      # Here we determine a normalisation factor to place the restraints on the
      # same scale as the average residual. This is the normalisation
      # factor suggested in Giacovazzo and similar to that used by shelxl.
      # (shelx manual, page 5-1).
      # The factor 2 comes from the fact that we minimize 1/2 sum w delta^2
      if self.restraints_normalisation_factor is None:
        self.restraints_normalisation_factor \
            = 2 * self.objective_data_only/(self.n_equations-self.n_parameters)
      linearised_eqns = self.restraints_manager.build_linearised_eqns(
        self.xray_structure, self.reparametrisation.parameter_map())
      jacobian = \
        self.reparametrisation.jacobian_transpose_matching(
          self.reparametrisation.mapping_to_grad_fc_all).transpose()
      self.reduced_problem().add_equations(
        linearised_eqns.deltas,
        linearised_eqns.design_matrix * jacobian,
        linearised_eqns.weights * self.restraints_normalisation_factor)
      self.n_restraints = linearised_eqns.n_restraints()
      self.chi_sq_data_and_restraints = self.chi_sq()
    if not objective_only:
      self.origin_fixing_restraint.add_to(
        self.step_equations(),
        self.reparametrisation.jacobian_transpose_matching_grad_fc(),
        self.reparametrisation.asu_scatterer_parameters)
Example #49
0
def tst_2d_zernike_mom(n,l, N=100, filename=None):
  nmax = max(20,n)
  rebuilt=open('rebuilt.dat','w')
  tt1=time.time()
  if(filename is not None):
    image=read_data(filename)
  else:
    image=generate_image(n,l)

  NP=int(smath.sqrt( image.size() ))
  N=NP/2
  grid_2d = math.two_d_grid(N, nmax)
  grid_2d.clean_space( image )
  grid_2d.construct_space_sum()
  tt2=time.time()
  print "time used: ", tt2-tt1
  zernike_2d_mom = math.two_d_zernike_moments( grid_2d, nmax )

  moments = zernike_2d_mom.moments()

  tt2=time.time()
  print "time used: ", tt2-tt1

  coefs = flex.real( moments )
  nl_array = math.nl_array( nmax )
  nls = nl_array.nl()
  nl_array.load_coefs( nls, coefs )
  lfg =  math.log_factorial_generator(nmax)

  print nl_array.get_coef(n,l)*2

  for nl, c in zip( nls, moments):
    if(abs(c)<1e-3):
      c=0
    print nl, c

  print
  reconst=flex.complex_double(NP**2, 0)
  for nl,c in zip( nls, moments):
    n=nl[0]
    l=nl[1]
    if(l>0):
      c=c*2
    #rzfa = math.zernike_2d_radial(n,l,lfg)
    rap = math.zernike_2d_polynome(n,l) #,rzfa)
    i=0
    for x in range(0,NP):
      x=x-N
      for y in range(0,NP):
        y=y-N
        rr = smath.sqrt(x*x+y*y)/N
        if rr>1.0:
          value=0.0
        else:
          tt = smath.atan2(y,x)
          value = rap.f(rr,tt)
        reconst[i]=reconst[i]+value*c
        i=i+1

  i = 0
  for x in range(0,NP):
    for y in range(0,NP):
      value=reconst[i].real
      if(value>0):
        print>>rebuilt, x,y,image[i][2],value
      i=i+1


  rebuilt.close()
 def fs(self):
   result = flex.complex_double()
   for hkl in self.miller_indices:
     result.append(structure_factor(
       xray_structure=self.xray_structure, hkl=hkl).f())
   return result
 def d2f_d_params(self):
   tphkl = 2 * math.pi * matrix.col(self.hkl)
   tphkl_outer = tphkl.outer_product()
   h,k,l = self.hkl
   d_exp_huh_d_u_star = matrix.col([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l])
   d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.outer_product()
   for scatterer in self.scatterers:
     assert scatterer.scattering_type == "const"
     w = scatterer.occupancy
     if (not scatterer.flags.use_u_aniso()):
       huh = scatterer.u_iso * self.d_star_sq
       dw = math.exp(mtps * huh)
     ffp = 1 + scatterer.fp
     fdp = scatterer.fdp
     ff = (ffp + 1j * fdp)
     d2_site_site = flex.complex_double(flex.grid(3,3), 0j)
     if (not scatterer.flags.use_u_aniso()):
       d2_site_u_iso = flex.complex_double(flex.grid(3,1), 0j)
       d2_site_u_star = None
     else:
       d2_site_u_iso = None
       d2_site_u_star = flex.complex_double(flex.grid(3,6), 0j)
     d2_site_occ = flex.complex_double(flex.grid(3,1), 0j)
     d2_site_fp = flex.complex_double(flex.grid(3,1), 0j)
     d2_site_fdp = flex.complex_double(flex.grid(3,1), 0j)
     if (not scatterer.flags.use_u_aniso()):
       d2_u_iso_u_iso = 0j
       d2_u_iso_occ = 0j
       d2_u_iso_fp = 0j
       d2_u_iso_fdp = 0j
     else:
       d2_u_star_u_star = flex.complex_double(flex.grid(6,6), 0j)
       d2_u_star_occ = flex.complex_double(flex.grid(6,1), 0j)
       d2_u_star_fp = flex.complex_double(flex.grid(6,1), 0j)
       d2_u_star_fdp = flex.complex_double(flex.grid(6,1), 0j)
     d2_occ_fp = 0j
     d2_occ_fdp = 0j
     for s in self.space_group:
       r = s.r().as_rational().as_float()
       s_site = s * scatterer.site
       alpha = matrix.col(s_site).dot(tphkl)
       if (scatterer.flags.use_u_aniso()):
         s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose()
         huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl))
         dw = math.exp(mtps * huh)
       e = cmath.exp(1j*alpha)
       site_gtmx = r.transpose()
       d2_site_site += flex.complex_double(
         site_gtmx *
           (w * dw * ff * e * (-1) * tphkl_outer)
              * site_gtmx.transpose())
       if (not scatterer.flags.use_u_aniso()):
         d2_site_u_iso += flex.complex_double(site_gtmx * (
           w * dw * ff * e * 1j * mtps * self.d_star_sq * tphkl))
       else:
         u_star_gtmx = matrix.sqr(tensor_rank_2_gradient_transform_matrix(r))
         d2_site_u_star += flex.complex_double(
             site_gtmx
           * ((w * dw * ff * e * 1j * tphkl).outer_product(
               mtps * d_exp_huh_d_u_star))
           * u_star_gtmx.transpose())
       d2_site_occ += flex.complex_double(site_gtmx * (
         dw * ff * e * 1j * tphkl))
       d2_site_fp += flex.complex_double(site_gtmx * (
         w * dw * e * 1j * tphkl))
       d2_site_fdp += flex.complex_double(site_gtmx * (
         w * dw * e * (-1) * tphkl))
       if (not scatterer.flags.use_u_aniso()):
         d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2
         d2_u_iso_occ += dw * ff * e * mtps * self.d_star_sq
         d2_u_iso_fp += w * dw * e * mtps * self.d_star_sq
         d2_u_iso_fdp += 1j * w * dw * e * mtps * self.d_star_sq
       else:
         d2_u_star_u_star += flex.complex_double(
             u_star_gtmx
           * (w * dw * ff * e * mtps**2 * d2_exp_huh_d_u_star_u_star)
           * u_star_gtmx.transpose())
         d2_u_star_occ += flex.complex_double(u_star_gtmx * (
           dw * ff * e * mtps * d_exp_huh_d_u_star))
         d2_u_star_fp += flex.complex_double(u_star_gtmx * (
           w * dw * e * mtps * d_exp_huh_d_u_star))
         d2_u_star_fdp += flex.complex_double(u_star_gtmx * (
           w * dw * 1j * e * mtps * d_exp_huh_d_u_star))
       d2_occ_fp += dw * e
       d2_occ_fdp += dw * e * 1j
     if (not scatterer.flags.use_u_aniso()):
       i_occ, i_fp, i_fdp, np = 4, 5, 6, 7
     else:
       i_occ, i_fp, i_fdp, np = 9, 10, 11, 12
     dp = flex.complex_double(flex.grid(np,np), 0j)
     paste = dp.matrix_paste_block_in_place
     paste(d2_site_site, 0,0)
     if (not scatterer.flags.use_u_aniso()):
       paste(d2_site_u_iso, 0,3)
       paste(d2_site_u_iso.matrix_transpose(), 3,0)
     else:
       paste(d2_site_u_star, 0,3)
       paste(d2_site_u_star.matrix_transpose(), 3,0)
     paste(d2_site_occ, 0,i_occ)
     paste(d2_site_occ.matrix_transpose(), i_occ,0)
     paste(d2_site_fp, 0,i_fp)
     paste(d2_site_fp.matrix_transpose(), i_fp,0)
     paste(d2_site_fdp, 0,i_fdp)
     paste(d2_site_fdp.matrix_transpose(), i_fdp,0)
     if (not scatterer.flags.use_u_aniso()):
       dp[3*7+3] = d2_u_iso_u_iso
       dp[3*7+4] = d2_u_iso_occ
       dp[4*7+3] = d2_u_iso_occ
       dp[3*7+5] = d2_u_iso_fp
       dp[5*7+3] = d2_u_iso_fp
       dp[3*7+6] = d2_u_iso_fdp
       dp[6*7+3] = d2_u_iso_fdp
     else:
       paste(d2_u_star_u_star, 3,3)
       paste(d2_u_star_occ, 3, 9)
       paste(d2_u_star_occ.matrix_transpose(), 9, 3)
       paste(d2_u_star_fp, 3, 10)
       paste(d2_u_star_fp.matrix_transpose(), 10, 3)
       paste(d2_u_star_fdp, 3, 11)
       paste(d2_u_star_fdp.matrix_transpose(), 11, 3)
     dp[i_occ*np+i_fp] = d2_occ_fp
     dp[i_fp*np+i_occ] = d2_occ_fp
     dp[i_occ*np+i_fdp] = d2_occ_fdp
     dp[i_fdp*np+i_occ] = d2_occ_fdp
     yield dp
 def __init__(
       self,
       crystal_gridding,
       fmodel,
       map_type,
       box_size_as_fraction=0.03,
       max_boxes=100,
       n_debias_cycles=2,
       neutral_volume_box_cushion_width=1,
       full_resolution_map=True,
       log=sys.stdout):
   self.crystal_gridding = crystal_gridding
   # assert compatibility of symops with griding
   assert self.crystal_gridding._symmetry_flags is not None
   self.sgt = fmodel.f_obs().space_group().type()
   self.zero_cmpl_ma = fmodel.f_calc().customized_copy(
     data = flex.complex_double(fmodel.f_calc().size(), 0))
   # embedded utility functions
   def get_map(fmodel, map_type, crystal_gridding, asu=True):
     f_map = fmodel.electron_density_map().map_coefficients(
       map_type                   = map_type,
       isotropize                 = True,
       exclude_free_r_reflections = True,
       fill_missing               = False)
     fft_map = cctbx.miller.fft_map(
       crystal_gridding     = crystal_gridding,
       fourier_coefficients = f_map)
     if(asu): return asu_map_ext.asymmetric_map(self.sgt,
       fft_map.real_map_unpadded()).data()
     else:
       return fft_map.real_map_unpadded()
   # f_model map
   f_model_map_data = fmodel.f_model_scaled_with_k1().fft_map(
     symmetry_flags   = maptbx.use_space_group_symmetry,
     crystal_gridding = self.crystal_gridding).real_map_unpadded()
   self.n_real = f_model_map_data.focus()
   # extract asu map from full P1
   f_model_map_data_asu=asu_map_ext.asymmetric_map(
     self.sgt, f_model_map_data).data()
   self.acc = f_model_map_data_asu.accessor()
   f_model_map_data_asu = f_model_map_data_asu.shift_origin()
   # set up boxes
   b = maptbx.boxes(
     n_real   = f_model_map_data_asu.focus(),
     fraction = box_size_as_fraction,
     max_boxes= max_boxes,
     log      = log)
   self.map_result_asu = flex.double(flex.grid(b.n_real))
   assert f_model_map_data_asu.focus()==b.n_real
   assert b.n_real==self.map_result_asu.focus()
   n_real_asu = b.n_real
   self.r = flex.double() # for regression test only
   n_boxes = len(b.starts)
   i_box = 0
   for s,e in zip(b.starts, b.ends):
     i_box+=1
     # define wide box: neutral + phased volumes
     if(neutral_volume_box_cushion_width>0):
       sh = neutral_volume_box_cushion_width
       ss = [max(s[i]-sh,0) for i in [0,1,2]]
       ee = [min(e[i]+sh,n_real_asu[i]) for i in [0,1,2]]
     else: ss,ee = s,e
     # omit wide box from f_model map, repeat n_debias_cycles times
     f_model_map_data_asu_ = f_model_map_data_asu.deep_copy()
     for i in xrange(n_debias_cycles):
       f_model_omit, f_model_map_data_asu_ = self.omit_box(s=ss, e=ee,
         md_asu=f_model_map_data_asu_)
     # get fmodel for omit map calculation
     fmodel_ = mmtbx.f_model.manager(
       f_obs        = fmodel.f_obs(),
       r_free_flags = fmodel.r_free_flags(),
       f_calc       = f_model_omit,
       f_mask       = self.zero_cmpl_ma)
     rw = fmodel_.r_work()
     self.r.append(rw) # for regression test only
     f_map_data_asu = get_map(fmodel=fmodel_, map_type=map_type,
       crystal_gridding=self.crystal_gridding)
     f_map_data_asu = f_map_data_asu.shift_origin()
     if(log):
       print >> log, "box %2d of %2d:"%(i_box, n_boxes), s, e, "%6.4f"%rw
     assert f_map_data_asu.focus() == self.map_result_asu.focus()
     maptbx.copy_box(
       map_data_from = f_map_data_asu,
       map_data_to   = self.map_result_asu,
       start         = s,
       end           = e)
   # result
   self.map_result_asu.reshape(self.acc)
   self.asu_map_omit = asu_map_ext.asymmetric_map(
     self.sgt, self.map_result_asu, self.n_real)
   self.map_coefficients = self.zero_cmpl_ma.customized_copy(
     indices = self.zero_cmpl_ma.indices(),
     data    = self.asu_map_omit.structure_factors(self.zero_cmpl_ma.indices()))
   # full resolution map (reflections in sphere, not in box!)
   if(full_resolution_map):
     cs = self.zero_cmpl_ma.complete_set(d_min=self.zero_cmpl_ma.d_min())
     asu_map_omit = asu_map_ext.asymmetric_map(
       self.sgt,self.map_result_asu,self.n_real)
     fill = self.zero_cmpl_ma.customized_copy(
       indices = cs.indices(),
       data    = asu_map_omit.structure_factors(cs.indices()))
     self.map_coefficients = self.map_coefficients.complete_with(
       other=fill, scale=True)
Example #53
0
def lewis(template, img):
    """The lewis() function computes the normalised cross-correlation
  (NCC) of an image, @p img, and a template, @p template.  Both image
  and template must be two-dimensional, real, and finite.  The image
  must be larger than the template, which in turn should contain more
  than one pixel, and the template must have positive variance.  The
  function returns the correlation coefficients in the range [-1, +1].
  See Lewis, J. P. (1995) "Fast Template Matching", Vision Interface,
  120-123.

  @note This function should be equivalent to MATLAB's normxcorr2()
        function.

  @param img      Two-dimensional intensity image
  @param template Two-dimensional intensity template
  @return         Correlation coefficients
  """

    import math
    import numpy
    from sys import float_info

    from scitbx import fftpack
    from scitbx.array_family import flex

    # Assert that image and template are two-dimensional arrays, and
    # that the template is no larger than image.  Assert that template
    # is not flat.  XXX Check for real and finite, too?
    assert len(img.focus()) == 2 and len(template.focus()) == 2
    assert img.focus()[0] >= template.focus()[0] and img.focus()[1] >= template.focus()[1]
    assert template.sample_standard_deviation() > 0

    # For conformance with MATLAB's normxcorr2() and geck 342320: for
    # numerical robustness, ensure that both image and template are
    # always non-negative.
    img_nn = img - min(0, flex.min(img))
    template_nn = template - min(0, flex.min(template))

    # Calculate the terms of the denominator of gamma.  Must guard
    # against negative variance of the image due to inaccuracies in the
    # one-pass formula.
    img_sum = _summed_area_table(img_nn, template_nn.focus()[0], template_nn.focus()[1])
    img_ssq = _summed_area_table(flex.pow2(img_nn), template_nn.focus()[0], template_nn.focus()[1])

    f_sigma = img_ssq - img_sum * img_sum / (template_nn.focus()[0] * template_nn.focus()[1])
    f_sigma.set_selected(f_sigma < 0, 0)
    f_sigma = flex.sqrt(f_sigma)
    t_sigma = (template_nn - flex.mean(template_nn)).norm()

    gamma_denominator = f_sigma * t_sigma

    # Zero-pad the image to permit partial overlap of template and
    # image, and embed the time-reversed template in a zero-padded array
    # of the same size.  Zero-padding means the entire template is
    # always overlapping the image, and terms involving the template
    # mean cancel in the expansion of the numerator of gamma.
    #
    # Note: the NCC demands the template to be time-reversed, which can
    # be accomplished by conjugation in the frequency domain.  An
    # implementation following that approach would however require
    # special care to be taken for the first rows and columns:
    #
    #   from numpy import roll
    #   t_embed.matrix_paste_block_in_place(
    #     block=template_nn,
    #     i_row=full[0] - template_nn.focus()[0],
    #     i_column=full[1] - template_nn.focus()[1])
    #   t_embed = flex.double(roll(
    #     roll(t_embed.as_numpy_array(), 1, axis=0), 1, axis=1))
    #
    # Calculate correlation in frequency domain.  XXX Could use spatial
    # domain calculation in cases where it's faster (see MATLAB's
    # implementation).
    full = (img_nn.focus()[0] + template_nn.focus()[0] - 1, img_nn.focus()[1] + template_nn.focus()[1] - 1)

    f_embed = flex.double(flex.grid(full))
    f_embed.matrix_paste_block_in_place(block=img_nn, i_row=0, i_column=0)
    f_prime = flex.complex_double(reals=f_embed, imags=flex.double(flex.grid(full)))

    t_embed = flex.double(flex.grid(full))
    t_embed.matrix_paste_block_in_place(block=template_nn.matrix_rot90(2), i_row=0, i_column=0)
    t_prime = flex.complex_double(reals=t_embed, imags=flex.double(flex.grid(full)))

    fft = fftpack.complex_to_complex_2d(full)
    fft.forward(f_prime)
    fft.forward(t_prime)
    gamma_numerator = f_prime * t_prime
    fft.backward(gamma_numerator)
    gamma_numerator = flex.real(gamma_numerator) / (fft.n()[0] * fft.n()[1]) - img_sum * flex.mean(template_nn)

    # For conformance with MATLAB: set the NCC to zero in regions where
    # the image has zero variance over the extent of the template.  If,
    # due to small variances in the image or the template, a correlation
    # coefficient falls outside the range [-1, 1], set it to zero to
    # reflect the undefined 0/0 condition.
    tol = math.sqrt(math.ldexp(float_info.epsilon, math.frexp(flex.max(flex.abs(gamma_denominator)))[1] - 1))
    sel = gamma_denominator <= tol
    gamma = gamma_numerator.set_selected(sel, 0) / gamma_denominator.set_selected(sel, 1)
    gamma.set_selected(flex.abs(gamma) > 1 + math.sqrt(float_info.epsilon), 0)

    return gamma
    output_labels = F-obs(+) SIGF-obs(+) F-obs(-) SIGF-obs(-)
  }
  miller_array {
    file_name = tst_data9.mtz
    labels = R-free-flags
    output_labels = R-free-flags
  }
}""")
  params = master_phil.fetch(source=new_phil).extract()
  miller_arrays = run_and_reload(params, "tst9.mtz")
  assert approx_equal(miller_arrays[0].info().wavelength, 1.54)
  # map coefficients
  n_refl = len(array2.indices())
  d1 = flex.random_double(n_refl)
  d2 = flex.random_double(n_refl)
  coeffs_data = flex.complex_double(d1, d2)
  map_coeffs = array2.customized_copy(data=coeffs_data,
    sigmas=None).average_bijvoet_mates()
  map_coeffs.as_mtz_dataset(column_root_label="2FOFCWT").mtz_object().write(
    "tst_data10.mtz")
  new_phil = libtbx.phil.parse("""
mtz_file {
  output_file = tst10.mtz
  crystal_symmetry.space_group = P212121
  crystal_symmetry.unit_cell = 6,7,8,90,90,90
  miller_array {
    file_name = tst_data10.mtz
    labels = 2FOFCWT,PHI2FOFCWT
    output_labels = 2FOFCWT PHI2FOFCWT
  }
}""")
 def d2f_d_params_diag(self):
   tphkl = 2 * math.pi * flex.double(self.hkl)
   tphkl_outer = tphkl.matrix_outer_product(tphkl) \
     .matrix_symmetric_as_packed_u()
   h,k,l = self.hkl
   d_exp_huh_d_u_star = flex.double([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l])
   d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.matrix_outer_product(
     d_exp_huh_d_u_star).matrix_symmetric_as_packed_u()
   for i_scatterer,scatterer in enumerate(self.scatterers):
     site_symmetry_ops = None
     if (self.site_symmetry_table.is_special_position(i_scatterer)):
       site_symmetry_ops = self.site_symmetry_table.get(i_scatterer)
       site_constraints = site_symmetry_ops.site_constraints()
       if (scatterer.flags.use_u_aniso()):
         adp_constraints = site_symmetry_ops.adp_constraints()
     w = scatterer.weight()
     if (not scatterer.flags.use_u_aniso()):
       huh = scatterer.u_iso * self.d_star_sq
       dw = math.exp(mtps * huh)
     gaussian = self.scattering_type_registry.gaussian_not_optional(
       scattering_type=scatterer.scattering_type)
     f0 = gaussian.at_d_star_sq(self.d_star_sq)
     ffp = f0 + scatterer.fp
     fdp = scatterer.fdp
     ff = (ffp + 1j * fdp)
     d2_site_site = flex.complex_double(3*(3+1)//2, 0j)
     if (not scatterer.flags.use_u_aniso()):
       d2_u_iso_u_iso = 0j
     else:
       d2_u_star_u_star = flex.complex_double(6*(6+1)//2, 0j)
     for s in self.space_group:
       r = s.r().as_rational().as_float()
       s_site = s * scatterer.site
       alpha = tphkl.dot(flex.double(s_site))
       if (scatterer.flags.use_u_aniso()):
         s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose()
         huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl))
         dw = math.exp(mtps * huh)
       e = cmath.exp(1j*alpha)
       site_gtmx = flex.double(r.transpose())
       site_gtmx.reshape(flex.grid(3,3))
       d2_site_site += (w * dw * ff * e * (-1)) * (
         site_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose(
           tphkl_outer))
       if (not scatterer.flags.use_u_aniso()):
         d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2
       else:
         u_star_gtmx = tensor_rank_2_gradient_transform_matrix(r)
         d2_u_star_u_star +=(w * dw * ff * e * mtps**2) \
           * u_star_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose(
               d2_exp_huh_d_u_star_u_star)
     if (site_symmetry_ops is None):
       i_u = 3
     else:
       i_u = site_constraints.n_independent_params()
     if (not scatterer.flags.use_u_aniso()):
       i_occ = i_u + 1
     elif (site_symmetry_ops is None):
       i_occ = i_u + 6
     else:
       i_occ = i_u + adp_constraints.n_independent_params()
     np = i_occ+3
     if (site_symmetry_ops is not None):
       gsm = site_constraints.gradient_sum_matrix()
       d2_site_site = gsm.matrix_multiply_packed_u_multiply_lhs_transpose(
         packed_u=d2_site_site)
       if (scatterer.flags.use_u_aniso()):
         gsm = adp_constraints.gradient_sum_matrix()
         d2_u_star_u_star = gsm \
           .matrix_multiply_packed_u_multiply_lhs_transpose(
             packed_u=d2_u_star_u_star)
     #
     dpd = flex.complex_double(flex.grid(np,1), 0j)
     def paste(d, i):
       d.reshape(flex.grid(d.size(),1))
       dpd.matrix_paste_block_in_place(d, i,0)
     paste(d2_site_site.matrix_packed_u_diagonal(), 0)
     if (not scatterer.flags.use_u_aniso()):
       dpd[i_u] = d2_u_iso_u_iso
     else:
       paste(d2_u_star_u_star.matrix_packed_u_diagonal(), i_u)
     yield dpd
Example #56
0
def exercise(mt, n_refl, log):
    f_obs = mt.random_double(size=n_refl)
    f_calc = flex.complex_double(mt.random_double(size=f_obs.size()), mt.random_double(size=f_obs.size()))
    f_calc_abs = flex.abs(f_calc)
    trg = r1.target(f_obs=f_obs, f_calc=f_calc)

    def check_f_calc_abs_derivs():
        eps = 1e-6
        g_fin = flex.double()
        c_fin = flex.double()
        for ih in xrange(f_calc_abs.size()):
            fs = []
            gs = []
            c_orig = f_calc_abs[ih]
            for signed_eps in [eps, -eps]:
                f_calc_abs[ih] = c_orig + signed_eps
                trg_eps = r1.target(f_obs=f_obs, f_calc_abs=f_calc_abs)
                fs.append(trg_eps.t)
                gs.append(trg_eps.g[ih])
            g_fin.append((fs[0] - fs[1]) / (2 * eps))
            c_fin.append((gs[0] - gs[1]) / (2 * eps))
            f_calc_abs[ih] = c_orig
        print >> log, "g fin:", numstr(g_fin)
        print >> log, "  ana:", numstr(trg.g)
        assert approx_equal(trg.g, g_fin)
        print >> log, "c fin:", numstr(c_fin)
        print >> log, "  ana:", numstr(trg.c)
        assert approx_equal(trg.c, c_fin)

    def check_f_calc_derivs():
        eps = 1e-6
        g_fin = flex.complex_double()
        c_fin = flex.vec3_double()
        for ih in xrange(f_calc.size()):
            c_orig = f_calc[ih]
            g_fin_ab = []
            c_fin_ab = []
            for iab in [0, 1]:
                fs = []
                gs = []
                for signed_eps in [eps, -eps]:
                    if iab == 0:
                        f_calc[ih] = complex(c_orig.real + signed_eps, c_orig.imag)
                    else:
                        f_calc[ih] = complex(c_orig.real, c_orig.imag + signed_eps)
                    trg_eps = r1.target(f_obs=f_obs, f_calc=f_calc)
                    fs.append(trg_eps.t)
                    gs.append(trg_eps.f_calc_gradients[ih])
                g_fin_ab.append((fs[0] - fs[1]) / (2 * eps))
                c_fin_ab.append((gs[0] - gs[1]) / (2 * eps))
            g_fin.append(complex(*g_fin_ab))
            assert approx_equal(c_fin_ab[0].imag, c_fin_ab[1].real)
            c_fin.append((c_fin_ab[0].real, c_fin_ab[1].imag, c_fin_ab[0].imag))
            f_calc[ih] = c_orig
        for pn, f, a in zip(g_fin.part_names(), g_fin.parts(), trg.f_calc_gradients.parts()):
            print >> log, "g fin %s:" % pn, numstr(f)
            print >> log, "  ana %s:" % pn, numstr(a)
        assert approx_equal(trg.f_calc_gradients, g_fin)
        for pn, f, a in zip(["aa", "bb", "ab"], c_fin.parts(), trg.f_calc_hessians.parts()):
            print >> log, "c fin %s:" % pn, numstr(f)
            print >> log, "  ana %s:" % pn, numstr(a)
        assert approx_equal(trg.f_calc_hessians, c_fin)

    check_f_calc_abs_derivs()
    check_f_calc_derivs()
    #
    f_calc[0] = 0j
    f_calc_abs = flex.abs(f_calc)
    trg = r1.target(f_obs=f_obs, f_calc=f_calc)
    assert trg.gradients_work()[0] == 0j
    assert trg.hessians_work()[0] == (0, 0, 0)
Example #57
0
 def __init__(self,
              f_calc,
              f_masks=None,
              f_part1=None,
              f_part2=None,
              k_masks=None,
              k_isotropic_exp=None,
              k_isotropic=None,
              k_anisotropic=None):
   adopt_init_args(self, locals())
   if(self.f_masks is None):
     self.f_masks = [self.f_calc.customized_copy(
       data=flex.complex_double(f_calc.data().size(), 0))]
   else:
     if(not (type(self.f_masks) in [list, tuple])):
       self.f_masks = [self.f_masks]
     for fm in self.f_masks:
       assert self.f_calc.indices().all_eq(fm.indices())
   if(self.k_isotropic_exp is not None):
     assert self.k_isotropic_exp.size() == self.f_calc.indices().size()
   else: self.k_isotropic_exp = flex.double(f_calc.data().size(), 1)
   if(self.k_isotropic is not None):
     assert self.k_isotropic.size() == self.f_calc.indices().size()
   else: self.k_isotropic = flex.double(f_calc.data().size(), 1)
   if(self.k_anisotropic is not None):
     assert self.k_anisotropic.size() == self.f_calc.indices().size()
   else: self.k_anisotropic = flex.double(f_calc.data().size(), 1)
   if(self.k_masks is None):
     n=len(self.f_masks)
     self.k_masks = [flex.double(f_calc.data().size(), 0)]*n
   else:
     if(not (type(self.k_masks) in [list, tuple])):
       self.k_masks = [self.k_masks]
   if(self.f_part1 is not None):
     assert self.f_calc.indices().all_eq(self.f_part1.indices())
   else:
     self.f_part1 = self.f_calc.customized_copy(
       data=flex.complex_double(f_calc.data().size(), 0))
   if(self.f_part2 is not None):
     assert self.f_calc.indices().all_eq(self.f_part2.indices())
   else:
     self.f_part2 = self.f_calc.customized_copy(
       data=flex.complex_double(f_calc.data().size(), 0))
   # assemble f_bulk
   f_bulk_data = flex.complex_double(f_calc.data().size(), 0)
   assert len(self.k_masks) == len(self.f_masks)
   for i in xrange(len(self.k_masks)):
     f_bulk_data += self.k_masks[i]*self.f_masks[i].data()
   #
   self.data = ext.data(
     f_calc          = self.f_calc.data(),
     f_bulk          = f_bulk_data,
     k_isotropic_exp = self.k_isotropic_exp,
     k_isotropic     = self.k_isotropic,
     k_anisotropic   = self.k_anisotropic,
     f_part1         = self.f_part1.data(),
     f_part2         = self.f_part2.data())
   self.f_model = miller.array(miller_set=self.f_calc, data=self.data.f_model)
   self.f_model_no_aniso_scale = miller.array(
     miller_set=self.f_calc,
     data      =self.data.f_model_no_aniso_scale)
 def d2f_d_params(self):
   tphkl = 2 * math.pi * flex.double(self.hkl)
   tphkl_outer = tphkl.matrix_outer_product(tphkl) \
     .matrix_symmetric_as_packed_u()
   h,k,l = self.hkl
   d_exp_huh_d_u_star = flex.double([h**2, k**2, l**2, 2*h*k, 2*h*l, 2*k*l])
   d2_exp_huh_d_u_star_u_star = d_exp_huh_d_u_star.matrix_outer_product(
     d_exp_huh_d_u_star).matrix_symmetric_as_packed_u()
   for i_scatterer,scatterer in enumerate(self.scatterers):
     site_symmetry_ops = None
     if (self.site_symmetry_table.is_special_position(i_scatterer)):
       site_symmetry_ops = self.site_symmetry_table.get(i_scatterer)
       site_constraints = site_symmetry_ops.site_constraints()
       if (scatterer.flags.use_u_aniso()):
         adp_constraints = site_symmetry_ops.adp_constraints()
     w = scatterer.weight()
     wwo = scatterer.weight_without_occupancy()
     if (not scatterer.flags.use_u_aniso()):
       huh = scatterer.u_iso * self.d_star_sq
       dw = math.exp(mtps * huh)
     gaussian = self.scattering_type_registry.gaussian_not_optional(
       scattering_type=scatterer.scattering_type)
     f0 = gaussian.at_d_star_sq(self.d_star_sq)
     ffp = f0 + scatterer.fp
     fdp = scatterer.fdp
     ff = (ffp + 1j * fdp)
     d2_site_site = flex.complex_double(3*(3+1)//2, 0j)
     if (not scatterer.flags.use_u_aniso()):
       d2_site_u_iso = flex.complex_double(flex.grid(3,1), 0j)
       d2_site_u_star = None
     else:
       d2_site_u_iso = None
       d2_site_u_star = flex.complex_double(flex.grid(3,6), 0j)
     d2_site_occ = flex.complex_double(flex.grid(3,1), 0j)
     d2_site_fp = flex.complex_double(flex.grid(3,1), 0j)
     d2_site_fdp = flex.complex_double(flex.grid(3,1), 0j)
     if (not scatterer.flags.use_u_aniso()):
       d2_u_iso_u_iso = 0j
       d2_u_iso_occ = 0j
       d2_u_iso_fp = 0j
       d2_u_iso_fdp = 0j
     else:
       d2_u_star_u_star = flex.complex_double(6*(6+1)//2, 0j)
       d2_u_star_occ = flex.complex_double(flex.grid(6,1), 0j)
       d2_u_star_fp = flex.complex_double(flex.grid(6,1), 0j)
       d2_u_star_fdp = flex.complex_double(flex.grid(6,1), 0j)
     d2_occ_fp = 0j
     d2_occ_fdp = 0j
     for s in self.space_group:
       r = s.r().as_rational().as_float()
       s_site = s * scatterer.site
       alpha = tphkl.dot(flex.double(s_site))
       if (scatterer.flags.use_u_aniso()):
         s_u_star_s = r*matrix.sym(sym_mat3=scatterer.u_star)*r.transpose()
         huh = (matrix.row(self.hkl) * s_u_star_s).dot(matrix.col(self.hkl))
         dw = math.exp(mtps * huh)
       e = cmath.exp(1j*alpha)
       site_gtmx = flex.double(r.transpose())
       site_gtmx.reshape(flex.grid(3,3))
       d2_site_site += (w * dw * ff * e * (-1)) * (
         site_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose(
           tphkl_outer))
       if (not scatterer.flags.use_u_aniso()):
         d2_site_u_iso += (w * dw * ff * e * 1j * mtps * self.d_star_sq) \
           * site_gtmx.matrix_multiply(tphkl)
       else:
         u_star_gtmx = tensor_rank_2_gradient_transform_matrix(r)
         d2_site_u_star += (w * dw * ff * e * 1j * mtps) \
           * site_gtmx.matrix_multiply(
               tphkl.matrix_outer_product(d_exp_huh_d_u_star)) \
                 .matrix_multiply(u_star_gtmx.matrix_transpose())
       site_gtmx_tphkl = site_gtmx.matrix_multiply(tphkl)
       d2_site_occ += (wwo * dw * ff * e * 1j) * site_gtmx_tphkl
       d2_site_fp += (w * dw * e * 1j) * site_gtmx_tphkl
       d2_site_fdp += (w * dw * e * (-1)) * site_gtmx_tphkl
       if (not scatterer.flags.use_u_aniso()):
         d2_u_iso_u_iso += w * dw * ff * e * (mtps * self.d_star_sq)**2
         d2_u_iso_occ += wwo * dw * ff * e * mtps * self.d_star_sq
         d2_u_iso_fp += w * dw * e * mtps * self.d_star_sq
         d2_u_iso_fdp += 1j * w * dw * e * mtps * self.d_star_sq
       else:
         d2_u_star_u_star +=(w * dw * ff * e * mtps**2) \
           * u_star_gtmx.matrix_multiply_packed_u_multiply_lhs_transpose(
               d2_exp_huh_d_u_star_u_star)
         u_star_gtmx_d_exp_huh_d_u_star = u_star_gtmx.matrix_multiply(
           d_exp_huh_d_u_star)
         d2_u_star_occ += (wwo * dw * ff * e * mtps) \
           * u_star_gtmx_d_exp_huh_d_u_star
         d2_u_star_fp += (w * dw * e * mtps) \
           * u_star_gtmx_d_exp_huh_d_u_star
         d2_u_star_fdp += (w * dw * 1j * e * mtps) \
           * u_star_gtmx_d_exp_huh_d_u_star
       d2_occ_fp += wwo * dw * e
       d2_occ_fdp += wwo * dw * e * 1j
     if (site_symmetry_ops is None):
       i_u = 3
     else:
       i_u = site_constraints.n_independent_params()
     if (not scatterer.flags.use_u_aniso()):
       i_occ = i_u + 1
     elif (site_symmetry_ops is None):
       i_occ = i_u + 6
     else:
       i_occ = i_u + adp_constraints.n_independent_params()
     i_fp, i_fdp, np = i_occ+1, i_occ+2, i_occ+3
     if (site_symmetry_ops is not None):
       gsm = site_constraints.gradient_sum_matrix()
       d2_site_site = gsm.matrix_multiply_packed_u_multiply_lhs_transpose(
         packed_u=d2_site_site)
       if (not scatterer.flags.use_u_aniso()):
         d2_site_u_iso = gsm.matrix_multiply(d2_site_u_iso)
       else:
         d2_site_u_star = gsm.matrix_multiply(d2_site_u_star)
       d2_site_occ = gsm.matrix_multiply(d2_site_occ)
       d2_site_fp = gsm.matrix_multiply(d2_site_fp)
       d2_site_fdp = gsm.matrix_multiply(d2_site_fdp)
       if (scatterer.flags.use_u_aniso()):
         gsm = adp_constraints.gradient_sum_matrix()
         d2_site_u_star = d2_site_u_star.matrix_multiply(
           gsm.matrix_transpose())
         d2_u_star_u_star = gsm \
           .matrix_multiply_packed_u_multiply_lhs_transpose(
             packed_u=d2_u_star_u_star)
         d2_u_star_occ = gsm.matrix_multiply(d2_u_star_occ)
         d2_u_star_fp = gsm.matrix_multiply(d2_u_star_fp)
         d2_u_star_fdp = gsm.matrix_multiply(d2_u_star_fdp)
     dp = flex.complex_double(flex.grid(np,np), 0j)
     paste = dp.matrix_paste_block_in_place
     paste(d2_site_site.matrix_packed_u_as_symmetric(), 0,0)
     if (not scatterer.flags.use_u_aniso()):
       paste(d2_site_u_iso, 0,i_u)
       paste(d2_site_u_iso.matrix_transpose(), i_u,0)
     else:
       paste(d2_site_u_star, 0,i_u)
       paste(d2_site_u_star.matrix_transpose(), i_u,0)
     paste(d2_site_occ, 0,i_occ)
     paste(d2_site_occ.matrix_transpose(), i_occ,0)
     paste(d2_site_fp, 0,i_fp)
     paste(d2_site_fp.matrix_transpose(), i_fp,0)
     paste(d2_site_fdp, 0,i_fdp)
     paste(d2_site_fdp.matrix_transpose(), i_fdp,0)
     if (not scatterer.flags.use_u_aniso()):
       dp[i_u*np+i_u] = d2_u_iso_u_iso
       dp[i_u*np+i_occ] = d2_u_iso_occ
       dp[i_occ*np+i_u] = d2_u_iso_occ
       dp[i_u*np+i_fp] = d2_u_iso_fp
       dp[i_fp*np+i_u] = d2_u_iso_fp
       dp[i_u*np+i_fdp] = d2_u_iso_fdp
       dp[i_fdp*np+i_u] = d2_u_iso_fdp
     else:
       paste(d2_u_star_u_star.matrix_packed_u_as_symmetric(), i_u, i_u)
       paste(d2_u_star_occ, i_u, i_occ)
       paste(d2_u_star_occ.matrix_transpose(), i_occ, i_u)
       paste(d2_u_star_fp, i_u, i_fp)
       paste(d2_u_star_fp.matrix_transpose(), i_fp, i_u)
       paste(d2_u_star_fdp, i_u, i_fdp)
       paste(d2_u_star_fdp.matrix_transpose(), i_fdp, i_u)
     dp[i_occ*np+i_fp] = d2_occ_fp
     dp[i_fp*np+i_occ] = d2_occ_fp
     dp[i_occ*np+i_fdp] = d2_occ_fdp
     dp[i_fdp*np+i_occ] = d2_occ_fdp
     yield dp
Example #59
0
 def anisotropic_scaling(self, r_start):
   r_expanal, r_poly, r_expmin = None,None,None
   k_anisotropic_expanal, k_anisotropic_poly, \
     k_anisotropic_expmin = None, None, None
   scale_matrix_expanal, scale_matrix_poly, scale_matrix_expmin= None,None,None
   sel         = self.selection_work.data()
   f_model_abs = flex.abs(self.core.f_model_no_aniso_scale.data().select(sel))
   f_obs       = self.f_obs.data().select(sel)
   mi          = self.f_obs.indices().select(sel)
   uc          = self.f_obs.unit_cell()
   mi_all      = self.f_obs.indices()
   # try exp_anal
   if(self.try_expanal):
     obj = bulk_solvent.aniso_u_scaler(
       f_model_abs    = f_model_abs,
       f_obs          = f_obs,
       miller_indices = mi,
       adp_constraint_matrix = self.adp_constraints.gradient_sum_matrix())
     u_star = self.adp_constraints.all_params(tuple(obj.u_star_independent))
     scale_matrix_expanal = adptbx.u_as_b(adptbx.u_star_as_u_cart(uc, u_star))
     k_anisotropic_expanal = ext.k_anisotropic(mi_all, u_star)
     r_expanal = self.try_scale(k_anisotropic = k_anisotropic_expanal)
     if(self.verbose):
       print >> self.log, "      r_expanal: %6.4f"%r_expanal
   # try poly
   if(self.try_poly):
     obj = bulk_solvent.aniso_u_scaler(
       f_model_abs    = f_model_abs,
       f_obs          = f_obs,
       miller_indices = mi,
       unit_cell      = uc)
     scale_matrix_poly = obj.a
     k_anisotropic_poly = ext.k_anisotropic(mi_all, obj.a, uc)
     r_poly = self.try_scale(k_anisotropic = k_anisotropic_poly)
     if(self.verbose):
       print >> self.log, "      r_poly   : %6.4f"%r_poly
   # pre-analyze
   force_to_use_expmin=False
   if(k_anisotropic_poly is not None and self.auto and r_poly<r_expanal and
      (k_anisotropic_poly<=0).count(True)>0):
     force_to_use_expmin = True
     self.try_expmin = True
   # try expmin
   if(self.try_expmin):
     zero = self.f_obs.select(sel).customized_copy(data =
       flex.complex_double(f_obs.size(), 0))
     fm = mmtbx.f_model.manager_kbu(
       f_obs         = self.f_obs.select(sel),
       f_calc        = self.core.f_model_no_aniso_scale.select(sel),
       f_masks       = [zero],
       f_part1       = zero,
       f_part2       = zero,
       ss            = self.ss)
     obj = kbu_refinery.tgc(
       f_obs   = self.f_obs.select(sel),
       f_calc  = self.core.f_model_no_aniso_scale.select(sel),
       f_masks = [zero],
       ss      = self.ss,
       k_sols  = [0,],
       b_sols  = [0,],
       u_star  = [0,0,0,0,0,0])
     obj.minimize_u()
     u_star = obj.kbu.u_star()
     scale_matrix_expmin = adptbx.u_as_b(adptbx.u_star_as_u_cart(uc, u_star))
     k_anisotropic_expmin = ext.k_anisotropic(mi_all, u_star)
     r_expmin = self.try_scale(k_anisotropic = k_anisotropic_expmin)
     if(self.verbose): print >> self.log, "    r_expmin   : %6.4f"%r_expmin
     if(force_to_use_expmin):
       self.core = self.core.update(k_anisotropic = k_anisotropic_expmin)
       if(self.verbose):
         self.format_scale_matrix(m=scale_matrix_expmin)
       return
   # select best
   r = [(r_expanal, k_anisotropic_expanal, scale_matrix_expanal),
        (r_poly,    k_anisotropic_poly,    scale_matrix_poly),
        (r_expmin,  k_anisotropic_expmin,  scale_matrix_expmin)]
   r_best = r_start
   k_anisotropic_best = None
   scale_matrix_best = None
   for result in r:
     r_factor, k_anisotropic, scale_matrix = result
     if(r_factor is not None and r_factor < r_best):
       r_best = r_factor
       k_anisotropic_best = k_anisotropic.deep_copy()
       scale_matrix_best = scale_matrix[:]
   if(scale_matrix_best is None):
     if(self.verbose):
       print >> self.log, "      result rejected due to r-factor increase"
   else:
     self.scale_matrices = scale_matrix_best
     self.core = self.core.update(k_anisotropic = k_anisotropic_best)
     r_aniso = self.r_factor()
     if(self.verbose):
       self.format_scale_matrix()
       print >> self.log, "      r_final  : %6.4f"%r_aniso