Spaces:
Running
Running
feat(autotune): apply_tuning helper for Forecast tab pre-fill
Browse files- autotune.py +10 -1
- tests/test_autotune.py +29 -0
autotune.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import json
|
| 2 |
import sqlite3
|
| 3 |
from datetime import datetime, timezone
|
| 4 |
-
from typing import Optional
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
|
@@ -240,3 +240,12 @@ def run_autotune(*, predict_fn, fetch_fn, db_path: str,
|
|
| 240 |
})
|
| 241 |
|
| 242 |
return fig, summary, grid_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import sqlite3
|
| 3 |
from datetime import datetime, timezone
|
| 4 |
+
from typing import Optional, Tuple
|
| 5 |
|
| 6 |
import numpy as np
|
| 7 |
import pandas as pd
|
|
|
|
| 240 |
})
|
| 241 |
|
| 242 |
return fig, summary, grid_df
|
| 243 |
+
|
| 244 |
+
def apply_tuning(db_path: str, symbol: str, interval: str,
|
| 245 |
+
use_tuned: bool) -> Tuple[Optional[float], Optional[float]]:
|
| 246 |
+
if not use_tuned:
|
| 247 |
+
return (None, None)
|
| 248 |
+
row = load_tuning(db_path, symbol, interval)
|
| 249 |
+
if row is None:
|
| 250 |
+
return (None, None)
|
| 251 |
+
return (float(row["best_T"]), float(row["best_top_p"]))
|
tests/test_autotune.py
CHANGED
|
@@ -216,3 +216,32 @@ def test_run_autotune_raises_when_all_cells_fail(tmp_db, monkeypatch):
|
|
| 216 |
sample_count=1, max_anchors=5,
|
| 217 |
)
|
| 218 |
assert load_tuning(tmp_db, "X", "5min") is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
sample_count=1, max_anchors=5,
|
| 217 |
)
|
| 218 |
assert load_tuning(tmp_db, "X", "5min") is None
|
| 219 |
+
|
| 220 |
+
def test_apply_tuning_returns_values_when_cached_and_enabled(tmp_db):
|
| 221 |
+
from autotune import apply_tuning
|
| 222 |
+
init_tuning_table(tmp_db)
|
| 223 |
+
save_tuning(tmp_db, "SPY", "5min",
|
| 224 |
+
{"T": 1.5, "top_p": 0.7, "total_return_pct": 7.0,
|
| 225 |
+
"hit_rate": 0.6, "mean_rmse": 0.9},
|
| 226 |
+
[], {"lookback": 200, "pred_len": 30, "stride": 30,
|
| 227 |
+
"sample_count": 1, "max_anchors": 20,
|
| 228 |
+
"start_date": "", "end_date": ""})
|
| 229 |
+
t_val, top_p_val = apply_tuning(tmp_db, "SPY", "5min", True)
|
| 230 |
+
assert t_val == 1.5
|
| 231 |
+
assert top_p_val == 0.7
|
| 232 |
+
|
| 233 |
+
def test_apply_tuning_returns_none_when_disabled(tmp_db):
|
| 234 |
+
from autotune import apply_tuning
|
| 235 |
+
init_tuning_table(tmp_db)
|
| 236 |
+
save_tuning(tmp_db, "SPY", "5min",
|
| 237 |
+
{"T": 1.5, "top_p": 0.7, "total_return_pct": 7.0,
|
| 238 |
+
"hit_rate": 0.6, "mean_rmse": 0.9},
|
| 239 |
+
[], {"lookback": 200, "pred_len": 30, "stride": 30,
|
| 240 |
+
"sample_count": 1, "max_anchors": 20,
|
| 241 |
+
"start_date": "", "end_date": ""})
|
| 242 |
+
assert apply_tuning(tmp_db, "SPY", "5min", False) == (None, None)
|
| 243 |
+
|
| 244 |
+
def test_apply_tuning_returns_none_when_no_cache(tmp_db):
|
| 245 |
+
from autotune import apply_tuning
|
| 246 |
+
init_tuning_table(tmp_db)
|
| 247 |
+
assert apply_tuning(tmp_db, "ZZZ", "1min", True) == (None, None)
|