From bf21d2e3292fc3d736cf56654327a25e7782241e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 31 May 2023 17:53:15 -0400 Subject: [PATCH] Rename default OHLCV `np.dtype` descriptions Use `def_iohlcv_fields` for a name and instead of copying and inserting the index field pop it for the non-index version. Drop creating `np.dtype()` instances since `numpy`'s apis accept both input forms so this is simpler on our end. --- piker/data/__init__.py | 11 ++++------- piker/data/_sharedmem.py | 5 ++--- piker/data/_source.py | 34 +++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/piker/data/__init__.py b/piker/data/__init__.py index 681bd3fe..cd0a1183 100644 --- a/piker/data/__init__.py +++ b/piker/data/__init__.py @@ -22,9 +22,6 @@ and storing data from your brokers as well as sharing live streams over a network. """ -from ._util import ( - get_console_log, -) from ._normalize import iterticks from ._sharedmem import ( maybe_open_shm_array, @@ -34,8 +31,8 @@ from ._sharedmem import ( ShmArray, ) from ._source import ( - base_iohlc_dtype, - base_ohlc_dtype, + def_iohlcv_fields, + def_ohlcv_fields, ) from .feed import ( open_feed, @@ -50,6 +47,6 @@ __all__ = [ 'attach_shm_array', 'open_shm_array', 'get_shm_token', - 'base_iohlc_dtype', - 'base_ohlc_dtype', + 'def_iohlcv_fields', + 'def_ohlcv_fields', ] diff --git a/piker/data/_sharedmem.py b/piker/data/_sharedmem.py index 2ed1c892..41dd0071 100644 --- a/piker/data/_sharedmem.py +++ b/piker/data/_sharedmem.py @@ -33,7 +33,7 @@ from numpy.lib import recfunctions as rfn import tractor from ._util import log -from ._source import base_iohlc_dtype +from ._source import def_iohlcv_fields from .types import Struct @@ -168,7 +168,7 @@ def _make_token( to access a shared array. ''' - dtype = base_iohlc_dtype if dtype is None else dtype + dtype = def_iohlcv_fields if dtype is None else dtype return _Token( shm_name=key, shm_first_index_name=key + "_first", @@ -258,7 +258,6 @@ class ShmArray: # to load an empty array.. if len(a) == 0 and self._post_init: raise RuntimeError('Empty array race condition hit!?') - # breakpoint() return a diff --git a/piker/data/_source.py b/piker/data/_source.py index d1d8be02..7f739d7d 100644 --- a/piker/data/_source.py +++ b/piker/data/_source.py @@ -23,26 +23,42 @@ from bidict import bidict import numpy as np -ohlc_fields = [ - ('time', float), +def_iohlcv_fields: list[tuple[str, type]] = [ + + # YES WE KNOW, this isn't needed in polars but we use it for doing + # ring-buffer like pre/append ops our our `ShmArray` real-time + # numpy-array buffering system such that there is a master index + # that can be used for index-arithmetic when write data to the + # "middle" of the array. See the ``tractor.ipc.shm`` pkg for more + # details. + ('index', int), + + # presume int for epoch stamps since it's most common + # and makes the most sense to avoid float rounding issues. + # TODO: if we want higher reso we should use the new + # ``time.time_ns()`` in python 3.10+ + ('time', int), ('open', float), ('high', float), ('low', float), ('close', float), ('volume', float), - ('bar_wap', float), + + # TODO: can we elim this from default field set to save on mem? + # i think only kraken really uses this in terms of what we get from + # their ohlc history API? + ('bar_wap', float), # shouldn't be default right? ] -ohlc_with_index = ohlc_fields.copy() -ohlc_with_index.insert(0, ('index', int)) - -# our minimum structured array layout for ohlc data -base_iohlc_dtype = np.dtype(ohlc_with_index) -base_ohlc_dtype = np.dtype(ohlc_fields) +# remove index field +def_ohlcv_fields: list[tuple[str, type]] = def_iohlcv_fields.copy() +def_ohlcv_fields.pop(0) +assert (len(def_iohlcv_fields) - len(def_ohlcv_fields)) == 1 # TODO: for now need to construct this manually for readonly arrays, see # https://github.com/numba/numba/issues/4511 # from numba import from_dtype +# base_ohlc_dtype = np.dtype(def_ohlc_fields) # numba_ohlc_dtype = from_dtype(base_ohlc_dtype) # map time frame "keys" to seconds values