shiftcorrection.py 4.8 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
# 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__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "02/10/2018"

30 31
from Orange.widgets import gui
from silx.gui import qt
32
from Orange.widgets.widget import OWWidget
33
from Orange.canvas.registry.description import InputSignal, OutputSignal
34 35
from id06workflow.core.experiment import Experiment
from id06workflow.core.types import _Image
36
from id06workflow.gui.shifcorrection import ShiftCorrectionWidget
37
from id06workflow.core.experiment.operation.shift import Shift
38 39 40
import logging

_logger = logging.getLogger(__file__)
41 42 43 44


class ShiftCorrectionOW(OWWidget):
    """
45 46
    Widget used to apply a shift correction. User can define two points which 
     is the same position on browsing dataset.
47
    """
48

49
    name = "Shift correction"
50 51
    id = "orange.widgets.id06workflow.shiftcorrection"
    description = "Apply shift correction on the dataset"
52
    icon = "icons/shift_correction.svg"
53 54 55 56 57

    priority = 10
    category = "esrfWidgets"
    keywords = ["dataset", "shift", "correction"]

58
    inputs = [InputSignal(name="data", type=Experiment, handler='_process')]
59 60
    outputs = [OutputSignal(name="data", type=Experiment),
               OutputSignal(name="image", type=_Image)]
61 62 63 64

    want_main_area = True
    resizing_enabled = True
    compress_signal = False
65 66 67

    def __init__(self):
        super().__init__()
68
        self._editedExperiment = None
69
        self._widget = ShiftCorrectionWidget(parent=self)
70 71
        self._progress = gui.ProgressBar(self, 100)
        self._widget.sigProgress.connect(self._setProgressValue)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        layout = gui.vBox(self.mainArea, 'data selection').layout()
        layout.addWidget(self._widget)
        layout.addWidget(self._widget)

        # buttons
        types = qt.QDialogButtonBox.Ok
        self._buttons = qt.QDialogButtonBox(parent=self)
        self._buttons.setStandardButtons(types)
        self._updateButton = qt.QPushButton("Update")
        self._buttons.addButton(self._updateButton, qt.QDialogButtonBox.ActionRole)
        self._updateButton.pressed.connect(self._widget._updateShift)

        layout.addWidget(self._buttons)

        self._buttons.hide()
        self._buttons.accepted.connect(self.validate)

        # expose API
        self.setShift = self._widget.setShift

92 93 94
    def _setProgressValue(self, value):
        self._progress.widget.progressBarSet(value)

95 96 97 98 99
    def _process(self, experiment):
        assert isinstance(experiment, (Experiment, type(None)))
        if experiment.dataset is None or experiment.dataset.is_valid() is False:
            _logger.warning('cannot process shift correction, need a valid dataset for this')
        else:
100
            _logger.info('Receive some data for shift application')
101
            self._editedExperiment = experiment
102
            self._widget.setExperiment(experiment)
103 104 105 106
            self._buttons.show()
            self.show()

    def validate(self):
107 108 109 110 111
        if self._widget.isProcessing() is True:
            _logger.error('you should wait the end of the processing before'
                          'validation')
            return
        self._progress.finish()
112 113 114 115 116 117 118 119 120 121 122
        if self._editedExperiment is not None:
            self._widget._updateShift()
            if self._widget.getShift() != (0, 0, 0):
                self._widget.getOperation().apply()

            self.send("data", self._editedExperiment)
            image = _Image(self._widget._sumPlot.getActiveImage(just_legend=False),
                           'shift_correction')
            self.send("image",
                      image)
            # TODO: clear widget to avoid reprocessing ?
123
            OWWidget.accept(self)