diff options
Diffstat (limited to 'silx/gui/plot/test/testPlotWidget.py')
-rwxr-xr-x[-rw-r--r--] | silx/gui/plot/test/testPlotWidget.py | 218 |
1 files changed, 216 insertions, 2 deletions
diff --git a/silx/gui/plot/test/testPlotWidget.py b/silx/gui/plot/test/testPlotWidget.py index 7449c12..9724ec6 100644..100755 --- a/silx/gui/plot/test/testPlotWidget.py +++ b/silx/gui/plot/test/testPlotWidget.py @@ -32,6 +32,7 @@ __date__ = "03/01/2019" import unittest import logging import numpy +import sys from silx.utils.testutils import ParametricTestCase, parameterize from silx.gui.utils.testutils import SignalListener @@ -42,6 +43,7 @@ from silx.test.utils import test_options from silx.gui import qt from silx.gui.plot import PlotWidget from silx.gui.plot.items.curve import CurveStyle +from silx.gui.plot.items.shape import BoundingRect from silx.gui.colors import Colormap from .utils import PlotWidgetTestCase @@ -57,6 +59,19 @@ DATA_2D = numpy.arange(SIZE ** 2).reshape(SIZE, SIZE) logger = logging.getLogger(__name__) +class TestSpecialBackend(PlotWidgetTestCase, ParametricTestCase): + + def __init__(self, methodName='runTest', backend=None): + TestCaseQt.__init__(self, methodName=methodName) + self.__backend = backend + + def _createPlot(self): + return PlotWidget(backend=self.__backend) + + def testPlot(self): + self.assertIsNotNone(self.plot) + + class TestPlotWidget(PlotWidgetTestCase, ParametricTestCase): """Basic tests for PlotWidget""" @@ -475,6 +490,56 @@ class TestPlotCurve(PlotWidgetTestCase): replace=False, resetzoom=False, color=color, symbol='o') + def testPlotBaselineNumpyArray(self): + """simple test of the API with baseline as a numpy array""" + x = numpy.arange(0, 10, step=0.1) + my_sin = numpy.sin(x) + y = numpy.arange(-4, 6, step=0.1) + my_sin + baseline = y - 1.0 + + self.plot.addCurve(x=x, y=y, color='grey', legend='curve1', fill=True, + baseline=baseline) + + def testPlotBaselineScalar(self): + """simple test of the API with baseline as an int""" + x = numpy.arange(0, 10, step=0.1) + my_sin = numpy.sin(x) + y = numpy.arange(-4, 6, step=0.1) + my_sin + + self.plot.addCurve(x=x, y=y, color='grey', legend='curve1', fill=True, + baseline=0) + + def testPlotBaselineList(self): + """simple test of the API with baseline as an int""" + x = numpy.arange(0, 10, step=0.1) + my_sin = numpy.sin(x) + y = numpy.arange(-4, 6, step=0.1) + my_sin + + self.plot.addCurve(x=x, y=y, color='grey', legend='curve1', fill=True, + baseline=list(range(0, 100, 1))) + + +class TestPlotHistogram(PlotWidgetTestCase): + """Basic tests for add Histogram""" + def setUp(self): + super(TestPlotHistogram, self).setUp() + self.edges = numpy.arange(0, 10, step=1) + self.histogram = numpy.random.random(len(self.edges)) + + def testPlot(self): + self.plot.addHistogram(histogram=self.histogram, + edges=self.edges, + legend='histogram1') + + def testPlotBaseline(self): + self.plot.addHistogram(histogram=self.histogram, + edges=self.edges, + legend='histogram1', + color='blue', + baseline=-2, + z=2, + fill=True) + class TestPlotScatter(PlotWidgetTestCase, ParametricTestCase): """Basic tests for addScatter""" @@ -487,7 +552,7 @@ class TestPlotScatter(PlotWidgetTestCase, ParametricTestCase): self.plot.resetZoom() def testScatterVisualization(self): - self.plot.addScatter((0, 1, 2, 3), (2, 0, 2, 1), (0, 1, 2, 3)) + self.plot.addScatter((0, 1, 0, 1), (0, 0, 2, 2), (0, 1, 2, 3)) self.plot.resetZoom() self.qapp.processEvents() @@ -495,12 +560,76 @@ class TestPlotScatter(PlotWidgetTestCase, ParametricTestCase): for visualization in ('solid', 'points', + 'regular_grid', scatter.Visualization.SOLID, - scatter.Visualization.POINTS): + scatter.Visualization.POINTS, + scatter.Visualization.REGULAR_GRID): with self.subTest(visualization=visualization): scatter.setVisualization(visualization) self.qapp.processEvents() + def testGridVisualization(self): + """Test regular and irregular grid mode with different points""" + points = { # name: (x, y, order) + 'single point': ((1.,), (1.,), 'row'), + 'horizontal line': ((0, 1, 2), (0, 0, 0), 'row'), + 'horizontal line backward': ((2, 1, 0), (0, 0, 0), 'row'), + 'vertical line': ((0, 0, 0), (0, 1, 2), 'row'), + 'vertical line backward': ((0, 0, 0), (2, 1, 0), 'row'), + 'grid fast x, +x +y': ((0, 1, 2, 0, 1, 2), (0, 0, 0, 1, 1, 1), 'row'), + 'grid fast x, +x -y': ((0, 1, 2, 0, 1, 2), (1, 1, 1, 0, 0, 0), 'row'), + 'grid fast x, -x -y': ((2, 1, 0, 2, 1, 0), (1, 1, 1, 0, 0, 0), 'row'), + 'grid fast x, -x +y': ((2, 1, 0, 2, 1, 0), (0, 0, 0, 1, 1, 1), 'row'), + 'grid fast y, +x +y': ((0, 0, 0, 1, 1, 1), (0, 1, 2, 0, 1, 2), 'column'), + 'grid fast y, +x -y': ((0, 0, 0, 1, 1, 1), (2, 1, 0, 2, 1, 0), 'column'), + 'grid fast y, -x -y': ((1, 1, 1, 0, 0, 0), (2, 1, 0, 2, 1, 0), 'column'), + 'grid fast y, -x +y': ((1, 1, 1, 0, 0, 0), (0, 1, 2, 0, 1, 2), 'column'), + } + + self.plot.addScatter((), (), ()) + scatter = self.plot.getItems()[0] + + self.qapp.processEvents() + + for visualization in (scatter.Visualization.REGULAR_GRID, + scatter.Visualization.IRREGULAR_GRID): + scatter.setVisualization(visualization) + self.assertIs(scatter.getVisualization(), visualization) + + for name, (x, y, ref_order) in points.items(): + with self.subTest(name=name, visualization=visualization.name): + scatter.setData(x, y, numpy.arange(len(x))) + self.plot.setGraphTitle(name) + self.plot.resetZoom() + self.qapp.processEvents() + + order = scatter.getCurrentVisualizationParameter( + scatter.VisualizationParameter.GRID_MAJOR_ORDER) + self.assertEqual(ref_order, order) + + ref_bounds = (x[0], y[0]), (x[-1], y[-1]) + bounds = scatter.getCurrentVisualizationParameter( + scatter.VisualizationParameter.GRID_BOUNDS) + self.assertEqual(ref_bounds, bounds) + + shape = scatter.getCurrentVisualizationParameter( + scatter.VisualizationParameter.GRID_SHAPE) + + self.plot.getXAxis().setLimits(numpy.min(x) - 1, numpy.max(x) + 1) + self.plot.getYAxis().setLimits(numpy.min(y) - 1, numpy.max(y) + 1) + self.qapp.processEvents() + + for index, position in enumerate(zip(x, y)): + xpixel, ypixel = self.plot.dataToPixel(*position) + result = scatter.pick(xpixel, ypixel) + if (visualization is scatter.Visualization.IRREGULAR_GRID and + (shape[0] < 2 or shape[1] < 2)): + self.assertIsNone(result) + else: + self.assertIsNotNone(result) + self.assertIs(result.getItem(), scatter) + self.assertEqual(result.getIndices()[0], (index,)) + class TestPlotMarker(PlotWidgetTestCase): """Basic tests for add*Marker""" @@ -593,6 +722,39 @@ class TestPlotMarker(PlotWidgetTestCase): self.plot.resetZoom() + def testPlotMarkerYAxis(self): + # Check only the API + + legend = self.plot.addMarker(10, 10) + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "left") + + legend = self.plot.addMarker(10, 10, yaxis="right") + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "right") + + legend = self.plot.addMarker(10, 10, yaxis="left") + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "left") + + legend = self.plot.addXMarker(10, yaxis="right") + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "right") + + legend = self.plot.addXMarker(10, yaxis="left") + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "left") + + legend = self.plot.addYMarker(10, yaxis="right") + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "right") + + legend = self.plot.addYMarker(10, yaxis="left") + item = self.plot._getMarker(legend) + self.assertEqual(item.getYAxis(), "left") + + self.plot.resetZoom() + # TestPlotItem ################################################################ @@ -1185,6 +1347,53 @@ class TestPlotAxes(TestCaseQt, ParametricTestCase): """Test coverage on setAxesDisplayed(True)""" self.plot.setAxesDisplayed(True) + def testBoundingRectItem(self): + item = BoundingRect() + item.setBounds((-1000, 1000, -2000, 2000)) + self.plot._add(item) + self.plot.resetZoom() + limits = numpy.array(self.plot.getXAxis().getLimits()) + numpy.testing.assert_almost_equal(limits, numpy.array([-1000, 1000])) + limits = numpy.array(self.plot.getYAxis().getLimits()) + numpy.testing.assert_almost_equal(limits, numpy.array([-2000, 2000])) + + def testBoundingRectRightItem(self): + item = BoundingRect() + item.setYAxis("right") + item.setBounds((-1000, 1000, -2000, 2000)) + self.plot._add(item) + self.plot.resetZoom() + limits = numpy.array(self.plot.getXAxis().getLimits()) + numpy.testing.assert_almost_equal(limits, numpy.array([-1000, 1000])) + limits = numpy.array(self.plot.getYAxis("right").getLimits()) + numpy.testing.assert_almost_equal(limits, numpy.array([-2000, 2000])) + + def testBoundingRectArguments(self): + item = BoundingRect() + with self.assertRaises(Exception): + item.setBounds((1000, -1000, -2000, 2000)) + with self.assertRaises(Exception): + item.setBounds((-1000, 1000, 2000, -2000)) + + def testBoundingRectWithLog(self): + item = BoundingRect() + self.plot._add(item) + + item.setBounds((-1000, 1000, -2000, 2000)) + self.plot.getXAxis()._setLogarithmic(True) + self.plot.getYAxis()._setLogarithmic(False) + self.assertEqual(item.getBounds(), (1000, 1000, -2000, 2000)) + + item.setBounds((-1000, 1000, -2000, 2000)) + self.plot.getXAxis()._setLogarithmic(False) + self.plot.getYAxis()._setLogarithmic(True) + self.assertEqual(item.getBounds(), (-1000, 1000, 2000, 2000)) + + item.setBounds((-1000, 0, -2000, 2000)) + self.plot.getXAxis()._setLogarithmic(True) + self.plot.getYAxis()._setLogarithmic(False) + self.assertIsNone(item.getBounds()) + class TestPlotCurveLog(PlotWidgetTestCase, ParametricTestCase): """Basic tests for addCurve with log scale axes""" @@ -1564,6 +1773,7 @@ def suite(): testClasses = (TestPlotWidget, TestPlotImage, TestPlotCurve, + TestPlotHistogram, TestPlotScatter, TestPlotMarker, TestPlotItem, @@ -1581,6 +1791,10 @@ def suite(): for testClass in testClasses: test_suite.addTest(parameterize(testClass, backend=None)) + test_suite.addTest(parameterize(TestSpecialBackend, backend=u"mpl")) + if sys.version_info[0] == 2: + test_suite.addTest(parameterize(TestSpecialBackend, backend=b"mpl")) + if test_options.WITH_GL_TEST: # Tests with OpenGL backend for testClass in testClasses: |