summaryrefslogtreecommitdiff
path: root/silx/gui/plot/tools/profile/ScatterProfileToolBar.py
blob: fd21515e29a4ced01187e1812e25fd102a1fbb5a (plain)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2018 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.
#
# ###########################################################################*/
"""This module profile tools for scatter plots.
"""

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "28/06/2018"


import logging
import threading
import time

import numpy

try:
    from scipy.interpolate import LinearNDInterpolator
except ImportError:
    LinearNDInterpolator = None

    # Fallback using local Delaunay and matplotlib interpolator
    from silx.third_party.scipy_spatial import Delaunay
    import matplotlib.tri

from ._BaseProfileToolBar import _BaseProfileToolBar
from .... import qt
from ... import items


_logger = logging.getLogger(__name__)


# TODO support log scale


class _InterpolatorInitThread(qt.QThread):
    """Thread building a scatter interpolator

    This works in greedy mode in that the signal is only emitted
    when no other request is pending
    """

    sigInterpolatorReady = qt.Signal(object)
    """Signal emitted whenever an interpolator is ready

    It provides a 3-tuple (points, values, interpolator)
    """

    _RUNNING_THREADS_TO_DELETE = []
    """Store reference of no more used threads but still running"""

    def __init__(self):
        super(_InterpolatorInitThread, self).__init__()
        self._lock = threading.RLock()
        self._pendingData = None
        self._firstFallbackRun = True

    def discard(self, obj=None):
        """Wait for pending thread to complete and delete then

        Connect this to the destroyed signal of widget using this thread
        """
        if self.isRunning():
            self.cancel()
            self._RUNNING_THREADS_TO_DELETE.append(self)  # Keep a reference
            self.finished.connect(self.__finished)

    def __finished(self):
        """Handle finished signal of threads to delete"""
        try:
            self._RUNNING_THREADS_TO_DELETE.remove(self)
        except ValueError:
            _logger.warning('Finished thread no longer in reference list')

    def request(self, points, values):
        """Request new initialisation of interpolator

        :param numpy.ndarray points: Point coordinates (N, D)
        :param numpy.ndarray values: Values the N points (1D array)
        """
        with self._lock:
            # Possibly replace already pending data
            self._pendingData = points, values

        if not self.isRunning():
            self.start()

    def cancel(self):
        """Cancel any running/pending requests"""
        with self._lock:
            self._pendingData = 'cancelled'

    def run(self):
        """Run the init of the scatter interpolator"""
        if LinearNDInterpolator is None:
            self.run_matplotlib()
        else:
            self.run_scipy()

    def run_matplotlib(self):
        """Run the init of the scatter interpolator"""
        if self._firstFallbackRun:
            self._firstFallbackRun = False
            _logger.warning(
                "scipy.spatial.LinearNDInterpolator not available: "
                "Scatter plot interpolator initialisation can freeze the GUI.")

        while True:
            with self._lock:
                data = self._pendingData
                self._pendingData = None

            if data in (None, 'cancelled'):
                return

            points, values = data

            startTime = time.time()
            try:
                delaunay = Delaunay(points)
            except:
                _logger.warning(
                    "Cannot triangulate scatter data")
            else:
                with self._lock:
                    data = self._pendingData

                if data is not None:  # Break point
                    _logger.info('Interpolator discarded after %f s',
                                 time.time() - startTime)
                else:

                    x, y = points.T
                    triangulation = matplotlib.tri.Triangulation(
                        x, y, triangles=delaunay.simplices)

                    interpolator = matplotlib.tri.LinearTriInterpolator(
                        triangulation, values)

                    with self._lock:
                        data = self._pendingData

                    if data is not None:
                        _logger.info('Interpolator discarded after %f s',
                                     time.time() - startTime)
                    else:
                        # No other processing requested: emit the signal
                        _logger.info("Interpolator initialised in %f s",
                                     time.time() - startTime)

                        # Wrap interpolator to have same API as scipy's one
                        def wrapper(points):
                            return interpolator(*points.T)

                        self.sigInterpolatorReady.emit(
                            (points, values, wrapper))

    def run_scipy(self):
        """Run the init of the scatter interpolator"""
        while True:
            with self._lock:
                data = self._pendingData
                self._pendingData = None

            if data in (None, 'cancelled'):
                return

            points, values = data

            startTime = time.time()
            try:
                interpolator = LinearNDInterpolator(points, values)
            except:
                _logger.warning(
                    "Cannot initialise scatter profile interpolator")
            else:
                with self._lock:
                    data = self._pendingData

                if data is not None:  # Break point
                    _logger.info('Interpolator discarded after %f s',
                                 time.time() - startTime)
                else:
                    # First call takes a while, do it here
                    interpolator([(0., 0.)])

                    with self._lock:
                        data = self._pendingData

                    if data is not None:
                        _logger.info('Interpolator discarded after %f s',
                                     time.time() - startTime)
                    else:
                        # No other processing requested: emit the signal
                        _logger.info("Interpolator initialised in %f s",
                                     time.time() - startTime)
                        self.sigInterpolatorReady.emit(
                            (points, values, interpolator))


class ScatterProfileToolBar(_BaseProfileToolBar):
    """QToolBar providing scatter plot profiling tools

    :param parent: See :class:`QToolBar`.
    :param plot: :class:`~silx.gui.plot.PlotWidget` on which to operate.
    :param str title: See :class:`QToolBar`.
    """

    def __init__(self, parent=None, plot=None, title='Scatter Profile'):
        super(ScatterProfileToolBar, self).__init__(parent, plot, title)

        self.__nPoints = 1024
        self.__interpolator = None
        self.__interpolatorCache = None  # points, values, interpolator

        self.__initThread = _InterpolatorInitThread()
        self.destroyed.connect(self.__initThread.discard)
        self.__initThread.sigInterpolatorReady.connect(
            self.__interpolatorReady)

        roiManager = self._getRoiManager()
        if roiManager is None:
            _logger.error(
                "Error during scatter profile toolbar initialisation")
        else:
            roiManager.sigInteractiveModeStarted.connect(
                self.__interactionStarted)
            roiManager.sigInteractiveModeFinished.connect(
                self.__interactionFinished)
            if roiManager.isStarted():
                self.__interactionStarted(roiManager.getCurrentInteractionModeRoiClass())

    def __interactionStarted(self, roiClass):
        """Handle start of ROI interaction"""
        plot = self.getPlotWidget()
        if plot is None:
            return

        plot.sigActiveScatterChanged.connect(self.__activeScatterChanged)

        scatter = plot._getActiveItem(kind='scatter')
        legend = None if scatter is None else scatter.getLegend()
        self.__activeScatterChanged(None, legend)

    def __interactionFinished(self):
        """Handle end of ROI interaction"""
        plot = self.getPlotWidget()
        if plot is None:
            return

        plot.sigActiveScatterChanged.disconnect(self.__activeScatterChanged)

        scatter = plot._getActiveItem(kind='scatter')
        legend = None if scatter is None else scatter.getLegend()
        self.__activeScatterChanged(legend, None)

    def __activeScatterChanged(self, previous, legend):
        """Handle change of active scatter

        :param Union[str,None] previous:
        :param Union[str,None] legend:
        """
        self.__initThread.cancel()

        # Reset interpolator
        self.__interpolator = None

        plot = self.getPlotWidget()
        if plot is None:
            _logger.error("Associated PlotWidget no longer exists")

        else:
            if previous is not None:  # Disconnect signal
                scatter = plot.getScatter(previous)
                if scatter is not None:
                    scatter.sigItemChanged.disconnect(
                        self.__scatterItemChanged)

            if legend is not None:
                scatter = plot.getScatter(legend)
                if scatter is None:
                    _logger.error("Cannot retrieve active scatter")

                else:
                    scatter.sigItemChanged.connect(self.__scatterItemChanged)
                    points = numpy.transpose(numpy.array((
                        scatter.getXData(copy=False),
                        scatter.getYData(copy=False))))
                    values = scatter.getValueData(copy=False)

                    self.__updateInterpolator(points, values)

        # Refresh profile
        self.updateProfile()

    def __scatterItemChanged(self, event):
        """Handle update of active scatter plot item

        :param ItemChangedType event:
        """
        if event == items.ItemChangedType.DATA:
            self.__interpolator = None
            scatter = self.sender()
            if scatter is None:
                _logger.error("Cannot retrieve updated scatter item")

            else:
                points = numpy.transpose(numpy.array((
                    scatter.getXData(copy=False),
                    scatter.getYData(copy=False))))
                values = scatter.getValueData(copy=False)

                self.__updateInterpolator(points, values)

    # Handle interpolator init thread

    def __updateInterpolator(self, points, values):
        """Update used interpolator with new data"""
        if (self.__interpolatorCache is not None and
                len(points) == len(self.__interpolatorCache[0]) and
                numpy.all(numpy.equal(self.__interpolatorCache[0], points)) and
                numpy.all(numpy.equal(self.__interpolatorCache[1], values))):
            # Reuse previous interpolator
            _logger.info(
                'Scatter changed: Reuse previous interpolator')
            self.__interpolator = self.__interpolatorCache[2]

        else:
            # Interpolator needs update: Start background processing
            _logger.info(
                'Scatter changed: Rebuild interpolator')
            self.__interpolator = None
            self.__interpolatorCache = None
            self.__initThread.request(points, values)

    def __interpolatorReady(self, data):
        """Handle end of init interpolator thread"""
        points, values, interpolator = data
        self.__interpolator = interpolator
        self.__interpolatorCache = None if interpolator is None else data
        self.updateProfile()

    def hasPendingOperations(self):
        return self.__initThread.isRunning()

    # Number of points

    def getNPoints(self):
        """Returns the number of points of the profiles

        :rtype: int
        """
        return self.__nPoints

    def setNPoints(self, npoints):
        """Set the number of points of the profiles

        :param int npoints:
        """
        npoints = int(npoints)
        if npoints < 1:
            raise ValueError("Unsupported number of points: %d" % npoints)
        else:
            self.__nPoints = npoints

    # Overridden methods

    def computeProfileTitle(self, x0, y0, x1, y1):
        """Compute corresponding plot title

        :param float x0: Profile start point X coord
        :param float y0: Profile start point Y coord
        :param float x1: Profile end point X coord
        :param float y1: Profile end point Y coord
        :return: Title to use
        :rtype: str
        """
        if self.hasPendingOperations():
            return 'Pre-processing data...'

        else:
            return super(ScatterProfileToolBar, self).computeProfileTitle(
                x0, y0, x1, y1)

    def computeProfile(self, x0, y0, x1, y1):
        """Compute corresponding profile

        :param float x0: Profile start point X coord
        :param float y0: Profile start point Y coord
        :param float x1: Profile end point X coord
        :param float y1: Profile end point Y coord
        :return: (points, values) profile data or None
        """
        if self.__interpolator is None:
            return None

        nPoints = self.getNPoints()

        points = numpy.transpose((
            numpy.linspace(x0, x1, nPoints, endpoint=True),
            numpy.linspace(y0, y1, nPoints, endpoint=True)))

        values = self.__interpolator(points)

        if not numpy.any(numpy.isfinite(values)):
            return None  # Profile outside convex hull

        return points, values