Support read-slice input to `Viz.maxmin()`

Acts as short cut when pipe-lining from `Viz.update_graphics()` (which
now returns the needed in-view array-relative-read-slice as output) such
that `Viz.read()` and `.datums_range()` doesn't need to be called
internally multiple times. In this case where `i_read_range` is provided
we of course skip doing time index translations and consequently lookup
the appropriate (epoch-time) index indices for caching.
multichartz_backup
Tyler Goodlet 2023-01-18 14:52:07 -05:00
parent 998f60b961
commit 55356ae7e8
1 changed files with 61 additions and 38 deletions

View File

@ -330,6 +330,7 @@ class Viz(msgspec.Struct): # , frozen=True):
self, self,
x_range: slice | tuple[int, int] | None = None, x_range: slice | tuple[int, int] | None = None,
i_read_range: tuple[int, int] | None = None,
use_caching: bool = True, use_caching: bool = True,
) -> tuple[float, float] | None: ) -> tuple[float, float] | None:
@ -343,7 +344,7 @@ class Viz(msgspec.Struct): # , frozen=True):
profiler = Profiler( profiler = Profiler(
msg=f'{name} -> `{str(self)}.maxmin()`', msg=f'{name} -> `{str(self)}.maxmin()`',
disabled=not pg_profile_enabled(), disabled=not pg_profile_enabled(),
ms_threshold=ms_slower_then, ms_threshold=4,
delayed=True, delayed=True,
) )
@ -351,26 +352,34 @@ class Viz(msgspec.Struct): # , frozen=True):
if shm is None: if shm is None:
return None return None
do_print: bool = False
arr = shm.array arr = shm.array
if x_range is None: if i_read_range is not None:
( read_slc = slice(*i_read_range)
l, index = arr[read_slc][self.index_field]
_, if not index.size:
lbar, return None
rbar, ixrng = (index[0], index[-1])
_,
r,
) = self.datums_range()
profiler(f'{self.name} got bars range') else:
x_range = lbar, rbar if x_range is None:
(
l,
_,
lbar,
rbar,
_,
r,
) = self.datums_range()
# TODO: hash the slice instead maybe? profiler(f'{self.name} got bars range')
# https://stackoverflow.com/a/29980872 x_range = lbar, rbar
ixrng = (round(lbar), round(rbar))
# TODO: hash the slice instead maybe?
# https://stackoverflow.com/a/29980872
lbar, rbar = ixrng = round(x_range[0]), round(x_range[1])
do_print: bool = False
if use_caching: if use_caching:
cached_result = self._mxmns.get(ixrng) cached_result = self._mxmns.get(ixrng)
if cached_result: if cached_result:
@ -386,21 +395,22 @@ class Viz(msgspec.Struct): # , frozen=True):
mxmn, mxmn,
) )
# get relative slice indexes into array if i_read_range is None:
if self.index_field == 'time': # get relative slice indexes into array
read_slc = slice_from_time( if self.index_field == 'time':
arr, read_slc = slice_from_time(
start_t=lbar, arr,
stop_t=rbar, start_t=lbar,
step=self.index_step(), stop_t=rbar,
) step=self.index_step(),
)
else: else:
ifirst = arr[0]['index'] ifirst = arr[0]['index']
read_slc = slice( read_slc = slice(
lbar - ifirst, lbar - ifirst,
(rbar - ifirst) + 1 (rbar - ifirst) + 1
) )
slice_view = arr[read_slc] slice_view = arr[read_slc]
@ -657,13 +667,17 @@ class Viz(msgspec.Struct): # , frozen=True):
profiler = Profiler( profiler = Profiler(
msg=f'Viz.update_graphics() for {self.name}', msg=f'Viz.update_graphics() for {self.name}',
disabled=not pg_profile_enabled(), disabled=not pg_profile_enabled(),
ms_threshold=4, ms_threshold=ms_slower_then,
# ms_threshold=ms_slower_then, # ms_threshold=4,
) )
# shm read and slice to view # shm read and slice to view
read = ( read = (
xfirst, xlast, src_array, xfirst,
ivl, ivr, in_view, xlast,
src_array,
ivl,
ivr,
in_view,
) = self.read(profiler=profiler) ) = self.read(profiler=profiler)
profiler('read src shm data') profiler('read src shm data')
@ -675,7 +689,10 @@ class Viz(msgspec.Struct): # , frozen=True):
or not render or not render
): ):
# print('exiting early') # print('exiting early')
return graphics return (
(ivl, ivr),
graphics,
)
should_redraw: bool = False should_redraw: bool = False
ds_allowed: bool = True # guard for m4 activation ds_allowed: bool = True # guard for m4 activation
@ -792,7 +809,10 @@ class Viz(msgspec.Struct): # , frozen=True):
if not out: if not out:
log.warning(f'{self.name} failed to render!?') log.warning(f'{self.name} failed to render!?')
return graphics return (
(ivl, ivr),
graphics,
)
path, reset_cache = out path, reset_cache = out
@ -843,7 +863,10 @@ class Viz(msgspec.Struct): # , frozen=True):
# track downsampled state # track downsampled state
self._in_ds = r._in_ds self._in_ds = r._in_ds
return graphics return (
(ivl, ivr),
graphics,
)
def draw_last( def draw_last(
self, self,
@ -1054,7 +1077,7 @@ class Viz(msgspec.Struct): # , frozen=True):
if do_ds: if do_ds:
# view.interaction_graphics_cycle() # view.interaction_graphics_cycle()
view.maybe_downsample_graphics() view.maybe_downsample_graphics()
view._set_yrange() view._set_yrange(viz=self)
def incr_info( def incr_info(
self, self,