def assign_random_r_free_flags (n_refl, fraction_free, format="cns") : assert (fraction_free > 0) and (fraction_free < 0.5) from scitbx.array_family import flex from libtbx.math_utils import iround group_size = 1/(fraction_free) assert group_size >= 2 if (format == "cns") or (format == "shelx") : result = flex.bool(n_refl, False) i_start = 0 for i_group in count(1): i_end = min(n_refl, iround(i_group*group_size) ) if (i_start == i_end): break if (i_end + 1 == n_refl): i_end += 1 assert i_end - i_start >= 2 result[random.randrange(i_start, i_end)] = True i_start = i_end if (format == "shelx") : result_ = flex.int(n_refl, 1) result_.set_selected(result, -1) result = result_ elif (format == "ccp4") : result = flex.int() flag_max = iround(group_size) - 1 for i in range(n_refl) : result.append(random.randint(0, flag_max)) return result
def assign_random_r_free_flags(n_refl, fraction_free, format="cns"): assert (fraction_free > 0) and (fraction_free < 0.5) from scitbx.array_family import flex from libtbx.math_utils import iround group_size = 1 / (fraction_free) assert group_size >= 2 if (format == "cns") or (format == "shelx"): result = flex.bool(n_refl, False) i_start = 0 for i_group in count(1): i_end = min(n_refl, iround(i_group * group_size)) if (i_start == i_end): break if (i_end + 1 == n_refl): i_end += 1 assert i_end - i_start >= 2 result[random.randrange(i_start, i_end)] = True i_start = i_end if (format == "shelx"): result_ = flex.int(n_refl, 1) result_.set_selected(result, -1) result = result_ elif (format == "ccp4"): result = flex.int() flag_max = iround(group_size) - 1 for i in range(n_refl): result.append(random.randint(0, flag_max)) return result
def format_f8_1_or_i8(h, label, value): if (value < 1.e6): return "%8.1f" % value result = "%8d" % iround(value) if (len(result) > 8): raise ValueError( "Value is too large for scalepack merge format: hkl=%s, %s=%.6g" % (str(h).replace(" ", ""), label, value)) return result
def assign_r_free_flags_by_shells (n_refl, fraction_free, n_bins) : assert (fraction_free > 0) and (fraction_free < 0.5) assert (n_bins > 1) and (n_bins < n_refl) # XXX this should be smarter from scitbx.array_family import flex from libtbx.math_utils import iround n_free = iround(n_refl * fraction_free) n_per_bin = iround(n_refl / n_bins) half_n_work_per_bin = iround((n_refl-n_free) / n_bins / 2) n_free_per_bin = n_per_bin - 2*half_n_work_per_bin flags = flex.bool() flags.reserve(n_refl) for i_bin in xrange(n_bins): flags.resize(min(n_refl, flags.size()+half_n_work_per_bin), False) flags.resize(min(n_refl, flags.size()+n_free_per_bin), True) flags.resize(min(n_refl, flags.size()+half_n_work_per_bin), False) flags.resize(n_refl, False) return flags
def assign_r_free_flags_by_shells(n_refl, fraction_free, n_bins): assert (fraction_free > 0) and (fraction_free < 0.5) assert (n_bins > 1) and (n_bins < n_refl) # XXX this should be smarter from scitbx.array_family import flex from libtbx.math_utils import iround n_free = iround(n_refl * fraction_free) n_per_bin = iround(n_refl / n_bins) half_n_work_per_bin = iround((n_refl - n_free) / n_bins / 2) n_free_per_bin = n_per_bin - 2 * half_n_work_per_bin flags = flex.bool() flags.reserve(n_refl) for i_bin in range(n_bins): flags.resize(min(n_refl, flags.size() + half_n_work_per_bin), False) flags.resize(min(n_refl, flags.size() + n_free_per_bin), True) flags.resize(min(n_refl, flags.size() + half_n_work_per_bin), False) flags.resize(n_refl, False) return flags
def format_f8_1_or_i8(h, label, value): if (value < 1.e6): return "%8.1f" % value result = "%8d" % iround(value) if (len(result) > 8): raise ValueError( "Value is too large for scalepack merge format: hkl=%s, %s=%.6g" % ( str(h).replace(" ",""), label, value)) return result
def exercise_integer(): from libtbx.math_utils import iround, iceil, ifloor, nearest_integer assert iround(0) == 0 assert iround(1.4) == 1 assert iround(-1.4) == -1 assert iround(1.6) == 2 assert iround(-1.6) == -2 assert iceil(0) == 0 assert iceil(1.1) == 2 assert iceil(-1.1) == -1 assert iceil(1.9) == 2 assert iceil(-1.9) == -1 assert ifloor(0) == 0 assert ifloor(1.1) == 1 assert ifloor(-1.1) == -2 assert ifloor(1.9) == 1 assert ifloor(-1.9) == -2 for i in xrange(-3, 3 + 1): assert nearest_integer(i + 0.3) == i assert nearest_integer(i + 0.7) == i + 1
def exercise_integer(): from libtbx.math_utils import iround, iceil, ifloor, nearest_integer assert iround(0) == 0 assert iround(1.4) == 1 assert iround(-1.4) == -1 assert iround(1.6) == 2 assert iround(-1.6) == -2 assert iceil(0) == 0 assert iceil(1.1) == 2 assert iceil(-1.1) == -1 assert iceil(1.9) == 2 assert iceil(-1.9) == -1 assert ifloor(0) == 0 assert ifloor(1.1) == 1 assert ifloor(-1.1) == -2 assert ifloor(1.9) == 1 assert ifloor(-1.9) == -2 for i in xrange(-3,3+1): assert nearest_integer(i+0.3) == i assert nearest_integer(i+0.7) == i+1
def sigmaa_estimator_kernel_width_d_star_cubed(r_free_flags, kernel_width_free_reflections): assert kernel_width_free_reflections > 0 n_refl = r_free_flags.size() n_free = r_free_flags.data().count(True) n_refl_per_bin = kernel_width_free_reflections if n_free != 0: n_refl_per_bin *= n_refl / n_free n_refl_per_bin = min(n_refl, iround(n_refl_per_bin)) n_bins = max(1, n_refl / max(1, n_refl_per_bin)) dsc_min, dsc_max = [dss ** (3 / 2) for dss in r_free_flags.min_max_d_star_sq()] return (dsc_max - dsc_min) / n_bins
def run(args): if (len(args) == 0): args = ["."] from libtbx.math_utils import iround import libtbx.str_utils import libtbx.path import os op = os.path file_names = [] for arg in args: if (op.isfile(arg)): file_names.append(top) elif (op.isdir(arg)): file_names.extend(libtbx.path.walk_source_tree(top=arg)) contents = [] sz_ln_fn_ext = [] for fn in file_names: i = fn.rfind(".") if (i < 0): continue ext = fn[i + 1:] if (ext in [ "c", "h", "cpp", "hpp", "py", "java", "f", "sh", "csh", "bat" ]): content = open(fn, "rb").read() contents.append(content) sz_ln_fn_ext.append( (len(content), len(content.splitlines()), fn, ext)) sz_ln_fn_ext.sort() sz_sum = 0 ln_sum = 0 ext_counts = libtbx.dict_with_default_0() for sz, ln, fn, ext in sz_ln_fn_ext: print("%10d %6d %s" % (sz, ln, fn)) sz_sum += sz ln_sum += ln ext_counts[ext] += 1 print() print("Number of files by extension:") libtbx.str_utils.show_sorted_by_counts(label_count_pairs=list( ext_counts.items()), prefix=" ") print() n = len(sz_ln_fn_ext) print("Number of files:", n) print("Number of lines:", ln_sum) if (n != 0): print( "Sum of sizes:", sz_sum, "(mean: %d, median: %d)" % (iround(sz_sum / n), sz_ln_fn_ext[n // 2][0])) import zlib print("Size of all files together zlib.compress'ed:", \ len(zlib.compress("".join(contents)))) print()
def sigmaa_estimator_kernel_width_d_star_cubed( r_free_flags, kernel_width_free_reflections): assert kernel_width_free_reflections > 0 n_refl = r_free_flags.size() n_free = r_free_flags.data().count(True) n_refl_per_bin = kernel_width_free_reflections if (n_free != 0): n_refl_per_bin *= n_refl / n_free n_refl_per_bin = min(n_refl, iround(n_refl_per_bin)) n_bins = max(1, n_refl / max(1, n_refl_per_bin)) dsc_min, dsc_max = [dss**(3/2) for dss in r_free_flags.min_max_d_star_sq()] return (dsc_max - dsc_min) / n_bins
def run(args): if (len(args) == 0): args = ["."] from libtbx.math_utils import iround import libtbx.str_utils import libtbx.path import os op = os.path file_names = [] for arg in args: if (op.isfile(arg)): file_names.append(top) elif (op.isdir(arg)): file_names.extend(libtbx.path.walk_source_tree(top=arg)) contents = [] sz_ln_fn_ext = [] for fn in file_names: i = fn.rfind(".") if (i < 0): continue ext = fn[i+1:] if (ext in ["c", "h", "cpp", "hpp", "py", "java", "f", "sh", "csh", "bat"]): content = open(fn, "rb").read() contents.append(content) sz_ln_fn_ext.append((len(content), len(content.splitlines()), fn, ext)) sz_ln_fn_ext.sort() sz_sum = 0 ln_sum = 0 ext_counts = libtbx.dict_with_default_0() for sz,ln,fn,ext in sz_ln_fn_ext: print "%10d %6d %s" % (sz,ln,fn) sz_sum += sz ln_sum += ln ext_counts[ext] += 1 print print "Number of files by extension:" libtbx.str_utils.show_sorted_by_counts( label_count_pairs=ext_counts.items(), prefix=" ") print n = len(sz_ln_fn_ext) print "Number of files:", n print "Number of lines:", ln_sum if (n != 0): print "Sum of sizes:", sz_sum, "(mean: %d, median: %d)" % ( iround(sz_sum/n), sz_ln_fn_ext[n//2][0]) import zlib print "Size of all files together zlib.compress'ed:", \ len(zlib.compress("".join(contents))) print
def exercise_fft_map_as_xplor_map(space_group_info, n_elements=10, d_min=3): structure = random_structure.xray_structure( space_group_info, elements=["Si"]*n_elements, volume_per_atom=1000, min_distance=3., general_positions_only=False) f_calc = structure.structure_factors( d_min=d_min, anomalous_flag=False).f_calc() fft_map = f_calc.fft_map() fft_map.as_xplor_map( file_name="tmp.map", gridding_last=[n-1 for n in fft_map.n_real()]) read = iotbx.xplor.map.reader(file_name="tmp.map") assert read.title_lines == ["cctbx.miller.fft_map"] assert read.gridding.n == fft_map.n_real() assert approx_equal(flex.linear_correlation( read.data.as_1d(), fft_map.real_map_unpadded(in_place=False).as_1d()).coefficient(), 1) for first,last in [[(0,0,0),(3,5,6)], [(-1,-3,4),(6,4,5)], [(-2,3,0),(-2,3,0)], [(-2,3,0),(-2,3,3)], [(-2,3,0),(-2,8,0)], [(-2,3,0),(-2,9,0)], [(-2,3,0),(3,3,0)], [(-2,3,0),(4,3,0)]]: fft_map.as_xplor_map( file_name="tmp.map", gridding_first=first, gridding_last=last) read = iotbx.xplor.map.reader(file_name="tmp.map") assert read.title_lines == ["cctbx.miller.fft_map"] assert read.gridding.n == fft_map.n_real() assert read.gridding.first == first assert read.gridding.last == last real_map = fft_map.real_map() first_p1 = [i%n for i,n in zip(first, fft_map.n_real())] assert eps_eq(read.data[first], real_map[first_p1], eps=1.e-4) last_p1 = [i%n for i,n in zip(last, fft_map.n_real())] assert eps_eq(read.data[last], real_map[last_p1], eps=1.e-4) for x in xrange(1,10): point = [iround(f+(l-f)*x/10.) for f,l in zip(first,last)] point_p1 = [i%n for i,n in zip(point, fft_map.n_real())] assert eps_eq(read.data[point], real_map[point_p1], eps=1.e-4)
def test_list(self,key): niggli_cell=self.coord_system_lookup[key] frac = matrix.sqr(niggli_cell.fractionalization_matrix()) orth = matrix.sqr(niggli_cell.orthogonalization_matrix()) for item in globals()[key].split('\n')[5:]: type_plus_minus = int(item.split(',')[0]) sense = int(item.split(',')[1]) direct_space_axis = [int(x) for x in item.split(',')[2].split(' ')] reference_W = tuple([int(x) for x in item.split(',')[3].split(' ')]) D = matrix.col(direct_space_axis) cartesian_axis = orth*D W_cart = matrix.sqr( sgtbx.n_fold_operator_from_axis_direction( ev_cart=cartesian_axis, n=abs(type_plus_minus), sense=sense)) W_frac = frac*W_cart*orth W_as_int = matrix.sqr([iround(e) for e in W_frac.elems]) if type_plus_minus<0: W_as_int = -1 * W_as_int assert W_as_int.elems == reference_W
def exercise_fft_map_as_xplor_map(space_group_info, n_elements=10, d_min=3): structure = random_structure.xray_structure(space_group_info, elements=["Si"] * n_elements, volume_per_atom=1000, min_distance=3., general_positions_only=False) f_calc = structure.structure_factors(d_min=d_min, anomalous_flag=False).f_calc() fft_map = f_calc.fft_map() fft_map.as_xplor_map(file_name="tmp.map", gridding_last=[n - 1 for n in fft_map.n_real()]) read = iotbx.xplor.map.reader(file_name="tmp.map") assert read.title_lines == ["cctbx.miller.fft_map"] assert read.gridding.n == fft_map.n_real() assert approx_equal( flex.linear_correlation( read.data.as_1d(), fft_map.real_map_unpadded(in_place=False).as_1d()).coefficient(), 1) for first, last in [[(0, 0, 0), (3, 5, 6)], [(-1, -3, 4), (6, 4, 5)], [(-2, 3, 0), (-2, 3, 0)], [(-2, 3, 0), (-2, 3, 3)], [(-2, 3, 0), (-2, 8, 0)], [(-2, 3, 0), (-2, 9, 0)], [(-2, 3, 0), (3, 3, 0)], [(-2, 3, 0), (4, 3, 0)]]: fft_map.as_xplor_map(file_name="tmp.map", gridding_first=first, gridding_last=last) read = iotbx.xplor.map.reader(file_name="tmp.map") assert read.title_lines == ["cctbx.miller.fft_map"] assert read.gridding.n == fft_map.n_real() assert read.gridding.first == first assert read.gridding.last == last real_map = fft_map.real_map() first_p1 = [i % n for i, n in zip(first, fft_map.n_real())] assert eps_eq(read.data[first], real_map[first_p1], eps=1.e-4) last_p1 = [i % n for i, n in zip(last, fft_map.n_real())] assert eps_eq(read.data[last], real_map[last_p1], eps=1.e-4) for x in xrange(1, 10): point = [ iround(f + (l - f) * x / 10.) for f, l in zip(first, last) ] point_p1 = [i % n for i, n in zip(point, fft_map.n_real())] assert eps_eq(read.data[point], real_map[point_p1], eps=1.e-4)
def is_allowed_origin_shift(self, shift, tolerance): """ Determine whether the specified fractional coordinate shift is allowed under the space group rules. :param shift: tuple specifying fractional coordinate shift :param tolerance: tolerance for coordinate shifts outside the allowed range :returns: Python boolean """ from libtbx.math_utils import iround is_ltr = lambda v: max([abs(x - iround(x)) for x in v]) < tolerance z2p_op = self.group().z2p_op() primitive_self = self.change_basis(z2p_op) primitive_shift = z2p_op.c() * shift if is_ltr(primitive_shift): return True for s in primitive_self.any_generator_set().primitive_generators: w_m_i = s.r().minus_unit_mx() t = w_m_i * primitive_shift if not is_ltr(t): return False else: return True
def is_allowed_origin_shift(self, shift, tolerance): """ Determine whether the specified fractional coordinate shift is allowed under the space group rules. :param shift: tuple specifying fractional coordinate shift :param tolerance: tolerance for coordinate shifts outside the allowed range :returns: Python boolean """ from libtbx.math_utils import iround is_ltr = lambda v: max([ abs(x-iround(x)) for x in v ]) < tolerance z2p_op = self.group().z2p_op() primitive_self = self.change_basis(z2p_op) primitive_shift = z2p_op.c() * shift if is_ltr(primitive_shift): return True for s in primitive_self.any_generator_set().primitive_generators: w_m_i = s.r().minus_unit_mx() t = w_m_i * primitive_shift if not is_ltr(t): return False else: return True
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 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 split_resolution_range(d_spacings, n_bodies, target, target_auto_switch_resolution, n_ref_first, multi_body_factor_n_ref_first, d_low, d_high, number_of_zones, zone_exponent, log=None): assert n_bodies > 0 assert target_auto_switch_resolution > 0 assert multi_body_factor_n_ref_first is None or multi_body_factor_n_ref_first > 0 assert n_ref_first is None or n_ref_first > 0 assert d_low is None or d_low > 0 assert d_high is None or d_high > 0 assert n_ref_first is not None or d_low is not None if (d_low is not None and d_high is not None): assert d_low > d_high assert number_of_zones is None or number_of_zones > 0 assert zone_exponent > 0 if (log is None): log = sys.stdout n_refl_data = d_spacings.size() d_spacings = d_spacings.select( flex.sort_permutation(d_spacings, reverse=True)) d_max, d_min = d_spacings[0], d_spacings[-1] if (d_high is not None and d_min < d_high): d_spacings = d_spacings.select(d_spacings >= d_high) d_high = d_spacings[-1] m_ref_first = n_ref_first if (n_ref_first is None): final_n_ref_first = (d_spacings >= d_low).count(True) else: if (multi_body_factor_n_ref_first is not None): m_ref_first += iround(m_ref_first * (n_bodies - 1) * multi_body_factor_n_ref_first) assert m_ref_first > 0 if (d_low is not None and m_ref_first <= d_spacings.size() and d_spacings[m_ref_first - 1] > d_low): final_n_ref_first = (d_spacings >= d_low).count(True) else: final_n_ref_first = m_ref_first d_mins = [] if (number_of_zones is not None and number_of_zones > 1): degenerate = final_n_ref_first > d_spacings.size() if (degenerate): d_mins.append(d_high) else: zone_factor = (d_spacings.size() - final_n_ref_first) \ / ((number_of_zones-1)**zone_exponent) d_mins.append(d_spacings[final_n_ref_first - 1]) for i_zone in range(1, number_of_zones): n = iround(final_n_ref_first + zone_factor * i_zone**zone_exponent) if (i_zone == number_of_zones - 1): assert n == d_spacings.size() # sanity check d_mins.append(d_spacings[n - 1]) else: final_n_ref_first = min(final_n_ref_first, d_spacings.size()) degenerate = False d_mins.append(d_high) print("Rigid body refinement:", file=log) print(" Requested number of resolution zones: %d" % number_of_zones, file=log) if (len(d_mins) != 1 or degenerate): print(" Calculation for first resolution zone:", file=log) print(" Requested number of reflections per body:", n_ref_first, file=log) print(" Requested factor per body:", \ multi_body_factor_n_ref_first, file=log) print(" Number of bodies:", n_bodies, file=log) print(" Resulting number of reflections:", m_ref_first, file=log) print(" Requested low-resolution limit:", d_low, end=' ', file=log) if (final_n_ref_first != m_ref_first): print("(determines final number)", end=' ', file=log) print(file=log) print(" Final number of reflections:", final_n_ref_first, file=log) print(" Data resolution: %6.2f - %6.2f" \ " (%d reflections)" % (d_max, d_min, n_refl_data), file=log) print(" Resolution for rigid body refinement: %6.2f - %6.2f" \ " (%d reflections)" % (d_max, d_high, d_spacings.size()), file=log) if (degenerate): print("""\ WARNING: Final number of reflections for first resolution zone is greater than the number of available reflections (%d > %d). INFO: Number of resolution zones reset to 1.""" % (final_n_ref_first, d_spacings.size()), file=log) target_names = [] for d_min in d_mins: if (target == "auto"): if (d_min > target_auto_switch_resolution): target_names.append("ls_wunit_k1") else: target_names.append("ml") else: target_names.append(target) if (len(d_mins) > 1): print(" Resolution cutoffs for multiple zones: ", file=log) print(" number of", file=log) print(" zone resolution reflections target", file=log) for i, d_i in enumerate(d_mins): n_ref = (d_spacings >= d_i).count(True) print(" %3d %6.2f -%6.2f %11d %s" % (i + 1, d_max, d_i, n_ref, target_names[i]), file=log) print(" zone number of reflections =" \ " %d + %.6g * (zone-1)**%.6g" % ( final_n_ref_first, zone_factor, zone_exponent), file=log) return d_mins, target_names
def split_resolution_range( d_spacings, n_bodies, target, target_auto_switch_resolution, n_ref_first, multi_body_factor_n_ref_first, d_low, d_high, number_of_zones, zone_exponent, log = None): assert n_bodies > 0 assert target_auto_switch_resolution > 0 assert multi_body_factor_n_ref_first is None or multi_body_factor_n_ref_first > 0 assert n_ref_first is None or n_ref_first > 0 assert d_low is None or d_low > 0 assert d_high is None or d_high > 0 assert n_ref_first is not None or d_low is not None if (d_low is not None and d_high is not None): assert d_low > d_high assert number_of_zones is None or number_of_zones > 0 assert zone_exponent > 0 if (log is None): log = sys.stdout n_refl_data = d_spacings.size() d_spacings = d_spacings.select( flex.sort_permutation(d_spacings, reverse = True)) d_max, d_min = d_spacings[0], d_spacings[-1] if (d_high is not None and d_min < d_high): d_spacings = d_spacings.select(d_spacings >= d_high) d_high = d_spacings[-1] m_ref_first = n_ref_first if (n_ref_first is None): final_n_ref_first = (d_spacings >= d_low).count(True) else: if (multi_body_factor_n_ref_first is not None): m_ref_first += iround( m_ref_first * (n_bodies-1) * multi_body_factor_n_ref_first) assert m_ref_first > 0 if ( d_low is not None and m_ref_first <= d_spacings.size() and d_spacings[m_ref_first-1] > d_low): final_n_ref_first = (d_spacings >= d_low).count(True) else: final_n_ref_first = m_ref_first d_mins = [] if (number_of_zones is not None and number_of_zones > 1): degenerate = final_n_ref_first > d_spacings.size() if (degenerate): d_mins.append(d_high) else: zone_factor = (d_spacings.size() - final_n_ref_first) \ / ((number_of_zones-1)**zone_exponent) d_mins.append(d_spacings[final_n_ref_first-1]) for i_zone in range(1, number_of_zones): n = iround(final_n_ref_first + zone_factor * i_zone**zone_exponent) if (i_zone == number_of_zones - 1): assert n == d_spacings.size() # sanity check d_mins.append(d_spacings[n-1]) else: final_n_ref_first = min(final_n_ref_first, d_spacings.size()) degenerate = False d_mins.append(d_high) print >> log, "Rigid body refinement:" print >> log, " Requested number of resolution zones: %d" % number_of_zones if (len(d_mins) != 1 or degenerate): print >> log, " Calculation for first resolution zone:" print >> log, " Requested number of reflections per body:", n_ref_first print >> log, " Requested factor per body:", \ multi_body_factor_n_ref_first print >> log, " Number of bodies:", n_bodies print >> log, " Resulting number of reflections:", m_ref_first print >> log, " Requested low-resolution limit:", d_low, if (final_n_ref_first != m_ref_first): print >> log, "(determines final number)", print >> log print >> log, " Final number of reflections:", final_n_ref_first print >> log, " Data resolution: %6.2f - %6.2f" \ " (%d reflections)" % (d_max, d_min, n_refl_data) print >> log, " Resolution for rigid body refinement: %6.2f - %6.2f" \ " (%d reflections)" % (d_max, d_high, d_spacings.size()) if (degenerate): print >> log, """\ WARNING: Final number of reflections for first resolution zone is greater than the number of available reflections (%d > %d). INFO: Number of resolution zones reset to 1.""" % ( final_n_ref_first, d_spacings.size()) target_names = [] for d_min in d_mins: if (target == "auto"): if (d_min > target_auto_switch_resolution): target_names.append("ls_wunit_k1") else: target_names.append("ml") else: target_names.append(target) if (len(d_mins) > 1): print >> log, " Resolution cutoffs for multiple zones: " print >> log, " number of" print >> log, " zone resolution reflections target" for i, d_i in enumerate(d_mins): n_ref = (d_spacings >= d_i).count(True) print >> log, " %3d %6.2f -%6.2f %11d %s" % ( i+1, d_max, d_i, n_ref, target_names[i]) print >> log, " zone number of reflections =" \ " %d + %.6g * (zone-1)**%.6g" % ( final_n_ref_first, zone_factor, zone_exponent) return d_mins, target_names