Skip to content

Commit

Permalink
Merge pull request #1505 from pints-team/i612-toy-inputs
Browse files Browse the repository at this point in the history
I612 toy inputs issue
  • Loading branch information
chonlei authored Nov 7, 2023
2 parents ea408ed + bf12ae1 commit 107af2f
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
### Deprecated
### Removed
### Fixed
- [#1505](https://github.com/pints-team/pints/pull/1505) Fixed issues with toy problems that accept invalid inputs.


## [0.5.0] - 2023-07-27
Expand Down
Empty file modified pints/tests/test_abc_rejection.py
100644 → 100755
Empty file.
Empty file modified pints/tests/test_abc_smc.py
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions pints/tests/test_toy_gaussian_logpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def test_gaussian_logpdf(self):
self.assertRaises(
ValueError, pints.toy.GaussianLogPDF, [1, 2, 3], [1, 2, 3, 4])

# Bad calls to function
f = pints.toy.GaussianLogPDF([1, 2, 3], [1, 1, 1])
self.assertRaises(ValueError, f.__call__, [1, 2])
self.assertRaises(ValueError, f.__call__, [1, 2, 3, 4, 5])

def test_sampling_and_kl_divergence(self):
# Test GaussianLogPDF.kl_divergence() and .sample().

Expand Down
5 changes: 5 additions & 0 deletions pints/tests/test_toy_high_dimensional_gaussian_logpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def test_high_dimensional_log_pdf(self):
self.assertRaises(
ValueError, pints.toy.HighDimensionalGaussianLogPDF, 11, -0.11)

# Bad calls to function
f = pints.toy.HighDimensionalGaussianLogPDF(3)
self.assertRaises(ValueError, f.__call__, [1, 2])
self.assertRaises(ValueError, f.__call__, [1, 2, 3, 4, 5])

def test_sensitivities(self):
# tests that sensitivities are correct

Expand Down
5 changes: 5 additions & 0 deletions pints/tests/test_toy_multimodal_gaussian_logpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def test_basic(self):
[1, 1], [1.5, 1.5], [3, 0], [0, 3.5]
])

# Bad calls to function
f = pints.toy.MultimodalGaussianLogPDF([[1, 1, 1]])
self.assertRaises(ValueError, f.__call__, [1, 2])
self.assertRaises(ValueError, f.__call__, [1, 2, 3, 4, 5])

def test_bad_constructors(self):
# Tests bad Constructors.

Expand Down
5 changes: 5 additions & 0 deletions pints/tests/test_toy_parabolic_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def test_parabolic_error(self):
self.assertAlmostEqual(dfx[1], 0)
self.assertAlmostEqual(dfx[2], -0.4)

# Bad calls to function
f = pints.toy.ParabolicError([1, 1, 1])
self.assertRaises(ValueError, f.__call__, [1, 2])
self.assertRaises(ValueError, f.__call__, [1, 2, 3, 4, 5])


if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions pints/tests/test_toy_twisted_gaussian_logpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def test_twisted_gaussian_logpdf(self):
self.assertRaises(ValueError, pints.toy.TwistedGaussianLogPDF, 1)
self.assertRaises(ValueError, pints.toy.TwistedGaussianLogPDF, b=-1)

# Bad calls to function
f = pints.toy.TwistedGaussianLogPDF(3)
self.assertRaises(ValueError, f.__call__, [1, 2])
self.assertRaises(ValueError, f.__call__, [1, 2, 3, 4, 5])

def test_sampling_and_kl_divergence(self):
# Test TwistedGaussianLogPDF.kl_divergence() and .sample().

Expand Down
4 changes: 3 additions & 1 deletion pints/toy/_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import scipy.stats

from . import ToyLogPDF
from .. import vector


class GaussianLogPDF(ToyLogPDF):
Expand Down Expand Up @@ -58,7 +59,8 @@ def __init__(self, mean=[0, 0], sigma=[1, 1]):
self._sigma_inv = np.linalg.inv(self._sigma)

def __call__(self, x):
return self._phi.logpdf(x)
y = vector(x).reshape(self._n_parameters)
return self._phi.logpdf(y)

def distance(self, samples):
"""
Expand Down
4 changes: 3 additions & 1 deletion pints/toy/_high_dimensional_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import scipy.stats

from . import ToyLogPDF
from .. import vector


class HighDimensionalGaussianLogPDF(ToyLogPDF):
Expand Down Expand Up @@ -61,7 +62,8 @@ def __init__(self, dimension=20, rho=0.5):
self._var = scipy.stats.multivariate_normal(self._mean, self._cov)

def __call__(self, x):
return self._var.logpdf(x)
y = vector(x).reshape(self._n_parameters)
return self._var.logpdf(y)

def distance(self, samples):
"""
Expand Down
3 changes: 2 additions & 1 deletion pints/toy/_multimodal_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def __init__(self, modes=None, covariances=None):
np.linalg.inv(self._covs[i]) for i, mode in enumerate(self._modes)]

def __call__(self, x):
f = np.sum([var.pdf(x) for var in self._vars])
y = pints.vector(x).reshape(self._n_parameters)
f = np.sum([var.pdf(y) for var in self._vars])
return -np.inf if f == 0 else np.log(f)

def distance(self, samples):
Expand Down
3 changes: 2 additions & 1 deletion pints/toy/_parabola.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def __init__(self, c=[0, 0]):
self._n = len(self._c)

def __call__(self, x):
return np.sum((self._c - x)**2)
y = pints.vector(x).reshape(self._n)
return np.sum((self._c - y)**2)

def evaluateS1(self, x):
""" See :meth:`pints.ErrorMeasure.evaluateS1()`. """
Expand Down
2 changes: 1 addition & 1 deletion pints/toy/_twisted_gaussian_banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, dimension=10, b=0.1, V=100):
np.zeros(self._n_parameters), self._sigma)

def __call__(self, x):
y = np.array(x, copy=True, dtype='float')
y = np.array(x, copy=True, dtype='float').reshape(self._n_parameters)
y[0] = float(y[0]) / np.sqrt(self._V)
y[1] += self._b * ((x[0] ** 2) - self._V)
return self._phi.logpdf(y)
Expand Down

0 comments on commit 107af2f

Please sign in to comment.