示例#1
0
def run(args):
    if "--fix_random_seeds" in args:
        random.seed(1)
        flex.set_random_seed(1)
        scitbx.random.set_random_seed(1)
    libtbx.utils.show_times_at_exit()
    for i in range(10):
        exercise_optimise_shelxl_weights()
    exercise_weighting_schemes()
def run(args):
  if "--fix_random_seeds" in args:
    random.seed(1)
    flex.set_random_seed(1)
    scitbx.random.set_random_seed(1)
  libtbx.utils.show_times_at_exit()
  for i in range(10):
    exercise_optimise_shelxl_weights()
  exercise_weighting_schemes()
示例#3
0
def generate_test_data(
    space_group,
    lattice_group=None,
    unit_cell=None,
    unit_cell_volume=1000,
    seed=0,
    d_min=1,
    sigma=0.1,
    sample_size=100,
    map_to_p1=False,
    twin_fractions=None,
    map_to_minimum=True,
):

    import random

    if seed is not None:
        flex.set_random_seed(seed)
        random.seed(seed)

    assert [unit_cell, lattice_group].count(None) > 0

    sgi = space_group.info()

    if unit_cell is not None:
        cs = crystal.symmetry(unit_cell=unit_cell, space_group_info=sgi)
    elif lattice_group is not None:
        subgrps = subgroups(lattice_group).groups_parent_setting()
        assert space_group in subgrps
        cs = lattice_group.any_compatible_crystal_symmetry(
            volume=unit_cell_volume).customized_copy(space_group_info=sgi)
    else:
        cs = sgi.any_compatible_crystal_symmetry(volume=unit_cell_volume)

    if map_to_minimum:
        cs = cs.minimum_cell()
    intensities = generate_intensities(cs, d_min=d_min)
    intensities.show_summary()

    twin_ops = generate_twin_operators(intensities)
    twin_ops = [
        sgtbx.change_of_basis_op(op.operator.as_xyz()) for op in twin_ops
    ]

    if twin_fractions is not None:
        assert len(twin_fractions) == len(twin_ops)
        assert len(
            twin_fractions) == 1, "Only 1 twin component currently supported"
        twin_op = twin_ops[0]
        twin_fraction = twin_fractions[0]
        intensities, intensities_twin = intensities.common_sets(
            intensities.change_basis(twin_op).map_to_asu())
        twinned_miller = intensities.customized_copy(
            data=(1.0 - twin_fraction) * intensities.data() +
            twin_fraction * intensities_twin.data(),
            sigmas=flex.sqrt(
                flex.pow2((1.0 - twin_fraction) * intensities.sigmas()) +
                flex.pow2(twin_fraction * intensities_twin.sigmas())),
        )
        intensities = twinned_miller

    cb_ops = twin_ops
    cb_ops.insert(0, sgtbx.change_of_basis_op())

    reindexing_ops = {}

    datasets = []
    rand_norm = scitbx.random.normal_distribution(mean=0, sigma=sigma)
    g = scitbx.random.variate(rand_norm)
    for i in range(sample_size):
        cb_op = random.choice(cb_ops)
        if cb_op.as_xyz() not in reindexing_ops:
            reindexing_ops[cb_op.as_xyz()] = set()
        reindexing_ops[cb_op.as_xyz()].add(i)
        d = intensities.change_basis(cb_op).customized_copy(
            crystal_symmetry=intensities.crystal_symmetry())

        if map_to_p1:
            cb_op_to_primitive = d.change_of_basis_op_to_primitive_setting()
            d = d.change_basis(cb_op_to_primitive)
            d = d.expand_to_p1()

        d = d.customized_copy(data=d.data() + g(d.size()))
        datasets.append(d)

    return datasets, reindexing_ops
示例#4
0
def exercise(space_group_info,
             fixed_random_seed=True,
             shifted_origin=None,
             elements=None,
             d_min=0.8,
             grid_resolution_factor=1/3.,
             verbose=False,
             **kwds):
  if elements is None:
    n_C = 5
    n_O = 1
    n_N = 1
    elements = ["C"]*n_C + ["O"]*n_O + ["N"]*n_N
  if verbose:
    print elements

  target_space_group_type = space_group_info.type()
  hall = sgtbx.space_group_symbols(
    target_space_group_type.lookup_symbol()).hall()
  print hall

  if fixed_random_seed:
    random.seed(1)
    flex.set_random_seed(1)

  # Generate a random structure in real space,
  # compute its structure factors,
  # that we will try to recover the symmetry of
  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,
  )
  if shifted_origin is None:
    shifted_origin = flex.random_double(3)
  shifted_origin = mat.col(shifted_origin)
  if verbose:
    print "new origin = (%.3f, %.3f, %.3f)" % shifted_origin.elems
    print
  target_structure_in_p1 = target_structure\
                         .expand_to_p1().apply_shift(shifted_origin)
  target_f_in_p1 = miller.build_set(
    crystal_symmetry=target_structure_in_p1,
    anomalous_flag=False,
    d_min=d_min
    ).structure_factors_from_scatterers(
      xray_structure=target_structure_in_p1,
      algorithm="direct").f_calc()

  # Recover space group?
  sf_symm = symmetry_search.structure_factor_symmetry(target_f_in_p1)
  if verbose:
    print sf_symm

  solution_hall, target_hall = [
    sgi.as_reference_setting().type().hall_symbol()
    for sgi in (sf_symm.space_group_info,
                target_structure.space_group_info()) ]
  assert solution_hall == target_hall, (solution_hall, target_hall)

  # Shift maximises goodness of symmetry?
  gos, solution_f = sf_symm.symmetrised_structure_factors()
  if space_group_info.type().hall_symbol() != ' P 1':
    assert gos.correlation > 0.99
    assert gos.gradient == (0, 0, 0)
    gos_away_from_max = sf_symm.symmetrised_structure_factors(
      delta=mat.col((0.1, 0.1, 0.1)))[0]
    assert gos_away_from_max.correlation < 0.9, gos_away_from_max.correlation

  # Recovered origin
  """The sequence of transform read:
  ----->target structure
  ^           V
  ^           V  (1, shifted_origin=sigma)
  ^           V
  ^      shifted target
  ^           V
  ^           V sf_symm.cb_op_to_primitive = (P, 0)
  ^           V
  ^      shifted target in primitive cell = shifted solution in primitive cell
  ^           V
  ^           V (1, -sf_symm.origin=-s)
  ^           V
  ^      solution in primitive cell
  ^           V
  ^           V solution_to_target_cb_op = (Q, q)
  ^           V
  ^------------

  The total transfrom from the target structure back to it reads
  (QP, q') with q' = (Q,q)(-s + P sigma) = (Q,q)delta_o
  with
  delta_o = sf_symm.cb_op_to_primitive(shifted_origin) - sf_symm.origin

  (Q, q') must leave the target structure space group invariant.
  Most of the time Q is the unit matrix and the test boils down to check
  whether delta is an allowed origin shift after changing to the input cell
  but it does not hurt to do the more general test all the time.
  """

  solution_to_target_cb_op = (
    target_structure.space_group_info()
    .change_of_basis_op_to_reference_setting().inverse()
    *
    sf_symm.space_group_info
    .change_of_basis_op_to_reference_setting())

  if verbose:
    print
    print "solution -> target: %s" % solution_to_target_cb_op.as_xyz()
  delta_o = (mat.col(sf_symm.cb_op_to_primitive(shifted_origin))
             - sf_symm.origin)
  delta = mat.col(solution_to_target_cb_op(delta_o))
  stabilising_cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(
    (solution_to_target_cb_op*sf_symm.cb_op_to_primitive).c().r(),
    sgtbx.tr_vec((delta*72).as_int()*2, tr_den=144)))
    # guarding against rounding errors on some platforms (e.g. FC8)
    # meaning that (delta*144).as_int() would not work.
  target_sg = target_structure.space_group()
  assert target_sg == target_sg.change_basis(stabilising_cb_op)
示例#5
0
def run(args):
    master_phil = libtbx.phil.parse("""
    d_min = 0.5
      .type = float
    constrained_refinement = True
      .type = bool
    random_seed = 1
      .type = int
    shake_sites_rmsd = 0.5
      .type = float
    shake_adp_spread = 20
      .type = float
    grad_site=True
      .type = bool
    grad_u_iso=False
      .type = bool
    grad_u_aniso=False
      .type = bool
    grad_occupancy=False
      .type = bool
    grad_fp_fdp=False
      .type = bool
    lbfgs_m = 5
      .type = int
    lbfgs_max_iterations = 1000
      .type = int
    verbose = 0
      .type = int
""")

    argument_interpreter = master_phil.command_line_argument_interpreter()
    phil_objects = []
    remaining_args = []
    for arg in args:
        if (arg.find("=") >= 0):
            phil_objects.append(argument_interpreter.process(arg=arg))
        else:
            remaining_args.append(arg)
    work_phil = master_phil.fetch(sources=phil_objects)
    work_phil.show()
    params = work_phil.extract()

    if params.random_seed is not None:
        import scitbx.random
        import random
        scitbx.random.set_random_seed(params.random_seed)
        flex.set_random_seed(params.random_seed)
        random.seed(params.random_seed)

    if len(remaining_args):
        assert len(remaining_args) == 1
        file_path = remaining_args[0]
        root, ext = os.path.splitext(file_path)

        if ext == ".cif":
            xs_dict = xray.structure.from_cif(file_path=file_path)
            assert len(
                xs_dict) == 1, "CIF should contain only one xray structure"
            xs = xs_dict.values()[0]
            xs.show_summary().show_scatterers()
            print
            constraints_list = None
            t_celsius = 20
        else:
            raise RuntimeError("Only CIF format currently supported!")

    else:
        test_case = tst_constrained_structure.sucrose_test_case(None)
        t_celsius = test_case.t_celsius
        xs = test_case.xray_structure
        constraints_list = test_case.constraints

    #from cctbx import adptbx
    #for sc in xs.scatterers():
    #if sc.flags.use_u_aniso():
    #sc.u_iso = adptbx.u_star_as_u_iso(xs.unit_cell(), sc.u_star)
    #sc.set_use_u_iso_only()

    if not params.constrained_refinement:
        constraints_list = []

    exercise_constrained_lbfgs(
        xray_structure=xs,
        constraints_list=constraints_list,
        t_celsius=t_celsius,
        d_min=params.d_min,
        shake_sites_rmsd=params.shake_sites_rmsd,
        shake_u_iso_spread=params.shake_adp_spread,
        grad_site=params.grad_site,
        grad_u_iso=params.grad_u_iso,
        grad_u_aniso=params.grad_u_aniso,
        grad_occupancy=params.grad_occupancy,
        grad_fp_fdp=params.grad_fp_fdp,
        lbfgs_m=params.lbfgs_m,
        lbfgs_max_iterations=params.lbfgs_max_iterations,
        verbose=params.verbose)
示例#6
0
def exercise(space_group_info,
             fixed_random_seed=True,
             shifted_origin=None,
             elements=None,
             d_min=0.8,
             grid_resolution_factor=1/3.,
             verbose=False,
             **kwds):
  if elements is None:
    n_C = 5
    n_O = 1
    n_N = 1
    elements = ["C"]*n_C + ["O"]*n_O + ["N"]*n_N
  if verbose:
    print(elements)

  target_space_group_type = space_group_info.type()
  hall = sgtbx.space_group_symbols(
    target_space_group_type.lookup_symbol()).hall()
  print(hall)

  if fixed_random_seed:
    random.seed(1)
    flex.set_random_seed(1)

  # Generate a random structure in real space,
  # compute its structure factors,
  # that we will try to recover the symmetry of
  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,
  )
  if shifted_origin is None:
    shifted_origin = flex.random_double(3)
  shifted_origin = mat.col(shifted_origin)
  if verbose:
    print("new origin = (%.3f, %.3f, %.3f)" % shifted_origin.elems)
    print()
  target_structure_in_p1 = target_structure\
                         .expand_to_p1().apply_shift(shifted_origin)
  target_f_in_p1 = miller.build_set(
    crystal_symmetry=target_structure_in_p1,
    anomalous_flag=False,
    d_min=d_min
    ).structure_factors_from_scatterers(
      xray_structure=target_structure_in_p1,
      algorithm="direct").f_calc()

  # Recover space group?
  sf_symm = symmetry_search.structure_factor_symmetry(target_f_in_p1)
  if verbose:
    print(sf_symm)

  solution_hall, target_hall = [
    sgi.as_reference_setting().type().hall_symbol()
    for sgi in (sf_symm.space_group_info,
                target_structure.space_group_info()) ]
  assert solution_hall == target_hall, (solution_hall, target_hall)

  # Shift maximises goodness of symmetry?
  gos, solution_f = sf_symm.symmetrised_structure_factors()
  if space_group_info.type().hall_symbol() != ' P 1':
    assert gos.correlation > 0.99
    assert gos.gradient == (0, 0, 0)
    gos_away_from_max = sf_symm.symmetrised_structure_factors(
      delta=mat.col((0.1, 0.1, 0.1)))[0]
    assert gos_away_from_max.correlation < 0.9, gos_away_from_max.correlation

  # Recovered origin
  """The sequence of transform read:
  ----->target structure
  ^           V
  ^           V  (1, shifted_origin=sigma)
  ^           V
  ^      shifted target
  ^           V
  ^           V sf_symm.cb_op_to_primitive = (P, 0)
  ^           V
  ^      shifted target in primitive cell = shifted solution in primitive cell
  ^           V
  ^           V (1, -sf_symm.origin=-s)
  ^           V
  ^      solution in primitive cell
  ^           V
  ^           V solution_to_target_cb_op = (Q, q)
  ^           V
  ^------------

  The total transfrom from the target structure back to it reads
  (QP, q') with q' = (Q,q)(-s + P sigma) = (Q,q)delta_o
  with
  delta_o = sf_symm.cb_op_to_primitive(shifted_origin) - sf_symm.origin

  (Q, q') must leave the target structure space group invariant.
  Most of the time Q is the unit matrix and the test boils down to check
  whether delta is an allowed origin shift after changing to the input cell
  but it does not hurt to do the more general test all the time.
  """

  solution_to_target_cb_op = (
    target_structure.space_group_info()
    .change_of_basis_op_to_reference_setting().inverse()
    *
    sf_symm.space_group_info
    .change_of_basis_op_to_reference_setting())

  if verbose:
    print()
    print("solution -> target: %s" % solution_to_target_cb_op.as_xyz())
  delta_o = (mat.col(sf_symm.cb_op_to_primitive(shifted_origin))
             - sf_symm.origin)
  delta = mat.col(solution_to_target_cb_op(delta_o))
  stabilising_cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(
    (solution_to_target_cb_op*sf_symm.cb_op_to_primitive).c().r(),
    sgtbx.tr_vec((delta*72).as_int()*2, tr_den=144)))
    # guarding against rounding errors on some platforms (e.g. FC8)
    # meaning that (delta*144).as_int() would not work.
  target_sg = target_structure.space_group()
  assert target_sg == target_sg.change_basis(stabilising_cb_op)
示例#7
0
def run(args):
  master_phil = libtbx.phil.parse("""
    d_min = 0.5
      .type = float
    constrained_refinement = True
      .type = bool
    random_seed = 1
      .type = int
    shake_sites_rmsd = 0.5
      .type = float
    shake_adp_spread = 20
      .type = float
    grad_site=True
      .type = bool
    grad_u_iso=False
      .type = bool
    grad_u_aniso=False
      .type = bool
    grad_occupancy=False
      .type = bool
    grad_fp_fdp=False
      .type = bool
    lbfgs_m = 5
      .type = int
    lbfgs_max_iterations = 1000
      .type = int
    verbose = 0
      .type = int
""")

  argument_interpreter = master_phil.command_line_argument_interpreter()
  phil_objects = []
  remaining_args = []
  for arg in args:
    if (arg.find("=") >= 0):
      phil_objects.append(argument_interpreter.process(arg=arg))
    else:
      remaining_args.append(arg)
  work_phil = master_phil.fetch(sources=phil_objects)
  work_phil.show()
  params = work_phil.extract()

  if params.random_seed is not None:
    import scitbx.random
    import random
    scitbx.random.set_random_seed(params.random_seed)
    flex.set_random_seed(params.random_seed)
    random.seed(params.random_seed)

  if len(remaining_args):
    assert len(remaining_args) == 1
    file_path = remaining_args[0]
    root, ext = os.path.splitext(file_path)

    if ext == ".cif":
      xs_dict = xray.structure.from_cif(file_path=file_path)
      assert len(xs_dict) == 1, "CIF should contain only one xray structure"
      xs = xs_dict.values()[0]
      xs.show_summary().show_scatterers()
      print
      constraints_list = None
      t_celsius = 20
    else:
      raise RuntimeError("Only CIF format currently supported!")

  else:
    test_case = tst_constrained_structure.sucrose_test_case(None)
    t_celsius = test_case.t_celsius
    xs = test_case.xray_structure
    constraints_list = test_case.constraints

  #from cctbx import adptbx
  #for sc in xs.scatterers():
    #if sc.flags.use_u_aniso():
      #sc.u_iso = adptbx.u_star_as_u_iso(xs.unit_cell(), sc.u_star)
      #sc.set_use_u_iso_only()

  if not params.constrained_refinement:
    constraints_list = []

  exercise_constrained_lbfgs(xray_structure=xs,
                             constraints_list=constraints_list,
                             t_celsius=t_celsius,
                             d_min=params.d_min,
                             shake_sites_rmsd=params.shake_sites_rmsd,
                             shake_u_iso_spread=params.shake_adp_spread,
                             grad_site=params.grad_site,
                             grad_u_iso=params.grad_u_iso,
                             grad_u_aniso=params.grad_u_aniso,
                             grad_occupancy=params.grad_occupancy,
                             grad_fp_fdp=params.grad_fp_fdp,
                             lbfgs_m=params.lbfgs_m,
                             lbfgs_max_iterations=params.lbfgs_max_iterations,
                             verbose=params.verbose)