forked from mynameisfiber/lpocolypse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_impact.py
More file actions
161 lines (136 loc) · 4.73 KB
/
sample_impact.py
File metadata and controls
161 lines (136 loc) · 4.73 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Force matplotlib to not use any Xwindows backend.
import matplotlib
matplotlib.use('Agg') # NOQA
import geopandas as gp
import pandas as pd
import numpy as np
from tqdm import tqdm
import pyemd
from shapely.geometry import Point
import pylab as py
from sample_api import time_sampler
import plot_utils
from multiprocessing import Pool
from functools import partial, partialmethod
import pickle
import os
import sys
def _num_samples(dirname):
try:
return len(os.listdir(dirname))
except IOError:
return 0
with open("./data/geoid_sorted_distances.npy", 'rb') as fd:
geoid_distances = np.load(fd)
class LocationImpact():
borough_cdfs = {}
boroughs = []
def __init__(self):
self.borough_cdfs, self.boroughs = self._calc_borough_cdf()
def _calc_borough_cdf(self):
borough_cdfs = {}
boroughs = os.listdir("./data/samples/L/00:00:00")
for time in os.listdir("./data/samples/L"):
weights = [
_num_samples(os.path.join(
"./data/samples/L/",
time,
b
))
for b in boroughs
]
weights_cdf = np.cumsum(weights).astype('float32')
weights_cdf /= weights_cdf[-1]
borough_cdfs[time] = weights_cdf
return borough_cdfs, boroughs
def borough_sampler(self, time):
idx = np.searchsorted(self.borough_cdfs[time], np.random.random())
return self.boroughs[idx]
def sample_transit_vector(self, time=None, borough=None):
time = time or time_sampler()
borough = borough or self.borough_sampler(time)
datadir = os.path.join(
"./data/samples/L/",
time,
borough,
)
points_str = os.listdir(datadir)
location_str = np.random.choice(points_str)
dfs = []
for exclude_l in ("L", "NO_L"):
filename = os.path.join(
"./data/samples/",
exclude_l,
time,
borough,
location_str
)
try:
with open(filename, 'rb') as fd:
dfs.append(pickle.load(fd))
except IOError:
return self.sample_transit_vector(time, borough)
return pd.concat(dfs)
def sample_impact(self, time=None, borough=None):
df = self.sample_transit_vector(time, borough)
df_loc = df.set_index('to_location').fillna(0)
df_l = df_loc.query('exclude_l == False')
df_nol = df_loc.query('exclude_l == True')
impact = pyemd.emd(
np.ascontiguousarray(df_nol.transit_time.values),
np.ascontiguousarray(df_l.transit_time.values),
geoid_distances
)
df_new = (df.drop(['transit_time', 'to_location', 'exclude_l'], axis=1)
.drop_duplicates())
df_new['impact'] = impact
return df_new
def _exception_eater(f, *args, **kwargs):
try:
return f()
except Exception as e:
print('Ate exception while sampling: {}'.format(e))
except KeyboardInterrupt:
raise
return None
def multiprocess_sampler(f, N):
pool = Pool()
samples = pool.imap(partial(_exception_eater, f),
range(N),
chunksize=8)
return list(filter(lambda x: x is not None,
tqdm(samples, total=N)))
if __name__ == "__main__":
try:
gdf = gp.read_file(sys.argv[1])
except (IndexError, IOError):
sampler = LocationImpact()
df = pd.concat(multiprocess_sampler(
sampler.sample_impact,
10000
))
points = [Point(xy) for xy in zip(df.location_x, df.location_y)]
crs = {'init': 'epsg:4326'}
gdf = gp.GeoDataFrame(df, crs=crs, geometry=points)
try:
gdf.to_file(sys.argv[1])
except IndexError:
pass
except IOError as e:
print("Couldn't save file: {}".format(e))
for borough in (None, 'Brooklyn', 'Queens', 'Bronx',
'Manhattan', 'Staten Island'):
print("Plotting for: " + (borough or 'nyc'))
if borough:
subset = gdf.query('borough == "{}"'.format(borough))
else:
subset = gdf
py.clf()
plot_utils.plot_points(subset, 'impact', neighbors=64, bins=(1024, 1024), percentile=True)
py.savefig("figures/impact_{}_percentile.png".format(borough or 'nyc'),
dpi=600)
py.clf()
plot_utils.plot_points(subset, 'impact', neighbors=64, bins=(1024, 1024),
percentile=False, log=True)
py.savefig("figures/impact_{}_log.png".format(borough or 'nyc'),
dpi=600)