-
Notifications
You must be signed in to change notification settings - Fork 9
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
gh-440: remove scipy as a test dependency #462
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
numpy | ||
--only-binary numpy | ||
|
||
scipy | ||
--only-binary scipy | ||
|
||
healpy | ||
--only-binary healpy | ||
|
||
|
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 |
---|---|---|
|
@@ -61,7 +61,6 @@ test = [ | |
"pytest-doctestplus", | ||
"pytest-mock", | ||
"pytest-rerunfailures", | ||
"scipy", | ||
] | ||
|
||
[project.urls] | ||
|
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 |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import importlib.util | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from glass.core.algorithm import nnls as nnls_glass | ||
|
||
# check if scipy is available for testing | ||
HAVE_SCIPY = importlib.util.find_spec("scipy") is not None | ||
from glass.core.algorithm import nnls | ||
|
||
|
||
@pytest.mark.skipif(not HAVE_SCIPY, reason="test requires SciPy") | ||
def test_nnls(rng: np.random.Generator) -> None: | ||
from scipy.optimize import nnls as nnls_scipy | ||
|
||
# cross-check output with scipy's nnls | ||
# check output | ||
|
||
a = rng.standard_normal((100, 20)) | ||
b = rng.standard_normal((100,)) | ||
a = np.arange(25.0).reshape(-1, 5) | ||
b = np.arange(5.0) | ||
y = a @ b | ||
res = nnls(a, y) | ||
assert np.linalg.norm((a @ res) - y) < 1e-7 | ||
|
||
x_glass = nnls_glass(a, b) | ||
x_scipy, _ = nnls_scipy(a, b) | ||
|
||
np.testing.assert_allclose(x_glass, x_scipy) | ||
a = rng.uniform(low=-10, high=10, size=[50, 10]) | ||
b = np.abs(rng.uniform(low=-2, high=2, size=[10])) | ||
b[::2] = 0 | ||
x = a @ b | ||
res = nnls(a, x, tol=500 * np.linalg.norm(a, 1) * np.spacing(1.0)) | ||
np.testing.assert_allclose(res, b, rtol=0.0, atol=1e-10) | ||
|
||
# check matrix and vector's shape | ||
|
||
a = rng.standard_normal((100, 20)) | ||
b = rng.standard_normal((100,)) | ||
|
||
with pytest.raises(ValueError, match="input `a` is not a matrix"): | ||
nnls_glass(b, a) | ||
nnls(b, a) | ||
with pytest.raises(ValueError, match="input `b` is not a vector"): | ||
nnls_glass(a, a) | ||
nnls(a, a) | ||
with pytest.raises(ValueError, match="the shapes of `a` and `b` do not match"): | ||
nnls_glass(a.T, b) | ||
nnls(a.T, b) |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import importlib.util | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
import pytest | ||
|
@@ -12,9 +10,6 @@ | |
trapezoid_product, | ||
) | ||
|
||
# check if scipy is available for testing | ||
HAVE_SCIPY = importlib.util.find_spec("scipy") is not None | ||
|
||
|
||
def test_broadcast_first() -> None: | ||
a = np.ones((2, 3, 4)) | ||
|
@@ -156,50 +151,47 @@ def test_trapezoid_product() -> None: | |
np.testing.assert_allclose(s, 1.0) | ||
|
||
|
||
@pytest.mark.skipif(not HAVE_SCIPY, reason="test requires SciPy") | ||
def test_cumulative_trapezoid() -> None: | ||
import scipy.integrate as spi | ||
|
||
# 1D f and x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scipy just compares the result with the actual values - https://github.com/scipy/scipy/blob/1d57f315d9454446d333d37d19f2a44f4baf35c9/scipy/integrate/tests/test_quadrature.py#L183 |
||
|
||
f = np.array([1, 2, 3, 4]) | ||
x = np.array([0, 1, 2, 3]) | ||
|
||
# default dtype (int - not supported by scipy) | ||
# default dtype (int) | ||
|
||
glass_ct = cumulative_trapezoid(f, x) | ||
np.testing.assert_allclose(glass_ct, np.array([0, 1, 4, 7])) | ||
ct = cumulative_trapezoid(f, x) | ||
np.testing.assert_allclose(ct, np.array([0, 1, 4, 7])) | ||
|
||
# explicit dtype (float) | ||
|
||
glass_ct = cumulative_trapezoid(f, x, dtype=float) | ||
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0) | ||
np.testing.assert_allclose(glass_ct, scipy_ct) | ||
ct = cumulative_trapezoid(f, x, dtype=float) | ||
np.testing.assert_allclose(ct, np.array([0.0, 1.5, 4.0, 7.5])) | ||
|
||
# explicit return array | ||
|
||
result = cumulative_trapezoid(f, x, dtype=float, out=np.zeros((4,))) | ||
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0) | ||
np.testing.assert_allclose(result, scipy_ct) | ||
out = np.zeros((4,)) | ||
ct = cumulative_trapezoid(f, x, dtype=float, out=out) | ||
np.testing.assert_equal(ct, out) | ||
|
||
# 2D f and 1D x | ||
|
||
f = np.array([[1, 4, 9, 16], [2, 3, 5, 7]]) | ||
x = np.array([0, 1, 2.5, 4]) | ||
|
||
# default dtype (int - not supported by scipy) | ||
# default dtype (int) | ||
|
||
glass_ct = cumulative_trapezoid(f, x) | ||
np.testing.assert_allclose(glass_ct, np.array([[0, 2, 12, 31], [0, 2, 8, 17]])) | ||
ct = cumulative_trapezoid(f, x) | ||
np.testing.assert_allclose(ct, np.array([[0, 2, 12, 31], [0, 2, 8, 17]])) | ||
|
||
# explicit dtype (float) | ||
|
||
glass_ct = cumulative_trapezoid(f, x, dtype=float) | ||
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0) | ||
np.testing.assert_allclose(glass_ct, scipy_ct) | ||
ct = cumulative_trapezoid(f, x, dtype=float) | ||
np.testing.assert_allclose( | ||
ct, np.array([[0.0, 2.5, 12.25, 31.0], [0.0, 2.5, 8.5, 17.5]]) | ||
) | ||
|
||
# explicit return array | ||
|
||
glass_ct = cumulative_trapezoid(f, x, dtype=float, out=np.zeros((2, 4))) | ||
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0) | ||
np.testing.assert_allclose(glass_ct, scipy_ct) | ||
out = np.zeros((2, 4)) | ||
ct = cumulative_trapezoid(f, x, dtype=float, out=out) | ||
np.testing.assert_equal(ct, out) |
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.
inspired by - https://github.com/scipy/scipy/blob/1d57f315d9454446d333d37d19f2a44f4baf35c9/scipy/optimize/tests/test_nnls.py#L7