Commit 1c4c59a9 authored by Julia Garriga Ferrer's avatar Julia Garriga Ferrer
Browse files

[gui][pca] Adds widget to see vals computed with pca

parent 30a14a85
# 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"
__date__ = "03/12/2019"
import numpy
from silx.gui import qt
from silx.gui.colors import Colormap
from silx.gui.plot import Plot1D
from darfix.core.blindSourceSeparation import BSS
from .operationThread import OperationThread
class PCAWidget(qt.QWidget):
"""
Widget to apply PCA to a set of images and ploat the eigenvalues found.
"""
signalComputed = qt.Signal()
def __init__(self, parent=None):
qt.QWidget.__init__(self, parent)
self.setLayout(qt.QVBoxLayout())
self._plot = Plot1D()
# self._plot.setColormap(Colormap(name='jet', normalization='linear'))
self.layout().addWidget(self._plot)
def setDataset(self, dataset):
"""
Dataset setter. Saves the dataset and updates the stack with the dataset
data
:param Dataset dataset: dataset
"""
self.dataset = dataset
print(self.dataset.hi_data.shape)
self.BSS = BSS(self.dataset.hi_data)
self._thread = OperationThread(self.BSS.PCA)
self.computePCA()
def computePCA(self):
"""
Function that starts the thread to compute the background
substraction.
"""
self._thread.finished.connect(self._updateData)
self._thread.start()
def _updateData(self):
"""
Updates the stack with the data computed in the thread
"""
self._thread.finished.disconnect(self._updateData)
mean, vecs, vals = self._thread.data
vals = [item for sublist in vals for item in sublist]
self._plot.addCurve(numpy.arange(len(vals)), vals, symbol='.', linestyle=' ')
self.signalComputed.emit()
def clearStack(self):
self._sv.setStack(None)
def setStack(self, *arg, **kwargs):
"""
Sets the data passed as aguments in the stack.
Mantains the current frame showed in the view.
"""
nframe = self._sv.getFrameNumber()
self._sv.setStack(*arg, **kwargs)
self._sv.setFrameNumber(nframe)
......@@ -109,7 +109,7 @@ class BackgroundSubstractionWidget(qt.QWidget):
def setStack(self, *arg, **kwargs):
"""
Sets the data passed as aguments in the stack.
Sets the data passed as arguments in the stack.
Mantains the current frame showed in the view.
"""
nframe = self._sv.getFrameNumber()
......
# 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.
#
# ###########################################################################*/
"""Example showing the widget :mod:`~darfix.gui.blindSourceSeparation.BlindSourceSeparation`.
"""
__authors__ = ["J. Garriga"]
__license__ = "MIT"
__date__ = "03/12/2019"
import glob
import numpy
import cv2
from silx.gui import qt
from darfix.test.utils import createDataset
from darfix.gui.PCAWidget import PCAWidget
def exec_():
qapp = qt.QApplication([])
w = PCAWidget()
images = glob.glob("figures/*")
stack = []
for i, image in enumerate(images):
im = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
stack.append(im)
num_images = 100
n_z = [15, 30, 45, 60, 75, 95]
# a = range(2,2+len(stack))
a = [1.0 for img in stack]
def J(z):
img = numpy.zeros(stack[0].shape, dtype=numpy.float)
for i, image in enumerate(stack):
G = (a[i] / numpy.sqrt(2 * numpy.pi * 10)) * numpy.exp(-0.5 * ((z - n_z[i]) ** 2) / 100)
img += G * numpy.array(image, dtype=numpy.float)
# img += abs(numpy.random.normal(0, 10, im.shape).reshape(im.shape))
return img
# Construct the input matrix
data = []
for i in numpy.arange(num_images):
data.append(J(i))
dataset = createDataset(data=data)
w.setDataset(dataset)
w.show()
qapp.exec_()
if __name__ == "__main__":
exec_()
\ No newline at end of file
# 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"
__date__ = "03/12/2019"
from Orange.widgets.widget import OWWidget, Input, Output
from darfix.gui.PCAWidget import PCAWidget
from darfix.core.dataset import Dataset
class PCAWidgetOW(OWWidget):
"""
Widget that computes the background substraction from a dataset
"""
name = "PCA"
icon = "icons/bss.png"
want_main_area = False
# Inputs
class Inputs:
dataset = Input("dataset", Dataset)
# Outputs
class Outputs:
dataset = Output("dataset", Dataset)
def __init__(self):
super().__init__()
self._widget = PCAWidget(parent=self)
self._widget.signalComputed.connect(self._show)
self.controlArea.layout().addWidget(self._widget)
@Inputs.dataset
def setDataset(self, dataset):
if dataset:
self._widget.setDataset(dataset=dataset)
self.Outputs.dataset.send(dataset)
def _show(self):
self.open()
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