diff options
Diffstat (limited to 'silx/gui/plot/test/testColorBar.py')
-rw-r--r-- | silx/gui/plot/test/testColorBar.py | 227 |
1 files changed, 169 insertions, 58 deletions
diff --git a/silx/gui/plot/test/testColorBar.py b/silx/gui/plot/test/testColorBar.py index 797ff03..80ae6a8 100644 --- a/silx/gui/plot/test/testColorBar.py +++ b/silx/gui/plot/test/testColorBar.py @@ -32,22 +32,37 @@ 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(unittest.TestCase): +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.colorScaleWidget.deleteLater() - self.colorScaleWidget = None + 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 = { 'name': 'gray', 'normalization': 'linear', - 'autoscale': False, 'vmin': 0.0, 'vmax': 1.0 } + self.colorMapLin1 = Colormap(name='gray', + normalization=Colormap.LINEAR, + vmin=0.0, + vmax=1.0) self.colorScaleWidget.setColormap(self.colorMapLin1) self.assertTrue( @@ -57,8 +72,10 @@ class TestColorScale(unittest.TestCase): self.assertTrue( self.colorScaleWidget.getValueFromRelativePosition(1.0) == 1.0) - self.colorMapLin2 = { 'name': 'viridis', 'normalization': 'linear', - 'autoscale': False, 'vmin': -10, 'vmax': 0 } + self.colorMapLin2 = Colormap(name='viridis', + normalization=Colormap.LINEAR, + vmin=-10, + vmax=0) self.colorScaleWidget.setColormap(self.colorMapLin2) self.assertTrue( @@ -69,8 +86,10 @@ class TestColorScale(unittest.TestCase): self.colorScaleWidget.getValueFromRelativePosition(1.0) == 0.0) def testRelativePositionLog(self): - self.colorMapLog1 = { 'name': 'temperature', 'normalization': 'log', - 'autoscale': False, 'vmin': 1.0, 'vmax': 100.0 } + self.colorMapLog1 = Colormap(name='temperature', + normalization=Colormap.LOGARITHM, + vmin=1.0, + vmax=100.0) self.colorScaleWidget.setColormap(self.colorMapLog1) @@ -83,41 +102,38 @@ class TestColorScale(unittest.TestCase): val = self.colorScaleWidget.getValueFromRelativePosition(0.0) self.assertTrue(val == 1.0) - def testNegativeLogMin(self): - colormap = { 'name': 'gray', 'normalization': 'log', - 'autoscale': False, 'vmin': -1.0, 'vmax': 1.0 } - - with self.assertRaises(ValueError): - self.colorScaleWidget.setColormap(colormap) - - def testNegativeLogMax(self): - colormap = { 'name': 'gray', 'normalization': 'log', - 'autoscale': False, 'vmin': 1.0, 'vmax': -1.0 } - with self.assertRaises(ValueError): - self.colorScaleWidget.setColormap(colormap) - -class TestNoAutoscale(unittest.TestCase): +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 = ColorBarWidget(parent=None, plot=self.plot) + 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 = { 'name': 'gray', 'normalization': 'log', - 'autoscale': False, 'vmin': 1.0, 'vmax': 100.0 } + 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') @@ -139,8 +155,10 @@ class TestNoAutoscale(unittest.TestCase): self.assertTrue(val == 1.0) def testLinearNormNoAutoscale(self): - colormapLog = { 'name': 'gray', 'normalization': 'linear', - 'autoscale': False, 'vmin': -4, 'vmax': 5 } + 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') @@ -159,20 +177,26 @@ class TestNoAutoscale(unittest.TestCase): val = self.colorScale.getValueFromRelativePosition(0.0) self.assertTrue(val == -4.0) -class TestColorbarWidget(TestCaseQt): - """Test interaction with the ColorScaleBar""" + +class TestColorBarWidget(TestCaseQt): + """Test interaction with the ColorBarWidget""" def setUp(self): - super(TestColorbarWidget, self).setUp() + super(TestColorBarWidget, self).setUp() self.plot = Plot2D() - self.colorBar = ColorBarWidget(parent=None, plot=self.plot) + 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() + super(TestColorBarWidget, self).tearDown() def testEmptyColorBar(self): colorBar = ColorBarWidget(parent=None) @@ -185,38 +209,43 @@ class TestColorbarWidget(TestCaseQt): Note : colorbar is modified by the Plot directly not ColorBarWidget """ - colormapLog = { 'name': 'gray', 'normalization': 'log', - 'autoscale': True, 'vmin': -1.0, 'vmax': 1.0 } - - colormapLog2 = { 'name': 'gray', 'normalization': 'log', - 'autoscale': False, 'vmin': -1.0, 'vmax': 1.0 } + 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 autoscale : set to minmal positive value - data[data<1] = data.max() - self.assertTrue(self.colorBar._colormap['vmin'] == data.min()) - self.assertTrue(self.colorBar._colormap['vmax'] == data.max()) - - data = numpy.linspace(-9, -2, 100).reshape(10, 10) + # 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) - self.plot.addImage(data=data, colormap=colormapLog2, legend='toto') + # if data is positive + data[data<1] = data.max() + self.plot.addImage(data=data, + colormap=colormapLog, + legend='toto', + replace=True) self.plot.setActiveImage('toto') - # if negative values, changing bounds for default : 1, 10 - self.assertTrue(self.colorBar._colormap['vmin'] == 1) - self.assertTrue(self.colorBar._colormap['vmax'] == 10) - def testPlotAssocation(self): - """Make sure the ColorBarWidget is proparly connected with the plot""" - colormap = { 'name': 'gray', 'normalization': 'linear', - 'autoscale': True, 'vmin': -1.0, 'vmax': 1.0 } + self.assertTrue(self.colorBar.getColorScaleBar().minVal == data.min()) + self.assertTrue(self.colorBar.getColorScaleBar().maxVal == data.max()) - # make sure that default settings are the same + 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() == self.plot.getDefaultColormap()) + 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') @@ -224,12 +253,94 @@ class TestColorbarWidget(TestCaseQt): # make sure the modification of the colormap has been done self.assertFalse( - self.colorBar.getColormap() == self.plot.getDefaultColormap()) + 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): + for ui in (TestColorScale, TestNoAutoscale, TestColorBarWidget, + TestColorBarUpdate): test_suite.addTest( unittest.defaultTestLoader.loadTestsFromTestCase(ui)) @@ -237,4 +348,4 @@ def suite(): if __name__ == '__main__': - unittest.main(defaultTest='suite')
\ No newline at end of file + unittest.main(defaultTest='suite') |