|
12 | 12 |
|
13 | 13 | import blosc2
|
14 | 14 |
|
| 15 | + |
| 16 | +@nb.jit(nopython=True, parallel=True) |
| 17 | +def numba1p(inputs_tuple, output, offset): |
| 18 | + x = inputs_tuple[0] |
| 19 | + output[:] = x + 1 |
| 20 | + |
| 21 | + |
15 | 22 | @pytest.mark.parametrize(
|
16 | 23 | "shape, chunks, blocks",
|
17 | 24 | [
|
| 25 | + # Test different shapes with and without padding |
18 | 26 | ((10, 10), (10, 10), (10, 10),),
|
19 | 27 | ((20, 20), (10, 10), (10, 10),),
|
20 | 28 | ((20, 20), (10, 10), (5, 5),),
|
|
24 | 32 | ((13, 13), (10, 10), (4, 4),),
|
25 | 33 | ],
|
26 | 34 | )
|
27 |
| -def test_numba_expr(shape, chunks, blocks): #, dtype, urlpath, contiguous |
28 |
| - @nb.jit(nopython=True, parallel=True) |
29 |
| - def func_numba(inputs_tuple, output, offset): |
30 |
| - x = inputs_tuple[0] |
31 |
| - output[:] = x + 1 |
32 |
| - |
33 |
| - # Create a NDArray from a NumPy array |
| 35 | +def test_numba1p(shape, chunks, blocks): |
34 | 36 | npa = np.linspace(0, 1, np.prod(shape)).reshape(shape)
|
35 | 37 | npc = npa + 1
|
36 | 38 |
|
37 |
| - # a = blosc2.asarray(npa) |
38 |
| - expr = blosc2.expr_from_udf(func_numba, ((npa, npa.dtype), ), npa.dtype, chunks=chunks, blocks=blocks) |
| 39 | + expr = blosc2.expr_from_udf(numba1p, ((npa, npa.dtype), ), npa.dtype, chunks=chunks, blocks=blocks) |
39 | 40 | res = expr.eval()
|
| 41 | + |
40 | 42 | tol = 1e-5 if res.dtype is np.float32 else 1e-14
|
41 | 43 | if res.dtype in (np.float32, np.float64):
|
42 | 44 | np.testing.assert_allclose(res[...], npc, rtol=tol, atol=tol)
|
43 | 45 |
|
| 46 | + |
| 47 | +@nb.jit(nopython=True, parallel=True) |
| 48 | +def numba2p(inputs_tuple, output, offset): |
| 49 | + x = inputs_tuple[0] |
| 50 | + y = inputs_tuple[1] |
| 51 | + for i in nb.prange(x.shape[0]): |
| 52 | + for j in nb.prange(x.shape[1]): |
| 53 | + output[i, j] = x[i, j] ** 2 + y[i, j] ** 2 + 2 * x[i, j] * y[i, j] + 1 |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize( |
| 57 | + "shape, chunks, blocks", |
| 58 | + [ |
| 59 | + ((20, 20), (10, 10), (5, 5),), |
| 60 | + ((13, 13, 10), (10, 10, 5), (5, 5, 3),), |
| 61 | + ((13, 13), (10, 10), (5, 5),), |
| 62 | + ], |
| 63 | +) |
| 64 | +def test_numba2p(shape, chunks, blocks): |
| 65 | + npa = np.arange(0, np.prod(shape)).reshape(shape) |
| 66 | + npb = np.arange(1, np.prod(shape) + 1).reshape(shape) |
| 67 | + npc = npa**2 + npb**2 + 2 * npa * npb + 1 |
| 68 | + |
| 69 | + b = blosc2.asarray(npb) |
| 70 | + expr = blosc2.expr_from_udf(numba2p, ((npa, npa.dtype), (b, b.dtype)), npa.dtype, chunks=chunks, blocks=blocks) |
| 71 | + res = expr.eval() |
| 72 | + |
| 73 | + np.testing.assert_allclose(res[...], npc) |
| 74 | + |
| 75 | + |
| 76 | +@nb.jit(nopython=True, parallel=True) |
| 77 | +def numba1dim(inputs_tuple, output, offset): |
| 78 | + x = inputs_tuple[0] |
| 79 | + y = inputs_tuple[1] |
| 80 | + z = inputs_tuple[2] |
| 81 | + output[:] = x + y + z |
| 82 | + |
| 83 | + |
| 84 | +# Test with np.ndarray, blosc2.SChunk and python scalar operands |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "shape, chunks, blocks", |
| 87 | + [ |
| 88 | + ((20, ), (10, ), (5, ),), |
| 89 | + ((23, ), (10, ), (3, ),), |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_numba1dim(shape, chunks, blocks): |
| 93 | + npa = np.arange(start=0, stop=np.prod(shape)).reshape(shape) |
| 94 | + npb = np.arange(start=1, stop=np.prod(shape) + 1).reshape(shape) |
| 95 | + py_scalar = np.e |
| 96 | + npc = npa + npb + py_scalar |
| 97 | + |
| 98 | + b = blosc2.SChunk(data=npb) |
| 99 | + expr = blosc2.expr_from_udf(numba1dim, ((npa, npa.dtype), (b, npb.dtype), (py_scalar, np.float64)), |
| 100 | + np.float64, chunks=chunks, blocks=blocks) |
| 101 | + res = expr.eval() |
| 102 | + |
| 103 | + tol = 1e-5 if res.dtype is np.float32 else 1e-14 |
| 104 | + if res.dtype in (np.float32, np.float64): |
| 105 | + np.testing.assert_allclose(res[...], npc, rtol=tol, atol=tol) |
0 commit comments