Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
## Unreleased

### Added
- [#1715](https://github.com/pints-team/pints/pull/1715) Added methods `ProblemErrorMeasure.problem()`, `ProblemLogLikelihood.problem()`, `SingleOutputProblem.model()` and `MultiOutputProblem.model()`.
### Changed
- [#1713](https://github.com/pints-team/pints/pull/1713) PINTS now requires matplotlib 2.2 or newer.
### Deprecated
Expand Down
12 changes: 9 additions & 3 deletions pints/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,12 @@ def evaluateS1(self, parameters):
np.asarray(dy).reshape((self._n_times, self._n_parameters))
)

def model(self):
""" Returns the :class:`ForwardModel` underlying this problem. """
return self._model

def n_outputs(self):
"""
Returns the number of outputs for this problem (always 1).
"""
""" Returns the number of outputs for this problem (always 1). """
return 1

def n_parameters(self):
Expand Down Expand Up @@ -281,6 +283,10 @@ def evaluateS1(self, parameters):
self._n_times, self._n_outputs, self._n_parameters)
)

def model(self):
""" Returns the :class:`ForwardModel` underlying this problem. """
return self._model

def n_outputs(self):
"""
Returns the number of outputs for this problem.
Expand Down
6 changes: 5 additions & 1 deletion pints/_error_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ProblemErrorMeasure(ErrorMeasure):
:class:`single<pints.SingleOutputProblem>` or
:class:`multi-output<pints.MultiOutputProblem>` problems.
"""
def __init__(self, problem=None):
def __init__(self, problem):
super(ProblemErrorMeasure, self).__init__()
self._problem = problem
self._times = problem.times()
Expand All @@ -62,6 +62,10 @@ def n_parameters(self):
""" See :meth:`ErrorMeasure.n_parameters()`. """
return self._n_parameters

def problem(self):
""" Returns the problem this error measure was defined on. """
return self._problem


class MeanSquaredError(ProblemErrorMeasure):
r"""
Expand Down
4 changes: 4 additions & 0 deletions pints/_log_pdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class ProblemLogLikelihood(LogPDF):
----------
problem
The time-series problem this log-likelihood is defined for.

"""
def __init__(self, problem):
super(ProblemLogLikelihood, self).__init__()
Expand All @@ -334,6 +335,9 @@ def n_parameters(self):
""" See :meth:`LogPDF.n_parameters()`. """
return self._n_parameters

def problem(self):
return self._problem


class LogPosterior(LogPDF):
"""
Expand Down
12 changes: 12 additions & 0 deletions pints/tests/test_error_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,18 @@ def test_bad_constructor(self):
pints.ProbabilityBasedError, MiniProblem())


class TestProblemErrorMeasure(unittest.TestCase):
""" Tests shared methods of the ProblemErrorMeasure abstract class. """

def test_shared(self):
# Test underlying ProblemErrorMeasure method problem()

problem = MiniProblem()
error = pints.MeanSquaredError(problem)
self.assertIs(error.problem(), problem)
self.assertEqual(error.n_parameters(), problem.n_parameters())


class TestRootMeanSquaredError(unittest.TestCase):

@classmethod
Expand Down
16 changes: 16 additions & 0 deletions pints/tests/test_log_likelihoods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,22 @@ def test_negative_sd(self):
self.assertEqual(log_likelihood([1, 1, 0]), -np.inf)


class TestProblemLogLikelihood(unittest.TestCase):
""" Test shared ProblemLogLikelihood methods. """

@classmethod
def setUpClass(cls):
cls.model = pints.toy.ConstantModel(1)
cls.times = np.array([1, 2, 3, 4])
cls.data = np.asarray([1.9, 2.1, 1.8, 2.2])

def test_shared_methods(self):
problem = pints.SingleOutputProblem(self.model, self.times, self.data)
log_likelihood = pints.GaussianKnownSigmaLogLikelihood(problem, 1.5)
self.assertEqual(log_likelihood.n_parameters(), problem.n_parameters())
self.assertIs(log_likelihood.problem(), problem)


class TestScaledLogLikelihood(unittest.TestCase):

@classmethod
Expand Down
1 change: 1 addition & 0 deletions pints/tests/test_multi_output_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_basics(self):
self.assertEqual(problem.n_parameters(), model.n_parameters(), 2)
self.assertEqual(problem.n_outputs(), model.n_outputs(), 3)
self.assertEqual(problem.n_times(), len(times))
self.assertIs(problem.model(), model)

# Test errors
times[0] = -2
Expand Down
1 change: 1 addition & 0 deletions pints/tests/test_single_output_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_basics(self):
self.assertEqual(problem.n_parameters(), model.n_parameters(), 2)
self.assertEqual(problem.n_outputs(), model.n_outputs(), 1)
self.assertEqual(problem.n_times(), len(times))
self.assertIs(problem.model(), model)

# Test errors
times[0] = -2
Expand Down