displayComponentsWidget.py 14 KB
Newer Older
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2019 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.
#
# ###########################################################################*/


__authors__ = ["J. Garriga"]
__license__ = "MIT"
__date__ = "07/02/2020"

import numpy

from silx.gui import qt
from silx.gui import icons
from silx.gui.colors import Colormap
from silx.gui.plot import ScatterView, StackView, Plot1D
from silx.gui.widgets.FrameBrowser import HorizontalSliderWithBrowser

import darfix
from darfix.io.utils import write_process


class DisplayComponentsWidget(qt.QMainWindow):
    """
    Widget to apply blind source separation.
    """
    signalComputed = qt.Signal()

    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

        # Widget with the type of bss and the number of components to compute
        widget = qt.QWidget(self)
        self._sv_components = StackView(parent=self)
        self._sv_components.setColormap(Colormap(
            name=darfix.config.DEFAULT_COLORMAP_NAME,
            normalization='linear',
            vmin=None,
            vmax=None))
        self._sv_components.setGraphTitle("Components")
        self._sv_components.sigFrameChanged.connect(self._setCurve)

        self._plot_rocking_curves = PlotRockingCurves()
        self._plot_rocking_curves.sigFrameChanged.connect(self._updateFrameNumber)
        self._plot_rocking_curves.sigMotorChanged.connect(self._updateMotorAxis)
        self._plot_rocking_curves.sigActiveCurveChanged.connect(self._activeCurveChanged)

        self._scatter_rocking_curves = ScatterView()
        self._scatter_rocking_curves.hide()

        self._toolbar = qt.QToolBar(parent=self)
        self._toolbar.setIconSize(self._toolbar.iconSize() * 1.2)
        curves_icon = icons.getQIcon('darfix:gui/icons/curves')
        scatter_icon = icons.getQIcon('darfix:gui/icons/scatter')
        self.curves_action = qt.QAction(curves_icon, "Curves", self)
        self.curves_action.setCheckable(True)
        self.curves_action.setChecked(True)
        self.colormap_action = qt.QAction(scatter_icon, "Scatter", self)
        self.colormap_action.setCheckable(True)
        self.curves_action.toggled.connect(self._activateCurvesPlot)
        self.colormap_action.triggered.connect(self.curves_action.toggle)
        self._toolbar.addAction(self.curves_action)
        self._toolbar.addAction(self.colormap_action)
        self._toolbar.setOrientation(qt.Qt.Vertical)
        self._toolbar.hide()

        self.bottom_widget = qt.QWidget(self)
        layout = qt.QGridLayout()
        componentsLabel = qt.QLabel("Components")
        rockingCurvesLabel = qt.QLabel("Rocking curves")
        font = qt.QFont()
        font.setBold(True)
        componentsLabel.setFont(font)
        rockingCurvesLabel.setFont(font)
        layout.addWidget(componentsLabel, 1, 0, 1, 2, qt.Qt.AlignCenter)
        layout.addWidget(rockingCurvesLabel, 1, 3, 1, 2, qt.Qt.AlignCenter)
        layout.addWidget(self._sv_components, 2, 0, 2, 2)
        layout.addWidget(self._plot_rocking_curves, 2, 3, 2, 2)
        layout.addWidget(self._scatter_rocking_curves, 2, 3, 2, 2)
        layout.addWidget(self._toolbar, 2, 5, 2, 1)
        self.saveB = qt.QPushButton("Save")
        self.saveB.pressed.connect(self._saveComp)
        layout.addWidget(self.saveB, 3, 0)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def hideButton(self):
        self._computeB.hide()

    def showButton(self):
        self._computeB.show()

    def setComponents(self, components, W, dimensions):
        """
        Dataset setter. Saves the dataset and updates the stack with the dataset
        data

        :param Dataset dataset: dataset
        """
        self.components = components
        self.W = W
        self.dimensions = dimensions
        self._activateCurvesPlot(True)
        self._sv_components.setStack(self.components)
        self._plot_rocking_curves.getPlot().clear()
        self._plot_rocking_curves.getPlot().setGraphYLabel("Values")
        for i in range(len(self.W.T)):
            self._plot_rocking_curves.getPlot().addCurve(numpy.arange(len(self.W.T[i])),
                                                         self.W.T[i], legend=str(i))
        if self.dimensions:
            colormap = Colormap(name='jet', normalization='linear')
            keys = list(self.dimensions.keys())
            self._scatter_rocking_curves.getPlotWidget().setGraphXLabel(keys[0])
            self._scatter_rocking_curves.getPlotWidget().setGraphYLabel(keys[1])
            self._scatter_rocking_curves.setData(
                self.dimensions[keys[0]].astype(numpy.float),
                self.dimensions[keys[1]].astype(numpy.float), self.W.T[0])
            self._scatter_rocking_curves.setColormap(colormap)
            self._scatter_rocking_curves.resetZoom()

            self._plot_rocking_curves.showMotors(True)
            self._toolbar.show()
            self._plot_rocking_curves.resetDimensions()
            # Motor dimension and index
            self.dimension = 0
            self.index = 0
            for dimension in self.dimensions:
                self._plot_rocking_curves.addDimension(dimension)
        else:
            self._plot_rocking_curves.showMotors(False)
            self._toolbar.hide()

        self._plot_rocking_curves.getPlot().setActiveCurve("0")

    def _activateCurvesPlot(self, checked=False):
        if checked:
            self.colormap_action.setChecked(False)
            self._scatter_rocking_curves.hide()
            self._plot_rocking_curves.show()
        else:
            self._plot_rocking_curves.hide()
            self._scatter_rocking_curves.show()

    def _saveComp(self):
        write_process('/users/garrigaf/Documents/tests/Nexus/test_process1.hdf5',
                      'entry', self.dimensions, self.W, self.components, 1)

    def _activeCurveChanged(self, prev_legend=None, legend=None):
        if legend:
            self._setComponent(int(legend))

    def _setComponent(self, index=None):
        if index is not None:
            status = self._sv_components.blockSignals(True)
            self._sv_components.setFrameNumber(index)
            self._sv_components.blockSignals(status)
            values = numpy.array(list(self.dimensions.values())).astype(numpy.float)
            if self.dimensions:
                self._scatter_rocking_curves.setData(values[0], values[1], self.W.T[index])

    def _setCurve(self, index=-1):
        if index >= 0:
            status = self._plot_rocking_curves.blockSignals(True)
            self._plot_rocking_curves.getPlot().setActiveCurve(str(index))
            self._plot_rocking_curves.blockSignals(status)
            values = numpy.array(list(self.dimensions.values())).astype(numpy.float)
            if self.dimensions:
                self._scatter_rocking_curves.setData(values[0], values[1], self.W.T[index])

    def _updateMotorAxis(self, axis=-1):
        """
        Updates the motor to show the rocking curve from.

        :param int axis: 0 if no motor is chosen, else axis of the motor.
        """
        if axis == 0:
            self._plot_rocking_curves.getPlot().setGraphYLabel("Values")
            # Whole rocking curves are showed
            self._plot_rocking_curves.clear()
            for i in range(len(self.W.T)):
                self._plot_rocking_curves.addCurve(numpy.arange(len(self.W.T[i])), self.W.T[i], legend=str(i))
            self._plot_rocking_curves.getBrowser().setEnabled(False)
        elif axis != -1 and axis is not None:
            text = self._plot_rocking_curves.getDimensionsCB().currentText()
            self._plot_rocking_curves.getPlot().setGraphYLabel(text)
            self.dimension = axis - 1
            self._plot_rocking_curves.clear()
            self._plot_rocking_curves.getBrowser().setEnabled(True)
            self._plot_rocking_curves.getBrowser().setRange(0, len(self.dimensions[text]) - 1)
            self._plot_rocking_curves.getBrowser().setValue(0)
            for i in range(len(self.W.T)):
                """
                if numpy.any(self.dataset.li_data):
                    # If filter data is activated, the matrix W has reduced dimensionality, so reshaping is not possible
                    # Create empty array with shape the total number of frames
                    W = numpy.empty(self.dataset.threshold.shape)
                    # Set actual values of W where threshold of filter is True
                    W[self.dataset.threshold] = self.W.T[i]
                    # Take only the thresholds for the dimension
                    threshold = numpy.take(self.dataset.threshold.reshape(self.dataset.dims.shape), 0, axis=self.dimension)
                    # Reshape W into the dimensions and take the first element of the chosen dimension
                    W = numpy.take(W.reshape(self.dataset.dims.shape), 0, self.dimension)
                    # Take only the values of W that correspond to high intensity data
                    W = W[threshold]
                else:"""
                dimensions_shape = numpy.array(self.dimensions.values()).shape
                W = numpy.take(self.W.T[i].reshape(dimensions_shape), self.index, self.dimension)
                self._plot_rocking_curves.addCurve(numpy.arange(len(W)), W, legend=str(i))
        else:
            raise ValueError('Axis %s is not managed' % axis)

    def _updateFrameNumber(self, index):
        """Update the current plot.

        :param index: index of the frame to be displayed
        """
        self.index = index
        self._plot_rocking_curves.clear()
        for i in range(len(self.W.T)):
            """if numpy.any(self.dataset.li_data):
                # If filter data is activated, the matrix W has reduced dimensionality, so reshaping is not possible
                # Create empty array with shape the total number of frames
                W = numpy.empty(self.dataset.threshold.shape)
                # Set actual values of W where threshold of filter is True
                W[self.dataset.threshold] = self.W.T[i]
                # Take only the thresholds for the dimension
                threshold = numpy.take(self.dataset.threshold.reshape(self.dataset.dims.shape), self.index, axis=self.dimension)
                # Reshape W into the dimensions and take the chosen element and dimension
                W = numpy.take(W.reshape(self.dataset.dims.shape), self.index, self.dimension)
                # Take only the values of W that correspond to high intensity data
                W = W[threshold]
            else:"""
            dimensions_shape = numpy.array(self.dimensions.values()).shape
            W = numpy.take(self.W.T[i].reshape(dimensions_shape), self.index, self.dimension)
            self._plot_rocking_curves.addCurve(numpy.arange(len(W)), W, legend=str(i))


class PlotRockingCurves(qt.QMainWindow):

    sigFrameChanged = qt.Signal(int)
    sigMotorChanged = qt.Signal(int)
    sigActiveCurveChanged = qt.Signal(object, object)

    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

        self._plot = Plot1D()
        self._plot.setGraphTitle("Rocking curves")
        self._plot.setGraphXLabel("Image id")
        self._plot.sigActiveCurveChanged.connect(self.sigActiveCurveChanged)

        self._motors = qt.QWidget()
        self._motors.setLayout(self._motorsLayout())
        self._motors.hide()
        layout = qt.QVBoxLayout()
        layout.addWidget(self._plot)
        layout.addWidget(self._motors)

        centralWidget = qt.QWidget()
        centralWidget.setLayout(layout)
        self.setCentralWidget(centralWidget)

        self.addCurve = self._plot.addCurve

    def _motorsLayout(self):
        # If dataset data is reshaped
        # Creates widget to move through the motors dimensions
        layout = qt.QHBoxLayout()
        cb_label = qt.QLabel("Static motor:")
        self._cb = qt.QComboBox()
        self._cb.setCurrentIndex(0)
        # Default: all of the rocking curves are shown
        self._cb.addItem("None")
        browser_label = qt.QLabel("Motor index:")
        self._browser = HorizontalSliderWithBrowser()
        self._browser.setRange(0, 0)
        # Browser only activated with choosen dimension
        self._browser.setEnabled(False)

        # Connect browser
        self._browser.valueChanged[int].connect(self.sigFrameChanged)
        # Connect combobox
        self._cb.currentIndexChanged.connect(self.sigMotorChanged)

        layout.addWidget(cb_label)
        layout.addWidget(self._cb)
        layout.addWidget(browser_label)
        layout.addWidget(self._browser)

        return layout

    def getPlot(self):
        return self._plot

    def getBrowser(self):
        return self._browser

    def getDimensionsCB(self):
        return self._cb

    def clear(self):
        self._plot.clear()

    def showMotors(self, show=True):
        self._motors.setVisible(show)

    def addDimension(self, dimension):
        self._cb.addItem(dimension)

    def resetDimensions(self):
        for i in range(1, self._cb.count()):
            self._cb.removeItem(i)