Spaces:
Running
Running
Update tools/forecaster.py
Browse files- tools/forecaster.py +45 -23
tools/forecaster.py
CHANGED
@@ -6,39 +6,61 @@ import plotly.graph_objects as go
|
|
6 |
|
7 |
def forecast_metric_tool(file_path: str, date_col: str, value_col: str):
|
8 |
"""
|
9 |
-
Forecast next 3 periods for any numeric metric.
|
10 |
-
Saves PNG
|
11 |
"""
|
12 |
-
# 1) Load
|
13 |
df = pd.read_csv(file_path)
|
14 |
-
|
15 |
-
# 2) Parse dates
|
16 |
try:
|
17 |
df[date_col] = pd.to_datetime(df[date_col])
|
18 |
except Exception:
|
19 |
return f"β Could not parse '{date_col}' as dates."
|
20 |
-
|
21 |
-
#
|
22 |
df[value_col] = pd.to_numeric(df[value_col], errors="coerce")
|
23 |
-
|
24 |
-
|
25 |
-
if
|
26 |
-
return f"β
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# 4) Fit ARIMA
|
29 |
try:
|
30 |
-
model = ARIMA(
|
31 |
model_fit = model.fit()
|
32 |
except Exception as e:
|
33 |
return f"β ARIMA fitting failed: {e}"
|
34 |
-
|
35 |
-
# 5)
|
36 |
-
|
|
|
|
|
|
|
37 |
fig = go.Figure()
|
38 |
-
fig.add_scatter(
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def forecast_metric_tool(file_path: str, date_col: str, value_col: str):
|
8 |
"""
|
9 |
+
Forecast next 3 periods for any numeric metric, and return a textual table.
|
10 |
+
Saves a date-indexed PNG under /tmp via our safe monkey-patch.
|
11 |
"""
|
12 |
+
# 1) Load & parse
|
13 |
df = pd.read_csv(file_path)
|
|
|
|
|
14 |
try:
|
15 |
df[date_col] = pd.to_datetime(df[date_col])
|
16 |
except Exception:
|
17 |
return f"β Could not parse '{date_col}' as dates."
|
18 |
+
|
19 |
+
# 2) Coerce metric to numeric & drop invalid
|
20 |
df[value_col] = pd.to_numeric(df[value_col], errors="coerce")
|
21 |
+
df = df.dropna(subset=[date_col, value_col])
|
22 |
+
|
23 |
+
if df.empty:
|
24 |
+
return f"β No valid data for '{value_col}'."
|
25 |
+
|
26 |
+
# 3) Sort and set index, infer frequency
|
27 |
+
df = df.sort_values(date_col)
|
28 |
+
df.set_index(date_col, inplace=True)
|
29 |
+
freq = pd.infer_freq(df.index)
|
30 |
+
if freq is None:
|
31 |
+
# fallback to daily if pandas can't infer
|
32 |
+
freq = "D"
|
33 |
+
df = df.asfreq(freq)
|
34 |
+
|
35 |
# 4) Fit ARIMA
|
36 |
try:
|
37 |
+
model = ARIMA(df[value_col], order=(1, 1, 1))
|
38 |
model_fit = model.fit()
|
39 |
except Exception as e:
|
40 |
return f"β ARIMA fitting failed: {e}"
|
41 |
+
|
42 |
+
# 5) Get a proper date-indexed forecast
|
43 |
+
fc_res = model_fit.get_forecast(steps=3)
|
44 |
+
forecast = fc_res.predicted_mean # a pd.Series with a DatetimeIndex
|
45 |
+
|
46 |
+
# 6) Plot historical + forecast
|
47 |
fig = go.Figure()
|
48 |
+
fig.add_scatter(
|
49 |
+
x=df.index, y=df[value_col],
|
50 |
+
mode="lines", name=value_col
|
51 |
+
)
|
52 |
+
fig.add_scatter(
|
53 |
+
x=forecast.index, y=forecast,
|
54 |
+
mode="lines+markers", name="Forecast"
|
55 |
+
)
|
56 |
+
fig.update_layout(
|
57 |
+
title=f"{value_col} Forecast",
|
58 |
+
xaxis_title=str(date_col),
|
59 |
+
yaxis_title=str(value_col),
|
60 |
+
template="plotly_dark"
|
61 |
+
)
|
62 |
+
fig.write_image("forecast_plot.png")
|
63 |
+
|
64 |
+
# 7) Return the forecast table as text
|
65 |
+
tbl = forecast.to_frame(name="Forecast")
|
66 |
+
return tbl.to_string()
|