Example #1
0
class Viz3DStrainField(Viz3D):

    label = tr.Str('<unnambed>')
    vis3d = tr.WeakRef

    def setup(self):
        m = mlab
        fname = self.vis3d.file_list[0]
        self.d = VTKXMLFileReader()
        self.d.initialize(fname)
        self.src = m.pipeline.add_dataset(self.d)
        self.warp_vector = m.pipeline.warp_vector(self.src)
        self.surf = m.pipeline.surface(self.warp_vector)
        engine = m.get_engine()
        etc = ExtractTensorComponents()
        engine.add_filter(etc, self.warp_vector)
        surface2 = Surface()
        engine.add_filter(surface2, etc)
        etc.filter.scalar_mode = 'component'
        lut = etc.children[0]
        lut.scalar_lut_manager.set(show_scalar_bar=True,
                                   show_legend=True,
                                   data_name='strain field')
        lut.scalar_lut_manager.scalar_bar.set(height=0.1,
                                              width=0.7,
                                              position=np.array([0.1, 0.1]))

    def plot(self, vot):
        idx = self.vis3d.tloop.get_time_idx(vot)
        self.d.file_list = self.vis3d.file_list
        self.d.timestep = idx + 1
Example #2
0
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.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']
Example #3
0
class Viz3DSDamage(Viz3D):

    label = tr.Str('damage')
    vis3d = tr.WeakRef

    warp_factor = tr.Float(1.0, auto_set=False, enter_set=True)

    def setup(self):
        m = mlab
        fname = self.vis3d.file_list[0]
        self.d = VTKXMLFileReader()
        self.d.initialize(fname)
        self.src = m.pipeline.add_dataset(self.d)
        self.warp_vector = m.pipeline.warp_vector(self.src)
        self.warp_vector.filter.scale_factor = self.warp_factor
        self.surf = m.pipeline.surface(self.warp_vector)
        lut = self.warp_vector.children[0]
        lut.scalar_lut_manager.set(
            lut_mode='Reds',
            show_scalar_bar=True,
            show_legend=True,
            data_name='damage',
            use_default_range=False,
            data_range=np.array([0, 1], dtype=np.float_)
        )

    def plot(self, vot):
        idx = self.vis3d.tloop.get_time_idx(vot)
        self.d.file_list = self.vis3d.file_list
        self.d.timestep = idx + 1
    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 a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('pyramid_ug.vtu'))
        e.add_source(r)

        # Create the filters.
        # CellDerivatives
        cd = tvtk.CellDerivatives()
        ud = UserDefined(filter=cd)
        e.add_filter(ud)
        ctp = CellToPointData()
        ctp.filter.pass_cell_data = False
        e.add_filter(ctp)
        evn = ExtractVectorNorm()
        e.add_filter(evn)
        evc = ExtractVectorComponents(component='y-component')
        o = Optional(filter=evc)
        e.add_filter(o)
        e.add_module(ScalarCutPlane())
        self.scene = e.current_scene
        s = self.scene
        return
Example #5
0
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):
        m = mlab
        fname = self.vis3d.file_list[0]
        var = self.vis3d.var
        self.d = VTKXMLFileReader()
        self.d.initialize(fname)
        self.src = m.pipeline.add_dataset(self.d)
        self.warp_vector = m.pipeline.warp_vector(self.src)
        self.warp_vector.filter.scale_factor = self.warp_factor
        self.surf = m.pipeline.surface(self.warp_vector)
        lut = self.warp_vector.children[0]
        lut.scalar_lut_manager.trait_set(
            lut_mode='Reds',
            show_scalar_bar=True,
            show_legend=True,
            data_name=var,
            use_default_range=False,
            data_range=np.array([0, 1], dtype=np.float_),
        )

        lut.scalar_lut_manager.scalar_bar.width = 0.5
        lut.scalar_lut_manager.scalar_bar.height = 0.15
        lut.scalar_lut_manager.scalar_bar.orientation = 'horizontal'
        lut.scalar_lut_manager.scalar_bar_representation.trait_set(
            maximum_size=np.array([100000, 100000]),
            minimum_size=np.array([1, 1]),
            position=np.array([0.5, 0.05]),
            position2=np.array([0.45, 0.1]),
        )
        lut.scalar_lut_manager.label_text_property.trait_set(
            font_family='times', italic=False, bold=False)
        lut.scalar_lut_manager.title_text_property.trait_set(
            font_family='times', italic=False, bold=False)
Example #7
0
    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 a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data("pyramid_ug.vtu"))
        e.add_source(r)

        # Create the filters.
        # CellDerivatives
        cd = tvtk.CellDerivatives()
        ud = UserDefined(filter=cd)
        e.add_filter(ud)
        ctp = CellToPointData()
        ctp.filter.pass_cell_data = False
        e.add_filter(ctp)
        evn = ExtractVectorNorm()
        e.add_filter(evn)
        evc = ExtractVectorComponents(component="y-component")
        o = Optional(filter=evc)
        e.add_filter(o)
        e.add_module(ScalarCutPlane())
        self.scene = e.current_scene
        s = self.scene
        return
    def setup(self):
        m = mlab
        fname = self.vis3d.file_list[0]
        var = self.vis3d.var
        self.d = VTKXMLFileReader()
        self.d.initialize(fname)
        self.src = m.pipeline.add_dataset(self.d)
        self.warp_vector = m.pipeline.warp_vector(self.src)
        #self.surf = m.pipeline.surface(self.warp_vector)
        engine = m.get_engine()
        self.etc = ExtractTensorComponents()
        engine.add_filter(self.etc, self.warp_vector)
        surface2 = Surface()
        self.surface = surface2
        engine.add_filter(surface2, self.etc)
        lut = self.etc.children[0]
        lut.scalar_lut_manager.set(show_scalar_bar=True,
                                   show_legend=True,
                                   data_name=var)
        lut.scalar_lut_manager.scalar_bar.orientation = 'horizontal'
        lut.scalar_lut_manager.scalar_bar_representation.trait_set(
            maximum_size=np.array([100000, 100000]),
            minimum_size=np.array([1, 1]),
            position=np.array([0.3, 0.05]),
            position2=np.array([0.65, 0.1]),
        )

        lut.scalar_lut_manager.label_text_property.trait_set(
            font_family='times', italic=False, bold=False)
        lut.scalar_lut_manager.title_text_property.trait_set(
            font_family='times', italic=False, bold=False)
        self.etc.filter.scalar_mode = 'component'
Example #9
0
def setup_data( fname ):
    """Given a VTK XML file name `fname`, this creates a mayavi2
    reader for it and adds it to the pipeline.  It returns the reader
    created.
    """
    r = VTKXMLFileReader()
    r.initialize( fname )
    mayavi.add_source( r )
    return r
Example #10
0
def setup_data(fname):
    """Given a VTK XML file name `fname`, this creates a mayavi2
    reader for it and adds it to the pipeline.  It returns the reader
    created.
    """
    r = VTKXMLFileReader()
    r.initialize(fname)
    mayavi.add_source(r)
    return r
    def test_update_files_updates_all_file_lists(self):
        # Given
        e = self.engine
        # Read a VTK XML data file.
        r = VTKXMLFileReader()
        r.initialize(self.abc1)
        e.add_source(r)

        r2 = VTKXMLFileReader()
        r2.initialize(self.def1)
        e.add_source(r2)
        r.sync_timestep = True
        r.timestep = 0

        self.assertEqual(r._max_timestep, 1)
        self.assertEqual(len(r.file_list), 2)

        # When
        shutil.copy(self.abc1, os.path.join(self.root, "abc_3.vti"))
        shutil.copy(self.def1, os.path.join(self.root, "def_3.vti"))
        r.update_files = True

        # Then
        self.assertEqual(r._max_timestep, 2)
        self.assertEqual(len(r.file_list), 3)
        self.assertEqual(r2._max_timestep, 2)
        self.assertEqual(len(r2.file_list), 3)
    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)
Example #13
0
    def make_data(self):
        script = self.script
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()

        # Read a VTK XML data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data("heart.vti"))
        script.add_source(r)
Example #14
0
    def make_data(self):
        script = self.script
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()

        # Read a VTK XML data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('heart.vti'))
        script.add_source(r)
    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)
Example #16
0
 def setup(self):
     m = mlab
     fname = self.vis3d.file_list[0]
     self.d = VTKXMLFileReader()
     self.d.initialize(fname)
     self.src = m.pipeline.add_dataset(self.d)
     self.warp_vector = m.pipeline.warp_vector(self.src)
     self.warp_vector.filter.scale_factor = self.warp_factor
     self.surf = m.pipeline.surface(self.warp_vector)
     lut = self.warp_vector.children[0]
     lut.scalar_lut_manager.set(lut_mode='Reds',
                                show_scalar_bar=True,
                                show_legend=True,
                                data_name='damage',
                                use_default_range=False,
                                data_range=np.array([0, 1],
                                                    dtype=np.float_))
    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
Example #18
0
    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
Example #19
0
 def setup(self):
     m = mlab
     fname = self.vis3d.file_list[0]
     self.d = VTKXMLFileReader()
     self.d.initialize(fname)
     self.src = m.pipeline.add_dataset(self.d)
     self.warp_vector = m.pipeline.warp_vector(self.src)
     self.surf = m.pipeline.surface(self.warp_vector)
     engine = m.get_engine()
     etc = ExtractTensorComponents()
     engine.add_filter(etc, self.warp_vector)
     surface2 = Surface()
     engine.add_filter(surface2, etc)
     etc.filter.scalar_mode = 'component'
     lut = etc.children[0]
     lut.scalar_lut_manager.set(show_scalar_bar=True,
                                show_legend=True,
                                data_name='strain field')
     lut.scalar_lut_manager.scalar_bar.set(height=0.1,
                                           width=0.7,
                                           position=np.array([0.1, 0.1]))
    def test_play_updates_timestep(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)

        r2 = VTKXMLFileReader()
        r2.initialize(self.def1)
        e.add_source(r2)

        # When
        r.play = True

        # Then
        self.assertEqual(r.timestep, 1)
        self.assertEqual(r2.timestep, 0)
Example #21
0
    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 a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('pyramid_ug.vtu'))
        e.add_source(r)

        # Create the filters.
        idp = ImageDataProbe()
        idp.rescale_scalars = True
        e.add_filter(idp)
        cgp = ContourGridPlane(enable_contours=False)
        e.add_module(cgp)
        cgp.grid_plane.axis = 'z'
        cgp.grid_plane.position = 1
        self.scene = e.current_scene
        return
Example #22
0
    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 a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('pyramid_ug.vtu'))
        e.add_source(r)

        # Create the filters.
        idp = ImageDataProbe()
        idp.rescale_scalars = True
        e.add_filter(idp)
        cgp = ContourGridPlane(enable_contours=False)
        e.add_module(cgp)
        cgp.grid_plane.axis = 'z'
        cgp.grid_plane.position = 1
        self.scene = e.current_scene
        return
    def test_play_updates_timestep(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)

        r2 = VTKXMLFileReader()
        r2.initialize(self.def1)
        e.add_source(r2)

        # When
        r.play = True

        # Then
        self.assertEqual(r.timestep, 1)
        self.assertEqual(r2.timestep, 0)
    def test_update_files_updates_file_list(self):
        # Given
        e = self.engine
        # Read a VTK XML data file.
        r = VTKXMLFileReader()
        r.initialize(self.abc1)
        r.timestep = 0
        e.add_source(r)

        self.assertEqual(r._max_timestep, 1)
        self.assertEqual(len(r.file_list), 2)

        # When
        shutil.copy(self.abc1, os.path.join(self.root, 'abc_3.vti'))
        r.update_files = True

        # Then
        self.assertEqual(r._max_timestep, 2)
        self.assertEqual(len(r.file_list), 3)
    def test_play_calls_movie_maker_correctly(self):
        # Given
        e = self.engine
        # Read a VTK XML data file.
        r = VTKXMLFileReader()
        r.initialize(self.abc1)
        r.timestep = 0
        e.add_source(r)
        # Mock
        s = make_mock_scene()
        r.scene = s
        mm = s.movie_maker

        # Create an outline for the data.
        o = Outline()
        e.add_module(o)

        # When
        r.play = True

        # Then
        mm.animation_start.assert_called_once_with()
        self.assertEqual(mm.animation_step.call_count, 1)
        mm.animation_stop.assert_called_once_with()
    def test_play_calls_movie_maker_correctly(self):
        # Given
        e = self.engine
        # Read a VTK XML data file.
        r = VTKXMLFileReader()
        r.initialize(self.abc1)
        r.timestep = 0
        e.add_source(r)
        # Mock
        s = make_mock_scene()
        r.scene = s
        mm = s.movie_maker

        # Create an outline for the data.
        o = Outline()
        e.add_module(o)

        # When
        r.play = True

        # Then
        mm.animation_start.assert_called_once_with()
        self.assertEqual(mm.animation_step.call_count, 1)
        mm.animation_stop.assert_called_once_with()
    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)
Example #28
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.modules.api import ScalarCutPlane
        from mayavi.modules.labels import Labels
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        cp = ScalarCutPlane()
        script.add_module(cp)
        l = Labels(object=cp)
        script.add_module(l)

        s.scene.isometric_view()
        self.check(saved=False)
        ############################################################
        # 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
        s.scene.isometric_view()

        # Seems to be needed for the test to pass. :(  Just flushes the
        # pipeline.
        s.children[0].pipeline_changed = True

        # Check.
        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)
Example #29
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.filters.image_data_probe import ImageDataProbe
        from mayavi.modules.api import ContourGridPlane
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        idp = ImageDataProbe()
        script.add_filter(idp)
        cgp = ContourGridPlane(enable_contours=False)
        script.add_module(cgp)
        cgp.grid_plane.axis = 'z'
        cgp.grid_plane.position = 2
        s.scene.isometric_view()

        # Check.
        self.check(saved=False)
        ############################################################
        # Test if saving a visualization and restoring it works.

        # 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
        s.scene.isometric_view()

        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)
Example #30
0
    def do(self):
        ############################################################
        # Imports.
        script = self.script
        from mayavi.filters.optional import Optional
        from mayavi.filters.warp_scalar import WarpScalar
        from mayavi.filters.cut_plane import CutPlane
        from mayavi.components.poly_data_normals import PolyDataNormals
        from mayavi.components.contour import Contour
        from mayavi.components.actor import Actor
        from mayavi.modules.generic_module import GenericModule
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # We now create the complete equivalent of a ScalarCutPlane in
        # the next block!
        cp = CutPlane()
        w = WarpScalar()
        warper = Optional(filter=w, label_text='Enable warping', enabled=False)
        c = Contour()
        ctr = Optional(filter=c, label_text='Enable contours', enabled=False)
        p = PolyDataNormals(name='Normals')
        normals = Optional(filter=p, label_text='Compute normals', enabled=False)
        a = Actor()
        components = [cp, warper, ctr, normals, a]
        m = GenericModule(name='ScalarCutPlane',
                          components=components,
                          contour=c, actor=a)

        script.add_module(m)
        s.scene.isometric_view()

        ########################################
        # do the testing.
        def check(mod):
            """Check if test status is OK for the given module."""
            cut, warper, ctr, normals, a = mod.components
            c = ctr.filter
            # The intermediate ones are disabled.
            assert normals.outputs[0] is cut.outputs[0]
            # Enable the contours.
            ctr.enabled = True
            assert ctr.outputs[0] is c.outputs[0]
            assert ctr.outputs[0] is normals.outputs[0]
            rng = normals.outputs[0].point_data.scalars.range
            assert (rng[1] - rng[0]) < 1e-4
            # Turn on auto-contours
            c.auto_contours = True
            # Increase number of contours and the range should change.
            c.number_of_contours = 10
            assert len(normals.outputs[0].points) != 0
            rng = normals.outputs[0].point_data.scalars.range
            assert rng[0] < rng[1]
            # Check if pipeline_changed is correctly propagated.
            old = normals.outputs[0]
            assert a.mapper.scalar_mode == 'default'
            c.filled_contours = True
            assert normals.outputs[0] != old
            assert normals.outputs[0] is c.outputs[0]
            # Check if the actor responds correctly to the
            # filled_contour change.
            assert a.mapper.scalar_mode == 'use_cell_data'

            # Set back everything to original state.
            c.filled_contours = False
            assert a.mapper.scalar_mode == 'default'
            c.number_of_contours = 1
            c.auto_contours = False
            ctr.enabled = False
            assert normals.outputs[0] is cut.outputs[0]

        check(m)

        ############################################################
        # 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
        s.scene.isometric_view()

        # Now do the check.
        m = s.children[0].children[0].children[0]
        check(m)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        m = s.children[0].children[0].children[0]
        s.scene.isometric_view()
        check(m)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        m = s.children[0].children[0].children[0]
        s.scene.isometric_view()
        check(m)
    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)
Example #32
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.filters.image_data_probe import ImageDataProbe
        from mayavi.modules.api import ContourGridPlane
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        idp = ImageDataProbe()
        script.add_filter(idp)
        cgp = ContourGridPlane(enable_contours=False)
        script.add_module(cgp)
        cgp.grid_plane.axis = 'z'
        cgp.grid_plane.position = 2
        s.scene.isometric_view()

        # Check.
        self.check(saved=False)
        ############################################################
        # Test if saving a visualization and restoring it works.

        # 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
        s.scene.isometric_view()

        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)
Example #33
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.filters.optional import Optional
        from mayavi.filters.user_defined import UserDefined
        from mayavi.filters.api import (CellToPointData, ExtractVectorNorm,
                                        ExtractVectorComponents)
        from mayavi.modules.api import ScalarCutPlane
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        # CellDerivatives
        cd = tvtk.CellDerivatives()
        ud = UserDefined(filter=cd)
        script.add_filter(ud)
        ctp = CellToPointData()
        ctp.filter.pass_cell_data = False
        script.add_filter(ctp)
        evn = ExtractVectorNorm()
        script.add_filter(evn)
        evc = ExtractVectorComponents(component='y-component')
        o = Optional(filter=evc)
        script.add_filter(o)

        script.add_module(ScalarCutPlane())
        s.scene.isometric_view()

        # Check.
        self.check(saved=False)
        ############################################################
        # Test if saving a visualization and restoring it works.

        # 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
        s.scene.isometric_view()

        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)
Example #34
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.modules.api import ScalarCutPlane
        from mayavi.modules.labels import Labels
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        cp = ScalarCutPlane()
        script.add_module(cp)
        l = Labels(object=cp)
        script.add_module(l)

        s.scene.isometric_view()
        GUI.process_events()
        self.check(saved=False)
        ############################################################
        # 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)

        # Remove existing scene.
        engine = script.engine
        engine.close_scene(s)

        # Load visualization
        script.load_visualization(f)
        s = engine.current_scene
        s.scene.isometric_view()

        # Seems to be needed for the test to pass. :(  Just flushes the
        # pipeline.
        s.children[0].pipeline_changed = True
        GUI.process_events()

        # Check.
        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        GUI.process_events()

        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        GUI.process_events()

        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        GUI.process_events()
Example #35
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.filters.optional import Optional
        from mayavi.filters.user_defined import UserDefined
        from mayavi.filters.api import (CellToPointData,
                ExtractVectorNorm, ExtractVectorComponents)
        from mayavi.modules.api import ScalarCutPlane
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        # CellDerivatives
        cd = tvtk.CellDerivatives()
        ud = UserDefined(filter=cd)
        script.add_filter(ud)
        ctp = CellToPointData()
        ctp.filter.pass_cell_data = False
        script.add_filter(ctp)
        evn = ExtractVectorNorm()
        script.add_filter(evn)
        evc = ExtractVectorComponents(component='y-component')
        o = Optional(filter=evc)
        script.add_filter(o)

        script.add_module(ScalarCutPlane())
        s.scene.isometric_view()

        # Check.
        self.check(saved=False)
        ############################################################
        # Test if saving a visualization and restoring it works.

        # 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
        s.scene.isometric_view()

        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)
Example #36
0
    def do(self):
        ############################################################
        # Imports.
        script = self.script
        from mayavi.filters.optional import Optional
        from mayavi.filters.warp_scalar import WarpScalar
        from mayavi.filters.cut_plane import CutPlane
        from mayavi.components.poly_data_normals import PolyDataNormals
        from mayavi.components.contour import Contour
        from mayavi.components.actor import Actor
        from mayavi.modules.generic_module import GenericModule
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # We now create the complete equivalent of a ScalarCutPlane in
        # the next block!
        cp = CutPlane()
        w = WarpScalar()
        warper = Optional(filter=w, label_text='Enable warping', enabled=False)
        c = Contour()
        ctr = Optional(filter=c, label_text='Enable contours', enabled=False)
        p = PolyDataNormals(name='Normals')
        normals = Optional(filter=p,
                           label_text='Compute normals',
                           enabled=False)
        a = Actor()
        components = [cp, warper, ctr, normals, a]
        m = GenericModule(name='ScalarCutPlane',
                          components=components,
                          contour=c,
                          actor=a)

        script.add_module(m)
        s.scene.isometric_view()

        ########################################
        # do the testing.
        def check(mod):
            """Check if test status is OK for the given module."""
            cut, warper, ctr, normals, a = mod.components
            c = ctr.filter
            # The intermediate ones are disabled.
            assert normals.outputs[0] is cut.outputs[0]
            # Enable the contours.
            ctr.enabled = True
            assert ctr.outputs[0] is c.outputs[0]
            assert ctr.outputs[0] is normals.outputs[0]
            n_output = self._get_output(normals.outputs[0])
            rng = n_output.point_data.scalars.range
            assert (rng[1] - rng[0]) < 1e-4
            # Turn on auto-contours
            c.auto_contours = True
            # Increase number of contours and the range should change.
            c.number_of_contours = 10
            n_output = self._get_output(normals.outputs[0])
            assert len(n_output.points) != 0
            rng = n_output.point_data.scalars.range
            assert rng[0] < rng[1]
            # Check if pipeline_changed is correctly propagated.
            old = self._get_output(normals.outputs[0])
            assert a.mapper.scalar_mode == 'default'
            c.filled_contours = True
            n_output = self._get_output(normals.outputs[0])
            c_output = self._get_output(c.outputs[0])
            assert n_output != old
            assert n_output is c_output
            # Check if the actor responds correctly to the
            # filled_contour change.
            assert a.mapper.scalar_mode == 'use_cell_data'

            # Set back everything to original state.
            c.filled_contours = False
            assert a.mapper.scalar_mode == 'default'
            c.number_of_contours = 1
            c.auto_contours = False
            ctr.enabled = False
            assert normals.outputs[0] is cut.outputs[0]

        check(m)

        ############################################################
        # 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
        s.scene.isometric_view()

        # Now do the check.
        m = s.children[0].children[0].children[0]
        check(m)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        # Now do the check.
        m = s.children[0].children[0].children[0]
        s.scene.isometric_view()
        check(m)

        # 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.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        # Now do the check.
        m = s.children[0].children[0].children[0]
        s.scene.isometric_view()
        check(m)