def exercise_match_multi_indices(): h0 = flex.miller_index(((1,2,3), (-1,-2,-3), (2,3,4), (-2,-3,-4), (3,4,5))) d0 = flex.double((1,2,3,4,5)) h1 = flex.miller_index(((1,2,3), (-2,-3,-4), (1,2,3), (2,3,4))) d1 = flex.double((10,20,30,40)) mi = miller.match_multi_indices(h0, h0) assert mi.have_singles() == 0 assert list(mi.pairs()) == list(zip(range(5), range(5))) mi = miller.match_multi_indices(h0, h1) assert tuple(mi.singles(0)) == (1,4,) assert tuple(mi.singles(1)) == () assert len(set(mi.pairs()) - set([(0,0), (0,2), (2,3), (3, 1)])) == 0 assert tuple(mi.number_of_matches(0)) == (2, 0, 1, 1, 0) assert tuple(mi.pair_selection(0)) == (1, 0, 1, 1, 0) assert tuple(mi.single_selection(0)) == (0, 1, 0, 0, 1) assert tuple(mi.number_of_matches(1)) == (1, 1, 1, 1) assert tuple(mi.pair_selection(1)) == (1, 1, 1, 1) assert tuple(mi.single_selection(1)) == (0, 0, 0, 0) assert tuple(mi.paired_miller_indices(0)) \ == tuple(h0.select(mi.pair_selection(0))) l1 = list(mi.paired_miller_indices(1)) l2 = list(h1.select(mi.pair_selection(1))) l1.sort() l2.sort() assert l1 == l2 try: miller.match_multi_indices(h1, h0) except RuntimeError as e: pass else: raise Exception_expected
def reindex(self, reindex_operation): """Reindex the reflections by the given reindexing operation.""" R = rt_mx(reindex_operation).inverse() # first construct mapping table - native, anomalous map_native = {} hkls = flex.miller_index() for hkl in self._merged_reflections: Fhkl = R * hkl Rhkl = nint(Fhkl[0]), nint(Fhkl[1]), nint(Fhkl[2]) hkls.append(Rhkl) map_to_asu(self._mf.get_space_group().type(), False, hkls) for j, hkl in enumerate(self._merged_reflections): map_native[hkl] = hkls[j] map_anomalous = {} hkls = flex.miller_index() for hkl in self._merged_reflections_anomalous: Fhkl = R * hkl Rhkl = nint(Fhkl[0]), nint(Fhkl[1]), nint(Fhkl[2]) hkls.append(Rhkl) map_to_asu(self._mf.get_space_group().type(), True, hkls) for j, hkl in enumerate(self._merged_reflections_anomalous): map_anomalous[hkl] = hkls[j] # then remap the actual measurements merged_reflections = {} for hkl in self._merged_reflections: Rhkl = map_native[hkl] merged_reflections[Rhkl] = self._merged_reflections[hkl] self._merged_reflections = merged_reflections merged_reflections = {} for hkl in self._merged_reflections_anomalous: Rhkl = map_anomalous[hkl] merged_reflections[Rhkl] = self._merged_reflections_anomalous[hkl] self._merged_reflections_anomalous = merged_reflections unmerged_reflections = {} for hkl in self._unmerged_reflections: Rhkl = map_native[hkl] unmerged_reflections[Rhkl] = self._unmerged_reflections[hkl] self._unmerged_reflections = unmerged_reflections
def exercise_match_multi_indices(): h0 = flex.miller_index(((1,2,3), (-1,-2,-3), (2,3,4), (-2,-3,-4), (3,4,5))) d0 = flex.double((1,2,3,4,5)) h1 = flex.miller_index(((1,2,3), (-2,-3,-4), (1,2,3), (2,3,4))) d1 = flex.double((10,20,30,40)) mi = miller.match_multi_indices(h0, h0) assert mi.have_singles() == 0 assert list(mi.pairs()) == zip(range(5), range(5)) mi = miller.match_multi_indices(h0, h1) assert tuple(mi.singles(0)) == (1,4,) assert tuple(mi.singles(1)) == () assert len(set(mi.pairs()) - set([(0,0), (0,2), (2,3), (3, 1)])) == 0 assert tuple(mi.number_of_matches(0)) == (2, 0, 1, 1, 0) assert tuple(mi.pair_selection(0)) == (1, 0, 1, 1, 0) assert tuple(mi.single_selection(0)) == (0, 1, 0, 0, 1) assert tuple(mi.number_of_matches(1)) == (1, 1, 1, 1) assert tuple(mi.pair_selection(1)) == (1, 1, 1, 1) assert tuple(mi.single_selection(1)) == (0, 0, 0, 0) assert tuple(mi.paired_miller_indices(0)) \ == tuple(h0.select(mi.pair_selection(0))) l1 = list(mi.paired_miller_indices(1)) l2 = list(h1.select(mi.pair_selection(1))) l1.sort() l2.sort() assert l1 == l2 try: miller.match_multi_indices(h1, h0) except RuntimeError, e: pass
def exercise_triplet_generator(): sg = sgtbx.space_group_info("P 41").group() i = flex.miller_index(((1,2,3),(2,3,4))) a = flex.double((1,2)) t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, False) assert t.t_den() == sg.t_den() assert t.max_relations_per_reflection() == 0 assert t.sigma_2_only() == False assert t.discard_weights() == False t = dmtbx.ext.triplet_generator(sg, i, a, 3, True, False) assert t.max_relations_per_reflection() == 3 assert t.sigma_2_only() == True assert t.discard_weights() == False t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, True) assert t.sigma_2_only() == False assert t.discard_weights() == True assert tuple(t.n_relations()) == (0,0) assert t.relations_for(0) == () assert approx_equal(tuple(t.sums_of_amplitude_products(a)), (0,0)) s = flex.bool() r = t.raw_apply_tangent_formula(a, a, s, False, False, 1.e-15) assert approx_equal(tuple(r), (1,2)) i = flex.miller_index(((4,6,0),(5,2,5),(6,1,5))) a = flex.double((1,2,3)) t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, False) assert tuple(t.n_relations()) == (4,2,2) assert [r.format(i, 0) for r in t.relations_for(0)] \ == ["(4,6,0) (5,2,5) %s (6,1,5) %s 3 2" % (True, False), "(4,6,0) (5,2,5) %s (6,1,5) %s 9 2" % (False, True)] assert [r.format(i, 1) for r in t.relations_for(1)] \ == ["(5,2,5) (4,6,0) %s (6,1,5) %s 3 2" % (False, False)] assert [r.format(i, 2) for r in t.relations_for(2)] \ == ["(6,1,5) (4,6,0) %s (5,2,5) %s 9 2" % (False, False)] assert approx_equal(tuple(t.sums_of_amplitude_products(a)), (24,6,4)) t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, True) assert tuple(t.n_relations()) == (1,1,1) assert [r.format(i, 0) for r in t.relations_for(0)] \ == ["(4,6,0) (5,2,5) %s (6,1,5) %s 3 1" % (True, False)] assert approx_equal(tuple(t.sums_of_amplitude_products(a)), (6,3,2)) t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, False) r0 = t.relations_for(0) r1 = t.relations_for(1) assert r0[0].is_sigma_2(0) assert r0[0].is_similar_to(r0[1]) assert not r0[0].is_similar_to(r1[0]) i = flex.miller_index(((4,6,0),(5,1,2))) t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, False) assert [r.format(i, 0) for r in t.relations_for(0)] \ == ["(4,6,0) (5,1,2) %s (5,1,2) %s 6 4" % (False, True)] assert [r.format(i, 1) for r in t.relations_for(1)] \ == ["(5,1,2) (4,6,0) %s (5,1,2) %s 6 4" % (False, False)] assert not t.relations_for(0)[0].is_sigma_2(0) t = dmtbx.ext.triplet_generator(sg, i, None, 0, False, True) assert [r.format(i, 0) for r in t.relations_for(0)] \ == ["(4,6,0) (5,1,2) %s (5,1,2) %s 6 1" % (False, True)] assert [r.format(i, 1) for r in t.relations_for(1)] \ == ["(5,1,2) (4,6,0) %s (5,1,2) %s 6 1" % (False, False)] t = dmtbx.ext.triplet_generator(sg, i, None, 0, True, False) assert tuple(t.n_relations()) == (0,0)
def exercise_union_of_indices(): u = miller.union_of_indices_registry() assert u.as_array().size() == 0 u.update(indices=flex.miller_index([(1,2,0)])) assert list(u.as_array()) == [(1,2,0)] u.update(indices=flex.miller_index([(1,2,0)])) assert list(u.as_array()) == [(1,2,0)] u.update(indices=flex.miller_index([(3,2,0)])) assert sorted(u.as_array()) == [(1,2,0), (3,2,0)]
def _get_unique_Hi(self): COMM.barrier() if COMM.rank == 0: from cctbx.crystal import symmetry from cctbx import miller from cctbx.array_family import flex as cctbx_flex ii = list(self.Modelers.keys())[0] uc = self.Modelers[ii].ucell_man params = uc.a, uc.b, uc.c, uc.al * 180 / np.pi, uc.be * 180 / np.pi, uc.ga * 180 / np.pi if self.params.refiner.force_unit_cell is not None: params = self.params.refiner.force_unit_cell symm = symmetry(unit_cell=params, space_group_symbol=self.symbol) hi_asu_flex = cctbx_flex.miller_index(self.Hi_asu_all_ranks) mset = miller.set(symm, hi_asu_flex, anomalous_flag=True) marr = miller.array(mset) binner = marr.setup_binner( d_max=self.params.refiner.stage_two.d_max, d_min=self.params.refiner.stage_two.d_min, n_bins=self.params.refiner.stage_two.n_bin) from collections import Counter print("Average multiplicities:") print("<><><><><><><><><><><><>") for i_bin in range(self.params.refiner.stage_two.n_bin - 1): dmax, dmin = binner.bin_d_range(i_bin + 1) F_in_bin = marr.resolution_filter(d_max=dmax, d_min=dmin) multi_in_bin = np.array( list(Counter(F_in_bin.indices()).values())) print("%2.5g-%2.5g : Multiplicity=%.4f" % (dmax, dmin, multi_in_bin.mean())) for ii in range(1, 100, 8): print("\t %d refls with multi %d" % (sum(multi_in_bin == ii), ii)) print("Overall completeness\n<><><><><><><><>") symm = symmetry(unit_cell=params, space_group_symbol=self.symbol) hi_flex_unique = cctbx_flex.miller_index( list(set(self.Hi_asu_all_ranks))) mset = miller.set(symm, hi_flex_unique, anomalous_flag=True) self.binner = mset.setup_binner( d_min=self.params.refiner.stage_two.d_min, d_max=self.params.refiner.stage_two.d_max, n_bins=self.params.refiner.stage_two.n_bin) mset.completeness(use_binning=True).show() marr_unique_h = miller.array(mset) print("Rank %d: total miller vars=%d" % (COMM.rank, len(set(self.Hi_asu_all_ranks)))) else: marr_unique_h = None marr_unique_h = COMM.bcast(marr_unique_h) return marr_unique_h
def exercise_match_indices(): h0 = flex.miller_index( ((1, 2, 3), (-1, -2, -3), (2, 3, 4), (-2, -3, -4), (3, 4, 5))) d0 = flex.double((1, 2, 3, 4, 5)) h1 = flex.miller_index(((-1, -2, -3), (-2, -3, -4), (1, 2, 3), (2, 3, 4))) d1 = flex.double((10, 20, 30, 40)) mi = miller.match_indices(h0, h0) assert mi.have_singles() == 0 assert list(mi.pairs()) == zip(range(5), range(5)) mi = miller.match_indices(h0, h1) assert tuple(mi.singles(0)) == (4, ) assert tuple(mi.singles(1)) == () assert tuple(mi.pairs()) == ((0, 2), (1, 0), (2, 3), (3, 1)) assert tuple(mi.pair_selection(0)) == (1, 1, 1, 1, 0) assert tuple(mi.single_selection(0)) == (0, 0, 0, 0, 1) assert tuple(mi.pair_selection(1)) == (1, 1, 1, 1) assert tuple(mi.single_selection(1)) == (0, 0, 0, 0) assert tuple(mi.paired_miller_indices(0)) \ == tuple(h0.select(mi.pair_selection(0))) l1 = list(mi.paired_miller_indices(1)) l2 = list(h1.select(mi.pair_selection(1))) l1.sort() l2.sort() assert l1 == l2 assert approx_equal(tuple(mi.plus(d0, d1)), (31, 12, 43, 24)) assert approx_equal(tuple(mi.minus(d0, d1)), (-29, -8, -37, -16)) assert approx_equal(tuple(mi.multiplies(d0, d1)), (30, 20, 120, 80)) assert approx_equal(tuple(mi.divides(d0, d1)), (1 / 30., 2 / 10., 3 / 40., 4 / 20.)) assert approx_equal(tuple(mi.additive_sigmas(d0, d1)), [ math.sqrt(x * x + y * y) for x, y in ((1, 30), (2, 10), (3, 40), (4, 20)) ]) q = flex.size_t((3, 2, 0, 4, 1)) h1 = h0.select(q) assert tuple(miller.match_indices(h1, h0).permutation()) == tuple(q) p = miller.match_indices(h0, h1).permutation() assert tuple(p) == (2, 4, 1, 0, 3) assert tuple(h1.select(p)) == tuple(h0) cd0 = [ complex(a, b) for (a, b) in (1, 1), (2, 0), (3.5, -1.5), (5, -3), (-8, 5.4) ] cd1 = [ complex(a, b) for (a, b) in (1, -1), (2, 1), (0.5, 1.5), (-1, -8), (10, 0) ] cd2 = flex.complex_double(cd0) cd3 = flex.complex_double(cd1) mi = miller.match_indices(h0, h0) assert approx_equal(tuple(mi.plus(cd2, cd3)), ((2 + 0j), (4 + 1j), (4 + 0j), (4 - 11j), (2 + 5.4j)))
def exercise_fast_nv1995(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3, 4, 5), (4, 5, 6))) f = translation_search.fast_nv1995( gridding=(20, 20, 36), space_group=space_group, anomalous_flag=False, miller_indices_f_obs=miller_indices_f_obs, f_obs=flex.double((1, 2)), f_part=flex.complex_double(), miller_indices_p1_f_calc=flex.miller_index(((1, 2, 3), )), p1_f_calc=flex.complex_double((12, ))) assert f.target_map().all() == (20, 20, 36)
def exercise_fast_nv1995(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3,4,5),(4,5,6))) f = translation_search.fast_nv1995( gridding=(20,20,36), space_group=space_group, anomalous_flag=False, miller_indices_f_obs=miller_indices_f_obs, f_obs=flex.double((1,2)), f_part=flex.complex_double(), miller_indices_p1_f_calc=flex.miller_index(((1,2,3),)), p1_f_calc=flex.complex_double((12,))) assert f.target_map().all() == (20,20,36)
def exercise_fast_terms(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3,4,5),(4,5,6))) f = translation_search.fast_terms( gridding=(20,20,36), anomalous_flag=False, miller_indices_p1_f_calc=flex.miller_index(((1,2,3),)), p1_f_calc=flex.complex_double((12,))) assert f.summation( space_group=space_group, miller_indices_f_obs=miller_indices_f_obs, m=flex.double((1,2)), f_part=None, squared_flag=False).fft().accu_real_copy().all() == (20,20,36)
def exercise_asu(): sg_type = sgtbx.space_group_type("P 41") asu = sgtbx.reciprocal_space_asu(sg_type) miller_indices = flex.miller_index(((1, 2, 3), (3, 5, 0))) for h in miller_indices: h_eq = miller.sym_equiv_indices(sg_type.group(), h) for i_eq in xrange(h_eq.multiplicity(False)): h_i = h_eq(i_eq) for anomalous_flag in (False, True): a = miller.asym_index(sg_type.group(), asu, h_i.h()) assert a.h() == h o = a.one_column(anomalous_flag) assert o.i_column() == 0 t = a.two_column(anomalous_flag) assert t.h() == h assert (o.h() != h) == (t.i_column() == 1) assert not anomalous_flag or (t.i_column() != 0) == h_i.friedel_flag() assert anomalous_flag or t.i_column() == 0 miller.map_to_asu(sg_type, False, miller_indices) data = flex.double((0, 0)) miller.map_to_asu(sg_type, False, miller_indices, data) miller.map_to_asu(sg_type, False, miller_indices, data, False) miller.map_to_asu(sg_type, False, miller_indices, data, True) data = flex.complex_double((0, 0)) miller.map_to_asu(sg_type, False, miller_indices, data) data = flex.hendrickson_lattman(((1, 2, 3, 4), (2, 3, 4, 5))) miller.map_to_asu(sg_type, False, miller_indices, data) for sg_symbol in ("P 41", "P 31 1 2"): exercise_map_to_asu(sg_symbol) # sg_type = sgtbx.space_group_type("P 2") miller_indices = flex.miller_index(((1, 2, 3), (-1, -2, -3))) assert not miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices) assert miller.is_unique_set_under_symmetry(space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices) assert list( miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices)) == [0] assert list( miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices)) == [0, 1]
def exercise_fast_terms(): space_group = sgtbx.space_group_info("P 21 21 21").group() miller_indices_f_obs = flex.miller_index(((3, 4, 5), (4, 5, 6))) f = translation_search.fast_terms( gridding=(20, 20, 36), anomalous_flag=False, miller_indices_p1_f_calc=flex.miller_index(((1, 2, 3), )), p1_f_calc=flex.complex_double((12, ))) assert f.summation(space_group=space_group, miller_indices_f_obs=miller_indices_f_obs, m=flex.double((1, 2)), f_part=None, squared_flag=False).fft().accu_real_copy().all() == (20, 20, 36)
def exercise_asu(): sg_type = sgtbx.space_group_type("P 41") asu = sgtbx.reciprocal_space_asu(sg_type) miller_indices = flex.miller_index(((1,2,3), (3,5,0))) for h in miller_indices: h_eq = miller.sym_equiv_indices(sg_type.group(), h) for i_eq in xrange(h_eq.multiplicity(False)): h_i = h_eq(i_eq) for anomalous_flag in (False,True): a = miller.asym_index(sg_type.group(), asu, h_i.h()) assert a.h() == h o = a.one_column(anomalous_flag) assert o.i_column() == 0 t = a.two_column(anomalous_flag) assert t.h() == h assert (o.h() != h) == (t.i_column() == 1) assert not anomalous_flag or (t.i_column() != 0) == h_i.friedel_flag() assert anomalous_flag or t.i_column() == 0 miller.map_to_asu(sg_type, False, miller_indices) data = flex.double((0,0)) miller.map_to_asu(sg_type, False, miller_indices, data) miller.map_to_asu(sg_type, False, miller_indices, data, False) miller.map_to_asu(sg_type, False, miller_indices, data, True) data = flex.complex_double((0,0)) miller.map_to_asu(sg_type, False, miller_indices, data) data = flex.hendrickson_lattman(((1,2,3,4),(2,3,4,5))) miller.map_to_asu(sg_type, False, miller_indices, data) for sg_symbol in ("P 41", "P 31 1 2"): exercise_map_to_asu(sg_symbol) # sg_type = sgtbx.space_group_type("P 2") miller_indices = flex.miller_index(((1,2,3), (-1,-2,-3))) assert not miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices) assert miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices) assert list(miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices)) == [0] assert list(miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices)) == [0,1]
def as_mtz(self, scale_factor, prefix): #This reads in an hkl map and returns a .mtz map #Read in hkl file and populate miller array inf = open(self.diffuse_file, 'r') indices = flex.miller_index() i_obs = flex.double() sig_i = flex.double() for line in inf.readlines(): assert len(line.split()) == 4 line = line.strip().split() #####ATTENTION:SCALE FACTOR############## i_obs_ = float( line[3] ) / scale_factor #is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros indices.append([int(line[0]), int(line[1]), int(line[2])]) i_obs.append(i_obs_) sig_i.append(sig_i_) inf.close() # get miller array object cs = self.symmetry ma = miller.array(miller_set=miller.set(cs, indices), data=i_obs, sigmas=sig_i) ma.set_observation_type_xray_intensity() mtz_dataset = ma.as_mtz_dataset(column_root_label="I") mtz_dataset.mtz_object().write(prefix + '.mtz')
def run(args): from dials.util.options import OptionParser from dials.util.options import flatten_experiments from libtbx.utils import Sorry import libtbx.load_env usage = "%s [options] experiments.json" %libtbx.env.dispatcher_name parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, check_format=False, epilog=help_message) params, options = parser.parse_args(show_diff_phil=True) experiments = flatten_experiments(params.input.experiments) if len(experiments) <= 1: parser.print_help() return hkl = flex.miller_index(params.hkl) from dials.algorithms.indexing.compare_orientation_matrices import \ show_rotation_matrix_differences show_rotation_matrix_differences(experiments.crystals(), miller_indices=hkl)
def __init__(self, data, lattice_id, resort=False, verbose=True): self.verbose = verbose ###### INPUTS ####### # data = miller array: ASU miller index & intensity # lattice_id = flex double: assignment of each miller index to a lattice number ###################### if resort: order = flex.sort_permutation(lattice_id.data()) sorted_lattice_id = flex.select(lattice_id.data(), order) sorted_data = data.data().select( order) sorted_indices = data.indices().select( order) self.lattice_id = sorted_lattice_id self.data = data.customized_copy(indices = sorted_indices, data = sorted_data) else: self.lattice_id = lattice_id.data() # type flex int self.data = data # type miller array with flex double data assert type(self.data.indices()) == type(flex.miller_index()) assert type(self.data.data()) == type(flex.double()) # construct a lookup for the separate lattices last_id = -1; self.lattices = flex.int() for n in xrange(len(self.lattice_id)): if self.lattice_id[n] != last_id: last_id = self.lattice_id[n] self.lattices.append(n)
def exercise_miller_array_data_types(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False ) for data in [ flex.bool([False, True]), flex.int([0, 1]), flex.size_t([0, 1]), flex.double([0, 1]), flex.complex_double([0, 1]), ]: miller_array = miller_set.array(data=data) if op.isfile("tmp_iotbx_mtz.mtz"): os.remove("tmp_iotbx_mtz.mtz") assert not op.isfile("tmp_iotbx_mtz.mtz") miller_array.as_mtz_dataset(column_root_label="DATA").mtz_object().write(file_name="tmp_iotbx_mtz.mtz") assert op.isfile("tmp_iotbx_mtz.mtz") mtz_obj = mtz.object(file_name="tmp_iotbx_mtz.mtz") miller_arrays_read_back = mtz_obj.as_miller_arrays() assert len(miller_arrays_read_back) == 1 miller_array_read_back = miller_arrays_read_back[0] assert miller_array_read_back.indices().all_eq(miller_array.indices()) if miller_array.is_integer_array() or miller_array.is_bool_array(): assert miller_array_read_back.data().all_eq(flex.int([0, 1])) elif miller_array.is_real_array(): assert miller_array_read_back.data().all_eq(flex.double([0, 1])) elif miller_array.is_complex_array(): assert miller_array_read_back.data().all_eq(flex.complex_double([0, 1])) else: raise RuntimeError("Programming error.")
def test_grouped_observations(): # some dummy observations for one reflection I1, I2, I3 = 101, 100, 99 w1, w2, w3 = 0.9, 1.0, 0.8 g1, g2, g3 = 1.01, 1.0, 0.99 # combine with observations from some other group to make a dataset hkl = flex.miller_index([(0, 0, 1)] * 3 + [(0, 0, 2)] * 2) intensity = flex.double([I1, I2, I3, 10, 10]) weight = flex.double([w1, w2, w3, 1.0, 1.0]) phi = flex.double(len(hkl), 0) # dummy scale = flex.double([g1, g2, g3, 1.0, 1.0]) # group the observations by Miller index go = GroupedObservations(hkl, intensity, weight, phi, scale) # ensure there are two groups, the first of size 3, the second of size 2 assert list(go.get_group_size()) == [3, 2] # the first group has an average intensity given by the HRS formula expanded: avI = (w1 * g1 * I1 + w2 * g2 * I2 + w3 * g3 * I3) / (w1 * g1 * g1 + w2 * g2 * g2 + w3 * g3 * g3) assert approx_equal(avI, go.get_average_intensity()[0]) print "OK"
def df2m(df, cell, spgr, data=None, sigmas=None): """Constructs a miller.array from the columns specified by data/sigmas in the dataframe, if both are None, returns just the indices. needs cell and spgr to generate a symmetry object.""" anomalous_flag = False if isinstance(df, pd.DataFrame): try: sel = df[data].notnull() # select notnull data items for index except ValueError: index = df.index else: index = df.index[sel] else: index = df indices = flex.miller_index(index) if data: data = flex.double(df[data][sel]) if sigmas: sigmas = flex.double(df[sigmas][sel]) symm = make_symmetry(cell, spgr) ms = miller.set( crystal_symmetry=symm, indices=indices, anomalous_flag=anomalous_flag) return miller.array(ms, data=data, sigmas=sigmas)
def exercise_map_to_asu(sg_symbol): sg_type = sgtbx.space_group_type(sg_symbol) index_abs_range = (4,4,4) for anomalous_flag in (False,True): m = miller.index_generator( sg_type, anomalous_flag, index_abs_range).to_array() a = flex.double() p = flex.double() c = flex.hendrickson_lattman() for i in xrange(m.size()): a.append(random.random()) p.append(random.random() * 2) c.append([random.random() for j in xrange(4)]) f = flex.polar(a, p) p = [p, p*(180/math.pi)] m_random = flex.miller_index() p_random = [flex.double(), flex.double()] c_random = flex.hendrickson_lattman() f_random = flex.complex_double() for i,h_asym in enumerate(m): h_eq = miller.sym_equiv_indices(sg_type.group(), h_asym) i_eq = random.randrange(h_eq.multiplicity(anomalous_flag)) h_i = h_eq(i_eq) m_random.append(h_i.h()) for deg in (False,True): p_random[deg].append(h_i.phase_eq(p[deg][i], deg)) f_random.append(h_i.complex_eq(f[i])) c_random.append(h_i.hendrickson_lattman_eq(c[i])) m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, f_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,f_asym in enumerate(f): assert abs(f_asym - f_random[i]) < 1.e-6 m_random_copy = m_random.deep_copy() a_random = a.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, a_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,a_asym in enumerate(a): assert a_asym == a_random[i] for deg in (False,True): m_random_copy = m_random.deep_copy() miller.map_to_asu( sg_type, anomalous_flag, m_random_copy, p_random[deg], deg) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,p_asym in enumerate(p[deg]): assert scitbx.math.phase_error(p_asym, p_random[deg][i], deg) < 1.e-5 m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, c_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,c_asym in enumerate(c): for j in xrange(4): assert abs(c_asym[j] - c_random[i][j]) < 1.e-5
def predict(self, indices, angles): # extract required information from the models sensor = self._detector.sensors()[0] # assume only one sensor for now axis = matrix.col(self._gonio.get_rotation_axis()) s0 = matrix.col(self._beam.get_s0()) UB = self._crystal.get_U() * self._crystal.get_B() # instantiate predictor rp = reflection_prediction(axis, s0, UB, sensor) # perform impact prediction # rp.predict does not like indices as miller_index. A conversion to # matrix.col fixes that temp = [] for hkl in indices: hkl = matrix.col(hkl) temp.append(hkl) Hc, Xc, Yc, Phic, Sc = rp.predict(temp, angles) # Hc is now an an array of floating point vec3. How annoying! We want # integer Miller indices. Force that to be so. # Once we have new (fast) reflection prediction code that does not # return floating point indices then this whole class can be replaced temp = flex.miller_index() for hkl in Hc: hkl = map(lambda x: int(round(x)), hkl) temp.append(tuple(hkl)) # Rescale normalised scattering vectors to the correct length wavelength = self._beam.get_wavelength() Sc = map(lambda s: matrix.col(s) / wavelength, Sc) return (temp, Xc, Yc, Phic, Sc)
def exercise_expand(): sg = sgtbx.space_group("P 41 (1,-1,0)") h = flex.miller_index(((3,1,-2), (1,-2,0))) assert tuple(sg.is_centric(h)) == (0, 1) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=False, indices=h, build_iselection=False) p1_i0 = ((-3,-1,2), (-1, 3,2),(3,1,2),(1,-3,2),(1,-2, 0),(2,1,0)) assert tuple(p1.indices) == p1_i0 assert p1.iselection.size() == 0 p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=True, indices=h, build_iselection=False) assert tuple(p1.indices) \ == ((3,1,-2), (1,-3,-2), (-3,-1,-2), (-1,3,-2), (1,-2,0), (-2,-1,0), (-1,2,0), (2,1,0)) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=False, indices=h, build_iselection=True) assert tuple(p1.indices) == p1_i0 assert tuple(p1.iselection) == (0,0,0,0,1,1) a = flex.double((1,2)) p = flex.double((10,90)) p1 = miller.expand_to_p1_phases( space_group=sg, anomalous_flag=False, indices=h, data=p, deg=True) assert approx_equal(tuple(p1.data), (-10,110,110,-10, 90,30)) p1 = miller.expand_to_p1_phases( space_group=sg, anomalous_flag=True, indices=h, data=p, deg=True) assert approx_equal(tuple(p1.data), (10,-110,-110,10, 90,-30,-90,30)) p = flex.double([x * math.pi/180 for x in p]) v = [x * math.pi/180 for x in p1.data] p1 = miller.expand_to_p1_phases( space_group=sg, anomalous_flag=True, indices=h, data=p, deg=False) assert approx_equal(tuple(p1.data), v) f = flex.polar(a, p) p1 = miller.expand_to_p1_complex( space_group=sg, anomalous_flag=True, indices=h, data=f) assert approx_equal(tuple(flex.abs(p1.data)), (1,1,1,1,2,2,2,2)) assert approx_equal(tuple(flex.arg(p1.data)), v) hl = flex.hendrickson_lattman([(1,2,3,4), (5,6,7,8)]) p1 = miller.expand_to_p1_hendrickson_lattman( space_group=sg, anomalous_flag=True, indices=h, data=hl) assert approx_equal(p1.data, [ [1,2,3,4], [1.232051,-1.866025,-4.964102,0.5980762], [1.232051,-1.866025,-4.964102,0.5980762], [1,2,3,4], [5,6,7,8], [2.696152,-7.330127,-10.4282,2.062178], [-5,-6,7,8], [7.696152,-1.330127,3.428203,-10.06218]]) b = flex.bool([True,False]) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=True, indices=h, build_iselection=True) assert b.select(p1.iselection).all_eq( flex.bool([True, True, True, True, False, False, False, False])) i = flex.int([13,17]) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=True, indices=h, build_iselection=True) assert i.select(p1.iselection).all_eq(flex.int([13,13,13,13,17,17,17,17])) # assert approx_equal(miller.statistical_mean(sg, False, h, a), 4/3.) assert approx_equal(miller.statistical_mean(sg, True, h, a), 3/2.)
def extend_symmetry(self,scale_factor,prefix): #This reads in an hkl map and returns a .sca map #Read in hkl file and populate miller array inf = open(self.diffuse_file, 'r') indices = flex.miller_index() i_obs = flex.double() sig_i = flex.double() for line in inf.readlines(): assert len(line.split())==4 line = line.strip().split() #####ATTENTION:SCALE FACTOR############## i_obs_ = float(line[3])/scale_factor #is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros indices.append([int(line[0]),int(line[1]),int(line[2])]) i_obs.append(i_obs_) sig_i.append(sig_i_) inf.close() # get miller array object cs = self.symmetry ma = miller.array(miller_set=miller.set(cs, indices), data=i_obs, sigmas=sig_i) ma.set_observation_type_xray_intensity() ma_anom = ma.customized_copy(anomalous_flag=False) ma_p1 = ma_anom.expand_to_p1() merge.write(file_name= prefix + '.sca', miller_array=ma_p1) self.p1_map = prefix + '.sca'
def exercise_f000(): miller_indices = flex.miller_index([(0,0,0)]) data = flex.complex_double([1-2j]) n_real = [1,2,3] conjugate_flag = True for hall_symbol in ["P 1", "P 3", "R 3*"]: for is_centric in [False, True]: if (not is_centric): space_group = sgtbx.space_group(hall_symbol) else: space_group.expand_smx("-x,-y,-z") for anomalous_flag in [False, True]: if (not anomalous_flag): rfft = fftpack.real_to_complex_3d(n_real) n_complex = rfft.n_complex() else: cfft = fftpack.complex_to_complex_3d(n_real) n_complex = cfft.n() for treat_restricted in [False, True]: map = maptbx.structure_factors.to_map( space_group=space_group, anomalous_flag=anomalous_flag, miller_indices=miller_indices, structure_factors=data, n_real=n_real, map_grid=flex.grid(n_complex), conjugate_flag=conjugate_flag, treat_restricted=treat_restricted) if (treat_restricted): assert approx_equal( map.complex_map()[0], data[0]) else: assert approx_equal( map.complex_map()[0], data[0]*space_group.order_p())
def __init__(self): self.data_obs1 = flex.double(2,1.0) self.data_obs2 = flex.double(2,3.0) self.sigma_obs1 = flex.double(2,0.1) self.sigma_obs2 = flex.double(2,1) self.unit_cell = uctbx.unit_cell('20, 30, 40, 90.0, 90.0, 90.0') #mi = flex.miller_index(((1,2,3), (1,2,3))) self.mi = flex.miller_index(((1,2,3), (5,6,7))) self.xs = crystal.symmetry((20,30,40), "P 2 2 2") self.ms = miller.set(self.xs, self.mi) self.u = [1,2,3,4,5,6] self.p_scale = 0.40 #self.u = [0,0,0,0,0,0] #self.p_scale = 0.00 self.ls_i_wt = scaling.least_squares_on_i_wt( self.mi, self.data_obs1, self.sigma_obs1, self.data_obs2, self.sigma_obs2, self.p_scale, self.unit_cell, self.u) self.ls_i = scaling.least_squares_on_i( self.mi, self.data_obs1, self.sigma_obs1, self.data_obs2, self.sigma_obs2, self.p_scale, self.unit_cell, self.u) self.ls_f_wt = scaling.least_squares_on_f_wt( self.mi, self.data_obs1, self.sigma_obs1, self.data_obs2, self.sigma_obs2, self.p_scale, self.unit_cell, self.u) self.ls_f = scaling.least_squares_on_f( self.mi, self.data_obs1, self.sigma_obs1, self.data_obs2, self.sigma_obs2, self.p_scale, self.unit_cell, self.u) self.tst_ls_f_wt() self.tst_ls_i_wt() self.tst_ls_f() self.tst_ls_i() self.tst_hes_ls_i_wt() self.tst_hes_ls_f_wt() self.tst_hes_ls_i() self.tst_hes_ls_f()
def __init__(self, file_name=None, file_object=None, max_header_lines=30): assert [file_name, file_object].count(None) == 1 if (file_object is None): file_object = open(file_name) self._names = None self._indices = flex.miller_index() self._data = flex.double() self._sigmas = flex.double() have_data = False self.n_lines = 0 for raw_line in file_object: self.n_lines += 1 ifs = index_fobs_sigma_line(raw_line) if (not ifs.is_complete): if (raw_line.strip().lower() == "end"): break if (self.n_lines == max_header_lines or have_data): raise RuntimeError, "Unkown file format." else: if (self._names is None): self._names = ifs.names self._indices.append(ifs.index) self._data.append(ifs.fobs) self._sigmas.append(ifs.sigma) have_data = True if (not have_data): raise RuntimeError, "No data found in file."
def exercise_miller_array_data_types(): miller_set = crystal.symmetry(unit_cell=(10, 10, 10, 90, 90, 90), space_group_symbol="P1").miller_set( indices=flex.miller_index([(1, 2, 3), (4, 5, 6)]), anomalous_flag=False) for data in [ flex.bool([False, True]), flex.int([0, 1]), flex.size_t([0, 1]), flex.double([0, 1]), flex.complex_double([0, 1]) ]: miller_array = miller_set.array(data=data) if (op.isfile("tmp_iotbx_mtz.mtz")): os.remove("tmp_iotbx_mtz.mtz") assert not op.isfile("tmp_iotbx_mtz.mtz") miller_array.as_mtz_dataset( column_root_label="DATA").mtz_object().write( file_name="tmp_iotbx_mtz.mtz") assert op.isfile("tmp_iotbx_mtz.mtz") mtz_obj = mtz.object(file_name="tmp_iotbx_mtz.mtz") miller_arrays_read_back = mtz_obj.as_miller_arrays() assert len(miller_arrays_read_back) == 1 miller_array_read_back = miller_arrays_read_back[0] assert miller_array_read_back.indices().all_eq(miller_array.indices()) if (miller_array.is_integer_array() or miller_array.is_bool_array()): assert miller_array_read_back.data().all_eq(flex.int([0, 1])) elif (miller_array.is_real_array()): assert miller_array_read_back.data().all_eq(flex.double([0, 1])) elif (miller_array.is_complex_array()): assert miller_array_read_back.data().all_eq( flex.complex_double([0, 1])) else: raise RuntimeError("Programming error.")
def exercise(): ma = miller.array( miller.set(crystal.symmetry(unit_cell=(5,5,5, 90, 90, 90), space_group=sgtbx.space_group('P 2x')), indices=flex.miller_index( [(1,0,0), (0,1,0), (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1), (1,1,0), (1,0,1), (0,1,1), (-1,-1,0), (-1,0,-1), (0,-1,-1), (1,-1,0), (1,0,-1), (0,1,-1), (-1,1,0), (-1,0,1), (0,-1,1), (1,1,1), (-1,1,1), (1,-1,1), (1,1,-1), (-1,-1,-1), (1,-1,-1), (-1,1,-1), (-1,-1,1)])), data=flex.complex_double(flex.random_double(26), flex.random_double(26))) f_at_h = dict(zip(ma.indices(), ma.data())) for op in ("-x, y+1/2, -z", "x+1/2, -y, z-1/2"): op = sgtbx.rt_mx(op) original, transformed = ma.common_sets( ma.change_basis(sgtbx.change_of_basis_op(op.inverse()))) for h, f in original: assert f == f_at_h[h] for h, op_f in transformed: assert approx_equal( op_f, f_at_h[h*op.r()]*exp(1j*2*pi*row(h).dot(col(op.t().as_double()))))
def observed_indices_and_angles_from_angle_range( self, phi_start_rad, phi_end_rad, indices ): """For a list of indices, return those indices that can be rotated into diffracting position, along with the corresponding angles""" # calculate conversion matrix to rossmann frame. R_to_rossmann = self.align_reference_frame( self._beam.get_unit_s0(), (0.0, 0.0, 1.0), self._gonio.get_rotation_axis(), (0.0, 1.0, 0.0), ) # Create a rotation_angles object for the current geometry ra = rotation_angles( self._dmin, R_to_rossmann * self._crystal.get_U() * self._crystal.get_B(), self._beam.get_wavelength(), R_to_rossmann * matrix.col(self._gonio.get_rotation_axis()), ) (obs_indices, obs_angles) = ra.observed_indices_and_angles_from_angle_range( phi_start_rad=0.0, phi_end_rad=pi, indices=indices ) # convert to integer miller indices obs_indices_int = flex.miller_index() for hkl in obs_indices: hkl = map(lambda x: int(round(x)), hkl) obs_indices_int.append(tuple(hkl)) return obs_indices_int, obs_angles
def convert_to_sf(hkl, intensities, phases, cs): """ Reformat intensities and phases into a CCTBX-style Miller array, with space group symmetry enforced. Inputs: ------- hkl: list of Miller indices, formatted as tuples intensities: array of intensities, with ordering matching hkl phases: array of phases with ordering matching hkl cs: CCTBX crystal.symmetry object Outputs: -------- ma: CCTBX-style Miller array """ # compute structure factors in complex number format if (phases.min() < -1*np.pi) or (phases.max() > np.pi): print "Error: Invalid phase values; may be in degrees rather than radians." A, B = np.sqrt(intensities)*np.cos(phases), np.sqrt(intensities)*np.sin(phases) B[np.abs(B)<1e-12] = 0 # reformat miller indices and structure factor information into CCTBX format indices = flex.miller_index(hkl) sf_data = flex.complex_double(flex.double(A),flex.double(B)) ma = miller.array(miller_set=miller.set(cs, indices, anomalous_flag=False), data=sf_data) return ma
def test_minimum_multiplicity_selection(): # some groups of indices with increasing multiplicity hkl = flex.miller_index([(0, 0, 1), (0, 0, 2), (0, 0, 2), (0, 0, 3), (0, 0, 3), (0, 0, 3), (0, 0, 4), (0, 0, 4), (0, 0, 4), (0, 0, 4), (0, 0, 5), (0, 0, 5), (0, 0, 5), (0, 0, 5), (0, 0, 5)]) # test the various possible selections with multiplicity from 1 to 6 sel = minimum_multiplicity_selection(hkl, 1) assert sel.count(True) == len(hkl) sel = minimum_multiplicity_selection(hkl, 2) assert (sel == flex.bool([ False, ] + [True] * 14)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 3) assert (sel == flex.bool([False] * 3 + [True] * 12)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 4) assert (sel == flex.bool([False] * 6 + [True] * 9)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 5) assert (sel == flex.bool([False] * 10 + [True] * 5)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 6) assert sel.count(False) == len(hkl) print "OK"
def cluster_completeness(self, clno, anomalous_flag, calc_redundancy=True): if clno not in self.clusters: print "Cluster No. %d not found" % clno return cls = self.clusters[clno][3] msets = map(lambda x: self.miller_sets[self.files[x-1]], cls) num_idx = sum(map(lambda x: x.size(), msets)) all_idx = flex.miller_index() all_idx.reserve(num_idx) for mset in msets: all_idx.extend(mset.indices()) # Calc median cell cells = numpy.array(map(lambda x: x.unit_cell().parameters(), msets)) median_cell = map(lambda i: numpy.median(cells[:,i]), xrange(6)) symm = msets[0].customized_copy(unit_cell=median_cell) assert anomalous_flag is not None # XXX all must belong to the same Laue group and appropriately reindexed.. all_set = miller.set(indices=all_idx, crystal_symmetry=symm, anomalous_flag=anomalous_flag) all_set = all_set.resolution_filter(d_min=self.d_min) # dummy for redundancy calculation. dirty way.. if calc_redundancy: dummy_array = miller.array(miller_set=all_set, data=flex.int(all_set.size())) merge = dummy_array.merge_equivalents() cmpl = merge.array().completeness() redun = merge.redundancies().as_double().mean() return cmpl, redun else: cmpl = all_set.unique_under_symmetry().completeness() return cmpl
def load_sfall(fname): """ special script for loading the structure factor file generated in main() :param fname: file generated in the main method above.. :return: mil_ar, energies mil_ar: dict of miller arrays (complex) energies: array of xray energies in electron volts such that mil_ar[0] is Fhkl at energy energies[0] """ f = h5py.File(fname, "r") data = f["data"][()] indices = f["indices"][()] hall = f["hall_symbol"][()] ucell_param = f["ucell_tuple"][()] energies = f["energies"][()] sg = sgtbx.space_group(hall) Symm = crystal.symmetry(unit_cell=ucell_param, space_group=sg) indices_flex = tuple(map(tuple, indices)) mil_idx = flex.miller_index(indices_flex) mil_set = miller.set(crystal_symmetry=Symm, indices=mil_idx, anomalous_flag=True) mil_ar = {} # load a dict of "sfall at each energy" for i_chan, data_chan in enumerate(data): data_flex = flex.complex_double(np.ascontiguousarray(data_chan)) mil_ar[i_chan] = miller.array(mil_set, data=data_flex) return mil_ar, energies
def test_grouped_observations(): # some dummy observations for one reflection I1, I2, I3 = 101, 100, 99 w1, w2, w3 = 0.9, 1.0, 0.8 g1, g2, g3 = 1.01, 1.0, 0.99 # combine with observations from some other group to make a dataset hkl = flex.miller_index([(0,0,1)] * 3 + [(0,0,2)] * 2) intensity = flex.double([I1, I2, I3, 10, 10]) weight = flex.double([w1, w2, w3, 1.0, 1.0]) phi = flex.double(len(hkl), 0) # dummy scale = flex.double([g1, g2, g3, 1.0, 1.0]) # group the observations by Miller index go = GroupedObservations(hkl, intensity, weight, phi, scale) # ensure there are two groups, the first of size 3, the second of size 2 assert list(go.get_group_size()) == [3,2] # the first group has an average intensity given by the HRS formula expanded: avI = (w1*g1*I1 + w2*g2*I2 + w3*g3*I3) / (w1*g1*g1 + w2*g2*g2 + w3*g3*g3) assert approx_equal(avI, go.get_average_intensity()[0]) print "OK"
def pha2mtz(phain, xs, mtzout): hkl, f, fom, phi, sigf = [], [], [], [], [] for l in open(phain): sp = l.split() if len(sp) != 7: break hkl.append(tuple(map(int, sp[:3]))) f.append(float(sp[3])) fom.append(float(sp[4])) phi.append(float(sp[5])) sigf.append(float(sp[6])) if not hkl: return f_array = miller.array(miller.set(xs, flex.miller_index(hkl)), data=flex.double(f), sigmas=flex.double(sigf)) mtz_ds = f_array.as_mtz_dataset( column_root_label="ANOM", column_types="FQ" ) # To open with Coot, column type F is required (not D) mtz_ds.add_miller_array(f_array.customized_copy(data=flex.double(phi), sigmas=None), column_root_label="PANOM", column_types="P") mtz_ds.add_miller_array(f_array.customized_copy(data=flex.double(fom), sigmas=None), column_root_label="FOM", column_types="W") mtz_ds.mtz_object().write(mtzout)
def get_miller_indices_containing_loops(self): loops = [] for loop in self.cif_block.loops.values(): for key in loop.keys(): if 'index_h' not in key: continue hkl_str = [ loop.get(key.replace('index_h', 'index_%s' % i)) for i in 'hkl' ] if hkl_str.count(None) > 0: raise CifBuilderError( "Miller indices missing from current CIF block (%s)" % key.replace('index_h', 'index_%s' % 'hkl'[hkl_str.index(None)])) hkl_int = [] for i, h_str in enumerate(hkl_str): try: h_int = flex.int(h_str) except ValueError, e: raise CifBuilderError( "Invalid item for Miller index %s: %s" % ("HKL"[i], str(e))) hkl_int.append(h_int) indices = flex.miller_index(*hkl_int) loops.append((indices, loop)) break
def observed_indices_and_angles_from_angle_range(self, phi_start_rad, phi_end_rad, indices): """For a list of indices, return those indices that can be rotated into diffracting position, along with the corresponding angles""" # calculate conversion matrix to rossmann frame. R_to_rossmann = self.align_reference_frame( self._beam.get_unit_s0(), (0.0, 0.0, 1.0), self._gonio.get_rotation_axis(), (0.0, 1.0, 0.0)) # Create a rotation_angles object for the current geometry ra = rotation_angles(self._dmin, R_to_rossmann * self._crystal.get_U() * self._crystal.get_B(), self._beam.get_wavelength(), R_to_rossmann * matrix.col(self._gonio.get_rotation_axis())) (obs_indices, obs_angles) = ra.observed_indices_and_angles_from_angle_range( phi_start_rad = 0.0, phi_end_rad = pi, indices = indices) # convert to integer miller indices obs_indices_int = flex.miller_index() for hkl in obs_indices: hkl = map(lambda x: int(round(x)), hkl) obs_indices_int.append(tuple(hkl)) return obs_indices_int, obs_angles
def exercise_match_bijvoet_mates(): h0 = flex.miller_index(((1,2,3), (-1,-2,-3), (2,3,4), (-2,-3,-4), (3,4,5))) d0 = flex.double((1,2,3,4,5)) bm = miller.match_bijvoet_mates( sgtbx.space_group_type(), h0) bm = miller.match_bijvoet_mates( sgtbx.reciprocal_space_asu(sgtbx.space_group_type()), h0) bm = miller.match_bijvoet_mates( h0) assert tuple(bm.pairs()) == ((0,1), (2,3)) assert tuple(bm.singles("+")) == (4,) assert tuple(bm.singles("-")) == () assert bm.n_singles() != 0 assert tuple(bm.pairs_hemisphere_selection("+")) == (0, 2) assert tuple(bm.pairs_hemisphere_selection("-")) == (1, 3) assert tuple(bm.singles_hemisphere_selection("+")) == (4,) assert tuple(bm.singles_hemisphere_selection("-")) == () assert tuple(bm.miller_indices_in_hemisphere("+")) == ((1,2,3), (2,3,4)) assert tuple(bm.miller_indices_in_hemisphere("-")) == ((-1,-2,-3),(-2,-3,-4)) assert approx_equal(tuple(bm.minus(d0)), (-1, -1)) assert approx_equal(tuple(bm.additive_sigmas(d0)), [math.sqrt(x*x+y*y) for x,y in ((1,2), (3,4))]) assert approx_equal(tuple(bm.average(d0)), (3/2., 7/2.)) h0.append((1,2,3)) try: miller.match_bijvoet_mates(h0) except Exception: pass else: raise Exception_expected
def run(args): from dials.util.options import OptionParser from dials.util.options import flatten_experiments import libtbx.load_env usage = "%s [options] experiments.json" %libtbx.env.dispatcher_name parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, check_format=False, epilog=help_message) params, options = parser.parse_args(show_diff_phil=True) experiments = flatten_experiments(params.input.experiments) if len(experiments) <= 1: parser.print_help() return hkl = flex.miller_index(params.hkl) from dials.algorithms.indexing.compare_orientation_matrices import \ show_rotation_matrix_differences crystals = [] for experiment in experiments: crystal = experiment.crystal if params.space_group is not None: crystal.set_space_group(params.space_group.group()) crystals.append(crystal) show_rotation_matrix_differences(crystals, miller_indices=hkl, comparison=params.comparison)
def generate_table(complex_data, indices, numpy_args=False, anom=True): """ :param complex_data: structure factors :param indices: miller indices in a list, Nx3 :param numpy_args: are the complex data and indices numpy type, if not assume flex :param anom: return a miller array with +H and -H ? :return: dictionary whose keys are miller index tuple and values are structure fact """ sg = sgtbx.space_group(" P 4nw 2abw") Symm = crystal.symmetry(unit_cell=(79, 79, 38, 90, 90, 90), space_group=sg) if numpy_args: assert type(indices) == tuple assert (type(indices[0]) == tuple) indices = flex.miller_index(indices) mil_set = miller.set(crystal_symmetry=Symm, indices=indices, anomalous_flag=anom) if numpy_args: complex_data = flex.complex_double(np.ascontiguousarray(complex_data)) mil_ar = miller.array(mil_set, data=complex_data) mil_dict = {h: val for h, val in izip(mil_ar.indices(), mil_ar.data())} return mil_dict
def run(args=None): usage = "dials.compare_orientation_matrices [options] models.expt" parser = ArgumentParser( usage=usage, phil=phil_scope, read_experiments=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args, show_diff_phil=True) experiments = flatten_experiments(params.input.experiments) if len(experiments) <= 1: parser.print_help() return hkl = flex.miller_index(params.hkl) crystals = [] for experiment in experiments: crystal = experiment.crystal if params.space_group is not None: crystal.set_space_group(params.space_group.group()) crystals.append(crystal) rmd = dials.algorithms.indexing.compare_orientation_matrices.rotation_matrix_differences( crystals, miller_indices=hkl, comparison=params.comparison) print(rmd)
def tst_pgtools(): unit_cell = uctbx.unit_cell('40, 40, 60, 90.0, 90.0, 90.0') mi = flex.miller_index(((2, 4, 6), (2, 4, 8))) xs = crystal.symmetry(unit_cell, "P 1 2 1") ms = miller.set(xs, mi) # Go to the minimum cell, for safety cob_min_cell = ms.change_of_basis_op_to_minimum_cell() ms_new = ms.change_basis(cob_min_cell) lattice_group = sgtbx.lattice_symmetry.group(ms_new.unit_cell(), max_delta=5.0) point_group_low = ms_new.space_group().build_derived_point_group() point_group_high = lattice_group.build_derived_point_group() pgtree = pt.point_group_graph(point_group_low, point_group_high) # find the possible routes from 'P 2' to 'P 4 2 2' atlas = pgtree.graph.find_all_paths('P 1 2 1', 'P 4 2 2') route_1 = ['P 1 2 1', 'P 4 2 2'] route_2 = ['P 1 2 1', 'P 2 2 2', 'P 4 2 2'] assert route_1 in atlas assert route_2 in atlas assert len(atlas) == 2 # Now lets 'disqualify' point group 'P 2 2 2' pgtree.remove_point_group_and_its_super_groups_from_graph( str(sgtbx.space_group_info(16))) assert len(pgtree.graph.node_objects) == 1 assert pgtree.graph.node_objects.has_key('P 1 2 1')
def aniso_convert(uc,sg,map,res): os.system('hkl2vtk %s_friedel.hkl %s_friedel.vtk %s_raw.vtk' %(map,map,map)) os.system('vtk2lat %s_friedel.vtk %s_friedel.lat' %(map,map)) os.system('avgrlt %s_friedel.lat %s_friedel.rf' %(map,map)) os.system('subrflt %s_friedel.rf %s_friedel.lat anisotropic.lat' %(map,map)) os.system('lat2hkl anisotropic.lat anisotropic.hkl') #Read in hkl file and populate miller array from cctbx import crystal inf = open('anisotropic.hkl', "r") indices = flex.miller_index() i_obs = flex.double() #sig_i = flex.double() for line in inf.readlines(): assert len(line.split())==4 line = line.strip().split() i_obs_ = float(line[3])#/10000 #10000 is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) #sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros if i_obs_ != -32768.0: indices.append([int(line[0]),int(line[1]),int(line[2])]) i_obs.append(i_obs_) #sig_i.append(sig_i_) inf.close() #Get miller array object cs = crystal.symmetry(unit_cell=(float(uc[0]), float(uc[1]), float(uc[2]), float(uc[3]), float(uc[4]), float(uc[5])), space_group=sg) miller_set=miller.set(cs, indices) ma = miller.array(miller_set=miller_set, data=i_obs, sigmas=None) mtz_dataset = ma.as_mtz_dataset(column_root_label="Amplitude") mtz_dataset.mtz_object().write('%s_anisotropic.mtz' %map)
def test_minimum_multiplicity_selection(): # some groups of indices with increasing multiplicity hkl = flex.miller_index( [(0,0,1), (0,0,2), (0,0,2), (0,0,3), (0,0,3), (0,0,3), (0,0,4), (0,0,4), (0,0,4), (0,0,4), (0,0,5), (0,0,5), (0,0,5), (0,0,5), (0,0,5)]) # test the various possible selections with multiplicity from 1 to 6 sel = minimum_multiplicity_selection(hkl, 1) assert sel.count(True) == len(hkl) sel = minimum_multiplicity_selection(hkl, 2) assert (sel == flex.bool([False,] + [True] * 14)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 3) assert (sel == flex.bool([False] * 3 + [True] * 12)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 4) assert (sel == flex.bool([False] * 6 + [True] * 9)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 5) assert (sel == flex.bool([False] * 10 + [True] * 5)).all_eq(True) sel = minimum_multiplicity_selection(hkl, 6) assert sel.count(False) == len(hkl) print "OK"
def read_x(xfile, take_full=False): """ according to http://www.hkl-xray.com/denzo-output Regular (film output file, Denzo _ip) imaging plate output format (i.e. the format you would you to read in in FORTRAN) is: format (3i4,i2,2f8.0,f7.0,f6.0,f6.0,2f7.0,f6.0,f8.0) h,k,l Flag 0 - full 1 - partial Intensity (F**2) by profile fitting s of intensity (F**2) c2 of profile fitting Intensity (F**2) by profile summation Cosine of incidence angle at detector Predicted pixel position of spot centroid (slow, fast) directions Lorentz, polarization, obliquity combined factor Strength of averaged profile in arbitrary units """ lines = open(xfile).readlines() indices = flex.miller_index() data = flex.double() sigmas = flex.double() full_flags = flex.bool() cell, sg_str = None, None read_hkl = True for l in lines[5:]: if l.startswith(" 999") and len(l.split()) == 3: read_hkl = False if read_hkl: hkl = map(int, (l[0:4], l[4:8], l[8:12])) ispart = int(l[13]) iobs, sigma = map(float, (l[14:22], l[22:30])) indices.append(hkl) data.append(iobs) sigmas.append(sigma) full_flags.append(ispart==0) else: if l.startswith("unit cell"): cell = map(float, l.split()[2:]) elif l.startswith("space group"): sg_str = l.split()[-1] #sg_str="p1" symm = crystal.symmetry(unit_cell=cell, space_group=sg_str) array = miller.array(miller_set=miller.set(crystal_symmetry=symm, indices=indices, anomalous_flag=True), data=data, sigmas=sigmas).set_observation_type_xray_intensity() if take_full: array = array.select(full_flags) return array
def exercise(prefix="tst_models_to_from_chains"): # inputs expected_n = 5 xrs = iotbx.pdb.input(source_info=None, lines=pdb_str).xray_structure_simple() input_file_name = "%s.pdb"%prefix of = open(input_file_name,"w") print >> of, pdb_str of.close() mi = flex.miller_index(((0,0,1), )) ms = miller.set(xrs.crystal_symmetry(), mi, anomalous_flag=False) complete_set = ms.complete_set(d_min=3) fc = complete_set.structure_factors_from_scatterers( xray_structure=xrs).f_calc() # models -> chains easy_run.call("phenix.models_as_chains %s"%input_file_name) pdb_inp = iotbx.pdb.input(file_name="chains_"+input_file_name) h = pdb_inp.construct_hierarchy() assert len(list(h.chains()))==expected_n assert len(list(h.models()))==1 xrs_c = pdb_inp.xray_structure_simple() fc_c = complete_set.structure_factors_from_scatterers( xray_structure=xrs_c).f_calc() # easy_run.call("phenix.chains_as_models chains_%s"%input_file_name) pdb_inp = iotbx.pdb.input(file_name="models_chains_"+input_file_name) h = pdb_inp.construct_hierarchy() assert len(list(h.chains()))==expected_n assert len(list(h.models()))==expected_n xrs_m = pdb_inp.xray_structure_simple() fc_m = complete_set.structure_factors_from_scatterers( xray_structure=xrs_m).f_calc() # assert approx_equal(0, r(fc, fc_c) ) assert approx_equal(0, r(fc, fc_m) ) assert approx_equal(0, r(fc_c, fc_m))
def __init__(self, data, lattice_id, resort=False, verbose=True): self.verbose = verbose ###### INPUTS ####### # data = miller array: ASU miller index & intensity # lattice_id = flex double: assignment of each miller index to a lattice number ###################### if resort: order = flex.sort_permutation(lattice_id.data()) sorted_lattice_id = flex.select(lattice_id.data(), order) sorted_data = data.data().select(order) sorted_indices = data.indices().select(order) self.lattice_id = sorted_lattice_id self.data = data.customized_copy(indices=sorted_indices, data=sorted_data) else: self.lattice_id = lattice_id.data() # type flex int self.data = data # type miller array with flex double data assert type(self.data.indices()) == type(flex.miller_index()) assert type(self.data.data()) == type(flex.double()) # construct a lookup for the separate lattices last_id = -1 self.lattices = flex.int() for n in xrange(len(self.lattice_id)): if self.lattice_id[n] != last_id: last_id = self.lattice_id[n] self.lattices.append(n)
def as_mtz(self,scale_factor,prefix): #This reads in an hkl map and returns a .mtz map #Read in hkl file and populate miller array inf = open(self.diffuse_file, 'r') indices = flex.miller_index() i_obs = flex.double() sig_i = flex.double() for line in inf.readlines(): assert len(line.split())==4 line = line.strip().split() #####ATTENTION:SCALE FACTOR############## i_obs_ = float(line[3])/scale_factor #is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros indices.append([int(line[0]),int(line[1]),int(line[2])]) i_obs.append(i_obs_) sig_i.append(sig_i_) inf.close() # get miller array object cs = self.symmetry ma = miller.array(miller_set=miller.set(cs, indices), data=i_obs, sigmas=sig_i) ma.set_observation_type_xray_intensity() mtz_dataset = ma.as_mtz_dataset(column_root_label="I") mtz_dataset.mtz_object().write(prefix + '.mtz')
def as_miller_array(gsas_exp, gsas_rfl, divide_pr, phase_id=1, histogram_id=1): from cctbx import miller from cctbx import crystal from cctbx.array_family import flex from scitbx.python_utils import easy_pickle info = gsas_exp.DESCR if (divide_pr): info += ", FIPS partitioning" indices = flex.miller_index() data = flex.double() if (not divide_pr): sigmas = flex.double() for r in gsas_rfl.records: indices.append(r.ihkl) data.append(r.fosq) sigmas.append(r.sig) else: sigmas = None for r in divide_pr.records: indices.append(r.hkl) data.append(r.fnew**2) miller_array = miller.array( miller_set=miller.set(crystal_symmetry=crystal.symmetry( unit_cell=gsas_exp.get_unit_cell_parameters(phase_id), space_group_symbol=gsas_exp.get_space_group_symbol(phase_id)), indices=indices, anomalous_flag=00000), data=data, sigmas=sigmas).set_info(info).set_observation_type_xray_intensity() miller_array.show_comprehensive_summary() print "Writing file: miller_array.pickle" easy_pickle.dump("miller_array.pickle", miller_array)
def compare_with_cctbx_structure_factors(n_scatt, n_refl, output_lines): from cctbx import xray from cctbx import miller from cctbx import crystal from cctbx.array_family import flex crystal_symmetry = crystal.symmetry(unit_cell=(11, 12, 13, 90, 90, 90), space_group_symbol="P1") scatterers = flex.xray_scatterer() miller_indices = flex.miller_index() f_calc = flex.complex_double() for line in output_lines: flds = line.split() assert len(flds) in [4, 5] if (len(flds) == 4): x, y, z, b_iso = [float(s) for s in flds] scatterers.append( xray.scatterer(site=(x, y, z), b=b_iso, scattering_type="const")) else: miller_indices.append([int(s) for s in flds[:3]]) f_calc.append(complex(float(flds[3]), float(flds[4]))) assert scatterers.size() == n_scatt assert miller_indices.size() == n_refl xs = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=scatterers) fc = miller_array = miller.set(crystal_symmetry=crystal_symmetry, indices=miller_indices, anomalous_flag=False).array(data=f_calc) fc2 = fc.structure_factors_from_scatterers(xray_structure=xs, algorithm="direct", cos_sin_table=False).f_calc() for f1, f2 in zip(fc.data(), fc2.data()): assert approx_equal(f1, f2, eps=1e-5)
def tst_pgtools(): unit_cell = uctbx.unit_cell('40, 40, 60, 90.0, 90.0, 90.0') mi = flex.miller_index(((2,4,6), (2,4,8))) xs = crystal.symmetry(unit_cell, "P 1 2 1") ms = miller.set(xs, mi) # Go to the minimum cell, for safety cob_min_cell = ms.change_of_basis_op_to_minimum_cell() ms_new = ms.change_basis( cob_min_cell ) lattice_group = sgtbx.lattice_symmetry.group( ms_new.unit_cell(), max_delta=5.0) point_group_low = ms_new.space_group().build_derived_point_group() point_group_high = lattice_group.build_derived_point_group() pgtree = pt.point_group_graph(point_group_low,point_group_high) # find the possible routes from 'P 2' to 'P 4 2 2' atlas = pgtree.graph.find_all_paths( 'P 1 2 1', 'P 4 2 2') route_1 = ['P 1 2 1', 'P 4 2 2'] route_2 = ['P 1 2 1', 'P 2 2 2', 'P 4 2 2'] assert route_1 in atlas assert route_2 in atlas assert len(atlas)==2 # Now lets 'disqualify' point group 'P 2 2 2' pgtree.remove_point_group_and_its_super_groups_from_graph( str(sgtbx.space_group_info(16))) assert len(pgtree.graph.node_objects)==1 assert pgtree.graph.node_objects.has_key ( 'P 1 2 1' )
def _compute_delta_ccs(self): delta_cc = flex.double() for (group_start, group_end), test_k in zip(self._group_to_batches, self._group_to_dataset_id): batches = self.batches[test_k].data() group_sel = (batches >= group_start) & (batches <= group_end) indices_i = flex.miller_index() data_i = flex.double() sigmas_i = flex.double() for k, unmerged in enumerate(self.intensities): if k == test_k: unmerged = unmerged.select(~group_sel) indices_i.extend(unmerged.indices()) data_i.extend(unmerged.data()) sigmas_i.extend(unmerged.sigmas()) unmerged_i = self.intensities[0].customized_copy(indices=indices_i, data=data_i, sigmas=sigmas_i) delta_cc.append(self._compute_delta_cc_for_dataset(unmerged_i)) logger.debug( u"Delta CC½ excluding batches %i-%i: %.3f", group_start, group_end, delta_cc[-1], ) return delta_cc
def exercise_systematic(verbose): from cctbx import miller from cctbx import crystal from cctbx.array_family import flex cs = crystal.symmetry( unit_cell=(13,15,14,90,90,100), space_group_symbol="P112") ms = miller.set( crystal_symmetry=cs, indices=flex.miller_index([ (0,0,1),(0,0,-1), (0,1,1), (1,0,0), (-1,-1,-1)]), anomalous_flag=True).map_to_asu() cf = ms.centric_flags().data() assert cf.count(True) == 1 mt = flex.mersenne_twister(seed=0) ma = ms.array( data=mt.random_double(size=5)+0.1, sigmas=mt.random_double(size=5)+0.1) def recycle(expected_column_data): mtz_obj = ma.as_mtz_dataset(column_root_label="X").mtz_object() sio = StringIO() mtz_obj.show_column_data_human_readable(out=sio) from libtbx.test_utils import show_diff if (verbose): sys.stdout.write(sio.getvalue()) assert not show_diff(sio.getvalue(), expected_column_data) ma_2 = only_element(mtz_obj.as_miller_arrays()) assert_equal_data_and_sigmas(ma, ma_2) recycle("""\ Column data: ------------------------------------------------------------------------------- X(+) SIGX(+) X(-) SIGX(-) 0 0 1 0.517022 0.192339 0.820324 0.28626 0 1 1 0.100114 0.445561 None None 1 0 0 0.402333 0.496767 None None 1 1 1 None None 0.246756 0.638817 ------------------------------------------------------------------------------- """) from cctbx.xray import observation_types ma.set_observation_type(observation_types.reconstructed_amplitude()) recycle("""\ Column data: ------------------------------------------------------------------------------- X SIGX DANOX SIGDANOX ISYMX 0 0 1 0.668673 0.172438 -0.303302 0.344875 0 0 1 1 0.100114 0.445561 None None 1 1 0 0 0.402333 0.496767 None None 0 1 1 1 0.246756 0.638817 None None 2 ------------------------------------------------------------------------------- """)
def dummy_miller_set(crystal_symmetry, log_n_reflections): indices = flex.miller_index(((1,2,3),)) for i in xrange(log_n_reflections): indices.append(indices) return miller.set( crystal_symmetry=crystal_symmetry, indices=indices, anomalous_flag=False)
def exercise_flex_miller_index(): from scitbx.array_family.flex import exercise_triple exercise_triple(flex_triple=flex.miller_index, flex_order=flex.order) a = flex.miller_index([(1,2,3), (2,3,4)]) assert approx_equal(a.as_vec3_double(), [(1,2,3), (2,3,4)]) h, k, l = [flex.int((0,1,2,3)), flex.int((1,2,3,4)), flex.int((2,3,4,5))] b = flex.miller_index(h, k, l) assert approx_equal(b, ((0,1,2),(1,2,3),(2,3,4),(3,4,5))) # i = flex.miller_index([(1,-2,3), (-2,3,4)]) c = flex.complex_double([3-4j, -7+8j]) x = (0.9284, -1.2845, -0.2293) assert approx_equal( i.fourier_transform_real_part_at_x(fourier_coeffs=c, x=x), 15.4357188164)