> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Run it

Every command in the project, in the order you would use them. Run them from the repository's root
folder, inside the project's Python environment (`source .venv/bin/activate`).

Spark 4 needs Java 17 or 21, not a newer default such as 26:

* `run_everything.sh` and `scripts/demo_live.sh` find Java 21 by themselves;
* before any other command that starts Spark (`job.py`, `train.py`), set it in that terminal:

```bash theme={null}
export JAVA_HOME=/opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home
```

## 1. Set up a computer (once)

| Step | Command | Why |
| - | - | - |
| Get the code | `git clone git@github.sfu.ca:aba395/Calendar-Effects.git` | SSH to github.sfu.ca works on campus or on the SFU VPN |
| Python | `python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt -r requirements-dev.txt` | the pinned packages, and the project itself (`-e .`) |
| Java | `brew install openjdk@21` (macOS) | Spark 4 runs on Java 17 or 21 only |
| Check | `python3 -m pytest -q` | every test should pass |
| Git hooks | `scripts/install_git_hooks.sh`, then `gh auth login` | after each commit on `main`, they push the docs site, the hosted dashboard and the cloud streamer's copy of the project (decisions 0011, 0013, 0015). Signing in needs write access to the `Calendar-Effects` GitHub organisation |
| Hugging Face | `hf auth login` (a *Write* token) | only to publish the dataset and model, or a live run from this computer |

## 2. Reproduce the results

```bash theme={null}
./run_everything.sh --skip-etl
```

About 20 minutes for all 38 retailers, on the clean data committed in `clean/`. In order, it:

1. rebuilds the master table;
2. replays it through the streaming job;
3. checks the stream against a direct computation;
4. runs the batch event study (the answer key) and compares the two;
5. trains the big-move model.

It stops with an error if any check fails.

| Option | What it does |
| - | - |
| (none) | also downloads and cleans the raw data again (Yahoo Finance, Wikimedia): slow, and can be rate-limited |
| `--skip-ml` | skips training the model |
| `--late-test` | sends about 2% of rows 3 trading days late, to test the job's watermark |
| `--kafka` | streams through Kafka instead of local files (the course cluster; see section 5) |
| `--bootstrap HOSTS`, `--seconds-per-day S` | Kafka servers and producer speed for `--kafka` |

**Model scores in the stream.** The model is trained last, so this run's stream has no model scores. The dashboard's recording needs them. Either:

* download the trained model before `run_everything.sh`:
  ```bash theme={null}
  hf download calendar-effects/big-move-model --local-dir data/models
  ```
* or stream once more after it:
  ```bash theme={null}
  python3 src/producers/replay_to_files.py && python3 src/streaming/job.py --source files --once --fresh
  ```

**Careful:** `job.py --fresh` with its default folders deletes `data/stream/`, the full run that the results and the recording come from. For anything else, use other folders (`--out`, `--checkpoint`), or `scripts/demo_live.sh`, which uses `data/demo/`.

The steps one at a time (local files):

```bash theme={null}
python3 etl/join_master.py
python3 src/producers/replay_to_files.py
python3 src/streaming/job.py --source files --once --fresh
python3 scripts/check_streaming.py
python3 src/eventstudy/car.py && python3 src/eventstudy/stats.py && python3 src/eventstudy/charts.py
python3 scripts/compare_stream_batch.py
python3 src/ml/train.py
```

To download and clean the raw data by hand, in order: `etl/prices_download.py`, `etl/prices_clean.py`, `etl/pageviews_download.py`, `etl/pageviews_clean.py`, `etl/retail_clean.py`, then `etl/join_master.py`. Retail sales need the FRED files in `raw/census/` (see `docs/etl-guide.md`).

## 3. Refresh what is published (after a full run)

In this order:

1. **The dashboard's recording:**
   ```bash theme={null}
   python3 src/hosting/snapshot.py
   ```
   It writes `hosting/snapshot/`, the Replay and Event history views, from `data/stream/`, the batch results and the model.
2. **The results pack:**
   ```bash theme={null}
   python3 scripts/export_results.py
   ```
   It writes `docs/results/`, the tables, charts and checks behind the report. Add `--cluster-stream DIR` to include a cluster run copied back.
3. **Commit, then `git push`.** The pre-commit hook rebuilds `docs-site/`. The post-commit hook pushes:
   * the docs site, which Mintlify redeploys;
   * the hosted dashboard, which Streamlit Community Cloud redeploys;
   * the cloud streamer's copy.
4. **Hugging Face:**
   ```bash theme={null}
   python3 src/hosting/publish_hf.py --upload
   ```
   Without `--upload` it only builds the folders in `data/publish/` for you to check. Add `--only dataset` (or `model`, `space`) to upload just one.

## 4. Watch and demo

**The dashboard:** [calendar-effects.streamlit.app](https://calendar-effects.streamlit.app), always on (if nobody has opened it for 12 hours, one click wakes it). The same app on your computer:

```bash theme={null}
streamlit run src/dashboard/app.py
```

Its views:

* **Replay:** plays back a recording of a full run, made earlier: pick the years, pause, jump, change the speed. Nothing else needs to run.
* **Live stream:** a streaming job running right now, each micro-batch about 15 seconds after it is processed. Start one of these two ways.
* **Event history & forecast** and **About.**

**A live run on GitHub's servers** (the cloud streamer, no computer needed): on github.com, `Calendar-Effects/live-stream` > **Actions** > **Live stream** > **Run workflow**. The same from a terminal:

```bash theme={null}
gh workflow run live-stream.yml --repo Calendar-Effects/live-stream -f mode=start -f start=2023-06-01
```

| To | Do |
| - | - |
| Stop | **Cancel run** on the run's page, or `gh run cancel <run id> --repo Calendar-Effects/live-stream`. The run is saved. |
| Resume | `-f mode=resume` instead of `mode=start` |
| Restart | start again: a run that is streaming is stopped first |

Every run plays on to the end of the data and then stops by itself: about 15 minutes from 2023-06-01, about an hour from 2016-01-04. It needs the repository secret `HF_TOKEN` (`hosting/live-stream/README.md`).

**A live run from your computer:**

```bash theme={null}
scripts/demo_live.sh
```

| Option | What it does |
| - | - |
| a date, e.g. `2016-01-04` | where to start; every run plays on to the end of the data (default 2023-06-01: each stock's 120-day baseline is built by January 2024, so the model's test years, 2024–2025, are fully scored) |
| `--resume` | carry on the last run from where it stopped |
| `--every=10` | seconds between micro-batches (default 5) |
| `--private` | do not publish; watch it on this computer's dashboard (`streamlit run src/dashboard/app.py`), *Live stream*, which picks *A job on this computer* by itself while the job runs |

Ctrl-C stops it cleanly. It needs `hf auth login` to publish.

Only one run shows on the hosted dashboard at a time: whichever started first. Another run keeps uploading and appears once the first ends.

"Live" means the processing is live; the data is a replay of 2016–2025, not today's market.

A run's last day is 17 December 2025, not the data's last day, 31 December: the job releases a day only once its 7-day watermark has passed it, and at the end of a replay nothing newer arrives to move the watermark. The batch study covers those days.

## 5. On the SFU cluster

On the SFU VPN: `ssh cluster.cs.sfu.ca`, and run long jobs inside `tmux`. The cluster's `python3` has no pyspark, so the job goes through `spark-submit` with the Kafka connector:

```bash theme={null}
cd Calendar-Effects && export PYTHONPATH=src
python3 scripts/create_topics.py --topic <topic>
python3 src/producers/replay_master.py --topic <topic> --seconds-per-day 0
spark-submit --packages org.apache.spark:spark-sql-kafka-0-10_2.13:3.5.8 src/streaming/job.py \
    --source kafka --topic <topic> --max-per-trigger 390 --once \
    --out calendar-effects/stream --checkpoint calendar-effects/checkpoint
hdfs dfs -get calendar-effects/stream ~/stream-out
```

* **The two paths:** `--out` and `--checkpoint` are HDFS paths. Clear them with `hdfs dfs -rm -r` before a rerun.
* **Topics:** the team topic is `calendar-effects-master`; use your own topic for experiments.
* **Late data:** add `--late-fraction 0.02` to the producer to send some rows late.
* **Checking a topic:** `python3 scripts/peek_topic.py --topic <topic>` prints its first messages.
* **Reports:** to put a cluster run in the reports, copy its output folder back to your computer, then run `scripts/export_results.py --cluster-stream DIR`.

## 6. Upkeep

| When | Command |
| - | - |
| Rebuild the docs site (the pre-commit hook does it) | `python3 scripts/build_docs.py` |
| Preview the docs site (Node 20.17+) | `cd docs-site && npx mint dev` |
| After editing the cloud streamer's workflow or page | `scripts/install_workflows.sh` |
| Push one of the three copies by hand | `scripts/sync_docs_site.sh`, `scripts/sync_dashboard_app.sh`, `scripts/sync_live_stream.sh` |
| Pull the team's work and see what changed | `git pull`, or `/sync` in Claude Code |

## 7. Submit

When the report and the video are done, tag the commit they describe:

```bash theme={null}
git tag final && git push origin final
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.