Skip to content

Commit ab91208

Browse files
authored
Merge pull request #1 from arrayfire/master
Up to Date Master Branch
2 parents e404ab7 + f90ef61 commit ab91208

File tree

9 files changed

+512
-2
lines changed

9 files changed

+512
-2
lines changed

arrayfire_wrapper/lib/create_and_modify_array/create_array/pad.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ def pad(arr: AFArray, begin_shape: tuple[int, ...], end_shape: tuple[int, ...],
2222
end_c_shape.c_array,
2323
border_type.value,
2424
)
25-
return NotImplemented
25+
return out

arrayfire_wrapper/lib/create_and_modify_array/manage_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def get_scalar(arr: AFArray, dtype: Dtype, /) -> int | float | complex | bool |
166166
out = dtype.c_type()
167167
call_from_clib(get_scalar.__name__, ctypes.pointer(out), arr)
168168
if dtype == c32 or dtype == c64:
169-
return complex(out[0], out[1]) # type: ignore
169+
return complex(out[0], out[1]) # type: ignore
170170
else:
171171
return cast(int | float | complex | bool | None, out.value)
172172

tests/test_diag.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
3+
import arrayfire_wrapper.dtypes as dtypes
4+
import arrayfire_wrapper.lib as wrapper
5+
6+
7+
@pytest.mark.parametrize("diagonal_shape", [(2,), (10,), (100,), (1000,)])
8+
def test_diagonal_shape(diagonal_shape: tuple) -> None:
9+
"""Test if diagonal array is keeping the shape of the passed into the input array"""
10+
in_arr = wrapper.constant(1, diagonal_shape, dtypes.s16)
11+
diag_array = wrapper.diag_create(in_arr, 0)
12+
13+
extracted_diagonal = wrapper.diag_extract(diag_array, 0)
14+
15+
assert wrapper.get_dims(extracted_diagonal)[0 : len(diagonal_shape)] == diagonal_shape # noqa: E203
16+
17+
18+
@pytest.mark.parametrize("diagonal_shape", [(2,), (10,), (100,), (1000,)])
19+
def test_diagonal_val(diagonal_shape: tuple) -> None:
20+
"""Test if diagonal array is keeping the same value as that of the values passed into the input array"""
21+
dtype = dtypes.s16
22+
in_arr = wrapper.constant(1, diagonal_shape, dtype)
23+
diag_array = wrapper.diag_create(in_arr, 0)
24+
25+
extracted_diagonal = wrapper.diag_extract(diag_array, 0)
26+
27+
assert wrapper.get_scalar(extracted_diagonal, dtype) == wrapper.get_scalar(in_arr, dtype)
28+
29+
30+
@pytest.mark.parametrize(
31+
"diagonal_shape",
32+
[
33+
(10, 10, 10),
34+
(100, 100, 100, 100),
35+
],
36+
)
37+
def test_invalid_diagonal(diagonal_shape: tuple) -> None:
38+
"""Test if an invalid diagonal shape is being properly handled"""
39+
with pytest.raises(RuntimeError):
40+
in_arr = wrapper.constant(1, diagonal_shape, dtypes.s16)
41+
diag_array = wrapper.diag_create(in_arr, 0)
42+
43+
wrapper.diag_extract(diag_array, 0)

tests/test_identity.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import random
2+
3+
import pytest
4+
5+
import arrayfire_wrapper.dtypes as dtypes
6+
import arrayfire_wrapper.lib as wrapper
7+
8+
9+
@pytest.mark.parametrize(
10+
"shape",
11+
[
12+
(),
13+
(random.randint(1, 10), 1),
14+
(random.randint(1, 10), random.randint(1, 10)),
15+
(random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)),
16+
(random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)),
17+
],
18+
)
19+
def test_identity_shape(shape: tuple) -> None:
20+
"""Test if identity creates an array with the correct shape"""
21+
dtype = dtypes.s16
22+
23+
result = wrapper.identity(shape, dtype)
24+
25+
assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa: E203
26+
27+
28+
def test_identity_invalid_shape() -> None:
29+
"""Test if identity handles a shape with greater than 4 dimensions"""
30+
with pytest.raises(TypeError) as excinfo:
31+
invalid_shape = (
32+
random.randint(1, 10),
33+
random.randint(1, 10),
34+
random.randint(1, 10),
35+
random.randint(1, 10),
36+
random.randint(1, 10),
37+
)
38+
dtype = dtypes.s16
39+
40+
wrapper.identity(invalid_shape, dtype)
41+
42+
assert f"CShape.__init__() takes from 1 to 5 positional arguments but {len(invalid_shape) + 1} were given" in str(
43+
excinfo.value
44+
)
45+
46+
47+
def test_identity_nonsquare_shape() -> None:
48+
dtype = dtypes.s16
49+
shape = (5, 6)
50+
51+
result = wrapper.identity(shape, dtype)
52+
53+
assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa: E203
54+
55+
56+
@pytest.mark.parametrize(
57+
"dtype_index",
58+
[i for i in range(13)],
59+
)
60+
def test_identity_dtype(dtype_index: int) -> None:
61+
"""Test if identity creates an array with the correct dtype"""
62+
if dtype_index in [2, 3] and not wrapper.get_dbl_support():
63+
pytest.skip()
64+
65+
shape = (5, 5)
66+
dtype = dtypes.c_api_value_to_dtype(dtype_index)
67+
68+
result = wrapper.identity(shape, dtype)
69+
70+
assert dtypes.c_api_value_to_dtype(wrapper.get_type(result)) == dtype

tests/test_iota.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import random
2+
3+
import numpy as np
4+
import pytest
5+
6+
import arrayfire_wrapper.dtypes as dtypes
7+
import arrayfire_wrapper.lib as wrapper
8+
9+
10+
@pytest.mark.parametrize(
11+
"shape",
12+
[
13+
(),
14+
(random.randint(1, 10), 1),
15+
(random.randint(1, 10), random.randint(1, 10)),
16+
(random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)),
17+
(random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)),
18+
],
19+
)
20+
def test_iota_shape(shape: tuple) -> None:
21+
"""Test if identity creates an array with the correct shape"""
22+
dtype = dtypes.s16
23+
t_shape = (1, 1)
24+
25+
result = wrapper.iota(shape, t_shape, dtype)
26+
27+
assert wrapper.get_dims(result)[0 : len(shape)] == shape # noqa: E203
28+
29+
30+
def test_iota_invalid_shape() -> None:
31+
"""Test if iota handles a shape with greater than 4 dimensions"""
32+
with pytest.raises(TypeError) as excinfo:
33+
invalid_shape = (
34+
random.randint(1, 10),
35+
random.randint(1, 10),
36+
random.randint(1, 10),
37+
random.randint(1, 10),
38+
random.randint(1, 10),
39+
)
40+
dtype = dtypes.s16
41+
t_shape = ()
42+
43+
wrapper.iota(invalid_shape, t_shape, dtype)
44+
45+
assert f"CShape.__init__() takes from 1 to 5 positional arguments but {len(invalid_shape) + 1} were given" in str(
46+
excinfo.value
47+
)
48+
49+
50+
@pytest.mark.parametrize(
51+
"t_shape",
52+
[
53+
(1,),
54+
(random.randint(1, 10), 1),
55+
(random.randint(1, 10), random.randint(1, 10)),
56+
(random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)),
57+
(random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)),
58+
],
59+
)
60+
def test_iota_tshape(t_shape: tuple) -> None:
61+
"""Test if iota properly uses t_shape to change the size of the array and result in correct dimensions"""
62+
shape = np.array([2, 2])
63+
dtype = dtypes.s64
64+
65+
if len(shape.shape) < len(t_shape):
66+
shape = np.append(shape, np.ones(len(t_shape) - len(shape), dtype=int))
67+
68+
result_shape = shape * t_shape
69+
70+
result = wrapper.iota(tuple(shape), t_shape, dtype)
71+
72+
result_dims = tuple(int(value) for value in wrapper.get_dims(result))
73+
74+
assert (result_dims[0 : len(result_shape)] == result_shape).all() # noqa: E203
75+
76+
77+
@pytest.mark.parametrize(
78+
"t_shape",
79+
[
80+
(0,),
81+
(-1, -1),
82+
],
83+
)
84+
def test_iota_tshape_zero(t_shape: tuple) -> None:
85+
"""Test it iota properly handles negative or zero t_shapes"""
86+
with pytest.raises(RuntimeError):
87+
shape = (2, 2)
88+
89+
dtype = dtypes.s16
90+
91+
wrapper.iota(shape, t_shape, dtype)
92+
93+
94+
def test_iota_tshape_invalid() -> None:
95+
"""Test it iota properly handles a tshape with greater than 4 dimensions"""
96+
with pytest.raises(TypeError):
97+
shape = (2, 2)
98+
invalid_tshape = (
99+
random.randint(1, 10),
100+
random.randint(1, 10),
101+
random.randint(1, 10),
102+
random.randint(1, 10),
103+
random.randint(1, 10),
104+
)
105+
dtype = dtypes.s16
106+
107+
wrapper.iota(shape, invalid_tshape, dtype)
108+
109+
110+
@pytest.mark.parametrize(
111+
"dtype_index",
112+
[i for i in range(13)],
113+
)
114+
def test_iota_dtype(dtype_index: int) -> None:
115+
"""Test if iota creates an array with the correct dtype"""
116+
if (dtype_index in [1, 4]) or (dtype_index in [2, 3] and not wrapper.get_dbl_support()):
117+
pytest.skip()
118+
119+
shape = (5, 5)
120+
t_shape = (2, 2)
121+
dtype = dtypes.c_api_value_to_dtype(dtype_index)
122+
123+
result = wrapper.iota(shape, t_shape, dtype)
124+
125+
assert dtypes.c_api_value_to_dtype(wrapper.get_type(result)) == dtype

tests/test_lower.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
import arrayfire_wrapper.dtypes as dtypes
4+
import arrayfire_wrapper.lib as wrapper
5+
6+
7+
@pytest.mark.parametrize(
8+
"shape",
9+
[
10+
(3, 3),
11+
(3, 3, 3),
12+
(3, 3, 3, 3),
13+
],
14+
)
15+
def test_diag_is_unit(shape: tuple) -> None:
16+
"""Test if when is_unit_diag in lower returns an array with a unit diagonal"""
17+
dtype = dtypes.s64
18+
constant_array = wrapper.constant(3, shape, dtype)
19+
20+
lower_array = wrapper.lower(constant_array, True)
21+
diagonal = wrapper.diag_extract(lower_array, 0)
22+
diagonal_value = wrapper.get_scalar(diagonal, dtype)
23+
24+
assert diagonal_value == 1
25+
26+
27+
@pytest.mark.parametrize(
28+
"shape",
29+
[
30+
(3, 3),
31+
(3, 3, 3),
32+
(3, 3, 3, 3),
33+
],
34+
)
35+
def test_is_original(shape: tuple) -> None:
36+
"""Test if is_original keeps the diagonal the same as the original array"""
37+
dtype = dtypes.s64
38+
constant_array = wrapper.constant(3, shape, dtype)
39+
original_value = wrapper.get_scalar(constant_array, dtype)
40+
41+
lower_array = wrapper.lower(constant_array, False)
42+
diagonal = wrapper.diag_extract(lower_array, 0)
43+
diagonal_value = wrapper.get_scalar(diagonal, dtype)
44+
45+
assert original_value == diagonal_value

tests/test_pad.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import random
2+
3+
import numpy as np
4+
import pytest
5+
6+
import arrayfire_wrapper.dtypes as dtypes
7+
import arrayfire_wrapper.lib as wrapper
8+
9+
10+
@pytest.mark.parametrize(
11+
"original_shape",
12+
[
13+
(random.randint(1, 100),),
14+
(random.randint(1, 100), random.randint(1, 100)),
15+
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
16+
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
17+
],
18+
)
19+
def test_zero_padding(original_shape: tuple) -> None:
20+
"""Test if pad creates an array with no padding if no padding is given"""
21+
original_array = wrapper.constant(2, original_shape, dtypes.s64)
22+
padding = wrapper.Pad(0)
23+
24+
zero_shape = tuple(0 for _ in range(len(original_shape)))
25+
result = wrapper.pad(original_array, zero_shape, zero_shape, padding)
26+
27+
assert wrapper.get_dims(result)[0 : len(original_shape)] == original_shape # noqa: E203
28+
29+
30+
@pytest.mark.parametrize(
31+
"original_shape",
32+
[
33+
(random.randint(1, 100),),
34+
(random.randint(1, 100), random.randint(1, 100)),
35+
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
36+
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
37+
],
38+
)
39+
def test_negative_padding(original_shape: tuple) -> None:
40+
"""Test if pad can properly handle if negative padding is given"""
41+
with pytest.raises(RuntimeError):
42+
original_array = wrapper.constant(2, original_shape, dtypes.s64)
43+
padding = wrapper.Pad(0)
44+
45+
neg_shape = tuple(-1 for _ in range(len(original_shape)))
46+
result = wrapper.pad(original_array, neg_shape, neg_shape, padding)
47+
48+
assert wrapper.get_dims(result)[0 : len(original_shape)] == original_shape # noqa: E203
49+
50+
51+
@pytest.mark.parametrize(
52+
"original_shape",
53+
[
54+
(random.randint(1, 100),),
55+
(random.randint(1, 100), random.randint(1, 100)),
56+
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
57+
(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100), random.randint(1, 100)),
58+
],
59+
)
60+
def test_padding_shape(original_shape: tuple) -> None:
61+
"""Test if pad outputs the correct shape when a padding is adding to the original array"""
62+
original_array = wrapper.constant(2, original_shape, dtypes.s64)
63+
padding = wrapper.Pad(0)
64+
65+
beg_shape = tuple(random.randint(1, 10) for _ in range(len(original_shape)))
66+
end_shape = tuple(random.randint(1, 10) for _ in range(len(original_shape)))
67+
68+
result = wrapper.pad(original_array, beg_shape, end_shape, padding)
69+
new_shape = np.array(beg_shape) + np.array(end_shape) + np.array(original_shape)
70+
71+
assert wrapper.get_dims(result)[0 : len(original_shape)] == tuple(new_shape) # noqa: E203

0 commit comments

Comments
 (0)