# -*- coding: utf-8 -*- # imageio is distributed under the terms of the (new) BSD License. """ SPE file reader """ from __future__ import absolute_import, print_function, division import os import logging import numpy as np from .. import formats from ..core import Format logger = logging.getLogger(__name__) class Spec: """SPE file specification data Tuples of (offset, datatype, count), where offset is the offset in the SPE file and datatype is the datatype as used in `numpy.fromfile`() `data_start` is the offset of actual image data. `dtypes` translates SPE datatypes (0...4) to numpy ones, e. g. dtypes[0] is dtype("= self._len: raise IndexError("Image index %i > %i" % (index, self._len)) if self.request.mode[1] in "vV": if index != 0: raise IndexError("Index has to be 0 in v and V modes") self._file.seek(Spec.data_start) data = np.fromfile( self._file, dtype=self._dtype, count=self._shape[0] * self._shape[1] * self._len, ) data = data.reshape((self._len,) + self._shape) else: self._file.seek( Spec.data_start + index * self._shape[0] * self._shape[1] * self._dtype.itemsize ) data = np.fromfile( self._file, dtype=self._dtype, count=self._shape[0] * self._shape[1] ) data = data.reshape(self._shape) return data, self._get_meta_data(index) def roi_array_to_dict(a): """Convert the `ROIs` structured arrays to :py:class:`dict` Parameters ---------- a : numpy.ndarray Structured array containing ROI data Returns ------- list of dict One dict per ROI. Keys are "top_left", "bottom_right", and "bin", values are tuples whose first element is the x axis value and the second element is the y axis value. """ l = [] a = a[["startx", "starty", "endx", "endy", "groupx", "groupy"]] for sx, sy, ex, ey, gx, gy in a: d = { "top_left": [int(sx), int(sy)], "bottom_right": [int(ex), int(ey)], "bin": [int(gx), int(gy)], } l.append(d) return l fmt = SpeFormat("spe", "SPE file format", ".spe", "iIvV") formats.add_format(fmt, overwrite=True)