Ejemplo n.º 1
0
    def mergeAnomaly(self, anoms, mergeLen=10):
        accum_num = 0
        find = 0
        ret = []
        l = len(anoms)
        if l == 0:
            return []
        timediff = pd.Timedelta('15s') * 30

        base = 10**9
        for i, x in enumerate(anoms.index):
            if x - anoms.index[i - 1] > timediff and i > 0:
                ret.append(
                    Anomaly(anoms.index[find].value / base,
                            anoms.index[i - 1].value / base, 0, 0))
                find = i
        print find
        ret.append(
            Anomaly(anoms.index[find].value / base,
                    anoms.index[l - 1].value / base, 0, 0))
        return ret
Ejemplo n.º 2
0
 def mergeAnomaly(self, mergeLen=3):
     accum_num = 0
     find = -1
     ret = []
     l = len(self.anoms)
     for i, x in enumerate(anoms):
         if x and find == -1:
             find = i
         if not x or i == l - 1:
             if i == l - 1:
                 i += 1
             if i - find >= mergeLen and find > -1:
                 print i, find
                 ret.append(Anomaly(find, i, 0, 0))
             find = -1
     return ret
Ejemplo n.º 3
0
    def _detect_anomalies(self):
        """
    Detect anomalies using a threshold on anomaly scores.
    """
        anom_scores = self.anom_scores
        max_anom_score = anom_scores.max()
        anomalies = []

        if max_anom_score:
            threshold = self.threshold or max_anom_score * self.score_percent_threshold
            # Find all the anomaly intervals.
            intervals = []
            start, end = None, None
            for timestamp, value in anom_scores.iteritems():
                if value > threshold:
                    end = timestamp
                    if not start:
                        start = timestamp
                elif start and end is not None:
                    intervals.append([start, end])
                    start = None
                    end = None
            if start is not None:
                intervals.append([start, end])

            # Locate the exact anomaly point within each anomaly interval.
            for interval_start, interval_end in intervals:
                interval_series = anom_scores.crop(interval_start,
                                                   interval_end)

                self.refine_algorithm_params['time_series'] = interval_series
                refine_algorithm = self.refine_algorithm(
                    **self.refine_algorithm_params)
                scores = refine_algorithm.run()
                max_refine_score = scores.max()

                # Get the timestamp of the maximal score.
                max_refine_timestamp = scores.timestamps[scores.values.index(
                    max_refine_score)]
                anomaly = Anomaly(interval_start, interval_end,
                                  interval_series.max(), max_refine_timestamp)
                anomalies.append(anomaly)

        self.anomalies = anomalies