-
Notifications
You must be signed in to change notification settings - Fork 2
/
Viz.py
234 lines (219 loc) · 9.53 KB
/
Viz.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from copy import deepcopy
from operator import attrgetter
import numpy as np
from numpy import linalg as LA
import argparse, csv, imageio, json, math, matplotlib, os, random
import matplotlib.pyplot as plt
from matplotlib import gridspec
import matplotlib.image as mpimg
from pylab import cm,imshow,colorbar
font = {'weight' : 'bold',
'size' : 20}
matplotlib.rc('font', **font)
###################################################################################################
###################################################################################################
###################################################################################################
################ __ ______ __ ___ __ __ __ #######################
################ / / / / _/ / |/ /__ / /_/ / ___ ___/ /__ #######################
################ / /_/ // / / /|_/ / -_) __/ _ \/ _ \/ _ (_-< #######################
################ \____/___/ /_/ /_/\__/\__/_//_/\___/\_,_/___/ #######################
###################################################################################################
###################################################################################################
###################################################################################################
def generate_gif(sim):
import imageio
images = []
for plot in range(sim.sim_length):
images.append(imageio.imread(os.path.join(sim.output_directory, "Simulation-T{}.png".format(plot))))
imageio.mimsave(os.path.join(sim.output_directory, 'movie.gif'), images)
def get_ui(sim, components=None, show=True, save=True):
components = sim.ui_components if components == None else components
plt.style.use(sim.ui_style)
cell_color = components[0]
fig = plt.figure(figsize=(20, 9))
subplots = []
ratios = [1.5]
if len(components) > 7:
rows = 4
columns = math.ceil((len(components)-1)/4.0)
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(rows, columns+1, width_ratios=ratios)
elif len(components) > 5:
rows = 3
columns = math.ceil((len(components)-1)/3.0)
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(rows, columns+1, width_ratios=ratios)
elif len(components) > 1:
rows = len(components)-1
columns = math.ceil((len(components)-1)/4.0)
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(rows, columns+1, width_ratios=ratios)
else:
columns = 0
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(1, columns+1, width_ratios=ratios)
subplots.append(plt.subplot(gs[:, 0]))
# plot the cells
coloring = []
if (cell_color == "Amenity"):
coloring = sim.get_amnenities()
elif (cell_color == "Market Prices"):
coloring = sim.get_market_prices()
elif (cell_color == "Proximity"):
coloring = sim.get_proximities()
else: # default to market_prices
cell_color = "Market Prices"
coloring = sim.get_market_prices()
im = imshow(coloring, cmap=cm.coolwarm, vmin=250)
cbar = colorbar(im)
subplots[0].set_title(cell_color, fontsize=20)
if len(components) > 7:
for i in range(len(components) - 1):
subplots.append(plt.subplot(gs[i%4,1+math.floor(i/4.0)]))
elif len(components) > 5:
for i in range(len(components) - 1):
subplots.append(plt.subplot(gs[i%3,1+math.floor(i/3.0)]))
else:
for i in range(len(components) - 1):
subplots.append(plt.subplot(gs[i,1]))
for i in range(1, len(components)):
if components[i] == "Histo: Agent Amenity":
histo_agent_amenity(sim,subplots[i])
elif components[i] == "Histo: Agent Budget":
histo_agent_budget(sim,subplots[i])
elif components[i] == "Histo: Agent Proximity":
histo_agent_proximity(sim,subplots[i])
elif components[i] == "Histo: Market Price":
histo_market_price(sim,subplots[i])
elif components[i] == "Line: Epsilon":
line_epsilon(sim,subplots[i])
elif components[i] == "Scatter: Amenity-Market Price":
scatter_amenity_market_price(sim,subplots[i])
elif components[i] == "Scatter: Proximity-Market Price":
scatter_proximity_market_price(sim,subplots[i])
elif components[i] == "Stackplot: Agent":
stackplot_agent(sim,subplots[i])
elif components[i] == "Stackplot: On Market":
stackplot_on_market(sim,subplots[i])
plt.tight_layout()
if show:
plt.show()
if save:
plt.savefig(os.path.join(sim.output_directory, "Simulation-T{}.png".format(sim.time_step)))
fig.clf()
plt.clf()
plt.close('all')
def histo_agent_budget(sim, subplot):
subplot.hist([agent.budget for agent in sim.agents])
subplot.set_title("Histogram of Agent's Budget")
subplot.set_xlabel("Budget")
subplot.set_ylabel("Number of Agents")
def histo_market_price(sim, subplot):
subplot.hist([cell.market_price for cell in sim.cells])
subplot.set_title("Histogram of Market Price")
subplot.set_xlabel("Market Price")
subplot.set_ylabel("Number of Cells")
def line_epsilon(sim, subplot):
subplot.plot(np.arange(sim.time_step+1), sim.epsilon_over_time, label="Epsilon")
subplot.legend(loc='upper right')
subplot.set_title("Epsilon During Simulation")
subplot.set_xlabel("Time Step")
subplot.set_xlim([0,sim.sim_length])
subplot.set_ylabel("Epsilon")
#subplot.set_ylim([0,sim.num_agents])
def plot_cells(sim, cell_color, components, show=False, save=True):
plt.style.use(sim.ui_style)
fig = plt.figure(figsize=(20, 9))
subplots = []
ratios = [1.5]
if len(components) > 7:
rows = 4
columns = math.ceil((len(components)-1)/4.0)
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(rows, columns+1, width_ratios=ratios)
elif len(components) > 5:
rows = 3
columns = math.ceil((len(components)-1)/3.0)
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(rows, columns+1, width_ratios=ratios)
elif len(components) > 1:
rows = len(components)-1
columns = math.ceil((len(components)-1)/4.0)
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(rows, columns+1, width_ratios=ratios)
else:
columns = 0
for i in range(columns):
ratios.append(1)
gs = gridspec.GridSpec(1, columns+1, width_ratios=ratios)
subplots.append(plt.subplot(gs[:, 0]))
# plot the cells
coloring = []
if (cell_color == "Amenity"):
coloring = sim.get_amenities()
elif (cell_color == "Market Prices"):
coloring = sim.get_market_prices()
elif (cell_color == "Proximity"):
coloring = sim.get_proximities()
else: # default to market_prices
cell_color = "Market Prices"
coloring = sim.get_market_prices()
im = imshow(coloring, cmap=cm.coolwarm)
cbar = colorbar(im)
subplots[0].set_title(cell_color, fontsize=20)
if len(components) > 7:
for i in range(len(components) - 1):
subplots.append(plt.subplot(gs[i%4,1+math.floor(i/4.0)]))
elif len(components) > 5:
for i in range(len(components) - 1):
subplots.append(plt.subplot(gs[i%3,1+math.floor(i/3.0)]))
else:
for i in range(len(components) - 1):
subplots.append(plt.subplot(gs[i,1]))
plt.tight_layout()
if show:
plt.show()
if save:
plt.savefig(os.path.join(sim.output_directory, "{}-Cells.png".format(cell_color)))
fig.clf()
plt.clf()
plt.close('all')
def scatter_amenity_market_price(sim, subplot):
amenities = [cell.amenity for cell in sim.cells]
subplot.scatter(amenities, [cell.market_price for cell in sim.cells])
subplot.set_title("Scatterplot of Market Price v.s. Amenity")
subplot.set_xlabel("Amenity")
_max = max(amenities)
subplot.set_xlim([-_max/50.0,1.02*max(sim.proximity)])
subplot.set_ylabel("Market Price")
def scatter_proximity_market_price(sim, subplot):
to_plot = [(prox, cell.market_price) for prox, cell in zip(sim.proximity,sim.cells) if cell.market_price > 0]
subplot.scatter([p[0] for p in to_plot], [p[1] for p in to_plot])
subplot.set_title("Scatterplot of Market Price v.s. Proximity")
subplot.set_xlabel("Proximity")
_max = max(sim.proximity)
subplot.set_xlim([-_max/50.0,1.02*max(sim.proximity)])
subplot.set_ylabel("Market Price")
def stackplot_agent(sim, subplot):
subplot.stackplot(np.arange(sim.time_step+1), sim.num_buyers, sim.num_sellers, sim.num_neither, labels=["buyers", "sellers", "neither"])
subplot.legend(loc='upper right')
subplot.set_title("Buyers, Sellers, and Non-Participants")
subplot.set_xlabel("Time Step")
subplot.set_xlim([0,sim.sim_length])
subplot.set_ylabel("Number of People")
subplot.set_ylim([0,sim.num_agents])
def stackplot_on_market(sim, subplot):
subplot.stackplot(np.arange(sim.time_step+1), sim.num_on_market, np.subtract([sim.num_cells]*(sim.time_step+1),sim.num_on_market), labels=["on market", "not on market"])
subplot.legend(loc='upper right')
subplot.set_title("Proportion of Cells on Market")
subplot.set_xlabel("Time Step")
subplot.set_xlim([0,sim.sim_length])
subplot.set_ylabel("Number of Cells")
subplot.set_ylim([0,sim.num_cells])