mgbam commited on
Commit
3dbe4ef
Β·
verified Β·
1 Parent(s): 9db6dea

Update tools/forecaster.py

Browse files
Files changed (1) hide show
  1. 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 to /tmp via our safe write monkey-patch, returns forecast table as text.
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
- # 3) Coerce metric to numeric, drop invalid
22
  df[value_col] = pd.to_numeric(df[value_col], errors="coerce")
23
- series = df.set_index(date_col)[value_col].dropna()
24
-
25
- if series.empty:
26
- return f"❌ Column '{value_col}' has no valid numeric data after coercion."
27
-
 
 
 
 
 
 
 
 
 
28
  # 4) Fit ARIMA
29
  try:
30
- model = ARIMA(series, order=(1, 1, 1))
31
  model_fit = model.fit()
32
  except Exception as e:
33
  return f"❌ ARIMA fitting failed: {e}"
34
-
35
- # 5) Forecast & plot
36
- forecast = model_fit.forecast(steps=3)
 
 
 
37
  fig = go.Figure()
38
- fig.add_scatter(x=series.index, y=series, mode="lines", name=value_col)
39
- fig.add_scatter(x=forecast.index, y=forecast, mode="lines", name="Forecast")
40
- fig.update_layout(title=f"{value_col} Forecast", template="plotly_dark")
41
- fig.write_image("forecast_plot.png") # goes into /tmp thanks to our monkey-patch
42
-
43
- # 6) Return textual table
44
- return forecast.to_frame(name="Forecast").to_string()
 
 
 
 
 
 
 
 
 
 
 
 
 
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()