From f6cd08c6faef264109f648d33e8a551bcbf227eb Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 21 Apr 2023 14:00:13 -0400 Subject: [PATCH] Attempt to guard against numercial "anomalies" in `Viz.maxmin()`, add cacheing flag --- piker/ui/_dataviz.py | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/piker/ui/_dataviz.py b/piker/ui/_dataviz.py index 3c686619..7f1ef41e 100644 --- a/piker/ui/_dataviz.py +++ b/piker/ui/_dataviz.py @@ -23,6 +23,8 @@ from functools import lru_cache from math import ( ceil, floor, + isnan, + log as logf, ) from typing import ( Literal, @@ -332,6 +334,8 @@ class Viz(Struct): float, ] = {} + _mxmn_cache_enabled: bool = True + # to make lru_cache-ing work, see # https://docs.python.org/3/faq/programming.html#how-do-i-cache-method-calls def __eq__(self, other): @@ -447,7 +451,10 @@ class Viz(Struct): # https://stackoverflow.com/a/29980872 ixrng = lbar, rbar = round(x_range[0]), round(x_range[1]) - if use_caching: + if ( + use_caching + and self._mxmn_cache_enabled + ): cached_result = self._mxmns.get(ixrng) if cached_result: if do_print: @@ -521,8 +528,31 @@ class Viz(Struct): ) # cache result for input range - assert mxmn - self._mxmns[ixrng] = (read_slc, mxmn) + ylow, yhi = mxmn + + try: + prolly_anomaly: bool = ( + ( + abs(logf(ylow, 10)) > 16 + if ylow + else False + ) + or ( + isnan(ylow) or isnan(yhi) + ) + ) + except ValueError: + prolly_anomaly = True + + if prolly_anomaly: + return None + + if ( + not isnan(ylow) + and not prolly_anomaly + ): + self._mxmns[ixrng] = (read_slc, mxmn) + self.vs.yrange = mxmn profiler(f'yrange mxmn cacheing: {x_range} -> {mxmn}') return (