Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
test_numpy_array.py
1
35
36
import
visp
37
from
visp.core
import
ArrayDouble2D, RotationMatrix, Matrix, HomogeneousMatrix, PoseVector, ColVector, RowVector
38
39
import
numpy
as
np
40
import
pytest
41
42
def
test_np_array_modifies_vp_array
():
43
# Test that numpy is a view of array and that writing to numpy array modifies vpArray
44
array = ArrayDouble2D(5, 5, 1.0)
45
assert
array.getRows() == array.getCols() == 5
46
array_np = np.array(array, copy=
False
)
47
assert
array_np.shape == (5, 5)
48
assert
np.all(array_np == 1.0)
49
array_np[0:2, 0:2] = 2
50
assert
array.getMinValue() == 1
and
array.getMaxValue() == 2
51
52
def
test_visp_view_of_np_array
():
53
a = np.zeros((5, 5))
54
with
pytest.raises(RuntimeError):
55
ColVector.view(a)
56
with
pytest.raises(RuntimeError):
57
RowVector.view(a)
58
m = Matrix.view(a)
59
m[0, 0] = 1
60
assert
a[0, 0] == 1
61
assert
m.getRows() == a.shape[0]
and
m.getCols() == a.shape[1]
62
a = np.zeros(10)
63
with
pytest.raises(RuntimeError):
64
Matrix.view(a)
65
66
v = ColVector.view(a)
67
v[0] = 1
68
assert
a[0] == 1
69
assert
v.getRows() == a.shape[0]
70
71
a = np.zeros(10)
72
v = RowVector.view(a)
73
v[0] = 1
74
assert
a[0] == 1
75
assert
v.getCols() == a.shape[0]
76
77
def
fn_test_not_writable_2d
(R):
78
R_np = np.array(R, copy=
False
)
79
with
pytest.raises(ValueError):
80
R_np[0, 0] = 1
81
with
pytest.raises(ValueError):
82
R.numpy()[:1] = 0
83
with
pytest.raises(ValueError):
84
row = R[0]
85
row[0] = 1
86
with
pytest.raises(ValueError):
87
sub = R[:2, :2]
88
sub[0, :] = 1
89
90
def
test_rotation_matrix_not_writable
():
91
R = RotationMatrix()
92
fn_test_not_writable_2d
(R)
93
94
def
test_homogeneous_matrix_not_writable
():
95
T = HomogeneousMatrix()
96
fn_test_not_writable_2d
(T)
97
98
def
test_numpy_constructor
():
99
n_invalid = np.array([1, 2, 3])
100
with
pytest.raises(RuntimeError):
101
a = ArrayDouble2D(n_invalid)
102
n_valid = np.array([[1, 2, 3], [4, 5, 6]])
103
a = ArrayDouble2D(n_valid)
104
assert
np.all(np.equal(a.numpy(), n_valid))
105
106
def
test_numpy_constructor_interpreted_as_1d_vector
():
107
n_1d = np.array([1, 2, 3])
108
with
pytest.raises(RuntimeError):
109
a = ArrayDouble2D(n_1d)
# R = 0, c = 0
110
ar = ArrayDouble2D(n_1d, r=len(n_1d))
111
ac = ArrayDouble2D(n_1d, c=len(n_1d))
112
113
def
test_numpy_conversion_and_back
():
114
a = ArrayDouble2D(10, 10, 2.0)
115
a_np = a.numpy().copy()
116
a2 = ArrayDouble2D(a_np)
117
mat = Matrix(a_np)
118
119
for
i
in
range(a.getRows()):
120
for
j
in
range(a.getCols()):
121
assert
a[i, j] == a_np[i, j]
122
assert
a[i, j] == a2[i, j]
123
assert
mat[i, j] == a[i, j]
124
125
def
test_indexing_array2D
():
126
a_np = np.asarray([[i
for
_
in
range(10)]
for
i
in
range(10)])
127
a = ArrayDouble2D(a_np)
128
col = list(range(10))
129
for
i
in
range(a.getRows()):
130
assert
np.all(a[i] == float(i))
131
assert
np.all(a[-i - 1] == float(a.getRows() - i - 1))
132
assert
np.all(a[:, i] == col)
133
assert
np.all(a[:, -i - 1] == col)
134
135
def
test_index_row_not_copy
():
136
a = ArrayDouble2D(5, 5, 1.0)
137
first_row_view = a[0]
138
first_row_view[0] = 0.0
139
assert
a[0, 0] == 0.0
140
141
def
test_index_slice_not_copy
():
142
a = ArrayDouble2D(5, 5, 1.0)
143
sub_matrix = a[1:3]
144
sub_matrix[0] = 0.0
145
for
i
in
range(a.getCols()):
146
assert
a[1, i] == 0.0
147
148
def
test_index_tuple_not_copy
():
149
a = ArrayDouble2D(5, 5, 1.0)
150
col = a[:, -1]
151
col[0] = 0.0
152
assert
a[0, -1] == 0.0
153
sub = a[0:2, 0:2]
154
sub[:, :] = 0.0
155
for
i
in
range(2):
156
for
j
in
range(2):
157
assert
a[i, j] == 0.0
158
159
def
test_setitem_2D_array
():
160
h,w = 50, 50
161
a = ArrayDouble2D(h, w, 5)
162
163
# 2D indexing (basic)
164
a[0, 0] = 5
165
assert
a[0, 0] == 5
166
a[0, 0] = 20
167
assert
a[0, 0] == 20
168
169
# Replace a row
170
a[1] = 20
171
for
i
in
range(a.getCols()):
172
assert
a[1, i] == 20
173
174
175
# Replace a row
176
a[:] = 20
177
for
i
in
range(a.getRows()):
178
for
j
in
range(a.getCols()):
179
assert
a[i, j] == 20
180
181
# Replace rows with a slice
182
a[:] = 5
183
a[::2] = 20
184
for
i
in
range(a.getRows()):
185
v = 5
if
i % 2 == 1
else
20
186
for
j
in
range(a.getCols()):
187
assert
a[i, j] == v
188
189
a[:] = 5
190
a[2:-2:2] = 20
191
for
i
in
range(a.getRows()):
192
v = 5
if
i % 2 == 1
or
i >= a.getRows() - 2
or
i < 2
else
20
193
for
j
in
range(a.getCols()):
194
assert
a[i, j] == v
195
196
a[:, :] = 5
197
for
i
in
range(a.getRows()):
198
for
j
in
range(a.getCols()):
199
assert
a[i, j] == 5
200
201
# Indexing with two slices
202
a[2:-2:2, 3:-3] = 20
203
for
i
in
range(a.getRows()):
204
is_v = i >= 2
and
i % 2 == 0
and
i < a.getRows() - 2
205
for
j
in
range(a.getCols()):
206
is_vj = is_v
and
j >= 3
and
j < a.getCols() - 3
207
v = 20
if
is_vj
else
5
208
assert
a[i, j] == v
209
210
211
212
# Negative step not supported
213
with
pytest.raises(RuntimeError):
214
a[::-1] = 20
215
with
pytest.raises(RuntimeError):
216
a[:, ::-1] = 20
217
218
# Wrong start and end values
219
with
pytest.raises(RuntimeError):
220
a[2:1] = 20
221
with
pytest.raises(RuntimeError):
222
a[:, 3:2] = 20
223
224
225
a = ArrayDouble2D(h, w, 0.0)
226
single_row = np.ones((w, ), dtype=np.double) * 20
227
228
a[2] = single_row
229
assert
not
np.any(np.equal(a.numpy()[list(set(range(h)) - {2})], single_row))
230
assert
np.all(np.equal(a.numpy()[2], single_row))
231
232
a[:] = 0
233
a[1:-2] = single_row
234
assert
np.all(np.equal(a.numpy()[list(set(range(h)) - {0, h - 2, h - 1})], single_row))
235
assert
np.all(np.equal(a.numpy()[[0, h - 2, h - 1]], 0))
236
237
multi_rows = np.asarray([[i * w + j
for
j
in
range(w)]
for
i
in
range(h - 5)])
238
239
a[:-5] = multi_rows
240
assert
np.all(np.equal(a.numpy()[:-5], multi_rows))
test_numpy_array.test_index_slice_not_copy
test_index_slice_not_copy()
Definition
test_numpy_array.py:141
test_numpy_array.test_index_tuple_not_copy
test_index_tuple_not_copy()
Definition
test_numpy_array.py:148
test_numpy_array.test_numpy_constructor
test_numpy_constructor()
Definition
test_numpy_array.py:98
test_numpy_array.test_rotation_matrix_not_writable
test_rotation_matrix_not_writable()
Definition
test_numpy_array.py:90
test_numpy_array.test_index_row_not_copy
test_index_row_not_copy()
Definition
test_numpy_array.py:135
test_numpy_array.test_homogeneous_matrix_not_writable
test_homogeneous_matrix_not_writable()
Definition
test_numpy_array.py:94
test_numpy_array.test_indexing_array2D
test_indexing_array2D()
Definition
test_numpy_array.py:125
test_numpy_array.test_setitem_2D_array
test_setitem_2D_array()
Definition
test_numpy_array.py:159
test_numpy_array.test_numpy_constructor_interpreted_as_1d_vector
test_numpy_constructor_interpreted_as_1d_vector()
Definition
test_numpy_array.py:106
test_numpy_array.test_numpy_conversion_and_back
test_numpy_conversion_and_back()
Definition
test_numpy_array.py:113
test_numpy_array.test_visp_view_of_np_array
test_visp_view_of_np_array()
Definition
test_numpy_array.py:52
test_numpy_array.test_np_array_modifies_vp_array
test_np_array_modifies_vp_array()
Definition
test_numpy_array.py:42
test_numpy_array.fn_test_not_writable_2d
fn_test_not_writable_2d(R)
Definition
test_numpy_array.py:77
modules
python
test
test_numpy_array.py
Generated by
1.17.0