def test_rescale_ctf(self): """Test rescaling a CTF.""" # Expected data. evp, ectf, eotf = make_volume_prop(0.0, 1.0) edata = save_ctfs(evp) # Rescaled data. ctf, otf = rescale_ctfs(self.vp, (0.0, 1.0)) data = save_ctfs(self.vp) # check that both the data are identical. self.assertEqual(edata, data)
def test_save_load_ctf(self): """Test saving and loading of a CTF.""" # Create a default ctf, save it. data = save_ctfs(self.vp) # load it into another volume property, mvp = tvtk.VolumeProperty() ctf = load_ctfs(data, mvp) # get the data from the new one mdata = save_ctfs(mvp) # check that both the data are identical. self.assertEqual(mdata, data)
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 _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
def __get_pure_state__(self): d = super(Volume, self).__get_pure_state__() d['ctf_state'] = save_ctfs(self._volume_property) for name in ('current_range', '_ctf', '_otf'): d.pop(name, None) return d