def surf_regular(): """Now visualize the data as done in mlab. """ w = WarpScalar() mayavi.add_filter(w) o = Outline() s = Surface() mayavi.add_module(o) mayavi.add_module(s)
def glyph(): """The script itself. We needn't have defined a function but having a function makes this more reusable. """ # 'mayavi' is always defined on the interpreter. # Create a new VTK scene. mayavi.new_scene() # Read a VTK (old style) data file. r = VTKXMLFileReader() r.initialize(join(dirname(enthought.mayavi.__file__), 'examples', 'data', 'fire_ug.vtu')) mayavi.add_source(r) # Create an outline and a vector cut plane. mayavi.add_module(Outline()) v = VectorCutPlane() mayavi.add_module(v) v.glyph.color_mode = 'color_by_scalar' # Now mask the points and show glyphs (we could also use # Vectors but glyphs are a bit more generic) m = MaskPoints() m.filter.set(on_ratio=10, random_mode=True) mayavi.add_filter(m) g = Glyph() mayavi.add_module(g) # Note that this adds the module to the filtered output. g.glyph.scale_mode = 'scale_by_vector' # Use arrows to view the scalars. g.glyph.glyph_source = g.glyph.glyph_list[1]
def streamline(): """Sets up the mayavi pipeline for the visualization. """ # Create an outline for the data. o = Outline() mayavi.add_module(o) s = Streamline(streamline_type='tube') mayavi.add_module(s) s.stream_tracer.integration_direction = 'both' s.seed.widget.center = 3.5, 0.625, 1.25 s.module_manager.scalar_lut_manager.show_scalar_bar = True i = IsoSurface() mayavi.add_module(i) i.contour.contours[0] = 550 i.actor.property.opacity = 0.5
def view_numpy(): """Example showing how to view a 3D numpy array in mayavi2. """ # 'mayavi' is always defined on the interpreter. mayavi.new_scene() # Make the data and add it to the pipeline. data = make_data() src = ArraySource(transpose_input_array=False) src.scalar_data = data mayavi.add_source(src) # Visualize the data. o = Outline() mayavi.add_module(o) ipw = ImagePlaneWidget() mayavi.add_module(ipw) ipw.module_manager.scalar_lut_manager.show_scalar_bar = True ipw_y = ImagePlaneWidget() mayavi.add_module(ipw_y) ipw_y.ipw.plane_orientation = 'y_axes'
def generate_mayavi_line_plot(self, srcid_sciclasses_list, use_linfit_segments=False, enable_mayavi2_interactive_gui=False): """ Generate a Mayavi mlab 3D plot which summarizes iterative classification of a TUTOR source over the number of epochs used/added. """ # TODO: I would like to plot each sci class a different color # - I should have each science class re-use a single number/color # - I should label which science class by color, as Y axis labels # TODO: I will need to insert a 0 point onto arrays (for s[0]?) if enable_mayavi2_interactive_gui: from enthought.mayavi.scripts import mayavi2 mayavi2.standalone(globals()) from enthought.mayavi import mlab # scalar cut plane plotting module stuff: import enthought.mayavi from enthought.mayavi.modules.scalar_cut_plane import ScalarCutPlane epoch_ids = [0] # x class_groups = [0] # y probs = [0] # z styles = [0] # numbers used for coloring & glyph sizes used_final_classes = [] i_srcid = 0 for final_class in self.pars['interested_sci_classes']: if not finalclass_ordered_dict.has_key(final_class): continue # skip this science class since nothing to plot else: used_final_classes.append(final_class) srcid_sciclasses_list = finalclass_ordered_dict[final_class] for src_id,sci_classes in srcid_sciclasses_list: print 'src_id:', src_id, '\t', final_class sci_classes.generate_linearfit_endpoints_segments() i = 0 for class_name,class_dict in sci_classes.class_dict.iteritems(): class_style = self.sciclass_style_dict[class_name] if use_linfit_segments: for segment_dict in class_dict['linfit_segments']: epoch_ids.extend([segment_dict['epoch_ids'][0]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) epoch_ids.extend(segment_dict['epoch_ids']) class_groups.extend([i_srcid]*len(segment_dict['epoch_ids'])) probs.extend(segment_dict['probs']) styles.extend([class_style]*len(segment_dict['epoch_ids'])) epoch_ids.extend([segment_dict['epoch_ids'][-1]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) else: epoch_ids.extend([class_dict['epoch_ids'][0]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) epoch_ids.extend(class_dict['epoch_ids']) class_groups.extend([i_srcid]*len(class_dict['epoch_ids'])) probs.extend(class_dict['probs']) styles.extend([class_style]*len(class_dict['epoch_ids'])) epoch_ids.extend([class_dict['epoch_ids'][-1]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) i += 1 i_srcid += 3 # Y spacing between srcids within a science-class i_srcid += 20 # Y spacing between science-class groups mlab.plot3d(numpy.array(epoch_ids), numpy.array(class_groups), numpy.array(probs)*100.0, numpy.array(styles), colormap="Paired", tube_radius=1) # extent=[0,600, # 0,i_srcid, # 15, 110]) mlab.axes(xlabel='N of epochs', ylabel='science class', zlabel='% Prob.')#, # DEBUG/UPGRADE: These seem to trigger some bug about Actor methods: # extent=[0,600, # 0,i_srcid, # -10, 110]) title_str = "num_srcids=%d probability_cut=%0.2lf factor_threshold=%0.2lf bin_size=%d poly_order=%d" % (\ self.pars['num_srcids_to_retrieve_plot'], self.pars['sciclass_probability_cut'], self.pars['polyfit_factor_threshold'], self.pars['polyfit_bin_size'], self.pars['polyfit_poly_order']) ##### TITLE: # The 'z' is a flag in Mayavi2 v3.1.0 documentation: #mlab.text(0.01, 0.97, title_str, width=1.0, name='title', z=0.0) mlab.text(0.01, 0.97, title_str, width=1.0, name='title') ##### SCIENCE CLASS LABELS: # TODO: Eventually I would like the class labels to be colored and # placed on the y axis, but this requires: # 1) later mayavi version to allow 3D text positioning # 2) ability to match text color to the line color-map. if 1: used_final_classes.reverse() y = 0.95 for class_name in used_final_classes: class_str = "%2d %s" %(len(finalclass_ordered_dict[class_name]), class_name) mlab.text(0.85, y, class_str, width=0.095*(len(class_str)/20.0)) y -= 0.018 ##### Add a x-axis plane (I can't figure out code to make it opaque) if 0: cp = ScalarCutPlane() mayavi.add_module(cp) cp.implicit_plane._hideshow() # this un-displays the plane cp.implicit_plane.normal = 0,0,1 cp.implicit_plane.origin = 150,168,15 #cp.implicit_plane.position= 0.15 # feature not available yet print '##### cp:' cp.print_traits() print '##### cp.implicit_plane:' cp.implicit_plane.print_traits() print '##### cp.implicit_plane._HideShowAction:' cp.implicit_plane._HideShowAction.print_traits() ##### Camera position: if enable_mayavi2_interactive_gui: camera_distance = 600 else: camera_distance = 1200 enthought.mayavi.tools.camera.view(azimuth=50, elevation=70, # 0:looking down to -z distance=camera_distance, focalpoint=(100,(i_srcid*0.4),50)) #enthought.mayavi.mlab.show_pipeline() # this introspecive feature is not available in current mayavi version. #####If no Mayavi2 GUI, we allow user to resize image before saving file if not enable_mayavi2_interactive_gui: print 'Please resize window & Press a Key.' import curses stdscr = curses.initscr() while 1: c = stdscr.getch() break curses.endwin() ##### Save figure: img_fpath ="/tmp/%s%s.png" %(title_str.replace('=','').replace(' ','_'), self.pars['save_plot_image_suffix']) if os.path.exists(img_fpath): os.system('rm ' + img_fpath) mlab.savefig(img_fpath)#, size=(500,500))#, dpi=200) #size flag doesn't do anything print "Saved:", img_fpath
def contour(): """The script itself. We needn't have defined a function but having a function makes this more reusable. """ # 'mayavi' is always defined on the interpreter. # Create a new scene. mayavi.new_scene() # Read a VTK (old style) data file. r = VTKFileReader() r.initialize(join(dirname(enthought.mayavi.__file__), 'examples', 'data', 'heart.vtk')) mayavi.add_source(r) # Create an outline for the data. o = Outline() mayavi.add_module(o) # Create three simple grid plane modules. # First normal to 'x' axis. gp = GridPlane() mayavi.add_module(gp) # Second normal to 'y' axis. gp = GridPlane() mayavi.add_module(gp) gp.grid_plane.axis = 'y' # Third normal to 'z' axis. gp = GridPlane() mayavi.add_module(gp) gp.grid_plane.axis = 'z' # Create one ContourGridPlane normal to the 'x' axis. cgp = ContourGridPlane() mayavi.add_module(cgp) # Set the position to the middle of the data. cgp.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp = ContourGridPlane() mayavi.add_module(cgp) # Set the axis and position to the middle of the data. cgp.grid_plane.axis = 'y' cgp.grid_plane.position = 15 cgp.contour.filled_contours = True # An isosurface module. iso = IsoSurface(compute_normals=True) mayavi.add_module(iso) iso.contour.contours = [220.0] # An interactive scalar cut plane. cp = ScalarCutPlane() mayavi.add_module(cp) cp.implicit_plane.normal = 0,0,1