2 A module to produce paraview / vtk objects 7 from collections
import defaultdict
12 make a 3d set of cells based on ring of pts extruded along X axis by dx 14 Return points and "relative cells" 18 pts2 = [ [pt[0]+dx,pt[1],pt[2]]
for pt
in pts]
23 bot_cell = range(n, 2*n)
24 cells = [top_cell, bot_cell]
29 cell = [top_cell[ind], top_cell[ind2], bot_cell[ind2], bot_cell[ind]]
47 ang = math.atan2(p[2]-c[2], p[1]-c[1]);
48 byang.append((ang, p))
50 return [p
for a,p
in byang]
55 Convert numpy array like which comes from 'depo_data_0' key of npz 56 file from NumpyDepoSaver to tvtk unstructured grid. 58 from tvtk.api
import tvtk, write_data
62 q = arr[1,:].reshape(npts)
65 indices = list(range(npts))
67 ret = tvtk.PolyData(points=pts)
68 verts = numpy.arange(0, npts, 1)
69 verts.shape = (npts,1)
71 ret.point_data.scalars = indices[:npts]
72 ret.point_data.scalars.name =
'indices' 74 ret.point_data.add_array(q)
75 ret.point_data.get_array(1).name =
'charge' 83 Given a graph object return a tvtk data object with blbos. 85 from tvtk.api
import tvtk, write_data
91 for node, ndata
in gr.nodes.data():
92 if ndata[
'code'] !=
'b':
96 for key,val
in ndata.items():
105 datasetnames.add(key)
107 pts,cells =
extrude(pts, thickness)
109 blob_cells.append((len(pts), cells))
112 ugrid = tvtk.UnstructuredGrid(points = all_points);
113 ptype = tvtk.Polyhedron().cell_type
115 for npts,cells
in blob_cells:
116 cell_ids = [len(cells)]
118 cell_ids.append(len(cell))
119 cell_ids += [offset+cid
for cid
in cell]
120 ugrid.insert_next_cell(ptype, cell_ids)
123 ugrid.cell_data.scalars = list(range(len(values)))
124 ugrid.cell_data.scalars.name =
"indices" 127 for datasetname
in sorted(datasetnames):
128 arr = numpy.asarray([vals.get(datasetname, 0.0)
for vals
in values], dtype=float)
129 ugrid.cell_data.add_array(arr)
130 ugrid.cell_data.get_array(narrays).name = datasetname
138 for other
in gr[node]:
139 odat = gr.nodes[other]
140 if odat[
'code'] ==
'b':
145 for other
in gr[bnode]:
146 odat = gr.nodes[other]
147 if odat[
'code'] ==
's':
153 from tvtk.api
import tvtk, write_data
160 perwpid = defaultdict(Perwpid)
161 for node, ndata
in gr.nodes.data():
162 if ndata[
'code'] !=
'm':
166 raise ValueError(
"bad graph structure")
171 raise ValueError(
"bad graph structure")
175 sdat = gr.nodes[snode]
177 perwpid[wpid].allind.append(snum)
178 sact = sdat[
'activity']
180 chids = ndata[
'chids']
181 perwpid[wpid].allchs += chids
184 perwpid[wpid].values.append((snum,chids,val))
186 for wpid, dat
in perwpid.items():
187 smin =
min(dat.allind)
188 smax =
max(dat.allind)
189 cmin =
min(dat.allchs)
190 cmax =
max(dat.allchs)
191 arr = numpy.zeros((smax-smin+1, cmax-cmin+1))
192 for ind,chids,val
in dat.values:
194 arr[ind - smin, ch - cmin] += val
195 imgdat = tvtk.ImageData(spacing=(1,1,1), origin=(0,0,0))
196 imgdat.point_data.scalars = arr.T.flatten()
197 imgdat.point_data.scalars.name =
'activity' 198 imgdat.dimensions = list(arr.shape)+[1]
199 all_imgdat[wpid] = imgdat
204 Return an array of one point at the center of the blob 206 thickness = bdat[
'span']
207 value = bdat[
'value']
208 arr = numpy.asarray(bdat[
'corners'])
211 center = numpy.array([0.0]*4, dtype=float)
212 center[:3] = numpy.sum(arr, 0) / npts
213 center[0] += 0.5*thickness
220 Return an array of points uniformly sampled in the blob 223 from shapely.geometry
import Polygon, Point
224 thickness = bdat[
'span']
225 value = bdat[
'value']
228 xstart = bdat[
'corners'][0][0]
229 corners = [(cp[2],cp[1])
for cp
in orderpoints(bdat[
'corners'])]
231 pgon = Polygon(corners)
232 nwant =
max(1,
int(pgon.area * thickness * density))
234 min_x, min_y, max_x, max_y = pgon.bounds
236 while len(pts) != nwant:
237 p =
Point([random.uniform(min_x, max_x), random.uniform(min_y, max_y)])
239 pts.append([random.uniform(xstart, xstart+thickness), p.y, p.x, value/nwant]);
240 return numpy.asarray(pts)
246 return Nx4 array with rows made of x,y,z,q. 250 for node, ndata
in gr.nodes.data():
251 if ndata[
'code'] !=
'b':
253 one = sample_method(ndata)
257 arr = numpy.vstack((arr, one))
def blob_uniform_sample(bdat, density)
def blobpoints(gr, sample_method=blob_center)
static int max(int a, int b)
def orderpoints(pointset)
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)