Compute anomaly scores using a lagging window.
556822
def _compute_anom_data_using_window(self):
        
        anom_scores = {}
        values = self.time_series.values
        stdev = numpy.std(values)
        for i, (timestamp, value) in enumerate(self.time_series_items):
            if i < self.lag_window_size:
                anom_score = self._compute_anom_score(values[:i + 1], value)
            else:
                anom_score = self._compute_anom_score(values[i - self.lag_window_size: i + 1], value)
            if stdev:
                anom_scores[timestamp] = anom_score / stdev
            else:
                anom_scores[timestamp] = anom_score
        self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
1320676
def display_grid_scores(grid_scores, top=None):
    """"""
    grid_scores = sorted(grid_scores, key=lambda x: x[1], reverse=True)
    if top is not None:
        grid_scores = grid_scores[:top]
        
    # Compute a threshold for staring models with overlapping stderr:
    _, best_mean, best_scores = grid_scores[0]
    threshold = best_mean - 2 * sem(best_scores)
    
    for params, mean_score, scores in grid_scores:
        append_star = mean_score + 2 * sem(scores) > threshold
        print(display_scores(params, scores, append_star=append_star))
1307779
def _set_scores(self):
        
        anom_scores = {}
        self._generate_SAX()
        self._construct_all_SAX_chunk_dict()
        length = self.time_series_length
        lws = self.lag_window_size
        fws = self.future_window_size

        for i, timestamp in enumerate(self.time_series.timestamps):
            if i < lws or i > length - fws:
                anom_scores[timestamp] = 0
            else:
                anom_scores[timestamp] = self._compute_anom_score_between_two_windows(i)
        self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
117281
def compute_rewards(self, scores):
        """"""
        for i in range(len(scores)):
            if i >= self.k:
                scores[i] = 0.
        return scores
254719
def scores_to_probs(scores, proba, eps=0.01):
    """"""
    if np.any(~proba):
        # Need to convert some of the scores into probabilities
        probs = copy.deepcopy(scores)
        n_class = len(proba)
        for m in range(n_class):
            if not proba[m]:
                #TODO: incorporate threshold (currently assuming zero)
                # find most extreme absolute score
                max_extreme_score = max(np.abs(np.min(scores[:,m])),\
                                    np.abs(np.max(scores[:,m])))
                k = np.log((1-eps)/eps)/max_extreme_score # scale factor
                self._probs[:,m] = expit(k * self.scores[:,m])
        return probs
    else:
        return scores
1318453
def compute(self, runner_results, setup=False, poll=False, ignore_errors=False):
        '''  '''

        for (host, value) in runner_results.get('contacted', {}).iteritems():
            if not ignore_errors and (('failed' in value and bool(value['failed'])) or
                ('rc' in value and value['rc'] != 0)):
                self._increment('failures', host)
            elif 'skipped' in value and bool(value['skipped']):
                self._increment('skipped', host)
            elif 'changed' in value and bool(value['changed']):
                if not setup and not poll:
                    self._increment('changed', host)
                self._increment('ok', host)
            else:
                if not poll or ('finished' in value and bool(value['finished'])):
                    self._increment('ok', host)

        for (host, value) in runner_results.get('dark', {}).iteritems():
            self._increment('dark', host)
270829
def laggeddates(ts, step=1):
    ''''''
    if step == 1:
        dates = ts.dates()
        if not hasattr(dates, 'next'):
            dates = dates.__iter__()
        dt0 = next(dates)
        for dt1 in dates:
            yield dt1, dt0
            dt0 = dt1
    else:
        while done:
            done += 1
            lag.append(next(dates))
        for dt1 in dates:
            lag.append(dt1)
            yield dt1, lag.pop(0)
134120
def _compute(self):
        """"""
        positive_vals, negative_vals = self._get_separated_values()

        if self.logarithmic:
            positive_vals = list(
                filter(lambda x: x > self.zero, positive_vals)
            )
            negative_vals = list(
                filter(lambda x: x > self.zero, negative_vals)
            )

        self._compute_box(positive_vals, negative_vals)
        positive_vals = positive_vals or [self.zero]
        negative_vals = negative_vals or [self.zero]

        self._x_pos = [
            x / self._len for x in range(self._len + 1)
        ] if self._len > 1 else [0, 1]  # Center if only one value

        self._points(self._x_pos)

        self.negative_cumulation = [0] * self._len
        self.positive_cumulation = [0] * self._len

        if self.secondary_series:
            positive_vals, negative_vals = self._get_separated_values(True)
            positive_vals = positive_vals or [self.zero]
            negative_vals = negative_vals or [self.zero]
            self.secondary_negative_cumulation = [0] * self._len
            self.secondary_positive_cumulation = [0] * self._len
            self._pre_compute_secondary(positive_vals, negative_vals)

        self._x_pos = [(i + .5) / self._len for i in range(self._len)]
93615
def rolling_performances(self, timestamp='one_month'):
        '''  '''
        # TODO Study the impact of month choice
        # TODO Check timestamp in an enumeration
        # TODO Implement other benchmarks for perf computation
        # (zipline issue, maybe expected)

        if self.metrics:
            perfs = {}
            length = range(len(self.metrics[timestamp]))
            index = self._get_index(self.metrics[timestamp])
            perf_keys = self.metrics[timestamp][0].keys()
            perf_keys.pop(perf_keys.index('period_label'))

            perfs['period'] = np.array(
                [pd.datetime.date(date) for date in index])
            for key in perf_keys:
                perfs[key] = self._to_perf_array(timestamp, key, length)
        else:
            # TODO Get it from DB if it exists
            raise NotImplementedError()

        return pd.DataFrame(perfs, index=index)
273768
def _analyze(self):
    
    output = defaultdict(list)
    output_by_name = defaultdict(list)
    scores = self.anomaly_detector.get_all_scores()

    if self.anomalies:
      for anomaly in self.anomalies:
        metrix_scores = scores
        start_t, end_t = anomaly.get_time_window()
        t = anomaly.exact_timestamp

        # Compute extended start timestamp and extended end timestamp.
        room = (end_t - start_t) / 2
        if not room:
          room = 30
        extended_start_t = start_t - room
        extended_end_t = end_t + room
        metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t)

        # Adjust the two timestamps if not enough data points are included.
        while len(metrix_scores_cropped) < 2:
          extended_start_t = extended_start_t - room
          extended_end_t = extended_end_t + room
          metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t)

        # Correlate with other metrics
        for entry in self.related_metrices:
          try:
            entry_correlation_result = Correlator(self.metrix, entry, time_period=(extended_start_t, extended_end_t),
                                                  use_anomaly_score=True).get_correlation_result()
            record = extended_start_t, extended_end_t, entry_correlation_result.__dict__, entry
            record_by_name = extended_start_t, extended_end_t, entry_correlation_result.__dict__
            output[t].append(record)
            output_by_name[entry].append(record_by_name)
          except exceptions.NotEnoughDataPoints:
            pass

    self.output = output
    self.output_by_name = output_by_name
154323
def roc(series, window=14):
    
    res = (series - series.shift(window)) / series.shift(window)
    return pd.Series(index=series.index, data=res)
117057
private void computeLOFScores(KNNQuery<O> knnq, DBIDs ids, DoubleDataStore lrds, WritableDoubleDataStore lofs, DoubleMinMax lofminmax) {
    FiniteProgress progressLOFs = LOG.isVerbose() ? new FiniteProgress("Local Outlier Factor (LOF) scores", ids.size(), LOG) : null;
    double lof;
    for(DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
      lof = computeLOFScore(knnq, iter, lrds);
      lofs.putDouble(iter, lof);
      // update minimum and maximum
      lofminmax.put(lof);
      LOG.incrementProcessed(progressLOFs);
    }
    LOG.ensureCompleted(progressLOFs);
  }
57711
def score_segmentation(segmentation, table):
    """"""
    stroke_nr = sum(1 for symbol in segmentation for stroke in symbol)
    score = 1
    for i in range(stroke_nr):
        for j in range(i+1, stroke_nr):
            qval = q(segmentation, i, j)
            if qval:
                score *= table[i][j]
            else:
                score *= table[j][i]
    return score
19690
def _find_max_lag(x, rho_limit=0.05, maxmaxlag=20000, verbose=0):
    """"""

    # Fetch autocovariance matrix
    acv = autocov(x)
    # Calculate rho
    rho = acv[0, 1] / acv[0, 0]

    lam = -1. / np.log(abs(rho))

    # Initial guess at 1.5 times lambda (i.e. 3 times mean life)
    maxlag = int(np.floor(3. * lam)) + 1

    # Jump forward 1% of lambda to look for rholimit threshold
    jump = int(np.ceil(0.01 * lam)) + 1

    T = len(x)

    while ((abs(rho) > rho_limit) & (maxlag < min(T / 2, maxmaxlag))):

        acv = autocov(x, maxlag)
        rho = acv[0, 1] / acv[0, 0]
        maxlag += jump

    # Add 30% for good measure
    maxlag = int(np.floor(1.3 * maxlag))

    if maxlag >= min(T / 2, maxmaxlag):
        maxlag = min(min(T / 2, maxlag), maxmaxlag)
        "maxlag fixed to %d" % maxlag
        return maxlag

    if maxlag <= 1:
        print_("maxlag = %d, fixing value to 10" % maxlag)
        return 10

    if verbose:
        print_("maxlag = %d" % maxlag)
    return maxlag
544088
def generate_anomaly(self, input_word, list_of_dict_words, num):
        

        results = []
        for i in range(0,num):
            index = randint(0,len(list_of_dict_words)-1)
            name = list_of_dict_words[index]
            if name != input_word and name not in results:
                results.append(PataLib().strip_underscore(name))
            else:
                i = i +1
        results = {'input' : input_word, 'results' : results, 'category' : 'anomaly'} 
        return results
265807
def compute_rewards(self, scores):
        """"""
        if len(scores) > self.k:
            scores = np.copy(scores)
            inds = np.argsort(scores)[:-self.k]
            scores[inds] = np.nan

        return list(scores)
717823
def weighted_moving_average(data, period):
    
    catch_errors.check_for_period_error(data, period)
    k = (period * (period + 1)) / 2.0

    wmas = []
    for idx in range(0, len(data)-period+1):
        product = [data[idx + period_idx] * (period_idx + 1) for period_idx in range(0, period)]
        wma = sum(product) / k
        wmas.append(wma)
    wmas = fill_for_noncomputable_vals(data, wmas)

    return wmas