|
3 | 3 | from cupy.cuda import nvtx |
4 | 4 | import numpy as np |
5 | 5 | import pytest |
6 | | -import pyfftw |
7 | | -import pyfftw.interfaces.numpy_fft as fft |
| 6 | + |
8 | 7 | from httomolibgpu.prep.normalize import normalize |
9 | 8 | from httomolibgpu.prep.stripe import ( |
10 | 9 | remove_stripe_based_sorting, |
|
13 | 12 | raven_filter, |
14 | 13 | ) |
15 | 14 | from numpy.testing import assert_allclose |
16 | | - |
17 | | -def raven_filter_cpu( |
18 | | - sinogram, |
19 | | - uvalue: int = 20, |
20 | | - nvalue: int = 4, |
21 | | - vvalue: int = 2, |
22 | | - pad_y: int = 20, |
23 | | - pad_x: int = 20, |
24 | | - pad_method: str = "edge"): |
25 | | - |
26 | | - # Parameters |
27 | | - v0 = vvalue |
28 | | - n = nvalue |
29 | | - u0 = uvalue |
30 | | - |
31 | | - # Make a padded copy |
32 | | - sinogram_padded = cp.pad(sinogram, ((pad_y,pad_y), (0, 0), (pad_x,pad_x)), pad_method).get() |
33 | | - |
34 | | - # Size |
35 | | - height, images, width = sinogram_padded.shape |
36 | | - |
37 | | - # Generate filter function |
38 | | - centerx = np.ceil(width / 2.0) - 1.0 |
39 | | - centery = np.int16(np.ceil(height / 2.0) - 1) |
40 | | - row1 = centery - v0 |
41 | | - row2 = centery + v0 + 1 |
42 | | - listx = np.arange(width) - centerx |
43 | | - filtershape = 1.0 / (1.0 + np.power(listx / u0, 2 * n)) |
44 | | - filtershapepad2d = np.zeros((row2 - row1, filtershape.size)) |
45 | | - filtershapepad2d[:] = np.float64(filtershape) |
46 | | - filtercomplex = filtershapepad2d + filtershapepad2d * 1j |
47 | | - |
48 | | - # Generate filter objects |
49 | | - a = pyfftw.empty_aligned((height, images, width), dtype='complex128', n=16) |
50 | | - b = pyfftw.empty_aligned((height, images, width), dtype='complex128', n=16) |
51 | | - c = pyfftw.empty_aligned((height, images, width), dtype='complex128', n=16) |
52 | | - d = pyfftw.empty_aligned((height, images, width), dtype='complex128', n=16) |
53 | | - fft_object = pyfftw.FFTW(a, b, axes=(0, 2)) |
54 | | - ifft_object = pyfftw.FFTW(c, d, axes=(0, 2), direction='FFTW_BACKWARD') |
55 | | - |
56 | | - sino = fft.fftshift(fft_object(sinogram_padded), axes=(0, 2)) |
57 | | - for m in range(sino.shape[1]): |
58 | | - sino[row1:row2, m] = sino[row1:row2, m] * filtercomplex |
59 | | - sino = ifft_object(fft.ifftshift(sino, axes=(0, 2))) |
60 | | - sinogram = sino[pad_y:height-pad_y, :, pad_x:width-pad_x] |
61 | | - |
62 | | - return sinogram.real |
| 15 | +from .stripe_cpu_reference import raven_filter_cpu |
63 | 16 |
|
64 | 17 | def test_remove_stripe_ti_on_data(data, flats, darks): |
65 | 18 | # --- testing the CuPy implementation from TomoCupy ---# |
@@ -119,10 +72,10 @@ def test_stripe_raven_cupy(data, flats, darks): |
119 | 72 |
|
120 | 73 | data = normalize(data, flats, darks, cutoff=10, minus_log=True) |
121 | 74 |
|
122 | | - data_after_raven_gpu = raven_filter(cp.copy(data)).get() |
123 | | - data_after_raven_cpu = raven_filter_cpu(cp.copy(data)) |
| 75 | + data_after_raven_gpu = raven_filter(cp.copy(data)) |
| 76 | + data_after_raven_cpu = cp.asarray(raven_filter_cpu(cp.copy(data).get())) |
124 | 77 |
|
125 | | - assert_allclose(data_after_raven_cpu, data_after_raven_gpu, 0, atol=4e-01) |
| 78 | + cp.testing.assert_allclose(data_after_raven_cpu, data_after_raven_gpu, rtol=0, atol=4e-01) |
126 | 79 |
|
127 | 80 | data = None #: free up GPU memory |
128 | 81 | # make sure the output is float32 |
@@ -210,11 +163,11 @@ def test_raven_filter_cpu_performance(ensure_clean_memory): |
210 | 163 | data = cp.asarray(data_host, dtype=np.float32) |
211 | 164 |
|
212 | 165 | # do a cold run first |
213 | | - raven_filter_cpu(cp.copy(data)) |
| 166 | + raven_filter_cpu(cp.copy(data).get()) |
214 | 167 |
|
215 | 168 | start = time.perf_counter_ns() |
216 | 169 | for _ in range(10): |
217 | | - raven_filter_cpu(cp.copy(data)) |
| 170 | + raven_filter_cpu(cp.copy(data).get()) |
218 | 171 |
|
219 | 172 | duration_ms = float(time.perf_counter_ns() - start) * 1e-6 / 10 |
220 | 173 |
|
|
0 commit comments