def _script_default(self): from enthought.mayavi.plugins.script import Script from enthought.mayavi.core.off_screen_engine import OffScreenEngine engine = OffScreenEngine() engine.start() s = Script(engine=engine) return s
def new_engine(self): """ Creates a new engine, envisage or not depending on the options. """ check_backend() if options.backend == 'envisage': from enthought.mayavi.plugins.app import Mayavi m = Mayavi(start_gui_event_loop=False) m.main() process_ui_events() window = m.application.workbench.active_window engine = window.get_service(Engine) elif options.backend == 'test': engine = NullEngine(name='Null Mlab Engine') engine.start() else: if options.offscreen: engine = OffScreenEngine(name='Mlab offscreen Engine') engine.start() else: engine = Engine(name='Mlab Engine') engine.start() self.current_engine = engine return engine
def plot_map_3d( map, affine, cut_coords=None, anat=None, anat_affine=None, threshold=None, offscreen=False, vmin=None, vmax=None, cmap=None, view=(38.5, 70.5, 300, (-2.7, -12, 9.1)), ): """ Plot a 3D volume rendering view of the activation, with an outline of the brain. Parameters ---------- map : 3D ndarray The activation map, as a 3D image. affine : 4x4 ndarray The affine matrix going from image voxel space to MNI space. cut_coords: 3-tuple of floats, optional The MNI coordinates of a 3D cursor to indicate a feature or a cut, in MNI coordinates and order. anat : 3D ndarray, optional The anatomical image to be used as a background. If None, the MNI152 T1 1mm template is used. If False, no anatomical image is used. anat_affine : 4x4 ndarray, optional The affine matrix going from the anatomical image voxel space to MNI space. This parameter is not used when the default anatomical is used, but it is compulsory when using an explicite anatomical image. threshold : float, optional The lower threshold of the positive activation. This parameter is used to threshold the activation map. offscreen: boolean, optional If True, Mayavi attempts to plot offscreen. Will work only with VTK >= 5.2. vmin : float, optional The minimal value, for the colormap vmax : float, optional The maximum value, for the colormap cmap : a callable, or a pylab colormap A callable returning a (n, 4) array for n values between 0 and 1 for the colors. This can be for instance a pylab colormap. Notes ----- If you are using a VTK version below 5.2, there is no way to avoid opening a window during the rendering under Linux. This is necessary to use the graphics card for the rendering. You must maintain this window on top of others and on the screen. """ # Late import to avoid triggering wx imports before needed. from enthought.mayavi import mlab if offscreen: global off_screen_engine if off_screen_engine is None: from enthought.mayavi.core.off_screen_engine import OffScreenEngine off_screen_engine = OffScreenEngine() off_screen_engine.start() fig = mlab.figure( "__private_plot_map_3d__", bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 330), engine=off_screen_engine ) mlab.clf(figure=fig) else: fig = mlab.gcf() fig = mlab.figure(fig, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 350)) disable_render = fig.scene.disable_render fig.scene.disable_render = True if threshold is None: threshold = stats.scoreatpercentile(np.abs(map).ravel(), 80) contours = [] lower_map = map[map <= -threshold] if np.any(lower_map): contours.append(lower_map.max()) upper_map = map[map >= threshold] if np.any(upper_map): contours.append(map[map > threshold].min()) ########################################################################### # Display the map using iso-surfaces if len(contours) > 0: map_src = affine_img_src(map, affine) module = mlab.pipeline.iso_surface(map_src, contours=contours, vmin=vmin, vmax=vmax) if callable(cmap): # Stick the colormap in mayavi module.module_manager.scalar_lut_manager.lut.table = (255 * cmap(np.linspace(0, 1, 256))).astype(np.int) else: module = None if not anat is False: plot_anat_3d(anat=anat, anat_affine=anat_affine, scale=1.05, outline_color=(0.9, 0.9, 0.9), gyri_opacity=0.2) ########################################################################### # Draw the cursor if cut_coords is not None: x0, y0, z0 = cut_coords mlab.plot3d((-90, 90), (y0, y0), (z0, z0), color=(0.5, 0.5, 0.5), tube_radius=0.25) mlab.plot3d((x0, x0), (-126, 91), (z0, z0), color=(0.5, 0.5, 0.5), tube_radius=0.25) mlab.plot3d((x0, x0), (y0, y0), (-72, 109), color=(0.5, 0.5, 0.5), tube_radius=0.25) mlab.view(*view) fig.scene.disable_render = disable_render return module