test_sino_normalization.py 2.98 KB
Newer Older
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
import os.path as path
import numpy as np
import pytest
from nabu.testutils import get_data
from nabu.cuda.utils import __has_pycuda__
from nabu.preproc.sinogram import SinoNormalization
if __has_pycuda__:
    from nabu.preproc.sinogram_cuda import CudaSinoNormalization
    import pycuda.gpuarray as garray


@pytest.fixture(scope="class")
def bootstrap(request):
    cls = request.cls
    cls.sino = get_data("sino_refill.npy")
    cls.tol = 1e-7


@pytest.mark.usefixtures("bootstrap")
class TestSinoNormalization:

    def test_sino_normalization(self):
        sino_proc = SinoNormalization(
            kind="chebyshev", sinos_shape=self.sino.shape
        )
        sino = self.sino.copy()
        sino_proc.normalize(sino)


    @pytest.mark.skipif(not(__has_pycuda__), reason="Need pycuda for sinogram normalization with cuda backend")
31
    def test_sino_normalization_cuda(self):
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        sino_proc = SinoNormalization(
            kind="chebyshev", sinos_shape=self.sino.shape
        )
        sino = self.sino.copy()
        ref = sino_proc.normalize(sino)

        cuda_sino_proc = CudaSinoNormalization(
            kind="chebyshev", sinos_shape=self.sino.shape
        )
        d_sino = garray.to_gpu(self.sino)
        cuda_sino_proc.normalize(d_sino)
        res = d_sino.get()

        assert np.max(np.abs(res - ref)) < self.tol

47
48
49

    def test_sino_array_subtraction(self):
        with pytest.raises(ValueError):
50
            SinoNormalization(kind="subtraction", sinos_shape=self.sino.shape)
51
52
53
54
55
56

        array_1D = np.arange(self.sino.shape[-1])
        array_2D = np.arange(self.sino.size).reshape(self.sino.shape)

        def compare_normalizations(normalization_arr):
            sino_normalization = SinoNormalization(
57
                kind="subtraction", sinos_shape=self.sino.shape,
58
59
60
61
62
63
64
65
66
                normalization_array=normalization_arr
            )
            sino = self.sino.copy()
            sino_normalization.normalize(sino)
            ref = self.sino - normalization_arr
            assert np.allclose(sino, ref)

        compare_normalizations(array_1D)
        compare_normalizations(array_2D)
67
68
69


    @pytest.mark.skipif(not(__has_pycuda__), reason="Need pycuda for sinogram normalization with cuda backend")
70
    def xtest_sino_array_subtraction_cuda(self):
71
        with pytest.raises(ValueError):
72
            CudaSinoNormalization(kind="subtraction", sinos_shape=self.sino.shape)
73
74
75
76
77
78

        array_1D = np.arange(self.sino.shape[-1])
        array_2D = np.arange(self.sino.size).reshape(self.sino.shape)

        def compare_normalizations(normalization_arr):
            sino_normalization = CudaSinoNormalization(
79
                kind="subtraction", sinos_shape=self.sino.shape,
80
81
82
83
84
85
86
87
88
                normalization_array=normalization_arr
            )
            sino = garray.to_gpu(self.sino)
            sino_normalization.normalize(sino)
            ref = self.sino - normalization_arr
            assert np.allclose(sino.get(), ref)

        compare_normalizations(array_1D)
        compare_normalizations(array_2D)