Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
test_numpy_array.py
35
36import visp
37from visp.core import ArrayDouble2D, RotationMatrix, Matrix, HomogeneousMatrix, PoseVector, ColVector, RowVector
38
39import numpy as np
40import pytest
41
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
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
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
91 R = RotationMatrix()
93
95 T = HomogeneousMatrix()
97
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
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
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
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
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
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
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
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_constructor_interpreted_as_1d_vector()