/
toolbox_idynomics.py
385 lines (322 loc) · 13.5 KB
/
toolbox_idynomics.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
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
#!/usr/bin/python
from __future__ import division
from __future__ import with_statement
import numpy
import os
import sys
import toolbox_basic
import toolbox_results
import toolbox_schematic_new as toolbox_schematic
pi = numpy.pi
class SimulationDirectory:
def __init__(self, path):
self.path = toolbox_basic.check_path(path)
self.iterate_numbers = []
self.iterate_information = []
self.min_max_concns = {}
# agent_Sum
try:
self.agent_Sum = os.path.join(self.path, 'agent_Sum')
if not os.path.isdir( self.agent_Sum ):
toolbox_basic.unzip_files(self.agent_Sum + '.zip')
self.agent_Sum = toolbox_basic.check_path(self.agent_Sum)
except TypeError:
print('Could not find agent_Sum info! '+self.path)
# agent_State
try:
self.agent_State = os.path.join(self.path, 'agent_State')
if not os.path.isdir( self.agent_State ):
toolbox_basic.unzip_files(self.agent_State + '.zip')
self.agent_State = toolbox_basic.check_path(self.agent_State)
except TypeError:
print('Could not find agent_State info! '+self.path)
# env_Sum
try:
self.env_Sum = os.path.join(self.path, 'env_Sum')
if not os.path.isdir( self.env_Sum ):
toolbox_basic.unzip_files(self.env_Sum + '.zip')
self.env_Sum = toolbox_basic.check_path(self.env_Sum)
except TypeError:
print('Could not find env_Sum info! '+self.path)
# env_State
try:
self.env_State = os.path.join(self.path, 'env_State')
if not os.path.isdir( self.env_State ):
toolbox_basic.unzip_files(self.env_State + '.zip')
self.env_State = toolbox_basic.check_path(self.env_State)
except TypeError:
print('Could not find env_State info! '+self.path)
# Figures directory
self.figures_dir = os.path.join(self.path, 'figures')
if not os.path.isdir(self.figures_dir):
toolbox_basic.make_dir(self.figures_dir)
self.movies_dir = os.path.join(self.path, 'movies')
if not os.path.isdir(self.movies_dir):
toolbox_basic.make_dir(self.movies_dir)
def get_iterate_numbers(self):
"""
Returns a (sorted) list of the iterate numbers, from agent_Sum
"""
if not self.iterate_numbers == []:
return self.iterate_numbers
for f in toolbox_basic.file_list(self.agent_Sum, filetype='*.xml'):
output = toolbox_results.Output(path=f)
self.iterate_numbers.append(output.iterate)
self.iterate_numbers.sort()
return self.iterate_numbers
def get_iterate_information(self):
"""
Tries to read in all of the iterates for this simulation. Can be
time-consuming for large or long simulations.
"""
self.iterate_information = []
for i in self.get_iterate_numbers():
self.iterate_information.append(IterateInformation(self, i))
return self.iterate_information
def get_last_iterate_number(self):
"""
"""
return max(self.get_iterate_numbers())
def get_single_iterate(self, number):
"""
Tries to get information for a single iteration, first by checking the
list of iterates already read in, then by reading in the output files.
"""
for i in self.iterate_information:
if i.number == number:
return i
i = IterateInformation(self, number)
self.iterate_information.append(i)
return i
def get_min_max_concns(self):
"""
"""
if self.min_max_concns == {}:
for solute_name in self.get_solute_names():
self.min_max_concns[solute_name] = [sys.float_info.max, 0.0]
for i in self.get_iterate_information():
iter_min_max = i.get_min_max_concns()
for solute_name in self.min_max_concns.keys():
self.min_max_concns[solute_name] = \
[min(self.min_max_concns[solute_name][0],
iter_min_max[solute_name][0]),
max(self.min_max_concns[solute_name][1],
iter_min_max[solute_name][1])]
return self.min_max_concns
def get_solute_names(self):
"""
"""
return self.get_iterate_information()[0].env_output.get_solute_names()
def get_species_names(self):
"""
"""
return self.get_single_iterate(0).agent_output.get_species_names()
def find_protocol_file_xml_tree(self, filename=None):
"""
"""
if filename is None:
filename = toolbox_basic.find_protocol_file_path(self.path)
self.protocol_file_xml_tree = toolbox_basic.get_xml_tree(filename)
def find_domain_dimensions(self):
"""
TODO Do this via the protocol file.
"""
env0 = self.get_single_iterate(0).env_output
name = env0.get_solute_names()[0]
sol0 = toolbox_results.SoluteOutput(env0, name)
return sol0.grid_nI, sol0.grid_nJ, sol0.grid_nK, sol0.grid_res
'''
try:
pfxt = self.protocol_file_xml_tree
except Error:
self.find_protocol_xml_tree()
'''
def clean_up(self):
"""
Deletes all unzipped output folders TODO
"""
pass
class ProtocolFile:
def __init__(self, path):
pass
class IterateInformation:
def __init__(self, simulation_directory, iterate_number):
self.number = iterate_number
self.min_max_concns = {}
agent_path = os.path.join(simulation_directory.agent_State,
'agent_State(%d).xml'%(iterate_number))
agent_path = toolbox_basic.check_path(agent_path)
self.agent_output = toolbox_results.AgentOutput(path=agent_path)
self.time = self.agent_output.time
env_path = os.path.join(simulation_directory.env_State,
'env_State(%d).xml'%(iterate_number))
env_path = toolbox_basic.check_path(env_path)
self.env_output = toolbox_results.EnvOutput(path=env_path)
def get_min_max_concns(self):
if self.min_max_concns == {}:
for solute_name in self.env_output.get_solute_names():
solute_output = toolbox_results.SoluteOutput(self.env_output,
name=solute_name)
self.min_max_concns[solute_name] = [min(solute_output.values),
max(solute_output.values)]
return self.min_max_concns
def draw_cell_2d(axis, cell_output, total_radius=True, zorder=0, y_limits=None):
"""
"""
(x, y, z) = cell_output.get_location()
rad = cell_output.get_radius(total_radius=total_radius)
if cell_output.color == None:
print 'Cell has no defined color!'
col = (0, 1, 0)
else:
col = cell_output.color
#col = (0, 1, 0) if cell_output.color == None else cell_output.color
#col = cell_output.color
if (y_limits != None) and (y - rad < y_limits[0]):
segment = toolbox_schematic.CircleSegment()
segment.set_defaults(edgecolor='none', facecolor=col, zorder=zorder)
angle = pi - numpy.arccos((y - y_limits[0])/rad)
segment.set_points((y, x), rad, [angle, -angle])
segment.draw(axis)
segment.set_points((y - y_limits[0] + y_limits[1], x), rad, [angle, 2*pi-angle])
segment.draw(axis)
elif (y_limits != None) and (y + rad > y_limits[1]):
segment = toolbox_schematic.CircleSegment()
segment.set_defaults(edgecolor='none', facecolor=col, zorder=zorder)
angle = numpy.arccos((y_limits[1] - y)/rad)
segment.set_points((y, x), rad, [angle, 2*pi-angle])
segment.draw(axis)
segment.set_points((y + y_limits[0] - y_limits[1], x), rad, [-angle, angle])
segment.draw(axis)
else:
circle = toolbox_schematic.Circle()
circle.set_defaults(edgecolor='none', facecolor=col, zorder=zorder)
circle.set_points((y, x), rad)
circle.draw(axis)
def plot_cells_2d(axis, agent_output, zorder=0):
"""
"""
print('Plotting %d cells'%(len(agent_output.get_all_cells())))
width = agent_output.grid_nJ * agent_output.grid_res
y_lims = [0, width]
for cell in agent_output.get_all_cells():
draw_cell_2d(axis, cell, zorder=zorder, y_limits=y_lims)
def draw_cell_3d(axis, cell_output, total_radius=True, zorder=0, y_limits=None):
"""
"""
(x, y, z) = cell_output.get_location()
rad = cell_output.get_radius(total_radius=total_radius)
if cell_output.color == None:
print 'Cell has no defined color!'
col = (0, 1, 0)
else:
col = cell_output.color
#col = (0, 1, 0) if cell_output.color == None else cell_output.color
#col = cell_output.color
sphere = toolbox_schematic.Sphere()
sphere.set_defaults(edgecolor='none', facecolor=col, zorder=zorder)
sphere.set_points((y, z, x+4), rad)
sphere.draw(axis)
def plot_cells_3d(axis, agent_output, zorder=0):
"""
"""
res = agent_output.grid_res
width = agent_output.grid_nJ * res
height = agent_output.grid_nI * res
depth = agent_output.grid_nK * res
num_cells = len(agent_output.get_all_cells())
counter = 0
for cell in agent_output.get_all_cells():
draw_cell_3d(axis, cell, zorder=zorder)
counter += 1
sys.stdout.write('\r')
i = int(20*counter/num_cells)
sys.stdout.write("Plotting cells [%-20s] %d%%" % ('='*i, 5*i))
sys.stdout.flush()
sys.stdout.write('\n')
axis.set_xlim(0, width)
axis.set_ylim(0, depth)
axis.set_zlim(0, height)
def get_default_species_colors(sim):
colors = ['red', 'blue', 'green', 'cyan', 'yellow', 'purple', 'brown']
out = {}
for species_name in sim.get_species_names():
if len(colors) == 0:
print "Not enough default colors for so many species!"
return out
out[species_name] = colors.pop(0)
return out
def save_color_dict(color_dict, file_path):
script = 'Item\t\tColor\n'
for key, value in color_dict.iteritems():
script += str(key)+'\t\t'+str(value)+'\n'
with open(file_path, 'w') as f:
f.write(script)
def read_color_dict(file_path):
out = {}
file_path = toolbox_basic.check_path(file_path)
with open(file_path, 'Ur') as f:
for line in f.readlines()[1:]:
line = line.replace('\n', '')
vals = line.split('\t\t')
out[vals[0]] = vals[1]
return out
def color_cells_by_species(agent_output, species_color_dict):
"""
"""
for species in agent_output.species_outputs:
print('Colouring %d %s cells %s'
%(len(species.members), species.name, species_color_dict[species.name]))
for cell in species.members:
cell.color = species_color_dict[species.name]
# Find a list of standard colormaps (cmap) at
# http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
# It is also possible to define your own
def solute_contour(axis, solute_output, interpolation='nearest', zorder=-10,
cmap='gray', concn_range=[None]*2, array_multiplier=1):
"""
"""
width = solute_output.grid_nJ * solute_output.grid_res
height = solute_output.grid_nI * solute_output.grid_res
extent = [0, width, 0, height]
array = solute_output.concentration_array()
if not array_multiplier == 1:
array = numpy.multiply(array, array_multiplier)
cs = axis.imshow(array,
interpolation=interpolation, origin='lower', cmap=cmap,
extent=extent, zorder=zorder, vmin=concn_range[0], vmax=concn_range[1])
return cs
def solute_contour_3d(axis, solute_output, zorder=-10,
cmap='gray', concn_range=[None]*2, array_multiplier=1):
"""
"""
array = solute_output.concentration_array()
# The array will be in 3D
if not array_multiplier == 1:
array = numpy.multiply(array, array_multiplier)
if not concn_range == [None]*2:
concn_range = [numpy.min(array), numpy.max(array)]
levels = numpy.linspace(concn_range[0], concn_range[1], 128)
res = solute_output.grid_res
nI = solute_output.grid_nI
nJ = solute_output.grid_nJ
nK = solute_output.grid_nK
Y, Z = numpy.meshgrid(numpy.linspace(0, res*nK, nK),
numpy.linspace(0, res*nI, nI))
axis.contourf(array[:, :, 0], Y, Z, zdir='x', cmap=cmap, offset=0,
zorder=zorder, levels=levels)
X, Z = numpy.meshgrid(numpy.linspace(0, res*nJ, nJ),
numpy.linspace(0, res*nI, nI))
cs = axis.contourf(X, array[:, 0, :], Z, zdir='y', cmap=cmap, offset=0,
zorder=zorder, levels=levels)
# Plots a black surface at the bottom. Could be done better!
array = numpy.ones([nJ, nK])*concn_range[0]
X, Y = numpy.meshgrid(numpy.linspace(0, res*nJ, nJ),
numpy.linspace(0, res*nK, nK))
axis.contourf(X, Y, array, zdir='z', cmap='gray', offset=0,
zorder=zorder, levels=levels)
X = [0, 0, 0, res*nJ, res*nJ]
Y = [res*nK, res*nK, 0, 0, 0]
Z = [0, res*nI, res*nI, res*nI, 0]
axis.plot(X, Y, Z, 'k-')
return cs