test_numpy_array.py 14.7 KB
Newer Older
1
import pytest
2
from pybind11_tests import numpy_array as m
3

4
5
pytestmark = pytest.requires_numpy

6
7
8
9
with pytest.suppress(ImportError):
    import numpy as np


10
11
@pytest.fixture(scope='function')
def arr():
12
    return np.array([[1, 2, 3], [4, 5, 6]], '=u2')
13
14


15
16
def test_array_attributes():
    a = np.array(0, 'f8')
17
18
19
    assert m.ndim(a) == 0
    assert all(m.shape(a) == [])
    assert all(m.strides(a) == [])
20
    with pytest.raises(IndexError) as excinfo:
21
        m.shape(a, 0)
22
23
    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
    with pytest.raises(IndexError) as excinfo:
24
        m.strides(a, 0)
25
    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
26
27
28
29
30
    assert m.writeable(a)
    assert m.size(a) == 1
    assert m.itemsize(a) == 8
    assert m.nbytes(a) == 8
    assert m.owndata(a)
31
32
33

    a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
    a.flags.writeable = False
34
35
36
37
38
39
40
    assert m.ndim(a) == 2
    assert all(m.shape(a) == [2, 3])
    assert m.shape(a, 0) == 2
    assert m.shape(a, 1) == 3
    assert all(m.strides(a) == [6, 2])
    assert m.strides(a, 0) == 6
    assert m.strides(a, 1) == 2
41
    with pytest.raises(IndexError) as excinfo:
42
        m.shape(a, 2)
43
44
    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
    with pytest.raises(IndexError) as excinfo:
45
        m.strides(a, 2)
46
    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
47
48
49
50
51
    assert not m.writeable(a)
    assert m.size(a) == 6
    assert m.itemsize(a) == 2
    assert m.nbytes(a) == 12
    assert not m.owndata(a)
52
53
54
55


@pytest.mark.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
def test_index_offset(arr, args, ret):
56
57
58
59
    assert m.index_at(arr, *args) == ret
    assert m.index_at_t(arr, *args) == ret
    assert m.offset_at(arr, *args) == ret * arr.dtype.itemsize
    assert m.offset_at_t(arr, *args) == ret * arr.dtype.itemsize
60
61
62


def test_dim_check_fail(arr):
63
64
    for func in (m.index_at, m.index_at_t, m.offset_at, m.offset_at_t, m.data, m.data_t,
                 m.mutate_data, m.mutate_data_t):
65
66
67
68
69
70
71
72
73
74
75
        with pytest.raises(IndexError) as excinfo:
            func(arr, 1, 2, 3)
        assert str(excinfo.value) == 'too many indices for an array: 3 (ndim = 2)'


@pytest.mark.parametrize('args, ret',
                         [([], [1, 2, 3, 4, 5, 6]),
                          ([1], [4, 5, 6]),
                          ([0, 1], [2, 3, 4, 5, 6]),
                          ([1, 2], [6])])
def test_data(arr, args, ret):
76
    from sys import byteorder
77
78
79
    assert all(m.data_t(arr, *args) == ret)
    assert all(m.data(arr, *args)[(0 if byteorder == 'little' else 1)::2] == ret)
    assert all(m.data(arr, *args)[(1 if byteorder == 'little' else 0)::2] == 0)
80
81
82
83


@pytest.mark.parametrize('dim', [0, 1, 3])
def test_at_fail(arr, dim):
84
    for func in m.at_t, m.mutate_at_t:
85
86
87
88
89
90
        with pytest.raises(IndexError) as excinfo:
            func(arr, *([0] * dim))
        assert str(excinfo.value) == 'index dimension mismatch: {} (ndim = 2)'.format(dim)


def test_at(arr):
91
92
93
94
95
    assert m.at_t(arr, 0, 2) == 3
    assert m.at_t(arr, 1, 0) == 4

    assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
    assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
96
97


98
99
100
101
102
103
def test_mutate_readonly(arr):
    arr.flags.writeable = False
    for func, args in (m.mutate_data, ()), (m.mutate_data_t, ()), (m.mutate_at_t, (0, 0)):
        with pytest.raises(ValueError) as excinfo:
            func(arr, *args)
        assert str(excinfo.value) == 'array is not writeable'
104
105
106


def test_mutate_data(arr):
107
108
109
110
111
    assert all(m.mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
    assert all(m.mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
    assert all(m.mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
    assert all(m.mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
    assert all(m.mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
112

113
114
115
116
117
    assert all(m.mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
    assert all(m.mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
    assert all(m.mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
    assert all(m.mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
    assert all(m.mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
118
119
120


def test_bounds_check(arr):
121
122
    for func in (m.index_at, m.index_at_t, m.data, m.data_t,
                 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
126
        assert str(excinfo.value) == 'index 2 is out of bounds for axis 0 with size 2'
        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

131
def test_make_c_f_array():
132
133
134
135
    assert m.make_c_array().flags.c_contiguous
    assert not m.make_c_array().flags.f_contiguous
    assert m.make_f_array().flags.f_contiguous
    assert not m.make_f_array().flags.c_contiguous
136
137
138


def test_wrap():
Jason Rhinelander's avatar
Jason Rhinelander committed
139
    def assert_references(a, b, base=None):
140
        from distutils.version import LooseVersion
Jason Rhinelander's avatar
Jason Rhinelander committed
141
142
        if base is None:
            base = a
143
144
145
146
147
148
149
150
        assert a is not b
        assert a.__array_interface__['data'][0] == b.__array_interface__['data'][0]
        assert a.shape == b.shape
        assert a.strides == b.strides
        assert a.flags.c_contiguous == b.flags.c_contiguous
        assert a.flags.f_contiguous == b.flags.f_contiguous
        assert a.flags.writeable == b.flags.writeable
        assert a.flags.aligned == b.flags.aligned
151
152
153
154
        if LooseVersion(np.__version__) >= LooseVersion("1.14.0"):
            assert a.flags.writebackifcopy == b.flags.writebackifcopy
        else:
            assert a.flags.updateifcopy == b.flags.updateifcopy
155
156
        assert np.all(a == b)
        assert not b.flags.owndata
Jason Rhinelander's avatar
Jason Rhinelander committed
157
        assert b.base is base
158
159
160
161
162
163
        if a.flags.writeable and a.ndim == 2:
            a[0, 0] = 1234
            assert b[0, 0] == 1234

    a1 = np.array([1, 2], dtype=np.int16)
    assert a1.flags.owndata and a1.base is None
164
    a2 = m.wrap(a1)
165
166
167
168
    assert_references(a1, a2)

    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='F')
    assert a1.flags.owndata and a1.base is None
169
    a2 = m.wrap(a1)
170
171
172
173
    assert_references(a1, a2)

    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='C')
    a1.flags.writeable = False
174
    a2 = m.wrap(a1)
175
176
177
    assert_references(a1, a2)

    a1 = np.random.random((4, 4, 4))
178
    a2 = m.wrap(a1)
179
180
    assert_references(a1, a2)

Jason Rhinelander's avatar
Jason Rhinelander committed
181
    a1t = a1.transpose()
182
    a2 = m.wrap(a1t)
Jason Rhinelander's avatar
Jason Rhinelander committed
183
    assert_references(a1t, a2, a1)
184

Jason Rhinelander's avatar
Jason Rhinelander committed
185
    a1d = a1.diagonal()
186
    a2 = m.wrap(a1d)
Jason Rhinelander's avatar
Jason Rhinelander committed
187
    assert_references(a1d, a2, a1)
188

189
    a1m = a1[::-1, ::-1, ::-1]
190
    a2 = m.wrap(a1m)
191
192
    assert_references(a1m, a2, a1)

193
194
195

def test_numpy_view(capture):
    with capture:
196
        ac = m.ArrayClass()
197
198
199
200
        ac_view_1 = ac.numpy_view()
        ac_view_2 = ac.numpy_view()
        assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
        del ac
Wenzel Jakob's avatar
Wenzel Jakob committed
201
        pytest.gc_collect()
202
203
204
205
206
207
208
209
210
211
212
213
    assert capture == """
        ArrayClass()
        ArrayClass::numpy_view()
        ArrayClass::numpy_view()
    """
    ac_view_1[0] = 4
    ac_view_1[1] = 3
    assert ac_view_2[0] == 4
    assert ac_view_2[1] == 3
    with capture:
        del ac_view_1
        del ac_view_2
Wenzel Jakob's avatar
Wenzel Jakob committed
214
215
        pytest.gc_collect()
        pytest.gc_collect()
216
217
218
    assert capture == """
        ~ArrayClass()
    """
219
220


Wenzel Jakob's avatar
Wenzel Jakob committed
221
@pytest.unsupported_on_pypy
222
def test_cast_numpy_int64_to_uint64():
223
224
    m.function_taking_uint64(123)
    m.function_taking_uint64(np.uint64(123))
225
226
227


def test_isinstance():
228
229
    assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array")
    assert m.isinstance_typed(np.array([1.0, 2.0, 3.0]))
230
231
232


def test_constructors():
233
    defaults = m.default_constructors()
234
235
236
237
238
239
    for a in defaults.values():
        assert a.size == 0
    assert defaults["array"].dtype == np.array([]).dtype
    assert defaults["array_t<int32>"].dtype == np.int32
    assert defaults["array_t<double>"].dtype == np.float64

240
    results = m.converting_constructors([1, 2, 3])
241
242
243
244
245
    for a in results.values():
        np.testing.assert_array_equal(a, [1, 2, 3])
    assert results["array"].dtype == np.int_
    assert results["array_t<int32>"].dtype == np.int32
    assert results["array_t<double>"].dtype == np.float64
246
247


248
249
def test_overload_resolution(msg):
    # Exact overload matches:
250
251
252
253
254
255
256
    assert m.overloaded(np.array([1], dtype='float64')) == 'double'
    assert m.overloaded(np.array([1], dtype='float32')) == 'float'
    assert m.overloaded(np.array([1], dtype='ushort')) == 'unsigned short'
    assert m.overloaded(np.array([1], dtype='intc')) == 'int'
    assert m.overloaded(np.array([1], dtype='longlong')) == 'long long'
    assert m.overloaded(np.array([1], dtype='complex')) == 'double complex'
    assert m.overloaded(np.array([1], dtype='csingle')) == 'float complex'
257
258

    # No exact match, should call first convertible version:
259
    assert m.overloaded(np.array([1], dtype='uint8')) == 'double'
260

261
    with pytest.raises(TypeError) as excinfo:
262
        m.overloaded("not an array")
263
264
265
266
267
268
269
270
271
272
273
274
275
    assert msg(excinfo.value) == """
        overloaded(): incompatible function arguments. The following argument types are supported:
            1. (arg0: numpy.ndarray[float64]) -> str
            2. (arg0: numpy.ndarray[float32]) -> str
            3. (arg0: numpy.ndarray[int32]) -> str
            4. (arg0: numpy.ndarray[uint16]) -> str
            5. (arg0: numpy.ndarray[int64]) -> str
            6. (arg0: numpy.ndarray[complex128]) -> str
            7. (arg0: numpy.ndarray[complex64]) -> str

        Invoked with: 'not an array'
    """

276
277
278
279
280
    assert m.overloaded2(np.array([1], dtype='float64')) == 'double'
    assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
    assert m.overloaded2(np.array([1], dtype='complex64')) == 'float complex'
    assert m.overloaded2(np.array([1], dtype='complex128')) == 'double complex'
    assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
281

282
283
    assert m.overloaded3(np.array([1], dtype='float64')) == 'double'
    assert m.overloaded3(np.array([1], dtype='intc')) == 'int'
284
285
    expected_exc = """
        overloaded3(): incompatible function arguments. The following argument types are supported:
286
287
            1. (arg0: numpy.ndarray[int32]) -> str
            2. (arg0: numpy.ndarray[float64]) -> str
288

289
        Invoked with: """
290
291

    with pytest.raises(TypeError) as excinfo:
292
        m.overloaded3(np.array([1], dtype='uintc'))
293
    assert msg(excinfo.value) == expected_exc + repr(np.array([1], dtype='uint32'))
294
    with pytest.raises(TypeError) as excinfo:
295
        m.overloaded3(np.array([1], dtype='float32'))
296
    assert msg(excinfo.value) == expected_exc + repr(np.array([1.], dtype='float32'))
297
    with pytest.raises(TypeError) as excinfo:
298
        m.overloaded3(np.array([1], dtype='complex'))
299
    assert msg(excinfo.value) == expected_exc + repr(np.array([1. + 0.j]))
300
301

    # Exact matches:
302
303
    assert m.overloaded4(np.array([1], dtype='double')) == 'double'
    assert m.overloaded4(np.array([1], dtype='longlong')) == 'long long'
304
305
306
    # Non-exact matches requiring conversion.  Since float to integer isn't a
    # save conversion, it should go to the double overload, but short can go to
    # either (and so should end up on the first-registered, the long long).
307
308
    assert m.overloaded4(np.array([1], dtype='float32')) == 'double'
    assert m.overloaded4(np.array([1], dtype='short')) == 'long long'
309

310
311
312
    assert m.overloaded5(np.array([1], dtype='double')) == 'double'
    assert m.overloaded5(np.array([1], dtype='uintc')) == 'unsigned int'
    assert m.overloaded5(np.array([1], dtype='float32')) == 'unsigned int'
313
314


315
316
def test_greedy_string_overload():
    """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
317

318
319
320
    assert m.issue685("abc") == "string"
    assert m.issue685(np.array([97, 98, 99], dtype='b')) == "array"
    assert m.issue685(123) == "other"
321
322


323
def test_array_unchecked_fixed_dims(msg):
324
    z1 = np.array([[1, 2], [3, 4]], dtype='float64')
325
    m.proxy_add2(z1, 10)
326
327
328
    assert np.all(z1 == [[11, 12], [13, 14]])

    with pytest.raises(ValueError) as excinfo:
329
        m.proxy_add2(np.array([1., 2, 3]), 5.0)
330
331
332
    assert msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2"

    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
333
    assert np.all(m.proxy_init3(3.0) == expect_c)
334
    expect_f = np.transpose(expect_c)
335
    assert np.all(m.proxy_init3F(3.0) == expect_f)
336

337
338
    assert m.proxy_squared_L2_norm(np.array(range(6))) == 55
    assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
339

340
341
    assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
    assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
342
343
344
345


def test_array_unchecked_dyn_dims(msg):
    z1 = np.array([[1, 2], [3, 4]], dtype='float64')
346
    m.proxy_add2_dyn(z1, 10)
347
348
349
    assert np.all(z1 == [[11, 12], [13, 14]])

    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
350
    assert np.all(m.proxy_init3_dyn(3.0) == expect_c)
351

352
353
    assert m.proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
    assert m.proxy_auxiliaries2_dyn(z1) == m.array_auxiliaries2(z1)
354
355
356
357


def test_array_failure():
    with pytest.raises(ValueError) as excinfo:
358
        m.array_fail_test()
359
360
361
    assert str(excinfo.value) == 'cannot create a pybind11::array from a nullptr'

    with pytest.raises(ValueError) as excinfo:
362
        m.array_t_fail_test()
363
    assert str(excinfo.value) == 'cannot create a pybind11::array_t from a nullptr'
uentity's avatar
uentity committed
364

365
    with pytest.raises(ValueError) as excinfo:
366
        m.array_fail_test_negative_size()
367
368
    assert str(excinfo.value) == 'negative dimensions are not allowed'

uentity's avatar
uentity committed
369

370
371
372
373
374
375
def test_initializer_list():
    assert m.array_initializer_list1().shape == (1,)
    assert m.array_initializer_list2().shape == (1, 2)
    assert m.array_initializer_list3().shape == (1, 2, 3)
    assert m.array_initializer_list4().shape == (1, 2, 3, 4)

uentity's avatar
uentity committed
376

377
def test_array_resize(msg):
uentity's avatar
uentity committed
378
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float64')
379
    m.array_reshape2(a)
uentity's avatar
uentity committed
380
381
382
383
    assert(a.size == 9)
    assert(np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]))

    # total size change should succced with refcheck off
384
    m.array_resize3(a, 4, False)
uentity's avatar
uentity committed
385
386
387
    assert(a.size == 64)
    # ... and fail with refcheck on
    try:
388
        m.array_resize3(a, 3, True)
uentity's avatar
uentity committed
389
390
391
392
393
    except ValueError as e:
        assert(str(e).startswith("cannot resize an array"))
    # transposed array doesn't own data
    b = a.transpose()
    try:
394
        m.array_resize3(b, 3, False)
uentity's avatar
uentity committed
395
396
397
    except ValueError as e:
        assert(str(e).startswith("cannot resize this array: it does not own its data"))
    # ... but reshape should be fine
398
    m.array_reshape2(b)
uentity's avatar
uentity committed
399
400
401
402
403
    assert(b.shape == (8, 8))


@pytest.unsupported_on_pypy
def test_array_create_and_resize(msg):
404
    a = m.create_and_resize(2)
uentity's avatar
uentity committed
405
406
    assert(a.size == 4)
    assert(np.all(a == 42.))