Exemple #1
0
def new_axis(fig=None):
    from mayavi.modules.axes import Axes
    from mayavi.tools.engine_manager import get_engine, engine_manager
    from mayavi.tools.figure import gcf
    scene = gcf()
    if fig is None:
        engine = get_engine()
    else:
        engine = engine_manager.find_figure_engine(fig)
        engine.current_scene = fig

    if scene.scene is not None:
        scene.scene.disable_render = True
    parent = engine.current_object

    # Try to find an existing module, if not add one to the pipeline
    if parent == None:
        target = scene
    else:
        target = parent

    ax = Axes()

    engine.add_module(ax, obj=parent)

    if scene.scene is not None:
        scene.scene.disable_render = False
    return ax
Exemple #2
0
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 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.

        #Make the grid
        grid = self.make_grid4scatter()
        e.add_source(grid)

        eg = ExtractGrid()
        e.add_filter(eg)

        nb_ticks = 6

        eg.x_ratio = eg.y_ratio = eg.z_ratio = 100 / (nb_ticks - 1) / 2

        gpx = GridPlane()
        e.add_module(gpx)
        gpx.grid_plane.axis = 'x'

        gpy = GridPlane()
        e.add_module(gpy)
        gpy.grid_plane.axis = 'y'

        gpz = GridPlane()
        e.add_module(gpz)
        gpz.grid_plane.axis = 'z'

        #Add the scatter
        d = VTKDataSource()
        d.data = self.make_scatter()
        e.add_source(d)
        if is_old_pipeline():
            a = Axes()
            e.add_module(a)
            a.axes.number_of_labels = nb_ticks

        self.eg = eg
        self.gpx = gpx
        self.gpy = gpy
        self.gpz = gpz
        self.scene = e.current_scene
        return
Exemple #4
0
    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
Exemple #5
0
    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'
Exemple #6
0
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

from mayavi.modules.axes import Axes
axes = Axes()
engine.add_module(axes, obj=None)

axes.axes.x_label = 'i'
axes.axes.y_label = 'j'
axes.axes.z_label = 'k'
axes.axes.label_format = ''
axes.property.display_location = 'background'

scene.scene.isometric_view()

camera_light = engine.scenes[0].scene.light_manager.lights[0]
camera_light.activate = True
camera_light.azimuth = -20.0
camera_light.elevation = 35.0
camera_light.color = (1.0, 0.0, 1.0)
    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
Exemple #8
0
    def run(self):
        """This is executed once the application GUI has started.
        *Make sure all other MayaVi specific imports are made here!*
        """

        # Various imports to do different things.
        from mayavi.sources.vtk_file_reader import VTKFileReader
        from mayavi.modules.outline import Outline
        from mayavi.modules.axes import Axes
        from mayavi.modules.grid_plane import GridPlane
        from mayavi.modules.image_plane_widget import ImagePlaneWidget
        from mayavi.modules.text import Text
        from mayavi.modules.contour_grid_plane import ContourGridPlane
        from mayavi.modules.iso_surface import IsoSurface

        script = self.script

        # Create a new scene.
        script.new_scene()

        # Read a VTK (old style) data file.
        r = VTKFileReader()
        r.initialize(join(get_data_dir(abspath(__file__)), 'heart.vtk'))
        script.add_source(r)

        # Put up some text.
        t = Text(text='MayaVi rules!',
                 x_position=0.2,
                 y_position=0.9,
                 width=0.8)
        t.property.color = 1, 1, 0  # Bright yellow, yeah!
        script.add_module(t)

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

        # Create an axes for the data.
        a = Axes()
        script.add_module(a)

        # Create three simple grid plane modules.
        # First normal to 'x' axis.
        gp = GridPlane()
        script.add_module(gp)
        # Second normal to 'y' axis.
        gp = GridPlane()
        gp.grid_plane.axis = 'y'
        script.add_module(gp)
        # Third normal to 'z' axis.
        gp = GridPlane()
        script.add_module(gp)
        gp.grid_plane.axis = 'z'

        # Create one ImagePlaneWidget.
        ipw = ImagePlaneWidget()
        script.add_module(ipw)
        # Set the position to the middle of the data.
        ipw.ipw.slice_position = 16

        # Create one ContourGridPlane normal to the 'x' axis.
        cgp = ContourGridPlane()
        script.add_module(cgp)
        # Set the position to the middle of the data.
        cgp.grid_plane.axis = 'y'
        cgp.grid_plane.position = 15

        # An isosurface module.
        iso = IsoSurface(compute_normals=True)
        script.add_module(iso)
        iso.contour.contours = [200.0]

        # Set the view.
        s = script.engine.current_scene
        cam = s.scene.camera
        cam.azimuth(45)
        cam.elevation(15)
        s.render()
Exemple #9
0
def plot(filename, number_bins=6):
    """Plot the cubes from a file.

    The cubes are stratified into number_bins norm bins. The
    transparency of the cubes is set depending on which norm bin the
    cube is in.
    """

    re_matrix_A = re.compile("^\s*Matrix A$")
    re_matrix_B = re.compile("^\s*Matrix B$")
    re_matrix_C = re.compile("^\s*Matrix C$")
    re_product_space = re.compile("^\s*Product Space$")

    fd = open(filename)
    (block_size, A_i, A_j, A_width_i, A_width_j,
     A_norm) = read_squares(fd, re_matrix_A, end=re_matrix_B)
    (block_size, B_i, B_j, B_width_i, B_width_j,
     B_norm) = read_squares(fd, re_matrix_B, end=re_matrix_C)
    (block_size, C_i, C_j, C_width_i, C_width_j,
     C_norm) = read_squares(fd, re_matrix_C, end=re_product_space)

    (prod_i, prod_j, prod_k, prod_width_i, prod_width_j, prod_width_k,
     prod_norm) = read_cubes(fd, re_product_space)

    # Get the current figure.
    figure = mlab.gcf()

    # Get the engine.
    engine = mlab.get_engine()

    # Clean the figure.
    mlab.clf()

    # Turn off rendering (for performance).
    figure.scene.disable_render = True

    # Tune background color.
    figure.scene.background = (1., 1., 1.)

    # Stratify matrix squares.
    (norms_stratified, A_i_stratified, A_j_stratified, A_width_i_stratified,
     A_width_j_stratified) = stratify(number_bins, A_norm, A_i, A_j, A_width_i,
                                      A_width_j)

    # Add matrices.
    print("Plotting matrix A")
    for i in range(number_bins):
        if len(A_i_stratified[i]) > 0:
            points = mlab.points3d(A_i_stratified[i],
                                   [1 for j in range(len(A_i_stratified[i]))],
                                   A_j_stratified[i],
                                   mode='cube',
                                   color=(0.0, 0.5019607843137255,
                                          0.5019607843137255),
                                   scale_factor=1,
                                   opacity=0.5 * (i + 1) / float(number_bins))

            points.glyph.glyph_source.glyph_source.x_length = block_size
            points.glyph.glyph_source.glyph_source.y_length = 0
            points.glyph.glyph_source.glyph_source.z_length = block_size

    (norms_stratified, B_i_stratified, B_j_stratified, B_width_i_stratified,
     B_width_j_stratified) = stratify(number_bins, B_norm, B_i, B_j, B_width_i,
                                      B_width_j)

    # Add matrices.
    print("Plotting matrix B")
    for i in range(number_bins):
        if len(B_i_stratified[i]) > 0:
            points = mlab.points3d([1 for j in range(len(B_i_stratified[i]))],
                                   B_j_stratified[i],
                                   B_i_stratified[i],
                                   mode='cube',
                                   color=(0.5019607843137255, 0.0, 0.0),
                                   scale_factor=1,
                                   opacity=0.5 * (i + 1) / float(number_bins))
            points.glyph.glyph_source.glyph_source.x_length = 0
            points.glyph.glyph_source.glyph_source.y_length = block_size
            points.glyph.glyph_source.glyph_source.z_length = block_size

    (norms_stratified, C_i_stratified, C_j_stratified, C_width_i_stratified,
     C_width_j_stratified) = stratify(number_bins, C_norm, C_i, C_j, C_width_i,
                                      C_width_j)

    # Add matrices.
    print("Plotting matrix C")
    for i in range(number_bins):
        if len(C_i_stratified[i]) > 0:
            points = mlab.points3d(C_i_stratified[i],
                                   C_j_stratified[i],
                                   [1 for j in range(len(C_i_stratified[i]))],
                                   mode='cube',
                                   color=(0.5019607843137255, 0.0,
                                          0.5019607843137255),
                                   scale_factor=1,
                                   opacity=0.5 * (i + 1) / float(number_bins))
            points.glyph.glyph_source.glyph_source.x_length = block_size
            points.glyph.glyph_source.glyph_source.y_length = block_size
            points.glyph.glyph_source.glyph_source.z_length = 0

    # Stratify cubes by norm.
    (norms_stratified, prod_i_stratified, prod_j_stratified,
     prod_k_stratified) = stratify(number_bins, prod_norm, prod_i, prod_j,
                                   prod_k)

    # Add cubes.
    print("Plotting product cubes")
    for i in range(number_bins):
        if len(prod_i_stratified[i]) > 0:
            points = mlab.points3d(prod_i_stratified[i],
                                   prod_j_stratified[i],
                                   prod_k_stratified[i],
                                   mode='cube',
                                   color=(0.2, .2, .2),
                                   scale_factor=1,
                                   opacity=0.75 * (i + 1) / float(number_bins))
            points.glyph.glyph_source.glyph_source.x_length = block_size
            points.glyph.glyph_source.glyph_source.y_length = block_size
            points.glyph.glyph_source.glyph_source.z_length = block_size

    i_max = max(numpy.amax(prod_i), numpy.amax(prod_j),
                numpy.amax(prod_k)) + block_size / 2
    print("i_max = {:e}".format(i_max))

    # Insert fake invisible data-set for axes.
    mlab.points3d([1, i_max], [1, i_max], [1, i_max],
                  mode='cube',
                  scale_factor=0)
    #mlab.axes(xlabel="i", ylabel="j", zlabel="k", extent=[1, xmax, 1, xmax, 1, xmax])

    # Box around the whole thing.
    mlab.outline(extent=[1, i_max, 1, i_max, 1, i_max])

    outline = engine.scenes[0].children[-1].children[0].children[1]
    outline.actor.property.color = (0, 0, 0)
    outline.actor.property.line_width = 2

    # Add axes.
    from mayavi.modules.axes import Axes
    axes = Axes()
    engine.add_module(axes, obj=None)

    axes.axes.label_format = '%-3.0f'
    axes.axes.width = 2
    axes.axes.x_label = 'i'
    axes.axes.y_label = 'j'
    axes.axes.z_label = 'k'
    axes.label_text_property.color = (0, 0, 0)
    axes.label_text_property.opacity = 0.0
    axes.label_text_property.shadow = True
    axes.label_text_property.shadow_offset = numpy.array([1, -1])
    axes.property.color = (0, 0, 0)
    axes.property.display_location = 'background'
    axes.title_text_property.color = (0, 0, 0)
    axes.title_text_property.shadow_offset = numpy.array([1, -1])

    figure.scene.disable_render = False
    figure.scene.camera.compute_view_plane_normal()

    import os.path
    #-------------------------------------------------------------------------------------------------------
    #./spammsand_invsqrt 33_x8_11_S.mm 1.d-1 1.d-3 1.d-1 1.d-1 D U R b=16
    #    figure.scene.isometric_view()
    #    png_filename = os.path.splitext(filename)[0] + "_isov.png"
    #    print("Saving image to " + png_filename)
    #    figure.scene.save(png_filename,size=(1024,1024))
    #    figure.scene.camera.position = [2381.7518163797836, 2526.3678093421449, 2530.13269951962]
    #    figure.scene.camera.focal_point = [440.00000000000028, 440.0000000000029, 439.99999999999733]
    #    figure.scene.camera.view_angle = 30.0
    #    figure.scene.camera.view_up = [-0.4189314063923294, -0.41776697205346547, 0.80620545383879905]
    #    figure.scene.camera.clipping_range = [1986.7866107311997, 5491.0522577990569]
    #    figure.scene.camera.compute_view_plane_normal()
    #    figure.scene.render()
    #    png_filename = os.path.splitext(filename)[0] + "_cant_x.png"
    #    print("Saving image to " + png_filename)
    #    figure.scene.save(png_filename,size=(1024,1024))

    #./spammsand_invsqrt water_500_to_6-311gss.mm 1.d-2 1.d-4 1.d-1 0.d0 D U R

    figure.scene.camera.position = [
        35816.735234550884, 38331.094829602851, 41443.525860211055
    ]
    figure.scene.camera.focal_point = [
        2614.1156973829502, 2621.6382407405645, -241.34477379674968
    ]
    figure.scene.camera.view_angle = 30.0
    figure.scene.camera.view_up = [
        -0.45361775222697864, -0.4654155004102597, 0.76001272807921516
    ]
    figure.scene.camera.clipping_range = [
        26313.825398895184, 87716.669164634935
    ]
    figure.scene.camera.compute_view_plane_normal()
    figure.scene.render()

    png_filename = os.path.splitext(filename)[0] + "_cant_x.png"
    print("Saving image to " + png_filename)
    figure.scene.save(png_filename, size=(768, 768))
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()
Exemple #11
0
    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
Exemple #12
0
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()
Exemple #13
0
    def run(self):
        """This is executed once the application GUI has started.
        *Make sure all other MayaVi specific imports are made here!*
        """
        # Various imports to do different things.
        from mayavi.sources.vtk_file_reader import VTKFileReader
        from mayavi.modules.outline import Outline
        from mayavi.modules.axes import Axes
        from mayavi.modules.grid_plane import GridPlane
        from mayavi.modules.image_plane_widget import ImagePlaneWidget
        from mayavi.modules.text import Text

        script = self.script
        # Create a new scene.
        script.new_scene()

        # Read a VTK (old style) data file.
        r = VTKFileReader()
        r.initialize(
            join(get_data_dir(dirname(abspath(__file__))), 'heart.vtk'))
        script.add_source(r)

        # Put up some text.
        t = Text(text='MayaVi rules!',
                 x_position=0.2,
                 y_position=0.9,
                 width=0.8)
        t.property.color = 1, 1, 0  # Bright yellow, yeah!
        script.add_module(t)

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

        # Create an axes for the data.
        a = Axes()
        script.add_module(a)

        # Create an orientation axes for the scene.  This only works with
        # VTK-4.5 and above which is why we have the try block.
        try:
            from mayavi.modules.orientation_axes import OrientationAxes
        except ImportError:
            pass
        else:
            a = OrientationAxes()
            a.marker.set_viewport(0.0, 0.8, 0.2, 1.0)
            script.add_module(a)

        # Create three simple grid plane modules.
        # First normal to 'x' axis.
        gp = GridPlane()
        script.add_module(gp)
        # Second normal to 'y' axis.
        gp = GridPlane()
        gp.grid_plane.axis = 'y'
        script.add_module(gp)
        # Third normal to 'z' axis.
        gp = GridPlane()
        script.add_module(gp)
        gp.grid_plane.axis = 'z'

        # Create one ImagePlaneWidget.
        ipw = ImagePlaneWidget()
        script.add_module(ipw)
        # Set the position to the middle of the data.
        ipw.ipw.slice_position = 16