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

Update tools/forecaster.py

Browse files
Files changed (1) hide show
  1. tools/forecaster.py +29 -35
tools/forecaster.py CHANGED
@@ -6,61 +6,55 @@ 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, 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()
 
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, saving
10
+ the PNG under /tmp and returning the forecast table as text.
11
  """
12
+ # 1) Load & parse dates
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 rows
20
  df[value_col] = pd.to_numeric(df[value_col], errors="coerce")
21
  df = df.dropna(subset=[date_col, value_col])
 
22
  if df.empty:
23
  return f"❌ No valid data for '{value_col}'."
24
+
25
+ # 3) Sort by date, set index, then collapse any duplicate timestamps
26
+ df = df.sort_values(date_col).set_index(date_col)
27
+ # If you have multiple rows for the same timestamp, take their mean
28
+ df = df[[value_col]].groupby(level=0).mean()
29
+
30
+ # 4) Infer frequency (e.g. 'D', 'M', etc.) and reindex
31
  freq = pd.infer_freq(df.index)
32
  if freq is None:
33
+ freq = "D" # fallback to daily
 
34
  df = df.asfreq(freq)
35
+
36
+ # 5) Fit ARIMA
37
  try:
38
  model = ARIMA(df[value_col], order=(1, 1, 1))
39
  model_fit = model.fit()
40
  except Exception as e:
41
  return f"❌ ARIMA fitting failed: {e}"
42
+
43
+ # 6) Forecast with a proper DatetimeIndex
44
  fc_res = model_fit.get_forecast(steps=3)
45
+ forecast = fc_res.predicted_mean # pd.Series indexed by future dates
46
+
47
+ # 7) Plot history + forecast
48
  fig = go.Figure()
49
+ fig.add_scatter(x=df.index, y=df[value_col], mode="lines", name=value_col)
50
+ fig.add_scatter(x=forecast.index, y=forecast, mode="lines+markers", name="Forecast")
 
 
 
 
 
 
51
  fig.update_layout(
52
  title=f"{value_col} Forecast",
53
+ xaxis_title=date_col,
54
+ yaxis_title=value_col,
55
+ template="plotly_dark",
56
  )
57
+ fig.write_image("forecast_plot.png") # safely lands in /tmp via monkey-patch
58
+
59
+ # 8) Return the forecast table as plain text
60
+ return forecast.to_frame(name="Forecast").to_string()