def convert_stills_to_sweep(self, imageset): from dxtbx.model import Scan assert self.params.geometry.scan.oscillation is not None for i in range(len(imageset)): self.override_beam( imageset.get_beam(index=i), self.params.geometry.beam) self.override_detector( imageset.get_detector(index=i), self.params.geometry.detector) self.override_goniometer( imageset.get_goniometer(index=i), self.params.geometry.goniometer) beam = imageset.get_beam(index=0) detector = imageset.get_detector(index=0) goniometer = imageset.get_goniometer(index=0) for i in range(1, len(imageset)): assert beam.is_similar_to( imageset.get_beam(index=i), wavelength_tolerance = self.params.input.tolerance.beam.wavelength, direction_tolerance = self.params.input.tolerance.beam.direction, polarization_normal_tolerance = self.params.input.tolerance.beam.polarization_normal, polarization_fraction_tolerance = self.params.input.tolerance.beam.polarization_fraction) assert detector.is_similar_to( imageset.get_detector(index=i), fast_axis_tolerance = self.params.input.tolerance.detector.fast_axis, slow_axis_tolerance = self.params.input.tolerance.detector.slow_axis, origin_tolerance = self.params.input.tolerance.detector.origin) assert goniometer.is_similar_to( imageset.get_goniometer(index=i), rotation_axis_tolerance = self.params.input.tolerance.goniometer.rotation_axis, fixed_rotation_tolerance = self.params.input.tolerance.goniometer.fixed_rotation, setting_rotation_tolerance = self.params.input.tolerance.goniometer.setting_rotation) assert beam is not None assert detector is not None assert goniometer is not None if self.params.geometry.scan.image_range is not None: image_range = self.params.geometry.scan.image_range else: image_range = (1, len(imageset)) oscillation = self.params.geometry.scan.oscillation scan = Scan(image_range=image_range, oscillation=oscillation) from dxtbx.sweep_filenames import template_regex from dxtbx.imageset import ImageSetFactory indices = list(range(image_range[0], image_range[1]+1)) template = template_regex(imageset.get_path(0))[0] if template is None: paths = [imageset.get_path(i) for i in range(len(imageset))] assert len(set(paths)) == 1 template = paths[0] new_sweep = ImageSetFactory.make_sweep( template = template, indices = indices, format_class = imageset.reader().get_format_class(), beam = beam, detector = detector, goniometer = goniometer, scan = scan) return new_sweep
def test_template_regex(): questions_answers = { "foo_bar_001.img": "foo_bar_###.img", "foo_bar001.img": "foo_bar###.img", "foo_bar_1.8A_001.img": "foo_bar_1.8A_###.img", "foo_bar.001": "foo_bar.###", "foo_bar_001.img1000": "foo_bar_###.img1000", "foo_bar_00001.img": "foo_bar_#####.img", } for filename in questions_answers: answer = template_regex(filename) assert answer[0] == questions_answers[filename]
def test_template_regex(): questions_answers = { 'foo_bar_001.img': 'foo_bar_###.img', 'foo_bar001.img': 'foo_bar###.img', 'foo_bar_1.8A_001.img': 'foo_bar_1.8A_###.img', 'foo_bar.001': 'foo_bar.###', 'foo_bar_001.img1000': 'foo_bar_###.img1000', 'foo_bar_00001.img': 'foo_bar_#####.img' } for filename in questions_answers: answer = template_regex(filename) assert answer[0] == questions_answers[filename]
def test_sweep(): from dxtbx.sweep_filenames import template_regex questions_answers = { 'foo_bar_001.img': 'foo_bar_###.img', 'foo_bar001.img': 'foo_bar###.img', 'foo_bar_1.8A_001.img': 'foo_bar_1.8A_###.img', 'foo_bar.001': 'foo_bar.###', 'foo_bar_001.img1000': 'foo_bar_###.img1000', 'foo_bar_00001.img': 'foo_bar_#####.img' } for filename in questions_answers: answer = template_regex(filename) assert answer[0] == questions_answers[filename]
def tst_sweep(): from dxtbx.sweep_filenames import template_regex questions_answers = { 'foo_bar_001.img':'foo_bar_###.img', 'foo_bar001.img':'foo_bar###.img', 'foo_bar_1.8A_001.img':'foo_bar_1.8A_###.img', 'foo_bar.001':'foo_bar.###', 'foo_bar_001.img1000':'foo_bar_###.img1000', 'foo_bar_00001.img':'foo_bar_#####.img' } for filename in questions_answers: answer = template_regex(filename) assert answer[0] == questions_answers[filename] print 'OK'
def get_imageset( Class, filenames, beam=None, detector=None, goniometer=None, scan=None, as_imageset=False, as_sweep=False, single_file_indices=None, format_kwargs=None, template=None, check_format=True, ): """ Factory method to create an imageset """ from dxtbx.imageset import ImageSetData from dxtbx.imageset import ImageSet from dxtbx.imageset import ImageSweep from dxtbx.sweep_filenames import template_regex from os.path import abspath # Get filename absolute paths filenames = map(abspath, filenames) # Make it a dict if format_kwargs is None: format_kwargs = {} # Get some information from the format class reader = Class.get_reader()(filenames, **format_kwargs) masker = Class.get_masker()(filenames, **format_kwargs) # Get the format instance if check_format is True: format_instance = Class(filenames[0], **format_kwargs) else: format_instance = None # Read the vendor type if check_format is True: vendor = format_instance.get_vendortype() else: vendor = "" # Get the format kwargs params = format_kwargs # Make sure only 1 or none is set assert [as_imageset, as_sweep].count(True) < 2 if as_imageset: is_sweep = False elif as_sweep: is_sweep = True else: if scan is None and format_instance is None: raise RuntimeError(""" One of the following needs to be set - as_imageset=True - as_sweep=True - scan - check_format=True """) if scan is None: test_scan = format_instance.get_scan() else: test_scan = scan if test_scan is not None and test_scan.get_oscillation()[1] != 0: is_sweep = True else: is_sweep = False # Create an imageset or sweep if not is_sweep: # Create the imageset iset = ImageSet( ImageSetData( reader=reader, masker=masker, vendor=vendor, params=params, format=Class, )) # If any are None then read from format if [beam, detector, goniometer, scan].count(None) != 0: # Get list of models beam = [] detector = [] goniometer = [] scan = [] for f in filenames: format_instance = Class(f, **format_kwargs) beam.append(format_instance.get_beam()) detector.append(format_instance.get_detector()) goniometer.append(format_instance.get_goniometer()) scan.append(format_instance.get_scan()) # Set the list of models for i in range(len(filenames)): iset.set_beam(beam[i], i) iset.set_detector(detector[i], i) iset.set_goniometer(goniometer[i], i) iset.set_scan(scan[i], i) else: # Get the template if template is None: template = template_regex(filenames[0])[0] else: template = str(template) # Check scan makes sense if scan: if check_format is True: assert scan.get_num_images() == len(filenames) # If any are None then read from format if beam is None and format_instance is not None: beam = format_instance.get_beam() if detector is None and format_instance is not None: detector = format_instance.get_detector() if goniometer is None and format_instance is not None: goniometer = format_instance.get_goniometer() if scan is None and format_instance is not None: scan = format_instance.get_scan() if scan is not None: for f in filenames[1:]: format_instance = Class(f, **format_kwargs) scan += format_instance.get_scan() assert beam is not None, "Can't create Sweep without beam" assert detector is not None, "Can't create Sweep without detector" assert goniometer is not None, "Can't create Sweep without goniometer" assert scan is not None, "Can't create Sweep without scan" # Create the sweep iset = ImageSweep( ImageSetData( reader=reader, masker=masker, vendor=vendor, params=params, format=Class, template=template, ), beam=beam, detector=detector, goniometer=goniometer, scan=scan, ) # Return the imageset return iset
def _extract_file_metadata(self, format_class, filenames, compare_beam=None, compare_detector=None, compare_goniometer=None, scan_tolerance=None, format_kwargs=None): ''' Extract the file meta data in order to sort them. ''' from dxtbx.sweep_filenames import template_regex import operator # If no comparison functions are set if compare_beam is None: compare_beam = operator.__eq__ if compare_detector is None: compare_detector = operator.__eq__ if compare_goniometer is None: compare_goniometer = operator.__eq__ if format_kwargs is None: format_kwargs = {} class Record(object): def __init__(self, beam=None, detector=None, goniometer=None, scan=None, template=None, filename=None, index=None, group=None): self.beam = beam self.detector = detector self.goniometer = goniometer self.scan = scan self.template = template self.filename = filename self.index = index self.group = group # Loop through all the filenames records = [] group = 0 for filename in filenames: # Read the image fmt = format_class(filename, **format_kwargs) # Get the meta data from the format try: b = fmt.get_beam() except Exception: b = None try: d = fmt.get_detector() except Exception: d = None try: g = fmt.get_goniometer() except Exception: g = None try: s = fmt.get_scan() except Exception: s = None # Get the template and index if possible if s is not None and abs(s.get_oscillation()[1]) > 0.0: template, index = template_regex(filename) else: template, index = None, None # Check the last record if available if len(records) > 0: last = records[-1] same = [False, False, False] if compare_beam(last.beam, b): b = last.beam same[0] = True if compare_detector(last.detector, d): d = last.detector same[1] = True if compare_goniometer(last.goniometer, g): g = last.goniometer same[2] = True # If the last was not a sweep then if the current is a sweep or none of # the models are the same, increment. Otherwise if the current is not a # sweep or if both sweeps don't share all models increment. If both # share, models try scan and increment if exception. if last.template is None: if template is not None or not any(same): group += 1 else: if template is None or not all(same): group += 1 else: try: if scan_tolerance is None: last.scan.append(s) else: last.scan.append(s, scan_tolerance=scan_tolerance) last.index = index continue except Exception: group += 1 # Add a record records.append(Record( beam=b, detector=d, goniometer=g, scan=s, template=template, filename=filename, index=index, group=group)) # Return the records return records
def _extract_file_metadata(self, format_class, filenames, compare_beam=None, compare_detector=None, compare_goniometer=None, scan_tolerance=None): ''' Extract the file meta data in order to sort them. ''' from dxtbx.sweep_filenames import template_regex import operator # If no comparison functions are set if compare_beam is None: compare_beam = operator.__eq__ if compare_detector is None: compare_detector = operator.__eq__ if compare_goniometer is None: compare_goniometer = operator.__eq__ class Record(object): def __init__(self, beam=None, detector=None, goniometer=None, scan=None, template=None, filename=None, index=None, group=None): self.beam = beam self.detector = detector self.goniometer = goniometer self.scan = scan self.template = template self.filename = filename self.index = index self.group = group # Loop through all the filenames records = [] group = 0 for filename in filenames: # Read the image fmt = format_class(filename) # Get the meta data from the format try: b = fmt.get_beam() except Exception: b = None try: d = fmt.get_detector() except Exception: d = None try: g = fmt.get_goniometer() except Exception: g = None try: s = fmt.get_scan() except Exception: s = None # Get the template and index if possible if s is not None and abs(s.get_oscillation()[1]) > 0.0: template, index = template_regex(filename) else: template, index = None, None # Check the last record if available if len(records) > 0: last = records[-1] same = [False, False, False] if compare_beam(last.beam, b): b = last.beam same[0] = True if compare_detector(last.detector, d): d = last.detector same[1] = True if compare_goniometer(last.goniometer, g): g = last.goniometer same[2] = True # If the last was not a sweep then if the current is a sweep or none of # the models are the same, increment. Otherwise if the current is not a # sweep or if both sweeps don't share all models increment. If both # share, models try scan and increment if exception. if last.template is None: if template is not None or not any(same): group += 1 else: if template is None or not all(same): group += 1 else: try: if scan_tolerance is None: last.scan.append(s) else: last.scan.append(s, scan_tolerance=scan_tolerance) last.index = index continue except Exception: group += 1 # Add a record records.append(Record( beam=b, detector=d, goniometer=g, scan=s, template=template, filename=filename, index=index, group=group)) # Return the records return records
def XDS_INP(self, out=None, space_group_number=None, real_space_a=None, real_space_b=None, real_space_c=None, job_card="XYCORR INIT COLSPOT IDXREF DEFPIX INTEGRATE CORRECT", as_str=False): if out is None: out = sys.stdout # horrible hack to allow returning result as string; would be nice if the # structure of this was to make the XDS.INP in memory then print it... # see also show() method on things. str_result = [] if as_str: def print(str='', file=None): str_result.append(str) else: from dxtbx import get_print print = get_print() assert [real_space_a, real_space_b, real_space_c].count(None) in (0, 3) sensor = self.get_detector()[0].get_type() fast, slow = self.detector_size f, s = self.pixel_size df = int(1000 * f) ds = int(1000 * s) # FIXME probably need to rotate by pi about the X axis detector = xds_detector_name( detector_helpers_types.get(sensor, fast, slow, df, ds)) trusted = self.get_detector()[0].get_trusted_range() print('DETECTOR=%s MINIMUM_VALID_PIXEL_VALUE=%d OVERLOAD=%d' % \ (detector, trusted[0] + 1, trusted[1]), file=out) if detector == 'PILATUS': print('SENSOR_THICKNESS= %.3f' % \ self.get_detector()[0].get_thickness(), file=out) if self.get_detector()[0].get_material(): from cctbx.eltbx import attenuation_coefficient material = self.get_detector()[0].get_material() table = attenuation_coefficient.get_table(material) mu = table.mu_at_angstrom(self.wavelength) / 10.0 print('!SENSOR_MATERIAL / THICKNESS %s %.3f' % \ (material, self.get_detector()[0].get_thickness()), file=out) print('!SILICON= %f' % mu, file=out) print('DIRECTION_OF_DETECTOR_X-AXIS= %.5f %.5f %.5f' % \ self.detector_x_axis, file=out) print('DIRECTION_OF_DETECTOR_Y-AXIS= %.5f %.5f %.5f' % \ self.detector_y_axis, file=out) print('NX=%d NY=%d QX=%.4f QY=%.4f' % (fast, slow, f, s), file=out) print('DETECTOR_DISTANCE= %.6f' % self.detector_distance, file=out) print('ORGX= %.2f ORGY= %.2f' % self.detector_origin, file=out) print('ROTATION_AXIS= %.5f %.5f %.5f' % \ self.rotation_axis, file=out) print('STARTING_ANGLE= %.3f' % \ self.starting_angle, file=out) print('OSCILLATION_RANGE= %.3f' % \ self.oscillation_range, file=out) print('X-RAY_WAVELENGTH= %.5f' % \ self.wavelength, file=out) print('INCIDENT_BEAM_DIRECTION= %.3f %.3f %.3f' % \ tuple([b / self.wavelength for b in self.beam_vector]), file=out) # FIXME LATER if hasattr(self.get_beam(), "get_polarization_fraction"): print('FRACTION_OF_POLARIZATION= %.3f' % \ self.get_beam().get_polarization_fraction(), file=out) print('POLARIZATION_PLANE_NORMAL= %.3f %.3f %.3f' % \ self.get_beam().get_polarization_normal(), file=out) template = self.get_template() if template.endswith('master.h5'): master_file = template g = glob.glob(template.split('master.h5')[0] + 'data_*[0-9].h5') assert g, 'No associated data files found for %s' % master_file template = template_regex(g[0])[0] template = master_file.split('master.h5')[0] + template.split( 'data_')[-1] print('NAME_TEMPLATE_OF_DATA_FRAMES= %s' % \ template.replace('#', '?'), file=out) print('TRUSTED_REGION= 0.0 1.41', file=out) for f0, s0, f1, s1 in self.get_detector()[0].get_mask(): print('UNTRUSTED_RECTANGLE= %d %d %d %d' % \ (f0, f1 + 1, s0, s1 + 1), file=out) start_end = self.get_scan().get_image_range() if start_end[0] == 0: start_end = (1, start_end[1]) print('DATA_RANGE= %d %d' % start_end, file=out) print('JOB=%s' % job_card, file=out) if space_group_number is not None: print('SPACE_GROUP_NUMBER= %i' % space_group_number, file=out) if [real_space_a, real_space_b, real_space_c].count(None) == 0: R = self.imagecif_to_xds_transformation_matrix unit_cell_a_axis = R * matrix.col(real_space_a) unit_cell_b_axis = R * matrix.col(real_space_b) unit_cell_c_axis = R * matrix.col(real_space_c) print("UNIT_CELL_A-AXIS= %.6f %.6f %.6f" % unit_cell_a_axis.elems, file=out) print("UNIT_CELL_B-AXIS= %.6f %.6f %.6f" % unit_cell_b_axis.elems, file=out) print("UNIT_CELL_C-AXIS= %.6f %.6f %.6f" % unit_cell_c_axis.elems, file=out) if len(self.panel_x_axis) > 1: for panel_id, panel_x_axis in enumerate(self.panel_x_axis): print(file=out) print("!", file=out) print("! SEGMENT %d" % (panel_id + 1), file=out) print("!", file=out) print('SEGMENT= %d %d %d %d' % self.panel_limits[panel_id], file=out) print('DIRECTION_OF_SEGMENT_X-AXIS= %.5f %.5f %.5f' % \ panel_x_axis, file=out) print('DIRECTION_OF_SEGMENT_Y-AXIS= %.5f %.5f %.5f' % \ self.panel_y_axis[panel_id], file=out) print('SEGMENT_DISTANCE= %.3f' % self.panel_distance[panel_id], file=out) print('SEGMENT_ORGX= %.2f SEGMENT_ORGY= %.2f' % self.panel_origin[panel_id], file=out) print(file=out) if as_str: return '\n'.join(str_result)