summaryrefslogtreecommitdiff
path: root/silx/gui/plot/stats/stats.py
blob: a81f7bb56a70da3ce3052b2aa28cf4ee2fb63b94 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2021 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 provides mechanism relative to stats calculation within a
:class:`PlotWidget`.
It also include the implementation of the statistics themselves.
"""

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "06/06/2018"


from collections import OrderedDict
from functools import lru_cache
import logging

import numpy
import numpy.ma

from .. import items
from ..CurvesROIWidget import ROI
from ..items.roi import RegionOfInterest

from ....math.combo import min_max
from silx.utils.proxy import docstring
from ....utils.deprecation import deprecated

logger = logging.getLogger(__name__)


class Stats(OrderedDict):
    """Class to define a set of statistic relative to a dataset
    (image, curve...).

    The goal of this class is to avoid multiple recalculation of some
    basic operations such as filtering data area where the statistics has to
    be apply.
    Min and max are also stored because they can be used several time.

    :param List statslist: List of the :class:`Stat` object to be computed.
    """
    def __init__(self, statslist=None):
        OrderedDict.__init__(self)
        _statslist = statslist if not None else []
        if statslist is not None:
            for stat in _statslist:
                self.add(stat)

    def calculate(self, item, plot, onlimits, roi, data_changed=False,
                  roi_changed=False):
        """
        Call all :class:`Stat` object registered and return the result of the
        computation.

        :param item: the item for which we want statistics
        :param plot: plot containing the item
        :param bool onlimits: True if we want to apply statistic only on
                              visible data.
        :param roi: region of interest for statistic calculation. Incompatible
                    with the `onlimits` option.
        :type roi: Union[None, :class:`~_RegionOfInterestBase`]
        :param bool data_changed: did the data changed since last calculation.
        :param bool roi_changed: did the associated roi (if any) has changed
                                 since last calculation.
        :return dict: dictionary with :class:`Stat` name as ket and result
                      of the calculation as value
        """
        res = {}
        context = self._getContext(item=item, plot=plot, onlimits=onlimits,
                                   roi=roi)
        for statName, stat in list(self.items()):
            if context.kind not in stat.compatibleKinds:
                logger.debug('kind %s not managed by statistic %s'
                             % (context.kind, stat.name))
                res[statName] = None
            else:
                if roi_changed is True:
                    context.clear_mask()
                if data_changed is True or roi_changed is True:
                    # if data changed or mask changed
                    context.clipData(item=item, plot=plot, onlimits=onlimits,
                                     roi=roi)
                # init roi and data
                res[statName] = stat.calculate(context)
        return res

    def __setitem__(self, key, value):
        assert isinstance(value, StatBase)
        OrderedDict.__setitem__(self, key, value)

    def add(self, stat):
        """Add a :class:`Stat` to the set

        :param Stat stat: stat to add to the set
        """
        self.__setitem__(key=stat.name, value=stat)

    @staticmethod
    @lru_cache(maxsize=50)
    def _getContext(item, plot, onlimits, roi):
        context = None
        # Check for PlotWidget items
        if isinstance(item, items.Curve):
            context = _CurveContext(item, plot, onlimits, roi=roi)
        elif isinstance(item, items.ImageData):
            context = _ImageContext(item, plot, onlimits, roi=roi)
        elif isinstance(item, items.Scatter):
            context = _ScatterContext(item, plot, onlimits, roi=roi)
        elif isinstance(item, items.Histogram):
            context = _HistogramContext(item, plot, onlimits, roi=roi)
        else:
            # Check for SceneWidget items
            from ...plot3d import items as items3d  # Lazy import

            if isinstance(item, (items3d.Scatter2D, items3d.Scatter3D)):
                context = _plot3DScatterContext(item, plot, onlimits,
                                                roi=roi)
            elif isinstance(item,
                            (items3d.ImageData, items3d.ScalarField3D)):
                context = _plot3DArrayContext(item, plot, onlimits,
                                              roi=roi)
        if context is None:
            raise ValueError('Item type not managed')
        return context


class _StatsContext(object):
    """
    The context is designed to be a simple buffer and avoid repetition of
    calculations that can appear during stats evaluation.

    .. warning:: this class gives access to the data to be used for computation
                 . It deal with filtering data visible by the user on plot.
                 The filtering is a simple data sub-sampling. No interpolation
                 is made to fit data to boundaries.

    :param item: the item for which we want to compute the context
    :param str kind: the kind of the item
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlimits` calculation
    :type roi: Union[None,:class:`_RegionOfInterestBase`]
    """
    def __init__(self, item, kind, plot, onlimits, roi):
        assert item
        assert plot
        assert type(onlimits) is bool
        self.kind = kind
        self.min = None
        self.max = None
        self.data = None
        self.roi = None
        self.onlimits = onlimits

        self.values = None
        """The array of data with limit filtering if any. Is a numpy.ma.array,
        meaning that it embed the mask applied by the roi if any"""

        self.axes = None
        """A list of array of position on each axis.

        If the signal is an array,
        then each axis has the length of that dimension,
        and the order is (z, y, x) (i.e., as the array shape).
        If the signal is not an array,
        then each axis has the same length as the signal,
        and the order is (x, y, z).
        """

        self.clipData(item, plot, onlimits, roi=roi)

    def clear_mask(self):
        """
        Remove the mask to force recomputation of it on next iteration
        :return:
        """
        raise NotImplementedError()

    @property
    def mask(self):
        if self.values is not None:
            assert isinstance(self.values, numpy.ma.MaskedArray)
            return self.values.mask
        else:
            return None

    @property
    def is_mask_valid(self, **kwargs):
        """Return if the mask is valid for the data or need to be recomputed"""
        raise NotImplementedError("Base class")

    def _set_mask_validity(self, **kwargs):
        """User to set some values that allows to define the mask properties
        and boundaries"""
        raise NotImplementedError("Base class")

    def clipData(self, item, plot, onlimits, roi):
        """Clip the data to the current mask to have accurate statistics

        Function called before computing each statistics associated to this
        context. It will insure the context for the (item, plot, onlimits, roi)
        is created.

        :param item: item for which we want statistics
        :param plot: plot containing the statistics
        :param bool onlimits: True if we want to apply statistic only on
                         visible data.
        :param roi: Region of interest for computing the statistics.
                    For now, incompatible with `onlimits` calculation
        :type roi: Union[None,:class:`_RegionOfInterestBase`]
        """
        raise NotImplementedError("Base class")

    @deprecated(reason="context are now stored and keep during stats life."
                       "So this function will be called only once",
                replacement="clipData", since_version="0.13.0")
    def createContext(self, item, plot, onlimits, roi):
        return self.clipData(item=item, plot=plot, onlimits=onlimits,
                             roi=roi)

    def isStructuredData(self):
        """Returns True if data as an array-like structure.

        :rtype: bool
        """
        if self.values is None or self.axes is None:
            return False

        if numpy.prod([len(axis) for axis in self.axes]) == self.values.size:
            return True
        else:
            # Make sure there is the right number of value in axes
            for axis in self.axes:
                assert len(axis) == self.values.size
            return False

    def isScalarData(self):
        """Returns True if data is a scalar.

        :rtype: bool
        """
        if self.values is None or self.axes is None:
            return False
        if self.isStructuredData():
            return len(self.axes) == self.values.ndim
        else:
            return self.values.ndim == 1

    def _checkContextInputs(self, item, plot, onlimits, roi):
        if roi is not None and onlimits is True:
            raise ValueError('Stats context is unable to manage both a ROI'
                             'and the `onlimits` option')


class _ScatterCurveHistoMixInContext(_StatsContext):
    def __init__(self, kind, item, plot, onlimits, roi):
        self.clear_mask()
        _StatsContext.__init__(self, item=item, kind=kind,
                               plot=plot, onlimits=onlimits, roi=roi)

    def _set_mask_validity(self, onlimits, from_, to_):
        self._onlimits = onlimits
        self._from_ = from_
        self._to_ = to_

    def clear_mask(self):
        self._onlimits = None
        self._from_ = None
        self._to_ = None

    def is_mask_valid(self, onlimits, from_, to_):
        return (onlimits == self.onlimits and from_ == self._from_ and
                to_ == self._to_)


class _CurveContext(_ScatterCurveHistoMixInContext):
    """
    StatsContext for :class:`Curve`

    :param item: the item for which we want to compute the context
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlinits` calculation
    :type roi: Union[None, :class:`ROI`]
    """
    def __init__(self, item, plot, onlimits, roi):
        _ScatterCurveHistoMixInContext.__init__(self, kind='curve', item=item,
                                                plot=plot, onlimits=onlimits,
                                                roi=roi)

    @docstring(_StatsContext)
    def clipData(self, item, plot, onlimits, roi):
        self._checkContextInputs(item=item, plot=plot, onlimits=onlimits,
                                 roi=roi)
        self.roi = roi
        self.onlimits = onlimits
        xData, yData = item.getData(copy=True)[0:2]

        if onlimits:
            minX, maxX = plot.getXAxis().getLimits()
            if self.is_mask_valid(onlimits=onlimits, from_=minX, to_=maxX):
                mask = self.mask
            else:
                mask = (minX <= xData) & (xData <= maxX)
                mask = mask == 0
                self._set_mask_validity(onlimits=onlimits, from_=minX, to_=maxX)
        elif roi:
            minX, maxX = roi.getFrom(), roi.getTo()
            if self.is_mask_valid(onlimits=onlimits, from_=minX, to_=maxX):
                mask = self.mask
            else:
                mask = (minX <= xData) & (xData <= maxX)
                mask = mask == 0
                self._set_mask_validity(onlimits=onlimits, from_=minX, to_=maxX)
        else:
            mask = numpy.zeros_like(yData)

        mask = mask.astype(numpy.uint32)
        self.xData = xData
        self.yData = yData
        self.values = numpy.ma.array(yData, mask=mask)
        unmasked_data = self.values.compressed()
        if len(unmasked_data) > 0:
            self.min, self.max = min_max(unmasked_data)
        else:
            self.min, self.max = None, None
        self.data = (xData, yData)
        self.axes = (xData,)

    def _checkContextInputs(self, item, plot, onlimits, roi):
        _StatsContext._checkContextInputs(self, item=item, plot=plot,
                                          onlimits=onlimits, roi=roi)
        if roi is not None and not isinstance(roi, ROI):
            raise TypeError('curve `context` can ony manage 1D roi')


class _HistogramContext(_ScatterCurveHistoMixInContext):
    """
    StatsContext for :class:`Histogram`

    :param item: the item for which we want to compute the context
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlinits` calculation
    :type roi: Union[None, :class:`ROI`]
    """
    def __init__(self, item, plot, onlimits, roi):
        _ScatterCurveHistoMixInContext.__init__(self, kind='histogram',
                                                item=item, plot=plot,
                                                onlimits=onlimits, roi=roi)

    @docstring(_StatsContext)
    def clipData(self, item, plot, onlimits, roi):
        self._checkContextInputs(item=item, plot=plot, onlimits=onlimits,
                                 roi=roi)
        yData, edges = item.getData(copy=True)[0:2]
        xData = item._revertComputeEdges(x=edges, histogramType=item.getAlignment())

        if onlimits:
            minX, maxX = plot.getXAxis().getLimits()
            if self.is_mask_valid(onlimits=onlimits, from_=minX, to_=maxX):
                mask = self.mask
            else:
                mask = (minX <= xData) & (xData <= maxX)
                mask = mask == 0
                self._set_mask_validity(onlimits=onlimits, from_=minX, to_=maxX)
        elif roi:
            if self.is_mask_valid(onlimits=onlimits, from_=roi._fromdata, to_=roi._todata):
                mask = self.mask
            else:
                mask = (roi._fromdata <= xData) & (xData <= roi._todata)
                mask = mask == 0
                self._set_mask_validity(onlimits=onlimits, from_=roi._fromdata,
                                        to_=roi._todata)
        else:
            mask = numpy.zeros_like(yData)
        mask = mask.astype(numpy.uint32)
        self.xData = xData
        self.yData = yData
        self.values = numpy.ma.array(yData, mask=(mask))
        unmasked_data = self.values.compressed()
        if len(unmasked_data) > 0:
            self.min, self.max = min_max(unmasked_data)
        else:
            self.min, self.max = None, None
        self.data = (self.xData, self.yData)
        self.axes = (self.xData,)

    def _checkContextInputs(self, item, plot, onlimits, roi):
        _StatsContext._checkContextInputs(self, item=item, plot=plot,
                                      onlimits=onlimits, roi=roi)

        if roi is not None and not isinstance(roi, ROI):
            raise TypeError('curve `context` can ony manage 1D roi')


class _ScatterContext(_ScatterCurveHistoMixInContext):
    """StatsContext scatter plots.

    It supports :class:`~silx.gui.plot.items.Scatter`.

    :param item: the item for which we want to compute the context
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlinits` calculation
    :type roi: Union[None, :class:`ROI`]
    """
    def __init__(self, item, plot, onlimits, roi):
        _ScatterCurveHistoMixInContext.__init__(self, kind='scatter',
                                                item=item, plot=plot,
                                                onlimits=onlimits, roi=roi)

    @docstring(_ScatterCurveHistoMixInContext)
    def clipData(self, item, plot, onlimits, roi):
        self._checkContextInputs(item=item, plot=plot, onlimits=onlimits,
                                 roi=roi)
        valueData = item.getValueData(copy=True)
        xData = item.getXData(copy=True)
        yData = item.getYData(copy=True)

        if onlimits:
            minX, maxX = plot.getXAxis().getLimits()
            minY, maxY = plot.getYAxis().getLimits()

            # filter on X axis
            valueData = valueData[(minX <= xData) & (xData <= maxX)]
            yData = yData[(minX <= xData) & (xData <= maxX)]
            xData = xData[(minX <= xData) & (xData <= maxX)]
            # filter on Y axis
            valueData = valueData[(minY <= yData) & (yData <= maxY)]
            xData = xData[(minY <= yData) & (yData <= maxY)]
            yData = yData[(minY <= yData) & (yData <= maxY)]

        if roi:
            if self.is_mask_valid(onlimits=onlimits, from_=roi.getFrom(),
                                  to_=roi.getTo()):
                mask = self.mask
            else:
                mask = (xData < roi.getFrom()) | (xData > roi.getTo())
        else:
            mask = numpy.zeros_like(xData)

        self.data = (xData, yData, valueData)
        self.values = numpy.ma.array(valueData, mask=mask)
        self.axes = (xData, yData)

        unmasked_values = self.values.compressed()
        if len(unmasked_values) > 0:
            self.min, self.max = min_max(unmasked_values)
        else:
            self.min, self.max = None, None

    def _checkContextInputs(self, item, plot, onlimits, roi):
        _StatsContext._checkContextInputs(self, item=item, plot=plot,
                                          onlimits=onlimits, roi=roi)

        if roi is not None and not isinstance(roi, ROI):
            raise TypeError('curve `context` can ony manage 1D roi')


class _ImageContext(_StatsContext):
    """StatsContext for images.

    It supports :class:`~silx.gui.plot.items.ImageData`.

    :warning: behaviour of scale images: now the statistics are computed on
              the entire data array (there is no sampling in the array or
              interpolation regarding the scale).
              This also mean that the result can differ from what is displayed.
              But I guess there is no perfect behaviour.

    :warning: `isIn` functions for image context: for now have basically a
              binary approach, the pixel is in a roi or not. To have a fully
              'correct behaviour' we should add a weight on stats calculation
              to moderate the pixel value.

    :param item: the item for which we want to compute the context
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlinits` calculation
    :type roi: Union[None, :class:`ROI`]
    """
    def __init__(self, item, plot, onlimits, roi):
        self.clear_mask()
        _StatsContext.__init__(self, kind='image', item=item,
                               plot=plot, onlimits=onlimits, roi=roi)

    def _set_mask_validity(self, xmin: float, xmax: float, ymin: float, ymax
                           : float):
        self._mask_x_min = xmin
        self._mask_x_max = xmax
        self._mask_y_min = ymin
        self._mask_y_max = ymax

    def clear_mask(self):
        self._mask_x_min = None
        self._mask_x_max = None
        self._mask_y_min = None
        self._mask_y_max = None

    def is_mask_valid(self, xmin, xmax, ymin, ymax):
        return (xmin == self._mask_x_min and xmax == self._mask_x_max and
                ymin == self._mask_y_min and ymax == self._mask_y_max)

    @docstring(_StatsContext)
    def clipData(self, item, plot, onlimits, roi):
        self._checkContextInputs(item=item, plot=plot, onlimits=onlimits,
                                 roi=roi)
        self.origin = item.getOrigin()
        self.scale = item.getScale()

        self.data = item.getData(copy=True)
        mask = numpy.zeros_like(self.data)
        """mask use to know of the stat should be count in or not"""

        if onlimits:
            minX, maxX = plot.getXAxis().getLimits()
            minY, maxY = plot.getYAxis().getLimits()

            XMinBound = int((minX - self.origin[0]) / self.scale[0])
            YMinBound = int((minY - self.origin[1]) / self.scale[1])
            XMaxBound = int((maxX - self.origin[0]) / self.scale[0])
            YMaxBound = int((maxY - self.origin[1]) / self.scale[1])

            XMinBound = max(XMinBound, 0)
            YMinBound = max(YMinBound, 0)

        if onlimits:
            if XMaxBound <= XMinBound or YMaxBound <= YMinBound:
                self.data = None
            else:
                self.data = self.data[YMinBound:YMaxBound + 1,
                                      XMinBound:XMaxBound + 1]
            mask = numpy.zeros_like(self.data)
        elif roi:
            minX, maxX = 0, self.data.shape[1]
            minY, maxY = 0, self.data.shape[0]

            XMinBound = max(minX, 0)
            YMinBound = max(minY, 0)
            XMaxBound = min(maxX, self.data.shape[1])
            YMaxBound = min(maxY, self.data.shape[0])

            if self.is_mask_valid(xmin=XMinBound, xmax=XMaxBound,
                                  ymin=YMinBound, ymax=YMaxBound):
                mask = self.mask
            else:
                for x in range(XMinBound, XMaxBound):
                    for y in range(YMinBound, YMaxBound):
                        _x = (x * self.scale[0]) + self.origin[0]
                        _y = (y * self.scale[1]) + self.origin[1]
                        mask[y, x] = not roi.contains((_x, _y))
                self._set_mask_validity(xmin=XMinBound, xmax=XMaxBound,
                                        ymin=YMinBound, ymax=YMaxBound)
        self.values = numpy.ma.array(self.data, mask=mask)
        if self.values.compressed().size > 0:
            self.min, self.max = min_max(self.values.compressed())
        else:
            self.min, self.max = None, None

        if self.values is not None:
            self.axes = (self.origin[1] + self.scale[1] * numpy.arange(self.data.shape[0]),
                         self.origin[0] + self.scale[0] * numpy.arange(self.data.shape[1]))

    def _checkContextInputs(self, item, plot, onlimits, roi):
        _StatsContext._checkContextInputs(self, item=item, plot=plot,
                                              onlimits=onlimits, roi=roi)

        if roi is not None and not isinstance(roi, RegionOfInterest):
            raise TypeError('curve `context` can ony manage 2D roi')


class _plot3DScatterContext(_StatsContext):
    """StatsContext for 3D scatter plots.

    It supports :class:`~silx.gui.plot3d.items.Scatter2D` and
    :class:`~silx.gui.plot3d.items.Scatter3D`.

    :param item: the item for which we want to compute the context
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlinits` calculation
    :type roi: Union[None, :class:`ROI`]
    """
    def __init__(self, item, plot, onlimits, roi):
        _StatsContext.__init__(self, kind='scatter', item=item, plot=plot,
                               onlimits=onlimits, roi=roi)

    @docstring(_StatsContext)
    def clipData(self, item, plot, onlimits, roi):
        self._checkContextInputs(item=item, plot=plot, onlimits=onlimits,
                                 roi=roi)
        if onlimits:
            raise RuntimeError("Unsupported plot %s" % str(plot))
        values = item.getValueData(copy=False)
        if roi:
            logger.warning("Roi are unsupported on volume for now")
            mask = numpy.zeros_like(values)
        else:
            mask = numpy.zeros_like(values)

        if values is not None and len(values) > 0:
            self.values = values
            axes = [item.getXData(copy=False), item.getYData(copy=False)]
            if self.values.ndim == 3:
                axes.append(item.getZData(copy=False))
            self.axes = tuple(axes)
            self.min, self.max = min_max(self.values)
            self.values = numpy.ma.array(self.values, mask=mask)
        else:
            self.values = None
            self.axes = None
            self.min, self.max = None, None

    def _checkContextInputs(self, item, plot, onlimits, roi):
        _StatsContext._checkContextInputs(self, item=item, plot=plot,
                                          onlimits=onlimits, roi=roi)

        if roi is not None and not isinstance(roi, RegionOfInterest):
            raise TypeError('curve `context` can ony manage 2D roi')


class _plot3DArrayContext(_StatsContext):
    """StatsContext for 3D scalar field and data image.

    It supports :class:`~silx.gui.plot3d.items.ScalarField3D` and
    :class:`~silx.gui.plot3d.items.ImageData`.

    :param item: the item for which we want to compute the context
    :param plot: the plot containing the item
    :param bool onlimits: True if we want to apply statistic only on
                          visible data.
    :param roi: Region of interest for computing the statistics.
                For now, incompatible with `onlinits` calculation
    :type roi: Union[None, :class:`ROI`]
    """
    def __init__(self, item, plot, onlimits, roi):
        _StatsContext.__init__(self, kind='image', item=item, plot=plot,
                               onlimits=onlimits, roi=roi)

    @docstring(_StatsContext)
    def clipData(self, item, plot, onlimits, roi):
        self._checkContextInputs(item=item, plot=plot, onlimits=onlimits,
                                 roi=roi)
        if onlimits:
            raise RuntimeError("Unsupported plot %s" % str(plot))

        values = item.getData(copy=False)
        if roi:
            logger.warning("Roi are unsuported on volume for now")
            mask = numpy.zeros_like(values)
        else:
            mask = numpy.zeros_like(values)

        if values is not None and len(values) > 0:
            self.values = values
            self.axes = tuple([numpy.arange(size) for size in self.values.shape])
            self.min, self.max = min_max(self.values)
            self.values = numpy.ma.array(self.values, mask=mask)
        else:
            self.values = None
            self.axes = None
            self.min, self.max = None, None

    def _checkContextInputs(self, item, plot, onlimits, roi):
        _StatsContext._checkContextInputs(self, item=item, plot=plot,
                                      onlimits=onlimits, roi=roi)

        if roi is not None and not isinstance(roi, RegionOfInterest):
            raise TypeError('curve `context` can ony manage 2D roi')


BASIC_COMPATIBLE_KINDS = 'curve', 'image', 'scatter', 'histogram'


class StatBase(object):
    """
    Base class for defining a statistic.

    :param str name: the name of the statistic. Must be unique.
    :param List[str] compatibleKinds:
        The kind of items (curve, scatter...) for which the statistic apply.
    """
    def __init__(self, name, compatibleKinds=BASIC_COMPATIBLE_KINDS, description=None):
        self.name = name
        self.compatibleKinds = compatibleKinds
        self.description = description

    def calculate(self, context):
        """
        compute the statistic for the given :class:`StatsContext`

        :param _StatsContext context:
        :return dict: key is stat name, statistic computed is the dict value
        """
        raise NotImplementedError('Base class')

    def getToolTip(self, kind):
        """
        If necessary add a tooltip for a stat kind

        :param str kind: the kind of item the statistic is compute for.
        :return: tooltip or None if no tooltip
        """
        return None


class Stat(StatBase):
    """
    Create a StatBase class based on a function pointer.

    :param str name: name of the statistic. Used as id
    :param fct: function which should have as unique mandatory parameter the
                data. Should be able to adapt to all `kinds` defined as
                compatible
    :param tuple kinds: the compatible item kinds of the function (curve,
                        image...)
    """
    def __init__(self, name, fct, kinds=BASIC_COMPATIBLE_KINDS):
        StatBase.__init__(self, name, kinds)
        self._fct = fct

    @docstring(StatBase)
    def calculate(self, context):
        if context.values is not None:
            if context.kind in self.compatibleKinds:
                return self._fct(context.values)
            else:
                raise ValueError('Kind %s not managed by %s'
                                 '' % (context.kind, self.name))
        else:
            return None


class StatMin(StatBase):
    """Compute the minimal value on data"""
    def __init__(self):
        StatBase.__init__(self, name='min')

    @docstring(StatBase)
    def calculate(self, context):
        return context.min


class StatMax(StatBase):
    """Compute the maximal value on data"""
    def __init__(self):
        StatBase.__init__(self, name='max')

    @docstring(StatBase)
    def calculate(self, context):
        return context.max


class StatDelta(StatBase):
    """Compute the delta between minimal and maximal on data"""
    def __init__(self):
        StatBase.__init__(self, name='delta')

    @docstring(StatBase)
    def calculate(self, context):
        return context.max - context.min


class _StatCoord(StatBase):
    """Base class for argmin and argmax stats"""

    def _indexToCoordinates(self, context, index):
        """Returns the coordinates of data point at given index

        If data is an array, coordinates are in reverse order from data shape.

        :param _StatsContext context:
        :param int index: Index in the flattened data array
        :rtype: List[int]
        """

        axes = context.axes

        if context.isStructuredData() or context.roi:
            coordinates = []
            for axis in reversed(axes):
                coordinates.append(axis[index % len(axis)])
                index = index // len(axis)
            return tuple(coordinates)
        else:
            return tuple(axis[index] for axis in axes)


class StatCoordMin(_StatCoord):
    """Compute the coordinates of the first minimum value of the data"""
    def __init__(self):
        _StatCoord.__init__(self, name='coords min')

    @docstring(StatBase)
    def calculate(self, context):
        if context.values is None or not context.isScalarData():
            return None

        index = context.values.argmin()
        return self._indexToCoordinates(context, index)

    @docstring(StatBase)
    def getToolTip(self, kind):
        return "Coordinates of the first minimum value of the data"


class StatCoordMax(_StatCoord):
    """Compute the coordinates of the first maximum value of the data"""
    def __init__(self):
        _StatCoord.__init__(self, name='coords max')

    @docstring(StatBase)
    def calculate(self, context):
        if context.values is None or not context.isScalarData():
            return None

        # TODO: the values should be a mask array by default, will be simpler
        # if possible
        index = context.values.argmax()
        return self._indexToCoordinates(context, index)

    @docstring(StatBase)
    def getToolTip(self, kind):
        return "Coordinates of the first maximum value of the data"


class StatCOM(StatBase):
    """Compute data center of mass"""
    def __init__(self):
        StatBase.__init__(self, name='COM', description='Center of mass')

    @docstring(StatBase)
    def calculate(self, context):
        if context.values is None or not context.isScalarData():
            return None

        values = numpy.ma.array(context.values, mask=context.mask, dtype=numpy.float64)
        sum_ = numpy.sum(values)
        if sum_ == 0.:
            return (numpy.nan,) * len(context.axes)

        if context.isStructuredData():
            centerofmass = []
            for index, axis in enumerate(context.axes):
                axes = tuple([i for i in range(len(context.axes)) if i != index])
                centerofmass.append(
                    numpy.sum(axis * numpy.sum(values, axis=axes)) / sum_)
            return tuple(reversed(centerofmass))
        else:
            return tuple(
                numpy.sum(axis * values) / sum_ for axis in context.axes)

    @docstring(StatBase)
    def getToolTip(self, kind):
        return "Compute the center of mass of the dataset"