def index_reflections( reflections, experiments, d_min=None, tolerance=0.3, verbosity=0): reciprocal_lattice_points = reflections['rlp'] reflections['miller_index'] = flex.miller_index(len(reflections), (0,0,0)) if d_min is not None: d_spacings = 1/reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool(reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections['id'] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs['xyzobs.mm.value'].parts()[2] diffs = [] norms = [] hkl_ints = [] UB_matrices = flex.mat3_double([cm.get_A() for cm in experiments.crystals()]) imgset_ids = reflections['imageset_id'].select(sel) for i_imgset, imgset in enumerate(experiments.imagesets()): sel_imgset = (imgset_ids == i_imgset) result = AssignIndices( rlps.select(sel_imgset), phi.select(sel_imgset), UB_matrices, tolerance=tolerance) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() n_rejects = result.n_rejects() expt_ids = flex.int(crystal_ids.size(), -1) for i_cryst, cryst in enumerate(experiments.crystals()): sel_cryst = (crystal_ids == i_cryst) for i_expt in experiments.where( crystal=cryst, imageset=imgset): expt_ids.set_selected(sel_cryst, i_expt) reflections['miller_index'].set_selected(isel.select(sel_imgset), miller_indices) reflections['id'].set_selected(isel.select(sel_imgset), expt_ids) reflections.set_flags( reflections['miller_index'] != (0,0,0), reflections.flags.indexed) if verbosity > 0: for i_expt, expt in enumerate(experiments): info("model %i (%i reflections):" %( i_expt+1, (reflections['id'] == i_expt).count(True))) info(expt.crystal) info("") info("%i unindexed reflections" %n_rejects)
def __call__(self, reflections, experiments, d_min=None): reciprocal_lattice_points = reflections["rlp"] reflections["miller_index"] = flex.miller_index( len(reflections), (0, 0, 0)) if d_min is not None: d_spacings = 1 / reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool( reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections["id"] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs["xyzobs.mm.value"].parts()[2] UB_matrices = flex.mat3_double( [cm.get_A() for cm in experiments.crystals()]) imgset_ids = reflections["imageset_id"].select(sel) for i_imgset, imgset in enumerate(experiments.imagesets()): sel_imgset = imgset_ids == i_imgset result = ext.AssignIndices( rlps.select(sel_imgset), phi.select(sel_imgset), UB_matrices, tolerance=self._tolerance, ) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() expt_ids = flex.int(crystal_ids.size(), -1) for i_cryst, cryst in enumerate(experiments.crystals()): sel_cryst = crystal_ids == i_cryst for i_expt in experiments.where(crystal=cryst, imageset=imgset): expt_ids.set_selected(sel_cryst, i_expt) if experiments[i_expt].identifier: reflections.experiment_identifiers( )[i_expt] = experiments[i_expt].identifier reflections["miller_index"].set_selected(isel.select(sel_imgset), miller_indices) reflections["id"].set_selected(isel.select(sel_imgset), expt_ids) reflections.set_flags(reflections["miller_index"] != (0, 0, 0), reflections.flags.indexed) reflections["id"].set_selected( reflections["miller_index"] == (0, 0, 0), -1)
def index_reflections(reflections, experiments, d_min=None, tolerance=0.3): reciprocal_lattice_points = reflections['rlp'] reflections['miller_index'] = flex.miller_index(len(reflections), (0, 0, 0)) if d_min is not None: d_spacings = 1 / reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool(reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections['id'] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs['xyzobs.mm.value'].parts()[2] diffs = [] norms = [] hkl_ints = [] UB_matrices = flex.mat3_double( [cm.get_A() for cm in experiments.crystals()]) imgset_ids = reflections['imageset_id'].select(sel) for i_imgset, imgset in enumerate(experiments.imagesets()): sel_imgset = (imgset_ids == i_imgset) result = AssignIndices(rlps.select(sel_imgset), phi.select(sel_imgset), UB_matrices, tolerance=tolerance) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() expt_ids = flex.int(crystal_ids.size(), -1) for i_cryst, cryst in enumerate(experiments.crystals()): sel_cryst = (crystal_ids == i_cryst) for i_expt in experiments.where(crystal=cryst, imageset=imgset): expt_ids.set_selected(sel_cryst, i_expt) reflections['miller_index'].set_selected(isel.select(sel_imgset), miller_indices) reflections['id'].set_selected(isel.select(sel_imgset), expt_ids) reflections.set_flags(reflections['miller_index'] != (0, 0, 0), reflections.flags.indexed) reflections['id'].set_selected( reflections['miller_index'] == (0, 0, 0), -1)
def index_reflections(reflections, experiments, d_min=None, tolerance=0.3): reciprocal_lattice_points = reflections["rlp"] reflections["miller_index"] = flex.miller_index(len(reflections), (0, 0, 0)) if d_min is not None: d_spacings = 1 / reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool(reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections["id"] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs["xyzobs.mm.value"].parts()[2] diffs = [] norms = [] hkl_ints = [] UB_matrices = flex.mat3_double([cm.get_A() for cm in experiments.crystals()]) imgset_ids = reflections["imageset_id"].select(sel) for i_imgset, imgset in enumerate(experiments.imagesets()): sel_imgset = imgset_ids == i_imgset result = AssignIndices(rlps.select(sel_imgset), phi.select(sel_imgset), UB_matrices, tolerance=tolerance) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() expt_ids = flex.int(crystal_ids.size(), -1) for i_cryst, cryst in enumerate(experiments.crystals()): sel_cryst = crystal_ids == i_cryst for i_expt in experiments.where(crystal=cryst, imageset=imgset): expt_ids.set_selected(sel_cryst, i_expt) reflections["miller_index"].set_selected(isel.select(sel_imgset), miller_indices) reflections["id"].set_selected(isel.select(sel_imgset), expt_ids) reflections.set_flags(reflections["miller_index"] != (0, 0, 0), reflections.flags.indexed)
def index_reflections_local(reflections, experiments, d_min=None, epsilon=0.05, delta=8, l_min=0.8, nearest_neighbours=20): from scitbx import matrix from libtbx.math_utils import nearest_integer as nint reciprocal_lattice_points = reflections['rlp'] if 'miller_index' not in reflections: reflections['miller_index'] = flex.miller_index(len(reflections)) if d_min is not None: d_spacings = 1 / reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool(reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections['id'] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs['xyzobs.mm.value'].parts()[2] if len(rlps) <= nearest_neighbours: from libtbx.utils import Sorry raise Sorry( "index_assignment.local.nearest_neighbour must be smaller than the number of accepted reflections (%d)" % len(rlps)) diffs = [] norms = [] hkl_ints = [] UB_matrices = flex.mat3_double( [cm.get_A() for cm in experiments.crystals()]) result = AssignIndicesLocal(rlps, phi, UB_matrices, epsilon=epsilon, delta=delta, l_min=l_min, nearest_neighbours=nearest_neighbours) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() hkl = miller_indices.as_vec3_double().iround() assert miller_indices.select(crystal_ids < 0).all_eq((0, 0, 0)) for i_cryst in set(crystal_ids): if i_cryst < 0: continue A = experiments[i_cryst].crystal.get_A() A_inv = A.inverse() cryst_sel = crystal_ids == i_cryst ref_sel = refs.select(cryst_sel) rlp_sel = rlps.select(cryst_sel) hkl_sel = hkl.select(cryst_sel).as_vec3_double() d_sel = 1 / rlp_sel.norms() d_perm = flex.sort_permutation(d_sel, reverse=True) hf_0 = A_inv * rlp_sel[d_perm[0]] h_0 = matrix.col([nint(j) for j in hf_0.elems]) offset = h_0 - matrix.col(hkl_sel[d_perm[0]]) #print "offset:", offset.elems h = hkl_sel + flex.vec3_double(hkl_sel.size(), offset.elems) refs['miller_index'].set_selected(cryst_sel, flex.miller_index(list(h.iround()))) refs['id'].set_selected(cryst_sel, i_cryst) crystal_ids.set_selected(crystal_ids < 0, -1) refs['id'] = crystal_ids refs['miller_index'].set_selected(crystal_ids < 0, (0, 0, 0)) reflections['miller_index'].set_selected(isel, refs['miller_index']) reflections['id'].set_selected(isel, refs['id']) reflections.set_flags(reflections['miller_index'] != (0, 0, 0), reflections.flags.indexed)
def ewald_proximity_test(self, beams, hkl_list, A, detector): ''' test used to determine which spots are on the Ewald sphere, returns a dictionary of results including a reflection table, spots arrays and orientation matrix ''' wavelength1 = beams[0].get_wavelength() wavelength2 = beams[1].get_wavelength() s0_1 = col((0, 0, -1 / wavelength1)) s0_2 = col((0, 0, -1 / wavelength2)) #create variables for h arrays filtered1 = flex.miller_index() #create variables for spots indices spots1 = flex.vec2_double() #create variables for h arrays filtered2 = flex.miller_index() #create variables for spots indices spots2 = flex.vec2_double() self.A = A q = flex.mat3_double([A] * len( hkl_list.all_selection())) * hkl_list.indices().as_vec3_double() EQ1 = q + s0_1 EQ2 = q + s0_2 len_EQ1 = flex.double([col(v).length() for v in EQ1]) ratio1 = len_EQ1 * wavelength1 len_EQ2 = flex.double([col(v).length() for v in EQ2]) ratio2 = len_EQ2 * wavelength2 for i in range(len(EQ1)): if ratio1[i] > 0.998 and ratio1[i] < 1.002: pix = detector[0].get_ray_intersection_px(EQ1[i]) if detector[0].is_coord_valid(pix): spots1.append(pix) filtered1.append(hkl_list.indices()[i]) for i in range(len(EQ2)): if ratio2[i] > 0.998 and ratio2[i] < 1.002: pix = detector[0].get_ray_intersection_px(EQ2[i]) if detector[0].is_coord_valid(pix): spots2.append(pix) filtered2.append(hkl_list.indices()[i]) #create reflection table #filtered s1 and r for reflection table spots = spots1.concatenate(spots2) lab_coord1 = flex.vec3_double( [detector[0].get_pixel_lab_coord(i) for i in spots1]) lab_coord2 = flex.vec3_double( [detector[0].get_pixel_lab_coord(i) for i in spots2]) s1_vecs1 = lab_coord1.each_normalize() * (1 / wavelength1) s0_vec1 = col((0, 0, -1 / wavelength1)) r_vecs1 = s1_vecs1 - s0_vec1 s1_vecs2 = lab_coord2.each_normalize() * (1 / wavelength2) s0_vec2 = col((0, 0, -1 / wavelength2)) r_vecs2 = s1_vecs2 - s0_vec2 #create one large reflection table for both experiments x_px, y_px = tuple(spots.parts()[0:2]) px_array = flex.vec3_double(x_px, y_px, flex.double(len(x_px), 0.0)) px_to_mm = detector[0].pixel_to_millimeter(spots) x_mm, y_mm = tuple(px_to_mm.parts()[0:2]) mm_array = flex.vec3_double(x_mm, y_mm, flex.double(len(x_px), 0.0)) px_var = flex.vec3_double(flex.double(len(x_px), 0.25), flex.double(len(x_px), 0.25), flex.double(len(x_px), 0.0)) px_sq_err = flex.vec3_double(flex.double(len(x_px), 0.1), flex.double(len(x_px), 0.1), flex.double(len(x_px), 0.0)) mm_var = flex.vec3_double(flex.double(len(x_px), 0.05), flex.double(len(x_px), 0.05), flex.double(len(x_px), 0.05)) mm_sq_err = flex.vec3_double(flex.double(len(x_px), 0.01), flex.double(len(x_px), 0.01), flex.double(len(x_px), 0.01)) obs_array = get_dials_obs(px_positions=px_array, px_var=px_var, px_sq_err=px_sq_err, mm_positions=mm_array, mm_var=mm_var, mm_sq_err=mm_sq_err) shoeboxes = flex.shoebox(len(obs_array)) refl = flex.reflection_table(obs_array, shoeboxes) refl['id'] = flex.int(len(refl), -1) s1_vecs = s1_vecs1.concatenate(s1_vecs2) refl['s1'] = s1_vecs refl['xyzobs.px.variance'] = px_var refl['xyzobs.mm.value'] = mm_array refl['xyzobs.mm.variance'] = flex.vec3_double([ (v[0] * 0.005, v[1] * 0.005, v[2] * 0.0) for v in refl['xyzobs.px.variance'] ]) filtered = filtered1.concatenate(filtered2) refl[ 'miller_index'] = filtered # is reset to all 0s once close spots are merged bbox = flex.int6([(0, 1, 0, 1, 0, 1)] * len(refl)) refl['bbox'] = bbox #refl['rlp'] = r_vecs refl['xyzobs.px.value'] = px_array # define experiment ids for each wavelength to check simulation with #indexing results in separate new column of reflection table not used in indexing exp_id1 = flex.int(len(spots1), 0) exp_id2 = flex.int(len(spots2), 1) exp_id = exp_id1.concatenate(exp_id2) refl['set_id'] = exp_id self.refl = refl sim_res = { 'reflection_table': refl, 'all_spots': spots, 'wavelength_1_spots': spots1, 'wavelength_2_spots': spots2, 'input_orientation': A } return sim_res
def index_reflections_local( reflections, experiments, d_min=None, epsilon=0.05, delta=8, l_min=0.8, nearest_neighbours=20, verbosity=0): from scitbx import matrix from libtbx.math_utils import nearest_integer as nint reciprocal_lattice_points = reflections['rlp'] if 'miller_index' not in reflections: reflections['miller_index'] = flex.miller_index(len(reflections)) if d_min is not None: d_spacings = 1/reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool(reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections['id'] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs['xyzobs.mm.value'].parts()[2] diffs = [] norms = [] hkl_ints = [] UB_matrices = flex.mat3_double([cm.get_A() for cm in experiments.crystals()]) result = AssignIndicesLocal( rlps, phi, UB_matrices, epsilon=epsilon, delta=delta, l_min=l_min, nearest_neighbours=nearest_neighbours) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() n_rejects = result.n_rejects() hkl = miller_indices.as_vec3_double().iround() n_rejects = (crystal_ids < 0).count(True) assert miller_indices.select(crystal_ids < 0).all_eq((0,0,0)) for i_cryst in set(crystal_ids): if i_cryst < 0: continue A = experiments[i_cryst].crystal.get_A() A_inv = A.inverse() cryst_sel = crystal_ids == i_cryst ref_sel = refs.select(cryst_sel) rlp_sel = rlps.select(cryst_sel) hkl_sel = hkl.select(cryst_sel).as_vec3_double() d_sel = 1/rlp_sel.norms() d_perm = flex.sort_permutation(d_sel, reverse=True) hf_0 = A_inv * rlp_sel[d_perm[0]] h_0 = matrix.col([nint(j) for j in hf_0.elems]) offset = h_0 - matrix.col(hkl_sel[d_perm[0]]) #print "offset:", offset.elems h = hkl_sel + flex.vec3_double(hkl_sel.size(), offset.elems) refs['miller_index'].set_selected( cryst_sel, flex.miller_index(list(h.iround()))) refs['id'].set_selected(cryst_sel, i_cryst) crystal_ids.set_selected(crystal_ids < 0, -1) refs['id'] = crystal_ids refs['miller_index'].set_selected(crystal_ids < 0, (0,0,0)) reflections['miller_index'].set_selected(isel, refs['miller_index']) reflections['id'].set_selected(isel, refs['id']) reflections.set_flags( reflections['miller_index'] != (0,0,0), reflections.flags.indexed) if verbosity > 0: for i_cryst, cryst in enumerate(experiments.crystals()): info("model %i (%i reflections):" %( i_cryst+1, (reflections['id'] == i_cryst).count(True))) info(cryst) info("") info("%i unindexed reflections" %n_rejects)
def index_reflections_local( reflections, experiments, d_min=None, epsilon=0.05, delta=8, l_min=0.8, nearest_neighbours=20 ): from scitbx import matrix from libtbx.math_utils import nearest_integer as nint reciprocal_lattice_points = reflections["rlp"] if "miller_index" not in reflections: reflections["miller_index"] = flex.miller_index(len(reflections)) if d_min is not None: d_spacings = 1 / reciprocal_lattice_points.norms() inside_resolution_limit = d_spacings > d_min else: inside_resolution_limit = flex.bool(reciprocal_lattice_points.size(), True) sel = inside_resolution_limit & (reflections["id"] == -1) isel = sel.iselection() rlps = reciprocal_lattice_points.select(isel) refs = reflections.select(isel) phi = refs["xyzobs.mm.value"].parts()[2] if len(rlps) <= nearest_neighbours: from libtbx.utils import Sorry raise Sorry( "index_assignment.local.nearest_neighbour must be smaller than the number of accepted reflections (%d)" % len(rlps) ) diffs = [] norms = [] hkl_ints = [] UB_matrices = flex.mat3_double([cm.get_A() for cm in experiments.crystals()]) result = AssignIndicesLocal( rlps, phi, UB_matrices, epsilon=epsilon, delta=delta, l_min=l_min, nearest_neighbours=nearest_neighbours ) miller_indices = result.miller_indices() crystal_ids = result.crystal_ids() hkl = miller_indices.as_vec3_double().iround() assert miller_indices.select(crystal_ids < 0).all_eq((0, 0, 0)) for i_cryst in set(crystal_ids): if i_cryst < 0: continue A = experiments[i_cryst].crystal.get_A() A_inv = A.inverse() cryst_sel = crystal_ids == i_cryst ref_sel = refs.select(cryst_sel) rlp_sel = rlps.select(cryst_sel) hkl_sel = hkl.select(cryst_sel).as_vec3_double() d_sel = 1 / rlp_sel.norms() d_perm = flex.sort_permutation(d_sel, reverse=True) hf_0 = A_inv * rlp_sel[d_perm[0]] h_0 = matrix.col([nint(j) for j in hf_0.elems]) offset = h_0 - matrix.col(hkl_sel[d_perm[0]]) # print "offset:", offset.elems h = hkl_sel + flex.vec3_double(hkl_sel.size(), offset.elems) refs["miller_index"].set_selected(cryst_sel, flex.miller_index(list(h.iround()))) refs["id"].set_selected(cryst_sel, i_cryst) crystal_ids.set_selected(crystal_ids < 0, -1) refs["id"] = crystal_ids refs["miller_index"].set_selected(crystal_ids < 0, (0, 0, 0)) reflections["miller_index"].set_selected(isel, refs["miller_index"]) reflections["id"].set_selected(isel, refs["id"]) reflections.set_flags(reflections["miller_index"] != (0, 0, 0), reflections.flags.indexed)