Skip to content

Commit 4a61fea

Browse files
committed
Run data prep with new nationwide history file and visualize results.
1 parent 5011b9d commit 4a61fea

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

scripts/osm_data/data_viz.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,13 @@ def get_preds_df(model_stub: str, subset: str | None = None) -> Path:
185185
)
186186
fig_save(fig, stub = f"osm_changes_{TAG_KEY}_all_preds")
187187

188-
# Create multi-panel plots for the top tags in each OSM category
188+
# Create multi-panel plots for the top tags in each OSM category. Only
189+
# iterate keys that are actually columns in the observations CSV — the
190+
# snapshot pipeline's filter_keys list can expand without the observations
191+
# CSV being regenerated, so we defend against the mismatch here.
189192
TOP_N_TYPES = config.get("osm_data", "top_n_types")
190-
for subtype in OSM_KEYS:
193+
present_keys = [k for k in OSM_KEYS if k in to_plot_df.columns]
194+
for subtype in present_keys:
191195
fig = change_multiplot_create(
192196
observations = to_plot_df,
193197
col = subtype,

scripts/osm_data/format_tabular.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
value, the timestamps of the previous tag assignment and the current
99
observation, and a flag for whether the tag changed.
1010
11+
At US scale the versions + changes Parquets together exceed typical RAM, so
12+
this script uses ``format_observations_duckdb`` to pivot changes wide, LEFT
13+
JOIN them against versions, and ORDER BY (type, id, version) inside DuckDB,
14+
spilling the sort to disk as needed. A Python scan then runs the per-POI
15+
state machine over the sorted row stream and writes observations directly to
16+
the output CSV — peak RSS stays bounded to roughly ``DUCKDB_MEMORY_LIMIT``.
17+
1118
Config keys used (config.yaml):
1219
directories.osm_data — directory containing input and output files
1320
download.osm.filter_keys — all tag keys collected (passed as keep_keys)
@@ -24,10 +31,9 @@
2431
plus all keep_keys columns for grouping
2532
"""
2633

27-
import pandas as pd
2834
from config_versioned import Config
2935

30-
from openpois.osm.format_observations import format_observations
36+
from openpois.osm.format_observations import format_observations_duckdb
3137

3238

3339
# ----------------------------------------------------------------------------------------
@@ -40,25 +46,29 @@
4046
OSM_KEYS = config.get("download", "osm", "filter_keys")
4147
TAG_KEY = config.get("osm_data", "tag_key")
4248

49+
CHANGES_PATH = config.get_file_path("osm_data", "osm_changes")
50+
VERSIONS_PATH = config.get_file_path("osm_data", "osm_versions")
51+
OUT_PATH = SAVE_DIR / f"osm_observations_{TAG_KEY}.csv"
52+
53+
# DuckDB execution limits. The sort operator spills past memory_limit so this
54+
# caps peak RAM independent of input size. Threads default to os.cpu_count()
55+
# when left as None.
56+
DUCKDB_MEMORY_LIMIT = "4GB"
57+
DUCKDB_THREADS = None
58+
4359

4460
# ----------------------------------------------------------------------------------------
4561
# Main workflow
4662
# ----------------------------------------------------------------------------------------
4763

4864
if __name__ == "__main__":
49-
# Read files
50-
changes_df = pd.read_parquet(config.get_file_path("osm_data", "osm_changes"))
51-
versions_df = pd.read_parquet(config.get_file_path("osm_data", "osm_versions"))
52-
53-
# Format changes and versions into observations
54-
observations_df = format_observations(
55-
changes_df = changes_df,
56-
versions_df = versions_df,
65+
n_written = format_observations_duckdb(
66+
changes_path = CHANGES_PATH,
67+
versions_path = VERSIONS_PATH,
68+
output_path = OUT_PATH,
5769
tag_key = TAG_KEY,
5870
keep_keys = OSM_KEYS,
71+
duckdb_memory_limit = DUCKDB_MEMORY_LIMIT,
72+
duckdb_threads = DUCKDB_THREADS,
5973
)
60-
61-
# Save observations
62-
out_path = SAVE_DIR / f"osm_observations_{TAG_KEY}.csv"
63-
observations_df.to_csv(out_path, index = False)
64-
print(f"Saved {len(observations_df)} observations to {out_path}")
74+
print(f"Saved {n_written} observations to {OUT_PATH}")

0 commit comments

Comments
 (0)