summaryrefslogtreecommitdiff
path: root/silx/gui/plot/stats/statshandler.py
blob: 0a62b31286c16d885d7a6b2be6d8359cfe5a9142 (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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-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.
#
# ###########################################################################*/
"""
"""

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


import logging

from silx.gui import qt
from silx.gui.plot import stats as statsmdl

logger = logging.getLogger(__name__)


class _FloatItem(qt.QTableWidgetItem):
    """Simple QTableWidgetItem allowing ordering on floats"""

    def __init__(self, type=qt.QTableWidgetItem.Type):
        qt.QTableWidgetItem.__init__(self, type=type)

    def __lt__(self, other):
        return float(self.text()) < float(other.text())


class StatFormatter(object):
    """
    Class used to apply format on :class:`Stat`

    :param formatter: the formatter. Defined as str.format()
    :param qItemClass: the class inheriting from :class:`QTableWidgetItem`
                       which will be used to display the result of the
                       statistic computation.
    """
    DEFAULT_FORMATTER = '{0:.3f}'

    def __init__(self, formatter=DEFAULT_FORMATTER, qItemClass=_FloatItem):
        self.formatter = formatter
        self.tabWidgetItemClass = qItemClass

    def format(self, val):
        if self.formatter is None or val is None:
            return str(val)
        else:
            return self.formatter.format(val)


class StatsHandler(object):
    """
    Give
    create:

    * Stats object which will manage the statistic computation
    * Associate formatter and :class:`Stat`

    :param statFormatters: Stat and optional formatter.
                           If elements are given as a tuple, elements
                           should be (:class:`Stat`, formatter).
                           Otherwise should be :class:`Stat` elements.
    :rtype: List or tuple
    """

    def __init__(self, statFormatters):
        self.stats = statsmdl.Stats()
        self.formatters = {}
        for elmt in statFormatters:
            helper = _StatHelper(elmt)
            self.add(stat=helper.stat, formatter=helper.statFormatter)

    def add(self, stat, formatter=None):
        assert isinstance(stat, statsmdl.StatBase)
        self.stats.add(stat)
        _formatter = formatter
        if type(_formatter) is str:
            _formatter = StatFormatter(formatter=_formatter)
        self.formatters[stat.name] = _formatter

    def format(self, name, val):
        """
        Apply the format for the `name` statistic and the given value
        :param name: the name of the associated statistic
        :param val: value before formatting
        :return: formatted value
        """
        if name not in self.formatters:
            logger.warning("statistic %s haven't been registred" % name)
            return val
        else:
            if self.formatters[name] is None:
                return str(val)
            else:
                if isinstance(val, (tuple, list)):
                    res = []
                    [res.append(self.formatters[name].format(_val)) for _val in val]
                    return ', '.join(res)
                else:
                    return self.formatters[name].format(val)

    def calculate(self, item, plot, onlimits):
        """
        compute all statistic registred and return the list of formatted
        statistics result.

        :param item: item for which we want to compute statistics
        :param plot: plot containing the item
        :param onlimits: True if we want to compute statistics on visible data
                         only
        :return: list of formatted statistics (as str)
        :rtype: dict
        """
        res = self.stats.calculate(item, plot, onlimits)
        for resName, resValue in list(res.items()):
            res[resName] = self.format(resName, res[resName])
        return res


class _StatHelper(object):
    """
    Helper class to generated the requested StatBase instance and the
    associated StatFormatter
    """
    def __init__(self, arg):
        self.statFormatter = None
        self.stat = None

        if isinstance(arg, statsmdl.StatBase):
            self.stat = arg
        else:
            assert len(arg) > 0
            if isinstance(arg[0], statsmdl.StatBase):
                self.dealWithStatAndFormatter(arg)
            else:
                _arg = arg
                if isinstance(arg[0], tuple):
                    _arg = arg[0]
                    if len(arg) > 1:
                        self.statFormatter = arg[1]
                self.createStatInstanceAndFormatter(_arg)

    def dealWithStatAndFormatter(self, arg):
        assert isinstance(arg[0], statsmdl.StatBase)
        self.stat = arg[0]
        if len(arg) > 2:
            raise ValueError('To many argument with %s. At most one '
                             'argument can be associated with the '
                             'BaseStat (the `StatFormatter`')
        if len(arg) is 2:
            assert isinstance(arg[1], (StatFormatter, type(None), str))
            self.statFormatter = arg[1]

    def createStatInstanceAndFormatter(self, arg):
        if type(arg[0]) is not str:
            raise ValueError('first element of the tuple should be a string'
                             ' or a StatBase instance')
        if len(arg) is 1:
            raise ValueError('A function should be associated with the'
                             'stat name')
        if len(arg) > 3:
            raise ValueError('Two much argument given for defining statistic.'
                             'Take at most three arguments (name, function, '
                             'kinds)')
        if len(arg) is 2:
            self.stat = statsmdl.Stat(name=arg[0], fct=arg[1])
        else:
            self.stat = statsmdl.Stat(name=arg[0], fct=arg[1], kinds=arg[2])