def test_2d_data(self): """Generic tests for 2D data arrays.""" d = self.data sc, vec = self.make_2d_data() d.origin = (-1, -1, 0) d.scalar_data = sc d.vector_data = vec d.start() # Start the object so it flushes the pipeline etc. # Create an outline for the data. o = Outline() d.add_child(o) o.start() self.assertEqual(tuple(o.actor.actor.bounds), (-1., 0., -1., 0., 0., 0.)) # Create a surface module. surf = Surface() d.add_child(surf) self.assertEqual(surf.running, True) tps = numpy.transpose expect = [tps(sc), tps(vec, (1, 0, 2))] sc1 = surf.actor.mapper.input.point_data.scalars.to_array() self.assertEqual(numpy.allclose(sc1.flatten(), expect[0].flatten()), True) vec1 = surf.actor.mapper.input.point_data.vectors.to_array() self.assertEqual(numpy.allclose(vec1.flatten(), expect[1].flatten()), True)
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() e.new_scene() self.e = e # Read the multi-block plot3d file. r = PLOT3DReader() r.reader.trait_set(has_byte_count=True, multi_grid=True, byte_order='little_endian') r.initialize(get_example_data('tiny.xyz'), get_example_data('tiny.q'), configure=False) e.add_source(r) # Add the filter. f = SelectOutput() e.add_filter(f) # Create an outline for the data. o = Outline() e.add_module(o) o.render() self.o = o self.r = r self.e = e self.scene = e.current_scene return
def test_3d_data(self): "Test for 3D data arrays." # Add a 3D data source d = self.data sc, vec = self.make_3d_data() d.scalar_data = sc d.vector_data = vec d.start() # Start the object so it flushes the pipeline etc. # Create an outline for the data. o = Outline() d.add_child(o) o.start() self.assertEqual(tuple(o.actor.actor.bounds), (0, 1., 0., 1., 0., 1.)) # Create a surface module. surf = Surface() d.add_child(surf) self.assertEqual(surf.running, True) tps = numpy.transpose expect = [tps(sc), tps(vec, (2, 1, 0, 3))] sc2 = surf.actor.mapper.input.point_data.scalars.to_array() self.assertEqual(numpy.allclose(sc2.flatten(), expect[0].flatten()), True) vec2 = surf.actor.mapper.input.point_data.vectors.to_array() self.assertEqual(numpy.allclose(vec2.flatten(), expect[1].flatten()), True)
def test_3d_data(self): "Test for 3D data arrays." # Add a 3D data source d = self.data sc, vec = self.make_3d_data() d.scalar_data = sc d.vector_data = vec d.start() # Start the object so it flushes the pipeline etc. # Create an outline for the data. o = Outline() d.add_child(o) o.start() self.assertEqual(tuple(o.actor.actor.bounds), (0, 1., 0., 1., 0., 1.)) # Create a surface module. surf = Surface() d.add_child(surf) self.assertEqual(surf.running, True) tps = numpy.transpose expect = [tps(sc), tps(vec, (2,1,0,3))] sc2 = surf.actor.mapper.input.point_data.scalars.to_array() self.assertEqual(numpy.allclose(sc2.flatten(), expect[0].flatten()), True) vec2 = surf.actor.mapper.input.point_data.vectors.to_array() self.assertEqual(numpy.allclose(vec2.flatten(), expect[1].flatten()), True)
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() e.new_scene() self.e=e # Read the multi-block plot3d file. r = PLOT3DReader() r.reader.set(has_byte_count=True, multi_grid=True, byte_order='little_endian') r.initialize(get_example_data('tiny.xyz'), get_example_data('tiny.q'), configure=False) e.add_source(r) # Add the filter. f = SelectOutput() e.add_filter(f) # Create an outline for the data. o = Outline() e.add_module(o) o.render() self.o=o self.r=r self.e=e self.scene = e.current_scene return
def setUp(self): e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() s=e.new_scene() image_data = BuiltinImage() e.add_source(image_data) outline = Outline() e.add_module(outline) surface = Surface() e.add_module(surface) image_data.data_source.radius = array([ 80., 80., 80.]) image_data.data_source.center = array([ 150., 150., 0.]) image_data.data_source.whole_extent = array([ 10, 245, 10, 245, 0, 0]) if is_old_pipeline(): image_data.data_source.update_whole_extent() else: image_data.data_source.set_update_extent_to_whole_extent() self.e=e self.scene = e.current_scene return
def view_data(filename, fieldName='x-velocity'): """Visualize 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 = read_data(filename, fieldName) 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.module_manager.scalar_lut_manager.data_name = fieldName ipw_y = ImagePlaneWidget() mayavi.add_module(ipw_y) ipw_y.ipw.plane_orientation = 'y_axes' ipw_z = ImagePlaneWidget() mayavi.add_module(ipw_z) ipw_z.ipw.plane_orientation = 'z_axes'
def test_loop_loops_timesteps(self): # Given e = self.engine # Read a VTK XML data file. r = VTKXMLFileReader() r.initialize(self.abc1) r.timestep = 0 e.add_source(r) # Create an outline for the data. o = Outline() e.add_module(o) values = [] def callback(new): values.append(new) # Shut off so only one loop is done. if new == 0: r.loop = False r.on_trait_change(callback, 'timestep') # When r.loop = True r.play = True # Then expected = [1, 0, 1] self.assertEqual(values, expected) self.assertEqual(r.timestep, 1) self.assertEqual(r.loop, False)
def test_sync_timesteps_steps_relevant_readers(self): # Given e = self.engine # Read a VTK XML data file. r = VTKXMLFileReader() r.initialize(self.abc1) e.add_source(r) # Create an outline for the data. o = Outline() e.add_module(o) r2 = VTKXMLFileReader() r2.initialize(self.def1) e.add_source(r2) r3 = VTKXMLFileReader() r3.initialize(self.xyz1) e.add_source(r3) # When r.sync_timestep = True r.timestep = 1 # Then self.assertEqual(r.timestep, r2.timestep) self.assertNotEqual(r.timestep, r3.timestep) r.timestep = 0 self.assertEqual(r.timestep, r2.timestep)
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(mayavi2.get_data_dir(dirname(abspath(__file__))), '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.trait_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. gs = g.glyph.glyph_source gs.glyph_source = gs.glyph_dict['arrow_source']
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() s = e.new_scene() poly_data = BuiltinSurface() e.add_source(poly_data) outline = Outline() e.add_module(outline) surface = Surface() e.add_module(surface) poly_data.data_source.shaft_radius = 0.05 poly_data.data_source.shaft_resolution = 7 poly_data.data_source.tip_radius = 0.1 self.e = e self.scene = e.current_scene return
def setUp(self): e = NullEngine() e.start() e.new_scene() scene = e.scenes[-1] s = ParametricSurface() e.add_source(s) o = Outline() s.add_child(o) o1 = Outline() s.add_child(o1) self.scene = scene self.e = e self.s = s self.o = o self.o1 = o1 return
def view(): from mayavi import mlab from mayavi.sources.vtk_data_source import VTKDataSource from mayavi.modules.outline import Outline from mayavi.modules.surface import Surface from mayavi.modules.vectors import Vectors from mayavi.modules.api import IsoSurface scene = mayavi.new_scene() #scene.background = "black" # The single type one src = VTKDataSource(data=ug) mayavi.add_source(src) mayavi.add_module(Outline()) #mayavi.add_module(Surface()) #mayavi.add_module(Vectors()) #translucent isosurfaces iso = IsoSurface() mayavi.add_module(iso) iso.module_manager.scalar_lut_manager.lut_mode = "plasma" iso.contour.contours = np.linspace(0.0, 0.03, 30).tolist() iso.actor.property.opacity = 0.3 #iso = mayavi.mlab.pipeline.iso_surface(ug, contours=[1e-15,1e-14,1e-12], opacity=0.3) #from mayavi import mlab #mlab.contour3d(ug, opacity=0.3) #corned outline = engine.scenes[0].children[0].children[0].children[0] outline.outline_filter.reference_count = 2 outline.outline_filter.progress = 1.0 outline.actor.mapper.scalar_range = np.array([0., 1.]) outline.actor.mapper.progress = 1.0 outline.outline_mode = 'cornered' #show the xyz arrow axis scene.scene.show_axes = True scene.scene.background = (0.0, 0.0, 0.0) scene.scene.isometric_view() #v = mlab.view() (azimuth, elevation, distance, focalpoint) = mlab.view() elevation += 20.0 #move cam a little lower distance *= 0.6 for i, ang in enumerate(np.linspace(0.0, 360, 100)): si = str(i).rjust(4, '0') scene.scene.save('out/snapshot{}.png'.format(si)) mlab.view(azimuth=azimuth + ang, elevation=elevation, distance=distance, focalpoint=focalpoint) scene.scene.render()
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() s = e.new_scene() self.e = e self.s = s ############################################################ # Create a new scene and set up the visualization. d = ArraySource() sc, vec = self.make_data() d.origin = (-5, -5, -5) d.scalar_data = sc d.vector_data = vec e.add_source(d) # Create an outline for the data. o = Outline() e.add_module(o) # View the data. st = Streamline() e.add_module(st) widget = st.seed.widget widget.trait_set(radius=1.0, center=(-4.0, -4.0, -4.0), theta_resolution=4, phi_resolution=4) st = Streamline(streamline_type='ribbon') seed = st.seed seed.widget = seed.widget_list[1] e.add_module(st) seed.widget.trait_set(point1=(-5.0, -4.5, -4.0), point2=(-5.0, -4.5, 4.0)) st.ribbon_filter.width = 0.25 st = Streamline(streamline_type='tube') seed = st.seed seed.widget = seed.widget_list[2] e.add_module(st) seed.widget.trait_set(center=(-5.0, 1.5, -2.5)) st.tube_filter.radius = 0.15 st = Streamline(streamline_type='tube') seed = st.seed seed.widget = seed.widget_list[3] e.add_module(st) seed.widget.position = (-5.0, 3.75, 3.75) st.tube_filter.radius = 0.2 self.st = st self.scene = e.current_scene return
def test_pickle(self): "Test if pickling works." # Test if saving a visualization and restoring it works. d = self.data sc, vec = self.make_3d_data() d.scalar_data = sc d.vector_data = vec d.spacing = [1, 2, 3] d.origin = [4, 5, 6] d.start() # Start the object so it flushes the pipeline etc. # Create an outline for the data. o = Outline() d.add_child(o) o.start() # Create a surface module. surf = Surface() d.add_child(surf) data = pickle.dumps(d) del d, surf, o d = pickle.loads(data) # We must explicitly start the object. d.start() mm = d.children[0] o, surf = mm.children # Test the unpciked state. self.assertEqual(tuple(o.actor.actor.bounds), (4., 5., 5., 7., 6., 9.)) self.assertEqual(surf.running, True) self.assertEqual(o.running, True) self.assertEqual(d.running, True) self.assertEqual(numpy.allclose(d.spacing, [1, 2, 3]), True) self.assertEqual(numpy.allclose(d.origin, [4, 5, 6]), True) tps = numpy.transpose expect = [tps(sc), tps(vec, (2,1,0,3))] sc2 = surf.actor.mapper.input.point_data.scalars.to_array() self.assertEqual(numpy.allclose(sc2.flatten(), expect[0].flatten()), True) vec2 = surf.actor.mapper.input.point_data.vectors.to_array() self.assertEqual(numpy.allclose(vec2.flatten(), expect[1].flatten()), True)
def user_outline(): """A Factory function that creates a new module to add to the pipeline. Note that the method safely does any mayavi imports inside avoiding any circular imports. """ print("User Outline") from mayavi.modules.outline import Outline o = Outline(outline_mode='cornered', name='UserOutline') return o
def view_mlab(): engine = mayavi.mlab.get_engine() from mayavi.modules.axes import Axes axes = Axes() axes.name = 'Axes' axes.axes.fly_mode = 'none' axes.axes.number_of_labels = 8 axes.axes.font_factor = 0.5 #module_manager = self.__module_manager() # Add the label / marker: engine.add_filter(axes) from mayavi.modules.outline import Outline outline = Outline() outline.name = 'Outline' engine.add_filter(outline) mayavi.mlab.show()
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 test_pickle(self): "Test if pickling works." # Test if saving a visualization and restoring it works. d = self.data sc, vec = self.make_3d_data() d.scalar_data = sc d.vector_data = vec d.spacing = [1, 2, 3] d.origin = [4, 5, 6] d.start() # Start the object so it flushes the pipeline etc. # Create an outline for the data. o = Outline() d.add_child(o) o.start() # Create a surface module. surf = Surface() d.add_child(surf) data = pickle.dumps(d) del d, surf, o d = pickle.loads(data) # We must explicitly start the object. d.start() mm = d.children[0] o, surf = mm.children # Test the unpciked state. self.assertEqual(tuple(o.actor.actor.bounds), (4., 5., 5., 7., 6., 9.)) self.assertEqual(surf.running, True) self.assertEqual(o.running, True) self.assertEqual(d.running, True) self.assertEqual(numpy.allclose(d.spacing, [1, 2, 3]), True) self.assertEqual(numpy.allclose(d.origin, [4, 5, 6]), True) tps = numpy.transpose expect = [tps(sc), tps(vec, (2, 1, 0, 3))] sc2 = surf.actor.mapper.input.point_data.scalars.to_array() self.assertEqual(numpy.allclose(sc2.flatten(), expect[0].flatten()), True) vec2 = surf.actor.mapper.input.point_data.vectors.to_array() self.assertEqual(numpy.allclose(vec2.flatten(), expect[1].flatten()), True)
def view(): from mayavi.sources.vtk_data_source import VTKDataSource from mayavi.modules.outline import Outline from mayavi.modules.image_plane_widget import ImagePlaneWidget mayavi.new_scene() src = VTKDataSource(data=spoints) mayavi.add_source(src) mayavi.add_module(Outline()) mayavi.add_module(ImagePlaneWidget())
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() filename = join(mayavi2.get_data_dir(dirname(abspath(__file__))), 'heart.vtk') r.initialize(filename) 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
def view_data(): """Sets up the mayavi pipeline for the visualization. """ # 'mayavi' is always defined on the interpreter. o = Outline() mayavi.add_module(o) c = ContourGridPlane() mayavi.add_module(c) c.grid_plane.position = 16 c.module_manager.scalar_lut_manager.show_scalar_bar = True
def view(): from mayavi.sources.vtk_data_source import VTKDataSource from mayavi.modules.outline import Outline from mayavi.modules.surface import Surface from mayavi.modules.vectors import Vectors mayavi.new_scene() # The single type one src = VTKDataSource(data = ug1) mayavi.add_source(src) mayavi.add_module(Outline()) mayavi.add_module(Surface()) mayavi.add_module(Vectors()) # Mixed types. src = VTKDataSource(data = ug2) mayavi.add_source(src) mayavi.add_module(Outline()) mayavi.add_module(Surface()) mayavi.add_module(Vectors())
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() s = e.new_scene() self.e = e self.s = s ############################################################ # Create a new scene and set up the visualization. d = ArraySource() sc, vec = self.make_data() d.origin = (-5, -5, -5) d.scalar_data = sc d.vector_data = vec e.add_source(d) # Create an outline for the data. o = Outline() e.add_module(o) # Glyphs for the scalars g = Glyph() e.add_module(g) g.glyph.glyph_source.glyph_position = 'center' g.glyph.glyph.vector_mode = 'use_normal' g.glyph.glyph.scale_factor = 0.5 g.glyph.mask_points.on_ratio = 20 g.actor.property.line_width = 1.0 v = VectorCutPlane() glyph = v.glyph gs = glyph.glyph_source gs.glyph_position = 'tail' gs.glyph_source = gs.glyph_list[1] e.add_module(v) v.implicit_plane.set(normal=(0, 1, 0), origin=(0, 3, 0)) v = VectorCutPlane() glyph = v.glyph gs = glyph.glyph_source gs.glyph_source = gs.glyph_list[2] gs.glyph_position = 'head' e.add_module(v) v.implicit_plane.set(normal=(0, 1, 0), origin=(0, -2, 0)) self.g = g self.v = v self.scene = e.current_scene return
def test_remove(self): "Does obj.remove() work correctly" # Fails only when the current object is the one that is removed. self.e.current_object = self.o1 mm = self.o1.module_manager # Remove the object. self.o1.remove() # Now add another object. o1 = Outline() self.e.add_module(o1) # Is it really added? self.assertEqual(o1.module_manager, mm) self.assertEqual(o1.parent, mm)
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() e.new_scene() self.e = e sgrid = datasets.generateStructuredGrid() src = VTKDataSource(data=sgrid) e.add_source(src) # Create an outline for the data. o = Outline() e.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp1 = ContourGridPlane() e.add_module(cgp1) # Set the position to the middle of the data. cgp1.grid_plane.position = 15 # Another with filled contours normal to 'y' axis. cgp2 = ContourGridPlane() cgp2.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp2.grid_plane.axis = 'y' cgp2.grid_plane.position = 15 e.add_module(cgp2) # An isosurface module. iso = IsoSurface(compute_normals=True) e.add_module(iso) iso.contour.contours = [5] # An interactive scalar cut plane. cp = ScalarCutPlane() e.add_module(cp) ip = cp.implicit_plane ip.normal = 0, 0, 1 ip.origin = 0.5, 0.5, 1.0 # Since this is running offscreen this seems necessary. ip.widget.origin = 0.5, 0.5, 1.0 ip.widget.enabled = False self.scene = e.current_scene self.cgp2 = cgp2 self.iso = iso self.cp = cp return
def _view_data_fired(self): mayavi = self.get_mayavi() from mayavi.modules.outline import Outline from mayavi.modules.image_plane_widget import ImagePlaneWidget # 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 test_that_custom_views_are_loaded(self): # Given/When s = Surface() # Then self.assertTrue(os.path.exists(s._view_filename)) self.assertTrue(s._module_view is not None) self.assertTrue(isinstance(s._module_view, View)) # When there is no view, it should work safely. # Given/When o = Outline() # Then self.assertFalse(os.path.exists(o._view_filename)) self.assertEqual(o._module_view, None)
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 setUp(self): e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() e.new_scene() self.e=e # Read a VTK XML data file. r = VTKXMLFileReader() r.initialize(get_example_data('cube.vti')) e.add_source(r) # Create an outline for the data. o = Outline() e.add_module(o) # Create one ContourGridPlane normal to the 'x' axis. cgp1 = ContourGridPlane() e.add_module(cgp1) # Set the position to the middle of the data. cgp1.grid_plane.position = 1 # Another with filled contours normal to 'y' axis. cgp2 = ContourGridPlane() cgp2.contour.filled_contours = True # Set the axis and position to the middle of the data. cgp2.grid_plane.axis = 'y' cgp2.grid_plane.position = 1 e.add_module(cgp2) # An interactive scalar cut plane. cp = ScalarCutPlane() e.add_module(cp) ip = cp.implicit_plane ip.normal = 0,0,1 ip.origin = 0.5, 0.5, 1.0 # Since this is running offscreen this seems necessary. ip.widget.origin = 0.5, 0.5, 1.0 ip.widget.enabled = False self.scene = e.current_scene self.cgp2=cgp2 self.cp=cp return
def _annotateScene(self): script = self.script # Domain (axes and outline) script.add_source(VTKDataSource(data=vtk_geometry.domain())) script.engine.current_object.name = "Domain" outline = Outline() script.add_module(outline) outline.actor.property.opacity = 0.2 axes = Axes() axes.axes.x_label = "X" axes.axes.y_label = "Y" axes.axes.z_label = "Z" axes.axes.label_format = "%-0.1f" script.add_module(axes) return
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 setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" e = NullEngine() # Uncomment to see visualization for debugging etc. #e = Engine() e.start() e.new_scene() self.e = e sgrid = datasets.generateStructuredGrid() src = VTKDataSource(data=sgrid) e.add_source(src) # Create an outline for the data. o = Outline() e.add_module(o) # Create three simple grid plane modules. # First normal to 'x' axis. gp1 = GridPlane() e.add_module(gp1) # Second normal to 'y' axis. gp2 = GridPlane() # We'll test how robust things are by setting attributes gp2.grid_plane.axis = 'y' gp2.grid_plane.position = 16 e.add_module(gp2) # Third normal to 'z' axis. gp3 = GridPlane() e.add_module(gp3) gp3.grid_plane.axis = 'z' gp3.grid_plane.position = 6 for gp in (gp1, gp2, gp3): gp.actor.property.trait_set(ambient=1.0) self.scene = e.current_scene return
def generate_custom_velocity_grid( vlsvReader, blocks_and_values, iso_surface=False ): '''Generates a velocity grid from a given spatial cell id :param vlsvReader: Some vlsv reader with a file open :param velocity_cell_map: Given velocity cell ids and values in python dict() format (see read_velocity_cells function in vlsvReader) :param iso_surface: If true, plots the iso surface # Example usage: import pytools as pt #vlsvReader = pt.vlsvfile.VlsvReader("example.vlsv") cellid = 1111 velocity_cell_map = vlsvReader.read_velocity_cells(cellid) velocity_cell_ids = velocity_cell_map.keys() velocity_cell_values = velocity_cell_map.values() velocity_cell_map[velocity_cell_ids[10]] = 3e-7 generate_custom_velocity_grid( vlsvReader, velocity_cell_map ) ''' # Create nodes # Get velocity blocks and avgs: # Get helper function: blocksAndAvgs = blocks_and_values if len(blocksAndAvgs) == 0: print "CELL " + str(cellid) + " HAS NO VELOCITY BLOCK" return False # Create a new scene #engine.new_scene() #mayavi.mlab.set_engine(engine) # Create a new figure #figure = mayavi.mlab.clf() #figure.scene.disable_render = True blocks = blocksAndAvgs[0] avgs = blocksAndAvgs[1] # Get nodes: nodesAndKeys = vlsvReader.construct_velocity_cell_nodes(blocks) # Create an unstructured grid: points = nodesAndKeys[0] tets = nodesAndKeys[1] tet_type=tvtk.Voxel().cell_type#VTK_VOXEL ug=tvtk.UnstructuredGrid(points=points) # Set up the cells ug.set_cells(tet_type,tets) # Input data values=np.ravel(avgs) ug.cell_data.scalars=values ug.cell_data.scalars.name='avgs' #figure.scene.disable_render = False d = mayavi.mlab.pipeline.add_dataset(ug) if iso_surface == False: iso = mayavi.mlab.pipeline.surface(d) else: ptdata = mayavi.mlab.pipeline.cell_to_point_data(d) iso = mayavi.mlab.pipeline.iso_surface(ptdata, contours=[1e-15,1e-14,1e-12], opacity=0.3) engine = mayavi.mlab.get_engine() from mayavi.modules.axes import Axes axes = Axes() axes.name = 'Axes' axes.axes.fly_mode = 'none' axes.axes.number_of_labels = 8 axes.axes.font_factor = 0.5 #module_manager = self.__module_manager() # Add the label / marker: engine.add_filter( axes ) from mayavi.modules.outline import Outline outline = Outline() outline.name = 'Outline' engine.add_filter( outline ) mayavi.mlab.show()
iso_surface2.actor.property.specular_color = (1.0, 1.0, 1.0) iso_surface2.actor.property.diffuse_color = (1.0, 1.0, 1.0) iso_surface2.actor.property.ambient_color = (1.0, 1.0, 1.0) iso_surface2.actor.property.color = (1.0, 1.0, 1.0) iso_surface2.actor.property.opacity = 0.3 iso_surface2.contour.contours[0:1] = [0.01] scene = engine.scenes[0] #from mayavi.modules.axes import Axes #axes = Axes() #engine.add_module(axes, obj=None) from mayavi.modules.outline import Outline outline1 = Outline() engine.add_module(outline1, obj=None) outline1.actor.mapper.scalar_range = array([ 0., 1.]) outline1.outline_mode = 'full' outline1.actor.property.specular_color = (0.0, 0.0, 0.0) outline1.actor.property.diffuse_color = (0.0, 0.0, 0.0) outline1.actor.property.ambient_color = (0.0, 0.0, 0.0) outline1.actor.property.color = (0.0, 0.0, 0.0) outline1.actor.property.line_width = 4. outline1.actor.property.line_width = 4. #scene.scene.background = (0.7529411764705882, 0.7529411764705882, 0.7529411764705882) scene.scene.background = (1.0, 1.0, 1.0) scene.scene.jpeg_quality = 100
def __generate_velocity_grid(self, cellid, iso_surface=False): """Generates a velocity grid from a given spatial cell id :param cellid: The spatial cell's ID :param iso_surface: If true, plots the iso surface """ # Create nodes # Get velocity blocks and avgs: blocksAndAvgs = self.vlsvReader.read_blocks(cellid) if len(blocksAndAvgs) == 0: print "CELL " + str(cellid) + " HAS NO VELOCITY BLOCK" return False # Create a new scene self.__engine.new_scene() mayavi.mlab.set_engine(self.__engine) # CONTINUE # Create a new figure figure = mayavi.mlab.gcf(engine=self.__engine) figure.scene.disable_render = True blocks = blocksAndAvgs[0] avgs = blocksAndAvgs[1] # Get nodes: nodesAndKeys = self.vlsvReader.construct_velocity_cell_nodes(blocks) # Create an unstructured grid: points = nodesAndKeys[0] tets = nodesAndKeys[1] tet_type = tvtk.Voxel().cell_type # VTK_VOXEL ug = tvtk.UnstructuredGrid(points=points) # Set up the cells ug.set_cells(tet_type, tets) # Input data values = np.ravel(avgs) ug.cell_data.scalars = values ug.cell_data.scalars.name = "avgs" # Plot B if possible: # Read B vector and plot it: if self.vlsvReader.check_variable("B") == True: B = self.vlsvReader.read_variable(name="B", cellids=cellid) elif self.vlsvReader.check_variable("B_vol") == True: B = self.vlsvReader.read_variable(name="B_vol", cellids=cellid) else: B = self.vlsvReader.read_variable(name="background_B", cellids=cellid) + self.vlsvReader.read_variable( name="perturbed_B", cellids=cellid ) points2 = np.array([[0, 0, 0]]) ug2 = tvtk.UnstructuredGrid(points=points2) ug2.point_data.vectors = [B / np.linalg.norm(B)] ug2.point_data.vectors.name = "B_vector" # src2 = VTKDataSource(data = ug2) d2 = mayavi.mlab.pipeline.add_dataset(ug2) # mayavi.mlab.add_module(Vectors()) vec = mayavi.mlab.pipeline.vectors(d2) vec.glyph.mask_input_points = True vec.glyph.glyph.scale_factor = 1e6 vec.glyph.glyph_source.glyph_source.center = [0, 0, 0] # Visualize d = mayavi.mlab.pipeline.add_dataset(ug) if iso_surface == False: iso = mayavi.mlab.pipeline.surface(d) else: ptdata = mayavi.mlab.pipeline.cell_to_point_data(d) iso = mayavi.mlab.pipeline.iso_surface(ptdata, contours=[1e-15, 1e-14, 1e-12], opacity=0.3) figure.scene.disable_render = False self.__unstructured_figures.append(figure) # Name the figure figure.name = ( str(cellid) + ", " + self.variable_plotted + " = " + str(self.vlsvReader.read_variable(self.variable_plotted, cellids=cellid)) ) from mayavi.modules.axes import Axes axes = Axes() axes.name = "Axes" axes.axes.fly_mode = "none" axes.axes.number_of_labels = 8 axes.axes.font_factor = 0.5 # module_manager = self.__module_manager() # Add the label / marker: self.__engine.add_filter(axes) from mayavi.modules.outline import Outline outline = Outline() outline.name = "Outline" self.__engine.add_filter(outline) return True