~ubuntu-branches/ubuntu/wily/numexpr/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/0002-feature-forwarded-upstream-fix-FTBFS-on-Hurd-and-KFr.patch/numexpr/tests/test_numexpr.py

  • Committer: Package Import Robot
  • Author(s): Antonio Valentino
  • Date: 2013-09-28 09:03:27 UTC
  • mfrom: (7.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130928090327-s69mvg0n2xnz6cn8
New upstream release (fixes a build failure on s390)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
###################################################################
2
 
#  Numexpr - Fast numerical array expression evaluator for NumPy.
3
 
#
4
 
#      License: MIT
5
 
#      Author:  See AUTHORS.txt
6
 
#
7
 
#  See LICENSE.txt and LICENSES/*.txt for details about copyright and
8
 
#  rights to use.
9
 
####################################################################
10
 
 
11
 
import new, sys, os
12
 
import warnings
13
 
 
14
 
import numpy
15
 
from numpy import (
16
 
    array, arange, empty, zeros, int32, int64, uint16, complex_, float64, rec,
17
 
    copy, ones_like, where, alltrue, linspace,
18
 
    sum, prod, sqrt, fmod,
19
 
    sin, cos, tan, arcsin, arccos, arctan, arctan2,
20
 
    sinh, cosh, tanh, arcsinh, arccosh, arctanh,
21
 
    log, log1p, log10, exp, expm1)
22
 
from numpy.testing import *
23
 
from numpy import shape, allclose, array_equal, ravel, isnan, isinf
24
 
 
25
 
import numexpr
26
 
from numexpr import E, NumExpr, evaluate, disassemble, use_vml
27
 
 
28
 
import unittest
29
 
TestCase = unittest.TestCase
30
 
 
31
 
double = numpy.double
32
 
 
33
 
 
34
 
# Recommended minimum versions
35
 
minimum_numpy_version = "1.6"
36
 
 
37
 
class test_numexpr(TestCase):
38
 
 
39
 
    def setUp(self):
40
 
        numexpr.set_num_threads(self.nthreads)
41
 
 
42
 
    def test_simple(self):
43
 
        ex = 2.0 * E.a + 3.0 * E.b * E.c
44
 
        sig = [('a', double), ('b', double), ('c', double)]
45
 
        func = NumExpr(ex, signature=sig)
46
 
        x = func(array([1., 2, 3]), array([4., 5, 6]), array([7., 8, 9]))
47
 
        assert_array_equal(x, array([  86.,  124.,  168.]))
48
 
 
49
 
    def test_simple_expr_small_array(self):
50
 
        func = NumExpr(E.a)
51
 
        x = arange(100.0)
52
 
        y = func(x)
53
 
        assert_array_equal(x, y)
54
 
 
55
 
    def test_simple_expr(self):
56
 
        func = NumExpr(E.a)
57
 
        x = arange(1e6)
58
 
        y = func(x)
59
 
        assert_array_equal(x, y)
60
 
 
61
 
    def test_rational_expr(self):
62
 
        func = NumExpr((E.a + 2.0*E.b) / (1 + E.a + 4*E.b*E.b))
63
 
        a = arange(1e6)
64
 
        b = arange(1e6) * 0.1
65
 
        x = (a + 2*b) / (1 + a + 4*b*b)
66
 
        y = func(a, b)
67
 
        assert_array_almost_equal(x, y)
68
 
 
69
 
    def test_reductions(self):
70
 
        # Check that they compile OK.
71
 
        assert_equal(disassemble(
72
 
            NumExpr("sum(x**2+2, axis=None)", [('x', double)])),
73
 
                     [('mul_ddd', 't3', 'r1[x]', 'r1[x]'),
74
 
                      ('add_ddd', 't3', 't3', 'c2[2.0]'),
75
 
                      ('sum_ddn', 'r0', 't3', None)])
76
 
        assert_equal(disassemble(
77
 
            NumExpr("sum(x**2+2, axis=1)", [('x', double)])),
78
 
                     [('mul_ddd', 't3', 'r1[x]', 'r1[x]'),
79
 
                      ('add_ddd', 't3', 't3', 'c2[2.0]'),
80
 
                      ('sum_ddn', 'r0', 't3', 1)])
81
 
        assert_equal(disassemble(
82
 
            NumExpr("prod(x**2+2, axis=2)", [('x', double)])),
83
 
                     [('mul_ddd', 't3', 'r1[x]', 'r1[x]'),
84
 
                      ('add_ddd', 't3', 't3', 'c2[2.0]'),
85
 
                      ('prod_ddn', 'r0', 't3', 2)])
86
 
        # Check that full reductions work.
87
 
        x = zeros(1e5)+.01   # checks issue #41
88
 
        assert_allclose(evaluate("sum(x+2,axis=None)"), sum(x+2,axis=None))
89
 
        assert_allclose(evaluate("sum(x+2,axis=0)"), sum(x+2,axis=0))
90
 
        assert_allclose(evaluate("prod(x,axis=0)"), prod(x,axis=0))
91
 
 
92
 
        x = arange(10.0)
93
 
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
94
 
        assert_allclose(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
95
 
 
96
 
        x = arange(100.0)
97
 
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
98
 
        assert_allclose(evaluate("prod(x-1,axis=0)"), prod(x-1,axis=0))
99
 
        x = linspace(0.1,1.0,2000)
100
 
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
101
 
        assert_allclose(evaluate("prod(x-1,axis=0)"), prod(x-1,axis=0))
102
 
 
103
 
        # Check that reductions along an axis work
104
 
        y = arange(9.0).reshape(3,3)
105
 
        assert_allclose(evaluate("sum(y**2, axis=1)"), sum(y**2, axis=1))
106
 
        assert_allclose(evaluate("sum(y**2, axis=0)"), sum(y**2, axis=0))
107
 
        assert_allclose(evaluate("sum(y**2, axis=None)"), sum(y**2, axis=None))
108
 
        assert_allclose(evaluate("prod(y**2, axis=1)"), prod(y**2, axis=1))
109
 
        assert_allclose(evaluate("prod(y**2, axis=0)"), prod(y**2, axis=0))
110
 
        assert_allclose(evaluate("prod(y**2, axis=None)"), prod(y**2, axis=None))
111
 
        # Check integers
112
 
        x = arange(10.)
113
 
        x = x.astype(int)
114
 
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
115
 
        assert_allclose(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
116
 
        # Check longs
117
 
        x = x.astype(long)
118
 
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
119
 
        assert_allclose(evaluate("prod(x**2+2,axis=0)"), prod(x**2+2,axis=0))
120
 
        # Check complex
121
 
        x = x + .1j
122
 
        assert_allclose(evaluate("sum(x**2+2,axis=0)"), sum(x**2+2,axis=0))
123
 
        assert_allclose(evaluate("prod(x-1,axis=0)"), prod(x-1,axis=0))
124
 
 
125
 
    def test_axis(self):
126
 
        y = arange(9.0).reshape(3,3)
127
 
        try:
128
 
            evaluate("sum(y, axis=2)")
129
 
        except ValueError:
130
 
            pass
131
 
        else:
132
 
            raise ValueError("should raise exception!")
133
 
        try:
134
 
            evaluate("sum(y, axis=-3)")
135
 
        except ValueError:
136
 
            pass
137
 
        else:
138
 
            raise ValueError("should raise exception!")
139
 
        try:
140
 
            # Negative axis are not supported
141
 
            evaluate("sum(y, axis=-1)")
142
 
        except ValueError:
143
 
            pass
144
 
        else:
145
 
            raise ValueError("should raise exception!")
146
 
 
147
 
    def test_r0_reuse(self):
148
 
        assert_equal(disassemble(NumExpr("x * x + 2", [('x', double)])),
149
 
                    [('mul_ddd', 'r0', 'r1[x]', 'r1[x]'),
150
 
                     ('add_ddd', 'r0', 'r0', 'c2[2.0]')])
151
 
 
152
 
 
153
 
class test_numexpr1(test_numexpr):
154
 
    """Testing with 1 thread"""
155
 
    nthreads = 1
156
 
 
157
 
class test_numexpr2(test_numexpr):
158
 
    """Testing with 2 threads"""
159
 
    nthreads = 2
160
 
 
161
 
 
162
 
class test_evaluate(TestCase):
163
 
    def test_simple(self):
164
 
        a = array([1., 2., 3.])
165
 
        b = array([4., 5., 6.])
166
 
        c = array([7., 8., 9.])
167
 
        x = evaluate("2*a + 3*b*c")
168
 
        assert_array_equal(x, array([  86.,  124.,  168.]))
169
 
 
170
 
    def test_simple_expr_small_array(self):
171
 
        x = arange(100.0)
172
 
        y = evaluate("x")
173
 
        assert_array_equal(x, y)
174
 
 
175
 
    def test_simple_expr(self):
176
 
        x = arange(1e6)
177
 
        y = evaluate("x")
178
 
        assert_array_equal(x, y)
179
 
 
180
 
    # Test for issue #37
181
 
    def test_zero_div(self):
182
 
        x = arange(100, dtype='i4')
183
 
        y = evaluate("1/x")
184
 
        x2 = zeros(100, dtype='i4')
185
 
        x2[1] = 1
186
 
        assert_array_equal(x2, y)
187
 
 
188
 
    # Test for issue #22
189
 
    def test_true_div(self):
190
 
        x = arange(10, dtype='i4')
191
 
        assert_array_equal(evaluate("x/2"), x / 2)
192
 
        assert_array_equal(evaluate("x/2", truediv=False), x / 2)
193
 
        assert_array_equal(evaluate("x/2", truediv='auto'), x / 2)
194
 
        assert_array_equal(evaluate("x/2", truediv=True), x / 2.0)
195
 
 
196
 
    # PyTables uses __nonzero__ among ExpressionNode objects internally
197
 
    # so this should be commented out for the moment.  See #24.
198
 
    def _test_boolean_operator(self):
199
 
        x = arange(10, dtype='i4')
200
 
        try:
201
 
            evaluate("(x > 1) and (x < 9)")
202
 
        except TypeError:
203
 
            pass
204
 
        else:
205
 
            raise ValueError("should raise exception!")
206
 
 
207
 
    def test_rational_expr(self):
208
 
        a = arange(1e6)
209
 
        b = arange(1e6) * 0.1
210
 
        x = (a + 2*b) / (1 + a + 4*b*b)
211
 
        y = evaluate("(a + 2*b) / (1 + a + 4*b*b)")
212
 
        assert_array_almost_equal(x, y)
213
 
 
214
 
    def test_complex_expr(self):
215
 
        def complex(a, b):
216
 
            c = zeros(a.shape, dtype=complex_)
217
 
            c.real = a
218
 
            c.imag = b
219
 
            return c
220
 
        a = arange(1e4)
221
 
        b = arange(1e4)**1e-5
222
 
        z = a + 1j*b
223
 
        x = z.imag
224
 
        x = sin(complex(a, b)).real + z.imag
225
 
        y = evaluate("sin(complex(a, b)).real + z.imag")
226
 
        assert_array_almost_equal(x, y)
227
 
 
228
 
    def test_complex_strides(self):
229
 
        a = arange(100).reshape(10,10)[::2]
230
 
        b = arange(50).reshape(5,10)
231
 
        assert_array_equal(evaluate("a+b"), a+b)
232
 
        c = empty([10], dtype=[('c1', int32), ('c2', uint16)])
233
 
        c['c1'] = arange(10)
234
 
        c['c2'].fill(0xaaaa)
235
 
        c1 = c['c1']
236
 
        a0 = a[0]
237
 
        assert_array_equal(evaluate("c1"), c1)
238
 
        assert_array_equal(evaluate("a0+c1"), a0+c1)
239
 
 
240
 
    def test_broadcasting(self):
241
 
        a = arange(100).reshape(10,10)[::2]
242
 
        c = arange(10)
243
 
        d = arange(5).reshape(5,1)
244
 
        assert_array_equal(evaluate("a+c"), a+c)
245
 
        assert_array_equal(evaluate("a+d"), a+d)
246
 
        expr = NumExpr("2.0*a+3.0*c",[('a', double),('c', double)])
247
 
        assert_array_equal(expr(a,c), 2.0*a+3.0*c)
248
 
 
249
 
    def test_all_scalar(self):
250
 
        a = 3.
251
 
        b = 4.
252
 
        assert_allclose(evaluate("a+b"), a+b)
253
 
        expr = NumExpr("2*a+3*b",[('a', double),('b', double)])
254
 
        assert_equal(expr(a,b), 2*a+3*b)
255
 
 
256
 
    def test_run(self):
257
 
        a = arange(100).reshape(10,10)[::2]
258
 
        b = arange(10)
259
 
        expr = NumExpr("2*a+3*b",[('a', double),('b', double)])
260
 
        assert_array_equal(expr(a,b), expr.run(a,b))
261
 
 
262
 
    def test_illegal_value(self):
263
 
        a = arange(3)
264
 
        try:
265
 
            evaluate("a < [0, 0, 0]")
266
 
        except TypeError:
267
 
            pass
268
 
        else:
269
 
            self.fail()
270
 
 
271
 
    # Execution order set here so as to not use too many threads
272
 
    # during the rest of the execution.  See #33 for details.
273
 
    def test_changing_nthreads_00_inc(self):
274
 
        a = linspace(-1, 1, 1e6)
275
 
        b = ((.25*a + .75)*a - 1.5)*a - 2
276
 
        for nthreads in range(1,7):
277
 
            numexpr.set_num_threads(nthreads)
278
 
            c = evaluate("((.25*a + .75)*a - 1.5)*a - 2")
279
 
            assert_array_almost_equal(b, c)
280
 
 
281
 
    def test_changing_nthreads_01_dec(self):
282
 
        a = linspace(-1, 1, 1e6)
283
 
        b = ((.25*a + .75)*a - 1.5)*a - 2
284
 
        for nthreads in range(6, 1, -1):
285
 
            numexpr.set_num_threads(nthreads)
286
 
            c = evaluate("((.25*a + .75)*a - 1.5)*a - 2")
287
 
            assert_array_almost_equal(b, c)
288
 
 
289
 
 
290
 
tests = [
291
 
('MISC', ['b*c+d*e',
292
 
          '2*a+3*b',
293
 
          '-a',
294
 
          'sinh(a)',
295
 
          '2*a + (cos(3)+5)*sinh(cos(b))',
296
 
          '2*a + arctan2(a, b)',
297
 
          'arcsin(0.5)',
298
 
          'where(a != 0.0, 2, a)',
299
 
          'where(a > 10, b < a, b > a)',
300
 
          'where((a-10).real != 0.0, a, 2)',
301
 
          '0.25 * (a < 5) + 0.33 * (a >= 5)',
302
 
          'cos(1+1)',
303
 
          '1+1',
304
 
          '1',
305
 
          'cos(a2)',
306
 
          ])]
307
 
 
308
 
optests = []
309
 
for op in list('+-*/%') + ['**']:
310
 
    optests.append("(a+1) %s (b+3)" % op)
311
 
    optests.append("3 %s (b+3)" % op)
312
 
    optests.append("(a+1) %s 4" % op)
313
 
    optests.append("2 %s (b+3)" % op)
314
 
    optests.append("(a+1) %s 2" % op)
315
 
    optests.append("(a+1) %s -1" % op)
316
 
    optests.append("(a+1) %s 0.5" % op)
317
 
tests.append(('OPERATIONS', optests))
318
 
 
319
 
cmptests = []
320
 
for op in ['<', '<=', '==', '>=', '>', '!=']:
321
 
    cmptests.append("a/2+5 %s b" % op)
322
 
    cmptests.append("a/2+5 %s 7" % op)
323
 
    cmptests.append("7 %s b" % op)
324
 
    cmptests.append("7.0 %s 5" % op)
325
 
tests.append(('COMPARISONS', cmptests))
326
 
 
327
 
func1tests = []
328
 
for func in ['copy', 'ones_like', 'sqrt',
329
 
             'sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan',
330
 
             'sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh',
331
 
             'log', 'log1p', 'log10', 'exp', 'expm1', 'abs']:
332
 
    func1tests.append("a + %s(b+c)" % func)
333
 
tests.append(('1_ARG_FUNCS', func1tests))
334
 
 
335
 
func2tests = []
336
 
for func in ['arctan2', 'fmod']:
337
 
    func2tests.append("a + %s(b+c, d+1)" % func)
338
 
    func2tests.append("a + %s(b+c, 1)" % func)
339
 
    func2tests.append("a + %s(1, d+1)" % func)
340
 
tests.append(('2_ARG_FUNCS', func2tests))
341
 
 
342
 
powtests = []
343
 
# n = -1, 0.5, 2, 4 already handled in section "OPERATIONS"
344
 
for n in (-7, -2.5, -1.5, -1.3, -.5, 0, 0.0, 1, 2.3, 2.5, 3):
345
 
    powtests.append("(a+1)**%s" % n)
346
 
tests.append(('POW_TESTS', powtests))
347
 
 
348
 
def equal(a, b, exact):
349
 
    if array_equal(a, b):
350
 
        return True
351
 
 
352
 
    if hasattr(a, 'dtype') and a.dtype in ['f4','f8']:
353
 
        nnans = isnan(a).sum()
354
 
        if nnans > 0:
355
 
            # For results containing NaNs, just check that the number
356
 
            # of NaNs is the same in both arrays.  This check could be
357
 
            # made more exhaustive, but checking element by element in
358
 
            # python space is very expensive in general.
359
 
            return nnans == isnan(b).sum()
360
 
        ninfs = isinf(a).sum()
361
 
        if ninfs > 0:
362
 
            # Ditto for Inf's
363
 
            return ninfs == isinf(b).sum()
364
 
    if exact:
365
 
        return (shape(a) == shape(b)) and alltrue(ravel(a) == ravel(b), axis=0)
366
 
    else:
367
 
        if hasattr(a, 'dtype') and a.dtype == 'f4':
368
 
            atol = 1e-5   # Relax precission for special opcodes, like fmod
369
 
        else:
370
 
            atol = 1e-8
371
 
        return (shape(a) == shape(b) and
372
 
                allclose(ravel(a), ravel(b), atol=atol))
373
 
 
374
 
class Skip(Exception): pass
375
 
 
376
 
def test_expressions():
377
 
    test_no = [0]
378
 
    def make_test_method(a, a2, b, c, d, e, x, expr,
379
 
                         test_scalar, dtype, optimization, exact, section):
380
 
        this_locals = locals()
381
 
        def method():
382
 
            # We don't want to listen at RuntimeWarnings like
383
 
            # "overflows" or "divide by zero".  Feel free to expand
384
 
            # the range for this filter, if needed.
385
 
            if dtype.__name__ == "float32" or 'arctanh' in expr:
386
 
                warnings.simplefilter("ignore")
387
 
                npval = eval(expr, globals(), this_locals)
388
 
                warnings.simplefilter("always")
389
 
            else:
390
 
                npval = eval(expr, globals(), this_locals)
391
 
            try:
392
 
                neval = evaluate(expr, local_dict=this_locals,
393
 
                                 optimization=optimization)
394
 
                assert equal(npval, neval, exact), """%r
395
 
(test_scalar=%r, dtype=%r, optimization=%r, exact=%r,
396
 
 npval=%r (%r - %r)\n neval=%r (%r - %r))""" % (expr, test_scalar, dtype.__name__,
397
 
                                     optimization, exact,
398
 
                                     npval, type(npval), shape(npval),
399
 
                                     neval, type(neval), shape(neval))
400
 
            except AssertionError:
401
 
                raise
402
 
            except NotImplementedError:
403
 
                print('%r not implemented for %s (scalar=%d, opt=%s)'
404
 
                      % (expr, dtype.__name__, test_scalar, optimization))
405
 
            except:
406
 
                print('numexpr error for expression %r' % (expr,))
407
 
                raise
408
 
        method.description = ('test_expressions(%s, test_scalar=%r, '
409
 
                              'dtype=%r, optimization=%r, exact=%r)') \
410
 
                    % (expr, test_scalar, dtype.__name__, optimization, exact)
411
 
        test_no[0] += 1
412
 
        method.__name__ = 'test_scalar%d_%s_%s_%s_%04d' % (test_scalar,
413
 
                                                           dtype.__name__,
414
 
                                                           optimization,
415
 
                                                           section,
416
 
                                                           test_no[0])
417
 
        return method
418
 
    x = None
419
 
    for test_scalar in (0, 1, 2):
420
 
        for dtype in (int, long, numpy.float32, double, complex):
421
 
            array_size = 100
422
 
            a = arange(2*array_size, dtype=dtype)[::2]
423
 
            a2 = zeros([array_size, array_size], dtype=dtype)
424
 
            b = arange(array_size, dtype=dtype) / array_size
425
 
            c = arange(array_size, dtype=dtype)
426
 
            d = arange(array_size, dtype=dtype)
427
 
            e = arange(array_size, dtype=dtype)
428
 
            if dtype == complex:
429
 
                a = a.real
430
 
                for x in [a2, b, c, d, e]:
431
 
                    x += 1j
432
 
                    x *= 1+1j
433
 
            if test_scalar == 1:
434
 
                a = a[array_size / 2]
435
 
            if test_scalar == 2:
436
 
                b = b[array_size / 2]
437
 
            for optimization, exact in [
438
 
                ('none', False), ('moderate', False), ('aggressive', False)]:
439
 
                for section_name, section_tests in tests:
440
 
                    for expr in section_tests:
441
 
                        if dtype == complex and (
442
 
                               '<' in expr or '>' in expr or '%' in expr
443
 
                               or "arctan2" in expr or "fmod" in expr):
444
 
                            # skip complex comparisons or functions not
445
 
                            # defined in complex domain.
446
 
                            continue
447
 
                        if (dtype in (int, long) and test_scalar and
448
 
                            expr == '(a+1) ** -1'):
449
 
                            continue
450
 
 
451
 
                        m = make_test_method(a, a2, b, c, d, e, x,
452
 
                                             expr, test_scalar, dtype,
453
 
                                             optimization, exact,
454
 
                                             section_name)
455
 
                        yield m
456
 
 
457
 
class test_int64(TestCase):
458
 
    def test_neg(self):
459
 
        a = array([2**31-1, 2**31, 2**32, 2**63-1], dtype=int64)
460
 
        res = evaluate('-a')
461
 
        assert_array_equal(res, [1-2**31, -(2**31), -(2**32), 1-2**63])
462
 
        self.assertEqual(res.dtype.name, 'int64')
463
 
 
464
 
class test_int32_int64(TestCase):
465
 
    def test_small_long(self):
466
 
        # Small longs should not be downgraded to ints.
467
 
        res = evaluate('42L')
468
 
        assert_array_equal(res, 42)
469
 
        self.assertEqual(res.dtype.name, 'int64')
470
 
 
471
 
    def test_big_int(self):
472
 
        # Big ints should be promoted to longs.
473
 
        # This test may only fail under 64-bit platforms.
474
 
        res = evaluate('2**40')
475
 
        assert_array_equal(res, 2**40)
476
 
        self.assertEqual(res.dtype.name, 'int64')
477
 
 
478
 
    def test_long_constant_promotion(self):
479
 
        int32array = arange(100, dtype='int32')
480
 
        res = int32array * 2
481
 
        res32 = evaluate('int32array * 2')
482
 
        res64 = evaluate('int32array * 2L')
483
 
        assert_array_equal(res, res32)
484
 
        assert_array_equal(res, res64)
485
 
        self.assertEqual(res32.dtype.name, 'int32')
486
 
        self.assertEqual(res64.dtype.name, 'int64')
487
 
 
488
 
    def test_int64_array_promotion(self):
489
 
        int32array = arange(100, dtype='int32')
490
 
        int64array = arange(100, dtype='int64')
491
 
        respy = int32array * int64array
492
 
        resnx = evaluate('int32array * int64array')
493
 
        assert_array_equal(respy, resnx)
494
 
        self.assertEqual(resnx.dtype.name, 'int64')
495
 
 
496
 
 
497
 
class test_uint32_int64(TestCase):
498
 
    def test_small_uint32(self):
499
 
        # Small uint32 should not be downgraded to ints.
500
 
        a = numpy.uint32(42)
501
 
        res = evaluate('a')
502
 
        assert_array_equal(res, 42)
503
 
        self.assertEqual(res.dtype.name, 'int64')
504
 
 
505
 
    def test_uint32_constant_promotion(self):
506
 
        int32array = arange(100, dtype='int32')
507
 
        a = numpy.uint32(2)
508
 
        res = int32array * a
509
 
        res32 = evaluate('int32array * 2')
510
 
        res64 = evaluate('int32array * a')
511
 
        assert_array_equal(res, res32)
512
 
        assert_array_equal(res, res64)
513
 
        self.assertEqual(res32.dtype.name, 'int32')
514
 
        self.assertEqual(res64.dtype.name, 'int64')
515
 
 
516
 
    def test_int64_array_promotion(self):
517
 
        uint32array = arange(100, dtype='uint32')
518
 
        int64array = arange(100, dtype='int64')
519
 
        respy = uint32array * int64array
520
 
        resnx = evaluate('uint32array * int64array')
521
 
        assert_array_equal(respy, resnx)
522
 
        self.assertEqual(resnx.dtype.name, 'int64')
523
 
 
524
 
 
525
 
class test_strings(TestCase):
526
 
    BLOCK_SIZE1 = 128
527
 
    BLOCK_SIZE2 = 8
528
 
    str_list1 = ['foo', 'bar', '', '  ']
529
 
    str_list2 = ['foo', '', 'x', ' ']
530
 
    str_nloops = len(str_list1) * (BLOCK_SIZE1 + BLOCK_SIZE2 + 1)
531
 
    str_array1 = array(str_list1 * str_nloops)
532
 
    str_array2 = array(str_list2 * str_nloops)
533
 
    str_constant = 'doodoo'
534
 
 
535
 
    def test_null_chars(self):
536
 
        str_list = [
537
 
            '\0\0\0', '\0\0foo\0', '\0\0foo\0b', '\0\0foo\0b\0',
538
 
            'foo\0', 'foo\0b', 'foo\0b\0', 'foo\0bar\0baz\0\0' ]
539
 
        for s in str_list:
540
 
            r = evaluate('s')
541
 
            self.assertEqual(s, r.tostring())  # check *all* stored data
542
 
 
543
 
    def test_compare_copy(self):
544
 
        sarr = self.str_array1
545
 
        expr = 'sarr'
546
 
        res1 = eval(expr)
547
 
        res2 = evaluate(expr)
548
 
        assert_array_equal(res1, res2)
549
 
 
550
 
    def test_compare_array(self):
551
 
        sarr1 = self.str_array1
552
 
        sarr2 = self.str_array2
553
 
        expr = 'sarr1 >= sarr2'
554
 
        res1 = eval(expr)
555
 
        res2 = evaluate(expr)
556
 
        assert_array_equal(res1, res2)
557
 
 
558
 
    def test_compare_variable(self):
559
 
        sarr = self.str_array1
560
 
        svar = self.str_constant
561
 
        expr = 'sarr >= svar'
562
 
        res1 = eval(expr)
563
 
        res2 = evaluate(expr)
564
 
        assert_array_equal(res1, res2)
565
 
 
566
 
    def test_compare_constant(self):
567
 
        sarr = self.str_array1
568
 
        expr = 'sarr >= %r' % self.str_constant
569
 
        res1 = eval(expr)
570
 
        res2 = evaluate(expr)
571
 
        assert_array_equal(res1, res2)
572
 
 
573
 
    def test_add_string_array(self):
574
 
        sarr1 = self.str_array1
575
 
        sarr2 = self.str_array2
576
 
        expr = 'sarr1 + sarr2'
577
 
        self.assert_missing_op('add_sss', expr, locals())
578
 
 
579
 
    def test_add_numeric_array(self):
580
 
        sarr = self.str_array1
581
 
        narr = arange(len(sarr), dtype='int32')
582
 
        expr = 'sarr >= narr'
583
 
        self.assert_missing_op('ge_bsi', expr, locals())
584
 
 
585
 
    def assert_missing_op(self, op, expr, local_dict):
586
 
        msg = "expected NotImplementedError regarding '%s'" % op
587
 
        try:
588
 
            evaluate(expr, local_dict)
589
 
        except NotImplementedError, nie:
590
 
            if "'%s'" % op not in nie.args[0]:
591
 
                self.fail(msg)
592
 
        else:
593
 
            self.fail(msg)
594
 
 
595
 
    def test_compare_prefix(self):
596
 
        # Check comparing two strings where one is a prefix of the
597
 
        # other.
598
 
        for s1, s2 in [ ('foo', 'foobar'), ('foo', 'foo\0bar'),
599
 
                        ('foo\0a', 'foo\0bar') ]:
600
 
            self.assert_(evaluate('s1 < s2'))
601
 
            self.assert_(evaluate('s1 <= s2'))
602
 
            self.assert_(evaluate('~(s1 == s2)'))
603
 
            self.assert_(evaluate('~(s1 >= s2)'))
604
 
            self.assert_(evaluate('~(s1 > s2)'))
605
 
 
606
 
        # Check for NumPy array-style semantics in string equality.
607
 
        s1, s2 = 'foo', 'foo\0\0'
608
 
        self.assert_(evaluate('s1 == s2'))
609
 
 
610
 
# Case for testing selections in fields which are aligned but whose
611
 
# data length is not an exact multiple of the length of the record.
612
 
# The following test exposes the problem only in 32-bit machines,
613
 
# because in 64-bit machines 'c2' is unaligned.  However, this should
614
 
# check most platforms where, while not unaligned, 'len(datatype) >
615
 
# boundary_alignment' is fullfilled.
616
 
class test_irregular_stride(TestCase):
617
 
    def test_select(self):
618
 
        f0 = arange(10, dtype=int32)
619
 
        f1 = arange(10, dtype=float64)
620
 
 
621
 
        irregular = rec.fromarrays([f0, f1])
622
 
 
623
 
        f0 = irregular['f0']
624
 
        f1 = irregular['f1']
625
 
 
626
 
        i0 = evaluate('f0 < 5')
627
 
        i1 = evaluate('f1 < 5')
628
 
 
629
 
        assert_array_equal(f0[i0], arange(5, dtype=int32))
630
 
        assert_array_equal(f1[i1], arange(5, dtype=float64))
631
 
 
632
 
# Cases for testing arrays with dimensions that can be zero.
633
 
class test_zerodim(TestCase):
634
 
 
635
 
    def test_zerodim1d(self):
636
 
        a0 = array([], dtype=int32)
637
 
        a1 = array([], dtype=float64)
638
 
 
639
 
        r0 = evaluate('a0 + a1')
640
 
        r1 = evaluate('a0 * a1')
641
 
 
642
 
        assert_array_equal(r0, a1)
643
 
        assert_array_equal(r1, a1)
644
 
 
645
 
    def test_zerodim3d(self):
646
 
        a0 = array([], dtype=int32).reshape(0,2,4)
647
 
        a1 = array([], dtype=float64).reshape(0,2,4)
648
 
 
649
 
        r0 = evaluate('a0 + a1')
650
 
        r1 = evaluate('a0 * a1')
651
 
 
652
 
        assert_array_equal(r0, a1)
653
 
        assert_array_equal(r1, a1)
654
 
 
655
 
# Case test for threads
656
 
class test_threading(TestCase):
657
 
    def test_thread(self):
658
 
        import threading
659
 
        class ThreadTest(threading.Thread):
660
 
            def run(self):
661
 
                a = arange(3)
662
 
                assert_array_equal(evaluate('a**3'), array([0, 1, 8]))
663
 
        test = ThreadTest()
664
 
        test.start()
665
 
 
666
 
# The worker function for the subprocess (needs to be here because Windows
667
 
# has problems pickling nested functions with the multiprocess module :-/)
668
 
def _worker(qout = None):
669
 
    ra = numpy.arange(1e3)
670
 
    rows = evaluate('ra > 0')
671
 
    #print "Succeeded in evaluation!\n"
672
 
    if qout is not None:
673
 
        qout.put("Done")
674
 
 
675
 
# Case test for subprocesses (via multiprocessing module)
676
 
class test_subprocess(TestCase):
677
 
    def test_multiprocess(self):
678
 
        try:
679
 
            import multiprocessing as mp
680
 
        except ImportError:
681
 
            return
682
 
        # Check for two threads at least
683
 
        numexpr.set_num_threads(2)
684
 
        #print "**** Running from main process:"
685
 
        _worker()
686
 
        #print "**** Running from subprocess:"
687
 
        qout = mp.Queue()
688
 
        ps = mp.Process(target=_worker, args=(qout,))
689
 
        ps.daemon = True
690
 
        ps.start()
691
 
 
692
 
        result = qout.get()
693
 
        #print result
694
 
 
695
 
 
696
 
 
697
 
 
698
 
def print_versions():
699
 
    """Print the versions of software that numexpr relies on."""
700
 
    if numpy.__version__ < minimum_numpy_version:
701
 
        print "*Warning*: NumPy version is lower than recommended: %s < %s" % \
702
 
              (numpy.__version__, minimum_numpy_version)
703
 
    print '-=' * 38
704
 
    print "Numexpr version:   %s" % numexpr.__version__
705
 
    print "NumPy version:     %s" % numpy.__version__
706
 
    print 'Python version:    %s' % sys.version
707
 
    if os.name == 'posix':
708
 
        (sysname, nodename, release, version, machine) = os.uname()
709
 
        print 'Platform:          %s-%s' % (sys.platform, machine)
710
 
    print "AMD/Intel CPU?     %s" % numexpr.is_cpu_amd_intel
711
 
    print "VML available?     %s" % use_vml
712
 
    if use_vml:
713
 
        print "VML/MKL version:   %s" % numexpr.get_vml_version()
714
 
    print 'Detected cores:    %s' % numexpr.ncores
715
 
    print '-=' * 38
716
 
 
717
 
 
718
 
def test():
719
 
    """
720
 
    Run all the tests in the test suite.
721
 
    """
722
 
 
723
 
    print_versions()
724
 
    unittest.TextTestRunner().run(suite())
725
 
test.__test__ = False
726
 
 
727
 
 
728
 
def suite():
729
 
    import unittest
730
 
 
731
 
    theSuite = unittest.TestSuite()
732
 
    niter = 1
733
 
 
734
 
    class TestExpressions(TestCase):
735
 
        pass
736
 
 
737
 
    def add_method(func):
738
 
        def method(self):
739
 
            return func()
740
 
        setattr(TestExpressions, func.__name__,
741
 
                new.instancemethod(method, None, TestExpressions))
742
 
 
743
 
    for func in test_expressions():
744
 
        add_method(func)
745
 
 
746
 
    for n in range(niter):
747
 
        theSuite.addTest(unittest.makeSuite(test_numexpr1))
748
 
        theSuite.addTest(unittest.makeSuite(test_numexpr2))
749
 
        theSuite.addTest(unittest.makeSuite(test_evaluate))
750
 
        theSuite.addTest(unittest.makeSuite(TestExpressions))
751
 
        theSuite.addTest(unittest.makeSuite(test_int32_int64))
752
 
        theSuite.addTest(unittest.makeSuite(test_uint32_int64))
753
 
        theSuite.addTest(unittest.makeSuite(test_strings))
754
 
        theSuite.addTest(
755
 
            unittest.makeSuite(test_irregular_stride) )
756
 
        theSuite.addTest(unittest.makeSuite(test_zerodim))
757
 
        theSuite.addTest(unittest.makeSuite(test_subprocess))
758
 
        # I need to put this test after test_subprocess because
759
 
        # if not, the test suite locks immediately before test_subproces.
760
 
        # This only happens with Windows, so I suspect of a subtle bad
761
 
        # interaction with threads and subprocess :-/
762
 
        theSuite.addTest(unittest.makeSuite(test_threading))
763
 
 
764
 
    return theSuite
765
 
 
766
 
if __name__ == '__main__':
767
 
    print_versions()
768
 
    unittest.main(defaultTest = 'suite')
769
 
#    suite = suite()
770
 
#    unittest.TextTestRunner(verbosity=2).run(suite)