Skip to content

Commit 6d67557

Browse files
authored
Merge pull request #108 from andrewm4894/clean-up-change-plot-labels
Clean up change plot labels
2 parents 6e75e94 + 85e8557 commit 6d67557

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

anomstack/alerts/email.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def send_email_with_plot(
2424
attachment_name,
2525
threshold=0.8,
2626
score_col="metric_score_smooth",
27+
score_title="anomaly_score"
2728
) -> None:
2829
"""
2930
Sends an email with a plot attached.
@@ -38,6 +39,8 @@ def send_email_with_plot(
3839
Defaults to 0.8.
3940
score_col (str, optional): The name of the column containing the
4041
anomaly scores. Defaults to 'metric_score_smooth'.
42+
score_title (str, optional): The title of the score plot. Defaults to
43+
'anomaly_score'.
4144
4245
Returns:
4346
None
@@ -54,7 +57,9 @@ def send_email_with_plot(
5457
with tempfile.NamedTemporaryFile(
5558
prefix=attachment_name, suffix=".png", delete=False
5659
) as temp:
57-
fig = make_alert_plot(df, metric_name, threshold, score_col)
60+
fig = make_alert_plot(
61+
df, metric_name, threshold, score_col, score_title
62+
)
5863
fig.savefig(temp.name)
5964

6065
msg = MIMEMultipart()

anomstack/alerts/send.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def send_alert(
2020
tags=None,
2121
score_col: str = "metric_score_smooth",
2222
metric_timestamp=None,
23+
score_title="anomaly_score",
2324
) -> pd.DataFrame:
2425
"""
2526
Sends an alert using the specified alert methods.
@@ -57,6 +58,7 @@ def send_alert(
5758
threshold=threshold,
5859
score_col=score_col,
5960
metric_timestamp=metric_timestamp,
61+
score_title=score_title,
6062
)
6163
if "email" in alert_methods:
6264
send_email_with_plot(
@@ -67,6 +69,7 @@ def send_alert(
6769
attachment_name=metric_name,
6870
threshold=threshold,
6971
score_col=score_col,
72+
score_title=score_title,
7073
)
7174

7275
return df

anomstack/alerts/slack.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def send_alert_slack_with_plot(
136136
score_col="metric_score_smooth",
137137
channel_name=None,
138138
metric_timestamp=None,
139+
score_title="anomaly_score",
139140
) -> None:
140141
"""
141142
Sends an alert to Slack with a plot attached.
@@ -149,7 +150,9 @@ def send_alert_slack_with_plot(
149150
suffix=".png",
150151
delete=False
151152
) as temp:
152-
fig = make_alert_plot(df, metric_name, threshold, score_col)
153+
fig = make_alert_plot(
154+
df, metric_name, threshold, score_col, score_title
155+
)
153156
fig.savefig(temp.name)
154157
plt.close(fig)
155158

anomstack/jobs/change.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def alert(df_change_alerts) -> pd.DataFrame:
154154
alert_methods=alert_methods,
155155
tags=tags,
156156
score_col="metric_score",
157+
score_title="change_score",
157158
)
158159

159160
return df_change_alerts

anomstack/jobs/score.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def score(df) -> pd.DataFrame:
149149
scores = pd.DataFrame(scores).min(axis=1).values
150150
else:
151151
raise ValueError(
152-
f"model_combination_method {model_combination_method} not supported."
152+
f"model_combination_method {model_combination_method} "
153+
f"not supported."
153154
)
154155

155156
# create initial df_score

anomstack/plots/plot.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def make_alert_plot(
1515
metric_name: str,
1616
threshold: float = 0.8,
1717
score_col: str = "metric_score_smooth",
18+
score_title: str = "anomaly_score",
1819
) -> Figure:
1920
"""
2021
Creates a plot with two subplots: one for the metric values and another
@@ -27,6 +28,8 @@ def make_alert_plot(
2728
Defaults to 0.8.
2829
score_col (str, optional): The name of the column containing the
2930
anomaly scores. Defaults to 'metric_score_smooth'.
31+
score_title (str, optional): The label for the y-axis of the score
32+
plot.
3033
3134
Returns:
3235
plt: The matplotlib figure object.
@@ -51,7 +54,7 @@ def make_alert_plot(
5154
ax1.legend(loc="upper left")
5255

5356
ax2 = df_plot[score_col].plot(
54-
title="Score",
57+
title=score_title,
5558
ax=axes[1],
5659
rot=45,
5760
linestyle="--",
@@ -74,7 +77,7 @@ def make_alert_plot(
7477
[f'{item.strftime("%Y-%m-%d %H:%M")}' for item in df_plot.index.tolist()],
7578
rotation=45,
7679
)
77-
ax2.set_ylabel("Score")
80+
ax2.set_ylabel(score_title)
7881
if df_plot[score_col].max() <= 1:
7982
ax2.set_ylim(0, 1)
8083
else:

0 commit comments

Comments
 (0)