fschwartzer commited on
Commit
a09ca43
1 Parent(s): edfa911

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -100,47 +100,60 @@ def apply_prophet(df_clean):
100
  st.error("DataFrame está vazio após o pré-processamento.")
101
  return pd.DataFrame()
102
 
 
 
 
 
103
  # Criar um DataFrame vazio para armazenar todas as anomalias
104
  all_anomalies = pd.DataFrame()
 
105
  # Processar cada linha no DataFrame
106
  for index, row in df_clean.iterrows():
 
 
107
  data = pd.DataFrame({
108
- 'ds': [col for col in df_clean.columns if isinstance(col, pd.Timestamp)],
109
- 'y': row[[isinstance(col, pd.Timestamp) for col in df_clean.columns]].values
110
  })
111
-
112
- # Remove lines where 'y' is zero until the first non-zero value
113
- data = data[data['y'] > 0].reset_index(drop=True)
 
 
 
 
 
 
114
  if data.empty or len(data) < 2:
115
- print(f"Skipping group {row['Rotulo']} because there are less than 2 non-zero observations.")
116
  continue
117
-
118
- # Try to create and train the model
119
  try:
120
- model = Prophet(interval_width=0.95) # Confidence interval
 
121
  model.fit(data)
122
  except ValueError as e:
123
- print(f"Skipping group {row['Rotulo']} due to error: {e}")
124
  continue
125
-
126
  # Make future predictions
127
  future = model.make_future_dataframe(periods=12, freq='M')
128
  forecast = model.predict(future)
129
-
130
- # Identify points outside the confidence interval
131
- num_real = len(data)
132
- num_forecast = len(forecast)
133
- real_values = list(data['y']) + [None] * (num_forecast - num_real)
134
  forecast['real'] = real_values
135
  anomalies = forecast[(forecast['real'] < forecast['yhat_lower']) | (forecast['real'] > forecast['yhat_upper'])]
136
-
137
- # Add group label to the anomalies
 
 
 
 
138
  anomalies['group'] = row['Rotulo']
139
-
140
- # Append anomalies to the all_anomalies DataFrame
141
  all_anomalies = pd.concat([all_anomalies, anomalies[['ds', 'real', 'group']]], ignore_index=True)
142
 
143
- # Renomear colunas e aplicar filtros
144
  return all_anomalies
145
 
146
  tab1, tab2 = st.tabs(["Meta Prophet", "Microsoft TAPEX"])
 
100
  st.error("DataFrame está vazio após o pré-processamento.")
101
  return pd.DataFrame()
102
 
103
+ # Debugging: Check structure of df_clean
104
+ st.write("Estrutura do DataFrame df_clean:")
105
+ st.write(df_clean)
106
+
107
  # Criar um DataFrame vazio para armazenar todas as anomalias
108
  all_anomalies = pd.DataFrame()
109
+
110
  # Processar cada linha no DataFrame
111
  for index, row in df_clean.iterrows():
112
+ # Extract timestamp and value columns
113
+ date_columns = [col for col in df_clean.columns if isinstance(col, pd.Timestamp)]
114
  data = pd.DataFrame({
115
+ 'ds': date_columns,
116
+ 'y': row[date_columns].values
117
  })
118
+
119
+ # Debugging: Check the data passed into Prophet
120
+ st.write(f"Dados para Prophet - Grupo {row['Rotulo']}:")
121
+ st.write(data)
122
+
123
+ # Remove rows where 'y' is zero or missing
124
+ data = data[data['y'] > 0].dropna().reset_index(drop=True)
125
+
126
+ # Ensure there's enough data for Prophet to run
127
  if data.empty or len(data) < 2:
128
+ st.write(f"Pular grupo {row['Rotulo']} por não ter observações suficientes.")
129
  continue
130
+
 
131
  try:
132
+ # Create and fit the Prophet model
133
+ model = Prophet(interval_width=0.95)
134
  model.fit(data)
135
  except ValueError as e:
136
+ st.write(f"Pular grupo {row['Rotulo']} devido ao erro: {e}")
137
  continue
138
+
139
  # Make future predictions
140
  future = model.make_future_dataframe(periods=12, freq='M')
141
  forecast = model.predict(future)
142
+
143
+ # Add real values and calculate anomalies
144
+ real_values = list(data['y']) + [None] * (len(forecast) - len(data))
 
 
145
  forecast['real'] = real_values
146
  anomalies = forecast[(forecast['real'] < forecast['yhat_lower']) | (forecast['real'] > forecast['yhat_upper'])]
147
+
148
+ # Debugging: Check the anomalies detected
149
+ st.write(f"Anomalias detectadas para o grupo {row['Rotulo']}:")
150
+ st.write(anomalies)
151
+
152
+ # Add group label and append anomalies to all_anomalies DataFrame
153
  anomalies['group'] = row['Rotulo']
 
 
154
  all_anomalies = pd.concat([all_anomalies, anomalies[['ds', 'real', 'group']]], ignore_index=True)
155
 
156
+ # Return the dataframe of all anomalies
157
  return all_anomalies
158
 
159
  tab1, tab2 = st.tabs(["Meta Prophet", "Microsoft TAPEX"])