From 433697cc4f0348a2ef5daf76cdd0f90fe3ef44f2 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 17 Jan 2023 17:08:17 -0500 Subject: [PATCH] Add cached refs to last 1d xy outputs For the purposes of avoiding another full format call we can stash the last rendered 1d xy pre-graphics formats as `IncrementalFormatter.x/y_1d: np.ndarray`s and allow readers in the viz and render machinery to use this data easily for things like "only drawing the last uppx's worth of data as a line". Also add a `.flat_index_ratio: float` which can be used similarly as a scalar applied to indexes into the src array but instead when indexing (flattened) 1d xy formatted outputs. Finally, this drops the way overdone/noisy `.__repr__()` meth we had XD --- piker/data/_formatters.py | 40 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/piker/data/_formatters.py b/piker/data/_formatters.py index f2b696c2..ef892c7a 100644 --- a/piker/data/_formatters.py +++ b/piker/data/_formatters.py @@ -55,6 +55,10 @@ class IncrementalFormatter(msgspec.Struct): shm: ShmArray viz: Viz + # the value to be multiplied any any index into the x/y_1d arrays + # given the input index is based on the original source data array. + flat_index_ratio: float = 1 + @property def index_field(self) -> 'str': ''' @@ -92,8 +96,8 @@ class IncrementalFormatter(msgspec.Struct): xy_nd_stop: int | None = None # TODO: eventually incrementally update 1d-pre-graphics path data? - # x_1d: Optional[np.ndarray] = None - # y_1d: Optional[np.ndarray] = None + x_1d: np.ndarray | None = None + y_1d: np.ndarray | None = None # incremental view-change state(s) tracking _last_vr: tuple[float, float] | None = None @@ -107,32 +111,6 @@ class IncrementalFormatter(msgspec.Struct): ''' return self.viz.index_step() - def __repr__(self) -> str: - msg = ( - f'{type(self)}: ->\n\n' - f'fqsn={self.viz.name}\n' - f'shm_name={self.shm.token["shm_name"]}\n\n' - - f'last_vr={self._last_vr}\n' - f'last_ivdr={self._last_ivdr}\n\n' - - f'xy_slice={self.xy_slice}\n' - # f'xy_nd_stop={self.xy_nd_stop}\n\n' - ) - - x_nd_len = 0 - y_nd_len = 0 - if self.x_nd is not None: - x_nd_len = len(self.x_nd) - y_nd_len = len(self.y_nd) - - msg += ( - f'x_nd_len={x_nd_len}\n' - f'y_nd_len={y_nd_len}\n' - ) - - return msg - def diff( self, new_read: tuple[np.ndarray], @@ -354,6 +332,11 @@ class IncrementalFormatter(msgspec.Struct): array_key, view_range, ) + # cache/save last 1d outputs for use by other + # readers (eg. `Viz.draw_last_datum()` in the + # only-draw-last-uppx case). + self.x_1d = x_1d + self.y_1d = y_1d # app_tres = None # if append_len: @@ -536,6 +519,7 @@ class OHLCBarsFmtr(IncrementalFormatter): fields: list[str] = field( default_factory=lambda: ['open', 'high', 'low', 'close'] ) + flat_index_ratio: float = 4 def allocate_xy_nd( self,