From ac1f37a2c26df3d23ba80ef4d6f6aaf08b174d61 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 30 Nov 2022 10:06:20 -0500 Subject: [PATCH] Expect `index_field: str` in all graphics objects --- piker/data/_compression.py | 11 ++++++++--- piker/ui/_curve.py | 15 ++++++--------- piker/ui/_ohlc.py | 29 ++++++++++++++++------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/piker/data/_compression.py b/piker/data/_compression.py index aed1d7d3..e6111fca 100644 --- a/piker/data/_compression.py +++ b/piker/data/_compression.py @@ -35,13 +35,17 @@ from ..log import get_logger log = get_logger(__name__) -def hl2mxmn(ohlc: np.ndarray) -> np.ndarray: +def hl2mxmn( + ohlc: np.ndarray, + index_field: str = 'index', + +) -> np.ndarray: ''' Convert a OHLC struct-array containing 'high'/'low' columns to a "joined" max/min 1-d array. ''' - index = ohlc['index'] + index = ohlc[index_field] hls = ohlc[[ 'low', 'high', @@ -109,6 +113,7 @@ def trace_hl( def ohlc_flatten( ohlc: np.ndarray, use_mxmn: bool = True, + index_field: str = 'index', ) -> tuple[np.ndarray, np.ndarray]: ''' @@ -117,7 +122,7 @@ def ohlc_flatten( evenly (by 0.5 steps) over each index. ''' - index = ohlc['index'] + index = ohlc[index_field] if use_mxmn: # traces a line optimally over highs to lows diff --git a/piker/ui/_curve.py b/piker/ui/_curve.py index 663b328f..8df1cda0 100644 --- a/piker/ui/_curve.py +++ b/piker/ui/_curve.py @@ -360,11 +360,12 @@ class Curve(pg.GraphicsObject): render_data: np.ndarray, reset: bool, array_key: str, + index_field: str, ) -> None: # default line draw last call # with self.reset_cache(): - x = render_data['index'] + x = render_data[index_field] y = render_data[array_key] # draw the "current" step graphic segment so it @@ -391,10 +392,11 @@ class FlattenedOHLC(Curve): render_data: np.ndarray, reset: bool, array_key: str, + index_field: str, ) -> None: lasts = src_data[-2:] - x = lasts['index'] + x = lasts[index_field] y = lasts['close'] # draw the "current" step graphic segment so it @@ -421,6 +423,7 @@ class StepCurve(Curve): render_data: np.ndarray, reset: bool, array_key: str, + index_field: str, w: float = 0.5, @@ -429,7 +432,7 @@ class StepCurve(Curve): # TODO: remove this and instead place all step curve # updating into pre-path data render callbacks. # full input data - x = src_data['index'] + x = src_data[index_field] y = src_data[array_key] x_last = x[-1] @@ -458,9 +461,3 @@ class StepCurve(Curve): # p.drawLines(*tuple(filter(bool, self._last_step_lines))) # p.drawRect(self._last_step_rect) p.fillRect(self._last_step_rect, self._brush) - - # def sub_br( - # self, - # parent_br: QRectF | None = None, - # ) -> QRectF: - # return self._last_step_rect diff --git a/piker/ui/_ohlc.py b/piker/ui/_ohlc.py index 2ce23d30..98ffcb85 100644 --- a/piker/ui/_ohlc.py +++ b/piker/ui/_ohlc.py @@ -59,8 +59,8 @@ def bar_from_ohlc_row( OHLC "bar" for use in the "last datum" of a series. ''' - open, high, low, close, index = row[ - ['open', 'high', 'low', 'close', 'index']] + open, high, low, close, index = row #[fields] + # ['open', 'high', 'low', 'close', 'index']] # TODO: maybe consider using `QGraphicsLineItem` ?? # gives us a ``.boundingRect()`` on the objects which may make @@ -217,30 +217,33 @@ class BarItems(pg.GraphicsObject): render_data: np.ndarray, reset: bool, array_key: str, - - fields: list[str] = [ - 'index', - 'open', - 'high', - 'low', - 'close', - ], + index_field: str, ) -> None: # relevant fields + fields: list[str] = [ + 'open', + 'high', + 'low', + 'close', + index_field, + ] ohlc = src_data[fields] # last_row = ohlc[-1:] # individual values - last_row = i, o, h, l, last = ohlc[-1] + last_row = o, h, l, last, i = ohlc[-1] # times = src_data['time'] # if times[-1] - times[-2]: # breakpoint() # generate new lines objects for updatable "current bar" - self._last_bar_lines = bar_from_ohlc_row(last_row) + self._last_bar_lines = bar_from_ohlc_row( + last_row, + # fields, + ) # assert i == graphics.start_index - 1 # assert i == last_index @@ -270,4 +273,4 @@ class BarItems(pg.GraphicsObject): # because i've seen it do this to bars i - 3 back? # return ohlc['time'], ohlc['close'] - return ohlc['index'], ohlc['close'] + return ohlc[index_field], ohlc['close']