forked from docarray/docarray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ndarray.py
More file actions
76 lines (67 loc) · 2.1 KB
/
test_ndarray.py
File metadata and controls
76 lines (67 loc) · 2.1 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import paddle
import pytest
import tensorflow as tf
import torch
from scipy.sparse import csr_matrix, coo_matrix, bsr_matrix, csc_matrix, issparse
from docarray.math.ndarray import get_array_rows, check_arraylike_equality
from docarray.proto.docarray_pb2 import NdArrayProto
from docarray.proto.io import flush_ndarray, read_ndarray
@pytest.mark.parametrize(
'data, expected_result',
[
([1, 2, 3], (1, 1)),
([[1, 2, 3]], (1, 2)),
([[1, 2], [3, 4]], (2, 2)),
([[1, 2], [3, 4], [5, 6], [7, 8]], (4, 2)),
],
)
@pytest.mark.parametrize(
'arraytype',
[
list,
torch.tensor,
tf.constant,
paddle.to_tensor,
torch.tensor,
csr_matrix,
bsr_matrix,
coo_matrix,
csc_matrix,
],
)
@pytest.mark.parametrize('ndarray_type', ['list', 'numpy'])
def test_get_array_rows(data, expected_result, arraytype, ndarray_type):
data_array = arraytype(data)
num_rows, ndim = get_array_rows(data_array)
if issparse(data_array):
# there is no ndim==1 in scipy sparse matrices therefore don't check
assert expected_result[0] == num_rows
else:
assert expected_result == (num_rows, ndim)
na_proto = NdArrayProto()
flush_ndarray(na_proto, value=data_array, ndarray_type=ndarray_type)
r_data_array = read_ndarray(na_proto)
if ndarray_type == 'list':
assert isinstance(r_data_array, list)
elif ndarray_type == 'numpy':
assert isinstance(r_data_array, np.ndarray)
def get_ndarrays():
a = np.random.random([10, 3])
a[a > 0.5] = 0
return [
a,
a.tolist(),
torch.tensor(a),
tf.constant(a),
paddle.to_tensor(a),
torch.tensor(a).to_sparse(),
csr_matrix(a),
bsr_matrix(a),
coo_matrix(a),
csc_matrix(a),
]
@pytest.mark.parametrize('ndarray_val', get_ndarrays())
def test_check_arraylike_equality(ndarray_val):
assert check_arraylike_equality(ndarray_val, ndarray_val) == True
assert check_arraylike_equality(ndarray_val, ndarray_val + ndarray_val) == False