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 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 reset(self, **traits): """Creates the dataset afresh or resets existing data source.""" # First set the attributes without really doing anything since # the notification handlers are not called. self.set(trait_change_notify=False, **traits) vectors = self.vectors scalars = self.scalars x, y, z = [np.atleast_3d(a) for a in self.x, self.y, self.z] u, v, w = self.u, self.v, self.w if 'vectors' in traits: u = vectors[:, 0].ravel() v = vectors[:, 1].ravel() w = vectors[:, 2].ravel() self.set(u=u, v=v, w=w, trait_change_notify=False) else: if u is not None and len(u) > 0: #vectors = np.concatenate([u[..., np.newaxis], # v[..., np.newaxis], # w[..., np.newaxis] ], # axis=3) vectors = np.c_[u.ravel(), v.ravel(), w.ravel()].ravel() vectors.shape = (u.shape[0], u.shape[1], w.shape[2], 3) self.set(vectors=vectors, trait_change_notify=False) if vectors is not None and len(vectors) > 0 and scalars is not None: assert len(scalars) == len(vectors) if x.shape[0] <= 1: dx = 1 else: dx = x[1, 0, 0] - x[0, 0, 0] if y.shape[1] <= 1: dy = 1 else: dy = y[0, 1, 0] - y[0, 0, 0] if z.shape[2] <= 1: dz = 1 else: dz = z[0, 0, 1] - z[0, 0, 0] if self.m_data is None: ds = ArraySource(transpose_input_array=True) else: ds = self.m_data old_scalar = ds.scalar_data ds.set(vector_data=vectors, origin=[x.min(), y.min(), z.min()], spacing=[dx, dy, dz], scalar_data=scalars) if scalars is old_scalar: ds._scalar_data_changed(scalars) self.dataset = ds.image_data self.m_data = ds
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 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.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.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.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 reset(self, **traits): """Creates the dataset afresh or resets existing data source.""" # First set the attributes without really doing anything since # the notification handlers are not called. self.set(trait_change_notify=False, **traits) x, y, mask = self.x, self.y, self.mask scalars = self.scalars # We may have used this without specifying x and y at all in # which case we set them from the shape of scalars. nx, ny = scalars.shape #Build X and Y from shape of Scalars if they are none if x is None and y is None: x, y = np.mgrid[-nx / 2.:nx / 2, -ny / 2.:ny / 2] if mask is not None and len(mask) > 0: scalars[mask.astype('bool')] = np.nan # The NaN trick only works with floats. scalars = scalars.astype('float') self.set(scalars=scalars, trait_change_notify=False) z = np.array([0]) self.set(x=x, y=y, z=z, trait_change_notify=False) # Do some magic to extract the first row/column, independently of # the shape of x and y x = np.atleast_2d(x.squeeze().T)[0, :].squeeze() y = np.atleast_2d(y.squeeze())[0, :].squeeze() if x.ndim == 0: dx = 1 else: dx = x[1] - x[0] if y.ndim == 0: dy = 1 else: dy = y[1] - y[0] if self.m_data is None: ds = ArraySource(transpose_input_array=True) else: ds = self.m_data old_scalar = ds.scalar_data ds.set(origin=[x.min(), y.min(), 0], spacing=[dx, dy, 1], scalar_data=scalars) if old_scalar is scalars: ds._scalar_data_changed(scalars) self.dataset = ds.image_data self.m_data = ds
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 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 _create_data_fired(self): mayavi = self.get_mayavi() from mayavi.sources.array_source import ArraySource s = self._make_data() src = ArraySource(transpose_input_array=False, scalar_data=s) self.source = src mayavi.add_source(src)
def test_threshold_with_other_filter_as_input(self): # Given x, y, z = np.mgrid[-1:1:10j, -1:1:10j, -1:1:10j] s = x * x + y * y + z * z src = ArraySource(scalar_data=s) self.e.add_source(src) scp = CutPlane() self.e.add_filter(scp) # When threshold = Threshold() self.e.add_filter(threshold) threshold.set(lower_threshold=0.25, upper_threshold=0.75, auto_reset_lower=False, auto_reset_upper=False) # Then output = threshold.get_output_dataset() self.assertTrue(output is not None) self.assertTrue(output.is_a('vtkUnstructuredGrid')) output_range = output.point_data.scalars.range self.assertTrue(output_range[0] >= 0.25) self.assertTrue(output_range[1] <= 0.75)
def _mk_image_data(self): """ Creates an ImageData VTK data set and the associated ArraySource using the factory's attributes. """ self._mayavi_source = ArraySource(transpose_input_array=True, scalar_data=self.scalar_data, origin=[0., 0., 0], spacing=[1, 1, 1]) self._vtk_source = self._mayavi_source.image_data
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 view_numpy( data ): """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. 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 reset(self, **traits): """Creates the dataset afresh or resets existing data source.""" # First set the attributes without really doing anything since # the notification handlers are not called. self.trait_set(trait_change_notify=False, **traits) vectors = self.vectors scalars = self.scalars x, y, z = [np.atleast_3d(a) for a in (self.x, self.y, self.z)] u, v, w = self.u, self.v, self.w if 'vectors' in traits: u = vectors[:, 0].ravel() v = vectors[:, 1].ravel() w = vectors[:, 2].ravel() self.trait_set(u=u, v=v, w=w, trait_change_notify=False) else: if u is not None and len(u) > 0: #vectors = np.concatenate([u[..., np.newaxis], # v[..., np.newaxis], # w[..., np.newaxis] ], # axis=3) vectors = np.c_[u.ravel(), v.ravel(), w.ravel()].ravel() vectors.shape = (u.shape[0], u.shape[1], w.shape[2], 3) self.trait_set(vectors=vectors, trait_change_notify=False) if vectors is not None and len(vectors) > 0 and scalars is not None: assert len(scalars) == len(vectors) if x.shape[0] <= 1: dx = 1 else: dx = x[1, 0, 0] - x[0, 0, 0] if y.shape[1] <= 1: dy = 1 else: dy = y[0, 1, 0] - y[0, 0, 0] if z.shape[2] <= 1: dz = 1 else: dz = z[0, 0, 1] - z[0, 0, 0] if self.m_data is None: ds = ArraySource(transpose_input_array=True) else: ds = self.m_data old_scalar = ds.scalar_data ds.trait_set(vector_data=vectors, origin=[x.min(), y.min(), z.min()], spacing=[dx, dy, dz], scalar_data=scalars) if scalars is old_scalar: ds._scalar_data_changed(scalars) self.dataset = ds.image_data self.m_data = ds
def reset(self, **traits): """Creates the dataset afresh or resets existing data source.""" # First set the attributes without really doing anything since # the notification handlers are not called. self.trait_set(trait_change_notify=False, **traits) x, y, mask = self.x, self.y, self.mask scalars = self.scalars # We may have used this without specifying x and y at all in # which case we set them from the shape of scalars. nx, ny = scalars.shape #Build X and Y from shape of Scalars if they are none if x is None and y is None: x, y = np.mgrid[-nx / 2.:nx / 2, -ny / 2.:ny / 2] if mask is not None and len(mask) > 0: scalars[mask.astype('bool')] = np.nan # The NaN trick only works with floats. scalars = scalars.astype('float') self.trait_set(scalars=scalars, trait_change_notify=False) z = np.array([0]) self.trait_set(x=x, y=y, z=z, trait_change_notify=False) # Do some magic to extract the first row/column, independently of # the shape of x and y x = np.atleast_2d(x.squeeze().T)[0, :].squeeze() y = np.atleast_2d(y.squeeze())[0, :].squeeze() if x.ndim == 0: dx = 1 else: dx = x[1] - x[0] if y.ndim == 0: dy = 1 else: dy = y[1] - y[0] if self.m_data is None: ds = ArraySource(transpose_input_array=True) else: ds = self.m_data old_scalar = ds.scalar_data ds.trait_set(origin=[x.min(), y.min(), 0], spacing=[dx, dy, 1], scalar_data=scalars) if old_scalar is scalars: ds._scalar_data_changed(scalars) self.dataset = ds.get_output_dataset() self.m_data = ds
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 = self.make_data() d.scalar_data = sc e.add_source(d) self.t = Text3D() e.add_module(self.t) self.scene = e.current_scene return
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 = self.make_data() d.scalar_data = sc e.add_source(d) # Create an outline for the data. o = Outline() e.add_module(o) # ImagePlaneWidgets for the scalars ipw = ImagePlaneWidget() e.add_module(ipw) ipw_y = ImagePlaneWidget() e.add_module(ipw_y) ipw_y.ipw.plane_orientation = 'y_axes' ipw_z = ImagePlaneWidget() e.add_module(ipw_z) ipw_z.ipw.plane_orientation = 'z_axes' self.scene = e.current_scene return
def _show_data(self): if self.source is not None: return mayavi = self.get_mayavi() if mayavi.engine.current_scene is None: mayavi.new_scene() from mayavi.sources.array_source import ArraySource vol = self.volume origin = vol[::2] spacing = (vol[1::2] - origin)/(self.dimensions -1) src = ArraySource(transpose_input_array=False, scalar_data=self.data, origin=origin, spacing=spacing) self.source = src mayavi.add_source(src) from mayavi.modules.outline import Outline from mayavi.modules.image_plane_widget import ImagePlaneWidget from mayavi.modules.axes import Axes # Visualize the data. o = Outline() mayavi.add_module(o) a = Axes() mayavi.add_module(a) self._ipw1 = ipw = ImagePlaneWidget() mayavi.add_module(ipw) ipw.module_manager.scalar_lut_manager.show_scalar_bar = True self._ipw2 = ipw_y = ImagePlaneWidget() mayavi.add_module(ipw_y) ipw_y.ipw.plane_orientation = 'y_axes' self._ipw3 = ipw_z = ImagePlaneWidget() mayavi.add_module(ipw_z) ipw_z.ipw.plane_orientation = 'z_axes'
def visualize(tetrahedrons, save=False, file_name='tetrahedron_mesh.vtu', just_surface=True, octmps_output_file='ClassI_ScattFilt.out'): if just_surface: data = get_surface_structure(tetrahedrons) else: data = get_complete_structure(tetrahedrons) # Saving as VTU file if save: write_data(data, file_name=file_name) mlab.figure(figure='OCTMPS', fgcolor=(1, 1, 1), bgcolor=(0.5, 0.5, 0.5)) src = VTKDataSource(data=data) surf = mlab.pipeline.surface(src, opacity=0.01) mlab.axes() mlab.pipeline.surface(mlab.pipeline.extract_edges(surf), color=(1, 1, 1), line_width=0.0) if octmps_output_file: # Make the data and add it to the pipeline. data, x_positions, z_positions = make_data( octmps_output_file=octmps_output_file) data = np.array([data]).swapaxes(1, 0) src = ArraySource(transpose_input_array=True) src.scalar_data = data src.spacing = (x_positions[1] - x_positions[0], 0., z_positions[1] - z_positions[0]) src.origin = (x_positions[0], 0.0, z_positions[0]) mlab.pipeline.surface(src, colormap='jet') mlab.colorbar(orientation='vertical', title='Reflectance') mlab.show()
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.array_source import ArraySource from mayavi.modules.outline import Outline from mayavi.modules.glyph import Glyph from mayavi.modules.vector_cut_plane import VectorCutPlane ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() d = ArraySource() sc, vec = self.make_data() d.origin = (-5, -5, -5) d.scalar_data = sc d.vector_data = vec script.add_source(d) # Create an outline for the data. o = Outline() script.add_module(o) # Glyphs for the scalars g = Glyph() script.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.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] script.add_module(v) v.implicit_plane.trait_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' script.add_module(v) v.implicit_plane.trait_set(normal=(0, 1, 0), origin=(0, -2, 0)) # Set the scene to a suitable view. self.set_view(s) self.check() ############################################################ # Test if the modules respond correctly when the components # are changed. g.actor = g.actor.__class__() glyph = g.glyph g.glyph = glyph.__class__() g.glyph = glyph glyph = v.glyph v.glyph = glyph.__class__() v.glyph = glyph v.actor = v.actor.__class__() v.cutter = v.cutter.__class__() ip = v.implicit_plane v.implicit_plane = ip.__class__() v.implicit_plane = ip s.render() self.check() ############################################################ # Test if saving a visualization and restoring it works. # Save visualization. f = BytesIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene # Set the scene to a suitable view. self.set_view(s) self.check() ############################################################ # Test if the MayaVi2 visualization can be deepcopied. # Pop the source object. sources = s.children s.children = [] # Add it back to see if that works without error. s.children.extend(sources) self.set_view(s) self.check() # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. sources1 = copy.deepcopy(sources) s.children[:] = sources self.set_view(s) self.check()
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.array_source import ArraySource from mayavi.modules.outline import Outline from mayavi.modules.image_plane_widget import ImagePlaneWidget ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() d = ArraySource() sc = self.make_data() d.scalar_data = sc script.add_source(d) # Create an outline for the data. o = Outline() script.add_module(o) # ImagePlaneWidgets for the scalars ipw = ImagePlaneWidget() script.add_module(ipw) ipw_y = ImagePlaneWidget() script.add_module(ipw_y) ipw_y.ipw.plane_orientation = 'y_axes' ipw_z = ImagePlaneWidget() script.add_module(ipw_z) ipw_z.ipw.plane_orientation = 'z_axes' # Set the scene to a suitable view. self.set_view(s) self.check() ############################################################ # Test if saving a visualization and restoring it works. # Save visualization. f = BytesIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene # Set the scene to a suitable view. self.set_view(s) self.check() ############################################################ # Test if the MayaVi2 visualization can be deepcopied. # Pop the source object. sources = s.children s.children = [] # Add it back to see if that works without error. s.children.extend(sources) self.set_view(s) self.check() # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. sources1 = copy.deepcopy(sources) s.children[:] = sources self.set_view(s) self.check()
def setUp(self): """Initial setting up of test fixture, automatically called by TestCase before any other test method is invoked""" d = ArraySource() self.data = d
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.array_source import ArraySource from mayavi.modules.outline import Outline from mayavi.modules.streamline import Streamline ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() d = ArraySource() sc, vec = self.make_data() d.origin = (-5, -5, -5) d.scalar_data = sc d.vector_data = vec script.add_source(d) # Create an outline for the data. o = Outline() script.add_module(o) # View the data. st = Streamline() script.add_module(st) widget = st.seed.widget widget.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] script.add_module(st) seed.widget.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] script.add_module(st) seed.widget.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] script.add_module(st) seed.widget.position = (-5.0, 3.75, 3.75) st.tube_filter.radius = 0.2 # Set the scene to a suitable view. s.scene.z_plus_view() c = s.scene.camera c.azimuth(-30) c.elevation(30) s.render() # Update the streamlines. mm = o.module_manager for child in mm.children[1:]: child.update_streamlines = True # Now compare the image. self.compare_image(s, 'images/test_streamline.png') ############################################################ # Test if the modules respond correctly when the components # are changed. tf = st.tube_filter st.tube_filter = tf.__class__() st.tube_filter = tf st.ribbon_filter = st.ribbon_filter.__class__() seed = st.seed st.seed = seed.__class__() st.seed = seed st.actor = st.actor.__class__() tracer = st.stream_tracer st.stream_tracer = tracer.__class__() st.stream_tracer = tracer s.render() # Now compare the image. self.compare_image(s, 'images/test_streamline.png') s.render() ############################################################ # Test if saving a visualization and restoring it works. bg = s.scene.background # Save visualization. f = BytesIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene # Set the scene to a suitable view. s.scene.z_plus_view() c = s.scene.camera c.azimuth(-30) c.elevation(30) s.render() s.scene.background = bg # Now compare the image. self.compare_image(s, 'images/test_streamline.png') ############################################################ # Test if the MayaVi2 visualization can be deepcopied. # Pop the source object. sources = s.children s.children = [] # Add it back to see if that works without error. s.children.extend(sources) s.scene.reset_zoom() # Now compare the image. self.compare_image(s, 'images/test_streamline.png') # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. sources1 = copy.deepcopy(sources) s.children[:] = sources s.scene.reset_zoom() self.compare_image(s, 'images/test_streamline.png')
def setUp(self): d = ArraySource() self.data = d
def setUp(self): s1 = numpy.ones((2, 2)) src = ArraySource(scalar_data=s1, scalar_name='s1') self.src = src
def make_src(self, nan=False): data = np.empty((3, 3, 3)) if nan: data[0] = np.nan data.flat[:] = np.arange(data.size) return ArraySource(scalar_data=data)
def view_data(data): """Example showing how to view a 3D numpy array in mayavi2. """ def set_labelE(ind): return '{0:.0f} eV'.format(source.energies[ind]) def move_view(obj, evt): labelE.text = set_labelE(ipwX.ipw.slice_index) pos = labelE.position labelE.position = ipwX.ipw.slice_index * src.spacing[0] + 1,\ pos[1], pos[2] labelE.vector_text.update() def set_lut(ipw): lutM = ipw.module_manager.scalar_lut_manager # lutM.show_scalar_bar = True # lutM.number_of_labels = 9 lutM.lut.scale = 'log10' lutM.lut.range = [dataMax * vmin, dataMax] lutM.lut_mode = 'hot' @animate() def anim(data, ipwX): scene.scene.off_screen_rendering = True scene.scene.anti_aliasing_frames = 0 for i in range(0, data.shape[0], 1): ipwX.ipw.slice_index = i move_view(None, None) if saveName is not None: scene.scene.save('{0}{1:04d}.png'.format(saveName, i)) yield # 'mayavi' is always defined on the interpreter. scene = mayavi.new_scene() # analysis:ignore scene.scene.background = (0, 0, 0) print(source.prefix_save_name()) src = ArraySource(transpose_input_array=True) sh = data.shape # print(sh) if 'xrt' in source.prefix_save_name() or\ 'srw' in source.prefix_save_name(): src.scalar_data = data[:, :sh[1] // 2 + 1, :sh[2] // 2 + 1].copy() else: src.scalar_data = data[:, ::-1, ::-1].copy() # src.spacing = np.array([-0.05, 1, 1]) # src.spacing = np.array([-0.25, 1, 1]) src.spacing = np.array([-0.25, 0.25, 0.25]) mayavi.add_source(src) # analysis:ignore # Visualize the data. # o = Outline() # mayavi.add_module(o) ipwY = ImagePlaneWidget() mayavi.add_module(ipwY) # analysis:ignore ipwY.ipw.plane_orientation = 'y_axes' # our x-axis ipwY.ipw.slice_index = int(data.shape[1] - 1) # if 'xrt' in source.prefix_save_name(): # ipwY.ipw.slice_index /= int(2) ipwY.ipw.left_button_action = 0 set_lut(ipwY) if isZplane: ipwZ = ImagePlaneWidget() mayavi.add_module(ipwZ) # analysis:ignore ipwZ.ipw.plane_orientation = 'z_axes' # our z-axis ipwZ.ipw.slice_index = int(data.shape[2] - 1) # if 'xrt' in source.prefix_save_name(): # ipwZ.ipw.slice_index /= int(2) ipwZ.ipw.left_button_action = 0 if 'xrt' in source.prefix_save_name() or\ 'srw' in source.prefix_save_name(): pass else: data = np.concatenate((data[:, :0:-1, :], data), axis=1) data = np.concatenate((data[:, :, :0:-1], data), axis=2) sh = data.shape print(sh) src = ArraySource(transpose_input_array=True) src.scalar_data = data.copy() # src.spacing = np.array([-0.05, 1, 1]) # src.spacing = np.array([-0.25, 1, 1]) src.spacing = np.array([-0.25, 0.25, 0.25]) mayavi.add_source(src) # analysis:ignore ipwX = ImagePlaneWidget() mayavi.add_module(ipwX) # analysis:ignore ipwX.ipw.plane_orientation = 'x_axes' # energy set_lut(ipwX) ipwX.ipw.add_observer('WindowLevelEvent', move_view) ipwX.ipw.add_observer('StartInteractionEvent', move_view) ipwX.ipw.add_observer('EndInteractionEvent', move_view) labelE = Text3D() mayavi.add_module(labelE) # analysis:ignore labelE.position = (1, data.shape[1] * 0.73 * src.spacing[1], data.shape[2] * 0.85 * src.spacing[2]) labelE.orientation = 90, 0, 90 labelE.text = 'Energy' labelE.scale = 3, 3, 1 labelE.actor.property.color = 0, 1, 1 labelE.orient_to_camera = False labelE.text = set_labelE(0) view(45, 70, 200) wantToAnimate = True if wantToAnimate: anim(data, ipwX) else: ipwX.ipw.slice_index = data.shape[0] - 1 move_view(None, None)
def view_data(data): """Example showing how to view a 3D numpy array in mayavi2. """ def set_labelE(ind): return '{0:.0f} eV'.format(source.energies[ind]) def move_view(obj, evt): labelE.text = set_labelE(ipwX.ipw.slice_index) pos = labelE.position labelE.position = ipwX.ipw.slice_index * src.spacing[0] + 1,\ pos[1], pos[2] labelE.vector_text.update() def set_lut(ipw): lutM = ipw.module_manager.scalar_lut_manager # lutM.show_scalar_bar = True # lutM.number_of_labels = 9 lutM.lut.scale = 'log10' lutM.lut.range = [dataMax*vmin, dataMax] lutM.lut_mode = 'hot' @animate() def anim(data, ipwX): scene.scene.off_screen_rendering = True scene.scene.anti_aliasing_frames = 0 for i in range(0, data.shape[0], 1): ipwX.ipw.slice_index = i move_view(None, None) if saveName is not None: scene.scene.save('{0}{1:04d}.png'.format(saveName, i)) yield # 'mayavi' is always defined on the interpreter. scene = mayavi.new_scene() # analysis:ignore scene.scene.background = (0, 0, 0) print(source.prefix_save_name()) src = ArraySource(transpose_input_array=True) sh = data.shape # print(sh) if 'xrt' in source.prefix_save_name() or\ 'srw' in source.prefix_save_name(): src.scalar_data = data[:, :sh[1]//2+1, :sh[2]//2+1].copy() else: src.scalar_data = data[:, ::-1, ::-1].copy() # src.spacing = np.array([-0.05, 1, 1]) # src.spacing = np.array([-0.25, 1, 1]) src.spacing = np.array([-0.25, 0.25, 0.25]) mayavi.add_source(src) # analysis:ignore # Visualize the data. # o = Outline() # mayavi.add_module(o) ipwY = ImagePlaneWidget() mayavi.add_module(ipwY) # analysis:ignore ipwY.ipw.plane_orientation = 'y_axes' # our x-axis ipwY.ipw.slice_index = int(data.shape[1] - 1) # if 'xrt' in source.prefix_save_name(): # ipwY.ipw.slice_index /= int(2) ipwY.ipw.left_button_action = 0 set_lut(ipwY) if isZplane: ipwZ = ImagePlaneWidget() mayavi.add_module(ipwZ) # analysis:ignore ipwZ.ipw.plane_orientation = 'z_axes' # our z-axis ipwZ.ipw.slice_index = int(data.shape[2] - 1) # if 'xrt' in source.prefix_save_name(): # ipwZ.ipw.slice_index /= int(2) ipwZ.ipw.left_button_action = 0 if 'xrt' in source.prefix_save_name() or\ 'srw' in source.prefix_save_name(): pass else: data = np.concatenate((data[:, :0:-1, :], data), axis=1) data = np.concatenate((data[:, :, :0:-1], data), axis=2) sh = data.shape print(sh) src = ArraySource(transpose_input_array=True) src.scalar_data = data.copy() # src.spacing = np.array([-0.05, 1, 1]) # src.spacing = np.array([-0.25, 1, 1]) src.spacing = np.array([-0.25, 0.25, 0.25]) mayavi.add_source(src) # analysis:ignore ipwX = ImagePlaneWidget() mayavi.add_module(ipwX) # analysis:ignore ipwX.ipw.plane_orientation = 'x_axes' # energy set_lut(ipwX) ipwX.ipw.add_observer('WindowLevelEvent', move_view) ipwX.ipw.add_observer('StartInteractionEvent', move_view) ipwX.ipw.add_observer('EndInteractionEvent', move_view) labelE = Text3D() mayavi.add_module(labelE) # analysis:ignore labelE.position = (1, data.shape[1]*0.73*src.spacing[1], data.shape[2]*0.85*src.spacing[2]) labelE.orientation = 90, 0, 90 labelE.text = 'Energy' labelE.scale = 3, 3, 1 labelE.actor.property.color = 0, 1, 1 labelE.orient_to_camera = False labelE.text = set_labelE(0) view(45, 70, 200) wantToAnimate = True if wantToAnimate: anim(data, ipwX) else: ipwX.ipw.slice_index = data.shape[0]-1 move_view(None, None)
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.array_source import ArraySource from mayavi.modules.outline import Outline from mayavi.modules.surface import Surface from mayavi.modules.vectors import Vectors ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() d = ArraySource() self.check_input_validation(d) sc, vec = self.make_2d_data() d.origin = (-1, -1, 0) d.scalar_data = sc d.vector_data = vec script.add_source(d) # Create an outline for the data. o = Outline() script.add_module(o) # View the data. s = Surface() script.add_module(s) v = Vectors() script.add_module(v) # Add a 3D data source d = ArraySource() sc, vec = self.make_3d_data() d.scalar_data = sc d.vector_data = vec script.add_source(d) # Create an outline for the data. o = Outline() script.add_module(o) # View a slice. s = Surface() script.add_module(s) v = Vectors() script.add_module(v) # Set the scene to a suitable view. s.scene.z_plus_view() c = s.scene.camera c.azimuth(-30) c.elevation(30) self.check() ############################################################ # Test if saving a visualization and restoring it works. bg = s.scene.background # Save visualization. f = StringIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene # Set the scene to a suitable view. s.scene.z_plus_view() c = s.scene.camera c.azimuth(-30) c.elevation(30) s.scene.background = bg self.check() ############################################################ # Test if the MayaVi2 visualization can be deepcopied. # Pop the source object. sources = s.children s.children = [] # Add it back to see if that works without error. s.children.extend(sources) s.scene.reset_zoom() self.check() # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. sources1 = copy.deepcopy(sources) s.children[:] = sources s.scene.reset_zoom() self.check()
def do(self): ############################################################ # Imports. script = self.script from mayavi.sources.array_source import ArraySource from mayavi.modules.outline import Outline from mayavi.modules.streamline import Streamline ############################################################ # Create a new scene and set up the visualization. s = self.new_scene() d = ArraySource() sc, vec = self.make_data() d.origin = (-5, -5, -5) d.scalar_data = sc d.vector_data = vec script.add_source(d) # Create an outline for the data. o = Outline() script.add_module(o) # View the data. st = Streamline() script.add_module(st) widget = st.seed.widget widget.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] script.add_module(st) seed.widget.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] script.add_module(st) seed.widget.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] script.add_module(st) seed.widget.position=(-5.0, 3.75, 3.75) st.tube_filter.radius = 0.2 # Set the scene to a suitable view. s.scene.z_plus_view() c = s.scene.camera c.azimuth(-30) c.elevation(30) s.render() # Update the streamlines. mm = o.module_manager for child in mm.children[1:]: child.update_streamlines = True # Now compare the image. self.compare_image(s, 'images/test_streamline.png') ############################################################ # Test if the modules respond correctly when the components # are changed. tf = st.tube_filter st.tube_filter = tf.__class__() st.tube_filter = tf st.ribbon_filter = st.ribbon_filter.__class__() seed = st.seed st.seed = seed.__class__() st.seed = seed st.actor = st.actor.__class__() tracer = st.stream_tracer st.stream_tracer = tracer.__class__() st.stream_tracer = tracer s.render() # Now compare the image. self.compare_image(s, 'images/test_streamline.png') s.render() ############################################################ # Test if saving a visualization and restoring it works. bg = s.scene.background # Save visualization. f = BytesIO() f.name = abspath('test.mv2') # We simulate a file. script.save_visualization(f) f.seek(0) # So we can read this saved data. # Remove existing scene. engine = script.engine engine.close_scene(s) # Load visualization script.load_visualization(f) s = engine.current_scene # Set the scene to a suitable view. s.scene.z_plus_view() c = s.scene.camera c.azimuth(-30) c.elevation(30) s.render() s.scene.background = bg # Now compare the image. self.compare_image(s, 'images/test_streamline.png') ############################################################ # Test if the MayaVi2 visualization can be deepcopied. # Pop the source object. sources = s.children s.children = [] # Add it back to see if that works without error. s.children.extend(sources) s.scene.reset_zoom() # Now compare the image. self.compare_image(s, 'images/test_streamline.png') # Now deepcopy the source and replace the existing one with # the copy. This basically simulates cutting/copying the # object from the UI via the right-click menu on the tree # view, and pasting the copy back. sources1 = copy.deepcopy(sources) s.children[:] = sources s.scene.reset_zoom() self.compare_image(s, 'images/test_streamline.png')