コード例 #1
0
 def test_gpu_available_returns_false_when_cupy_cant_be_loaded(self):
     """
     Test that the GPU utility reports the GPU is not available if cupy isn't installed.
     """
     with mock.patch("mantidimaging.core.gpu.utility.cp", side_effect=ModuleNotFoundError):
         from mantidimaging.core.gpu import utility
         assert not utility.gpu_available()
コード例 #2
0
 def test_gpu_available_returns_false_when_cupy_isnt_installed_properly(self):
     """
     Test that the GPU utility reports the GPU is not available if cupy is installed but not set up properly.
     """
     import cupy
     with mock.patch("cupy.add", side_effect=cupy.cuda.compiler.CompileException("", "", "", None)):
         from mantidimaging.core.gpu import utility
         assert not utility.gpu_available()
コード例 #3
0
    def test_executed_with_nan(self, _, use_cpu):
        if not use_cpu and not gpu.gpu_available():
            self.skipTest(reason="Skip GPU tests if cupy isn't installed")
        shape = (1, 20, 20)
        images = th.generate_images(shape=shape, seed=2021)

        images.data[0, 0, 1] = np.nan  # single edge
        images.data[0, 4, 4] = np.nan  # single
        images.data[0, 4, 7] = np.nan  # diagonal neighbours
        images.data[0, 5, 8] = np.nan
        images.data[0, 7:9, 2:4] = np.nan  # 2x2 block
        images.data[0, 7:9, 6:9] = np.nan  # 2x3
        images.data[0, 12:15, 2:5] = np.nan  # 3x3
        self.assertTrue(np.any(np.isnan(images.data)))

        result = MedianFilter.filter_func(images.copy(),
                                          3,
                                          'reflect',
                                          force_cpu=use_cpu)

        npt.assert_equal(np.isnan(result.data), np.isnan(images.data))
コード例 #4
0
# Copyright (C) 2021 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later

import unittest

from unittest import mock

from mantidimaging.core.gpu import utility as gpu

GPU_NOT_AVAIL = not gpu.gpu_available()


class GPUImportTest(unittest.TestCase):
    @unittest.skip("I don't know how to mock a failed import.")
    def test_gpu_available_returns_false_when_cupy_cant_be_loaded(self):
        """
        Test that the GPU utility reports the GPU is not available if cupy isn't installed.
        """
        with mock.patch("mantidimaging.core.gpu.utility.cp", side_effect=ModuleNotFoundError):
            from mantidimaging.core.gpu import utility
            assert not utility.gpu_available()

    @unittest.skipIf(GPU_NOT_AVAIL, reason="Can't run GPU tests without cupy installed.")
    def test_gpu_available_returns_false_when_cupy_isnt_installed_properly(self):
        """
        Test that the GPU utility reports the GPU is not available if cupy is installed but not set up properly.
        """
        import cupy
        with mock.patch("cupy.add", side_effect=cupy.cuda.compiler.CompileException("", "", "", None)):
            from mantidimaging.core.gpu import utility
            assert not utility.gpu_available()
コード例 #5
0
class MedianTest(unittest.TestCase):
    """
    Test median filter.

    Tests return value and in-place modified data.
    """
    def __init__(self, *args, **kwargs):
        super(MedianTest, self).__init__(*args, **kwargs)

    def test_not_executed(self):
        images = th.generate_images()

        size = None
        mode = None

        original = np.copy(images.data[0])
        result = MedianFilter.filter_func(images, size, mode)

        th.assert_not_equals(result.data, original)

    def test_executed_no_helper_parallel(self):
        images = th.generate_images()

        size = 3
        mode = 'reflect'

        original = np.copy(images.data[0])
        result = MedianFilter.filter_func(images, size, mode)

        th.assert_not_equals(result.data, original)

    @unittest.skipIf(not gpu.gpu_available(),
                     reason="Skip GPU tests if cupy isn't installed")
    def test_executed_no_helper_gpu(self):
        images = th.generate_images()

        size = 3
        mode = 'reflect'

        original = np.copy(images.data[0])
        result = MedianFilter.filter_func(images, size, mode, force_cpu=False)

        th.assert_not_equals(result.data, original)

    def test_executed_no_helper_seq(self):
        images = th.generate_images()

        size = 3
        mode = 'reflect'

        original = np.copy(images.data[0])
        th.switch_mp_off()
        result = MedianFilter.filter_func(images, size, mode)
        th.switch_mp_on()

        th.assert_not_equals(result.data, original)

    def test_memory_change_acceptable(self):
        """
        Expected behaviour for the filter is to be done in place
        without using more memory.

        In reality the memory is increased by about 40MB (4 April 2017),
        but this could change in the future.

        The reason why a 10% window is given on the expected size is
        to account for any library imports that may happen.

        This will still capture if the data is doubled, which is the main goal.
        """
        images = th.generate_images()
        size = 3
        mode = 'reflect'

        cached_memory = get_memory_usage_linux(kb=True)[0]

        MedianFilter.filter_func(images, size, mode)

        self.assertLess(
            get_memory_usage_linux(kb=True)[0], cached_memory * 1.1)

    def test_execute_wrapper_return_is_runnable(self):
        """
        Test that the partial returned by execute_wrapper can be executed (kwargs are named correctly)
        """
        size_field = mock.Mock()
        size_field.value = mock.Mock(return_value=0)
        mode_field = mock.Mock()
        mode_field.currentText = mock.Mock(return_value=0)
        use_gpu_field = mock.Mock()
        use_gpu_field.isChecked = mock.Mock(return_value=False)
        execute_func = MedianFilter.execute_wrapper(size_field, mode_field,
                                                    use_gpu_field)

        images = th.generate_images()
        execute_func(images)

        self.assertEqual(size_field.value.call_count, 1)
        self.assertEqual(mode_field.currentText.call_count, 1)
        self.assertEqual(use_gpu_field.isChecked.call_count, 1)
コード例 #6
0
class MedianTest(unittest.TestCase):
    """
    Test median filter.

    Tests return value and in-place modified data.
    """
    @parameterized.expand([("None", None), ("1", 1)])
    def test_exception_raised_for_invalid_size(self, _, size):
        images = th.generate_images()

        mode = None

        npt.assert_raises(ValueError, MedianFilter.filter_func, images, size,
                          mode)

    def test_executed_no_helper_parallel(self):
        images = th.generate_images()

        size = 3
        mode = 'reflect'

        original = np.copy(images.data[0])
        result = MedianFilter.filter_func(images, size, mode)

        th.assert_not_equals(result.data, original)

    @unittest.skipIf(not gpu.gpu_available(),
                     reason="Skip GPU tests if cupy isn't installed")
    def test_executed_no_helper_gpu(self):
        images = th.generate_images()

        size = 3
        mode = 'reflect'

        original = np.copy(images.data[0])
        result = MedianFilter.filter_func(images, size, mode, force_cpu=False)

        th.assert_not_equals(result.data, original)

    def test_executed_seq(self):
        self.do_execute(th.generate_images())

    def test_executed_par(self):
        self.do_execute(th.generate_images_for_parallel())

    def do_execute(self, images: Images):
        size = 3
        mode = 'reflect'

        original = np.copy(images.data[0])
        result = MedianFilter.filter_func(images, size, mode)
        th.assert_not_equals(result.data, original)

    def test_execute_wrapper_return_is_runnable(self):
        """
        Test that the partial returned by execute_wrapper can be executed (kwargs are named correctly)
        """
        size_field = mock.Mock()
        size_field.value = mock.Mock(return_value=3)
        mode_field = mock.Mock()
        mode_field.currentText = mock.Mock(return_value='reflect')
        use_gpu_field = mock.Mock()
        use_gpu_field.isChecked = mock.Mock(return_value=False)
        execute_func = MedianFilter.execute_wrapper(size_field, mode_field,
                                                    use_gpu_field)

        images = th.generate_images()
        execute_func(images)

        self.assertEqual(size_field.value.call_count, 1)
        self.assertEqual(mode_field.currentText.call_count, 1)
        self.assertEqual(use_gpu_field.isChecked.call_count, 1)

    @parameterized.expand([("CPU", True), ("GPU", False)])
    def test_executed_with_nan(self, _, use_cpu):
        if not use_cpu and not gpu.gpu_available():
            self.skipTest(reason="Skip GPU tests if cupy isn't installed")
        shape = (1, 20, 20)
        images = th.generate_images(shape=shape, seed=2021)

        images.data[0, 0, 1] = np.nan  # single edge
        images.data[0, 4, 4] = np.nan  # single
        images.data[0, 4, 7] = np.nan  # diagonal neighbours
        images.data[0, 5, 8] = np.nan
        images.data[0, 7:9, 2:4] = np.nan  # 2x2 block
        images.data[0, 7:9, 6:9] = np.nan  # 2x3
        images.data[0, 12:15, 2:5] = np.nan  # 3x3
        self.assertTrue(np.any(np.isnan(images.data)))

        result = MedianFilter.filter_func(images.copy(),
                                          3,
                                          'reflect',
                                          force_cpu=use_cpu)

        npt.assert_equal(np.isnan(result.data), np.isnan(images.data))