def exercise_flood_fill(): uc = uctbx.unit_cell('10 10 10 90 90 90') for uc in (uctbx.unit_cell('10 10 10 90 90 90'), uctbx.unit_cell('9 10 11 87 91 95')): gridding = maptbx.crystal_gridding( unit_cell=uc, pre_determined_n_real=(5,5,5)) corner_cube = (0,4,20,24,100,104,120,124) # cube across all 8 corners channel = (12,37,38,39,42,43,62,63,67,68,87,112) data = flex.int(flex.grid(gridding.n_real())) for i in (corner_cube + channel): data[i] = 1 flood_fill = masks.flood_fill(data, uc) assert data.count(0) == 105 for i in corner_cube: assert data[i] == 2 for i in channel: assert data[i] == 3 assert approx_equal(flood_fill.centres_of_mass(), ((-0.5, -0.5, -0.5), (-2.5, 7/3, 2.5))) assert approx_equal(flood_fill.centres_of_mass_frac(), ((-0.1, -0.1, -0.1), (-0.5, 7/15, 0.5))) assert approx_equal(flood_fill.centres_of_mass_cart(), uc.orthogonalize(flood_fill.centres_of_mass_frac())) assert flood_fill.n_voids() == 2 assert approx_equal(flood_fill.grid_points_per_void(), (8, 12)) if 0: from crys3d import wx_map_viewer wx_map_viewer.display(raw_map=data.as_double(), unit_cell=uc, wires=False) # gridding = maptbx.crystal_gridding( unit_cell=uc, pre_determined_n_real=(10,10,10)) data = flex.int(flex.grid(gridding.n_real())) # parallelogram points = [(2,4,5),(3,4,5),(4,4,5),(5,4,5),(6,4,5), (3,5,5),(4,5,5),(5,5,5),(6,5,5),(7,5,5), (4,6,5),(5,6,5),(6,6,5),(7,6,5),(8,6,5)] points_frac = flex.vec3_double() for p in points: data[p] = 1 points_frac.append([p[i]/gridding.n_real()[i] for i in range(3)]) points_cart = uc.orthogonalize(points_frac) flood_fill = masks.flood_fill(data, uc) assert data.count(2) == 15 assert approx_equal(flood_fill.centres_of_mass_frac(), ((0.5,0.5,0.5),)) pai_cart = math.principal_axes_of_inertia( points=points_cart, weights=flex.double(points_cart.size(),1.0)) F = matrix.sqr(uc.fractionalization_matrix()) O = matrix.sqr(uc.orthogonalization_matrix()) assert approx_equal( pai_cart.center_of_mass(), flood_fill.centres_of_mass_cart()[0]) assert approx_equal( flood_fill.covariance_matrices_cart()[0], (F.transpose() * matrix.sym( sym_mat3=flood_fill.covariance_matrices_frac()[0]) * F).as_sym_mat3()) assert approx_equal( pai_cart.inertia_tensor(), flood_fill.inertia_tensors_cart()[0]) assert approx_equal(pai_cart.eigensystem().vectors(), flood_fill.eigensystems_cart()[0].vectors()) assert approx_equal(pai_cart.eigensystem().values(), flood_fill.eigensystems_cart()[0].values()) return
def exercise_compare(): u1 = uctbx.unit_cell((3,2,5,90,100,90)) u2 = uctbx.unit_cell((2,3,5,90,80,90)) assert u1.compare_orthorhombic(other=u1) == 0 assert u2.compare_orthorhombic(other=u2) == 0 assert u1.compare_orthorhombic(other=u2) == 1 assert u2.compare_orthorhombic(other=u1) == -1 assert u1.compare_monoclinic( other=u1, unique_axis=1, angular_tolerance=3) == 0 assert u2.compare_monoclinic( other=u2, unique_axis=1, angular_tolerance=3) == 0 assert u1.compare_monoclinic( other=u2, unique_axis=1, angular_tolerance=3) == -1 assert u2.compare_monoclinic( other=u1, unique_axis=1, angular_tolerance=3) == 1 # u = uctbx.unit_cell((31.8764, 6.35, 22.54, 90, 135, 90)) c = u.change_of_basis_op_for_best_monoclinic_beta() assert str(c) == "a+c,b,c" assert c.c().r().den() == 12 assert c.c().t().den() == 144 u = uctbx.unit_cell((6.35, 31.8764, 16.2514, 90, 101.266, 90)) c = u.change_of_basis_op_for_best_monoclinic_beta() assert str(c) == "a,b,c" assert c.c().r().den() == 12 assert c.c().t().den() == 144
def any_compatible_unit_cell(self, volume=None, asu_volume=None): """ Generate a unit cell of arbitrary dimensions (optionally filling the desired volume) with parameters compatible with the specified space group. :param volume: desired unit cell volume :param asu_volume: desired volume of the asymmetric unit :returns: uctbx.unit_cell object """ assert [volume, asu_volume].count(None) == 1 if (volume is None): volume = asu_volume * self.group().order_z() sg_number = self.type().number() if (sg_number < 3): params = (1., 1.3, 1.7, 83, 109, 129) elif (sg_number < 16): params = (1., 1.3, 1.7, 90, 109, 90) elif (sg_number < 75): params = (1., 1.3, 1.7, 90, 90, 90) elif (sg_number < 143): params = (1., 1., 1.7, 90, 90, 90) elif (sg_number < 195): params = (1., 1., 1.7, 90, 90, 120) else: params = (1., 1., 1., 90, 90, 90) unit_cell = uctbx.unit_cell(params).change_basis( cb_op=self.change_of_basis_op_to_reference_setting().inverse()) f = (volume / unit_cell.volume())**(1/3.) params = list(unit_cell.parameters()) for i in xrange(3): params[i] *= f return uctbx.unit_cell(params)
def tst_compare(): uc1 = uctbx.unit_cell("61.28,95.92,145.02,90,90,90") xs1 = crystal.symmetry(uc1, "P212121") uc2 = uctbx.unit_cell("115.5,149.0,115.60,90,115.3,90") xs2 = crystal.symmetry(uc2, "P1211") out = StringIO() compare_object = compare_lattice(xs1, xs2, order=1, out=out) assert len(compare_object.possible_solutions) == 1
def exercise () : m = iotbx.symmetry.manager(prefer_pdb_space_group=True) (uc_mismatch, sg_mismatch) = m.add_reflections_file( file_name="data.mtz", space_group=sgtbx.space_group_info("P222"), unit_cell=uctbx.unit_cell("50 60 70 90 90 90")) assert (m.get_current_as_strings() == ('P 2 2 2', '50 60 70 90 90 90')) (uc_mismatch, sg_mismatch) = m.add_pdb_file( file_name="model.pdb", space_group=sgtbx.space_group_info("P212121"), unit_cell=uctbx.unit_cell("50 60 70 90 90 90")) assert (not (uc_mismatch or sg_mismatch)) (uc_mismatch, sg_mismatch) = m.add_pdb_file( file_name="reference_model.pdb", space_group=sgtbx.space_group_info("P63"), unit_cell=uctbx.unit_cell("40 40 75 90 90 120")) assert ((uc_mismatch, sg_mismatch) == (True, True)) assert (m.get_current_as_strings() == ('P 21 21 21', '50 60 70 90 90 90')) (uc_mismatch, sg_mismatch) = m.add_reflections_file( file_name="data_neutron.mtz", space_group=sgtbx.space_group_info("P222"), unit_cell=uctbx.unit_cell("50.1 60 70.1 90 90 90")) assert (not (uc_mismatch or sg_mismatch)) (uc_mismatch, sg_mismatch) = m.add_reflections_file( file_name="data_rfree.hkl", space_group=None, unit_cell=None) assert (not (uc_mismatch or sg_mismatch)) assert (m.get_current_as_strings() == ('P 21 21 21', '50 60 70 90 90 90')) assert (m.check_cell_compatibility("phenix.refine")) symm_choices = m.get_symmetry_choices() assert (symm_choices.space_group_files == [('model.pdb', 'P 21 21 21'), ('reference_model.pdb', 'P 63'), ('data.mtz', 'P 2 2 2'), ('data_neutron.mtz', 'P 2 2 2')]) assert (symm_choices.unit_cell_files == [ ('model.pdb', '(50, 60, 70, 90, 90, 90)'), ('reference_model.pdb', '(40, 40, 75, 90, 90, 120)'), ('data.mtz', '(50, 60, 70, 90, 90, 90)'), ('data_neutron.mtz', '(50.1, 60, 70.1, 90, 90, 90)')]) m.set_current_as_strings("P63", "50 60 70 90 90 90") try : m.check_cell_compatibility( program_name="phenix.refine", raise_error_if_incomplete=True) except Sorry : pass else : raise Exception_expected out = StringIO() m.show(out=out) assert (out.getvalue() == """\ model.pdb: (50, 60, 70, 90, 90, 90) P 21 21 21 reference_model.pdb: (40, 40, 75, 90, 90, 120) P 63 data.mtz: (50, 60, 70, 90, 90, 90) P 2 2 2 data_neutron.mtz: (50.1, 60, 70.1, 90, 90, 90) P 2 2 2 data_rfree.hkl: None None """)
def exercise_writer () : from iotbx import file_reader from cctbx import uctbx, sgtbx from scitbx.array_family import flex file_name = libtbx.env.find_in_repositories( relative_path="phenix_regression/wizards/partial_refine_001_map_coeffs.mtz", test=os.path.isfile) if file_name is None : print "Can't find map coefficients file, skipping." return mtz_in = file_reader.any_file(file_name, force_type="hkl").file_object miller_arrays = mtz_in.as_miller_arrays() map_coeffs = miller_arrays[0] fft_map = map_coeffs.fft_map(resolution_factor=1/3.0) fft_map.apply_sigma_scaling() fft_map.as_ccp4_map(file_name="2mFo-DFc.map") m = iotbx.ccp4_map.map_reader(file_name="2mFo-DFc.map") real_map = fft_map.real_map_unpadded() mmm = flex.double(list(real_map)).min_max_mean() assert approx_equal(m.unit_cell_parameters, map_coeffs.unit_cell().parameters()) assert approx_equal(mmm.min, m.header_min) assert approx_equal(mmm.max, m.header_max) #assert approx_equal(mmm.mean, m.header_mean) # random small maps of different sizes for nxyz in flex.nested_loop((1,1,1),(4,4,4)): mt = flex.mersenne_twister(0) grid = flex.grid(nxyz) map = mt.random_double(size=grid.size_1d()) map.reshape(grid) real_map = fft_map.real_map_unpadded() iotbx.ccp4_map.write_ccp4_map( file_name="random.map", unit_cell=uctbx.unit_cell((1,1,1,90,90,90)), space_group=sgtbx.space_group_info("P1").group(), gridding_first=(0,0,0), gridding_last=tuple(fft_map.n_real()), map_data=real_map, labels=flex.std_string(["iotbx.ccp4_map.tst"])) m = iotbx.ccp4_map.map_reader(file_name="random.map") mmm = flex.double(list(real_map)).min_max_mean() assert approx_equal(m.unit_cell_parameters, (1,1,1,90,90,90)) assert approx_equal(mmm.min, m.header_min) assert approx_equal(mmm.max, m.header_max) # gridding_first = (0,0,0) gridding_last = tuple(fft_map.n_real()) map_box = maptbx.copy(map, gridding_first, gridding_last) map_box.reshape(flex.grid(map_box.all())) iotbx.ccp4_map.write_ccp4_map( file_name="random_box.map", unit_cell=uctbx.unit_cell((1,1,1,90,90,90)), space_group=sgtbx.space_group_info("P1").group(), map_data=map_box, labels=flex.std_string(["iotbx.ccp4_map.tst"]))
def exercise_extreme(): uc = uctbx.unit_cell(( 69.059014477286041, 48.674386086971339, 0.0048194797114296736, 89.995145576185806, 89.999840576946085, 99.484656090034875)) red = uc.niggli_reduction() assert red.as_unit_cell().is_similar_to(uctbx.unit_cell(( 0.00481948, 48.6744, 69.059, 80.5153, 89.9962, 89.9951))) uc = uctbx.unit_cell(( 80.816186392181365, 81.021289502648813, 140.6784408482614, 29.932540128999769, 89.92047105556459, 119.85301114570319)) uc.niggli_reduction(iteration_limit=10000)
def exercise_mean_square_displacement(options, n_trials): if options.fix_random_seeds: scitbx.random.set_random_seed(0) # check adptbx.mean_square_displacement_difference # against adp_restraints.rigid_bond_pair for i in xrange(n_trials): x1 = matrix.col(site_coord(3)) x2 = matrix.col(site_coord(3)) u1 = matrix.col(as_sym_mat3(symm_mat(u_eigenval(3)))) u2 = matrix.col(as_sym_mat3(symm_mat(u_eigenval(3)))) g = as_sym_mat3(symm_mat(g_eigenval(3))) uc = uctbx.unit_cell(metrical_matrix=g) hirshfeld = adptbx.mean_square_displacement(uc, x1-x2) rigid = adp_restraints.rigid_bond_pair(x1, x2, u1, u2, uc) del uc assert hirshfeld.well_defined h1 = hirshfeld(u1).value h2 = hirshfeld(u2).value assert approx_equal(abs(h1 - h2), rigid.delta_z(), eps=1e-12) # check gradients with finite difference finite_difference_computation = scitbx.math.finite_difference_computation() best_delta = finite_difference_computation.best_delta for i in xrange(n_trials): z = site_coord(3) dz = best_delta(z, site_coord(3)) z = matrix.col(z) dz = matrix.col(dz) u_eigen = u_eigenval(3) du_eigen = best_delta(u_eigen, direction(3)) u = matrix.col(as_sym_mat3(symm_mat(u_eigen))) du = matrix.col(as_sym_mat3(symm_mat(du_eigen))) g_eigen = g_eigenval(3) dg_eigen = best_delta(g_eigen, direction(3)) g = matrix.col(as_sym_mat3(symm_mat(g_eigen))) dg = matrix.col(as_sym_mat3(symm_mat(dg_eigen))) uc = uctbx.unit_cell(metrical_matrix=g) h = adptbx.mean_square_displacement(uc, z)(u) uc_p = uctbx.unit_cell(metrical_matrix=g+dg) h_p = adptbx.mean_square_displacement(uc_p, z+dz)(u+du).value uc_m = uctbx.unit_cell(metrical_matrix=g-dg) h_m = adptbx.mean_square_displacement(uc_m, z-dz)(u-du).value finite_diff = (h_p - h_m)/2 taylor_diff = ( matrix.col(h.grad_u).dot(du) + matrix.col(h.grad_z).dot(dz) + matrix.col(h.grad_g).dot(dg) ) assert approx_equal(taylor_diff, finite_diff, eps=5*finite_difference_computation.precision),\ (taylor_diff, finite_diff)
def exercise_fast_minimum_reduction(): mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90))) assert mr.iteration_limit() == 100 assert mr.multiplier_significant_change_test() == 16 assert mr.min_n_no_significant_change() == 2 mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90) assert mr.iteration_limit() == 90 assert mr.multiplier_significant_change_test() == 16 assert mr.min_n_no_significant_change() == 2 mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90,8) assert mr.iteration_limit() == 90 assert mr.multiplier_significant_change_test() == 8 assert mr.min_n_no_significant_change() == 2 mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90,8,4) assert mr.iteration_limit() == 90 assert mr.multiplier_significant_change_test() == 8 assert mr.min_n_no_significant_change() == 4 mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((2,3,5,80,90,100))) assert approx_equal(mr.as_gruber_matrix(),(4,9,25,-5.209445,0,-2.083778)) assert approx_equal(mr.as_niggli_matrix(),(4,9,25,-5.209445/2,0,-2.083778/2)) assert approx_equal(mr.as_sym_mat3(),(4,9,25,-2.083778/2,0,-5.209445/2)) assert mr.as_unit_cell().is_similar_to(uctbx.unit_cell((2,3,5,100,90,100))) assert approx_equal(mr.r_inv(), (-1,0,0,0,-1,0,0,0,1)) assert mr.n_iterations() == 1 assert not mr.termination_due_to_significant_change_test() assert mr.type() == 2 mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 8) assert mr.n_iterations() == 8 assert not mr.termination_due_to_significant_change_test() try: uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 2, 7) except RuntimeError, e: assert str(e) == "cctbx Error: Iteration limit exceeded."
def exercise_real_world_examples(): # SSZ-59, cell by Michael Treacy, infinite loop in GSAS rducll (Linux) uc = uctbx.unit_cell(( 12.7366, 29.2300, 5.0242, 94.6570, 100.8630, 99.7561)) nc = uc.niggli_cell() assert nc.is_similar_to(uctbx.unit_cell( (5.0242, 12.7366, 29.23, 99.7561, 94.657, 100.863))) # SSZ-59, Burton et al., Table 4 uc = uctbx.unit_cell(( 12.7806, 12.7366, 29.457, 103.42, 103.57, 22.71)) red = uc.niggli_reduction() assert red.as_unit_cell().is_similar_to(nc) assert red.r_inv().elems == (-1, 0, 1, 1, -1, 0, 0, 0, 1)
def finite_differences(unit_cell, eps=1e-6): grads = [] for i in range(6): params = list(unit_cell.parameters()) params[i] += eps uc = uctbx.unit_cell(parameters=params) qm = matrix.col(uc.metrical_matrix()) params[i] -= 2*eps uc = uctbx.unit_cell(parameters=params) qp = matrix.col(uc.metrical_matrix()) dq = (qm-qp)/(2*eps) grads.extend(list(dq)) grads = flex.double(grads) grads.resize(flex.grid((6,6))) return grads.matrix_transpose()
def exercise_gruber_1973_example(): start = ucgmx((4,136,76,-155,-31,44)) assert start.is_similar_to(uctbx.unit_cell( (2, 11.66, 8.718, 139+40/60., 152+45/60., 19+24/60.))) buerger = ucgmx((4,16,16,-16,-1,-3)) assert buerger.is_similar_to(uctbx.unit_cell( (2, 4, 4, 120, 93.5833, 100.807))) niggli = ucgmx((4,16,16,16,3,4)) assert niggli.is_similar_to(uctbx.unit_cell( (2, 4, 4, 60, 79.1931, 75.5225))) red = reduction_base.gruber_parameterization(start) assert not red.is_buerger_cell() assert approx_equal(red.as_gruber_matrix(), (4,136,76,-155,-31,44)) assert approx_equal(red.as_niggli_matrix(), (4,136,76,-155/2.,-31/2.,44/2.)) assert approx_equal(red.as_sym_mat3(), (4,136,76,44/2.,-31/2.,-155/2.)) assert red.as_unit_cell().is_similar_to(start) red = reduction_base.gruber_parameterization(buerger) assert red.is_buerger_cell() assert not red.is_niggli_cell() red = reduction_base.gruber_parameterization(niggli) assert red.is_niggli_cell() red = reduce(start) assert red.as_unit_cell().is_similar_to(niggli) assert red.r_inv().elems == (-1, 5, 9, 0, -1, -1, 0, 0, 1) assert red.n_iterations() == 29 red = reduce(buerger) assert red.as_unit_cell().is_similar_to(niggli) assert red.r_inv().elems == (-1, 0, 0, 0, 1, 1, 0, 1, 0) assert red.n_iterations() == 4 red = reduce(niggli) assert red.as_unit_cell().is_similar_to(niggli) assert red.r_inv().elems == (1, 0, 0, 0, 1, 0, 0, 0, 1) assert red.n_iterations() == 1 try: red = krivy_gruber_1976.reduction(buerger, iteration_limit=1) except krivy_gruber_1976.iteration_limit_exceeded: pass else: raise Exception_expected assert not start.is_buerger_cell() assert not start.is_niggli_cell() assert buerger.is_buerger_cell() assert not buerger.is_niggli_cell() assert niggli.is_buerger_cell() assert niggli.is_niggli_cell() red = start.niggli_reduction() assert red.n_iterations() == 29 assert start.niggli_cell().is_similar_to(niggli)
def compute_u(mosflm_a_matrix, unit_cell, wavelength): uc = uctbx.unit_cell(unit_cell) A = (1.0 / wavelength) * matrix.sqr(mosflm_a_matrix) B = matrix.sqr(uc.orthogonalization_matrix()).inverse() return A * B.inverse()
def exercise_12(): missing = check_external_dependencies(['scipy', 'sklearn', 'networkx']) if len(missing): print ("Skipping exercise_12: missing dependencies" + " %s" * len(missing)) %(tuple(missing)) return # test indexing from single image of i04_weak_data data_dir = os.path.join(dials_regression, "indexing_test_data", "i04_weak_data") pickle_path = os.path.join(data_dir, "first_image.pickle") sweep_path = os.path.join(data_dir, "datablock_orig.json") extra_args = ["indexing.method=fft3d", "known_symmetry.space_group=P4", "known_symmetry.unit_cell=57.8,57.8,150,90,90,90", "peak_search=clean", "cluster_analysis_search=True", "min_samples=15", "n_macro_cycles=4", "reciprocal_space_grid.d_min=4" ] expected_unit_cell = uctbx.unit_cell((57.8,57.8,150,90,90,90)) expected_rmsds = (0.06, 0.07, 0.003) expected_hall_symbol = ' P 4' result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell, expected_rmsds, expected_hall_symbol)
def exercise_problem_parameters(): problem_parameters = ( (13.892443989449804, 13.892443989449804, 14.7648230602334, 61.936000954634402, 61.936000954634402, 88.515487291567879), (10.0, 10.0, 20.0, 90.0, 45.0, 120.0), (10.0, 20.0, 30.0, 120.0, 60.0, 120.0), (10.816653826391969, 13.820274961085254, 13.820274961085254, 60.0, 66.962544368849834, 66.962544368849834), (10.148891565092219, 13.379088160259652, 13.379088160259652, 107.33461190548745, 107.94415159713115, 109.72759194290299), (19.798989873223331, 231.21851136965654, 14.352700094407323, 133.37207519042573, 92.016673840743408, 134.55815348093702), (10.392304845413264, 13.19090595827292, 13.19090595827292, 112.64730819498385, 104.36056979415913, 106.96527532101391), (16.046806535881213, 13.341664064126334, 197.64614845728718, 153.28759931491018, 114.05435960569044, 92.543256980798247), (10.488088481701515, 13.820274961085254, 13.820274961085254, 109.9226012907464, 104.00699650533103, 110.31922490992999), (10.04987562112089, 13.19090595827292, 13.19090595827292, 118.05419482122835, 97.404049814230376, 106.92070123011929), (10.04987562112089, 13.45362404707371, 13.45362404707371, 109.02416163919622, 105.88181549565937, 109.44017310001107), (11.357816691600547, 13.638181696985853, 13.638181696985855, 115.81608733396159, 104.29612977641231, 104.29612977641233), (11.832159566199232, 13.784048752090222, 13.784048752090222, 110.67521616123457, 104.95317005195066, 110.01926787579129)) for parameters in problem_parameters: reduce(uctbx.unit_cell(parameters))
def exercise_13(): # test on spots derived from imosflm tutorial data: # http://www.ccp4.ac.uk/courses/BCA2005/tutorials/dataproc-tutorial.html data_dir = os.path.join(dials_regression, "indexing_test_data", "imosflm_hg_mar") pickle_path = os.path.join(data_dir, "strong.pickle") sweep_path = os.path.join(data_dir, "datablock.json") unit_cell = uctbx.unit_cell((58.373, 58.373, 155.939, 90, 90, 120)) hall_symbol = '-R 3 2"' for uc, hall in ((unit_cell, hall_symbol), (None, hall_symbol)): extra_args = ["bin_size_fraction=0.25"] if uc is not None: extra_args.append("known_symmetry.unit_cell=\"%s %s %s %s %s %s\"" %unit_cell.parameters()) if hall is not None: extra_args.append("known_symmetry.space_group=\"Hall: %s\"" %hall.replace('"', '\\"')) expected_unit_cell = unit_cell if hall is not None: expected_hall_symbol = hall else: expected_hall_symbol = ' P 1' expected_rmsds = (0.08, 0.11, 0.004) result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell, expected_rmsds, expected_hall_symbol)
def exercise_8(): # synthetic trypsin multi-lattice dataset (4 lattices) data_dir = os.path.join(dials_regression, "indexing_test_data", "trypsin") pickle_path = os.path.join(data_dir, "P1_X6_1_2_3_4.pickle") sweep_path = os.path.join(data_dir, "datablock_P1_X6_1_2_3_4.json") extra_args = ["indexing.method=real_space_grid_search", "reflections_per_degree=10", "n_macro_cycles=5", "known_symmetry.unit_cell=54.3,58.3,66.5,90,90,90", "known_symmetry.space_group=P212121", "scan_range=0,10", "beam.fix=all", "detector.fix=all", "max_cell=70", ] expected_unit_cell = uctbx.unit_cell((54.3, 58.3, 66.5, 90, 90, 90)) expected_rmsds = (0.28, 0.30, 0.006) expected_hall_symbol = ' P 2ac 2ab' n_expected_lattices = 1 result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell, expected_rmsds, expected_hall_symbol, n_expected_lattices=n_expected_lattices, relative_length_tolerance=0.02, absolute_angle_tolerance=1)
def exercise_10(): # synthetic trypsin multi-lattice dataset (3 lattices) data_dir = os.path.join(dials_regression, "indexing_test_data", "trypsin") pickle_path = os.path.join(data_dir, "P1_X6_1_2_3.pickle") sweep_path = os.path.join(data_dir, "datablock_P1_X6_1_2_3.json") extra_args = ["indexing.method=real_space_grid_search", "d_min_start=3", "n_macro_cycles=3", "known_symmetry.unit_cell=54.3,58.3,66.5,90,90,90", "known_symmetry.space_group=P212121", "scan_range=0,10", "beam.fix=all", "detector.fix=all", "max_lattices=3", "index_assignment.method=local", "nearest_neighbours=50", ] expected_unit_cell = uctbx.unit_cell((54.3, 58.3, 66.5, 90, 90, 90)) expected_rmsds = (0.33, 0.40, 0.0022) expected_hall_symbol = ' P 2ac 2ab' n_expected_lattices = 3 result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell, expected_rmsds, expected_hall_symbol, n_expected_lattices=n_expected_lattices, relative_length_tolerance=0.02, absolute_angle_tolerance=1)
def exercise_3(): missing = check_external_dependencies(['scipy', 'sklearn', 'networkx']) if len(missing): print ("Skipping exercise_3: missing dependencies" + " %s" * len(missing)) %(tuple(missing)) return # thaumatin data_dir = os.path.join(dials_regression, "indexing_test_data", "i04_weak_data") pickle_path = os.path.join(data_dir, "full.pickle") sweep_path = os.path.join(data_dir, "datablock_orig.json") extra_args = ["cluster_analysis_search=True", "n_macro_cycles=3", "bin_size_fraction=0.25", "reciprocal_space_grid.d_min=4"] expected_unit_cell = uctbx.unit_cell( (58, 58, 150, 90, 90, 90)) expected_rmsds = (0.05, 0.041, 0.0004) # now enforce symmetry extra_args.append("known_symmetry.space_group=P4") expected_hall_symbol = ' P 4' result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell, expected_rmsds, expected_hall_symbol) a, b, c = result.crystal_model.get_real_space_vectors() assert approx_equal(a.length(), b.length()) assert c.length() > b.length() assert approx_equal(a.angle(b, deg=True), 90) assert approx_equal(b.angle(c, deg=True), 90) assert approx_equal(c.angle(a, deg=True), 90)
def miller_array_as_mtz_dataset(self, column_root_label, column_types=None, label_decorator=None, title=None, crystal_name="crystal", project_name="project", dataset_name="dataset", wavelength=0.0): if (title is None): title = str(self.info()) if (title is None): title = "cctbx.miller.array" unit_cell = self.unit_cell() if (unit_cell is None): unit_cell = uctbx.unit_cell((1,1,1,90,90,90)) space_group_info = self.space_group_info() if (space_group_info is None): space_group_info = sgtbx.space_group_info(symbol="P 1") mtz_object = object() \ .set_title(title=title) \ .set_space_group_info(space_group_info=space_group_info) mtz_object.set_hkl_base(unit_cell=unit_cell) return mtz_object.add_crystal( name=crystal_name, project_name=project_name, unit_cell=unit_cell).add_dataset( name=dataset_name, wavelength=wavelength).add_miller_array( miller_array=self, column_root_label=column_root_label, column_types=column_types, label_decorator=label_decorator)
def exercise_7(): missing = check_external_dependencies(['scipy', 'sklearn', 'networkx']) if len(missing): print ("Skipping exercise_7: missing dependencies" + " %s" * len(missing)) %(tuple(missing)) return # synthetic trypsin multi-lattice dataset (4 lattices) data_dir = os.path.join(dials_regression, "indexing_test_data", "trypsin") pickle_path = os.path.join(data_dir, "P1_X6_1_2_3_4.pickle") sweep_path = os.path.join(data_dir, "datablock_P1_X6_1_2_3_4.json") extra_args = ["cluster_analysis_search=True", "reflections_per_degree=10", "n_macro_cycles=2", "reciprocal_space_grid.d_min=4", "max_cell=70", "max_lattices=4" ] expected_unit_cell = uctbx.unit_cell( (54.3, 58.3, 66.5, 90, 90, 90)) expected_rmsds = (0.24, 0.23, 0.006) expected_hall_symbol = ' P 1' n_expected_lattices = 4 result = run_one_indexing(pickle_path, sweep_path, extra_args, expected_unit_cell, expected_rmsds, expected_hall_symbol, n_expected_lattices=n_expected_lattices, relative_length_tolerance=0.01, absolute_angle_tolerance=1)
def run(params): if params.datout is None: params.datout = os.path.basename(params.lstin)+".dat" xac_files = read_path_list(params.lstin) ofs_dat = open(params.datout, "w") ref_v6cell = None if params.ref_cell is not None: ref_v6cell = v6cell(uctbx.unit_cell(params.ref_cell).niggli_cell()) ofs_dat.write("# ref_cell= %s\n" % params.ref_cell) if params.n_residues is not None: ofs_dat.write("# n_residues= %d\n" % params.n_residues) ofs_dat.write("file ioversigma resnatsnr1 wilsonb abdist a b c al be ga\n") ret = easy_mp.pool_map(fixed_func=lambda x: calc_stats(x, params.stats, params.n_residues, ref_v6cell, params.min_peak, params.min_peak_percentile, params.correct_peak), args=xac_files, processes=params.nproc) for stat in ret: getornan = lambda x: stat.get(x, float("nan")) # get or nan ofs_dat.write("%s %.3f %.3f %.3f %.3e"%(stat["filename"], getornan("ioversigma"), getornan("resnatsnr1"), getornan("wilsonb"), getornan("abdist"))) ofs_dat.write(" %.3f %.3f %.3f %.2f %.2f %.2f\n" % stat["cell"]) ofs_dat.close()
def check_refine_uc_cr(work_params, image_mdls, unit_cell_perturbation_factor=2, crystal_rotation_perturbation_angle=10): from cctbx import uctbx from scitbx.array_family import flex from scitbx import matrix for i_img,im in enumerate(image_mdls.array): print "Image number:", i_img mt = flex.mersenne_twister(seed=work_params.noise.random_seed+i_img) unit_cell = uctbx.unit_cell([ v + unit_cell_perturbation_factor*(mt.random_double()-0.5) for v in im.unit_cell.parameters()]) crystal_rotation = matrix.sqr(im.crystal_rotation) \ * matrix.col(mt.random_double_point_on_sphere()) \ .axis_and_angle_as_r3_rotation_matrix( angle=crystal_rotation_perturbation_angle, deg=True) refined = refine_uc_cr.refinery( work_params=work_params, spots_xy0=im.spot_positions, miller_indices=image_mdls.miller_indices.select(im.miller_index_i_seqs), unit_cell=unit_cell, crystal_rotation_uq=crystal_rotation .r3_rotation_matrix_as_unit_quaternion()) refined.show_summary().show_distances() print
def tst_pgtools(): unit_cell = uctbx.unit_cell('40, 40, 60, 90.0, 90.0, 90.0') mi = flex.miller_index(((2,4,6), (2,4,8))) xs = crystal.symmetry(unit_cell, "P 1 2 1") ms = miller.set(xs, mi) # Go to the minimum cell, for safety cob_min_cell = ms.change_of_basis_op_to_minimum_cell() ms_new = ms.change_basis( cob_min_cell ) lattice_group = sgtbx.lattice_symmetry.group( ms_new.unit_cell(), max_delta=5.0) point_group_low = ms_new.space_group().build_derived_point_group() point_group_high = lattice_group.build_derived_point_group() pgtree = pt.point_group_graph(point_group_low,point_group_high) # find the possible routes from 'P 2' to 'P 4 2 2' atlas = pgtree.graph.find_all_paths( 'P 1 2 1', 'P 4 2 2') route_1 = ['P 1 2 1', 'P 4 2 2'] route_2 = ['P 1 2 1', 'P 2 2 2', 'P 4 2 2'] assert route_1 in atlas assert route_2 in atlas assert len(atlas)==2 # Now lets 'disqualify' point group 'P 2 2 2' pgtree.remove_point_group_and_its_super_groups_from_graph( str(sgtbx.space_group_info(16))) assert len(pgtree.graph.node_objects)==1 assert pgtree.graph.node_objects.has_key ( 'P 1 2 1' )
def make_new_xs(self, mat, cb_op, to_reference=True): # make new lattice new_basis = self.basis * mat.as_float() new_uc = uctbx.unit_cell(orthogonalization_matrix=new_basis) tmp_xs = crystal.symmetry( unit_cell=new_uc, space_group=sgtbx.lattice_symmetry.group(new_uc, self.max_delta), assert_is_compatible_unit_cell=False, ) extra_cb_op = tmp_xs.change_of_basis_op_to_reference_setting() self.extra_cb_op.append(extra_cb_op) # new_sg = None try: new_sg = sgtbx.space_group_info(group=self.basic_xs_n.space_group()).change_basis(cb_op) except Exception: pass if to_reference: tmp_xs = tmp_xs.change_basis(extra_cb_op) try: new_sg = new_sg.change_basis(extra_cb_op) except Exception: pass self.xs_list.append(tmp_xs) self.sg_list.append(new_sg)
def get_one_example(): return subgroup_comparator(symmetry = # symmetry from 3ged crystal.symmetry( unit_cell=uctbx.unit_cell((124.287,124.287,162.608,90.0,90.0,90.0)), space_group=sgtbx.space_group_info('I 41 2 2').group()) )
def rotate_around_origin(self, axis, angle, deg=True): ''' Rotate the model around an axis and angle :param axis: The axis to rotate around :param angle: The angle to rotate around :param deg: Degrees or radians ''' from scitbx import matrix # Compute the matrix R = matrix.col(axis).axis_and_angle_as_r3_rotation_matrix(angle, deg=deg) # Update U self._U = R * self._U # Update A at scan points if self._A_at_scan_points is not None: for i, At in enumerate(self._A_at_scan_points): uc = unit_cell(orthogonalization_matrix=At.transpose().inverse()) Bt = matrix.sqr(uc.fractionalization_matrix()).transpose() Ut = At * Bt.inverse() Ut = R * Ut At = Ut * Bt self._A_at_scan_points[i] = At
def set_params(self, observations=None, observations_original=None, refined_params=None, stats=None, partiality=None, rs_set=None, rh_set=None, frame_no=None, pickle_filename=None, wavelength=None, crystal_orientation=None, detector_distance_mm=None, identified_isoform=None, mapped_predictions=None, xbeam=None, ybeam=None): self.observations = observations self.observations_original = observations_original self.refined_params = refined_params self.partiality = partiality self.rs_set = rs_set self.rh_set = rh_set self.frame_no = frame_no self.pickle_filename = pickle_filename self.wavelength = wavelength self.detector_distance_mm = detector_distance_mm self.identified_isoform = identified_isoform self.mapped_predictions = mapped_predictions self.xbeam = xbeam self.ybeam = ybeam #refined_params #note params = G,B,rotx,roty,ry,a,b,c,alpha,beta,gamma self.G = refined_params[0] self.B = refined_params[1] self.rotx = refined_params[2] self.roty = refined_params[3] self.ry = refined_params[4] self.rz = refined_params[5] self.r0 = refined_params[6] self.re = refined_params[7] self.voigt_nu = refined_params[8] self.uc_params = flex.double([refined_params[9], refined_params[10], refined_params[11], refined_params[12], refined_params[13], refined_params[14]]) self.unit_cell = unit_cell((refined_params[9], refined_params[10], refined_params[11], refined_params[12], refined_params[13], refined_params[14])) self.crystal_orientation = crystal_orientation #SE, R_sq, CC_init, CC_final, R_init, R_final, R_xy_init, R_xy_final self.SE = stats[0] self.R_sq = stats[1] self.CC_init = stats[2] self.CC_final = stats[3] self.R_init = stats[4] self.R_final = stats[5] self.R_xy_init = stats[6] self.R_xy_final = stats[7] self.CC_iso_init = stats[8] self.CC_iso_final = stats[9]
def GetPhilValue (self) : self.Validate() val_str = str(wx.TextCtrl.GetValue(self)) if (val_str == "") : return self.ReturnNoneIfOptional() from cctbx import uctbx return uctbx.unit_cell(val_str)
def __init__(self, file_name, header_only=False): f = open(file_name, "r") f.readline() self.title_lines = [] ntitle = int(f.readline().strip().split("!")[0]) self.title_lines = [] for x in xrange(ntitle): line = f.readline().rstrip() self.title_lines.append(line) line = f.readline() values = [int(line[i : i + 8]) for i in xrange(0, 72, 8)] self.gridding = gridding( n=[values[i] for i in xrange(0, 9, 3)], first=[values[i] for i in xrange(1, 9, 3)], last=[values[i] for i in xrange(2, 9, 3)], ) line = f.readline() params = [float(line[i : i + 12]) for i in xrange(0, 72, 12)] self.unit_cell = uctbx.unit_cell(params) order = f.readline().strip() assert order == "ZYX" f.close() if header_only: self.data = None self.average = None self.standard_deviation = None else: ext_reader = ext.map_reader( file_name=file_name, n_header_lines=len(self.title_lines) + 5, grid=self.gridding.as_flex_grid() ) self.data = ext_reader.data self.average = ext_reader.average self.standard_deviation = ext_reader.standard_deviation
def exercise_frac_orth(): u = uctbx.unit_cell(()) assert approx_equal(u.fractionalization_matrix(), u.orthogonalization_matrix()) u = uctbx.unit_cell((2, 3, 5)) assert approx_equal(u.fractionalize((1, 2, 4)), (1 / 2., 2 / 3., 4 / 5.)) assert approx_equal(u.orthogonalize((1 / 2., 2 / 3., 4 / 5.)), (1, 2, 4)) assert approx_equal(u.fractionalize(flex.vec3_double([(1, 2, 4)])), [(1 / 2., 2 / 3., 4 / 5.)]) assert approx_equal( u.orthogonalize(flex.vec3_double([(1 / 2., 2 / 3., 4 / 5.)])), [(1, 2, 4)]) assert approx_equal( u.length((1 / 2., 2 / 3., 4 / 5.))**2, 1**2 + 2**2 + 4**2) assert approx_equal( u.distance((7 / 2., 8 / 3., 9 / 5.), (3, 2, 1))**2, 1**2 + 2**2 + 4**2) assert approx_equal(u.angle((0, 0, 0), (1 / 2., 0, 0), (1, 0, 0)), 180.) assert approx_equal( u.angle((0, 0, 0), (1 / 2., 0, 0), (1 / 2., 1 / 3., 0)), 90.) assert approx_equal( u.angle((1 / 2., 0, 0), (1 / 2., 1 / 3., 0), (0, 0, 0)), 45.) assert u.angle((0, 0, 0), (0, 0, 0), (1, 0, 0)) is None assert approx_equal( u.dihedral((0, 0, 0), (1 / 2., 0, 0), (1 / 2., 0, 1 / 5.), (1, 0, 1 / 5.)), 180.) assert approx_equal( u.dihedral((0, 0, 0), (1 / 2., 0, 0), (1 / 2., 0, 1 / 5.), (1 / 2., -1 / 3., 1 / 5.)), 90.) assert approx_equal( u.dihedral((0, 0, 0), (1 / 2., 0, 0), (1 / 2., 0, 1 / 5.), (1, -1 / 3., 1 / 5.)), 135.) assert approx_equal( u.dihedral((0, 0, 0), (1 / 2., 0, 0), (1 / 2., 0, 1 / 5.), (1, 1 / 3., 1 / 5.)), -135.) assert u.dihedral((0, 0, 0), (1 / 2., 0, 0), (1 / 2., 0, 0), (1, 0, 1 / 5.)) is None assert approx_equal(u.mod_short_length((1 / 4., 2 / 3., 4 / 5.)), u.length((1 / 4., -1 / 3., -1 / 5.))) assert approx_equal( u.mod_short_distance((13 / 4., 8 / 3., 9 / 5.), (3, 2, 1)), u.length((1 / 4., -1 / 3., -1 / 5.))) c = flex.vec3_double(((7 / 2., 8 / 3., 9 / 5.), (13 / 4., 8 / 3., 9 / 5.))) assert approx_equal( u.min_mod_short_distance(c, (3, 2, 1)), u.mod_short_distance((13 / 4., 8 / 3., 9 / 5.), (3, 2, 1))) # u = uctbx.unit_cell((13, 17, 19, 83, 111, 95)) fm = matrix.sqr(u.fractionalization_matrix()) assert ",".join(["%.3g" % e for e in fm.elems]) \ == "0.0769,0.00673,0.029,0,0.059,-0.00578,0,0,0.0566" om = matrix.sqr(u.orthogonalization_matrix()) assert ",".join(["%.3g" % e for e in om.elems]) \ == "13,-1.48,-6.81,0,16.9,1.73,0,0,17.7" gm = matrix.sqr(u.grid_index_as_site_cart_matrix(gridding=(11, 13, 17))) pg = matrix.col((5, -7, 23)) pf = matrix.col((5 / 11, -7 / 13, 23 / 17)) assert approx_equal(u.orthogonalize(pf), om * pf) assert approx_equal(gm * pg, om * pf) f = flex.vec3_double(flex.random_double(size=12) * 2 - 1) c = u.orthogonalize(sites_frac=f) assert approx_equal(u.fractionalize(sites_cart=c), f) for fi, ci in zip(f, c): assert approx_equal(u.orthogonalize(site_frac=fi), ci) assert approx_equal(u.fractionalize(site_cart=ci), fi) assert approx_equal(om * matrix.col(fi), ci) assert approx_equal(fm * matrix.col(ci), fi) # from cctbx import sgtbx s = sgtbx.rt_mx("-x,-x+y,-x+z", r_den=12) assert approx_equal(u.matrix_cart(rot_mx=s.r()), [ -0.3622586, -0.1191822, -0.5137527, -1.435689, 0.8743934, -0.5414459, -1.357969, -0.1188069, 0.4878651 ]) from scitbx.math import r3_rotation_axis_and_angle_from_matrix as from_matrix def check(u, sg): for s in sg: t = s.r().info().type() c = matrix.sqr(u.matrix_cart(rot_mx=s.r())) d = c.determinant() assert approx_equal(abs(d), 1) assert (t < 0) is (d < 0) fm = from_matrix(r=c * d) expected = { 1: [0], 2: [180, -180], 3: [120, -120], 4: [90, -90], 6: [60, -60] }[abs(t)] observed = round(fm.angle(deg=True)) if (observed not in expected): raise RuntimeError( "%s not in %s (%s)" % (str(observed), str(expected), s.r().as_xyz())) check( # primitive settig of space group No. 230 u=uctbx.unit_cell([10.911236359717213] * 3 + [109.47122063449069] * 3), sg=sgtbx.space_group("-I 4bd 2c 3 (y+z,x+z,x+y)")) check( # P 6/m m m u=uctbx.unit_cell((13, 13, 17, 90, 90, 120)), sg=sgtbx.space_group("-P 6 2"))
def prepare_data_microcycle( self, refine_mode, iparams, observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, I_r_flex, init_params, crystal_init_orientation, wavelength, detector_distance_mm, ): # prepare data if refine_mode == "crystal_orientation": pr_d_min = iparams.postref.crystal_orientation.d_min pr_d_max = iparams.postref.crystal_orientation.d_max pr_sigma_min = iparams.postref.crystal_orientation.sigma_min pr_partiality_min = iparams.postref.crystal_orientation.partiality_min pr_uc_tol = iparams.postref.unit_cell.uc_tolerance elif refine_mode == "reflecting_range": pr_d_min = iparams.postref.reflecting_range.d_min pr_d_max = iparams.postref.reflecting_range.d_max pr_sigma_min = iparams.postref.reflecting_range.sigma_min pr_partiality_min = iparams.postref.reflecting_range.partiality_min pr_uc_tol = iparams.postref.unit_cell.uc_tolerance elif refine_mode == "unit_cell": pr_d_min = iparams.postref.unit_cell.d_min pr_d_max = iparams.postref.unit_cell.d_max pr_sigma_min = iparams.postref.unit_cell.sigma_min pr_partiality_min = iparams.postref.unit_cell.partiality_min pr_uc_tol = iparams.postref.unit_cell.uc_tolerance elif refine_mode == "allparams": pr_d_min = iparams.postref.allparams.d_min pr_d_max = iparams.postref.allparams.d_max pr_sigma_min = iparams.postref.allparams.sigma_min pr_partiality_min = iparams.postref.allparams.partiality_min pr_uc_tol = iparams.postref.unit_cell.uc_tolerance # filter by resolution observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "resolution", [pr_d_min, pr_d_max], observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, I_r_flex, ) # filter by sigma observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "sigma", [pr_sigma_min], observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel, ) # extract refined parameters G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma = ( init_params) # filter by partiality two_theta = observations_original_sel.two_theta( wavelength=wavelength).data() uc = unit_cell((a, b, c, alpha, beta, gamma)) ph = partiality_handler() partiality_init, delta_xy_init, rs_init, dummy = ph.calc_partiality_anisotropy_set( uc, rotx, roty, observations_original_sel.indices(), ry, rz, r0, re, voigt_nu, two_theta, alpha_angle_sel, wavelength, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, iparams.partiality_model, iparams.flag_beam_divergence, ) observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "partiality", [pr_partiality_min], observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel, partiality_in=partiality_init, ) return ( observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel, )
from rovinj_numopt_tut.constraints import rotating_oh from cctbx import xray, uctbx pivot = xray.scatterer(site=(0, 0, 0), label="C") pivot_neighbour = xray.scatterer(site=(1, 1, 1), label="O") unit_cell = uctbx.unit_cell((1, 2, 3, 90, 90, 90)) constraint = rotating_oh(pivot, pivot_neighbour, 0, unit_cell) print constraint.hydrogen_site print constraint.hydrogen_site_derivative
final_cell = unit_cells[0] final_group = space_groups[0] from cctbx import crystal return crystal.symmetry(unit_cell=final_cell, space_group_info=final_group) if (__name__ == "__main__"): app = wx.App(0) dlg = SymmetryDialog(None, -1, "Enter symmetry") dlg.SetSpaceGroup("P21") if (dlg.ShowModal() == wx.ID_OK): symm = dlg.GetSymmetry() assert (symm.space_group_info() is not None) assert (symm.unit_cell() is not None) wx.CallAfter(dlg.Destroy) from cctbx import uctbx, sgtbx unit_cells = [ uctbx.unit_cell((10, 20, 30, 90, 90, 90)), uctbx.unit_cell((11, 21, 29, 90.05, 90.2, 89.8)), ] source_info = ["File 1", "File 2"] space_groups = [ sgtbx.space_group_info("P21"), sgtbx.space_group_info("P1") ] symm = get_unique_symmetry(space_groups=space_groups, unit_cells=unit_cells, sg_source_info=source_info, uc_source_info=source_info) symm.show_summary()
def set_A(self, A): uc = unit_cell(orthogonalization_matrix=A.transpose().inverse()) B = matrix.sqr(uc.fractionalization_matrix()).transpose() U = A * B.inverse() self.set_B(B) self.set_U(U)
def __init__(self, file_handle, header_only=False, allow_unmerged=True): "http://www.mpimf-heidelberg.mpg.de/~kabsch/xds/" f = iter(file_handle) flds = f.next().split() assert flds[0] == "!FORMAT=XDS_ASCII" assert (allow_unmerged) or (flds[1] == "MERGE=TRUE") self.unmerged_data = (flds[1] == "MERGE=FALSE") if (flds[2] == "FRIEDEL'S_LAW=FALSE"): self.anomalous_flag = True elif (flds[2] == "FRIEDEL'S_LAW=TRUE"): self.anomalous_flag = self.unmerged_data else: raise RuntimeError, "Expected FRIEDEL'S_LAW=FALSE|TRUE" self.unit_cell = None self.space_group_number = None self.number_of_items_in_each_data_record = None self.miller_index_columns = [None, None, None] self.iobs_column = None self.sigma_iobs_column = None self.zd_column = None self.wavelength = None for line in f: if (line.startswith("!SPACE_GROUP_NUMBER=")): self.space_group_number = int(get_rhs(line)) assert 1 <= self.space_group_number <= 230 elif (line.startswith("!UNIT_CELL_CONSTANTS=")): self.unit_cell = uctbx.unit_cell(get_rhs(line)) elif (line.startswith("!NUMBER_OF_ITEMS_IN_EACH_DATA_RECORD=")): self.number_of_items_in_each_data_record = int(get_rhs(line)) elif (line.startswith("!ITEM_H=")): self.miller_index_columns[0] = self.column_index(line) elif (line.startswith("!ITEM_K=")): self.miller_index_columns[1] = self.column_index(line) elif (line.startswith("!ITEM_L=")): self.miller_index_columns[2] = self.column_index(line) elif (line.startswith("!ITEM_IOBS=")): self.iobs_column = self.column_index(line) elif (line.startswith("!ITEM_SIGMA(IOBS)=")): self.sigma_iobs_column = self.column_index(line) elif (line.startswith("!ITEM_ZD=")): self.zd_column = self.column_index(line) elif (line.startswith("!X-RAY_WAVELENGTH=")): self.wavelength = float(get_rhs(line)) elif (line.startswith("!END_OF_HEADER")): break assert self.unit_cell is not None assert self.space_group_number is not None assert self.column_index is not None assert None not in self.miller_index_columns assert self.iobs_column is not None assert self.sigma_iobs_column is not None if (header_only): self.miller_indices = None self.iobs = None self.sigma_iobs = None self.zd = None else: self.miller_indices = flex.miller_index() self.iobs = flex.double() self.sigma_iobs = flex.double() self.zd = None if (self.zd_column is not None): self.zd = flex.double() for line in f: if (line.startswith("!END_OF_DATA")): break data = line.split() assert len(data) == self.number_of_items_in_each_data_record h = [int(data[i]) for i in self.miller_index_columns] self.miller_indices.append(h) self.iobs.append(float(data[self.iobs_column])) self.sigma_iobs.append(float(data[self.sigma_iobs_column])) if (self.zd_column is not None): self.zd.append(float(data[self.zd_column]))
if math.fabs(len_H1 - 1.0 / wavelength) > 0.0001: raise RuntimeError('length error for %d %d %d' % hkl) if rp(hkl, omegas[omegaidx]): x, y = rp.get_prediction() assert (0 < x < detector_size) assert (0 < y < detector_size) if __name__ == '__main__': wavelength = 1.2 resolution = 1.5 uc = unit_cell([5.01, 5.01, 5.47, 90.0, 90.0, 120.0]) bmat = matrix.sqr(uc.orthogonalization_matrix()).inverse().transpose() #---------------------------- # prove that the matrix bmat is consistent with the ewald_sphere class # requirements: # / A*x B*x C*x \ # Matrix A* = | A*y B*y C*y | # \ A*z B*z C*z / uc_reciprocal = uc.reciprocal() astar, bstar, cstar, alphastar, betastar, gammastar = uc_reciprocal.parameters( ) astar_vec = matrix.col([bmat[0], bmat[3], bmat[6]]) bstar_vec = matrix.col([bmat[1], bmat[4], bmat[7]])
def median_unit_cell(experiments): uc_params = [flex.double() for i in range(6)] for c in experiments.crystals(): for i, p in enumerate(c.get_unit_cell().parameters()): uc_params[i].append(p) return uctbx.unit_cell(parameters=[flex.median(p) for p in uc_params])
def optimize( self, I_r_flex, observations_original, wavelength, crystal_init_orientation, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, iparams, pres_in, observations_non_polar, detector_distance_mm, ): ph = partiality_handler() lph = lbfgs_partiality_handler() if iparams.postref.allparams.flag_on: refine_steps = ["allparams"] else: refine_steps = ["crystal_orientation"] if iparams.postref.reflecting_range.flag_on: refine_steps.append("reflecting_range") if iparams.postref.unit_cell.flag_on: refine_steps.append("unit_cell") # get miller array iso, if given. miller_array_iso = None # prepare data pr_d_min = iparams.postref.allparams.d_min pr_d_max = iparams.postref.allparams.d_max pr_sigma_min = iparams.postref.allparams.sigma_min pr_partiality_min = iparams.postref.allparams.partiality_min pr_uc_tol = iparams.postref.allparams.uc_tolerance cs = observations_original.crystal_symmetry().space_group( ).crystal_system() # filter by resolution observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "resolution", [pr_d_min, pr_d_max], observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, I_r_flex, ) # filter by sigma observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "sigma", [pr_sigma_min], observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel, ) # initialize values only in the first sub cycle and the first refine step. spot_radius = ph.calc_spot_radius( sqr(crystal_init_orientation.reciprocal_matrix()), observations_original_sel.indices(), wavelength, ) if pres_in is None: ry, rz, r0, re, voigt_nu, rotx, roty = ( 0, 0, spot_radius, iparams.gamma_e, iparams.voigt_nu, 0.0, 0.0, ) # apply constrain on the unit cell using crystal system uc_scale_inp = lph.prep_input( observations_original.unit_cell().parameters(), cs) uc_scale_constrained = lph.prep_output(uc_scale_inp, cs) a, b, c, alpha, beta, gamma = uc_scale_constrained const_params_scale = ( rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma, ) xopt_scalefactors, stats = self.optimize_scalefactors( I_r_flex, observations_original, wavelength, crystal_init_orientation, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, iparams, pres_in, observations_non_polar, detector_distance_mm, const_params_scale, ) G, B = xopt_scalefactors else: G, B, ry, rz, r0, re, voigt_nu, rotx, roty = ( pres_in.G, pres_in.B, pres_in.ry, pres_in.rz, pres_in.r0, pres_in.re, pres_in.voigt_nu, 0.0, 0.0, ) a, b, c, alpha, beta, gamma = pres_in.unit_cell.parameters() crystal_init_orientation = pres_in.crystal_orientation # filter by partiality two_theta = observations_original_sel.two_theta( wavelength=wavelength).data() uc = unit_cell((a, b, c, alpha, beta, gamma)) partiality_init, delta_xy_init, rs_init, dummy = ph.calc_partiality_anisotropy_set( uc, rotx, roty, observations_original_sel.indices(), ry, rz, r0, re, voigt_nu, two_theta, alpha_angle_sel, wavelength, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, iparams.partiality_model, iparams.flag_beam_divergence, ) observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "partiality", [pr_partiality_min], observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel, partiality_in=partiality_init, ) I_r_true = I_ref_sel[:] I_o_true = observations_original_sel.data()[:] # calculate initial residual_xy error const_params_uc = (G, B, rotx, roty, ry, rz, r0, re, voigt_nu) xinp_uc = lph.prep_input((a, b, c, alpha, beta, gamma), cs) args_uc = ( I_r_true, observations_original_sel, wavelength, alpha_angle_sel, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, "unit_cell", const_params_uc, B, miller_array_iso, iparams, ) uc_params_err = lph.func(xinp_uc, args_uc) init_residual_xy_err = flex.sum(uc_params_err**2) # calculate initial residual_pr error const_params_all = (G, B) xinp_all = flex.double([rotx, roty, ry, rz, r0, re, voigt_nu]) xinp_all.extend(lph.prep_input((a, b, c, alpha, beta, gamma), cs)) args_all = ( I_r_true, observations_original_sel, wavelength, alpha_angle_sel, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, "allparams", const_params_all, B, miller_array_iso, iparams, ) all_params_err = lph.func(xinp_all, args_all) init_residual_err = flex.sum(all_params_err**2) # keep in list t_pr_list = [init_residual_err] t_xy_list = [init_residual_xy_err] refined_params_hist = [(G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma)] txt_out = "" for i_sub_cycle in range(iparams.n_postref_sub_cycle): for j_refine_step in range(len(refine_steps)): refine_mode = refine_steps[j_refine_step] # prepare data init_params = ( G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma, ) observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.prepare_data_microcycle( refine_mode, iparams, observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, I_r_flex, init_params, crystal_init_orientation, wavelength, detector_distance_mm, ) I_r_true = I_ref_sel[:] I_o_true = observations_original_sel.data() if refine_mode == "crystal_orientation": xinp = flex.double([rotx, roty]) const_params = ( G, B, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma, ) elif refine_mode == "reflecting_range": xinp = flex.double([ry, rz, r0, re, voigt_nu]) const_params = (G, B, rotx, roty, a, b, c, alpha, beta, gamma) elif refine_mode == "unit_cell": xinp = lph.prep_input((a, b, c, alpha, beta, gamma), cs) const_params = (G, B, rotx, roty, ry, rz, r0, re, voigt_nu) elif refine_mode == "allparams": xinp = flex.double([rotx, roty, ry, rz, r0, re, voigt_nu]) xinp.extend( lph.prep_input((a, b, c, alpha, beta, gamma), cs)) const_params = (G, B) args = ( I_r_true, observations_original_sel, wavelength, alpha_angle_sel, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, refine_mode, const_params, B, miller_array_iso, iparams, ) lh = lbfgs_handler(current_x=xinp, args=args) xopt = flex.double(list(lh.x)) if (refine_mode == "crystal_orientation" or refine_mode == "reflecting_range" or refine_mode == "allparams"): current_residual_err = lh.f # calculate residual_xy_error (for refine_mode = SF, CO, RR, and all params) xinp_uc = lph.prep_input((a, b, c, alpha, beta, gamma), cs) if refine_mode == "crystal_orientation": rotx, roty = xopt elif refine_mode == "reflecting_range": ry, rz, r0, re, voigt_nu = xopt elif refine_mode == "allparams": rotx, roty, ry, rz, r0, re, voigt_nu = xopt[:7] xinp_uc = xopt[7:] a, b, c, alpha, beta, gamma = lph.prep_output( xinp_uc, cs) const_params_uc = (G, B, rotx, roty, ry, rz, r0, re, voigt_nu) xinp_uc = lph.prep_input((a, b, c, alpha, beta, gamma), cs) args_uc = ( I_r_true, observations_original_sel, wavelength, alpha_angle_sel, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, "unit_cell", const_params_uc, B, miller_array_iso, iparams, ) uc_params_err = lph.func(xinp_uc, args_uc) current_residual_xy_err = flex.sum(uc_params_err**2) elif refine_mode == "unit_cell": current_residual_xy_err = lh.f xopt_uc = lph.prep_output(xopt, cs) a, b, c, alpha, beta, gamma = xopt_uc # check the unit-cell with the reference intensity xinp = flex.double([rotx, roty, ry, rz, r0, re, voigt_nu]) xinp.extend( lph.prep_input((a, b, c, alpha, beta, gamma), cs)) const_params_all = (G, B) args_all = ( I_r_true, observations_original_sel, wavelength, alpha_angle_sel, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, "allparams", const_params_all, B, miller_array_iso, iparams, ) all_params_err = lph.func(xinp_all, args_all) current_residual_err = flex.sum(all_params_err**2) flag_success = False if refine_mode == "allparams": # if allparams refinement, only check the post-refine target function if current_residual_err < ( t_pr_list[len(t_pr_list) - 1] + (t_pr_list[len(t_pr_list) - 1] * iparams.postref.residual_threshold / 100)): t_pr_list.append(current_residual_err) t_xy_list.append(current_residual_xy_err) refined_params_hist.append(( G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma, )) flag_success = True else: if current_residual_err < ( t_pr_list[len(t_pr_list) - 1] + (t_pr_list[len(t_pr_list) - 1] * iparams.postref.residual_threshold / 100)): if current_residual_xy_err < ( t_xy_list[len(t_xy_list) - 1] + (t_xy_list[len(t_xy_list) - 1] * iparams.postref.residual_threshold_xy / 100)): t_pr_list.append(current_residual_err) t_xy_list.append(current_residual_xy_err) refined_params_hist.append(( G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma, )) flag_success = True if flag_success is False: G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma = refined_params_hist[ len(refined_params_hist) - 1] tmp_txt_out = ( refine_mode + " %3.0f %6.4f %6.4f %6.4f %6.4f %10.8f %10.8f %10.8f %10.8f %10.8f %6.3f %6.3f %.4g %6.3f\n" % ( i_sub_cycle, G, B, rotx * 180 / math.pi, roty * 180 / math.pi, ry, rz, r0, re, voigt_nu, a, c, t_pr_list[len(t_pr_list) - 1], t_xy_list[len(t_pr_list) - 1], )) txt_out += tmp_txt_out # apply the refined parameters on the full (original) reflection set two_theta = observations_original.two_theta( wavelength=wavelength).data() sin_theta_over_lambda_sq = (observations_original.two_theta( wavelength=wavelength).sin_theta_over_lambda_sq().data()) if pres_in is None: partiality_init, delta_xy_init, rs_init, rh_init = ph.calc_partiality_anisotropy_set( observations_original.unit_cell(), 0.0, 0.0, observations_original.indices(), 0, 0, spot_radius, iparams.gamma_e, iparams.voigt_nu, two_theta, alpha_angle, wavelength, crystal_init_orientation, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, iparams.partiality_model, iparams.flag_beam_divergence, ) I_o_init = ph.calc_full_refl( observations_original.data(), sin_theta_over_lambda_sq, 1, 0, partiality_init, rs_init, ) else: partiality_init, delta_xy_init, rs_init, rh_init = ph.calc_partiality_anisotropy_set( pres_in.unit_cell, 0.0, 0.0, observations_original.indices(), pres_in.ry, pres_in.rz, pres_in.r0, pres_in.re, pres_in.voigt_nu, two_theta, alpha_angle, wavelength, crystal_init_orientation, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, iparams.partiality_model, iparams.flag_beam_divergence, ) I_o_init = ph.calc_full_refl( observations_original.data(), sin_theta_over_lambda_sq, pres_in.G, pres_in.B, partiality_init, rs_init, ) partiality_fin, delta_xy_fin, rs_fin, rh_fin = ph.calc_partiality_anisotropy_set( unit_cell((a, b, c, alpha, beta, gamma)), rotx, roty, observations_original.indices(), ry, rz, r0, re, voigt_nu, two_theta, alpha_angle, wavelength, crystal_init_orientation, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, iparams.partiality_model, iparams.flag_beam_divergence, ) I_o_fin = ph.calc_full_refl( observations_original.data(), sin_theta_over_lambda_sq, G, B, partiality_fin, rs_fin, ) SE_of_the_estimate = standard_error_of_the_estimate( I_r_flex, I_o_fin, 13) R_sq = coefficient_of_determination(I_r_flex, I_o_fin) * 100 CC_init = flex.linear_correlation(I_r_flex, I_o_init).coefficient() CC_final = flex.linear_correlation(I_r_flex, I_o_fin).coefficient() err_init = (I_r_flex - I_o_init) / observations_original.sigmas() R_init = math.sqrt(flex.sum(err_init**2)) err_final = (I_r_flex - I_o_fin) / observations_original.sigmas() R_final = math.sqrt(flex.sum(err_final**2)) R_xy_init = math.sqrt(flex.sum(delta_xy_init**2)) R_xy_final = math.sqrt(flex.sum(delta_xy_fin**2)) if R_init < R_final or re > (iparams.gamma_e * 3): CC_final = CC_init R_final = R_init R_xy_final = R_xy_init if pres_in is None: G, B, r0, ry, rz, re, rotx, roty = ( 1.0, 0.0, spot_radius, 0.0, 0.0, iparams.gamma_e, 0.0, 0.0, ) a, b, c, alpha, beta, gamma = ( observations_original.unit_cell().parameters()) else: G, B, r0, ry, rz, re, rotx, roty = ( pres_in.G, pres_in.B, pres_in.r0, pres_in.ry, pres_in.rz, pres_in.re, 0.0, 0.0, ) a, b, c, alpha, beta, gamma = pres_in.unit_cell.parameters() crystal_init_orientation = pres_in.crystal_orientation # calculate CCiso if hklisoin is given CC_iso_init, CC_iso_final = (0, 0) if iparams.hklisoin is not None: if miller_array_iso is not None: from cctbx import miller matches = miller.match_multi_indices( miller_indices_unique=miller_array_iso.indices(), miller_indices=observations_non_polar.indices(), ) I_iso_match = flex.double([ miller_array_iso.data()[pair[0]] for pair in matches.pairs() ]) I_o_init_match = flex.double( [I_o_init[pair[1]] for pair in matches.pairs()]) I_o_fin_match = flex.double( [I_o_fin[pair[1]] for pair in matches.pairs()]) CC_iso_init = flex.linear_correlation( I_iso_match, I_o_init_match).coefficient() CC_iso_final = flex.linear_correlation( I_iso_match, I_o_fin_match).coefficient() xopt = (G, B, rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma) return ( xopt, ( SE_of_the_estimate, R_sq, CC_init, CC_final, R_init, R_final, R_xy_init, R_xy_final, CC_iso_init, CC_iso_final, ), len(I_ref_sel), )
def test(init_test): single_panel_detector = init_test.experiments_single_panel.detectors()[0] multi_panel_detector = init_test.experiments_multi_panel.detectors()[0] beam = init_test.experiments_single_panel.beams()[0] gonio = init_test.experiments_single_panel.goniometers()[0] crystal = init_test.experiments_single_panel.crystals()[0] # Parameterise the models det_param = DetectorParameterisationSinglePanel(single_panel_detector) s0_param = BeamParameterisation(beam, gonio) xlo_param = CrystalOrientationParameterisation(crystal) xluc_param = CrystalUnitCellParameterisation(crystal) multi_det_param = DetectorParameterisationMultiPanel( multi_panel_detector, beam) # Fix beam to the X-Z plane (imgCIF geometry), fix wavelength s0_param.set_fixed([True, False, True]) # Link model parameterisations together into a parameterisation of the # prediction equation, first for the single panel detector pred_param = XYPhiPredictionParameterisation( init_test.experiments_single_panel, [det_param], [s0_param], [xlo_param], [xluc_param], ) # ... and now for the multi-panel detector pred_param2 = XYPhiPredictionParameterisation( init_test.experiments_multi_panel, [multi_det_param], [s0_param], [xlo_param], [xluc_param], ) ################################ # Apply known parameter shifts # ################################ # shift detectors by 1.0 mm each translation and 2 mrad each rotation det_p_vals = det_param.get_param_vals() p_vals = [ a + b for a, b in zip(det_p_vals, [1.0, 1.0, 1.0, 2.0, 2.0, 2.0]) ] det_param.set_param_vals(p_vals) multi_det_p_vals = multi_det_param.get_param_vals() p_vals = [ a + b for a, b in zip(multi_det_p_vals, [1.0, 1.0, 1.0, 2.0, 2.0, 2.0]) ] multi_det_param.set_param_vals(p_vals) # shift beam by 2 mrad in free axis s0_p_vals = s0_param.get_param_vals() p_vals = list(s0_p_vals) p_vals[0] += 2.0 s0_param.set_param_vals(p_vals) # rotate crystal a bit (=2 mrad each rotation) xlo_p_vals = xlo_param.get_param_vals() p_vals = [a + b for a, b in zip(xlo_p_vals, [2.0, 2.0, 2.0])] xlo_param.set_param_vals(p_vals) # change unit cell a bit (=0.1 Angstrom length upsets, 0.1 degree of # gamma angle) xluc_p_vals = xluc_param.get_param_vals() cell_params = crystal.get_unit_cell().parameters() cell_params = [ a + b for a, b in zip(cell_params, [0.1, 0.1, 0.1, 0.0, 0.0, 0.1]) ] new_uc = unit_cell(cell_params) newB = matrix.sqr(new_uc.fractionalization_matrix()).transpose() S = symmetrize_reduce_enlarge(crystal.get_space_group()) S.set_orientation(orientation=newB) X = tuple([e * 1.0e5 for e in S.forward_independent_parameters()]) xluc_param.set_param_vals(X) ############################### # Undo known parameter shifts # ############################### s0_param.set_param_vals(s0_p_vals) det_param.set_param_vals(det_p_vals) multi_det_param.set_param_vals(det_p_vals) xlo_param.set_param_vals(xlo_p_vals) xluc_param.set_param_vals(xluc_p_vals) ##################################### # Select reflections for refinement # ##################################### refman = ReflectionManager(init_test.observations_single_panel, init_test.experiments_single_panel) refman2 = ReflectionManager(init_test.observations_multi_panel, init_test.experiments_multi_panel) ############################### # Set up the target functions # ############################### target = LeastSquaresPositionalResidualWithRmsdCutoff( init_test.experiments_single_panel, ScansExperimentsPredictor(init_test.experiments_single_panel), refman, pred_param, restraints_parameterisation=None, ) target2 = LeastSquaresPositionalResidualWithRmsdCutoff( init_test.experiments_multi_panel, ScansExperimentsPredictor(init_test.experiments_multi_panel), refman2, pred_param2, restraints_parameterisation=None, ) ################################# # Set up the refinement engines # ################################# refiner = setup_minimiser.Extract(master_phil, target, pred_param).refiner refiner2 = setup_minimiser.Extract(master_phil, target2, pred_param2).refiner refiner.run() # reset parameters and run refinement with the multi panel detector s0_param.set_param_vals(s0_p_vals) multi_det_param.set_param_vals(det_p_vals) xlo_param.set_param_vals(xlo_p_vals) xluc_param.set_param_vals(xluc_p_vals) refiner2.run() # same number of steps assert refiner.get_num_steps() == refiner2.get_num_steps() # same rmsds for rmsd, rmsd2 in zip(refiner.history["rmsd"], refiner2.history["rmsd"]): assert approx_equal(rmsd, rmsd2) # same parameter values each step for params, params2 in zip(refiner.history["parameter_vector"], refiner.history["parameter_vector"]): assert approx_equal(params, params2)
if (item == "?"): item = "90" # enumeration default for angles is 90 degrees items.append(item) ic = items.count(None) if (ic == 6): if (strict): raise CifBuilderError( "Unit cell parameters not found in the cif file") unit_cell = None elif (ic == 0): try: vals = [float_from_string(s) for s in items] except ValueError: raise CifBuilderError("Invalid unit cell parameters are given") try: unit_cell = uctbx.unit_cell(vals) except RuntimeError, e: if "cctbx Error: Unit cell" in str(e): raise CifBuilderError(e) else: raise elif (space_group is not None): unit_cell = uctbx.infer_unit_cell_from_symmetry( [float_from_string(s) for s in items if s is not None], space_group) else: raise CifBuilderError( "Not all unit cell parameters are given in the cif file") if unit_cell is not None and space_group is not None: if not space_group.is_compatible_unit_cell(unit_cell): # try primitive setting
def set_params(self, observations=None, observations_original=None, refined_params=None, stats=None, partiality=None, rs_set=None, rh_set=None, frame_no=None, pickle_filename=None, wavelength=None, crystal_orientation=None, detector_distance_mm=None, identified_isoform=None, mapped_predictions=None, xbeam=None, ybeam=None): self.observations = observations self.observations_original = observations_original self.refined_params = refined_params self.partiality = partiality self.rs_set = rs_set self.rh_set = rh_set self.frame_no = frame_no self.pickle_filename = pickle_filename self.wavelength = wavelength self.detector_distance_mm = detector_distance_mm self.identified_isoform = identified_isoform self.mapped_predictions = mapped_predictions self.xbeam = xbeam self.ybeam = ybeam #refined_params #note params = G,B,rotx,roty,ry,a,b,c,alpha,beta,gamma self.G = refined_params[0] self.B = refined_params[1] self.rotx = refined_params[2] self.roty = refined_params[3] self.ry = refined_params[4] self.rz = refined_params[5] self.r0 = refined_params[6] self.re = refined_params[7] self.voigt_nu = refined_params[8] self.uc_params = flex.double([ refined_params[9], refined_params[10], refined_params[11], refined_params[12], refined_params[13], refined_params[14] ]) self.unit_cell = unit_cell( (refined_params[9], refined_params[10], refined_params[11], refined_params[12], refined_params[13], refined_params[14])) self.crystal_orientation = crystal_orientation #SE, R_sq, CC_init, CC_final, R_init, R_final, R_xy_init, R_xy_final self.SE = stats[0] self.R_sq = stats[1] self.CC_init = stats[2] self.CC_final = stats[3] self.R_init = stats[4] self.R_final = stats[5] self.R_xy_init = stats[6] self.R_xy_final = stats[7] self.CC_iso_init = stats[8] self.CC_iso_final = stats[9]
mtz.mtz_object().write("tst_combine_symmetry_2.mtz") try: cmdline = mmtbx.command_line.load_model_and_data( args=["tst_combine_symmetry.pdb", "tst_combine_symmetry_2.mtz"], master_phil=mmtbx.command_line.generic_simple_input_phil(), process_pdb_file=False, create_fmodel=True, out=null_out()) except Sorry, s: assert ("Incompatible space groups" in str(s)) else: raise Exception_expected # Part 3: unit cell mismatch f_calc_3 = f_calc.customized_copy( crystal_symmetry=f_calc.crystal_symmetry().customized_copy( unit_cell=uctbx.unit_cell((21.9371, 4.8659, 23.4774, 90.0, 104.0123, 90.00)))) flags_3 = f_calc_3.generate_r_free_flags() mtz = f_calc_3.as_mtz_dataset(column_root_label="F") mtz.add_miller_array(flags_3, column_root_label="FreeR_flag") mtz.mtz_object().write("tst_combine_symmetry_3.mtz") try: cmdline = mmtbx.command_line.load_model_and_data( args=["tst_combine_symmetry.pdb", "tst_combine_symmetry_3.mtz"], master_phil=mmtbx.command_line.generic_simple_input_phil(), process_pdb_file=False, create_fmodel=True, out=null_out()) except Sorry, s: assert ("Unit cell mismatch" in str(s)) else: raise Exception_expected
def optimize_scalefactors( self, I_r_flex, observations_original, wavelength, crystal_init_orientation, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, iparams, pres_in, observations_non_polar, detector_distance_mm, const_params, ): ph = partiality_handler() pr_d_min = iparams.postref.scale.d_min pr_d_max = iparams.postref.scale.d_max pr_sigma_min = iparams.postref.scale.sigma_min pr_partiality_min = iparams.postref.scale.partiality_min # filter by resolution observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "resolution", [pr_d_min, pr_d_max], observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, I_r_flex, ) # filter by sigma observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel = self.get_filtered_data( "sigma", [pr_sigma_min], observations_original_sel, alpha_angle_sel, spot_pred_x_mm_sel, spot_pred_y_mm_sel, I_ref_sel, ) I_r_true = I_ref_sel[:] I_o_true = observations_original_sel.data()[:] if pres_in is not None: G, B, b0 = pres_in.G, pres_in.B, pres_in.B else: G, B, b0 = (1, 0, 0) refine_mode = "scale_factor" xinp = flex.double([G, B]) args = ( I_r_true, observations_original_sel, wavelength, alpha_angle_sel, crystal_init_orientation, spot_pred_x_mm_sel, spot_pred_y_mm_sel, detector_distance_mm, refine_mode, const_params, b0, None, iparams, ) lh = lbfgs_handler(current_x=xinp, args=args) G_fin, B_fin = (lh.x[0], lh.x[1]) rotx, roty, ry, rz, r0, re, voigt_nu, a, b, c, alpha, beta, gamma = const_params two_theta = observations_original.two_theta(wavelength=wavelength) sin_theta_over_lambda_sq = two_theta.sin_theta_over_lambda_sq().data() uc = unit_cell((a, b, c, alpha, beta, gamma)) ph = partiality_handler() partiality_init, delta_xy_init, rs_init, dummy = ph.calc_partiality_anisotropy_set( uc, rotx, roty, observations_original.indices(), ry, rz, r0, re, voigt_nu, two_theta.data(), alpha_angle, wavelength, crystal_init_orientation, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, iparams.partiality_model, iparams.flag_beam_divergence, ) I_o_init = ph.calc_full_refl( observations_original.data(), sin_theta_over_lambda_sq, G, B, partiality_init, rs_init, ) I_o_fin = ph.calc_full_refl( observations_original.data(), sin_theta_over_lambda_sq, G_fin, B_fin, partiality_init, rs_init, ) SE_of_the_estimate = standard_error_of_the_estimate( I_r_flex, I_o_fin, 2) R_sq = coefficient_of_determination(I_r_flex, I_o_fin) * 100 CC_init = flex.linear_correlation(I_r_flex, I_o_init).coefficient() CC_final = flex.linear_correlation(I_r_flex, I_o_fin).coefficient() err_init = (I_r_flex - I_o_init) / observations_original.sigmas() R_init = math.sqrt(flex.sum(err_init**2)) err_final = (I_r_flex - I_o_fin) / observations_original.sigmas() R_final = math.sqrt(flex.sum(err_final**2)) R_xy_init = 0 R_xy_final = 0 CC_iso_init = 0 CC_iso_final = 0 return ( flex.double(list(lh.x)), ( SE_of_the_estimate, R_sq, CC_init, CC_final, R_init, R_final, R_xy_init, R_xy_final, CC_iso_init, CC_iso_final, ), )
def exercise_box_frac_around_sites(): unit_cell = uctbx.unit_cell((10, 10, 10, 90, 90, 120)) buffer = 2 sites_frac = flex.vec3_double([(1 / 2., 2 / 3., 0.), (1 / 2., 1 / 3., 0.)]) min_, max_ = unit_cell.box_frac_around_sites(sites_frac=sites_frac, buffer=buffer) assert approx_equal(min_, (0.26905989232414967, 0.10239322565748302, -0.2)) assert approx_equal(max_, (0.73094010767585038, 0.8976067743425169, 0.2)) sites_cart = unit_cell.orthogonalize(sites_frac=sites_frac) min_, max_ = sites_cart.min(), sites_cart.max() min_ = unit_cell.fractionalize([m - buffer for m in min_]) max_ = unit_cell.fractionalize([m + buffer for m in max_]) assert approx_equal(min_, (0.017863279495408259, 0.10239322565748302, -0.2)) assert approx_equal(max_, (0.98213672050459189, 0.8976067743425169, 0.2)) unit_cells = [ uctbx.unit_cell(params) for params in [(10, 15, 20, 90, 90, 90), (10, 10, 20, 90, 90, 120), (10, 10, 10, 60, 60, 60)] ] sites_cart = flex.vec3_double([(2.23474, 8.72834, 4.70562), (3.72656, 3.28621, 9.19121), (-6.83519, -7.5707, 4.62386)]) c_inv_rs = [(1, 0, 0, 0, 1, 0, 0, 0, 1), (0, 1, 0, 0, 0, 1, 1, 0, 0), (0, 0, 1, 1, 0, 0, 0, 1, 0)] for unit_cell_0 in unit_cells: for c_inv_r in c_inv_rs: unit_cell = unit_cell_0.change_basis(c_inv_r) sites_frac = unit_cell.fractionalize(sites_cart=sites_cart) min0, max0 = unit_cell.box_frac_around_sites(sites_cart=sites_cart) for x, y in zip(min0, max0): assert x < y for buffer in [None, 0]: min_, max_ = unit_cell.box_frac_around_sites( sites_cart=sites_cart, buffer=buffer) assert approx_equal(min_, min0) assert approx_equal(max_, max0) min_, max_ = unit_cell.box_frac_around_sites( sites_frac=sites_frac, buffer=buffer) assert approx_equal(min_, min0) assert approx_equal(max_, max0) for buffer in [0, 3, 5, 7]: min0, max0 = unit_cell.box_frac_around_sites( sites_cart=sites_cart, buffer=buffer) for x, y in zip(min0, max0): assert x < y min_, max_ = unit_cell.box_frac_around_sites( sites_frac=sites_frac, buffer=buffer) assert approx_equal(min_, min0) assert approx_equal(max_, max0) min_, max_ = sites_cart.min(), sites_cart.max() min_ = unit_cell.fractionalize([m - buffer for m in min_]) max_ = unit_cell.fractionalize([m + buffer for m in max_]) if (unit_cell_0 is unit_cells[0]): assert approx_equal(min_, min0) assert approx_equal(max_, max0) elif (buffer == 0 and unit_cell_0 is unit_cells[1]): assert approx_equal(min_, min0) if (c_inv_r is c_inv_rs[2]): assert approx_equal(max_, max0) else: assert not_approx_equal(max_, max0) else: assert not_approx_equal(min_, min0) assert not_approx_equal(max_, max0)
def exercise_basic(): d = (1, 1, 1, 90, 90, 90) u = uctbx.unit_cell() assert approx_equal(u.parameters(), d) u = uctbx.unit_cell(d) assert u.parameters() == d assert approx_equal(u.parameters(), u.reciprocal_parameters()) assert approx_equal(u.volume(), 1) assert approx_equal(u.longest_vector_sq(), 3) assert approx_equal(u.shortest_vector_sq(), 1) p = (2, 3, 4, 80, 100, 110) for i in xrange(7): u = uctbx.unit_cell(p[:i]) assert u.parameters() == p[:i] + d[i:] v = uctbx.unit_cell(p[:i]) assert v.parameters() == u.parameters() if (i): assert not_approx_equal(u.parameters(), u.reciprocal_parameters()) assert not u.is_similar_to(u.reciprocal()) assert not u.is_similar_to(u.reciprocal(), 1.e-3) assert not u.is_similar_to(u.reciprocal(), 1.e-3, 1.e-3) assert u.is_similar_to(u.reciprocal(), 1000, 180) assert approx_equal(u.reciprocal_parameters(), u.reciprocal().parameters()) assert approx_equal(u.parameters(), u.reciprocal().reciprocal_parameters()) assert approx_equal(u.reciprocal_metrical_matrix(), u.reciprocal().metrical_matrix()) assert approx_equal(u.metrical_matrix(), u.reciprocal().reciprocal_metrical_matrix()) v = u.reciprocal().reciprocal() assert u.is_similar_to(v, 1.e-3, 1.e-3) assert approx_equal(u.volume(), 1 / u.reciprocal().volume()) u = uctbx.unit_cell(p) assert not u.is_degenerate() assert not u.is_degenerate(1.e-10) assert not u.is_degenerate(1.e-10, 1.e-5) assert u.is_degenerate(10) assert u.is_degenerate(1.e-10, 20) m = u.metrical_matrix() n = (2 * 2, 3 * 3, 4 * 4, 2 * 3 * cos(110 * pi / 180), 2 * 4 * cos(100 * pi / 180), 3 * 4 * cos(80 * pi / 180)) assert approx_equal(m, n) v = uctbx.unit_cell(metrical_matrix=m) assert approx_equal(u.parameters(), v.parameters()) u = uctbx.unit_cell((2, 3, 4)) assert approx_equal(u.volume(), 2 * 3 * 4) assert approx_equal(u.longest_vector_sq(), 2 * 2 + 3 * 3 + 4 * 4) assert approx_equal(u.shortest_vector_sq(), 2 * 2) u = uctbx.unit_cell(p) assert approx_equal(u.volume(), 22.04006625) assert approx_equal( u.d_volume_d_params(), (11.020033123326023, 7.3466887488840156, 5.5100165616630115, 0.051324088220620838, -0.051324088220620769, -0.13367230402431379)) for alpha in xrange(70, 121, 10): for beta in xrange(70, 121, 10): for gamma in xrange(70, 121, 10): u = uctbx.unit_cell([7, 11, 13, alpha, beta, gamma]) v = uctbx.unit_cell( orthogonalization_matrix=u.orthogonalization_matrix()) assert v.is_similar_to(u)
def get_average_cell(self): a = flex.mean(self.all_uc_a_values) b = flex.mean(self.all_uc_b_values) c = flex.mean(self.all_uc_c_values) return uctbx.unit_cell(list((a,b,c)) + list(self.reference_unit_cell.parameters()[3:]))
def exercise_pickle(): u = uctbx.unit_cell((2, 3, 5, 80, 100, 110)) p = pickle.dumps(u) v = pickle.loads(p) assert u.parameters() == v.parameters()
def finite_diffs_aniso(p_scale, u_star, centric=False, h=0.0001): d_star_sq = flex.double(2, 0.25) f_obs = flex.double(2, 1.0) centric_array = flex.bool(2, centric) sigma_f_obs = f_obs / 10.0 sigma_sq = flex.double(2, 1.0) epsilon = flex.double(2, 1.0) gamma = flex.double(2, 0.0) unit_cell = uctbx.unit_cell('20, 30, 40, 90.0, 90.0, 90.0') mi = flex.miller_index(((1, 2, 3), (1, 2, 3))) xs = crystal.symmetry((20, 30, 40), "P 2 2 2") ms = miller.set(xs, mi) nll_norm = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) nll_scale = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale + h, unit_cell, u_star) u_star[0] += h nll_u11 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) u_star[0] -= h u_star[1] += h nll_u22 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) u_star[1] -= h u_star[2] += h nll_u33 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) u_star[2] -= h u_star[3] += h nll_u12 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) u_star[3] -= h u_star[4] += h nll_u13 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) u_star[4] -= h u_star[5] += h nll_u23 = scaling.wilson_single_nll_aniso(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) g = scaling.wilson_single_nll_aniso_gradient(ms.indices()[0], f_obs[0], sigma_f_obs[0], epsilon[0], sigma_sq[0], gamma[0], centric_array[0], p_scale, unit_cell, u_star) g2 = scaling.wilson_total_nll_aniso_gradient(ms.indices(), f_obs, sigma_f_obs, epsilon, sigma_sq, gamma, centric_array, p_scale, unit_cell, u_star) ds = (nll_norm - nll_scale) / -h du11 = (nll_norm - nll_u11) / -h du22 = (nll_norm - nll_u22) / -h du33 = (nll_norm - nll_u33) / -h du12 = (nll_norm - nll_u12) / -h du13 = (nll_norm - nll_u13) / -h du23 = (nll_norm - nll_u23) / -h assert approx_equal(ds, g[0]), (ds, g[0]) assert approx_equal(du11, g[1]), (du11, g[1]) assert approx_equal(du22, g[2]) assert approx_equal(du33, g[3]) assert approx_equal(du12, g[4]) assert approx_equal(du13, g[5]) assert approx_equal(du23, g[6]) assert approx_equal(ds, g2[0] / 2.0) assert approx_equal(du11, g2[1] / 2.0) assert approx_equal(du22, g2[2] / 2.0) assert approx_equal(du33, g2[3] / 2.0) assert approx_equal(du12, g2[4] / 2.0) assert approx_equal(du13, g2[5] / 2.0) assert approx_equal(du23, g2[6] / 2.0)
def test_run(dials_regression, tmpdir): expected_unit_cell = uctbx.unit_cell( (11.624, 13.550, 30.103, 89.964, 93.721, 90.132)) expected_rmsds = (0.039, 0.035, 0.002) experiments_old = os.path.join(dials_regression, "indexing_test_data", "phi_scan", "datablock_old.json") experiments_new = os.path.join(dials_regression, "indexing_test_data", "phi_scan", "datablock.json") strong_pickle = os.path.join(dials_regression, "indexing_test_data", "phi_scan", "strong.pickle") from dxtbx.serialize import load imageset_old = load.experiment_list(experiments_old, check_format=False).imagesets()[0] imageset_new = load.experiment_list(experiments_new, check_format=False).imagesets()[0] gonio_old = imageset_old.get_goniometer() gonio_new = imageset_new.get_goniometer() assert gonio_old.get_rotation_axis() == pytest.approx( (0.7497646259807715, -0.5517923303436749, 0.36520984351713554)) assert gonio_old.get_setting_rotation() == pytest.approx( (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) assert gonio_old.get_fixed_rotation() == pytest.approx(( 0.7497646259807748, -0.20997265900532208, -0.6275065641872948, -0.5517923303436731, 0.3250014637526764, -0.7680490041218182, 0.3652098435171313, 0.9221092836691605, 0.12781329809272568, )) assert gonio_new.get_rotation_axis() == pytest.approx( gonio_old.get_rotation_axis()) assert gonio_new.get_rotation_axis_datum() == pytest.approx((1, 0, 0)) assert gonio_new.get_setting_rotation() == pytest.approx(( 0.7497646259807705, -0.20997265900532142, -0.6275065641873, -0.5517923303436786, 0.3250014637526763, -0.768049004121814, 0.3652098435171315, 0.9221092836691607, 0.12781329809272335, )) assert gonio_new.get_fixed_rotation() == pytest.approx( (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) result_old = run_indexing( strong_pickle, experiments_old, tmpdir, extra_args=[], expected_unit_cell=expected_unit_cell, expected_rmsds=expected_rmsds, expected_hall_symbol=" P 1", ) result_new = run_indexing( strong_pickle, experiments_new, tmpdir, extra_args=[], expected_unit_cell=expected_unit_cell, expected_rmsds=expected_rmsds, expected_hall_symbol=" P 1", ) assert result_old.rmsds == pytest.approx(result_new.rmsds, abs=1e-6) assert result_old.experiments[0].crystal.get_unit_cell().parameters( ) == pytest.approx( result_new.experiments[0].crystal.get_unit_cell().parameters(), abs=1e-6) # Now test refinement gradients are correct from dxtbx.model.experiment_list import Experiment, ExperimentList old_exps = ExperimentList([ Experiment( beam=imageset_old.get_beam(), detector=imageset_old.get_detector(), goniometer=gonio_old, scan=imageset_old.get_scan(), crystal=result_old.experiments[0].crystal, imageset=None, ) ]) new_exps = ExperimentList([ Experiment( beam=imageset_new.get_beam(), detector=imageset_new.get_detector(), goniometer=gonio_new, scan=imageset_new.get_scan(), crystal=result_new.experiments[0].crystal, imageset=None, ) ]) from libtbx.phil import parse from dials.algorithms.refinement.refiner import phil_scope params = phil_scope.fetch(source=parse("")).extract() from dials.algorithms.refinement.refiner import RefinerFactory refiner_old = RefinerFactory.from_parameters_data_experiments( params, result_old.indexed_reflections, old_exps) refiner_new = RefinerFactory.from_parameters_data_experiments( params, result_new.indexed_reflections, new_exps) # Analytical gradients should be approximately the same in either case an_grads_old = refiner_old._pred_param.get_gradients( refiner_old.get_matches()) an_grads_new = refiner_new._pred_param.get_gradients( refiner_new.get_matches()) for g1, g2 in zip(an_grads_old, an_grads_new): assert g1["dX_dp"] == pytest.approx(g2["dX_dp"], abs=1.0e-6) assert g1["dY_dp"] == pytest.approx(g2["dY_dp"], abs=1.0e-6) assert g1["dphi_dp"] == pytest.approx(g2["dphi_dp"], abs=1.0e-6) # Analytical gradients should be approximately equal to finite difference # gradients in either case fd_grads_old = calc_fd_grads(refiner_old) for g1, g2 in zip(fd_grads_old, an_grads_old): assert g1["dX_dp"] == pytest.approx(g2["dX_dp"], abs=5.0e-6) assert g1["dY_dp"] == pytest.approx(g2["dY_dp"], abs=5.0e-6) assert g1["dphi_dp"] == pytest.approx(g2["dphi_dp"], abs=5.0e-6) fd_grads_new = calc_fd_grads(refiner_new) for g1, g2 in zip(fd_grads_new, an_grads_new): assert g1["dX_dp"] == pytest.approx(g2["dX_dp"], abs=5.0e-6) assert g1["dY_dp"] == pytest.approx(g2["dY_dp"], abs=5.0e-6) assert g1["dphi_dp"] == pytest.approx(g2["dphi_dp"], abs=5.0e-6)
def __init__(self, cif_block, strict=False): # The order of priority for determining space group is: # sym_ops, hall symbol, H-M symbol, space group number self.cif_block = cif_block sym_ops = self.get_cif_item('_space_group_symop_operation_xyz') sym_op_ids = self.get_cif_item('_space_group_symop_id') space_group = None if sym_ops is not None: if isinstance(sym_ops, string_types): sym_ops = flex.std_string([sym_ops]) if sym_op_ids is not None: if isinstance(sym_op_ids, string_types): sym_op_ids = flex.std_string([sym_op_ids]) assert len(sym_op_ids) == len(sym_ops) self.sym_ops = {} space_group = sgtbx.space_group() if isinstance(sym_ops, string_types): sym_ops = [sym_ops] for i, op in enumerate(sym_ops): try: s = sgtbx.rt_mx(op) except RuntimeError as e: str_e = str(e) if "Parse error: " in str_e: raise CifBuilderError( "Error interpreting symmetry operator: %s" % (str_e.split("Parse error: ")[-1])) else: raise if sym_op_ids is None: sym_op_id = i + 1 else: try: sym_op_id = int(sym_op_ids[i]) except ValueError as e: raise CifBuilderError( "Error interpreting symmetry operator id: %s" % (str(e))) self.sym_ops[sym_op_id] = s space_group.expand_smx(s) else: hall_symbol = self.get_cif_item('_space_group_name_Hall') hm_symbol = self.get_cif_item('_space_group_name_H-M_alt') sg_number = self.get_cif_item('_space_group_IT_number') if space_group is None and hall_symbol not in (None, '?'): try: space_group = sgtbx.space_group(hall_symbol) except Exception: pass if space_group is None and hm_symbol not in (None, '?'): try: space_group = sgtbx.space_group_info( symbol=hm_symbol).group() except Exception: pass if space_group is not None and sg_number not in (None, '?'): try: space_group = sgtbx.space_group_info( number=sg_number).group() except Exception: pass if (space_group is None and strict): raise CifBuilderError( "No symmetry instructions could be extracted from the cif block" ) items = [self.get_cif_item("_cell_length_" + s) for s in "abc"] for i, item in enumerate(items): if isinstance(item, flex.std_string): raise CifBuilderError( "Data item _cell_length_%s cannot be declared in a looped list" % ("abc"[i])) for s in ["alpha", "beta", "gamma"]: item = self.get_cif_item("_cell_angle_" + s) if isinstance(item, flex.std_string): raise CifBuilderError( "Data item _cell_angle_%s cannot be declared in a looped list" % s) if (item == "?"): item = "90" # enumeration default for angles is 90 degrees items.append(item) ic = items.count(None) if (ic == 6): if (strict): raise CifBuilderError( "Unit cell parameters not found in the cif file") unit_cell = None elif (ic == 0): try: vals = [float_from_string(s) for s in items] except ValueError: raise CifBuilderError("Invalid unit cell parameters are given") try: unit_cell = uctbx.unit_cell(vals) except RuntimeError as e: if "cctbx Error: Unit cell" in str(e): raise CifBuilderError(e) else: raise elif (space_group is not None): unit_cell = uctbx.infer_unit_cell_from_symmetry( [float_from_string(s) for s in items if s is not None], space_group) else: raise CifBuilderError( "Not all unit cell parameters are given in the cif file") if unit_cell is not None and space_group is not None: if not space_group.is_compatible_unit_cell(unit_cell): # try primitive setting space_group_input = space_group space_group = space_group.info().primitive_setting().group() if not space_group.is_compatible_unit_cell(unit_cell): raise CifBuilderError( "Space group is incompatible with unit cell parameters:\n" + \ " Space group: %s\n" %space_group_input.info() + \ " Unit cell: %s" %unit_cell) self.crystal_symmetry = crystal.symmetry(unit_cell=unit_cell, space_group=space_group)
def find_basis_vector_combinations_cluster_analysis(self): self.fft() # hijack the xray.structure class to facilitate calculation of distances xs = xray.structure(crystal_symmetry=self.crystal_symmetry) for i, site in enumerate(self.sites): xs.add_scatterer(xray.scatterer("C%i" % i, site=site)) xs = xs.sites_mod_short() xs = xs.select(xs.sites_frac().norms() < 0.45) cell_multiplier = 10 xs1 = xs.customized_copy(unit_cell=uctbx.unit_cell( [xs.unit_cell().parameters()[0] * cell_multiplier] * 3)) xs1.set_sites_cart(xs.sites_cart()) xs = xs1 sites_cart = xs.sites_cart() lengths = flex.double([matrix.col(sc).length() for sc in sites_cart]) xs = xs.select(flex.sort_permutation(lengths)) if self.params.debug: with open('peaks.pdb', 'wb') as f: f.write(xs.as_pdb_file()) vector_heights = flex.double() sites_frac = xs.sites_frac() pair_asu_table = xs.pair_asu_table( distance_cutoff=self.params.max_cell) asu_mappings = pair_asu_table.asu_mappings() distances = crystal.calculate_distances(pair_asu_table, sites_frac) vectors = [] difference_vectors = [] pairs = [] for di in distances: if di.distance < self.params.min_cell: continue i_seq, j_seq = di.i_seq, di.j_seq if i_seq > j_seq: continue pairs.append((i_seq, j_seq)) rt_mx_ji = di.rt_mx_ji site_frac_ji = rt_mx_ji * sites_frac[j_seq] site_cart_ji = xs.unit_cell().orthogonalize(site_frac_ji) site_cart_i = xs.unit_cell().orthogonalize(sites_frac[i_seq]) vectors.append(matrix.col(site_cart_ji)) diff_vec = matrix.col(site_cart_i) - matrix.col(site_cart_ji) if diff_vec[0] < 0: # only one hemisphere of difference vector space diff_vec = -diff_vec difference_vectors.append(diff_vec) params = self.params.multiple_lattice_search.cluster_analysis if params.method == 'dbscan': i_cluster = self.cluster_analysis_dbscan(difference_vectors) min_cluster_size = 1 elif params.method == 'hcluster': i_cluster = self.cluster_analysis_hcluster(difference_vectors) i_cluster -= 1 # hcluster starts counting at 1 min_cluster_size = params.min_cluster_size if self.params.debug_plots: self.debug_plot_clusters(difference_vectors, i_cluster, min_cluster_size=min_cluster_size) clusters = [] min_cluster_size = params.min_cluster_size for i in range(max(i_cluster) + 1): isel = (i_cluster == i).iselection() if len(isel) < min_cluster_size: continue clusters.append(isel) cluster_point_sets = [] centroids = [] cluster_sizes = flex.int() difference_vectors = flex.vec3_double(difference_vectors) from libtbx.utils import flat_list for cluster in clusters: points = flat_list([pairs[i] for i in cluster]) cluster_point_sets.append(set(points)) d_vectors = difference_vectors.select(cluster) cluster_sizes.append(len(d_vectors)) centroids.append(d_vectors.mean()) # build a graph where each node is a centroid from the difference vector # cluster analysis above, and an edge is defined when there is a # significant overlap between the sets of peaks in the FFT map that # contributed to the difference vectors in two clusters import networkx as nx G = nx.Graph() G.add_nodes_from(range(len(cluster_point_sets))) cutoff_frac = 0.25 for i in range(len(cluster_point_sets)): for j in range(i + 1, len(cluster_point_sets)): intersection_ij = cluster_point_sets[i].intersection( cluster_point_sets[j]) union_ij = cluster_point_sets[i].union(cluster_point_sets[j]) frac_connected = len(intersection_ij) / len(union_ij) if frac_connected > cutoff_frac: G.add_edge(i, j) # iteratively find the maximum cliques in the graph # break from the loop if there are no cliques remaining or there are # fewer than 3 vectors in the remaining maximum clique # Allow 1 basis vector to be shared between two cliques, to allow for # cases where two lattices share one basis vectors (e.g. two plate # crystals exactly aligned in one direction, but not in the other two) distinct_cliques = [] cliques = list(nx.find_cliques(G)) cliques = sorted(cliques, key=len, reverse=True) for i, clique in enumerate(cliques): clique = set(clique) if len(clique) < 3: break is_distinct = True for c in distinct_cliques: if len(c.intersection(clique)) > 1: is_distinct = False break if is_distinct: distinct_cliques.append(clique) this_set = set() for i_cluster in clique: this_set = this_set.union(cluster_point_sets[i_cluster]) logger.info("Clique %i: %i lattice points" % (i + 1, len(this_set))) assert len(distinct_cliques) > 0 logger.info("Estimated number of lattices: %i" % len(distinct_cliques)) self.candidate_basis_vectors = [] self.candidate_crystal_models = [] for clique in distinct_cliques: sel = flex.size_t(list(clique)) vectors = flex.vec3_double(centroids).select(sel) perm = flex.sort_permutation(vectors.norms()) vectors = [matrix.col(vectors[p]) for p in perm] # exclude vectors that are (approximately) integer multiples of a shorter # vector unique_vectors = [] for v in vectors: is_unique = True for v_u in unique_vectors: if is_approximate_integer_multiple(v_u, v, relative_tolerance=0.01, angular_tolerance=0.5): is_unique = False break if is_unique: unique_vectors.append(v) vectors = unique_vectors self.candidate_basis_vectors.extend(vectors) candidate_orientation_matrices \ = self.find_candidate_orientation_matrices(vectors) if len(candidate_orientation_matrices) == 0: continue crystal_model, n_indexed = self.choose_best_orientation_matrix( candidate_orientation_matrices) if crystal_model is None: continue # map to minimum reduced cell crystal_symmetry = crystal.symmetry( unit_cell=crystal_model.get_unit_cell(), space_group=crystal_model.get_space_group()) cb_op = crystal_symmetry.change_of_basis_op_to_minimum_cell() crystal_model = crystal_model.change_basis(cb_op) self.candidate_crystal_models.append(crystal_model) if self.params.debug: file_name = "vectors.pdb" a = self.params.max_cell cs = crystal.symmetry(unit_cell=(a, a, a, 90, 90, 90), space_group="P1") xs = xray.structure(crystal_symmetry=cs) for v in difference_vectors: v = matrix.col(v) xs.add_scatterer(xray.scatterer("C", site=v / (a / 10))) xs.sites_mod_short() with open(file_name, 'wb') as f: f.write(xs.as_pdb_file()) for crystal_model in self.candidate_crystal_models: logger.debug(crystal_model)
def test_index_after_search(dials_data, run_in_tmpdir): """Integrate the beam centre search with the rest of the toolchain Do the following: 1. Run dials.import with a specified beam centre, check for expected output; 2. Run dials.find_spots, check for expected output; 3. Run dials.search_beam_centre on the resultant datablock and pickled reflection table, check for expected output; 4. Run dials.index, using the datablock from the beam centre search, and check that the expected unit ecll is obtained and that the RMSDs are smaller than or equal to some expected values.""" dials_data = dials_data("thaumatin_i04").listdir(sort=True) g = [f.strpath for f in dials_data if f.ext == ".cbf"] # beam centre from image headers: 205.28,210.76 mm args = ["dials.import", "mosflm_beam_centre=207,212"] + g print(args) if os.name != "nt": result = procrunner.run(args) assert not result.returncode and not result.stderr else: # Can't run this command on Windows, # as it will exceed the maximum Windows command length limits. # So, instead: with mock.patch.object(sys, "argv", args): dials.command_line.dials_import.Script().run() assert os.path.exists("imported.expt") # spot-finding, just need a subset of the data args = [ "dials.find_spots", "imported.expt", "scan_range=1,10", "scan_range=531,540", ] print(args) result = procrunner.run(args) assert not result.returncode and not result.stderr assert os.path.exists("strong.refl") # actually run the beam centre search search_beam_position.run(["imported.expt", "strong.refl"]) assert os.path.exists("optimised.expt") # look at the results experiments = load.experiment_list("imported.expt", check_format=False) original_imageset = experiments.imagesets()[0] optimized_experiments = load.experiment_list("optimised.expt", check_format=False) detector_1 = original_imageset.get_detector() detector_2 = optimized_experiments.detectors()[0] shift = scitbx.matrix.col(detector_1[0].get_origin()) - scitbx.matrix.col( detector_2[0].get_origin()) print(shift) # check we can actually index the resulting optimized experiments expected_unit_cell = uctbx.unit_cell( (57.780, 57.800, 150.017, 89.991, 89.990, 90.007)) expected_rmsds = (0.06, 0.05, 0.001) expected_hall_symbol = " P 1" run_indexing( "strong.refl", "optimised.expt", py.path.local(os.path.curdir), [], expected_unit_cell, expected_rmsds, expected_hall_symbol, )
assert u.parameters() == v.parameters() def exercise_exceptions(): if ("--skip" in sys.argv[1:]): print "SKIPPING: exercise_exceptions" return try: u = uctbx.unit_cell((0, 0, 0, 0, 0, 0)) except ValueError, e: assert str(e) == "Unit cell parameter is zero or negative.",\ str(e) else: raise AssertionError, 'exception expected' try: u = uctbx.unit_cell(metrical_matrix=(0, 0, 0, 0, 0, 0)) except ValueError, e: assert str(e) == "Corrupt metrical matrix.", str(e) else: raise AssertionError, 'exception expected' u = uctbx.unit_cell((2, 3, 5, 80, 100, 110)) try: u.two_theta((-3, 4, -5), 1.5) except RuntimeError, e: assert str(e).endswith("CCTBX_ASSERT(sin_theta <= 1.0) failure."), \ str(e) else: raise AssertionError, 'exception expected' def exercise_fast_minimum_reduction():
def xplor_map_type(m,N,radius,file_name='map.xplor'): gridding = xplor_map.gridding( [N*2+1]*3, [0]*3, [2*N]*3) grid = flex.grid(N*2+1, N*2+1,N*2+1) m.reshape( grid ) uc = uctbx.unit_cell(" %s"%(radius*2.0)*3+"90 90 90") xplor_map.writer( file_name, ['no title lines'],uc, gridding,m) # is_p1_cell=True) # True)
def unit_cell_info(sub_clusters): """ Print unit cell information for a list of clusters. :param sub_clusters: a list of cluster objects :return: a string containing median unit cells, standard deviations and point group composition of each cluster. """ from libtbx.utils import plural_s # 3. print out some information that is useful. out_str = "\n\n{:<16} {:<8} {:<13} {:<13} {:<13} {:<12} {:<12} {:<12}{:<8}\n".format( "Cluster_id", "N_xtals", "Med_a", "Med_b", "Med_c", "Med_alpha", "Med_beta", "Med_gamma", "Delta(deg)") singletons = [] for cluster in sub_clusters: if len(cluster.members) != 1: # New approach, takes niggli setting of the cluster median and converts # back to reference setting for cluster report. Fixes cctbx#97. from cctbx import crystal from cctbx.uctbx import unit_cell from cctbx.sgtbx.lattice_symmetry import metric_subgroups input_symmetry = crystal.symmetry(unit_cell=unit_cell( cluster.medians[0:6]), space_group_symbol="P 1") groups = metric_subgroups( input_symmetry, 3.00, enforce_max_delta_for_generated_two_folds=True) group = groups.result_groups[0] # suppress stdout output for now from six.moves import StringIO SS = StringIO() import sys sys.stdout = SS group['best_subsym'].space_group_info().show_summary() sys.stdout = sys.__stdout__ print " Unit cell:", group[ 'best_subsym'].unit_cell() uc_params_conv = group['best_subsym'].unit_cell().parameters() sorted_pg_comp = sorted(cluster.pg_composition.items(), key=lambda x: -1 * x[1]) pg_strings = [ "{} in {}".format(pg[1], pg[0]) for pg in sorted_pg_comp ] point_group_string = ", ".join(pg_strings) + "." out_str += point_group_string out_str += ("\n{:<16} {:<8} {:<6.2f}({:<5.2f}) {:<6.2f}({:<5.2f})" " {:<6.2f}({:<5.2f}) {:<6.2f}({:<4.2f}) {:<6.2f}" "({:<4.2f}) {:<6.2f}({:<4.2f})").format( cluster.cname, len(cluster.members), cluster.medians[0], cluster.stdevs[0], cluster.medians[1], cluster.stdevs[1], cluster.medians[2], cluster.stdevs[2], cluster.medians[3], cluster.stdevs[3], cluster.medians[4], cluster.stdevs[4], cluster.medians[5], cluster.stdevs[5]) out_str += ("\n{:>24} {:<6.2f}{:<7} {:<6.2f}{:<7}" " {:<6.2f}{:<7} {:<6.2f}{:<6} {:<6.2f}" "{:<6} {:<6.2f}{:<6} {:<6.2}").format( SS.getvalue().strip()[13:], uc_params_conv[0], "", uc_params_conv[1], "", uc_params_conv[2], "", uc_params_conv[3], "", uc_params_conv[4], "", uc_params_conv[5], "", group["max_angular_difference"]) + "\n\n" else: singletons.append("".join([ ("{:<14} {:<11.2f} {:<11.2f} {:<11.2f}" "{:<12.1f} {:<12.1f} {:<12.1f}").format( cluster.pg_composition.keys()[0], cluster.members[0].uc[0], cluster.members[0].uc[1], cluster.members[0].uc[2], cluster.members[0].uc[3], cluster.members[0].uc[4], cluster.members[0].uc[5]), '\n' ])) out_str += "\nStandard deviations are in brackets." explanation = """\nEach cluster: Input lattice count, with integration Bravais setting space group. Cluster median with Niggli cell parameters (std dev in brackets). Highest possible metric symmetry and unit cell using LePage (J Appl Cryst 1982, 15:255) method, maximum delta 3deg.""" out_str += explanation singleton_str = "\n%i singleton%s:" % plural_s(len(singletons)) singleton_str += "\n\n{:<14} {:<11} {:<11} {:<11}{:<12} {:<12} {:<12}\n".format( "Point group", "a", "b", "c", "alpha", "beta", "gamma") singleton_str += "".join(singletons) n_clusters = len(sub_clusters) - len(singletons) out_str = "\n%i cluster%s:" % plural_s(n_clusters) + out_str return singleton_str + out_str
def test_symmetry_analysis(): coords = flex.double([ [0.835, 0.158], [0.772, 0.104], [0.108, 0.907], [0.058, 0.76], [0.926, 0.189], [0.221, 0.888], [0.957, 0.137], [0.958, 0.143], [-0.015, 0.726], [-0.066, 0.29], [0.135, 0.848], [0.085, 0.788], [0.897, 0.126], [0.749, 0.073], [0.166, 0.943], [0.871, 0.248], [0.116, 0.968], [0.116, 0.973], [0.706, 0.007], [0.288, -0.055], [0.137, 0.848], [0.089, 0.78], [0.893, 0.122], [0.749, 0.077], [0.165, 0.941], [0.877, 0.242], [0.114, 0.968], [0.12, 0.971], [0.716, 0.002], [0.292, -0.062], [0.841, 0.162], [0.774, 0.104], [0.1, 0.909], [0.054, 0.761], [0.927, 0.184], [0.227, 0.88], [0.957, 0.137], [0.961, 0.143], [-0.007, 0.716], [-0.061, 0.287], [0.13, 0.848], [0.084, 0.783], [0.898, 0.124], [0.749, 0.075], [0.169, 0.94], [0.871, 0.247], [0.114, 0.969], [0.12, 0.969], [0.717, 0.0], [0.296, -0.066], [0.84, 0.154], [0.776, 0.103], [0.104, 0.908], [0.057, 0.755], [0.925, 0.19], [0.227, 0.883], [0.958, 0.136], [0.962, 0.143], [-0.017, 0.724], [-0.067, 0.295], ]) sym_ops = [ sgtbx.rt_mx(s) for s in ("-z,-y,-x", "y,z,x", "x,y,z", "-x,-z,-y", "z,x,y", "-y,-x,-z") ] crystal_symmetry = crystal.symmetry( unit_cell=uctbx.unit_cell((98.33, 98.33, 135.99, 90, 90, 120)), space_group_info=sgtbx.space_group_info("R3:H"), ).minimum_cell() from cctbx.sgtbx.lattice_symmetry import metric_subgroups subgroups = metric_subgroups(crystal_symmetry, max_delta=5, bravais_types_only=False) cb_op_inp_min = sgtbx.change_of_basis_op() from dials.algorithms.symmetry.cosym import SymmetryAnalysis analysis = SymmetryAnalysis(coords, sym_ops, subgroups, cb_op_inp_min) assert analysis.best_solution.likelihood > 0.99 assert analysis.best_solution.confidence > 0.98 assert (analysis.best_solution.subgroup["best_subsym"].space_group().type( ).number() == 148) # R -3 :H assert (str(analysis) == """\ Scoring individual symmetry elements +--------------+--------+------+-----+-----------------+ | likelihood | Z-CC | CC | | Operator | |--------------+--------+------+-----+-----------------| | 0.087 | 1.96 | 0.2 | | 2 |(0, -1, 1) | | 0.087 | 1.96 | 0.2 | | 2 |(-1, 0, 1) | | 0.949 | 10 | 1 | *** | 3^-1 |(1, 1, 1) | | 0.087 | 1.96 | 0.2 | | 2 |(-1, 1, 0) | | 0.949 | 10 | 1 | *** | 3 |(1, 1, 1) | +--------------+--------+------+-----+-----------------+ Scoring all possible sub-groups +-------------------+-----+--------------+----------+--------+--------+---------+--------------------+ | Patterson group | | Likelihood | NetZcc | Zcc+ | Zcc- | delta | Reindex operator | |-------------------+-----+--------------+----------+--------+--------+---------+--------------------| | R -3 :H | *** | 0.995 | 8.04 | 10 | 1.96 | 0 | b-c,-a+c,a+b+c | | P -1 | | 0.003 | -6.5 | 0 | 6.5 | 0 | a,b,c | | R -3 m :H | | 0.001 | 6.5 | 6.5 | 0 | 0 | b-c,-a+c,a+b+c | | C 1 2/m 1 | | 0 | -5.24 | 1.96 | 7.21 | 0 | -a-b,a-b,c | | C 1 2/m 1 | | 0 | -5.24 | 1.96 | 7.21 | 0 | -b-c,b-c,a | | C 1 2/m 1 | | 0 | -5.24 | 1.96 | 7.21 | 0 | -a-c,-a+c,b | +-------------------+-----+--------------+----------+--------+--------+---------+--------------------+ Best solution: R -3 :H Unit cell: (98.33, 98.33, 135.99, 90, 90, 120) Reindex operator: b-c,-a+c,a+b+c Laue group probability: 0.995 Laue group confidence: 0.994""") d = analysis.as_dict() assert d["sym_op_scores"][0] == { "cc": pytest.approx(0.19620531091685714), "operator": "-x,-z,-y", "likelihood": pytest.approx(0.08665625555575088), "stars": "", "z_cc": pytest.approx(1.9620531091685713), } assert d["subgroup_scores"][0] == { "confidence": pytest.approx(0.9940687431995551), "z_cc_for": pytest.approx(9.999725360190128), "stars": "***", "patterson_group": "-R 3", "max_angular_difference": 0.0, "likelihood": pytest.approx(0.995493024305035), "cb_op": "-1/3*x+2/3*y-1/3*z,-2/3*x+1/3*y+1/3*z,1/3*x+1/3*y+1/3*z", "z_cc_against": pytest.approx(1.9620621986200772), "unit_cell": pytest.approx(( 98.32999999999998, 98.32999999999998, 135.99, 90.0, 90.0, 119.99999999999999, )), "z_cc_net": pytest.approx(8.037663161570052), }
def tst_find_best_cell(): uc_array = [ uctbx.unit_cell('40, 50, 60, 90, 90, 90'), uctbx.unit_cell('40, 60, 50, 90, 90, 90'), uctbx.unit_cell('50, 40, 60, 90, 90, 90'), uctbx.unit_cell('50, 60, 40, 90, 90, 90'), uctbx.unit_cell('60, 40, 50, 90, 90, 90'), uctbx.unit_cell('60, 50, 40, 90, 90, 90') ] uc_correct = [ uctbx.unit_cell('40, 50, 60, 90, 90, 90'), uctbx.unit_cell('40, 60, 50, 90, 90, 90'), uctbx.unit_cell('40, 50, 60, 90, 90, 90'), uctbx.unit_cell('50, 60, 40, 90, 90, 90'), uctbx.unit_cell('40, 60, 50, 90, 90, 90'), uctbx.unit_cell('50, 60, 40, 90, 90, 90') ] sg_info = sgtbx.space_group_info('P 21 21 2') sg_info_2 = sgtbx.space_group_info('I 21 21 21') sg = sg_info.group() sg_2 = sg_info_2.group() for uc, correct in zip(uc_array, uc_correct): best_cell_finder = fbc(uc, sg) assert approx_equal(correct.parameters(), best_cell_finder.return_best_cell().parameters()) cb_op = best_cell_finder.return_change_of_basis_op_to_best_cell() xs = crystal.symmetry(uc, space_group=sg) assert approx_equal(correct.parameters(), xs.change_basis(cb_op).unit_cell().parameters()) best_cell_finder = fbc(uc, sg_2) assert approx_equal(uc_array[0].parameters(), best_cell_finder.return_best_cell().parameters()) xs = crystal.symmetry(uc, space_group=sg_2) cb_op = best_cell_finder.return_change_of_basis_op_to_best_cell() assert approx_equal(uc_array[0].parameters(), xs.change_basis(cb_op).unit_cell().parameters()) # test with incomming sg not in reference setting uc = uctbx.unit_cell('60, 40, 30, 90, 90, 90') sg_info_3 = sgtbx.space_group_info('P 1 1 21') sg_3 = sg_info_3.group() best_cell_finder = fbc(uc, sg_3) xs_best = best_cell_finder.return_best_xs() uc_correct = uctbx.unit_cell('40, 30, 60, 90, 90, 90') sg_correct = sgtbx.space_group_info('P 1 21 1').group() assert approx_equal(xs_best.unit_cell().parameters(), uc_correct.parameters()) assert sg_correct == xs_best.space_group()
def exercise_load_unmerged(): flex.set_random_seed(123456) random.seed(123456) base = "tst_load_unmerged" pdb_in = iotbx.pdb.hierarchy.input(pdb_string=model_1yjp) xrs = pdb_in.xray_structure_simple() xrs.set_inelastic_form_factors(photon=1.54, table="sasaki") fc = abs(xrs.structure_factors(d_min=1.5).f_calc()).average_bijvoet_mates() fc.set_observation_type_xray_amplitude() flags = fc.generate_r_free_flags() mtz = fc.as_mtz_dataset(column_root_label="F") mtz.add_miller_array(flags, column_root_label="FreeR_flag") mtz.mtz_object().write(base + ".mtz") xrs_p1 = xrs.expand_to_p1() xrs_p1.shake_sites_in_place(rms_difference=0.1) fc_p1 = xrs_p1.structure_factors(d_min=1.4).f_calc() fc_p1_extra = fc_p1.randomize_amplitude_and_phase(amplitude_error=1.0, phase_error_deg=0, random_seed=123456) fc_p1 = abs( fc_p1.concatenate(other=fc_p1_extra)).sort(by_value="packed_indices") fc_p1.set_observation_type_xray_amplitude() sg_p2 = sgtbx.space_group_info("P2") ic = fc_p1.f_as_f_sq().customized_copy(space_group_info=sg_p2, sigmas=flex.double( fc_p1.size(), 10.0)) ic.export_as_scalepack_unmerged(file_name=base + ".sca") open(base + ".pdb", "w").write(model_1yjp) args = [ base + ".mtz", base + ".pdb", "unmerged_data=%s.sca" % base, ] master_phil = mmtbx.command_line.generate_master_phil_with_inputs( phil_string="", enable_unmerged_data=True) cmdline = mmtbx.command_line.load_model_and_data( args=[base + ext for ext in [ ".pdb", ".mtz", ]] + ["unmerged_data.file_name=%s.sca" % base], master_phil=master_phil, out=StringIO(), create_fmodel=False, process_pdb_file=False, create_log_buffer=True) # now with .sca in P1 (raises Sorry) ic2 = fc_p1.f_as_f_sq().customized_copy( sigmas=flex.double(fc_p1.size(), 10.0)) ic2.export_as_scalepack_unmerged(file_name=base + "_p1.sca") args = [ base + ".mtz", base + ".pdb", "unmerged_data=%s_p1.sca" % base, ] try: cmdline = mmtbx.command_line.load_model_and_data( args=[base + ext for ext in [ ".pdb", ".mtz", ]] + ["unmerged_data.file_name=%s_p1.sca" % base], master_phil=master_phil, out=StringIO(), create_fmodel=False, process_pdb_file=False, create_log_buffer=True) except Sorry as s: assert ( str(s) == "Incompatible space groups in merged and unmerged data:P 1 21 1 versus P 1" ), s else: raise Exception_expected # XXX f = open(base + ".cif", "w") ic.as_cif_simple(array_type="meas", out=f) f.close() args = [ base + ".mtz", base + ".pdb", "unmerged_data=%s.cif" % base, ] cmdline = mmtbx.command_line.load_model_and_data( args=[base + ext for ext in [ ".pdb", ".mtz", ]] + ["unmerged_data.file_name=%s.cif" % base], master_phil=master_phil, out=StringIO(), create_fmodel=False, process_pdb_file=False, create_log_buffer=True) # bad unit cell uc2 = uctbx.unit_cell((23, 6.5, 23.5, 90, 108, 90)) ic3 = ic.customized_copy(unit_cell=uc2) f = open(base + "_new_uc.cif", "w") ic3.as_cif_simple(array_type="meas", out=f) f.close() args = [ base + ".mtz", base + ".pdb", "unmerged_data=%s_new_uc.cif" % base, ] try: cmdline = mmtbx.command_line.load_model_and_data( args=[base + ext for ext in [ ".pdb", ".mtz", ]] + ["unmerged_data.file_name=%s_new_uc.cif" % base], master_phil=master_phil, out=StringIO(), create_fmodel=False, process_pdb_file=False, create_log_buffer=True) except Sorry as s: assert ("Incompatible symmetry definitions" in str(s)), s else: raise Exception_expected
def make_prime_input(self, filename='prime.phil'): """ Imports default PRIME input parameters, modifies correct entries and prints out a starting PHIL file to be used with PRIME """ img_pickle = self.final_objects[0].final['img'] pixel_size = pickle.load(open(img_pickle, "rb"))['PIXEL_SIZE'] triclinic = ['P1'] monoclinic = ['C2', 'P2'] orthorhombic = ['P222', 'C222', 'I222', 'F222'] tetragonal = ['I4', 'I422', 'P4', 'P422'] hexagonal = ['P3', 'P312', 'P321', 'P6', 'P622'] rhombohedral = ['R3', 'R32'] cubic = ['F23', 'F432', 'I23', 'I432', 'P23', 'P432'] sg = self.cons_pg.replace(" ", "") uc = ['{:4.2f}'.format(i) for i in self.cons_uc] if sg in triclinic: crystal_system = 'Triclinic' elif sg in monoclinic: crystal_system = 'Monoclinic' elif sg in orthorhombic: crystal_system = 'Orthorhombic' elif sg in tetragonal: crystal_system = 'Tetragonal' elif sg in hexagonal: crystal_system = 'Hexagonal' elif sg in rhombohedral: crystal_system = 'Rhombohedral' elif sg in cubic: crystal_system = 'Cubic' else: crystal_system = 'None' prime_params = mod_input.master_phil.extract() prime_params.data = [self.prime_data_path] prime_params.run_no = os.path.join(os.path.dirname(self.prime_data_path), '001') prime_params.title = 'Auto-generated by IOTA v{} on {}'.format(self.ver, self.now) prime_params.scale.d_min = np.mean(self.hres) prime_params.postref.scale.d_min = np.mean(self.hres) prime_params.postref.crystal_orientation.d_min = np.mean(self.hres) prime_params.postref.reflecting_range.d_min = np.mean(self.hres) prime_params.postref.unit_cell.d_min = np.mean(self.hres) prime_params.postref.allparams.d_min = np.mean(self.hres) prime_params.merge.d_min = np.mean(self.hres) prime_params.target_unit_cell = unit_cell(self.cons_uc) prime_params.target_space_group = sg prime_params.target_crystal_system = crystal_system prime_params.pixel_size_mm = pixel_size prime_phil = mod_input.master_phil.format(python_object=prime_params) with Capturing() as output: prime_phil.show() txt_out = '' for one_output in output: txt_out += one_output + '\n' prime_file = os.path.join(self.output_dir, filename) with open(prime_file, 'w') as pf: pf.write(txt_out) return prime_phil