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 from_format(cls, fmt): # type: (Format) -> Any """ Read metadata information from a Format instance. This will only pull information out of a single format instance while it is open - combining metadata records must be done separately. Args: format: The instance of the format class to read data from Returns: A new ImageMetadataRecord with the pre-read information """ record = cls() record.filename = fmt.get_image_file() # Get the metadata from the format try: record.beam = fmt.get_beam() except Exception: pass try: record.detector = fmt.get_detector() except Exception: pass try: record.goniometer = fmt.get_goniometer() except Exception: pass try: record.scan = fmt.get_scan() except Exception: pass # Get the template and index if possible - and only if we've got a # recorded oscillation value if record.scan is not None: record.template, record.index = template_regex(record.filename) return record
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 test_template_regex(filename, template, digits): assert template_regex(filename) == (template, digits)
def get_import_run_string(in_str_lst): """ Calculate the dials.import filename and image_range parameters. Args: in_str_lst ([str]): List of files to open Returns: (Tuple[str,str]): Tuple containing dir_path (str): The location of the data. Note: This only appears to be used for re-opening the data dialog? import_string (str): The string containing parts to pass to dials.import. This could be of the forms: '/some/path/single_file_0002.cbf' '/some/path/images_master.nxs' '/some/path/filename_*.cbf' '/some/path/filename_*.cbf image_range=1,100' but in other cases may include several filenames... """ logger.debug("Converting string for import: %s", in_str_lst) if len(in_str_lst) == 1: template, index = template_regex(in_str_lst[0]) indices = [index] else: try: template, indices = template_regex_from_list(in_str_lst) except (AssertionError, TypeError): template = None indices = None if template is None: # Unable to collapse?? Just pass through all filenames as <name> # and trust dials to process return os.path.dirname(in_str_lst[0]), " ".join(in_str_lst) dirname = os.path.dirname(template) # We're currently using wildcards, so continue with this by # replacing the template placeholder for now. out_str = re.sub("#+", "*", template) # Do we have a restricted image range? image_range = None if len(indices) > 1: min_image_range = min(indices) max_image_range = max(indices) image_range = (min_image_range, max_image_range) # Warn if things were missing - this may be perfectly normal if not set(indices) == set(range(min_image_range, max_image_range + 1)): logger.warning("Non-continuous image range selected - output may be wrong") print(indices) print(list(sorted(set(range(min_image_range, max_image_range + 1))))) if image_range is not None: out_str += " image_range={},{}".format(*image_range) logger.debug("Filename template output = %s", out_str) logger.debug("Filename template dir = %s", dirname) return dirname, out_str