summaryrefslogtreecommitdiff
path: root/silx/gui/plot/test/testColorBar.py
blob: 80ae6a832361e7dd502445d1d653f5f2c392b7c7 (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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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.
#
# ###########################################################################*/
"""Basic tests for ColorBar featues and sub widgets of Colorbar module"""

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "11/04/2017"

import unittest
from silx.gui.test.utils import TestCaseQt
from silx.gui.plot.ColorBar import _ColorScale
from silx.gui.plot.ColorBar import ColorBarWidget
from silx.gui.plot.Colormap import Colormap
from silx.gui.plot import Plot2D
from silx.gui import qt
import numpy


class TestColorScale(TestCaseQt):
    """Test that interaction with the colorScale is correct"""
    def setUp(self):
        super(TestColorScale, self).setUp()
        self.colorScaleWidget = _ColorScale(colormap=None, parent=None)
        self.colorScaleWidget.show()
        self.qWaitForWindowExposed(self.colorScaleWidget)

    def tearDown(self):
        self.qapp.processEvents()
        self.colorScaleWidget.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.colorScaleWidget.close()
        del self.colorScaleWidget
        super(TestColorScale, self).tearDown()

    def testNoColormap(self):
        """Test _ColorScale without a colormap"""
        colormap = self.colorScaleWidget.getColormap()
        self.assertIsNone(colormap)

    def testRelativePositionLinear(self):
        self.colorMapLin1 = Colormap(name='gray',
                                     normalization=Colormap.LINEAR,
                                     vmin=0.0,
                                     vmax=1.0)
        self.colorScaleWidget.setColormap(self.colorMapLin1)
        
        self.assertTrue(
            self.colorScaleWidget.getValueFromRelativePosition(0.25) == 0.25)
        self.assertTrue(
            self.colorScaleWidget.getValueFromRelativePosition(0.5) == 0.5)
        self.assertTrue(
            self.colorScaleWidget.getValueFromRelativePosition(1.0) == 1.0)

        self.colorMapLin2 = Colormap(name='viridis',
                                     normalization=Colormap.LINEAR,
                                     vmin=-10,
                                     vmax=0)
        self.colorScaleWidget.setColormap(self.colorMapLin2)
        
        self.assertTrue(
            self.colorScaleWidget.getValueFromRelativePosition(0.25) == -7.5)
        self.assertTrue(
            self.colorScaleWidget.getValueFromRelativePosition(0.5) == -5.0)
        self.assertTrue(
            self.colorScaleWidget.getValueFromRelativePosition(1.0) == 0.0)

    def testRelativePositionLog(self):
        self.colorMapLog1 = Colormap(name='temperature',
                                     normalization=Colormap.LOGARITHM,
                                     vmin=1.0,
                                     vmax=100.0)

        self.colorScaleWidget.setColormap(self.colorMapLog1)

        val = self.colorScaleWidget.getValueFromRelativePosition(1.0)
        self.assertTrue(val == 100.0)

        val = self.colorScaleWidget.getValueFromRelativePosition(0.5)
        self.assertTrue(val == 10.0)
        
        val = self.colorScaleWidget.getValueFromRelativePosition(0.0)
        self.assertTrue(val == 1.0)


class TestNoAutoscale(TestCaseQt):
    """Test that ticks and color displayed are correct in the case of a colormap
    with no autoscale
    """

    def setUp(self):
        super(TestNoAutoscale, self).setUp()
        self.plot = Plot2D()
        self.colorBar = self.plot.getColorBarWidget()
        self.colorBar.setVisible(True)  # Makes sure the colormap is visible
        self.tickBar = self.colorBar.getColorScaleBar().getTickBar()
        self.colorScale = self.colorBar.getColorScaleBar().getColorScale()

        self.plot.show()
        self.qWaitForWindowExposed(self.plot)

    def tearDown(self):
        self.qapp.processEvents()
        self.tickBar = None
        self.colorScale = None
        del self.colorBar
        self.plot.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.plot.close()
        del self.plot
        super(TestNoAutoscale, self).tearDown()

    def testLogNormNoAutoscale(self):
        colormapLog = Colormap(name='gray',
                               normalization=Colormap.LOGARITHM,
                               vmin=1.0,
                               vmax=100.0)

        data = numpy.linspace(10, 1e10, 9).reshape(3, 3)
        self.plot.addImage(data=data, colormap=colormapLog, legend='toto')
        self.plot.setActiveImage('toto')

        # test Ticks
        self.tickBar.setTicksNumber(10)
        self.tickBar.computeTicks()

        ticksTh = numpy.linspace(1.0, 100.0, 10)
        ticksTh = 10**ticksTh
        numpy.array_equal(self.tickBar.ticks, ticksTh)

        # test ColorScale
        val = self.colorScale.getValueFromRelativePosition(1.0)
        self.assertTrue(val == 100.0)

        val = self.colorScale.getValueFromRelativePosition(0.0)
        self.assertTrue(val == 1.0)

    def testLinearNormNoAutoscale(self):
        colormapLog = Colormap(name='gray',
                               normalization=Colormap.LINEAR,
                               vmin=-4,
                               vmax=5)

        data = numpy.linspace(1, 9, 9).reshape(3, 3)
        self.plot.addImage(data=data, colormap=colormapLog, legend='toto')
        self.plot.setActiveImage('toto')

        # test Ticks
        self.tickBar.setTicksNumber(10)
        self.tickBar.computeTicks()

        numpy.array_equal(self.tickBar.ticks, numpy.linspace(-4, 5, 10))

        # test ColorScale
        val = self.colorScale.getValueFromRelativePosition(1.0)
        self.assertTrue(val == 5.0)

        val = self.colorScale.getValueFromRelativePosition(0.0)
        self.assertTrue(val == -4.0)


class TestColorBarWidget(TestCaseQt):
    """Test interaction with the ColorBarWidget"""

    def setUp(self):
        super(TestColorBarWidget, self).setUp()
        self.plot = Plot2D()
        self.colorBar = self.plot.getColorBarWidget()
        self.colorBar.setVisible(True)  # Makes sure the colormap is visible

        self.plot.show()
        self.qWaitForWindowExposed(self.plot)

    def tearDown(self):
        self.qapp.processEvents()
        del self.colorBar
        self.plot.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.plot.close()
        del self.plot
        super(TestColorBarWidget, self).tearDown()

    def testEmptyColorBar(self):
        colorBar = ColorBarWidget(parent=None)
        colorBar.show()
        self.qWaitForWindowExposed(colorBar)

    def testNegativeColormaps(self):
        """test the behavior of the ColorBarWidget in the case of negative
        values

        Note : colorbar is modified by the Plot directly not ColorBarWidget
        """
        colormapLog = Colormap(name='gray',
                               normalization=Colormap.LOGARITHM,
                               vmin=None,
                               vmax=None)

        data = numpy.array([-5, -4, 0, 2, 3, 5, 10, 20, 30])
        data = data.reshape(3, 3)
        self.plot.addImage(data=data, colormap=colormapLog, legend='toto')
        self.plot.setActiveImage('toto')

        # default behavior when with log and negative values: should set vmin
        # to 1 and vmax to 10
        self.assertTrue(self.colorBar.getColorScaleBar().minVal == 2)
        self.assertTrue(self.colorBar.getColorScaleBar().maxVal == 30)

        # if data is positive
        data[data<1] = data.max()
        self.plot.addImage(data=data,
                           colormap=colormapLog,
                           legend='toto',
                           replace=True)
        self.plot.setActiveImage('toto')

        self.assertTrue(self.colorBar.getColorScaleBar().minVal == data.min())
        self.assertTrue(self.colorBar.getColorScaleBar().maxVal == data.max())

    def testPlotAssocation(self):
        """Make sure the ColorBarWidget is properly connected with the plot"""
        colormap = Colormap(name='gray',
                            normalization=Colormap.LINEAR,
                            vmin=None,
                            vmax=None)

        # make sure that default settings are the same (but a copy of the
        self.colorBar.setPlot(self.plot)
        self.assertTrue(
            self.colorBar.getColormap() is self.plot.getDefaultColormap())

        data = numpy.linspace(0, 10, 100).reshape(10, 10)
        self.plot.addImage(data=data, colormap=colormap, legend='toto')
        self.plot.setActiveImage('toto')

        # make sure the modification of the colormap has been done
        self.assertFalse(
            self.colorBar.getColormap() is self.plot.getDefaultColormap())
        self.assertTrue(
            self.colorBar.getColormap() is colormap)

        # test that colorbar is updated when default plot colormap changes
        self.plot.clear()
        plotColormap = Colormap(name='gray',
                                normalization=Colormap.LOGARITHM,
                                vmin=None,
                                vmax=None)
        self.plot.setDefaultColormap(plotColormap)
        self.assertTrue(self.colorBar.getColormap() is plotColormap)

    def testColormapWithoutRange(self):
        """Test with a colormap with vmin==vmax"""
        colormap = Colormap(name='gray',
                            normalization=Colormap.LINEAR,
                            vmin=1.0,
                            vmax=1.0)
        self.colorBar.setColormap(colormap)


class TestColorBarUpdate(TestCaseQt):
    """Test that the ColorBar is correctly updated when the signal 'sigChanged'
    of the colormap is emitted
    """

    def setUp(self):
        super(TestColorBarUpdate, self).setUp()
        self.plot = Plot2D()
        self.colorBar = self.plot.getColorBarWidget()
        self.colorBar.setVisible(True)  # Makes sure the colormap is visible
        self.colorBar.setPlot(self.plot)

        self.plot.show()
        self.qWaitForWindowExposed(self.plot)
        self.data = numpy.random.rand(9).reshape(3, 3)

    def tearDown(self):
        self.qapp.processEvents()
        del self.colorBar
        self.plot.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.plot.close()
        del self.plot
        super(TestColorBarUpdate, self).tearDown()

    def testUpdateColorMap(self):
        colormap = Colormap(name='gray',
                            normalization='linear',
                            vmin=0,
                            vmax=1)

        # check inital state
        self.plot.addImage(data=self.data, colormap=colormap, legend='toto')
        self.plot.setActiveImage('toto')

        self.assertTrue(self.colorBar.getColorScaleBar().minVal == 0)
        self.assertTrue(self.colorBar.getColorScaleBar().maxVal == 1)
        self.assertTrue(
            self.colorBar.getColorScaleBar().getTickBar()._vmin == 0)
        self.assertTrue(
            self.colorBar.getColorScaleBar().getTickBar()._vmax == 1)
        self.assertTrue(
            self.colorBar.getColorScaleBar().getTickBar()._norm == "linear")

        # update colormap
        colormap.setVMin(0.5)
        self.assertTrue(self.colorBar.getColorScaleBar().minVal == 0.5)
        self.assertTrue(
            self.colorBar.getColorScaleBar().getTickBar()._vmin == 0.5)

        colormap.setVMax(0.8)
        self.assertTrue(self.colorBar.getColorScaleBar().maxVal == 0.8)
        self.assertTrue(
            self.colorBar.getColorScaleBar().getTickBar()._vmax == 0.8)

        colormap.setNormalization('log')
        self.assertTrue(
            self.colorBar.getColorScaleBar().getTickBar()._norm == 'log')

    # TODO : should also check that if the colormap is changing then values (especially in log scale)
    # should be coherent if in autoscale


def suite():
    test_suite = unittest.TestSuite()
    for ui in (TestColorScale, TestNoAutoscale, TestColorBarWidget,
               TestColorBarUpdate):
        test_suite.addTest(
            unittest.defaultTestLoader.loadTestsFromTestCase(ui))

    return test_suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')