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
Tyler Goodlet 2023-01-18 14:52:07 -05:00
parent b45c027db8
commit 7d404ed7ef
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,8 +352,17 @@ 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 i_read_range is not None:
read_slc = slice(*i_read_range)
index = arr[read_slc][self.index_field]
if not index.size:
return None
ixrng = (index[0], index[-1])
else:
if x_range is None: if x_range is None:
( (
l, l,
@ -368,9 +378,8 @@ class Viz(msgspec.Struct): # , frozen=True):
# TODO: hash the slice instead maybe? # TODO: hash the slice instead maybe?
# https://stackoverflow.com/a/29980872 # https://stackoverflow.com/a/29980872
ixrng = (round(lbar), round(rbar)) 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,6 +395,7 @@ class Viz(msgspec.Struct): # , frozen=True):
mxmn, mxmn,
) )
if i_read_range is None:
# get relative slice indexes into array # get relative slice indexes into array
if self.index_field == 'time': if self.index_field == 'time':
read_slc = slice_from_time( read_slc = slice_from_time(
@ -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,