Skip to main content
src/hosting/snapshot.py · chapter The dashboard and publishing, file 5 · 353 lines · open in the repository

What it does

Build the recorded stream and event history the dashboard shows (decisions 0010 and 0013). The dashboard’s Replay and Event history views (src/dashboard/app.py) read these files, on a laptop and hosted, so the hosted app works without Kafka or Spark. They are committed (derived statistics only, about 1 MB), so a fresh clone can show them too. This script writes three files into hosting/snapshot/: replay.json the streaming job’s output for every year (rows in replay_rows.parquet), which the Replay view plays day by day: each stock-day’s abnormal return, z-scores, Wikipedia views and alerts, the big-move model’s prediction, and each event window’s CAR on the day it closes. It plays the model’s test years by default, the only years the model never saw in training. events.json for each event type: every year’s average cumulative abnormal return over the window (days -5..+5), the average across the years, a straight-line trend through the years extended one year ahead with a prediction interval, and what the big-move model expected on each day of the window in the test years against what happened. meta.json when and from which commit it was built, and the model’s test scores. Nothing here is a price, a volume or a raw return: Yahoo’s terms do not allow republishing those. Abnormal returns, z-scores and CARs are our own statistics, and Wikipedia pageviews are public (CC0). Run after run_everything.sh (it needs the batch study, the model and a streaming run):
Then look at it: streamlit run src/dashboard/app.py (views Replay and Event history)

What is inside

  • TEST_PREDICTIONS · line 49
  • DAYS · line 50
  • FORECAST_YEAR · line 51
  • clean(value, digits) · line 54: A JSON-safe rounded number: NaN and None become null.
  • column(values, digits) · line 61
  • whole(values) · line 65: Counts and 0/1 flags as JSON integers (null when missing).
  • read_stream(dataset) · line 70: Every micro-batch of one streaming output that Spark finished writing.
  • trend_forecast(years, values, at, confidence) · line 85: Straight line through (year, value) by ordinary least squares, extended to year at.
  • yearly_paths(paths, tickers) · line 110: Average cumulative abnormal return on each window day, one row per event occurrence (columns: event_type, day0_date, year, n_tickers, then one per day). paths comes from eventstudy.charts.window_paths; tickers limits it to a group.
  • distinct_event_types(cars) · line 125: Event types in config order, merging types that share every window.
  • model_by_day(cars, test, trading_days) · line 147: What the big-move model expected on each window day, against what happened.
  • group_block(event_type, rows, summary, group, risk, test) · line 172: Everything the event view draws for one event type and one retailer group.
  • build_events(cars, paths, summary, test, trading_days, alert_cutoff) · line 211
  • check_against_stats(events, trend_table) · line 238: The forecast’s last day is the CAR, so its slope must equal stats.py’s trend.
  • build_replay(signals, predictions, windows) · line 250: The whole recorded stream, every year, for the Replay view: an index (days, tickers, event windows, markers; JSON) and one row per stock-day (Parquet, about 1.5 MB). start is the first day of the model’s test years, which the view plays by default.
  • write_meta(folder) · line 311: meta.json: provenance and the model’s test scores.
  • write(name, payload) · line 322
  • main() · line 329

Connections

Uses: src/common/contracts.py, src/common/settings.py, src/common/study.py, src/eventstudy/car.py, src/eventstudy/charts.py, src/eventstudy/stats.py Used by: scripts/export_results.py, src/hosting/publish_hf.py, tests/test_hosting.py

Source

src/hosting/snapshot.py