Skip to content

Commit

Permalink
windstats runner comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yu-wang authored Jun 18, 2024
1 parent 40da7c0 commit 5815dd4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions merlion/models/anomaly/windstats_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ def __init__(self, threshold, enable_weekly = True, enable_monthly = True, post_
# If apply post rules on anomaly score
self.post_rule = post_rule_on_anom_score

# Intialize according model if enable weekly/monthly analysis
if self.enable_weekly:
self.model_weekly = WindStats(WeeklyWindStatsConfig)

if self.enable_monthly:
self.model_monthly = MonthlyWindStats(MonthlyWindStatsConfig)

# Identify anomaly based on the hard threshold.
def anomalyByScore(self, scores, threshold):
labels = scores.copy()
labels.loc[abs(labels["anom_score"]) <= threshold] = 0
Expand All @@ -38,6 +39,7 @@ def anomalyByScore(self, scores, threshold):
labels.rename(columns = {"anom_score": "anomaly"}, inplace = True)
return labels

# Filter anomaly scores based on post rules. Same as "get_anomaly_label" in WindStats
def get_anomaly_label(self, model, ts):
scores = model.train(ts)
return model.post_rule(scores) if model.post_rule is not None else scores
Expand All @@ -48,9 +50,7 @@ def run(self, ts):
scores_weekly = self.get_anomaly_label(self.model_weekly, ts).to_pd()
else:
scores_weekly = self.model_weekly.train(ts).to_pd()
print(scores_weekly)
labels_weekly = self.anomalyByScore(scores_weekly, self.threshold)
print(scores_weekly)

if self.enable_monthly:
if self.post_rule:
Expand All @@ -59,6 +59,7 @@ def run(self, ts):
scores_monthly = self.model_monthly.train(ts).to_pd()
labels_monthly = self.anomalyByScore(scores_monthly, self.threshold)

# Anomaly is identified if and only if it's detected in both weekly and monthly patterns.
if self.enable_weekly and self.enable_monthly:
return scores_weekly, scores_monthly, labels_weekly * labels_monthly
elif self.enable_weekly:
Expand Down

0 comments on commit 5815dd4

Please sign in to comment.