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

move tests to tests folder #211

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
11 changes: 2 additions & 9 deletions INSTALL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ or, if additional access rights are needed (Unix):

sudo python setup.py install

* The tests programs (test_*.py) are meant to be run through the Nose
testing framework. This can be achieved for instance with a command
* The tests programs (test_*.py) are meant to be run through pytest. This can be achieved for instance with a command
like

nosetests -sv uncertainties/

or simply

nosetests uncertainties/

(for a less verbose output).
pytest ./tests
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ Changelog = "https://github.com/lmfit/uncertainties/blob/master/CHANGES.rst"

[project.optional-dependencies]
optional = ["numpy"]

[tool.pytest.ini_options]
testpaths = ["tests"]
152 changes: 152 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
from uncertainties.core import ufloat
from math import isnan

def power_all_cases(op):
'''
Checks all cases for the value and derivatives of power-like
operator op (op is typically the built-in pow(), or math.pow()).

Checks only the details of special results like 0, 1 or NaN).

Different cases for the value of x**p and its derivatives are
tested by dividing the (x, p) plane with:

- x < 0, x = 0, x > 0
- p integer or not, p < 0, p = 0, p > 0

(not all combinations are distinct: for instance x > 0 gives
identical formulas for all p).
'''

zero = ufloat(0, 0.1)
zero2 = ufloat(0, 0.1)
one = ufloat(1, 0.1)
positive = ufloat(0.3, 0.01)
positive2 = ufloat(0.3, 0.01)
negative = ufloat(-0.3, 0.01)
integer = ufloat(-3, 0)
non_int_larger_than_one = ufloat(3.1, 0.01)
positive_smaller_than_one = ufloat(0.3, 0.01)

## negative**integer

result = op(negative, integer)
assert not isnan(result.derivatives[negative])
assert isnan(result.derivatives[integer])

# Limit cases:
result = op(negative, one)
assert result.derivatives[negative] == 1
assert isnan(result.derivatives[one])

result = op(negative, zero)
assert result.derivatives[negative] == 0
assert isnan(result.derivatives[zero])

## negative**non-integer

## zero**...

result = op(zero, non_int_larger_than_one)
assert isnan(result.derivatives[zero])
assert result.derivatives[non_int_larger_than_one] == 0

# Special cases:
result = op(zero, one)
assert result.derivatives[zero] == 1
assert result.derivatives[one] == 0

result = op(zero, 2*one)
assert result.derivatives[zero] == 0
assert result.derivatives[one] == 0

result = op(zero, positive_smaller_than_one)
assert isnan(result.derivatives[zero])
assert result.derivatives[positive_smaller_than_one] == 0

result = op(zero, zero2)
assert result.derivatives[zero] == 0
assert isnan(result.derivatives[zero2])

## positive**...: this is a quite regular case where the value and
## the derivatives are all defined.

result = op(positive, positive2)
assert not isnan(result.derivatives[positive])
assert not isnan(result.derivatives[positive2])

result = op(positive, zero)
assert result.derivatives[positive] == 0
assert not isnan(result.derivatives[zero])

result = op(positive, negative)
assert not isnan(result.derivatives[positive])
assert not isnan(result.derivatives[negative])


def power_special_cases(op):
'''
Checks special cases of the uncertainty power operator op (where
op is typically the built-in pow or uncertainties.umath.pow).

The values x = 0, x = 1 and x = NaN are special, as are null,
integral and NaN values of p.
'''

zero = ufloat(0, 0)
one = ufloat(1, 0)
p = ufloat(0.3, 0.01)

assert op(0, p) == 0
assert op(zero, p) == 0

# The outcome of 1**nan and nan**0 was undefined before Python
# 2.6 (http://docs.python.org/library/math.html#math.pow):
assert op(float('nan'), zero) == 1.0
assert op(one, float('nan')) == 1.0

# …**0 == 1.0:
assert op(p, 0) == 1.0
assert op(zero, 0) == 1.0
assert op((-p), 0) == 1.0
# …**zero:
assert op((-10.3), zero) == 1.0
assert op(0, zero) == 1.0
assert op(0.3, zero) == 1.0
assert op((-p), zero) == 1.0
assert op(zero, zero) == 1.0
assert op(p, zero) == 1.0

# one**… == 1.0
assert op(one, -3) == 1.0
assert op(one, -3.1) == 1.0
assert op(one, 0) == 1.0
assert op(one, 3) == 1.0
assert op(one, 3.1) == 1.0

# … with two numbers with uncertainties:
assert op(one, (-p)) == 1.0
assert op(one, zero) == 1.0
assert op(one, p) == 1.0
# 1**… == 1.0:
assert op(1., (-p)) == 1.0
assert op(1., zero) == 1.0
assert op(1., p) == 1.0

def power_wrt_ref(op, ref_op):
'''
Checks special cases of the uncertainty power operator op (where
op is typically the built-in pow or uncertainties.umath.pow), by
comparing its results to the reference power operator ref_op
(which is typically the built-in pow or math.pow).
'''

# Negative numbers with uncertainty can be exponentiated to an
# integral power:
assert op(ufloat(-1.1, 0.1), -9).nominal_value == ref_op(-1.1, -9)

# Case of numbers with no uncertainty: should give the same result
# as numbers with uncertainties:
assert op(ufloat(-1, 0), 9) == ref_op(-1, 9)
assert op(ufloat(-1.1, 0), 9) == ref_op(-1.1, 9)

10 changes: 1 addition & 9 deletions uncertainties/unumpy/test_ulinalg.py → tests/test_ulinalg.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
"""
Tests for uncertainties.unumpy.ulinalg.

These tests can be run through the Nose testing framework.

(c) 2010-2016 by Eric O. LEBIGOT (EOL) <[email protected]>.
"""

# Some tests are already performed in test_unumpy (unumpy contains a
# matrix inversion, for instance). They are not repeated here.

Expand All @@ -18,7 +10,7 @@
sys.exit() # There is no reason to test the interface to NumPy

from uncertainties import unumpy, ufloat
from uncertainties.unumpy.test_unumpy import arrays_close
from uncertainties.testing import arrays_close

def test_list_inverse():
"Test of the inversion of a square matrix"
Expand Down
43 changes: 15 additions & 28 deletions uncertainties/test_umath.py → tests/test_umath.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
"""
Tests of the code in uncertainties.umath.

These tests can be run through the Nose testing framework.

(c) 2010-2016 by Eric O. LEBIGOT (EOL).
"""

from __future__ import division
from __future__ import absolute_import

# Standard modules
import sys
import math
from math import isnan, isinf

# Local modules:
from uncertainties import ufloat
import uncertainties.core as uncert_core
import uncertainties.umath_core as umath_core

from . import test_uncertainties

from uncertainties.testing import compare_derivatives, numbers_close
from helpers import power_special_cases, power_all_cases, power_wrt_ref
###############################################################################
# Unit tests

Expand All @@ -31,13 +19,12 @@ def test_fixed_derivatives_math_funcs():
"""

for name in umath_core.many_scalars_to_scalar_funcs:
# print "Checking %s..." % name
func = getattr(umath_core, name)
# Numerical derivatives of func: the nominal value of func() results
# is used as the underlying function:
numerical_derivatives = uncert_core.NumericalDerivatives(
lambda *args: func(*args))
test_uncertainties.compare_derivatives(func, numerical_derivatives)
compare_derivatives(func, numerical_derivatives)

# Functions that are not in umath_core.many_scalars_to_scalar_funcs:

Expand All @@ -48,11 +35,11 @@ def frac_part_modf(x):
def int_part_modf(x):
return umath_core.modf(x)[1]

test_uncertainties.compare_derivatives(
compare_derivatives(
frac_part_modf,
uncert_core.NumericalDerivatives(
lambda x: frac_part_modf(x)))
test_uncertainties.compare_derivatives(
compare_derivatives(
int_part_modf,
uncert_core.NumericalDerivatives(
lambda x: int_part_modf(x)))
Expand All @@ -64,11 +51,11 @@ def mantissa_frexp(x):
def exponent_frexp(x):
return umath_core.frexp(x)[1]

test_uncertainties.compare_derivatives(
compare_derivatives(
mantissa_frexp,
uncert_core.NumericalDerivatives(
lambda x: mantissa_frexp(x)))
test_uncertainties.compare_derivatives(
compare_derivatives(
exponent_frexp,
uncert_core.NumericalDerivatives(
lambda x: exponent_frexp(x)))
Expand Down Expand Up @@ -172,7 +159,7 @@ def monte_carlo_calc(n_samples):
# or assert_array_max_ulp. This is relevant for all vectorized
# occurrences of numbers_close.

assert numpy.vectorize(test_uncertainties.numbers_close)(
assert numpy.vectorize(numbers_close)(
covariances_this_module,
covariances_samples,
0.06).all(), (
Expand All @@ -183,7 +170,7 @@ def monte_carlo_calc(n_samples):
)

# The nominal values must be close:
assert test_uncertainties.numbers_close(
assert numbers_close(
nominal_value_this_module,
nominal_value_samples,
# The scale of the comparison depends on the standard
Expand Down Expand Up @@ -279,14 +266,14 @@ def test_hypot():
# Derivatives that cannot be calculated simply return NaN, with no
# exception being raised, normally:
result = umath_core.hypot(x, y)
assert test_uncertainties.isnan(result.derivatives[x])
assert test_uncertainties.isnan(result.derivatives[y])
assert isnan(result.derivatives[x])
assert isnan(result.derivatives[y])

def test_power_all_cases():
'''
Test special cases of umath_core.pow().
'''
test_uncertainties.power_all_cases(umath_core.pow)
power_all_cases(umath_core.pow)

# test_power_special_cases() is similar to
# test_uncertainties.py:test_power_special_cases(), but with small
Expand All @@ -297,7 +284,7 @@ def test_power_special_cases():
Checks special cases of umath_core.pow().
'''

test_uncertainties.power_special_cases(umath_core.pow)
power_special_cases(umath_core.pow)

# We want the same behavior for numbers with uncertainties and for
# math.pow() at their nominal values.
Expand Down Expand Up @@ -341,4 +328,4 @@ def test_power_wrt_ref():
'''
Checks special cases of the umath_core.pow() power operator.
'''
test_uncertainties.power_wrt_ref(umath_core.pow, math.pow)
power_wrt_ref(umath_core.pow, math.pow)
Loading