improved softcal

- better error handling
- bug fix
Change-Id: I43bdd4aa35723f43f9e4baf2723af812f04689d3

Change-Id: I5990c75a7a8153e95abee9548475783ee893bd08
This commit is contained in:
zolliker 2021-03-08 10:25:06 +01:00
parent adc9a6da71
commit 1ca35cf8e9

View File

@ -85,7 +85,7 @@ class Parser340(StdParser):
self.logx, self.logy = True, True # logOhm, logK
elif value not in ('1', '2', '3'):
raise ValueError('invalid Data Format')
elif line.contains('No.'):
elif 'No.' in line:
self.header = False
return
super().parse(line)
@ -139,16 +139,26 @@ class CalCurve:
cls, args = KINDS.get(kind, (StdParser, {}))
args.update(optargs)
parser = cls(**args)
with open(filename) as f:
for line in f:
parser.parse(line)
try:
parser = cls(**args)
with open(filename) as f:
for line in f:
parser.parse(line)
except Exception as e:
raise ValueError('calib curve %s: %s' % (calibspec, e))
self.convert_x = nplog if parser.logx else linear
self.convert_y = npexp if parser.logy else linear
x = np.asarray(parser.xdata)
y = np.asarray(parser.ydata)
if np.all(x[:-1] > x[1:]): # all decreasing
x = np.flip(x)
y = np.flip(y)
elif np.any(x[:-1] >= x[1:]): # some not increasing
raise ValueError('calib curve %s is not monotonic' % calibspec)
try:
self.spline = splrep(np.asarray(parser.xdata), np.asarray(parser.ydata), s=0)
except:
raise ValueError('invalid curve, may be not monotonic?')
self.spline = splrep(x, y, s=0, k=min(3, len(x) - 1))
except (ValueError, TypeError):
raise ValueError('invalid calib curve %s' % calibspec)
def __call__(self, value):
"""convert value
@ -169,9 +179,12 @@ class Sensor(Readable):
status = Parameter(default=(Readable.Status.ERROR, 'unintialized'))
pollerClass = None
description = 'a calibrated sensor value'
_value_error = None
def __init__(self, name, logger, cfgdict, srv):
cfgdict.setdefault('description', 'calibrated value of module %r' % cfgdict['rawsensor'])
super().__init__(name, logger, cfgdict, srv)
def initModule(self):
self._rawsensor.registerCallbacks(self, ['status']) # auto update status
self._calib = CalCurve(self.calib)