def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in xrange(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot(x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
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 run(args): assert len(args) == 1 # Read file into pdb_input class inp = iotbx.pdb.input(file_name=args[0]) # create a model manager model = mmtbx.model.manager(model_input=inp) # get number of atoms in the input model n_atoms = model.get_number_of_atoms() # extract atom coordinates old_sites_cart = model.get_sites_cart() # generate random additions random_addition = flex.vec3_double( flex.random_double(size=n_atoms * 3) - 0.5) # actually add them to old coordinates new_xyz = old_sites_cart + random_addition # Update coordinates in model manager model.set_sites_cart(sites_cart=new_xyz) # get xray structure xrs = model.get_xray_structure() # reset B-factors (min=1, max=20) # generate array of new B-factors new_b = flex.random_double(size=n_atoms, factor=19) + 1 # set them in xray structure xrs.set_b_iso(values=new_b) # update model manager with this xray structure model.set_xray_structure(xrs) # output result in PDB format to the screen print model.model_as_pdb() print "END"
def exercise_core_LS(target_class, verbose): n_refl = 10 f_calc = flex.polar( flex.random_double(n_refl)*10-5, flex.random_double(n_refl)*10-5) f_obs = flex.abs(f_calc) + (flex.random_double(n_refl)*2-1) weights = flex.random_double(n_refl) r = xray.targets_least_squares_residual( f_obs, weights, f_calc, True, 0) scale_factor = r.scale_factor() gr_ana = r.derivatives() gr_fin = flex.complex_double() eps = 1.e-6 for i_refl in xrange(n_refl): gc = [] for i_part in [0,1]: fc0 = f_calc[i_refl] ts = [] for signed_eps in [eps,-eps]: if (i_part == 0): f_calc[i_refl] = complex(fc0.real + signed_eps, fc0.imag) else: f_calc[i_refl] = complex(fc0.real, fc0.imag + signed_eps) r = xray.targets_least_squares_residual( f_obs, weights, f_calc, False, scale_factor) ts.append(r.target()) f_calc[i_refl] = fc0 gc.append((ts[0]-ts[1])/(2*eps)) gr_fin.append(complex(*gc)) if (verbose): print "ana:", list(gr_ana) print "fin:", list(gr_fin) assert approx_equal(gr_fin, gr_ana)
def exercise_core_LS(target_class, verbose): n_refl = 10 f_calc = flex.polar( flex.random_double(n_refl) * 10 - 5, flex.random_double(n_refl) * 10 - 5) f_obs = flex.abs(f_calc) + (flex.random_double(n_refl) * 2 - 1) weights = flex.random_double(n_refl) r = xray.targets_least_squares_residual(f_obs, weights, f_calc, True, 0) scale_factor = r.scale_factor() gr_ana = r.derivatives() gr_fin = flex.complex_double() eps = 1.e-6 for i_refl in xrange(n_refl): gc = [] for i_part in [0, 1]: fc0 = f_calc[i_refl] ts = [] for signed_eps in [eps, -eps]: if (i_part == 0): f_calc[i_refl] = complex(fc0.real + signed_eps, fc0.imag) else: f_calc[i_refl] = complex(fc0.real, fc0.imag + signed_eps) r = xray.targets_least_squares_residual( f_obs, weights, f_calc, False, scale_factor) ts.append(r.target()) f_calc[i_refl] = fc0 gc.append((ts[0] - ts[1]) / (2 * eps)) gr_fin.append(complex(*gc)) if (verbose): print "ana:", list(gr_ana) print "fin:", list(gr_fin) assert approx_equal(gr_fin, gr_ana)
def exercise_trigonometric_ff(): from math import cos, sin, pi sgi = sgtbx.space_group_info("P1") cs = sgi.any_compatible_crystal_symmetry(volume=1000) miller_set = miller.build_set(cs, anomalous_flag=False, d_min=1) miller_set = miller_set.select(flex.random_double(miller_set.size()) < 0.2) for i in range(5): sites = flex.random_double(9) x1, x2, x3 = (matrix.col(sites[:3]), matrix.col(sites[3:6]), matrix.col(sites[6:])) xs = xray.structure(crystal.special_position_settings(cs)) for x in (x1, x2, x3): sc = xray.scatterer(site=x, scattering_type="const") sc.flags.set_grad_site(True) xs.add_scatterer(sc) f_sq = structure_factors.f_calc_modulus_squared(xs) for h in miller_set.indices(): h = matrix.col(h) phi1, phi2, phi3 = 2 * pi * h.dot(x1), 2 * pi * h.dot( x2), 2 * pi * h.dot(x3) fc_mod_sq = 3 + 2 * (cos(phi1 - phi2) + cos(phi2 - phi3) + cos(phi3 - phi1)) g = [] g.extend(-2 * (sin(phi1 - phi2) - sin(phi3 - phi1)) * 2 * pi * h) g.extend(-2 * (sin(phi2 - phi3) - sin(phi1 - phi2)) * 2 * pi * h) g.extend(-2 * (sin(phi3 - phi1) - sin(phi2 - phi3)) * 2 * pi * h) grad_fc_mod_sq = g f_sq.linearise(h) assert approx_equal(f_sq.observable, fc_mod_sq) assert approx_equal(f_sq.grad_observable, grad_fc_mod_sq)
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 exercise_unmerged () : quartz_structure = xray.structure( special_position_settings=crystal.special_position_settings( crystal_symmetry=crystal.symmetry( unit_cell=(5.01,5.01,5.47,90,90,120), space_group_symbol="P6222")), scatterers=flex.xray_scatterer([ xray.scatterer( label="Si", site=(1/2.,1/2.,1/3.), u=0.2), xray.scatterer( label="O", site=(0.197,-0.197,0.83333), u=0.1)])) quartz_structure.set_inelastic_form_factors( photon=1.54, table="sasaki") fc = abs(quartz_structure.structure_factors(d_min=1.0).f_calc()) symm = fc.crystal_symmetry() icalc = fc.expand_to_p1().f_as_f_sq().set_observation_type_xray_intensity() # generate 'unmerged' data i_obs = icalc.customized_copy(crystal_symmetry=symm) # now make up sigmas and some (hopefully realistic) error flex.set_random_seed(12345) n_refl = i_obs.size() sigmas = flex.random_double(n_refl) * flex.mean(fc.data()) sigmas = icalc.customized_copy(data=sigmas).apply_debye_waller_factors( u_iso=0.15) err = (flex.double(n_refl, 0.5) - flex.random_double(n_refl)) * 2 i_obs = i_obs.customized_copy( sigmas=sigmas.data(), data=i_obs.data() + err) # check for unmerged acentrics assert i_obs.is_unmerged_intensity_array() i_obs_centric = i_obs.select(i_obs.centric_flags().data()) i_obs_acentric = i_obs.select(~(i_obs.centric_flags().data())) i_mrg_acentric = i_obs_acentric.merge_equivalents().array() i_mixed = i_mrg_acentric.concatenate(i_obs_centric) assert not i_mixed.is_unmerged_intensity_array() # XXX These results of these functions are heavily dependent on the # behavior of the random number generator, which is not consistent across # platforms - therefore we can only check for very approximate values. # Exact numerical results are tested with real data (stored elsewhere). # CC1/2, etc. assert approx_equal(i_obs.cc_one_half(), 0.9998, eps=0.001) assert i_obs.resolution_filter(d_max=1.2).cc_one_half() > 0 assert i_obs.cc_anom() > 0.1 r_ano = i_obs.r_anom() assert approx_equal(r_ano, 0.080756, eps=0.0001) # merging stats i_mrg = i_obs.merge_equivalents() assert i_mrg.r_merge() < 0.1 assert i_mrg.r_meas() < 0.1 assert i_mrg.r_pim() < 0.05
def exercise_least_squares_residual(): crystal_symmetry = crystal.symmetry(unit_cell=(6, 3, 8, 90, 90, 90), space_group_symbol="P222") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=0.7) f_obs = miller_set.array(data=flex.random_double(miller_set.size()), sigmas=flex.random_double(miller_set.size()) * 0.05) ls = xray.least_squares_residual( f_obs, use_sigmas_as_weights=True, )
def randomize_phases(f_calc, fudge_factor): assert 0 <= fudge_factor <= 1 phases = flex.arg(f_calc.data(), True) centric_flags = f_calc.centric_flags().data() acentric_flags = ~centric_flags centric_phases = phases.select(centric_flags) acentric_phases = phases.select(acentric_flags) sel = flex.random_double(size=centric_phases.size()) < (0.5 * fudge_factor) centric_phases.set_selected(sel, centric_phases.select(sel) + 180) acentric_phases += (flex.random_double(size=acentric_phases.size()) * 360 - 180) * fudge_factor phases.set_selected(centric_flags, centric_phases) phases.set_selected(acentric_flags, acentric_phases) return f_calc.phase_transfer(phases, deg=True)
def exercise_least_squares_residual(): crystal_symmetry = crystal.symmetry( unit_cell=(6,3,8,90,90,90), space_group_symbol="P222") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=0.7) f_obs = miller_set.array( data=flex.random_double(miller_set.size()), sigmas=flex.random_double(miller_set.size())*0.05) ls = xray.least_squares_residual( f_obs, use_sigmas_as_weights=True, )
def exercise_unmerged(): quartz_structure = xray.structure( special_position_settings=crystal.special_position_settings( crystal_symmetry=crystal.symmetry(unit_cell=(5.01, 5.01, 5.47, 90, 90, 120), space_group_symbol="P6222")), scatterers=flex.xray_scatterer([ xray.scatterer(label="Si", site=(1 / 2., 1 / 2., 1 / 3.), u=0.2), xray.scatterer(label="O", site=(0.197, -0.197, 0.83333), u=0.1) ])) quartz_structure.set_inelastic_form_factors(photon=1.54, table="sasaki") fc = abs(quartz_structure.structure_factors(d_min=1.0).f_calc()) symm = fc.crystal_symmetry() icalc = fc.expand_to_p1().f_as_f_sq().set_observation_type_xray_intensity() # generate 'unmerged' data i_obs = icalc.customized_copy(crystal_symmetry=symm) # now make up sigmas and some (hopefully realistic) error flex.set_random_seed(12345) n_refl = i_obs.size() sigmas = flex.random_double(n_refl) * flex.mean(fc.data()) sigmas = icalc.customized_copy(data=sigmas).apply_debye_waller_factors( u_iso=0.15) err = (flex.double(n_refl, 0.5) - flex.random_double(n_refl)) * 2 i_obs = i_obs.customized_copy(sigmas=sigmas.data(), data=i_obs.data() + err) # check for unmerged acentrics assert i_obs.is_unmerged_intensity_array() i_obs_centric = i_obs.select(i_obs.centric_flags().data()) i_obs_acentric = i_obs.select(~(i_obs.centric_flags().data())) i_mrg_acentric = i_obs_acentric.merge_equivalents().array() i_mixed = i_mrg_acentric.concatenate(i_obs_centric) assert not i_mixed.is_unmerged_intensity_array() # XXX These results of these functions are heavily dependent on the # behavior of the random number generator, which is not consistent across # platforms - therefore we can only check for very approximate values. # Exact numerical results are tested with real data (stored elsewhere). # CC1/2, etc. assert approx_equal(i_obs.cc_one_half(), 0.9999, eps=0.001) assert approx_equal(i_obs.cc_one_half_sigma_tau(), 0.9999, eps=0.001) assert i_obs.resolution_filter(d_max=1.2).cc_one_half() > 0 assert i_obs.cc_anom() > 0.1 r_ano = i_obs.r_anom() assert approx_equal(r_ano, 0.080756, eps=0.0001) # merging stats i_mrg = i_obs.merge_equivalents() assert i_mrg.r_merge() < 0.1 assert i_mrg.r_meas() < 0.1 assert i_mrg.r_pim() < 0.05
def recycle(): for n, first, last in [[(5, 3, 4), (0, 0, 0), (3, 5, 6)], [(4, 3, 5), (-1, -3, 4), (6, 4, 5)], [(3, 4, 5), (-2, 3, 0), (-2, 3, 0)], [(3, 4, 5), (-2, 3, 0), (-2, 3, 3)], [(3, 4, 5), (-2, 3, 0), (-2, 8, 0)], [(3, 4, 5), (-2, 3, 0), (-2, 9, 0)], [(3, 4, 5), (-2, 3, 0), (3, 3, 0)], [(3, 4, 5), (-2, 3, 0), (4, 3, 0)]]: gridding = iotbx.xplor.map.gridding(n=n, first=first, last=last) flex_grid = gridding.as_flex_grid() data = 20000 * flex.random_double(size=flex_grid.size_1d()) - 10000 data.resize(flex_grid) stats = maptbx.statistics(data) iotbx.xplor.map.writer(file_name="tmp.map", title_lines=["regression test"], unit_cell=uctbx.unit_cell( (10, 20, 30, 80, 90, 100)), gridding=gridding, data=data, average=stats.mean(), standard_deviation=stats.sigma()) read = iotbx.xplor.map.reader(file_name="tmp.map") assert read.title_lines == ["regression test"] assert read.gridding.n == n assert read.gridding.first == first assert read.gridding.last == last assert read.unit_cell.is_similar_to( uctbx.unit_cell((10, 20, 30, 80, 90, 100))) assert eps_eq(read.average, stats.mean(), eps=1.e-4) assert eps_eq(read.standard_deviation, stats.sigma(), eps=1.e-4) assert read.data.origin() == first assert read.data.last(False) == last assert read.data.focus() == data.focus() assert eps_eq(read.data, data, eps=1.e-4)
def generate_mtz_files(space_group_info, anomalous_flag): crystal_symmetry = crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=1000), space_group_info=space_group_info) miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=anomalous_flag, d_min=1) miller_array = miller.array( miller_set=miller_set, data=flex.random_double(size=miller_set.indices().size())) miller_array_p1 = miller_array.expand_to_p1() miller_arrays = [] file_names = [] subgrs = subgroups.subgroups(space_group_info).groups_parent_setting() for i_subgroup, subgroup in enumerate(subgrs): subgroup_miller_array = miller_array_p1.customized_copy( space_group_info=sgtbx.space_group_info(group=subgroup)) \ .merge_equivalents() \ .array() \ .as_reference_setting() \ .set_observation_type_xray_intensity() file_name = "tmp_refl_stats%d.mtz" % i_subgroup mtz_object = subgroup_miller_array.as_mtz_dataset( column_root_label="FOBS").mtz_object().write(file_name=file_name) miller_arrays.append( subgroup_miller_array.f_sq_as_f().expand_to_p1().map_to_asu()) file_names.append(file_name) return miller_arrays, file_names
def structures_forward(self, xs, xs_forward, eta_norm): while True: direction = flex.random_double(xs.n_parameters()) direction /= direction.norm() eta = eta_norm * direction i = 0 for sc_forward, sc in zip(xs_forward.scatterers(), xs.scatterers()): eta_site = matrix.col(eta[i:i + 3]) eta_iso = eta[i + 3] eta_aniso = matrix.col(eta[i + 4:i + 10]) eta_occ = eta[i + 10] if self.inelastic_scattering: eta_fp = eta[i + 11] eta_fdp = eta[i + 12] i += 13 else: i += 11 sc_forward.site = matrix.col(sc.site) + eta_site sc_forward.u_iso = sc.u_iso + eta_iso sc_forward.u_star = matrix.col(sc.u_star) + eta_aniso sc_forward.occupancy = sc.occupancy + eta_occ if self.inelastic_scattering: sc_forward.fp = sc.fp + eta_fp sc_forward.fdp = sc.fdp + eta_fdp yield direction
def generate_mtz_files(space_group_info, anomalous_flag): crystal_symmetry = crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=1000), space_group_info=space_group_info) miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=anomalous_flag, d_min=1) miller_array = miller.array( miller_set=miller_set, data=flex.random_double(size=miller_set.indices().size())) miller_array_p1 = miller_array.expand_to_p1() miller_arrays = [] file_names = [] subgrs = subgroups.subgroups(space_group_info).groups_parent_setting() for i_subgroup, subgroup in enumerate(subgrs): subgroup_miller_array = miller_array_p1.customized_copy( space_group_info=sgtbx.space_group_info(group=subgroup)) \ .merge_equivalents() \ .array() \ .as_reference_setting() \ .set_observation_type_xray_intensity() file_name = "tmp_refl_stats%d.mtz" % i_subgroup mtz_object = subgroup_miller_array.as_mtz_dataset( column_root_label="FOBS").mtz_object().write(file_name=file_name) miller_arrays.append( subgroup_miller_array.f_sq_as_f().expand_to_p1().map_to_asu()) file_names.append(file_name) return miller_arrays, file_names
def exercise(xray_structure, anomalous_flag, max_n_indices, out): xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max(1, min(xray_structure.unit_cell().parameters()[:3]) / 2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors(xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) noise_fin = compare_analytical_and_finite(f_obs=f_obs, xray_structure=xray_structure, gradients_should_be_zero=True, eps=1.e-5, out=out) compare_analytical_and_finite(f_obs=f_obs.customized_copy( data=f_obs.data() * (flex.random_double(size=f_obs.size()) + 0.5)), xray_structure=xray_structure, gradients_should_be_zero=False, eps=max(1.e-5, noise_fin), out=out)
def testing_function_for_rsfit(basic_map,delta_h,xray_structure,out): for i_trial in xrange(100): sites_cart = flex.vec3_double((flex.random_double(size=3)-0.5)*1) tmp_sites_cart = sites_cart.deep_copy() for i in xrange(3): ref = lbfgs( basic_map=basic_map, sites_cart=tmp_sites_cart, delta_h=delta_h) temp = flex.double(ref.sites_cart[0])-flex.double((0,0,0)) temp = math.sqrt(temp.dot(temp)) if temp <= 2*delta_h: break print >> out, "recycling:", ref.sites_cart[0] tmp_sites_cart = ref.sites_cart for site,sitec in zip(ref.sites_cart,xray_structure.sites_cart()): print >> out, i_trial print >> out, sitec print >> out, sites_cart[0] print >> out, site temp = flex.double(site)-flex.double(sitec) temp = math.sqrt(temp.dot(temp)) print >> out, temp, delta_h assert temp <= delta_h*2 print >> out
def exercise_SFweight_spline_core(structure, d_min, verbose=0): structure.scattering_type_registry(d_min=d_min) f_obs = abs(structure.structure_factors( d_min=d_min, anomalous_flag=False).f_calc()) if (0 or verbose): f_obs.show_summary() f_obs = miller.array( miller_set=f_obs, data=f_obs.data(), sigmas=flex.sqrt(f_obs.data())) partial_structure = xray.structure( crystal_symmetry=structure, scatterers=structure.scatterers()[:-2]) f_calc = f_obs.structure_factors_from_scatterers( xray_structure=partial_structure).f_calc() test_set_flags = (flex.random_double(size=f_obs.indices().size()) < 0.1) sfweight = clipper.SFweight_spline_interface( unit_cell=f_obs.unit_cell(), space_group=f_obs.space_group(), miller_indices=f_obs.indices(), anomalous_flag=f_obs.anomalous_flag(), f_obs_data=f_obs.data(), f_obs_sigmas=f_obs.sigmas(), f_calc=f_calc.data(), test_set_flags=test_set_flags, n_refln=f_obs.indices().size()//10, n_param=20) if (0 or verbose): print "number_of_spline_parameters:",sfweight.number_of_spline_parameters() print "mean fb: %.8g" % flex.mean(flex.abs(sfweight.fb())) print "mean fd: %.8g" % flex.mean(flex.abs(sfweight.fd())) print "mean phi: %.8g" % flex.mean(sfweight.centroid_phases()) print "mean fom: %.8g" % flex.mean(sfweight.figures_of_merit()) return sfweight
def exercise(self, fixed_twin_fraction): # Create a shaken structure xs ready for refinement xs0 = self.structure emma_ref = xs0.as_emma_model() xs = xs0.deep_copy_scatterers() xs.shake_sites_in_place(rms_difference=0.15) xs.shake_adp() for sc in xs.scatterers(): sc.flags.set_use_u_iso(False).set_use_u_aniso(True) sc.flags.set_grad_site(True).set_grad_u_aniso(True) # Setup L.S. problem connectivity_table = smtbx.utils.connectivity_table(xs) shaken_twin_fraction = (self.twin_fraction if fixed_twin_fraction else self.twin_fraction + 0.1 * flex.random_double()) # 2nd domain in __init__ twin_components = (xray.twin_component(twin_law=self.twin_law.r(), value=shaken_twin_fraction, grad=not fixed_twin_fraction), ) reparametrisation = constraints.reparametrisation( structure=xs, constraints=[], connectivity_table=connectivity_table, twin_fractions=twin_components) obs = self.fo_sq.as_xray_observations(twin_components=twin_components) ls = least_squares.crystallographic_ls( obs, reparametrisation, weighting_scheme=least_squares.unit_weighting(), origin_fixing_restraints_type=origin_fixing_restraints. atomic_number_weighting) # Refine till we get back the original structure (so we hope) cycles = normal_eqns_solving.levenberg_marquardt_iterations( ls, gradient_threshold=1e-12, step_threshold=1e-6, track_all=True) # Now let's start to check it all worked assert ls.n_parameters == 63 if fixed_twin_fraction else 64 match = emma.model_matches(emma_ref, xs.as_emma_model()).refined_matches[0] assert match.rt.r == matrix.identity(3) for pair in match.pairs: assert approx_equal(match.calculate_shortest_dist(pair), 0, eps=1e-4), pair if fixed_twin_fraction: assert ls.twin_fractions[0].value == self.twin_fraction else: assert approx_equal(ls.twin_fractions[0].value, self.twin_fraction, eps=1e-2) assert approx_equal(ls.scale_factor(), 1, eps=1e-5) assert approx_equal(ls.objective(), 0)
def run(args): assert len(args) == 1 pdb_inp = pdb.input(file_name=args[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() pdb_atoms = pdb_hierarchy.atoms() # add random numbers [-0.5,0.5) to coordinates new_xyz = pdb_atoms.extract_xyz() + flex.vec3_double( flex.random_double(size=pdb_atoms.size() * 3) - 0.5) pdb_atoms.set_xyz(new_xyz=new_xyz) # reset B-factors (min=1, max=20) new_b = flex.random_double(size=pdb_atoms.size(), factor=19) + 1 pdb_atoms.set_b(new_b=new_b) sys.stdout.write( pdb_hierarchy.as_pdb_string( crystal_symmetry=pdb_inp.crystal_symmetry(), append_end=True))
def run(args): assert len(args) == 1 pdb_inp = pdb.input(file_name=args[0]) pdb_hierarchy = pdb_inp.construct_hierarchy() pdb_atoms = pdb_hierarchy.atoms() # add random numbers [-0.5,0.5) to coordinates new_xyz = pdb_atoms.extract_xyz() + flex.vec3_double( flex.random_double(size=pdb_atoms.size()*3)-0.5) pdb_atoms.set_xyz(new_xyz=new_xyz) # reset B-factors (min=1, max=20) new_b = flex.random_double(size=pdb_atoms.size(), factor=19) + 1 pdb_atoms.set_b(new_b=new_b) sys.stdout.write(pdb_hierarchy.as_pdb_string( crystal_symmetry=pdb_inp.crystal_symmetry(), append_end=True))
def run(): crystal_symmetry = crystal.symmetry(unit_cell=(10, 11, 12, 85, 95, 100), space_group_symbol="P 1") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) f_calc = miller_set.array(data=flex.polar( flex.random_double(miller_set.size()) * 10 - 5, flex.random_double(miller_set.size()) * 10 - 5)) scale_factor = flex.random_double() obs = miller_set.array(data=scale_factor * flex.norm(f_calc.data()) + (flex.random_double(miller_set.size()) * 2 - 1), sigmas=flex.random_double(miller_set.size())) obs.set_observation_type_xray_intensity() exercise_shelx_weighting(f_calc, obs, scale_factor) exercise_quasi_unit_weighting(obs) print('OK')
def exercise_core(n=10, verbose=0): from libtbx.test_utils import approx_equal import random # make two random sets of sites please c = euler_angles_as_matrix([random.uniform(0, 360) for i in xrange(3)]) set_1 = flex.vec3_double(flex.random_double(n * 3) * 10 - 2) set_2 = flex.vec3_double(flex.random_double(n * 3) * 10 - 2) set_3 = tuple(c) * set_2 set_a = set_1.concatenate(set_2) set_b = set_1.concatenate(set_3) tmp = find_domain(set_a, set_b, initial_rms=0.02, match_radius=0.03, minimum_size=1) if verbose: tmp.show() assert len(tmp.matches) == 2 assert approx_equal(tmp.matches[0][3], 0, eps=1e-5) assert approx_equal(tmp.matches[0][4], n, eps=1e-5)
def __init__(selfOO): selfOO.starting_simplex = [] selfOO.n = 1 for ii in range(selfOO.n + 1): selfOO.starting_simplex.append(flex.random_double(selfOO.n)) selfOO.optimizer = simplex_opt(dimension=selfOO.n, matrix=selfOO.starting_simplex, evaluator=selfOO, tolerance=1e-4) selfOO.x = selfOO.optimizer.get_solution()
def __init__(selfOO): selfOO.starting_simplex=[] selfOO.n = 1 for ii in range(selfOO.n+1): selfOO.starting_simplex.append(flex.random_double(selfOO.n)) selfOO.optimizer = simplex_opt( dimension=selfOO.n, matrix = selfOO.starting_simplex, evaluator = selfOO, tolerance=1e-4) selfOO.x = selfOO.optimizer.get_solution()
def exercise(self, fixed_twin_fraction): # Create a shaken structure xs ready for refinement xs0 = self.structure emma_ref = xs0.as_emma_model() xs = xs0.deep_copy_scatterers() xs.shake_sites_in_place(rms_difference=0.15) xs.shake_adp() for sc in xs.scatterers(): sc.flags.set_use_u_iso(False).set_use_u_aniso(True) sc.flags.set_grad_site(True).set_grad_u_aniso(True) # Setup L.S. problem connectivity_table = smtbx.utils.connectivity_table(xs) shaken_twin_fraction = ( self.twin_fraction if fixed_twin_fraction else self.twin_fraction + 0.1*flex.random_double()) # 2nd domain in __init__ twin_components = (xray.twin_component( twin_law=self.twin_law.r(), value=shaken_twin_fraction, grad=not fixed_twin_fraction),) reparametrisation = constraints.reparametrisation( structure=xs, constraints=[], connectivity_table=connectivity_table, twin_fractions=twin_components) obs = self.fo_sq.as_xray_observations(twin_components=twin_components) ls = least_squares.crystallographic_ls( obs, reparametrisation, weighting_scheme=least_squares.unit_weighting(), origin_fixing_restraints_type= origin_fixing_restraints.atomic_number_weighting) # Refine till we get back the original structure (so we hope) cycles = normal_eqns_solving.levenberg_marquardt_iterations( ls, gradient_threshold=1e-12, step_threshold=1e-6, track_all=True) # Now let's start to check it all worked assert ls.n_parameters == 63 if fixed_twin_fraction else 64 match = emma.model_matches(emma_ref, xs.as_emma_model()).refined_matches[0] assert match.rt.r == matrix.identity(3) for pair in match.pairs: assert approx_equal(match.calculate_shortest_dist(pair), 0, eps=1e-4), pair if fixed_twin_fraction: assert ls.twin_fractions[0].value == self.twin_fraction else: assert approx_equal(ls.twin_fractions[0].value, self.twin_fraction, eps=1e-2) assert approx_equal(ls.scale_factor(), 1, eps=1e-5) assert approx_equal(ls.objective(), 0)
def exercise_optimise_shelxl_weights(): def calc_goof(fo2, fc, w, k, n_params): fc2 = fc.as_intensity_array() w = w(fo2.data(), fo2.sigmas(), fc2.data(), k) return math.sqrt( flex.sum(w * flex.pow2(fo2.data() - k * fc2.data())) / (fo2.size() - n_params)) xs = smtbx.development.sucrose() k = 0.05 + 10 * flex.random_double() fc = xs.structure_factors(anomalous_flag=False, d_min=0.7).f_calc() fo = fc.as_amplitude_array() fo = fo.customized_copy(data=fo.data() * math.sqrt(k)) fo = fo.customized_copy(sigmas=0.03 * fo.data()) sigmas = fo.sigmas() for i in range(fo.size()): fo.data()[i] += 2 * scitbx.random.variate( scitbx.random.normal_distribution(sigma=sigmas[i]))() \ + 0.5*random.random() fo2 = fo.as_intensity_array() fc2 = fc.as_intensity_array() w = least_squares.mainstream_shelx_weighting(a=0.1) s = calc_goof(fo2, fc, w, k, xs.n_parameters()) w2 = w.optimise_parameters(fo2, fc2, k, xs.n_parameters()) s2 = calc_goof(fo2, fc, w2, k, xs.n_parameters()) # sort data and setup binning by fc/fc_max fc_sq = fc.as_intensity_array() fc_sq_over_fc_sq_max = fc_sq.data() / flex.max(fc_sq.data()) permutation = flex.sort_permutation(fc_sq_over_fc_sq_max) fc_sq_over_fc_sq_max = fc_sq.customized_copy( data=fc_sq_over_fc_sq_max).select(permutation) fc_sq = fc_sq.select(permutation) fo_sq = fo2.select(permutation) n_bins = 10 bin_max = 0 bin_limits = flex.size_t(1, 0) bin_count = flex.size_t() for i in range(n_bins): bin_limits.append(int(math.ceil((i + 1) * fc_sq.size() / n_bins))) bin_count.append(bin_limits[i + 1] - bin_limits[i]) goofs_w = flex.double() goofs_w2 = flex.double() for i_bin in range(n_bins): sel = flex.size_t_range(bin_limits[i_bin], bin_limits[i_bin + 1]) goofs_w2.append( calc_goof(fo_sq.select(sel), fc_sq.select(sel), w2, k, xs.n_parameters())) goofs_w.append( calc_goof(fo_sq.select(sel), fc_sq.select(sel), w, k, xs.n_parameters())) a = flex.mean_and_variance(goofs_w).unweighted_sample_variance() b = flex.mean_and_variance(goofs_w2).unweighted_sample_variance() assert a > b or abs(1 - s) > abs(1 - s2) assert a > b # flat analysis of variance assert abs(1 - s) > abs(1 - s2) # GooF close to 1
def cartesian_random_displacements(sites_cart, target_rmsd, max_trials=10): assert sites_cart.size() != 0 for i in xrange(max_trials): shifts_cart = flex.vec3_double(flex.random_double( size=sites_cart.size()*3, factor=2) - 1) rmsd = (flex.sum(shifts_cart.dot()) / sites_cart.size()) ** 0.5 if (rmsd > 1.e-6): break # to avoid numerical problems else: raise RuntimeError shifts_cart *= (target_rmsd / rmsd) return sites_cart + shifts_cart
def exercise_optimise_shelxl_weights(): def calc_goof(fo2, fc, w, k, n_params): fc2 = fc.as_intensity_array() w = w(fo2.data(), fo2.sigmas(), fc2.data(), k) return math.sqrt(flex.sum( w * flex.pow2(fo2.data() - k*fc2.data()))/(fo2.size() - n_params)) xs = smtbx.development.sucrose() k = 0.05 + 10 * flex.random_double() fc = xs.structure_factors(anomalous_flag=False, d_min=0.7).f_calc() fo = fc.as_amplitude_array() fo = fo.customized_copy(data=fo.data()*math.sqrt(k)) fo = fo.customized_copy(sigmas=0.03*fo.data()) sigmas = fo.sigmas() for i in range(fo.size()): fo.data()[i] += 2 * scitbx.random.variate( scitbx.random.normal_distribution(sigma=sigmas[i]))() \ + 0.5*random.random() fo2 = fo.as_intensity_array() fc2 = fc.as_intensity_array() w = least_squares.mainstream_shelx_weighting(a=0.1) s = calc_goof(fo2, fc, w, k, xs.n_parameters()) w2 = w.optimise_parameters(fo2, fc2, k, xs.n_parameters()) s2 = calc_goof(fo2, fc, w2, k, xs.n_parameters()) # sort data and setup binning by fc/fc_max fc_sq = fc.as_intensity_array() fc_sq_over_fc_sq_max = fc_sq.data()/flex.max(fc_sq.data()) permutation = flex.sort_permutation(fc_sq_over_fc_sq_max) fc_sq_over_fc_sq_max = fc_sq.customized_copy( data=fc_sq_over_fc_sq_max).select(permutation) fc_sq = fc_sq.select(permutation) fo_sq = fo2.select(permutation) n_bins = 10 bin_max = 0 bin_limits = flex.size_t(1, 0) bin_count = flex.size_t() for i in range(n_bins): bin_limits.append(int(math.ceil((i+1) * fc_sq.size()/n_bins))) bin_count.append(bin_limits[i+1] - bin_limits[i]) goofs_w = flex.double() goofs_w2 = flex.double() for i_bin in range(n_bins): sel = flex.size_t_range(bin_limits[i_bin], bin_limits[i_bin+1]) goofs_w2.append(calc_goof(fo_sq.select(sel), fc_sq.select(sel), w2, k, xs.n_parameters())) goofs_w.append(calc_goof(fo_sq.select(sel), fc_sq.select(sel), w, k, xs.n_parameters())) a = flex.mean_and_variance(goofs_w).unweighted_sample_variance() b = flex.mean_and_variance(goofs_w2).unweighted_sample_variance() assert a > b or abs(1-s) > abs(1-s2) assert a > b # flat analysis of variance assert abs(1-s) > abs(1-s2) # GooF close to 1
def exercise( space_group_info, use_u_aniso, anomalous_flag, max_n_indices=5, verbose=0): if (not verbose): out = StringIO() else: out = sys.stdout for n_scatterers in xrange(3,3+1): for i_trial in xrange(1): xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=["const"]*n_scatterers, volume_per_atom=100, general_positions_only=True, random_f_prime_d_min=1, random_f_double_prime=anomalous_flag, use_u_aniso = use_u_aniso, use_u_iso = (not use_u_aniso), random_u_iso=True, random_u_iso_scale=0.3, random_occupancy=True) xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max(1, min(xray_structure.unit_cell().parameters()[:3])/2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors( xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) compare_analytical_and_finite( f_obs=f_obs, xray_structure=xray_structure, out=out) compare_analytical_and_finite( f_obs=f_obs.customized_copy( data=f_obs.data()*(flex.random_double(size=f_obs.size())+0.5)), xray_structure=xray_structure, out=out)
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C']*6+['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set( crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/2 ) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def exercise_core(n=10, verbose=0): from libtbx.test_utils import approx_equal import random # make two random sets of sites please c = euler_angles_as_matrix([random.uniform(0, 360) for i in xrange(3)]) set_1 = flex.vec3_double(flex.random_double(n * 3) * 10 - 2) set_2 = flex.vec3_double(flex.random_double(n * 3) * 10 - 2) set_3 = tuple(c) * set_2 set_a = set_1.concatenate(set_2) set_b = set_1.concatenate(set_3) tmp = find_domain(set_a, set_b, initial_rms=0.02, match_radius=0.03, minimum_size=1) if (verbose): tmp.show() assert len(tmp.matches) == 2 assert approx_equal(tmp.matches[0][3], 0, eps=1e-5) assert approx_equal(tmp.matches[0][4], n, eps=1e-5)
def run(): crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) f_calc = miller_set.array( data=flex.polar( flex.random_double(miller_set.size())*10-5, flex.random_double(miller_set.size())*10-5)) scale_factor = flex.random_double() obs = miller_set.array( data=scale_factor * flex.norm(f_calc.data()) + (flex.random_double(miller_set.size())*2-1), sigmas=flex.random_double(miller_set.size())) obs.set_observation_type_xray_intensity() exercise_shelx_weighting(f_calc, obs, scale_factor) exercise_quasi_unit_weighting(obs) print 'OK'
def exercise(space_group_info, use_u_aniso, anomalous_flag, max_n_indices=5, verbose=0): if (not verbose): out = StringIO() else: out = sys.stdout for n_scatterers in xrange(3, 3 + 1): for i_trial in xrange(1): xray_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=["const"] * n_scatterers, volume_per_atom=100, general_positions_only=True, random_f_prime_d_min=1, random_f_double_prime=anomalous_flag, use_u_aniso=use_u_aniso, use_u_iso=(not use_u_aniso), random_u_iso=True, random_u_iso_scale=0.3, random_occupancy=True) xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max( 1, min(xray_structure.unit_cell().parameters()[:3]) / 2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors(xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) compare_analytical_and_finite(f_obs=f_obs, xray_structure=xray_structure, out=out) compare_analytical_and_finite(f_obs=f_obs.customized_copy( data=f_obs.data() * (flex.random_double(size=f_obs.size()) + 0.5)), xray_structure=xray_structure, out=out)
def __init__(selfOO, wide_search_offset=None): selfOO.starting_simplex=[] selfOO.n = 2 selfOO.wide_search_offset = wide_search_offset for ii in range(selfOO.n+1): selfOO.starting_simplex.append(flex.random_double(selfOO.n)) selfOO.optimizer = simplex_opt(dimension=selfOO.n, matrix=selfOO.starting_simplex, evaluator=selfOO, tolerance=1e-7) selfOO.x = selfOO.optimizer.get_solution() selfOO.offset = selfOO.x[0]*0.2*beamr1 + selfOO.x[1]*0.2*beamr2 if selfOO.wide_search_offset is not None: selfOO.offset += selfOO.wide_search_offset
def exercise_factor_u_star_u_iso(): for i_trial in xrange(100): a = flex.random_double(size=9, factor=3) a.resize(flex.grid(3, 3)) u = a.matrix_transpose().matrix_multiply(a) # always positive-definite u_cart = [u[0], u[4], u[8], u[1], u[2], u[5]] unit_cell = uctbx.unit_cell((3, 5, 7, 80, 100, 110)) u_star = adptbx.u_cart_as_u_star(unit_cell, u_cart) airlie = u_star_minus_u_iso_airlie(unit_cell, u_star) ralf = u_star_minus_u_iso_ralf(unit_cell, u_star) assert approx_equal(ralf, airlie, 1.0e-10) f = adptbx.factor_u_star_u_iso(unit_cell=unit_cell, u_star=u_star) assert approx_equal(f.u_iso, adptbx.u_cart_as_u_iso(u_cart)) assert approx_equal(f.u_star_minus_u_iso, airlie, 1.0e-10)
def exercise_factor_u_star_u_iso(): for i_trial in xrange(100): a = flex.random_double(size=9, factor=3) a.resize(flex.grid(3,3)) u = a.matrix_transpose().matrix_multiply(a) # always positive-definite u_cart = [u[0],u[4],u[8],u[1],u[2],u[5]] unit_cell = uctbx.unit_cell((3,5,7,80,100,110)) u_star = adptbx.u_cart_as_u_star(unit_cell, u_cart) airlie = u_star_minus_u_iso_airlie(unit_cell, u_star) ralf = u_star_minus_u_iso_ralf(unit_cell, u_star) assert approx_equal(ralf, airlie, 1.e-10) f = adptbx.factor_u_star_u_iso(unit_cell=unit_cell, u_star=u_star) assert approx_equal(f.u_iso, adptbx.u_cart_as_u_iso(u_cart)) assert approx_equal(f.u_star_minus_u_iso, airlie, 1.e-10)
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C'] * 6 + ['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set(crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1 / 2) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def exercise_automation_wrappers(): from iotbx.reflection_file_utils import process_raw_data, \ change_space_group, load_f_obs_and_r_free from cctbx import sgtbx from libtbx.test_utils import approx_equal mtz_file = "tmp_iotbx_reflection_file_utils.mtz" crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,90,95,90), space_group_symbol="P 2") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=True, d_min=1.5) n_obs = miller_set.indices().size() i_obs = miller_set.array( data=flex.random_double(size=n_obs)).set_observation_type_xray_intensity() i_obs = i_obs.customized_copy(sigmas=flex.sqrt(i_obs.data())) r_free_flags = miller_set.generate_r_free_flags() r_free_flags_partial = r_free_flags.select(flex.random_bool(n_obs, 0.9)) out = StringIO() processed = process_raw_data( obs=i_obs, r_free_flags=None, test_flag_value=None, log=out) assert ("""WARNING: R-free flags not supplied.""" in out.getvalue()) assert (processed.data_labels() == "F(+),SIGF(+),F(-),SIGF(-)") assert (processed.phase_labels() is None) assert (processed.flags_are_new()) out2 = StringIO() processed2 = process_raw_data( obs=i_obs, r_free_flags=r_free_flags_partial, test_flag_value=True, log=out2) assert ("""WARNING: R-free flags are incomplete""" in out2.getvalue()) assert (not processed2.flags_are_new()) assert (processed.n_obs() == processed2.n_obs()) processed.write_mtz_file(mtz_file, title="tst_iotbx", wavelength=0.9792) f_obs, r_free = load_f_obs_and_r_free(mtz_file) change_space_group(mtz_file, sgtbx.space_group_info("P21")) f_obs_new, r_free_new = load_f_obs_and_r_free(mtz_file) assert (f_obs_new.size() == f_obs.size() - 4) f_obs_new, r_free_new = load_f_obs_and_r_free(mtz_file, anomalous_flag=True) assert (str(f_obs_new.space_group_info()) == "P 1 21 1") assert (approx_equal(f_obs_new.info().wavelength, 0.9792))
def exercise_automation_wrappers () : from iotbx.reflection_file_utils import process_raw_data, \ change_space_group, load_f_obs_and_r_free from cctbx import sgtbx from libtbx.test_utils import approx_equal mtz_file = "tmp_iotbx_reflection_file_utils.mtz" crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,90,95,90), space_group_symbol="P 2") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=True, d_min=1.5) n_obs = miller_set.indices().size() i_obs = miller_set.array( data=flex.random_double(size=n_obs)).set_observation_type_xray_intensity() i_obs = i_obs.customized_copy(sigmas=flex.sqrt(i_obs.data())) r_free_flags = miller_set.generate_r_free_flags() r_free_flags_partial = r_free_flags.select(flex.random_bool(n_obs, 0.9)) out = StringIO() processed = process_raw_data( obs=i_obs, r_free_flags=None, test_flag_value=None, log=out) assert ("""WARNING: R-free flags not supplied.""" in out.getvalue()) assert (processed.data_labels() == "F(+),SIGF(+),F(-),SIGF(-)") assert (processed.phase_labels() is None) assert (processed.flags_are_new()) out2 = StringIO() processed2 = process_raw_data( obs=i_obs, r_free_flags=r_free_flags_partial, test_flag_value=True, log=out2) assert ("""WARNING: R-free flags are incomplete""" in out2.getvalue()) assert (not processed2.flags_are_new()) assert (processed.n_obs() == processed2.n_obs()) processed.write_mtz_file(mtz_file, title="tst_iotbx", wavelength=0.9792) f_obs, r_free = load_f_obs_and_r_free(mtz_file) change_space_group(mtz_file, sgtbx.space_group_info("P21")) f_obs_new, r_free_new = load_f_obs_and_r_free(mtz_file) assert (f_obs_new.size() == f_obs.size() - 4) f_obs_new, r_free_new = load_f_obs_and_r_free(mtz_file, anomalous_flag=True) assert (str(f_obs_new.space_group_info()) == "P 1 21 1") assert (approx_equal(f_obs_new.info().wavelength, 0.9792))
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout crystal_symmetry = crystal.symmetry( unit_cell=(8,9,10,83,97,113), space_group_symbol="P1") miller_set = miller.set( crystal_symmetry=crystal_symmetry, indices=flex.miller_index([(1,2,3), (2,3,4), (-1,3,-2)]), anomalous_flag=False) for n_scatterers in xrange(2,2+5): for i_trial in xrange(5): scatterers = flex.xray_scatterer() for i in xrange(n_scatterers): scatterers.append(xray.scatterer( site=[random.random() for i in xrange(3)], u=random.random()*0.1, occupancy=random.random(), scattering_type="const", fp=(random.random()-0.5)*2, fdp=(random.random()-0.5)*2)) xray_structure = xray.structure( crystal_symmetry=crystal_symmetry, scatterers=scatterers) sf = structure_factors( xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) compare_analytical_and_finite( f_obs=f_obs, xray_structure=xray_structure, out=out) compare_analytical_and_finite( f_obs=f_obs.customized_copy( data=f_obs.data()*(flex.random_double(size=f_obs.size())+0.5)), xray_structure=xray_structure, out=out) print "OK"
def exercise_hkl(): #--- HKL crystal_symmetry = crystal.symmetry(unit_cell=(10, 11, 12, 85, 95, 100), space_group_symbol="P 1") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_arrays = [ miller_set.array(data=flex.random_double(size=miller_set.indices( ).size())).set_observation_type_xray_amplitude() for i in [0, 1] ] mtz_dataset = input_arrays[0].as_mtz_dataset(column_root_label="F0") mtz_dataset.mtz_object().write("tmp1.mtz") hkl = any_file("tmp1.mtz") assert hkl.file_type == "hkl" #assert hkl.file_server assert hkl.file_server.miller_arrays[0].info().labels == ["F0"] os.remove("tmp1.mtz")
def structures_forward(self, xs, xs_forward, eta_norm): while True: direction = flex.random_double(xs.n_parameters()) direction /= direction.norm() eta = eta_norm * direction i = 0 for sc_forward, sc in izip(xs_forward.scatterers(), xs.scatterers()): eta_site = matrix.col(eta[i : i + 3]) eta_iso = eta[i + 3] eta_aniso = matrix.col(eta[i + 4 : i + 10]) eta_occ = eta[i + 10] i += 11 sc_forward.site = matrix.col(sc.site) + eta_site sc_forward.u_iso = sc.u_iso + eta_iso sc_forward.u_star = matrix.col(sc.u_star) + eta_aniso sc_forward.occupancy = sc.occupancy + eta_occ yield direction
def __init__(self, space_group_info, **kwds): libtbx.adopt_optional_init_args(self, kwds) self.space_group_info = space_group_info self.structure = random_structure.xray_structure( space_group_info, elements=self.elements, volume_per_atom=20., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10), ) self.structure.set_inelastic_form_factors(1.54, "sasaki") self.scale_factor = 0.05 + 10 * flex.random_double() fc = self.structure.structure_factors(anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo = fc.as_amplitude_array() fo.set_observation_type_xray_amplitude() if self.use_students_t_errors: nu = random.uniform(1, 10) normal_g = variate(normal_distribution()) gamma_g = variate(gamma_distribution(0.5 * nu, 2)) errors = normal_g(fc.size()) / flex.sqrt(2 * gamma_g(fc.size())) else: # use gaussian errors g = variate(normal_distribution()) errors = g(fc.size()) fo2 = fo.as_intensity_array() self.fo2 = fo2.customized_copy( data=(fo2.data() + errors) * self.scale_factor, sigmas=flex.double(fc.size(), 1), ) self.fc = fc xs_i = self.structure.inverse_hand() self.fc_i = xs_i.structure_factors(anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo2_twin = self.fc.customized_copy( data=self.fc.data() + self.fc_i.data()).as_intensity_array() self.fo2_twin = fo2_twin.customized_copy( data=(errors + fo2_twin.data()) * self.scale_factor, sigmas=self.fo2.sigmas())
def exercise(args): verbose = "--verbose" in args if (not verbose): out = StringIO() else: out = sys.stdout crystal_symmetry = crystal.symmetry(unit_cell=(8, 9, 10, 83, 97, 113), space_group_symbol="P1") miller_set = miller.set(crystal_symmetry=crystal_symmetry, indices=flex.miller_index([(1, 2, 3), (2, 3, 4), (-1, 3, -2)]), anomalous_flag=False) for n_scatterers in xrange(2, 2 + 5): for i_trial in xrange(5): scatterers = flex.xray_scatterer() for i in xrange(n_scatterers): scatterers.append( xray.scatterer(site=[random.random() for i in xrange(3)], u=random.random() * 0.1, occupancy=random.random(), scattering_type="const", fp=(random.random() - 0.5) * 2, fdp=(random.random() - 0.5) * 2)) xray_structure = xray.structure(crystal_symmetry=crystal_symmetry, scatterers=scatterers) sf = structure_factors(xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) compare_analytical_and_finite(f_obs=f_obs, xray_structure=xray_structure, out=out) compare_analytical_and_finite(f_obs=f_obs.customized_copy( data=f_obs.data() * (flex.random_double(size=f_obs.size()) + 0.5)), xray_structure=xray_structure, out=out) print "OK"
def exercise_hkl () : #--- HKL crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_arrays = [miller_set.array( data=flex.random_double(size=miller_set.indices().size())) .set_observation_type_xray_amplitude() for i in [0,1]] mtz_dataset = input_arrays[0].as_mtz_dataset(column_root_label="F0") mtz_dataset.mtz_object().write("tmp1.mtz") hkl = any_file("tmp1.mtz") assert hkl.file_type == "hkl" #assert hkl.file_server assert hkl.file_server.miller_arrays[0].info().labels == ["F0"] os.remove("tmp1.mtz")
def structures_forward(self, xs, xs_forward, eta_norm): while True: direction = flex.random_double(xs.n_parameters()) direction /= direction.norm() eta = eta_norm * direction i = 0 for sc_forward, sc in izip(xs_forward.scatterers(), xs.scatterers()): eta_site = matrix.col(eta[i:i + 3]) eta_iso = eta[i + 3] eta_aniso = matrix.col(eta[i + 4:i + 10]) eta_occ = eta[i + 10] i += 11 sc_forward.site = matrix.col(sc.site) + eta_site sc_forward.u_iso = sc.u_iso + eta_iso sc_forward.u_star = matrix.col(sc.u_star) + eta_aniso sc_forward.occupancy = sc.occupancy + eta_occ yield direction
def __init__(self, space_group_info, **kwds): libtbx.adopt_optional_init_args(self, kwds) self.space_group_info = space_group_info self.structure = random_structure.xray_structure( space_group_info, elements=self.elements, volume_per_atom=20., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10), ) self.structure.set_inelastic_form_factors(1.54, "sasaki") self.scale_factor = 0.05 + 10 * flex.random_double() fc = self.structure.structure_factors( anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo = fc.as_amplitude_array() fo.set_observation_type_xray_amplitude() if self.use_students_t_errors: nu = random.uniform(1, 10) normal_g = variate(normal_distribution()) gamma_g = variate(gamma_distribution(0.5*nu, 2)) errors = normal_g(fc.size())/flex.sqrt(2*gamma_g(fc.size())) else: # use gaussian errors g = variate(normal_distribution()) errors = g(fc.size()) fo2 = fo.as_intensity_array() self.fo2 = fo2.customized_copy( data=(fo2.data()+errors)*self.scale_factor, sigmas=flex.double(fc.size(), 1), ) self.fc = fc xs_i = self.structure.inverse_hand() self.fc_i = xs_i.structure_factors( anomalous_flag=True, d_min=self.d_min, algorithm="direct").f_calc() fo2_twin = self.fc.customized_copy( data=self.fc.data()+self.fc_i.data()).as_intensity_array() self.fo2_twin = fo2_twin.customized_copy( data=(errors + fo2_twin.data()) * self.scale_factor, sigmas=self.fo2.sigmas())
def exercise_basic(verbose): assert abs(constants.boltzmann_constant_akma-0.001987) < 1e-6 assert abs(constants.akma_time_as_pico_seconds-0.04889) < 1e-5 t = mmtbx.dynamics.kinetic_energy_as_temperature(dof=5, e=1.3) assert approx_equal(t, 261.678053105) e = mmtbx.dynamics.temperature_as_kinetic_energy(dof=5, t=t) assert approx_equal(e, 1.3) # masses = flex.random_double(size=10) * 4 + 1 temps = flex.double() for i_pass in range(100): for i in range(100): velocities = cartesian_dynamics.random_velocities( masses=masses, target_temperature=300) kt = mmtbx.dynamics.kinetic_energy_and_temperature(velocities, masses) temps.append(kt.temperature) mmm = temps.min_max_mean() if (verbose): mmm.show() if (295 < mmm.mean < 305): break else: raise AssertionError("Failure reaching target_temperature.")
def exercise_basic(verbose): assert abs(constants.boltzmann_constant_akma - 0.001987) < 1e-6 assert abs(constants.akma_time_as_pico_seconds - 0.04889) < 1e-5 t = mmtbx.dynamics.kinetic_energy_as_temperature(dof=5, e=1.3) assert approx_equal(t, 261.678053105) e = mmtbx.dynamics.temperature_as_kinetic_energy(dof=5, t=t) assert approx_equal(e, 1.3) # masses = flex.random_double(size=10) * 4 + 1 temps = flex.double() for i_pass in xrange(100): for i in xrange(100): velocities = cartesian_dynamics.random_velocities(masses=masses, target_temperature=300) kt = mmtbx.dynamics.kinetic_energy_and_temperature(velocities, masses) temps.append(kt.temperature) mmm = temps.min_max_mean() if verbose: mmm.show() if 295 < mmm.mean < 305: break else: raise AssertionError("Failure reaching target_temperature.")
def exercise( xray_structure, anomalous_flag, max_n_indices, out): xray_structure.show_summary(f=out).show_scatterers(f=out) miller_set = miller.build_set( crystal_symmetry=xray_structure, anomalous_flag=anomalous_flag, d_min=max(1, min(xray_structure.unit_cell().parameters()[:3])/2.5)) n_indices = miller_set.indices().size() if (n_indices > max_n_indices): miller_set = miller_set.select( flex.random_size_t(size=max_n_indices) % n_indices) sf = structure_factors( xray_structure=xray_structure, miller_set=miller_set) f_calc = miller_set.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm="direct", cos_sin_table=False).f_calc() f_calc.show_summary(f=out) assert approx_equal(sf.fs(), f_calc.data()) f_obs = miller_set.array(data=flex.abs(sf.fs())) noise_fin = compare_analytical_and_finite( f_obs=f_obs, xray_structure=xray_structure, gradients_should_be_zero=True, eps=1.e-5, out=out) compare_analytical_and_finite( f_obs=f_obs.customized_copy( data=f_obs.data()*(flex.random_double(size=f_obs.size())+0.5)), xray_structure=xray_structure, gradients_should_be_zero=False, eps=max(1.e-5, noise_fin), out=out)
def testing_function_for_rsfit(basic_map, delta_h, xray_structure, out): for i_trial in xrange(100): sites_cart = flex.vec3_double((flex.random_double(size=3) - 0.5) * 1) tmp_sites_cart = sites_cart.deep_copy() for i in xrange(3): ref = lbfgs(basic_map=basic_map, sites_cart=tmp_sites_cart, delta_h=delta_h) temp = flex.double(ref.sites_cart[0]) - flex.double((0, 0, 0)) temp = math.sqrt(temp.dot(temp)) if temp <= 2 * delta_h: break print >> out, "recycling:", ref.sites_cart[0] tmp_sites_cart = ref.sites_cart for site, sitec in zip(ref.sites_cart, xray_structure.sites_cart()): print >> out, i_trial print >> out, sitec print >> out, sites_cart[0] print >> out, site temp = flex.double(site) - flex.double(sitec) temp = math.sqrt(temp.dot(temp)) print >> out, temp, delta_h assert temp <= delta_h * 2 print >> out
def recycle(): for n,first,last in [[(5,3,4),(0,0,0),(3,5,6)], [(4,3,5),(-1,-3,4),(6,4,5)], [(3,4,5),(-2,3,0),(-2,3,0)], [(3,4,5),(-2,3,0),(-2,3,3)], [(3,4,5),(-2,3,0),(-2,8,0)], [(3,4,5),(-2,3,0),(-2,9,0)], [(3,4,5),(-2,3,0),(3,3,0)], [(3,4,5),(-2,3,0),(4,3,0)]]: gridding = iotbx.xplor.map.gridding( n=n, first=first, last=last) flex_grid = gridding.as_flex_grid() data = 20000*flex.random_double(size=flex_grid.size_1d())-10000 data.resize(flex_grid) stats = maptbx.statistics(data) iotbx.xplor.map.writer( file_name="tmp.map", title_lines=["regression test"], unit_cell=uctbx.unit_cell((10,20,30,80,90,100)), gridding=gridding, data=data, average=stats.mean(), standard_deviation=stats.sigma()) read = iotbx.xplor.map.reader(file_name="tmp.map") assert read.title_lines == ["regression test"] assert read.gridding.n == n assert read.gridding.first == first assert read.gridding.last == last assert read.unit_cell.is_similar_to( uctbx.unit_cell((10,20,30,80,90,100))) assert eps_eq(read.average, stats.mean(), eps=1.e-4) assert eps_eq(read.standard_deviation, stats.sigma(), eps=1.e-4) assert read.data.origin() == first assert read.data.last(False) == last assert read.data.focus() == data.focus() assert eps_eq(read.data, data, eps=1.e-4)
def run(args): assert args in [[], ["--verbose"]] verbose = "--verbose" in args exercise_least_squares_residual() exercise_core_LS(xray.targets_least_squares_residual, verbose) exercise_core_LS(xray.targets_least_squares_residual_for_intensity, verbose) crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) f_calc = miller_set.array( data=flex.polar( flex.random_double(miller_set.size())*10-5, flex.random_double(miller_set.size())*10-5)) obs = miller_set.array( data=flex.abs(f_calc.data()) + (flex.random_double(miller_set.size())*2-1), sigmas=flex.random_double(miller_set.size())) obs.set_observation_type_xray_amplitude() weighting = xray.weighting_schemes.amplitude_unit_weighting() exercise_py_LS(obs, f_calc, weighting, verbose) obs = miller_set.array( data=flex.norm(f_calc.data()) + (flex.random_double(miller_set.size())*2-1), sigmas=flex.random_double(miller_set.size())) obs.set_observation_type_xray_intensity() weighting = xray.weighting_schemes.intensity_quasi_unit_weighting() exercise_py_LS(obs, f_calc, weighting, verbose) weighting = xray.weighting_schemes.simple_shelx_weighting(a=100, b=150) exercise_py_LS(obs, f_calc, weighting, verbose) weighting = xray.weighting_schemes.shelx_weighting(a=100, b=150) exercise_py_LS(obs, f_calc, weighting, verbose) print "OK"
def exercise(space_group_info, fixed_random_seed=True, shifted_origin=None, elements=None, d_min=0.8, grid_resolution_factor=1/3., verbose=False, **kwds): if elements is None: n_C = 5 n_O = 1 n_N = 1 elements = ["C"]*n_C + ["O"]*n_O + ["N"]*n_N if verbose: print elements target_space_group_type = space_group_info.type() hall = sgtbx.space_group_symbols( target_space_group_type.lookup_symbol()).hall() print hall if fixed_random_seed: random.seed(1) flex.set_random_seed(1) # Generate a random structure in real space, # compute its structure factors, # that we will try to recover the symmetry of target_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=elements, use_u_iso=True, random_u_iso=True, random_u_iso_scale=0.04, use_u_aniso=False, ) if shifted_origin is None: shifted_origin = flex.random_double(3) shifted_origin = mat.col(shifted_origin) if verbose: print "new origin = (%.3f, %.3f, %.3f)" % shifted_origin.elems print target_structure_in_p1 = target_structure\ .expand_to_p1().apply_shift(shifted_origin) target_f_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=False, d_min=d_min ).structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() # Recover space group? sf_symm = symmetry_search.structure_factor_symmetry(target_f_in_p1) if verbose: print sf_symm solution_hall, target_hall = [ sgi.as_reference_setting().type().hall_symbol() for sgi in (sf_symm.space_group_info, target_structure.space_group_info()) ] assert solution_hall == target_hall, (solution_hall, target_hall) # Shift maximises goodness of symmetry? gos, solution_f = sf_symm.symmetrised_structure_factors() if space_group_info.type().hall_symbol() != ' P 1': assert gos.correlation > 0.99 assert gos.gradient == (0, 0, 0) gos_away_from_max = sf_symm.symmetrised_structure_factors( delta=mat.col((0.1, 0.1, 0.1)))[0] assert gos_away_from_max.correlation < 0.9, gos_away_from_max.correlation # Recovered origin """The sequence of transform read: ----->target structure ^ V ^ V (1, shifted_origin=sigma) ^ V ^ shifted target ^ V ^ V sf_symm.cb_op_to_primitive = (P, 0) ^ V ^ shifted target in primitive cell = shifted solution in primitive cell ^ V ^ V (1, -sf_symm.origin=-s) ^ V ^ solution in primitive cell ^ V ^ V solution_to_target_cb_op = (Q, q) ^ V ^------------ The total transfrom from the target structure back to it reads (QP, q') with q' = (Q,q)(-s + P sigma) = (Q,q)delta_o with delta_o = sf_symm.cb_op_to_primitive(shifted_origin) - sf_symm.origin (Q, q') must leave the target structure space group invariant. Most of the time Q is the unit matrix and the test boils down to check whether delta is an allowed origin shift after changing to the input cell but it does not hurt to do the more general test all the time. """ solution_to_target_cb_op = ( target_structure.space_group_info() .change_of_basis_op_to_reference_setting().inverse() * sf_symm.space_group_info .change_of_basis_op_to_reference_setting()) if verbose: print print "solution -> target: %s" % solution_to_target_cb_op.as_xyz() delta_o = (mat.col(sf_symm.cb_op_to_primitive(shifted_origin)) - sf_symm.origin) delta = mat.col(solution_to_target_cb_op(delta_o)) stabilising_cb_op = sgtbx.change_of_basis_op(sgtbx.rt_mx( (solution_to_target_cb_op*sf_symm.cb_op_to_primitive).c().r(), sgtbx.tr_vec((delta*72).as_int()*2, tr_den=144))) # guarding against rounding errors on some platforms (e.g. FC8) # meaning that (delta*144).as_int() would not work. target_sg = target_structure.space_group() assert target_sg == target_sg.change_basis(stabilising_cb_op)