예제 #1
0
def exercise_complex_to_complex_3d():
  print "complex_to_complex_3d"
  for n_complex,n_repeats in [((100,80,90),2), ((200,160,180),1)]:
    print "  dimensions:", n_complex
    print "  repeats:", n_repeats
    np = n_complex[0]*n_complex[1]*n_complex[2]
    d0 = (flex.random_double(size=np)*2-1) * flex.polar(
      1, flex.random_double(size=np)*2-1)
    d0.reshape(flex.grid(n_complex))
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
    overhead = time.time()-t0
    print "    overhead: %.2f seconds" % overhead
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      fftw3tbx.complex_to_complex_3d_in_place(data=d, exp_sign=-1)
      fftw3tbx.complex_to_complex_3d_in_place(data=d, exp_sign=+1)
    print "    fftw:     %.2f seconds" % (time.time()-t0-overhead)
    rw = d / np
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      fftpack.complex_to_complex_3d(n_complex).forward(d)
      fftpack.complex_to_complex_3d(n_complex).backward(d)
    print "    fftpack:  %.2f seconds" % (time.time()-t0-overhead)
    sys.stdout.flush()
    rp = d / np
    #
    assert flex.max(flex.abs(rw-rp)) < 1.e-6
예제 #2
0
def exercise_complex_to_complex_3d():
    from scitbx.array_family import flex
    from cudatbx import cufft
    from scitbx import fftpack
    import time
    import sys
    print ""
    print "complex_to_complex_3d"
    for n_complex, n_repeats in [((100, 80, 90), 16), ((200, 160, 180), 16)]:
        print "  dimensions:", n_complex
        print "  repeats:", n_repeats
        np = n_complex[0] * n_complex[1] * n_complex[2]
        d0 = flex.polar(
            flex.random_double(size=np) * 2 - 1,
            flex.random_double(size=np) * 2 - 1)
        d0.reshape(flex.grid(n_complex))
        #
        t0 = time.time()
        for i_trial in xrange(n_repeats):
            d = d0.deep_copy()
        overhead = time.time() - t0
        print "    overhead: %.2f seconds" % overhead
        #
        # XXX extra CuFFT to initialize device - can we avoid this somehow?
        d = d0.deep_copy()
        cufft.complex_to_complex_3d(n_complex).forward(d)
        cufft.complex_to_complex_3d(n_complex).backward(d)
        # benchmarking run
        t0 = time.time()
        for i_trial in xrange(n_repeats):
            d = d0.deep_copy()
            cufft.complex_to_complex_3d(n_complex).forward(d)
            cufft.complex_to_complex_3d(n_complex).backward(d)
        print "    cufft:    %6.2f seconds" % (
            (time.time() - t0 - overhead) / n_repeats)
        rw = d / np
        #
        t0 = time.time()
        for i_trial in xrange(n_repeats):
            d = d0.deep_copy()
            fftpack.complex_to_complex_3d(n_complex).forward(d)
            fftpack.complex_to_complex_3d(n_complex).backward(d)
        print "    fftpack:  %6.2f seconds" % (
            (time.time() - t0 - overhead) / n_repeats)
        sys.stdout.flush()
        rp = d / np
        #
        print ""
        assert flex.max(flex.abs(rw - rp)) < 1.e-6
예제 #3
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 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, 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)
def exercise_f000():
    miller_indices = flex.miller_index([(0, 0, 0)])
    data = flex.complex_double([1 - 2j])
    n_real = [1, 2, 3]
    conjugate_flag = True
    for hall_symbol in ["P 1", "P 3", "R 3*"]:
        for is_centric in [False, True]:
            if (not is_centric):
                space_group = sgtbx.space_group(hall_symbol)
            else:
                space_group.expand_smx("-x,-y,-z")
            for anomalous_flag in [False, True]:
                if (not anomalous_flag):
                    rfft = fftpack.real_to_complex_3d(n_real)
                    n_complex = rfft.n_complex()
                else:
                    cfft = fftpack.complex_to_complex_3d(n_real)
                    n_complex = cfft.n()
                for treat_restricted in [False, True]:
                    map = maptbx.structure_factors.to_map(
                        space_group=space_group,
                        anomalous_flag=anomalous_flag,
                        miller_indices=miller_indices,
                        structure_factors=data,
                        n_real=n_real,
                        map_grid=flex.grid(n_complex),
                        conjugate_flag=conjugate_flag,
                        treat_restricted=treat_restricted)
                    if (treat_restricted):
                        assert approx_equal(map.complex_map()[0], data[0])
                    else:
                        assert approx_equal(map.complex_map()[0],
                                            data[0] * space_group.order_p())
예제 #5
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
def exercise_f000():
  miller_indices = flex.miller_index([(0,0,0)])
  data = flex.complex_double([1-2j])
  n_real = [1,2,3]
  conjugate_flag = True
  for hall_symbol in ["P 1", "P 3", "R 3*"]:
    for is_centric in [False, True]:
      if (not is_centric):
        space_group = sgtbx.space_group(hall_symbol)
      else:
        space_group.expand_smx("-x,-y,-z")
      for anomalous_flag in [False, True]:
        if (not anomalous_flag):
          rfft = fftpack.real_to_complex_3d(n_real)
          n_complex = rfft.n_complex()
        else:
          cfft = fftpack.complex_to_complex_3d(n_real)
          n_complex = cfft.n()
        for treat_restricted in [False, True]:
          map = maptbx.structure_factors.to_map(
            space_group=space_group,
            anomalous_flag=anomalous_flag,
            miller_indices=miller_indices,
            structure_factors=data,
            n_real=n_real,
            map_grid=flex.grid(n_complex),
            conjugate_flag=conjugate_flag,
            treat_restricted=treat_restricted)
          if (treat_restricted):
            assert approx_equal(
              map.complex_map()[0], data[0])
          else:
            assert approx_equal(
              map.complex_map()[0], data[0]*space_group.order_p())
예제 #7
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)
예제 #8
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
예제 #9
0
def exercise_complex_to_complex_3d () :
  from scitbx.array_family import flex
  from cudatbx import cufft
  from scitbx import fftpack
  import time
  import sys
  print ""
  print "complex_to_complex_3d"
  for n_complex,n_repeats in [((100,80,90),16), ((200,160,180),16)]:
    print "  dimensions:", n_complex
    print "  repeats:", n_repeats
    np = n_complex[0]*n_complex[1]*n_complex[2]
    d0 = flex.polar(
      flex.random_double(size=np)*2-1,
      flex.random_double(size=np)*2-1)
    d0.reshape(flex.grid(n_complex))
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
    overhead = time.time()-t0
    print "    overhead: %.2f seconds" % overhead
    #
    # XXX extra CuFFT to initialize device - can we avoid this somehow?
    d = d0.deep_copy()
    cufft.complex_to_complex_3d(n_complex).forward(d)
    cufft.complex_to_complex_3d(n_complex).backward(d)
    # benchmarking run
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      cufft.complex_to_complex_3d(n_complex).forward(d)
      cufft.complex_to_complex_3d(n_complex).backward(d)
    print "    cufft:    %6.2f seconds" % ((time.time()-t0-overhead)/n_repeats)
    rw = d / np
    #
    t0 = time.time()
    for i_trial in xrange(n_repeats):
      d = d0.deep_copy()
      fftpack.complex_to_complex_3d(n_complex).forward(d)
      fftpack.complex_to_complex_3d(n_complex).backward(d)
    print "    fftpack:  %6.2f seconds" % ((time.time()-t0-overhead)/n_repeats)
    sys.stdout.flush()
    rp = d / np
    #
    print ""
    assert flex.max(flex.abs(rw-rp)) < 1.e-6
예제 #10
0
파일: fft3d.py 프로젝트: kek-pf-mx/dials
    def fft(self):
        if self.params.fft3d.reciprocal_space_grid.d_min is libtbx.Auto:
            # rough calculation of suitable d_min based on max cell
            # see also Campbell, J. (1998). J. Appl. Cryst., 31(3), 407-413.
            # fft_cell should be greater than twice max_cell, so say:
            #   fft_cell = 2.5 * max_cell
            # then:
            #   fft_cell = n_points * d_min/2
            #   2.5 * max_cell = n_points * d_min/2
            # a little bit of rearrangement:
            #   d_min = 5 * max_cell/n_points

            max_cell = self.params.max_cell
            d_min = (5 * max_cell /
                     self.params.fft3d.reciprocal_space_grid.n_points)
            d_spacings = 1 / self.reflections['rlp'].norms()
            self.params.fft3d.reciprocal_space_grid.d_min = max(
                d_min, min(d_spacings))
            logger.info("Setting d_min: %.2f" %
                        self.params.fft3d.reciprocal_space_grid.d_min)
        n_points = self.params.fft3d.reciprocal_space_grid.n_points
        self.gridding = fftpack.adjust_gridding_triple(
            (n_points, n_points, n_points), max_prime=5)
        n_points = self.gridding[0]
        self.map_centroids_to_reciprocal_space_grid()
        self.d_min = self.params.fft3d.reciprocal_space_grid.d_min

        logger.info("Number of centroids used: %i" %
                    ((self.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=self.reciprocal_space_grid,
            imags=flex.double(self.reciprocal_space_grid.size(), 0))
        grid_transformed = fft.forward(grid_complex)
        #self.grid_real = flex.pow2(flex.abs(grid_transformed))
        self.grid_real = flex.pow2(flex.real(grid_transformed))
        #self.grid_real = flex.pow2(flex.imag(self.grid_transformed))
        del grid_transformed

        if self.params.debug:
            self.debug_write_ccp4_map(map_data=self.grid_real,
                                      file_name="fft3d.map")
        if self.params.fft3d.peak_search == 'flood_fill':
            self.find_peaks()
        elif self.params.fft3d.peak_search == 'clean':
            self.find_peaks_clean()
 def fft(self):
   fft = fftpack.complex_to_complex_3d(self.gridding)
   grid_complex = flex.complex_double(
     reals=self.reciprocal_space_grid,
     imags=flex.double(self.reciprocal_space_grid.size(), 0))
   grid_transformed = fft.forward(grid_complex)
   #self.grid_real = flex.pow2(flex.abs(grid_transformed))
   self.grid_real = flex.abs(grid_transformed)
   #self.grid_real = flex.pow2(flex.real(grid_transformed))
   #self.grid_real = flex.pow2(flex.imag(self.grid_transformed))
   del grid_transformed
 def fft(self):
     fft = fftpack.complex_to_complex_3d(self.gridding)
     grid_complex = flex.complex_double(
         reals=self.reciprocal_space_grid,
         imags=flex.double(self.reciprocal_space_grid.size(), 0),
     )
     grid_transformed = fft.forward(grid_complex)
     # self.grid_real = flex.pow2(flex.abs(grid_transformed))
     self.grid_real = flex.abs(grid_transformed)
     # self.grid_real = flex.pow2(flex.real(grid_transformed))
     # self.grid_real = flex.pow2(flex.imag(self.grid_transformed))
     del grid_transformed
예제 #13
0
파일: run.py 프로젝트: ryan-dai/recviewer
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"]))
예제 #14
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
예제 #15
0
파일: run.py 프로젝트: stsukui/recviewer
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"]))
예제 #16
0
  def fft(self):
    #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=self.reciprocal_space_grid,
      imags=flex.double(self.reciprocal_space_grid.size(), 0))
    grid_transformed = fft.forward(grid_complex)
    #self.grid_real = flex.pow2(flex.abs(grid_transformed))
    self.grid_real = flex.pow2(flex.real(grid_transformed))
    #self.grid_real = flex.pow2(flex.imag(self.grid_transformed))
    del grid_transformed
예제 #17
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
예제 #18
0
def get_3D_transform():
    with mrcfile.open("new.mrc") as mrc:

        # coerce to double
        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
예제 #19
0
 def cfft(self):
   if (self._cfft is None):
     self._cfft = fftpack.complex_to_complex_3d(
       self.crystal_gridding().n_real())
   return self._cfft
def exercise_under_sampled(space_group_info, anomalous_flag, conjugate_flag,
                           under_sampling,
                           d_min=2., resolution_factor=0.5, max_prime=5,
                           verbose=0):
  structure_factors = random_structure.xray_structure(
    space_group_info,
    elements=("N", "C", "C", "O"),
    random_f_prime_d_min=1,
    random_f_double_prime=anomalous_flag,
    use_u_aniso=True,
    random_u_iso=True,
    random_occupancy=True
    ).structure_factors(
        anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct")
  f_calc = structure_factors.f_calc()
  n_real = maptbx.crystal_gridding(
    unit_cell=f_calc.unit_cell(),
    d_min=d_min,
    resolution_factor=resolution_factor,
    max_prime=max_prime,
    mandatory_factors=(under_sampling,)*3).n_real()
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real)
    n_complex = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real)
    n_complex = cfft.n()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  f_calc_p1 = f_calc.expand_to_p1()
  map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    real_map = rfft.backward(map.complex_map())
    assert real_map.all() == rfft.m_real()
  else:
    real_map = cfft.backward(map.complex_map())
    assert not real_map.is_padded()
  if (0 or verbose):
    if (not anomalous_flag):
      maptbx.statistics(real_map).show_summary()
      maptbx.statistics(real_map).show_summary()
    else:
      maptbx.statistics(flex.real(real_map)).show_summary()
      maptbx.statistics(flex.imag(real_map)).show_summary()
  n_real_under_sampled = [n//under_sampling for n in n_real]
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real_under_sampled)
    n_complex_under_sampled = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real_under_sampled)
    n_complex_under_sampled = cfft.n()
  under_sampled_map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real_under_sampled,
    map_grid=flex.grid(n_complex_under_sampled),
    conjugate_flag=conjugate_flag)
  under_sampled_map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real_under_sampled,
    map_grid=flex.grid(n_complex_under_sampled),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(under_sampled_map_p1.complex_map()
                         - under_sampled_map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    under_sampled_map_before_fft = under_sampled_map.complex_map().deep_copy()
    under_sampled_real_map = rfft.backward(under_sampled_map.complex_map())
    assert under_sampled_real_map.all() == rfft.m_real()
  else:
    under_sampled_real_map = cfft.backward(under_sampled_map.complex_map())
    assert not under_sampled_real_map.is_padded()
  if (0 or verbose):
    if (not anomalous_flag):
      maptbx.statistics(under_sampled_real_map).show_summary()
      maptbx.statistics(under_sampled_real_map).show_summary()
    else:
      maptbx.statistics(flex.real(under_sampled_real_map)).show_summary()
      maptbx.statistics(flex.imag(under_sampled_real_map)).show_summary()
  if (0 or verbose):
    print real_map.all(), n_complex
    print under_sampled_real_map.all(), n_complex_under_sampled
  if (not anomalous_flag):
    x_source = real_map
    y_source = under_sampled_real_map
  else:
    x_source = flex.real(real_map)
    y_source = flex.real(under_sampled_real_map)
  x = flex.double()
  n = x_source.focus()
  for i in xrange(0, n[0], under_sampling):
    for j in xrange(0, n[1], under_sampling):
      for k in xrange(0, n[2], under_sampling):
        x.append(x_source[(i,j,k)])
  y = maptbx.copy(y_source, flex.grid(y_source.focus())).as_1d()
  if (0 or verbose):
    print "x:", tuple(x)
    print "y:", tuple(y)
  assert flex.max(flex.abs(x-y)) \
      < (flex.max(flex.abs(x))+flex.max(flex.abs(y)))/2*1.e-6
  if (under_sampling == 1):
    x = maptbx.copy(x_source, flex.grid(x_source.focus())).as_1d()
    c = flex.linear_correlation(x, y)
    assert c.coefficient() >= 0.9999
def exercise_shannon_sampled(space_group_info, anomalous_flag, conjugate_flag,
                             d_min=3., resolution_factor=0.5, max_prime=5,
                             verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=("N", "C", "C", "O"),
    random_f_prime_d_min=1,
    random_f_double_prime=anomalous_flag,
    use_u_aniso=True,
    random_u_iso=True,
    random_occupancy=True)
  f_calc = structure.structure_factors(
    anomalous_flag=anomalous_flag,
    d_min=d_min,
    algorithm="direct").f_calc()
  n_real = f_calc.crystal_gridding(
    resolution_factor=resolution_factor,
    d_min=d_min,
    max_prime=max_prime).n_real()
  if (not anomalous_flag):
    rfft = fftpack.real_to_complex_3d(n_real)
    n_complex = rfft.n_complex()
  else:
    cfft = fftpack.complex_to_complex_3d(n_real)
    n_complex = cfft.n()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    structure_factors=f_calc.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  f_calc_p1 = f_calc.expand_to_p1()
  map_p1 = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  assert flex.max(flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
  if (not anomalous_flag):
    real_map = rfft.backward(map.complex_map())
    assert real_map.all() == rfft.m_real()
    complex_map = rfft.forward(real_map)
  else:
    real_map = cfft.backward(map.complex_map())
    assert not real_map.is_padded()
    complex_map = cfft.forward(real_map)
  complex_map /= n_real[0] * n_real[1] * n_real[2]
  assert real_map.focus() == n_real
  assert complex_map.focus() == n_complex
  from_map = maptbx.structure_factors.from_map(
    unit_cell=f_calc.unit_cell(),
    space_group_type=f_calc.space_group_info().type(),
    anomalous_flag=anomalous_flag,
    d_min=d_min,
    complex_map=complex_map,
    conjugate_flag=conjugate_flag)
  from_map = f_calc.customized_copy(
    indices=from_map.miller_indices(),
    data=from_map.data())
  lone_sets = f_calc.lone_sets(from_map)
  for lone_set in lone_sets:
    if (lone_set.indices().size() > 0):
      flex.max(lone_set.d_spacings().data()-d_min) < 1.e-5
  common_sets = f_calc.common_sets(from_map)
  assert flex.max(flex.abs(common_sets[0].data()
                         - common_sets[1].data())) < 1.e-10
  from_map = maptbx.structure_factors.from_map(
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    complex_map=complex_map,
    conjugate_flag=conjugate_flag)
  assert from_map.miller_indices().size() == 0
  assert flex.max(flex.abs(f_calc.data()-from_map.data())) < 1.e-10
  structure_p1 = structure.asymmetric_unit_in_p1()
  f_calc_p1 = f_calc_p1.structure_factors_from_scatterers(
    xray_structure=structure_p1,
    algorithm="direct").f_calc()
  map = maptbx.structure_factors.to_map(
    space_group=f_calc_p1.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc_p1.indices(),
    structure_factors=f_calc_p1.data(),
    n_real=n_real,
    map_grid=flex.grid(n_complex),
    conjugate_flag=conjugate_flag)
  from_map = maptbx.structure_factors.from_map(
    space_group=f_calc.space_group(),
    anomalous_flag=anomalous_flag,
    miller_indices=f_calc.indices(),
    complex_map=map.complex_map(),
    conjugate_flag=conjugate_flag)
  assert from_map.miller_indices().size() == 0
  assert flex.max(flex.abs(f_calc.data()-from_map.data())) < 1.e-10
예제 #22
0
 def cfft(self):
     if (self._cfft is None):
         self._cfft = fftpack.complex_to_complex_3d(
             self.crystal_gridding().n_real())
     return self._cfft
예제 #23
0
def exercise(space_group_info,
             const_gaussian,
             negative_gaussian,
             anomalous_flag,
             allow_mix,
             use_u_iso,
             use_u_aniso,
             d_min=1.,
             resolution_factor=1. / 3,
             max_prime=5,
             quality_factor=100,
             wing_cutoff=1.e-6,
             exp_table_one_over_step_size=-100,
             force_complex=False,
             verbose=0):
    if (const_gaussian):
        elements = ["const"] * 8
    elif (negative_gaussian):
        elements = ["H"] * 8
    else:
        elements = ["N", "C", "C", "O", "N", "C", "C", "O"]
    if (random.random() < 0.5):
        random_f_prime_scale = 0.6
    else:
        random_f_prime_scale = 0
    structure = random_structure.xray_structure(
        space_group_info,
        elements=elements,
        random_f_prime_d_min=1,
        random_f_prime_scale=random_f_prime_scale,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        use_u_iso=False,
        random_u_cart_scale=0.3,
        random_u_iso=True,
        random_occupancy=True)
    random_structure.random_modify_adp_and_adp_flags_2(
        scatterers=structure.scatterers(),
        use_u_iso=use_u_iso,
        use_u_aniso=use_u_aniso,
        allow_mix=allow_mix,
        random_u_iso_scale=0.3,
        random_u_iso_min=0.0)
    sampled_density_must_be_positive = True
    if (negative_gaussian):
        reg = structure.scattering_type_registry(
            custom_dict={"H": eltbx.xray_scattering.gaussian(-1)})
        assert reg.gaussian("H").n_terms() == 0
        assert reg.gaussian("H").c() == -1
        sampled_density_must_be_positive = False
    elif (not const_gaussian and random.random() < 0.5):
        if (random.random() < 0.5):
            sampled_density_must_be_positive = False
        assign_custom_gaussians(
            structure, negative_a=not sampled_density_must_be_positive)
    f_direct = structure.structure_factors(anomalous_flag=anomalous_flag,
                                           d_min=d_min,
                                           algorithm="direct").f_calc()
    crystal_gridding = f_direct.crystal_gridding(
        resolution_factor=resolution_factor, d_min=d_min, max_prime=max_prime)
    assert crystal_gridding.symmetry_flags() is None
    rfft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
    u_base = xray.calc_u_base(d_min, resolution_factor, quality_factor)
    omptbx.env.num_threads = libtbx.introspection.number_of_processors()
    sampled_density = xray.sampled_model_density(
        unit_cell=structure.unit_cell(),
        scatterers=structure.scatterers(),
        scattering_type_registry=structure.scattering_type_registry(),
        fft_n_real=rfft.n_real(),
        fft_m_real=rfft.m_real(),
        u_base=u_base,
        wing_cutoff=wing_cutoff,
        exp_table_one_over_step_size=exp_table_one_over_step_size,
        force_complex=force_complex,
        sampled_density_must_be_positive=sampled_density_must_be_positive,
        tolerance_positive_definite=1.e-5,
        use_u_base_as_u_extra=False)
    focus = sampled_density.real_map_unpadded().focus()
    all = sampled_density.real_map_unpadded().all()
    last = sampled_density.real_map_unpadded().last()
    assert approx_equal(focus, last)
    assert approx_equal(all, last)
    assert sampled_density.anomalous_flag() == (anomalous_flag
                                                or force_complex)
    if (0 or verbose):
        print("const_gaussian:", const_gaussian)
        print("negative_gaussian:", negative_gaussian)
        print("number of scatterers passed:", \
          sampled_density.n_scatterers_passed())
        print("number of contributing scatterers:", \
          sampled_density.n_contributing_scatterers())
        print("number of anomalous scatterers:", \
          sampled_density.n_anomalous_scatterers())
        print("wing_cutoff:", sampled_density.wing_cutoff())
        print("exp_table_one_over_step_size:", \
          sampled_density.exp_table_one_over_step_size())
        print("exp_table_size:", sampled_density.exp_table_size())
        print("max_sampling_box_edges:",
              sampled_density.max_sampling_box_edges(),
              end=' ')
        print("(%.4f, %.4f, %.4f)" %
              sampled_density.max_sampling_box_edges_frac())
        if (not sampled_density.anomalous_flag()):
            print("map min:", flex.min(sampled_density.real_map()))
            print("map max:", flex.max(sampled_density.real_map()))
        else:
            print("map min:",
                  flex.min(flex.real(sampled_density.complex_map())),
                  end=' ')
            print(flex.min(flex.imag(sampled_density.complex_map())))
            print("map max:",
                  flex.max(flex.real(sampled_density.complex_map())),
                  end=' ')
            print(flex.max(flex.imag(sampled_density.complex_map())))
    if (not sampled_density.anomalous_flag() and negative_gaussian):
        assert flex.min(sampled_density.real_map()) < 0
        assert flex.max(sampled_density.real_map()) == 0
    if (not sampled_density.anomalous_flag()):
        map = sampled_density.real_map()
        assert map.all() == rfft.m_real()
        assert map.focus() == rfft.n_real()
        sf_map = rfft.forward(map)
        assert sf_map.all() == rfft.n_complex()
        assert sf_map.focus() == rfft.n_complex()
        collect_conj = True
    else:
        cfft = fftpack.complex_to_complex_3d(rfft.n_real())
        map = sampled_density.complex_map()
        assert map.all() == cfft.n()
        assert map.focus() == cfft.n()
        sf_map = cfft.backward(map)
        assert sf_map.all() == cfft.n()
        assert sf_map.focus() == cfft.n()
        collect_conj = False
    f_fft_data = maptbx.structure_factors.from_map(
        space_group=f_direct.space_group(),
        anomalous_flag=sampled_density.anomalous_flag(),
        miller_indices=f_direct.indices(),
        complex_map=sf_map,
        conjugate_flag=collect_conj).data()
    sampled_density.eliminate_u_extra_and_normalize(f_direct.indices(),
                                                    f_fft_data)
    structure_factor_utils.check_correlation("direct/fft_regression",
                                             f_direct.indices(),
                                             0,
                                             f_direct.data(),
                                             f_fft_data,
                                             min_corr_ampl=1 * 0.99,
                                             max_mean_w_phase_error=1 * 3.,
                                             verbose=verbose)
    f_fft = xray.structure_factors.from_scatterers(
        miller_set=f_direct,
        grid_resolution_factor=resolution_factor,
        quality_factor=quality_factor,
        wing_cutoff=wing_cutoff,
        exp_table_one_over_step_size=exp_table_one_over_step_size,
        sampled_density_must_be_positive=sampled_density_must_be_positive,
        max_prime=max_prime)(xray_structure=structure,
                             miller_set=f_direct,
                             algorithm="fft").f_calc()
    structure_factor_utils.check_correlation("direct/fft_xray",
                                             f_direct.indices(),
                                             0,
                                             f_direct.data(),
                                             f_fft.data(),
                                             min_corr_ampl=1 * 0.99,
                                             max_mean_w_phase_error=1 * 3.,
                                             verbose=verbose)
def exercise(space_group_info, const_gaussian, negative_gaussian,
             anomalous_flag,
             allow_mix,
             use_u_iso,
             use_u_aniso,
             d_min=1., resolution_factor=1./3, max_prime=5,
             quality_factor=100, wing_cutoff=1.e-6,
             exp_table_one_over_step_size=-100,
             force_complex=False,
             verbose=0):
  if (const_gaussian):
    elements=["const"]*8
  elif (negative_gaussian):
    elements=["H"]*8
  else:
    elements=["N", "C", "C", "O", "N", "C", "C", "O"]
  if (random.random() < 0.5):
    random_f_prime_scale=0.6
  else:
    random_f_prime_scale=0
  structure = random_structure.xray_structure(
    space_group_info,
    elements=elements,
    random_f_prime_d_min=1,
    random_f_prime_scale=random_f_prime_scale,
    random_f_double_prime=anomalous_flag,
    use_u_aniso= True,
    use_u_iso= False,
    random_u_cart_scale=0.3,
    random_u_iso=True,
    random_occupancy=True)
  random_structure.random_modify_adp_and_adp_flags_2(
                                 scatterers         = structure.scatterers(),
                                 use_u_iso          = use_u_iso,
                                 use_u_aniso        = use_u_aniso,
                                 allow_mix          = allow_mix,
                                 random_u_iso_scale = 0.3,
                                 random_u_iso_min   = 0.0)
  sampled_density_must_be_positive = True
  if (negative_gaussian):
    reg = structure.scattering_type_registry(
      custom_dict={"H": eltbx.xray_scattering.gaussian(-1)})
    assert reg.gaussian("H").n_terms() == 0
    assert reg.gaussian("H").c() == -1
    sampled_density_must_be_positive = False
  elif (not const_gaussian and random.random() < 0.5):
    if (random.random() < 0.5):
      sampled_density_must_be_positive = False
    assign_custom_gaussians(
      structure,
      negative_a=not sampled_density_must_be_positive)
  f_direct = structure.structure_factors(
    anomalous_flag=anomalous_flag,
    d_min=d_min,
    algorithm="direct").f_calc()
  crystal_gridding = f_direct.crystal_gridding(
    resolution_factor=resolution_factor,
    d_min=d_min,
    max_prime=max_prime)
  assert crystal_gridding.symmetry_flags() is None
  rfft = fftpack.real_to_complex_3d(crystal_gridding.n_real())
  u_base = xray.calc_u_base(d_min, resolution_factor, quality_factor)
  omptbx.env.num_threads = libtbx.introspection.number_of_processors()
  sampled_density = xray.sampled_model_density(
    unit_cell=structure.unit_cell(),
    scatterers=structure.scatterers(),
    scattering_type_registry=structure.scattering_type_registry(),
    fft_n_real=rfft.n_real(),
    fft_m_real=rfft.m_real(),
    u_base=u_base,
    wing_cutoff=wing_cutoff,
    exp_table_one_over_step_size=exp_table_one_over_step_size,
    force_complex=force_complex,
    sampled_density_must_be_positive=sampled_density_must_be_positive,
    tolerance_positive_definite=1.e-5,
    use_u_base_as_u_extra=False)
  focus = sampled_density.real_map_unpadded().focus()
  all   = sampled_density.real_map_unpadded().all()
  last  = sampled_density.real_map_unpadded().last()
  assert approx_equal(focus, last)
  assert approx_equal(all, last)
  assert sampled_density.anomalous_flag() == (anomalous_flag or force_complex)
  if (0 or verbose):
    print "const_gaussian:", const_gaussian
    print "negative_gaussian:", negative_gaussian
    print "number of scatterers passed:", \
      sampled_density.n_scatterers_passed()
    print "number of contributing scatterers:", \
      sampled_density.n_contributing_scatterers()
    print "number of anomalous scatterers:", \
      sampled_density.n_anomalous_scatterers()
    print "wing_cutoff:", sampled_density.wing_cutoff()
    print "exp_table_one_over_step_size:", \
      sampled_density.exp_table_one_over_step_size()
    print "exp_table_size:", sampled_density.exp_table_size()
    print "max_sampling_box_edges:", sampled_density.max_sampling_box_edges(),
    print "(%.4f, %.4f, %.4f)" % sampled_density.max_sampling_box_edges_frac()
    if (not sampled_density.anomalous_flag()):
      print "map min:", flex.min(sampled_density.real_map())
      print "map max:", flex.max(sampled_density.real_map())
    else:
      print "map min:", flex.min(flex.real(sampled_density.complex_map())),
      print             flex.min(flex.imag(sampled_density.complex_map()))
      print "map max:", flex.max(flex.real(sampled_density.complex_map())),
      print             flex.max(flex.imag(sampled_density.complex_map()))
  if (not sampled_density.anomalous_flag() and negative_gaussian):
    assert flex.min(sampled_density.real_map()) < 0
    assert flex.max(sampled_density.real_map()) == 0
  if (not sampled_density.anomalous_flag()):
    map = sampled_density.real_map()
    assert map.all() == rfft.m_real()
    assert map.focus() == rfft.n_real()
    sf_map = rfft.forward(map)
    assert sf_map.all() == rfft.n_complex()
    assert sf_map.focus() == rfft.n_complex()
    collect_conj = True
  else:
    cfft = fftpack.complex_to_complex_3d(rfft.n_real())
    map = sampled_density.complex_map()
    assert map.all() == cfft.n()
    assert map.focus() == cfft.n()
    sf_map = cfft.backward(map)
    assert sf_map.all() == cfft.n()
    assert sf_map.focus() == cfft.n()
    collect_conj = False
  f_fft_data = maptbx.structure_factors.from_map(
    space_group=f_direct.space_group(),
    anomalous_flag=sampled_density.anomalous_flag(),
    miller_indices=f_direct.indices(),
    complex_map=sf_map,
    conjugate_flag=collect_conj).data()
  sampled_density.eliminate_u_extra_and_normalize(
    f_direct.indices(),
    f_fft_data)
  structure_factor_utils.check_correlation(
    "direct/fft_regression", f_direct.indices(), 0,
    f_direct.data(), f_fft_data,
    min_corr_ampl=1*0.99, max_mean_w_phase_error=1*3.,
    verbose=verbose)
  f_fft = xray.structure_factors.from_scatterers(
    miller_set=f_direct,
    grid_resolution_factor=resolution_factor,
    quality_factor=quality_factor,
    wing_cutoff=wing_cutoff,
    exp_table_one_over_step_size=exp_table_one_over_step_size,
    sampled_density_must_be_positive=sampled_density_must_be_positive,
    max_prime=max_prime)(
      xray_structure=structure,
      miller_set=f_direct,
      algorithm="fft").f_calc()
  structure_factor_utils.check_correlation(
    "direct/fft_xray", f_direct.indices(), 0,
    f_direct.data(), f_fft.data(),
    min_corr_ampl=1*0.99, max_mean_w_phase_error=1*3.,
    verbose=verbose)
def exercise_shannon_sampled(space_group_info,
                             anomalous_flag,
                             conjugate_flag,
                             d_min=3.,
                             resolution_factor=0.5,
                             max_prime=5,
                             verbose=0):
    structure = random_structure.xray_structure(
        space_group_info,
        elements=("N", "C", "C", "O"),
        random_f_prime_d_min=1,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        random_u_iso=True,
        random_occupancy=True)
    f_calc = structure.structure_factors(anomalous_flag=anomalous_flag,
                                         d_min=d_min,
                                         algorithm="direct").f_calc()
    n_real = f_calc.crystal_gridding(resolution_factor=resolution_factor,
                                     d_min=d_min,
                                     max_prime=max_prime).n_real()
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real)
        n_complex = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real)
        n_complex = cfft.n()
    map = maptbx.structure_factors.to_map(space_group=f_calc.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc.indices(),
                                          structure_factors=f_calc.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    f_calc_p1 = f_calc.expand_to_p1()
    map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real,
        map_grid=flex.grid(n_complex),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        real_map = rfft.backward(map.complex_map())
        assert real_map.all() == rfft.m_real()
        complex_map = rfft.forward(real_map)
    else:
        real_map = cfft.backward(map.complex_map())
        assert not real_map.is_padded()
        complex_map = cfft.forward(real_map)
    complex_map /= n_real[0] * n_real[1] * n_real[2]
    assert real_map.focus() == n_real
    assert complex_map.focus() == n_complex
    from_map = maptbx.structure_factors.from_map(
        unit_cell=f_calc.unit_cell(),
        space_group_type=f_calc.space_group_info().type(),
        anomalous_flag=anomalous_flag,
        d_min=d_min,
        complex_map=complex_map,
        conjugate_flag=conjugate_flag)
    from_map = f_calc.customized_copy(indices=from_map.miller_indices(),
                                      data=from_map.data())
    lone_sets = f_calc.lone_sets(from_map)
    for lone_set in lone_sets:
        if (lone_set.indices().size() > 0):
            flex.max(lone_set.d_spacings().data() - d_min) < 1.e-5
    common_sets = f_calc.common_sets(from_map)
    assert flex.max(
        flex.abs(common_sets[0].data() - common_sets[1].data())) < 1.e-10
    from_map = maptbx.structure_factors.from_map(
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        complex_map=complex_map,
        conjugate_flag=conjugate_flag)
    assert from_map.miller_indices().size() == 0
    assert flex.max(flex.abs(f_calc.data() - from_map.data())) < 1.e-10
    structure_p1 = structure.asymmetric_unit_in_p1()
    f_calc_p1 = f_calc_p1.structure_factors_from_scatterers(
        xray_structure=structure_p1, algorithm="direct").f_calc()
    map = maptbx.structure_factors.to_map(space_group=f_calc_p1.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc_p1.indices(),
                                          structure_factors=f_calc_p1.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    from_map = maptbx.structure_factors.from_map(
        space_group=f_calc.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        complex_map=map.complex_map(),
        conjugate_flag=conjugate_flag)
    assert from_map.miller_indices().size() == 0
    assert flex.max(flex.abs(f_calc.data() - from_map.data())) < 1.e-10
def exercise_under_sampled(space_group_info,
                           anomalous_flag,
                           conjugate_flag,
                           under_sampling,
                           d_min=2.,
                           resolution_factor=0.5,
                           max_prime=5,
                           verbose=0):
    structure_factors = random_structure.xray_structure(
        space_group_info,
        elements=("N", "C", "C", "O"),
        random_f_prime_d_min=1,
        random_f_double_prime=anomalous_flag,
        use_u_aniso=True,
        random_u_iso=True,
        random_occupancy=True).structure_factors(anomalous_flag=anomalous_flag,
                                                 d_min=d_min,
                                                 algorithm="direct")
    f_calc = structure_factors.f_calc()
    n_real = maptbx.crystal_gridding(unit_cell=f_calc.unit_cell(),
                                     d_min=d_min,
                                     resolution_factor=resolution_factor,
                                     max_prime=max_prime,
                                     mandatory_factors=(under_sampling, ) *
                                     3).n_real()
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real)
        n_complex = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real)
        n_complex = cfft.n()
    map = maptbx.structure_factors.to_map(space_group=f_calc.space_group(),
                                          anomalous_flag=anomalous_flag,
                                          miller_indices=f_calc.indices(),
                                          structure_factors=f_calc.data(),
                                          n_real=n_real,
                                          map_grid=flex.grid(n_complex),
                                          conjugate_flag=conjugate_flag)
    f_calc_p1 = f_calc.expand_to_p1()
    map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real,
        map_grid=flex.grid(n_complex),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(map_p1.complex_map() - map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        real_map = rfft.backward(map.complex_map())
        assert real_map.all() == rfft.m_real()
    else:
        real_map = cfft.backward(map.complex_map())
        assert not real_map.is_padded()
    if (0 or verbose):
        if (not anomalous_flag):
            maptbx.statistics(real_map).show_summary()
            maptbx.statistics(real_map).show_summary()
        else:
            maptbx.statistics(flex.real(real_map)).show_summary()
            maptbx.statistics(flex.imag(real_map)).show_summary()
    n_real_under_sampled = [n // under_sampling for n in n_real]
    if (not anomalous_flag):
        rfft = fftpack.real_to_complex_3d(n_real_under_sampled)
        n_complex_under_sampled = rfft.n_complex()
    else:
        cfft = fftpack.complex_to_complex_3d(n_real_under_sampled)
        n_complex_under_sampled = cfft.n()
    under_sampled_map = maptbx.structure_factors.to_map(
        space_group=f_calc.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc.indices(),
        structure_factors=f_calc.data(),
        n_real=n_real_under_sampled,
        map_grid=flex.grid(n_complex_under_sampled),
        conjugate_flag=conjugate_flag)
    under_sampled_map_p1 = maptbx.structure_factors.to_map(
        space_group=f_calc_p1.space_group(),
        anomalous_flag=anomalous_flag,
        miller_indices=f_calc_p1.indices(),
        structure_factors=f_calc_p1.data(),
        n_real=n_real_under_sampled,
        map_grid=flex.grid(n_complex_under_sampled),
        conjugate_flag=conjugate_flag)
    assert flex.max(
        flex.abs(under_sampled_map_p1.complex_map() -
                 under_sampled_map.complex_map())) < 1.e-10
    if (not anomalous_flag):
        under_sampled_map_before_fft = under_sampled_map.complex_map(
        ).deep_copy()
        under_sampled_real_map = rfft.backward(under_sampled_map.complex_map())
        assert under_sampled_real_map.all() == rfft.m_real()
    else:
        under_sampled_real_map = cfft.backward(under_sampled_map.complex_map())
        assert not under_sampled_real_map.is_padded()
    if (0 or verbose):
        if (not anomalous_flag):
            maptbx.statistics(under_sampled_real_map).show_summary()
            maptbx.statistics(under_sampled_real_map).show_summary()
        else:
            maptbx.statistics(flex.real(under_sampled_real_map)).show_summary()
            maptbx.statistics(flex.imag(under_sampled_real_map)).show_summary()
    if (0 or verbose):
        print(real_map.all(), n_complex)
        print(under_sampled_real_map.all(), n_complex_under_sampled)
    if (not anomalous_flag):
        x_source = real_map
        y_source = under_sampled_real_map
    else:
        x_source = flex.real(real_map)
        y_source = flex.real(under_sampled_real_map)
    x = flex.double()
    n = x_source.focus()
    for i in range(0, n[0], under_sampling):
        for j in range(0, n[1], under_sampling):
            for k in range(0, n[2], under_sampling):
                x.append(x_source[(i, j, k)])
    y = maptbx.copy(y_source, flex.grid(y_source.focus())).as_1d()
    if (0 or verbose):
        print("x:", tuple(x))
        print("y:", tuple(y))
    assert flex.max(flex.abs(x-y)) \
        < (flex.max(flex.abs(x))+flex.max(flex.abs(y)))/2*1.e-6
    if (under_sampling == 1):
        x = maptbx.copy(x_source, flex.grid(x_source.focus())).as_1d()
        c = flex.linear_correlation(x, y)
        assert c.coefficient() >= 0.9999
예제 #27
0
  def find_peaks_clean(self):
    import omptbx
    # doesn't seem to be any benefit to using more than say 8 threads
    num_threads = min(8, omptbx.omp_get_num_procs(), self.params.nproc)
    omptbx.omp_set_num_threads(num_threads)
    d_min = self.params.fft3d.reciprocal_space_grid.d_min
    rlgrid = 2 / (d_min * self.gridding[0])

    frame_number = self.reflections['xyzobs.px.value'].parts()[2]
    scan_range_min = max(
      int(math.floor(flex.min(frame_number))),
      self.imagesets[0].get_array_range()[0]) # XXX what about multiple imagesets?
    scan_range_max = min(
      int(math.ceil(flex.max(frame_number))),
      self.imagesets[0].get_array_range()[1]) # XXX what about multiple imagesets?
    scan_range = self.params.scan_range
    if not len(scan_range):
      scan_range = [[scan_range_min, scan_range_max]]

    scan = self.imagesets[0].get_scan() # XXX what about multiple imagesets?
    angle_ranges = [
      [scan.get_angle_from_array_index(i, deg=False) for i in range_]
      for range_ in scan_range]

    grid = flex.double(flex.grid(self.gridding), 0)
    sampling_volume_map(grid, flex.vec2_double(angle_ranges),
                        self.imagesets[0].get_beam().get_s0(),
                        self.imagesets[0].get_goniometer().get_rotation_axis(),
                        rlgrid, d_min, self.params.b_iso)

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

    gamma = 1
    peaks = flex.vec3_double()
    #n_peaks = 200
    n_peaks = 100 # XXX how many do we need?

    dirty_beam = grid_real
    dirty_map = self.grid_real.deep_copy()
    import time
    t0 = time.time()
    peaks = clean_3d(dirty_beam, dirty_map, n_peaks, gamma=gamma)
    t1 = time.time()
    #print "clean_3d took %.2f s" %(t1-t0)

    reciprocal_lattice_points = self.reflections['rlp'].select(
      self.reflections_used_for_indexing)

    peaks = self.optimise_peaks(peaks, reciprocal_lattice_points)

    peaks_frac = flex.vec3_double()
    for p in peaks:
      peaks_frac.append((p[0]/self.gridding[0],
                         p[1]/self.gridding[1],
                         p[2]/self.gridding[2]))
      #print p, peaks_frac[-1]

    if self.params.debug:
      self.debug_write_ccp4_map(grid, "sampling_volume.map")
      self.debug_write_ccp4_map(grid_real, "sampling_volume_FFT.map")
      self.debug_write_ccp4_map(dirty_map, "clean.map")

    self.sites = peaks_frac
    # we don't really know the "volume"" of the peaks, but this method should
    # find the peaks in order of their intensity (strongest first)
    self.volumes = flex.double(range(len(self.sites), 0, -1))

    return