def to_vtk(self, prefix): """ export the result to vtk the mesh and its triangle data go to prefix_name_mesh.vtk, the potential/field/pseudopotential go to prefix_name.vtk all arrays are named """ if self.configuration is not None: self.configuration.to_vtk(prefix) sp = tvtk.StructuredPoints( origin=self.grid.get_origin(), spacing=self.grid.step, dimensions=self.grid.shape) # spw = tvtk.StructuredPointsWriter(input=sp) spw = tvtk.StructuredPointsWriter() spw.set_input_data(sp) spw.file_name = "%s_%s.vtk" % (prefix, self.configuration.name) #xidr = tvtk.XMLImageDataWriter(input=sp) for data_name in "potential field pseudo_potential".split(): data = getattr(self, data_name) if data is None: continue if len(data.shape) > 3: data = data.T.reshape(-1, data.shape[0]) else: data = data.T.flatten() d = tvtk.DoubleArray(name=data_name) d.from_array(data) sp.point_data.add_array(d) spw.write() logging.info("wrote %s", spw.file_name)
def transformNormals(t, pts): """Apply a vtkTransform to a numpy array of normals. I.e. ignore the translation component and normalise the resulting magnitudes""" out = tvtk.DoubleArray(number_of_components=3) t.transform_normals(pts, out) return numpy.asarray(out)
def _get_vtk_data(self): if self.position == 'nodes': ug = self.vtk_node_structure # vtk_r = self.vtk_node_points # vtk_cell_data = self.vtk_node_cell_data elif self.position == 'int_pnts': ug = self.vtk_ip_structure # vtk_r = self.custom_vtk_r # vtk_cell_data = self.custom_vtk_cell_data # ug = self.vtk_structure field_arr = tvtk.DoubleArray(name=self.name) field_arr.from_array(self._get_field_data()) # TODO:naming ug.point_data.add_array(field_arr) # add data for warping if self.warp: warp_arr = tvtk.DoubleArray(name=self.warp_var) warp_arr.from_array(self._get_warp_data()) ug.point_data.add_array(warp_arr) return ug
def get_rs_structured_grid(self, **args): sg = tvtk.StructuredGrid() arr0 = self.recip_arrs[list(self.recip_arrs.keys())[0]] dims = list(arr0.shape) sg.points = self.recip_coords for a in self.recip_arrs.keys(): arr = tvtk.DoubleArray() arr.from_array(self.recip_arrs[a].ravel()) arr.name = a sg.point_data.add_array(arr) sg.dimensions = (dims[2], dims[1], dims[0]) sg.extent = 0, dims[2] - 1, 0, dims[1] - 1, 0, dims[0] - 1 return sg
def to_polydata(self, **kwargs): """ return a vtk PolyData object for this mesh, add named cell_data arrays from **kwargs (need to have same length as self.triangles) """ pd = tvtk.PolyData(points=self.points, polys=self.triangles) e = tvtk.IntArray(name="electrode_index") e.from_array(self.groups) pd.cell_data.add_array(e) for name, data in kwargs.items(): if data is None: continue c = tvtk.DoubleArray(name=name) assert data.shape[0] == self.triangles.shape[0] c.from_array(data) pd.cell_data.add_array(c) return pd
def get_structured_grid(self, **args): self.update_coords() dims = list(self.arr[self.cropobj].shape) self.sg.points = self.coords if args.has_key("mode"): if args["mode"] == "Phase": arr1 = self.arr[self.cropobj].ravel() arr = (np.arctan2(arr1.imag, arr1.real)) else: arr = np.abs(self.arr[self.cropobj].ravel()) else: arr = self.arr[self.cropobj].ravel() if (arr.dtype == np.complex128 or arr.dtype == np.complex64): self.sg.point_data.scalars = np.abs(arr) self.sg.point_data.scalars.name = "Amp" ph = tvtk.DoubleArray() ph.from_array(np.arctan2(arr.imag, arr.real)) ph.name = "Phase" self.sg.point_data.add_array(ph) else: self.sg.point_data.scalars = arr self.sg.dimensions = (dims[2], dims[1], dims[0]) self.sg.extent = 0, dims[2] - 1, 0, dims[1] - 1, 0, dims[0] - 1 return self.sg
# print 'sig_Emab', sig_Enab delta23_ab = np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float_) cell_class = tvtk.Quad().cell_type n_E, n_i, n_a = x_Eia.shape n_Ei = n_E * n_i points = np.einsum('Ia,ab->Ib', x_Eia.reshape(-1, n_c), delta23_ab) ug = tvtk.UnstructuredGrid(points=points) ug.set_cells(cell_class, np.arange(n_Ei).reshape(n_E, n_i)) vectors = np.einsum('Ia,ab->Ib', d_Eia.reshape(-1, n_c), delta23_ab) ug.point_data.vectors = vectors ug.point_data.vectors.name = 'displacement' # Now view the data. warp_arr = tvtk.DoubleArray(name='displacement') warp_arr.from_array(vectors) ug.point_data.add_array(warp_arr) eps_Encd = tensors = np.einsum('...ab,ac,bd->...cd', eps_Enab, delta23_ab, delta23_ab) tensors = eps_Encd[:, :, [0, 1, 2, 0, 1, 2], [0, 1, 2, 1, 2, 0]].reshape(-1, 6) tensors = eps_Encd.reshape(-1, 9) ug.point_data.tensors = tensors ug.point_data.tensors.name = 'strain' @mayavi2.standalone def view(): from mayavi.sources.vtk_data_source import VTKDataSource from mayavi.modules.outline import Outline
def grapher(self): g = nx.read_gpickle(self.subject_path) g.remove_node(83) position_key = "dn_position" edge_key = "fa_mean" node_label_key = "dn_fsname" create_label = g.nodes() #nodes of interest: 8, 18, 35, 37, 49, 59, 76, 78 nr_nodes = len(create_label) position_array = np.zeros( (nr_nodes, 3) ) val_array = np.zeros((nr_nodes, 1)) for i,nodeid in enumerate(create_label): #gives position of certain node pos = g.node[nodeid][position_key] pos = np.array(pos) position_array[i,:] = pos x, y, z = position_array[:,0], position_array[:,1], position_array[:,2] edges = np.array(g.edges()) nr_edges = len(edges) ev = np.zeros( (nr_edges, 1) ) for i,d in enumerate(g.edges_iter(data=True)): ev[i] = d[2][edge_key] assert d[0] == edges[i,0] and d[1] == edges[i,1] edges=edges - 1 start_positions = position_array[edges[:, 0], :].T end_positions = position_array[edges[:, 1], :].T vectors = end_positions - start_positions # Creates Edges nodesource = mlab.pipeline.scalar_scatter(x, y, z, name = 'Node Source') nodes = mlab.pipeline.glyph(nodesource, scale_factor=3.0, scale_mode='none', name = 'Nodes', mode='cube') nodes.glyph.color_mode = 'color_by_scalar' vectorsrc = mlab.pipeline.vector_scatter(start_positions[0], start_positions[1], start_positions[2], vectors[0], vectors[1], vectors[2], name = 'Connectivity Source') da = tvtk.DoubleArray(name=edge_key) da.from_array(ev) vectorsrc.mlab_source.dataset.point_data.add_array(da) vectorsrc.mlab_source.dataset.point_data.scalars = da.to_array() vectorsrc.mlab_source.dataset.point_data.scalars.name = edge_key vectorsrc.outputs[0].update() thres = mlab.pipeline.threshold(vectorsrc, name="Thresholding") #control appearance of edges and nodes (color vs size) #how to mix and match color and size differences myvectors = mlab.pipeline.vectors(thres,colormap='OrRd', #mode='cylinder', name='Connections', scale_factor=1, resolution=20, # make the opacity of the actor depend on the scalar. transparent=True, scale_mode = 'vector') myvectors.glyph.glyph_source.glyph_source.glyph_type = 'dash' myvectors.glyph.color_mode = 'color_by_scalar' myvectors.glyph.glyph.clamping = False # create labels for la in create_label: row_index = la - 1 label = g.node[la][node_label_key] mlab.text3d(position_array[row_index,0], position_array[row_index,1], position_array[row_index,2], ' ' + label, name = 'Node ' + label, color = (0,0,0), scale = 3, line_width = 3) #Scales the nodes for i,nodeid in enumerate(g.nodes()): #nodes of interest: 8, 18, 35, 37, 49, 59, 76, 78 #if nodeid==8 or nodeid==18 or nodeid==35 or nodeid==37 or nodeid==49 or nodeid==59 or nodeid==76 or nodeid==78: pos = g.node[nodeid][position_key] val = g.degree(nodeid) pos = np.array(pos) val = np.array(val) position_array[i,:] = pos val_array[i] = val x, y, z, value = position_array[:,0], position_array[:,1], position_array[:,2], val_array[:,0] #mlab.figure() mlab.points3d(x, y, z, value) mlab.show()
def transformVectors(t, pts): """Apply a vtkTransform to a numpy array of vectors. I.e. ignore the translation component""" out = tvtk.DoubleArray(number_of_components=3) t.transform_vectors(pts, out) return numpy.asarray(out)