Attempt to guard against numercial "anomalies" in `Viz.maxmin()`, add cacheing flag

rekt_pps
Tyler Goodlet 2023-04-21 14:00:13 -04:00
parent 34ff5ff249
commit f6cd08c6fa
1 changed files with 33 additions and 3 deletions

View File

@ -23,6 +23,8 @@ from functools import lru_cache
from math import ( from math import (
ceil, ceil,
floor, floor,
isnan,
log as logf,
) )
from typing import ( from typing import (
Literal, Literal,
@ -332,6 +334,8 @@ class Viz(Struct):
float, float,
] = {} ] = {}
_mxmn_cache_enabled: bool = True
# to make lru_cache-ing work, see # to make lru_cache-ing work, see
# https://docs.python.org/3/faq/programming.html#how-do-i-cache-method-calls # https://docs.python.org/3/faq/programming.html#how-do-i-cache-method-calls
def __eq__(self, other): def __eq__(self, other):
@ -447,7 +451,10 @@ class Viz(Struct):
# https://stackoverflow.com/a/29980872 # https://stackoverflow.com/a/29980872
ixrng = lbar, rbar = round(x_range[0]), round(x_range[1]) 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) cached_result = self._mxmns.get(ixrng)
if cached_result: if cached_result:
if do_print: if do_print:
@ -521,8 +528,31 @@ class Viz(Struct):
) )
# cache result for input range # cache result for input range
assert mxmn ylow, yhi = mxmn
self._mxmns[ixrng] = (read_slc, 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 self.vs.yrange = mxmn
profiler(f'yrange mxmn cacheing: {x_range} -> {mxmn}') profiler(f'yrange mxmn cacheing: {x_range} -> {mxmn}')
return ( return (