Search code examples
pythonnumpymultidimensional-arraymayavi

Time dependent data in Mayavi


Assuming I have a 4d numpy array like this: my_array[x,y,z,t].

Is there a simple way to load the whole array into Mayavi, and simply selecting the t I want to investigate for?

I know that it is possible to animate the data, but I would like to rotate my figure "on the go".


Solution

  • It is possible to set up a dialogue with a input box in which you can set t. You have to use the traits.api, it could look like this:

    from traits.api import HasTraits, Int, Instance, on_trait_change
    from traitsui.api import View, Item, Group
    from mayavi.core.ui.api import SceneEditor, MlabSceneModel, MayaviScene
    
    class Data_plot(HasTraits):
      a = my_array
      t = Int(0)
      scene = Instance(MlabSceneModel, ())
      plot = Instance(PipelineBase)
    
      @on_trait_change('scene.activated')
      def show_plot(self):
        self.plot = something(self.a[self.t]) #something can be surf or mesh or other
    
      @on_trait_change('t')
      def update_plot(self):
        self.plot.parent.parent.scalar_data = self.a[self.t] #Change the data
    
      view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                    show_label=False),
                    Group('_', 't'),
                    resizable=True,
                    )
    
    my_plot = Data_plot(a=my_array)
    my_plot.configure_traits()
    

    You can also set up a slider with the command Range instead of Int if you prefer this.