Use @cache and @cached_property for memoization#2465
Use @cache and @cached_property for memoization#2465valentinsulzer merged 8 commits intopybamm-team:developfrom
@cache and @cached_property for memoization#2465Conversation
|
I assume flake8 seems to be failing because it does not recognise |
@cache and @cached_property for memoization
|
I think you have to import cache and cached property from functools. Did this work locally on your machine? |
|
Oh, that makes more sense. I had run
I'll push the changes in a moment |
|
The class DataSet:
def __init__(self, sequence_of_numbers):
self._data = sequence_of_numbers
@property
@cache
def stdev(self):
return statistics.stdev(self._data)instead of class DataSet:
def __init__(self, sequence_of_numbers):
self._data = sequence_of_numbers
@property
@cache
def stdev(self):
self._stdev = statistics.stdev(self._data)
return self._stdevexample is from https://docs.python.org/3/library/functools.html#functools.cached_property |
|
I happened to memoize functions one-by-one and have been able to identify specific functions that cause errors upon memoization (the others are working as necessary):
Would it be recommended to open a separate PR to clear out the mess or should I try and fix individual commits in this one? P.S. Can this be memoized as well? I do not see any specific test failing upon doing so |
valentinsulzer
left a comment
There was a problem hiding this comment.
I happened to memoize functions one-by-one and have been able to identify specific functions that cause errors upon memoization (the others are working as necessary):
I see 800 failing tests in this PR
Would it be recommended to open a separate PR to clear out the mess or should I try and fix individual commits in this one?
Keep in same PR
P.S. Can this be memoized as well? I do not see any specific test failing upon doing so
Yes, it can
b2acc2e to
dfb31da
Compare
|
Apologies, I closed the PR by error (😅) – I removed a commit too many I'll reopen this with the new changes in a single commit |
Codecov ReportBase: 99.72% // Head: 99.72% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## develop #2465 +/- ##
===========================================
- Coverage 99.72% 99.72% -0.01%
===========================================
Files 257 257
Lines 19004 18973 -31
===========================================
- Hits 18951 18920 -31
Misses 53 53
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
valentinsulzer
left a comment
There was a problem hiding this comment.
Looks good, try using lru_cache instead of cache for compatibility with python 3.8
|
I saw that functools also describes using Is this an approach that can be taken forward? This would again not be compatible with Python 3.8 however |
|
It's not obvious to me why using Happy to merge this as-is if it's ready |
|
Great! I am not aware of any ways (yet) to measure if there is any performance increase by using Edit: why did these checks fail? The build on 3.9 was passing earlier |
|
Some of the solvers fail inconsistently, we haven't yet been able to figure out why |
|
@all-contributors add @agriyakhetarpal for code |
|
@tinosulzer I've put up a pull request to add @agriyakhetarpal! 🎉 |
Description
Uses
@cacheand@cached_propertyvia functools to implement memoization. I shall add more commits to this PR for more changes in the remaining files.Fixes #2288
Type of change
Key checklist:
$ flake8$ python run-tests.py --unit$ cd docsand then$ make clean; make htmlYou can run all three at once, using
$ python run-tests.py --quick.Further checks: