class CustomColormap(HasTraits): lut_list = lut_manager.lut_mode_list() lut_list.remove('black-white') lut_list.remove('blue-red') lut_list.append('custom_heat') imgs_path = 'cmap_images' map_type = Enum('default', 'scalar', 'activation', 'connmat') cmap = Enum(lut_list) reverse = Bool fname = File label = Str threshold = Range(0.0, 0.5, 0.2) _pl = Property(Instance(LinearSegmentedColormap)) def __init__(self, type): self.map_type = type def _cmap_default(self): if self.map_type == 'scalar': return 'BuGn' elif self.map_type == 'activation': return 'YlOrRd' elif self.map_type == 'connmat': return 'RdYlBu' else: return 'cool' #return default def _reverse_default(self): if self.map_type == 'connmat': return True else: return False def _label_default(self): if self.map_type == 'scalar': return 'Scalars Colormap' elif self.map_type == 'activation': return 'Conns Colormap' elif self.map_type == 'connmat': return 'Matrix Colormap' else: return 'Default Colormap' def _get__pl(self): '''return the LinearSegmentedColormap describing this CustomColormap''' if self.cmap == 'file' and self.fname: colors = lut_manager.parse_lut_file(self.fname) if self.reverse: colors.reverse() return LinearSegmentedColormap.from_list('file', colors) elif self.cmap == 'custom_heat': return gen_heatmap(t=self.threshold, reverse=self.reverse) elif self.reverse: return get_cmap(self.cmap + '_r') else: return get_cmap(self.cmap)
class DataModuleFactory(ModuleFactory): """ Base class for all the module factories operating on data (ie not text and outline) """ reset_zoom = true(help="""Reset the zoom to accomodate the data newly added to the scene. Defaults to True.""") extent = CArray(shape=(6,), help="""[xmin, xmax, ymin, ymax, zmin, zmax] Default is the x, y, z arrays extent. Use this to change the extent of the object created.""", ) def _extent_changed(self): tools.set_extent(self._target, self.extent) transparent = false(help="""make the opacity of the actor depend on the scalar.""") def _transparent_changed(self): if self.transparent: data_range = \ self._target.module_manager.scalar_lut_manager.data_range self._target.module_manager.scalar_lut_manager.lut.alpha_range = \ (0.2, 0.8) data_range = ( numpy.mean(data_range) + 0.4 * ( data_range.max() - data_range.min()) * numpy.array([-1, 1])) self._target.module_manager.scalar_lut_manager.data_range = \ data_range colormap = Trait('blue-red', lut_mode_list(), help="""type of colormap to use.""") def _colormap_changed(self): colormap = self.colormap if colormap[-2:] == "_r": colormap = colormap[:-2] self._target.module_manager.scalar_lut_manager.reverse_lut = True self._target.module_manager.vector_lut_manager.reverse_lut = True self._target.module_manager.scalar_lut_manager.lut_mode = colormap self._target.module_manager.vector_lut_manager.lut_mode = colormap vmin = Trait(None, None, CFloat, help="""vmin is used to scale the colormap. If None, the min of the data will be used""") vmax = Trait(None, None, CFloat, help="""vmax is used to scale the colormap. If None, the max of the data will be used""") def _vmin_changed(self): if self.vmin == None and self.vmax == None: self._target.module_manager.scalar_lut_manager.use_default_range\ = True return self._target.module_manager.scalar_lut_manager.use_default_range \ = False vmin, vmax = \ self._target.module_manager.scalar_lut_manager.data_range if self.vmin is not None: vmin = self.vmin if self.vmax is not None: vmax = self.vmax self._target.module_manager.scalar_lut_manager.data_range = \ (vmin, vmax) _vmax_changed = _vmin_changed def __init__(self, *args, **kwargs): super(DataModuleFactory, self).__init__(*args, **kwargs) # We are adding data to the scene, reset the zoom: scene = self._scene.scene if scene is not None and self.reset_zoom: scene.reset_zoom()
cortex = "classic" # get colors cortex_map = { "classic": ("Greys", -1, 2, False), "high_contrast": ("Greys", -.1, 1.3, False), "low_contrast": ("Greys", -5, 5, False), "bone": ("bone", -.2, 2, True) } # 在这里解释一下为什么"low_contrast"对应的是("Greys", -5, 5, False),其它的举一反三都改知道是什么含义了 # 首先colormap是颜色映射的意思,也就是将颜色和scalar value做映射。"Grey"指定了颜色映射表用的是灰色, # -5~5指定了映射表映射的值的范围,这里是将灰色映射到-5~5宽度为10的值域,而二值化的曲率的值只是0和1,它们之间 # 的灰度差只占了整体的1/10,所以对比度较低,然而high_contrast的值域是-.1~1.3,宽度为1.4,可想而知! if cortex in cortex_map.keys(): color_data = cortex_map[cortex] elif cortex in lut_manager.lut_mode_list(): color_data = cortex, -1, 2, False else: color_data = cortex # collect key-word args colormap, vmin, vmax, reverse = color_data meshargs = dict(figure=figure, scalars=bin_curv) # 第二个参数是取了0,1两个标量 surfargs = dict(colormap=colormap, vmin=vmin, vmax=vmax) # 获取坐标信息 x = coords[:, 0] y = coords[:, 1] z = coords[:, 2] geo_mesh = mayavi.mlab.pipeline.triangular_mesh_source(x, y, z, faces, **meshargs) # 返回一个二维网格 # 再根据顶点法向量调整成surface?
""" Script to generate the preview images for the mayavi2 LUTs. Requires ImageMagick. """ import os from mayavi import mlab from mayavi.core.lut_manager import lut_mode_list, lut_image_dir import numpy as np # Create some data X = np.arange(0, 255) X = X * np.ones((200, 1)) mlab.clf() image = mlab.imshow(X.T) mlab.view(0, 0, 118) # Make a preview for each possible lut for lut in lut_mode_list(): filebasename = os.path.join(lut_image_dir, lut.lower()) if not lut == 'file': image.module_manager.scalar_lut_manager.lut_mode = lut mlab.savefig(filebasename + '.png', size=(80, 20)) #os.system('convert %s.png %s.gif &' %(filebasename, filebasename)) os.system('montage -geometry -0-0 -label "%s" %s.png %s.gif &' % (lut, filebasename, filebasename))
""" Script to generate the preview images for the mayavi2 LUTs. Requires ImageMagick. """ import os from mayavi import mlab from mayavi.core.lut_manager import lut_mode_list, lut_image_dir import numpy as np # Create some data X = np.arange(0, 255) X = X * np.ones((200, 1)) mlab.clf() image = mlab.imshow(X.T) mlab.view(0, 0, 118) # Make a preview for each possible lut for lut in lut_mode_list(): filebasename = os.path.join(lut_image_dir, lut.lower()) if not lut == 'file': image.module_manager.scalar_lut_manager.lut_mode = lut mlab.savefig(filebasename + '.png', size=(80, 20)) #os.system('convert %s.png %s.gif &' %(filebasename, filebasename)) os.system('montage -geometry -0-0 -label "%s" %s.png %s.gif &' % (lut, filebasename, filebasename) )
from traits.etsconfig.api import ETSConfig from traitsui.api \ import Item, Group, View, ImageEnumEditor, InstanceEditor, HGroup from mayavi.core.lut_manager import lut_mode_list, \ lut_image_dir def _number_of_lut_cols(): return 1 if ETSConfig.toolkit == 'qt4' else 6 # The view of the LUT Manager object. view = View( Group( Item(name='lut_mode', editor=ImageEnumEditor(values=lut_mode_list(), cols=_number_of_lut_cols(), path=lut_image_dir)), Item(name='file_name', visible_when="lut_mode=='file'"), Item(name='number_of_colors'), Item(name='reverse_lut'), Item(name='lut', show_label=False, editor=InstanceEditor(label='Edit LUT properties', id='mayavi.core.lut_manager.edit_lut')), Item(name='scalar_bar_representation', show_label=False, visible_when='scalar_bar_representation is not None', editor=InstanceEditor( label='Edit Legend representation', id='mayavi.core.lut_manager.edit_represetation')),
# Judah De Paula <*****@*****.**> # Copyright (c) 2005-2015, Enthought, Inc. # License: BSD Style. from traits.etsconfig.api import ETSConfig from traitsui.api \ import Item, Group, View, ImageEnumEditor, InstanceEditor, HGroup from mayavi.core.lut_manager import lut_mode_list, \ lut_image_dir def _number_of_lut_cols(): return 1 if ETSConfig.toolkit == 'qt4' else 6 # The view of the LUT Manager object. view = View(Group(Item(name='lut_mode', editor=ImageEnumEditor(values=lut_mode_list(), cols=_number_of_lut_cols(), path=lut_image_dir)), Item(name='file_name', visible_when="lut_mode=='file'"), Item(name='number_of_colors'), Item(name='reverse_lut'), Item(name='lut', show_label=False, editor=InstanceEditor(label='Edit LUT properties', id='mayavi.core.lut_manager.edit_lut')), Item(name='scalar_bar_representation', show_label=False, visible_when='scalar_bar_representation is not None', editor=InstanceEditor(label='Edit Legend representation', id='mayavi.core.lut_manager.edit_represetation')), Item(name='create_lut', show_label=False),
#!/usr/bin/env python3 # # Copyright 2014 Simone Campagna # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # __author__ = "Simone Campagna" __all__ = ['COLORMAPS'] from mayavi.core.lut_manager import lut_mode_list COLORMAPS = lut_mode_list()