diff --git a/secop_psi/softcal.py b/secop_psi/softcal.py index a56c865..bfa06a1 100644 --- a/secop_psi/softcal.py +++ b/secop_psi/softcal.py @@ -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)