def test_MosaicCrystalKabsch2010(): mosaic_model = MosaicCrystalKabsch2010( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) mosaic_model2 = MosaicCrystalKabsch2010( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) assert (str(mosaic_model).replace("-0.0000", " 0.0000") == """\ Crystal: Unit cell: 10.000, 11.000, 12.000, 90.000, 90.000, 90.000 Space group: P 1 U matrix: {{ 1.0000, 0.0000, 0.0000}, { 0.0000, 1.0000, 0.0000}, { 0.0000, 0.0000, 1.0000}} B matrix: {{ 0.1000, 0.0000, 0.0000}, { 0.0000, 0.0909, 0.0000}, { 0.0000, 0.0000, 0.0833}} A = UB: {{ 0.1000, 0.0000, 0.0000}, { 0.0000, 0.0909, 0.0000}, { 0.0000, 0.0000, 0.0833}} Mosaicity: 0.000000""") assert approx_equal(mosaic_model.get_mosaicity(), 0) assert mosaic_model == mosaic_model2 mosaic_model2.set_mosaicity(0.01) assert mosaic_model != mosaic_model2 # lgtm
def test_similarity(): model_1 = MosaicCrystalKabsch2010( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) model_1.set_mosaicity(0.5) model_2 = MosaicCrystalKabsch2010( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) model_2.set_mosaicity(0.5) assert model_1.is_similar_to(model_2) model_1.set_mosaicity(-1) model_2.set_mosaicity(-0.5) assert model_1.is_similar_to(model_2) # test ignores negative mosaicity model_1.set_mosaicity(0.5) model_2.set_mosaicity(0.63) # outside tolerance assert not model_1.is_similar_to(model_2) model_2.set_mosaicity(0.62) # just inside tolerance # orientation tests R = matrix.sqr(model_2.get_U()) dr1 = matrix.col((1, 0, 0)).axis_and_angle_as_r3_rotation_matrix(0.0101, deg=True) dr2 = matrix.col((1, 0, 0)).axis_and_angle_as_r3_rotation_matrix(0.0099, deg=True) model_2.set_U(dr1 * R) assert not model_1.is_similar_to(model_2) # outside tolerance model_2.set_U(dr2 * R) assert model_1.is_similar_to(model_2) # inside tolerance
def test_MosaicCrystalKabsch2010(): mosaic_model = MosaicCrystalKabsch2010( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) mosaic_model2 = MosaicCrystalKabsch2010( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) assert approx_equal(mosaic_model.get_mosaicity(), 0) assert mosaic_model == mosaic_model2 mosaic_model2.set_mosaicity(0.01) assert mosaic_model != mosaic_model2 # lgtm # FIXME Crystal == MosaicCrystal gives unexpected result, depending on # parameter order # model4 = Crystal(real_space_a=(10,0,0), # real_space_b=(0,11,0), # real_space_c=(0,0,12), # space_group_symbol="P 1") # print "model4 == mosaic_model", model4 == mosaic_model # True # print "mosaic_model == model4", mosaic_model == model4 # False # print "mosaic_model2 == model4", mosaic_model2 == model4 # False # print "model4 == mosaic_model2", model4 == mosaic_model2 # True mosaic_model = MosaicCrystalSauter2014( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) mosaic_model2 = MosaicCrystalSauter2014( real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1", ) assert approx_equal(mosaic_model.get_half_mosaicity_deg(), 0) assert approx_equal(mosaic_model.get_domain_size_ang(), 0) assert mosaic_model == mosaic_model2 mosaic_model2.set_half_mosaicity_deg(0.01) assert mosaic_model != mosaic_model2 # lgtm mosaic_model2.set_half_mosaicity_deg(0) assert mosaic_model == mosaic_model2 # lgtm mosaic_model2.set_domain_size_ang(1000) assert mosaic_model != mosaic_model2 # lgtm
def from_dict(d, t=None): ''' Convert the dictionary to a crystal model Params: d The dictionary of parameters t The template dictionary to use Returns: The crystal model ''' # If None, return None if d == None: if t == None: return None else: return from_dict(t, None) elif t != None: d = dict(t.items() + d.items()) # Create the model from the dictionary if 'ML_half_mosaicity_deg' in d: assert 'ML_domain_size_ang' in d if d['ML_half_mosaicity_deg'] is None or d['ML_domain_size_ang'] is None: assert d['ML_half_mosaicity_deg'] is None and d['ML_domain_size_ang'] is None else: if 'mosaicity' in d and d['mosaicity'] > 0: print "Warning, two kinds of mosaicity found. Using Sauter2014 model" from dxtbx.model import MosaicCrystalSauter2014 return MosaicCrystalSauter2014.from_dict(d) if 'mosaicity' in d: from dxtbx.model import MosaicCrystalKabsch2010 return MosaicCrystalKabsch2010.from_dict(d) else: from dxtbx.model import Crystal return Crystal.from_dict(d)
def expt_crystal_maker(self): """Construct the crystal object for the experiments file.""" a, b, c = self.ucell.parameters()[0:3] direct_matrix = self.ori.direct_matrix() real_a = direct_matrix[0:3] real_b = direct_matrix[3:6] real_c = direct_matrix[6:9] lattice = self.ucell.lattice_symmetry_group() found_it = False if 'ML_half_mosaicity_deg' in self.data: assert 'ML_domain_size_ang' in self.data if d['ML_half_mosaicity_deg'][0] is None or d['ML_domain_size_ang'][0] is None: assert d['ML_half_mosaicity_deg'][0] is None and d['ML_domain_size_ang'][0] is None else: found_it = True if 'mosaicity' in self.data and self.data['mosaicity'] > 0: print "Warning, two kinds of mosaicity found. Using Sauter2014 model" from dxtbx.model import MosaicCrystalSauter2014 self.crystal = MosaicCrystalSauter2014(real_a, real_b, real_c, space_group=lattice) self.crystal.set_half_mosaicity_deg(self.data['ML_half_mosaicity_deg'][0]) self.crystal.set_domain_size_ang(self.data['ML_domain_size_ang'][0]) if not found_it: if 'mosaicity' in self.data: from dxtbx.model import MosaicCrystalKabsch2010 self.crystal = MosaicCrystalKabsch2010(real_a, real_b, real_c, space_group=lattice) self.crystal.set_mosaicity(self.data['mosaicity']) else: from dxtbx.model import Crystal self.crystal = Crystal(real_a, real_b, real_c, space_group=lattice) if 'identified_isoform' in self.data.keys() and self.data['identified_isoform'] is not None: self.crystal.identified_isoform = self.data['identified_isoform']
def test_crystal(mosaic): from dxtbx.model import CrystalFactory from scitbx import matrix real_space_a = matrix.col((35.2402102454, -7.60002142787, 22.080026774)) real_space_b = matrix.col((22.659572494, 1.47163505925, -35.6586361881)) real_space_c = matrix.col((5.29417246554, 38.9981792999, 4.97368666613)) if mosaic: from dxtbx.model import MosaicCrystalKabsch2010 c1 = MosaicCrystalKabsch2010( real_space_a=real_space_a, real_space_b=real_space_b, real_space_c=real_space_c, space_group_symbol="P 1 2/m 1") c1.set_mosaicity(0.1) else: from dxtbx.model import Crystal c1 = Crystal( real_space_a=real_space_a, real_space_b=real_space_b, real_space_c=real_space_c, space_group_symbol="P 1 2/m 1") d = c1.to_dict() c2 = CrystalFactory.from_dict(d) eps = 1e-7 assert abs(matrix.col(d['real_space_a']) - real_space_a) <= eps assert abs(matrix.col(d['real_space_b']) - real_space_b) <= eps assert abs(matrix.col(d['real_space_c']) - real_space_c) <= eps assert d['space_group_hall_symbol'] == "-P 2y" if mosaic: assert d['mosaicity'] == 0.1 assert c1 == c2
def test_mosaic_crystal(example_crystal): c1 = MosaicCrystalKabsch2010(**example_crystal) c1.set_mosaicity(0.1) d = c1.to_dict() c2 = CrystalFactory.from_dict(d) for direction in ("real_space_a", "real_space_b", "real_space_c"): assert abs(matrix.col(d[direction]) - example_crystal[direction]) <= 1e-7 assert d["space_group_hall_symbol"] == "-P 2y" assert d["mosaicity"] == 0.1 assert c1 == c2
def to_crystal(filename): """Get the crystal model from the xparm file Params: filename The xparm/or integrate filename Return: The crystal model """ from rstbx.cftbx.coordinate_frame_converter import coordinate_frame_converter from cctbx.sgtbx import space_group, space_group_symbols # Get the real space coordinate frame cfc = coordinate_frame_converter(filename) real_space_a = cfc.get("real_space_a") real_space_b = cfc.get("real_space_b") real_space_c = cfc.get("real_space_c") sg = cfc.get("space_group_number") space_group = space_group(space_group_symbols(sg).hall()) mosaicity = cfc.get("mosaicity") # Return the crystal model if mosaicity is None: from dxtbx.model import Crystal crystal = Crystal( real_space_a=real_space_a, real_space_b=real_space_b, real_space_c=real_space_c, space_group=space_group, ) else: from dxtbx.model import MosaicCrystalKabsch2010 crystal = MosaicCrystalKabsch2010( real_space_a=real_space_a, real_space_b=real_space_b, real_space_c=real_space_c, space_group=space_group, ) crystal.set_mosaicity(mosaicity) return crystal
def prepare_dxtbx_models(self, setting_specific_ai, sg, isoform=None): from dxtbx.model import BeamFactory beam = BeamFactory.simple(wavelength=self.inputai.wavelength) from dxtbx.model import DetectorFactory detector = DetectorFactory.simple( sensor=DetectorFactory.sensor("PAD"), distance=setting_specific_ai.distance(), beam_centre=[ setting_specific_ai.xbeam(), setting_specific_ai.ybeam() ], fast_direction="+x", slow_direction="+y", pixel_size=[self.pixel_size, self.pixel_size], image_size=[self.inputpd['size1'], self.inputpd['size1']], ) direct = matrix.sqr( setting_specific_ai.getOrientation().direct_matrix()) from dxtbx.model import MosaicCrystalKabsch2010 crystal = MosaicCrystalKabsch2010( real_space_a=matrix.row(direct[0:3]), real_space_b=matrix.row(direct[3:6]), real_space_c=matrix.row(direct[6:9]), space_group_symbol=sg, ) crystal.set_mosaicity(setting_specific_ai.getMosaicity()) if isoform is not None: newB = matrix.sqr(isoform.fractionalization_matrix()).transpose() crystal.set_B(newB) from dxtbx.model import Experiment, ExperimentList experiments = ExperimentList() experiments.append( Experiment(beam=beam, detector=detector, crystal=crystal)) print(beam) print(detector) print(crystal) return experiments
def from_dict(d, t=None): """Convert the dictionary to a crystal model Params: d The dictionary of parameters t The template dictionary to use Returns: The crystal model """ if d is None and t is None: return None joint = t.copy() if t else {} joint.update(d) # Create the model from the dictionary if "ML_half_mosaicity_deg" in joint: assert "ML_domain_size_ang" in joint if (joint["ML_half_mosaicity_deg"] is None or joint["ML_domain_size_ang"] is None): assert (joint["ML_half_mosaicity_deg"] is None and joint["ML_domain_size_ang"] is None) else: if joint.get("mosaicity", 0) > 0: print( "Warning, two kinds of mosaicity found. Using Sauter2014 model" ) from dxtbx.model import MosaicCrystalSauter2014 return MosaicCrystalSauter2014.from_dict(joint) if "mosaicity" in joint: from dxtbx.model import MosaicCrystalKabsch2010 return MosaicCrystalKabsch2010.from_dict(joint) else: from dxtbx.model import Crystal return Crystal.from_dict(joint)
def test_crystal_model(): real_space_a = matrix.col((10, 0, 0)) real_space_b = matrix.col((0, 11, 0)) real_space_c = matrix.col((0, 0, 12)) model = Crystal(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1") # This doesn't work as python class uctbx.unit_cell(uctbx_ext.unit_cell) # so C++ and python classes are different types #assert isinstance(model.get_unit_cell(), uctbx.unit_cell) assert model.get_unit_cell().parameters() == (10.0, 11.0, 12.0, 90.0, 90.0, 90.0) assert approx_equal(model.get_A(), (1 / 10, 0, 0, 0, 1 / 11, 0, 0, 0, 1 / 12)) assert approx_equal( matrix.sqr(model.get_A()).inverse(), (10, 0, 0, 0, 11, 0, 0, 0, 12)) assert approx_equal(model.get_B(), model.get_A()) assert approx_equal(model.get_U(), (1, 0, 0, 0, 1, 0, 0, 0, 1)) assert approx_equal(model.get_real_space_vectors(), (real_space_a, real_space_b, real_space_c)) assert model.get_crystal_symmetry().unit_cell().parameters() == \ model.get_unit_cell().parameters() assert model.get_crystal_symmetry().space_group() == model.get_space_group( ) model2 = Crystal(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1") assert model == model2 and not (model != model2) model2a = Crystal(model.get_A(), model.get_space_group()) assert model == model2a and not (model != model2a) model2b = Crystal(matrix.sqr(model.get_A()).inverse().elems, model.get_space_group().type().lookup_symbol(), reciprocal=False) assert model == model2b and not (model != model2b) # rotate 45 degrees about x-axis R1 = matrix.sqr( (1, 0, 0, 0, math.cos(math.pi / 4), -math.sin(math.pi / 4), 0, math.sin(math.pi / 4), math.cos(math.pi / 4))) # rotate 30 degrees about y-axis R2 = matrix.sqr((math.cos(math.pi / 6), 0, math.sin(math.pi / 6), 0, 1, 0, -math.sin(math.pi / 6), 0, math.cos(math.pi / 6))) # rotate 60 degrees about z-axis R3 = matrix.sqr((math.cos(math.pi / 3), -math.sin(math.pi / 3), 0, math.sin(math.pi / 3), math.cos(math.pi / 3), 0, 0, 0, 1)) R = R1 * R2 * R3 model.set_U(R) # B is unchanged assert approx_equal(model.get_B(), (1 / 10, 0, 0, 0, 1 / 11, 0, 0, 0, 1 / 12)) assert approx_equal(model.get_U(), R) assert approx_equal(model.get_A(), matrix.sqr(model.get_U()) * matrix.sqr(model.get_B())) a_, b_, c_ = model.get_real_space_vectors() assert approx_equal(a_, R * real_space_a) assert approx_equal(b_, R * real_space_b) assert approx_equal(c_, R * real_space_c) assert str(model).replace("-0.0000", " 0.0000") == """\ Crystal: Unit cell: (10.000, 11.000, 12.000, 90.000, 90.000, 90.000) Space group: P 1 U matrix: {{ 0.4330, -0.7500, 0.5000}, { 0.7891, 0.0474, -0.6124}, { 0.4356, 0.6597, 0.6124}} B matrix: {{ 0.1000, 0.0000, 0.0000}, { 0.0000, 0.0909, 0.0000}, { 0.0000, 0.0000, 0.0833}} A = UB: {{ 0.0433, -0.0682, 0.0417}, { 0.0789, 0.0043, -0.0510}, { 0.0436, 0.0600, 0.0510}} """ model.set_B((1 / 12, 0, 0, 0, 1 / 12, 0, 0, 0, 1 / 12)) assert approx_equal(model.get_unit_cell().parameters(), (12, 12, 12, 90, 90, 90)) U = matrix.sqr((0.3455, -0.2589, -0.9020, 0.8914, 0.3909, 0.2293, 0.2933, -0.8833, 0.3658)) B = matrix.sqr((1 / 13, 0, 0, 0, 1 / 13, 0, 0, 0, 1 / 13)) model.set_A(U * B) assert approx_equal(model.get_A(), U * B) assert approx_equal(model.get_U(), U, 1e-4) assert approx_equal(model.get_B(), B, 1e-5) model3 = Crystal(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group=sgtbx.space_group_info("P 222").group()) assert model3.get_space_group().type().hall_symbol() == " P 2 2" assert model != model3 # sgi_ref = sgtbx.space_group_info(number=230) model_ref = Crystal(real_space_a=(44, 0, 0), real_space_b=(0, 44, 0), real_space_c=(0, 0, 44), space_group=sgi_ref.group()) assert approx_equal(model_ref.get_U(), (1, 0, 0, 0, 1, 0, 0, 0, 1)) assert approx_equal(model_ref.get_B(), (1 / 44, 0, 0, 0, 1 / 44, 0, 0, 0, 1 / 44)) assert approx_equal(model_ref.get_A(), model_ref.get_B()) assert approx_equal(model_ref.get_unit_cell().parameters(), (44, 44, 44, 90, 90, 90)) a_ref, b_ref, c_ref = map(matrix.col, model_ref.get_real_space_vectors()) cb_op_to_primitive = sgi_ref.change_of_basis_op_to_primitive_setting() model_primitive = model_ref.change_basis(cb_op_to_primitive) cb_op_to_reference = model_primitive.get_space_group().info()\ .change_of_basis_op_to_reference_setting() a_prim, b_prim, c_prim = map(matrix.col, model_primitive.get_real_space_vectors()) #print cb_op_to_primitive.as_abc() ##'-1/2*a+1/2*b+1/2*c,1/2*a-1/2*b+1/2*c,1/2*a+1/2*b-1/2*c' assert approx_equal(a_prim, -1 / 2 * a_ref + 1 / 2 * b_ref + 1 / 2 * c_ref) assert approx_equal(b_prim, 1 / 2 * a_ref - 1 / 2 * b_ref + 1 / 2 * c_ref) assert approx_equal(c_prim, 1 / 2 * a_ref + 1 / 2 * b_ref - 1 / 2 * c_ref) #print cb_op_to_reference.as_abc() ##b+c,a+c,a+b assert approx_equal(a_ref, b_prim + c_prim) assert approx_equal(b_ref, a_prim + c_prim) assert approx_equal(c_ref, a_prim + b_prim) assert approx_equal(model_primitive.get_U(), [ -0.5773502691896258, 0.40824829046386285, 0.7071067811865476, 0.5773502691896257, -0.4082482904638631, 0.7071067811865476, 0.5773502691896257, 0.8164965809277259, 0.0 ]) assert approx_equal(model_primitive.get_B(), [ 0.0262431940540739, 0.0, 0.0, 0.00927837023781507, 0.02783511071344521, 0.0, 0.01607060866333063, 0.01607060866333063, 0.03214121732666125 ]) assert approx_equal( model_primitive.get_A(), (0, 1 / 44, 1 / 44, 1 / 44, 0, 1 / 44, 1 / 44, 1 / 44, 0)) assert approx_equal(model_primitive.get_unit_cell().parameters(), [ 38.1051177665153, 38.1051177665153, 38.1051177665153, 109.47122063449069, 109.47122063449069, 109.47122063449069 ]) assert model_ref != model_primitive model_ref_recycled = model_primitive.change_basis(cb_op_to_reference) assert approx_equal(model_ref.get_U(), model_ref_recycled.get_U()) assert approx_equal(model_ref.get_B(), model_ref_recycled.get_B()) assert approx_equal(model_ref.get_A(), model_ref_recycled.get_A()) assert approx_equal(model_ref.get_unit_cell().parameters(), model_ref_recycled.get_unit_cell().parameters()) assert model_ref == model_ref_recycled # uc = uctbx.unit_cell( (58.2567, 58.1264, 39.7093, 46.9077, 46.8612, 62.1055)) sg = sgtbx.space_group_info(symbol="P1").group() cs = crystal.symmetry(unit_cell=uc, space_group=sg) cb_op_to_minimum = cs.change_of_basis_op_to_minimum_cell() # the reciprocal matrix B = matrix.sqr(uc.fractionalization_matrix()).transpose() U = random_rotation() direct_matrix = (U * B).inverse() model = Crystal(direct_matrix[:3], direct_matrix[3:6], direct_matrix[6:9], space_group=sg) assert uc.is_similar_to(model.get_unit_cell()) uc_minimum = uc.change_basis(cb_op_to_minimum) model_minimum = model.change_basis(cb_op_to_minimum) assert uc_minimum.is_similar_to(model_minimum.get_unit_cell()) assert model_minimum != model model_minimum.update(model) assert model_minimum == model # from scitbx.math import euler_angles A_static = matrix.sqr(model.get_A()) A_as_scan_points = [A_static] num_scan_points = 11 for i in range(num_scan_points - 1): A_as_scan_points.append( A_as_scan_points[-1] * matrix.sqr(euler_angles.xyz_matrix(0.1, 0.2, 0.3))) model.set_A_at_scan_points(A_as_scan_points) model_minimum = model.change_basis(cb_op_to_minimum) assert model.num_scan_points == model_minimum.num_scan_points == num_scan_points M = matrix.sqr(cb_op_to_minimum.c_inv().r().transpose().as_double()) M_inv = M.inverse() for i in range(num_scan_points): A_orig = matrix.sqr(model.get_A_at_scan_point(i)) A_min = matrix.sqr(model_minimum.get_A_at_scan_point(i)) assert approx_equal(A_min, A_orig * M_inv) mosaic_model = MosaicCrystalKabsch2010(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1") mosaic_model2 = MosaicCrystalKabsch2010(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1") assert approx_equal(mosaic_model.get_mosaicity(), 0) assert mosaic_model == mosaic_model2 mosaic_model2.set_mosaicity(0.01) assert mosaic_model != mosaic_model2 # FIXME Crystal == MosaicCrystal gives unexpected result, depending on # parameter order # model4 = Crystal(real_space_a=(10,0,0), # real_space_b=(0,11,0), # real_space_c=(0,0,12), # space_group_symbol="P 1") # print "model4 == mosaic_model", model4 == mosaic_model # True # print "mosaic_model == model4", mosaic_model == model4 # False # print "mosaic_model2 == model4", mosaic_model2 == model4 # False # print "model4 == mosaic_model2", model4 == mosaic_model2 # True mosaic_model = MosaicCrystalSauter2014(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1") mosaic_model2 = MosaicCrystalSauter2014(real_space_a=(10, 0, 0), real_space_b=(0, 11, 0), real_space_c=(0, 0, 12), space_group_symbol="P 1") assert approx_equal(mosaic_model.get_half_mosaicity_deg(), 0) assert approx_equal(mosaic_model.get_domain_size_ang(), 0) assert mosaic_model == mosaic_model2 mosaic_model2.set_half_mosaicity_deg(0.01) assert mosaic_model != mosaic_model2 mosaic_model2.set_half_mosaicity_deg(0) assert mosaic_model == mosaic_model2 mosaic_model2.set_domain_size_ang(1000) assert mosaic_model != mosaic_model2