File: MethodLabel.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 (191 lines) | stat: -rw-r--r-- 7,673 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
# coding: utf-8
# /*##########################################################################
#
# Copyright (C) 2016-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__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "16/10/2020"

import logging
_logger = logging.getLogger(__name__)

from silx.gui import qt

from ... import method_registry


class MethodLabel(qt.QLabel):
    """Readonly line display"""

    _HUMAN_READABLE = {
        "*": "Any",
        "any": "Any",
        "all": "Any",
        "no": "No splitting",
        "bbox": "Bounding box",
        "pseudo": "Pseudo split",
        "full": "Full splitting",
        "histogram": "Histogram",
        "lut": "LUT",
        "csr": "CSR",
        "python": "Python",
        "cython": "Cython",
        "opencl": "OpenCL",
    }

    _TOOLTIP_TEMPLATE = """<ul>
    <li><b>Pixel splitting:</b> {split}</li>
    <li><b>Implementation:</b> {impl}</li>
    <li><b>Algorithm:</b> {algo}</li>
    <li><b>Availability:</b> {availability}</li>
    </ul>"""

    def __init__(self, parent=None):
        super(MethodLabel, self).__init__(parent)
        self.__method = None
        self.__labelTemplate = "{split} / {impl} / {algo}"
        self.__availability = False
        self.__updateFeedback()

    def method(self):
        """
        :rtype: Union[None,method_registry.Method]
        """
        return self.__method

    def setMethod(self, method):
        if self.__method == method:
            return
        self.__method = method
        self.__updateFeedback()

    def setLabelTemplate(self, template):
        """Set the template used to format the label

        :param str template: The template used to format the label
        """
        self.__labelTemplate = template
        self.__updateFeedback()

    def labelTemplate(self):
        return self.__labelTemplate

    def __compare(self, method, methodReference):
        if method == methodReference:
            return "same"
        any_set = set(["*", "any", "all", None])
        if method.split != methodReference.split and methodReference.split not in any_set:
            return "degraded"
        if method.impl != methodReference.impl and methodReference.impl not in any_set:
            return "degraded"
        if method.algo != methodReference.algo and methodReference.algo not in any_set:
            return "degraded"
        return "specialized"

    def setMethodAvailability(self, availability):
        """Display or not in the widget if the method is available or not.

        :param bool availability: Display in the widget if the method is
            available or not.
        """
        self.__availability = availability
        self.__updateFeedback()

    def methodAvailability(self):
        return self.__availability

    def __updateFeedback(self):
        method = self.__method
        if method is None:
            label = "No method"
            toolTip = "No method"
        else:
            if not self.__availability:
                    label = self.__methodToString(method, self.__labelTemplate)
                    toolTip = "<html>%s</html>" % self.__methodToString(method, self._TOOLTIP_TEMPLATE)
            else:
                usedMethods = method_registry.IntegrationMethod.select_method(method=method)
                if len(usedMethods) == 0:
                    label = "No method fit"
                    toolTip = self.__methodToString(method, self._TOOLTIP_TEMPLATE)
                    toolTip = ("No method fit. Integration could be compromized. "
                               "The following configuration is defined:"
                               "%s</html>" % toolTip)
                else:
                    usedMethod = usedMethods[0]
                    usedMethod = usedMethod.method
                    compare = self.__compare(usedMethod, method)

                    if compare == "same":
                        label = self.__methodToString(method, self.__labelTemplate)
                        toolTip = "<html>%s</html>" % self.__methodToString(method, self._TOOLTIP_TEMPLATE)
                    else:
                        original = self.__methodToString(method, self.__labelTemplate)
                        label = self.__methodToString(usedMethod, self.__labelTemplate)
                        toolTip = self.__methodToString(usedMethod, self._TOOLTIP_TEMPLATE)

                        if compare == "degraded":
                            label = "Degraded to: " + label
                            toolTip = ("<html>The method %s is not available, at least, in this computer. "
                                       "The following method will be used:"
                                       "%s</html>" % (original, toolTip))
                        elif compare == "specialized":
                            label = "Specialized with: " + label
                            toolTip = ("<html>The generic selection %s will use the following method in this computer:"
                                       "%s</html>" % (original, toolTip))
                        else:
                            assert(False)

        self.setText(label)
        self.setToolTip(toolTip)

    def __methodToString(self, method, template):
        _dim, split, algo, impl, _target = method

        methods = method_registry.IntegrationMethod.select_method(dim="*",
                                                                  split=split,
                                                                  algo=algo,
                                                                  impl=impl,
                                                                  degradable=False)
        dimensions = set([m.dimension for m in methods])

        if dimensions == set([1, 2]):
            availability = "1D and 2D"
        elif dimensions == set([1]):
            availability = "Only 1D"
        elif dimensions == set([2]):
            availability = "Only 2D"
        elif dimensions == set([]):
            availability = "Not available"
        else:
            _logger.error("Unexpected dimensions %s", dimensions)
            availability = "Unsupported"

        split = self._HUMAN_READABLE.get(split, split)
        algo = self._HUMAN_READABLE.get(algo, algo)
        impl = self._HUMAN_READABLE.get(impl, impl)
        return template.format(split=split,
                               algo=algo,
                               impl=impl,
                               availability=availability)