Ejemplo n.º 1
0
    def __init__(self,
                 xray_structure,
                 obs_,
                 exti=None,
                 connectivity_table=None):
        if exti is None:
            exti = xray.dummy_extinction_correction()
        adopt_init_args(self, locals())
        assert obs_.fo_sq.anomalous_flag()
        assert not (obs_.twin_fractions and obs_.merohedral_components)

        xray_structure = xray_structure.deep_copy_scatterers()
        for sc in xray_structure.scatterers():
            f = xray.scatterer_flags()
            f.set_use_u_aniso(sc.flags.use_u_aniso())
            f.set_use_u_iso(sc.flags.use_u_iso())
            f.set_use_fp_fdp(True)
            sc.flags = f

        twin_fractions = ()
        it = xray.twin_component(sgtbx.rot_mx((-1, 0, 0, 0, -1, 0, 0, 0, -1)),
                                 0.2, True)
        twin_components = (it, )
        obs = observations.customized_copy(obs_, twin_fractions,
                                           twin_components)
        # reparameterisation needs all fractions
        twin_fractions += twin_components
        if connectivity_table is None:
            connectivity_table = smtbx.utils.connectivity_table(xray_structure)
        reparametrisation = constraints.reparametrisation(
            xray_structure, [],
            connectivity_table,
            twin_fractions=twin_fractions,
            extinction=exti)
        normal_eqns = least_squares.crystallographic_ls(obs, reparametrisation)
        cycles = normal_eqns_solving.naive_iterations(normal_eqns,
                                                      n_max_iterations=10,
                                                      gradient_threshold=1e-7,
                                                      step_threshold=1e-4)
        self.flack_x = it.value
        self.sigma_x = math.sqrt(
            normal_eqns.covariance_matrix(
                jacobian_transpose=reparametrisation.
                jacobian_transpose_matching(
                    reparametrisation.mapping_to_grad_fc_independent_scalars))
            [0])
Ejemplo n.º 2
0
  def __init__(self, xray_structure, obs_, exti=None, connectivity_table=None):
    if exti is None:
      exti = xray.dummy_extinction_correction()
    adopt_init_args(self, locals())
    assert obs_.fo_sq.anomalous_flag()
    xray_structure = xray_structure.deep_copy_scatterers()
    flags = xray_structure.scatterer_flags()
    for sc in xray_structure.scatterers():
      f = xray.scatterer_flags()
      f.set_use_u_aniso(sc.flags.use_u_aniso())
      f.set_use_u_iso(sc.flags.use_u_iso())
      f.set_use_fp_fdp(True)
      sc.flags = f

    twin_fractions = obs_.twin_fractions
    twin_components = obs_.merohedral_components
    for tw in twin_fractions: tw.grad = False
    for tc in twin_components: tc.grad = False

    it = xray.twin_component(sgtbx.rot_mx((-1,0,0,0,-1,0,0,0,-1)), 0.2, True)
    twin_components += (it,)
    obs = observations.customized_copy(obs_, twin_fractions, twin_components)
    # reparameterisation needs all fractions
    twin_fractions += twin_components
    if connectivity_table is None:
      connectivity_table = smtbx.utils.connectivity_table(xray_structure)
    reparametrisation = constraints.reparametrisation(
      xray_structure, [], connectivity_table,
      twin_fractions=twin_fractions,
      extinction=exti
    )
    normal_eqns = least_squares.crystallographic_ls(obs,
      reparametrisation)
    cycles = normal_eqns_solving.naive_iterations(
      normal_eqns, n_max_iterations=10,
      gradient_threshold=1e-7,
      step_threshold=1e-4)
    self.flack_x = it.value
    self.sigma_x = math.sqrt(normal_eqns.covariance_matrix(
      jacobian_transpose=reparametrisation.jacobian_transpose_matching(
        reparametrisation.mapping_to_grad_fc_independent_scalars))[0])
Ejemplo n.º 3
0
def excersise():
    s = """\
  -1  -6   9624.8650 54.9190   1
  -1   6   9749.5660 52.1870  -2
   1   6  -9749.5660 52.1870   1
   1  -6  -8695.6020 53.8100  -2
  -1  -6   8695.6020 53.8100   1
  -1   6   8746.7970 51.3980  -2
   1   6  -8746.7970 51.3980   1
  -1  -6 -12281.3590 49.5060   1
   1   6  12185.9370 47.3950   1
  -1  -6 -1316.0700044.01400   1
   1   6  13 4.7500043.54900   1
  -1  -6 -1479.6380048.66400   2
   1   6  1432.7830051.51700   2
  """
    sg = sgtbx.space_group("P 1")
    uc = uctbx.unit_cell((11, 11, 13, 90, 90, 120))
    cs = crystal.symmetry(unit_cell=uc, space_group=sg)
    ma = hklf.reader(file_object=StringIO(s))\
           .as_miller_arrays(crystal_symmetry=cs, merge_equivalents=False)
    fo_sq = ma[0]
    batch_numbers = ma[1]
    obs = fo_sq.as_xray_observations(scale_indices=batch_numbers.data(),
                                     twin_fractions=(xray.twin_fraction(
                                         0.4, True), ))
    measured_cnt = 0
    measured_scale_indices = obs.measured_scale_indices
    for bn in batch_numbers.data():
        if bn > 0:
            assert (measured_scale_indices[measured_cnt] == bn)
            measured_cnt = measured_cnt + 1
    assert (measured_cnt == obs.indices.size())

    itr = obs.iterator(0)
    assert (not itr.has_next())
    itr = obs.iterator(1)
    assert (itr.next().h == (-1, 6, 9))
    itr = obs.iterator(2)
    assert (itr.next().h == (1, -6, -8))

    # this is supposed to fail for now until somebody re-implements
    # mixed HKLF 5 + merohedral twinning in a meaningful way...
    try:
        obs = observations.customized_copy(
            obs,
            twin_fractions=(xray.twin_fraction(0.7, True), ),
            twin_components=(xray.twin_component(
                sgtbx.rot_mx((-1, 0, 0, 0, -1, 0, 0, 0, -1)), 0.25, True), ))

        itr = obs.iterator(0)
        assert (itr.has_next())
        assert (itr.next().h == (1, 6, -9))
        assert (not itr.has_next())
        itr = obs.iterator(1)
        assert (itr.next().h == (-1, -6, 9))
        assert (itr.next().h == (-1, 6, 9))
        assert (itr.next().h == (1, -6, -9))
        assert (not itr.has_next())

        ts = 1 - obs.ref_twin_components[0].value
        ps = 1 - obs.ref_twin_fractions[0].value
        itr = obs.iterator(0)
        assert obs.scale(0) == ts * ps
        nv = next(itr)
        assert nv.scale == obs.ref_twin_components[0].value * ps
    except RuntimeError as e:
        pass
Ejemplo n.º 4
0
def excersise():
  s = """\
  -1  -6   9624.8650 54.9190   1
  -1   6   9749.5660 52.1870  -2
   1   6  -9749.5660 52.1870   1
   1  -6  -8695.6020 53.8100  -2
  -1  -6   8695.6020 53.8100   1
  -1   6   8746.7970 51.3980  -2
   1   6  -8746.7970 51.3980   1
  -1  -6 -12281.3590 49.5060   1
   1   6  12185.9370 47.3950   1
  -1  -6 -1316.0700044.01400   1
   1   6  13 4.7500043.54900   1
  -1  -6 -1479.6380048.66400   2
   1   6  1432.7830051.51700   2
  """
  sg = sgtbx.space_group("P 1")
  uc = uctbx.unit_cell((11,11,13,90,90,120))
  cs = crystal.symmetry(unit_cell=uc, space_group=sg)
  ma = hklf.reader(file_object=StringIO(s))\
         .as_miller_arrays(crystal_symmetry=cs, merge_equivalents=False)
  fo_sq = ma[0]
  batch_numbers = ma[1]
  obs = fo_sq.as_xray_observations(
          scale_indices=batch_numbers.data(),
          twin_fractions=(xray.twin_fraction(0.4,True),))
  measured_cnt = 0
  measured_scale_indices = obs.measured_scale_indices
  for bn in batch_numbers.data():
    if bn > 0:
      assert(measured_scale_indices[measured_cnt]==bn)
      measured_cnt = measured_cnt+1
  assert(measured_cnt == obs.indices.size())

  itr = obs.iterator(0)
  assert(not itr.has_next())
  itr = obs.iterator(1)
  assert(itr.next().h==(-1,6,9))
  itr = obs.iterator(2)
  assert(itr.next().h==(1,-6,-8))

  obs = observations.customized_copy(obs,
          twin_fractions=(xray.twin_fraction(0.7,True),),
          twin_components=(xray.twin_component(
              sgtbx.rot_mx((-1,0,0,0,-1,0,0,0,-1)), 0.25, True),))

  itr = obs.iterator(0)
  assert(itr.has_next())
  assert(itr.next().h==(1,6,-9))
  assert(not itr.has_next())
  itr = obs.iterator(1)
  assert(itr.next().h==(-1,-6,9))
  assert(itr.next().h==(-1,6,9))
  assert(itr.next().h==(1,-6,-9))
  assert(not itr.has_next())

  ts = 1-obs.ref_twin_components[0].value
  ps = 1-obs.ref_twin_fractions[0].value
  itr = obs.iterator(0)
  assert obs.scale(0) == ts*ps
  nv = itr.next()
  assert nv.scale == obs.ref_twin_components[0].value*ps
Ejemplo n.º 5
0
  def exercise(self):
    xs0 = self.structure
    xs = xs0.deep_copy_scatterers()
    xs.shake_sites_in_place(rms_difference=0.15)
    xs.shake_adp()

    for sc in xs.scatterers():
      sc.flags.set_use_u_iso(False).set_use_u_aniso(True)
      sc.flags.set_grad_site(True).set_grad_u_aniso(True)
    connectivity_table = smtbx.utils.connectivity_table(xs)
    if self.twin_fractions[0] < 0.5:
      twin_fractions = self.twin_fractions.deep_copy() + 0.1
    else:
      twin_fractions = self.twin_fractions.deep_copy() - 0.1
    twin_components = tuple(
      [xray.twin_component(law, fraction, grad=True)
       for law, fraction in zip(self.twin_laws, twin_fractions)])
    reparametrisation = constraints.reparametrisation(
      structure=xs,
      constraints=[],
      connectivity_table=connectivity_table,
      twin_fractions=twin_components)
    obs = self.fo_sq.as_xray_observations(twin_components=twin_components)
    normal_eqns = least_squares.crystallographic_ls(
      obs, reparametrisation,
      weighting_scheme=least_squares.unit_weighting(),
      origin_fixing_restraints_type=
      origin_fixing_restraints.atomic_number_weighting)
    cycles = normal_eqns_solving.naive_iterations(
      normal_eqns,
      n_max_iterations=10,
      track_all=True)
    assert approx_equal(
      [twin.value for twin in normal_eqns.twin_fractions],
      self.twin_fractions, eps=1e-2)
    assert approx_equal(normal_eqns.objective(), 0, eps=1e-5)
    assert normal_eqns.n_parameters == 64
    # now with fixed twin fraction
    xs.shake_sites_in_place(rms_difference=0.15)
    xs.shake_adp()
    twin_components = tuple(
      [xray.twin_component(law, fraction, grad=False)
       for law, fraction in zip(self.twin_laws, twin_fractions)])
    #change the twin_components of the observations...
    obs = observations.customized_copy(obs, twin_components=twin_components)
    reparametrisation = constraints.reparametrisation(
      structure=xs,
      constraints=[],
      connectivity_table=connectivity_table,
      twin_fractions=twin_components)
    normal_eqns = least_squares.crystallographic_ls(
      obs, reparametrisation,
      weighting_scheme=least_squares.unit_weighting(),
      origin_fixing_restraints_type=
      origin_fixing_restraints.atomic_number_weighting)
    cycles = normal_eqns_solving.naive_iterations(
      normal_eqns,
      n_max_iterations=10,
      track_all=True)
    assert approx_equal(
      [twin.value for twin in normal_eqns.twin_fractions],
      twin_fractions)
    assert normal_eqns.objective() != 0 # since the twin fraction is not refined
    assert normal_eqns.n_parameters == 63