summaryrefslogtreecommitdiff
path: root/silx/opencl/test/test_linalg.py
blob: 7d0398314128b69f0620187e2ef296a45dfab6c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016 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.
#
# ###########################################################################*/
"""Test of the linalg module"""

from __future__ import division, print_function

__authors__ = ["Pierre paleo"]
__license__ = "MIT"
__copyright__ = "2013-2017 European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "14/06/2017"


import time
import logging
import numpy as np
import unittest
try:
    import mako
except ImportError:
    mako = None
from ..common import ocl
if ocl:
    import pyopencl as cl
    import pyopencl.array as parray
    from .. import linalg
from silx.test.utils import utilstest

logger = logging.getLogger(__name__)
try:
    from scipy.ndimage.filters import laplace
    _has_scipy = True
except ImportError:
    _has_scipy = False


# TODO move this function in math or image ?
def gradient(img):
    '''
    Compute the gradient of an image as a numpy array
    Code from https://github.com/emmanuelle/tomo-tv/
    '''
    shape = [img.ndim, ] + list(img.shape)
    gradient = np.zeros(shape, dtype=img.dtype)
    slice_all = [0, slice(None, -1),]
    for d in range(img.ndim):
        gradient[slice_all] = np.diff(img, axis=d)
        slice_all[0] = d + 1
        slice_all.insert(1, slice(None))
    return gradient


# TODO move this function in math or image ?
def divergence(grad):
    '''
    Compute the divergence of a gradient
    Code from https://github.com/emmanuelle/tomo-tv/
    '''
    res = np.zeros(grad.shape[1:])
    for d in range(grad.shape[0]):
        this_grad = np.rollaxis(grad[d], d)
        this_res = np.rollaxis(res, d)
        this_res[:-1] += this_grad[:-1]
        this_res[1:-1] -= this_grad[:-2]
        this_res[-1] -= this_grad[-2]
    return res


@unittest.skipUnless(ocl and mako, "PyOpenCl is missing")
class TestLinAlg(unittest.TestCase):

    def setUp(self):
        if ocl is None:
            return
        self.getfiles()
        self.la = linalg.LinAlg(self.image.shape)
        self.allocate_arrays()

    def allocate_arrays(self):
        """
        Allocate various types of arrays for the tests
        """
        # numpy images
        self.grad = np.zeros(self.image.shape, dtype=np.complex64)
        self.grad2 = np.zeros((2,) + self.image.shape, dtype=np.float32)
        self.grad_ref = gradient(self.image)
        self.div_ref = divergence(self.grad_ref)
        self.image2 = np.zeros_like(self.image)
        # Device images
        self.gradient_parray = parray.zeros(self.la.queue, self.image.shape, np.complex64)
        # we should be using cl.Buffer(self.la.ctx, cl.mem_flags.READ_WRITE, size=self.image.nbytes*2),
        # but platforms not suporting openCL 1.2 have a problem with enqueue_fill_buffer,
        # so we use the parray "fill" utility
        self.gradient_buffer = self.gradient_parray.data
        # Do the same for image
        self.image_parray = parray.to_device(self.la.queue, self.image)
        self.image_buffer = self.image_parray.data
        # Refs
        tmp = np.zeros(self.image.shape, dtype=np.complex64)
        tmp.real = np.copy(self.grad_ref[0])
        tmp.imag = np.copy(self.grad_ref[1])
        self.grad_ref_parray = parray.to_device(self.la.queue, tmp)
        self.grad_ref_buffer = self.grad_ref_parray.data

    def tearDown(self):
        self.image = None
        self.image2 = None
        self.grad = None
        self.grad2 = None
        self.grad_ref = None
        self.div_ref = None
        self.gradient_parray.data.release()
        self.gradient_parray = None
        self.gradient_buffer = None
        self.image_parray.data.release()
        self.image_parray = None
        self.image_buffer = None
        self.grad_ref_parray.data.release()
        self.grad_ref_parray = None
        self.grad_ref_buffer = None

    def getfiles(self):
        # load 512x512 MRI phantom - TODO include Lena or ascent once a .npz is available
        self.image = np.load(utilstest.getfile("Brain512.npz"))["data"]

    def compare(self, result, reference, abstol, name):
        errmax = np.max(np.abs(result - reference))
        logger.info("%s: Max error = %e" % (name, errmax))
        self.assertTrue(errmax < abstol, str("%s: Max error is too high" % name))

    @unittest.skipUnless(ocl and mako, "pyopencl is missing")
    def test_gradient(self):
        arrays = {
            "numpy.ndarray": self.image,
            "buffer": self.image_buffer,
            "parray": self.image_parray
        }
        for desc, image in arrays.items():
            # Test with dst on host (numpy.ndarray)
            res = self.la.gradient(image, return_to_host=True)
            self.compare(res, self.grad_ref, 1e-6, str("gradient[src=%s, dst=numpy.ndarray]" % desc))
            # Test with dst on device (pyopencl.Buffer)
            self.la.gradient(image, dst=self.gradient_buffer)
            cl.enqueue_copy(self.la.queue, self.grad, self.gradient_buffer)
            self.grad2[0] = self.grad.real
            self.grad2[1] = self.grad.imag
            self.compare(self.grad2, self.grad_ref, 1e-6, str("gradient[src=%s, dst=buffer]" % desc))
            # Test with dst on device (pyopencl.Array)
            self.la.gradient(image, dst=self.gradient_parray)
            self.grad = self.gradient_parray.get()
            self.grad2[0] = self.grad.real
            self.grad2[1] = self.grad.imag
            self.compare(self.grad2, self.grad_ref, 1e-6, str("gradient[src=%s, dst=parray]" % desc))

    @unittest.skipUnless(ocl and mako, "pyopencl is missing")
    def test_divergence(self):
        arrays = {
            "numpy.ndarray": self.grad_ref,
            "buffer": self.grad_ref_buffer,
            "parray": self.grad_ref_parray
        }
        for desc, grad in arrays.items():
            # Test with dst on host (numpy.ndarray)
            res = self.la.divergence(grad, return_to_host=True)
            self.compare(res, self.div_ref, 1e-6, str("divergence[src=%s, dst=numpy.ndarray]" % desc))
            # Test with dst on device (pyopencl.Buffer)
            self.la.divergence(grad, dst=self.image_buffer)
            cl.enqueue_copy(self.la.queue, self.image2, self.image_buffer)
            self.compare(self.image2, self.div_ref, 1e-6, str("divergence[src=%s, dst=buffer]" % desc))
            # Test with dst on device (pyopencl.Array)
            self.la.divergence(grad, dst=self.image_parray)
            self.image2 = self.image_parray.get()
            self.compare(self.image2, self.div_ref, 1e-6, str("divergence[src=%s, dst=parray]" % desc))

    @unittest.skipUnless(ocl and mako and _has_scipy, "pyopencl and/or scipy is missing")
    def test_laplacian(self):
        laplacian_ref = laplace(self.image)
        # Laplacian = div(grad)
        self.la.gradient(self.image)
        laplacian_ocl = self.la.divergence(self.la.d_gradient, return_to_host=True)
        self.compare(laplacian_ocl, laplacian_ref, 1e-6, "laplacian")


def suite():
    testSuite = unittest.TestSuite()
    testSuite.addTest(TestLinAlg("test_gradient"))
    testSuite.addTest(TestLinAlg("test_divergence"))
    testSuite.addTest(TestLinAlg("test_laplacian"))
    return testSuite


if __name__ == '__main__':
    unittest.main(defaultTest="suite")