summaryrefslogtreecommitdiff
path: root/silx/gui/dialog/DataFileDialog.py
blob: 7ff1258396b97722092b5e37cf1a53c66aa19563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""
This module contains an :class:`DataFileDialog`.
"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "14/02/2018"

import logging
from silx.gui import qt
from silx.gui.hdf5.Hdf5Formatter import Hdf5Formatter
import silx.io
from .AbstractDataFileDialog import AbstractDataFileDialog
from silx.third_party import enum
try:
    import fabio
except ImportError:
    fabio = None


_logger = logging.getLogger(__name__)


class _DataPreview(qt.QWidget):
    """Provide a preview of the selected image"""

    def __init__(self, parent=None):
        super(_DataPreview, self).__init__(parent)

        self.__formatter = Hdf5Formatter(self)
        self.__data = None
        self.__info = qt.QTableView(self)
        self.__model = qt.QStandardItemModel(self)
        self.__info.setModel(self.__model)
        self.__info.horizontalHeader().hide()
        self.__info.horizontalHeader().setStretchLastSection(True)
        layout = qt.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.__info)
        self.setLayout(layout)

    def colormap(self):
        return None

    def setColormap(self, colormap):
        # Ignored
        pass

    def sizeHint(self):
        return qt.QSize(200, 200)

    def setData(self, data, fromDataSelector=False):
        self.__info.setEnabled(data is not None)
        if data is None:
            self.__model.clear()
        else:
            self.__model.clear()

            if silx.io.is_dataset(data):
                kind = "Dataset"
            elif silx.io.is_group(data):
                kind = "Group"
            elif silx.io.is_file(data):
                kind = "File"
            else:
                kind = "Unknown"

            headers = []

            basename = data.name.split("/")[-1]
            if basename == "":
                basename = "/"
            headers.append("Basename")
            self.__model.appendRow([qt.QStandardItem(basename)])
            headers.append("Kind")
            self.__model.appendRow([qt.QStandardItem(kind)])
            if hasattr(data, "dtype"):
                headers.append("Type")
                text = self.__formatter.humanReadableType(data)
                self.__model.appendRow([qt.QStandardItem(text)])
            if hasattr(data, "shape"):
                headers.append("Shape")
                text = self.__formatter.humanReadableShape(data)
                self.__model.appendRow([qt.QStandardItem(text)])
            if hasattr(data, "attrs") and "NX_class" in data.attrs:
                headers.append("NX_class")
                value = data.attrs["NX_class"]
                formatter = self.__formatter.textFormatter()
                old = formatter.useQuoteForText()
                formatter.setUseQuoteForText(False)
                text = self.__formatter.textFormatter().toString(value)
                formatter.setUseQuoteForText(old)
                self.__model.appendRow([qt.QStandardItem(text)])
            self.__model.setVerticalHeaderLabels(headers)
        self.__data = data

    def __imageItem(self):
        image = self.__plot.getImage("data")
        return image

    def data(self):
        if self.__data is not None:
            if hasattr(self.__data, "name"):
                # in case of HDF5
                if self.__data.name is None:
                    # The dataset was closed
                    self.__data = None
        return self.__data

    def clear(self):
        self.__data = None
        self.__info.setText("")


class DataFileDialog(AbstractDataFileDialog):
    """The `DataFileDialog` class provides a dialog that allow users to select
    any datasets or groups from an HDF5-like file.

    The `DataFileDialog` class enables a user to traverse the file system in
    order to select an HDF5-like file. Then to traverse the file to select an
    HDF5 node.

    .. image:: img/datafiledialog.png

    The selected data is any kind of group or dataset. It can be restricted
    to only existing datasets or only existing groups using
    :meth:`setFilterMode`. A callback can be defining using
    :meth:`setFilterCallback` to filter even more data which can be returned.

    Filtering data which can be returned by a `DataFileDialog` can be done like
    that:

    .. code-block:: python

        # Force to return only a dataset
        dialog = DataFileDialog()
        dialog.setFilterMode(DataFileDialog.FilterMode.ExistingDataset)

    .. code-block:: python

        def customFilter(obj):
            if "NX_class" in obj.attrs:
                return obj.attrs["NX_class"] in [b"NXentry", u"NXentry"]
            return False

        # Force to return an NX entry
        dialog = DataFileDialog()
        # 1st, filter out everything which is not a group
        dialog.setFilterMode(DataFileDialog.FilterMode.ExistingGroup)
        # 2nd, check what NX_class is an NXentry
        dialog.setFilterCallback(customFilter)

    Executing a `DataFileDialog` can be done like that:

    .. code-block:: python

        dialog = DataFileDialog()
        result = dialog.exec_()
        if result:
            print("Selection:")
            print(dialog.selectedFile())
            print(dialog.selectedUrl())
        else:
            print("Nothing selected")

    If the selection is a dataset you can access to the data using
    :meth:`selectedData`.

    If the selection is a group or if you want to read the selected object on
    your own you can use the `silx.io` API.

    .. code-block:: python

        url = dialog.selectedUrl()
        with silx.io.open(url) as data:
            pass

    Or by loading the file first

    .. code-block:: python

        url = dialog.selectedDataUrl()
        with silx.io.open(url.file_path()) as h5:
            data = h5[url.data_path()]

    Or by using `h5py` library

    .. code-block:: python

        url = dialog.selectedDataUrl()
        with h5py.File(url.file_path()) as h5:
            data = h5[url.data_path()]
    """

    class FilterMode(enum.Enum):
        """This enum is used to indicate what the user may select in the
        dialog; i.e. what the dialog will return if the user clicks OK."""

        AnyNode = 0
        """Any existing node from an HDF5-like file."""
        ExistingDataset = 1
        """An existing HDF5-like dataset."""
        ExistingGroup = 2
        """An existing HDF5-like group. A file root is a group."""

    def __init__(self, parent=None):
        AbstractDataFileDialog.__init__(self, parent=parent)
        self.__filter = DataFileDialog.FilterMode.AnyNode
        self.__filterCallback = None

    def selectedData(self):
        """Returns the selected data by using the :meth:`silx.io.get_data`
        API with the selected URL provided by the dialog.

        If the URL identify a group of a file it will raise an exception. For
        group or file you have to use on your own the API :meth:`silx.io.open`.

        :rtype: numpy.ndarray
        :raise ValueError: If the URL do not link to a dataset
        """
        url = self.selectedUrl()
        return silx.io.get_data(url)

    def _createPreviewWidget(self, parent):
        previewWidget = _DataPreview(parent)
        previewWidget.setSizePolicy(qt.QSizePolicy.Expanding, qt.QSizePolicy.Expanding)
        return previewWidget

    def _createSelectorWidget(self, parent):
        # There is no selector
        return None

    def _createPreviewToolbar(self, parent, dataPreviewWidget, dataSelectorWidget):
        # There is no toolbar
        return None

    def _isDataSupportable(self, data):
        """Check if the selected data can be supported at one point.

        If true, the data selector will be checked and it will update the data
        preview. Else the selecting is disabled.

        :rtype: bool
        """
        # Everything is supported
        return True

    def _isFabioFilesSupported(self):
        # Everything is supported
        return False

    def _isDataSupported(self, data):
        """Check if the data can be returned by the dialog.

        If true, this data can be returned by the dialog and the open button
        will be enabled. If false the button will be disabled.

        :rtype: bool
        """
        if self.__filter == DataFileDialog.FilterMode.AnyNode:
            accepted = True
        elif self.__filter == DataFileDialog.FilterMode.ExistingDataset:
            accepted = silx.io.is_dataset(data)
        elif self.__filter == DataFileDialog.FilterMode.ExistingGroup:
            accepted = silx.io.is_group(data)
        else:
            raise ValueError("Filter %s is not supported" % self.__filter)
        if not accepted:
            return False
        if self.__filterCallback is not None:
            try:
                return self.__filterCallback(data)
            except Exception:
                _logger.error("Error while executing custom callback", exc_info=True)
                return False
        return True

    def setFilterCallback(self, callback):
        """Set the filter callback. This filter is applied only if the filter
        mode (:meth:`filterMode`) first accepts the selected data.

        It is not supposed to be set while the dialog is being used.

        :param callable callback: Define a custom function returning a boolean
            and taking as argument an h5-like node. If the function returns true
            the dialog can return the associated URL.
        """
        self.__filterCallback = callback

    def setFilterMode(self, mode):
        """Set the filter mode.

        It is not supposed to be set while the dialog is being used.

        :param DataFileDialog.FilterMode mode: The new filter.
        """
        self.__filter = mode

    def fileMode(self):
        """Returns the filter mode.

        :rtype: DataFileDialog.FilterMode
        """
        return self.__filter

    def _displayedDataInfo(self, dataBeforeSelection, dataAfterSelection):
        """Returns the text displayed under the data preview.

        This zone is used to display error in case or problem of data selection
        or problems with IO.

        :param numpy.ndarray dataAfterSelection: Data as it is after the
            selection widget (basically the data from the preview widget)
        :param numpy.ndarray dataAfterSelection: Data as it is before the
            selection widget (basically the data from the browsing widget)
        :rtype: bool
        """
        return u""