Commit 6acaed3f authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Softfail if pycuda not found ; start using pytest with test_padding

parent 211e7b01
......@@ -19,6 +19,9 @@ test:
- source $HOME/.venv/py3/bin/activate
- python --version
- which python
# Install a version of silx containing not-released-yet features needed for Nabu
# Hopefully this wont be needed after the 0.12 release
- pip install silx --no-index --find-links http://www.silx.org/pub/nabu/wheels/ --trusted-host www.silx.org
- pip install --upgrade pip
- pip install --upgrade setuptools
- pip install --upgrade numpy
......
import numpy as np
from os.path import dirname
import pycuda.gpuarray as garray
from pycuda.compiler import SourceModule
from ..utils import updiv, get_cuda_srcfile
from .utils import __has_pycuda__
if __has_pycuda__:
import pycuda.gpuarray as garray
from pycuda.compiler import SourceModule
from .processing import CudaProcessing
from silx.opencl.utils import ConvolutionInfos
from silx.image.utils import gaussian_kernel
from ..utils import updiv, get_cuda_srcfile
from .processing import CudaProcessing
class Convolution(CudaProcessing):
......
import pycuda.gpuarray as garray
from pycuda.compiler import SourceModule
from ..utils import updiv
from .utils import __has_pycuda__
if __has_pycuda__:
import pycuda.gpuarray as garray
from pycuda.compiler import SourceModule
class CudaKernel(object):
"""
......
import numpy as np
from os.path import dirname
import pycuda.gpuarray as garray
from pycuda.compiler import SourceModule
from silx.opencl.utils import ConvolutionInfos
from silx.image.utils import gaussian_kernel
from ..utils import updiv, get_cuda_srcfile
from .processing import CudaProcessing
from .utils import __has_pycuda__
if __has_pycuda__:
import pycuda.gpuarray as garray
from pycuda.compiler import SourceModule
class MedianFilter(CudaProcessing):
......
import numpy as np
import pycuda.driver as cuda
import pycuda.gpuarray as garray
from .utils import get_cuda_context
dev_attrs = cuda.device_attribute
from .utils import get_cuda_context, __has_pycuda__
if __has_pycuda__:
import pycuda.driver as cuda
import pycuda.gpuarray as garray
dev_attrs = cuda.device_attribute
# TODO add logger ? inherit from Processing class with logger ?
# NB: we must detach from a context before creating another context
......
import unittest
import pytest
from . import test_padding
from . import test_medfilt
......
import unittest
from itertools import product
import numpy as np
import pycuda.gpuarray as garray
from nabu.cuda.medfilt import MedianFilter
from nabu.cuda.utils import __has_pycuda__
if __has_pycuda__:
import pycuda.gpuarray as garray
try:
from scipy.misc import ascent
from scipy.ndimage.filters import median_filter
......
import numpy as np
import unittest
import pycuda.gpuarray as garray
import pytest
from nabu.cuda.utils import get_cuda_context, __has_pycuda__
from nabu.utils import calc_padding_lengths, get_cuda_srcfile
from nabu.cuda.utils import get_cuda_context
from nabu.cuda.kernel import CudaKernel
from nabu.testutils import get_test_data, ParametrizedTestCase
if __has_pycuda__:
import pycuda.gpuarray as garray
class TestPadding(ParametrizedTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.data0 = get_test_data("mri_proj_astra")["data"]
cls.tol = 1e-7
cls.ctx = get_cuda_context()
scenarios = [
{
"shape": (512, 501),
"shape_padded": (1023, 1022),
"constant_values": ((1., 2.), (3., 4.)),
},
]
@classmethod
def tearDownClass(cls):
super().tearDownClass()
def __init__(self, methodName='runTest', param=None):
unittest.TestCase.__init__(self, methodName)
self.param = param
@pytest.fixture(scope='class')
def bootstrap(request):
cls = request.cls
cls.data = get_test_data("mri_proj_astra")["data"]
cls.tol = 1e-7
cls.ctx = get_cuda_context()
cls.calc_pad()
cls.init_kernels()
def setUp(self):
self.calc_pad()
self.init_kernels()
@pytest.mark.skipif(not(__has_pycuda__), reason="Need Cuda and pycuda for this test")
@pytest.mark.usefixtures('bootstrap')
class TestPadding:
def calc_pad(self):
self.shape = self.param["shape"]
self.data = np.ascontiguousarray(self.data0[:self.shape[0], :self.shape[1]])
self.shape_padded = self.param["shape_padded"]
((pt, pb), (pl, pr)) = calc_padding_lengths(self.shape, self.shape_padded)
self.pad_top_len = pt
self.pad_bottom_len = pb
self.pad_left_len = pl
self.pad_right_len = pr
params = {
"shape": (512, 501),
"shape_padded": (1023, 1022),
"constant_values": ((1., 2.), (3., 4.)),
}
@classmethod
def calc_pad(cls):
cls.shape = cls.params["shape"]
cls.data = np.ascontiguousarray(cls.data[:cls.shape[0], :cls.shape[1]])
cls.shape_padded = cls.params["shape_padded"]
((pt, pb), (pl, pr)) = calc_padding_lengths(cls.shape, cls.shape_padded)
cls.pad_top_len = pt
cls.pad_bottom_len = pb
cls.pad_left_len = pl
cls.pad_right_len = pr
def init_kernels(self):
self.pad_kern = CudaKernel(
@classmethod
def init_kernels(cls):
cls.pad_kern = CudaKernel(
"padding_constant",
filename=get_cuda_srcfile("padding.cu"),
signature="Piiiiiiiiffff",
)
self.pad_edge_kern = CudaKernel(
cls.pad_edge_kern = CudaKernel(
"padding_edge",
filename=get_cuda_srcfile("padding.cu"),
signature="Piiiiiiii",
)
self.d_data_padded = garray.zeros(self.shape_padded, "f")
cls.d_data_padded = garray.zeros(cls.shape_padded, "f")
def _init_padding(self, arr=None):
......@@ -59,7 +70,6 @@ class TestPadding(ParametrizedTestCase):
Ny, Nx = self.shape
self.d_data_padded[:Ny, :Nx] = self.data
def _pad_numpy(self, arr=None, **np_pad_kwargs):
arr = arr or self.data
data_padded_ref = np.pad(
......@@ -75,11 +85,10 @@ class TestPadding(ParametrizedTestCase):
)
return data_padded_ref
def test_constant_padding(self):
self._init_padding()
# Pad using the cuda kernel
((val_top, val_bottom), (val_left, val_right)) = self.param["constant_values"]
((val_top, val_bottom), (val_left, val_right)) = self.params["constant_values"]
Ny, Nx = self.shape
Nyp, Nxp = self.shape_padded
......@@ -94,17 +103,16 @@ class TestPadding(ParametrizedTestCase):
# Pad using numpy
data_padded_ref = self._pad_numpy(
mode="constant",
constant_values=self.param["constant_values"]
constant_values=self.params["constant_values"]
)
# Compare
errmax = np.max(np.abs(self.d_data_padded.get() - data_padded_ref))
self.assertLess(errmax, self.tol, "Max error is too high")
assert errmax < self.tol, "Max error is too high"
def test_edge_padding(self):
self._init_padding()
# Pad using the cuda kernel
((val_top, val_bottom), (val_left, val_right)) = self.param["constant_values"]
((val_top, val_bottom), (val_left, val_right)) = self.params["constant_values"]
Ny, Nx = self.shape
Nyp, Nxp = self.shape_padded
......@@ -119,36 +127,5 @@ class TestPadding(ParametrizedTestCase):
data_padded_ref = self._pad_numpy(mode="edge")
# Compare
errmax = np.max(np.abs(self.d_data_padded.get() - data_padded_ref))
self.assertLess(errmax, self.tol, "Max error is too high")
def test_padding():
testSuite = unittest.TestSuite()
testCase = ParametrizedTestCase.parametrize(
TestPadding,
param={
"shape": (512, 501),
"shape_padded": (1023, 1022),
"constant_values": ((1., 2.), (3., 4.)),
}
)
testSuite.addTest(testCase)
return testSuite
def suite():
suite = unittest.TestSuite()
suite.addTest(test_padding())
return suite
assert errmax < self.tol, "Max error is too high"
if __name__ == '__main__':
unittest.main(defaultTest="suite")
import numpy as np
from math import sqrt, pi
from silx.math.fft.cufft import CUFFT
import pycuda.driver as cuda
from pycuda import gpuarray as garray
from ..utils import updiv, get_cuda_srcfile, _sizeof
from .phase import PaganinPhaseRetrieval
from ..cuda.utils import __has_pycuda__
if __has_pycuda__:
from silx.math.fft.cufft import CUFFT
import pycuda.driver as cuda
from pycuda import gpuarray as garray
from ..cuda.processing import CudaProcessing
from ..cuda.kernel import CudaKernel
from .phase import PaganinPhaseRetrieval
class CudaPaganinPhaseRetrieval(PaganinPhaseRetrieval, CudaProcessing):
......
......@@ -3,12 +3,9 @@ import unittest
from nabu.preproc.ccd import CCDCorrection
from nabu.utils import median2 as nabu_median_filter
from nabu.testutils import get_test_data
try:
from nabu.preproc.ccd_cuda import CudaCCDCorrection
from nabu.cuda.utils import __has_pycuda__ as __have_cuda__
if __have_cuda__:
import pycuda.gpuarray as garray
__have_cuda__ = True
except ImportError:
__have_cuda__ = False
......
import numpy as np
import unittest
from nabu.cuda.utils import get_cuda_context
from nabu.cuda.utils import get_cuda_context, __has_pycuda__
from nabu.preproc.phase import PaganinPhaseRetrieval, PaddingMode
from nabu.preproc.phase_cuda import CudaPaganinPhaseRetrieval, __have_cuda__
from nabu.preproc.phase_cuda import CudaPaganinPhaseRetrieval
from nabu.testutils import get_test_data, ParametrizedTestCase
from nabu.thirdparty.tomopy_phase import retrieve_phase
__have_cuda__ = __has_pycuda__
if __have_cuda__:
import pycuda.gpuarray as garray
......
import numpy as np
from math import sqrt, pi
import pycuda.driver as cuda
from pycuda import gpuarray as garray
from ..utils import updiv, get_cuda_srcfile, _sizeof
from ..cuda.utils import copy_array, get_cuda_context
from ..cuda.utils import copy_array, get_cuda_context, __has_pycuda__
from ..cuda.processing import CudaProcessing
from ..cuda.kernel import CudaKernel
from .filtering import SinoFilter
if __has_pycuda__:
import pycuda.driver as cuda
from pycuda import gpuarray as garray
class Backprojector(CudaProcessing):
......
......@@ -2,13 +2,14 @@ from math import pi
from bisect import bisect
from itertools import product
import numpy as np
import pycuda.gpuarray as garray
from silx.math.fft.cufft import CUFFT
from silx.image.tomography import compute_fourier_filter, get_next_power
from ..cuda.kernel import CudaKernel
from ..cuda.processing import CudaProcessing
from ..utils import get_cuda_srcfile, check_supported, updiv
from ..cuda.utils import __has_pycuda__
if __has_pycuda__:
from silx.math.fft.cufft import CUFFT
import pycuda.gpuarray as garray
class SinoFilter(CudaProcessing):
......
......@@ -95,12 +95,12 @@ def setup_package():
},
install_requires = [
'numpy > 1.9.0',
'silx >= 0.10.0',
'silx >= 0.11.0',
'distributed',
# ~ 'pycuda',
# ~ 'pyopencl',
'tomoscan @ git+https://gitlab.esrf.fr/tomotools/tomoscan.git',
#~ 'sidi', #
'pytest',
],
long_description = """
Nabu - Tomography software
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment