Skip to content

Commit 978f74d

Browse files
committed
Add test with different shapes and operands
1 parent 06c63bb commit 978f74d

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

blosc2/blosc2_ext.pyx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,6 @@ cdef int general_numba(blosc2_prefilter_params *params):
15711571
cdef int64_t start_ndim[B2ND_MAX_DIM]
15721572
for i in range(nd):
15731573
start_ndim[i] = chunk_ndim[i] * udata.array.chunkshape[i] + block_ndim[i] * udata.array.blockshape[i]
1574-
# print("start_ndim[", i, "] = ", start_ndim[i])
15751574

15761575
padding = False
15771576
blockshape = []
@@ -1606,23 +1605,22 @@ cdef int general_numba(blosc2_prefilter_params *params):
16061605
elif isinstance(obj, np.ndarray):
16071606
inputs.append(obj[start_ndim[0] : start_ndim[0] + blockshape[0]])
16081607
elif isinstance(obj, (int, float, bool, complex)):
1609-
inputs.append(np.full(blockshape[0], obj, dtype=dtype))
1608+
inputs.append(obj)
16101609
else:
16111610
raise ValueError("Unsupported operand")
16121611
else:
16131612
# Get tuple of slices
16141613
l = []
16151614
for i in range(nd):
16161615
l.append(slice(start_ndim[i], start_ndim[i] + blockshape[i]))
1617-
# print("slice dim ", i, " = inici = ", start_ndim[i], " final = ", start_ndim[i] + blockshape[i])
16181616
slices = tuple(l)
16191617
for obj, dtype in inputs_tuple:
16201618
if isinstance(obj, blosc2.NDArray):
16211619
inputs.append(obj[slices])
16221620
elif isinstance(obj, np.ndarray):
16231621
inputs.append(obj[slices])
16241622
elif isinstance(obj, (int, float, bool, complex)):
1625-
inputs.append(np.full(blockshape, obj, dtype=dtype))
1623+
inputs.append(obj)
16261624
else:
16271625
raise ValueError("Unsupported operand")
16281626

tests/ndarray/test_numba_expr.py

+71-9
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212

1313
import blosc2
1414

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+
1522
@pytest.mark.parametrize(
1623
"shape, chunks, blocks",
1724
[
25+
# Test different shapes with and without padding
1826
((10, 10), (10, 10), (10, 10),),
1927
((20, 20), (10, 10), (10, 10),),
2028
((20, 20), (10, 10), (5, 5),),
@@ -24,20 +32,74 @@
2432
((13, 13), (10, 10), (4, 4),),
2533
],
2634
)
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):
3436
npa = np.linspace(0, 1, np.prod(shape)).reshape(shape)
3537
npc = npa + 1
3638

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)
3940
res = expr.eval()
41+
4042
tol = 1e-5 if res.dtype is np.float32 else 1e-14
4143
if res.dtype in (np.float32, np.float64):
4244
np.testing.assert_allclose(res[...], npc, rtol=tol, atol=tol)
4345

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

Comments
 (0)