Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
from statsmodels.tsa.seasonal import STL
|
5 |
-
from pmdarima import auto_arima
|
6 |
import matplotlib.pyplot as plt
|
7 |
|
8 |
-
def
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
decomposition = STL(series, seasonal=seasonality_window).fit()
|
16 |
-
trend = decomposition.trend
|
17 |
-
seasonal = decomposition.seasonal
|
18 |
-
residual = decomposition.resid
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
# Fit the Isolation Forest model to detect anomalies in the detrended series
|
24 |
-
model = IsolationForest(contamination=anomaly_threshold)
|
25 |
-
model.fit(detrended_series.values.reshape(-1, 1))
|
26 |
-
|
27 |
-
# Predict outliers in the detrended series
|
28 |
-
outlier_predictions = model.predict(detrended_series.values.reshape(-1, 1))
|
29 |
-
anomalies = detrended_series[outlier_predictions == -1]
|
30 |
|
31 |
return anomalies.index
|
32 |
|
@@ -41,17 +27,14 @@ def plot_anomalies(series, anomalies):
|
|
41 |
plt.show()
|
42 |
|
43 |
# Função para o Gradio
|
44 |
-
def anomaly_detection_app(
|
45 |
-
# Carregar a série temporal a partir do arquivo
|
46 |
-
df = pd.read_csv(
|
47 |
df['Date'] = pd.to_datetime(df['Date'])
|
48 |
df = df.set_index('Date')
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
# Detectar anomalias na série temporal
|
54 |
-
anomalies = detect_anomalies(df['Value'], seasonality_window=periodicity)
|
55 |
|
56 |
# Plotar a série temporal com as anomalias destacadas
|
57 |
plot_anomalies(df['Value'], anomalies)
|
@@ -61,10 +44,14 @@ def anomaly_detection_app(time_series_txt):
|
|
61 |
# Interface do Gradio
|
62 |
iface = gr.Interface(
|
63 |
fn=anomaly_detection_app,
|
64 |
-
inputs=
|
|
|
|
|
|
|
|
|
65 |
outputs="text",
|
66 |
title="Análise de Anomalias em Séries Temporais",
|
67 |
-
description="Este aplicativo detecta anomalias em uma série temporal carregada através de um arquivo
|
68 |
live=True
|
69 |
)
|
70 |
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
|
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
|
6 |
+
def detect_anomalies(series, window_size=7, threshold=2.5):
|
7 |
+
# Calculate the rolling mean and standard deviation using the specified window size
|
8 |
+
rolling_mean = series.rolling(window=window_size, center=True).mean()
|
9 |
+
rolling_std = series.rolling(window=window_size, center=True).std()
|
10 |
|
11 |
+
# Calculate the z-score for each data point
|
12 |
+
z_scores = np.abs((series - rolling_mean) / rolling_std)
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Detect anomalies based on the threshold
|
15 |
+
anomalies = series[z_scores > threshold]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
return anomalies.index
|
18 |
|
|
|
27 |
plt.show()
|
28 |
|
29 |
# Função para o Gradio
|
30 |
+
def anomaly_detection_app(time_series_csv, window_size=7, threshold=2.5):
|
31 |
+
# Carregar a série temporal a partir do arquivo CSV
|
32 |
+
df = pd.read_csv(time_series_csv)
|
33 |
df['Date'] = pd.to_datetime(df['Date'])
|
34 |
df = df.set_index('Date')
|
35 |
|
36 |
+
# Detectar anomalias na série temporal usando a média móvel
|
37 |
+
anomalies = detect_anomalies(df['Value'], window_size=window_size, threshold=threshold)
|
|
|
|
|
|
|
38 |
|
39 |
# Plotar a série temporal com as anomalias destacadas
|
40 |
plot_anomalies(df['Value'], anomalies)
|
|
|
44 |
# Interface do Gradio
|
45 |
iface = gr.Interface(
|
46 |
fn=anomaly_detection_app,
|
47 |
+
inputs=[
|
48 |
+
gr.inputs.File(label="Carregar série temporal (CSV)"),
|
49 |
+
gr.inputs.Number(default=7, label="Tamanho da janela da média móvel"),
|
50 |
+
gr.inputs.Number(default=2.5, label="Threshold para anomalias (em desvios-padrão)")
|
51 |
+
],
|
52 |
outputs="text",
|
53 |
title="Análise de Anomalias em Séries Temporais",
|
54 |
+
description="Este aplicativo detecta anomalias em uma série temporal carregada através de um arquivo CSV usando a média móvel.",
|
55 |
live=True
|
56 |
)
|
57 |
|