-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_decay_configs.py
46 lines (37 loc) · 1.62 KB
/
get_decay_configs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
import pickle
import json
import os
import matplotlib.pyplot as plt
from city_conf import city_mappings
WEIGHT_AT_UPPER_THRESHOLD = 0.1
def derive_exponential_decay_params(dists):
"""Calibrate decay to put weight 0.1 at 90th percentile of road distance from centroid,
and 1.0 at 10th percentile"""
lower_threshold = np.percentile(dists, 10)
upper_threshold = np.minimum(np.percentile(dists, 90), 15)
decay_coef = np.log(WEIGHT_AT_UPPER_THRESHOLD) / (upper_threshold - lower_threshold)
return {
"lower_threshold": round(lower_threshold, 1),
"upper_threshold": round(upper_threshold, 1),
"decay_coef": round(decay_coef, 2)
}
for country_map in city_mappings:
for city in city_mappings[country_map]:
city_name = list(city.keys())[0]
if not (os.path.exists(f"results/{city_name}_distances.pkl") and os.path.exists(
f"results/{city_name}_decay_conf.json")):
print(f"{city_name} - working")
with open(f"results/{city_name}_distances.pkl", "rb") as f:
dists = pickle.load(f)
decay_conf = derive_exponential_decay_params(dists)
with open(f"results/{city_name}_decay_conf.json", "w") as f:
json.dump(decay_conf, f)
plt.hist(dists, bins=100)
plt.axvline(x=decay_conf["lower_threshold"], color="red")
plt.axvline(x=decay_conf["upper_threshold"], color="red")
plt.title(city_name)
plt.savefig(f"results/{city_name}_distance_plot.png")
plt.close()
else:
print(f"{city_name} - skipped")