-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate3d.py
389 lines (353 loc) · 14.5 KB
/
animate3d.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
386
387
388
389
# animate3d.py ---
#
# Filename: animate3d.py
# Description:
# Author: Subhasis Ray
# Maintainer:
# Created: Thu Aug 11 09:49:49 2011 (+0530)
# Version:
# Last-Updated: Tue Sep 27 20:51:36 2011 (+0530)
# By: Subhasis Ray
# Update #: 730
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
# Attempt to use vtk for 3D visualization of data from Traub model simulation
#
#
# Change log:
#
#
#
# Code:
import sys
from collections import defaultdict
import numpy
import csv
import h5py
import vtk
from vtk.util import numpy_support as vtknp
cmp_spring_matrix = numpy.loadtxt('spring.cmp')
cmp_autumn_matrix = numpy.loadtxt('autumn.cmp')
cmp_winter_matrix = numpy.loadtxt('winter.cmp')
cmp_summer_matrix = numpy.loadtxt('summer.cmp')
cmp_bone_matrix = numpy.loadtxt('bone.cmp')
cmp_hot_matrix = numpy.loadtxt('hot.cmp')
cmp_cool_matrix = numpy.loadtxt('cool.cmp')
class TraubDataHandler(object):
"""Reader for data generated by Traub model."""
def __init__(self):
self.class_cell = defaultdict(list)
self.class_pos = {}
self.cellname = [] # List of cells
self.pos = None # List of positions - must have same order as cellname
self.vm = None # List of vms - must have the same order as cellname
self.datafile = None
self.cellclass = []
self.simtime = None
self.plotdt = None
def read_posdata(self, filename):
"""Read position data from file"""
with open(filename, 'r') as filehandle:
reader = csv.DictReader(filehandle, fieldnames=['cellclass', 'depth', 'start', 'end', 'dia', 'layer', 'isInTraub'], delimiter=',', quotechar='"')
reader.next() #Skip the header row
for row in reader:
self.class_pos[row['cellclass']] = row
self.cellclass.append(row['cellclass'])
def read_celldata(self,filename):
"""Read the Vm data from the hdf5 file"""
print 'Start read_celldata'
self.datafile = h5py.File(filename, 'r')
self.vm_node = self.datafile['/Vm']
for cellname in self.vm_node.keys():
tmp = cellname.partition('_')
cellclass = tmp[0]
self.class_cell[cellclass].append(cellname)
for cellclass in self.cellclass:
print cellclass, len(self.class_cell[cellclass])
self.cellname.extend(self.class_cell[cellclass])
for cellname in self.cellname:
tmp = numpy.zeros(shape=self.vm_node[cellname].shape, dtype=self.vm_node[cellname].dtype)
tmp[:] = self.vm_node[cellname][:]
if self.vm is None:
self.vm = tmp
else:
self.vm = numpy.vstack((self.vm, tmp))
print 'Finished read_celldata'
def generate_cellpos(self, diascale=1.0):
"""Create random positions based on depth and diameter data
for the cells"""
old_pos_len = 0
for cellclass in self.cellclass:
start = float(self.class_pos[cellclass]['start'])
end = float(self.class_pos[cellclass]['end'])
# print cellclass, 'Start:', start, 'End:', end
rad = float(self.class_pos[cellclass]['dia'])/2.0
size = len(self.class_cell[cellclass])
zpos = -numpy.random.uniform(low=start, high=end, size=size)
rpos = rad * numpy.sqrt(numpy.random.uniform(low=0, high=1.0, size=size))
theta = numpy.random.uniform(low=0, high=2*numpy.pi, size=size)
xpos = rpos * numpy.cos(theta)
ypos = rpos * numpy.sin(theta)
pos = numpy.column_stack((xpos, ypos, zpos))
# print cellclass, 'Positions:'
# print pos
if pos.size == 0:
print 'Zero length position for', cellclass
continue
if self.pos is None:
self.pos = pos
else:
self.pos = numpy.concatenate((self.pos, pos))
print cellclass, 'start: %d, end: %d' % (old_pos_len, len(self.pos))
old_pos_len = len(self.pos)
self.pos = numpy.array(self.pos, copy=True, order='C')
print 'Position has shape:', self.pos.shape
def update_times(self):
try:
self.simtime = self.datafile.attrs['simtime']
self.plotdt = self.datafile.attrs['plotdt']
except KeyError, e:
print 'simtime or plotdt attribute absent'
self.simtime = None
self.plotdt = None
try:
firstcell = self.cellname[0]
self.num_time = len(self.vm_node[firstcell])
except IndexError, e:
print 'There are no Vm arrays in the data file.'
raise e
if self.simtime is None or self.plotdt is None:
self.simtime = float(self.num_time)
self.plotdt = 1.0
def get_vm(self, cellclass, step):
"""Get the Vm for all cells at step ordered in the same
sequence as cellname."""
cellrange = self.get_range(cellclass)
if cellrange[0] != cellrange[1]:
try:
vm_range = self.vm[cellrange[0]:cellrange[1], step]
return numpy.array(vm_range, copy=True, order='C')
except IndexError:
print 'Index out of bounds:', cellrange, step, min([len(self.vm[ii]) for ii in range(cellrange[0], cellrange[1])])
return numpy.array([])
def get_range(self, cellclass):
start = 0
end = 0
ii = 0
for classname in self.cellclass:
celllist = self.class_cell[classname]
if classname == cellclass:
end = start + len(celllist)
break
else:
start = start + len(celllist)
return (start, end)
def __del__(self):
if self.datafile:
self.datafile.close()
class TraubDataVis(object):
"""Visualizer for Traub model data"""
def __init__(self):
self.datafile = None
self.posfile = None
self.datahandler = TraubDataHandler()
self.vrange = (-120e-3, 40e-3)
self.cmap = {'SupPyrRS': cmp_spring_matrix,
'SupPyrFRB': cmp_summer_matrix,
'SupLTS': cmp_autumn_matrix,
'SupAxoaxonic': cmp_winter_matrix,
'SupBasket': cmp_bone_matrix,
'SpinyStellate': cmp_spring_matrix,
'TuftedIB': cmp_winter_matrix,
'TuftedRS': cmp_summer_matrix,
'NontuftedRS': cmp_autumn_matrix,
'DeepBasket': cmp_winter_matrix,
'DeepAxoaxonic': cmp_summer_matrix,
'DeepLTS': cmp_spring_matrix,
'TCR': cmp_bone_matrix,
'nRT': cmp_hot_matrix
}
self.static_colors = {'SupPyrRS': (0.0, 0.0, 1.0),
'SupPyrFRB': (0.0, 1.0, 0.0),
'SupLTS': (1.0, 0.0, 0.0),
'SupAxoaxonic': (1.0, 0.0, 1.0),
'SupBasket': (1.0, 1.0, 0.0),
'SpinyStellate': (0.0, 1.0, 1.0),
'TuftedIB': (1.0, 0.5, 0.5),
'TuftedRS': (0.5, 0.5, 1.0),
'NontuftedRS': (0.5, 1.0, 0.5),
'DeepBasket': (0.2, 0.5, 1.0),
'DeepAxoaxonic': (1.0, 0.5, 0.2),
'DeepLTS': (0.5, 1.0, 0.2),
'TCR': (0.5, 0.2, 1.0),
'nRT': (1.0, 0.2, 0.5)
}
# scalarbar_pos: (x_top_left, y_top_left)
def load_data(self, posfilename, datafilename):
self.datahandler.read_posdata(posfilename)
self.datahandler.read_celldata(datafilename)
self.datahandler.generate_cellpos()
self.datahandler.update_times()
def setup_visualization(self, animate=True):
self.positionSource = {}
self.glyphSource = {}
self.mapper = {}
self.glyph = {}
self.actor = {}
self.colorXfun = {}
self.scalarBar = {}
self.renderer = vtk.vtkRenderer()
self.renwin = vtk.vtkRenderWindow()
self.renwin.SetSize(1280, 900)
# self.renwin.SetFullScreen(1)
self.renwin.AddRenderer(self.renderer)
scalarbarX = 0.01
scalarbarY = 0.95
for classname in self.datahandler.cellclass:
cellrange = self.datahandler.get_range(classname)
if cellrange[0] == cellrange[1]:
continue
# print cellrange,
pos = self.datahandler.pos[cellrange[0]: cellrange[1]]
print classname, 'strat-->end', cellrange
# print pos, pos.size
pos_array = vtknp.numpy_to_vtk(pos, deep=True)
points = vtk.vtkPoints()
points.SetData(pos_array)
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
# polydata.GlobalReleaseDataFlagOn()
# data = self.datahandler.get_vm(0)
self.positionSource[classname] = polydata
source = None
if classname.find('Pyr') >= 0:
print 'Cone for', classname
source = vtk.vtkConeSource()
source.SetRadius(1)
source.SetResolution(20)
source.SetHeight(2)
source.SetDirection(0, 0, 1)
else:
source = vtk.vtkSphereSource()
source.SetRadius(1)
source.SetThetaResolution(20)
source.SetPhiResolution(20)
self.glyphSource[classname] = source
glyph = vtk.vtkGlyph3D()
glyph.SetSource(source.GetOutput())
glyph.SetInput(polydata)
glyph.SetScaleModeToDataScalingOff()
glyph.SetScaleFactor(10)
self.glyph[classname] = glyph
colorXfun = vtk.vtkColorTransferFunction()
cmap_matrix = self.cmap[classname]
values = numpy.linspace(self.vrange[0], self.vrange[1], len(cmap_matrix))
for ii in range(len(cmap_matrix)):
colorXfun.AddRGBPoint(values[ii], cmap_matrix[ii][0], cmap_matrix[ii][1], cmap_matrix[ii][2])
self.colorXfun[classname] = colorXfun
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(glyph.GetOutput())
mapper.SetLookupTable(colorXfun)
mapper.ImmediateModeRenderingOn()
self.mapper[classname] = mapper
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetOpacity(0.5)
self.actor[classname] = actor
self.renderer.AddActor(actor)
scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(colorXfun)
scalarBar.SetTitle(classname)
# scalarBar.SetNumberOfLabels(4)
scalarBar.SetPosition(scalarbarX, scalarbarY)
scalarbarY -= 0.07
scalarBar.SetHeight(0.05)
scalarBar.SetWidth(0.30)
scalarBar.SetOrientationToHorizontal()
scalarBar.GetTitleTextProperty().SetOrientation(90.0)
self.scalarBar[classname] = scalarBar
self.renderer.AddActor2D(scalarBar)
# for key, value in self.mapper.items():
# value.GlobalImmediateModeRenderingOn()
# break
print 'End setup_visualization'
def display(self, animate=True, movie=False, filename=None):
print 'TraubDataVis.display::Start: animate: %d, movie: %d, filename: %s' % (animate, movie, filename)
self.camera = vtk.vtkCamera() #self.renderer.GetActiveCamera()
self.camera.SetPosition(0.0, 500.0, -1200.0)
self.camera.SetFocalPoint(0, 0, -1200)
self.camera.ComputeViewPlaneNormal()
self.renderer.SetActiveCamera(self.camera)
self.renderer.ResetCamera()
if not animate:
self.interactor = vtk.vtkRenderWindowInteractor()
self.interactor.SetRenderWindow(self.renwin)
self.interactor.Initialize()
self.interactor.Start()
else:
# self.renwin.SetOffScreenRendering(True)
# self.win2image = vtk.vtkWindowToImageFilter()
# self.win2image.SetInput(self.renwin)
if movie:
self.moviewriter = vtk.vtkFFMPEGWriter()
self.moviewriter.SetQuality(2)
self.moviewriter.SetRate(10)
self.moviewriter.SetInputConnection(self.win2image.GetOutputPort())
self.moviewriter.SetFileName(filename)
self.moviewriter.Start()
else:
pass
# self.imwriter = vtk.vtkPNGWriter()
# self.imwriter.SetInputConnection(self.win2image.GetOutputPort())
time = 0.0
for ii in range(self.datahandler.num_time):
time += self.datahandler.plotdt
print 'Time:', time
for cellclass in self.datahandler.cellclass:
vm = self.datahandler.get_vm(cellclass, ii)
if (vm is None) or len(vm) == 0:
# print 'Error:', cellclass, vm
continue
self.positionSource[cellclass].GetPointData().SetScalars(vtknp.numpy_to_vtk(vm))
self.renwin.Render()
# self.win2image.Modified()
if movie:
self.moviewriter.Write()
else:
pass
# self.imwriter.SetFileName('frame_%05d.png' % (ii))
# self.imwriter.Write()
if movie:
self.moviewriter.End()
print 'TraubDataVis.display::End'
if __name__ == '__main__':
args = sys.argv
posfile = None
datafile = None
animate = False
movie = False
filename = 'traub_animated.avi'
print 'Args', args, len(args)
if len(args) >= 3:
posfile = args[1]
datafile = args[2]
if len(args) > 3:
animate = True
if len(args) > 4:
movie = True
filename = args[4]
else:
posfile = '/home/subha/src/sim/cortical/dataviz/cellpos.csv'
datafile = '/home/subha/src/sim/cortical/py/data/data_20101201_102647_8854.h5'
print 'Visualizing: positions from %s and data from %s' % (posfile, datafile)
vis = TraubDataVis()
vis.load_data(posfile, datafile)
vis.setup_visualization(animate=animate)
vis.display(animate=animate, movie=movie, filename=filename)
#
# animate3d.py ends here