Spaces:
Running
Running
Sync from GitHub (tests passed)
Browse files
deep_learning/data/feature_store.py
CHANGED
|
@@ -213,7 +213,7 @@ def _build_daily_embedding_features(
|
|
| 213 |
def build_tft_dataframe(
|
| 214 |
session,
|
| 215 |
cfg: Optional[TFTASROConfig] = None,
|
| 216 |
-
) -> tuple[pd.DataFrame, list[str], list[str], list[str]]:
|
| 217 |
"""
|
| 218 |
Build the master DataFrame for TFT training / inference.
|
| 219 |
|
|
@@ -362,4 +362,7 @@ def build_tft_dataframe(
|
|
| 362 |
len([c for c in master.columns if c.startswith("emb_pca_")]),
|
| 363 |
)
|
| 364 |
|
| 365 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
def build_tft_dataframe(
|
| 214 |
session,
|
| 215 |
cfg: Optional[TFTASROConfig] = None,
|
| 216 |
+
) -> tuple[pd.DataFrame, list[str], list[str], list[str], float]:
|
| 217 |
"""
|
| 218 |
Build the master DataFrame for TFT training / inference.
|
| 219 |
|
|
|
|
| 362 |
len([c for c in master.columns if c.startswith("emb_pca_")]),
|
| 363 |
)
|
| 364 |
|
| 365 |
+
# Also return the last close price for baseline_price calculation
|
| 366 |
+
last_close = float(close.iloc[-1]) if len(close) > 0 else float('nan')
|
| 367 |
+
|
| 368 |
+
return master, time_varying_unknown, time_varying_known, target_cols, last_close
|
deep_learning/inference/predictor.py
CHANGED
|
@@ -117,9 +117,8 @@ class TFTPredictor:
|
|
| 117 |
from deep_learning.models.tft_copper import format_prediction
|
| 118 |
from pytorch_forecasting import TimeSeriesDataSet
|
| 119 |
|
| 120 |
-
master_df, tv_unknown, tv_known, target_cols = build_tft_dataframe(session, self.cfg)
|
| 121 |
|
| 122 |
-
last_known_price = self._get_last_price(session, symbol)
|
| 123 |
logger.info("TFT predict: baseline_price=%.4f for %s", last_known_price, symbol)
|
| 124 |
|
| 125 |
encoder_length = self.cfg.model.max_encoder_length
|
|
@@ -201,31 +200,6 @@ class TFTPredictor:
|
|
| 201 |
|
| 202 |
return result
|
| 203 |
|
| 204 |
-
def _get_last_price(self, session, symbol: str) -> float:
|
| 205 |
-
"""Fetch the latest close price from the database."""
|
| 206 |
-
import math
|
| 207 |
-
from app.models import PriceBar
|
| 208 |
-
|
| 209 |
-
row = (
|
| 210 |
-
session.query(PriceBar.close)
|
| 211 |
-
.filter(PriceBar.symbol == symbol)
|
| 212 |
-
.order_by(PriceBar.date.desc())
|
| 213 |
-
.first()
|
| 214 |
-
)
|
| 215 |
-
if row is None:
|
| 216 |
-
logger.warning("No PriceBar found for %s — prices will be null", symbol)
|
| 217 |
-
return float('nan')
|
| 218 |
-
|
| 219 |
-
price = float(row.close)
|
| 220 |
-
if math.isnan(price) or math.isinf(price) or price <= 0:
|
| 221 |
-
logger.warning(
|
| 222 |
-
"Invalid close price for %s: %s — prices will be null",
|
| 223 |
-
symbol, price,
|
| 224 |
-
)
|
| 225 |
-
return float('nan')
|
| 226 |
-
|
| 227 |
-
return price
|
| 228 |
-
|
| 229 |
def get_model_metadata(self, session) -> Optional[Dict]:
|
| 230 |
"""Load persisted TFT model metadata from DB."""
|
| 231 |
from app.models import TFTModelMetadata
|
|
|
|
| 117 |
from deep_learning.models.tft_copper import format_prediction
|
| 118 |
from pytorch_forecasting import TimeSeriesDataSet
|
| 119 |
|
| 120 |
+
master_df, tv_unknown, tv_known, target_cols, last_known_price = build_tft_dataframe(session, self.cfg)
|
| 121 |
|
|
|
|
| 122 |
logger.info("TFT predict: baseline_price=%.4f for %s", last_known_price, symbol)
|
| 123 |
|
| 124 |
encoder_length = self.cfg.model.max_encoder_length
|
|
|
|
| 200 |
|
| 201 |
return result
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
def get_model_metadata(self, session) -> Optional[Dict]:
|
| 204 |
"""Load persisted TFT model metadata from DB."""
|
| 205 |
from app.models import TFTModelMetadata
|