def test_imagesetfactory(centroid_files, dials_data): sequence = ImageSetFactory.new(centroid_files) assert isinstance(sequence[0], ImageSequence) template = dials_data("centroid_test_data").join( "centroid_####.cbf").strpath image_range = (3, 6) sequence = ImageSetFactory.from_template(template, image_range) assert isinstance(sequence[0], ImageSequence) assert len(sequence[0]) == 4 assert sequence[0].paths()[0].endswith("3.cbf") assert sequence[0].paths()[-1].endswith("6.cbf") imageset = ImageSetFactory.make_imageset(centroid_files) assert len(imageset) == 9 imageset = ImageSetFactory.make_imageset(centroid_files, check_format=False) assert len(imageset) == 9 sequence = ImageSetFactory.make_sequence(template, list(range(1, 9 + 1))) assert len(sequence) == 9 sequence = ImageSetFactory.make_sequence(template, list(range(3, 6 + 1))) assert len(sequence) == 4
def to_imageset(input_filename, extra_filename=None): '''Get an image set from the xds input filename plus an extra filename Params: input_filename The XDS.INP file extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file Returns: The imageset ''' from iotbx.xds import xds_inp from dxtbx.imageset import ImageSetFactory import dxtbx # Read the input filename handle = xds_inp.reader() handle.read_file(input_filename) # Get the template template = handle.name_template_of_data_frames[0].replace('?', '#') image_range = handle.data_range detector_name = handle.detector if extra_filename is not None: # we can get all the extra dxtbx models from extra_filename check_format = False else: # we need the image files present to get the dxtbx models check_format = True # Create the imageset imageset = ImageSetFactory.from_template( template, image_range=image_range, check_format=False)[0] # If an extra filename has been specified, try to load models if extra_filename: models = dxtbx.load(extra_filename) detector = models.get_detector() if detector_name.strip() == 'PILATUS': from dxtbx.model import ParallaxCorrectedPxMmStrategy from cctbx.eltbx import attenuation_coefficient table = attenuation_coefficient.get_table("Si") wavelength = models.get_beam().get_wavelength() mu = table.mu_at_angstrom(wavelength) / 10.0 t0 = handle.sensor_thickness for panel in detector: panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0)) panel.set_trusted_range( (handle.minimum_valid_pixel_value, handle.overload)) imageset.set_beam(models.get_beam()) imageset.set_detector(detector) imageset.set_goniometer(models.get_goniometer()) # take the image range from XDS.INP scan = models.get_scan() scan.set_image_range(image_range) imageset.set_scan(scan) # Return the imageset return imageset
def test_imagesetfactory(centroid_files, dials_regression): from dxtbx.imageset import ImageSetFactory, ImageSweep filenames = centroid_files sweep = ImageSetFactory.new(filenames) assert isinstance(sweep[0], ImageSweep) template = os.path.join(dials_regression, "centroid_test_data", "centroid_####.cbf") image_range = (3, 6) sweep = ImageSetFactory.from_template(template, image_range) assert isinstance(sweep[0], ImageSweep) assert len(sweep[0]) == 4 assert sweep[0].paths()[0].endswith("3.cbf") assert sweep[0].paths()[-1].endswith("6.cbf") imageset = ImageSetFactory.make_imageset(filenames) assert len(imageset) == 9 imageset = ImageSetFactory.make_imageset(filenames, check_format=False) assert len(imageset) == 9 sweep = ImageSetFactory.make_sweep(template, list(range(1, 9 + 1))) assert len(sweep) == 9 sweep = ImageSetFactory.make_sweep(template, list(range(3, 6 + 1))) assert len(sweep) == 4
def imagesweep_from_dict(d, check_format=True, directory=None): """Construct and image sweep from the dictionary.""" # Get the template (required) template = load_path(str(d["template"]), directory=directory) # If the scan isn't set, find all available files scan_dict = d.get("scan") if scan_dict is None: image_range = None else: image_range = scan_dict.get("image_range") # Set the models with the exisiting models as templates beam = BeamFactory.from_dict(d.get("beam")) goniometer = GoniometerFactory.from_dict(d.get("goniometer")) detector = DetectorFactory.from_dict(d.get("detector")) scan = ScanFactory.from_dict(d.get("scan")) # Construct the sweep try: sweep = ImageSetFactory.from_template( template, image_range, beam=beam, detector=detector, goniometer=goniometer, scan=scan, check_format=check_format, )[0] except Exception: indices = range(image_range[0], image_range[1] + 1) sweep = ImageSetFactory.make_sweep( template, indices, beam=beam, detector=detector, goniometer=goniometer, scan=scan, check_format=check_format, ) # Set some external lookups if "mask" in d and d["mask"] is not None and d["mask"] is not "": path = load_path(d["mask"], directory=directory) with open(path) as infile: sweep.external_lookup.mask.filename = path sweep.external_lookup.mask.data = ImageBool(pickle.load(infile)) if "gain" in d and d["gain"] is not None and d["gain"] is not "": path = load_path(d["gain"], directory=directory) with open(path) as infile: sweep.external_lookup.gain.filename = path sweep.external_lookup.gain.data = ImageDouble(pickle.load(infile)) if "pedestal" in d and d["pedestal"] is not None and d["pedestal"] is not "": path = load_path(d["pedestal"], directory=directory) with open(path) as infile: sweep.external_lookup.pedestal.filename = path sweep.external_lookup.pedestal.data = ImageDouble(pickle.load(infile)) # Return the sweep return sweep
def test_make_sequence_with_percent_character(dials_data, tmp_path): images = [ dials_data("centroid_test_data").join(f"centroid_{i:04}.cbf") for i in range(1, 10) ] directory = tmp_path / "test%" directory.mkdir() try: for image in images: try: (directory / image.basename).symlink_to(image) except OSError: shutil.copy(image, directory) template = str(directory / "centroid_####.cbf") sequence = ImageSetFactory.make_sequence(template, range(1, 10)) assert len(sequence) == 9 sequences = ImageSetFactory.new( [str(directory / image.basename) for image in images]) assert len(sequences) == 1 assert len(sequences[0]) == 9 sequences = ImageSetFactory.from_template(template) assert len(sequences) == 1 assert len(sequences[0]) == 9 finally: # clean up potentially copied files after running test for image in images: try: (directory / image.basename).unlink() except FileNotFoundError: pass
def imagesweep_from_dict(d, check_format=True): '''Construct and image sweep from the dictionary.''' from dxtbx.imageset import ImageSetFactory from dxtbx.model import BeamFactory, DetectorFactory, GoniometerFactory, ScanFactory from dxtbx.serialize.filename import load_path # Get the template (required) template = load_path(str(d['template'])) # If the scan isn't set, find all available files scan_dict = d.get('scan') if scan_dict is None: image_range = None else: image_range = scan_dict.get('image_range') # Set the models with the exisiting models as templates beam = BeamFactory.from_dict(d.get('beam')) goniometer = GoniometerFactory.from_dict(d.get('goniometer')) detector = DetectorFactory.from_dict(d.get('detector')) scan = ScanFactory.from_dict(d.get('scan')) # Construct the sweep try: sweep = ImageSetFactory.from_template(template, image_range, beam=beam, detector=detector, goniometer=goniometer, scan=scan, check_format=check_format)[0] except Exception: indices = range(image_range[0], image_range[1] + 1) sweep = ImageSetFactory.make_sweep(template, indices, beam=beam, detector=detector, goniometer=goniometer, scan=scan, check_format=check_format) # Set some external lookups if 'mask' in d and d['mask'] is not None and d['mask'] is not "": with open(d['mask']) as infile: sweep.external_lookup.mask.filename = d['mask'] sweep.external_lookup.mask.data = ImageBool(pickle.load(infile)) if 'gain' in d and d['gain'] is not None and d['gain'] is not "": with open(d['gain']) as infile: sweep.external_lookup.gain.filename = d['gain'] sweep.external_lookup.gain.data = ImageDouble(pickle.load(infile)) if 'pedestal' in d and d['pedestal'] is not None and d[ 'pedestal'] is not "": with open(d['pedestal']) as infile: sweep.external_lookup.pedestal.filename = d['pedestal'] sweep.external_lookup.pedestal.data = ImageDouble( pickle.load(infile)) # Return the sweep return sweep
def imagesweep_from_dict(d, check_format=True): '''Construct and image sweep from the dictionary.''' from dxtbx.imageset import ImageSetFactory from dxtbx.serialize import beam, detector, goniometer, scan from dxtbx.serialize.filename import load_path # Get the template (required) template = load_path(str(d['template'])) # If the scan isn't set, find all available files scan_dict = d.get('scan') if scan_dict is None: image_range = None else: image_range = scan_dict.get('image_range') # Construct the sweep try: sweep = ImageSetFactory.from_template( template, image_range, check_format=check_format)[0] # Get the existing models as dictionaries beam_dict = beam.to_dict(sweep.get_beam()) gonio_dict = goniometer.to_dict(sweep.get_goniometer()) detector_dict = detector.to_dict(sweep.get_detector()) scan_dict = scan.to_dict(sweep.get_scan()) except Exception: indices = range(image_range[0], image_range[1] + 1) sweep = ImageSetFactory.make_sweep( template, indices, check_format=False) beam_dict = None gonio_dict = None detector_dict = None scan_dict = None # Set some external lookups if 'mask' in d and d['mask'] is not None: with open(d['mask']) as infile: sweep.external_lookup.mask.filename = d['mask'] sweep.external_lookup.mask.data = pickle.load(infile) if 'gain' in d and d['gain'] is not None: with open(d['gain']) as infile: sweep.external_lookup.gain.filename = d['gain'] sweep.external_lookup.gain.data = pickle.load(infile) if 'pedestal' in d and d['pedestal'] is not None: with open(d['pedestal']) as infile: sweep.external_lookup.pedestal.filename = d['pedestal'] sweep.external_lookup.pedestal.data = pickle.load(infile) # Set the models with the exisiting models as templates sweep.set_beam(beam.from_dict(d.get('beam'), beam_dict)) sweep.set_goniometer(goniometer.from_dict(d.get('goniometer'), gonio_dict)) sweep.set_detector(detector.from_dict(d.get('detector'), detector_dict)) sweep.set_scan(scan.from_dict(d.get('scan'), scan_dict)) # Return the sweep return sweep
def run(self): from dxtbx.imageset import ImageSetFactory, ImageSweep from os.path import join filenames = self.get_file_list() sweep = ImageSetFactory.new(filenames) assert(isinstance(sweep[0], ImageSweep) == True) print 'OK' template = join(dials_regression, "centroid_test_data", "centroid_####.cbf") image_range = (3, 6) sweep = ImageSetFactory.from_template(template, image_range) assert(isinstance(sweep[0], ImageSweep) == True) assert len(sweep[0]) == 4 assert sweep[0].paths()[0].endswith("3.cbf") assert sweep[0].paths()[-1].endswith("6.cbf") print 'OK' imageset = ImageSetFactory.make_imageset(filenames) assert len(imageset) == 9 print 'OK' imageset = ImageSetFactory.make_imageset( filenames, check_format=False) assert len(imageset) == 9 print 'OK' sweep = ImageSetFactory.make_sweep( template, list(range(1, 9+1))) assert len(sweep) == 9 print 'OK' sweep = ImageSetFactory.make_sweep( template, list(range(3, 6+1))) assert len(sweep) == 4 print 'OK'
def test_make_sequence_with_percent_character(dials_data, tmp_path): images = [ dials_data("centroid_test_data").join(f"centroid_{i:04}.cbf") for i in range(1, 10) ] directory = tmp_path / "test%" directory.mkdir() for image in images: (directory / image.basename).symlink_to(image) template = str(directory / "centroid_####.cbf") sequence = ImageSetFactory.make_sequence(template, range(1, 10)) assert len(sequence) == 9 sequences = ImageSetFactory.new( [str(directory / image.basename) for image in images] ) assert len(sequences) == 1 assert len(sequences[0]) == 9 sequences = ImageSetFactory.from_template(template) assert len(sequences) == 1 assert len(sequences[0]) == 9
def to_imageset(input_filename, extra_filename=None): """Get an image set from the xds input filename plus an extra filename Params: input_filename The XDS.INP file extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file Returns: The imageset """ from iotbx.xds import xds_inp from dxtbx.imageset import ImageSetFactory import dxtbx # Read the input filename handle = xds_inp.reader() handle.read_file(input_filename) # Get the template template = handle.name_template_of_data_frames[0].replace("?", "#") if template.endswith("h5"): template = template.replace("######", "master") image_range = handle.data_range detector_name = handle.detector if extra_filename is not None: # we can get all the extra dxtbx models from extra_filename check_format = False else: # we need the image files present to get the dxtbx models check_format = True # If an extra filename has been specified, try to load models if extra_filename: models = dxtbx.load(extra_filename) detector = models.get_detector() if detector_name.strip() in ("PILATUS", "EIGER") or handle.silicon is not None: from dxtbx.model import ParallaxCorrectedPxMmStrategy from cctbx.eltbx import attenuation_coefficient if handle.silicon is None: table = attenuation_coefficient.get_table("Si") wavelength = models.get_beam().get_wavelength() mu = table.mu_at_angstrom(wavelength) / 10.0 else: mu = handle.silicon t0 = handle.sensor_thickness for panel in detector: panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0)) panel.set_trusted_range( (handle.minimum_valid_pixel_value, handle.overload)) beam = models.get_beam() detector = models.get_detector() goniometer = models.get_goniometer() scan = models.get_scan() scan.set_image_range(image_range) else: beam = None detector = None goniometer = None scan = None # Create the imageset imageset = ImageSetFactory.from_template( template, image_range=image_range, check_format=check_format, beam=beam, detector=detector, goniometer=goniometer, scan=scan, )[0] # Return the imageset return imageset
from dxtbx.datablock import DataBlockFactory, DataBlockDumper from dxtbx.imageset import ImageSetFactory, ImageSweep, ImageSetData from dxtbx.model.goniometer import GoniometerFactory from dxtbx.model.scan import ScanFactory import glob, os, sys """ Modification of AB's prepare_sweep.py. Usage: libtbx.python prepare_sweep.py img_dir start_img, end_img savefile """ root = sys.argv[1] start, end = int(sys.argv[2]), int(sys.argv[3]) g = GoniometerFactory.single_axis() s = ScanFactory.make_scan((start, end), 0, (0, 1), [0] * (end - start + 1)) sw = ImageSetFactory.from_template(template=os.path.join( root, "fft_frame_I_mf_####.cbf"), scan=s, goniometer=g, image_range=(start, end)) dump = DataBlockDumper(DataBlockFactory.from_imageset(sw)) dump.as_file(sys.argv[4])
def to_imageset(input_filename, extra_filename=None): '''Get an image set from the xds input filename plus an extra filename Params: input_filename The XDS.INP file extra_filename A (G)XPARM.XDS, INTGRATE.HKL or XDS_ASCII.HKL file Returns: The imageset ''' from iotbx.xds import xds_inp from dxtbx.imageset import ImageSetFactory import dxtbx # Read the input filename handle = xds_inp.reader() handle.read_file(input_filename) # Get the template template = handle.name_template_of_data_frames[0].replace('?', '#') image_range = handle.data_range detector_name = handle.detector if extra_filename is not None: # we can get all the extra dxtbx models from extra_filename check_format = False else: # we need the image files present to get the dxtbx models check_format = True # Create the imageset imageset = ImageSetFactory.from_template(template, image_range=image_range, check_format=False)[0] # If an extra filename has been specified, try to load models if extra_filename: models = dxtbx.load(extra_filename) detector = models.get_detector() if detector_name.strip() == 'PILATUS': from dxtbx.model import ParallaxCorrectedPxMmStrategy from cctbx.eltbx import attenuation_coefficient table = attenuation_coefficient.get_table("Si") wavelength = models.get_beam().get_wavelength() mu = table.mu_at_angstrom(wavelength) / 10.0 t0 = handle.sensor_thickness for panel in detector: panel.set_px_mm_strategy(ParallaxCorrectedPxMmStrategy(mu, t0)) panel.set_trusted_range( (handle.minimum_valid_pixel_value, handle.overload)) imageset.set_beam(models.get_beam()) imageset.set_detector(detector) imageset.set_goniometer(models.get_goniometer()) # take the image range from XDS.INP scan = models.get_scan() scan.set_image_range(image_range) imageset.set_scan(scan) # Return the imageset return imageset