Skip to content

Commit 0bf4f13

Browse files
committed
Move globals from exploratory scripts to config.yaml.
1 parent 80e0c47 commit 0bf4f13

5 files changed

Lines changed: 109 additions & 50 deletions

File tree

config.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Shared settings
2+
osm_keys:
3+
- amenity
4+
- shop
5+
- healthcare
6+
- leisure
7+
end_date: 2025-12-31
8+
9+
# Settings for exploratory/osm_data/download.py
10+
download:
11+
timeout: 1000
12+
bbox:
13+
ymin: 47.41
14+
xmin: -122.48
15+
ymax: 47.79
16+
xmax: -122.16
17+
start_date: 2016-01-01
18+
date_interval_days: 7
19+
20+
# Settings for exploratory/osm_data/format_tabular.py
21+
format_tabular:
22+
tag_key: shop
23+
24+
# Settings for exploratory/osm_data/data_viz.py
25+
data_viz:
26+
tag_key: name
27+
top_n_types: 10
28+
timestamp_cols:
29+
- obs_timestamp
30+
- last_obs_timestamp
31+
- last_tag_timestamp
32+
33+
# Settings for exploratory/models/pytorch_simple.py
34+
pytorch_simple:
35+
tag_key: name
36+
group_key: null
37+
group_values:
38+
- park
39+
n_draws: 250
40+
save_full_model: false
41+
42+
# Directory definitions (used with cfg.get_dir_path())
43+
directories:
44+
osm_download:
45+
versioned: false
46+
path: ~/data/osm_example_data
47+
files:
48+
osm_elements: osm_elements.csv
49+
osm_versions: osm_versions.csv
50+
osm_changes: osm_changes.csv
51+
osm_failed_elements: osm_failed_elements.csv
52+
osm_data:
53+
versioned: true
54+
path: ~/data/openpois
55+
files:
56+
osm_changes: osm_changes.csv
57+
osm_versions: osm_versions.csv
58+
model_output:
59+
versioned: true
60+
path: ~/data/openpois
61+
files: {}
62+
63+
versions:
64+
osm_data: "20260129"
65+
model_output: "20260212"

exploratory/models/pytorch_simple.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
import pandas as pd
1212
import torch
1313
from pathlib import Path
14+
from config_versioned import Config
1415

1516
from openpois.models.model_fitter import ModelFitter
1617
from openpois.models.setup import pytorch_setup, prepare_data_for_model
1718

1819
# Globals
19-
DATA_VERSION = "20260129"
20-
MODEL_VERSION = "20260212"
21-
DATA_DIR = Path("~/data/openpois").expanduser() / DATA_VERSION
22-
MODEL_DIR = Path("~/data/openpois").expanduser() / MODEL_VERSION
23-
TAG_KEY = "name"
24-
GROUP_KEY = None
25-
GROUP_VALUES = ["park"]
26-
N_DRAWS = 250
27-
SAVE_FULL_MODEL = False
20+
_cfg = Config("~/repos/openpois/config.yaml")
21+
22+
DATA_DIR = _cfg.get_dir_path("osm_data")
23+
MODEL_DIR = _cfg.get_dir_path("model_output")
24+
TAG_KEY = _cfg.get("pytorch_simple", "tag_key")
25+
GROUP_KEY = _cfg.get("pytorch_simple", "group_key")
26+
GROUP_VALUES = _cfg.get("pytorch_simple", "group_values")
27+
N_DRAWS = _cfg.get("pytorch_simple", "n_draws")
28+
SAVE_FULL_MODEL = _cfg.get("pytorch_simple", "save_full_model")
2829

2930

3031
if __name__ == "__main__":

exploratory/osm_data/data_viz.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
import pandas as pd
1111
from pathlib import Path
12+
from config_versioned import Config
1213

1314
import matplotlib
1415
matplotlib.use("Agg")
@@ -20,12 +21,13 @@
2021
# Configuration constants
2122
# ----------------------------------------------------------------------------------------
2223

23-
DATA_VERSION = "20260129"
24-
SAVE_DIR = Path("~/data/openpois").expanduser() / DATA_VERSION
24+
_cfg = Config("~/repos/openpois/config.yaml")
25+
26+
SAVE_DIR = _cfg.get_dir_path("osm_data")
2527
VIZ_DIR = SAVE_DIR / "viz"
26-
OSM_KEYS = ["amenity", "shop", "healthcare", "leisure"]
27-
TAG_KEY = "name"
28-
END_DATE = pd.Timestamp('2025-12-31', tz = 'UTC')
28+
OSM_KEYS = _cfg.get("osm_keys")
29+
TAG_KEY = _cfg.get("data_viz", "tag_key")
30+
END_DATE = pd.Timestamp(_cfg.get("end_date"), tz='UTC')
2931

3032
max_days = 365 * 10
3133
VIZ_DIR.mkdir(parents = True, exist_ok = True)
@@ -60,7 +62,7 @@ def fig_save(
6062
# Read observations
6163
# Drop the first observation for each POI (when the POI was first added) - the last
6264
# observation timestamp will be missing for these rows
63-
timestamp_cols = ['obs_timestamp', 'last_obs_timestamp', 'last_tag_timestamp']
65+
timestamp_cols = _cfg.get("data_viz", "timestamp_cols")
6466
observations_df = (pd.read_csv(SAVE_DIR / f"osm_observations_{TAG_KEY}.csv")
6567
.dropna(subset = timestamp_cols)
6668
)
@@ -110,7 +112,7 @@ def fig_save(
110112
fig_save(fig, stub = f"osm_changes_{TAG_KEY}_all")
111113

112114
# Create multi-panel plots for the top tags in each OSM category
113-
TOP_N_TYPES = 10
115+
TOP_N_TYPES = _cfg.get("data_viz", "top_n_types")
114116
for subtype in OSM_KEYS:
115117
fig = change_multiplot_create(
116118
observations = to_plot_df,

exploratory/osm_data/download.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
3. Save the results to CSV files.
88
"""
99
import datetime
10-
import os
10+
from config_versioned import Config
1111
from openpois.osm.download import (
1212
build_date_range,
1313
collect_element_ids,
@@ -18,15 +18,17 @@
1818
# Configuration constants
1919
# -----------------------------------------------------------------------------
2020

21-
TIMEOUT = 1000
22-
BBOX = {"ymin": 47.41, "xmin": -122.48, "ymax": 47.79, "xmax": -122.16}
23-
START_DATE = datetime.datetime(2016, 1, 1) # Earliest option is September 13, 2012
24-
END_DATE = datetime.datetime(2025, 12, 31) # Latest
25-
DATE_INTERVAL = datetime.timedelta(days = 7)
26-
OSM_KEYS = ["amenity", "shop", "healthcare", "leisure"]
27-
SAVE_DIR = "~/data/osm_example_data"
21+
_cfg = Config("~/repos/openpois/config.yaml")
2822

29-
os.makedirs(SAVE_DIR, exist_ok = True)
23+
TIMEOUT = _cfg.get("download", "timeout")
24+
BBOX = _cfg.get("download", "bbox")
25+
START_DATE = datetime.datetime.combine(_cfg.get("download", "start_date"), datetime.time.min) # Earliest option is September 13, 2012
26+
END_DATE = datetime.datetime.combine(_cfg.get("end_date"), datetime.time.min) # Latest
27+
DATE_INTERVAL = datetime.timedelta(days=_cfg.get("download", "date_interval_days"))
28+
OSM_KEYS = _cfg.get("osm_keys")
29+
SAVE_DIR = _cfg.get_dir_path("osm_download")
30+
31+
SAVE_DIR.mkdir(parents=True, exist_ok=True)
3032

3133

3234
# -----------------------------------------------------------------------------
@@ -50,12 +52,11 @@
5052
)
5153

5254
# Save elements table
53-
elements_table.to_csv(
54-
os.path.join(SAVE_DIR, "osm_elements.csv"),
55-
index = False,
55+
_cfg.write(elements_table, "osm_download", "osm_elements")
56+
print(
57+
f"Succeeded on {len(succeed_dates)} dates, "
58+
f"failed on {len(failed_dates)}"
5659
)
57-
print(f"Saved {len(elements_table)} elements to osm_elements.csv")
58-
print(f"Succeeded on {len(succeed_dates)} dates, failed on {len(failed_dates)}")
5960

6061
# Download element histories
6162
versions_df, changes_df, failed_rows = download_element_histories(
@@ -65,19 +66,8 @@
6566
)
6667

6768
# Save results
68-
versions_df.to_csv(
69-
os.path.join(SAVE_DIR, "osm_versions.csv"),
70-
index=False,
71-
)
72-
changes_df.to_csv(
73-
os.path.join(SAVE_DIR, "osm_changes.csv"),
74-
index = False,
75-
)
69+
_cfg.write(versions_df, "osm_download", "osm_versions")
70+
_cfg.write(changes_df, "osm_download", "osm_changes")
7671
print(f"Saved {len(versions_df)} versions and {len(changes_df)} changes")
77-
78-
print(f"Failed on {len(failed_rows)} elements")
79-
failed_elements = elements_table.iloc[failed_rows, :]
80-
failed_elements.to_csv(
81-
os.path.join(SAVE_DIR, "osm_failed_elements.csv"),
82-
index = False,
83-
)
72+
_cfg.write(failed_rows, "osm_download", "osm_failed_elements")
73+
print(f"Failed on {len(failed_rows)} elements")

exploratory/osm_data/format_tabular.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010

1111
import pandas as pd
12-
from pathlib import Path
12+
from config_versioned import Config
1313

1414
from openpois.osm.format_observations import format_observations
1515

@@ -18,10 +18,11 @@
1818
# Configuration constants
1919
# ----------------------------------------------------------------------------------------
2020

21-
DATA_VERSION = "20260129"
22-
SAVE_DIR = Path("~/data/openpois").expanduser() / DATA_VERSION
23-
OSM_KEYS = ["amenity", "shop", "healthcare", "leisure"]
24-
TAG_KEY = "shop"
21+
_cfg = Config("~/repos/openpois/config.yaml")
22+
23+
SAVE_DIR = _cfg.get_dir_path("osm_data")
24+
OSM_KEYS = _cfg.get("osm_keys")
25+
TAG_KEY = _cfg.get("format_tabular", "tag_key")
2526

2627

2728
# ----------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)