File: IntegrationDialog.py

package info (click to toggle)
pyfai 0.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,460 kB
  • sloc: python: 49,743; lisp: 7,059; sh: 225; ansic: 165; makefile: 119
file content (365 lines) | stat: -rw-r--r-- 12,936 bytes parent folder | download | duplicates (2)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    Project: Azimuthal integration
#             https://github.com/silx-kit/pyFAI
#
#    Copyright (C) 2013-2018 European Synchrotron Radiation Facility, Grenoble, France
#
#    Principal author:       Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
#  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.

"""pyFAI-integrate

A graphical tool for performing azimuthal integration on series of files.

"""

__author__ = "Jérôme Kieffer"
__contact__ = "Jerome.Kieffer@ESRF.eu"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "16/10/2020"
__status__ = "development"

import logging
import json
import os.path as op
import time

logger = logging.getLogger(__name__)

from silx.gui import qt
from silx.gui import icons
from silx.gui import colors

from .. import worker as worker_mdl
from .widgets.WorkerConfigurator import WorkerConfigurator
from ..io import integration_config
from .utils import projecturl
from ..utils import get_ui_file
from ..app import integrate
from .. import containers
from pyFAI.gui.utils.eventutils import QtProxifier


class _ThreadSafeIntegrationProcess(QtProxifier):

    def is_interruption_requested(self):
        # NOTE: Not thread safe, but it usually do not call anything but only
        #       returns a boolean
        result = self._target().is_interruption_requested()
        return result

    def request_interruption(self):
        raise RuntimeError("Not supposed to be called")


class IntegrationProcess(qt.QDialog, integrate.IntegrationObserver):

    def __init__(self, parent=None):
        qt.QDialog.__init__(self, parent=parent)
        filename = get_ui_file("integration-process.ui")
        qt.loadUi(filename, self)

        integrate.IntegrationObserver.__init__(self)
        self.setWindowTitle("Processing...")
        self.__undisplayedResult = None
        self._displayResult.toggled.connect(self.__displayResultUpdated)
        self._plot.setDataMargins(0.05, 0.05, 0.05, 0.05)
        self.__firstPlot = True
        self.__lastDisplay = None
        self._progressBar.setFormat("Preprocessing...")
        self._cancelButton.clicked.connect(self.__interruptionRequested)

    def __interruptionRequested(self):
        self.setEnabled(False)
        self.request_interruption()

    def __resultReceived(self, result):
        isFiltered = not self._displayResult.isChecked()
        now = time.perf_counter()
        if self.__lastDisplay is not None:
            # Display a new result every 500ms, no more
            if now - self.__lastDisplay < 0.5:
                isFiltered = True
        if not isFiltered:
            self.__undisplayedResult = None
            self.__lastDisplay = now
            self.__displayResult(result, resetZoom=self.__firstPlot)
        else:
            self.__undisplayedResult = result
        self.__firstPlot = False

    def __displayResultUpdated(self):
        self._plot.setVisible(self._displayResult.isChecked())
        if self._displayResult.isChecked():
            if self.__undisplayedResult is not None:
                result = self.__undisplayedResult
                self.__undisplayedResult = None
                self.__displayResult(result, True)
        self.adjustSize()

    def __displayResult(self, result, resetZoom=False):
        self._plot.clear()
        if isinstance(result, containers.Integrate1dResult):
            self._plot.setGraphXLabel("Radial")
            self._plot.setGraphYLabel("Intensity")
            self._plot.addHistogram(
                legend="result1d",
                align="center",
                edges=result.radial,
                color="blue",
                histogram=result.intensity,
                resetzoom=False)
        elif isinstance(result, containers.Integrate2dResult):

            def computeLocation(result):
                # Assume that axes are linear
                if result.intensity.shape[1] > 1:
                    scaleX = (result.radial[-1] - result.radial[0]) / (result.intensity.shape[1] - 1)
                else:
                    scaleX = 1.0
                if result.intensity.shape[0] > 1:
                    scaleY = (result.azimuthal[-1] - result.azimuthal[0]) / (result.intensity.shape[0] - 1)
                else:
                    scaleY = 1.0
                halfPixel = 0.5 * scaleX, 0.5 * scaleY
                origin = (result.radial[0] - halfPixel[0], result.azimuthal[0] - halfPixel[1])
                return origin, (scaleX, scaleY)

            self._plot.setGraphXLabel("Radial")
            self._plot.setGraphYLabel("Azimuthal")
            origin, scale = computeLocation(result)
            colormap = colors.Colormap("inferno", normalization=colors.Colormap.LOGARITHM)
            self._plot.addImage(
                legend="result2d",
                data=result.intensity,
                origin=origin,
                scale=scale,
                colormap=colormap,
                resetzoom=False)
        else:
            logger.error("Unsupported result type %s", type(result))
        if resetZoom:
            self._plot.resetZoom()

    def worker_initialized(self, worker):
        """
        Called when the worker is initialized

        :param int data_count: Number of data to integrate
        """
        pass

    def processing_started(self, data_count):
        """
        Called before starting the full processing.

        :param int data_count: Number of data to integrate
        """
        self._progressBar.setRange(0, data_count + 1)

    def processing_data(self, data_info, approximate_count=None):
        """
        Start processing the data `data_info`

        :param DataInfo data_info: Contains data and metadata from the data
            to integrate
        :param int approximate_count: If set, the amount of total data to
            process have changed
        """
        if approximate_count is not None:
            self._progressBar.setRange(0, approximate_count + 1)
        self._progressBar.setValue(data_info.data_id)
        if data_info.source_filename is None:
            filename = ""
        elif len(data_info.source_filename) > 20:
            filename = op.basename(data_info.source_filename)
        else:
            filename = data_info.source_filename
        self._progressBar.setFormat("%s (%%p%%)..." % filename)

    def data_result(self, data_info, result):
        self.__resultReceived(result)

    def processing_interrupted(self):
        self.__was_interrupted = True

    def processing_succeeded(self):
        self.__was_interrupted = False

    def processing_finished(self):
        """Called when the full processing is finisehd."""
        self._progressBar.setValue(self._progressBar.maximum())
        self.__lastResult = None
        if self.__was_interrupted:
            self.reject()
        else:
            self.accept()

    def createObserver(self, qtSafe=True):
        """Returns a processing observer connected to this widget.

        :param bool qtSafe: If True the returned observer can be called from
            any thread. Else it have to be called from the main Qt thread.
        :rtype: integrate.IntegrationObserver
        """
        if qtSafe:
            return _ThreadSafeIntegrationProcess(self)
        else:
            self


class IntegrationDialog(qt.QWidget):
    """Dialog to configure an azimuthal integration.
    """

    batchProcessRequested = qt.Signal()

    def __init__(self, input_data=None, output_path=None, json_file=".azimint.json", context=None):
        qt.QWidget.__init__(self)
        filename = get_ui_file("integration-dialog.ui")
        qt.loadUi(filename, self)

        pyfaiIcon = icons.getQIcon("pyfai:gui/images/icon")
        self.setWindowIcon(pyfaiIcon)

        self.__context = context
        if context is not None:
            context.restoreWindowLocationSettings("main-window", self)

        self.__workerConfigurator = WorkerConfigurator(self._holder)
        self.__content = qt.QWidget(self)
        layout = qt.QVBoxLayout(self.__content)
        layout.addWidget(self.__workerConfigurator)

        self._holder.setWidget(self.__content)
        self._holder.minimumSizeHint = self.__minimumScrollbarSizeHint
        size = self.__content.minimumSizeHint()
        self._holder.setMaximumHeight(size.height() + 2)
        size = self.minimumSizeHint() - self._holder.minimumSizeHint() + size
        self.setMaximumHeight(size.height() + 2)

        self.input_data = input_data
        self.output_path = output_path

        self.json_file = json_file

        self.batch_processing.clicked.connect(self.__fireBatchProcess)
        self.save_json_button.clicked.connect(self.save_config)
        self.quit_button.clicked.connect(self.die)

        if self.json_file is not None:
            self.restore(self.json_file)

    def __minimumScrollbarSizeHint(self):
        size = self.__content.minimumSizeHint()
        extend = self.style().pixelMetric(qt.QStyle.PM_ScrollBarExtent)
        size = qt.QSize(size.width() + extend, 100)
        return size

    def closeEvent(self, event):
        context = self.__context
        if context is not None:
            self.__context.saveWindowLocationSettings("main-window", self)

    def __fireBatchProcess(self):
        self.batchProcessRequested.emit()

    def die(self):
        logger.debug("bye bye")
        self.deleteLater()

    def help(self):
        logger.debug("Please, help")
        url = projecturl.get_documentation_url("man/pyFAI-integrate.html")
        qt.QDesktopServices.openUrl(qt.QUrl(url))

    def get_config(self):
        """Read the configuration of the plugin and returns it as a dictionary

        :return: dict with all information.
        """
        config = self.__workerConfigurator.getConfig()
        return config

    def dump(self, filename=None):
        """
        Dump the status of the current widget to a file in JSON

        :param filename: path where to save the config
        :type filename: string
        :return: dict with configuration
        """
        to_save = self.get_config()
        if filename is None:
            filename = self.json_file
        if filename is not None:
            logger.info("Dump to %s", filename)
            try:
                with open(filename, "w") as myfile:
                    json.dump(to_save, myfile, indent=4)
            except IOError as error:
                logger.error("Error while saving config: %s", error)
            else:
                logger.debug("Saved")
        return to_save

    def restore(self, filename=".azimint.json"):
        """Restore from JSON file the status of the current widget

        :param filename: path where the config was saved
        :type filename: str
        """
        logger.debug("Restore from %s", filename)
        if not op.isfile(filename):
            logger.error("No such file: %s", filename)
            return
        with open(filename) as f:
            data = json.load(f)
        self.set_config(data)

    def set_config(self, dico):
        """Setup the widget from its description

        :param dico: dictionary with description of the widget
        :type dico: dict
        """
        dico = integration_config.normalize(dico)
        self.__workerConfigurator.setConfig(dico)

    def set_input_data(self, stack):
        self.input_data = stack

    def save_config(self):
        logger.debug("save_config")

        result = qt.QFileDialog.getSaveFileName(
            caption="Save configuration as json",
            directory=self.json_file,
            filter="Config (*.json)")
        if isinstance(result, tuple):
            # PyQt5 compatibility
            result = result[0]

        json_file = result
        if json_file:
            self.dump(json_file)