import numpy as np
import pandas as pd
import plotly.graph_objects as go
from spotforecast2_safe.preprocessing.outlier import get_outliers
from spotforecast2.plots.outlier_plots import visualize_outliers_plotly_scatter
rng = np.random.default_rng(0)
dates = pd.date_range("2024-01-01", periods=30, freq="h")
normal_vals = rng.normal(loc=20.0, scale=2.0, size=28)
outlier_vals_arr = [60.0, 65.0] # two obvious outliers
data_original = pd.DataFrame(
{"temperature": np.concatenate([normal_vals, outlier_vals_arr])},
index=dates,
)
data_cleaned = data_original.copy()
# Verify that get_outliers detects the planted outliers before plotting
detected = get_outliers(
data_original, data_original=data_original, contamination=0.07
)
assert len(detected["temperature"]) >= 1, "Expected at least one outlier"
# Renders an interactive Plotly time series with outliers marked in red
visualize_outliers_plotly_scatter(
data_cleaned,
data_original,
columns=["temperature"],
contamination=0.07,
)
print(f"Detected {len(detected['temperature'])} outlier(s) in 'temperature'")