def _vmin_changed(self): vmin = self.vmin vmax = self.vmax range_min, range_max = self._target.current_range if vmin is None: vmin = range_min if vmax is None: vmax = range_max # Change the opacity function from tvtk.util.ctf import PiecewiseFunction, save_ctfs otf = PiecewiseFunction() if range_min < vmin: otf.add_point(range_min, 0.) if range_max > vmax: otf.add_point(range_max, 0.2) otf.add_point(vmin, 0.) otf.add_point(vmax, 0.2) self._target._otf = otf self._target._volume_property.set_scalar_opacity(otf) if self.color is None and not self.__ctf_rescaled and \ ( (self.vmin is not None) or (self.vmax is not None) ): # FIXME: We don't use 'rescale_ctfs' because it screws up the nodes. def _rescale_value(x): nx = (x - range_min)/(range_max - range_min) return vmin + nx*(vmax - vmin) # The range of the existing ctf can vary. scale_min, scale_max = self._target._ctf.range def _rescale_node(x): nx = (x - scale_min)/(scale_max - scale_min) return range_min + nx*(range_max - range_min) if hasattr(self._target._ctf, 'nodes'): rgb = list() for value in self._target._ctf.nodes: r, g, b = \ self._target._ctf.get_color(value) rgb.append((_rescale_node(value), r, g, b)) else: rgb = save_ctfs(self._target.volume_property)['rgb'] from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() try: ctf.range = (range_min, range_max) except Exception: # VTK versions < 5.2 don't seem to need this. pass rgb.sort() v = rgb[0] ctf.add_rgb_point(range_min, v[1], v[2], v[3]) for v in rgb: ctf.add_rgb_point(_rescale_value(v[0]), v[1], v[2], v[3]) ctf.add_rgb_point(range_max, v[1], v[2], v[3]) self._target._ctf = ctf self._target._volume_property.set_color(ctf) self.__ctf_rescaled = True self._target.update_ctf = True
def ImproveFilterDisplay(): # Changing the ctf: from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() ctf.range = [-1, 1] # Add points to CTF # Note : ctf.add_rgb_point(value, r, g, b) r, g, b are float numbers in [0,1] ctf.add_rgb_point(-1, 0, 0, 1) ctf.add_rgb_point(-0.7, 0, 0, 1) ctf.add_rgb_point(0, 0, 1, 0) ctf.add_rgb_point(0.7, 1, 0, 0) ctf.add_rgb_point(1, 1, 0, 0) # Changing the otf: from tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() # Add points to OTF # Note : otf.add_point(value, opacity) -> opacity is a float number in [0,1] otf.add_point(-1, 0.005) otf.add_point(0, 0.01) otf.add_point(0.6, 0.1) otf.add_point(1, 1) return ctf, otf
def _color_im_planes(im_planes): ctf = ColorTransferFunction() ctf.range = (0.0, 1.0) ctf.add_rgb_point(0.0, 1.0, 1.0, 1.0) ctf.add_rgb_point(1.0, 1.0, 0.275, 0.0) for ip in im_planes: lut = ip.module_manager.scalar_lut_manager.lut.table.to_array() for i in xrange(len(lut)): c = 255 * np.asarray(ctf.get_color(float(i) / (len(lut) - 1))) lut[i] = np.concatenate((c, (255,))) ip.module_manager.scalar_lut_manager.lut.table = lut
def _set_cutoff(volume, cutoff): range_min, range_max = volume.current_range otf = PiecewiseFunction() otf.add_point(range_min, 0.0) otf.add_point(range_max, 0.2) volume._otf = otf volume.volume_property.set_scalar_opacity(otf) ctf = ColorTransferFunction() ctf.range = volume.current_range ctf.add_rgb_point(range_min, 1.0, 0.275, 0.0) ctf.add_rgb_point(range_max, 1.0, 0.275, 0.0) volume._ctf = ctf volume.volume_property.set_color(ctf) set_lut(volume.lut_manager.lut, volume.volume_property)
def make_CTF(x1, x2, hue_range=(2.0 / 3.0, 0.0), sat_range=(1.0, 1.0), val_range=(1.0, 1.0), n=10, mode='sqrt'): """Creates a CTF as per given arguments. Lets one set a hue, saturation and value range. The mode can be one of 'sqrt', or 'linear'. The idea of this function is to create a CTF that is similar to the LUT being used. 's_curve' is not implemented. Patches welcome. """ maxs = max(x1, x2) mins = min(x1, x2) ds = maxs - mins dhue = hue_range[1] - hue_range[0] dsat = sat_range[1] - sat_range[0] dval = val_range[1] - val_range[0] ctf = ColorTransferFunction() try: ctf.range = (mins, maxs) except Exception: # VTK versions < 5.2 don't seem to need this. pass if mode == 'sqrt': for i in range(n + 1): # Generate x in [0, 1] x = 0.5 * (1.0 + cos((n - i) * pi / n)) # Chebyshev nodes. h = hue_range[0] + dhue * x s = sat_range[0] + dsat * x v = val_range[0] + dval * x r, g, b, a = [sqrt(c) for c in hsva_to_rgba(h, s, v, 1.0)] ctf.add_rgb_point(mins + x * ds, r, g, b) elif mode == 'linear': for i in range(n + 1): # Generate x in [0, 1] x = float(i) / n # Uniform nodes. h = hue_range[0] + dhue * x s = sat_range[0] + dsat * x v = val_range[0] + dval * x r, g, b, a = hsva_to_rgba(h, s, v, 1.0) ctf.add_rgb_point(mins + x * ds, r, g, b) return ctf
def _color_changed(self): if not self.color: return range_min, range_max = self._target.current_range from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() try: ctf.range = (range_min, range_max) except Exception: # VTK versions < 5.2 don't seem to need this. pass r, g, b = self.color ctf.add_rgb_point(range_min, r, g, b) ctf.add_rgb_point(range_max, r, g, b) self._target._ctf = ctf self._target._volume_property.set_color(ctf) self._target.update_ctf = True
def make_volume_prop(mins=255, maxs=355): """Make a volume property for the testing.""" table = tvtk.VolumeProperty() ctf = ColorTransferFunction() ds = (maxs - mins) / 4.0 try: ctf.range = (mins, maxs) except Exception: # VTK versions < 5.2 don't seem to need this. pass ctf.add_rgb_point(mins, 0.00, 0.0, 1.00) ctf.add_rgb_point(mins + ds, 0.25, 0.5, 0.75) ctf.add_rgb_point(mins + 2 * ds, 0.50, 1.0, 0.50) ctf.add_rgb_point(mins + 3 * ds, 0.75, 0.5, 0.25) ctf.add_rgb_point(maxs, 1.00, 0.0, 0.00) otf = PiecewiseFunction() otf.add_point(mins, 0.0) otf.add_point(maxs, 0.2) table.set_color(ctf) table.set_scalar_opacity(otf) return table, ctf, otf
def make_volume_prop(mins=255, maxs=355): """Make a volume property for the testing.""" table = tvtk.VolumeProperty() ctf = ColorTransferFunction() ds = (maxs-mins)/4.0 try: ctf.range = (mins, maxs) except Exception: # VTK versions < 5.2 don't seem to need this. pass ctf.add_rgb_point(mins, 0.00, 0.0, 1.00) ctf.add_rgb_point(mins+ds, 0.25, 0.5, 0.75) ctf.add_rgb_point(mins+2*ds, 0.50, 1.0, 0.50) ctf.add_rgb_point(mins+3*ds, 0.75, 0.5, 0.25) ctf.add_rgb_point(maxs, 1.00, 0.0, 0.00) otf = PiecewiseFunction() otf.add_point(mins, 0.0) otf.add_point(maxs, 0.2) table.set_color(ctf) table.set_scalar_opacity(otf) return table, ctf, otf
def NewColorMap(h): ctf = ColorTransferFunction() otf = PiecewiseFunction() ctf.remove_all_points() otf.remove_all_points() ctf.add_rgb_point(0, 1, 1, 1) otf.add_point(0, 0) for i in range(128): q = (i + 1.0) / 128.0 ctf.add_rgb_point(-q, 1 - q * q, 1 - q * q, 1) # -1 --> blue, 0 --> white, 1-->red ctf.add_rgb_point(q, 1, 1 - q * q, 1 - q * q) otf.add_point(-q, 0.5 * q) otf.add_point(q, 0.5 * q) ctf.range = [-1, 1] h._volume_property.set_color(ctf) h._ctf = ctf h.update_ctf = True h._otf = otf h._volume_property.set_scalar_opacity(otf)
def make_CTF(x1, x2, hue_range=(2.0/3.0, 0.0), sat_range=(1.0, 1.0), val_range=(1.0, 1.0), n=10, mode='sqrt'): """Creates a CTF as per given arguments. Lets one set a hue, saturation and value range. The mode can be one of 'sqrt', or 'linear'. The idea of this function is to create a CTF that is similar to the LUT being used. 's_curve' is not implemented. Patches welcome. """ maxs = max(x1, x2) mins = min(x1, x2) ds = maxs - mins dhue = hue_range[1] - hue_range[0] dsat = sat_range[1] - sat_range[0] dval = val_range[1] - val_range[0] ctf = ColorTransferFunction() try: ctf.range = (mins, maxs) except Exception: # VTK versions < 5.2 don't seem to need this. pass if mode == 'sqrt': for i in range(n+1): # Generate x in [0, 1] x = 0.5*(1.0 + cos((n-i)*pi/n)) # Chebyshev nodes. h = hue_range[0] + dhue*x s = sat_range[0] + dsat*x v = val_range[0] + dval*x r, g, b, a = [sqrt(c) for c in hsva_to_rgba(h, s, v, 1.0)] ctf.add_rgb_point(mins+x*ds, r, g, b) elif mode == 'linear': for i in range(n+1): # Generate x in [0, 1] x = float(i)/n # Uniform nodes. h = hue_range[0] + dhue*x s = sat_range[0] + dsat*x v = val_range[0] + dval*x r, g, b, a = hsva_to_rgba(h, s, v, 1.0) ctf.add_rgb_point(mins+x*ds, r, g, b) return ctf
def plot_volume(x, y, z, s, logplot=False, **kwargs): if (logplot): maxval = log(abs(s).max()) minval = maxval - logrange logs = log(abs(s)) logs = where(logs > minval, logs - minval, 0) logs /= logs.max() src = mlab.pipeline.scalar_field(x, y, z, logs) else: maxval = abs(s).max() src = mlab.pipeline.scalar_field(x, y, z, abs(s) / maxval) vol = mlab.pipeline.volume(src) ctf = ColorTransferFunction() ctf.range = [0, 1] otf = PiecewiseFunction() otf.add_point((0, 0)) otf.add_point((1, 1)) otf.range = [0, 1] vol.otf = otf vol.volume_property.set_scalar_opacity(otf) return vol
def _vmin_changed(self): vmin = self.vmin vmax = self.vmax range_min, range_max = self._target.current_range if vmin is None: vmin = range_min if vmax is None: vmax = range_max # Change the opacity function from tvtk.util.ctf import PiecewiseFunction, save_ctfs otf = PiecewiseFunction() if range_min < vmin: otf.add_point(range_min, 0.) if range_max > vmax: otf.add_point(range_max, 0.2) otf.add_point(vmin, 0.) otf.add_point(vmax, 0.2) self._target._otf = otf self._target._volume_property.set_scalar_opacity(otf) if self.color is None and \ ((self.vmin is not None) or (self.vmax is not None)): # FIXME: We don't use 'rescale_ctfs' because it screws up the # nodes, this is because, the values are actually scaled between # the specified vmin/vmax and NOT the full range of values # specified in the CTF or in the volume object. if self.__last_vrange: last_min, last_max = self.__last_vrange else: last_min, last_max = range_min, range_max def _rescale_value(x): nx = (x - last_min) / (last_max - last_min) return vmin + nx * (vmax - vmin) # For some reason on older versions of VTK (< 8.1 at least), # The range trait is not updated correctly when the rgb points # are added, this causes problems so we explicitly update them. self._target._ctf.update_traits() scale_min, scale_max = self._target._ctf.range def _rescale_node(x): nx = (x - scale_min) / (scale_max - scale_min) return range_min + nx * (range_max - range_min) if hasattr(self._target._ctf, 'nodes'): rgb = list() for value in self._target._ctf.nodes: r, g, b = \ self._target._ctf.get_color(value) rgb.append((_rescale_node(value), r, g, b)) else: rgb = save_ctfs(self._target.volume_property)['rgb'] from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() try: ctf.range = (range_min, range_max) except Exception: # VTK versions < 5.2 don't seem to need this. pass rgb.sort() v = rgb[0] ctf.add_rgb_point(range_min, v[1], v[2], v[3]) for v in rgb: ctf.add_rgb_point(_rescale_value(v[0]), v[1], v[2], v[3]) ctf.add_rgb_point(range_max, v[1], v[2], v[3]) self._target._ctf = ctf self._target._volume_property.set_color(ctf) self.__last_vrange = vmin, vmax self._target.update_ctf = True
position=[0.81, 0.1], position2=[0.11,0.31]) mpf.change_surface_scalars(new_tube, surf_bar_label, 'vphi', lim=1.5) new_tube.parent.scalar_lut_manager.label_text_property.color = (1,1,1) slines.parent.scalar_lut_manager.number_of_labels = 4 surf_bar_label.property.color = text_color surf_bar_label.y_position = 0.05 surf_bar_label.x_position = 0.93 # Add GBand volume render vol = mlab.pipeline.volume(gband) vol.volume.mapper.blend_mode = 'maximum_intensity' #vol.volume.mapper.number_of_threads = 16 # Make a decent ctf and otf ctf = ColorTransferFunction() ctf.range = [0.1, 1] ctf.add_rgb_point(1, 1., 1, 0.01) ctf.add_rgb_point(0.85, 1., 0.8, 0.) ctf.add_rgb_point(0.6, 0.9, 0.3, 0.) ctf.add_rgb_point(0.4, 0.8, 0.0, 0.) ctf.add_rgb_point(0., 0.01, 0., 0.) otf = PiecewiseFunction() otf.add_point(1., 1.) otf.add_point(0.6, 0.9) otf.add_point(0.2, 0.) otf.add_point(0., 0.) vol._volume_property.set_color(ctf) vol._ctf = ctf vol._otf = otf
def potential3d(self, mode='', export_figure=True): """ Creates a 3D interactive ESP potential plot. :param mode: mode of the plot: '', 'iso_surface', 'contour' :param export_figure: boolean, whether to save an image or not :raises: ValueError when the visualization mode is invalid """ from mayavi import mlab potential = self.electron_potential if mode == '': grid = mlab.pipeline.scalar_field(potential) min = potential.min() negative_steps = np.percentile(potential[potential < 0], [2.0, 3.0, 7.5]) positive_steps = np.percentile(potential[potential > 0], [92.5, 97.0, 98.0]) max = potential.max() vol = mlab.pipeline.volume(grid, vmin=min, vmax=max) from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() ctf.add_rgb_point(min, 1, 0.3, 0.3) # numbers are r,g,b in [0;1] ctf.add_rgb_point(negative_steps[1], 1, 0.3, 0.3) ctf.add_rgb_point(negative_steps[2], 1, 1, 1) ctf.add_rgb_point(positive_steps[0], 1, 1, 1) ctf.add_rgb_point(positive_steps[1], 0.3, 0.3, 1) ctf.add_rgb_point(max, 0.3, 0.3, 1) ctf.range = np.asarray([min, max]) vol._volume_property.set_color(ctf) vol._ctf = ctf vol.update_ctf = True # Changing the otf: from tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() otf.add_point(min, 1.0) otf.add_point(negative_steps[1], 1.0) otf.add_point(negative_steps[2], 0.0) otf.add_point(positive_steps[0], 0.0) otf.add_point(positive_steps[1], 1.0) otf.add_point(max, 1.0) vol._otf = otf vol._volume_property.set_scalar_opacity(otf) mlab.axes() elif mode == 'iso_surface': source = mlab.pipeline.scalar_field(potential) clip = mlab.pipeline.data_set_clipper(source) mlab.pipeline.iso_surface(clip) elif mode == 'contour': n = self.electron_potential.shape[0] range = 100 x, y, z = np.mgrid[-range / 2:range / 2:complex(n), -range / 2:range / 2:complex(n), -range / 2:range / 2:complex(n)] mlab.contour3d(x, y, z, self.electron_potential, contours=10, opacity=0.5) else: log.error("The visualisation mode of the electrostatic potential" " can be one of ['', 'iso_surface', 'contour']") raise ValueError if export_figure: mlab.savefig(filename=os.path.join( self.figures_dir, '{0}_elstpot3d.png'.format( self.molecule_name))) mlab.show()
new_tube, surf_bar, surf_bar_label = mpf.draw_surface(surf_poly,'RdBu',lim=0.4, position=[0.81, 0.1], position2=[0.11,0.31]) mpf.change_surface_scalars(new_tube, surf_bar_label, 'vphi', lim=1.5) new_tube.parent.scalar_lut_manager.label_text_property.color = (1,1,1) surf_bar_label.property.color = text_color #surf_bar_label.y_position = 0.05 surf_bar_label.x_position = 0.93 # Add GBand volume render vol = mlab.pipeline.volume(gband) vol.volume.mapper.blend_mode = 'maximum_intensity' # Make a decent ctf and otf ctf = ColorTransferFunction() ctf.range = [0, 1] ctf.add_rgb_point(1, 1., 1, 0.01) ctf.add_rgb_point(0.85, 1., 0.8, 0.) ctf.add_rgb_point(0.6, 0.9, 0.3, 0.) ctf.add_rgb_point(0.4, 0.8, 0.0, 0.) ctf.add_rgb_point(0., 0.01, 0., 0.) otf = PiecewiseFunction() otf.add_point(1., 1.) otf.add_point(0.6, 0.9) otf.add_point(0.2, 0.) otf.add_point(0., 0.) vol._volume_property.set_color(ctf) vol._ctf = ctf vol._otf = otf