NSruman commited on
Commit
d7cdf88
Β·
verified Β·
1 Parent(s): 92d5def

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -1,7 +1,7 @@
1
  # app.py β€” DataSynthis_ML_JobTask
2
  # Evaluate pretrained ARIMA + LSTM on a user-picked historical window inside 2020-10-03..2025-10-03
3
 
4
- import os, math, json, pickle, traceback
5
  from typing import Tuple
6
  import numpy as np
7
  import pandas as pd
@@ -15,6 +15,9 @@ import matplotlib.pyplot as plt
15
  from statsmodels.tsa.arima.model import ARIMA
16
  from sklearn.metrics import mean_squared_error, mean_absolute_error, mean_absolute_percentage_error
17
 
 
 
 
18
  # -------------------------
19
  # Historical universe bounds (DO NOT change)
20
  # -------------------------
@@ -37,6 +40,14 @@ _LSTM_META = None
37
 
38
  POPULAR = ["AAPL","MSFT","GOOGL","AMZN","TSLA","NVDA","META","NFLX","IBM","ORCL"]
39
 
 
 
 
 
 
 
 
 
40
  # =========================
41
  # Data utilities
42
  # =========================
@@ -67,7 +78,6 @@ def build_train_test_for_window(df: pd.DataFrame, start_sel: date, end_sel: date
67
  Train = all history strictly before start_sel (business days).
68
  Test = start_sel..end_sel inclusive (business days).
69
  """
70
- # align selection to universe
71
  start_sel = clip_to_universe(start_sel)
72
  end_sel = clip_to_universe(end_sel)
73
  if start_sel > end_sel:
@@ -111,9 +121,8 @@ def arima_roll_eval(train_s: pd.Series, test_s: pd.Series, order=(2,1,2)):
111
  preds, trues = [], []
112
  for i in range(len(test_s)):
113
  fcast = res.forecast(1)
114
- fval = float(np.asarray(fcast).reshape(-1)[0]) # robust scalar
115
- preds.append(fval)
116
- trues.append(float(test_s.iloc[i]))
117
  try:
118
  res = res.append(test_s.iloc[i:i+1]) # update state with realized value
119
  except Exception:
@@ -202,7 +211,7 @@ def lstm_roll_eval(train_s: pd.Series, test_s: pd.Series):
202
  yhat_scaled = model.predict(x, verbose=0).reshape(-1)[0]
203
  yhat = _minmax_unscale_1d([yhat_scaled], vmin, vmax, fr_low, fr_high)[0]
204
  preds.append(float(yhat))
205
- trues.append(float(test_s.iloc[i]))
206
  # update history with the REAL observed value (fair evaluation)
207
  full_hist = pd.concat([full_hist, test_s.iloc[i:i+1]])
208
 
@@ -248,6 +257,7 @@ def run_evaluation(ticker_choice: str, start_sel_txt: str, end_sel_txt: str):
248
  e_sel = clip_to_universe(parse_date(end_sel_txt))
249
  if s_sel > e_sel:
250
  raise ValueError("Start date must be before or equal to end date.")
 
251
  # Fetch universe data and build window train/test
252
  df = fetch_universe_data(ticker)
253
  train_s, test_s = build_train_test_for_window(df, s_sel, e_sel)
@@ -287,11 +297,11 @@ def run_evaluation(ticker_choice: str, start_sel_txt: str, end_sel_txt: str):
287
  fig_lstm = plot_series(pd.to_datetime(dates), actual, lstm_p,
288
  f"LSTM (lookback={lookback}) β€” Actual vs Prediction")
289
 
290
- # KPI Markdown
291
  kpi = (
292
  f"**Ticker:** {ticker} | **Window:** {s_sel} β†’ {e_sel} \n"
293
  f"**ARIMA {order}** β€” RMSE: {ar_tbl['RMSE'][0]:.2f} | MAE: {ar_tbl['MAE'][0]:.2f} | MAPE: {ar_tbl['MAPE (%)'][0]:.2f}% \n"
294
- f"**LSTM (lookback={lookback})** β€” RMSE: {lstm_tbl['RMSE'][0]:.2f} | MAE: {lstm_tbl['MAE'][0]:.2f} | MAPE: {lstm_tbl['MAPE (%)'][0]::.2f}%"
295
  )
296
 
297
  return kpi, fig_arima, fig_lstm, table
@@ -332,6 +342,7 @@ with gr.Blocks(title="DataSynthis_ML_JobTask", theme=gr.themes.Soft()) as demo:
332
  run_btn = gr.Button("Run Evaluation", variant="primary")
333
 
334
  gr.Markdown("### Results")
 
335
  kpi = gr.Markdown(label="Model scores (lower = better)", value="")
336
  with gr.Row():
337
  chart_arima = gr.Plot(label="ARIMA β€” Actual vs Predicted")
 
1
  # app.py β€” DataSynthis_ML_JobTask
2
  # Evaluate pretrained ARIMA + LSTM on a user-picked historical window inside 2020-10-03..2025-10-03
3
 
4
+ import os, math, json, pickle, traceback, warnings
5
  from typing import Tuple
6
  import numpy as np
7
  import pandas as pd
 
15
  from statsmodels.tsa.arima.model import ARIMA
16
  from sklearn.metrics import mean_squared_error, mean_absolute_error, mean_absolute_percentage_error
17
 
18
+ # Silence pandas/np deprecation spam in logs
19
+ warnings.simplefilter("ignore", FutureWarning)
20
+
21
  # -------------------------
22
  # Historical universe bounds (DO NOT change)
23
  # -------------------------
 
40
 
41
  POPULAR = ["AAPL","MSFT","GOOGL","AMZN","TSLA","NVDA","META","NFLX","IBM","ORCL"]
42
 
43
+ # =========================
44
+ # Small helpers
45
+ # =========================
46
+ def _as_scalar(x) -> float:
47
+ """Robustly turn a pandas/np value into a plain float without FutureWarnings."""
48
+ arr = np.asarray(x).reshape(-1,)
49
+ return float(arr[0])
50
+
51
  # =========================
52
  # Data utilities
53
  # =========================
 
78
  Train = all history strictly before start_sel (business days).
79
  Test = start_sel..end_sel inclusive (business days).
80
  """
 
81
  start_sel = clip_to_universe(start_sel)
82
  end_sel = clip_to_universe(end_sel)
83
  if start_sel > end_sel:
 
121
  preds, trues = [], []
122
  for i in range(len(test_s)):
123
  fcast = res.forecast(1)
124
+ preds.append(_as_scalar(fcast))
125
+ trues.append(_as_scalar(test_s.iloc[i]))
 
126
  try:
127
  res = res.append(test_s.iloc[i:i+1]) # update state with realized value
128
  except Exception:
 
211
  yhat_scaled = model.predict(x, verbose=0).reshape(-1)[0]
212
  yhat = _minmax_unscale_1d([yhat_scaled], vmin, vmax, fr_low, fr_high)[0]
213
  preds.append(float(yhat))
214
+ trues.append(_as_scalar(test_s.iloc[i]))
215
  # update history with the REAL observed value (fair evaluation)
216
  full_hist = pd.concat([full_hist, test_s.iloc[i:i+1]])
217
 
 
257
  e_sel = clip_to_universe(parse_date(end_sel_txt))
258
  if s_sel > e_sel:
259
  raise ValueError("Start date must be before or equal to end date.")
260
+
261
  # Fetch universe data and build window train/test
262
  df = fetch_universe_data(ticker)
263
  train_s, test_s = build_train_test_for_window(df, s_sel, e_sel)
 
297
  fig_lstm = plot_series(pd.to_datetime(dates), actual, lstm_p,
298
  f"LSTM (lookback={lookback}) β€” Actual vs Prediction")
299
 
300
+ # KPI Markdown (fixed format specifiers)
301
  kpi = (
302
  f"**Ticker:** {ticker} | **Window:** {s_sel} β†’ {e_sel} \n"
303
  f"**ARIMA {order}** β€” RMSE: {ar_tbl['RMSE'][0]:.2f} | MAE: {ar_tbl['MAE'][0]:.2f} | MAPE: {ar_tbl['MAPE (%)'][0]:.2f}% \n"
304
+ f"**LSTM (lookback={lookback})** β€” RMSE: {lstm_tbl['RMSE'][0]:.2f} | MAE: {lstm_tbl['MAE'][0]:.2f} | MAPE: {lstm_tbl['MAPE (%)'][0]:.2f}%"
305
  )
306
 
307
  return kpi, fig_arima, fig_lstm, table
 
342
  run_btn = gr.Button("Run Evaluation", variant="primary")
343
 
344
  gr.Markdown("### Results")
345
+ kpi = gr.Mardown if hasattr(gr, "Mardown") else gr.Markdown # just in case
346
  kpi = gr.Markdown(label="Model scores (lower = better)", value="")
347
  with gr.Row():
348
  chart_arima = gr.Plot(label="ARIMA β€” Actual vs Predicted")