def filter_ice(reflections, steps): from cctbx import miller, sgtbx, uctbx from matplotlib import pyplot as plt d_spacings = 1 / reflections["rlp"].norms() d_star_sq = uctbx.d_as_d_star_sq(d_spacings) from dials.algorithms.spot_finding.per_image_analysis import ice_rings_selection from dials.algorithms.integration import filtering ice_uc = uctbx.unit_cell((4.498, 4.498, 7.338, 90, 90, 120)) ice_sg = sgtbx.space_group_info(number=194).group() ice_generator = miller.index_generator(ice_uc, ice_sg.type(), False, flex.min(d_spacings)) ice_indices = ice_generator.to_array() ice_d_spacings = flex.sorted(ice_uc.d(ice_indices)) ice_d_star_sq = uctbx.d_as_d_star_sq(ice_d_spacings) cubic_ice_uc = uctbx.unit_cell((6.358, 6.358, 6.358, 90, 90, 90)) cubic_ice_sg = sgtbx.space_group_info(number=227).group() cubic_ice_generator = miller.index_generator(cubic_ice_uc, cubic_ice_sg.type(), False, flex.min(d_spacings)) cubic_ice_indices = cubic_ice_generator.to_array() cubic_ice_d_spacings = flex.sorted(cubic_ice_uc.d(cubic_ice_indices)) cubic_ice_d_star_sq = uctbx.d_as_d_star_sq(cubic_ice_d_spacings) import numpy widths = flex.double(numpy.geomspace(start=0.0001, stop=0.01, num=steps)) n_spots = flex.double() total_intensity = flex.double() for width in widths: d_min = flex.min(d_spacings) ice_filter = filtering.PowderRingFilter(ice_uc, ice_sg, d_min, width) ice_sel = ice_filter(d_spacings) n_spots.append(ice_sel.count(False)) if "intensity.sum.value" in reflections: total_intensity.append( flex.sum(reflections["intensity.sum.value"].select(~ice_sel))) fig, axes = plt.subplots(nrows=2, figsize=(12, 8), sharex=True) axes[0].plot(widths, n_spots, label="#spots", marker="+") if total_intensity.size(): axes[1].plot(widths, total_intensity, label="total intensity", marker="+") axes[0].set_ylabel("# spots remaining") axes[1].set_xlabel("Ice ring width (1/d^2)") axes[1].set_ylabel("Total intensity") for ax in axes: ax.set_xlim(0, flex.max(widths)) plt.savefig("ice_ring_filtering.png") plt.clf() return
def __init__(self, unit_cell, space_group, d_min, width): ''' Initialise the filter. :param unit_cell: The unit_cell of the powder rings :param space_group: The space group of the powder rings :param d_min: The maximum resolution to filter to :param width: The resolution width to filter around ''' from cctbx.miller import index_generator from dials.array_family import flex assert (d_min > 0) assert (width > 0) # Correct unit cell unit_cell = space_group.average_unit_cell(unit_cell) # Generate a load of indices generator = index_generator(unit_cell, space_group.type(), False, d_min) indices = generator.to_array() # Compute d spacings and sort by resolution self.d_star_sq = flex.sorted(unit_cell.d_star_sq(indices)) self.half_width = width / 2.0
def run(self): params, options = self.parser.parse_args(show_diff_phil=False) with open(params.input.powder_pattern) as f: data = [] for line in f.readlines(): x, y = [float(n) for n in line.split()] data.append((x, y)) uc = params.unit_cell sg = params.space_group d_spacings = [] mig = miller.index_generator(uc, sg.type(), 0, 0.8*params.d_min) for h in mig: d_spacings.append(uc.d(h)) error_cumul = 0 for x, y in data: if x < params.d_min: break best_match = min(d_spacings, key=lambda d: abs(x-d)) error = abs(x-best_match) error_cumul += error*y print("Cumul. error: ", error_cumul)
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 __init__(self, unit_cell, space_group, d_min, width): """ Initialise the filter. :param unit_cell: The unit_cell of the powder rings :param space_group: The space group of the powder rings :param d_min: The maximum resolution to filter to :param width: The resolution width to filter around """ assert d_min > 0 assert width > 0 # Correct unit cell unit_cell = space_group.average_unit_cell(unit_cell) self.half_width = width / 2.0 d_min = uctbx.d_star_sq_as_d( uctbx.d_as_d_star_sq(d_min) + self.half_width) # Generate a load of indices generator = index_generator(unit_cell, space_group.type(), False, d_min) indices = generator.to_array() # Compute d spacings and sort by resolution self.d_star_sq = flex.sorted(unit_cell.d_star_sq(indices))
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 range(m.size()): a.append(random.random()) p.append(random.random() * 2) c.append([random.random() for j in range(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 range(4): assert abs(c_asym[j] - c_random[i][j]) < 1.e-5
def __init__(self, unit_cell, space_group, d_min, width): ''' Initialise the filter. :param unit_cell: The unit_cell of the powder rings :param space_group: The space group of the powder rings :param d_min: The maximum resolution to filter to :param width: The resolution width to filter around ''' from cctbx.miller import index_generator from dials.array_family import flex assert(d_min > 0) assert(width > 0) # Correct unit cell unit_cell = space_group.average_unit_cell(unit_cell) # Generate a load of indices generator = index_generator(unit_cell, space_group.type(), False, d_min) indices = generator.to_array() # Compute d spacings and sort by resolution self.d_spacings = flex.sorted(unit_cell.d(indices)) self.half_width = width / 2.0
def make_sys_abs_list(self, space_group): max_index = (5, 5, 5) sg_type = sgtbx.space_group_info(1).type() tmp_miller_list = miller.index_generator(sg_type, False, max_index).to_array() abs_list = flex.miller_index() for miller_index in tmp_miller_list: if space_group.is_sys_absent(miller_index): abs_list.append(miller_index) return abs_list
def make_sys_abs_list(self, space_group): max_index=(5,5,5) sg_type = sgtbx.space_group_info(1).type() tmp_miller_list = miller.index_generator(sg_type, False, max_index).to_array() abs_list = flex.miller_index() for miller_index in tmp_miller_list: if space_group.is_sys_absent(miller_index): abs_list.append(miller_index) return abs_list
def exercise_index_generator(): uc = uctbx.unit_cell((11, 11, 13, 90, 90, 120)) sg_type = sgtbx.space_group_type("P 3 1 2") for anomalous_flag in (False, True): mig = miller.index_generator(uc, sg_type, anomalous_flag, 8) assert mig.unit_cell().is_similar_to(uc) assert mig.space_group_type().group() == sg_type.group() assert mig.anomalous_flag() == anomalous_flag assert mig.asu().reference_as_string( ) == "h>=k and k>=0 and (k>0 or l>=0)" assert mig.next() == (0, 0, 1) if (not anomalous_flag): assert tuple(mig.to_array()) == ((1, 0, 0), ) else: assert tuple(mig.to_array()) == ((1, 0, 0), (-1, 0, 0)) assert tuple(miller.index_generator(uc, sg_type, False, 8)) \ == ((0,0,1), (1, 0, 0)) index_abs_range = (4, 4, 4) for sg_symbol in ("P 31 1 2", "P 31 2 1"): sg_type = sgtbx.space_group_type(sg_symbol) for anomalous_flag in (False, True): miller_indices = miller.index_generator(sg_type, anomalous_flag, index_abs_range) miller_dict = {} for h in miller_indices: miller_dict[h] = 0 sg = sg_type.group() h = [0, 0, 0] for h[0] in range(-index_abs_range[0], index_abs_range[0] + 1): for h[1] in range(-index_abs_range[1], index_abs_range[1] + 1): for h[2] in range(-index_abs_range[2], index_abs_range[2] + 1): if (sg.is_sys_absent(h) or h == [0, 0, 0]): continue h_eq = miller.sym_equiv_indices(sg, h) found_h_asu = 0 for i_eq in xrange(h_eq.multiplicity(anomalous_flag)): h_i = h_eq(i_eq).h() if (h_i in miller_dict): assert found_h_asu == 0 found_h_asu = 1 assert found_h_asu != 0
def generate_intensities(crystal_symmetry, anomalous_flag=False, d_min=1): from cctbx import miller indices = miller.index_generator(crystal_symmetry.unit_cell(), crystal_symmetry.space_group().type(), anomalous_flag, d_min).to_array() miller_set = crystal_symmetry.miller_set(indices, anomalous_flag) intensities = flex.random_double(indices.size()) miller_array = miller.array( miller_set, data=intensities, sigmas=flex.double(intensities.size(), 1)).set_observation_type_xray_intensity() return miller_array
def exercise_index_generator(): uc = uctbx.unit_cell((11,11,13,90,90,120)) sg_type = sgtbx.space_group_type("P 3 1 2") for anomalous_flag in (False,True): mig = miller.index_generator(uc, sg_type, anomalous_flag, 8) assert mig.unit_cell().is_similar_to(uc) assert mig.space_group_type().group() == sg_type.group() assert mig.anomalous_flag() == anomalous_flag assert mig.asu().reference_as_string() == "h>=k and k>=0 and (k>0 or l>=0)" assert mig.next() == (0,0,1) if (not anomalous_flag): assert tuple(mig.to_array()) == ((1, 0, 0),) else: assert tuple(mig.to_array()) == ((1, 0, 0), (-1, 0, 0)) assert tuple(miller.index_generator(uc, sg_type, False, 8)) \ == ((0,0,1), (1, 0, 0)) index_abs_range = (4,4,4) for sg_symbol in ("P 31 1 2", "P 31 2 1"): sg_type = sgtbx.space_group_type(sg_symbol) for anomalous_flag in (False,True): miller_indices = miller.index_generator( sg_type, anomalous_flag, index_abs_range) miller_dict = {} for h in miller_indices: miller_dict[h] = 0 sg = sg_type.group() h = [0,0,0] for h[0] in range(-index_abs_range[0], index_abs_range[0]+1): for h[1] in range(-index_abs_range[1], index_abs_range[1]+1): for h[2] in range(-index_abs_range[2], index_abs_range[2]+1): if (sg.is_sys_absent(h) or h == [0,0,0]): continue h_eq = miller.sym_equiv_indices(sg, h) found_h_asu = 0 for i_eq in xrange(h_eq.multiplicity(anomalous_flag)): h_i = h_eq(i_eq).h() if (h_i in miller_dict): assert found_h_asu == 0 found_h_asu = 1 assert found_h_asu != 0
def count_ice_rings(self, width=0.002, verbose=False): """ A function to find and count ice rings (modeled after dials.algorithms.integration.filtering.PowderRingFilter, with some alterations: 1. Hard-coded with ice unit cell / space group 2. Returns spot counts vs. water diffraction resolution "bin" Rather than finding ice rings themselves (which may be laborious and time consuming), this method relies on counting the number of found spots that land in regions of water diffraction. A weakness of this approach is that if any spot filtering or spot-finding parameters are applied by prior methods, not all ice rings may be found. This is acceptable, since the purpose of this method is to determine if water and protein diffraction occupy the same resolutions. """ ice_start = time.time() unit_cell = uctbx.unit_cell((4.498, 4.498, 7.338, 90, 90, 120)) space_group = sgtbx.space_group_info(number=194).group() # Correct unit cell unit_cell = space_group.average_unit_cell(unit_cell) half_width = width / 2 d_min = uctbx.d_star_sq_as_d(uctbx.d_as_d_star_sq(self.d_min) + half_width) # Generate a load of indices generator = index_generator(unit_cell, space_group.type(), False, d_min) indices = generator.to_array() # Compute d spacings and sort by resolution d_star_sq = flex.sorted(unit_cell.d_star_sq(indices)) d = uctbx.d_star_sq_as_d(d_star_sq) dd = list(zip(d_star_sq, d)) # identify if spots fall within ice ring areas results = [] for ds2, d_res in dd: result = [i for i in (flex.abs(self.ref_d_star_sq - ds2) < half_width) if i] results.append((d_res, len(result))) possible_ice = [r for r in results if r[1] / len(self.observed) * 100 >= 5] if verbose: print( "SCORER: ice ring time = {:.5f} seconds".format(time.time() - ice_start) ) self.n_ice_rings = len(possible_ice) # output in info return self.n_ice_rings
def GenHBravais(dmin, Bravais, A, sg_type=None): """Generate the positionally unique powder diffraction reflections :param dmin: minimum d-spacing in A :param Bravais: lattice type (see GetBraviasNum). Bravais is one of: * 0 F cubic * 1 I cubic * 2 P cubic * 3 R hexagonal (trigonal not rhombohedral) * 4 P hexagonal * 5 I tetragonal * 6 P tetragonal * 7 F orthorhombic * 8 I orthorhombic * 9 A orthorhombic * 10 B orthorhombic * 11 C orthorhombic * 12 P orthorhombic * 13 I monoclinic * 14 C monoclinic * 15 P monoclinic * 16 P triclinic :param A: reciprocal metric tensor elements as [G11,G22,G33,2*G12,2*G13,2*G23] :param st_type: an sgtbx.space_group_type object. Constructing these is slow so it's good to precalculate if possible. :return: HKL unique d list of [h,k,l,d,-1] sorted with largest d first """ g_inv = np.array([[A[0], A[3] / 2, A[4] / 2], [A[3] / 2, A[1], A[5] / 2], [A[4] / 2, A[5] / 2, A[2]]]) g = np.linalg.inv(g_inv) g_elems = (g[0][0], g[1][1], g[2][2], g[0][1], g[0][2], g[1][2]) try: uc = uctbx.unit_cell(metrical_matrix=g_elems) except ValueError: # this function sometimes receives an A matrix that gives # numbers <0 in the diagonal elems of g. Not sure why. return [] if sg_type is None: sg_type = make_sgtype(Bravais) mig = miller.index_generator(uc, sg_type, 0, dmin) result = [] for h, k, l in mig: d = uc.d((h, k, l)) result.append([h, k, l, d, -1]) result.sort(key=lambda l: l[3], reverse=True) return result
def powder_score(self, powder_pattern, d_min): '''Take a list of (d, counts) tuples and return a figure of merit (lower-better) ''' assert self.sg is not None from cctbx import miller mig = miller.index_generator(self.uc, self.sg.type(), 0, 0.8 * d_min) d_spacings = [] for h in mig: d_spacings.append(self.uc.d(h)) error_cumul = 0 for x, y in data: best_match = min(d_spacings, key=lambda d: abs(x - d)) error = abs(x - best_match) error_cumul += error * y return error_cumul
def generate_indices(cell, spgr, dmin=1.0, ret='index'): """http://cci.lbl.gov/cctbx_sources/cctbx/miller/index_generator.h""" dmin = dmin-0.0000000000001 # because resolution_limit is < and not <= anomalous_flag = False symm = make_symmetry(cell, spgr) unit_cell = uctbx.unit_cell(cell) sg_type = space_group_type(spgr) # input hkl or resolution(d_min) mig = index_generator( unit_cell, sg_type, anomalous_flag=anomalous_flag, resolution_limit=dmin) indices = mig.to_array() if ret == 'index': # suitable for df index return indices else: return miller.array(miller.set(crystal_symmetry=symm, indices=indices, anomalous_flag=anomalous_flag))
def calc_powder_score(self, powder_pattern, d_min): '''Take a list of (d, counts) tuples and return a figure of merit (lower-better) ''' if powder_pattern is None: return 100 assert self.sg is not None mig = miller.index_generator(self.uc, self.sg.type(), 0, 0.8 * d_min) d_spacings = [] for h in mig: d_spacings.append(self.uc.d(h)) error_cumul = 0 for x, y in powder_pattern: if x < d_min: break best_match = min(d_spacings, key=lambda d: abs(x - d)) error = abs(x - best_match) / x error_cumul += error * y return error_cumul
def generate_indices(cell, spgr, dmin=1.0, ret='index'): """http://cci.lbl.gov/cctbx_sources/cctbx/miller/index_generator.h""" dmin = dmin - 0.0000000000001 # because resolution_limit is < and not <= anomalous_flag = False symm = make_symmetry(cell, spgr) unit_cell = uctbx.unit_cell(cell) sg_type = space_group_type(spgr) # input hkl or resolution(d_min) mig = index_generator(unit_cell, sg_type, anomalous_flag=anomalous_flag, resolution_limit=dmin) indices = mig.to_array() if ret == 'index': # suitable for df index return indices else: return miller.array( miller.set(crystal_symmetry=symm, indices=indices, anomalous_flag=anomalous_flag))
def exercise(space_group_info, redundancy_counter=0): n_real = (12,12,12) miller_max = (2,2,2) gt = maptbx.grid_tags(n_real) uc = space_group_info.any_compatible_unit_cell(volume=1000) fl = sgtbx.search_symmetry_flags(use_space_group_symmetry=True) gt.build(space_group_info.type(), fl) fft = fftpack.real_to_complex_3d(n_real) map0 = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()), 0) weight_map = map0.deep_copy() map = map0.deep_copy() ta = gt.tag_array() order_z = space_group_info.group().order_z() problems_expected = (redundancy_counter != 0) for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): xyz = [i/n for i,n in zip(ijk, n_real)] ss = sgtbx.site_symmetry( unit_cell=uc, space_group=space_group_info.group(), original_site=xyz, min_distance_sym_equiv=1e-5) m = space_group_info.group().multiplicity( site=boost.rational.vector(ijk, n_real)) assert m == ss.multiplicity() w = m / order_z weight_map[ijk] = w map[ijk] = w elif (redundancy_counter != 0): redundancy_counter -= 1 ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t map[ijk] = map[ijk_asu] sf_map = fft.forward(map) del map mi = miller.index_generator( space_group_info.type(), False, miller_max).to_array() assert mi.size() != 0 from_map = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map, conjugate_flag=True) sf = [iround(abs(f)) for f in from_map.data()] if (sf != [0]*len(sf)): assert problems_expected return else: not problems_expected # map_p1 = map0.deep_copy() map_sw = map0.deep_copy() for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): v = random.random()*2-1 map_p1[ijk] = v map_sw[ijk] = v * weight_map[ijk] else: ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t assert map_p1[ijk_asu] != 0 map_p1[ijk] = map_p1[ijk_asu] # # fft followed by symmetry summation in reciprocal space sf_map_sw = fft.forward(map_sw) del map_sw sf_sw = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_sw, conjugate_flag=True).data() del sf_map_sw # # symmetry expansion in real space (done above already) followed fft sf_map_p1 = fft.forward(map_p1) del map_p1 sf_p1 = maptbx.structure_factors.from_map( space_group=sgtbx.space_group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_p1, conjugate_flag=True).data() del sf_map_p1 # corr = flex.linear_correlation(x=flex.abs(sf_sw), y=flex.abs(sf_p1)) assert corr.is_well_defined assert approx_equal(corr.coefficient(), 1)
def run(args): assert args in [[], ["python"], ["c++"]] # sgno_list_by_index_list_by_cs = {} from cctbx import sgtbx from cctbx import miller for symbols in sgtbx.space_group_symbol_iterator(): psgi = sgtbx.space_group_info(symbols.universal_hermann_mauguin()) \ .primitive_setting() p_indices = miller.index_generator( space_group_type=psgi.type(), anomalous_flag=False, max_index=[4]*3).to_array() # 4 is the smallest value leading to correct results; any larger # value will work, too, but will make this procedure slower p1_indices = miller.expand_to_p1_iselection( space_group=psgi.group(), anomalous_flag=False, indices=p_indices, build_iselection=False).indices from cctbx.array_family import flex sort_perm = flex.sort_permutation( data=miller.index_span(p1_indices).pack(p1_indices)) p1_indices = p1_indices.select(sort_perm) index_list = tuple(p1_indices) sgno = psgi.type().number() sgno_list_by_index_list = sgno_list_by_index_list_by_cs \ .setdefault(symbols.crystal_system(), {}) sgno_list_by_index_list.setdefault(index_list, []).append(sgno) from scitbx.graph import tardy_tree cluster_manager = tardy_tree.cluster_manager(n_vertices=231) for cs,sgno_list_by_index_list in sgno_list_by_index_list_by_cs.items(): for sgno_list in sgno_list_by_index_list.values(): i = sgno_list[0] for j in sgno_list[1:]: cluster_manager.connect_vertices(i=i, j=j, optimize=True) cluster_manager.tidy() # # everything below is just to format the results # if (args == []): for cluster in cluster_manager.clusters: if (len(cluster) == 1): break print cluster else: note = ("""\ Output of: cctbx/examples/find_sys_abs_equiv_space_groups.py %s If you have to edit this table, please send email to: [email protected] """ % args[0]).splitlines() # if (args == ["python"]): print "space_group_numbers = [" for line in note: print " #", line ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for sgno in xrange(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): s = "None" else: s = str(tuple(cluster)) if (sgno == 230): comma = "" else: comma = "," print " %s%s" % (s, comma) print "]" else: print """\ #ifndef CCTBX_SGTBX_SYS_ABS_EQUIV_H #define CCTBX_SGTBX_SYS_ABS_EQUIV_H namespace cctbx { namespace sgtbx { namespace sys_abs_equiv { """ data = [] ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for line in note: print " //", line for sgno in xrange(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): data.append("0") else: cid = "data_%03d" % sgno data.append(cid) print " static const unsigned %s[] = {%d, %s};" % ( cid, len(cluster), ", ".join([str(i) for i in cluster])) print "" print " static const unsigned* space_group_numbers[] = {" print " ", ",\n ".join(data) print """\
def miller_indices(self, space_group_info): space_group = space_group_info.group() return flex.miller_index( miller.index_generator(space_group.type(), anomalous_flag=True, max_index=(10, 10, 10)) )
def exercise_bins(): uc = uctbx.unit_cell((11,11,13,90,90,120)) sg_type = sgtbx.space_group_type("P 3 2 1") anomalous_flag = False d_min = 1 m = miller.index_generator(uc, sg_type, anomalous_flag, d_min).to_array() f = flex.double() for i in range(m.size()): f.append(random.random()) n_bins = 10 b = miller.binning(uc, n_bins, 0, d_min) b = miller.binning(uc, n_bins, 0, d_min, 1.e-6) b = miller.binning(uc, n_bins, m) b = miller.binning(uc, n_bins, m, 0) b = miller.binning(uc, n_bins, m, 0, d_min) b = miller.binning(uc, n_bins, m, 0, d_min, 1.e-6) assert b.d_max() == -1 assert approx_equal(b.d_min(), d_min) assert b.bin_d_range(0) == (-1,-1) assert approx_equal(b.bin_d_range(1), (-1,2.1544336)) assert approx_equal(b.bin_d_range(b.n_bins_all()-1), (1,-1)) d_star_sq = 0.5 r = b.bin_d_range(b.get_i_bin(d_star_sq)) d = 1/math.sqrt(d_star_sq) assert r[1] <= d <= r[0] h = (3,4,5) r = b.bin_d_range(b.get_i_bin(h)) assert r[1] <= uc.d(h) <= r[0] # a quick test to excercise d-spacings on fractional Miller indices: assert approx_equal( uc.d((3,4,5)), uc.d_frac((3.001,4,5)), eps=0.001) binning1 = miller.binning(uc, n_bins, m) assert binning1.unit_cell().is_similar_to(uc) assert binning1.n_bins_used() == n_bins assert binning1.limits().size() == n_bins + 1 assert binning1.n_bins_all() == n_bins + 2 s = pickle.dumps(binning1) l = pickle.loads(s) assert str(l.unit_cell()) == "(11, 11, 13, 90, 90, 120)" assert approx_equal(l.limits(), binning1.limits()) # binner1 = miller.ext.binner(binning1, m) assert binner1.miller_indices().id() == m.id() assert binner1.count(binner1.i_bin_d_too_large()) == 0 assert binner1.count(binner1.i_bin_d_too_small()) == 0 counts = binner1.counts() for i_bin in binner1.range_all(): assert binner1.count(i_bin) == counts[i_bin] assert binner1.selection(i_bin).count(True) == counts[i_bin] assert list(binner1.range_all()) == list(range(binner1.n_bins_all())) assert list(binner1.range_used()) == list(range(1, binner1.n_bins_used()+1)) binning2 = miller.binning(uc, n_bins - 2, binning1.bin_d_min(2), binning1.bin_d_min(n_bins)) binner2 = miller.ext.binner(binning2, m) assert tuple(binner1.counts())[1:-1] == tuple(binner2.counts()) array_indices = flex.size_t(range(m.size())) perm_array_indices1 = flex.size_t() perm_array_indices2 = flex.size_t() for i_bin in binner1.range_all(): perm_array_indices1.extend(array_indices.select(binner1.selection(i_bin))) perm_array_indices2.extend(binner1.array_indices(i_bin)) assert perm_array_indices1.size() == m.size() assert perm_array_indices2.size() == m.size() assert tuple(perm_array_indices1) == tuple(perm_array_indices2) b = miller.ext.binner(miller.binning(uc, n_bins, m, 0, d_min), m) assert approx_equal(b.bin_centers(1), (0.23207956, 0.52448148, 0.62711856, 0.70311998, 0.7652538, 0.818567, 0.86566877, 0.90811134, 0.94690405, 0.98274518)) assert approx_equal(b.bin_centers(2), (0.10772184, 0.27871961, 0.39506823, 0.49551249, 0.58642261, 0.67067026, 0.74987684, 0.82507452, 0.89697271, 0.96608584)) assert approx_equal(b.bin_centers(3), (0.050000075, 0.15000023, 0.25000038, 0.35000053, 0.45000068, 0.55000083, 0.65000098, 0.75000113, 0.85000128, 0.95000143)) v = flex.double(range(b.n_bins_used())) i = b.interpolate(v, 0) for i_bin in b.range_used(): assert i.select(b.selection(i_bin)).all_eq(v[i_bin-1]) dss = uc.d_star_sq(m) for d_star_power in (1,2,3): j = b.interpolate(v, d_star_power) x = flex.pow(dss, (d_star_power/2.)) r = flex.linear_correlation(x, j) assert r.is_well_defined() assert approx_equal( r.coefficient(), (0.946401,0.990764,1.0)[d_star_power-1], eps=1.e-4, multiplier=None) # s = pickle.dumps(binner2) l = pickle.loads(s) assert str(l.unit_cell()) == "(11, 11, 13, 90, 90, 120)" assert approx_equal(l.limits(), binner2.limits()) assert l.miller_indices().all_eq(binner2.miller_indices()) assert l.bin_indices().all_eq(binner2.bin_indices()) # limits = flex.random_double(size=10) bng = miller.binning(uc, limits) assert bng.unit_cell().is_similar_to(uc) assert approx_equal(bng.limits(), limits)
def miller_indices(self, space_group_info): space_group = space_group_info.group() return flex.miller_index( miller.index_generator(space_group.type(), anomalous_flag=True, max_index=(10, 10, 10)))
def exercise(space_group_info, redundancy_counter=0): n_real = (12, 12, 12) miller_max = (2, 2, 2) gt = maptbx.grid_tags(n_real) uc = space_group_info.any_compatible_unit_cell(volume=1000) fl = sgtbx.search_symmetry_flags(use_space_group_symmetry=True) gt.build(space_group_info.type(), fl) fft = fftpack.real_to_complex_3d(n_real) map0 = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()), 0) weight_map = map0.deep_copy() map = map0.deep_copy() ta = gt.tag_array() order_z = space_group_info.group().order_z() problems_expected = (redundancy_counter != 0) for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): xyz = [i / n for i, n in zip(ijk, n_real)] ss = sgtbx.site_symmetry(unit_cell=uc, space_group=space_group_info.group(), original_site=xyz, min_distance_sym_equiv=1e-5) m = space_group_info.group().multiplicity( site=boost.rational.vector(ijk, n_real)) assert m == ss.multiplicity() w = m / order_z weight_map[ijk] = w map[ijk] = w elif (redundancy_counter != 0): redundancy_counter -= 1 ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t map[ijk] = map[ijk_asu] sf_map = fft.forward(map) del map mi = miller.index_generator(space_group_info.type(), False, miller_max).to_array() assert mi.size() != 0 from_map = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map, conjugate_flag=True) sf = [iround(abs(f)) for f in from_map.data()] if (sf != [0] * len(sf)): assert problems_expected return else: not problems_expected # map_p1 = map0.deep_copy() map_sw = map0.deep_copy() for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): v = random.random() * 2 - 1 map_p1[ijk] = v map_sw[ijk] = v * weight_map[ijk] else: ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t assert map_p1[ijk_asu] != 0 map_p1[ijk] = map_p1[ijk_asu] # # fft followed by symmetry summation in reciprocal space sf_map_sw = fft.forward(map_sw) del map_sw sf_sw = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_sw, conjugate_flag=True).data() del sf_map_sw # # symmetry expansion in real space (done above already) followed fft sf_map_p1 = fft.forward(map_p1) del map_p1 sf_p1 = maptbx.structure_factors.from_map(space_group=sgtbx.space_group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_p1, conjugate_flag=True).data() del sf_map_p1 # corr = flex.linear_correlation(x=flex.abs(sf_sw), y=flex.abs(sf_p1)) assert corr.is_well_defined assert approx_equal(corr.coefficient(), 1)
def run(args): assert args in [[], ["python"], ["c++"]] # sgno_list_by_index_list_by_cs = {} from cctbx import sgtbx from cctbx import miller for symbols in sgtbx.space_group_symbol_iterator(): psgi = sgtbx.space_group_info(symbols.universal_hermann_mauguin()) \ .primitive_setting() p_indices = miller.index_generator(space_group_type=psgi.type(), anomalous_flag=False, max_index=[4] * 3).to_array() # 4 is the smallest value leading to correct results; any larger # value will work, too, but will make this procedure slower p1_indices = miller.expand_to_p1_iselection( space_group=psgi.group(), anomalous_flag=False, indices=p_indices, build_iselection=False).indices from cctbx.array_family import flex sort_perm = flex.sort_permutation( data=miller.index_span(p1_indices).pack(p1_indices)) p1_indices = p1_indices.select(sort_perm) index_list = tuple(p1_indices) sgno = psgi.type().number() sgno_list_by_index_list = sgno_list_by_index_list_by_cs \ .setdefault(symbols.crystal_system(), {}) sgno_list_by_index_list.setdefault(index_list, []).append(sgno) from scitbx.graph import tardy_tree cluster_manager = tardy_tree.cluster_manager(n_vertices=231) for cs, sgno_list_by_index_list in sgno_list_by_index_list_by_cs.items(): for sgno_list in sgno_list_by_index_list.values(): i = sgno_list[0] for j in sgno_list[1:]: cluster_manager.connect_vertices(i=i, j=j, optimize=True) cluster_manager.tidy() # # everything below is just to format the results # if (args == []): for cluster in cluster_manager.clusters: if (len(cluster) == 1): break print(cluster) else: note = ("""\ Output of: cctbx/examples/find_sys_abs_equiv_space_groups.py %s If you have to edit this table, please send email to: [email protected] """ % args[0]).splitlines() # if (args == ["python"]): print("space_group_numbers = [") for line in note: print(" #", line) ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for sgno in range(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): s = "None" else: s = str(tuple(cluster)) if (sgno == 230): comma = "" else: comma = "," print(" %s%s" % (s, comma)) print("]") else: print("""\ #ifndef CCTBX_SGTBX_SYS_ABS_EQUIV_H #define CCTBX_SGTBX_SYS_ABS_EQUIV_H namespace cctbx { namespace sgtbx { namespace sys_abs_equiv { """) data = [] ci = cluster_manager.cluster_indices cl = cluster_manager.clusters for line in note: print(" //", line) for sgno in range(231): cluster = list(cl[ci[sgno]]) cluster.remove(sgno) if (len(cluster) == 0): data.append("0") else: cid = "data_%03d" % sgno data.append(cid) print(" static const unsigned %s[] = {%d, %s};" % (cid, len(cluster), ", ".join( [str(i) for i in cluster]))) print("") print(" static const unsigned* space_group_numbers[] = {") print(" ", ",\n ".join(data)) print("""\ }; }}} #endif // GUARD""")
def exercise_bins(): uc = uctbx.unit_cell((11,11,13,90,90,120)) sg_type = sgtbx.space_group_type("P 3 2 1") anomalous_flag = False d_min = 1 m = miller.index_generator(uc, sg_type, anomalous_flag, d_min).to_array() f = flex.double() for i in xrange(m.size()): f.append(random.random()) n_bins = 10 b = miller.binning(uc, n_bins, 0, d_min) b = miller.binning(uc, n_bins, 0, d_min, 1.e-6) b = miller.binning(uc, n_bins, m) b = miller.binning(uc, n_bins, m, 0) b = miller.binning(uc, n_bins, m, 0, d_min) b = miller.binning(uc, n_bins, m, 0, d_min, 1.e-6) assert b.d_max() == -1 assert approx_equal(b.d_min(), d_min) assert b.bin_d_range(0) == (-1,-1) assert approx_equal(b.bin_d_range(1), (-1,2.1544336)) assert approx_equal(b.bin_d_range(b.n_bins_all()-1), (1,-1)) d_star_sq = 0.5 r = b.bin_d_range(b.get_i_bin(d_star_sq)) d = 1/math.sqrt(d_star_sq) assert r[1] <= d <= r[0] h = (3,4,5) r = b.bin_d_range(b.get_i_bin(h)) assert r[1] <= uc.d(h) <= r[0] # a quick test to excercise d-spacings on fractional Miller indices: assert approx_equal( uc.d((3,4,5)), uc.d_frac((3.001,4,5)), eps=0.001) binning1 = miller.binning(uc, n_bins, m) assert binning1.unit_cell().is_similar_to(uc) assert binning1.n_bins_used() == n_bins assert binning1.limits().size() == n_bins + 1 assert binning1.n_bins_all() == n_bins + 2 s = pickle.dumps(binning1) l = pickle.loads(s) assert str(l.unit_cell()) == "(11, 11, 13, 90, 90, 120)" assert approx_equal(l.limits(), binning1.limits()) # binner1 = miller.ext.binner(binning1, m) assert binner1.miller_indices().id() == m.id() assert binner1.count(binner1.i_bin_d_too_large()) == 0 assert binner1.count(binner1.i_bin_d_too_small()) == 0 counts = binner1.counts() for i_bin in binner1.range_all(): assert binner1.count(i_bin) == counts[i_bin] assert binner1.selection(i_bin).count(True) == counts[i_bin] assert list(binner1.range_all()) == range(binner1.n_bins_all()) assert list(binner1.range_used()) == range(1, binner1.n_bins_used()+1) binning2 = miller.binning(uc, n_bins - 2, binning1.bin_d_min(2), binning1.bin_d_min(n_bins)) binner2 = miller.ext.binner(binning2, m) assert tuple(binner1.counts())[1:-1] == tuple(binner2.counts()) array_indices = flex.size_t(range(m.size())) perm_array_indices1 = flex.size_t() perm_array_indices2 = flex.size_t() for i_bin in binner1.range_all(): perm_array_indices1.extend(array_indices.select(binner1.selection(i_bin))) perm_array_indices2.extend(binner1.array_indices(i_bin)) assert perm_array_indices1.size() == m.size() assert perm_array_indices2.size() == m.size() assert tuple(perm_array_indices1) == tuple(perm_array_indices2) b = miller.ext.binner(miller.binning(uc, n_bins, m, 0, d_min), m) assert approx_equal(b.bin_centers(1), (0.23207956, 0.52448148, 0.62711856, 0.70311998, 0.7652538, 0.818567, 0.86566877, 0.90811134, 0.94690405, 0.98274518)) assert approx_equal(b.bin_centers(2), (0.10772184, 0.27871961, 0.39506823, 0.49551249, 0.58642261, 0.67067026, 0.74987684, 0.82507452, 0.89697271, 0.96608584)) assert approx_equal(b.bin_centers(3), (0.050000075, 0.15000023, 0.25000038, 0.35000053, 0.45000068, 0.55000083, 0.65000098, 0.75000113, 0.85000128, 0.95000143)) v = flex.double(xrange(b.n_bins_used())) i = b.interpolate(v, 0) for i_bin in b.range_used(): assert i.select(b.selection(i_bin)).all_eq(v[i_bin-1]) dss = uc.d_star_sq(m) for d_star_power in (1,2,3): j = b.interpolate(v, d_star_power) x = flex.pow(dss, (d_star_power/2.)) r = flex.linear_correlation(x, j) assert r.is_well_defined() assert approx_equal( r.coefficient(), (0.946401,0.990764,1.0)[d_star_power-1], eps=1.e-4, multiplier=None) # s = pickle.dumps(binner2) l = pickle.loads(s) assert str(l.unit_cell()) == "(11, 11, 13, 90, 90, 120)" assert approx_equal(l.limits(), binner2.limits()) assert l.miller_indices().all_eq(binner2.miller_indices()) assert l.bin_indices().all_eq(binner2.bin_indices()) # limits = flex.random_double(size=10) bng = miller.binning(uc, limits) assert bng.unit_cell().is_similar_to(uc) assert approx_equal(bng.limits(), limits)