Skip to content

Commit

Permalink
fix(gpd-plot): handle non-zero params and exceedances in POTDetector (
Browse files Browse the repository at this point in the history
#26)

* fix(gpd_plot): add params processing before using as argument for "gpd+ov"

* fix(gpd_plot): handle non-zero exceedances in models, not in plot

* refactor(example-notebook): create new flow for the notebook using `POTDetector`

* fix(pot-detector-plot): handle non-zero anomaly scores in method instead in plot function

---------

Co-authored-by: N. L <nino@pleno.earth>
  • Loading branch information
Aeternalis-Ingenium and ninopleno authored Dec 11, 2023
1 parent feaffa3 commit b4aa501
Show file tree
Hide file tree
Showing 3 changed files with 6,182 additions and 302 deletions.
6,462 changes: 6,169 additions & 293 deletions docs/example.ipynb

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/anomalytics/models/peaks_over_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ def plot(
th_line_width: int = 2,
alpha: float = 0.8,
):
if isinstance(self.__exceedance, pd.Series):
nonzero_exceedences = [exceedence for exceedence in self.__exceedance.values if exceedence > 0]
if plot_type == "l":
plot_line(
dataset=self.__dataset,
Expand All @@ -566,8 +568,10 @@ def plot(
alpha=alpha,
)
elif plot_type == "l+ath":
if isinstance(self.__anomaly_score, pd.Series):
nonzero_anomaly_scores = self.__anomaly_score[self.__anomaly_score.values > 0]
plot_line(
dataset=self.__exceedance,
dataset=nonzero_anomaly_scores,
threshold=self.__anomaly_threshold,
title=title,
xlabel=xlabel,
Expand Down Expand Up @@ -611,7 +615,7 @@ def plot(
)
elif plot_type == "gpd":
plot_gen_pareto(
dataset=self.__exceedance,
dataset=nonzero_exceedences,
title=title,
xlabel=xlabel,
ylabel=ylabel,
Expand All @@ -623,8 +627,9 @@ def plot(
params=None,
)
elif plot_type == "gpd+ov":
last_nonzero_params = self.__get_nonzero_params[-1]
plot_gen_pareto(
dataset=self.__exceedance,
dataset=nonzero_exceedences,
title=title,
xlabel=xlabel,
ylabel=ylabel,
Expand All @@ -633,7 +638,7 @@ def plot(
plot_height=plot_height,
plot_color=plot_color,
alpha=alpha,
params=self.__params,
params=last_nonzero_params,
)

def __str__(self) -> str:
Expand Down
9 changes: 4 additions & 5 deletions src/anomalytics/plots/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ def plot_gen_pareto(
plot_height: int = 8,
plot_color: str = "black",
alpha: float = 0.8,
params: typing.Union[typing.Dict, None] = None,
params: typing.Optional[typing.Dict] = None,
):
fig = plt.figure(figsize=(plot_width, plot_height))

nonzero_exceedences = [exceedence for exceedence in dataset if exceedence > 0]
if params:
param_label = f"\n{round(params['c'], 3)}\n{round(params['loc'], 3)}\n{round(params['scale'], 3)}\n"
overlay = np.linspace(
stats.genpareto.ppf(0.1, c=params["c"], loc=params["loc"], scale=params["scale"]),
stats.genpareto.ppf(0.999, c=params["c"], loc=params["loc"], scale=params["scale"]),
len(nonzero_exceedences),
len(dataset),
)
plt.plot(
overlay,
Expand All @@ -99,12 +98,12 @@ def plot_gen_pareto(
label=f"\nFitted Params:{param_label}",
)
plt.hist(
nonzero_exceedences,
dataset,
bins=bins,
density=True,
alpha=alpha,
color=plot_color,
label=f"{len(nonzero_exceedences)}",
label=f"{len(dataset)}",
)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
Expand Down

0 comments on commit b4aa501

Please sign in to comment.