Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Add the points to the mayavi pipeline"""
_scale = self.scene.camera.parallel_scale
self.clear()
if not np.any(self.tris):
return
fig = self.scene.mayavi_scene
surf = dict(rr=self.points, tris=self.tris)
normals = _create_mesh_surf(surf, fig=fig)
self.src = normals.parent
rep = 'wireframe' if self.rep == 'Wireframe' else 'surface'
# Add the opaque "inside" first to avoid the translucent "outside"
# from being occluded (gh-5152)
if self._block_behind:
surf_rear = pipeline.surface(
normals, figure=fig, color=self.color, representation=rep,
line_width=1)
surf_rear.actor.property.frontface_culling = True
self.surf_rear = surf_rear
self.sync_trait('color', self.surf_rear.actor.property,
mutual=False)
self.sync_trait('visible', self.surf_rear, 'visible')
self.surf_rear.actor.property.opacity = 1.
surf = pipeline.surface(
normals, figure=fig, color=self.color, representation=rep,
line_width=1)
surf.actor.property.backface_culling = True
self.surf = surf
self.sync_trait('visible', self.surf, 'visible')
self.sync_trait('color', self.surf.actor.property, mutual=False)
self.sync_trait('opacity', self.surf.actor.property)
return
if hasattr(self.glyph, 'remove'):
self.glyph.remove()
if hasattr(self.src, 'remove'):
self.src.remove()
_toggle_mlab_render(self, False)
x, y, z = self.points.T
fig = self.scene.mayavi_scene
scatter = pipeline.scalar_scatter(x, y, z, fig=fig)
if not scatter.running:
# this can occur sometimes during testing w/ui.dispose()
return
# fig.scene.engine.current_object is scatter
mode = 'arrow' if self._view == 'arrow' else 'sphere'
glyph = pipeline.glyph(scatter, color=self.color,
figure=fig, scale_factor=self.point_scale,
opacity=1., resolution=self.resolution,
mode=mode)
glyph.actor.property.backface_culling = True
glyph.glyph.glyph.vector_mode = 'use_normal'
glyph.glyph.glyph.clamping = False
if mode == 'arrow':
glyph.glyph.glyph_source.glyph_position = 'tail'
glyph.actor.mapper.color_mode = 'map_scalars'
glyph.actor.mapper.scalar_mode = 'use_point_data'
glyph.actor.mapper.use_lookup_table_scalar_range = False
self.src = scatter
self.glyph = glyph
def view_dft_density(grid,D,bbox,npts=50,doshow=True):
from mayavi import mlab
rho = grid.getdens_interpolated(D,bbox,npts)
# mlab.contour3d(rho,contours=8,opacity=0.5)
mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(rho),
plane_orientation='x_axes',
slice_index=25)
mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(rho),
plane_orientation='y_axes',
slice_index=25)
mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(rho),
plane_orientation='z_axes',
slice_index=25)
if doshow: mlab.show()
return
# starts[i,:] = coor[r1,:]
# vecs[i,:] = coor[r2,:] - coor[r1,:]
# adjdat[i,:]
# i+=1
adjdat = A.flat[ixes]
A_r = np.tile(coor,(n,1,1))
starts = np.reshape(A_r,(n*n,3))[ixes,:]
vecs = np.reshape(A_r - np.transpose(A_r,(1,0,2)),(n*n,3))[ixes,:]
#plotting
fig=mlab.figure()
nodesource = mlab.pipeline.scalar_scatter(
coor[:,0],coor[:,1],coor[:,2],figure=fig)
nodes = mlab.pipeline.glyph(nodesource, scale_mode='none',
scale_factor=3., mode='sphere', figure=fig)
nodes.glyph.color_mode='color_by_scalar'
vectorsrc = mlab.pipeline.vector_scatter(
starts[:,0],starts[:,1],starts[:,2],vecs[:,0],vecs[:,1],vecs[:,2],
figure=fig)
vectorsrc.mlab_source.dataset.point_data.scalars=adjdat
thres=mlab.pipeline.threshold(vectorsrc,
low=0.0001,up=np.max(A),figure=fig)
vectors=mlab.pipeline.vectors(thres, colormap='YlOrRd',
scale_mode='vector', figure=fig)
#assumes that all LH nodes start with L. This is not ideal.
nodesource_lh = mlab.pipeline.scalar_scatter(
self.ds.lab_pos[self.ds.lhnodes,0],
self.ds.lab_pos[self.ds.lhnodes,1],
self.ds.lab_pos[self.ds.lhnodes,2],
figure=self.scene.mayavi_scene)
self.nodes_lh=mlab.pipeline.glyph(nodesource_lh,
scale_mode='none',scale_factor=3.0,mode='sphere',
figure=self.scene.mayavi_scene)
nodesource_rh=mlab.pipeline.scalar_scatter(
self.ds.lab_pos[self.ds.rhnodes,0],
self.ds.lab_pos[self.ds.rhnodes,1],
self.ds.lab_pos[self.ds.rhnodes,2],
figure=self.scene.mayavi_scene)
self.nodes_rh=mlab.pipeline.glyph(nodesource_rh,
scale_mode='none',scale_factor=3.0,mode='sphere',
figure=self.scene.mayavi_scene)
self.txt = mlab.text3d(0,0,83,'',scale=4.0,color=(.8,.6,.98,),
figure=self.scene.mayavi_scene)
self.txt.actor.actor.pickable=0
for nodes in (self.nodes_lh,self.nodes_rh):
nodes.glyph.color_mode='color_by_scalar'
self.ds.chg_lh_nodemask(); self.ds.chg_rh_nodemask()
self.ds.chg_scalar_colorbar()
#scalar colorbar is tied to lh_surf, but we dont really care if surf switches
#between cracked and uncracked so it is more convenient to set up here
self.draw_nodes()
vmax = grid_data.max()
else:
if vmin is None:
vmin = np.min(grid_data)
if vmax is None:
vmax = np.max(grid_data)
grid_data[grid_datavmax]=vmax
otf = PiecewiseFunction()
otf.add_point(vmin,0.0)
otf.add_point(vmax,1.0)
sf = mayavi.tools.pipeline.scalar_field(grid_data)
V = mlab.pipeline.volume(sf,color=color,vmin=vmin,vmax=vmax)
V.trait_get('volume_mapper')['volume_mapper'].blend_mode = 'maximum_intensity'
if color is None:
ctf = ColorTransferFunction()
ctf.add_rgb_point(vmin,107./255,124./255,132./255)
ctf.add_rgb_point(vmin+(vmax-vmin)*0.8,200./255,178./255,164./255)
ctf.add_rgb_point(vmin+(vmax-vmin)*0.9,1.0,210./255,149./255)
ctf.add_rgb_point(vmax,1.0,222./255,141./255)
V._volume_property.set_color(ctf)
V._ctf = ctf
V.update_ctf = True
height = comp.max_y - comp.min_y + 1
width = comp.max_x - comp.min_x + 1
print('height, width', height, width)
print('image shape (should match)', img.shape)
try:
indX = comp.min_x - minX
indY = comp.min_y - minY
data[indY:indY+height,indX:indX+width,comp.z] = img
except:
pass
# marching cube to extract surface
# visualize surface
from mayavi import mlab
mlab.figure(bgcolor=(0, 0, 0), size=(400, 400))
src = mlab.pipeline.scalar_field(data)
# Our data is not equally spaced in all directions:
sx = stack_info['resolution']['x']
sy = stack_info['resolution']['y']
sz = stack_info['resolution']['z']
src.spacing = [float(sy), float(sx), float(sz)]
src.update_image_data = True
x_edges[:,iedge], y_edges[:,iedge], z_edges[:,iedge] = np.dot(coord_edge.T,BasesOneD)
# PLOT CURVED EDGES
connections_elements = np.arange(x_edges.size).reshape(x_edges.shape[1],x_edges.shape[0])
connections = np.zeros((x_edges.size,2),dtype=np.int64)
for i in range(connections_elements.shape[0]):
connections[i*(x_edges.shape[0]-1):(i+1)*(x_edges.shape[0]-1),0] = connections_elements[i,:-1]
connections[i*(x_edges.shape[0]-1):(i+1)*(x_edges.shape[0]-1),1] = connections_elements[i,1:]
connections = connections[:(i+1)*(x_edges.shape[0]-1),:]
# point_cloulds = np.concatenate((x_edges.flatten()[:,None],y_edges.flatten()[:,None],z_edges.flatten()[:,None]),axis=1)
# src = mlab.pipeline.scalar_scatter(x_edges.flatten(), y_edges.flatten(), z_edges.flatten())
src = mlab.pipeline.scalar_scatter(x_edges.T.copy().flatten(), y_edges.T.copy().flatten(), z_edges.T.copy().flatten())
src.mlab_source.dataset.lines = connections
lines = mlab.pipeline.stripper(src)
h_edges = mlab.pipeline.surface(lines, color = (0,0,0), line_width=2)
# mlab.pipeline.surface(lines, color = (0.72,0.72,0.72), line_width=2)
# OLDER VERSION
# for i in range(x_edges.shape[1]):
# mlab.plot3d(x_edges[:,i],y_edges[:,i],z_edges[:,i],color=(0,0,0),tube_radius=edge_width)
# CURVED SURFACES
if plot_surfaces:
nface = smesh.elements.shape[0]
nnode = nsize*nface
nelem = Triangles.shape[0]*nface
Xplot = np.zeros((nnode,3),dtype=np.float64)
xyz = np.array([pos[v] for v in sorted(G)])
# scalar colors
scalars = np.array(G.nodes()) + 5
mlab.figure(1, bgcolor=(0, 0, 0))
mlab.clf()
pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
scalars,
scale_factor=0.1,
scale_mode='none',
colormap='Blues',
resolution=20)
pts.mlab_source.dataset.lines = np.array(G.edges())
tube = mlab.pipeline.tube(pts, tube_radius=0.01)
mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
mlab.savefig('mayavi2_spring.png')
mlab.show() # interactive window
# Unfortunately, adjusting the surface.actor.actor.scale seems to lead to more problems than solutions.
# See: http://stackoverflow.com/questions/13015097/how-do-i-scale-the-x-and-y-axes-in-mayavi2
t_scaling = np.mean( [(max(xyts[:,0])-min(xyts[:,0])), (max(xyts[:,1])-min(xyts[:,1]))] ) / (max(xyts[:,2])-min(xyts[:,2]))
xyts[:,2] *= t_scaling
self.trajectory_temporal_scaling = t_scaling
# Taken from http://docs.enthought.com/mayavi/mayavi/auto/example_plotting_many_lines.html
# Create the lines
self.trajectory_line_source = mlab.pipeline.scalar_scatter(xyts[:,0], xyts[:,1], xyts[:,2], xyts[:,3], \
figure = self.trajectory_scene.mayavi_scene)
# Connect them using the graph edge matrix
self.trajectory_line_source.mlab_source.dataset.lines = np.array(G.edges())
# Finally, display the set of lines by using the surface module. Using a wireframe
# representation allows to control the line-width.
self.trajectory_line_collection = mlab.pipeline.surface(mlab.pipeline.stripper(self.trajectory_line_source), # The stripper filter cleans up connected lines; it regularizes surfaces by creating triangle strips
line_width=1,
colormap=selected_colormap,
figure = self.trajectory_scene.mayavi_scene)
# Generate the corresponding set of nodes
dt = np.median(np.diff(np.unique(nx.get_node_attributes(directed_graph,"t").values())))
self.lineage_temporal_scaling = dt
# Try to scale the nodes in a reasonable way
# To inspect, see pts.glyph.glpyh.scale_factor
node_scale_factor = 0.5*dt
pts = mlab.points3d(xyts[:,0], xyts[:,1], xyts[:,2], xyts[:,3],
scale_factor = 0.0,
scale_mode = 'none',
colormap = selected_colormap,
figure = self.trajectory_scene.mayavi_scene)