Commit ba736506 authored by payno's avatar payno
Browse files

Merge branch 'save_components' into 'master'

Save components

See merge request julia.garriga/darfix!42
parents edf9704a 98603df5
Pipeline #21222 passed with stage
in 2 minutes and 17 seconds
...@@ -89,7 +89,8 @@ class Dataset(qt.QObject): ...@@ -89,7 +89,8 @@ class Dataset(qt.QObject):
self.dark_filename = dark_filename self.dark_filename = dark_filename
self.filenames = filenames self.filenames = filenames
self.__dims = AcquisitionDims() self.__dims = AcquisitionDims()
self._dimensions_values = None # Keys: dimensions names, values: dimensions values
self._dimensions_values = {}
# Initialize data # Initialize data
self._file_series = fabio.open_series(first_filename=raw_filename, self._file_series = fabio.open_series(first_filename=raw_filename,
...@@ -325,7 +326,7 @@ class Dataset(qt.QObject): ...@@ -325,7 +326,7 @@ class Dataset(qt.QObject):
unique values. unique values.
""" """
self.__dims.clear() self.__dims.clear()
self._dimensions_values = None self._dimensions_values = {}
keys = numpy.array(list(self.metadata[0].get_keys(kind))) keys = numpy.array(list(self.metadata[0].get_keys(kind)))
values = numpy.array([[data.get_value(kind=kind, name=key)[0] for data values = numpy.array([[data.get_value(kind=kind, name=key)[0] for data
...@@ -357,18 +358,14 @@ class Dataset(qt.QObject): ...@@ -357,18 +358,14 @@ class Dataset(qt.QObject):
:returns: array_like :returns: array_like
""" """
if self._dimensions_values is None: if not self._dimensions_values:
data = self.metadata data = self.metadata
# Use only metadata of high intensity data for dimension in self.__dims:
if self._filter_data: values = numpy.empty((len(data)))
data = numpy.array(self.metadata)[self.threshold] for row, metadata_frame in enumerate(data):
values = numpy.empty((self.__dims.ndim, len(data))) values[row] = (metadata_frame.get_value(kind=dimension[1].kind,
for row, metadata_frame in enumerate(data): name=dimension[1].name)[0])
for dimension in self.__dims: self._dimensions_values[dimension[1].name] = values
val = metadata_frame.get_value(kind=dimension[1].kind, name=dimension[1].name)
values[dimension[0]][row] = val[0]
self._dimensions_values = values
return self._dimensions_values return self._dimensions_values
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
......
...@@ -42,7 +42,7 @@ class PCAWidget(qt.QMainWindow): ...@@ -42,7 +42,7 @@ class PCAWidget(qt.QMainWindow):
""" """
Widget to apply PCA to a set of images and plot the eigenvalues found. Widget to apply PCA to a set of images and plot the eigenvalues found.
""" """
signalComputed = qt.Signal() sigComputed = qt.Signal()
def __init__(self, parent=None): def __init__(self, parent=None):
qt.QWidget.__init__(self, parent) qt.QWidget.__init__(self, parent)
...@@ -102,4 +102,4 @@ class PCAWidget(qt.QMainWindow): ...@@ -102,4 +102,4 @@ class PCAWidget(qt.QMainWindow):
vals = [item for sublist in vals for item in sublist] vals = [item for sublist in vals for item in sublist]
self._plot.show() self._plot.show()
self._plot.addCurve(numpy.arange(len(vals)), vals, symbol='.', linestyle=' ') self._plot.addCurve(numpy.arange(len(vals)), vals, symbol='.', linestyle=' ')
self.signalComputed.emit() self.sigComputed.emit()
...@@ -26,26 +26,21 @@ ...@@ -26,26 +26,21 @@
__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):
""" """
Widget to apply blind source separation. Widget to apply blind source separation.
""" """
signalComputed = qt.Signal()
def __init__(self, parent=None): def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent) qt.QMainWindow.__init__(self, parent)
...@@ -88,7 +83,10 @@ class BSSWidget(qt.QMainWindow): ...@@ -88,7 +83,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 +108,6 @@ class BSSWidget(qt.QMainWindow): ...@@ -110,88 +108,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 +129,11 @@ class BSSWidget(qt.QMainWindow): ...@@ -213,11 +129,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 +144,17 @@ class BSSWidget(qt.QMainWindow): ...@@ -228,25 +144,17 @@ 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)
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.zeros((self.dataset.nframes, n_comp))
# Set actual values of W where threshold of filter is True
W[self.dataset.threshold] = self.W
self.W = W
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 +175,3 @@ class BSSWidget(qt.QMainWindow): ...@@ -267,165 +175,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)
...@@ -45,7 +45,7 @@ class DatasetSelectionWidget(qt.QTabWidget): ...@@ -45,7 +45,7 @@ class DatasetSelectionWidget(qt.QTabWidget):
If both options are filled up, only the files in the list of filenames If both options are filled up, only the files in the list of filenames
are read. are read.
""" """
signalProgress = qt.Signal(int) sigProgressChanged = qt.Signal(int)
def __init__(self, parent=None): def __init__(self, parent=None):
qt.QTabWidget.__init__(self, parent) qt.QTabWidget.__init__(self, parent)
...@@ -90,7 +90,7 @@ class DatasetSelectionWidget(qt.QTabWidget): ...@@ -90,7 +90,7 @@ class DatasetSelectionWidget(qt.QTabWidget):
self._dataset = Dataset(filenames=self._rawFilesData.getFiles(), self._dataset = Dataset(filenames=self._rawFilesData.getFiles(),
dark_filename=self._darkFilenameData.getFilename(), dark_filename=self._darkFilenameData.getFilename(),
load_data=False, filter_data=self._filterData) load_data=False, filter_data=self._filterData)
self._dataset.signalProgress.connect(self.updateProgress) self._dataset.sigProgressChanged.connect(self.updateProgress)
_logger.info("Loading data urls") _logger.info("Loading data urls")
self._dataset.load_data(0.3) self._dataset.load_data(0.3)
_logger.info("Loading data") _logger.info("Loading data")
...@@ -102,7 +102,7 @@ class DatasetSelectionWidget(qt.QTabWidget): ...@@ -102,7 +102,7 @@ class DatasetSelectionWidget(qt.QTabWidget):
return self._dataset return self._dataset
def updateProgress(self, progress): def updateProgress(self, progress):
self.signalProgress.emit(progress) self.sigProgressChanged.emit(progress)
def __filterData(self, filterData): def __filterData(self, filterData):
self._filterData = filterData self._filterData = filterData
......
# 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_components
class DisplayComponentsWidget(qt.QMainWindow):
"""
Widget to apply blind source separation.
"""
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)