[python-dtcwt] 149/497: opencl: support importing without pyopencl

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Tue Jul 21 18:05:59 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository python-dtcwt.

commit a834e0f359519b6c23d115fcd2b09edea4249b49
Author: Rich Wareham <rjw57 at cam.ac.uk>
Date:   Thu Nov 7 15:32:12 2013 +0000

    opencl: support importing without pyopencl
    
    Allow importing of the opencl code but raise a NoCLPresentError if one
    tries to use it without OpenCL installed. This allows the tests to be
    skipped on machines without an implementation. It also allows Sphinx to
    process API documentation for OpenCL modules even if OpenCL is not
    present.
---
 dtcwt/opencl/lowlevel.py     | 21 +++++++++++++++++++--
 tests/testopenclcoldfilt.py  | 12 ++++++++++--
 tests/testopenclcolfilter.py | 10 +++++++++-
 tests/testopenclcolifilt.py  | 11 ++++++++++-
 tests/util.py                | 12 ++++++++++++
 5 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/dtcwt/opencl/lowlevel.py b/dtcwt/opencl/lowlevel.py
index 8b74b71..34743e1 100644
--- a/dtcwt/opencl/lowlevel.py
+++ b/dtcwt/opencl/lowlevel.py
@@ -1,5 +1,15 @@
-import pyopencl as cl
-import pyopencl.array as cl_array
+# Wrap importing of pyopencl in a try/except block since it is not an error to
+# not have OpenCL installed when using dtcwt.
+try:
+    import pyopencl as cl
+    import pyopencl.array as cl_array
+    _HAVE_CL = True
+except ImportError:
+    _HAVE_CL = False
+
+class NoCLPresentError(RuntimeError):
+    pass
+
 import numpy as np
 from six.moves import xrange
 import struct
@@ -202,12 +212,17 @@ def colifilt(X, ha, hb):
 
     return Y
 
+def _check_cl():
+    if not _HAVE_CL:
+        raise NoCLPresentError('PyOpenCL must be installed to use OpenCL routines')
+
 @memoize
 def get_default_queue():
     """Return the default queue used for computation if one is not specified.
 
     This function is memoized and so only one queue is created after multiple invocations.
     """
+    _check_cl()
     ctx = cl.create_some_context()
     return cl.CommandQueue(ctx)
 
@@ -291,6 +306,7 @@ def axis_convolve(X, h, axis=0, queue=None, output=None):
     created.
     """
 
+    _check_cl()
     queue = _to_queue(queue)
     kern = _convolve_kernel_for_queue(queue.context)
 
@@ -304,6 +320,7 @@ def axis_convolve(X, h, axis=0, queue=None, output=None):
     return _apply_kernel(X, h, kern, output, axis=axis)
 
 def axis_convolve_dfilter(X, h, axis=0, queue=None, output=None):
+    _check_cl()
     queue = _to_queue(queue)
     kern = _dfilter_kernel_for_queue(queue.context)
 
diff --git a/tests/testopenclcoldfilt.py b/tests/testopenclcoldfilt.py
index a4d0a73..a813d06 100644
--- a/tests/testopenclcoldfilt.py
+++ b/tests/testopenclcoldfilt.py
@@ -2,12 +2,13 @@ import os
 
 import numpy as np
 from dtcwt.lowlevel import coldfilt as coldfilt_gold
-from dtcwt.opencl.lowlevel import coldfilt
+from dtcwt.opencl.lowlevel import coldfilt, NoCLPresentError
 from dtcwt.coeffs import biort, qshift
 
 from nose.tools import raises
+from nose import SkipTest
 
-from .util import assert_almost_equal
+from .util import assert_almost_equal, skip_if_no_cl
 
 def setup():
     global lena
@@ -20,33 +21,40 @@ def test_lena_loaded():
     assert lena.dtype == np.float32
 
 @raises(ValueError)
+ at skip_if_no_cl
 def test_odd_filter():
     coldfilt(lena, (-1,2,-1), (-1,2,1))
 
 @raises(ValueError)
+ at skip_if_no_cl
 def test_different_size():
     coldfilt(lena, (-0.5,-1,2,1,0.5), (-1,2,-1))
 
 @raises(ValueError)
+ at skip_if_no_cl
 def test_bad_input_size():
     coldfilt(lena[:511,:], (-1,1), (1,-1))
 
+ at skip_if_no_cl
 def test_real_wavelet():
     h0a, h0b, g0a, g0b, h1a, h1b, g1a, g1b = qshift('qshift_d')
     A = coldfilt(lena[:,:511], h1b, h1a)
     B = coldfilt_gold(lena[:,:511], h1b, h1a)
     assert_almost_equal(A, B)
 
+ at skip_if_no_cl
 def test_good_input_size():
     A = coldfilt(lena[:,:511], (-1,1), (1,-1))
     B = coldfilt_gold(lena[:,:511], (-1,1), (1,-1))
     assert_almost_equal(A, B)
 
+ at skip_if_no_cl
 def test_good_input_size_non_orthogonal():
     A = coldfilt(lena[:,:511], (1,1), (1,1))
     B = coldfilt_gold(lena[:,:511], (1,1), (1,1))
     assert_almost_equal(A, B)
 
+ at skip_if_no_cl
 def test_output_size():
     Y = coldfilt(lena, (-1,1), (1,-1))
     assert Y.shape == (lena.shape[0]/2, lena.shape[1])
diff --git a/tests/testopenclcolfilter.py b/tests/testopenclcolfilter.py
index 0f0f63f..5818dca 100644
--- a/tests/testopenclcolfilter.py
+++ b/tests/testopenclcolfilter.py
@@ -5,7 +5,7 @@ from dtcwt import biort, qshift
 from dtcwt.opencl.lowlevel import colfilter
 from dtcwt.lowlevel import colfilter as colfilter_gold
 
-from .util import assert_almost_equal
+from .util import assert_almost_equal, skip_if_no_cl
 
 def setup():
     global lena
@@ -17,6 +17,7 @@ def test_lena_loaded():
     assert lena.max() <= 1
     assert lena.dtype == np.float32
 
+ at skip_if_no_cl
 def test_odd_size():
     y = colfilter(lena, (-1,2,-1))
     assert y.shape == lena.shape
@@ -24,6 +25,7 @@ def test_odd_size():
     z = colfilter_gold(lena, (-1,2,-1))
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_even_size():
     y = colfilter(lena, (-1,1))
     assert y.shape == (lena.shape[0]+1, lena.shape[1])
@@ -31,6 +33,7 @@ def test_even_size():
     z = colfilter_gold(lena, (-1,1))
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_odd_size():
     y = colfilter(lena, (-1,2,-1))
     assert y.shape == lena.shape
@@ -38,6 +41,7 @@ def test_odd_size():
     z = colfilter_gold(lena, (-1,2,-1))
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_qshift():
     y = colfilter(lena, qshift('qshift_a')[0])
     assert y.shape == (lena.shape[0]+1, lena.shape[1])
@@ -45,6 +49,7 @@ def test_qshift():
     z = colfilter_gold(lena, qshift('qshift_a')[0])
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_biort():
     y = colfilter(lena, biort('antonini')[0])
     assert y.shape == lena.shape
@@ -52,6 +57,7 @@ def test_biort():
     z = colfilter_gold(lena, biort('antonini')[0])
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_even_size():
     y = colfilter(np.zeros_like(lena), (-1,1))
     assert y.shape == (lena.shape[0]+1, lena.shape[1])
@@ -60,6 +66,7 @@ def test_even_size():
     z = colfilter_gold(np.zeros_like(lena), (-1,1))
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_odd_size_non_array():
     y = colfilter(lena.tolist(), (-1,2,-1))
     assert y.shape == lena.shape
@@ -67,6 +74,7 @@ def test_odd_size_non_array():
     z = colfilter_gold(lena.tolist(), (-1,2,-1))
     assert_almost_equal(y, z)
 
+ at skip_if_no_cl
 def test_even_size_non_array():
     y = colfilter(lena.tolist(), (-1,1))
     assert y.shape == (lena.shape[0]+1, lena.shape[1])
diff --git a/tests/testopenclcolifilt.py b/tests/testopenclcolifilt.py
index 6c107c2..cfe911e 100644
--- a/tests/testopenclcolifilt.py
+++ b/tests/testopenclcolifilt.py
@@ -6,7 +6,7 @@ from dtcwt.lowlevel import colifilt as colifilt_gold
 
 from nose.tools import raises
 
-from .util import assert_almost_equal
+from .util import assert_almost_equal, skip_if_no_cl
 
 def setup():
     global lena
@@ -18,45 +18,54 @@ def test_lena_loaded():
     assert lena.max() <= 1
     assert lena.dtype == np.float32
 
+ at skip_if_no_cl
 @raises(ValueError)
 def test_odd_filter():
     colifilt(lena, (-1,2,-1), (-1,2,1))
 
+ at skip_if_no_cl
 @raises(ValueError)
 def test_different_size_h():
     colifilt(lena, (-1,2,1), (-0.5,-1,2,-1,0.5))
 
+ at skip_if_no_cl
 def test_zero_input():
     Y = colifilt(np.zeros_like(lena), (-1,1), (1,-1))
     assert np.all(Y[:0] == 0)
 
+ at skip_if_no_cl
 @raises(ValueError)
 def test_bad_input_size():
     colifilt(lena[:511,:], (-1,1), (1,-1))
 
+ at skip_if_no_cl
 def test_good_input_size():
     Y = colifilt(lena[:,:511], (-1,1), (1,-1))
     Z = colifilt_gold(lena[:,:511], (-1,1), (1,-1))
     assert_almost_equal(Y,Z)
 
+ at skip_if_no_cl
 def test_output_size():
     Y = colifilt(lena, (-1,1), (1,-1))
     assert Y.shape == (lena.shape[0]*2, lena.shape[1])
     Z = colifilt_gold(lena, (-1,1), (1,-1))
     assert_almost_equal(Y,Z)
 
+ at skip_if_no_cl
 def test_non_orthogonal_input():
     Y = colifilt(lena, (1,1), (1,1))
     assert Y.shape == (lena.shape[0]*2, lena.shape[1])
     Z = colifilt_gold(lena, (1,1), (1,1))
     assert_almost_equal(Y,Z)
 
+ at skip_if_no_cl
 def test_output_size_non_mult_4():
     Y = colifilt(lena, (-1,0,0,1), (1,0,0,-1))
     assert Y.shape == (lena.shape[0]*2, lena.shape[1])
     Z = colifilt_gold(lena, (-1,0,0,1), (1,0,0,-1))
     assert_almost_equal(Y,Z)
 
+ at skip_if_no_cl
 def test_non_orthogonal_input_non_mult_4():
     Y = colifilt(lena, (1,0,0,1), (1,0,0,1))
     assert Y.shape == (lena.shape[0]*2, lena.shape[1])
diff --git a/tests/util.py b/tests/util.py
index dd3ea60..4be8e72 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -1,5 +1,8 @@
+import functools
 import numpy as np
 
+from dtcwt.opencl.lowlevel import NoCLPresentError
+
 TOLERANCE = 1e-6
 
 def assert_almost_equal(a, b, tolerance=TOLERANCE):
@@ -36,3 +39,12 @@ def summarise_mat(M, apron=8):
         np.hstack((_mean(M[apron:-apron,:apron,...], axis=0), centre_sum, _mean(M[apron:-apron,-apron:,...], axis=0))),
         np.hstack((M[-apron:,:apron,...], _mean(M[-apron:,apron:-apron,...], axis=1), M[-apron:,-apron:,...])),
     ))
+
+def skip_if_no_cl(f):
+    @functools.wraps(f)
+    def wrapper(*args, **kwargs):
+        try:
+            return f(*args, **kwargs)
+        except NoCLPresentError:
+            raise SkipTest('Skipping due to no CL library being present')
+    return wrapper

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-dtcwt.git



More information about the debian-science-commits mailing list