Commit 48a26c39 authored by Julia Garriga Ferrer's avatar Julia Garriga Ferrer
Browse files

[gui][bss] Incorpore displayComponentsWidget

parent ee69ce7e
...@@ -26,19 +26,15 @@ ...@@ -26,19 +26,15 @@
__authors__ = ["J. Garriga"] __authors__ = ["J. Garriga"]
__license__ = "MIT" __license__ = "MIT"
__date__ = "28/01/2020" __date__ = "07/02/2020"
import numpy import numpy
from silx.gui import qt 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.core.blindSourceSeparation import Method, BSS from darfix.core.blindSourceSeparation import Method, BSS
from .operationThread import OperationThread from .operationThread import OperationThread
from .displayComponentsWidget import DisplayComponentsWidget
class BSSWidget(qt.QMainWindow): class BSSWidget(qt.QMainWindow):
...@@ -88,7 +84,10 @@ class BSSWidget(qt.QMainWindow): ...@@ -88,7 +84,10 @@ class BSSWidget(qt.QMainWindow):
self.splitter = qt.QSplitter(qt.Qt.Vertical) self.splitter = qt.QSplitter(qt.Qt.Vertical)
self.splitter.addWidget(top_widget) self.splitter.addWidget(top_widget)
self.setCentralWidget(self.splitter) self.setCentralWidget(self.splitter)
self._init_plots()
self._displayComponentsWidget = DisplayComponentsWidget()
self.splitter.addWidget(self._displayComponentsWidget)
self._displayComponentsWidget.hide()
self.computeButton.clicked.connect(self._computeBSS) self.computeButton.clicked.connect(self._computeBSS)
self.detectButton.clicked.connect(self._detectComp) self.detectButton.clicked.connect(self._detectComp)
...@@ -110,88 +109,6 @@ class BSSWidget(qt.QMainWindow): ...@@ -110,88 +109,6 @@ class BSSWidget(qt.QMainWindow):
self.BSS = BSS(self.dataset.hi_data) self.BSS = BSS(self.dataset.hi_data)
self.computeButton.setEnabled(True) self.computeButton.setEnabled(True)
self.detectButton.setEnabled(True) self.detectButton.setEnabled(True)
self.bottom_widget.hide()
self._activateCurvesPlot(True)
if self.dataset.reshaped_data is not None:
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.dataset.dims:
self._plot_rocking_curves.addDimension(dimension[1].name)
else:
self._plot_rocking_curves.showMotors(False)
self._toolbar.hide()
def _init_plots(self):
"""
Initialize the plots to show in the widget: a stackview window and a plot1d.
The first plot will be used to show the components found with the bss, the
second one will show the rocking curves and, if so, let the user filter them
by the moving motor.
"""
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.bottom_widget.setLayout(layout)
self.bottom_widget.hide()
self.splitter.addWidget(self.bottom_widget)
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 _computeBSS(self): def _computeBSS(self):
""" """
...@@ -213,11 +130,11 @@ class BSSWidget(qt.QMainWindow): ...@@ -213,11 +130,11 @@ class BSSWidget(qt.QMainWindow):
raise ValueError('BSS method not managed') raise ValueError('BSS method not managed')
self._thread.setArgs(n_comp) self._thread.setArgs(n_comp)
self._thread.finished.connect(self._setPlot) self._thread.finished.connect(self._displayComponents)
self._thread.start() self._thread.start()
def _setPlot(self): def _displayComponents(self):
self._thread.finished.disconnect(self._setPlot) self._thread.finished.disconnect(self._displayComponents)
comp, self.W = self._thread.data comp, self.W = self._thread.data
n_comp = int(self.nComponentsLE.text()) n_comp = int(self.nComponentsLE.text())
if comp.shape[0] < n_comp: if comp.shape[0] < n_comp:
...@@ -228,25 +145,10 @@ class BSSWidget(qt.QMainWindow): ...@@ -228,25 +145,10 @@ class BSSWidget(qt.QMainWindow):
msg.setStandardButtons(qt.QMessageBox.Ok) msg.setStandardButtons(qt.QMessageBox.Ok)
msg.exec_() msg.exec_()
self.comp = comp.reshape(n_comp, self.dataset.data.shape[1], self.dataset.data.shape[2]) self.comp = comp.reshape(n_comp, self.dataset.data.shape[1], self.dataset.data.shape[2])
self._sv_components.setStack(self.comp) self._displayComponentsWidget.show()
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.dataset.reshaped_data is not None:
values = self.dataset.get_dimensions_values().astype(numpy.float)
colormap = Colormap(name='jet', normalization='linear')
self._scatter_rocking_curves.setData(values[0], values[1], self.W.T[0])
self._scatter_rocking_curves.setColormap(colormap)
self._scatter_rocking_curves.getPlotWidget().setGraphXLabel(self.dataset.dims.get(0).name)
self._scatter_rocking_curves.getPlotWidget().setGraphYLabel(self.dataset.dims.get(1).name)
self._scatter_rocking_curves.resetZoom()
self._plot_rocking_curves.getPlot().setActiveCurve("0")
self.bottom_widget.show()
self.computeButton.setEnabled(True) self.computeButton.setEnabled(True)
self.nComponentsLE.setEnabled(True) self.nComponentsLE.setEnabled(True)
self._displayComponentsWidget.setComponents(self.comp, self.W, self.dataset.get_dimensions_values())
def _detectComp(self): def _detectComp(self):
txt = self.maxNumComp.text() txt = self.maxNumComp.text()
...@@ -267,165 +169,3 @@ class BSSWidget(qt.QMainWindow): ...@@ -267,165 +169,3 @@ class BSSWidget(qt.QMainWindow):
components = len(vals[vals > 0.01]) components = len(vals[vals > 0.01])
self.detectButton.setEnabled(True) self.detectButton.setEnabled(True)
self.nComponentsLE.setText(str(components)) self.nComponentsLE.setText(str(components))
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 = self.dataset.get_dimensions_values().astype(numpy.float)
if self.dataset.reshaped_data is not None:
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 = self.dataset.get_dimensions_values().astype(numpy.float)
if self.dataset.reshaped_data is not None:
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:
self._plot_rocking_curves.getPlot().setGraphYLabel(self._plot_rocking_curves.getDimensionsCB().currentText())
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.dataset.dims.get(self.dimension).unique_values) - 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:
W = numpy.take(self.W.T[i].reshape(self.dataset.dims.shape), 0, 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:
W = numpy.take(self.W.T[i].reshape(self.dataset.dims.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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment