Skip to content

Commit

Permalink
TST: weave tests: silence a large amount of test noise. Add BlitzWarn…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
rgommers committed Feb 16, 2014
1 parent 6901951 commit 2d86fa0
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 84 deletions.
2 changes: 1 addition & 1 deletion scipy/weave/__init__.py
Expand Up @@ -26,7 +26,7 @@
from .weave_version import weave_version as __version__

try:
from .blitz_tools import blitz
from .blitz_tools import blitz, BlitzWarning
except ImportError:
pass # scipy (core) wasn't available

Expand Down
25 changes: 13 additions & 12 deletions scipy/weave/blitz_tools.py
Expand Up @@ -2,20 +2,26 @@

import parser
import sys
import warnings
import copy

import numpy

from . import ast_tools
from . import slice_handler
from . import size_check
from . import converters

import numpy
import copy

from . import inline_tools
from .inline_tools import attempt_function_call
function_catalog = inline_tools.function_catalog
function_cache = inline_tools.function_cache


class BlitzWarning(UserWarning):
"""Warns about compilation failures etc."""
pass


def blitz(expr,local_dict=None, global_dict=None,check_size=1,verbose=0,**kw):
# this could call inline, but making a copy of the
# code here is more efficient for several reasons.
Expand Down Expand Up @@ -54,9 +60,6 @@ def blitz(expr,local_dict=None, global_dict=None,check_size=1,verbose=0,**kw):
expr_code = ast_to_blitz_expr(ast_list)
arg_names = ast_tools.harvest_variables(ast_list)
module_dir = global_dict.get('__file__',None)
#func = inline_tools.compile_function(expr_code,arg_names,
# local_dict,global_dict,
# module_dir,auto_downcast = 1)
func = inline_tools.compile_function(expr_code,arg_names,local_dict,
global_dict,module_dir,
compiler='gcc',auto_downcast=1,
Expand All @@ -67,14 +70,13 @@ def blitz(expr,local_dict=None, global_dict=None,check_size=1,verbose=0,**kw):
try:
results = attempt_function_call(expr,local_dict,global_dict)
except ValueError:
print('warning: compilation failed. Executing as python code')
warnings.warn('compilation failed. Executing as python code',
BlitzWarning)
exec(expr, global_dict, local_dict)


def ast_to_blitz_expr(ast_seq):
""" Convert an ast_sequence to a blitz expression.
"""

"""Convert an ast_sequence to a blitz expression."""
# Don't overwrite orignal sequence in call to transform slices.
ast_seq = copy.deepcopy(ast_seq)
slice_handler.transform_slices(ast_seq)
Expand Down Expand Up @@ -114,7 +116,6 @@ def test_function():
expr = "ex[:,1:,1:] = k + ca_x[:,1:,1:] * ex[:,1:,1:]" \
"+ cb_y_x[:,1:,1:] * (hz[:,1:,1:] - hz[:,:-1,1:])"\
"- cb_z_x[:,1:,1:] * (hy[:,1:,1:] - hy[:,1:,:-1])"
#ast = parser.suite('a = (b + c) * sin(d)')
ast = parser.suite(expr)
k = 1.
ex = numpy.ones((1,1,1),dtype=numpy.float32)
Expand Down
5 changes: 1 addition & 4 deletions scipy/weave/converters.py
@@ -1,5 +1,3 @@
""" converters.py
"""
from __future__ import absolute_import, print_function

from . import common_info
Expand Down Expand Up @@ -65,8 +63,7 @@
#----------------------------------------------------------------------------
# Blitz conversion classes
#
# same as default, but will convert numerix arrays to blitz C++ classes
# !! only available if numerix is installed !!
# same as default, but will convert numpy arrays to blitz C++ classes
#----------------------------------------------------------------------------
try:
from . import blitz_spec
Expand Down
56 changes: 24 additions & 32 deletions scipy/weave/tests/test_blitz_tools.py
Expand Up @@ -2,21 +2,23 @@

import os
import time
import parser
import warnings

from numpy import (float32, float64, complex64, complex128,
zeros, random, array, sum, abs, allclose)
zeros, random, array)

from numpy.testing import (TestCase, dec, assert_equal, assert_,
from numpy.testing import (TestCase, dec, assert_equal,
assert_allclose, run_module_suite)

from scipy.weave import blitz_tools, blitz
from scipy.weave import blitz_tools, blitz, BlitzWarning
from scipy.weave.ast_tools import harvest_variables
from weave_test_utils import empty_temp_dir, cleanup_temp_dir, remove_whitespace
from weave_test_utils import (empty_temp_dir, cleanup_temp_dir,
remove_whitespace, debug_print)


class TestAstToBlitzExpr(TestCase):
def generic_check(self,expr,desired):
import parser
ast = parser.suite(expr)
ast_list = ast.tolist()
actual = blitz_tools.ast_to_blitz_expr(ast_list)
Expand All @@ -25,7 +27,7 @@ def generic_check(self,expr,desired):
assert_equal(actual,desired,expr)

def test_simple_expr(self):
# convert simple expr to blitz: a[:1:2] = b[:1+i+2:]
# convert simple expr to blitz
expr = "a[:1:2] = b[:1+i+2:]"
desired = "a(blitz::Range(_beg,1-1,2))="\
"b(blitz::Range(_beg,1+i+2-1));"
Expand Down Expand Up @@ -67,35 +69,23 @@ def generic_check(self,expr,arg_dict,type,size,mod_location):
old_env = os.environ.get('PYTHONCOMPILED','')
os.environ['PYTHONCOMPILED'] = mod_location
blitz_tools.blitz(expr,arg_dict,{},verbose=0) # ,
# extra_compile_args = ['-O3','-malign-double','-funroll-loops'])
os.environ['PYTHONCOMPILED'] = old_env
t2 = time.time()
compiled = t2 - t1
actual = arg_dict['result']
# this really should give more info...
try:
# this isn't very stringent. Need to tighten this up and
# learn where failures are occurring.
assert_(allclose(abs(actual.ravel()),abs(desired.ravel()),1e-4,1e-6))
except:
diff = actual-desired
print(diff[:4,:4])
print(diff[:4,-4:])
print(diff[-4:,:4])
print(diff[-4:,-4:])
print(sum(abs(diff.ravel()),axis=0))
raise AssertionError
return standard,compiled
# TODO: this isn't very stringent. Need to tighten this up and
# learn where failures are occuring.
assert_allclose(abs(actual.ravel()), abs(desired.ravel()),
rtol=1e-4, atol=1e-6)
return standard, compiled

def generic_2d(self,expr,typ):
# The complex testing is pretty lame...
mod_location = empty_temp_dir()
import parser
ast = parser.suite(expr)
arg_list = harvest_variables(ast.tolist())
# print arg_list
all_sizes = [(10,10), (50,50), (100,100), (500,500), (1000,1000)]
print('\nExpression:', expr)
debug_print('\nExpression:', expr)
for size in all_sizes:
arg_dict = {}
for arg in arg_list:
Expand All @@ -105,23 +95,23 @@ def generic_2d(self,expr,typ):
arg_dict[arg].imag = arg_dict[arg].real
except:
pass
print('Run:', size,typ)
debug_print('Run:', size,typ)
standard,compiled = self.generic_check(expr,arg_dict,type,size,
mod_location)
try:
speed_up = standard/compiled
except:
speed_up = -1.
print("1st run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, "
"%3.4f" % (standard,compiled,speed_up))
debug_print("1st run(numpy,compiled,speed up): %3.4f, %3.4f, "
"%3.4f" % (standard,compiled,speed_up))
standard,compiled = self.generic_check(expr,arg_dict,type,size,
mod_location)
try:
speed_up = standard/compiled
except:
speed_up = -1.
print("2nd run(numpy.numerix,compiled,speed up): %3.4f, %3.4f, "
"%3.4f" % (standard,compiled,speed_up))
debug_print("2nd run(numpy,compiled,speed up): %3.4f, %3.4f, "
"%3.4f" % (standard,compiled,speed_up))
cleanup_temp_dir(mod_location)

@dec.slow
Expand All @@ -132,9 +122,11 @@ def test_5point_avg_2d_float(self):

@dec.slow
def test_5point_avg_2d_double(self):
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
self.generic_2d(expr,float64)
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=BlitzWarning)
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
self.generic_2d(expr,float64)

@dec.slow
def _check_5point_avg_2d_complex_float(self):
Expand Down
12 changes: 7 additions & 5 deletions scipy/weave/tests/test_c_spec.py
Expand Up @@ -17,6 +17,8 @@
from scipy.weave.build_tools import msvc_exists, gcc_exists
from scipy.weave.catalog import unique_file

from weave_test_utils import debug_print


def unique_mod(d,file_name):
f = os.path.basename(unique_file(d,file_name))
Expand Down Expand Up @@ -491,13 +493,13 @@ def test_speed(self):
t1 = time.time()
sum1 = with_cxx(a)
t2 = time.time()
print('speed test for list access')
print('compiler:', self.compiler)
print('scxx:', t2 - t1)
debug_print('speed test for list access')
debug_print('compiler:', self.compiler)
debug_print('scxx:', t2 - t1)
t1 = time.time()
sum2 = no_checking(a)
t2 = time.time()
print('C, no checking:', t2 - t1)
debug_print('C, no checking:', t2 - t1)
sum3 = 0
t1 = time.time()
for i in a:
Expand All @@ -506,7 +508,7 @@ def test_speed(self):
else:
sum3 -= i
t2 = time.time()
print('python:', t2 - t1)
debug_print('python:', t2 - t1)
assert_(sum1 == sum2 and sum1 == sum3)


Expand Down
10 changes: 6 additions & 4 deletions scipy/weave/tests/test_scxx_object.py
Expand Up @@ -10,6 +10,8 @@

from scipy.weave import inline_tools

from weave_test_utils import debug_print


class TestObjectConstruct(TestCase):
#------------------------------------------------------------------------
Expand Down Expand Up @@ -95,7 +97,7 @@ def test_stringio(self):
val.print(file_imposter);
"""
res = inline_tools.inline(code,['file_imposter'])
print(file_imposter.getvalue())
debug_print(file_imposter.getvalue())
assert_equal(file_imposter.getvalue(),"'how now brown cow'")

## @dec.slow
Expand Down Expand Up @@ -223,8 +225,8 @@ def test_inline(self):
except:
after2 = sys.getrefcount(a)

print("after and after2 should be equal in the following")
print('before, after, after2:', before, after, after2)
debug_print("after and after2 should be equal in the following")
debug_print('before, after, after2:', before, after, after2)

@dec.slow
def test_func(self):
Expand Down Expand Up @@ -777,7 +779,7 @@ def __hash__(self):

a = Foo()
res = inline_tools.inline('return_val = a.hash(); ',['a'])
print('hash:', res)
debug_print('hash:', res)
assert_equal(res,123)


Expand Down

0 comments on commit 2d86fa0

Please sign in to comment.