Move axis-tick-values lru caching into our existing `Axis`

pg_exts_fork
Tyler Goodlet 2022-10-31 14:23:29 -04:00
parent be24473fb4
commit e71bd2cb1e
2 changed files with 34 additions and 59 deletions

View File

@ -39,12 +39,17 @@ class Axis(pg.AxisItem):
''' '''
A better axis that sizes tick contents considering font size. A better axis that sizes tick contents considering font size.
Also includes tick values lru caching originally proposed in but never
accepted upstream:
https://github.com/pyqtgraph/pyqtgraph/pull/2160
''' '''
def __init__( def __init__(
self, self,
linkedsplits, linkedsplits,
typical_max_str: str = '100 000.000', typical_max_str: str = '100 000.000',
text_color: str = 'bracket', text_color: str = 'bracket',
lru_cache_tick_strings: bool = True,
**kwargs **kwargs
) -> None: ) -> None:
@ -91,6 +96,34 @@ class Axis(pg.AxisItem):
# size the pertinent axis dimension to a "typical value" # size the pertinent axis dimension to a "typical value"
self.size_to_values() self.size_to_values()
# NOTE: requires override ``.tickValues()`` method seen below.
if lru_cache_tick_strings:
self.tickStrings = lru_cache(
maxsize=2**20
)(self.tickStrings)
# NOTE: only overriden to cast tick values entries into tuples
# for use with the lru caching.
def tickValues(
self,
minVal: float,
maxVal: float,
size: int,
) -> list[tuple[float, tuple[str]]]:
'''
Repack tick values into tuples for lru caching.
'''
ticks = []
for scalar, values in super().tickValues(minVal, maxVal, size):
ticks.append((
scalar,
tuple(values), # this
))
return ticks
@property @property
def text_color(self) -> str: def text_color(self) -> str:
return self._text_color return self._text_color

View File

@ -22,7 +22,6 @@ Generally, our does not require "scentific precision" for pixel perfect
view transforms. view transforms.
""" """
import functools
from typing import Optional from typing import Optional
import pyqtgraph as pg import pyqtgraph as pg
@ -52,7 +51,6 @@ def _do_overrides() -> None:
# we don't care about potential fp issues inside Qt # we don't care about potential fp issues inside Qt
pg.functions.invertQTransform = invertQTransform pg.functions.invertQTransform = invertQTransform
pg.PlotItem = PlotItem pg.PlotItem = PlotItem
pg.AxisItem = AxisItem
# NOTE: the below customized type contains all our changes on a method # NOTE: the below customized type contains all our changes on a method
@ -211,7 +209,7 @@ class PlotItem(pg.PlotItem):
# adding this is without it there's some weird # adding this is without it there's some weird
# ``ViewBox`` geometry bug.. where a gap for the # ``ViewBox`` geometry bug.. where a gap for the
# 'bottom' axis is somehow left in? # 'bottom' axis is somehow left in?
axis = AxisItem(orientation=name, parent=self) axis = pg.AxisItem(orientation=name, parent=self)
axis.linkToView(self.vb) axis.linkToView(self.vb)
@ -260,59 +258,3 @@ class PlotItem(pg.PlotItem):
# self.getAxis('bottom').setGrid(x) # self.getAxis('bottom').setGrid(x)
# self.getAxis('left').setGrid(y) # self.getAxis('left').setGrid(y)
# self.getAxis('right').setGrid(y) # self.getAxis('right').setGrid(y)
# NOTE: overrides to lru_cache the ``.tickStrings()`` output.
class AxisItem(pg.AxisItem):
def __init__(
self,
orientation,
pen=None,
textPen=None,
linkView=None,
parent=None,
maxTickLength=-5,
showValues=True,
text='',
units='',
unitPrefix='',
lru_cache_tick_strings: bool = True,
**args,
):
super().__init__(
orientation=orientation,
pen=pen,
textPen=textPen,
linkView=linkView,
maxTickLength=maxTickLength,
showValues=showValues,
text=text,
units=units,
unitPrefix=unitPrefix,
parent=parent,
)
if lru_cache_tick_strings:
self.tickStrings = functools.lru_cache(
maxsize=2**20
)(self.tickStrings)
def tickValues(
self,
minVal: float,
maxVal: float,
size: int,
) -> list[tuple[float, tuple[str]]]:
'''
Repack tick values into tuples for lru caching.
'''
ticks = []
for scalar, values in super().tickValues(minVal, maxVal, size):
ticks.append((
scalar,
tuple(values), # this
))
return ticks