def do_import(filename): logger.info("Loading %s" % os.path.basename(filename)) datablocks = DataBlockFactory.from_filenames([filename]) if len(datablocks) == 0: try: datablocks = DataBlockFactory.from_json_file(filename) except ValueError: raise Abort("Could not load %s" % filename) if len(datablocks) == 0: raise Abort("Could not load %s" % filename) if len(datablocks) > 1: raise Abort("Got multiple datablocks from file %s" % filename) # Ensure the indexer and downstream applications treat this as set of stills from dxtbx.imageset import ImageSet reset_sets = [] for imageset in datablocks[0].extract_imagesets(): imageset = ImageSet(imageset.data(), imageset.indices()) imageset.set_scan(None) imageset.set_goniometer(None) reset_sets.append(imageset) return DataBlockFactory.from_imageset(reset_sets)[0]
def __call__(self, imageset): ''' Override the parameters ''' from dxtbx.imageset import ImageSet from dxtbx.imageset import ImageSweep from dxtbx.model import BeamFactory from dxtbx.model import DetectorFactory from dxtbx.model import GoniometerFactory from dxtbx.model import ScanFactory from copy import deepcopy if self.params.geometry.convert_sweeps_to_stills: imageset = ImageSet(reader=imageset.reader()) if not isinstance(imageset, ImageSweep): if self.params.geometry.convert_stills_to_sweeps: imageset = self.convert_stills_to_sweep(imageset) if isinstance(imageset, ImageSweep): beam = BeamFactory.from_phil( self.params.geometry, imageset.get_beam()) detector = DetectorFactory.from_phil( self.params.geometry, imageset.get_detector(), beam) goniometer = GoniometerFactory.from_phil( self.params.geometry, imageset.get_goniometer()) scan = ScanFactory.from_phil( self.params.geometry, deepcopy(imageset.get_scan())) i0, i1 = scan.get_array_range() j0, j1 = imageset.get_scan().get_array_range() imageset.set_beam(beam) imageset.set_detector(detector) imageset.set_goniometer(goniometer) imageset.set_scan(scan) else: for i in range(len(imageset)): beam = BeamFactory.from_phil( self.params.geometry, imageset.get_beam(i)) detector = DetectorFactory.from_phil( self.params.geometry, imageset.get_detector(i), beam) goniometer = GoniometerFactory.from_phil( self.params.geometry, imageset.get_goniometer(i)) scan = ScanFactory.from_phil( self.params.geometry, imageset.get_scan(i)) imageset.set_beam(beam, i) imageset.set_detector(detector, i) imageset.set_goniometer(goniometer, i) imageset.set_scan(scan, i) return imageset
def do_import(filename): logger.info("Loading %s"%os.path.basename(filename)) try: datablocks = DataBlockFactory.from_json_file(filename) except ValueError: datablocks = DataBlockFactory.from_filenames([filename]) if len(datablocks) == 0: raise Abort("Could not load %s"%filename) if len(datablocks) > 1: raise Abort("Got multiple datablocks from file %s"%filename) # Ensure the indexer and downstream applications treat this as set of stills from dxtbx.imageset import ImageSet reset_sets = [] for imageset in datablocks[0].extract_imagesets(): imageset = ImageSet(imageset.reader(), imageset.indices()) imageset._models = imageset._models imageset.set_scan(None) imageset.set_goniometer(None) reset_sets.append(imageset) return DataBlockFactory.from_imageset(reset_sets)[0]
def get_imageset( Class, input_filenames, beam=None, detector=None, goniometer=None, scan=None, as_imageset=False, as_sequence=False, single_file_indices=None, format_kwargs=None, template=None, check_format=True, ): """ Factory method to create an imageset """ # Import here to avoid cyclic imports from dxtbx.imageset import ImageSequence, ImageSet, ImageSetData # Get filename absolute paths, for entries that are filenames filenames = [ os.path.abspath(x) if not get_url_scheme(x) else x for x in input_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) # Get the format instance if check_format is True: Class._current_filename_ = None Class._current_instance_ = None format_instance = Class.get_instance(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_sequence].count(True) < 2 if as_imageset: is_sequence = False elif as_sequence: is_sequence = 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_sequence=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_sequence = True else: is_sequence = False # Create an imageset or sequence if not is_sequence: # Create the imageset iset = ImageSet( ImageSetData( reader=reader, masker=None, 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 Sequence without beam" assert detector is not None, "Can't create Sequence without detector" assert goniometer is not None, "Can't create Sequence without goniometer" assert scan is not None, "Can't create Sequence without scan" # Create the masker if format_instance is not None: masker = format_instance.get_masker(goniometer=goniometer) else: masker = None # Create the sequence iset = ImageSequence( ImageSetData( reader=reader, masker=masker, vendor=vendor, params=params, format=Class, template=template, ), beam=beam, detector=detector, goniometer=goniometer, scan=scan, ) if format_instance is not None: static_mask = format_instance.get_static_mask() if static_mask is not None: if not iset.external_lookup.mask.data.empty(): for m1, m2 in zip(static_mask, iset.external_lookup.mask.data): m1 &= m2.data() iset.external_lookup.mask.data = ImageBool(static_mask) else: iset.external_lookup.mask.data = ImageBool(static_mask) 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 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): ''' Factory method to create an imageset ''' from dxtbx.imageset import ImageSetData from dxtbx.imageset import ImageSet from dxtbx.imageset import ImageSweep from os.path import abspath from dials.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 we have no specific format class, we need indices for number of images if Class == FormatMultiImage: 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 # 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 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: # Create the 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