Commit e622d9c9 authored by R.K.Garcia's avatar R.K.Garcia
Browse files

Add autoscaler for potential scaled integer conversion and sensitivity testing

parent 766fa048
Pipeline #36841 passed with stage
in 7 minutes and 29 seconds
#!/usr/bin/env python3
import numpy as np
def nanrange(npa: np.ndarray) -> (float, float):
q = npa.ravel()
return np.nanmin(q), np.nanmax(q)
class Autoscale:
vmin: int = None
vmax: int = None
vnan: int = None
vdtype = None
scale: float = None
offset: float = None
def __init__(self, data: np.ndarray = None, qmin: float = None, qmax: float = None,
vmin: int = -32767, vmax: int = 32767, vnan: int = -32768, vdtype=np.int16):
self.vmin = vmin
self.vmax = vmax
self.vnan = vnan
self.vdtype = vdtype
if (data is not None) and (qmin is None or qmax is None):
qrn, qrx = nanrange(data)
qmin = qmin if (qmin is not None) else qrn
qmax = qmax if (qmax is not None) else qrx
if qmin == qmax or (qmin is None) or (qmax is None):
raise ValueError(f"invalid range on input: {qmin} - {qmax}")
# print(f"{qmin} - {qmax}, {vmin} - {vmax}")
self.scale = (qmax - qmin) / (self.vmax - self.vmin)
self.offset = self.vmin * -self.scale + qmin
def invert(self, scaled_value):
""" convert from scaled integer representation
return np.where(self.vnan == scaled_value, np.NAN,
self.scale * float(scaled_value) + self.offset)
def convert(self, q: np.ndarray) -> np.ndarray:
""" convert to scaled integer representation
return np.array(
np.isfinite(q), np.around((q - self.offset) / self.scale),
def __call__(self, value, invert=False):
return self.convert(value) if not invert else self.invert(value)
def attrs(self):
return {'scale': self.scale,
'offset': self.offset,
'_FillValue': self.vnan
def _rando(shape=(5, 5)):
r = np.random.random(shape)
r[np.random.random(shape) < 0.1] = np.NAN
return r
def test_autoscale(qin=None):
q = qin or _rando()
a = Autoscale(q)
qs = a.convert(q)
assert np.isclose(a.invert(a.vmax), np.nanmax(q.ravel()))
assert np.isclose(a.invert(a.vmin), np.nanmin(q.ravel()))
assert np.all((qs != a.vnan) == np.isfinite(q))
return a, q
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment