def _openingpathiterator(pathnames: Iterable[str]): """Utility function to efficiently open all paths. A path is a potential file or directory. Each path will be opened with :meth:`dxtbx.format.Format.open_file`, but in order to do so each file will only be opened once, and extraneous use of :func:`os.stat` will be avoided. Any path entries that are a directory will be recursed into, once - any further directories found will be ignored. Any path that is not a file or directory, or on which IO fails for any reason, will still be returned. Args: pathnames: Paths to attempt to open """ # Store a tuple of (recurse, pathname) to track what was root level paths = collections.deque((True, x) for x in sorted(pathnames)) while paths: # Get the next path from the queue (do_recurse, pathname) = paths.popleft() pathname = os.fspath(pathname) try: # Attempt to open this 'path' Format.open_file(pathname) except OSError as e: if e.errno == errno.EISDIR: if do_recurse: # We've tried to open a directory. Get all the entries... subdir_paths = sorted( os.path.join(pathname, x) for x in os.listdir(pathname)) # ... and add them to our queue. Make sure not to mark for recursion paths.extendleft( (False, x) for x in reversed(subdir_paths)) logger.debug("Adding %d files from %s", len(subdir_paths), pathname) else: logger.debug("Not adding sub-level directory entry %s", pathname) # Don't return directory instances continue else: # A non-directory-related IO error logger.debug("Could not import %s: %s", pathname, os.strerror(e.errno)) yield pathname
def understand(image_file): '''See if this looks like an RAXIS format image - clue is first 5 letters of file should be RAXIS.''' if Format.open_file(image_file).read(5) == 'RAXIS': return True return False
def _start(self): self._header_bytes = Format.open_file(self._image_file).read(1024) if self._header_bytes[812:822].strip() in ["SGI", "IRIS"]: self._f = ">f" self._i = ">i" else: self._f = "<f" self._i = "<i"
def _start(self): self._header_bytes = Format.open_file(self._image_file).read(1024) if self._header_bytes[812:822].strip() in ['SGI', 'IRIS']: self._f = '>f' self._i = '>i' else: self._f = '<f' self._i = '<i'
def _start(self): with Format.open_file(self._image_file) as fh: self._header_bytes = fh.read(1024) if self._header_bytes[812:822].strip() in (b"SGI", b"IRIS"): self._f = ">f" self._i = ">i" else: self._f = "<f" self._i = "<i"
def read_basic_tiff_header(filename): """Read the TIFF header (assuming for the moment a 4k header...) and return ... something.""" # things we hope to learn from the vanilla TIFF header image_width = None image_height = None image_depth = None header_size = None with Format.open_file(filename, "rb") as fh: tiff_header = fh.read(1024) byte_order = _tiff_byte_order(tiff_header[:4]) if not byte_order: return False if byte_order == LITTLE_ENDIAN: _I = "<I" _H = "<H" else: _I = ">I" _H = ">H" offset = struct.unpack(_I, tiff_header[4:8])[0] ntags = struct.unpack(_H, tiff_header[offset:offset + 2])[0] start = offset + 2 for j in range(ntags): type_desc = struct.unpack(_H, tiff_header[start:start + 2])[0] start += 2 type_type = struct.unpack(_H, tiff_header[start:start + 2])[0] start += 2 # type_size = struct.unpack(_I, tiff_header[start : start + 4])[0] start += 4 if type_type == 4: type_offset_or_value = struct.unpack(_I, tiff_header[start:start + 4])[0] start += 4 elif type_type == 3: type_offset_or_value = struct.unpack(_H, tiff_header[start:start + 2])[0] start += 4 if type_desc == 256: image_width = type_offset_or_value elif type_desc == 257: image_height = type_offset_or_value elif type_desc == 258: image_depth = type_offset_or_value elif type_desc == 273: header_size = type_offset_or_value return image_width, image_height, image_depth, header_size, byte_order
def read_basic_tiff_header(filename): '''Read the TIFF header (assuming for the moment a 4k header...) and return ... something.''' # things we hope to learn from the vanilla TIFF header image_width = None image_height = None image_depth = None header_size = None byte_order = None # OK then let's get started - and let's assume that the size is > 1 kb byte_order = tiff_byte_order(filename) tiff_header = Format.open_file(filename, 'rb').read(1024) if byte_order == LITTLE_ENDIAN: _I = '<I' _H = '<H' else: _I = '>I' _H = '>H' offset = struct.unpack(_I, tiff_header[4:8])[0] ntags = struct.unpack(_H, tiff_header[offset:offset + 2])[0] start = offset + 2 for j in range(ntags): type_desc = struct.unpack(_H, tiff_header[start:start + 2])[0] start += 2 type_type = struct.unpack(_H, tiff_header[start:start + 2])[0] start += 2 type_size = struct.unpack(_I, tiff_header[start:start + 4])[0] start += 4 if type_type == 4: type_offset_or_value = struct.unpack( _I, tiff_header[start:start + 4])[0] start += 4 elif type_type == 3: type_offset_or_value = struct.unpack( _H, tiff_header[start:start + 2])[0] start += 4 if type_desc == 256: image_width = type_offset_or_value elif type_desc == 257: image_height = type_offset_or_value elif type_desc == 258: image_depth = type_offset_or_value elif type_desc == 273: header_size = type_offset_or_value return image_width, image_height, image_depth, header_size, byte_order
def _start(self): self._header_bytes = Format.open_file(self._image_file).read(1024) if self._header_bytes[812:822].strip() in ['SGI', 'IRIS']: self._f = '>f' self._i = '>i' else: self._f = '<f' self._i = '<i' from iotbx.detectors.raxis import RAXISImage self.detectorbase = RAXISImage(self._image_file) self.detectorbase.readHeader()
def tiff_byte_order(filename): '''Determine the byte order for the file from the magic numbers at the very start of the file.''' four_bytes = Format.open_file(filename, 'rb').read(4) if 'II' in four_bytes[:2]: assert(struct.unpack('<H', four_bytes[2:])[0] == 42) return LITTLE_ENDIAN elif 'MM' in four_bytes[:2]: assert(struct.unpack('>H', four_bytes[2:])[0] == 42) return BIG_ENDIAN raise RuntimeError, '%s not recognised as TIFF' % filename
def tiff_byte_order(filename): """Determine the byte order for the file from the magic numbers at the very start of the file.""" four_bytes = Format.open_file(filename, "rb").read(4) if "II" in four_bytes[:2]: assert struct.unpack("<H", four_bytes[2:])[0] == 42 return LITTLE_ENDIAN elif "MM" in four_bytes[:2]: assert struct.unpack(">H", four_bytes[2:])[0] == 42 return BIG_ENDIAN raise RuntimeError("%s not recognised as TIFF" % filename)
def understand(image_file): try: header = Format.open_file(image_file, 'rb').read(1024) except IOError: return False # A few items expected to be the same from image to image that we can use # as a fingerprint for this instrument if header[0:7] != "R-AXIS4": return False # We expect an invalid goniometer section, indicated by wrong magic number if struct.unpack(">i", header[852:856]) == 1: return False if header[812:822].strip() != "IRIS": return False return True
type_offset_or_value = struct.unpack(_I, tiff_header[start:start + 4])[0] start += 4 elif type_type == 3: type_offset_or_value = struct.unpack(_H, tiff_header[start:start + 2])[0] start += 4 elif type_type == 2: type_offset_or_value = struct.unpack(_I, tiff_header[start:start + 4])[0] start += 4 if type_desc == 270: start = type_offset_or_value end = type_offset_or_value + type_size header_text = tiff_header[start:end].strip() return header_text if __name__ == "__main__": for arg in sys.argv[1:]: width, height, depth, header, order = read_basic_tiff_header(arg) print("(%d x %d) @ %d + %d" % (width, height, depth, header)) tiff_header = Format.open_file(arg, "rb").read(header) text = _read_tiff_image_description(tiff_header, order) if text: print(text) else: print("No text found")
elif type_type == 2: type_offset_or_value = struct.unpack( _I, tiff_header[start:start + 4])[0] start += 4 if type_desc == 270: start = type_offset_or_value end = type_offset_or_value + type_size header_text = tiff_header[start:end].strip() return header_text if __name__ == '__main__': import sys for arg in sys.argv[1:]: width, height, depth, header, order = read_basic_tiff_header(arg) print '(%d x %d) @ %d + %d' % (width, height, depth, header) tiff_header = Format.open_file(arg, 'rb').read(header) text = read_tiff_image_description(tiff_header, order) if text: print text else: print 'No text found'
def understand(image_file): """See if this looks like an RAXIS format image - clue is first 5 letters of file should be RAXIS.""" with Format.open_file(image_file) as fh: return fh.read(5) == b"RAXIS"
def read_emi(filename): """Read the meta data from an emi file. Parameters ---------- filename: str or pathlib.Path Path to the emi file. Returns ------- : dict Dictionary of experimental metadata stored in the EMI file. """ # check filename type if isinstance(filename, str): pass elif isinstance(filename, Path): filename = str(filename) else: raise TypeError("Filename is supposed to be a string or pathlib.Path") # Format.open provides transparent decompression with Format.open_file(filename, "rb") as f_emi: emi_data = f_emi.read() # dict to store _emi stuff _emi = {} # need anything readable from <ObjectInfo> to </ObjectInfo> # collect = False # data = b'' # for line in f_emi: # if b'<ObjectInfo>' in line: # collect = True # if collect: # data += line.strip() # if b'</ObjectInfo>' in line: # collect = False # close the file # f_emi.close() metaStart = emi_data.find(b"<ObjectInfo>") metaEnd = emi_data.find( b"</ObjectInfo>" ) # need to add len('</ObjectInfo>') = 13 to encompass this final tag root = ET.fromstring(emi_data[metaStart : metaEnd + 13]) # strip of binary stuff still around # data = data.decode('ascii', errors='ignore') # matchObj = re.search('<ObjectInfo>(.+?)</ObjectInfo', data) # try: # data = matchObj.group(1) # except: # raise RuntimeError('Could not find _emi metadata in specified file.') # parse metadata as xml # root = ET.fromstring('<_emi>' + data + '</_emi>') # single items _emi["Uuid"] = root.findtext("Uuid") _emi["AcquireDate"] = root.findtext("AcquireDate") _emi["Manufacturer"] = root.findtext("Manufacturer") _emi["DetectorPixelHeight"] = root.findtext("DetectorPixelHeight") _emi["DetectorPixelWidth"] = root.findtext("DetectorPixelWidth") # Microscope Conditions grp = root.find("ExperimentalConditions/MicroscopeConditions") for elem in grp: _emi[elem.tag] = _parseEntry_emi(elem.text) # Experimental Description grp = root.find("ExperimentalDescription/Root") for elem in grp: _emi[ "{} [{}]".format(elem.findtext("Label"), elem.findtext("Unit")) ] = _parseEntry_emi(elem.findtext("Value")) # AcquireInfo grp = root.find("AcquireInfo") for elem in grp: _emi[elem.tag] = _parseEntry_emi(elem.text) # DetectorRange grp = root.find("DetectorRange") for elem in grp: _emi["DetectorRange_" + elem.tag] = _parseEntry_emi(elem.text) return _emi