def check_regular_expression(cls, regex): '''Checks whether a named regular expression has all required fields. Parameters ---------- regex: str regular expression Raises ------ tmlib.erros.RegexError when a provided field is not supported ''' if not regex: raise RegexError('No regular expression provided.') provided_fields = re.findall(r'\(\?P\<(\w+)\>', regex) for name in provided_fields: if name not in _SUPPORTED__FIELDS: raise RegexError( '"%s" is not a supported regular expression field.\n' 'Supported are "%s"' % (name, '", "'.join(_SUPPORTED__FIELDS))) for name in _SUPPORTED__FIELDS: if name not in provided_fields and name in _FIELD_DEFAULTS: logger.warning( 'regular expression field "%s" not provided, defaults to %s', name, str(_FIELD_DEFAULTS[name]))
def extract_fields_from_filename(cls, regex, filename, defaults=True): '''Extracts fields from image filenames using a regular expression. Parameters ---------- regex: str regular expression filename: str name of a microscope image file defaults: bool, optional whether default values should be used Returns ------- tmlib.workflow.metaconfig.base.MetadataFields named tuple with extracted values ''' r = re.compile(regex) match = r.search(str(filename)) if match is None: raise RegexError('Metadata attributes could not be retrieved from ' 'filename "%s" using regular expression "%s"' % (filename, regex)) captures = match.groupdict() for k in _SUPPORTED__FIELDS: v = captures.get(k, None) if v is None: if defaults: captures[k] = str(_FIELD_DEFAULTS[k]) else: captures[k] = v return MetadataFields(**captures)
def get_bit_depth(pixel_type): r = re.compile(r'(\d+)$') m = r.search(pixel_type) if not m: raise RegexError( 'Bit depth could not be determined from pixel type.') return int(m.group(1))
def get_image_ix(image_id): ''' Get the index of an image within the OMEXML metadata object given the ID of the image. Parameters ---------- image_id: str image identifier in the format ``Image:\d+`` Returns ------- int zero-based index number ''' match = re.search(r'^Image:(\d+)$', image_id) if not match: RegexError('Index of image could not be determined from image ID.') return int(match.group(1))