test_numpy_array.py revision 12391:ceeca8b41e4b
1import pytest
2from pybind11_tests import numpy_array as m
3
4pytestmark = pytest.requires_numpy
5
6with pytest.suppress(ImportError):
7    import numpy as np
8
9
10@pytest.fixture(scope='function')
11def arr():
12    return np.array([[1, 2, 3], [4, 5, 6]], '=u2')
13
14
15def test_array_attributes():
16    a = np.array(0, 'f8')
17    assert m.ndim(a) == 0
18    assert all(m.shape(a) == [])
19    assert all(m.strides(a) == [])
20    with pytest.raises(IndexError) as excinfo:
21        m.shape(a, 0)
22    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
23    with pytest.raises(IndexError) as excinfo:
24        m.strides(a, 0)
25    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
26    assert m.writeable(a)
27    assert m.size(a) == 1
28    assert m.itemsize(a) == 8
29    assert m.nbytes(a) == 8
30    assert m.owndata(a)
31
32    a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
33    a.flags.writeable = False
34    assert m.ndim(a) == 2
35    assert all(m.shape(a) == [2, 3])
36    assert m.shape(a, 0) == 2
37    assert m.shape(a, 1) == 3
38    assert all(m.strides(a) == [6, 2])
39    assert m.strides(a, 0) == 6
40    assert m.strides(a, 1) == 2
41    with pytest.raises(IndexError) as excinfo:
42        m.shape(a, 2)
43    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
44    with pytest.raises(IndexError) as excinfo:
45        m.strides(a, 2)
46    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
47    assert not m.writeable(a)
48    assert m.size(a) == 6
49    assert m.itemsize(a) == 2
50    assert m.nbytes(a) == 12
51    assert not m.owndata(a)
52
53
54@pytest.mark.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
55def test_index_offset(arr, args, ret):
56    assert m.index_at(arr, *args) == ret
57    assert m.index_at_t(arr, *args) == ret
58    assert m.offset_at(arr, *args) == ret * arr.dtype.itemsize
59    assert m.offset_at_t(arr, *args) == ret * arr.dtype.itemsize
60
61
62def test_dim_check_fail(arr):
63    for func in (m.index_at, m.index_at_t, m.offset_at, m.offset_at_t, m.data, m.data_t,
64                 m.mutate_data, m.mutate_data_t):
65        with pytest.raises(IndexError) as excinfo:
66            func(arr, 1, 2, 3)
67        assert str(excinfo.value) == 'too many indices for an array: 3 (ndim = 2)'
68
69
70@pytest.mark.parametrize('args, ret',
71                         [([], [1, 2, 3, 4, 5, 6]),
72                          ([1], [4, 5, 6]),
73                          ([0, 1], [2, 3, 4, 5, 6]),
74                          ([1, 2], [6])])
75def test_data(arr, args, ret):
76    from sys import byteorder
77    assert all(m.data_t(arr, *args) == ret)
78    assert all(m.data(arr, *args)[(0 if byteorder == 'little' else 1)::2] == ret)
79    assert all(m.data(arr, *args)[(1 if byteorder == 'little' else 0)::2] == 0)
80
81
82@pytest.mark.parametrize('dim', [0, 1, 3])
83def test_at_fail(arr, dim):
84    for func in m.at_t, m.mutate_at_t:
85        with pytest.raises(IndexError) as excinfo:
86            func(arr, *([0] * dim))
87        assert str(excinfo.value) == 'index dimension mismatch: {} (ndim = 2)'.format(dim)
88
89
90def test_at(arr):
91    assert m.at_t(arr, 0, 2) == 3
92    assert m.at_t(arr, 1, 0) == 4
93
94    assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
95    assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
96
97
98def test_mutate_readonly(arr):
99    arr.flags.writeable = False
100    for func, args in (m.mutate_data, ()), (m.mutate_data_t, ()), (m.mutate_at_t, (0, 0)):
101        with pytest.raises(ValueError) as excinfo:
102            func(arr, *args)
103        assert str(excinfo.value) == 'array is not writeable'
104
105
106def test_mutate_data(arr):
107    assert all(m.mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
108    assert all(m.mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
109    assert all(m.mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
110    assert all(m.mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
111    assert all(m.mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
112
113    assert all(m.mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
114    assert all(m.mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
115    assert all(m.mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
116    assert all(m.mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
117    assert all(m.mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
118
119
120def test_bounds_check(arr):
121    for func in (m.index_at, m.index_at_t, m.data, m.data_t,
122                 m.mutate_data, m.mutate_data_t, m.at_t, m.mutate_at_t):
123        with pytest.raises(IndexError) as excinfo:
124            func(arr, 2, 0)
125        assert str(excinfo.value) == 'index 2 is out of bounds for axis 0 with size 2'
126        with pytest.raises(IndexError) as excinfo:
127            func(arr, 0, 4)
128        assert str(excinfo.value) == 'index 4 is out of bounds for axis 1 with size 3'
129
130
131def test_make_c_f_array():
132    assert m.make_c_array().flags.c_contiguous
133    assert not m.make_c_array().flags.f_contiguous
134    assert m.make_f_array().flags.f_contiguous
135    assert not m.make_f_array().flags.c_contiguous
136
137
138def test_wrap():
139    def assert_references(a, b, base=None):
140        if base is None:
141            base = a
142        assert a is not b
143        assert a.__array_interface__['data'][0] == b.__array_interface__['data'][0]
144        assert a.shape == b.shape
145        assert a.strides == b.strides
146        assert a.flags.c_contiguous == b.flags.c_contiguous
147        assert a.flags.f_contiguous == b.flags.f_contiguous
148        assert a.flags.writeable == b.flags.writeable
149        assert a.flags.aligned == b.flags.aligned
150        assert a.flags.updateifcopy == b.flags.updateifcopy
151        assert np.all(a == b)
152        assert not b.flags.owndata
153        assert b.base is base
154        if a.flags.writeable and a.ndim == 2:
155            a[0, 0] = 1234
156            assert b[0, 0] == 1234
157
158    a1 = np.array([1, 2], dtype=np.int16)
159    assert a1.flags.owndata and a1.base is None
160    a2 = m.wrap(a1)
161    assert_references(a1, a2)
162
163    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='F')
164    assert a1.flags.owndata and a1.base is None
165    a2 = m.wrap(a1)
166    assert_references(a1, a2)
167
168    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='C')
169    a1.flags.writeable = False
170    a2 = m.wrap(a1)
171    assert_references(a1, a2)
172
173    a1 = np.random.random((4, 4, 4))
174    a2 = m.wrap(a1)
175    assert_references(a1, a2)
176
177    a1t = a1.transpose()
178    a2 = m.wrap(a1t)
179    assert_references(a1t, a2, a1)
180
181    a1d = a1.diagonal()
182    a2 = m.wrap(a1d)
183    assert_references(a1d, a2, a1)
184
185    a1m = a1[::-1, ::-1, ::-1]
186    a2 = m.wrap(a1m)
187    assert_references(a1m, a2, a1)
188
189
190def test_numpy_view(capture):
191    with capture:
192        ac = m.ArrayClass()
193        ac_view_1 = ac.numpy_view()
194        ac_view_2 = ac.numpy_view()
195        assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
196        del ac
197        pytest.gc_collect()
198    assert capture == """
199        ArrayClass()
200        ArrayClass::numpy_view()
201        ArrayClass::numpy_view()
202    """
203    ac_view_1[0] = 4
204    ac_view_1[1] = 3
205    assert ac_view_2[0] == 4
206    assert ac_view_2[1] == 3
207    with capture:
208        del ac_view_1
209        del ac_view_2
210        pytest.gc_collect()
211        pytest.gc_collect()
212    assert capture == """
213        ~ArrayClass()
214    """
215
216
217@pytest.unsupported_on_pypy
218def test_cast_numpy_int64_to_uint64():
219    m.function_taking_uint64(123)
220    m.function_taking_uint64(np.uint64(123))
221
222
223def test_isinstance():
224    assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array")
225    assert m.isinstance_typed(np.array([1.0, 2.0, 3.0]))
226
227
228def test_constructors():
229    defaults = m.default_constructors()
230    for a in defaults.values():
231        assert a.size == 0
232    assert defaults["array"].dtype == np.array([]).dtype
233    assert defaults["array_t<int32>"].dtype == np.int32
234    assert defaults["array_t<double>"].dtype == np.float64
235
236    results = m.converting_constructors([1, 2, 3])
237    for a in results.values():
238        np.testing.assert_array_equal(a, [1, 2, 3])
239    assert results["array"].dtype == np.int_
240    assert results["array_t<int32>"].dtype == np.int32
241    assert results["array_t<double>"].dtype == np.float64
242
243
244def test_overload_resolution(msg):
245    # Exact overload matches:
246    assert m.overloaded(np.array([1], dtype='float64')) == 'double'
247    assert m.overloaded(np.array([1], dtype='float32')) == 'float'
248    assert m.overloaded(np.array([1], dtype='ushort')) == 'unsigned short'
249    assert m.overloaded(np.array([1], dtype='intc')) == 'int'
250    assert m.overloaded(np.array([1], dtype='longlong')) == 'long long'
251    assert m.overloaded(np.array([1], dtype='complex')) == 'double complex'
252    assert m.overloaded(np.array([1], dtype='csingle')) == 'float complex'
253
254    # No exact match, should call first convertible version:
255    assert m.overloaded(np.array([1], dtype='uint8')) == 'double'
256
257    with pytest.raises(TypeError) as excinfo:
258        m.overloaded("not an array")
259    assert msg(excinfo.value) == """
260        overloaded(): incompatible function arguments. The following argument types are supported:
261            1. (arg0: numpy.ndarray[float64]) -> str
262            2. (arg0: numpy.ndarray[float32]) -> str
263            3. (arg0: numpy.ndarray[int32]) -> str
264            4. (arg0: numpy.ndarray[uint16]) -> str
265            5. (arg0: numpy.ndarray[int64]) -> str
266            6. (arg0: numpy.ndarray[complex128]) -> str
267            7. (arg0: numpy.ndarray[complex64]) -> str
268
269        Invoked with: 'not an array'
270    """
271
272    assert m.overloaded2(np.array([1], dtype='float64')) == 'double'
273    assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
274    assert m.overloaded2(np.array([1], dtype='complex64')) == 'float complex'
275    assert m.overloaded2(np.array([1], dtype='complex128')) == 'double complex'
276    assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
277
278    assert m.overloaded3(np.array([1], dtype='float64')) == 'double'
279    assert m.overloaded3(np.array([1], dtype='intc')) == 'int'
280    expected_exc = """
281        overloaded3(): incompatible function arguments. The following argument types are supported:
282            1. (arg0: numpy.ndarray[int32]) -> str
283            2. (arg0: numpy.ndarray[float64]) -> str
284
285        Invoked with:"""
286
287    with pytest.raises(TypeError) as excinfo:
288        m.overloaded3(np.array([1], dtype='uintc'))
289    assert msg(excinfo.value) == expected_exc + " array([1], dtype=uint32)"
290    with pytest.raises(TypeError) as excinfo:
291        m.overloaded3(np.array([1], dtype='float32'))
292    assert msg(excinfo.value) == expected_exc + " array([ 1.], dtype=float32)"
293    with pytest.raises(TypeError) as excinfo:
294        m.overloaded3(np.array([1], dtype='complex'))
295    assert msg(excinfo.value) == expected_exc + " array([ 1.+0.j])"
296
297    # Exact matches:
298    assert m.overloaded4(np.array([1], dtype='double')) == 'double'
299    assert m.overloaded4(np.array([1], dtype='longlong')) == 'long long'
300    # Non-exact matches requiring conversion.  Since float to integer isn't a
301    # save conversion, it should go to the double overload, but short can go to
302    # either (and so should end up on the first-registered, the long long).
303    assert m.overloaded4(np.array([1], dtype='float32')) == 'double'
304    assert m.overloaded4(np.array([1], dtype='short')) == 'long long'
305
306    assert m.overloaded5(np.array([1], dtype='double')) == 'double'
307    assert m.overloaded5(np.array([1], dtype='uintc')) == 'unsigned int'
308    assert m.overloaded5(np.array([1], dtype='float32')) == 'unsigned int'
309
310
311def test_greedy_string_overload():
312    """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
313
314    assert m.issue685("abc") == "string"
315    assert m.issue685(np.array([97, 98, 99], dtype='b')) == "array"
316    assert m.issue685(123) == "other"
317
318
319def test_array_unchecked_fixed_dims(msg):
320    z1 = np.array([[1, 2], [3, 4]], dtype='float64')
321    m.proxy_add2(z1, 10)
322    assert np.all(z1 == [[11, 12], [13, 14]])
323
324    with pytest.raises(ValueError) as excinfo:
325        m.proxy_add2(np.array([1., 2, 3]), 5.0)
326    assert msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2"
327
328    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
329    assert np.all(m.proxy_init3(3.0) == expect_c)
330    expect_f = np.transpose(expect_c)
331    assert np.all(m.proxy_init3F(3.0) == expect_f)
332
333    assert m.proxy_squared_L2_norm(np.array(range(6))) == 55
334    assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
335
336    assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
337    assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
338
339
340def test_array_unchecked_dyn_dims(msg):
341    z1 = np.array([[1, 2], [3, 4]], dtype='float64')
342    m.proxy_add2_dyn(z1, 10)
343    assert np.all(z1 == [[11, 12], [13, 14]])
344
345    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
346    assert np.all(m.proxy_init3_dyn(3.0) == expect_c)
347
348    assert m.proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
349    assert m.proxy_auxiliaries2_dyn(z1) == m.array_auxiliaries2(z1)
350
351
352def test_array_failure():
353    with pytest.raises(ValueError) as excinfo:
354        m.array_fail_test()
355    assert str(excinfo.value) == 'cannot create a pybind11::array from a nullptr'
356
357    with pytest.raises(ValueError) as excinfo:
358        m.array_t_fail_test()
359    assert str(excinfo.value) == 'cannot create a pybind11::array_t from a nullptr'
360
361    with pytest.raises(ValueError) as excinfo:
362        m.array_fail_test_negative_size()
363    assert str(excinfo.value) == 'negative dimensions are not allowed'
364
365
366def test_initializer_list():
367    assert m.array_initializer_list1().shape == (1,)
368    assert m.array_initializer_list2().shape == (1, 2)
369    assert m.array_initializer_list3().shape == (1, 2, 3)
370    assert m.array_initializer_list4().shape == (1, 2, 3, 4)
371
372
373def test_array_resize(msg):
374    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float64')
375    m.array_reshape2(a)
376    assert(a.size == 9)
377    assert(np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
378
379    # total size change should succced with refcheck off
380    m.array_resize3(a, 4, False)
381    assert(a.size == 64)
382    # ... and fail with refcheck on
383    try:
384        m.array_resize3(a, 3, True)
385    except ValueError as e:
386        assert(str(e).startswith("cannot resize an array"))
387    # transposed array doesn't own data
388    b = a.transpose()
389    try:
390        m.array_resize3(b, 3, False)
391    except ValueError as e:
392        assert(str(e).startswith("cannot resize this array: it does not own its data"))
393    # ... but reshape should be fine
394    m.array_reshape2(b)
395    assert(b.shape == (8, 8))
396
397
398@pytest.unsupported_on_pypy
399def test_array_create_and_resize(msg):
400    a = m.create_and_resize(2)
401    assert(a.size == 4)
402    assert(np.all(a == 42.))
403