test_tinyarray.py 16.6 KB
Newer Older
1
# Copyright 2012-2016 Tinyarray authors.
2
3
4
5
6
7
8
9
#
# This file is part of Tinyarray.  It is subject to the license terms in the
# file LICENSE.rst found in the top-level directory of this distribution and
# at https://gitlab.kwant-project.org/kwant/tinyarray/blob/master/LICENSE.rst.
# A list of Tinyarray authors can be found in the README.rst file at the
# top-level directory of this distribution and at
# https://gitlab.kwant-project.org/kwant/tinyarray.

Christoph Groth's avatar
Christoph Groth committed
10
import operator, warnings
11
import platform
12
import itertools as it
Christoph Groth's avatar
Christoph Groth committed
13
import tinyarray as ta
14
from pytest import raises
Christoph Groth's avatar
Christoph Groth committed
15
16
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal
Michael Wimmer's avatar
Michael Wimmer committed
17
import sys
18
import random
Christoph Groth's avatar
Christoph Groth committed
19

20
21
22
23
24

# numpy.testing.assert_equal() is still used even with pytest, since it does
# the right thing for arrays containing anomalous values (e.g. NaNs).


25
26
27
28
29
30
31
32
33
def machine_wordsize():
    bits, _ = platform.architecture()
    if bits == '32bit':
        return 4
    elif bits == '64bit':
        return 8
    else:
        raise RuntimeError('unknown architecture', bits)

Christoph Groth's avatar
Christoph Groth committed
34
35
dtypes = [int, float, complex]

36
37
38
39
40
41
dtype_size = {
    int: machine_wordsize(),
    float: 8,
    complex: 16
}

Christoph Groth's avatar
Christoph Groth committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
some_shapes = [(), 0, 1, 2, 3,
               (0, 0), (1, 0), (0, 1), (2, 2), (17, 17),
               (0, 0, 0), (1, 1, 1), (2, 2, 1), (2, 0, 3)]


def make(shape, dtype):
    result = np.arange(np.prod(shape), dtype=int)
    if dtype in (float, complex):
        result = result + 0.1 * result
    if dtype == complex:
        result = result + -0.5j * result
    return result.reshape(shape)


def shape_of_seq(seq, r=()):
    try:
        l = len(seq)
    except:
        return r
    if l == 0:
        return r + (0,)
    return shape_of_seq(seq[0], r + (l,))


def test_array():
    for dtype in dtypes:
        for a_shape in some_shapes:
            a = make(a_shape, dtype)

Christoph Groth's avatar
Christoph Groth committed
71
            # Creation from list.  This also tests creation from scalars.
Christoph Groth's avatar
Christoph Groth committed
72
73
74
75
76
77
78
79
            l = a.tolist()
            b = ta.array(l)
            b_shape = shape_of_seq(l)

            # a_shape and b_shape are not always equal.
            # Example: a_shape == (0, 0), b_shape = (0,).

            assert isinstance(repr(b), str)
80
81
82
            assert b.ndim == len(b_shape)
            assert tuple(b.shape) == b_shape
            assert b.size == a.size
Christoph Groth's avatar
Christoph Groth committed
83
            if a_shape != ():
84
                assert len(b) == len(a)
Christoph Groth's avatar
Christoph Groth committed
85
86
                assert_equal(np.array(ta.array(b)), np.array(l))
            else:
87
88
                assert b.dtype == dtype
                raises(TypeError, len, b)
Christoph Groth's avatar
Christoph Groth committed
89
90
            if sys.version_info[:2] > (2, 6):
                # Python 2.6 does not have memoryview.
91
                assert memoryview(b).tobytes() == memoryview(a).tobytes()
Christoph Groth's avatar
Christoph Groth committed
92
            assert_equal(np.array(b), np.array(l))
93
            assert ta.transpose(l) == np.transpose(l)
Christoph Groth's avatar
Christoph Groth committed
94
95
96

            # Here, the tinyarray is created via the buffer interface.  It's
            # possible to distinguish shape 0 from (0, 0).
Michael Wimmer's avatar
Michael Wimmer committed
97
98
            b = ta.array(a)

99
            # This tests creation of arrays from non-C-contiguous buffers.
100
            assert b == ta.array(a.transpose()).transpose()
101

Michael Wimmer's avatar
Michael Wimmer committed
102
            assert isinstance(repr(b), str)
103
104
105
106
            assert b.ndim == len(b.shape)
            assert b.shape == a.shape
            assert b.size == a.size
            assert b == a
Michael Wimmer's avatar
Michael Wimmer committed
107
108
            assert_equal(np.array(b), a)
            if a_shape != ():
109
                assert len(b) == len(a)
Michael Wimmer's avatar
Michael Wimmer committed
110
            else:
111
                raises(TypeError, len, b)
Christoph Groth's avatar
Christoph Groth committed
112
113
            if sys.version_info[:2] > (2, 6):
                # Python 2.6 does not have memoryview.
114
115
                assert memoryview(b).tobytes() == memoryview(a).tobytes()
            assert ta.transpose(b) == np.transpose(a)
Michael Wimmer's avatar
Michael Wimmer committed
116

Christoph Groth's avatar
Christoph Groth committed
117
118
119
120
121
            # Check creation from NumPy matrix.  This only works for Python >
            # 2.6.  I don't know whether this is our bug or their's.
            if sys.version_info[:2] > (2, 6):
                if not isinstance(a_shape, tuple) or len(a_shape) <= 2:
                    b = ta.array(np.matrix(a))
122
123
                    assert b.ndim == 2
                    assert b == np.matrix(a)
Christoph Groth's avatar
Christoph Groth committed
124
125
126
127

        l = []
        for i in range(16):
            l = [l]
128
        raises(ValueError, ta.array, l, dtype)
Christoph Groth's avatar
Christoph Groth committed
129

130
131
132
133
134
        raises(TypeError, ta.array, [0, [0, 0]], dtype)
        raises(ValueError, ta.array, [[0], [0, 0]], dtype)
        raises(ValueError, ta.array, [[0, 0], 0], dtype)
        raises(ValueError, ta.array, [[0, 0], [0]], dtype)
        raises(ValueError, ta.array, [[0, 0], [[0], [0]]], dtype)
Christoph Groth's avatar
Christoph Groth committed
135
136
137
138
139
140


def test_matrix():
    for l in [(), 3, (3,), ((3,)), (1, 2), ((1, 2), (3, 4))]:
        a = ta.matrix(l)
        b = np.matrix(l)
141
142
        assert a == b
        assert a.shape == b.shape
Christoph Groth's avatar
Christoph Groth committed
143
        a = ta.matrix(ta.array(l))
144
145
        assert a == b
        assert a.shape == b.shape
146
        a = ta.matrix(np.array(l))
147
148
        assert a == b
        assert a.shape == b.shape
Michael Wimmer's avatar
Michael Wimmer committed
149

Christoph Groth's avatar
Christoph Groth committed
150
151
152
153
        if sys.version_info[:2] > (2, 6):
            # Creation of tinyarrays from NumPy matrices only works for Python >
            # 2.6.  I don't know whether this is our bug or their's.
            a = ta.matrix(b)
154
            assert a == b
Christoph Groth's avatar
Christoph Groth committed
155
156

    for l in [(((),),), ((3,), ()), ((1, 2), (3,))]:
157
        raises(ValueError, ta.matrix, l)
Christoph Groth's avatar
Christoph Groth committed
158
159
160
161
162
163
164


def test_conversion():
    for src_dtype in dtypes:
        for dest_dtype in dtypes:
            src = ta.zeros(3, src_dtype)
            tsrc = tuple(src)
Michael Wimmer's avatar
Michael Wimmer committed
165
            npsrc = np.array(tsrc)
Christoph Groth's avatar
Christoph Groth committed
166
            impossible = src_dtype is complex and dest_dtype in [int, float]
Michael Wimmer's avatar
Michael Wimmer committed
167
            for s in [src, tsrc, npsrc]:
Christoph Groth's avatar
Christoph Groth committed
168
                if impossible:
169
                    raises(TypeError, ta.array, s, dest_dtype)
Christoph Groth's avatar
Christoph Groth committed
170
171
172
                else:
                    dest = ta.array(s, dest_dtype)
                    assert isinstance(dest[0], dest_dtype)
173
                    assert src == dest
Christoph Groth's avatar
Christoph Groth committed
174
175

    # Check for overflow.
Michael Wimmer's avatar
Michael Wimmer committed
176
    long_overflow = [1e300, np.array([1e300])]
Christoph Groth's avatar
Christoph Groth committed
177
    # This check only works for Python 2.
Joseph Weston's avatar
Joseph Weston committed
178
    if 18446744073709551615 > sys.maxsize:
Michael Wimmer's avatar
Michael Wimmer committed
179
180
181
182
        long_overflow.extend([np.array([18446744073709551615], np.uint64),
                              18446744073709551615])

    for s in long_overflow:
183
        raises(OverflowError, ta.array, s, int)
Michael Wimmer's avatar
Michael Wimmer committed
184

Christoph Groth's avatar
Christoph Groth committed
185
186
187
188

def test_special_constructors():
    for dtype in dtypes:
        for shape in some_shapes:
189
190
            assert ta.zeros(shape, dtype) == np.zeros(shape, dtype)
            assert ta.ones(shape, dtype) == np.ones(shape, dtype)
Christoph Groth's avatar
Christoph Groth committed
191
        for n in [0, 1, 2, 3, 17]:
192
            assert ta.identity(n, dtype) == np.identity(n, dtype)
Christoph Groth's avatar
Christoph Groth committed
193
194
195
196


def test_dot():
    # Check acceptance of non-tinyarray arguments.
197
    assert ta.dot([1, 2], (3, 4)) == 11
Christoph Groth's avatar
Christoph Groth committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

    for dtype in dtypes:
        shape_pairs = [(1, 1), (2, 2), (3, 3),
                       (0, 0),
                       (0, (0, 1)), ((0, 1), 1),
                       (0, (0, 2)), ((0, 2), 2),
                       (1, (1, 2)), ((2, 1), 1),
                       (2, (2, 1)), ((1, 2), 2),
                       (2, (2, 3)), ((3, 2), 2),
                       ((1, 1), (1, 1)), ((2, 2), (2, 2)),
                       ((3, 3), (3, 3)), ((2, 3), (3, 2)), ((2, 1), (1, 2)),
                       ((2, 3, 4), (4, 3)),
                       ((2, 3, 4), 4),
                       ((3, 4), (2, 4, 3)),
                       (4, (2, 4, 3))]

        # We have to use almost_equal here because the result of numpy's dot
        # does not always agree to the last bit with a naive implementation.
        # (This is probably due to their usage of SSE or parallelization.)
        #
        # On my machine in summer 2012 with Python 2.7 and 3.2 the program
        #
        # import numpy as np
        # a = np.array([13.2, 14.3, 15.4, 16.5])
        # b = np.array([-5.0, -3.9, -2.8, -1.7])
        # r = np.dot(a, b)
        # rr = sum(x * y for x, y in zip(a, b))
        # print(r - rr)
        #
        # outputs 2.84217094304e-14.
        for sa, sb in shape_pairs:
            a = make(sa, dtype)
            b = make(sb, dtype) - 5
            assert_almost_equal(ta.dot(ta.array(a), ta.array(b)), np.dot(a, b),
                                13)

        shape_pairs = [((), 2), (2, ()),
                       (1, 2),
                       (1, (2, 2)), ((1, 1), 2),
                       ((2, 2), (3, 2)),
                       ((2, 3, 2), (4, 3)),
                       ((2, 3, 4), 3),
                       ((3, 3), (2, 4, 3)),
                       (3, (2, 4, 3))]
        for sa, sb in shape_pairs:
            a = make(sa, dtype)
            b = make(sb, dtype) - 5
245
246
247
            raises(ValueError, ta.dot, ta.array(a.tolist()),
                   ta.array(b.tolist()))
            raises(ValueError, ta.dot, ta.array(a), ta.array(b))
Christoph Groth's avatar
Christoph Groth committed
248
249
250
251


def test_iteration():
    for dtype in dtypes:
252
        raises(TypeError, tuple, ta.array(1, dtype))
Christoph Groth's avatar
Christoph Groth committed
253
254
255
        for shape in [0, 1, 2, 3, (1, 0), (2, 2), (17, 17),
                      (1, 1, 1), (2, 2, 1), (2, 0, 3)]:
            a = make(shape, dtype)
256
            assert tuple(ta.array(a)) == tuple(a)
Christoph Groth's avatar
Christoph Groth committed
257
258
259
260
261
262


def test_as_dict_key():
    n = 100
    d = {}
    for dtype in dtypes + dtypes:
Joseph Weston's avatar
Joseph Weston committed
263
264
        for i in range(n):
            d[ta.array(range(i), dtype)] = i
265
        assert len(d) == n
Joseph Weston's avatar
Joseph Weston committed
266
    for i in range(n):
267
        assert d[tuple(range(i))] == i
Christoph Groth's avatar
Christoph Groth committed
268
269
270


def test_hash_equality():
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    random.seed(123)
    maxint = sys.maxsize + 1    # will be typically 2**31 or 2**63
    int_bits = 63 if maxint > 2**32 else 31

    special = [float('nan'), float('inf'), float('-inf'),
               0, -1, -1.0, -1 + 0j,
               303, -312424, -0.3, 1.7, 0.4j, -12.3j, 1 - 12.3j, 1.3 - 12.3j,
               (), (-1,), (2,),
               (0, 0), (-1, -1), (-5, 7), (3, -1, 0),
               ((0, 1), (2, 3)), (((-1,),),)]
    powers = [sign * (2**e + a) for sign in [1, -1] for a in [-1, 0, 1]
              for e in range(int_bits)]
    powers.extend([2**int_bits - 1, -2**int_bits, -2**int_bits + 1])
    small_random_ints = (random.randrange(-2**16, 2**16) for i in range(1000))
    large_random_ints = (random.randrange(-maxint, maxint) for i in range(1000))
    small_random_floats = (random.gauss(0, 1) for i in range(1000))
    large_random_floats = (random.gauss(0, 1e100) for i in range(1000))

    for collection in [special, powers,
                       small_random_ints, large_random_ints,
                       small_random_floats, large_random_floats]:
        for thing in collection:
            arr = ta.array(thing)
            if thing == thing:
                assert arr == thing
                assert not (arr != thing)
297
            assert hash(arr) == hash(thing), repr(thing)
Christoph Groth's avatar
Christoph Groth committed
298
299
300
301
302
303
304


def test_broadcasting():
    for sa in [(), 1, (1, 1, 1, 1), 2, (3, 2), (4, 3, 2), (5, 4, 3, 2)]:
        for sb in [(), 1, (1, 1), (4, 1, 1), 2, (1, 2), (3, 1), (1, 3, 2)]:
            a = make(sa, int)
            b = make(sb, int)
305
306
            assert ta.array(a.tolist()) + ta.array(b.tolist()) == a + b
            assert ta.array(a) + ta.array(b) == a + b
Christoph Groth's avatar
Christoph Groth committed
307
308
309
310
311
312
313


def test_promotion():
    for dtypea in dtypes:
        for dtypeb in dtypes:
            a = make(3, dtypea)
            b = make(3, dtypeb)
314
315
            assert ta.array(a.tolist()) + ta.array(b.tolist()) == a + b
            assert ta.array(a) + ta.array(b) == a + b
Christoph Groth's avatar
Christoph Groth committed
316
317
318
319


def test_binary_operators():
    ops = operator
320
321
    operations = [ops.add, ops.sub, ops.mul, ops.mod, ops.floordiv, ops.truediv]
    if sys.version_info.major < 3:
Joseph Weston's avatar
Joseph Weston committed
322
323
        operations.append(ops.div)

Christoph Groth's avatar
Christoph Groth committed
324
325
326
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=RuntimeWarning)

Joseph Weston's avatar
Joseph Weston committed
327
        for op in operations:
Christoph Groth's avatar
Christoph Groth committed
328
329
330
331
332
333
            for dtype in dtypes:
                for shape in [(), 1, 3, (3, 2)]:
                    if dtype is complex and op in [ops.mod, ops.floordiv]:
                        continue
                    a = make(shape, dtype)
                    b = make(shape, dtype)
334
335
                    assert_equal(op(ta.array(a.tolist()), ta.array(b.tolist())),
                                 op(a, b))
Michael Wimmer's avatar
Michael Wimmer committed
336
                    assert_equal(op(ta.array(a), ta.array(b)), op(a, b))
Christoph Groth's avatar
Christoph Groth committed
337
338


339

Christoph Groth's avatar
Christoph Groth committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def test_binary_ufuncs():
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=RuntimeWarning)

        for name in ["add", "subtract", "multiply", "divide",
                     "remainder", "floor_divide"]:
            np_func = np.__dict__[name]
            ta_func = ta.__dict__[name]
            for dtype in dtypes:
                for shape in [(), 1, 3, (3, 2)]:
                    if dtype is complex and \
                            name in ["remainder", "floor_divide"]:
                        continue
                    a = make(shape, dtype)
                    b = make(shape, dtype)
                    assert_equal(ta_func(a.tolist(), b.tolist()),
                                 np_func(a, b))
Michael Wimmer's avatar
Michael Wimmer committed
357
                    assert_equal(ta_func(a, b), np_func(a, b))
Christoph Groth's avatar
Christoph Groth committed
358
359
360
361
362
363
364
365


def test_unary_operators():
    ops = operator
    for op in [ops.neg, ops.pos, ops.abs]:
        for dtype in dtypes:
            for shape in [(), 1, 3, (3, 2)]:
                a = make(shape, dtype)
366
367
                assert op(ta.array(a.tolist())) == op(a)
                assert op(ta.array(a)) == op(a)
Joseph Weston's avatar
Joseph Weston committed
368
369


Christoph Groth's avatar
Christoph Groth committed
370
371
372
373
374
375
376
377
378
def test_unary_ufuncs():
    for name in ["negative", "abs", "absolute", "round", "floor", "ceil",
                 "conjugate"]:
        np_func = np.__dict__[name]
        ta_func = ta.__dict__[name]
        for dtype in dtypes:
            for shape in [(), 1, 3, (3, 2)]:
                a = make(shape, dtype)
                if dtype is complex and name in ["round", "floor", "ceil"]:
379
                    raises(TypeError, ta_func, a.tolist())
Christoph Groth's avatar
Christoph Groth committed
380
                else:
381
                    assert ta_func(a.tolist()) == np_func(a)
Christoph Groth's avatar
Christoph Groth committed
382
383
384
        for x in [-987654322.5, -987654321.5, -4.51, -3.51, -2.5, -2.0,
                   -1.7, -1.5, -0.5, -0.3, -0.0, 0.0, 0.3, 0.5, 1.5, 1.7,
                   2.0, 2.5, 3.51, 4.51, 987654321.5, 987654322.5]:
385
386
387
388
            if x == -0.5 and name == "round":
                # Work around an inconsistency in NumPy: on Unix, np.round(-0.5)
                # is -0.0, and on Windows it is 0.0, while np.ceil(-0.5) is -0.0
                # always.
389
                assert ta.round(-0.5) == -0.0
390
            else:
391
                assert ta_func(x) == np_func(x)
Christoph Groth's avatar
Christoph Groth committed
392
393


394
395
396
397
398
399
def test_other_scalar_types():
    types = [np.int16, np.int32, np.int64,
             np.float16, np.float32, np.float64]
    for t in types:
        a = t(123.456)
        assert_equal(ta.array(a), np.array(a))
400
        assert_equal(ta.matrix(a), np.matrix(a))
401
402


403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
def test_sizeof():
    obj = object()
    word_size = machine_wordsize()
    for shape in some_shapes:
        for dtype in dtypes:
            a = ta.zeros(shape, dtype)
            sizeof = a.__sizeof__()
            # basic buffer size
            n_elements = a.size
            # if the array is > 1D then the shape is stored
            # at the start of the buffer
            if len(a.shape) > 1:
                n_elements += (a.ndim * machine_wordsize() +
                               dtype_size[dtype] - 1) // dtype_size[dtype]
            buffer_size = n_elements * dtype_size[dtype]
            # basic Python object has 3 pointer-sized members
            sizeof_should_be = buffer_size + 3 * machine_wordsize()
420
            assert sizeof == sizeof_should_be
421
422


423
424
425
426
427
428
429
430
431
432
def test_comparison():
    ops = operator
    for op in [ops.ge, ops.gt, ops.le, ops.lt, ops.eq, ops.ne]:
        for dtype in (int, float, complex):
            for left, right in it.product((np.zeros, np.ones), repeat=2):
                for shape in [(), (1,), (2,), (2, 2), (2, 2, 2), (2, 3)]:
                    a = left(shape, dtype)
                    b = right(shape, dtype)
                    if dtype is complex and op not in [ops.eq, ops.ne]:
                        # unorderable types
433
                        raises(TypeError, op, ta.array(a), ta.array(b))
434
435
436
                    else:
                        # passing the same object
                        same = ta.array(a)
437
                        assert op(same, same) == op(a.tolist(), a.tolist())
438
                        # passing different objects, but equal
439
440
                        assert (op(ta.array(a), ta.array(a)) ==
                                op(a.tolist(), a.tolist()))
441
                        # passing different objects, not equal
442
443
                        assert (op(ta.array(a), ta.array(b)) ==
                                op(a.tolist(), b.tolist()))
444
445
446
447
448
449
                # test different ndims and different shapes
                for shp1, shp2 in [((2,), (2, 2)), ((2, 2), (2, 3))]:
                    a = left(shp1, dtype)
                    b = right(shp2, dtype)
                    if op not in (ops.eq, ops.ne):
                        # unorderable types
450
                        raises(TypeError, op, ta.array(a), ta.array(b))
451
452


Joseph Weston's avatar
Joseph Weston committed
453
def test_pickle():
Joseph Weston's avatar
Joseph Weston committed
454
    import pickle
Christoph Groth's avatar
Christoph Groth committed
455
456
457
458

    for dtype in dtypes:
        for shape in some_shapes:
            a = ta.array(make(shape, dtype))
459
            assert pickle.loads(pickle.dumps(a)) == a