Ejemplo n.º 1
0
    def __init__(self, parent=None, textFormatter=None):
        """
        Constructor

        :param qt.QObject parent: Owner of the object
        :param TextFormatter formatter: Text formatter
        """
        qt.QObject.__init__(self, parent)
        if textFormatter is not None:
            self.__formatter = textFormatter
        else:
            self.__formatter = TextFormatter(self)
        self.__formatter.formatChanged.connect(self.__formatChanged)
Ejemplo n.º 2
0
class _ScalarView(DataView):
    """View displaying data using text"""
    def __init__(self, parent):
        DataView.__init__(self, parent, modeId=RAW_SCALAR_MODE)

    def createWidget(self, parent):
        widget = qt.QTextEdit(parent)
        widget.setTextInteractionFlags(qt.Qt.TextSelectableByMouse)
        widget.setAlignment(qt.Qt.AlignLeft | qt.Qt.AlignTop)
        self.__formatter = TextFormatter(parent)
        return widget

    def clear(self):
        self.getWidget().setText("")

    def setData(self, data):
        d = self.normalizeData(data)
        if silx.io.is_dataset(d):
            d = d[()]
        text = self.__formatter.toString(d, data.dtype)
        self.getWidget().setText(text)

    def axesNames(self, data, info):
        return []

    def getDataPriority(self, data, info):
        data = self.normalizeData(data)
        if info.shape is None:
            return DataView.UNSUPPORTED
        if data is None:
            return DataView.UNSUPPORTED
        if silx.io.is_group(data):
            return DataView.UNSUPPORTED
        return 2
Ejemplo n.º 3
0
    def __init__(self, parent=None, textFormatter=None):
        """
        Constructor

        :param qt.QObject parent: Owner of the object
        :param TextFormatter formatter: Text formatter
        """
        qt.QObject.__init__(self, parent)
        if textFormatter is not None:
            self.__formatter = textFormatter
        else:
            self.__formatter = TextFormatter(self)
        self.__formatter.formatChanged.connect(self.__formatChanged)
Ejemplo n.º 4
0
    def __init__(self, parent=None, data=None, perspective=None):
        qt.QAbstractTableModel.__init__(self, parent)

        self._array = None
        """n-dimensional numpy array"""

        self._bgcolors = None
        """(n+1)-dimensional numpy array containing RGB(A) color data
        for the background color
        """

        self._fgcolors = None
        """(n+1)-dimensional numpy array containing RGB(A) color data
        for the foreground color
        """

        self._formatter = None
        """Formatter for text representation of data"""

        formatter = TextFormatter(self)
        formatter.setUseQuoteForText(False)
        self.setFormatter(formatter)

        self._index = None
        """This attribute stores the slice index, as a list of indices
        where the frame intersects orthogonal axis."""

        self._perspective = None
        """Sequence of dimensions orthogonal to the frame to be viewed.
        For an array with ``n`` dimensions, this is a sequence of ``n-2``
        integers. the first dimension is numbered ``0``.
        By default, the data frames use the last two dimensions as their axes
        and therefore the perspective is a sequence of the first ``n-2``
        dimensions.
        For example, for a 5-D array, the default perspective is ``(0, 1, 2)``
        and the default frames axes are ``(3, 4)``."""

        # set _data and _perspective
        self.setArrayData(data, perspective=perspective)
    def __init__(self, parent=None, data=None, perspective=None):
        qt.QAbstractTableModel.__init__(self, parent)

        self._array = None
        """n-dimensional numpy array"""

        self._bgcolors = None
        """(n+1)-dimensional numpy array containing RGB(A) color data
        for the background color
        """

        self._fgcolors = None
        """(n+1)-dimensional numpy array containing RGB(A) color data
        for the foreground color
        """

        self._formatter = None
        """Formatter for text representation of data"""

        formatter = TextFormatter(self)
        formatter.setUseQuoteForText(False)
        self.setFormatter(formatter)

        self._index = None
        """This attribute stores the slice index, as a list of indices
        where the frame intersects orthogonal axis."""

        self._perspective = None
        """Sequence of dimensions orthogonal to the frame to be viewed.
        For an array with ``n`` dimensions, this is a sequence of ``n-2``
        integers. the first dimension is numbered ``0``.
        By default, the data frames use the last two dimensions as their axes
        and therefore the perspective is a sequence of the first ``n-2``
        dimensions.
        For example, for a 5-D array, the default perspective is ``(0, 1, 2)``
        and the default frames axes are ``(3, 4)``."""

        # set _data and _perspective
        self.setArrayData(data, perspective=perspective)
Ejemplo n.º 6
0
class _ScalarView(DataView):
    """View displaying data using text"""

    def __init__(self, parent):
        DataView.__init__(self, parent, modeId=RAW_SCALAR_MODE)

    def createWidget(self, parent):
        widget = qt.QTextEdit(parent)
        widget.setTextInteractionFlags(qt.Qt.TextSelectableByMouse)
        widget.setAlignment(qt.Qt.AlignLeft | qt.Qt.AlignTop)
        self.__formatter = TextFormatter(parent)
        return widget

    def clear(self):
        self.getWidget().setText("")

    def setData(self, data):
        d = self.normalizeData(data)
        if silx.io.is_dataset(d):
            d = d[()]
        dtype = None
        if data is not None:
            if hasattr(data, "dtype"):
                dtype = data.dtype
        text = self.__formatter.toString(d, dtype)
        self.getWidget().setText(text)

    def axesNames(self, data, info):
        return []

    def getDataPriority(self, data, info):
        if info.size <= 0:
            return DataView.UNSUPPORTED
        data = self.normalizeData(data)
        if info.shape is None:
            return DataView.UNSUPPORTED
        if data is None:
            return DataView.UNSUPPORTED
        if silx.io.is_group(data):
            return DataView.UNSUPPORTED
        return 2
Ejemplo n.º 7
0
class Hdf5Formatter(qt.QObject):
    """Formatter to convert HDF5 data to string.
    """

    formatChanged = qt.Signal()
    """Emitted when properties of the formatter change."""
    def __init__(self, parent=None, textFormatter=None):
        """
        Constructor

        :param qt.QObject parent: Owner of the object
        :param TextFormatter formatter: Text formatter
        """
        qt.QObject.__init__(self, parent)
        if textFormatter is not None:
            self.__formatter = textFormatter
        else:
            self.__formatter = TextFormatter(self)
        self.__formatter.formatChanged.connect(self.__formatChanged)

    def textFormatter(self):
        """Returns the used text formatter

        :rtype: TextFormatter
        """
        return self.__formatter

    def setTextFormatter(self, textFormatter):
        """Set the text formatter to be used

        :param TextFormatter textFormatter: The text formatter to use
        """
        if textFormatter is None:
            raise ValueError("Formatter expected but None found")
        if self.__formatter is textFormatter:
            return
        self.__formatter.formatChanged.disconnect(self.__formatChanged)
        self.__formatter = textFormatter
        self.__formatter.formatChanged.connect(self.__formatChanged)
        self.__formatChanged()

    def __formatChanged(self):
        self.formatChanged.emit()

    def humanReadableShape(self, dataset):
        if dataset.shape is None:
            return "none"
        if dataset.shape == tuple():
            return "scalar"
        shape = [str(i) for i in dataset.shape]
        text = u" \u00D7 ".join(shape)
        return text

    def humanReadableValue(self, dataset):
        if dataset.shape is None:
            return "No data"

        dtype = dataset.dtype
        if dataset.dtype.type == numpy.void:
            if dtype.fields is None:
                return "Raw data"

        if dataset.shape == tuple():
            numpy_object = dataset[()]
            text = self.__formatter.toString(numpy_object, dtype=dataset.dtype)
        else:
            if dataset.size < 5 and dataset.compression is None:
                numpy_object = dataset[0:5]
                text = self.__formatter.toString(numpy_object,
                                                 dtype=dataset.dtype)
            else:
                dimension = len(dataset.shape)
                if dataset.compression is not None:
                    text = "Compressed %dD data" % dimension
                else:
                    text = "%dD data" % dimension
        return text

    def humanReadableType(self, dataset, full=False):
        if hasattr(dataset, "dtype"):
            dtype = dataset.dtype
        else:
            # Fallback...
            dtype = type(dataset)
        return self.humanReadableDType(dtype, full)

    def humanReadableDType(self, dtype, full=False):
        if dtype == six.binary_type or numpy.issubdtype(dtype, numpy.string_):
            text = "string"
            if full:
                text = "ASCII " + text
            return text
        elif dtype == six.text_type or numpy.issubdtype(dtype, numpy.unicode_):
            text = "string"
            if full:
                text = "UTF-8 " + text
            return text
        elif dtype.type == numpy.object_:
            ref = h5py.check_dtype(ref=dtype)
            if ref is not None:
                return "reference"
            vlen = h5py.check_dtype(vlen=dtype)
            if vlen is not None:
                text = self.humanReadableDType(vlen, full=full)
                if full:
                    text = "variable-length " + text
                return text
            return "object"
        elif dtype.type == numpy.bool_:
            return "bool"
        elif dtype.type == numpy.void:
            if dtype.fields is None:
                return "opaque"
            else:
                if not full:
                    return "compound"
                else:
                    fields = sorted(dtype.fields.items(),
                                    key=lambda e: e[1][1])
                    compound = [d[1][0] for d in fields]
                    compound = [self.humanReadableDType(d) for d in compound]
                    return "compound(%s)" % ", ".join(compound)
        elif numpy.issubdtype(dtype, numpy.integer):
            if h5py is not None:
                enumType = h5py.check_dtype(enum=dtype)
                if enumType is not None:
                    return "enum"

        text = str(dtype.newbyteorder('N'))
        if full:
            if dtype.byteorder == "<":
                text = "Little-endian " + text
            elif dtype.byteorder == ">":
                text = "Big-endian " + text
            elif dtype.byteorder == "=":
                text = "Native " + text

        dtype = dtype.newbyteorder('N')
        return text

    def humanReadableHdf5Type(self, dataset):
        """Format the internal HDF5 type as a string"""
        t = dataset.id.get_type()
        class_ = t.get_class()
        if class_ == h5py.h5t.NO_CLASS:
            return "NO_CLASS"
        elif class_ == h5py.h5t.INTEGER:
            return "INTEGER"
        elif class_ == h5py.h5t.FLOAT:
            return "FLOAT"
        elif class_ == h5py.h5t.TIME:
            return "TIME"
        elif class_ == h5py.h5t.STRING:
            charset = t.get_cset()
            strpad = t.get_strpad()
            text = ""

            if strpad == h5py.h5t.STR_NULLTERM:
                text += "NULLTERM"
            elif strpad == h5py.h5t.STR_NULLPAD:
                text += "NULLPAD"
            elif strpad == h5py.h5t.STR_SPACEPAD:
                text += "SPACEPAD"
            else:
                text += "UNKNOWN_STRPAD"

            if t.is_variable_str():
                text += " VARIABLE"

            if charset == h5py.h5t.CSET_ASCII:
                text += " ASCII"
            elif charset == h5py.h5t.CSET_UTF8:
                text += " UTF8"
            else:
                text += " UNKNOWN_CSET"

            return text + " STRING"
        elif class_ == h5py.h5t.BITFIELD:
            return "BITFIELD"
        elif class_ == h5py.h5t.OPAQUE:
            return "OPAQUE"
        elif class_ == h5py.h5t.COMPOUND:
            return "COMPOUND"
        elif class_ == h5py.h5t.REFERENCE:
            return "REFERENCE"
        elif class_ == h5py.h5t.ENUM:
            return "ENUM"
        elif class_ == h5py.h5t.VLEN:
            return "VLEN"
        elif class_ == h5py.h5t.ARRAY:
            return "ARRAY"
        else:
            return "UNKNOWN_CLASS"
Ejemplo n.º 8
0
class Hdf5Formatter(qt.QObject):
    """Formatter to convert HDF5 data to string.
    """

    formatChanged = qt.Signal()
    """Emitted when properties of the formatter change."""

    def __init__(self, parent=None, textFormatter=None):
        """
        Constructor

        :param qt.QObject parent: Owner of the object
        :param TextFormatter formatter: Text formatter
        """
        qt.QObject.__init__(self, parent)
        if textFormatter is not None:
            self.__formatter = textFormatter
        else:
            self.__formatter = TextFormatter(self)
        self.__formatter.formatChanged.connect(self.__formatChanged)

    def textFormatter(self):
        """Returns the used text formatter

        :rtype: TextFormatter
        """
        return self.__formatter

    def setTextFormatter(self, textFormatter):
        """Set the text formatter to be used

        :param TextFormatter textFormatter: The text formatter to use
        """
        if textFormatter is None:
            raise ValueError("Formatter expected but None found")
        if self.__formatter is textFormatter:
            return
        self.__formatter.formatChanged.disconnect(self.__formatChanged)
        self.__formatter = textFormatter
        self.__formatter.formatChanged.connect(self.__formatChanged)
        self.__formatChanged()

    def __formatChanged(self):
        self.formatChanged.emit()

    def humanReadableShape(self, dataset):
        if dataset.shape is None:
            return "none"
        if dataset.shape == tuple():
            return "scalar"
        shape = [str(i) for i in dataset.shape]
        text = u" \u00D7 ".join(shape)
        return text

    def humanReadableValue(self, dataset):
        if dataset.shape is None:
            return "No data"

        dtype = dataset.dtype
        if dataset.dtype.type == numpy.void:
            if dtype.fields is None:
                return "Raw data"

        if dataset.shape == tuple():
            numpy_object = dataset[()]
            text = self.__formatter.toString(numpy_object, dtype=dataset.dtype)
        else:
            if dataset.size < 5 and dataset.compression is None:
                numpy_object = dataset[0:5]
                text = self.__formatter.toString(numpy_object, dtype=dataset.dtype)
            else:
                dimension = len(dataset.shape)
                if dataset.compression is not None:
                    text = "Compressed %dD data" % dimension
                else:
                    text = "%dD data" % dimension
        return text

    def humanReadableType(self, dataset, full=False):
        if hasattr(dataset, "dtype"):
            dtype = dataset.dtype
        else:
            # Fallback...
            dtype = type(dataset)
        return self.humanReadableDType(dtype, full)

    def humanReadableDType(self, dtype, full=False):
        if dtype == six.binary_type or numpy.issubdtype(dtype, numpy.string_):
            text = "string"
            if full:
                text = "ASCII " + text
            return text
        elif dtype == six.text_type or numpy.issubdtype(dtype, numpy.unicode_):
            text = "string"
            if full:
                text = "UTF-8 " + text
            return text
        elif dtype.type == numpy.object_:
            ref = h5py.check_dtype(ref=dtype)
            if ref is not None:
                return "reference"
            vlen = h5py.check_dtype(vlen=dtype)
            if vlen is not None:
                text = self.humanReadableDType(vlen, full=full)
                if full:
                    text = "variable-length " + text
                return text
            return "object"
        elif dtype.type == numpy.bool_:
            return "bool"
        elif dtype.type == numpy.void:
            if dtype.fields is None:
                return "opaque"
            else:
                if not full:
                    return "compound"
                else:
                    fields = sorted(dtype.fields.items(), key=lambda e: e[1][1])
                    compound = [d[1][0] for d in fields]
                    compound = [self.humanReadableDType(d) for d in compound]
                    return "compound(%s)" % ", ".join(compound)
        elif numpy.issubdtype(dtype, numpy.integer):
            enumType = h5py.check_dtype(enum=dtype)
            if enumType is not None:
                return "enum"

        text = str(dtype.newbyteorder('N'))
        if numpy.issubdtype(dtype, numpy.floating):
            if hasattr(numpy, "float128") and dtype == numpy.float128:
                text = "float80"
                if full:
                    text += " (padding 128bits)"
            elif hasattr(numpy, "float96") and dtype == numpy.float96:
                text = "float80"
                if full:
                    text += " (padding 96bits)"

        if full:
            if dtype.byteorder == "<":
                text = "Little-endian " + text
            elif dtype.byteorder == ">":
                text = "Big-endian " + text
            elif dtype.byteorder == "=":
                text = "Native " + text

        dtype = dtype.newbyteorder('N')
        return text

    def humanReadableHdf5Type(self, dataset):
        """Format the internal HDF5 type as a string"""
        t = dataset.id.get_type()
        class_ = t.get_class()
        if class_ == h5py.h5t.NO_CLASS:
            return "NO_CLASS"
        elif class_ == h5py.h5t.INTEGER:
            return "INTEGER"
        elif class_ == h5py.h5t.FLOAT:
            return "FLOAT"
        elif class_ == h5py.h5t.TIME:
            return "TIME"
        elif class_ == h5py.h5t.STRING:
            charset = t.get_cset()
            strpad = t.get_strpad()
            text = ""

            if strpad == h5py.h5t.STR_NULLTERM:
                text += "NULLTERM"
            elif strpad == h5py.h5t.STR_NULLPAD:
                text += "NULLPAD"
            elif strpad == h5py.h5t.STR_SPACEPAD:
                text += "SPACEPAD"
            else:
                text += "UNKNOWN_STRPAD"

            if t.is_variable_str():
                text += " VARIABLE"

            if charset == h5py.h5t.CSET_ASCII:
                text += " ASCII"
            elif charset == h5py.h5t.CSET_UTF8:
                text += " UTF8"
            else:
                text += " UNKNOWN_CSET"

            return text + " STRING"
        elif class_ == h5py.h5t.BITFIELD:
            return "BITFIELD"
        elif class_ == h5py.h5t.OPAQUE:
            return "OPAQUE"
        elif class_ == h5py.h5t.COMPOUND:
            return "COMPOUND"
        elif class_ == h5py.h5t.REFERENCE:
            return "REFERENCE"
        elif class_ == h5py.h5t.ENUM:
            return "ENUM"
        elif class_ == h5py.h5t.VLEN:
            return "VLEN"
        elif class_ == h5py.h5t.ARRAY:
            return "ARRAY"
        else:
            return "UNKNOWN_CLASS"
Ejemplo n.º 9
0
__date__ = "10/10/2017"


import logging
import collections
from .. import qt
from .. import icons
from . import _utils
from .Hdf5Node import Hdf5Node
import silx.io.utils
from silx.gui.data.TextFormatter import TextFormatter
from ..hdf5.Hdf5Formatter import Hdf5Formatter

_logger = logging.getLogger(__name__)

_formatter = TextFormatter()
_hdf5Formatter = Hdf5Formatter(textFormatter=_formatter)
# FIXME: The formatter should be an attribute of the Hdf5Model


class Hdf5Item(Hdf5Node):
    """Subclass of :class:`qt.QStandardItem` to represent an HDF5-like
    item (dataset, file, group or link) as an element of a HDF5-like
    tree structure.
    """

    def __init__(self, text, obj, parent, key=None, h5Class=None, linkClass=None, populateAll=False):
        """
        :param str text: text displayed
        :param object obj: Pointer to a h5py-link object. See the `obj` attribute.
        """
Ejemplo n.º 10
0
 def createWidget(self, parent):
     widget = qt.QTextEdit(parent)
     widget.setTextInteractionFlags(qt.Qt.TextSelectableByMouse)
     widget.setAlignment(qt.Qt.AlignLeft | qt.Qt.AlignTop)
     self.__formatter = TextFormatter(parent)
     return widget
Ejemplo n.º 11
0
 def createWidget(self, parent):
     widget = qt.QTextEdit(parent)
     widget.setTextInteractionFlags(qt.Qt.TextSelectableByMouse)
     widget.setAlignment(qt.Qt.AlignLeft | qt.Qt.AlignTop)
     self.__formatter = TextFormatter(parent)
     return widget