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 dxtbx.model import Crystal 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 crystal = Crystal(real_space_a=real_space_a, real_space_b=real_space_b, real_space_c=real_space_c, space_group=space_group) if (mosaicity is not None): crystal.set_mosaicity(mosaicity) return crystal
def tst_crystal(self): from dxtbx.model import Crystal, 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)) 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") c1.set_mosaicity(0.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") assert (d['mosaicity'] == 0.1) assert (c1 == c2) print 'OK'
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 Crystal crystal = Crystal( 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
class construct_reflection_table_and_experiment_list(object): def __init__(self, pickle, img_location, pixel_size, proceed_without_image=False): # unpickle pickle file and keep track of image location if img_location is None: if proceed_without_image: self.img_location = [] else: raise Sorry, "No image found at specified location. Override by setting proceed_without_image to False" \ + "to produce experiment lists that may only be read when check_format is False." else: self.img_location = [img_location] # pickle can be already unpickled, if so, loading it will fail with an AttributeError. A load # error will fail with EOFError try: self.data = easy_pickle.load(pickle) except EOFError: self.data = None self.pickle = None except AttributeError: self.data = pickle self.pickle = None else: self.pickle = pickle if self.data is not None: self.length = len(self.data['observations'][0].data()) self.pixel_size = pixel_size # extract things from pickle file def unpack_pickle(self): """Extract all relevant information from an integration pickle file.""" # crystal-dependent self.ori = self.data['current_orientation'][0] self.ucell = self.data['current_orientation'][0].unit_cell() # experiment-dependent self.wavelength = self.data['wavelength'] self.det_dist = self.data['distance'] # observation-dependent self.observations = self.data['observations'][0] self.predictions = self.data['mapped_predictions'][0] if 'fuller_kapton_absorption_correction' in self.data: self.fuller_correction = self.data[ 'fuller_kapton_absorption_correction'][0] if 'fuller_kapton_absorption_correction_sigmas' in self.data: self.fuller_correction_sigmas = self.data[ 'fuller_kapton_absorption_correction_sigmas'][0] # construct the experiments and experiment_list objects def expt_beam_maker(self): """Construct the beam object for the experiments file.""" self.beam = BeamFactory.simple(self.wavelength) 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() self.crystal = Crystal(real_a, real_b, real_c, space_group=lattice) self.crystal.set_mosaicity(self.data['mosaicity']) self.crystal._ML_half_mosaicity_deg = self.data[ 'ML_half_mosaicity_deg'][0] self.crystal._ML_domain_size_ang = self.data['ML_domain_size_ang'][0] if 'identified_isoform' in self.data.keys( ) and self.data['identified_isoform'] is not None: self.crystal.identified_isoform = self.data['identified_isoform'] def expt_detector_maker(self): """Construct the detector object for the experiments file. This function generates a monolithic flattening of the CSPAD detector if not supplied with an image file.""" self.distance = self.data['distance'] self.xbeam, self.ybeam = self.data['xbeam'], self.data['ybeam'] if len(self.img_location) > 0 and not dxtbx.load( self.img_location[0])._image_file.endswith("_00000.pickle"): self.detector = dxtbx.load(self.img_location[0])._detector() else: self.detector = DetectorFactory.simple( 'SENSOR_UNKNOWN', self.distance, (self.xbeam, self.ybeam), '+x', '-y', (self.pixel_size, self.pixel_size), (1765, 1765)) def expt_gonio_maker(self): """XFEL data consisting of stills is expected to have been generated by an experiment without a goniometer -- use placeholder None.""" self.goniometer = None def expt_imageset_maker(self): """Construct the imageset object for the experiments file.""" if len(self.img_location) == 0: self.imageset = None return self.filename = self.img_location self.format = FormatMultiImage.FormatMultiImage() self.reader = imageset.MultiFileReader(self.format, self.filename) self.imageset = imageset.ImageSet(self.reader) def expt_scan_maker(self): """XFEL data consisting of stills is expected not to contain scans -- use placeholder None.""" self.scan = None def assemble_experiments(self): self.unpack_pickle() self.expt_beam_maker() self.expt_crystal_maker() self.expt_detector_maker() self.expt_gonio_maker() self.expt_imageset_maker() self.expt_scan_maker() self.experiment = Experiment(beam=self.beam, crystal=self.crystal, detector=self.detector, goniometer=self.goniometer, imageset=self.imageset, scan=self.scan) self.experiment_list = ExperimentList([self.experiment]) def experiments_to_json(self, path_name=None): if path_name is None: loc = os.path.dirname(self.pickle) else: loc = path_name name = os.path.basename(self.pickle).split(".pickle")[0] expt_name = int_pickle_to_filename(name, "idx-", "_experiments.json") experiments = os.path.join(loc, expt_name) dumper = experiment_list.ExperimentListDumper(self.experiment_list) dumper.as_json(experiments) # construct the reflection table def refl_table_maker(self): self.reflections = flex.reflection_table() def refl_bkgd_maker(self): self.reflections['background.mean'] = sciflex.double(self.length) self.reflections['background.mse'] = sciflex.double(self.length) def refl_bbox_maker(self): self.reflections['bbox'] = flex.int6(self.length) def refl_correlation_maker(self): self.reflections['correlation.ideal.profile'] = sciflex.double( self.length) def refl_entering_maker(self): self.reflections['entering'] = flex.bool(self.length) def refl_flags_maker(self): self.reflections['flags'] = flex.size_t(self.length, 1) def refl_id_maker(self): self.reflections['id'] = sciflex.size_t(self.length) def refl_intensities_maker(self): self.reflections['intensity.sum.value'] = self.observations.data() self.reflections['intensity.sum.variance'] = self.observations.sigmas( )**2 def refl_lp_maker(self): self.reflections['lp'] = sciflex.double(self.length) def refl_millers_maker(self): self.reflections['miller_index'] = self.observations._indices def refl_nbackgroundforeground_maker(self): self.reflections['n_background'] = sciflex.size_t(self.length) self.reflections['n_foreground'] = sciflex.size_t(self.length) def refl_panel_maker(self): self.reflections['panel'] = sciflex.size_t(self.length, 0) def refl_profile_maker(self): self.reflections['profile.correlation'] = sciflex.double(self.length) def refl_s1_maker(self): from scitbx.matrix import col self.reflections['s1'] = sciflex.vec3_double(self.length) for idx in xrange(self.length): coords = col(self.detector[0].get_pixel_lab_coord( self.reflections['xyzobs.px.value'][idx][0:2])).normalize() self.reflections['s1'][idx] = tuple(coords) def refl_xyzcal_maker(self): self.reflections['xyzcal.px'] = sciflex.vec3_double( self.predictions.parts()[1], self.predictions.parts()[0], sciflex.double(self.length, 0.0)) self.reflections[ 'xyzcal.mm'] = self.pixel_size * self.reflections['xyzcal.px'] def refl_xyzobs_maker(self): self.reflections['xyzobs.px.value'] = sciflex.vec3_double( self.predictions.parts()[1], self.predictions.parts()[0], sciflex.double(self.length, 0.5)) self.reflections['xyzobs.px.variance'] = sciflex.vec3_double( self.length, (0.0, 0.0, 0.0)) def refl_zeta_maker(self): self.reflections['zeta'] = sciflex.double(self.length) def refl_kapton_correction_maker(self): if hasattr(self, 'fuller_correction'): self.reflections[ 'kapton_absorption_correction'] = self.fuller_correction if hasattr(self, 'fuller_correction_sigmas'): self.reflections[ 'kapton_absorption_correction_sigmas'] = self.fuller_correction_sigmas def assemble_reflections(self): self.refl_table_maker() self.refl_bkgd_maker() self.refl_bbox_maker() self.refl_correlation_maker() self.refl_entering_maker() self.refl_flags_maker() self.refl_id_maker() self.refl_intensities_maker() self.refl_millers_maker() self.refl_nbackgroundforeground_maker() self.refl_panel_maker() self.refl_xyzcal_maker() self.refl_xyzobs_maker() self.refl_zeta_maker() self.refl_kapton_correction_maker() self.refl_s1_maker( ) # depends on successful completion of refl_xyz_obs_maker def reflections_to_pickle(self, path_name=None): if path_name is None: loc = os.path.dirname(self.pickle) else: loc = path_name name = os.path.basename(self.pickle).split(".pickle")[0] refl_name = int_pickle_to_filename(name, "idx-", "_integrated.pickle") reflections = os.path.join(loc, refl_name) self.reflections.as_pickle(reflections)
def exercise_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 approx_equal(model.get_mosaicity(), 0) 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 model2.set_mosaicity(0.01) assert model != model2 # 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) s = StringIO() print >> s, model assert not show_diff( s.getvalue().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()) model3.set_mosaicity(0.01) 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)
def exercise_similarity(): model_1 = Crystal(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 = Crystal(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 # unit_cell.is_similar_to is tested elsewhere return
def from_dict(d): ''' Convert the dictionary to a crystal model Params: d The dictionary of parameters Returns: The crystal model ''' from dxtbx.model import Crystal # If None, return None if d is None: return None # Check the version and id if str(d['__id__']) != "crystal": raise ValueError("\"__id__\" does not equal \"crystal\"") # Extract from the dictionary real_space_a = d['real_space_a'] real_space_b = d['real_space_b'] real_space_c = d['real_space_c'] # str required to force unicode to ascii conversion space_group = str("Hall:" + d['space_group_hall_symbol']) xl = Crystal(real_space_a, real_space_b, real_space_c, space_group_symbol=space_group) # New parameters for maximum likelihood values try: xl._ML_half_mosaicity_deg = d['ML_half_mosaicity_deg'] except KeyError: pass try: xl._ML_domain_size_ang = d['ML_domain_size_ang'] except KeyError: pass # Isoforms used for stills try: xl.identified_isoform = d['identified_isoform'] except KeyError: pass # Extract scan point setting matrices, if present try: A_at_scan_points = d['A_at_scan_points'] xl.set_A_at_scan_points(A_at_scan_points) except KeyError: pass # Extract covariance of B, if present try: cov_B = d['B_covariance'] xl.set_B_covariance(cov_B) except KeyError: pass # Extract mosaicity, if present try: mosaicity = d['mosaicity'] xl.set_mosaicity(mosaicity) except KeyError: pass return xl