Example #1
0
def exercise_recycle(space_group_info,
                     anomalous_flag,
                     n_scatterers=8,
                     d_min=2.5,
                     verbose=0):
    f_calc = random_f_calc(space_group_info=space_group_info,
                           n_scatterers=n_scatterers,
                           d_min=d_min,
                           anomalous_flag=anomalous_flag,
                           verbose=verbose)
    if (f_calc is None): return
    recycle(f_calc, "f_calc", verbose=verbose)
    for column_root_label, column_types in [("f_obs", None), ("Ework", "E")]:
        if (anomalous_flag and column_types == "E"): continue
        recycle(miller_array=abs(f_calc),
                column_root_label=column_root_label,
                column_types=column_types,
                verbose=verbose)
    if (not anomalous_flag):
        recycle(abs(f_calc), "f_obs", column_types="R", verbose=verbose)
    for column_root_label, column_types in [("f_obs", None), ("Ework", "EQ")]:
        if (anomalous_flag and column_types == "EQ"): continue
        recycle(miller_array=miller.array(miller_set=f_calc,
                                          data=flex.abs(f_calc.data()),
                                          sigmas=flex.abs(f_calc.data()) / 10),
                column_root_label=column_root_label,
                column_types=column_types,
                verbose=verbose)
    recycle(f_calc.centric_flags(), "cent", verbose=verbose)
    recycle(generate_random_hl(miller_set=f_calc), "prob", verbose=verbose)
Example #2
0
def exercise_recycle(space_group_info, anomalous_flag, n_scatterers=8, d_min=2.5, verbose=0):
    f_calc = random_f_calc(
        space_group_info=space_group_info,
        n_scatterers=n_scatterers,
        d_min=d_min,
        anomalous_flag=anomalous_flag,
        verbose=verbose,
    )
    if f_calc is None:
        return
    recycle(f_calc, "f_calc", verbose=verbose)
    for column_root_label, column_types in [("f_obs", None), ("Ework", "E")]:
        if anomalous_flag and column_types == "E":
            continue
        recycle(
            miller_array=abs(f_calc), column_root_label=column_root_label, column_types=column_types, verbose=verbose
        )
    if not anomalous_flag:
        recycle(abs(f_calc), "f_obs", column_types="R", verbose=verbose)
    for column_root_label, column_types in [("f_obs", None), ("Ework", "EQ")]:
        if anomalous_flag and column_types == "EQ":
            continue
        recycle(
            miller_array=miller.array(
                miller_set=f_calc, data=flex.abs(f_calc.data()), sigmas=flex.abs(f_calc.data()) / 10
            ),
            column_root_label=column_root_label,
            column_types=column_types,
            verbose=verbose,
        )
    recycle(f_calc.centric_flags(), "cent", verbose=verbose)
    recycle(generate_random_hl(miller_set=f_calc), "prob", verbose=verbose)
Example #3
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
Example #4
0
def exercise(space_group_info,
             anomalous_flag,
             use_u_aniso,
             n_elements=3,
             d_min=3.,
             verbose=0):
    structure_z = random_structure.xray_structure(
        space_group_info,
        elements=("Se", ) * n_elements,
        volume_per_atom=200,
        random_f_prime_d_min=1.0,
        random_f_double_prime=anomalous_flag,
        random_u_iso=True,
        use_u_aniso=use_u_aniso,
        random_occupancy=True)
    check_weight_without_occupancy(structure_z)
    f_z = structure_z.structure_factors(anomalous_flag=anomalous_flag,
                                        d_min=d_min,
                                        algorithm="direct").f_calc()
    f_abs_z = abs(f_z)
    f_rad_z = f_z.phases()
    f_deg_z = f_z.phases(deg=True)
    hl_z = generate_random_hl(miller_set=f_z)
    hl_z_rad = hl_z.phase_integrals()
    if (0 or verbose):
        structure_z.show_summary().show_scatterers()
        print("n_special_positions:", \
              structure_z.special_position_indices().size())
    z2p_op = structure_z.space_group().z2p_op()
    z2p_op = sgtbx.change_of_basis_op(z2p_op.c() + sgtbx.tr_vec(
        (2, -1, 3), 12).new_denominator(z2p_op.c().t().den()))
    for change_hand in [False, True]:
        if (change_hand):
            z2p_op = z2p_op * sgtbx.change_of_basis_op("-x,-y,-z")
        structure_p = structure_z.change_basis(z2p_op)
        check_weight_without_occupancy(structure_p)
        check_site_symmetry_table(structure_z, z2p_op, structure_p)
        if (0 or verbose):
            structure_p.show_summary().show_scatterers()
            print("n_special_positions:", \
                  structure_p.special_position_indices().size())
        assert tuple(structure_p.special_position_indices()) \
            == tuple(structure_z.special_position_indices())
        structure_pz = structure_p.change_basis(z2p_op.inverse())
        check_weight_without_occupancy(structure_pz)
        check_site_symmetry_table(structure_p, z2p_op.inverse(), structure_pz)
        assert structure_pz.unit_cell().is_similar_to(structure_z.unit_cell())
        assert structure_pz.space_group() == structure_z.space_group()
        f_pz = f_z.structure_factors_from_scatterers(
            xray_structure=structure_pz, algorithm="direct").f_calc()
        f_abs_pz = abs(f_pz)
        f_rad_pz = f_pz.phases()
        f_deg_pz = f_pz.phases(deg=True)
        c = flex.linear_correlation(f_abs_z.data(), f_abs_pz.data())
        assert c.is_well_defined()
        if (0 or verbose):
            print("correlation:", c.coefficient())
        assert c.coefficient() > 0.999
        f_p_cb = f_z.change_basis(z2p_op)
        f_abs_p_cb = f_abs_z.change_basis(z2p_op)
        f_rad_p_cb = f_rad_z.change_basis(z2p_op, deg=False)
        f_deg_p_cb = f_deg_z.change_basis(z2p_op, deg=True)
        hl_p_cb = hl_z.change_basis(z2p_op)
        hl_p_cb_rad = hl_p_cb.phase_integrals()
        assert approx_equal(
            hl_z_rad.change_basis(z2p_op).data(), hl_p_cb_rad.data())
        assert f_abs_p_cb.indices().all_eq(f_p_cb.indices())
        if (not change_hand):
            o = flex.order(f_abs_p_cb.indices(), f_abs_z.indices())
        else:
            o = flex.order(-f_abs_p_cb.indices(), f_abs_z.indices())
        if (f_abs_z.space_group().n_ltr() == 1):
            assert o == 0
        else:
            assert o != 0
        f_pz = f_p_cb.change_basis(z2p_op.inverse())
        f_abs_pz = f_abs_p_cb.change_basis(z2p_op.inverse())
        f_rad_pz = f_rad_p_cb.change_basis(z2p_op.inverse(), deg=False)
        f_deg_pz = f_deg_p_cb.change_basis(z2p_op.inverse(), deg=True)
        hl_pz = hl_p_cb.change_basis(z2p_op.inverse())
        hl_pz_rad = hl_pz.phase_integrals()
        assert approx_equal(hl_z_rad.data(), hl_pz_rad.data())
        for i, o in zip(hl_z.data(), hl_pz.data()):
            assert approx_equal(i, o)
        assert approx_equal(flex.max(flex.abs(f_pz.data() - f_z.data())), 0)
        assert flex.order(f_abs_pz.indices(), f_abs_z.indices()) == 0
        assert f_abs_pz.indices().all_eq(f_pz.indices())
        assert approx_equal(
            flex.max(
                scitbx.math.phase_error(phi1=f_rad_pz.data(),
                                        phi2=f_rad_z.data())), 0)
        assert approx_equal(
            flex.max(
                scitbx.math.phase_error(phi1=f_deg_pz.data(),
                                        phi2=f_deg_z.data(),
                                        deg=True)), 0)
        assert approx_equal(f_deg_pz.data(), f_rad_pz.data() * (180 / math.pi))
        f_p_sf = f_p_cb.structure_factors_from_scatterers(
            xray_structure=structure_p, algorithm="direct").f_calc()
        det = abs(z2p_op.c().r().determinant())
        assert approx_equal(
            flex.max(flex.abs(f_p_sf.data() * complex(det) - f_p_cb.data())),
            0)
        f_abs_p_sf = abs(f_p_sf)
        f_rad_p_sf = f_p_sf.phases()
        f_deg_p_sf = f_p_sf.phases(deg=True)
        assert approx_equal(
            flex.max(
                scitbx.math.phase_error(phi1=f_rad_p_sf.data(),
                                        phi2=f_rad_p_cb.data())), 0)
        assert approx_equal(
            flex.max(
                scitbx.math.phase_error(phi1=f_deg_p_sf.data(),
                                        phi2=f_deg_p_cb.data(),
                                        deg=True)), 0)
        c = flex.linear_correlation(f_abs_p_sf.data(), f_abs_p_cb.data())
        assert c.is_well_defined()
        if (0 or verbose):
            print("correlation:", c.coefficient())
        assert c.coefficient() > 0.999
Example #5
0
def exercise(space_group_info,
             n_elements=10,
             table="wk1995",
             d_min=2.0,
             k_sol=0.35,
             b_sol=45.0,
             b_cart=None,
             quick=False,
             verbose=0):
    xray_structure = random_structure.xray_structure(
        space_group_info=space_group_info,
        elements=(("O", "N", "C") * (n_elements // 3 + 1))[:n_elements],
        volume_per_atom=100,
        min_distance=1.5,
        general_positions_only=True,
        random_u_iso=False,
        random_occupancy=False)
    xray_structure.scattering_type_registry(table=table)
    sg = xray_structure.space_group()
    uc = xray_structure.unit_cell()
    u_cart_1 = adptbx.random_u_cart(u_scale=5, u_min=5)
    u_star_1 = adptbx.u_cart_as_u_star(uc, u_cart_1)
    b_cart = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star=u_star_1))
    for anomalous_flag in [False, True]:
        scatterers = xray_structure.scatterers()
        if (anomalous_flag):
            assert scatterers.size() >= 7
            for i in [1, 7]:
                scatterers[i].fp = -0.2
                scatterers[i].fdp = 5
            have_non_zero_fdp = True
        else:
            for i in [1, 7]:
                scatterers[i].fp = 0
                scatterers[i].fdp = 0
            have_non_zero_fdp = False
        f_obs = abs(
            xray_structure.structure_factors(
                d_min=d_min,
                anomalous_flag=anomalous_flag,
                cos_sin_table=sfg_params.cos_sin_table,
                algorithm=sfg_params.algorithm).f_calc())
        f_obs_comp = f_obs.structure_factors_from_scatterers(
            xray_structure=xray_structure,
            algorithm=sfg_params.algorithm,
            cos_sin_table=sfg_params.cos_sin_table).f_calc()
        f_obs = abs(f_obs_comp)
        flags = f_obs.generate_r_free_flags(fraction=0.1, max_free=99999999)
        #flags = flags.array(data = flex.bool(f_obs.data().size(), False))
        xrs = xray_structure.deep_copy_scatterers()
        xrs.shake_sites_in_place(rms_difference=0.3)
        for target in mmtbx.refinement.targets.target_names:
            if target == "mli": continue
            if (quick):
                if (target not in ["ls_wunit_k1", "ml", "mlhl", "ml_sad"]):
                    continue
            if (target == "mlhl"):
                if (have_non_zero_fdp): continue  # XXX gradients not correct!
                experimental_phases = generate_random_hl(miller_set=f_obs)
            else:
                experimental_phases = None
            if (target == "ml_sad"
                    and (not anomalous_flag
                         or mmtbx.refinement.targets.phaser is None)):
                continue
            print("  ", target)
            xray.set_scatterer_grad_flags(scatterers=xrs.scatterers(),
                                          site=True)
            ss = 1. / flex.pow2(f_obs.d_spacings().data()) / 4.
            u_star = adptbx.u_cart_as_u_star(f_obs.unit_cell(),
                                             adptbx.b_as_u(b_cart))
            k_anisotropic = mmtbx.f_model.ext.k_anisotropic(
                f_obs.indices(), u_star)
            k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol)
            fmodel = mmtbx.f_model.manager(
                xray_structure=xrs,
                f_obs=f_obs,
                r_free_flags=flags,
                target_name=target,
                abcd=experimental_phases,
                sf_and_grads_accuracy_params=sfg_params,
                k_mask=k_mask,
                k_anisotropic=k_anisotropic,
                mask_params=masks.mask_master_params.extract())
            fmodel.update_xray_structure(xray_structure=xrs,
                                         update_f_calc=True,
                                         update_f_mask=True)
            xray.set_scatterer_grad_flags(
                scatterers=fmodel.xray_structure.scatterers(), site=True)
            fmodel.update_xray_structure(update_f_calc=True)
            t_f = fmodel.target_functor()
            t_f.prepare_for_minimization()
            gs = t_f(compute_gradients=True).d_target_d_site_cart().as_double()
            gfd = finite_differences_site(target_functor=t_f)
            cc = flex.linear_correlation(gs, gfd).coefficient()
            if (0 or verbose):
                print("ana:", list(gs))
                print("fin:", list(gfd))
                print("rat:", [f / a for a, f in zip(gs, gfd)])
                print(target, "corr:", cc, space_group_info)
                print()
            diff = gs - gfd
            diff /= max(1, flex.max(flex.abs(gfd)))
            tolerance = 1.2e-5
            assert approx_equal(abs(flex.min(diff)), 0.0, tolerance)
            assert approx_equal(abs(flex.mean(diff)), 0.0, tolerance)
            assert approx_equal(abs(flex.max(diff)), 0.0, tolerance)
            assert approx_equal(cc, 1.0, tolerance)
            fmodel.model_error_ml()
def exercise(
      space_group_info,
      anomalous_flag,
      use_u_aniso,
      n_elements=3,
      d_min=3.,
      verbose=0):
  structure_z = random_structure.xray_structure(
    space_group_info,
    elements=("Se",)*n_elements,
    volume_per_atom=200,
    random_f_prime_d_min=1.0,
    random_f_double_prime=anomalous_flag,
    random_u_iso=True,
    use_u_aniso=use_u_aniso,
    random_occupancy=True)
  check_weight_without_occupancy(structure_z)
  f_z = structure_z.structure_factors(
    anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct").f_calc()
  f_abs_z = abs(f_z)
  f_rad_z = f_z.phases()
  f_deg_z = f_z.phases(deg=True)
  hl_z = generate_random_hl(miller_set=f_z)
  hl_z_rad = hl_z.phase_integrals()
  if (0 or verbose):
    structure_z.show_summary().show_scatterers()
    print "n_special_positions:", \
          structure_z.special_position_indices().size()
  z2p_op = structure_z.space_group().z2p_op()
  z2p_op = sgtbx.change_of_basis_op(
      z2p_op.c()
    + sgtbx.tr_vec((2,-1,3), 12).new_denominator(z2p_op.c().t().den()))
  for change_hand in [False, True]:
    if (change_hand):
      z2p_op = z2p_op * sgtbx.change_of_basis_op("-x,-y,-z")
    structure_p = structure_z.change_basis(z2p_op)
    check_weight_without_occupancy(structure_p)
    check_site_symmetry_table(structure_z, z2p_op, structure_p)
    if (0 or verbose):
      structure_p.show_summary().show_scatterers()
      print "n_special_positions:", \
            structure_p.special_position_indices().size()
    assert tuple(structure_p.special_position_indices()) \
        == tuple(structure_z.special_position_indices())
    structure_pz = structure_p.change_basis(z2p_op.inverse())
    check_weight_without_occupancy(structure_pz)
    check_site_symmetry_table(structure_p, z2p_op.inverse(), structure_pz)
    assert structure_pz.unit_cell().is_similar_to(structure_z.unit_cell())
    assert structure_pz.space_group() == structure_z.space_group()
    f_pz = f_z.structure_factors_from_scatterers(
      xray_structure=structure_pz,
      algorithm="direct").f_calc()
    f_abs_pz = abs(f_pz)
    f_rad_pz = f_pz.phases()
    f_deg_pz = f_pz.phases(deg=True)
    c = flex.linear_correlation(f_abs_z.data(), f_abs_pz.data())
    assert c.is_well_defined()
    if (0 or verbose):
      print "correlation:", c.coefficient()
    assert c.coefficient() > 0.999
    f_p_cb = f_z.change_basis(z2p_op)
    f_abs_p_cb = f_abs_z.change_basis(z2p_op)
    f_rad_p_cb = f_rad_z.change_basis(z2p_op, deg=False)
    f_deg_p_cb = f_deg_z.change_basis(z2p_op, deg=True)
    hl_p_cb = hl_z.change_basis(z2p_op)
    hl_p_cb_rad = hl_p_cb.phase_integrals()
    assert approx_equal(
      hl_z_rad.change_basis(z2p_op).data(), hl_p_cb_rad.data())
    assert f_abs_p_cb.indices().all_eq(f_p_cb.indices())
    if (not change_hand):
      o = flex.order(f_abs_p_cb.indices(), f_abs_z.indices())
    else:
      o = flex.order(-f_abs_p_cb.indices(), f_abs_z.indices())
    if (f_abs_z.space_group().n_ltr() == 1):
      assert o == 0
    else:
      assert o != 0
    f_pz = f_p_cb.change_basis(z2p_op.inverse())
    f_abs_pz = f_abs_p_cb.change_basis(z2p_op.inverse())
    f_rad_pz = f_rad_p_cb.change_basis(z2p_op.inverse(), deg=False)
    f_deg_pz = f_deg_p_cb.change_basis(z2p_op.inverse(), deg=True)
    hl_pz = hl_p_cb.change_basis(z2p_op.inverse())
    hl_pz_rad = hl_pz.phase_integrals()
    assert approx_equal(hl_z_rad.data(), hl_pz_rad.data())
    for i,o in zip(hl_z.data(), hl_pz.data()):
      assert approx_equal(i, o)
    assert approx_equal(
      flex.max(flex.abs(f_pz.data() - f_z.data())), 0)
    assert flex.order(f_abs_pz.indices(), f_abs_z.indices()) == 0
    assert f_abs_pz.indices().all_eq(f_pz.indices())
    assert approx_equal(flex.max(scitbx.math.phase_error(
      phi1=f_rad_pz.data(), phi2=f_rad_z.data())), 0)
    assert approx_equal(flex.max(scitbx.math.phase_error(
      phi1=f_deg_pz.data(), phi2=f_deg_z.data(), deg=True)), 0)
    assert approx_equal(f_deg_pz.data(), f_rad_pz.data()*(180/math.pi))
    f_p_sf = f_p_cb.structure_factors_from_scatterers(
      xray_structure=structure_p,
      algorithm="direct").f_calc()
    det = abs(z2p_op.c().r().determinant())
    assert approx_equal(
      flex.max(flex.abs(f_p_sf.data()*complex(det) - f_p_cb.data())), 0)
    f_abs_p_sf = abs(f_p_sf)
    f_rad_p_sf = f_p_sf.phases()
    f_deg_p_sf = f_p_sf.phases(deg=True)
    assert approx_equal(flex.max(scitbx.math.phase_error(
      phi1=f_rad_p_sf.data(), phi2=f_rad_p_cb.data())), 0)
    assert approx_equal(flex.max(scitbx.math.phase_error(
      phi1=f_deg_p_sf.data(), phi2=f_deg_p_cb.data(), deg=True)), 0)
    c = flex.linear_correlation(f_abs_p_sf.data(), f_abs_p_cb.data())
    assert c.is_well_defined()
    if (0 or verbose):
      print "correlation:", c.coefficient()
    assert c.coefficient() > 0.999
Example #7
0
def exercise(space_group_info,
             n_elements       = 10,
             table            = "wk1995",
             d_min            = 2.0,
             k_sol            = 0.35,
             b_sol            = 45.0,
             b_cart           = None,
             quick=False,
             verbose=0):
  xray_structure = random_structure.xray_structure(
    space_group_info       = space_group_info,
    elements               =(("O","N","C")*(n_elements//3+1))[:n_elements],
    volume_per_atom        = 100,
    min_distance           = 1.5,
    general_positions_only = True,
    random_u_iso           = False,
    random_occupancy       = False)
  xray_structure.scattering_type_registry(table = table)
  sg = xray_structure.space_group()
  uc = xray_structure.unit_cell()
  u_cart_1 = adptbx.random_u_cart(u_scale=5, u_min=5)
  u_star_1 = adptbx.u_cart_as_u_star(uc, u_cart_1)
  b_cart   = adptbx.u_star_as_u_cart(uc, sg.average_u_star(u_star = u_star_1))
  for anomalous_flag in [False, True]:
      scatterers = xray_structure.scatterers()
      if (anomalous_flag):
        assert scatterers.size() >= 7
        for i in [1,7]:
          scatterers[i].fp = -0.2
          scatterers[i].fdp = 5
        have_non_zero_fdp = True
      else:
        for i in [1,7]:
          scatterers[i].fp = 0
          scatterers[i].fdp = 0
        have_non_zero_fdp = False
      f_obs = abs(xray_structure.structure_factors(
                               d_min          = d_min,
                               anomalous_flag = anomalous_flag,
                               cos_sin_table  = sfg_params.cos_sin_table,
                               algorithm      = sfg_params.algorithm).f_calc())
      f_obs_comp = f_obs.structure_factors_from_scatterers(
                            xray_structure = xray_structure,
                            algorithm      = sfg_params.algorithm,
                            cos_sin_table  = sfg_params.cos_sin_table).f_calc()
      f_obs = abs(f_obs_comp)
      flags = f_obs.generate_r_free_flags(fraction = 0.1,
                                          max_free = 99999999)
      #flags = flags.array(data = flex.bool(f_obs.data().size(), False))
      xrs = xray_structure.deep_copy_scatterers()
      xrs.shake_sites_in_place(rms_difference=0.3)
      for target in mmtbx.refinement.targets.target_names:
        if (quick):
          if (target not in ["ls_wunit_k1", "ml", "mlhl", "ml_sad"]):
            continue
        if (target == "mlhl"):
          if (have_non_zero_fdp): continue # XXX gradients not correct!
          experimental_phases = generate_random_hl(miller_set=f_obs)
        else:
          experimental_phases = None
        if (target == "ml_sad"
              and (not anomalous_flag or mmtbx.refinement.targets.phaser is None)):
          continue
        print "  ",target
        xray.set_scatterer_grad_flags(
          scatterers = xrs.scatterers(),
          site       = True)
        ss = 1./flex.pow2(f_obs.d_spacings().data()) / 4.
        u_star = adptbx.u_cart_as_u_star(
          f_obs.unit_cell(), adptbx.b_as_u(b_cart))
        k_anisotropic = mmtbx.f_model.ext.k_anisotropic(
          f_obs.indices(), u_star)
        k_mask = mmtbx.f_model.ext.k_mask(ss, k_sol, b_sol)
        fmodel = mmtbx.f_model.manager(
          xray_structure               = xrs,
          f_obs                        = f_obs,
          r_free_flags                 = flags,
          target_name                  = target,
          abcd                         = experimental_phases,
          sf_and_grads_accuracy_params = sfg_params,
          k_mask                       = k_mask,
          k_anisotropic                = k_anisotropic,
          mask_params                  = masks.mask_master_params.extract())
        fmodel.update_xray_structure(
          xray_structure=xrs,
          update_f_calc=True,
          update_f_mask=True)
        xray.set_scatterer_grad_flags(
          scatterers=fmodel.xray_structure.scatterers(),
          site=True)
        fmodel.update_xray_structure(update_f_calc=True)
        t_f = fmodel.target_functor()
        t_f.prepare_for_minimization()
        gs = t_f(compute_gradients=True).d_target_d_site_cart().as_double()
        gfd = finite_differences_site(target_functor=t_f)
        cc = flex.linear_correlation(gs, gfd).coefficient()
        if (0 or verbose):
          print "ana:", list(gs)
          print "fin:", list(gfd)
          print "rat:", [f/a for a,f in zip(gs,gfd)]
          print target, "corr:", cc, space_group_info
          print
        diff = gs - gfd
        diff /= max(1, flex.max(flex.abs(gfd)))
        tolerance = 1.2e-5
        assert approx_equal(abs(flex.min(diff) ), 0.0, tolerance)
        assert approx_equal(abs(flex.mean(diff)), 0.0, tolerance)
        assert approx_equal(abs(flex.max(diff) ), 0.0, tolerance)
        assert approx_equal(cc, 1.0, tolerance)
        fmodel.model_error_ml()
Example #8
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