Debian Codegraph @

pypy/module/micronumpy/test/test_flagsobj.py

raw symbols SCIP
1 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
2
3
4 class AppTestFlagsObj(BaseNumpyAppTest):
5 def test_init(self):
6 import numpy as np
7 a = np.array([1,2,3])
8 assert a.flags['C'] is True
9 b = type(a.flags)()
10 assert b is not a.flags
11 assert b['C'] is True
12 s = str(b)
13 assert s == '%s' %(' C_CONTIGUOUS : True\n F_CONTIGUOUS : True'
14 '\n OWNDATA : True\n WRITEABLE : False'
15 '\n ALIGNED : True\n UPDATEIFCOPY : False')
16 a = np.array(2)
17 assert a.flags.owndata
18
19 def test_repr(self):
20 import numpy as np
21 a = np.array([1,2,3])
22 assert repr(type(a.flags)) == "<type 'numpy.flagsobj'>"
23
24 def test_array_flags(self):
25 import numpy as np
26 a = np.array([1,2,3])
27 assert a.flags.c_contiguous == True
28 assert a.flags['W'] == True
29 assert a.flags.fnc == False
30 assert a.flags.forc == True
31 assert a.flags['FNC'] == False
32 assert a.flags['FORC'] == True
33 assert a.flags.num == 1287
34 raises(KeyError, "a.flags['blah']")
35 raises(KeyError, "a.flags['C_CONTIGUOUS'] = False")
36 raises((TypeError, AttributeError), "a.flags.c_contiguous = False")
37
38 def test_scalar_flags(self):
39 import numpy as np
40 a = np.int32(2)
41 assert a.flags.c_contiguous == True
42 assert a.flags.num == 263
43
44 def test_compare(self):
45 import numpy as np
46 a = np.array([1,2,3])
47 b = np.array([4,5,6,7])
48 assert a.flags == b.flags
49 assert not a.flags != b.flags
50
51 def test_copy_order(self):
52 import numpy as np
53 tst = np.ones((10, 1), order='C').flags.f_contiguous
54 NPY_RELAXED_STRIDES_CHECKING = tst
55 a = np.arange(24).reshape(2, 1, 3, 4)
56 b = a.copy(order='F')
57 c = np.arange(24).reshape(2, 1, 4, 3).swapaxes(2, 3)
58
59 def check_copy_result(x, y, ccontig, fcontig, strides=False):
60 assert x is not y
61 assert (x == y).all()
62 assert res.flags.c_contiguous == ccontig
63 assert res.flags.f_contiguous == fcontig
64 # This check is impossible only because
65 # NPY_RELAXED_STRIDES_CHECKING changes the strides actively
66 if not NPY_RELAXED_STRIDES_CHECKING:
67 if strides:
68 assert x.strides == y.strides
69 else:
70 assert x.strides != y.strides
71
72 # Validate the initial state of a, b, and c
73 assert a.flags.c_contiguous
74 assert not a.flags.f_contiguous
75 assert not b.flags.c_contiguous
76 assert b.flags.f_contiguous
77 assert not c.flags.c_contiguous
78 assert not c.flags.f_contiguous
79
80 # Copy with order='C'
81 res = a.copy(order='C')
82 check_copy_result(res, a, ccontig=True, fcontig=False, strides=True)
83 res = b.copy(order='C')
84 check_copy_result(res, b, ccontig=True, fcontig=False, strides=False)
85 res = c.copy(order='C')
86 check_copy_result(res, c, ccontig=True, fcontig=False, strides=False)
87
88 # Copy with order='F'
89 res = a.copy(order='F')
90 check_copy_result(res, a, ccontig=False, fcontig=True, strides=False)
91 res = b.copy(order='F')
92 check_copy_result(res, b, ccontig=False, fcontig=True, strides=True)
93 res = c.copy(order='F')
94 check_copy_result(res, c, ccontig=False, fcontig=True, strides=False)
95
96 # Copy with order='K'
97 res = a.copy(order='K')
98 check_copy_result(res, a, ccontig=True, fcontig=False, strides=True)
99 res = b.copy(order='K')
100 check_copy_result(res, b, ccontig=False, fcontig=True, strides=True)
101 res = c.copy(order='K')
102 check_copy_result(res, c, ccontig=False, fcontig=False, strides=True)
103
104 def test_contiguous_flags(self):
105 import numpy as np
106 tst = np.ones((10, 1), order='C').flags.f_contiguous
107 NPY_RELAXED_STRIDES_CHECKING = tst
108 a = np.ones((4, 4, 1))[::2,:,:]
109 if NPY_RELAXED_STRIDES_CHECKING:
110 a.strides = a.strides[:2] + (-123,)
111 b = np.ones((2, 2, 1, 2, 2)).swapaxes(3, 4)
112
113 def check_contig(a, ccontig, fcontig):
114 assert a.flags.c_contiguous == ccontig
115 assert a.flags.f_contiguous == fcontig
116
117 # Check if new arrays are correct:
118 check_contig(a, False, False)
119 check_contig(b, False, False)
120 if NPY_RELAXED_STRIDES_CHECKING:
121 check_contig(np.empty((2, 2, 0, 2, 2)), True, True)
122 check_contig(np.array([[[1], [2]]], order='F'), True, True)
123 else:
124 check_contig(np.empty((2, 2, 0, 2, 2)), True, False)
125 check_contig(np.array([[[1], [2]]], order='F'), False, True)
126 check_contig(np.empty((2, 2)), True, False)
127 check_contig(np.empty((2, 2), order='F'), False, True)
128
129 # Check that np.array creates correct contiguous flags:
130 check_contig(np.array(a, copy=False), False, False)
131 check_contig(np.array(a, copy=False, order='C'), True, False)
132 check_contig(np.array(a, ndmin=4, copy=False, order='F'), False, True)
133
134 if NPY_RELAXED_STRIDES_CHECKING:
135 # Check slicing update of flags and :
136 check_contig(a[0], True, True)
137 check_contig(a[None, ::4, ..., None], True, True)
138 check_contig(b[0, 0, ...], False, True)
139 check_contig(b[:,:, 0:0,:,:], True, True)
140 else:
141 # Check slicing update of flags:
142 check_contig(a[0], True, False)
143 # Would be nice if this was C-Contiguous:
144 check_contig(a[None, 0, ..., None], False, False)
145 check_contig(b[0, 0, 0, ...], False, True)
146
147 # Test ravel and squeeze.
148 check_contig(a.ravel(), True, True)
149 check_contig(np.ones((1, 3, 1)).squeeze(), True, True)
150