You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There could be use cases for caching at several levels, e.g.
CachingForwardModel(model), checks times and parameters against a (limited size?) dict and returns cached results if possible. Not sure when you'd use this
CachingLikelihood and CachingError, checks parameters against a (limited size?) dict and returns cached results if possible, useful if we expect methods to test the same parameter sets multiple times (do we?)
I've written them as wrapper classes here, as that's the minimal effort for developers solution (no changes to underlying classes). But we could also consider updating the base classes with some reusable caching code and making all derived classes use it
The only complication is that, because it needs to place x in a dict, x needs to be hashable. Numpy arrays are not hashable, so here it converts it to a tuple first.
classCachingError(pints.ErrorMeasure):
""" Wraps around another error measure and provides a ``sensitivities()`` method for use with e.g. ``scipy``. All calls to the error and to :meth:`sensitivities` are mapped to :meth:`evaluateS1`. To reduce redundant calculations, up to 32 results of calling ``evaluateS1`` will be kept in cache. Note: Using this wrapper for methods that don't require sensitivities will result in very poor performance. """def__init__(self, error):
self._e=errordef__call__(self, x):
returnself._both(tuple(x))[0]
@functools.lru_cache(maxsize=32)def_both(self, x):
returnself._e.evaluateS1(x)
defevaluateS1(self, x):
returnself._both(tuple(x))
defn_parameters(self):
returnself._e.n_parameters()
defsensitivities(self, x):
returnself._both(tuple(x))[1]
Activity
MichaelClerx commentedon Aug 13, 2022
We might only need it for
ErrorandLogPdf?Here's an example for an error.
It uses the
lru_cachedecorator.The only complication is that, because it needs to place
xin a dict,xneeds to be hashable. Numpy arrays are not hashable, so here it converts it to a tuple first.MichaelClerx commentedon Aug 13, 2022
Just noticed that this is not required for scipy, you should set
jac=Trueinstead and have it call evaluateS1 :D