Esempio n. 1
0
    def card(self, timestamp, window=None):
        """
        Returns the estimate of the cardinality at 'timestamp' using 'window'
        """
        if window is None:
            window = self.window

        if not 0 < window <= self.window:
            raise ValueError('0 < window <= W')

        def max_r(l):
            return max(l) if l else 0

        M = [
            max_r([R for ts, R in lpfm
                   if ts >= (timestamp - window)]) if lpfm else 0
            for lpfm in self.LPFM
        ]

        #count number or registers equal to 0
        V = M.count(0)
        if V > 0:
            H = self.m * math.log(self.m / float(V))
            return H if H <= get_treshold(self.p) else self._Ep(M)
        else:
            return self._Ep(M)
Esempio n. 2
0
    def card_wlist(self, timestamp, window_list):
        """
        Returns the estimate of the cardinality at 'timestamp' using list of windows
        """
        for window in window_list:
            if not 0 < window <= self.window:
                raise ValueError("0 < window <= W")

        tsl = [(timestamp - window, idx) for idx, window in enumerate(window_list)]
        tsl.sort()

        M_list = [[] for _ in window_list]

        # Highly optimized code (PyPy), but may be slow in CPython
        for lpfm in self.LPFM:
            R_max = 0
            _p = len(tsl) - 1
            if lpfm:
                i = len(lpfm) - 1
                while i >= 0:
                    ts, R = lpfm[i]
                    while _p >= 0:
                        _ts, _idx = tsl[_p]
                        if ts >= _ts:
                            break
                        M_list[_idx].append(R_max)
                        _p -= 1
                    if _p < 0:
                        break
                    R_max = R
                    i -= 1

            for i in xrange(0, _p + 1):
                M_list[tsl[i][1]].append(R_max)

        res = []
        for M in M_list:
            # count number or registers equal to 0
            V = M.count(0)
            if V > 0:
                H = self.m * math.log(self.m / float(V))
                res.append(H if H <= get_treshold(self.p) else self._Ep(M))
            else:
                res.append(self._Ep(M))
        return res
Esempio n. 3
0
    def card_wlist(self, timestamp, window_list):
        """
        Returns the estimate of the cardinality at 'timestamp' using list of windows
        """
        for window in window_list:
            if not 0 < window <= self.window:
                raise ValueError('0 < window <= W')

        tsl = [(timestamp - window, idx)
               for idx, window in enumerate(window_list)]
        tsl.sort()

        M_list = [[] for _ in window_list]

        # Highly optimized code (PyPy), but may be slow in CPython
        for lpfm in self.LPFM:
            R_max = 0
            _p = len(tsl) - 1
            if lpfm:
                i = len(lpfm) - 1
                while i >= 0:
                    ts, R = lpfm[i]
                    while _p >= 0:
                        _ts, _idx = tsl[_p]
                        if ts >= _ts: break
                        M_list[_idx].append(R_max)
                        _p -= 1
                    if _p < 0: break
                    R_max = R
                    i -= 1

            for i in xrange(0, _p + 1):
                M_list[tsl[i][1]].append(R_max)

        res = []
        for M in M_list:
            #count number or registers equal to 0
            V = M.count(0)
            if V > 0:
                H = self.m * math.log(self.m / float(V))
                res.append(H if H <= get_treshold(self.p) else self._Ep(M))
            else:
                res.append(self._Ep(M))
        return res
Esempio n. 4
0
    def card(self, timestamp, window=None):
        """
        Returns the estimate of the cardinality at 'timestamp' using 'window'
        """
        if window is None:
            window = self.window

        if not 0 < window <= self.window:
            raise ValueError("0 < window <= W")

        def max_r(l):
            return max(l) if l else 0

        M = [max_r([R for ts, R in lpfm if ts >= (timestamp - window)]) if lpfm else 0 for lpfm in self.LPFM]

        # count number or registers equal to 0
        V = M.count(0)
        if V > 0:
            H = self.m * math.log(self.m / float(V))
            return H if H <= get_treshold(self.p) else self._Ep(M)
        else:
            return self._Ep(M)