コード例 #1
0
ファイル: array.py プロジェクト: riccardomarotti/misura.canon
    def interpolate(self, step=1, kind=1):
        """Array interpolation for summary synchronization."""
        vt = Reference.interpolate(self, step)
        if vt is False:
            return False
        # Value sequence
        # starting from the oldest time minus step
        oldi = self.get_time(vt[0] - step)
        # Check if we have enough points to interpolate
        if len(self) - oldi < 5:
            return False
        # If possible, go back one more point, for interpolation safety
        if oldi > 1:
            oldi -= 1
        # Decode values and separate time and value vectors
        dat = self[oldi:]
#       print 'Getting data',self.path,dat,vt
        dat = np.array(dat)
        dat = dat.transpose()
        # Build a linear spline using vt points as knots
        #f=LSQUnivariateSpline(dat[0],dat[1],vt, k=kind)
        # Do a linear fitting
        (slope, const), res, rank, sing, rcond = np.polyfit(
            dat[0], dat[1], kind, full=True)
        # Build a vectorized evaluator
        f = np.vectorize(lambda x: slope * x + const)
        while vt[0] < dat[0][0] and len(vt) > 1:
            vt = vt[1:]
        while vt[-1] > dat[0][-1] and len(vt) > 1:
            vt = vt[:-1]
        if len(vt) <= 1:
            return False
        try:
            # Interpret time series
            out = f(vt)
        except:
            print 'Array.interpolate', self.path, vt, dat
            raise
        # Encode in (t,v) append-able list
        out = np.array([vt, out]).transpose()
        self.summary.commit(out)
        return True