Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .github/workflows/ml-picks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ name: ML Daily Picks
# docs/ml-picks-runbook.md.

on:
schedule:
# 05:30 ET-equivalent to the retired EventBridge trigger's cron 30 9.
# DST caveat: GitHub cron is a FIXED UTC clock while the pipeline's
# "today" is America/Toronto - 09:30 UTC is 05:30 EDT but 04:30 EST,
# so the local wall-clock anchor shifts one hour across clock changes
# (same accepted behaviour the template documented for its crons).
- cron: '30 9 * * *'
workflow_dispatch:
inputs:
date:
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ Cloudflare `smartscore-api` worker that fronted it. The other two Supabase table
are downstream reporting, not the training set: `Picks-{ENV}` is the current day's
full ranked roster (wiped and rewritten on every `PublishToDb`), and
`Historic-Picks-{ENV}` is a ~7-day visual of who we've been picking (only 3 picks
per date, via `choose_picks`/`NUM_EXPECTED_PLAYERS`).
per date). The site's own pipeline no longer writes today's historic picks, since
its Rust prediction now lives in `legacy_stat`; the ML pass owns the historic
write and records its top-3 against `stat`.

Table shape, added by
`supabase/migrations/20261002_add_player_snapshots_table.sql`:
Expand Down Expand Up @@ -145,6 +147,8 @@ Other constraints worth knowing:
otshga/hppg/home), so lineup data is recorded but not yet a model input.
`MakePredictions` now runs after `GetLineups` on the `first_run` path, so a future
lineup feature has the merged data available without another re-order.
It writes its weighted-average score to `legacy_stat` only; `stat` is left for the
ML pass to fill, and `choose_picks` skips rows with no `stat`.
- **`lineup_status` distinguishes outcomes.** `PROJECTED` vs `UNKNOWN` lets a training
job separate a real negative (not on a forward line) from a failed/empty fetch.
Filter on `PROJECTED` rather than treating null as "not a top-9 forward".
Expand Down
66 changes: 66 additions & 0 deletions smartscore/scripts/backtrack/run_daily_picks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,67 @@ def pick_rows_to_picks_db(picks):
return rows


def _historic_pick_columns(env):
table = f"Historic-Picks-{env}"
try:
rows = SUPABASE_ADMIN_CLIENT.table(table).select("*").limit(1).execute().data or []
if rows:
return list(rows[0].keys())
except Exception as exc: # noqa: BLE001
print(f"WARNING: could not discover Historic-Picks columns: {exc}", flush=True)
# Conservative fallback that exists on every Picks/Historic-Picks table.
return ["id", "date", "player_id", "name", "team_name", "tims", "stat"]


def write_historic_picks_if_missing(picks, on_date, env):
"""Idempotently write the ML top-3 picks to Historic-Picks-{env}.

The AWS pipeline no longer writes today's historic picks (it skips them so
the ML pass owns them). This runs on every ML pass but only writes when the
date is absent, so repeat kicks / the nightly cron are no-ops.
"""
table = f"Historic-Picks-{env}"
existing = SUPABASE_ADMIN_CLIENT.table(table).select("date").eq("date", on_date).limit(1).execute().data or []
if existing:
print(f"Historic-Picks-{env}: {on_date} already present - skipping write", flush=True)
return

if picks is None or (hasattr(picks, "empty") and picks.empty):
print("Historic-Picks write skipped: no ML picks", flush=True)
return

columns = _historic_pick_columns(env)
records = []
for i, (_, row) in enumerate(picks.iterrows()):
record = {"date": on_date}
pid = row.get("player_id", row.get("id"))
if isinstance(pid, (np.generic,)):
pid = pid.item()
record["player_id"] = int(pid) if pid is not None and not (isinstance(pid, float) and np.isnan(pid)) else None
if "id" in columns:
record["id"] = i + 1
if "Scored" in columns:
record["Scored"] = None
for col in columns:
if col in ("id", "date", "player_id", "Scored"):
continue
if col == "stat":
value = stat_value(row.get("ensemble", np.nan))
elif col in getattr(row, "index", []):
value = row.get(col)
if isinstance(value, (np.generic,)):
value = value.item()
if isinstance(value, float) and np.isnan(value):
value = None
record[col] = value
records.append(record)

if not records:
return
SUPABASE_ADMIN_CLIENT.table(table).insert(records).execute()
print(f"Historic-Picks-{env}: wrote {len(records)} ML picks for {on_date}", flush=True)


# ------------------------------------------------------------- stat update
# The DEFAULT write mode (the owner-corrected contract, 2026-11-03): the
# Picks-{ENV} table is the FULL PLAYER SLATE the site's own pipeline
Expand Down Expand Up @@ -1639,6 +1700,11 @@ def run(event=None, context=None):
picks = model.daily_picks(sim, "ensemble")
validate_picks(picks, on_date)
rows = pick_rows_to_picks_db(picks)
if not dry:
try:
write_historic_picks_if_missing(picks, on_date, ENV)
except Exception as exc: # noqa: BLE001
print(f"WARNING: Historic-Picks write failed: {exc}", flush=True)
mean_max_prob = round(float(pd.to_numeric(picks["ensemble"], errors="coerce").mean()), 4) if len(picks) else None
stat_payload = {}
if stat_result is not None:
Expand Down
20 changes: 12 additions & 8 deletions smartscore/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ def make_predictions_teams(players, teams):
)
rust_probabilities = make_predictions_rust.predict(rust_players, min_max, WEIGHTS)
for i, player in enumerate(players):
player["stat"] = rust_probabilities[i]
# Preserve the site's legacy weighted-average prediction alongside the
# ML-overwritten `stat` (legacy_stat = same value, copied not recomputed).
# Rust's weighted-average prediction is the site's *legacy* score.
# `stat` is intentionally left unset here: it is filled later by the ML
# pass (run_daily_picks) directly, so the two never need copying over.
player["legacy_stat"] = rust_probabilities[i]

return players
Expand Down Expand Up @@ -440,9 +440,12 @@ def choose_picks(players):
tims_picks = {}
for player in players:
tims = int(player["tims"])
stat = player.get("stat")
if stat is None:
continue
if tims not in tims_picks:
tims_picks[tims] = player
elif player["stat"] > tims_picks[tims]["stat"]:
elif stat > tims_picks[tims].get("stat", float("-inf")):
tims_picks[tims] = player
tims_picks.pop(0, None)

Expand All @@ -467,11 +470,12 @@ def write_historic_db(picks):
for entry in old_entries:
table[entry["date"]].append((entry["player_id"], entry["Scored"]))

if today in table.keys():
today_already_present = today in table.keys()
if today_already_present:
logger.info(f"Today already in table: {table[today]}")
# Already processed today (e.g. a retry or a second run). Returning []
# keeps the cumulative metrics idempotent instead of recounting.
return []
# ML pass (or a retry) already recorded today's picks. Do not append
# them again, but still backfill Scored for earlier dates below.
picks = []

if picks:
while len(table) >= DAYS_TO_KEEP_HISTORIC_DATA:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_service_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def test_make_predictions_teams_basic(mock_rust, mock_min_max):
result = make_predictions_teams(players, teams)

assert len(result) == 2
assert result[0]["stat"] == 0.65
assert result[0].get("stat") is None
assert result[0]["legacy_stat"] == 0.65
assert result[1]["stat"] == 0.75
assert result[1].get("stat") is None
assert result[1]["legacy_stat"] == 0.75
mock_rust.predict.assert_called_once()

Expand Down
Loading