Jitendra12421 commited on
Commit
d6e9c20
·
verified ·
1 Parent(s): 8e561ff

Upload runtime.py

Browse files
Files changed (1) hide show
  1. runtime.py +7 -72
runtime.py CHANGED
@@ -1144,41 +1144,13 @@ def build_prediction_track_record(
1144
  **(meta or {}),
1145
  }
1146
 
1147
- # 1. Backtest predictions (lowest priority)
1148
- for _, row in t5_test.iterrows():
1149
- pred = row.get("prediction")
1150
- if pd.isna(pred) and "pred" in row:
1151
- pred = "UP" if int(row.get("pred")) == 1 else "DOWN"
1152
- add_prediction(row.get("target_date") or row.get("date"), pred, "T+5 (Backtest)", 10, {"prob_up": row.get("prob_up")})
1153
-
1154
- for _, row in tplus1_test.iterrows():
1155
- pred = row.get("prediction")
1156
- if pd.isna(pred) and "pred" in row:
1157
- pred = "UP" if int(row.get("pred")) == 1 else "DOWN"
1158
- add_prediction(row.get("target_date") or row.get("date"), pred, "T+1 (Backtest)", 15, {"prob_up": row.get("prob_up")})
1159
-
1160
  for _, row in tomorrow_test.iterrows():
1161
  pred = row.get("prediction")
1162
  if pd.isna(pred) and "pred" in row:
1163
  pred = "UP" if int(row.get("pred")) == 1 else "DOWN"
1164
- add_prediction(row.get("target_date") or row.get("date"), pred, "Tomorrow (Backtest)", 20, {"prob_up": row.get("prob_up")})
1165
 
1166
- # 2. Live Ledger predictions (higher priority)
1167
- try:
1168
- ledger = load_live_accuracy()
1169
- for entry in ledger.get("t5", {}).get("entries", []):
1170
- add_prediction(entry.get("date"), entry.get("prediction"), "T+5 (Live)", 30)
1171
- for entry in ledger.get("tplus1", {}).get("entries", []):
1172
- add_prediction(entry.get("date"), entry.get("prediction"), "T+1 (Live)", 35)
1173
- for entry in ledger.get("tomorrow", {}).get("entries", []):
1174
- add_prediction(entry.get("date"), entry.get("prediction"), "Tomorrow (Live)", 40)
1175
- except Exception:
1176
- pass
1177
-
1178
- # 3. Latest predictions (highest priority, overwriting if same date)
1179
- add_prediction(t5_latest.get("target_date") or t5_latest.get("input_date"), t5_latest.get("prediction"), "T+5", 50, {"prob_up": t5_latest.get("prob_up")})
1180
- add_prediction(tplus1_latest.get("target_date"), tplus1_latest.get("prediction"), "T+1", 55, {"prob_up": tplus1_latest.get("prob_up")})
1181
- add_prediction(tomorrow_latest.get("target_date"), tomorrow_latest.get("prediction"), "Tomorrow", 60, {"prob_up": tomorrow_latest.get("prob_up")})
1182
 
1183
  records: list[dict[str, Any]] = []
1184
  for _, row in daily_rows.tail(20).iterrows():
@@ -1527,57 +1499,20 @@ def update_live_accuracy(session_date: date) -> dict[str, Any]:
1527
  except Exception:
1528
  pass
1529
 
1530
- # Backtest baseline stats
1531
- t5_summary = load_model_summary()
1532
- tom_summary = load_tomorrow_summary()
1533
- t1_summary = load_tplus1_summary()
1534
-
1535
- t5_test_total = int(t5_summary.get("test_rows") or len(load_test_predictions()) or 0)
1536
- t5_test_correct = int(round(t5_test_total * float(t5_summary.get("test_accuracy", 0.0))))
1537
-
1538
- tom_test_total = int(tom_summary.get("n_test") or len(load_tomorrow_test_predictions()) or 0)
1539
- tom_test_correct = int(round(tom_test_total * float(tom_summary.get("test_accuracy", 0.0))))
1540
-
1541
- t1_test_total = int(t1_summary.get("test_rows") or len(load_tplus1_test_predictions()) or 0)
1542
- t1_test_correct = int(round(t1_test_total * float(t1_summary.get("test_accuracy", 0.0))))
1543
-
1544
- baselines = {
1545
- "t5": {"total": t5_test_total, "correct": t5_test_correct},
1546
- "tomorrow": {"total": tom_test_total, "correct": tom_test_correct},
1547
- "tplus1": {"total": t1_test_total, "correct": t1_test_correct},
1548
- }
1549
-
1550
  # Recompute summary stats
1551
  for model_id in ("t5", "tomorrow", "tplus1"):
1552
- if model_id not in ledger:
1553
- ledger[model_id] = {"entries": []}
1554
- if "entries" not in ledger[model_id]:
1555
- ledger[model_id]["entries"] = []
1556
-
1557
  entries = ledger[model_id]["entries"]
1558
- live_total = len(entries)
1559
- live_correct = sum(1 for e in entries if e.get("correct"))
1560
-
1561
- base_total = baselines[model_id]["total"]
1562
- base_correct = baselines[model_id]["correct"]
1563
-
1564
- combined_total = base_total + live_total
1565
- combined_correct = base_correct + live_correct
1566
-
1567
- ledger[model_id]["live_total"] = live_total
1568
- ledger[model_id]["live_correct_count"] = live_correct
1569
- ledger[model_id]["live_accuracy"] = live_correct / live_total if live_total > 0 else None
1570
-
1571
- ledger[model_id]["total"] = combined_total
1572
- ledger[model_id]["correct_count"] = combined_correct
1573
- ledger[model_id]["accuracy"] = combined_correct / combined_total if combined_total > 0 else None
1574
 
1575
  save_live_accuracy(ledger)
1576
  clear_dashboard_payload_cache()
1577
  return ledger
1578
 
1579
 
1580
-
1581
  def refresh_market_close_data(session_date: date | None = None) -> dict[str, Any]:
1582
  now = datetime.now(IST)
1583
  session_date = session_date or now.date()
 
1144
  **(meta or {}),
1145
  }
1146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1147
  for _, row in tomorrow_test.iterrows():
1148
  pred = row.get("prediction")
1149
  if pd.isna(pred) and "pred" in row:
1150
  pred = "UP" if int(row.get("pred")) == 1 else "DOWN"
1151
+ add_prediction(row.get("target_date") or row.get("date"), pred, "Tomorrow", 20, {"prob_up": row.get("prob_up")})
1152
 
1153
+ add_prediction(tomorrow_latest.get("target_date"), tomorrow_latest.get("prediction"), "Tomorrow", 40, {"prob_up": tomorrow_latest.get("prob_up")})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1154
 
1155
  records: list[dict[str, Any]] = []
1156
  for _, row in daily_rows.tail(20).iterrows():
 
1499
  except Exception:
1500
  pass
1501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1502
  # Recompute summary stats
1503
  for model_id in ("t5", "tomorrow", "tplus1"):
 
 
 
 
 
1504
  entries = ledger[model_id]["entries"]
1505
+ total = len(entries)
1506
+ correct = sum(1 for e in entries if e.get("correct"))
1507
+ ledger[model_id]["total"] = total
1508
+ ledger[model_id]["correct_count"] = correct
1509
+ ledger[model_id]["accuracy"] = correct / total if total > 0 else None
 
 
 
 
 
 
 
 
 
 
 
1510
 
1511
  save_live_accuracy(ledger)
1512
  clear_dashboard_payload_cache()
1513
  return ledger
1514
 
1515
 
 
1516
  def refresh_market_close_data(session_date: date | None = None) -> dict[str, Any]:
1517
  now = datetime.now(IST)
1518
  session_date = session_date or now.date()