From e71bd2cb1e40e404f4e9a787a7552fce22395ba0 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 31 Oct 2022 14:23:29 -0400 Subject: [PATCH] Move axis-tick-values lru caching into our existing `Axis` --- piker/ui/_axes.py | 33 +++++++++++++++++++++ piker/ui/_pg_overrides.py | 60 +-------------------------------------- 2 files changed, 34 insertions(+), 59 deletions(-) diff --git a/piker/ui/_axes.py b/piker/ui/_axes.py index 7ba52055..3ed5b420 100644 --- a/piker/ui/_axes.py +++ b/piker/ui/_axes.py @@ -39,12 +39,17 @@ class Axis(pg.AxisItem): ''' 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__( self, linkedsplits, typical_max_str: str = '100 000.000', text_color: str = 'bracket', + lru_cache_tick_strings: bool = True, **kwargs ) -> None: @@ -91,6 +96,34 @@ class Axis(pg.AxisItem): # size the pertinent axis dimension to a "typical value" 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 def text_color(self) -> str: return self._text_color diff --git a/piker/ui/_pg_overrides.py b/piker/ui/_pg_overrides.py index d7d7d3bc..a961e567 100644 --- a/piker/ui/_pg_overrides.py +++ b/piker/ui/_pg_overrides.py @@ -22,7 +22,6 @@ Generally, our does not require "scentific precision" for pixel perfect view transforms. """ -import functools from typing import Optional import pyqtgraph as pg @@ -52,7 +51,6 @@ def _do_overrides() -> None: # we don't care about potential fp issues inside Qt pg.functions.invertQTransform = invertQTransform pg.PlotItem = PlotItem - pg.AxisItem = AxisItem # 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 # ``ViewBox`` geometry bug.. where a gap for the # 'bottom' axis is somehow left in? - axis = AxisItem(orientation=name, parent=self) + axis = pg.AxisItem(orientation=name, parent=self) axis.linkToView(self.vb) @@ -260,59 +258,3 @@ class PlotItem(pg.PlotItem): # self.getAxis('bottom').setGrid(x) # self.getAxis('left').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