Running a Simple Access Study from Python
This guide shows how to build the default walk / bike / drive road network and run the same access study from Python.
Note for Python Users: At present, all data import steps and model configuration updates (covered in the subsequent chapters of this book) are performed via the command-line tools or by editing the
bambam.tomlconfiguration file directly, after whichBambamRunner.from_config_file(...)is used to load and run the configured model in Python. In a future release, we plan to roll these import and configuration operations into a unified Python builder API.
Prerequisites
Compile the Rust binaries, install the Python package, and set your U.S. Census API key:
cargo build --release --manifest-path rust/Cargo.toml
uv sync
uv pip install -e ".[all]"
export CENSUS_API_TOKEN="<your-census-api-key>"
1. Build the Network
Run script/bambam_build_open_data.sh with --to-stage network to download the road network for query/denver_470_buffer.txt into denver_buf/:
./script/bambam_build_open_data.sh \
--extent-file query/denver_470_buffer.txt \
--output-dir denver_buf \
--to-stage network
This creates the walk/, bike/, and drive/ network files and generates denver_buf/bambam.toml.
2. Run BAMBAM in Python
Load denver_buf/bambam.toml with BambamRunner and pass the study grid extent from query/denver_extent.json:
import json
from nlr.bambam import BambamRunner
# Load the generated configuration
app = BambamRunner.from_config_file("denver_buf/bambam.toml")
# Load the grid extent query
with open("query/denver_extent.json") as f:
query = json.load(f)
# Run the access study
app.run(query)
When app.run(query) completes, the results are written to result.csv and result.json with 10-, 20-, 30-, and 40-minute isochrones and opportunity counts for each grid cell and travel mode:
import pandas as pd
df = pd.read_csv("result.csv")
print(df[["grid_id", "mode", "opps_jobs_10", "opps_jobs_20", "opps_jobs_30", "opps_jobs_40"]].head())