Skip to content

Commit fe056d1

Browse files
authored
Merge pull request #641 from CUQI-DTU/remove_scipy_version
Update scipy version requirement and related issues
2 parents c18048b + 691a59f commit fe056d1

File tree

8 files changed

+27
-7
lines changed

8 files changed

+27
-7
lines changed

cuqi/distribution/_truncated_normal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _gradient(self, x, *args, **kwargs):
107107
"""
108108
# check if x falls in the range between np.array a and b
109109
if np.any(x < self.low) or np.any(x > self.high):
110-
return np.NaN*np.ones_like(x)
110+
return np.nan*np.ones_like(x)
111111
else:
112112
return self._normal.gradient(x, *args, **kwargs)
113113

cuqi/distribution/_uniform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def gradient(self, x):
4747
Computes the gradient of logpdf at the given values of x.
4848
"""
4949
if np.any(x < self.low) or np.any(x > self.high):
50-
return np.NaN*np.ones_like(x)
50+
return np.nan*np.ones_like(x)
5151
else:
5252
return np.zeros_like(x)
5353

cuqi/implicitprior/_regularizedGaussian.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,18 @@ def _parse_preset_constraint_input(self, constraint, optional_regularization_par
189189
elif c_lower == "increasing":
190190
if not isinstance(self.geometry, Continuous1D):
191191
raise ValueError("Geometry not supported for " + c_lower)
192-
self._constraint_prox = lambda z, _: spoptimize.isotonic_regression(z, increasing=True).x
192+
if hasattr(spoptimize, 'isotonic_regression'):
193+
self._constraint_prox = lambda z, _: spoptimize.isotonic_regression(z, increasing=True).x
194+
else:
195+
raise AttributeError(f"The function 'isotonic_regression' does not exist in scipy.optimize. Installed scipy version: {spoptimize.__version__}. You need to install a scipy >= 1.12.0")
193196
self._preset["constraint"] = "increasing"
194197
elif c_lower == "decreasing":
195198
if not isinstance(self.geometry, Continuous1D):
196199
raise ValueError("Geometry not supported for " + c_lower)
197-
self._constraint_prox = lambda z, _: spoptimize.isotonic_regression(z, increasing=False).x
200+
if hasattr(spoptimize, 'isotonic_regression'):
201+
self._constraint_prox = lambda z, _: spoptimize.isotonic_regression(z, increasing=False).x
202+
else:
203+
raise AttributeError(f"The function 'isotonic_regression' does not exist in scipy.optimize. Installed scipy version: {spoptimize.__version__}. You need to install a scipy >= 1.12.0")
198204
self._preset["constraint"] = "decreasing"
199205
elif c_lower == "convex":
200206
if not isinstance(self.geometry, Continuous1D):

cuqi/utilities/_utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def approx_derivative(func, wrt, direction=None, epsilon=np.sqrt(np.finfo(float)
188188
# We compute the Jacobian matrix of func using forward differences.
189189
# If the function is scalar-valued, we compute the gradient instead.
190190
# If the direction is provided, we compute the direction-Jacobian product.
191-
wrt = np.asfarray(wrt)
191+
wrt = np.asarray(wrt)
192192
f0 = func(wrt)
193193
Matr = np.zeros([infer_len(wrt), infer_len(f0)])
194194
dx = np.zeros(len(wrt))

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
matplotlib
22
numpy>=1.17.0
3-
scipy<1.13
3+
scipy==1.12.0; python_version <= '3.9'
4+
scipy; python_version > '3.9'
45
arviz
56
tqdm

tests/test_bayesian_inversion.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import pytest
44
import numpy as np
5+
import scipy
56
import sys
7+
from packaging import version
68

79
from cuqi.testproblem import Deconvolution1D
810
from cuqi.distribution import Gaussian, GMRF, CMRF, LMRF, Gamma
@@ -25,6 +27,14 @@ def test_TP_BayesianProblem_sample(copy_reference, TP_type, phantom, prior, Ns,
2527
# SKIP NUTS test if not windows (for now)
2628
if isinstance(prior, CMRF) and not sys.platform.startswith('win'):
2729
pytest.skip("NUTS(CMRF) regression test is not implemented for this platform")
30+
elif (
31+
isinstance(prior, RegularizedGaussian)
32+
or isinstance(prior, RegularizedGMRF)
33+
) and version.parse(scipy.__version__) >= version.parse('1.13.0'):
34+
pytest.skip(
35+
"Saved reference data is not consistent with scipy>=1.13.0 "
36+
"(due to different default behaviour of scipy.optimize)"
37+
)
2838

2939
np.random.seed(19937)
3040

tests/test_implicit_priors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import cuqi
22
import numpy as np
3+
import scipy
34
import pytest
5+
from packaging import version
46

57
def test_RegularizedGaussian_default_init():
68
""" Test that the implicit regularized Gaussian requires at least 1 regularization argument """
@@ -186,6 +188,7 @@ def test_RegularizedGaussian_double_preset():
186188
assert len(x.proximal[0]) == 2
187189
assert len(x.proximal[1]) == 2
188190

191+
@pytest.mark.skipif(version.parse(scipy.__version__) < version.parse('1.12.0'), reason="isotonic_regression in not available in scipy < 1.12.0")
189192
def test_regression_increasing():
190193
""" Regression test for the increasing constraints in RegularizedGaussian"""
191194

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_LinearModel_getMatrix(seed):
1919
mat1 = model1.get_matrix() #Normal matrix
2020
mat2 = model2.get_matrix() #Sparse matrix (generated from functions)
2121

22-
assert np.allclose(mat1,mat2.A)
22+
assert np.allclose(mat1,mat2.toarray())
2323

2424
def test_initialize_model_dim():
2525
model1 = cuqi.model.Model(lambda x:x, range_geometry=4, domain_geometry=4)

0 commit comments

Comments
 (0)