def _detect_anomalies(self):
anom_scores = self.anom_scores<EOL>max_anom_score = anom_scores.max()<EOL>anomalies = []<EOL>if max_anom_score:<EOL><INDENT>threshold = self.threshold or max_anom_score * self.score_percent_threshold<EOL>intervals = []<EOL>start, end = None, None<EOL>for timestamp, value in anom_scores.iteritems():<EOL><INDENT>if value > threshold:<EOL><INDENT>end = timestamp<EOL>if not start:<EOL><INDENT>start = timestamp<EOL><DEDENT><DEDENT>elif start and end is not None:<EOL><INDENT>intervals.append([start, end])<EOL>start = None<EOL>end = None<EOL><DEDENT><DEDENT>if start is not None:<EOL><INDENT>intervals.append([start, end])<EOL><DEDENT>for interval_start, interval_end in intervals:<EOL><INDENT>interval_series = anom_scores.crop(interval_start, interval_end)<EOL>self.refine_algorithm_params['<STR_LIT>'] = interval_series<EOL>refine_algorithm = self.refine_algorithm(**self.refine_algorithm_params)<EOL>scores = refine_algorithm.run()<EOL>max_refine_score = scores.max()<EOL>max_refine_timestamp = scores.timestamps[scores.values.index(max_refine_score)]<EOL>anomaly = Anomaly(interval_start, interval_end, interval_series.max(), max_refine_timestamp)<EOL>anomalies.append(anomaly)<EOL><DEDENT><DEDENT>self.anomalies = anomalies<EOL>
Detect anomalies using a threshold on anomaly scores.