PCAWidget.py 3.83 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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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"
29
__date__ = "30/01/2020"
30
31
32
33
34
35
36
37
38
39
40

import numpy

from silx.gui import qt
from silx.gui.plot import Plot1D

from darfix.core.blindSourceSeparation import BSS

from .operationThread import OperationThread


41
class PCAWidget(qt.QMainWindow):
42
    """
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
43
    Widget to apply PCA to a set of images and plot the eigenvalues found.
44
    """
45
    sigComputed = qt.Signal()
46
47
48
49
50

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

        self._plot = Plot1D()
51
        self._plot.setDataMargins(0.05, 0.05, 0.05, 0.05)
52
53
54
55
56
57
58
59
60
61
62
63
64

        maxNComponentsLabel = qt.QLabel("Max number of components:")
        self.maxNumComp = qt.QLineEdit("")
        self.maxNumComp.setToolTip("Maximum number of components to compute")
        self.maxNumComp.setValidator(qt.QDoubleValidator())
        self.computeB = qt.QPushButton("Compute")
        widget = qt.QWidget(parent=self)
        layout = qt.QGridLayout()
        layout.addWidget(maxNComponentsLabel, 0, 0, 1, 1)
        layout.addWidget(self.maxNumComp, 0, 1, 1, 1)
        layout.addWidget(self.computeB, 0, 2, 1, 1)
        layout.addWidget(self._plot, 1, 0, 1, 3)
        widget.setLayout(layout)
65
        widget.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Minimum)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        self.setCentralWidget(widget)
        self._plot.hide()

    def _computePCA(self):
        self.computeB.setEnabled(False)
        try:
            txt = self.maxNumComp.text()
            if txt != "":
                maxNumComp = float(self.maxNumComp.text())
            else:
                maxNumComp = None
            self._thread = OperationThread(self.BSS.PCA)
            self._thread.setArgs(None, maxNumComp)
            self._thread.finished.connect(self._updateData)
            self._thread.start()
        except Exception as e:
            self.computeB.setEnabled(True)
            raise e
84
85
86

    def setDataset(self, dataset):
        """
87
        Dataset setter. Starts BSS class and initializes thread.
88
89
90
91
92

        :param Dataset dataset: dataset
        """
        self.dataset = dataset
        self.BSS = BSS(self.dataset.hi_data)
93
        self.computeB.pressed.connect(self._computePCA)
94
95
96

    def _updateData(self):
        """
Julia Garriga Ferrer's avatar
Julia Garriga Ferrer committed
97
        Plots the eigenvalues.
98
99
        """
        self._thread.finished.disconnect(self._updateData)
100
        self.computeB.setEnabled(True)
101
102
        mean, vecs, vals = self._thread.data
        vals = [item for sublist in vals for item in sublist]
103
        self._plot.show()
104
        self._plot.addCurve(numpy.arange(len(vals)), vals, symbol='.', linestyle=' ')
105
        self.sigComputed.emit()