Beispiel #1
0
def sample_e_pot(id_code, f_obs, xray_structure, edge_list, params):
    if (edge_list is None):
        print("NO_TARDY: no edge_list")
        return
    #
    xs = xray_structure
    if (xs.special_position_indices().size() != 0):
        print("NO_TARDY: special positions")
        return
    sites_cart = xs.sites_cart()
    labels = xs.scatterers().extract_labels()
    pat = xs.pair_asu_table(distance_cutoff=2.5)
    if (0):
        pat.show_distances(sites_cart=sites_cart, site_labels=labels)
    pst = pat.extract_pair_sym_table()
    for i, j in edge_list:
        assert i <= j
        sym_dict = pst[i].get(j)
        if (sym_dict is None):
            print("NO_TARDY: large distance for edge:", labels[i], labels[j])
            return
    #
    import scitbx.graph.tardy_tree
    tt = scitbx.graph.tardy_tree.construct(sites=sites_cart,
                                           edge_list=edge_list)
    import scitbx.rigid_body
    tardy_model = scitbx.rigid_body.tardy_model(
        labels=labels,
        sites=sites_cart,
        masses=xs.atomic_weights(),
        tardy_tree=tt,
        potential_obj=potential_object(f_obs, xs),
        near_singular_hinges_angular_tolerance_deg=5)
    if (tardy_model.number_of_trees != 1):
        print("NO_TARDY: multiple trees")
        return
    #
    print("Single tardy tree:", \
      id_code, xs.scatterers().size(), xs.space_group_info())
    tt.show_summary(vertex_labels=labels, prefix="  ")
    print("dof each joint:", list(tardy_model.degrees_of_freedom_each_joint()))
    print("q_size each joint:", list(tardy_model.q_size_each_joint()))
    q_packed = tardy_model.pack_q()
    print("q_packed.size():", q_packed.size())
    print("q_packed:", numstr(q_packed))
    if (params.iq < 0):
        return
    assert params.iq < q_packed.size()
    #
    xy = []
    from libtbx.utils import xsamples
    from math import pi
    for q_deg in xsamples(params.qmin, params.qmax, params.qstep):
        q_packed[params.iq] = q_deg * pi / 180
        tardy_model.unpack_q(q_packed=q_packed)
        e_pot = tardy_model.e_pot()
        xy.append((q_deg, e_pot))
    from libtbx import pyplot
    pyplot.plot_pairs(xy, "r-")
    pyplot.show()
def sample_e_pot(id_code, f_obs, xray_structure, edge_list, params):
  if (edge_list is None):
    print "NO_TARDY: no edge_list"
    return
  #
  xs = xray_structure
  if (xs.special_position_indices().size() != 0):
    print "NO_TARDY: special positions"
    return
  sites_cart = xs.sites_cart()
  labels = xs.scatterers().extract_labels()
  pat = xs.pair_asu_table(distance_cutoff=2.5)
  if (0):
    pat.show_distances(sites_cart=sites_cart, site_labels=labels)
  pst = pat.extract_pair_sym_table()
  for i,j in edge_list:
    assert i <= j
    sym_dict = pst[i].get(j)
    if (sym_dict is None):
      print "NO_TARDY: large distance for edge:", labels[i], labels[j]
      return
  #
  import scitbx.graph.tardy_tree
  tt = scitbx.graph.tardy_tree.construct(
    sites=sites_cart, edge_list=edge_list)
  import scitbx.rigid_body
  tardy_model = scitbx.rigid_body.tardy_model(
    labels=labels,
    sites=sites_cart,
    masses=xs.atomic_weights(),
    tardy_tree=tt,
    potential_obj=potential_object(f_obs, xs),
    near_singular_hinges_angular_tolerance_deg=5)
  if (tardy_model.number_of_trees != 1):
    print "NO_TARDY: multiple trees"
    return
  #
  print "Single tardy tree:", \
    id_code, xs.scatterers().size(), xs.space_group_info()
  tt.show_summary(vertex_labels=labels, prefix="  ")
  print "dof each joint:", list(tardy_model.degrees_of_freedom_each_joint())
  print "q_size each joint:", list(tardy_model.q_size_each_joint())
  q_packed = tardy_model.pack_q()
  print "q_packed.size():", q_packed.size()
  print "q_packed:", numstr(q_packed)
  if (params.iq < 0):
    return
  assert params.iq < q_packed.size()
  #
  xy = []
  from libtbx.utils import xsamples
  from math import pi
  for q_deg in xsamples(params.qmin, params.qmax, params.qstep):
    q_packed[params.iq] = q_deg*pi/180
    tardy_model.unpack_q(q_packed=q_packed)
    e_pot = tardy_model.e_pot()
    xy.append((q_deg,e_pot))
  from libtbx import pyplot
  pyplot.plot_pairs(xy, "r-")
  pyplot.show()
    def f_obs_and_f_calc_agree_well(O, co):
        if (O.c_obs.indices().size() == 0): return False
        from cctbx.array_family import flex
        f_obs = O.c_obs.as_amplitude_array(algorithm="xtal_3_7")
        f_calc = f_obs.structure_factors_from_scatterers(
            xray_structure=O.xray_structure).f_calc().amplitudes()
        fan_out_sel = f_obs.f_obs_f_calc_fan_outlier_selection(
            f_calc=f_calc,
            offset_low=co.fan_offset_low,
            offset_high=co.fan_offset_high,
            also_return_x_and_y=True)
        if (fan_out_sel is None):
            return False
        if (co.i_obs_i_calc_plot and f_obs.indices().size() != 0):
            from libtbx import pyplot
            xs = O.c_obs.as_intensity_array(algorithm="simple").data()
            ys = flex.pow2(f_calc.data())
            pyplot.plot(xs.as_numpy_array(), ys.as_numpy_array(), "ro")
            pyplot.show()
        fan_out_sel, x, y = fan_out_sel
        fan_in_sel = ~fan_out_sel
        if (co.f_obs_f_calc_plot):
            from libtbx import pyplot
            xs = x.select(fan_out_sel)
            ys = y.select(fan_out_sel)
            if (xs.size() == 0):
                pyplot.plot(x.as_numpy_array(), y.as_numpy_array(), "bo")
            else:
                pyplot.plot(xs.as_numpy_array(), ys.as_numpy_array(), "ro")
                xs = x.select(fan_in_sel)
                ys = y.select(fan_in_sel)
                if (xs.size() != 0):
                    pyplot.plot(xs.as_numpy_array(), ys.as_numpy_array(), "bo")
            pyplot.plot_pairs([(co.fan_offset_low, 0),
                               (1, 1 - co.fan_offset_high)], "r-")
            pyplot.plot_pairs([(0, co.fan_offset_low),
                               (1 - co.fan_offset_high, 1)], "r-")
            pyplot.plot_pairs([(0, 0), (1, 1)], "k--")
            pyplot.show()
        fan_outlier_fraction = fan_out_sel.count(True) / fan_out_sel.size()

        def cc_r1(fo, fc):
            lc = flex.linear_correlation(fo.data(), fc.data())
            assert lc.is_well_defined()
            cc = lc.coefficient()
            from libtbx import Auto
            r1 = f_obs.r1_factor(other=f_calc, scale_factor=Auto)
            return cc, r1

        cc_all, r1_all = cc_r1(f_obs, f_calc)
        cc_in, r1_in = cc_r1(f_obs.select(fan_in_sel),
                             f_calc.select(fan_in_sel))
        print "f_obs_f_calc %s" % O.cod_id, \
          "| cc_all %.4f | r1_all %.4f | out %.4f | cc_in %.4f | r1_in %.4f |" % (
            cc_all, r1_all, fan_outlier_fraction, cc_in, r1_in)
        if (fan_outlier_fraction > co.max_fan_outlier_fraction):
            return False
        if (cc_all < co.min_f_obs_f_calc_correlation):
            return False
        return True
    def f_obs_and_f_calc_agree_well(O, co):
        if O.c_obs.indices().size() == 0:
            return False
        from cctbx.array_family import flex

        f_obs = O.c_obs.as_amplitude_array(algorithm="xtal_3_7")
        f_calc = f_obs.structure_factors_from_scatterers(xray_structure=O.xray_structure).f_calc().amplitudes()
        fan_out_sel = f_obs.f_obs_f_calc_fan_outlier_selection(
            f_calc=f_calc, offset_low=co.fan_offset_low, offset_high=co.fan_offset_high, also_return_x_and_y=True
        )
        if fan_out_sel is None:
            return False
        if co.i_obs_i_calc_plot and f_obs.indices().size() != 0:
            from libtbx import pyplot

            xs = O.c_obs.as_intensity_array(algorithm="simple").data()
            ys = flex.pow2(f_calc.data())
            pyplot.plot(xs.as_numpy_array(), ys.as_numpy_array(), "ro")
            pyplot.show()
        fan_out_sel, x, y = fan_out_sel
        fan_in_sel = ~fan_out_sel
        if co.f_obs_f_calc_plot:
            from libtbx import pyplot

            xs = x.select(fan_out_sel)
            ys = y.select(fan_out_sel)
            if xs.size() == 0:
                pyplot.plot(x.as_numpy_array(), y.as_numpy_array(), "bo")
            else:
                pyplot.plot(xs.as_numpy_array(), ys.as_numpy_array(), "ro")
                xs = x.select(fan_in_sel)
                ys = y.select(fan_in_sel)
                if xs.size() != 0:
                    pyplot.plot(xs.as_numpy_array(), ys.as_numpy_array(), "bo")
            pyplot.plot_pairs([(co.fan_offset_low, 0), (1, 1 - co.fan_offset_high)], "r-")
            pyplot.plot_pairs([(0, co.fan_offset_low), (1 - co.fan_offset_high, 1)], "r-")
            pyplot.plot_pairs([(0, 0), (1, 1)], "k--")
            pyplot.show()
        fan_outlier_fraction = fan_out_sel.count(True) / fan_out_sel.size()

        def cc_r1(fo, fc):
            lc = flex.linear_correlation(fo.data(), fc.data())
            assert lc.is_well_defined()
            cc = lc.coefficient()
            from libtbx import Auto

            r1 = f_obs.r1_factor(other=f_calc, scale_factor=Auto)
            return cc, r1

        cc_all, r1_all = cc_r1(f_obs, f_calc)
        cc_in, r1_in = cc_r1(f_obs.select(fan_in_sel), f_calc.select(fan_in_sel))
        print "f_obs_f_calc %s" % O.cod_id, "| cc_all %.4f | r1_all %.4f | out %.4f | cc_in %.4f | r1_in %.4f |" % (
            cc_all,
            r1_all,
            fan_outlier_fraction,
            cc_in,
            r1_in,
        )
        if fan_outlier_fraction > co.max_fan_outlier_fraction:
            return False
        if cc_all < co.min_f_obs_f_calc_correlation:
            return False
        return True