-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathvisualize.py
More file actions
386 lines (297 loc) · 13.5 KB
/
visualize.py
File metadata and controls
386 lines (297 loc) · 13.5 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#==============================================================================
# Visualization functions for the model
#
# By Johnny Lin
# May 2015
#==============================================================================
#------------------------------------------------------------------------------
import numpy as N
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#------------------------------------------------------------------------------
def plot_farm(aFarm, list_of_cattle=[], show=False):
"""Plot a farm.
Parameters
----------
show : boolean
If True, the show function of pyplot is called to show the plot.
Returns
-------
Figure, Axes
The matplotlib Figure and Axes objects defining this plot are
returned.
Notes
-----
For information on plotting grids and gridlines, see:
http://stackoverflow.com/a/19591764 (some code from there, except
their solutions is slightly incorrect in that they do not account
for the fact that the colormap is normalized. See http://
matplotlib.org/api/colors_api.html#matplotlib.colors.Colormap for
details regarding colormap normalization. As a result, I create
the RGB triples for each data location.
"""
#- Initialize data for the farm: where cattle state values of 0 are
# mapped to black, 1 to blue, 2 to white, 3 to gray, and 4 to darker
# gray. The colors in the cmap corresponds to environment, Susceptible,
# Infected, Recovered, Processed. For information about color codes,
# see: http://matplotlib.org/api/colors_api.html:
data = N.zeros((aFarm.length, aFarm.width, 3), dtype='f')
cmap = ['k', 'b', 'w', '0.5', '0.1']
convert = matplotlib.colors.ColorConverter()
if len(list_of_cattle) != 0: #- data for a non-empty farm
for i in list_of_cattle:
temp = N.array( convert.to_rgb( cmap[i.state_as_int()] ) )
data[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
if id(aFarm) != id(i.environ):
raise ValueError("Cattle not all on same farm")
else: #- data for empty farm
pass
#- Create figure and axes objects:
fig, ax = plt.subplots(1, 1)
#- Plot the image, turn off the axes labels, and (maybe) show the plot:
ax.imshow(data, interpolation='none',
extent=[0, aFarm.width, 0, aFarm.length],
zorder=0)
ax.axis('off')
if show == True:
plt.show()
#- Return values:
return fig, ax
#------------------------------------------------------------------------------
def plot_ranch(modelobj, use_objs=None):
"""Plot the entire ranch given a model object.
See the comments in plot_farm for more information.
Parameters
----------
use_objs : tuple, list
Three element Tuple or list giving the Figure (first element),
Axes (next element, a tuple of n elements), and Image objects
(next element, a tuple of n elements), where n is the number of
farms modelobj.num_farms, on which to draw another plot on top of.
Returns
-------
The Figure, Axes, and Image objects of the plot.
Notes
-----
In order for the plot to work, you have to manually pick where the
subplots are in the frame using add_axes (see http://matplotlib.org/
users/artists.html). Note imshow needs to set the aspect keyword to
"auto". See: http://stackoverflow.com/a/13390798.
"""
#- Preliminaries: The colors in each cmap corresponds to environment,
# Susceptible, Infected, Recovered, Processed.
cmap_farm = ['0.0', 'b', 'w', '0.5', '0.1'] #+ Default (element 0) color
cmap_road = ['0.8', 'b', 'w', '0.5', '0.1'] # for an environment must
cmap_stock = ['0.5', 'b', 'w', '0.5', '0.1'] # be a gray
cmap_sale = ['0.2', 'b', 'w', '0.5', '0.1']
cmap_feed = ['0.4', 'b', 'w', '0.5', '0.1']
cmap_abat = ['0.9', 'b', 'w', '0.5', '0.1']
convert = matplotlib.colors.ColorConverter()
if use_objs == None:
fig = plt.figure(figsize=(6,7))
axs = []
imgs = []
#+ Abbreviations:
l_farm = modelobj.list_farms[0].length
w_farm = modelobj.list_farms[0].width
l_stock = modelobj.stocker.length
w_stock = modelobj.stocker.width
l_road = modelobj.roadeast.length
w_road = modelobj.roadeast.width
l_sale = modelobj.salebarn.length
w_sale = modelobj.salebarn.width
l_feed = modelobj.feedlot.length
w_feed = modelobj.feedlot.width
l_abat = modelobj.abattoir.length
w_abat = modelobj.abattoir.width
tot_horiz = (w_farm * 6) + 5
tot_vert = l_farm + l_road + l_stock + 2
tot_horiz = float(tot_horiz)
tot_vert = float(tot_vert)
#+ Setup farms subplots:
axs.append(fig.add_axes( (0.0,
(l_stock+l_road+1)/tot_vert,
w_farm/tot_horiz, l_farm/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_farm+1)/tot_horiz,
(l_stock+l_road+1)/tot_vert,
w_farm/tot_horiz, l_farm/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_farm+1)*2/tot_horiz,
(l_stock+l_road+1)/tot_vert,
w_farm/tot_horiz, l_farm/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_farm+1)*3/tot_horiz,
(l_stock+l_road+1)/tot_vert,
w_farm/tot_horiz, l_farm/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_farm+1)*4/tot_horiz,
(l_stock+l_road+1)/tot_vert,
w_farm/tot_horiz, l_farm/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_farm+1)*5/tot_horiz,
(l_stock+l_road+1)/tot_vert,
w_farm/tot_horiz, l_farm/tot_vert),
frameon=False ))
#+ Setup roadeast and roadwest subplots:
axs.append(fig.add_axes( (0.0,
(l_stock+1)/tot_vert,
(w_road+2)/tot_horiz, l_road/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_road+2+w_sale)/tot_horiz,
(l_stock+1)/tot_vert,
(w_road+2)/tot_horiz, l_road/tot_vert),
frameon=False ))
#+ Setup stocker subplots:
axs.append(fig.add_axes( (0.0, 0.0,
(w_stock+2)/tot_horiz, l_stock/tot_vert),
frameon=False ))
#+ Setup salebarn, feedlot, and abattoir subplots:
axs.append(fig.add_axes( ((w_stock+2)/tot_horiz, 0.0,
w_sale/tot_horiz, (l_stock+2)/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_stock+2+w_sale)/tot_horiz, 0.0,
w_feed/tot_horiz, l_feed/tot_vert),
frameon=False ))
axs.append(fig.add_axes( ((w_stock+2+w_sale+w_feed)/tot_horiz, 0.0,
w_abat/tot_horiz, l_abat/tot_vert),
frameon=False ))
#+ Setup annotation subplot:
axs.append(fig.add_axes( ((w_stock+2+w_sale+w_feed+w_abat)/tot_horiz,
0.0,
(tot_horiz-w_stock+2+w_sale+w_feed+w_abat)/tot_horiz,
l_abat/tot_vert),
frameon=False ))
else:
fig = use_objs[0]
axs = use_objs[1]
imgs = use_objs[2]
#- Plot farms (note that you need to reverse the width position for
# farms to the east of the salebarn):
for ifcount in range(len(modelobj.list_farms)):
iFarm = modelobj.list_farms[ifcount]
data = N.ones((iFarm.length, iFarm.width, 3), dtype='f') \
* float(cmap_farm[0])
for i in iFarm.list_cattle:
temp = N.array( convert.to_rgb( cmap_farm[i.state_as_int()] ) )
if ifcount >= 3:
data[i.loc_in_environ[0],
iFarm.width-1-i.loc_in_environ[1], :] = temp[:]
else:
data[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
if id(iFarm) != id(i.environ):
raise ValueError("Cattle not all on same farm")
if use_objs == None:
ax = axs[ifcount]
imgs.append( ax.imshow(data, interpolation='none',
extent=[0, iFarm.width, 0, iFarm.length],
aspect="auto",
zorder=0) )
ax.axis('off')
else:
img = imgs[ifcount]
img.set_data(data)
plt.draw()
#- Plot roads (note that you need to reverse the width position for
# roadwest which is to the east of the salebarn):
data_re = N.ones((modelobj.roadeast.length, modelobj.roadeast.width, 3),
dtype='f') * float(cmap_road[0])
for i in modelobj.roadeast.list_cattle:
temp = N.array( convert.to_rgb( cmap_road[i.state_as_int()] ) )
data_re[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
data_rw = N.ones((modelobj.roadwest.length, modelobj.roadwest.width, 3),
dtype='f') * float(cmap_road[0])
for i in modelobj.roadwest.list_cattle:
temp = N.array( convert.to_rgb( cmap_road[i.state_as_int()] ) )
data_rw[i.loc_in_environ[0],
modelobj.roadwest.width-1-i.loc_in_environ[1], :] = temp[:]
if use_objs == None:
imgs.append( axs[6].imshow(data_re, interpolation='none',
extent=[0, modelobj.roadeast.width, 0, modelobj.roadeast.length],
aspect="auto",
zorder=0) )
axs[6].axis('off')
imgs.append( axs[7].imshow(data_rw, interpolation='none',
extent=[0, modelobj.roadwest.width, 0, modelobj.roadwest.length],
aspect="auto",
zorder=0) )
axs[7].axis('off')
else:
imgs[6].set_data(data_re)
imgs[7].set_data(data_rw)
plt.draw()
#- Plot stocker:
data = N.ones((modelobj.stocker.length, modelobj.stocker.width, 3),
dtype='f') * float(cmap_stock[0])
for i in modelobj.stocker.list_cattle:
temp = N.array( convert.to_rgb( cmap_stock[i.state_as_int()] ) )
data[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
if use_objs == None:
imgs.append( axs[8].imshow(data, interpolation='none',
extent=[0, modelobj.stocker.width, 0, modelobj.stocker.length],
aspect="auto",
zorder=0) )
axs[8].axis('off')
else:
imgs[8].set_data(data)
plt.draw()
#- Plot salebarn:
data = N.ones((modelobj.salebarn.length, modelobj.salebarn.width, 3),
dtype='f') * float(cmap_sale[0])
for i in modelobj.salebarn.list_cattle:
temp = N.array( convert.to_rgb( cmap_sale[i.state_as_int()] ) )
data[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
if use_objs == None:
imgs.append( axs[9].imshow(data, interpolation='none',
extent=[0, modelobj.salebarn.width, 0, modelobj.salebarn.length],
aspect="auto",
zorder=0) )
axs[9].axis('off')
else:
imgs[9].set_data(data)
plt.draw()
#- Plot feedlot:
data = N.ones((modelobj.feedlot.length, modelobj.feedlot.width, 3),
dtype='f') * float(cmap_feed[0])
for i in modelobj.feedlot.list_cattle:
temp = N.array( convert.to_rgb( cmap_feed[i.state_as_int()] ) )
data[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
if use_objs == None:
imgs.append( axs[10].imshow(data, interpolation='none',
extent=[0, modelobj.feedlot.width, 0, modelobj.feedlot.length],
aspect="auto",
zorder=0) )
axs[10].axis('off')
else:
imgs[10].set_data(data)
plt.draw()
#- Plot abattoir:
data = N.ones((modelobj.abattoir.length, modelobj.abattoir.width, 3),
dtype='f') * float(cmap_abat[0])
for i in modelobj.abattoir.list_cattle:
temp = N.array( convert.to_rgb( cmap_abat[i.state_as_int()] ) )
data[i.loc_in_environ[0], i.loc_in_environ[1], :] = temp[:]
if use_objs == None:
imgs.append( axs[11].imshow(data, interpolation='none',
extent=[0, modelobj.abattoir.width, 0, modelobj.abattoir.length],
aspect="auto",
zorder=0) )
axs[11].axis('off')
else:
imgs[11].set_data(data)
plt.draw()
#- Plot text: On removing text: http://stackoverflow.com/a/5600964:
if use_objs == None:
imgs.append( axs[12].text( 0.05, 0.5,
"t = " + str(modelobj.sim_day) + " d" ) )
axs[12].axis('off')
else:
imgs[12].remove()
imgs[12] = axs[12].text( 0.05, 0.5,
"t = " + str(modelobj.sim_day) + " d" )
axs[12].axis('off')
plt.draw()
#- Return values:
return fig, axs, imgs
#===== end file =====