def exercise(space_group_info, n_scatterers=8, d_min=2, verbose=0, e_min=1.5):
    structure = random_structure.xray_structure(
        space_group_info,
        elements=["const"] * n_scatterers,
        volume_per_atom=200,
        min_distance=3.0,
        general_positions_only=True,
        u_iso=0.0,
    )
    if 0 or verbose:
        structure.show_summary().show_scatterers()
    f_calc = structure.structure_factors(d_min=d_min, anomalous_flag=False).f_calc()
    f_obs = abs(f_calc)
    q_obs = miller.array(
        miller_set=f_obs,
        data=f_obs.data() / math.sqrt(f_obs.space_group().order_p() * n_scatterers) / f_obs.space_group().n_ltr(),
    )
    q_obs = q_obs.sort(by_value="abs")
    q_obs.setup_binner(auto_binning=True)
    n_obs = q_obs.quasi_normalize_structure_factors()
    r = flex.linear_regression(q_obs.data(), n_obs.data())
    if 0 or verbose:
        r.show_summary()
    assert r.is_well_defined()
    assert abs(r.y_intercept()) < 0.1
    assert abs(r.slope() - 1) < 0.2
    q_large = q_obs.select(q_obs.quasi_normalized_as_normalized().data() > e_min)
    if 0 or verbose:
        print "Number of e-values > %.6g: %d" % (e_min, q_large.size())
    other_structure = random_structure.xray_structure(
        space_group_info,
        elements=["const"] * n_scatterers,
        volume_per_atom=200,
        min_distance=3.0,
        general_positions_only=True,
        u_iso=0.0,
    )
    assert other_structure.unit_cell().is_similar_to(structure.unit_cell())
    q_calc = q_large.structure_factors_from_scatterers(other_structure, algorithm="direct").f_calc()
    start = q_large.phase_transfer(q_calc.data())
    for selection_fixed in (None, flex.double([random.random() for i in xrange(start.size())]) < 0.4):
        from_map_data = direct_space_squaring(start, selection_fixed)
        direct_space_result = start.phase_transfer(phase_source=from_map_data)
        new_phases = reciprocal_space_squaring(start, selection_fixed, verbose)
        reciprocal_space_result = start.phase_transfer(phase_source=flex.polar(1, new_phases))
        mwpe = direct_space_result.mean_weighted_phase_error(reciprocal_space_result)
        if 0 or verbose:
            print "mwpe: %.2f" % mwpe, start.space_group_info()
        for i, h in enumerate(direct_space_result.indices()):
            amp_d, phi_d = complex_math.abs_arg(direct_space_result.data()[i], deg=True)
            amp_r, phi_r = complex_math.abs_arg(reciprocal_space_result.data()[i], deg=True)
            phase_err = scitbx.math.phase_error(phi_d, phi_r, deg=True)
            assert phase_err < 1.0 or abs(from_map_data[i]) < 1.0e-6
    exercise_truncate(q_large)
def test_grid_step(n_sites = 50,
                   volume_per_atom = 50,
                   d_min = 2.0):
  grid_step = (0.2,0.4,0.6,0.7,0.9,1.0)
  for step in grid_step:
    symmetry = crystal.symmetry(space_group_symbol="P1")
    structure = random_structure.xray_structure(space_group_info = symmetry.space_group_info(),
                                                elements=["C"]*n_sites,
                                                volume_per_atom=volume_per_atom,
                                                random_u_iso=False)
    fc = structure.structure_factors(d_min = d_min,
                                     anomalous_flag=False,
                                     algorithm="fft").f_calc()
    manager = max_like_non_uniform.ordered_solvent_distribution(
                                      structure = structure,
                                      fo = fc,
                                      grid_step = step)
    f_water_dist = manager.fcalc_from_distribution()
    ### check phase compatibility with the symmetry:
    centrics = f_water_dist.select_centric()
    if(centrics.indices().size() > 0):
       ideal = centrics.phase_transfer(centrics)
       assert flex.max(flex.abs(ideal.data() - centrics.data())) < 1.e-6
    ###
    #print "max = ", flex.max( flex.abs( f_water_dist.data() ) )
    #print "min = ", flex.min( flex.abs( f_water_dist.data() ) )
    #print "ave = ", flex.mean( flex.abs( f_water_dist.data() ) )
    assert flex.max( flex.abs( f_water_dist.data() ) ) < 1.0
def exercise_subtract_continuous_allowed_origin_shifts(
      space_group_info,
      use_niggli_cell,
      n_elements=3):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=["Si"]*n_elements,
    volume_per_atom=300,
    min_distance=3.,
    general_positions_only=False)
  if (use_niggli_cell):
    structure = structure.niggli_cell()
  f_obs = abs(structure.structure_factors(
    d_min=3, algorithm="direct").f_calc())
  assert f_obs.indices().size() >= 10
  transl = matrix.col(flex.random_double_point_on_sphere()) * 2.345
  transl_no_cont = matrix.col(
    structure.subtract_continuous_allowed_origin_shifts(
      translation_cart=transl))
  transl_cont = transl - transl_no_cont
  structure_transl = structure.apply_shift(
    shift=structure.unit_cell().fractionalize(transl_cont),
    recompute_site_symmetries=True)
  f_transl = abs(f_obs.structure_factors_from_scatterers(
    xray_structure=structure_transl, algorithm="direct").f_calc())
  assert approx_equal(f_transl.data(), f_obs.data())
def exercise_twin_detwin () :
  random.seed(12345)
  flex.set_random_seed(12345)
  xrs = random_structure.xray_structure(
    unit_cell=(12,5,12,90,90,90),
    space_group_symbol="P1",
    n_scatterers=12,
    elements="random")
  fc = abs(xrs.structure_factors(d_min=1.5).f_calc())
  fc = fc.set_observation_type_xray_amplitude()
  mtz_file = "tmp_massage_in.mtz"
  fc.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
  massage_data.run(
    args=[
      mtz_file,
      "aniso.action=None",
      "outlier.action=None",
      "symmetry.action=twin",
      "twin_law='l,-k,h'",
      "fraction=0.3",
      "hklout=tmp_massage_twinned.mtz",
    ],
    out=null_out())
  assert op.isfile("tmp_massage_twinned.mtz")
  mtz_in = file_reader.any_file("tmp_massage_twinned.mtz")
  fc_twin = mtz_in.file_server.miller_arrays[0].f_sq_as_f()
  fc_twin, fc_tmp = fc_twin.common_sets(other=fc)
  for hkl, f1, f2 in zip(fc_tmp.indices(), fc_tmp.data(), fc_twin.data()) :
    if (abs(hkl[0]) != abs(hkl[2])) :
      assert not approx_equal(f1, f2, eps=0.01, out=null_out()), (hkl, f1, f2)
  massage_data.run(
    args=[
      mtz_file,
      "aniso.action=None",
      "outlier.action=None",
      "symmetry.action=twin",
      "twin_law='l,-k,h'",
      "fraction=0.3",
      "hklout=tmp_massage_twinned.sca",
    ],
    out=null_out())
  assert op.isfile("tmp_massage_twinned.sca")
  massage_data.run(
    args=[
      "tmp_massage_twinned.mtz",
      "aniso.action=None",
      "outlier.action=None",
      "symmetry.action=detwin",
      "twin_law='l,-k,h'",
      "fraction=0.3",
      "hklout=tmp_massage_detwinned.mtz",
    ],
    out=null_out())
  mtz_in = file_reader.any_file("tmp_massage_detwinned.mtz")
  fc_detwin = mtz_in.file_server.miller_arrays[0].f_sq_as_f()
  fc_detwin, fc_tmp = fc_detwin.common_sets(other=fc)
  # XXX we appear to lose some accuracy here, possibly due to the use of
  # MTZ format
  for hkl, f1, f2 in zip(fc_tmp.indices(), fc_tmp.data(), fc_detwin.data()) :
    assert approx_equal(f1, f2, eps=0.01), hkl
Exemple #5
0
def exercise_xray_structure(use_u_aniso, verbose=0):
    structure = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("P 31"),
        elements=["N", "C", "C", "O", "Si"] * 2,
        volume_per_atom=500,
        min_distance=2.0,
        general_positions_only=False,
        random_u_iso=True,
        use_u_aniso=use_u_aniso,
    )
    f_abs = abs(structure.structure_factors(anomalous_flag=False, d_min=2, algorithm="direct").f_calc())
    for resname in (None, "res"):
        for fractional_coordinates in (False, True):
            pdb_file = structure.as_pdb_file(
                remark="Title", remarks=["Any", "Thing"], fractional_coordinates=fractional_coordinates, resname=resname
            )
            if 0 or verbose:
                sys.stdout.write(pdb_file)
            structure_read = iotbx.pdb.input(
                source_info=None, lines=flex.std_string(pdb_file.splitlines())
            ).xray_structure_simple(fractional_coordinates=fractional_coordinates, use_scale_matrix_if_available=False)
            f_read = abs(
                f_abs.structure_factors_from_scatterers(xray_structure=structure_read, algorithm="direct").f_calc()
            )
            regression = flex.linear_regression(f_abs.data(), f_read.data())
            assert regression.is_well_defined()
            if 0 or verbose:
                regression.show_summary()
            assert approx_equal(regression.slope(), 1, eps=1.0e-2)
            assert approx_equal(regression.y_intercept(), 0, eps=flex.max(f_abs.data()) * 0.01)
def get_xray_structure_random(space_group_info):
  xray_structure = random_structure.xray_structure(
      space_group_info  = space_group_info,
      elements          = ["N"]*100,
      volume_per_atom   = 50.0,
      random_u_iso      = True)
  return xray_structure
Exemple #7
0
def exercise_sdb(verbose=0):
  structure = random_structure.xray_structure(
    space_group_info=sgtbx.space_group_info("P 31"),
    elements=["N","C","C","O"]*2,
    volume_per_atom=500,
    min_distance=2.,
    general_positions_only=False,
    random_u_iso=True)
  f_abs = abs(structure.structure_factors(
    anomalous_flag=False, d_min=2, algorithm="direct").f_calc())
  sdb_out = structure.as_cns_sdb_file(
    file="foo.sdb",
    description="random_structure",
    comment=["any", "thing"],
    group="best")
  if (0 or verbose):
    sys.stdout.write(sdb_out)
  sdb_files = sdb_reader.multi_sdb_parser(StringIO(sdb_out))
  assert len(sdb_files) == 1
  structure_read = sdb_files[0].as_xray_structure(
    crystal_symmetry=crystal.symmetry(
      unit_cell=structure.unit_cell(),
      space_group_info=None))
  f_read = abs(f_abs.structure_factors_from_scatterers(
    xray_structure=structure_read, algorithm="direct").f_calc())
  regression = flex.linear_regression(f_abs.data(), f_read.data())
  assert regression.is_well_defined()
  if (0 or verbose):
    regression.show_summary()
  assert abs(regression.slope()-1) < 1.e-4
  assert abs(regression.y_intercept()) < 1.e-3
Exemple #8
0
def exercise_translational_phase_shift(n_sites=100,
                                       d_min=1.5,
                                       resolution_factor=0.3):
    sgi = space_group_info("P1")
    xrs = random_structure.xray_structure(
        space_group_info=sgi,
        elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
        volume_per_atom=50,
        min_distance=1.5)
    f_calc = xrs.structure_factors(d_min=d_min).f_calc()
    print f_calc.unit_cell()
    from scitbx.matrix import col
    shift_frac = col((.23984120, .902341127, .51219021))

    # Shift phases directly
    phase_shifted = f_calc.translational_shift(shift_frac=shift_frac)

    # Check that map from phase_shifted FC matches map calculated from
    #   translated xrs

    # Map from phase-shifted FC
    shifted_fft_map = phase_shifted.fft_map(
        resolution_factor=resolution_factor)
    shifted_fft_map.apply_sigma_scaling()
    shifted_map_data = shifted_fft_map.real_map_unpadded()
    cs = xrs.crystal_symmetry()
    from cctbx.maptbx import crystal_gridding
    cg = crystal_gridding(unit_cell=cs.unit_cell(),
                          space_group_info=cs.space_group_info(),
                          pre_determined_n_real=shifted_map_data.all())

    # Map from translated xrs
    sites_shifted = xrs.sites_frac() + shift_frac
    xrs.set_sites_frac(sites_shifted)
    f_calc_from_shifted_xrs = xrs.structure_factors(d_min=d_min).f_calc()
    fft_map_from_shifted_xrs = f_calc_from_shifted_xrs.fft_map(
        resolution_factor=resolution_factor, crystal_gridding=cg)
    map_data_from_shifted_xrs = fft_map_from_shifted_xrs.real_map_unpadded()

    # shifted_map_data (map from phase shifted f_calc),
    # map_data_from_shifted_xrs (recalculated with shifted xrs)

    assert shifted_map_data.all() == map_data_from_shifted_xrs.all()
    from cctbx import maptbx
    sel = maptbx.grid_indices_around_sites(unit_cell=xrs.unit_cell(),
                                           fft_n_real=shifted_map_data.focus(),
                                           fft_m_real=shifted_map_data.all(),
                                           sites_cart=xrs.sites_cart(),
                                           site_radii=flex.double(
                                               xrs.scatterers().size(), 1.5))
    shifted_map_data = shifted_map_data.select(sel)
    map_data_from_shifted_xrs = map_data_from_shifted_xrs.select(sel)

    cc_map_data_from_shifted_xrs_shifted_map_data = flex.linear_correlation(
        x=map_data_from_shifted_xrs.as_1d(),
        y=shifted_map_data.as_1d()).coefficient()
    print "cc_map_data_from_shifted_xrs_shifted_map_data",\
       cc_map_data_from_shifted_xrs_shifted_map_data
    assert cc_map_data_from_shifted_xrs_shifted_map_data > 0.99
    print "*" * 25
def run_call_back(flags, space_group_info):
  d_min = 2.0
  structure = random_structure.xray_structure(
    space_group_info=space_group_info,
    elements=["N", "C", "O", "S"]*3 + ["Fe"]*2,
    volume_per_atom=100)
  if (not space_group_info.group().is_centric()):
    fp_fdp_targets = [(-1,2), (-2,6)]
  else:
    fp_fdp_targets = [(-1,0), (-2,0)]
  anomalous_scatterer_groups = [
    xray.anomalous_scatterer_group(
      iselection=flex.size_t(),
      f_prime=fp,
      f_double_prime=fdp,
      refine=["f_prime", "f_double_prime"]) for fp,fdp in fp_fdp_targets]
  for i_seq,scatterer in enumerate(structure.scatterers()):
    if (scatterer.scattering_type == "S"):
      anomalous_scatterer_groups[0].iselection.append(i_seq)
    if (scatterer.scattering_type == "Fe"):
      anomalous_scatterer_groups[1].iselection.append(i_seq)
  for group in anomalous_scatterer_groups:
    group.copy_to_scatterers_in_place(scatterers=structure.scatterers())
  if (flags.Verbose):
    structure.show_summary().show_scatterers()
  f_obs = abs(structure.structure_factors(
    d_min=2.0, anomalous_flag=True).f_calc())
  if (flags.Verbose):
    f_obs.show_comprehensive_summary()
  #
  for group in anomalous_scatterer_groups:
    group.f_prime = 0
    group.f_double_prime = 0
    group.copy_to_scatterers_in_place(scatterers=structure.scatterers())
  sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
  sfg_params.algorithm = "direct"
  fmodel = mmtbx.f_model.manager(
    xray_structure=structure,
    f_obs=f_obs,
    r_free_flags=f_obs.generate_r_free_flags(),
    sf_and_grads_accuracy_params = sfg_params,
    target_name="ls")
  #
  n_cycles = [0]
  def call_back(minimizer):
    n_cycles[0] += 1
    return True
  minimized = mmtbx.refinement.anomalous_scatterer_groups.minimizer(
    fmodel=fmodel,
    groups=anomalous_scatterer_groups,
    call_back_after_minimizer_cycle=call_back,
    number_of_finite_difference_tests=3)
  assert n_cycles == [3]
  #
  for group,(fp,fdp) in zip(anomalous_scatterer_groups, fp_fdp_targets):
    # Large eps because the minimization doesn't reliably converge.
    # We don't want to exercise the minimizer here, the important
    # test is the finite difference test embedded in the minimizer.
    assert approx_equal(group.f_prime, fp, eps=1)
    assert approx_equal(group.f_double_prime, fdp, eps=1)
Exemple #10
0
def generate_random_f_calc(
      space_group_info,
      n_elements=10,
      d_min=1.5,
      verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=["Si"]*n_elements,
    volume_per_atom=1000,
    min_distance=3.,
    general_positions_only=False)
  if (0 or verbose):
    structure.show_summary()
    structure.show_scatterers()
    print
  print "Writing tmp.pdb"
  s = structure.as_pdb_file(
    remark="random structure",
    resname="RND")
  open("tmp.pdb", "w").write(s)
  if (0 or verbose):
    print
  f_calc = structure.structure_factors(
    d_min=d_min, anomalous_flag=False).f_calc()
  if (0 or verbose):
    f_calc.show_summary()
    print
  print "Writing: tmp.phs"
  f_calc.as_phases_phs(out=open("tmp.phs", "w"))
  if (0 or verbose):
    print
Exemple #11
0
def test_grid_step(n_sites=50, volume_per_atom=50, d_min=2.0):
    grid_step = (0.2, 0.4, 0.6, 0.7, 0.9, 1.0)
    for step in grid_step:
        symmetry = crystal.symmetry(space_group_symbol="P1")
        structure = random_structure.xray_structure(
            space_group_info=symmetry.space_group_info(),
            elements=["C"] * n_sites,
            volume_per_atom=volume_per_atom,
            random_u_iso=False)
        fc = structure.structure_factors(d_min=d_min,
                                         anomalous_flag=False,
                                         algorithm="fft").f_calc()
        manager = max_like_non_uniform.ordered_solvent_distribution(
            structure=structure, fo=fc, grid_step=step)
        f_water_dist = manager.fcalc_from_distribution()
        ### check phase compatibility with the symmetry:
        centrics = f_water_dist.select_centric()
        if (centrics.indices().size() > 0):
            ideal = centrics.phase_transfer(centrics)
            assert flex.max(flex.abs(ideal.data() - centrics.data())) < 1.e-6
        ###
        #print "max = ", flex.max( flex.abs( f_water_dist.data() ) )
        #print "min = ", flex.min( flex.abs( f_water_dist.data() ) )
        #print "ave = ", flex.mean( flex.abs( f_water_dist.data() ) )
        assert flex.max(flex.abs(f_water_dist.data())) < 1.0
Exemple #12
0
def skewness_calculation(space_group_info,
                         n_test_points=10,
                         n_sites=20,
                         d_min=3,
                         volume_per_atom=200):
    structure = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=["Se"] * n_sites,
        volume_per_atom=volume_per_atom,
        random_u_iso=True)
    structure.show_summary()
    print()
    f_calc = structure.structure_factors(d_min=d_min,
                                         anomalous_flag=False).f_calc()
    f_calc.show_summary()
    print()
    for i_fudge_factor in range(n_test_points + 1):
        fudge_factor = i_fudge_factor / float(n_test_points)
        randomized_f_calc = randomize_phases(f_calc, fudge_factor)
        mwpe = f_calc.mean_weighted_phase_error(randomized_f_calc)
        rho = randomized_f_calc.fft_map().real_map_unpadded()
        # <(rho-rho_bar)**3>/<(rho-rho_bar)**2>**3/2
        rho_rho_bar = rho - flex.mean(rho)
        num = flex.mean(flex.pow(rho_rho_bar, 3))
        den = flex.mean(flex.pow(rho_rho_bar, 2))**(3 / 2.)
        assert den != 0
        skewness = num / den
        print("fudge factor, phase difference, map skewness:", end=' ')
        print("%4.2f, %5.2f, %.4g" % (fudge_factor, mwpe, skewness))
    print()
def get_xray_structure_random(space_group_info):
    xray_structure = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=["N"] * 100,
        volume_per_atom=50.0,
        random_u_iso=True)
    return xray_structure
Exemple #14
0
def exercise_2():
    xray_structure = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("C 1 2/c 1"),
        elements=("O", "N", "C") * 50,
        volume_per_atom=100,
        min_distance=1.5,
        general_positions_only=True,
        random_u_iso=True,
        random_occupancy=True)
    xray_structure.scattering_type_registry(table="wk1995")
    f_obs = abs(xray_structure.structure_factors(d_min=2.0).f_calc())
    sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
    for algorithm in ["fft", "direct"]:
        flags = f_obs.generate_r_free_flags(fraction=0.1, max_free=99999999)
        fmodel = mmtbx.f_model.manager(xray_structure=xray_structure,
                                       f_obs=f_obs,
                                       r_free_flags=flags,
                                       sf_and_grads_accuracy_params=sfg_params)
        f_calc_1 = abs(fmodel.f_calc()).data()
        f_calc_2 = abs(
            f_obs.structure_factors_from_scatterers(
                xray_structure=xray_structure,
                algorithm=sfg_params.algorithm).f_calc()).data()
        delta = flex.abs(f_calc_1 - f_calc_2)
        assert approx_equal(flex.sum(delta), 0.0)
def run():
  xrs = random_structure.xray_structure(
    space_group_info = sgtbx.space_group_info("P1"),
    elements         = ["N"]*500,
    unit_cell        = (20, 30, 40, 70, 80, 120))
  xrs = xrs.set_b_iso(value=25)
  d_mins = [1.5]
  result = []
  for d_min in d_mins:
    f_obs = abs(xrs.structure_factors(d_min=d_min).f_calc())
    f_obs = f_obs.customized_copy(data = f_obs.data() * 135.)
    shifts = [0.0, 0.3]
    for xyz_shake_amount in shifts:
      xrs_shaken = xrs.deep_copy_scatterers()
      xrs_shaken.shake_sites_in_place(mean_distance = xyz_shake_amount)
      ml_err = flex.double()
      ml_err_new = flex.double()
      for trial in xrange(10):
        r_free_flags = f_obs.generate_r_free_flags()
        fmodel = mmtbx.f_model.manager(
          f_obs          = f_obs,
          r_free_flags   = r_free_flags,
          xray_structure = xrs_shaken)
        ml_err_ = fmodel.model_error_ml()
        ml_err.append(ml_err_)
      result.append(flex.mean(ml_err))
  assert result[0] > 0 and result[0] < 0.03
  assert result[1] > 0.2 and result[1] <= 0.32
def exercise_01 () :
  """
  Sanity check - don't crash when mean intensity for a bin is zero.
  """
  xrs = random_structure.xray_structure(
    unit_cell=(50,50,50,90,90,90),
    space_group_symbol="P1",
    n_scatterers=1200,
    elements="random")
  fc = abs(xrs.structure_factors(d_min=1.5).f_calc())
  fc = fc.set_observation_type_xray_amplitude()
  cs = fc.complete_set(d_min=1.4)
  ls = cs.lone_set(other=fc)
  f_zero = ls.array(data=flex.double(ls.size(), 0))
  f_zero.set_observation_type_xray_amplitude()
  fc = fc.concatenate(other=f_zero)
  sigf = flex.double(fc.size(), 0.1) + (fc.data() * 0.03)
  fc = fc.customized_copy(sigmas=sigf)
  try :
    fc_fc = french_wilson.french_wilson_scale(miller_array=fc, log=null_out())
  except Sorry :
    pass
  else :
    raise Exception_expected
  ic = fc.f_as_f_sq().set_observation_type_xray_intensity()
  fc_fc = french_wilson.french_wilson_scale(miller_array=ic, log=null_out())
Exemple #17
0
def exercise_centrics(space_group_info, n_sites=10):
    structure = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
        volume_per_atom=30,
        min_distance=1)
    for anomalous_flag in [False, True]:
        miller_set = miller.build_set(crystal_symmetry=structure,
                                      d_min=1,
                                      anomalous_flag=anomalous_flag)
        for shrink_truncation_radius in [0, .5 * 6**.5]:
            for solvent_radius in [0, .5 * 5**.5]:
                bulk_solvent_mask = mmtbx.masks.bulk_solvent(
                    xray_structure=structure,
                    grid_step=0.5,
                    ignore_zero_occupancy_atoms=False,
                    solvent_radius=solvent_radius,
                    shrink_truncation_radius=shrink_truncation_radius)
                f_mask = bulk_solvent_mask.structure_factors(
                    miller_set=miller_set)
                centrics = f_mask.select_centric()
                if (centrics.indices().size() > 0):
                    ideal = centrics.phase_transfer(centrics)
                    assert flex.max(
                        flex.abs(ideal.data() - centrics.data())) < 1.e-6
Exemple #18
0
def generate_random_f_calc(space_group_info,
                           n_elements=10,
                           d_min=1.5,
                           verbose=0):
    structure = random_structure.xray_structure(space_group_info,
                                                elements=["Si"] * n_elements,
                                                volume_per_atom=1000,
                                                min_distance=3.,
                                                general_positions_only=False)
    if (0 or verbose):
        structure.show_summary()
        structure.show_scatterers()
        print
    print "Writing tmp.pdb"
    s = structure.as_pdb_file(remark="random structure", resname="RND")
    open("tmp.pdb", "w").write(s)
    if (0 or verbose):
        print
    f_calc = structure.structure_factors(d_min=d_min,
                                         anomalous_flag=False).f_calc()
    if (0 or verbose):
        f_calc.show_summary()
        print
    print "Writing: tmp.phs"
    f_calc.as_phases_phs(out=open("tmp.phs", "w"))
    if (0 or verbose):
        print
Exemple #19
0
def run():
    xrs = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("P1"),
        elements=["N"] * 500,
        unit_cell=(20, 30, 40, 70, 80, 120))
    xrs = xrs.set_b_iso(value=25)
    d_mins = [1.5]
    result = []
    for d_min in d_mins:
        f_obs = abs(xrs.structure_factors(d_min=d_min).f_calc())
        f_obs = f_obs.customized_copy(data=f_obs.data() * 135.)
        shifts = [0.0, 0.3]
        for xyz_shake_amount in shifts:
            xrs_shaken = xrs.deep_copy_scatterers()
            xrs_shaken.shake_sites_in_place(mean_distance=xyz_shake_amount)
            ml_err = flex.double()
            ml_err_new = flex.double()
            for trial in range(10):
                r_free_flags = f_obs.generate_r_free_flags()
                fmodel = mmtbx.f_model.manager(f_obs=f_obs,
                                               r_free_flags=r_free_flags,
                                               xray_structure=xrs_shaken)
                ml_err_ = fmodel.model_error_ml()
                ml_err.append(ml_err_)
            result.append(flex.mean(ml_err))
    assert result[0] > 0 and result[0] < 0.03
    assert result[1] > 0.2 and result[1] <= 0.32
Exemple #20
0
def exercise_3():
    from mmtbx import masks
    from cctbx import sgtbx
    xs = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("P1"),
        elements=["C"] * 2,
        unit_cell=(10, 20, 30, 70, 80, 120))
    f_calc = xs.structure_factors(d_min=2.0).f_calc()
    mp = masks.mask_master_params.extract()
    mm1 = masks.manager(miller_array=f_calc, xray_structure=xs)
    assert list(xs.scatterers().extract_occupancies()) == [1.0, 1.0]
    fmasks1 = mm1.shell_f_masks()
    assert len(fmasks1) == 1
    assert flex.mean(flex.abs(fmasks1[0].data())) > 4.0
    #
    xs.set_occupancies(value=0)
    mp.ignore_zero_occupancy_atoms = False
    mp.use_asu_masks = False
    mm2 = masks.manager(miller_array=f_calc, xray_structure=xs, mask_params=mp)
    assert list(xs.scatterers().extract_occupancies()) == [0.0, 0.0]
    fmasks1 = mm1.shell_f_masks()
    assert len(fmasks1) == 1
    fmasks2 = mm2.shell_f_masks()
    assert len(fmasks2) == 1
    assert flex.mean( flex.abs(fmasks1[0].data()) ) == \
           flex.mean( flex.abs(fmasks2[0].data()) )
    #
    mp.ignore_zero_occupancy_atoms = True
    mp.use_asu_masks = False
    mm3 = masks.manager(miller_array=f_calc, xray_structure=xs, mask_params=mp)
    assert list(xs.scatterers().extract_occupancies()) == [0.0, 0.0]
    fmasks3 = mm3.shell_f_masks()
    assert len(fmasks3) == 1
    assert approx_equal(
        flex.abs(fmasks3[0].data()).min_max_mean().as_tuple(), (0.0, 0.0, 0.0))
def exercise_ellipsoidal_truncation(space_group_info, n_sites=100, d_min=1.5):
    xrs = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
        volume_per_atom=50,
        min_distance=1.5,
    )
    f_obs = abs(xrs.structure_factors(d_min=d_min).f_calc())
    # exercise reciprocal_space_vector()
    for mi, d in zip(f_obs.indices(), f_obs.d_spacings().data()):
        rsv = flex.double(f_obs.unit_cell().reciprocal_space_vector(mi))
        assert approx_equal(d, 1.0 / math.sqrt(rsv.dot(rsv)))
    ##
    print f_obs.unit_cell()
    f = flex.random_double(f_obs.data().size()) * flex.mean(f_obs.data()) / 10
    #
    f_obs1 = f_obs.customized_copy(data=f_obs.data(), sigmas=f_obs.data() * f)
    print "datat in:", f_obs1.data().size()
    r = f_obs1.ellipsoidal_truncation_by_sigma(sigma_cutoff=1)
    print "data left:", r.data().size()
    r.miller_indices_as_pdb_file(file_name="indices1.pdb", expand_to_p1=False)
    r.miller_indices_as_pdb_file(file_name="indices2.pdb", expand_to_p1=True)
    #
    f_obs.miller_indices_as_pdb_file(file_name="indices3.pdb", expand_to_p1=False)
    f_obs.miller_indices_as_pdb_file(file_name="indices4.pdb", expand_to_p1=True)
    print "*" * 25
Exemple #22
0
def get_random_structure_and_map(
    use_static_structure=False,
    random_seed=171413,
):

    if use_static_structure:
        mmm = map_model_manager()
        mmm.generate_map()
        return group_args(model=mmm.model(), mm=mmm.map_manager())
    import random
    random.seed(random_seed)
    i = random.randint(1, 714717)
    flex.set_random_seed(i)

    xrs = random_structure.xray_structure(
        space_group_info=space_group_info(19),
        volume_per_atom=25.,
        elements=('C', 'N', 'O', 'H') * 10,
        min_distance=1.5)
    fc = xrs.structure_factors(d_min=2).f_calc()
    fft_map = fc.fft_map(resolution_factor=0.25)
    fft_map.apply_volume_scaling()
    ph = iotbx.pdb.input(source_info=None,
                         lines=xrs.as_pdb_file()).construct_hierarchy()
    ph.atoms().set_xyz(xrs.sites_cart())
    map_data = fft_map.real_map_unpadded()
    mm = map_manager(unit_cell_grid=map_data.accessor().all(),
                     unit_cell_crystal_symmetry=fc.crystal_symmetry(),
                     origin_shift_grid_units=(0, 0, 0),
                     map_data=map_data)
    model = mmtbx.model.manager(model_input=None,
                                pdb_hierarchy=ph,
                                crystal_symmetry=fc.crystal_symmetry())
    return group_args(model=model, mm=mm)
def exercise_f_model_no_scales(symbol = "C 2"):
  random.seed(0)
  flex.set_random_seed(0)
  x = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info(symbol=symbol),
    elements               =(("O","N","C")*5+("H",)*10),
    volume_per_atom        = 200,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = False)
  f_obs = abs(x.structure_factors(d_min = 1.5, algorithm="fft").f_calc())
  x.shake_sites_in_place(mean_distance=1)
  k_iso = flex.double(f_obs.data().size(), 2)
  k_aniso = flex.double(f_obs.data().size(), 3)
  fmodel = mmtbx.f_model.manager(
    xray_structure = x,
    k_isotropic    = k_iso,
    k_anisotropic  = k_aniso,
    f_obs          = f_obs)
  fc = abs(fmodel.f_calc()).data()
  fm = abs(fmodel.f_model()).data()
  fmns = abs(fmodel.f_model_no_scales()).data()
  assert approx_equal(flex.mean(fm/fc), 6)
  assert approx_equal(flex.mean(fmns/fc), 1)
Exemple #24
0
def exercise_xray_structure(use_u_aniso, verbose=0):
  structure = random_structure.xray_structure(
    space_group_info=sgtbx.space_group_info("P 31"),
    elements=["N","C","C","O","Si"]*2,
    volume_per_atom=500,
    min_distance=2.,
    general_positions_only=False,
    random_u_iso=True,
    use_u_aniso=use_u_aniso)
  f_abs = abs(structure.structure_factors(
    anomalous_flag=False, d_min=2, algorithm="direct").f_calc())
  for resname in (None, "res"):
    for fractional_coordinates in (False, True):
      pdb_file = structure.as_pdb_file(
        remark="Title", remarks=["Any", "Thing"],
        fractional_coordinates=fractional_coordinates,
        resname=resname)
      if (0 or verbose):
        sys.stdout.write(pdb_file)
      structure_read = iotbx.pdb.input(
        source_info=None,
        lines=flex.std_string(pdb_file.splitlines())).xray_structure_simple(
          fractional_coordinates=fractional_coordinates,
          use_scale_matrix_if_available=False)
      f_read = abs(f_abs.structure_factors_from_scatterers(
        xray_structure=structure_read, algorithm="direct").f_calc())
      regression = flex.linear_regression(f_abs.data(), f_read.data())
      assert regression.is_well_defined()
      if (0 or verbose):
        regression.show_summary()
      assert approx_equal(regression.slope(), 1, eps=1.e-2)
      assert approx_equal(
        regression.y_intercept(), 0, eps=flex.max(f_abs.data())*0.01)
Exemple #25
0
def exercise_twin_detwin():
  random.seed(12345)
  flex.set_random_seed(12345)
  xrs = random_structure.xray_structure(
    unit_cell=(12,5,12,90,90,90),
    space_group_symbol="P1",
    n_scatterers=12,
    elements="random")
  fc = abs(xrs.structure_factors(d_min=1.5).f_calc())
  fc = fc.set_observation_type_xray_amplitude()
  mtz_file = "tmp_massage_in.mtz"
  fc.as_mtz_dataset(column_root_label="F").mtz_object().write(mtz_file)
  massage_data.run(
    args=[
      mtz_file,
      "aniso.action=None",
      "outlier.action=None",
      "symmetry.action=twin",
      "twin_law='l,-k,h'",
      "fraction=0.3",
      "hklout=tmp_massage_twinned.mtz",
    ],
    out=null_out())
  assert op.isfile("tmp_massage_twinned.mtz")
  mtz_in = file_reader.any_file("tmp_massage_twinned.mtz")
  fc_twin = mtz_in.file_server.miller_arrays[0].f_sq_as_f()
  fc_twin, fc_tmp = fc_twin.common_sets(other=fc)
  for hkl, f1, f2 in zip(fc_tmp.indices(), fc_tmp.data(), fc_twin.data()):
    if (abs(hkl[0]) != abs(hkl[2])):
      assert not approx_equal(f1, f2, eps=0.01, out=null_out()), (hkl, f1, f2)
  massage_data.run(
    args=[
      mtz_file,
      "aniso.action=None",
      "outlier.action=None",
      "symmetry.action=twin",
      "twin_law='l,-k,h'",
      "fraction=0.3",
      "hklout=tmp_massage_twinned.sca",
    ],
    out=null_out())
  assert op.isfile("tmp_massage_twinned.sca")
  massage_data.run(
    args=[
      "tmp_massage_twinned.mtz",
      "aniso.action=None",
      "outlier.action=None",
      "symmetry.action=detwin",
      "twin_law='l,-k,h'",
      "fraction=0.3",
      "hklout=tmp_massage_detwinned.mtz",
    ],
    out=null_out())
  mtz_in = file_reader.any_file("tmp_massage_detwinned.mtz")
  fc_detwin = mtz_in.file_server.miller_arrays[0].f_sq_as_f()
  fc_detwin, fc_tmp = fc_detwin.common_sets(other=fc)
  # XXX we appear to lose some accuracy here, possibly due to the use of
  # MTZ format
  for hkl, f1, f2 in zip(fc_tmp.indices(), fc_tmp.data(), fc_detwin.data()):
    assert approx_equal(f1, f2, eps=0.01), hkl
Exemple #26
0
def exercise(flags, space_group_info):
    # Prepare a structure compatible with the ShelX model
    xs = random_structure.xray_structure(space_group_info,
                                         elements="random",
                                         n_scatterers=10,
                                         use_u_iso=True,
                                         random_u_iso=True,
                                         use_u_aniso=True)
    xs.apply_symmetry_sites()
    xs.apply_symmetry_u_stars()
    for isotropic, sc in itertools.izip(variate(bernoulli_distribution(0.4)),
                                        xs.scatterers()):
        sc.flags.set_grad_site(True)
        if isotropic:
            sc.flags.set_use_u_iso(True)
            sc.flags.set_use_u_aniso(False)
            sc.flags.set_grad_u_iso(True)
        else:
            sc.flags.set_use_u_iso(False)
            sc.flags.set_use_u_aniso(True)
            sc.flags.set_grad_u_aniso(True)

    not_origin_centric = (xs.space_group().is_centric()
                          and not xs.space_group().is_origin_centric())

    try:
        ins = list(
            shelx.writer.generator(xs,
                                   full_matrix_least_squares_cycles=4,
                                   weighting_scheme_params=(0, 0),
                                   sort_scatterers=False))
    except AssertionError:
        if (not_origin_centric):
            print(
                "Omitted %s\n  because it is centric but not origin centric" %
                xs.space_group().type().hall_symbol())
            return
        raise
    else:
        if (not_origin_centric):
            raise Exception_expected

    ins = cStringIO.StringIO("".join(ins))
    xs1 = xs.from_shelx(file=ins)
    xs.crystal_symmetry().is_similar_symmetry(xs1.crystal_symmetry(),
                                              relative_length_tolerance=1e-3,
                                              absolute_angle_tolerance=1e-3)
    uc = xs.unit_cell()
    uc1 = xs1.unit_cell()
    for sc, sc1 in itertools.izip(xs.scatterers(), xs1.scatterers()):
        assert sc.label.upper() == sc1.label.upper()
        assert sc.scattering_type == sc1.scattering_type
        assert sc.flags.bits == sc1.flags.bits
        assert approx_equal(sc.site, sc1.site, eps=1e-6)
        if sc.flags.use_u_iso():
            assert approx_equal(sc.u_iso, sc1.u_iso, eps=1e-5)
        else:
            assert approx_equal(adptbx.u_star_as_u_cif(uc, sc.u_star),
                                adptbx.u_star_as_u_cif(uc1, sc1.u_star),
                                eps=1e-5)
def randomly_exercise(
    flipping_type,
    space_group_info,
    elements,
    anomalous_flag,
    d_min,
    grid_resolution_factor=1.0 / 2,
    verbose=False,
    amplitude_type="F",
):

    # Generate a random structure in real space, that we will try to recover
    target_structure = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=elements,
        use_u_iso=True,
        random_u_iso=True,
        random_u_iso_scale=0.04,
        use_u_aniso=False,
    )
    exercise_one_structure(
        target_structure,
        flipping_type,
        anomalous_flag,
        d_min,
        grid_resolution_factor=grid_resolution_factor,
        verbose=verbose,
        amplitude_type=amplitude_type,
    )
Exemple #28
0
 def __init__(self, model_id="SBT", n_elements=4):
     if (model_id is None): return
     self.model_id = model_id
     pos = emma.position
     if (type(model_id) == type("")):
         if (model_id == "SBT"):
             emma.model.__init__(
                 self,
                 crystal.special_position_settings(
                     crystal.symmetry((16.8986, 16.8986, 16.8986, 61.1483,
                                       61.1483, 61.1483), "R -3 m :R")),
                 (pos("SI1", (-0.3584, 0.2844, 0.4622)),
                  pos("SI2", (-0.2133, 0.9659, -0.6653)),
                  pos("SI3", (-0.8358, 0.7, 0.3431)),
                  pos("SI4", (0.4799, 1.836, 0.6598))))
         else:
             raise RuntimeError("Unknown model_id: " + model_id)
     else:
         structure = random_structure.xray_structure(model_id,
                                                     elements=["S"] *
                                                     n_elements,
                                                     volume_per_atom=50.,
                                                     min_distance=2.0)
         positions = []
         for scatterer in structure.scatterers():
             positions.append(emma.position(scatterer.label,
                                            scatterer.site))
         emma.model.__init__(self, structure, positions)
Exemple #29
0
def exercise_01(grid_step = 0.03, d_min = 1.0, wing_cutoff = 1.e-9):
  xrs = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info("P 1"),
    elements               = ["O","N","C","P","S","U","AU"]*1,
    random_u_iso           = True,
    general_positions_only = False)
  # avoid excessive_range_error_limit crash
  bs = xrs.extract_u_iso_or_u_equiv()*adptbx.u_as_b(1)
  sel = bs < 1
  bs = bs.set_selected(sel, 1)
  xrs.set_b_iso(values = bs)
  #
  p = xrs.unit_cell().parameters()
  timer = user_plus_sys_time()
  res = manager(nx = int(p[0]/grid_step),
                ny = int(p[1]/grid_step),
                nz = int(p[2]/grid_step),
                scattering_type_registry = xrs.scattering_type_registry(),
                unit_cell = xrs.unit_cell(),
                scatterers = xrs.scatterers(),
                wing_cutoff = wing_cutoff)
  print "time: %10.4f" % (timer.elapsed())
  f_calc_dir = xrs.structure_factors(
    d_min     = d_min,
    algorithm = "direct").f_calc()
  #
  f_calc_den = f_calc_dir.structure_factors_from_map(map = res.density_array,
    use_scale = True)
  f1 = flex.abs(f_calc_dir.data())
  f2 = flex.abs(f_calc_den.data())
  r = flex.sum(flex.abs(f1-f2))/flex.sum(f2)
  print "r-factor:", r
  assert r < 1.e-4, r
    def random_structure(self, crystal):
        """We're going to do some very approximate stuff here.  Given a unit
     cell & SG, will put typical atomic contents in the unit cell & get
     structure factors.
    """
        import random
        random.seed(0)
        from scitbx.array_family import flex
        flex.set_random_seed(0)
        from cctbx.development import random_structure

        uc_volume = crystal.get_unit_cell().volume()
        asu_volume = uc_volume / crystal.get_space_group().order_z()
        target_number_scatterers = int(
            asu_volume
        ) // 128  # Very approximate rule of thumb for proteins with ~50% solvent content
        element_unit = ['O'] * 19 + ['N'] * 18 + ['C'] * 62 + ['S'] * 1 + [
            'Fe'
        ] * 1
        element_pallet = element_unit * (
            1 + (target_number_scatterers // len(element_unit)))
        assert len(element_pallet) >= target_number_scatterers
        # Ersatz hard limit to prevent excessive execution time of xray_structure() below.
        elements = element_pallet[:min(1000, target_number_scatterers)]

        xs = random_structure.xray_structure(
            space_group_info=crystal.get_space_group().info(),
            unit_cell=crystal.get_unit_cell(),
            elements=elements,
            min_distance=1.2)
        self.xs = xs
Exemple #31
0
def exercise_sdb(verbose=0):
    structure = random_structure.xray_structure(
        space_group_info=sgtbx.space_group_info("P 31"),
        elements=["N", "C", "C", "O"] * 2,
        volume_per_atom=500,
        min_distance=2.,
        general_positions_only=False,
        random_u_iso=True)
    f_abs = abs(
        structure.structure_factors(anomalous_flag=False,
                                    d_min=2,
                                    algorithm="direct").f_calc())
    sdb_out = structure.as_cns_sdb_file(file="foo.sdb",
                                        description="random_structure",
                                        comment=["any", "thing"],
                                        group="best")
    if (0 or verbose):
        sys.stdout.write(sdb_out)
    sdb_files = sdb_reader.multi_sdb_parser(StringIO(sdb_out))
    assert len(sdb_files) == 1
    structure_read = sdb_files[0].as_xray_structure(
        crystal_symmetry=crystal.symmetry(unit_cell=structure.unit_cell(),
                                          space_group_info=None))
    f_read = abs(
        f_abs.structure_factors_from_scatterers(xray_structure=structure_read,
                                                algorithm="direct").f_calc())
    regression = flex.linear_regression(f_abs.data(), f_read.data())
    assert regression.is_well_defined()
    if (0 or verbose):
        regression.show_summary()
    assert abs(regression.slope() - 1) < 1.e-4
    assert abs(regression.y_intercept()) < 1.e-3
Exemple #32
0
def exercise_ellipsoidal_truncation(space_group_info, n_sites=100, d_min=1.5):
    xrs = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
        volume_per_atom=50,
        min_distance=1.5)
    f_obs = abs(xrs.structure_factors(d_min=d_min).f_calc())
    # exercise reciprocal_space_vector()
    for mi, d in zip(f_obs.indices(), f_obs.d_spacings().data()):
        rsv = flex.double(f_obs.unit_cell().reciprocal_space_vector(mi))
        assert approx_equal(d, 1. / math.sqrt(rsv.dot(rsv)))
    ##
    print f_obs.unit_cell()
    f = flex.random_double(f_obs.data().size()) * flex.mean(f_obs.data()) / 10
    #
    f_obs1 = f_obs.customized_copy(data=f_obs.data(), sigmas=f_obs.data() * f)
    print "datat in:", f_obs1.data().size()
    r = f_obs1.ellipsoidal_truncation_by_sigma(sigma_cutoff=1)
    print "data left:", r.data().size()
    r.miller_indices_as_pdb_file(file_name="indices1.pdb", expand_to_p1=False)
    r.miller_indices_as_pdb_file(file_name="indices2.pdb", expand_to_p1=True)
    #
    f_obs.miller_indices_as_pdb_file(file_name="indices3.pdb",
                                     expand_to_p1=False)
    f_obs.miller_indices_as_pdb_file(file_name="indices4.pdb",
                                     expand_to_p1=True)
    print "*" * 25
 def __init__(self, model_id="SBT", n_elements=4):
   if (model_id is None): return
   self.model_id = model_id
   pos = emma.position
   if (type(model_id) == type("")):
     if (model_id == "SBT"):
       emma.model.__init__(self,
         crystal.special_position_settings(crystal.symmetry(
           (16.8986, 16.8986, 16.8986, 61.1483, 61.1483, 61.1483),
           "R -3 m :R")),
         (pos("SI1", (-0.3584, 0.2844, 0.4622)),
          pos("SI2", (-0.2133, 0.9659, -0.6653)),
          pos("SI3", (-0.8358, 0.7, 0.3431)),
          pos("SI4", (0.4799, 1.836, 0.6598))))
     else:
       raise RuntimeError, "Unknown model_id: " + model_id
   else:
     structure = random_structure.xray_structure(
       model_id,
       elements=["S"]*n_elements,
       volume_per_atom=50.,
       min_distance=2.0)
     positions = []
     for scatterer in structure.scatterers():
       positions.append(emma.position(scatterer.label, scatterer.site))
     emma.model.__init__(self, structure, positions)
Exemple #34
0
def exercise_writer(space_group_info, n_sites=100, d_min=1.5):
    if op.isfile("tst_iotbx_dsn6.omap"):
        os.remove("tst_iotbx_dsn6.omap")
    xrs = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=(("O", "N", "C") * (n_sites // 3 + 1))[:n_sites],
        volume_per_atom=50,
        min_distance=1.5)
    fc = xrs.structure_factors(d_min=d_min).f_calc()
    fft_map = fc.fft_map(resolution_factor=1 / 3).apply_sigma_scaling()
    n_real = fft_map.n_real()
    n_blocks = iceil(n_real[0] / 8) * iceil(n_real[1] / 8) * iceil(
        n_real[2] / 8)
    fft_map.as_dsn6_map(file_name="tst_iotbx_dsn6.omap")
    assert op.isfile("tst_iotbx_dsn6.omap")
    size = op.getsize("tst_iotbx_dsn6.omap")
    assert (size == (n_blocks + 1) * 512)
    os.remove("tst_iotbx_dsn6.omap")
    fft_map.as_dsn6_map(file_name="tst_iotbx_dsn6.omap",
                        gridding_first=[-5, -5, -5],
                        gridding_last=n_real)
    n_blocks = iceil(1 + n_real[0] / 8) * iceil(1 + n_real[1] /
                                                8) * iceil(1 + n_real[2] / 8)
    size = op.getsize("tst_iotbx_dsn6.omap")
    assert (size == (n_blocks + 1) * 512)
def exercise(space_group_info, anomalous_flag,
             n_scatterers=8, d_min=2, verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=["const"]*n_scatterers)
  f_calc = structure.structure_factors(
    d_min=d_min, anomalous_flag=anomalous_flag).f_calc()
  f = abs(f_calc)
  f = miller.array(miller_set=f, data=f.data(), sigmas=flex.sqrt(f.data()))
  f = f.f_as_f_sq()
  g = f.expand_to_p1()
  merger_p1 = xray.merger( g.indices(),
                              g.data(),
                              g.sigmas(),
                              g.space_group(),
                              g.anomalous_flag(),
                              g.unit_cell() )
  p1_bic = merger_p1.bic()
  p1_r = merger_p1.r_abs()

  merger_nat = xray.merger( g.indices(),
                              g.data(),
                              g.sigmas(),
                              f.space_group(),
                              g.anomalous_flag(),
                              g.unit_cell() )
  nat_bic = merger_nat.bic()
  nat_r = merger_nat.r_abs()
  assert nat_bic >= p1_bic
  assert p1_r <= 1e-8
Exemple #36
0
def test_less_one(space_group_info, volume_per_atom=100, d_min=1.8):

    #symmetry = crystal.symmetry(space_group_symbol="p212121")
    #space_group_info = symmetry.space_group_info()
    #n_sym = space_group_info.type().group().n_smx()
    for n_atoms in (1, 10):
        if (n_atoms == 1):
            denom = 16.0
        else:
            denom = 4.0

        for element in ("C", "O", "N"):
            structure = random_structure.xray_structure(
                space_group_info=space_group_info,
                elements=[element] * n_atoms,
                volume_per_atom=volume_per_atom,
                random_u_iso=False)

            fc = structure.structure_factors(d_min=d_min,
                                             anomalous_flag=False,
                                             algorithm="fft").f_calc()
            manager = max_like_non_uniform.ordered_solvent_distribution(
                structure=structure, fo=fc, grid_step=fc.d_min() / denom)
            f_water_dist = manager.fcalc_from_distribution()
            ### check phase compatibility with the symmetry:
            centrics = f_water_dist.select_centric()
            if (centrics.indices().size() > 0):
                ideal = centrics.phase_transfer(centrics)
                assert flex.max(
                    flex.abs(ideal.data() - centrics.data())) < 1.e-6
            ###
            #print "max = ", flex.max( flex.abs( f_water_dist.data() ) )
            #print "min = ", flex.min( flex.abs( f_water_dist.data() ) )
            #print "ave = ", flex.mean( flex.abs( f_water_dist.data() ) )
            assert flex.max(flex.abs(f_water_dist.data())) < 1.0
def skewness_calculation(space_group_info, n_test_points=10,
                         n_sites=20, d_min=3, volume_per_atom=200):
  structure = random_structure.xray_structure(
    space_group_info=space_group_info,
    elements=["Se"]*n_sites,
    volume_per_atom=volume_per_atom,
    random_u_iso=True)
  structure.show_summary()
  print
  f_calc = structure.structure_factors(
    d_min=d_min, anomalous_flag=False).f_calc()
  f_calc.show_summary()
  print
  for i_fudge_factor in xrange(n_test_points+1):
    fudge_factor = i_fudge_factor/float(n_test_points)
    randomized_f_calc = randomize_phases(f_calc, fudge_factor)
    mwpe = f_calc.mean_weighted_phase_error(randomized_f_calc)
    rho = randomized_f_calc.fft_map().real_map_unpadded()
    # <(rho-rho_bar)**3>/<(rho-rho_bar)**2>**3/2
    rho_rho_bar = rho - flex.mean(rho)
    num = flex.mean(flex.pow(rho_rho_bar, 3))
    den = flex.mean(flex.pow(rho_rho_bar, 2))**(3/2.)
    assert den != 0
    skewness = num / den
    print "fudge factor, phase difference, map skewness:",
    print "%4.2f, %5.2f, %.4g" % (fudge_factor, mwpe, skewness)
  print
Exemple #38
0
def exercise_f_model_no_scales(symbol = "C 2"):
  random.seed(0)
  flex.set_random_seed(0)
  x = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info(symbol=symbol),
    elements               =(("O","N","C")*5+("H",)*10),
    volume_per_atom        = 200,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = False)
  f_obs = abs(x.structure_factors(d_min = 1.5, algorithm="fft").f_calc())
  x.shake_sites_in_place(mean_distance=1)
  k_iso = flex.double(f_obs.data().size(), 2)
  k_aniso = flex.double(f_obs.data().size(), 3)
  fmodel = mmtbx.f_model.manager(
    xray_structure = x,
    k_isotropic    = k_iso,
    k_anisotropic  = k_aniso,
    f_obs          = f_obs)
  fc = abs(fmodel.f_calc()).data()
  fm = abs(fmodel.f_model()).data()
  fmns = abs(fmodel.f_model_no_scales()).data()
  assert approx_equal(flex.mean(fm/fc), 6)
  assert approx_equal(flex.mean(fmns/fc), 1)
Exemple #39
0
def exercise_01():
    """
  Sanity check - don't crash when mean intensity for a bin is zero.
  """
    xrs = random_structure.xray_structure(unit_cell=(50, 50, 50, 90, 90, 90),
                                          space_group_symbol="P1",
                                          n_scatterers=1200,
                                          elements="random")
    fc = abs(xrs.structure_factors(d_min=1.5).f_calc())
    fc = fc.set_observation_type_xray_amplitude()
    cs = fc.complete_set(d_min=1.4)
    ls = cs.lone_set(other=fc)
    f_zero = ls.array(data=flex.double(ls.size(), 0))
    f_zero.set_observation_type_xray_amplitude()
    fc = fc.concatenate(other=f_zero)
    sigf = flex.double(fc.size(), 0.1) + (fc.data() * 0.03)
    fc = fc.customized_copy(sigmas=sigf)
    try:
        fc_fc = french_wilson.french_wilson_scale(miller_array=fc,
                                                  log=null_out())
    except Sorry:
        pass
    else:
        raise Exception_expected
    ic = fc.f_as_f_sq().set_observation_type_xray_intensity()
    fc_fc = french_wilson.french_wilson_scale(miller_array=ic, log=null_out())
def exercise(flags, space_group_info, n_sampled):
  symbol = space_group_info.type().hall_symbol()
  print symbol,
  if flags.fix_seed:
    random.seed(0)
  if not flags.include_high_symmetry:
    if space_group_info.group().order_p() > 8:
      if len(symbol) > 15: print
      print "  [ Omitted, rerun with --include_high_symmetry to override ]"
      return
  print
  n = int(flags.repeats)
  if n == 0: n = 1
  progress = progress_displayed_as_fraction(n)
  for i in xrange(n):
    xs = random_structure.xray_structure(
      space_group_info=space_group_info,
      elements=['C']*5 + ['O']*2 + ['N'],
      use_u_iso=True,
      random_u_iso=True,
      random_u_iso_scale=0.04,
      use_u_aniso=False)
    f_c = miller.build_set(
      xs, anomalous_flag=False, d_min=0.8
      ).structure_factors_from_scatterers(
        xs, algorithm='direct').f_calc()
    f_o = f_c.as_amplitude_array()
    f_c_in_p1 = f_c.expand_to_p1()
    exercise_value(f_c_in_p1, f_o, flags, n_sampled)
    exercise_gradient(f_c_in_p1, f_o, flags, n_sampled)
    progress.advance()
  progress.done()
def run():
  structure = random_structure.xray_structure(
    sgtbx.space_group_info("P21/c"),
    elements=["Si"]*10,
    volume_per_atom=18.6,
    min_distance=1.2,
    general_positions_only=False)
  miller_set_f_obs = miller.build_set(
    crystal_symmetry=structure,
    anomalous_flag=True,
    d_min=0.8)
  f_obs = miller_set_f_obs.structure_factors_from_scatterers(
    xray_structure=structure,
    algorithm="direct").f_calc()
  fft_map = f_obs.fft_map(symmetry_flags=maptbx.use_space_group_symmetry)

  padded = fft_map.real_map()
  unpadded = fft_map.real_map_unpadded() # copy
  unpadded_1d = unpadded.as_1d() # 1D view => in-place
  mmm = flex.min_max_mean_double(unpadded_1d)

  for delta in ((mmm.min + mmm.mean)/2, mmm.mean, (mmm.mean + mmm.max)/2):
    # in-place charge flipping
    ab_initio.ext.flip_charges_in_place(padded, delta)

    # same but on an unpadded copy using the flex tools
    flipped_selection = unpadded_1d < delta
    flipped = unpadded_1d.select(flipped_selection)
    flipped *= -1
    unpadded_1d.set_selected(flipped_selection, flipped)

    assert approx_equal(padded, unpadded, 1e-15)

  print format_cpu_times()
Exemple #42
0
def random_f_calc(space_group_info,
                  n_scatterers,
                  d_min,
                  anomalous_flag,
                  verbose=0):
    if (anomalous_flag and space_group_info.group().is_centric()):
        return None
    structure = random_structure.xray_structure(space_group_info,
                                                elements=["const"] *
                                                n_scatterers,
                                                volume_per_atom=500,
                                                min_distance=2.,
                                                general_positions_only=True)
    if (0 or verbose):
        structure.show_summary().show_scatterers()
    f_calc = structure.structure_factors(
        d_min=d_min, anomalous_flag=anomalous_flag).f_calc()
    f_calc = miller.array(miller_set=f_calc,
                          data=f_calc.data() /
                          flex.mean(flex.abs(f_calc.data())))
    if (f_calc.anomalous_flag()):
        selection = flex.bool(f_calc.indices().size(), True)
        for i in xrange(f_calc.indices().size() // 10):
            j = random.randrange(f_calc.indices().size())
            selection[j] = False
        f_calc = f_calc.select(selection)
    return f_calc
Exemple #43
0
def exercise_subtract_continuous_allowed_origin_shifts(space_group_info,
                                                       use_niggli_cell,
                                                       n_elements=3):
    structure = random_structure.xray_structure(space_group_info,
                                                elements=["Si"] * n_elements,
                                                volume_per_atom=300,
                                                min_distance=3.,
                                                general_positions_only=False)
    if (use_niggli_cell):
        structure = structure.niggli_cell()
    f_obs = abs(
        structure.structure_factors(d_min=3, algorithm="direct").f_calc())
    assert f_obs.indices().size() >= 10
    transl = matrix.col(flex.random_double_point_on_sphere()) * 2.345
    transl_no_cont = matrix.col(
        structure.subtract_continuous_allowed_origin_shifts(
            translation_cart=transl))
    transl_cont = transl - transl_no_cont
    structure_transl = structure.apply_shift(
        shift=structure.unit_cell().fractionalize(transl_cont),
        recompute_site_symmetries=True)
    f_transl = abs(
        f_obs.structure_factors_from_scatterers(
            xray_structure=structure_transl, algorithm="direct").f_calc())
    assert approx_equal(f_transl.data(), f_obs.data())
def exercise(flags, space_group_info, n_sampled):
    symbol = space_group_info.type().hall_symbol()
    print symbol,
    if flags.fix_seed:
        random.seed(0)
    if not flags.include_high_symmetry:
        if space_group_info.group().order_p() > 8:
            if len(symbol) > 15: print
            print "  [ Omitted, rerun with --include_high_symmetry to override ]"
            return
    print
    n = int(flags.repeats)
    if n == 0: n = 1
    progress = progress_displayed_as_fraction(n)
    for i in xrange(n):
        xs = random_structure.xray_structure(space_group_info=space_group_info,
                                             elements=['C'] * 5 + ['O'] * 2 +
                                             ['N'],
                                             use_u_iso=True,
                                             random_u_iso=True,
                                             random_u_iso_scale=0.04,
                                             use_u_aniso=False)
        f_c = miller.build_set(xs, anomalous_flag=False,
                               d_min=0.8).structure_factors_from_scatterers(
                                   xs, algorithm='direct').f_calc()
        f_o = f_c.as_amplitude_array()
        f_c_in_p1 = f_c.expand_to_p1()
        exercise_value(f_c_in_p1, f_o, flags, n_sampled)
        exercise_gradient(f_c_in_p1, f_o, flags, n_sampled)
        progress.advance()
    progress.done()
Exemple #45
0
def exercise(space_group_info, anomalous_flag=False, d_min=2., verbose=0):
  sg_fcalc = random_structure.xray_structure(
    space_group_info,
    elements=("N", "C", "C", "O"),
    random_f_double_prime=anomalous_flag,
    random_u_iso=True,
    random_occupancy=True
    ).structure_factors(
      anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct").f_calc()
  sg_hl = generate_random_hl(sg_fcalc)
  write_cns_input(sg_fcalc, sg_hl.data())
  try: os.unlink("tmp_sg.hkl")
  except OSError: pass
  try: os.unlink("tmp_p1.hkl")
  except OSError: pass
  easy_run.fully_buffered(command="cns < tmp.cns > tmp.out") \
    .raise_if_errors_or_output()
  sg_cns = read_reflection_arrays("tmp_sg.hkl", anomalous_flag, verbose)
  p1_cns = read_reflection_arrays("tmp_p1.hkl", anomalous_flag, verbose)
  verify(sg_fcalc, sg_hl.data(), sg_cns, p1_cns)
  if (anomalous_flag):
    hl_merged = sg_hl.average_bijvoet_mates()
    fc_merged = sg_fcalc.average_bijvoet_mates()
    write_cns_input(sg_fcalc, sg_hl.data(), test_merge=True)
    try: os.unlink("tmp_merged.hkl")
    except OSError: pass
    easy_run.fully_buffered(command="cns < tmp.cns > tmp.out") \
      .raise_if_errors_or_output()
    reflection_file = reflection_reader.cns_reflection_file(
      open("tmp_merged.hkl"))
    if (not sg_fcalc.space_group().is_centric()):
      fc_merged_cns = reflection_file.reciprocal_space_objects["FCALC"]
      fc_merged_cns = fc_merged.customized_copy(
        indices=fc_merged_cns.indices,
        data=fc_merged_cns.data).map_to_asu().common_set(fc_merged)
      assert fc_merged_cns.indices().all_eq(fc_merged.indices())
      fc_merged_a = fc_merged.select_acentric()
      fc_merged_cns_a = fc_merged_cns.select_acentric()
      for part in [flex.real, flex.imag]:
        cc = flex.linear_correlation(
          part(fc_merged_a.data()),
          part(fc_merged_cns_a.data())).coefficient()
        if (cc < 1-1.e-6):
          print "FAILURE acentrics", sg_fcalc.space_group_info()
          if (0): return
          raise AssertionError
    names, miller_indices, hl = reflection_file.join_hl_group()
    assert names == ["PA", "PB", "PC", "PD"]
    hl_merged_cns = hl_merged.customized_copy(indices=miller_indices, data=hl)\
      .map_to_asu().common_set(hl_merged)
    assert hl_merged_cns.indices().all_eq(hl_merged.indices())
    for h,a,b in zip(hl_merged.indices(),
                     hl_merged.data(),
                     hl_merged_cns.data()):
      if (not approx_equal(a, b, eps=5.e-3)):
        print h
        print "cctbx:", a
        print "  cns:", b
        if (0): return
        raise AssertionError
def run():
    structure = random_structure.xray_structure(
        sgtbx.space_group_info("P21/c"),
        elements=["Si"] * 10,
        volume_per_atom=18.6,
        min_distance=1.2,
        general_positions_only=False)
    miller_set_f_obs = miller.build_set(crystal_symmetry=structure,
                                        anomalous_flag=True,
                                        d_min=0.8)
    f_obs = miller_set_f_obs.structure_factors_from_scatterers(
        xray_structure=structure, algorithm="direct").f_calc()
    fft_map = f_obs.fft_map(symmetry_flags=maptbx.use_space_group_symmetry)

    padded = fft_map.real_map()
    unpadded = fft_map.real_map_unpadded()  # copy
    unpadded_1d = unpadded.as_1d()  # 1D view => in-place
    mmm = flex.min_max_mean_double(unpadded_1d)

    for delta in ((mmm.min + mmm.mean) / 2, mmm.mean,
                  (mmm.mean + mmm.max) / 2):
        # in-place charge flipping
        ab_initio.ext.flip_charges_in_place(padded, delta)

        # same but on an unpadded copy using the flex tools
        flipped_selection = unpadded_1d < delta
        flipped = unpadded_1d.select(flipped_selection)
        flipped *= -1
        unpadded_1d.set_selected(flipped_selection, flipped)

        assert approx_equal(padded, unpadded, 1e-15)

    print(format_cpu_times())
Exemple #47
0
def run_call_back(flags, space_group_info, params):
    structure_shake = random_structure.xray_structure(
        space_group_info,
        elements=("N", "C", "O", "S", "Yb"),
        volume_per_atom=200,
        min_distance=2.0,
        general_positions_only=params.general_positions_only,
        random_u_iso=True)
    structure_ideal = structure_shake.deep_copy_scatterers()
    structure_shake.shake_sites_in_place(
        rms_difference=params.shake_sites_rmsd)
    structure_shake.shake_adp(spread=params.shake_adp_spread)
    #
    run_id = ""
    if (params.pickle_root_name is not None):
        run_id += params.pickle_root_name + "_"
    run_id += str(space_group_info).replace(" ", "").replace("/", "_").lower()
    if (params.pickle_root_name is not None):
        pickle_file_name = run_id + "_ideal_shake.pickle"
        print "writing file:", pickle_file_name
        easy_pickle.dump(file_name=pickle_file_name,
                         obj=(structure_ideal, structure_shake))
        print
        sys.stdout.flush()
    #
    ls_result = run_refinement(structure_ideal=structure_ideal,
                               structure_shake=structure_shake,
                               params=params,
                               run_id=run_id)
    if (ls_result is not None and params.pickle_root_name is not None):
        pickle_file_name = run_id + "_ls_history.pickle"
        print "writing file:", pickle_file_name
        easy_pickle.dump(file_name=pickle_file_name, obj=ls_result.history)
        print
        sys.stdout.flush()
def exercise(space_group_info,
             anomalous_flag,
             n_scatterers=8,
             d_min=2,
             verbose=0):
    structure = random_structure.xray_structure(space_group_info,
                                                elements=["const"] *
                                                n_scatterers)
    f_calc = structure.structure_factors(
        d_min=d_min, anomalous_flag=anomalous_flag).f_calc()
    f = abs(f_calc)
    f = miller.array(miller_set=f, data=f.data(), sigmas=flex.sqrt(f.data()))
    f = f.f_as_f_sq()
    g = f.expand_to_p1()
    merger_p1 = xray.merger(g.indices(), g.data(), g.sigmas(), g.space_group(),
                            g.anomalous_flag(), g.unit_cell())
    p1_bic = merger_p1.bic()
    p1_r = merger_p1.r_abs()

    merger_nat = xray.merger(g.indices(), g.data(),
                             g.sigmas(), f.space_group(), g.anomalous_flag(),
                             g.unit_cell())
    nat_bic = merger_nat.bic()
    nat_r = merger_nat.r_abs()
    assert nat_bic >= p1_bic
    assert p1_r <= 1e-8
Exemple #49
0
def randomly_exercise(
    flipping_type,
    space_group_info,
    elements,
    anomalous_flag,
    d_min,
    grid_resolution_factor=1. / 2,
    verbose=False,
    amplitude_type="F",
):

    # Generate a random structure in real space, that we will try to recover
    target_structure = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=elements,
        use_u_iso=True,
        random_u_iso=True,
        random_u_iso_scale=0.04,
        use_u_aniso=False,
    )
    exercise_one_structure(
        target_structure,
        flipping_type,
        anomalous_flag,
        d_min,
        grid_resolution_factor=grid_resolution_factor,
        verbose=verbose,
        amplitude_type=amplitude_type,
    )
def exercise(space_group_info, anomalous_flag,
             d_min=1.0, reflections_per_bin=200, n_bins=10, verbose=0):
  elements = ("N", "C", "C", "O") * 5
  structure_factors = random_structure.xray_structure(
    space_group_info,
    elements=elements,
    volume_per_atom=50.,
    min_distance=1.5,
    general_positions_only=True,
    use_u_aniso=False,
    u_iso=adptbx.b_as_u(10)
    ).structure_factors(
        anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct")
  if (0 or verbose):
    structure_factors.xray_structure().show_summary()
  asu_contents = dicts.with_default_value(0)
  for elem in elements: asu_contents[elem] += 1
  f_calc = abs(structure_factors.f_calc())
  f_calc.setup_binner(
    auto_binning=True,
    reflections_per_bin=reflections_per_bin,
    n_bins=n_bins)
  if (0 or verbose):
    f_calc.binner().show_summary()
  for k_given in [1,0.1,0.01,10,100]:
    f_obs = miller.array(
      miller_set=f_calc,
      data=f_calc.data()*k_given).set_observation_type_xray_amplitude()
    f_obs.use_binner_of(f_calc)
    wp = statistics.wilson_plot(f_obs, asu_contents, e_statistics=True)
    if (0 or verbose):
      print "wilson_k, wilson_b:", wp.wilson_k, wp.wilson_b
      print "space group:", space_group_info.group().type().hall_symbol()
      print "<E^2-1>:", wp.mean_e_sq_minus_1

    assert 0.8 < wp.wilson_k/k_given < 1.2
    assert 0.64 < wp.wilson_intensity_scale_factor/(k_given*k_given) < 1.44
    assert 9 < wp.wilson_b < 11
    assert wp.xy_plot_info().fit_correlation == wp.fit_correlation
    if space_group_info.group().is_centric():
      assert 0.90 < wp.mean_e_sq_minus_1 < 1.16
      assert 3.15 < wp.percent_e_sq_gt_2 < 6.5
    else:
      assert 0.65 < wp.mean_e_sq_minus_1 < 0.90
      assert 1.0 < wp.percent_e_sq_gt_2 < 3.15
    assert wp.normalised_f_obs.size() == f_obs.size()
  f_obs = f_calc.array(data=flex.double(f_calc.indices().size(), 0))
  f_obs.use_binner_of(f_calc)
  n_bins = f_obs.binner().n_bins_used()
  try:
    statistics.wilson_plot(f_obs, asu_contents)
  except RuntimeError, e:
    assert not show_diff(str(e), """\
wilson_plot error: %d empty bins:
  Number of bins: %d
  Number of f_obs > 0: 0
  Number of f_obs <= 0: %d""" % (n_bins, n_bins, f_obs.indices().size()))
Exemple #51
0
def exercise_3_f_part1_and_f_part2():
  x = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info("P 4"),
    elements               =(("O","N","C")*10),
    volume_per_atom        = 200,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = False)
  x.scattering_type_registry(table="wk1995")
  fc = x.structure_factors(d_min = 2.0, algorithm="direct").f_calc()
  #
  x1 = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info("P 4"),
    unit_cell              = x.unit_cell(),
    elements               =(("C")*10),
    volume_per_atom        = 200,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = False)
  x1.scattering_type_registry(table="wk1995")
  fc1 = x1.structure_factors(d_min = 2.0, algorithm="direct").f_calc()
  #
  x2 = random_structure.xray_structure(
    space_group_info       = sgtbx.space_group_info("P 4"),
    unit_cell              = x.unit_cell(),
    elements               =(("C")*10),
    volume_per_atom        = 200,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = False)
  x2.scattering_type_registry(table="wk1995")
  fc2 = x2.structure_factors(d_min = 2.0, algorithm="direct").f_calc()
  #
  fc_all = fc.customized_copy(data = fc.data()+fc1.data()+fc2.data())
  f_obs = abs(fc_all.deep_copy())
  r_free_flags = f_obs.generate_r_free_flags(fraction = 0.1)
  sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract()
  sfg_params.algorithm = "direct"
  #
  t_exercise_3_1(x=x, f_obs=f_obs, fc1=fc1, fc2=fc2, r_free_flags=r_free_flags,
    sfg_params=sfg_params)
def run_00():
  time_aniso_u_scaler = 0
  for symbol in sgtbx.bravais_types.acentric + sgtbx.bravais_types.centric:
    #print symbol, "-"*50
    space_group_info = sgtbx.space_group_info(symbol = symbol)
    xrs = random_structure.xray_structure(
      space_group_info  = space_group_info,
      elements          = ["N"]*100,
      volume_per_atom   = 50.0,
      random_u_iso      = True)
    # XXX ad a method to adptbx to do this
    point_group = sgtbx.space_group_info(
      symbol=symbol).group().build_derived_point_group()
    adp_constraints = sgtbx.tensor_rank_2_constraints(
      space_group=point_group,
      reciprocal_space=True)
    u_star = adptbx.u_cart_as_u_star(xrs.unit_cell(),
      adptbx.random_u_cart(u_scale=1,u_min=0.1))
    u_indep = adp_constraints.independent_params(all_params=u_star)
    u_star = adp_constraints.all_params(independent_params=u_indep)
    b_cart_start=adptbx.u_as_b(adptbx.u_star_as_u_cart(xrs.unit_cell(), u_star))
    #
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    b_cart_start = [b_cart_start[0]-tr,b_cart_start[1]-tr,b_cart_start[2]-tr,
           b_cart_start[3],b_cart_start[4],b_cart_start[5]]
    tr = (b_cart_start[0]+b_cart_start[1]+b_cart_start[2])/3
    #
    #print "Input b_cart :", " ".join(["%8.4f"%i for i in b_cart_start]), "tr:", tr
    F = xrs.structure_factors(d_min = 2.0).f_calc()
    u_star = adptbx.u_cart_as_u_star(
      F.unit_cell(), adptbx.b_as_u(b_cart_start))
    fbc = mmtbx.f_model.ext.k_anisotropic(F.indices(), u_star)
    fc = F.structure_factors_from_scatterers(xray_structure=xrs).f_calc()
    f_obs = F.customized_copy(data = flex.abs(fc.data()*fbc))
    t0 = time.time()
    #
    obj = bulk_solvent.aniso_u_scaler(
      f_model_abs    = flex.abs(fc.data()),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices(),
      adp_constraint_matrix = adp_constraints.gradient_sum_matrix())
    time_aniso_u_scaler += (time.time()-t0)
    b_cart_final = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(),
      adp_constraints.all_params(tuple(obj.u_star_independent))))
    #
    obj = bulk_solvent.aniso_u_scaler(
      f_model_abs    = flex.abs(fc.data()),
      f_obs          = f_obs.data(),
      miller_indices = f_obs.indices())
    b_cart_final2 = adptbx.u_as_b(adptbx.u_star_as_u_cart(f_obs.unit_cell(),
      tuple(obj.u_star)))
    #
    assert approx_equal(b_cart_final, b_cart_final2)
    #print "Output b_cart:", " ".join(["%8.4f"%i for i in b_cart_final])
    assert approx_equal(b_cart_start, b_cart_final, 1.e-4)
  print "Time (aniso_u_scaler only): %6.4f"%time_aniso_u_scaler
Exemple #53
0
def exercise_SFweight_spline(
      space_group_info,
      n_scatterers=10,
      d_min=4,
      verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=["C"]*n_scatterers,
    volume_per_atom=1000)
  sfweight = exercise_SFweight_spline_core(
    structure=structure, d_min=d_min, verbose=verbose)
def run_group(symbol):
  group = space_group_info(symbol);
  print "\n=="
  elements = ('C', 'N', 'O', 'H')*11
  xrs = random_structure.xray_structure(
    space_group_info = group,
    volume_per_atom = 25.,
    general_positions_only = False,
    elements = elements,
    min_distance = 1.0)
  fo = abs(xrs.structure_factors(d_min=2).f_calc())
  fmodel = mmtbx.f_model.manager(
    f_obs = fo,
    xray_structure = xrs)
  #
  k_sol=flex.double([10.35,5.34])
  b_sol=flex.double([30.0, 24.0])
  b_cart = [10,20,30,40,50,60]
  u_star = flex.double(adptbx.b_as_u(
    adptbx.u_cart_as_u_star(xrs.unit_cell(), b_cart)))
  #
  TGO = cpp_tg(fmodel=fmodel)
  tg = TGO.get_tg(k_sol=k_sol, b_sol=b_sol, u_star=u_star)
  # k_sol
  gk_a=list(tg.grad_k_sols())
  ck_a=list(tg.curv_k_sols())
  gk_fd, ck_fd=fd(TGO=TGO, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="k_sol")
  # b_sol
  gb_a=list(tg.grad_b_sols())
  cb_a=list(tg.curv_b_sols())
  gb_fd, cb_fd=fd(TGO=TGO, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="b_sol")
  # u_star
  gu_a=list(tg.grad_u_star())
  gu_fd, junk=fd(TGO=TGO, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="u_star")
  print "u_star:",gu_a
  print "u_star:",gu_fd
  TGO2 = cpp_tg_u_star_only(fmodel=fmodel)
  tg2 = TGO2.get_tg(k_sol=k_sol, b_sol=b_sol, u_star=u_star)
  gu_a2=list(tg2.grad_u_star())
  gu_fd2, junk=fd(TGO=TGO2, k_sol=k_sol, b_sol=b_sol, u_star=u_star, param="u_star")
  print "u_star:",gu_a2
  print "u_star:",gu_fd2
  #
  print "k_sol:", gk_a,  ck_a
  print "k_sol:", gk_fd, ck_fd
  print "b_sol:", gb_a,  cb_a
  print "b_sol:", gb_fd, cb_fd
  #
  assert approx_equal(gk_a, gk_fd, eps=1.e-4)
  assert approx_equal(gb_a, gb_fd, eps=1.e-4)
  assert approx_equal(ck_a, ck_fd, eps=1.e-4)
  assert approx_equal(cb_a, cb_fd, eps=1.e-4)
  assert approx_equal(gu_a, gu_fd, eps=1.e-4)
  assert approx_equal(gu_a2, gu_fd2, eps=1.e-6)
def exercise_all(flags, space_group_info):
  exercise_float_asu(space_group_info)
  exercise_asu_mappings(space_group_info)
  exercise_neighbors_pair_generators(
    structure = random_structure.xray_structure(
      space_group_info,
      elements=["Si"]*5,
      volume_per_atom=100,
      min_distance=3.,
      general_positions_only=False),
    verbose=flags.Verbose)
Exemple #56
0
def exercise_4(grid_step,
               radius,
               shell,
               a,
               b,
               d_min,
               volume_per_atom,
               use_weights,
               optimize_cutoff_radius,
               scatterer_chemical_type = "C"):
  xray_structure = random_structure.xray_structure(
                        space_group_info       = sgtbx.space_group_info("P 1"),
                        elements               = ((scatterer_chemical_type)*1),
                        volume_per_atom        = volume_per_atom,
                        min_distance           = 1.5,
                        general_positions_only = True)
  custom_dict = \
             {scatterer_chemical_type: eltbx.xray_scattering.gaussian([a],[b])}
  xray_structure.scattering_type_registry(custom_dict = custom_dict)
  miller_set = miller.build_set(
                          crystal_symmetry = xray_structure.crystal_symmetry(),
                          anomalous_flag   = False,
                          d_min            = d_min,
                          d_max            = None)
  f_calc = miller_set.structure_factors_from_scatterers(
                                 xray_structure               = xray_structure,
                                 algorithm                    = "direct",
                                 cos_sin_table                = False,
                                 exp_table_one_over_step_size = False).f_calc()
  fft_map = f_calc.fft_map(grid_step = grid_step, symmetry_flags = None)
  fft_map = fft_map.apply_volume_scaling()
  site_frac = xray_structure.sites_frac()
  m = fft_map.real_map_unpadded()
  amm = abs(flex.max(m))
  r = abs(amm-abs(m.value_at_closest_grid_point(site_frac[0]))) / amm
  assert r < 0.15, r
  around_atom_obj_ = mmtbx.real_space.around_atom(
                            unit_cell = xray_structure.unit_cell(),
                            map_data  = fft_map.real_map_unpadded(),
                            radius    = radius,
                            shell     = shell,
                            site_frac = list(xray_structure.sites_frac())[0])
  data = around_atom_obj_.data()
  dist = around_atom_obj_.distances()
  approx_obj_ = maptbx.one_gaussian_peak_approximation(
                               data_at_grid_points    = data,
                               distances              = dist,
                               use_weights            = use_weights,
                               optimize_cutoff_radius = optimize_cutoff_radius)
  assert approx_equal(approx_obj_.a_reciprocal_space(), 6.0, 0.1)
  assert approx_equal(approx_obj_.b_reciprocal_space(), 3.0, 0.1)
  assert approx_obj_.gof() < 1.0
  assert approx_obj_.cutoff_radius() < radius
Exemple #57
0
def exercise(flags, space_group_info):
    # Prepare a structure compatible with the ShelX model
    xs = random_structure.xray_structure(
        space_group_info, elements="random", n_scatterers=10, use_u_iso=True, random_u_iso=True, use_u_aniso=True
    )
    xs.apply_symmetry_sites()
    xs.apply_symmetry_u_stars()
    for isotropic, sc in itertools.izip(variate(bernoulli_distribution(0.4)), xs.scatterers()):
        sc.flags.set_grad_site(True)
        if isotropic:
            sc.flags.set_use_u_iso(True)
            sc.flags.set_use_u_aniso(False)
            sc.flags.set_grad_u_iso(True)
        else:
            sc.flags.set_use_u_iso(False)
            sc.flags.set_use_u_aniso(True)
            sc.flags.set_grad_u_aniso(True)

    not_origin_centric = xs.space_group().is_centric() and not xs.space_group().is_origin_centric()

    try:
        ins = list(
            shelx.writer.generator(
                xs, full_matrix_least_squares_cycles=4, weighting_scheme_params=(0, 0), sort_scatterers=False
            )
        )
    except AssertionError:
        if not_origin_centric:
            print("Omitted %s\n  because it is centric but not origin centric" % xs.space_group().type().hall_symbol())
            return
        raise
    else:
        if not_origin_centric:
            raise Exception_expected

    ins = cStringIO.StringIO("".join(ins))
    xs1 = xs.from_shelx(file=ins)
    xs.crystal_symmetry().is_similar_symmetry(
        xs1.crystal_symmetry(), relative_length_tolerance=1e-3, absolute_angle_tolerance=1e-3
    )
    uc = xs.unit_cell()
    uc1 = xs1.unit_cell()
    for sc, sc1 in itertools.izip(xs.scatterers(), xs1.scatterers()):
        assert sc.label.upper() == sc1.label.upper()
        assert sc.scattering_type == sc1.scattering_type
        assert sc.flags.bits == sc1.flags.bits
        assert approx_equal(sc.site, sc1.site, eps=1e-6)
        if sc.flags.use_u_iso():
            assert approx_equal(sc.u_iso, sc1.u_iso, eps=1e-5)
        else:
            assert approx_equal(
                adptbx.u_star_as_u_cif(uc, sc.u_star), adptbx.u_star_as_u_cif(uc1, sc1.u_star), eps=1e-5
            )
def set_up_random_structure(space_group_info):
  from cctbx.development import random_structure
  xray_structure = random_structure.xray_structure(
    space_group_info       = space_group_info,
    elements               =("O", "P")*4,
    volume_per_atom        = 100,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = True,
    random_occupancy       = True)
  xray_structure.scattering_type_registry(table="wk1995")
  return xray_structure