Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coverage checking to the tests. #224

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ MANIFEST
*.so
*.c
autograd.egg-info/

# Unit test / coverage reports
htmlcov/
.coverage
.coverage.*
coverage.xml
*,cover
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ before_install:
- conda update --yes conda
- conda config --add channels conda-forge
install:
- conda install --yes python=$TRAVIS_PYTHON_VERSION pip numpy scipy nose future
- conda install --yes python=$TRAVIS_PYTHON_VERSION pip numpy scipy nose future coveralls
- pip install -v .
script: nosetests tests
after_success:
- coveralls
11 changes: 11 additions & 0 deletions autograd/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from copy import copy

EPS, RTOL, ATOL = 1e-4, 1e-4, 1e-6
COVERAGE_CHECKING = False

if COVERAGE_CHECKING:
import coverage
cover = coverage.Coverage()

def nd(f, *args):
unary_f = lambda x : f(*x)
Expand Down Expand Up @@ -54,12 +59,18 @@ def check_equivalent(A, B, rtol=RTOL, atol=ATOL):
A_flat - B_flat, A_flat, B_flat)

def check_grads(fun, *args):
if COVERAGE_CHECKING:
cover.load()
cover.start()
if not args:
raise Exception("No args given")
exact = tuple([grad(fun, i)(*args) for i in range(len(args))])
args = [float(x) if isinstance(x, int) else x for x in args]
numeric = nd(fun, *args)
check_equivalent(exact, numeric)
if COVERAGE_CHECKING:
cover.stop()
cover.save()

def to_scalar(x):
if isinstance(getval(x), list) or isinstance(getval(x), tuple):
Expand Down
6 changes: 6 additions & 0 deletions tests/numpy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from autograd.util import check_equivalent, check_grads, to_scalar
from builtins import range
import warnings
import coverage
cover = coverage.Coverage()

test_complex = True

Expand All @@ -21,6 +23,8 @@ def combo_check(fun, argnums, *args, **kwargs):
print(".", end=' ')

def check_fun_and_grads(fun, args, kwargs, argnums):
cover.load()
cover.start()
wrt_args = [args[i] for i in argnums]
try:
if isinstance(fun, primitive):
Expand Down Expand Up @@ -54,6 +58,8 @@ def d_scalar_fun(*args):
except:
print("Second derivative test failed! Args were", args, kwargs)
raise
cover.stop()
cover.save()

def stat_check(fun, test_complex=test_complex):
# Tests functions that compute statistics, like sum, mean, etc
Expand Down