def tst_dump_empty_sweep(self): from dxtbx.imageset import ImageSweep, NullReader, SweepFileList from dxtbx.model import Beam, Detector, Goniometer, Scan from dxtbx.model.crystal import crystal_model from uuid import uuid4 imageset = ImageSweep(NullReader(SweepFileList("filename%01d.cbf", (0, 3)))) imageset.set_beam(Beam((1, 0, 0))) imageset.set_detector(Detector()) imageset.set_goniometer(Goniometer()) imageset.set_scan(Scan((1, 3), (0.0, 1.0))) crystal = crystal_model((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol=1) experiments = ExperimentListFactory.from_imageset_and_crystal( imageset, crystal) dump = ExperimentListDumper(experiments) filename = 'temp%s.json' % uuid4().hex dump.as_json(filename) experiments2 = ExperimentListFactory.from_json_file(filename, check_format=False) self.check(experiments, experiments2) print 'OK'
def tst_from_datablock(self): from dxtbx.imageset import ImageSweep, NullReader, SweepFileList from dxtbx.model import Beam, Detector, Goniometer, Scan from dxtbx.datablock import DataBlockFactory from dxtbx.model.crystal import crystal_model imageset = ImageSweep(NullReader(SweepFileList("filename%01d.cbf", (0, 2)))) imageset.set_beam(Beam()) imageset.set_detector(Detector()) imageset.set_goniometer(Goniometer()) imageset.set_scan(Scan((1, 2), (0, 1))) crystal = crystal_model((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol=0) datablock = DataBlockFactory.from_imageset(imageset) experiments = ExperimentListFactory.from_datablock_and_crystal( datablock, crystal) assert(len(experiments) == 1) assert(experiments[0].imageset is not None) assert(experiments[0].beam is not None) assert(experiments[0].detector is not None) assert(experiments[0].goniometer is not None) assert(experiments[0].scan is not None) assert(experiments[0].crystal is not None) print 'OK'
def tst_from_datablock(self): from dxtbx.imageset import ImageSweep, NullReader, SweepFileList from dxtbx.model import Beam, Detector, Goniometer, Scan from dxtbx.datablock import DataBlockFactory from dxtbx.model.crystal import crystal_model imageset = ImageSweep(NullReader(SweepFileList("filename%01d.cbf", (0, 2)))) imageset.set_beam(Beam()) imageset.set_detector(Detector()) imageset.set_goniometer(Goniometer()) imageset.set_scan(Scan((1, 2), (0, 1))) crystal = crystal_model((1, 0, 0), (0, 1, 0), (0, 0, 1), space_group_symbol=0) datablock = DataBlockFactory.from_imageset(imageset) experiments = ExperimentListFactory.from_datablock_and_crystal( datablock, crystal) assert(len(experiments) == 1) assert(experiments[0].imageset is not None) assert(experiments[0].beam is not None) assert(experiments[0].detector is not None) assert(experiments[0].goniometer is not None) assert(experiments[0].scan is not None) assert(experiments[0].crystal is not None) print 'OK' pass
def generate_spots(crystal_model, detector, beam, goniometer=None, scan=None, sel_fraction=1.0): import math experiment = Experiment(beam=beam, detector=detector, goniometer=goniometer, scan=scan, crystal=crystal_model) # if we don't set the imageset then from_predictions uses the StillsReflectionPredictor :-( from dxtbx.imageset import NullReader, ImageSweep imageset = ImageSweep(NullReader, indices=range(len(scan.get_epochs())), beam=beam, goniometer=goniometer, detector=detector, scan=scan) experiment.imageset = imageset predicted = flex.reflection_table.from_predictions(experiment) sel = flex.random_selection(len(predicted), int(math.floor(sel_fraction * len(predicted)))) predicted = predicted.select(sel) predicted['imageset_id'] = flex.size_t(len(predicted), 0) predicted['xyzobs.px.value'] = predicted['xyzcal.px'] predicted['xyzobs.px.variance'] = flex.vec3_double(len(predicted), (0.5, 0.5, 0.5)) return predicted
def run(self): from dxtbx.imageset import MultiFileReader, ImageSweep from dxtbx.format.Registry import Registry # Get the filenames filenames = self.get_file_list() # Create the format class format_class = Registry.find(filenames[0]) # Create the reader reader = MultiFileReader(format_class, filenames) # Create the sweep sweep = ImageSweep(reader) # Run a load of tests self.tst_get_item(sweep) self.tst_len(sweep, len(filenames)) self.tst_iter(sweep) self.tst_indices(sweep, range(0, 9)) self.tst_paths(sweep, filenames) self.tst_is_valid(sweep) self.tst_get_detectorbase(sweep, range(len(filenames)), 9) self.tst_get_models(sweep, range(len(filenames)), 9) self.tst_get_array_range(sweep, (0, 9)) self.tst_to_array(sweep, (3, 7), (3, 7, 50, 100, 100, 200)) self.tst_set_models(sweep)
def generate_spots(crystal_model, detector, beam, goniometer=None, scan=None, sel_fraction=1.0): experiment = Experiment(beam=beam, detector=detector, goniometer=goniometer, scan=scan, crystal=crystal_model) # if we don't set the imageset then from_predictions uses the StillsReflectionPredictor :-( filenames = [""] * len(scan) reader = Reader(filenames) masker = Masker(filenames) data = ImageSetData(reader, masker) imageset = ImageSweep(data, beam, detector, goniometer, scan) experiment.imageset = imageset predicted = flex.reflection_table.from_predictions(experiment) sel = flex.random_selection(len(predicted), int(math.floor(sel_fraction * len(predicted)))) predicted = predicted.select(sel) predicted['imageset_id'] = flex.size_t(len(predicted), 0) predicted['xyzobs.px.value'] = predicted['xyzcal.px'] predicted['xyzobs.px.variance'] = flex.vec3_double(len(predicted), (0.5, 0.5, 0.5)) return predicted
def as_imageset(self, filename): ''' Return as an imageset ''' from dxtbx.format.FormatEigerStream import FormatEigerStream from dxtbx.imageset import MultiFileReader, ImageSweep # Create the reader reader = MultiFileReader(FormatEigerStream, [filename]) # Create the sweep return ImageSweep(reader)
def _create_single_file_imageset(self, format_class, filename, format_kwargs=None): ''' Create an imageset from a multi image file. ''' from dxtbx.imageset import SingleFileReader, ImageSet, ImageSweep from os.path import abspath if format_kwargs is None: format_kwargs = {} format_instance = format_class(abspath(filename), **format_kwargs) try: scan = format_instance.get_scan() if abs(scan.get_oscillation()[1]) > 0.0: return ImageSweep(SingleFileReader(format_instance), format_kwargs=format_kwargs) except Exception: pass return ImageSet(SingleFileReader(format_instance), format_kwargs=format_kwargs)
def get_imageset( Class, filenames, beam=None, detector=None, goniometer=None, scan=None, as_sweep=False, as_imageset=False, single_file_indices=None, format_kwargs=None, template=None, check_format=True, lazy=False, ): """ Factory method to create an imageset """ from dxtbx.imageset import ImageSetData from dxtbx.imageset import ImageSweep from os.path import abspath from scitbx.array_family import flex if isinstance(filenames, str): filenames = [filenames] elif len(filenames) > 1: assert len(set(filenames)) == 1 filenames = filenames[0:1] # Make filenames absolute filenames = map(abspath, filenames) # Make it a dictionary if format_kwargs is None: format_kwargs = {} # If get_num_images hasn't been implemented, we need indices for number of images if Class.get_num_images == FormatMultiImage.get_num_images: assert single_file_indices is not None assert min(single_file_indices) >= 0 num_images = max(single_file_indices) + 1 else: num_images = None # Get some information from the format class reader = Class.get_reader()(filenames, num_images=num_images, **format_kwargs) masker = Class.get_masker()(filenames, num_images=num_images, **format_kwargs) # Get the format instance assert len(filenames) == 1 if check_format is True: format_instance = Class(filenames[0], **format_kwargs) else: format_instance = None if not as_sweep: lazy = True # Read the vendor type if check_format is True: vendor = format_instance.get_vendortype() else: vendor = "" # Get the format kwargs params = format_kwargs # Check if we have a sweep # 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 assert not (as_sweep and lazy), "No lazy support for sweeps" if single_file_indices is not None: single_file_indices = flex.size_t(single_file_indices) # Create an imageset or sweep if not is_sweep: # Use imagesetlazy # Setup ImageSetLazy and just return it. No models are set. if lazy: from dxtbx.imageset import ImageSetLazy iset = ImageSetLazy( ImageSetData( reader=reader, masker=masker, vendor=vendor, params=params, format=Class, ), indices=single_file_indices, ) return iset # Create the imageset from dxtbx.imageset import ImageSet iset = ImageSet( ImageSetData( reader=reader, masker=masker, vendor=vendor, params=params, format=Class, ), indices=single_file_indices, ) # 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 i in range(format_instance.get_num_images()): beam.append(format_instance.get_beam(i)) detector.append(format_instance.get_detector(i)) goniometer.append(format_instance.get_goniometer(i)) scan.append(format_instance.get_scan(i)) if single_file_indices is None: single_file_indices = list(range(format_instance.get_num_images())) # Set the list of models for i in range(len(single_file_indices)): iset.set_beam(beam[single_file_indices[i]], i) iset.set_detector(detector[single_file_indices[i]], i) iset.set_goniometer(goniometer[single_file_indices[i]], i) iset.set_scan(scan[single_file_indices[i]], i) else: # Get the template template = filenames[0] # Check indices are sequential if single_file_indices is not None: assert all( i + 1 == j for i, j in zip(single_file_indices[:-1], single_file_indices[1:]) ) num_images = len(single_file_indices) else: num_images = format_instance.get_num_images() # Check the scan makes sense - we must want to use <= total images if scan is not None: assert scan.get_num_images() <= num_images # If any are None then read from format if beam is None: beam = format_instance.get_beam() if detector is None: detector = format_instance.get_detector() if goniometer is None: goniometer = format_instance.get_goniometer() if scan is 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() isetdata = ImageSetData( reader=reader, masker=masker, vendor=vendor, params=params, format=Class, template=template, ) # Create the sweep iset = ImageSweep( isetdata, beam=beam, detector=detector, goniometer=goniometer, scan=scan, indices=single_file_indices, ) # Return the imageset return iset
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 tst_from_null_sweep(self): from dxtbx.imageset import NullReader, ImageSweep, SweepFileList from dxtbx.model import Beam, Detector, Goniometer, Scan sweep = ImageSweep(NullReader(SweepFileList("template_%2d.cbf", (0, 10)))) sweep.set_beam(Beam((0, 0, 1))) sweep.set_detector(Detector()) sweep.set_goniometer(Goniometer((1, 0, 0))) sweep.set_scan(Scan((1, 10), (0, 0.1))) # Create the datablock datablock = DataBlockFactory.from_imageset(sweep) assert(len(datablock) == 1) datablock = datablock[0] sweeps = datablock.extract_sweeps() assert(len(sweeps) == 1) assert(sweeps[0].get_beam() == sweep.get_beam()) assert(sweeps[0].get_detector() == sweep.get_detector()) assert(sweeps[0].get_goniometer() == sweep.get_goniometer()) assert(sweeps[0].get_scan() == sweep.get_scan()) print 'OK'
def tst_null_reader_sweep(self): from dxtbx.imageset import NullReader, ImageSweep, SweepFileList from dxtbx.model import Beam, Detector, Goniometer, Scan template = 'hello_world_%d.cbf' paths = [template % 1] # Create the null reader reader = NullReader(SweepFileList(template, (0, 1))) # Create the imageset imageset = ImageSweep(reader) # Try to get an item try: imageset[0] assert(False) except Exception: print 'OK' # Try to slice the imageset imageset2 = imageset[0:1] print 'OK' # Try some functions which should work assert(len(imageset) == 1) assert(imageset == imageset) assert(imageset.indices() == [0]) assert(imageset.is_valid()) assert(imageset.get_template() == template) print 'OK' # Get the image paths assert(imageset.paths() == paths) assert(imageset.get_path(0) == paths[0]) print 'OK' imageset.set_beam(Beam()) imageset.set_detector(Detector()) imageset.set_goniometer(Goniometer()) imageset.set_scan(Scan((1,1), (0, 1))) assert(isinstance(imageset.get_beam(), Beam)) assert(isinstance(imageset.get_detector(), Detector)) assert(isinstance(imageset.get_goniometer(), Goniometer)) assert(isinstance(imageset.get_scan(), Scan)) print 'OK'
def tst_from_null_sweep(self): from dxtbx.datablock import DataBlockFactory from dxtbx.imageset import NullReader, ImageSweep, SweepFileList from dxtbx.model import Beam, Detector, Goniometer, Scan sweep = ImageSweep(NullReader(SweepFileList("template_%2d.cbf", (0, 10)))) sweep.set_beam(Beam((0, 0, 1))) sweep.set_detector(Detector()) sweep.set_goniometer(Goniometer((1, 0, 0))) sweep.set_scan(Scan((1, 10), (0, 0.1))) # Create the datablock datablock = DataBlockFactory.from_imageset(sweep) assert(len(datablock) == 1) datablock = datablock[0] sweeps = datablock.extract_sweeps() assert(len(sweeps) == 1) assert(sweeps[0].get_beam() == sweep.get_beam()) assert(sweeps[0].get_detector() == sweep.get_detector()) assert(sweeps[0].get_goniometer() == sweep.get_goniometer()) assert(sweeps[0].get_scan() == sweep.get_scan()) print 'OK'