-
Notifications
You must be signed in to change notification settings - Fork 80
Colormap upgrade cache #4371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Colormap upgrade cache #4371
Conversation
010b974 to
1b78b29
Compare
src/silx/gui/plot/items/core.py
Outdated
| class ColormapMixIn(_Colormappable, ItemMixInBase): | ||
| """Mix-in class for items with colormap""" | ||
|
|
||
| COLORMAP_CACHE_SIZE = 25 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The size is pretty arbitrary. Maybe it can be worth to increase it a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me as it is, but you can increase it a bit if you want:
If you propose a slider with integer steps for the percentile, having COLORMAP_CACHE_SIZE = 128 would store all possible values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good point, lets go for 128.
| None if data is None else numpy.array(data, copy=copy or NP_OPTIONAL_COPY) | ||
| ) | ||
| self.__cacheColormapRange = {} # Reset cache | ||
| self.__cacheColormapRange.clear() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using clear is clearer... without bad wordplay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearer indeed!
1b78b29 to
e00da8a
Compare
e00da8a to
870dcdd
Compare
5101584 to
f9d95ba
Compare
| from collections import OrderedDict | ||
|
|
||
|
|
||
| class LRUCache: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Originally I wanted to inherit directly from OrderedDict. See this commit.
But it turned out that this implementation fails with python 3.10. Because the getitem API is used when calling move_to_end. So the appropriate way seems to have composition instead of inheritance. This forces me to expose the API...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not add the LRUCache class to the public API for a single usage inside the library.
Then, best to implement only the method that gets used.
Also, since it is used only in one class and get is called at a single place, you could call move_to_end directly from the code if vRange is not None here (with a comment):
silx/src/silx/gui/plot/items/core.py
Lines 702 to 705 in bff0f83
| vRange = self.__cacheColormapRange.get(key, None) | |
| if vRange is None: | |
| vRange = colormap._computeAutoscaleRange(data) | |
| self.__cacheColormapRange[key] = vRange |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌 I moved the cache to silx.gui.plot.items._cache. After discussion we will keep the class for now as it also handle the limitation of the size and not only keeping the last element access at the end.
t20100
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO much simpler not to have a complete LRUCache (and maybe not at all).
Anyway, best to not put LRUCache in the API
| None if data is None else numpy.array(data, copy=copy or NP_OPTIONAL_COPY) | ||
| ) | ||
| self.__cacheColormapRange = {} # Reset cache | ||
| self.__cacheColormapRange.clear() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearer indeed!
| from collections import OrderedDict | ||
|
|
||
|
|
||
| class LRUCache: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not add the LRUCache class to the public API for a single usage inside the library.
Then, best to implement only the method that gets used.
Also, since it is used only in one class and get is called at a single place, you could call move_to_end directly from the code if vRange is not None here (with a comment):
silx/src/silx/gui/plot/items/core.py
Lines 702 to 705 in bff0f83
| vRange = self.__cacheColormapRange.get(key, None) | |
| if vRange is None: | |
| vRange = colormap._computeAutoscaleRange(data) | |
| self.__cacheColormapRange[key] = vRange |
src/silx/gui/plot/items/core.py
Outdated
| class ColormapMixIn(_Colormappable, ItemMixInBase): | ||
| """Mix-in class for items with colormap""" | ||
|
|
||
| COLORMAP_CACHE_SIZE = 25 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me as it is, but you can increase it a bit if you want:
If you propose a slider with integer steps for the percentile, having COLORMAP_CACHE_SIZE = 128 would store all possible values.
bcbcc24 to
03cb6da
Compare
Uh oh!
There was an error while loading. Please reload this page.