Debian Codegraph @

petl/test/io/test_numpy.py

raw
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import, print_function, division
3
4
5 import pytest
6
7 import petl as etl
8 from petl.test.helpers import ieq, eq_, assert_almost_equal
9 from petl.io.numpy import toarray, fromarray, torecarray
10
11
12 try:
13 import numpy as np # noqa: F401
14 except ImportError as e:
15 pytest.skip('SKIP numpy tests: %s' % e, allow_module_level=True)
16 else:
17
18 def test_toarray_nodtype():
19 t = [('foo', 'bar', 'baz'),
20 ('apples', 1, 2.5),
21 ('oranges', 3, 4.4),
22 ('pears', 7, .1)]
23 a = toarray(t)
24 assert isinstance(a, np.ndarray)
25 assert isinstance(a['foo'], np.ndarray)
26 assert isinstance(a['bar'], np.ndarray)
27 assert isinstance(a['baz'], np.ndarray)
28 eq_('apples', a['foo'][0])
29 eq_('oranges', a['foo'][1])
30 eq_('pears', a['foo'][2])
31 eq_(1, a['bar'][0])
32 eq_(3, a['bar'][1])
33 eq_(7, a['bar'][2])
34 assert_almost_equal(2.5, a['baz'][0])
35 assert_almost_equal(4.4, a['baz'][1])
36 assert_almost_equal(.1, a['baz'][2])
37
38 def test_toarray_lists():
39 t = [['foo', 'bar', 'baz'],
40 ['apples', 1, 2.5],
41 ['oranges', 3, 4.4],
42 ['pears', 7, .1]]
43 a = toarray(t)
44 assert isinstance(a, np.ndarray)
45 assert isinstance(a['foo'], np.ndarray)
46 assert isinstance(a['bar'], np.ndarray)
47 assert isinstance(a['baz'], np.ndarray)
48 eq_('apples', a['foo'][0])
49 eq_('oranges', a['foo'][1])
50 eq_('pears', a['foo'][2])
51 eq_(1, a['bar'][0])
52 eq_(3, a['bar'][1])
53 eq_(7, a['bar'][2])
54 assert_almost_equal(2.5, a['baz'][0], places=6)
55 assert_almost_equal(4.4, a['baz'][1], places=6)
56 assert_almost_equal(.1, a['baz'][2], places=6)
57
58 def test_torecarray():
59 t = [('foo', 'bar', 'baz'),
60 ('apples', 1, 2.5),
61 ('oranges', 3, 4.4),
62 ('pears', 7, .1)]
63 a = torecarray(t)
64 assert isinstance(a, np.ndarray)
65 assert isinstance(a.foo, np.ndarray)
66 assert isinstance(a.bar, np.ndarray)
67 assert isinstance(a.baz, np.ndarray)
68 eq_('apples', a.foo[0])
69 eq_('oranges', a.foo[1])
70 eq_('pears', a.foo[2])
71 eq_(1, a.bar[0])
72 eq_(3, a.bar[1])
73 eq_(7, a.bar[2])
74 assert_almost_equal(2.5, a.baz[0], places=6)
75 assert_almost_equal(4.4, a.baz[1], places=6)
76 assert_almost_equal(.1, a.baz[2], places=6)
77
78 def test_toarray_stringdtype():
79 t = [('foo', 'bar', 'baz'),
80 ('apples', 1, 2.5),
81 ('oranges', 3, 4.4),
82 ('pears', 7, .1)]
83 a = toarray(t, dtype='U4, i2, f4')
84 assert isinstance(a, np.ndarray)
85 assert isinstance(a['foo'], np.ndarray)
86 assert isinstance(a['bar'], np.ndarray)
87 assert isinstance(a['baz'], np.ndarray)
88 eq_('appl', a['foo'][0])
89 eq_('oran', a['foo'][1])
90 eq_('pear', a['foo'][2])
91 eq_(1, a['bar'][0])
92 eq_(3, a['bar'][1])
93 eq_(7, a['bar'][2])
94 assert_almost_equal(2.5, a['baz'][0], places=6)
95 assert_almost_equal(4.4, a['baz'][1], places=6)
96 assert_almost_equal(.1, a['baz'][2], places=6)
97
98 def test_toarray_dictdtype():
99 t = [('foo', 'bar', 'baz'),
100 ('apples', 1, 2.5),
101 ('oranges', 3, 4.4),
102 ('pears', 7, .1)]
103 a = toarray(t, dtype={'foo': 'U4'}) # specify partial dtype
104 assert isinstance(a, np.ndarray)
105 assert isinstance(a['foo'], np.ndarray)
106 assert isinstance(a['bar'], np.ndarray)
107 assert isinstance(a['baz'], np.ndarray)
108 eq_('appl', a['foo'][0])
109 eq_('oran', a['foo'][1])
110 eq_('pear', a['foo'][2])
111 eq_(1, a['bar'][0])
112 eq_(3, a['bar'][1])
113 eq_(7, a['bar'][2])
114 assert_almost_equal(2.5, a['baz'][0])
115 assert_almost_equal(4.4, a['baz'][1])
116 assert_almost_equal(.1, a['baz'][2])
117
118 def test_toarray_explicitdtype():
119 t = [('foo', 'bar', 'baz'),
120 ('apples', 1, 2.5),
121 ('oranges', 3, 4.4),
122 ('pears', 7, .1)]
123 a = toarray(t, dtype=[('A', 'U4'), ('B', 'i2'), ('C', 'f4')])
124 assert isinstance(a, np.ndarray)
125 assert isinstance(a['A'], np.ndarray)
126 assert isinstance(a['B'], np.ndarray)
127 assert isinstance(a['C'], np.ndarray)
128 eq_('appl', a['A'][0])
129 eq_('oran', a['A'][1])
130 eq_('pear', a['A'][2])
131 eq_(1, a['B'][0])
132 eq_(3, a['B'][1])
133 eq_(7, a['B'][2])
134 assert_almost_equal(2.5, a['C'][0], places=6)
135 assert_almost_equal(4.4, a['C'][1], places=6)
136 assert_almost_equal(.1, a['C'][2], places=6)
137
138 def test_fromarray():
139 t = [('foo', 'bar', 'baz'),
140 ('apples', 1, 2.5),
141 ('oranges', 3, 4.4),
142 ('pears', 7, .1)]
143 a = toarray(t)
144 u = fromarray(a)
145 ieq(t, u)
146
147 def test_integration():
148 t = etl.wrap([('foo', 'bar', 'baz'),
149 ('apples', 1, 2.5),
150 ('oranges', 3, 4.4),
151 ('pears', 7, .1)])
152 a = t.toarray()
153 u = etl.fromarray(a).convert('bar', int)
154 ieq(t, u)
155
156 def test_valuesarray_no_dtype():
157 t = [('foo', 'bar', 'baz'),
158 ('apples', 1, 2.5),
159 ('oranges', 3, 4.4),
160 ('pears', 7, .1)]
161
162 expect = np.array([1, 3, 7])
163 actual = etl.wrap(t).values('bar').array()
164 eq_(expect.dtype, actual.dtype)
165 assert np.all(expect == actual)
166
167 def test_valuesarray_explicit_dtype():
168 t = [('foo', 'bar', 'baz'),
169 ('apples', 1, 2.5),
170 ('oranges', 3, 4.4),
171 ('pears', 7, .1)]
172 expect = np.array([1, 3, 7], dtype='i2')
173 actual = etl.wrap(t).values('bar').array(dtype='i2')
174 eq_(expect.dtype, actual.dtype)
175 assert np.all(expect == actual)