Factor ohlc to line data conversion into `._pathops.ohlc_to_line()`

pre_flow
Tyler Goodlet 2022-05-15 15:45:06 -04:00
parent fdd4255246
commit 06d3cadcc0
2 changed files with 76 additions and 21 deletions

View File

@ -52,13 +52,13 @@ from .._profile import (
) )
from ._pathops import ( from ._pathops import (
gen_ohlc_qpath, gen_ohlc_qpath,
ohlc_to_line,
) )
from ._ohlc import ( from ._ohlc import (
BarItems, BarItems,
) )
from ._curve import ( from ._curve import (
FastAppendCurve, FastAppendCurve,
# step_path_arrays_from_1d,
) )
from ..log import get_logger from ..log import get_logger
@ -426,29 +426,34 @@ class Flow(msgspec.Struct): # , frozen=True):
# create a flattened view onto the OHLC array # create a flattened view onto the OHLC array
# which can be read as a line-style format # which can be read as a line-style format
shm = self.shm shm = self.shm
(
self._iflat_first,
self._iflat_last,
self.gx,
self.gy,
) = ohlc_to_line(shm)
# flat = self.gy = self.shm.unstruct_view(fields) # self.gy = self.shm.ustruct(fields)
self.gy = self.shm.ustruct(fields) # first = self._iflat_first = self.shm._first.value
first = self._iflat_first = self.shm._first.value # last = self._iflat_last = self.shm._last.value
last = self._iflat_last = self.shm._last.value
# write pushed data to flattened copy # # write pushed data to flattened copy
self.gy[first:last] = rfn.structured_to_unstructured( # self.gy[first:last] = rfn.structured_to_unstructured(
self.shm.array[fields] # self.shm.array[fields]
) # )
# generate an flat-interpolated x-domain # # generate an flat-interpolated x-domain
self.gx = ( # self.gx = (
np.broadcast_to( # np.broadcast_to(
shm._array['index'][:, None], # shm._array['index'][:, None],
( # (
shm._array.size, # shm._array.size,
# 4, # only ohlc # # 4, # only ohlc
self.gy.shape[1], # self.gy.shape[1],
), # ),
) + np.array([-0.5, 0, 0, 0.5]) # ) + np.array([-0.5, 0, 0, 0.5])
) # )
assert self.gy.any() # assert self.gy.any()
# print(f'unstruct diff: {time.time() - start}') # print(f'unstruct diff: {time.time() - start}')
# profiler('read unstr view bars to line') # profiler('read unstr view bars to line')

View File

@ -22,11 +22,15 @@ from typing import (
) )
import numpy as np import numpy as np
from numpy.lib import recfunctions as rfn
from numba import njit, float64, int64 # , optional from numba import njit, float64, int64 # , optional
import pyqtgraph as pg import pyqtgraph as pg
from PyQt5 import QtGui from PyQt5 import QtGui
# from PyQt5.QtCore import QLineF, QPointF # from PyQt5.QtCore import QLineF, QPointF
from ..data._sharedmem import (
ShmArray,
)
from .._profile import pg_profile_enabled, ms_slower_then from .._profile import pg_profile_enabled, ms_slower_then
from ._compression import ( from ._compression import (
# ohlc_flatten, # ohlc_flatten,
@ -176,3 +180,49 @@ def gen_ohlc_qpath(
profiler("generate path with arrayToQPath") profiler("generate path with arrayToQPath")
return path return path
def ohlc_to_line(
ohlc_shm: ShmArray,
fields: list[str] = ['open', 'high', 'low', 'close']
) -> tuple[
int, # flattened first index
int, # flattened last index
np.ndarray,
np.ndarray,
]:
'''
Convert an input struct-array holding OHLC samples into a pair of
flattened x, y arrays with the same size (datums wise) as the source
data.
'''
y_out = ohlc_shm.ustruct(fields)
first = ohlc_shm._first.value
last = ohlc_shm._last.value
# write pushed data to flattened copy
y_out[first:last] = rfn.structured_to_unstructured(
ohlc_shm.array[fields]
)
# generate an flat-interpolated x-domain
x_out = (
np.broadcast_to(
ohlc_shm._array['index'][:, None],
(
ohlc_shm._array.size,
# 4, # only ohlc
y_out.shape[1],
),
) + np.array([-0.5, 0, 0, 0.5])
)
assert y_out.any()
return (
first,
last,
x_out,
y_out,
)