diff options
author | Alexandre Marie <alexandre.marie@synchrotron-soleil.fr> | 2019-07-09 10:20:20 +0200 |
---|---|---|
committer | Alexandre Marie <alexandre.marie@synchrotron-soleil.fr> | 2019-07-09 10:20:20 +0200 |
commit | 654a6ac93513c3cc1ef97cacd782ff674c6f4559 (patch) | |
tree | 3b986e4972de7c57fa465820367602fc34bcb0d3 /silx/test | |
parent | a763e5d1b3921b3194f3d4e94ab9de3fbe08bbdd (diff) |
New upstream version 0.11.0+dfsg
Diffstat (limited to 'silx/test')
-rw-r--r-- | silx/test/__init__.py | 4 | ||||
-rw-r--r-- | silx/test/test_resources.py | 57 | ||||
-rw-r--r-- | silx/test/test_sx.py | 292 | ||||
-rw-r--r-- | silx/test/utils.py | 11 |
4 files changed, 305 insertions, 59 deletions
diff --git a/silx/test/__init__.py b/silx/test/__init__.py index 8bada7f..0991469 100644 --- a/silx/test/__init__.py +++ b/silx/test/__init__.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2015-2018 European Synchrotron Radiation Facility +# Copyright (c) 2015-2019 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 @@ -60,7 +60,7 @@ def suite(): suite.addTest(SkipSXTest()) return suite else: - from ..sx.test import suite as test_sx_suite + from .test_sx import suite as test_sx_suite from . import test_version from . import test_resources diff --git a/silx/test/test_resources.py b/silx/test/test_resources.py index 8d3bd41..7f5f432 100644 --- a/silx/test/test_resources.py +++ b/silx/test/test_resources.py @@ -26,19 +26,15 @@ __authors__ = ["T. Vincent"] __license__ = "MIT" -__date__ = "17/01/2018" +__date__ = "08/03/2019" import os import unittest import shutil -import socket import tempfile -import six - import silx.resources -from .utils import utilstest class TestResources(unittest.TestCase): @@ -191,63 +187,12 @@ class TestResourcesWithCustomDirectory(TestResources): super(TestResourcesWithCustomDirectory, cls).tearDownClass() -def isSilxWebsiteAvailable(): - try: - six.moves.urllib.request.urlopen('http://www.silx.org', timeout=1) - return True - except six.moves.urllib.error.URLError: - return False - except socket.timeout: - # This exception is still received in Python 2.7 - return False - - -class TestExternalResources(unittest.TestCase): - """This is a test for the ExternalResources""" - - @classmethod - def setUpClass(cls): - if not isSilxWebsiteAvailable(): - raise unittest.SkipTest("Network or silx website not available") - - def setUp(self): - self.utilstest = silx.resources.ExternalResources("toto", "http://www.silx.org/pub/silx/") - - def tearDown(self): - if self.utilstest.data_home: - shutil.rmtree(self.utilstest.data_home) - self.utilstest = None - - def test_download(self): - "test the download from silx.org" - f = self.utilstest.getfile("lena.png") - self.assertTrue(os.path.exists(f)) - di = utilstest.getdir("source.tar.gz") - for fi in di: - self.assertTrue(os.path.exists(fi)) - - def test_download_all(self): - "test the download of all files from silx.org" - filename = self.utilstest.getfile("lena.png") - directory = "source.tar.gz" - _filelist = self.utilstest.getdir(directory) - # download file and remove it to create a json mapping file - os.remove(filename) - directory_path = os.path.join(self.utilstest.data_home, "source") - shutil.rmtree(directory_path) - directory_path = os.path.join(self.utilstest.data_home, directory) - os.remove(directory_path) - filelist = self.utilstest.download_all() - self.assertGreater(len(filelist), 1, "At least 2 items were downloaded") - - def suite(): loadTests = unittest.defaultTestLoader.loadTestsFromTestCase test_suite = unittest.TestSuite() test_suite.addTest(loadTests(TestResources)) test_suite.addTest(loadTests(TestResourcesWithoutPkgResources)) test_suite.addTest(loadTests(TestResourcesWithCustomDirectory)) - test_suite.addTest(loadTests(TestExternalResources)) return test_suite diff --git a/silx/test/test_sx.py b/silx/test/test_sx.py new file mode 100644 index 0000000..a32cc06 --- /dev/null +++ b/silx/test/test_sx.py @@ -0,0 +1,292 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016-2019 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__ = ["T. Vincent", "P. Knobel"] +__license__ = "MIT" +__date__ = "06/11/2018" + + +import logging +import unittest +import numpy + +from silx.utils.testutils import ParametricTestCase +from silx.test.utils import test_options + +from silx.gui import qt +# load TestCaseQt before sx +from silx.gui.utils.testutils import TestCaseQt +from silx.gui.colors import rgba +from silx.gui.colors import Colormap + + +_logger = logging.getLogger(__name__) + + +class SXTest(TestCaseQt, ParametricTestCase): + """Test the sx module""" + + def _expose_and_close(self, plot): + self.qWaitForWindowExposed(plot) + self.qapp.processEvents() + plot.setAttribute(qt.Qt.WA_DeleteOnClose) + plot.close() + + def test_plot(self): + """Test plot function""" + from silx import sx # Lazy loading to avoid it to create QApplication + + y = numpy.random.random(100) + x = numpy.arange(len(y)) * 0.5 + + # Nothing + plt = sx.plot() + self._expose_and_close(plt) + + # y + plt = sx.plot(y, title='y') + self._expose_and_close(plt) + + # y, style + plt = sx.plot(y, 'blued ', title='y, "blued "') + self._expose_and_close(plt) + + # x, y + plt = sx.plot(x, y, title='x, y') + self._expose_and_close(plt) + + # x, y, style + plt = sx.plot(x, y, 'ro-', xlabel='x', title='x, y, "ro-"') + self._expose_and_close(plt) + + # x, y, style, y + plt = sx.plot(x, y, 'ro-', y ** 2, xlabel='x', ylabel='y', + title='x, y, "ro-", y ** 2') + self._expose_and_close(plt) + + # x, y, style, y, style + plt = sx.plot(x, y, 'ro-', y ** 2, 'b--', + title='x, y, "ro-", y ** 2, "b--"') + self._expose_and_close(plt) + + # x, y, style, x, y, style + plt = sx.plot(x, y, 'ro-', x, y ** 2, 'b--', + title='x, y, "ro-", x, y ** 2, "b--"') + self._expose_and_close(plt) + + # x, y, x, y + plt = sx.plot(x, y, x, y ** 2, title='x, y, x, y ** 2') + self._expose_and_close(plt) + + def test_imshow(self): + """Test imshow function""" + from silx import sx # Lazy loading to avoid it to create QApplication + + img = numpy.arange(100.).reshape(10, 10) + 1 + + # Nothing + plt = sx.imshow() + self._expose_and_close(plt) + + # image + plt = sx.imshow(img) + self._expose_and_close(plt) + + # image, named cmap + plt = sx.imshow(img, cmap='jet', title='jet cmap') + self._expose_and_close(plt) + + # image, custom colormap + plt = sx.imshow(img, cmap=Colormap(), title='custom colormap') + self._expose_and_close(plt) + + # image, log cmap + plt = sx.imshow(img, norm='log', title='log cmap') + self._expose_and_close(plt) + + # image, fixed range + plt = sx.imshow(img, vmin=10, vmax=20, + title='[10,20] cmap') + self._expose_and_close(plt) + + # image, keep ratio + plt = sx.imshow(img, aspect=True, + title='keep ratio') + self._expose_and_close(plt) + + # image, change origin and scale + plt = sx.imshow(img, origin=(10, 10), scale=(2, 2), + title='origin=(10, 10), scale=(2, 2)') + self._expose_and_close(plt) + + # image, origin='lower' + plt = sx.imshow(img, origin='upper', title='origin="lower"') + self._expose_and_close(plt) + + def test_scatter(self): + """Test scatter function""" + from silx import sx # Lazy loading to avoid it to create QApplication + + x = numpy.arange(100) + y = numpy.arange(100) + values = numpy.arange(100) + + # simple scatter + plt = sx.scatter(x, y, values) + self._expose_and_close(plt) + + # No value + plt = sx.scatter(x, y, values) + self._expose_and_close(plt) + + # single value + plt = sx.scatter(x, y, 10.) + self._expose_and_close(plt) + + # set size + plt = sx.scatter(x, y, values, size=20) + self._expose_and_close(plt) + + # set colormap + plt = sx.scatter(x, y, values, cmap='jet') + self._expose_and_close(plt) + + # set colormap range + plt = sx.scatter(x, y, values, vmin=2, vmax=50) + self._expose_and_close(plt) + + # set colormap normalisation + plt = sx.scatter(x, y, values, norm='log') + self._expose_and_close(plt) + + def test_ginput(self): + """Test ginput function + + This does NOT perform interactive tests + """ + from silx import sx # Lazy loading to avoid it to create QApplication + + for create_plot in (sx.plot, sx.imshow, sx.scatter): + with self.subTest(create_plot.__name__): + plt = create_plot() + self.qWaitForWindowExposed(plt) + self.qapp.processEvents() + + result = sx.ginput(1, timeout=0.1) + self.assertEqual(len(result), 0) + + plt.setAttribute(qt.Qt.WA_DeleteOnClose) + plt.close() + + @unittest.skipUnless(test_options.WITH_GL_TEST, + test_options.WITH_GL_TEST_REASON) + def test_contour3d(self): + """Test contour3d function""" + from silx import sx # Lazy loading to avoid it to create QApplication + + coords = numpy.linspace(-10, 10, 64) + z = coords.reshape(-1, 1, 1) + y = coords.reshape(1, -1, 1) + x = coords.reshape(1, 1, -1) + data = numpy.sin(x * y * z) / (x * y * z) + + # Just data + window = sx.contour3d(data) + + isosurfaces = window.getIsosurfaces() + self.assertEqual(len(isosurfaces), 1) + + if not window.getPlot3DWidget().isValid(): + self.skipTest("OpenGL context is not valid") + + # N contours + color + colors = ['red', 'green', 'blue'] + window = sx.contour3d(data, copy=False, contours=len(colors), + color=colors) + + isosurfaces = window.getIsosurfaces() + self.assertEqual(len(isosurfaces), len(colors)) + for iso, color in zip(isosurfaces, colors): + self.assertEqual(rgba(iso.getColor()), rgba(color)) + + # by isolevel, single color + contours = 0.2, 0.5 + window = sx.contour3d(data, copy=False, contours=contours, + color='yellow') + + isosurfaces = window.getIsosurfaces() + self.assertEqual(len(isosurfaces), len(contours)) + for iso, level in zip(isosurfaces, contours): + self.assertEqual(iso.getLevel(), level) + self.assertEqual(rgba(iso.getColor()), + rgba('yellow')) + + # Single isolevel, colormap + window = sx.contour3d(data, copy=False, contours=0.5, + colormap='gray', vmin=0.6, opacity=0.4) + + isosurfaces = window.getIsosurfaces() + self.assertEqual(len(isosurfaces), 1) + self.assertEqual(isosurfaces[0].getLevel(), 0.5) + self.assertEqual(rgba(isosurfaces[0].getColor()), + (0., 0., 0., 0.4)) + + @unittest.skipUnless(test_options.WITH_GL_TEST, + test_options.WITH_GL_TEST_REASON) + def test_points3d(self): + """Test points3d function""" + from silx import sx # Lazy loading to avoid it to create QApplication + + x = numpy.random.random(1024) + y = numpy.random.random(1024) + z = numpy.random.random(1024) + values = numpy.random.random(1024) + + # 3D positions, no value + window = sx.points3d(x, y, z) + + if not window.getSceneWidget().isValid(): + self.skipTest("OpenGL context is not valid") + + # 3D positions, values + window = sx.points3d(x, y, z, values, mode='2dsquare', + colormap='magma', vmin=0.4, vmax=0.5) + + # 2D positions, no value + window = sx.points3d(x, y) + + # 2D positions, values + window = sx.points3d(x, y, values=values, mode=',', + colormap='magma', vmin=0.4, vmax=0.5) + + +def suite(): + test_suite = unittest.TestSuite() + test_suite.addTest( + unittest.defaultTestLoader.loadTestsFromTestCase(SXTest)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') diff --git a/silx/test/utils.py b/silx/test/utils.py index 12bede9..77746c6 100644 --- a/silx/test/utils.py +++ b/silx/test/utils.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2016-2017 European Synchrotron Radiation Facility +# Copyright (c) 2016-2019 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 @@ -96,6 +96,15 @@ class _TestOptions(object): elif os.environ.get('WITH_GL_TEST', 'True') == 'False': self.WITH_GL_TEST = False self.WITH_GL_TEST_REASON = "Skipped by WITH_GL_TEST env var" + elif sys.platform.startswith('linux') and not os.environ.get('DISPLAY', ''): + self.WITH_GL_TEST = False + self.WITH_GL_TEST_REASON = "DISPLAY env variable not set" + else: + try: + import OpenGL + except ImportError: + self.WITH_GL_TEST = False + self.WITH_GL_TEST_REASON = "OpenGL package not available" if (parsed_options is not None and parsed_options.low_mem) or os.environ.get('SILX_TEST_LOW_MEM', 'True') == 'False': self.TEST_LOW_MEM = True |