test_numpy_array.py 18.5 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
3
import pytest

4
5
6
import env  # noqa: F401

from pybind11_tests import numpy_array as m
7

8
np = pytest.importorskip("numpy")
9
10


11
12
13
14
15
16
17
18
19
20
21
def test_dtypes():
    # See issue #1328.
    # - Platform-dependent sizes.
    for size_check in m.get_platform_dtype_size_checks():
        print(size_check)
        assert size_check.size_cpp == size_check.size_numpy, size_check
    # - Concrete sizes.
    for check in m.get_concrete_dtype_checks():
        print(check)
        assert check.numpy == check.pybind11, check
        if check.numpy.num != check.pybind11.num:
22
23
24
25
26
            print(
                "NOTE: typenum mismatch for {}: {} != {}".format(
                    check, check.numpy.num, check.pybind11.num
                )
            )
27
28


29
@pytest.fixture(scope="function")
30
def arr():
31
    return np.array([[1, 2, 3], [4, 5, 6]], "=u2")
32
33


34
def test_array_attributes():
35
    a = np.array(0, "f8")
36
37
38
    assert m.ndim(a) == 0
    assert all(m.shape(a) == [])
    assert all(m.strides(a) == [])
39
    with pytest.raises(IndexError) as excinfo:
40
        m.shape(a, 0)
41
    assert str(excinfo.value) == "invalid axis: 0 (ndim = 0)"
42
    with pytest.raises(IndexError) as excinfo:
43
        m.strides(a, 0)
44
    assert str(excinfo.value) == "invalid axis: 0 (ndim = 0)"
45
46
47
48
49
    assert m.writeable(a)
    assert m.size(a) == 1
    assert m.itemsize(a) == 8
    assert m.nbytes(a) == 8
    assert m.owndata(a)
50

51
    a = np.array([[1, 2, 3], [4, 5, 6]], "u2").view()
52
    a.flags.writeable = False
53
54
55
56
57
58
59
    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
60
    with pytest.raises(IndexError) as excinfo:
61
        m.shape(a, 2)
62
    assert str(excinfo.value) == "invalid axis: 2 (ndim = 2)"
63
    with pytest.raises(IndexError) as excinfo:
64
        m.strides(a, 2)
65
    assert str(excinfo.value) == "invalid axis: 2 (ndim = 2)"
66
67
68
69
70
    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)
71
72


73
74
75
@pytest.mark.parametrize(
    "args, ret", [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)]
)
76
def test_index_offset(arr, args, ret):
77
78
79
80
    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
81
82
83


def test_dim_check_fail(arr):
84
85
86
87
88
89
90
91
92
93
    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,
    ):
94
95
        with pytest.raises(IndexError) as excinfo:
            func(arr, 1, 2, 3)
96
        assert str(excinfo.value) == "too many indices for an array: 3 (ndim = 2)"
97
98


99
100
101
102
103
104
105
106
107
@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]),
    ],
)
108
def test_data(arr, args, ret):
109
    from sys import byteorder
110

111
    assert all(m.data_t(arr, *args) == ret)
112
113
    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)
114
115


116
@pytest.mark.parametrize("dim", [0, 1, 3])
117
def test_at_fail(arr, dim):
118
    for func in m.at_t, m.mutate_at_t:
119
120
        with pytest.raises(IndexError) as excinfo:
            func(arr, *([0] * dim))
121
122
123
        assert str(excinfo.value) == "index dimension mismatch: {} (ndim = 2)".format(
            dim
        )
124
125
126


def test_at(arr):
127
128
129
130
131
    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])
132
133


134
135
def test_mutate_readonly(arr):
    arr.flags.writeable = False
136
137
138
139
140
    for func, args in (
        (m.mutate_data, ()),
        (m.mutate_data_t, ()),
        (m.mutate_at_t, (0, 0)),
    ):
141
142
        with pytest.raises(ValueError) as excinfo:
            func(arr, *args)
143
        assert str(excinfo.value) == "array is not writeable"
144
145
146


def test_mutate_data(arr):
147
148
149
150
151
    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])
152

153
154
155
156
157
    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])
158
159
160


def test_bounds_check(arr):
161
162
163
164
165
166
167
168
169
170
    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,
    ):
171
        with pytest.raises(IndexError) as excinfo:
172
            func(arr, 2, 0)
173
        assert str(excinfo.value) == "index 2 is out of bounds for axis 0 with size 2"
174
        with pytest.raises(IndexError) as excinfo:
175
            func(arr, 0, 4)
176
        assert str(excinfo.value) == "index 4 is out of bounds for axis 1 with size 3"
177

178

179
def test_make_c_f_array():
180
181
182
183
    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
184
185


186
187
188
def test_make_empty_shaped_array():
    m.make_empty_shaped_array()

189
190
191
192
193
    # empty shape means numpy scalar, PEP 3118
    assert m.scalar_int().ndim == 0
    assert m.scalar_int().shape == ()
    assert m.scalar_int() == 42

194

195
def test_wrap():
Jason Rhinelander's avatar
Jason Rhinelander committed
196
    def assert_references(a, b, base=None):
197
        from distutils.version import LooseVersion
198

Jason Rhinelander's avatar
Jason Rhinelander committed
199
200
        if base is None:
            base = a
201
        assert a is not b
202
        assert a.__array_interface__["data"][0] == b.__array_interface__["data"][0]
203
204
205
206
207
208
        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
209
210
211
212
        if LooseVersion(np.__version__) >= LooseVersion("1.14.0"):
            assert a.flags.writebackifcopy == b.flags.writebackifcopy
        else:
            assert a.flags.updateifcopy == b.flags.updateifcopy
213
214
        assert np.all(a == b)
        assert not b.flags.owndata
Jason Rhinelander's avatar
Jason Rhinelander committed
215
        assert b.base is base
216
217
218
219
220
221
        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
222
    a2 = m.wrap(a1)
223
224
    assert_references(a1, a2)

225
    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order="F")
226
    assert a1.flags.owndata and a1.base is None
227
    a2 = m.wrap(a1)
228
229
    assert_references(a1, a2)

230
    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order="C")
231
    a1.flags.writeable = False
232
    a2 = m.wrap(a1)
233
234
235
    assert_references(a1, a2)

    a1 = np.random.random((4, 4, 4))
236
    a2 = m.wrap(a1)
237
238
    assert_references(a1, a2)

Jason Rhinelander's avatar
Jason Rhinelander committed
239
    a1t = a1.transpose()
240
    a2 = m.wrap(a1t)
Jason Rhinelander's avatar
Jason Rhinelander committed
241
    assert_references(a1t, a2, a1)
242

Jason Rhinelander's avatar
Jason Rhinelander committed
243
    a1d = a1.diagonal()
244
    a2 = m.wrap(a1d)
Jason Rhinelander's avatar
Jason Rhinelander committed
245
    assert_references(a1d, a2, a1)
246

247
    a1m = a1[::-1, ::-1, ::-1]
248
    a2 = m.wrap(a1m)
249
250
    assert_references(a1m, a2, a1)

251
252
253

def test_numpy_view(capture):
    with capture:
254
        ac = m.ArrayClass()
255
256
257
258
        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
259
        pytest.gc_collect()
260
261
262
    assert (
        capture
        == """
263
264
265
266
        ArrayClass()
        ArrayClass::numpy_view()
        ArrayClass::numpy_view()
    """
267
    )
268
269
270
271
272
273
274
    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
275
276
        pytest.gc_collect()
        pytest.gc_collect()
277
278
279
    assert (
        capture
        == """
280
281
        ~ArrayClass()
    """
282
    )
283
284
285


def test_cast_numpy_int64_to_uint64():
286
287
    m.function_taking_uint64(123)
    m.function_taking_uint64(np.uint64(123))
288
289
290


def test_isinstance():
291
292
    assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array")
    assert m.isinstance_typed(np.array([1.0, 2.0, 3.0]))
293
294
295


def test_constructors():
296
    defaults = m.default_constructors()
297
298
299
300
301
302
    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

303
    results = m.converting_constructors([1, 2, 3])
304
305
306
307
308
    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
309
310


311
312
def test_overload_resolution(msg):
    # Exact overload matches:
313
314
315
316
317
318
319
    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"
320
321

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

324
    with pytest.raises(TypeError) as excinfo:
325
        m.overloaded("not an array")
326
327
328
    assert (
        msg(excinfo.value)
        == """
329
        overloaded(): incompatible function arguments. The following argument types are supported:
330
331
332
333
334
335
336
            1. (arg0: numpy.ndarray[numpy.float64]) -> str
            2. (arg0: numpy.ndarray[numpy.float32]) -> str
            3. (arg0: numpy.ndarray[numpy.int32]) -> str
            4. (arg0: numpy.ndarray[numpy.uint16]) -> str
            5. (arg0: numpy.ndarray[numpy.int64]) -> str
            6. (arg0: numpy.ndarray[numpy.complex128]) -> str
            7. (arg0: numpy.ndarray[numpy.complex64]) -> str
337
338
339

        Invoked with: 'not an array'
    """
340
    )
341

342
343
344
345
346
    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"
347

348
349
    assert m.overloaded3(np.array([1], dtype="float64")) == "double"
    assert m.overloaded3(np.array([1], dtype="intc")) == "int"
350
351
    expected_exc = """
        overloaded3(): incompatible function arguments. The following argument types are supported:
352
353
            1. (arg0: numpy.ndarray[numpy.int32]) -> str
            2. (arg0: numpy.ndarray[numpy.float64]) -> str
354

355
        Invoked with: """
356
357

    with pytest.raises(TypeError) as excinfo:
358
359
        m.overloaded3(np.array([1], dtype="uintc"))
    assert msg(excinfo.value) == expected_exc + repr(np.array([1], dtype="uint32"))
360
    with pytest.raises(TypeError) as excinfo:
361
362
        m.overloaded3(np.array([1], dtype="float32"))
    assert msg(excinfo.value) == expected_exc + repr(np.array([1.0], dtype="float32"))
363
    with pytest.raises(TypeError) as excinfo:
364
365
        m.overloaded3(np.array([1], dtype="complex"))
    assert msg(excinfo.value) == expected_exc + repr(np.array([1.0 + 0.0j]))
366
367

    # Exact matches:
368
369
    assert m.overloaded4(np.array([1], dtype="double")) == "double"
    assert m.overloaded4(np.array([1], dtype="longlong")) == "long long"
370
371
372
    # 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).
373
374
    assert m.overloaded4(np.array([1], dtype="float32")) == "double"
    assert m.overloaded4(np.array([1], dtype="short")) == "long long"
375

376
377
378
    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"
379
380


381
382
def test_greedy_string_overload():
    """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
383

384
    assert m.issue685("abc") == "string"
385
    assert m.issue685(np.array([97, 98, 99], dtype="b")) == "array"
386
    assert m.issue685(123) == "other"
387
388


389
def test_array_unchecked_fixed_dims(msg):
390
    z1 = np.array([[1, 2], [3, 4]], dtype="float64")
391
    m.proxy_add2(z1, 10)
392
393
394
    assert np.all(z1 == [[11, 12], [13, 14]])

    with pytest.raises(ValueError) as excinfo:
395
396
397
398
        m.proxy_add2(np.array([1.0, 2, 3]), 5.0)
    assert (
        msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2"
    )
399

400
    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype="int")
401
    assert np.all(m.proxy_init3(3.0) == expect_c)
402
    expect_f = np.transpose(expect_c)
403
    assert np.all(m.proxy_init3F(3.0) == expect_f)
404

405
406
    assert m.proxy_squared_L2_norm(np.array(range(6))) == 55
    assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
407

408
409
    assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
    assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
410

411
412
413
    assert m.proxy_auxiliaries1_const_ref(z1[0, :])
    assert m.proxy_auxiliaries2_const_ref(z1)

414
415

def test_array_unchecked_dyn_dims(msg):
416
    z1 = np.array([[1, 2], [3, 4]], dtype="float64")
417
    m.proxy_add2_dyn(z1, 10)
418
419
    assert np.all(z1 == [[11, 12], [13, 14]])

420
    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype="int")
421
    assert np.all(m.proxy_init3_dyn(3.0) == expect_c)
422

423
424
    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)
425
426
427
428


def test_array_failure():
    with pytest.raises(ValueError) as excinfo:
429
        m.array_fail_test()
430
    assert str(excinfo.value) == "cannot create a pybind11::array from a nullptr"
431
432

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

436
    with pytest.raises(ValueError) as excinfo:
437
        m.array_fail_test_negative_size()
438
    assert str(excinfo.value) == "negative dimensions are not allowed"
439

uentity's avatar
uentity committed
440

441
442
443
444
445
446
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
447

448
def test_array_resize(msg):
449
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype="float64")
450
    m.array_reshape2(a)
451
452
    assert a.size == 9
    assert np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
uentity's avatar
uentity committed
453
454

    # total size change should succced with refcheck off
455
    m.array_resize3(a, 4, False)
456
    assert a.size == 64
uentity's avatar
uentity committed
457
458
    # ... and fail with refcheck on
    try:
459
        m.array_resize3(a, 3, True)
uentity's avatar
uentity committed
460
    except ValueError as e:
461
        assert str(e).startswith("cannot resize an array")
uentity's avatar
uentity committed
462
463
464
    # transposed array doesn't own data
    b = a.transpose()
    try:
465
        m.array_resize3(b, 3, False)
uentity's avatar
uentity committed
466
    except ValueError as e:
467
        assert str(e).startswith("cannot resize this array: it does not own its data")
uentity's avatar
uentity committed
468
    # ... but reshape should be fine
469
    m.array_reshape2(b)
470
    assert b.shape == (8, 8)
uentity's avatar
uentity committed
471
472


473
@pytest.mark.xfail("env.PYPY")
uentity's avatar
uentity committed
474
def test_array_create_and_resize(msg):
475
    a = m.create_and_resize(2)
476
477
    assert a.size == 4
    assert np.all(a == 42.0)
478
479
480
481
482


def test_index_using_ellipsis():
    a = m.index_using_ellipsis(np.zeros((5, 6, 7)))
    assert a.shape == (6,)
483
484


485
486
487
488
489
490
491
492
493
494
495
496
497
@pytest.mark.parametrize(
    "test_func",
    [
        m.test_fmt_desc_float,
        m.test_fmt_desc_double,
        m.test_fmt_desc_const_float,
        m.test_fmt_desc_const_double,
    ],
)
def test_format_descriptors_for_floating_point_types(test_func):
    assert "numpy.ndarray[numpy.float" in test_func.__doc__


498
@pytest.mark.parametrize("forcecast", [False, True])
499
@pytest.mark.parametrize("contiguity", [None, "C", "F"])
500
501
502
503
504
505
@pytest.mark.parametrize("noconvert", [False, True])
@pytest.mark.filterwarnings(
    "ignore:Casting complex values to real discards the imaginary part:numpy.ComplexWarning"
)
def test_argument_conversions(forcecast, contiguity, noconvert):
    function_name = "accept_double"
506
    if contiguity == "C":
507
        function_name += "_c_style"
508
    elif contiguity == "F":
509
510
511
512
513
514
515
        function_name += "_f_style"
    if forcecast:
        function_name += "_forcecast"
    if noconvert:
        function_name += "_noconvert"
    function = getattr(m, function_name)

516
517
    for dtype in [np.dtype("float32"), np.dtype("float64"), np.dtype("complex128")]:
        for order in ["C", "F"]:
518
519
520
521
522
            for shape in [(2, 2), (1, 3, 1, 1), (1, 1, 1), (0,)]:
                if not noconvert:
                    # If noconvert is not passed, only complex128 needs to be truncated and
                    # "cannot be safely obtained". So without `forcecast`, the argument shouldn't
                    # be accepted.
523
                    should_raise = dtype.name == "complex128" and not forcecast
524
525
526
527
528
                else:
                    # If noconvert is passed, only float64 and the matching order is accepted.
                    # If at most one dimension has a size greater than 1, the array is also
                    # trivially contiguous.
                    trivially_contiguous = sum(1 for d in shape if d > 1) <= 1
529
530
531
532
                    should_raise = dtype.name != "float64" or (
                        contiguity is not None
                        and contiguity != order
                        and not trivially_contiguous
533
534
535
536
537
538
                    )

                array = np.zeros(shape, dtype=dtype, order=order)
                if not should_raise:
                    function(array)
                else:
539
540
541
                    with pytest.raises(
                        TypeError, match="incompatible function arguments"
                    ):
542
543
544
                        function(array)


545
@pytest.mark.xfail("env.PYPY")
546
547
def test_dtype_refcount_leak():
    from sys import getrefcount
548

549
550
551
552
553
554
    dtype = np.dtype(np.float_)
    a = np.array([1], dtype=dtype)
    before = getrefcount(dtype)
    m.ndim(a)
    after = getrefcount(dtype)
    assert after == before