-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
implement numpy ufuncs #249
Open
andrewgsavage
wants to merge
8
commits into
lmfit:master
Choose a base branch
from
andrewgsavage:np
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b0a9c82
add ufloatnumpy
andrewgsavage a0bbea5
add ufloatnumpy
andrewgsavage e820830
merge
andrewgsavage 637e3d4
tests
andrewgsavage 15d2d07
pass tests
andrewgsavage 9ae0a0b
lint
andrewgsavage e928cbb
make AffineScalarFunc not require np
andrewgsavage f45193a
make AffineScalarFunc not require np
andrewgsavage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
from uncertainties import umath, ufloat | ||
from helpers import nominal_and_std_dev_close | ||
import numpy as np | ||
import pytest | ||
|
||
a = ufloat(1, 0.1) | ||
b = ufloat(2, 0.2) | ||
|
||
|
||
class TestArithmetic: | ||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, ufloat(3.0, 0.223606797749979)), | ||
(a, a, ufloat(2.0, 0.2)), | ||
], | ||
) | ||
def test_add(self, first, second, expected): | ||
result = first + second | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
result = np.add(first, second) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, ufloat(-1.00, 0.223606797749979)), | ||
(a, a, ufloat(0.0, 0.0)), | ||
], | ||
) | ||
def test_subtact(self, first, second, expected): | ||
result = first - second | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
result = np.subtract(first, second) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, ufloat(2.0, 0.28284271247461906)), | ||
(a, a, ufloat(1.0, 0.2)), | ||
], | ||
) | ||
def test_multiply(self, first, second, expected): | ||
result = first * second | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
result = np.multiply(first, second) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, ufloat(0.5, 0.07071067811865477)), | ||
(a, a, ufloat(1.0, 0.0)), | ||
], | ||
) | ||
def test_divide(self, first, second, expected): | ||
result = first / second | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
result = np.divide(first, second) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
result = np.true_divide(first, second) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, ufloat(0.0, 0.0)), | ||
(a, a, ufloat(1.0, 0.0)), | ||
], | ||
) | ||
def test_floor_divide(self, first, second, expected): | ||
result = first // second | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
result = np.floor_divide(first, second) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
|
||
class TestComparative: | ||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, False), | ||
(a, a, True), | ||
], | ||
) | ||
def test_equal(self, first, second, expected): | ||
result = first == second | ||
assert result == expected | ||
|
||
result = np.equal(first, second) | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, True), | ||
(a, a, False), | ||
], | ||
) | ||
def test_not_equal(self, first, second, expected): | ||
result = first != second | ||
assert result == expected | ||
|
||
result = np.not_equal(first, second) | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, True), | ||
(a, a, False), | ||
], | ||
) | ||
def test_less(self, first, second, expected): | ||
result = first < second | ||
assert result == expected | ||
|
||
result = np.less(first, second) | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, True), | ||
(a, a, True), | ||
], | ||
) | ||
def test_less_equal(self, first, second, expected): | ||
result = first <= second | ||
assert result == expected | ||
|
||
result = np.less_equal(first, second) | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, False), | ||
(a, a, False), | ||
], | ||
) | ||
def test_greater(self, first, second, expected): | ||
result = first > second | ||
assert result == expected | ||
|
||
result = np.greater(first, second) | ||
assert result == expected | ||
|
||
@pytest.mark.parametrize( | ||
"first, second, expected", | ||
[ | ||
(a, b, False), | ||
(a, a, True), | ||
], | ||
) | ||
def test_greater_equal(self, first, second, expected): | ||
result = first >= second | ||
assert result == expected | ||
|
||
result = np.greater_equal(first, second) | ||
assert result == expected | ||
|
||
|
||
class TestUfuncs: | ||
zero = ufloat(0.0, 0.1) | ||
one = ufloat(1.0, 0.1) | ||
pi_4 = ufloat(0.7853981633974483, 0.1) # pi/4 | ||
pi_2 = ufloat(1.5707963267948966, 0.1) # pi/2 | ||
|
||
@pytest.mark.parametrize( | ||
"numpy_func, umath_func, arg, expected", | ||
[ | ||
("cos", "cos", zero, ufloat(1.0, 0.0)), | ||
("cos", "cos", pi_4, ufloat(0.7071067811865476, 0.07071067811865477)), | ||
("cos", "cos", pi_2, ufloat(6.123233995736766e-17, 0.1)), | ||
("cosh", "cosh", zero, ufloat(1.0, 0.0)), | ||
("cosh", "cosh", pi_4, ufloat(1.324609089252006, 0.08686709614860096)), | ||
("cosh", "cosh", pi_2, ufloat(2.5091784786580567, 0.2301298902307295)), | ||
("sin", "sin", zero, ufloat(0.0, 0.1)), | ||
("sin", "sin", pi_4, ufloat(0.7071067811865476, 0.07071067811865477)), | ||
("sin", "sin", pi_2, ufloat(1.0, 6.123233995736766e-18)), | ||
("sinh", "sinh", zero, ufloat(0.0, 0.1)), | ||
("sinh", "sinh", pi_4, ufloat(0.8686709614860095, 0.1324609089252006)), | ||
("sinh", "sinh", pi_2, ufloat(2.3012989023072947, 0.2509178478658057)), | ||
("tan", "tan", zero, ufloat(0.0, 0.1)), | ||
("tan", "tan", pi_4, ufloat(0.9999999999999999, 0.19999999999999998)), | ||
("tan", "tan", pi_2, ufloat(1.633123935319537e16, 2.6670937881135717e31)), | ||
("tanh", "tanh", zero, ufloat(0.0, 0.1)), | ||
("tanh", "tanh", pi_4, ufloat(0.6557942026326724, 0.05699339637933774)), | ||
("tanh", "tanh", pi_2, ufloat(0.9171523356672744, 0.015883159318006324)), | ||
("arccos", "acos", zero, ufloat(1.5707963267948966, 0.1)), | ||
("arccos", "acos", one, ufloat(0.0, float("nan"))), | ||
("arccosh", "acosh", one, ufloat(0.0, float("nan"))), | ||
("arcsin", "asin", zero, ufloat(0.0, 0.1)), | ||
("arcsin", "asin", one, ufloat(1.5707963267948966, float("nan"))), | ||
("arcsinh", "asinh", zero, ufloat(0.0, 0.1)), | ||
("arcsinh", "asinh", one, ufloat(0.8813735870195429, 0.07071067811865475)), | ||
("arctan", "atan", zero, ufloat(0.0, 0.1)), | ||
("arctan", "atan", one, ufloat(0.7853981633974483, 0.05)), | ||
("arctanh", "atanh", zero, ufloat(0.0, 0.1)), | ||
("exp", "exp", zero, ufloat(1.0, 0.1)), | ||
("exp", "exp", one, ufloat(2.718281828459045, 0.27182818284590454)), | ||
("exp2", None, zero, ufloat(1.0, 0.06931471805599453)), | ||
("exp2", None, one, ufloat(2.0, 0.13862943611198905)), | ||
("expm1", "expm1", zero, ufloat(0.0, 0.1)), | ||
("expm1", "expm1", one, ufloat(1.718281828459045, 0.27182818284590454)), | ||
("log10", "log10", one, ufloat(0.0, 0.04342944819032518)), | ||
("log1p", "log1p", zero, ufloat(0.0, 0.1)), | ||
("log1p", "log1p", one, ufloat(0.6931471805599453, 0.05)), | ||
("degrees", "degrees", zero, ufloat(0.0, 5.729577951308233)), | ||
("degrees", "degrees", one, ufloat(57.29577951308232, 5.729577951308233)), | ||
("radians", "radians", zero, ufloat(0.0, 0.0017453292519943296)), | ||
( | ||
"radians", | ||
"radians", | ||
one, | ||
ufloat(0.017453292519943295, 0.0017453292519943296), | ||
), | ||
("rad2deg", "degrees", zero, ufloat(0.0, 5.729577951308233)), | ||
("rad2deg", "degrees", one, ufloat(57.29577951308232, 5.729577951308233)), | ||
("deg2rad", "radians", zero, ufloat(0.0, 0.0017453292519943296)), | ||
( | ||
"deg2rad", | ||
"radians", | ||
one, | ||
ufloat(0.017453292519943295, 0.0017453292519943296), | ||
), | ||
("sqrt", "sqrt", zero, ufloat(0.0, float("nan"))), | ||
("sqrt", "sqrt", one, ufloat(1.0, 0.05)), | ||
], | ||
) | ||
def test_single_arg(self, numpy_func, umath_func, arg, expected): | ||
func = getattr(np, numpy_func) | ||
result = func(arg) | ||
assert nominal_and_std_dev_close(result, expected) | ||
|
||
if umath_func: | ||
func = getattr(umath, umath_func) | ||
result = func(arg) | ||
assert nominal_and_std_dev_close(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes
AffineScalarFunc
and thus all ofuncertainties
dependent on numpy which I don't think has been discussed recently? The only previous discussion I can find is #47 where the conclusion seemed to be that improving numpy support should go intouarray
rather thanAffineScalarFunc
/UFloat
which makes sense to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @wshanks.
AffineScalarFunc
should not require numpy.It is a scalar, it does not need broadcasted ufuncs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is how to get np.sin(x) to work, removing the need for unumpy.sin
I meant to make UFloatNumpy an empty class when numpy is not installed. Got a bit trigger happy when finally getting the tests to pass! (The ufuncs get used when a
AffineScalarFunc
/UFloat
gets multiplied, added etc by a np.array containing anything)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also wouldn't need a umath.sin, although that may be worth keeping so users don't need to have numpy installed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewgsavage I have not looked in detail at this. I think I may not really understand your responses.
Are you saying that the thing called
UFloatNumpy
will not depend on numpy? That is going to be super-confusing. Should this class usenumpy
if available andmath
otherwise? Maybe call thatUMath
?Again agreeing with @wshanks please you give an overview of the design goals and concepts, or a link to an earlier discussion.