Esempio n. 1
0
def exercise_monoclinic_cell_choices_core(space_group_number, verbose):
  # transformation matrices for cell choices
  # columns are basis vectors "new in terms of old"
  # see Int. Tab. Vol. A, p. 22, Fig. 2.2.6.4.
  b1 = (1, 0, 0,
        0, 1, 0,
        0, 0, 1)
  b2 = (-1, 0, 1,
         0, 1, 0,
        -1, 0, 0)
  b3 = (0, 0, -1,
        0, 1,  0,
        1, 0, -1)
  flip = (0,  0, 1,
          0, -1, 0,
          1,  0, 0)
  p3s = sgtbx.space_group("P 3*")
  done = {}
  ref = sgtbx.space_group_info(number=space_group_number)
  ref_uhm = ref.type().universal_hermann_mauguin_symbol()
  for i_fl,fl in enumerate([b1, flip]):
    rfl = sgtbx.rot_mx(fl)
    cfl = sgtbx.change_of_basis_op(sgtbx.rt_mx(rfl))
    for i_rt,rt in enumerate(p3s):
      rp3 = rt.r()
      cp3 = sgtbx.change_of_basis_op(sgtbx.rt_mx(rp3))
      for i_cs,cs in enumerate([b1,b2,b3]):
        rcs = sgtbx.rot_mx(cs).inverse()
        ccs = sgtbx.change_of_basis_op(sgtbx.rt_mx(rcs))
        cb_all = cp3 * cfl * ccs
        refcb = ref.change_basis(cb_all)
        refcb2 = sgtbx.space_group_info(symbol=ref_uhm+"("+str(cb_all.c())+")")
        assert refcb2.group() == refcb.group()
        s = sgtbx.space_group_symbols(str(refcb))
        q = s.qualifier()
        hm = str(refcb)
        if (0 or verbose): print hm, q, cb_all.c()
        if (i_fl == 0):
          assert q[0] == "bca"[i_rt]
          if (len(q) == 2): assert q[1] == "123"[i_cs]
        elif (q[0] == "-"):
          assert q[1] == "bca"[i_rt]
          if (len(q) == 3): assert q[2] == "123"[i_cs]
        else:
          assert q[0] == "bca"[i_rt]
          if (len(q) == 2 and q[1] != "123"[i_cs]):
            assert done[hm] == 1
        done.setdefault(hm, 0)
        done[hm] += 1
  assert len(done) in [3, 9, 18]
  assert done.values() == [18/len(done)]*len(done)
  if (0 or verbose): print
  return done
def run(args):
    if "--full" in args:
        to_do = range(1, 230 + 1)
    elif "--special" in args:
        to_do = sorted(special.keys())
    else:
        to_do = [75, 151]
    for space_group_number in to_do:
        sgi = sgtbx.space_group_info(number=space_group_number)
        sgi.show_summary(prefix="")
        sys.stdout.flush()
        n_special = 0
        for m in scitbx.math.unimodular_generator(range=1).all():
            cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(sgtbx.rot_mx(m, 1), 1)).new_denominators(12, 144)
            cb_sgi = sgi.change_basis(cb_op=cb_op)
            cb_op_ref = cb_sgi.change_of_basis_op_to_reference_setting()
            ref_sgi = cb_sgi.change_basis(cb_op=cb_op_ref)
            assert ref_sgi.group() == sgi.group()
            c = cb_op_ref.c()
            if c.r().is_unit_mx() and c.t().num() != (0, 0, 0):
                n_special += 1
                cb_ref_sgi = sgi.change_basis(cb_op=cb_op_ref)
                print "  cb_op=%s -> %s" % (str(cb_op.c()), cb_ref_sgi.type().universal_hermann_mauguin_symbol())
                sys.stdout.flush()
                # verify that c.t() is not an allowed origin shift
                assert cb_ref_sgi.group() != sgi.group()
        assert special.get(space_group_number, 0) == n_special
    print format_cpu_times()
 def __init__(self, file_name, header_only=False):
   self.file_name = os.path.normpath(file_name)
   f = open(file_name)
   line = f.readline()
   assert line[5] == " "
   n_sym_ops_from_file = int(line[:5].strip())
   assert n_sym_ops_from_file > 0
   self.space_group_symbol = line[6:].strip()
   self.space_group_from_ops = sgtbx.space_group()
   for i in xrange(n_sym_ops_from_file):
     line = f.readline().rstrip()
     assert len(line) == 27
     r = sgtbx.rot_mx([int(line[j*3:(j+1)*3]) for j in xrange(9)], 1)
     line = f.readline().rstrip()
     assert len(line) == 9
     t = sgtbx.tr_vec([int(line[j*3:(j+1)*3]) for j in xrange(3)], 12)
     self.space_group_from_ops.expand_smx(sgtbx.rt_mx(r, t))
   f.close()
   if (header_only):
     self.original_indices = None
     return
   all_arrays = scalepack_ext.no_merge_original_index_arrays(
     file_name, n_sym_ops_from_file*2+1)
   self.original_indices = all_arrays.original_indices()
   self.unique_indices = all_arrays.unique_indices()
   self.batch_numbers = all_arrays.batch_numbers()
   self.centric_tags = all_arrays.centric_tags()
   self.spindle_flags = all_arrays.spindle_flags()
   self.asymmetric_unit_indices = all_arrays.asymmetric_unit_indices()
   self.i_obs = all_arrays.i_obs()
   self.sigmas = all_arrays.sigmas()
Esempio n. 4
0
 def __init__(self, file_name, header_only=False):
   self.file_name = os.path.normpath(file_name)
   f = open(file_name)
   line = f.readline()
   assert line[5] == " "
   n_sym_ops_from_file = int(line[:5].strip())
   assert n_sym_ops_from_file > 0
   self.space_group_symbol = line[6:].strip()
   self.space_group_from_ops = sgtbx.space_group()
   for i in range(n_sym_ops_from_file):
     line = f.readline().rstrip()
     assert len(line) == 27
     r = sgtbx.rot_mx([int(line[j*3:(j+1)*3]) for j in range(9)], 1)
     line = f.readline().rstrip()
     assert len(line) == 9
     t = sgtbx.tr_vec([int(line[j*3:(j+1)*3]) for j in range(3)], 12)
     self.space_group_from_ops.expand_smx(sgtbx.rt_mx(r, t))
   f.close()
   if (header_only):
     self.original_indices = None
     return
   all_arrays = scalepack_ext.no_merge_original_index_arrays(
     file_name, n_sym_ops_from_file*2+1)
   self.original_indices = all_arrays.original_indices()
   self.unique_indices = all_arrays.unique_indices()
   self.batch_numbers = all_arrays.batch_numbers()
   self.centric_tags = all_arrays.centric_tags()
   self.spindle_flags = all_arrays.spindle_flags()
   self.asymmetric_unit_indices = all_arrays.asymmetric_unit_indices()
   self.i_obs = all_arrays.i_obs()
   self.sigmas = all_arrays.sigmas()
Esempio n. 5
0
def run(args):
    if ("--full" in args):
        to_do = range(1, 230 + 1)
    elif ("--special" in args):
        to_do = sorted(special.keys())
    else:
        to_do = [75, 151]
    for space_group_number in to_do:
        sgi = sgtbx.space_group_info(number=space_group_number)
        sgi.show_summary(prefix="")
        sys.stdout.flush()
        n_special = 0
        for m in scitbx.math.unimodular_generator(range=1).all():
            cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(sgtbx.rot_mx(m,1), 1)) \
              .new_denominators(12, 144)
            cb_sgi = sgi.change_basis(cb_op=cb_op)
            cb_op_ref = cb_sgi.change_of_basis_op_to_reference_setting()
            ref_sgi = cb_sgi.change_basis(cb_op=cb_op_ref)
            assert ref_sgi.group() == sgi.group()
            c = cb_op_ref.c()
            if (c.r().is_unit_mx() and c.t().num() != (0, 0, 0)):
                n_special += 1
                cb_ref_sgi = sgi.change_basis(cb_op=cb_op_ref)
                print "  cb_op=%s -> %s" % (str(cb_op.c()), cb_ref_sgi.type().
                                            universal_hermann_mauguin_symbol())
                sys.stdout.flush()
                # verify that c.t() is not an allowed origin shift
                assert cb_ref_sgi.group() != sgi.group()
        assert special.get(space_group_number, 0) == n_special
    print format_cpu_times()
Esempio n. 6
0
def run():
    for elements in flex.nested_loop([-1] * 9, [1 + 1] * 9):
        r = sgtbx.rot_mx(elements)
        if (r.determinant() != 1): continue
        if (not r.multiply(r).is_unit_mx()): continue
        if (r.is_unit_mx()): continue
        print(elements, r.info().ev(), r.transpose().info().ev())
    print("OK")
Esempio n. 7
0
 def algebraic(self):
     op = self.operation()
     # eliminate minus signs to make the expression look more like
     # what people are used to see
     r = op.r().num()
     d = op.r().den()
     signs = [None, None, None]
     for j in xrange(3):
         for i in xrange(3):
             rij = r[i * 3 + j]
             if (signs[i] is None and rij != 0):
                 if (rij < 0): signs[i] = -1
                 else: signs[i] = 1
     m = list(sgtbx.rot_mx(d, d).num())
     for i in xrange(3):
         if (signs[i] == -1): m[i * 4] *= -1
     return str(sgtbx.rt_mx(op.r().multiply(sgtbx.rot_mx(m, d)), op.t()))
def run():
  for elements in flex.nested_loop([-1]*9,[1+1]*9):
    r = sgtbx.rot_mx(elements)
    if (r.determinant() != 1): continue
    if (not r.multiply(r).is_unit_mx()): continue
    if (r.is_unit_mx()): continue
    print elements, r.info().ev(), r.transpose().info().ev()
  print "OK"
Esempio n. 9
0
def generate_unimodular_cells(cell):
    Amat = sqr(cell.orthogonalization_matrix()).transpose()

    for m in unimodular_generator(range=1).all():
      c_inv = sgtbx.rt_mx(sgtbx.rot_mx(m))
      orientation_similarity_cb_op = sgtbx.change_of_basis_op(c_inv).inverse()
      new_cell = cell.change_basis(orientation_similarity_cb_op)
      yield new_cell,orientation_similarity_cb_op
Esempio n. 10
0
def exercise_monoclinic_cell_choices_core(space_group_number, verbose):
    # transformation matrices for cell choices
    # columns are basis vectors "new in terms of old"
    # see Int. Tab. Vol. A, p. 22, Fig. 2.2.6.4.
    b1 = (1, 0, 0, 0, 1, 0, 0, 0, 1)
    b2 = (-1, 0, 1, 0, 1, 0, -1, 0, 0)
    b3 = (0, 0, -1, 0, 1, 0, 1, 0, -1)
    flip = (0, 0, 1, 0, -1, 0, 1, 0, 0)
    p3s = sgtbx.space_group("P 3*")
    done = {}
    ref = sgtbx.space_group_info(number=space_group_number)
    ref_uhm = ref.type().universal_hermann_mauguin_symbol()
    for i_fl, fl in enumerate([b1, flip]):
        rfl = sgtbx.rot_mx(fl)
        cfl = sgtbx.change_of_basis_op(sgtbx.rt_mx(rfl))
        for i_rt, rt in enumerate(p3s):
            rp3 = rt.r()
            cp3 = sgtbx.change_of_basis_op(sgtbx.rt_mx(rp3))
            for i_cs, cs in enumerate([b1, b2, b3]):
                rcs = sgtbx.rot_mx(cs).inverse()
                ccs = sgtbx.change_of_basis_op(sgtbx.rt_mx(rcs))
                cb_all = cp3 * cfl * ccs
                refcb = ref.change_basis(cb_all)
                refcb2 = sgtbx.space_group_info(symbol=ref_uhm + "(" +
                                                str(cb_all.c()) + ")")
                assert refcb2.group() == refcb.group()
                s = sgtbx.space_group_symbols(str(refcb))
                q = s.qualifier()
                hm = str(refcb)
                if (0 or verbose): print(hm, q, cb_all.c())
                if (i_fl == 0):
                    assert q[0] == "bca"[i_rt]
                    if (len(q) == 2): assert q[1] == "123"[i_cs]
                elif (q[0] == "-"):
                    assert q[1] == "bca"[i_rt]
                    if (len(q) == 3): assert q[2] == "123"[i_cs]
                else:
                    assert q[0] == "bca"[i_rt]
                    if (len(q) == 2 and q[1] != "123"[i_cs]):
                        assert done[hm] == 1
                done.setdefault(hm, 0)
                done[hm] += 1
    assert len(done) in [3, 9, 18]
    assert list(done.values()) == [18 / len(done)] * len(done)
    if (0 or verbose): print()
    return done
Esempio n. 11
0
 def algebraic(self):
   op = self.operation()
   # eliminate minus signs to make the expression look more like
   # what people are used to see
   r = op.r().num()
   d = op.r().den()
   signs = [None,None,None]
   for j in xrange(3):
     for i in xrange(3):
       rij = r[i*3+j]
       if (signs[i] is None and rij != 0):
         if (rij < 0): signs[i] = -1
         else: signs[i] = 1
   m = list(sgtbx.rot_mx(d, d).num())
   for i in xrange(3):
     if (signs[i] == -1): m[i*4] *= -1
   return str(sgtbx.rt_mx(op.r().multiply(sgtbx.rot_mx(m,d)), op.t()))
Esempio n. 12
0
def generate_unimodular_cells(cell):
    Amat = sqr(cell.orthogonalization_matrix()).transpose()

    for m in unimodular_generator(range=1).all():
      c_inv = sgtbx.rt_mx(sgtbx.rot_mx(m))
      orientation_similarity_cb_op = sgtbx.change_of_basis_op(c_inv).inverse()
      new_cell = cell.change_basis(orientation_similarity_cb_op)
      yield new_cell,orientation_similarity_cb_op
def enumerate_reduced_cell_two_folds():
  result = []
  for elements in flex.nested_loop([-1]*9,[1+1]*9):
    r = sgtbx.rot_mx(elements)
    if (r.determinant() != 1): continue
    if (r.inverse() != r): continue
    if (r.is_unit_mx()): continue
    result.append(r)
  return result
Esempio n. 14
0
def enumerate_reduced_cell_two_folds():
    result = []
    for elements in flex.nested_loop([-1] * 9, [1 + 1] * 9):
        r = sgtbx.rot_mx(elements)
        if (r.determinant() != 1): continue
        if (r.inverse() != r): continue
        if (r.is_unit_mx()): continue
        result.append(r)
    return result
Esempio n. 15
0
  def __call__(self, params, options):
    ''' Import the integrate.hkl file. '''

    from iotbx.xds import integrate_hkl
    from dials.array_family import flex
    from dials.util.command_line import Command
    from cctbx import sgtbx

    # Get the unit cell to calculate the resolution
    uc = self._experiment.crystal.get_unit_cell()

    # Read the INTEGRATE.HKL file
    Command.start('Reading INTEGRATE.HKL')
    handle = integrate_hkl.reader()
    handle.read_file(self._integrate_hkl)
    hkl    = flex.miller_index(handle.hkl)
    xyzcal = flex.vec3_double(handle.xyzcal)
    xyzobs = flex.vec3_double(handle.xyzobs)
    iobs   = flex.double(handle.iobs)
    sigma  = flex.double(handle.sigma)
    rlp = flex.double(handle.rlp)
    peak = flex.double(handle.peak) * 0.01
    Command.end('Read %d reflections from INTEGRATE.HKL file.' % len(hkl))

    # Derive the reindex matrix
    rdx = self.derive_reindex_matrix(handle)
    print 'Reindex matrix:\n%d %d %d\n%d %d %d\n%d %d %d' % (rdx.elems)

    # Reindex the reflections
    Command.start('Reindexing reflections')
    cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(sgtbx.rot_mx(rdx.elems)))
    hkl = cb_op.apply(hkl)
    Command.end('Reindexed %d reflections' % len(hkl))

    # Create the reflection list
    Command.start('Creating reflection table')
    table = flex.reflection_table()
    table['id'] = flex.int(len(hkl), 0)
    table['panel'] = flex.size_t(len(hkl), 0)
    table['miller_index'] = hkl
    table['xyzcal.px'] = xyzcal
    table['xyzobs.px.value'] = xyzobs
    table['intensity.cor.value'] = iobs
    table['intensity.cor.variance'] = sigma**2
    table['intensity.prf.value'] = iobs * peak / rlp
    table['intensity.prf.variance'] = (sigma * peak / rlp)**2
    table['lp'] = 1.0 / rlp
    table['d'] = flex.double(uc.d(h) for h in hkl)
    Command.end('Created table with {0} reflections'.format(len(table)))

    # Output the table to pickle file
    if params.output.filename is None:
      params.output.filename = 'integrate_hkl.pickle'
    Command.start('Saving reflection table to %s' % params.output.filename)
    table.as_pickle(params.output.filename)
    Command.end('Saved reflection table to %s' % params.output.filename)
Esempio n. 16
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])
Esempio n. 17
0
def exercise_comprehensive(args):
    if ("--verbose" in args):
        out = sys.stdout
    else:
        out = StringIO()
    if ("--paranoid" in args):
        cb_range = 2
    else:
        cb_range = 1
    for symbol in bravais_types.acentric:
        print("bravais type:", symbol)
        sym = sgtbx.space_group_info(symbol=symbol) \
          .any_compatible_crystal_symmetry(volume=1000) \
          .niggli_cell()
        abc = list(sym.unit_cell().parameters()[:3])
        abc.sort()
        for cb_elements in flex.nested_loop([-cb_range] * 9,
                                            [cb_range + 1] * 9):
            r = sgtbx.rot_mx(cb_elements)
            if (r.determinant() != 1): continue
            cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(r))
            sym_cb = sym.change_basis(cb_op)
            abc_cb = list(sym_cb.unit_cell().parameters()[:3])
            abc_cb.sort()
            for x, y in zip(abc, abc_cb):
                assert y - x > -1.e-6
                if (y - x > 1.e-4): break
            else:
                print("cb_ob:", cb_op.c(), cb_elements, file=out)
                assert min(cb_elements) >= -1
                assert max(cb_elements) <= 1
                for s in sym_cb.space_group():
                    assert s.r().den() == 1
                    r_num = s.r().num()
                    print("r:", r_num, file=out)
                    assert min(r_num) >= -1
                    assert max(r_num) <= 1
                for enforce in [False, True]:
                    lattice_group = sgtbx.lattice_symmetry.group(
                        reduced_cell=sym_cb.unit_cell(),
                        max_delta=1.4,
                        enforce_max_delta_for_generated_two_folds=enforce)
                assert lattice_group == sym_cb.space_group()
                sys.stdout.flush()
        print(file=out)
Esempio n. 18
0
def derive_change_of_basis_op(from_hkl, to_hkl):

    # exclude those reflections that we couldn't index
    sel = (to_hkl != (0, 0, 0)) & (from_hkl != (0, 0, 0))
    assert sel.count(True) >= 3  # need minimum of 3 equations ?
    to_hkl = to_hkl.select(sel)
    from_hkl = from_hkl.select(sel)

    # for each miller index, solve a system of linear equations to find the
    # change of basis operator
    h, k, l = to_hkl.as_vec3_double().parts()

    r = []
    from scitbx.lstbx import normal_eqns

    for i in range(3):
        eqns = normal_eqns.linear_ls(3)
        for index, hkl in zip((h, k, l)[i], from_hkl):
            eqns.add_equation(
                right_hand_side=index, design_matrix_row=flex.double(hkl), weight=1
            )
        eqns.solve()
        r.extend(eqns.solution())

    from scitbx import matrix
    from scitbx.math import continued_fraction

    denom = 12
    r = [
        int(denom * continued_fraction.from_real(r_, eps=1e-2).as_rational())
        for r_ in r
    ]
    r = matrix.sqr(r).transpose()
    # print (1/denom)*r

    # now convert into a cctbx change_of_basis_op object
    change_of_basis_op = sgtbx.change_of_basis_op(
        sgtbx.rt_mx(sgtbx.rot_mx(r, denominator=denom))
    ).inverse()
    print("discovered change_of_basis_op=%s" % (str(change_of_basis_op)))

    # sanity check that this is the right cb_op
    assert (change_of_basis_op.apply(from_hkl) == to_hkl).count(False) == 0

    return change_of_basis_op
def exercise_comprehensive(args):
  if ("--verbose" in args):
    out = sys.stdout
  else:
    out = StringIO()
  if ("--paranoid" in args):
    cb_range = 2
  else:
    cb_range = 1
  for symbol in bravais_types.acentric:
    print "bravais type:", symbol
    sym = sgtbx.space_group_info(symbol=symbol) \
      .any_compatible_crystal_symmetry(volume=1000) \
      .niggli_cell()
    abc = list(sym.unit_cell().parameters()[:3])
    abc.sort()
    for cb_elements in flex.nested_loop([-cb_range]*9,[cb_range+1]*9):
      r = sgtbx.rot_mx(cb_elements)
      if (r.determinant() != 1): continue
      cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(r))
      sym_cb = sym.change_basis(cb_op)
      abc_cb = list(sym_cb.unit_cell().parameters()[:3])
      abc_cb.sort()
      for x,y in zip(abc, abc_cb):
        assert y-x > -1.e-6
        if (y-x > 1.e-4): break
      else:
        print >> out, "cb_ob:", cb_op.c(), cb_elements
        assert min(cb_elements) >= -1
        assert max(cb_elements) <= 1
        for s in sym_cb.space_group():
          assert s.r().den() == 1
          r_num = s.r().num()
          print >> out, "r:", r_num
          assert min(r_num) >= -1
          assert max(r_num) <= 1
        for enforce in [False, True]:
          lattice_group = sgtbx.lattice_symmetry.group(
            reduced_cell=sym_cb.unit_cell(),
            max_delta=1.4,
            enforce_max_delta_for_generated_two_folds=enforce)
        assert lattice_group == sym_cb.space_group()
        sys.stdout.flush()
    print >> out
Esempio n. 20
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])
Esempio n. 21
0
def derive_change_of_basis_op(from_hkl, to_hkl):

  # exclude those reflections that we couldn't index
  sel = (to_hkl != (0,0,0)) & (from_hkl != (0,0,0))
  assert sel.count(True) >= 3 # need minimum of 3 equations ?
  to_hkl = to_hkl.select(sel)
  from_hkl = from_hkl.select(sel)

  # for each miller index, solve a system of linear equations to find the
  # change of basis operator
  h, k, l = to_hkl.as_vec3_double().parts()

  r = []
  from scitbx.lstbx import normal_eqns
  for i in range(3):
    eqns = normal_eqns.linear_ls(3)
    for index, hkl in zip((h,k,l)[i], from_hkl):
      eqns.add_equation(right_hand_side=index,
                        design_matrix_row=flex.double(hkl),
                        weight=1)
    eqns.solve()
    r.extend(eqns.solution())

  from scitbx.math import continued_fraction
  from scitbx import matrix
  denom = 12
  r = [int(denom * continued_fraction.from_real(r_, eps=1e-2).as_rational())
       for r_ in r]
  r = matrix.sqr(r).transpose()
  #print (1/denom)*r

  # now convert into a cctbx change_of_basis_op object
  change_of_basis_op = sgtbx.change_of_basis_op(
    sgtbx.rt_mx(sgtbx.rot_mx(r, denominator=denom))).inverse()
  print "discovered change_of_basis_op=%s" %(str(change_of_basis_op))

  # sanity check that this is the right cb_op
  assert (change_of_basis_op.apply(from_hkl) == to_hkl).count(False) == 0

  return change_of_basis_op
Esempio n. 22
0
 def __init__(self, lf):
     self.symbol = None
     l = lf.next()
     print l.strip()
     if (lf.eof): return
     no, number, symbol, m = l.split()
     assert no == "NO."
     number = int(number)
     assert int(float(m)) == float(m)
     m = int(float(m))
     space_group = sgtbx.space_group()
     if (m < 0):
         space_group.expand_inv(sgtbx.tr_vec((0, 0, 0)))
     space_group.expand_conventional_centring_type(symbol[0])
     t_den = space_group.t_den()
     matrices = []
     for i in xrange(abs(m)):
         l = lf.next()
         assert not lf.eof
         flds = l.split()
         assert len(flds) == 12
         r = [int(e) for e in flds[1:4] + flds[5:8] + flds[9:12]]
         t = [
             int(round(float(e) * t_den))
             for e in (flds[0], flds[4], flds[8])
         ]
         try:
             s = sgtbx.rt_mx(sgtbx.rot_mx(r), sgtbx.tr_vec(t))
         except RuntimeError, e:
             print e
             print l
         else:
             try:
                 matrices.append(s)
                 space_group.expand_smx(s)
             except RuntimeError, e:
                 print e
                 print l
                 print s
def run():
  lines = iter(matrices_from_email.splitlines())
  while True:
    for line in lines:
      if (line.rstrip() == "S:"): break
    else:
      break
    s = []
    for i in xrange(3):
      s.extend([float(v) for v in lines.next().split()])
    for line in lines:
      if (line.rstrip() == "M:"): break
    else:
      raise RuntimeError("S: found but not M:")
    m = []
    for i in xrange(3):
      m.extend([int(v) for v in lines.next().split()])
    for line in lines:
      if (line.rstrip() == "Score:"): break
    else:
      raise RuntimeError("S: and M: found but not Score:")
    score = float(lines.next().strip())
    s = matrix.sqr(s)
    m = matrix.sqr(m)
    print s.mathematica_form(label="s", one_row_per_line=True)
    print m.mathematica_form(label="m", one_row_per_line=True)
    r = sgtbx.rot_mx(m, 1)
    print "rotation type:", r.info().type()
    print "axis direction:", r.info().ev()
    u = uctbx.unit_cell(metrical_matrix=s.as_sym_mat3())
    p = r.lebedev_2005_perturbation(reduced_cell=u)
    print "score given:     ", score
    print "score reproduced:", p
    assert abs(p-score) < 1.e-6
    delta = r.le_page_1982_delta(reduced_cell=u)
    print "Le Page delta:   ", delta
    print
  print "OK"
def run():
  lines = iter(matrices_from_email.splitlines())
  while True:
    for line in lines:
      if (line.rstrip() == "S:"): break
    else:
      break
    s = []
    for i in range(3):
      s.extend([float(v) for v in lines.next().split()])
    for line in lines:
      if (line.rstrip() == "M:"): break
    else:
      raise RuntimeError("S: found but not M:")
    m = []
    for i in range(3):
      m.extend([int(v) for v in lines.next().split()])
    for line in lines:
      if (line.rstrip() == "Score:"): break
    else:
      raise RuntimeError("S: and M: found but not Score:")
    score = float(lines.next().strip())
    s = matrix.sqr(s)
    m = matrix.sqr(m)
    print(s.mathematica_form(label="s", one_row_per_line=True))
    print(m.mathematica_form(label="m", one_row_per_line=True))
    r = sgtbx.rot_mx(m, 1)
    print("rotation type:", r.info().type())
    print("axis direction:", r.info().ev())
    u = uctbx.unit_cell(metrical_matrix=s.as_sym_mat3())
    p = r.lebedev_2005_perturbation(reduced_cell=u)
    print("score given:     ", score)
    print("score reproduced:", p)
    assert abs(p-score) < 1.e-6
    delta = r.le_page_1982_delta(reduced_cell=u)
    print("Le Page delta:   ", delta)
    print()
  print("OK")
 def __init__(self, lf):
   self.symbol = None
   l = lf.next()
   print l.strip()
   if (lf.eof): return
   no, number, symbol, m = l.split()
   assert no == "NO."
   number = int(number)
   assert int(float(m)) == float(m)
   m = int(float(m))
   space_group = sgtbx.space_group()
   if (m < 0):
     space_group.expand_inv(sgtbx.tr_vec((0,0,0)))
   space_group.expand_conventional_centring_type(symbol[0])
   t_den = space_group.t_den()
   matrices = []
   for i in xrange(abs(m)):
     l = lf.next()
     assert not lf.eof
     flds = l.split()
     assert len(flds) == 12
     r = [int(e) for e in flds[1:4] + flds[5:8] + flds[9:12]]
     t = [int(round(float(e)*t_den)) for e in (flds[0],flds[4],flds[8])]
     try:
       s = sgtbx.rt_mx(sgtbx.rot_mx(r), sgtbx.tr_vec(t))
     except RuntimeError, e:
       print e
       print l
     else:
       try:
         matrices.append(s)
         space_group.expand_smx(s)
       except RuntimeError, e:
         print e
         print l
         print s
Esempio n. 26
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
Esempio n. 27
0
    def __call__(self, params, options):
        ''' Import the integrate.hkl file. '''

        from iotbx.xds import integrate_hkl
        from dials.array_family import flex
        from dials.util.command_line import Command
        from cctbx import sgtbx

        # Get the unit cell to calculate the resolution
        uc = self._experiment.crystal.get_unit_cell()

        # Read the INTEGRATE.HKL file
        Command.start('Reading INTEGRATE.HKL')
        handle = integrate_hkl.reader()
        handle.read_file(self._integrate_hkl)
        hkl = flex.miller_index(handle.hkl)
        xyzcal = flex.vec3_double(handle.xyzcal)
        xyzobs = flex.vec3_double(handle.xyzobs)
        iobs = flex.double(handle.iobs)
        sigma = flex.double(handle.sigma)
        rlp = flex.double(handle.rlp)
        peak = flex.double(handle.peak) * 0.01
        if len(handle.iseg):
            panel = flex.size_t(handle.iseg) - 1
        else:
            panel = flex.size_t(len(hkl), 0)
        Command.end('Read %d reflections from INTEGRATE.HKL file.' % len(hkl))

        if len(self._experiment.detector) > 1:
            for p_id, p in enumerate(self._experiment.detector):
                sel = (panel == p_id)
                offset = p.get_raw_image_offset()
                xyzcal.set_selected(
                    sel,
                    xyzcal.select(sel) - (offset[0], offset[1], 0))
                xyzobs.set_selected(
                    sel,
                    xyzobs.select(sel) - (offset[0], offset[1], 0))

        # Derive the reindex matrix
        rdx = self.derive_reindex_matrix(handle)
        print('Reindex matrix:\n%d %d %d\n%d %d %d\n%d %d %d' % (rdx.elems))

        # Reindex the reflections
        Command.start('Reindexing reflections')
        cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(sgtbx.rot_mx(rdx.elems)))
        hkl = cb_op.apply(hkl)
        Command.end('Reindexed %d reflections' % len(hkl))

        # Create the reflection list
        Command.start('Creating reflection table')
        table = flex.reflection_table()
        table['id'] = flex.int(len(hkl), 0)
        table['panel'] = panel
        table['miller_index'] = hkl
        table['xyzcal.px'] = xyzcal
        table['xyzobs.px.value'] = xyzobs
        table['intensity.cor.value'] = iobs
        table['intensity.cor.variance'] = sigma**2
        table['intensity.prf.value'] = iobs * peak / rlp
        table['intensity.prf.variance'] = (sigma * peak / rlp)**2
        table['lp'] = 1.0 / rlp
        table['d'] = flex.double(uc.d(h) for h in hkl)
        Command.end('Created table with {0} reflections'.format(len(table)))

        # Output the table to pickle file
        if params.output.filename is None:
            params.output.filename = 'integrate_hkl.pickle'
        Command.start('Saving reflection table to %s' % params.output.filename)
        table.as_pickle(params.output.filename)
        Command.end('Saved reflection table to %s' % params.output.filename)
Esempio n. 28
0
    def __init__(self,
                 xs_a,
                 xs_b,
                 max_delta=2.0,
                 out=None,
                 relative_length_tolerance=0.05,
                 absolute_angle_tolerance=10.0,
                 order=1):

        self.relative_length_tolerance = relative_length_tolerance
        self.absolute_angle_tolerance = absolute_angle_tolerance
        self.order = order

        self.max_delta = max_delta
        self.out = out
        if self.out is None:
            self.out = sys.stdout

        self.xs_a = xs_a
        self.xs_b = xs_b
        # Go to niggli cell
        self.xs_a_n = self.xs_a.niggli_cell()
        self.xs_b_n = self.xs_b.niggli_cell()

        if (self.xs_a.niggli_cell().unit_cell().volume() >
                self.xs_b.niggli_cell().unit_cell().volume()):
            self.xs_a = xs_b
            self.xs_b = xs_a
            # go to niggli cell please
            self.xs_a_n = self.xs_a.niggli_cell()
            self.xs_b_n = self.xs_b.niggli_cell()

        volume_ratio = self.xs_b_n.unit_cell().volume(
        ) / self.xs_a_n.unit_cell().volume()
        n_volume_ratio = math.floor(volume_ratio + 1)

        self.show_basic_info()

        self.basis_a = matrix.sqr(
            self.xs_a_n.unit_cell().orthogonalization_matrix())
        print("Cartesian basis (column) vectors of lego cell:", file=self.out)
        tmp_bas = self.basis_a.as_list_of_lists()
        print("  / %5.1f %5.1f %5.1f \\  " %
              (tmp_bas[0][0], tmp_bas[0][1], tmp_bas[0][2]),
              file=self.out)
        print("  | %5.1f %5.1f %5.1f |  " %
              (tmp_bas[1][0], tmp_bas[1][1], tmp_bas[1][2]),
              file=self.out)
        print("  \\ %5.1f %5.1f %5.1f /  " %
              (tmp_bas[2][0], tmp_bas[2][1], tmp_bas[2][2]),
              file=self.out)
        print(file=self.out)
        self.basis_b = matrix.sqr(
            self.xs_b_n.unit_cell().orthogonalization_matrix())
        print("Cartesian basis (column) vectors of target cell:",
              file=self.out)
        tmp_bas = self.basis_b.as_list_of_lists()
        print("  / %5.1f %5.1f %5.1f \\  " %
              (tmp_bas[0][0], tmp_bas[0][1], tmp_bas[0][2]),
              file=self.out)
        print("  | %5.1f %5.1f %5.1f |  " %
              (tmp_bas[1][0], tmp_bas[1][1], tmp_bas[1][2]),
              file=self.out)
        print("  \\ %5.1f %5.1f %5.1f /  " %
              (tmp_bas[2][0], tmp_bas[2][1], tmp_bas[2][2]),
              file=self.out)
        print(file=self.out)

        self.lattice_symm_a = sgtbx.lattice_symmetry.group(
            self.xs_a_n.unit_cell(), self.max_delta)
        self.lattice_symm_b = sgtbx.lattice_symmetry.group(
            self.xs_b_n.unit_cell(), self.max_delta)

        self.xs_ref = crystal.symmetry(unit_cell=self.xs_b_n.unit_cell(),
                                       space_group=self.lattice_symm_b,
                                       assert_is_compatible_unit_cell=False)

        # make a list of matrices
        self.matrix_list = generate_matrix_up_to_order(
            n_volume_ratio, max(n_volume_ratio - 1, 1))
        self.uc_and_symm_list = []
        self.possible_solutions = []
        print(
            "A total of %4i matrices in the hermite normal form have been generated."
            % (len(self.matrix_list)),
            file=self.out)
        print("The volume changes they cause lie between %4i and %4i." %
              (n_volume_ratio, max(n_volume_ratio - 1, 1)),
              file=self.out)

        count = 0

        print(file=self.out)
        print("Trying all matrices", file=self.out)
        print(file=self.out)
        print("   1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0", file=self.out)
        print("  ", end=' ', file=self.out)
        for mat in self.matrix_list:
            count += 1
            found_it = False
            tmp_xs = self.make_new_cell_and_symmetry(mat)

            self.uc_and_symm_list.append(tmp_xs)

            tmp_gen = self.xs_b_n.unit_cell().similarity_transformations(
                tmp_xs[2].unit_cell(), self.relative_length_tolerance,
                self.absolute_angle_tolerance, self.order)

            if tmp_gen.size() > 0:
                found_it = True
                cb_op = sgtbx.change_of_basis_op(
                    sgtbx.rt_mx(sgtbx.rot_mx(tmp_gen[0]))).inverse()
                tmp_sol = (mat, cb_op, tmp_xs[2].change_basis(cb_op), tmp_xs[3]
                           )  # matrix, corresponding cb_op, cell + lattice sym
                self.possible_solutions.append(tmp_sol)
                #self.show_solution(tmp_sol)
            if found_it:
                print("*", end=' ', file=self.out)
                self.out.flush()
            else:
                if count % 10 == 0:
                    print("|", end=' ', file=self.out)
                    self.out.flush()
                else:
                    print(".", end=' ', file=self.out)
                    self.out.flush()
            if count % 20 == 0:
                print(file=self.out)
                print("  ", end=' ', file=self.out)
                self.out.flush()

        print(file=self.out)
        print(" Listing all possible solutions", file=self.out)
        count = 0
        if len(self.possible_solutions) == 0:
            print(file=out)
            print(
                "No relations found for this particular sublattice of the specified target cell.",
                file=out)
            print("   ", file=out)
            print(file=out)

        for tmp_sol in self.possible_solutions:
            count += 1
            print(file=self.out)
            print("Solution %4i" % (count), file=self.out)
            self.show_solution(tmp_sol)
Esempio n. 29
0
 def __init__(self, lf):
     self.symbol = None
     l = next(lf)
     print(l.strip())
     if (lf.eof): return
     no, number, symbol, m = l.split()
     assert no == "NO."
     number = int(number)
     assert int(float(m)) == float(m)
     m = int(float(m))
     space_group = sgtbx.space_group()
     if (m < 0):
         space_group.expand_inv(sgtbx.tr_vec((0, 0, 0)))
     space_group.expand_conventional_centring_type(symbol[0])
     t_den = space_group.t_den()
     matrices = []
     for i in range(abs(m)):
         l = next(lf)
         assert not lf.eof
         flds = l.split()
         assert len(flds) == 12
         r = [int(e) for e in flds[1:4] + flds[5:8] + flds[9:12]]
         t = [
             int(round(float(e) * t_den))
             for e in (flds[0], flds[4], flds[8])
         ]
         try:
             s = sgtbx.rt_mx(sgtbx.rot_mx(r), sgtbx.tr_vec(t))
         except RuntimeError as e:
             print(e)
             print(l)
         else:
             try:
                 matrices.append(s)
                 space_group.expand_smx(s)
             except RuntimeError as e:
                 print(e)
                 print(l)
                 print(s)
     space_group_info = sgtbx.space_group_info(group=space_group)
     if (space_group_info.type().number() != number):
         print("Space group number mismatch:")
         print("   from file:", number)
         print("  operations:", space_group_info.type().number())
         for s in matrices:
             space_group = sgtbx.space_group_info(symbol=number).group()
             order_z = space_group.order_z()
             space_group.expand_smx(s)
             if (space_group.order_z() != order_z):
                 print("  misfit:", s)
         space_group = sgtbx.space_group_info(symbol=number).group()
         for i_smx in range(space_group.n_smx()):
             OK = False
             for i_inv in range(space_group.f_inv()):
                 for i_ltr in range(space_group.n_ltr()):
                     sg = space_group(i_ltr, i_inv, i_smx).mod_positive()
                     for sm in matrices:
                         sm = sm.mod_positive()
                         if (sm == sg):
                             OK = True
                             break
                     if (OK): break
                 if (OK): break
             if (not OK):
                 print("  missing:", sg)
     self.number = number
     self.symbol = symbol
     self.space_group_info = space_group_info
def exercise_change_basis():
    assert approx_equal(O1.unit_cell().parameters(),
                        (47.659, 47.6861, 49.6444, 62.9615, 73.8222, 73.5269),
                        1E-3)
    reindex = (0.0, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, -1.0
               )  # swap a & b and take inverse
    O2 = O1.change_basis(reindex)
    assert approx_equal(O2.unit_cell().parameters(),
                        (47.6861, 47.659, 49.6444, 73.8222, 62.9615, 73.5269),
                        1E-3)

    rhombohedral_test = crystal_orientation(
        (0.002737747939994224, -0.0049133768326561278, 0.0023634556852316566,
         0.0062204242383498082, 0.006107332242442573, 0.0047036576949967112,
         -0.0057640198753891566, -0.0025891042237382953,
         0.0023674924674260264), basis_type.reciprocal)
    rhombohedral_reference = crystal_orientation(
        (-0.0076361080646872997, 0.0049061665572297979, 0.0023688116121433865,
         -0.00011109895272056645, -0.0061110173438898583,
         0.0047062769738302939, 0.0031790372319626674, 0.0025876279220667518,
         0.0023669727051432361), basis_type.reciprocal)
    # Find a similarity transform that maps the two cells onto each other
    c_inv_r_best = rhombohedral_test.best_similarity_transformation(
        other=rhombohedral_reference,
        fractional_length_tolerance=1.00,
        unimodular_generator_range=1)
    c_inv_r_int = tuple([int(round(ij, 0)) for ij in c_inv_r_best])
    assert c_inv_r_int == (-1, 0, 0, 1, -1, 0, 0, 0, 1)
    c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r_int))
    cb_op = sgtbx.change_of_basis_op(c_inv)
    rhombohedral_reindex = rhombohedral_test.change_basis(cb_op)
    assert rhombohedral_reindex.difference_Z_score(
        rhombohedral_reference) < 0.40
    assert rhombohedral_reindex.direct_mean_square_difference(
        rhombohedral_reference) < 0.1

    #an alternative test from ana that should fail (gives high msd~0.22; cell axes don't match):
    ana_reference = crystal_orientation(
        (0.0023650364919947241, 0.012819317075171401, 0.003042762222847376,
         0.0081242553464681254, 0.0050052660206998077, -0.01472465697193685,
         -0.01373896574061278, 0.0083781530252581681, -0.0035301340829149005),
        basis_type.reciprocal)
    ana_current = crystal_orientation(
        (-0.014470153848927263, 0.0095185368147633793, 0.00087746490483763798,
         -0.0049989006573928716, -0.0079714727432991222, 0.014778692772530192,
         0.0051268914129933571, 0.010264066188909109, 0.0044244589492769002),
        basis_type.reciprocal)
    c_inv_r_best = ana_current.best_similarity_transformation(
        other=ana_reference,
        fractional_length_tolerance=200.0,
        unimodular_generator_range=1)
    c_inv_r_int = tuple([int(round(ij, 0)) for ij in c_inv_r_best])
    c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r_int))
    cb_op = sgtbx.change_of_basis_op(c_inv)
    ana_reindex = ana_reference.change_basis(cb_op.inverse())
    assert 200.0 > ana_reindex.difference_Z_score(ana_current) > 20.

    u = uctbx.unit_cell((10., 10., 10., 90., 90., 90.))
    CO = crystal_orientation(u.fractionalization_matrix(), True)
    assert approx_equal(
        CO.unit_cell().parameters(),
        CO.change_basis((1, 0, 0, 0, 1, 0, 0, 0, 1)).unit_cell().parameters())
    u = uctbx.unit_cell((2, 3, 5))
    CO = crystal_orientation(u.fractionalization_matrix(), True)
    assert approx_equal(
        CO.change_basis((0, 1, 0, 0, 0, 1, 1, 0, 0)).unit_cell().parameters(),
        (5, 2, 3, 90, 90, 90))
    cb_op = sgtbx.change_of_basis_op("y,z,x")
    assert approx_equal(
        CO.change_basis(cb_op).unit_cell().parameters(), (5, 2, 3, 90, 90, 90))

    import scitbx.math
    from scitbx import matrix
    fmx = matrix.sqr(
        uctbx.unit_cell((10, 13, 17, 85, 95, 105)).fractionalization_matrix())
    crm = matrix.sqr(
        scitbx.math.r3_rotation_axis_and_angle_as_matrix(axis=(-3, 5, -7),
                                                         angle=37,
                                                         deg=True))
    co = crystal_orientation(crm * fmx.transpose(), True)
    assert approx_equal(co.crystal_rotation_matrix(), crm)
Esempio n. 31
0
 def filtered_commands(self):
   from scitbx.math import continued_fraction
   temperature = 20
   for command, line in self.command_stream:
     cmd, args = command[0], command[-1]
     n_args = len(args)
     if cmd == 'OMIT':
       if n_args == 3 and isinstance(args[0], float):
         self.instructions.setdefault('omit_hkl', [])
         self.instructions['omit_hkl'].append([int(i) for i in args])
       elif n_args == 2 and isinstance(args[0], float):
         self.instructions['omit'] = {
           's': args[0],
           'two_theta': args[1]}
       else:
         yield command, line
     elif cmd == 'SHEL':
       if args:
         shel = {'lowres': args[0]}
         if n_args > 1:
           shel['highres'] = args[1]
     elif cmd == 'MERG':
       if args:
         self.instructions['merg'] = args[0]
     elif cmd == 'TEMP':
       if len(args) > 1:
         raise shelx_error("TEMP takes at most 1 argument")
       if args: temperature = args[0]
       self.instructions['temp'] = temperature
       self.builder.temperature_in_celsius = temperature
     elif cmd == 'WGHT':
       if n_args > 6:
         raise shelx_error("Too many argument for %s" % cmd, line)
       default_weighting_scheme = { 'a': 0.1,
                                    'b': 0,
                                    'c': 0,
                                    'd': 0,
                                    'e': 0,
                                    'f': 1/3 }
       weighting_scheme = dict([
         (key, (arg is not None and arg) or default_weighting_scheme[key])
         for key, arg in itertools.izip_longest('abcdef', args) ])
       self.instructions['wght'] = weighting_scheme
       self.builder.make_shelx_weighting_scheme(**weighting_scheme)
     elif cmd == 'HKLF':
       # only ONE HKLF instruction allowed
       assert 'hklf' not in self.instructions
       hklf = {'s': 1, 'matrix': sgtbx.rot_mx()}
       hklf['n'] = args[0]
       if n_args > 1:
         hklf['s'] = args[1]
         if n_args > 2:
           assert n_args > 10
           mx = [ continued_fraction.from_real(e, eps=1e-3).as_rational()
                  for e in args[2:11] ]
           den = reduce(rational.lcm, [ r.denominator() for r in mx ])
           nums = [ r.numerator()*(den//r.denominator()) for r in mx ]
           hklf['matrix'] = sgtbx.rot_mx(nums, den)
           if n_args > 11:
             hklf['wt'] = args[11]
             if n_args == 13:
               hklf['m'] = args[12]
       self.instructions['hklf'] = hklf
       assert not self.builder or ('wt' not in hklf and 'm' not in hklf)
       self.builder.create_shelx_reflection_data_source(
         format=hklf['n'],
         indices_transform=hklf['matrix'],
         data_scale=hklf['s'])
       if 'basf' in self.instructions and hklf['n'] == 5:
         self.builder.make_non_merohedral_twinning_with_transformed_hkl(
           fractions=self.instructions['basf'])
     elif cmd == 'TWIN':
       # only ONE twin instruction allowed
       assert 'twin' not in self.instructions
       twin = {}
       if n_args > 0:
         assert n_args >= 9
         twin['matrix'] = sgtbx.rt_mx(
           sgtbx.rot_mx([int(i) for i in args[0:9]]))
         if n_args > 9:
           twin['n'] = args[9]
       self.instructions['twin'] = twin
       if 'basf' in self.instructions: self.issue_merohedral_twinning()
     elif cmd == 'BASF':
       self.instructions['basf'] = args
       if 'twin' in self.instructions: self.issue_merohedral_twinning()
     elif cmd == 'EXTI':
       if len(args) == 1:
         self.instructions['exti'] = args[0]
     else:
       yield command, line
   else:
     # All token have been read without errors or early bailout
     assert 'hklf' in self.instructions, "Missing HKLF instruction"
Esempio n. 32
0
    def __init__(
        self,
        xs_a,
        xs_b,
        max_delta=2.0,
        out=None,
        relative_length_tolerance=0.05,
        absolute_angle_tolerance=10.0,
        order=1,
    ):

        self.relative_length_tolerance = relative_length_tolerance
        self.absolute_angle_tolerance = absolute_angle_tolerance
        self.order = order

        self.max_delta = max_delta
        self.out = out
        if self.out is None:
            self.out = sys.stdout

        self.xs_a = xs_a
        self.xs_b = xs_b
        # Go to niggli cell
        self.xs_a_n = self.xs_a.niggli_cell()
        self.xs_b_n = self.xs_b.niggli_cell()

        if self.xs_a.niggli_cell().unit_cell().volume() > self.xs_b.niggli_cell().unit_cell().volume():
            self.xs_a = xs_b
            self.xs_b = xs_a
            # go to niggli cell please
            self.xs_a_n = self.xs_a.niggli_cell()
            self.xs_b_n = self.xs_b.niggli_cell()

        volume_ratio = self.xs_b_n.unit_cell().volume() / self.xs_a_n.unit_cell().volume()
        n_volume_ratio = math.floor(volume_ratio + 1)

        self.show_basic_info()

        self.basis_a = matrix.sqr(self.xs_a_n.unit_cell().orthogonalization_matrix())
        print >> self.out, "Cartesian basis (column) vectors of lego cell:"
        tmp_bas = self.basis_a.as_list_of_lists()
        print >> self.out, "  / %5.1f %5.1f %5.1f \  " % (tmp_bas[0][0], tmp_bas[0][1], tmp_bas[0][2])
        print >> self.out, "  | %5.1f %5.1f %5.1f |  " % (tmp_bas[1][0], tmp_bas[1][1], tmp_bas[1][2])
        print >> self.out, "  \ %5.1f %5.1f %5.1f /  " % (tmp_bas[2][0], tmp_bas[2][1], tmp_bas[2][2])
        print >> self.out
        self.basis_b = matrix.sqr(self.xs_b_n.unit_cell().orthogonalization_matrix())
        print >> self.out, "Cartesian basis (column) vectors of target cell:"
        tmp_bas = self.basis_b.as_list_of_lists()
        print >> self.out, "  / %5.1f %5.1f %5.1f \  " % (tmp_bas[0][0], tmp_bas[0][1], tmp_bas[0][2])
        print >> self.out, "  | %5.1f %5.1f %5.1f |  " % (tmp_bas[1][0], tmp_bas[1][1], tmp_bas[1][2])
        print >> self.out, "  \ %5.1f %5.1f %5.1f /  " % (tmp_bas[2][0], tmp_bas[2][1], tmp_bas[2][2])
        print >> self.out

        self.lattice_symm_a = sgtbx.lattice_symmetry.group(self.xs_a_n.unit_cell(), self.max_delta)
        self.lattice_symm_b = sgtbx.lattice_symmetry.group(self.xs_b_n.unit_cell(), self.max_delta)

        self.xs_ref = crystal.symmetry(
            unit_cell=self.xs_b_n.unit_cell(), space_group=self.lattice_symm_b, assert_is_compatible_unit_cell=False
        )

        # make a list of matrices
        self.matrix_list = generate_matrix_up_to_order(n_volume_ratio, max(n_volume_ratio - 1, 1))
        self.uc_and_symm_list = []
        self.possible_solutions = []
        print >> self.out, "A total of %4i matrices in the hermite normal form have been generated." % (
            len(self.matrix_list)
        )
        print >> self.out, "The volume changes they cause lie between %4i and %4i." % (
            n_volume_ratio,
            max(n_volume_ratio - 1, 1),
        )

        count = 0

        print >> self.out
        print >> self.out, "Trying all matrices"
        print >> self.out
        print >> self.out, "   1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0"
        print >> self.out, "  ",
        for mat in self.matrix_list:
            count += 1
            found_it = False
            tmp_xs = self.make_new_cell_and_symmetry(mat)

            self.uc_and_symm_list.append(tmp_xs)

            tmp_gen = self.xs_b_n.unit_cell().similarity_transformations(
                tmp_xs[2].unit_cell(), self.relative_length_tolerance, self.absolute_angle_tolerance, self.order
            )

            if tmp_gen.size() > 0:
                found_it = True
                cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(sgtbx.rot_mx(tmp_gen[0]))).inverse()
                tmp_sol = (
                    mat,
                    cb_op,
                    tmp_xs[2].change_basis(cb_op),
                    tmp_xs[3],
                )  # matrix, corresponding cb_op, cell + lattice sym
                self.possible_solutions.append(tmp_sol)
                # self.show_solution(tmp_sol)
            if found_it:
                print >> self.out, "*",
                self.out.flush()
            else:
                if count % 10 == 0:
                    print >> self.out, "|",
                    self.out.flush()
                else:
                    print >> self.out, ".",
                    self.out.flush()
            if count % 20 == 0:
                print >> self.out
                print >> self.out, "  ",
                self.out.flush()

        print >> self.out
        print >> self.out, " Listing all possible solutions"
        count = 0
        if len(self.possible_solutions) == 0:
            print >> out
            print >> out, "No relations found for this particular sublattice of the specified target cell."
            print >> out, "   "
            print >> out

        for tmp_sol in self.possible_solutions:
            count += 1
            print >> self.out
            print >> self.out, "Solution %4i" % (count)
            self.show_solution(tmp_sol)
Esempio n. 33
0
    def __call__(self, params, options):
        """Import the integrate.hkl file."""
        # Get the unit cell to calculate the resolution
        uc = self._experiment.crystal.get_unit_cell()

        # Read the INTEGRATE.HKL file
        Command.start("Reading INTEGRATE.HKL")
        handle = integrate_hkl.reader()
        handle.read_file(self._integrate_hkl)
        hkl = flex.miller_index(handle.hkl)
        xyzcal = flex.vec3_double(handle.xyzcal)
        xyzobs = flex.vec3_double(handle.xyzobs)
        iobs = flex.double(handle.iobs)
        sigma = flex.double(handle.sigma)
        rlp = flex.double(handle.rlp)
        peak = flex.double(handle.peak) * 0.01
        if len(handle.iseg):
            panel = flex.size_t(handle.iseg) - 1
        else:
            panel = flex.size_t(len(hkl), 0)
        Command.end(f"Read {len(hkl)} reflections from INTEGRATE.HKL file.")

        if len(self._experiment.detector) > 1:
            for p_id, p in enumerate(self._experiment.detector):
                sel = panel == p_id
                offset = p.get_raw_image_offset()
                xyzcal.set_selected(sel, xyzcal.select(sel) - (offset[0], offset[1], 0))
                xyzobs.set_selected(sel, xyzobs.select(sel) - (offset[0], offset[1], 0))

        # Derive the reindex matrix
        rdx = self.derive_reindex_matrix(handle)
        print("Reindex matrix:\n%d %d %d\n%d %d %d\n%d %d %d" % (rdx.elems))

        # Reindex the reflections
        Command.start("Reindexing reflections")
        cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx(sgtbx.rot_mx(rdx.elems)))
        hkl = cb_op.apply(hkl)
        Command.end(f"Reindexed {len(hkl)} reflections")

        # Create the reflection list
        Command.start("Creating reflection table")
        table = flex.reflection_table()
        table["id"] = flex.int(len(hkl), 0)
        table["panel"] = panel
        table["miller_index"] = hkl
        table["xyzcal.px"] = xyzcal
        table["xyzobs.px.value"] = xyzobs
        table["intensity.cor.value"] = iobs
        table["intensity.cor.variance"] = flex.pow2(sigma)
        table["intensity.prf.value"] = iobs * peak / rlp
        table["intensity.prf.variance"] = flex.pow2(sigma * peak / rlp)
        table["lp"] = 1.0 / rlp
        table["d"] = flex.double(uc.d(h) for h in hkl)
        Command.end(f"Created table with {len(table)} reflections")

        # Output the table to pickle file
        if params.output.filename is None:
            params.output.filename = "integrate_hkl.refl"
        Command.start(f"Saving reflection table to {params.output.filename}")
        table.as_file(params.output.filename)
        Command.end(f"Saved reflection table to {params.output.filename}")
Esempio n. 34
0
 def change_of_basis_op(self):
     from cctbx import sgtbx
     return sgtbx.change_of_basis_op(
         sgtbx.rt_mx(sgtbx.rot_mx(self._r_inv.elems, 1))).inverse()
Esempio n. 35
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
Esempio n. 36
0
  def __init__(self,
               xs1,
               xs2,
               relative_length_tolerance=0.05,
               absolute_angle_tolerance=10,
               max_delta=3.0,
               anomalous_flag=True,
               out=None):
    # first we have to go to the niggli setting
    self.cb_op_to_n_1 = xs1.change_of_basis_op_to_niggli_cell()
    self.cb_op_to_n_2 = xs2.change_of_basis_op_to_niggli_cell()

    nxs1 = xs1.change_basis( self.cb_op_to_n_1 )
    nxs2 = xs2.change_basis( self.cb_op_to_n_2 )

    # get the lattice symmetry please
    lat_sym_1 = lattice_symmetry.group( nxs1.unit_cell(), max_delta  )
    lat_sym_2 = lattice_symmetry.group( nxs2.unit_cell(), max_delta )
    # and the intensity symmetry please
    int_sym_1 = nxs1.reflection_intensity_symmetry( anomalous_flag ).space_group()
    int_sym_2 = nxs2.reflection_intensity_symmetry( anomalous_flag ).space_group()

    # Now we have to find a similarity transform that maps the two niggli cells onto each other
    c_inv_rs = nxs1.unit_cell().similarity_transformations(
      other=nxs2.unit_cell(),
      relative_length_tolerance=relative_length_tolerance,
      absolute_angle_tolerance=absolute_angle_tolerance)
    min_bases_msd = None
    self.similarity_cb_op = None

    for c_inv_r in c_inv_rs:
      # make the similarity transform into a cb_op
      c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r))
      cb_op = sgtbx.change_of_basis_op(c_inv).inverse()
      # compute the mean square difference for the bases
      bases_msd = nxs1.unit_cell() \
                  .bases_mean_square_difference(
        other=nxs2.unit_cell().change_basis(cb_op=cb_op))

      # and find cb_op correspondiong to the the minimum rmsd
      if (min_bases_msd is None
          or min_bases_msd > bases_msd):
        min_bases_msd = bases_msd
        self.similarity_cb_op = cb_op
    # if nothing is found, do not continue
    self.double_cosets = None
    if (self.similarity_cb_op is not None):
      # make the common lattice group please
      common_lattice_group = lat_sym_1
      for s in lat_sym_2.build_derived_acentric_group().change_basis(
        self.similarity_cb_op ):
        try: common_lattice_group.expand_smx(s)
        except RuntimeError:
          common_lattice_group=None
      if  common_lattice_group is not None:
        common_lattice_group.make_tidy()

        h1 = int_sym_1.build_derived_acentric_group().make_tidy()
        h2 = int_sym_2.build_derived_acentric_group().change_basis( self.similarity_cb_op ).make_tidy()

        # do the double coset decomposition
        self.double_cosets = cosets.double_cosets( common_lattice_group,
                                                   h1,
                                                   h2,
                                                   )
def exercise_change_basis():
  assert approx_equal(O1.unit_cell().parameters(),
                      (47.659, 47.6861, 49.6444, 62.9615, 73.8222, 73.5269),1E-3)
  reindex = (0.0, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, -1.0) # swap a & b and take inverse
  O2 = O1.change_basis(reindex)
  assert approx_equal(O2.unit_cell().parameters(),
                      (47.6861, 47.659, 49.6444, 73.8222, 62.9615, 73.5269),1E-3)

  rhombohedral_test = crystal_orientation((
    0.002737747939994224, -0.0049133768326561278, 0.0023634556852316566,
    0.0062204242383498082, 0.006107332242442573, 0.0047036576949967112,
    -0.0057640198753891566, -0.0025891042237382953, 0.0023674924674260264),basis_type.reciprocal)
  rhombohedral_reference = crystal_orientation((
    -0.0076361080646872997, 0.0049061665572297979, 0.0023688116121433865,
    -0.00011109895272056645, -0.0061110173438898583, 0.0047062769738302939,
     0.0031790372319626674, 0.0025876279220667518, 0.0023669727051432361),basis_type.reciprocal)
  # Find a similarity transform that maps the two cells onto each other
  c_inv_r_best = rhombohedral_test.best_similarity_transformation(
      other = rhombohedral_reference, fractional_length_tolerance = 1.00,
      unimodular_generator_range=1)
  c_inv_r_int = tuple([int(round(ij,0)) for ij in c_inv_r_best])
  assert c_inv_r_int == (-1, 0, 0, 1, -1, 0, 0, 0, 1)
  c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r_int))
  cb_op = sgtbx.change_of_basis_op(c_inv)
  rhombohedral_reindex = rhombohedral_test.change_basis(cb_op)
  assert rhombohedral_reindex.difference_Z_score(rhombohedral_reference) < 0.40
  assert rhombohedral_reindex.direct_mean_square_difference(rhombohedral_reference) < 0.1

  #an alternative test from ana that should fail (gives high msd~0.22; cell axes don't match):
  ana_reference = crystal_orientation((
    0.0023650364919947241, 0.012819317075171401, 0.003042762222847376,
    0.0081242553464681254, 0.0050052660206998077, -0.01472465697193685,
   -0.01373896574061278, 0.0083781530252581681, -0.0035301340829149005),basis_type.reciprocal)
  ana_current = crystal_orientation((
   -0.014470153848927263, 0.0095185368147633793, 0.00087746490483763798,
   -0.0049989006573928716, -0.0079714727432991222, 0.014778692772530192,
    0.0051268914129933571, 0.010264066188909109, 0.0044244589492769002),basis_type.reciprocal)
  c_inv_r_best = ana_current.best_similarity_transformation(
      other = ana_reference,
      fractional_length_tolerance = 200.0,
      unimodular_generator_range=1)
  c_inv_r_int = tuple([int(round(ij,0)) for ij in c_inv_r_best])
  c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r_int))
  cb_op = sgtbx.change_of_basis_op(c_inv)
  ana_reindex = ana_reference.change_basis(cb_op.inverse())
  assert 200.0 > ana_reindex.difference_Z_score(ana_current) > 20.

  u = uctbx.unit_cell((10., 10., 10., 90., 90., 90.))
  CO = crystal_orientation(u.fractionalization_matrix(),True)
  assert approx_equal(
    CO.unit_cell().parameters(),
    CO.change_basis((1,0,0, 0,1,0, 0,0,1)).unit_cell().parameters())
  u = uctbx.unit_cell((2,3,5))
  CO = crystal_orientation(u.fractionalization_matrix(),True)
  assert approx_equal(
    CO.change_basis((0,1,0, 0,0,1, 1,0,0)).unit_cell().parameters(),
    (5,2,3,90,90,90))
  cb_op = sgtbx.change_of_basis_op("y,z,x")
  assert approx_equal(
    CO.change_basis(cb_op).unit_cell().parameters(),
    (5,2,3,90,90,90))

  import scitbx.math
  from scitbx import matrix
  fmx = matrix.sqr(
    uctbx.unit_cell((10, 13, 17, 85, 95, 105)).fractionalization_matrix())
  crm = matrix.sqr(scitbx.math.r3_rotation_axis_and_angle_as_matrix(
    axis=(-3,5,-7), angle=37, deg=True))
  co = crystal_orientation(crm * fmx.transpose(), True)
  assert approx_equal(co.crystal_rotation_matrix(), crm)
Esempio n. 38
0
 def filtered_commands(self):
   from scitbx.math import continued_fraction
   temperature = 20
   for command, line in self.command_stream:
     cmd, args = command[0], command[-1]
     n_args = len(args)
     if cmd == 'OMIT':
       if n_args == 3 and isinstance(args[0], float):
         self.instructions.setdefault('omit_hkl', [])
         self.instructions['omit_hkl'].append([int(i) for i in args])
       elif n_args == 2 and isinstance(args[0], float):
         self.instructions['omit'] = {
           's': args[0],
           'two_theta': args[1]}
       else:
         yield command, line
     elif cmd == 'SHEL':
       if args:
         shel = {'lowres': args[0]}
         if n_args > 1:
           shel['highres'] = args[1]
     elif cmd == 'MERG':
       if args:
         self.instructions['merg'] = args[0]
     elif cmd == 'TEMP':
       if len(args) > 1:
         raise shelx_error("TEMP takes at most 1 argument")
       if args: temperature = args[0]
       self.instructions['temp'] = temperature
       self.builder.temperature_in_celsius = temperature
     elif cmd == 'WGHT':
       if n_args > 6:
         raise shelx_error("Too many argument for %s" % cmd, line)
       default_weighting_scheme = { 'a': 0.1,
                                    'b': 0,
                                    'c': 0,
                                    'd': 0,
                                    'e': 0,
                                    'f': 1/3 }
       weighting_scheme = dict([
         (key, (arg is not None and arg) or default_weighting_scheme[key])
         for key, arg in itertools.izip_longest('abcdef', args) ])
       self.instructions['wght'] = weighting_scheme
       self.builder.make_shelx_weighting_scheme(**weighting_scheme)
     elif cmd == 'HKLF':
       # only ONE HKLF instruction allowed
       assert 'hklf' not in self.instructions
       hklf = {'s': 1, 'matrix': sgtbx.rot_mx()}
       hklf['n'] = args[0]
       if n_args > 1:
         hklf['s'] = args[1]
         if n_args > 2:
           assert n_args > 10
           mx = [ continued_fraction.from_real(e, eps=1e-3).as_rational()
                  for e in args[2:11] ]
           den = reduce(rational.lcm, [ r.denominator() for r in mx ])
           nums = [ r.numerator()*(den//r.denominator()) for r in mx ]
           hklf['matrix'] = sgtbx.rot_mx(nums, den)
           if n_args > 11:
             hklf['wt'] = args[11]
             if n_args == 13:
               hklf['m'] = args[12]
       self.instructions['hklf'] = hklf
       assert not self.builder or ('wt' not in hklf and 'm' not in hklf)
       self.builder.create_shelx_reflection_data_source(
         format=hklf['n'],
         indices_transform=hklf['matrix'],
         data_scale=hklf['s'])
       if 'basf' in self.instructions and hklf['n'] == 5:
         self.builder.make_non_merohedral_twinning_with_transformed_hkl(
           fractions=self.instructions['basf'])
     elif cmd == 'TWIN':
       # only ONE twin instruction allowed
       assert 'twin' not in self.instructions
       twin = {}
       if n_args > 0:
         assert n_args >= 9
         twin['matrix'] = sgtbx.rt_mx(
           sgtbx.rot_mx([int(i) for i in args[0:9]]))
         if n_args > 9:
           twin['n'] = args[9]
       self.instructions['twin'] = twin
       if 'basf' in self.instructions: self.issue_merohedral_twinning()
     elif cmd == 'BASF':
       self.instructions['basf'] = args
       if 'twin' in self.instructions: self.issue_merohedral_twinning()
     elif cmd == 'EXTI':
       if len(args) == 1:
         self.instructions['exti'] = args[0]
     else:
       yield command, line
   else:
     # All token have been read without errors or early bailout
     assert 'hklf' in self.instructions, "Missing HKLF instruction"
Esempio n. 39
0
 def __init__(self):
   self.structure = xray.structure(
     crystal_symmetry=crystal.symmetry(
     unit_cell=(7.6338, 7.6338, 9.8699, 90, 90, 120),
     space_group_symbol='hall:  P 3 -2c'),
     scatterers=flex.xray_scatterer((
       xray.scatterer( #0
     label='LI',
     site=(0.032717, 0.241544, 0.254924),
     u=(0.000544, 0.000667, 0.000160,
        0.000326, 0.000072, -0.000030)),
       xray.scatterer( #1
     label='NA',
     site=(0.033809, 0.553123, 0.484646),
     u=(0.000554, 0.000731, 0.000174,
        0.000212, 0.000032, -0.000015)),
       xray.scatterer( #2
     label='S1',
     site=(0.000000, 0.000000, -0.005908),
     u=(0.000370, 0.000370, 0.000081,
        0.000185, 0.000000, 0.000000)),
       xray.scatterer( #3
     label='S2',
     site=(0.333333, 0.666667, 0.211587),
     u=(0.000244, 0.000244, 0.000148,
        0.000122, 0.000000, 0.000000)),
       xray.scatterer( #4
     label='S3',
     site=(0.666667, 0.333333, 0.250044),
     u=(0.000349, 0.000349, 0.000094,
        0.000174, 0.000000, 0.000000)),
       xray.scatterer( #5
     label='O1',
     site=(0.000000, -0.000000, 0.154207),
     u=(0.000360, 0.000360, 0.000149,
        0.000180, 0.000000, 0.000000),
     occupancy=0.999000),
       xray.scatterer( #6
     label='O2',
     site=(0.333333, 0.666667, 0.340665),
     u=(0.000613, 0.000613, 0.000128,
        0.000306, 0.000000, 0.000000)),
       xray.scatterer( #7
     label='O3',
     site=(0.666667, 0.333333, 0.112766),
     u=(0.000724, 0.000724, 0.000118,
        0.000362, 0.000000, 0.000000)),
       xray.scatterer( #8
     label='O4',
     site=(0.225316, 0.110088, -0.035765),
     u=(0.000477, 0.000529, 0.000213,
        0.000230, 0.000067, -0.000013)),
       xray.scatterer( #9
     label='O5',
     site=(0.221269, 0.442916, 0.153185),
     u=(0.000767, 0.000286, 0.000278,
        0.000210, 0.000016, -0.000082)),
       xray.scatterer( #10
     label='O6',
     site=(0.487243, 0.169031, 0.321690),
     u=(0.000566, 0.000582, 0.000354,
        0.000007, 0.000022, 0.000146))
     )))
   self.twin_laws = (sgtbx.rot_mx((0,1,0,1,0,0,0,0,-1)),)
   self.twin_fractions = flex.double((flex.random_double(),))
   self.scale_factor = 0.05 + 10 * flex.random_double()
   self.crystal_symmetry = self.structure.crystal_symmetry()
   mi = self.crystal_symmetry.build_miller_set(anomalous_flag=False,
                                               d_min=0.5)
   fo_sq = mi.structure_factors_from_scatterers(
     self.structure, algorithm="direct").f_calc().norm()
   fo_sq = fo_sq.customized_copy(
     data=fo_sq.data()*self.scale_factor,
     sigmas=flex.double(fo_sq.size(), 1))
   twin_completion = xray.twin_completion(
     mi.indices(),
     mi.space_group(),
     mi.anomalous_flag(),
     self.twin_laws[0].as_double())
   sigmas = flex.double(fo_sq.size(), 1)
   from cctbx import miller
   twin_complete_set = miller.set(
     crystal_symmetry=self.crystal_symmetry,
     indices=twin_completion.twin_complete(),
     anomalous_flag=mi.anomalous_flag()).map_to_asu()
   detwinner = xray.hemihedral_detwinner(
     hkl_obs=fo_sq.indices(),
     hkl_calc=twin_complete_set.indices(),
     space_group=mi.space_group(),
     anomalous_flag=mi.anomalous_flag(),
     twin_law=self.twin_laws[0].as_double())
   twinned_i, twinned_s = detwinner.twin_with_twin_fraction(
     fo_sq.data(),
     sigmas,
     twin_fraction=self.twin_fractions[0])
   self.fo_sq = fo_sq.customized_copy(data=twinned_i, sigmas=twinned_s)
def filter_known_symmetry(
    crystal_models,
    target_symmetry,
    relative_length_tolerance=0.1,
    absolute_angle_tolerance=5,
    max_delta=5,
):
    """Filter crystal models for known symmetry.

    Args:
        crystal_models (list): A list of :class:`dxtbx.model.Crystal` objects.
        target_symmetry (cctbx.crystal.symmetry): The target symmetry for filtering.
        relative_length_tolerance (float): Relative tolerance for unit cell lengths in
            unit cell comparision (default value is 0.1).
        absolute_angle_tolerance (float): Angular tolerance (in degrees) in unit cell
            comparison (default value is 5).
        max_delta (float): Maximum allowed Le Page delta used in searching for basis
            vector combinations that are consistent with the given symmetry (default
            value is 5).
    """

    cb_op_ref_to_primitive = target_symmetry.change_of_basis_op_to_primitive_setting(
    )

    if target_symmetry.unit_cell() is not None:
        target_symmetry_primitive = target_symmetry.change_basis(
            cb_op_ref_to_primitive)
        target_min_cell = target_symmetry_primitive.minimum_cell().unit_cell()
    else:
        target_symmetry_primitive = target_symmetry.customized_copy(
            space_group_info=target_symmetry.space_group_info().change_basis(
                cb_op_ref_to_primitive))
        target_min_cell = None

    transformations = [
        sgtbx.change_of_basis_op(
            str(sgtbx.rt_mx(sgtbx.rot_mx(T["trans"].transpose().as_int()))))
        for T in tools.R if T["mod"] < 5
    ]
    transformations = []  # XXX temporarily disable cell doubling checks
    transformations.insert(0, sgtbx.change_of_basis_op())

    for model in crystal_models:
        best_model = None
        for T in transformations:
            uc = model.get_unit_cell()
            det = T.c().r().determinant()
            if target_symmetry_primitive.unit_cell() is None:
                if det > 1:
                    break
            else:
                primitive_volume = target_symmetry_primitive.unit_cell(
                ).volume()
            if det > 1 and abs(uc.volume() / primitive_volume - det) < 1e-1:
                uc = uc.change_basis(T)
            best_subgroup = find_matching_symmetry(
                uc,
                target_symmetry_primitive.space_group(),
                max_delta=max_delta)
            cb_op_extra = None
            if best_subgroup is None:
                if not cb_op_ref_to_primitive.is_identity_op():
                    # if we have been told we have a centred unit cell check that
                    # indexing hasn't found the centred unit cell instead of the
                    # primitive cell
                    best_subgroup = find_matching_symmetry(
                        uc,
                        target_symmetry.space_group().
                        build_derived_point_group(),
                        max_delta=max_delta,
                    )
                    cb_op_extra = cb_op_ref_to_primitive
                    if best_subgroup is None:
                        continue
                else:
                    continue
            cb_op_inp_best = best_subgroup["cb_op_inp_best"]
            best_subsym = best_subgroup["best_subsym"]
            cb_op_best_ref = best_subsym.change_of_basis_op_to_reference_setting(
            )
            ref_subsym = best_subsym.change_basis(cb_op_best_ref)
            cb_op_ref_primitive = ref_subsym.change_of_basis_op_to_primitive_setting(
            )
            cb_op_to_primitive = (cb_op_ref_primitive * cb_op_best_ref *
                                  cb_op_inp_best * T)
            if cb_op_extra is not None:
                cb_op_to_primitive = cb_op_extra * cb_op_to_primitive
            best_model = model.change_basis(cb_op_to_primitive)

            if target_symmetry_primitive.unit_cell(
            ) is not None and not (best_model.get_unit_cell().is_similar_to(
                    target_symmetry_primitive.unit_cell(),
                    relative_length_tolerance=relative_length_tolerance,
                    absolute_angle_tolerance=absolute_angle_tolerance,
            ) or best_model.get_unit_cell().minimum_cell().is_similar_to(
                    target_min_cell,
                    relative_length_tolerance=relative_length_tolerance,
                    absolute_angle_tolerance=absolute_angle_tolerance,
            )):
                best_model = None
                continue
            else:
                break

        if best_model is not None:
            yield best_model
Esempio n. 41
0
def image_case_factory(item_no):
    mosflm = sqr(
        (1, 0, 0, 0, 1, 0, 0, 0, 1))  # mosflm lab vectors in their own frame
    labelit = sqr(
        (0, 0, -1, -1, 0, 0, 0, 1, 0))  # mosflm basis vectors in labelit frame
    MLx = (0, 0, -1)
    MLy = (-1, 0, 0)
    MLz = (0, 1, 0)  # "virtual" rotation axis being used by cctbx.xfel for CXI
    LM = mosflm * labelit.inverse(
    )  # converts labelit frame coords to mosflm frame
    SWAPXY = sqr((0, 1, 0, 1, 0, 0, 0, 0, 1))  # in labelit frame
    SWAPZ = sqr((1, 0, 0, 0, 1, 0, 0, 0, -1))  # in labelit frame
    R90 = sqr((0, -1, 0, 1, 0, 0, 0, 0,
               1))  # in labelit frame, rotation 90 on beam axis

    CONTAINER_SZ = 1000
    WAVELENGTH = 1.32  # given by James Holton

    container_no = item // CONTAINER_SZ
    filename = "/net/viper/raid1/sauter/fake/result/basic00/"
    filename = os.path.join(filename, "r%02d" % container_no, TRIAL,
                            "integration", "int-data_%05d.pickle" % item_no)
    trial_results = pickle.load(open(filename, "rb"))

    current_hexagonal_ori = trial_results["current_orientation"][0]
    current_cb_op_to_primitive = trial_results["current_cb_op_to_primitive"][0]
    current_triclinic_ori = current_hexagonal_ori.change_basis(
        current_cb_op_to_primitive)
    filename = "/net/viper/raid1/sauter/fake/holton/mosflm_matrix"
    filename = os.path.join(filename, "%02d" % container_no,
                            "%05d.mat" % item_no)
    lines = open(filename).readlines()

    A0 = lines[0].strip().split()
    A1 = lines[1].strip().split()
    A2 = lines[2].strip().split()
    A = sqr(
        (float(A0[0]), float(A0[1]), float(A0[2]), float(A1[0]), float(A1[1]),
         float(A1[2]), float(A2[0]), float(A2[1]), float(A2[2])))

    A = A / WAVELENGTH
    Holton_hexagonal_ori = crystal_orientation(SWAPZ * SWAPXY * R90 * LM * A,
                                               True)
    Holton_triclinic_ori = Holton_hexagonal_ori.change_basis(
        current_cb_op_to_primitive)

    c_inv_r_best = Holton_triclinic_ori.best_similarity_transformation(
        other=current_triclinic_ori,
        fractional_length_tolerance=50.,
        unimodular_generator_range=1)
    c_inv_r_int = tuple([int(round(ij, 0)) for ij in c_inv_r_best])
    from cctbx import sgtbx
    c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r_int))
    cb_op = sgtbx.change_of_basis_op(c_inv)
    comparison_triclinic = Holton_triclinic_ori.change_basis(cb_op)
    comparison_hexagonal = comparison_triclinic.change_basis(
        current_cb_op_to_primitive.inverse())
    print(
        sqr(current_hexagonal_ori.direct_matrix()) -
        sqr(comparison_hexagonal.direct_matrix()))
    print("item %d" % item_no)

    SC = ScoringContainer()
    SC.model = current_hexagonal_ori
    SC.reference = comparison_hexagonal
    return SC
Esempio n. 42
0
            gamma = ground_truth_ori.unit_cell().parameters()[5]
            alpha = ground_truth_ori.unit_cell().parameters()[3]
            beta = ground_truth_ori.unit_cell().parameters()[4]
            if gamma < 80 or gamma > 85 or alpha < 73 or alpha > 77 or beta < 73 or beta > 77:
                continue

            c_inv_r_best = ground_truth_ori.best_similarity_transformation(
                other=refined_triclinic_ori,
                fractional_length_tolerance=50.,
                unimodular_generator_range=1)
            print((sqr(c_inv_r_best)).determinant())
            #c_inv_r_int = tuple([int(round(ij,0)) for ij in c_inv_r_best])
            c_inv_r_int = tuple([int(round(ij, 0)) for ij in c_inv_r_best])
            from cctbx import sgtbx
            c_inv = sgtbx.rt_mx(sgtbx.rot_mx(c_inv_r_int))
            cb_op = sgtbx.change_of_basis_op(c_inv)
            print(cb_op)
            comparison_triclinic = ground_truth_ori.change_basis(cb_op)
            print("comparison---> %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f" %
                  (comparison_triclinic.unit_cell().parameters()))

            missetting_rot = sqr(refined_ori.direct_matrix()) * sqr(
                ground_truth_ori.direct_matrix()).inverse()
            print("deter", missetting_rot.determinant())
            from LS49.missetting.mark0 import ScoringContainer
            SC = ScoringContainer()
            SC.model = refined_triclinic_ori
            SC.reference = ground_truth_ori
            angle_deg = SC.angular_rotation() * 180. / math.pi
            #print missetting_rot.elems
Esempio n. 43
0
 def change_of_basis_op(self):
   from cctbx import sgtbx
   return sgtbx.change_of_basis_op(
     sgtbx.rt_mx(sgtbx.rot_mx(self._r_inv.elems, 1))).inverse()