run_everything.sh · chapter Orientation, file 2 · 259 lines · open in the repository
What it does
Run the whole Calendar Effects pipeline in one command: ETL -> master table -> streaming job -> check against a direct computation -> batch event study -> stream vs batch comparison -> big-move model. See README “Running it locally”. ./run_everything.sh —skip-etl quick local run on the committed clean/ files ./run_everything.sh re-download and re-clean everything first ./run_everything.sh —skip-etl —kafka stream through the course Kafka cluster Run ./run_everything.sh —help for every option.Source
run_everything.sh
#!/usr/bin/env bash
# Run the whole Calendar Effects pipeline in one command: ETL -> master table ->
# streaming job -> check against a direct computation -> batch event study ->
# stream vs batch comparison -> big-move model. See README "Running it locally".
#
# ./run_everything.sh --skip-etl quick local run on the committed clean/ files
# ./run_everything.sh re-download and re-clean everything first
# ./run_everything.sh --skip-etl --kafka stream through the course Kafka cluster
#
# Run ./run_everything.sh --help for every option.
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./run_everything.sh [options]
Runs the Calendar Effects pipeline end to end, then checks the streaming output.
Steps:
1. Python setup activate .venv (created if missing) and pip install -r requirements.txt
2. Java check Spark 4 needs Java 17 or 21; sets JAVA_HOME if it can find one
3. ETL etl/prices_download.py, prices_clean.py, pageviews_download.py,
pageviews_clean.py, retail_clean.py
(the downloads hit Yahoo Finance and Wikimedia live; skip with --skip-etl)
4. Master table etl/join_master.py rebuilds clean/master.parquet (fast, always run)
5. Streaming default: local files, no Kafka needed
src/producers/replay_to_files.py
src/streaming/job.py --source files --once --fresh
with --kafka: the real cluster
scripts/create_topics.py
src/producers/replay_master.py (sends all 10 years, then exits)
src/streaming/job.py --source kafka --once --fresh
6. Check scripts/check_streaming.py compares the stream's output with a direct
numpy computation and prints OK or FAILED
7. Batch study src/eventstudy/car.py, stats.py, charts.py (answer key, tests, charts)
8. Comparison scripts/compare_stream_batch.py: stream vs answer key, window by window
9. Model src/ml/train.py: the big-move model with Spark MLlib (skip with --skip-ml)
Then, to watch alerts live: streamlit run src/dashboard/app.py
Options:
--skip-etl use the committed clean/*.parquet files instead of re-downloading
and re-cleaning (the normal choice; the ETL takes a long time)
--kafka stream through Kafka instead of local files
--skip-ml do not train the big-move model (step 9)
--bootstrap HOSTS Kafka servers for --kafka (default: node1.local:9092,node2.local:9092)
--seconds-per-day S producer speed for --kafka (default 0.1, about 4 minutes in total)
--late-test send about 2% of messages 3 trading days late, to exercise the
watermark (same option in both modes)
-h, --help show this help and exit
Outputs: data/replay/ (local mode); data/stream/signals/, event_car/ and, with a trained model,
predictions/; data/results/ (batch study); data/models/ (the model). Every command: docs/running.md.
Exit code: 0 only if every step ran and the check printed OK.
EOF
}
SKIP_ETL=0
SKIP_ML=0
KAFKA=0
BOOTSTRAP="node1.local:9092,node2.local:9092"
SECONDS_PER_DAY="0.1"
LATE_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
--skip-etl) SKIP_ETL=1 ;;
--skip-ml) SKIP_ML=1 ;;
--kafka) KAFKA=1 ;;
--bootstrap)
[[ $# -ge 2 ]] || { echo "error: --bootstrap needs a value" >&2; exit 2; }
BOOTSTRAP="$2"; shift ;;
--seconds-per-day)
[[ $# -ge 2 ]] || { echo "error: --seconds-per-day needs a value" >&2; exit 2; }
SECONDS_PER_DAY="$2"; shift ;;
--late-test) LATE_ARGS=(--late-fraction 0.02 --late-days 3) ;;
-h|--help) usage; exit 0 ;;
*) echo "error: unknown option '$1' (see --help)" >&2; exit 2 ;;
esac
shift
done
# Every script expects to be run from the repo root.
cd "$(dirname "$0")"
step() { printf '\n==> %s\n' "$*"; }
die() { printf '\nERROR: %s\n' "$*" >&2; exit 1; }
# Run one pipeline script; stop with its name if it fails.
run() {
echo "+ $*"
"$@" || die "'$*' failed (exit code $?). Fix the error above, then re-run."
}
# Stop if a step did not leave behind the file or folder the next step reads.
require() {
local path="$1" made_by="$2"
if [[ -d "$path" ]]; then
[[ -n "$(ls -A "$path" 2>/dev/null)" ]] || die "$path is empty; it should have been filled by $made_by."
elif [[ ! -s "$path" ]]; then
die "$path is missing or empty; it should have been made by $made_by."
fi
}
# ---------------------------------------------------------------- 1. Python
step "1. Python environment"
if [[ -z "${VIRTUAL_ENV:-}" ]]; then
if [[ ! -f .venv/bin/activate ]]; then
command -v python3 >/dev/null || die "python3 not found; install Python 3.10+ first."
echo "No .venv yet; creating one."
python3 -m venv .venv || die "could not create .venv"
fi
# shellcheck disable=SC1091
source .venv/bin/activate
fi
echo "Using $(command -v python3) ($(python3 --version 2>&1))"
run python3 -m pip install --quiet --disable-pip-version-check -r requirements.txt
# ---------------------------------------------------------------- 2. Java
# Spark 4 runs only on Java 17 or 21. A newer default Java (e.g. 26) makes Spark
# fail with a confusing error deep in its startup, so check before starting it.
step "2. Java (Spark 4 needs Java 17 or 21)"
java_major() { # prints the major version of $1/bin/java, or nothing
[[ -x "$1/bin/java" ]] || return 0
"$1/bin/java" -version 2>&1 | awk -F'"' '/version/ {split($2, v, "."); print v[1]; exit}'
}
java_ok() { local v; v="$(java_major "$1")"; [[ "$v" == 17 || "$v" == 21 ]]; }
if [[ -n "${JAVA_HOME:-}" ]] && java_ok "$JAVA_HOME"; then
echo "JAVA_HOME is already Java $(java_major "$JAVA_HOME"): $JAVA_HOME"
else
[[ -n "${JAVA_HOME:-}" ]] && echo "JAVA_HOME=$JAVA_HOME is not Java 17 or 21 (found: '$(java_major "$JAVA_HOME")'); looking for one."
candidates=(/opt/homebrew/opt/openjdk@21 /opt/homebrew/opt/openjdk@17
/usr/local/opt/openjdk@21 /usr/local/opt/openjdk@17)
if [[ -x /usr/libexec/java_home ]]; then # macOS registry of installed JDKs
for v in 21 17; do
p="$(/usr/libexec/java_home -v "$v" 2>/dev/null || true)"
[[ -n "$p" ]] && candidates+=("$p")
done
fi
for p in /usr/lib/jvm/*21* /usr/lib/jvm/*17*; do [[ -d "$p" ]] && candidates+=("$p"); done
found=""
for p in "${candidates[@]}"; do
if java_ok "$p"; then found="$p"; break; fi
done
if [[ -n "$found" ]]; then
export JAVA_HOME="$found"
echo "Set JAVA_HOME=$JAVA_HOME (Java $(java_major "$JAVA_HOME"))"
else
die "no Java 17 or 21 found, and Spark 4 will not start without one.
macOS: brew install openjdk@21 then export JAVA_HOME=/opt/homebrew/opt/openjdk@21
Or set JAVA_HOME yourself to a Java 17 or 21 install and re-run."
fi
fi
export PATH="$JAVA_HOME/bin:$PATH"
# ---------------------------------------------------------------- 3. ETL
if [[ "$SKIP_ETL" == 1 ]]; then
step "3. ETL skipped (--skip-etl): using the committed clean/*.parquet files"
for f in clean/prices.parquet clean/pageviews.parquet; do
require "$f" "the ETL (it is committed to the repo; try 'git checkout -- $f')"
done
else
step "3. ETL (live downloads; can be slow or rate-limited)"
run python3 etl/prices_download.py
require raw/prices "etl/prices_download.py"
run python3 etl/prices_clean.py
require clean/prices.parquet "etl/prices_clean.py"
run python3 etl/pageviews_download.py
require raw/wiki "etl/pageviews_download.py"
run python3 etl/pageviews_clean.py
require clean/pageviews.parquet "etl/pageviews_clean.py"
# Retail sales are not part of the master table. Their FRED CSVs are downloaded by
# hand (there is no download script), so only clean them if they are there.
if [[ -d raw/census ]]; then
run python3 etl/retail_clean.py
require clean/retail_sales.parquet "etl/retail_clean.py"
else
echo "WARNING: raw/census/ not found; skipping etl/retail_clean.py (download the FRED"
echo " CSVs by hand to rebuild it; the committed clean/retail_sales.parquet is kept)."
fi
fi
# ---------------------------------------------------------------- 4. Master
step "4. Master table"
run python3 etl/join_master.py
require clean/master.parquet "etl/join_master.py"
# A different pyarrow version writes different bytes for the same table, which would
# show clean/master.parquet as changed in git. If the data are identical to the
# committed file, put the committed file back so git stays clean.
if git ls-files --error-unmatch clean/master.parquet >/dev/null 2>&1 \
&& ! git diff --quiet -- clean/master.parquet 2>/dev/null; then
if git show HEAD:clean/master.parquet 2>/dev/null | python3 -c '
import io, sys, pandas as pd
old = pd.read_parquet(io.BytesIO(sys.stdin.buffer.read()))
new = pd.read_parquet("clean/master.parquet")
sys.exit(0 if old.equals(new) and old.dtypes.equals(new.dtypes) else 1)'; then
git checkout -- clean/master.parquet
echo "clean/master.parquet: same data as the committed file (kept the committed copy)"
else
echo "NOTE: clean/master.parquet now differs from the committed file; review before committing."
fi
fi
# ---------------------------------------------------------------- 5. Streaming
if [[ "$KAFKA" == 1 ]]; then
step "5. Streaming through Kafka ($BOOTSTRAP)"
run python3 scripts/create_topics.py --bootstrap "$BOOTSTRAP"
# Send the whole replay first, then let the job read the topic from the start and
# stop when it has caught up (--once), so the run ends on its own.
run python3 src/producers/replay_master.py --bootstrap "$BOOTSTRAP" \
--seconds-per-day "$SECONDS_PER_DAY" ${LATE_ARGS[@]+"${LATE_ARGS[@]}"}
run python3 src/streaming/job.py --source kafka --bootstrap "$BOOTSTRAP" \
--max-per-trigger 390 --once --fresh
else
step "5. Streaming from local files (no Kafka)"
run python3 src/producers/replay_to_files.py ${LATE_ARGS[@]+"${LATE_ARGS[@]}"}
require data/replay "src/producers/replay_to_files.py"
run python3 src/streaming/job.py --source files --once --fresh
fi
require data/stream/signals "src/streaming/job.py"
require data/stream/event_car "src/streaming/job.py"
# ---------------------------------------------------------------- 6. Check
step "6. Check the stream against a direct computation"
echo "+ python3 scripts/check_streaming.py"
if python3 scripts/check_streaming.py; then
printf '\n==> PASSED: the streaming output matches the direct computation.\n'
else
printf '\n==> FAILED: scripts/check_streaming.py found differences (see above).\n' >&2
exit 1
fi
# ---------------------------------------------------------------- 7. Batch event study
step "7. Batch event study (answer key, significance tests, report charts)"
run python3 src/eventstudy/car.py
run python3 src/eventstudy/stats.py
run python3 src/eventstudy/charts.py
# ---------------------------------------------------------------- 8. Stream vs batch
step "8. Compare the stream with the batch answer key"
echo "+ python3 scripts/compare_stream_batch.py"
if ! python3 scripts/compare_stream_batch.py; then
printf '\n==> FAILED: the stream and the batch answer key disagree (see above).\n' >&2
exit 1
fi
# ---------------------------------------------------------------- 9. Big-move model
if [[ "$SKIP_ML" == 1 ]]; then
step "9. Big-move model skipped (--skip-ml)"
else
step "9. Big-move model (Spark MLlib, decision 0007)"
run python3 src/ml/train.py
fi
printf '\n==> ALL DONE. Results in data/results/. To watch alerts live:\n'
printf ' streamlit run src/dashboard/app.py (in a second terminal)\n'
printf ' python3 src/streaming/job.py --source files --fresh (re-runs the replay, one file every 5 s)\n'