def changeVolumeColormap(vol,color): '''change the ctf and otf property of volume Parameters ---------- vol : mlab.volume color : [[]] For each color [datavalue, R, G, B, A] R,G,B,A in range(0.0,255.0) inclusive ''' # Changing the ctf: from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() for c in color: ctf.add_rgb_point(float(c[0]), float(c[1])/255.0, float(c[2])/255.0, float(c[3])/255.0) vol._volume_property.set_color(ctf) vol._ctf = ctf vol.update_ctf = True # Changing the otf from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() for c in color: otf.add_point(float(c[0]), float(c[4])/255.0) vol._otf = otf vol._volume_property.set_scalar_opacity(otf)
def change_volume_property(vol): color1 = [[0,0,0,0,0], [1,255,255,0,10], [10,255,255,0,50], [100,255,100,0,150], [1000,255,0,0,255]] color2 = [[0,0,0,0,0], [100,0,255,255,10], [1000,0,255,255,50], [10000,0,100,255,150], [100000,0,0,255,255]] color = color1 # Changing the ctf: from tvtk.util.ctf import ColorTransferFunction ctf = ColorTransferFunction() for c in color: ctf.add_rgb_point(c[0], c[1]/255.0, c[2]/255.0, c[3]/255.0) vol._volume_property.set_color(ctf) vol._ctf = ctf vol.update_ctf = True # Changing the otf from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() for c in color: otf.add_point(c[0], c[4]/255.0) vol._otf = otf vol._volume_property.set_scalar_opacity(otf)
def change_test(vol): from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() otf.add_point(0, 0) otf.add_point(1000,1) vol._otf = otf vol._volume_property.set_scalar_opacity(otf)
def default_OTF(x1, x2): """Creates a default opacity transfer function. """ maxs = max(x1, x2) mins = min(x1, x2) otf = PiecewiseFunction() otf.add_point(mins, 0.0) otf.add_point(maxs, 0.2) return otf
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 enthought.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 enthought.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 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 show_volume(D, cm="Spectral", minfact=0.1, maxfact=0.9, visible=True, normalize=True): print "Show volume" mind = D.min() D -= D.min() src = mlab.pipeline.scalar_field(D) ptpd = D.ptp() if normalize: R = (mind + minfact * ptpd, mind + maxfact * ptpd) else: R = (minfact - mind, maxfact - mind) v = mlab.pipeline.volume(src, vmin=R[0], vmax=R[1]) if False and not (cm == "Spectral"): ctf = ColorTransferFunction() ctf.range = R ctf.add_rgb_point(mind, 1, 1, 1) ctf.add_rgb_point(R[0], 1, 1, 1) ctf.add_rgb_point(R[1], 0, 0, 0) ctf.add_rgb_point(mind + ptpd, 0, 0, 0) v._volume_property.set_color(ctf) v._ctf = ctf v.update_ctf = True if False: from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() otf.add_point(mind, 0) otf.add_point(R[0], 0) #otf.add_point(R[0]+0.1*ptpd, 0.1) #otf.add_point(R[0]+0.2*ptpd, 0.3) #otf.add_point(R[0]+0.3*ptpd, 0.5) otf.add_point(R[0] + 0.2 * ptpd, 0.7) otf.add_point(R[1], 1.0) otf.add_point(mind + ptpd, 1.0) v._otf = otf v._volume_property.set_scalar_opacity(otf) v.update_ctf = True v.volume_mapper.cropping_region_planes = np.array( [0., 1., 0., 1., 0., 1.]) v.volume_mapper.lock_sample_distance_to_input_spacing = True print "done"
def show_volume(D, cm="Spectral", minfact=0.1, maxfact=0.9,visible=True, normalize=True): print "Show volume" mind = D.min() D -= D.min() src = mlab.pipeline.scalar_field(D) ptpd = D.ptp() if normalize: R = (mind+minfact*ptpd, mind+maxfact*ptpd) else: R = (minfact-mind, maxfact-mind) v = mlab.pipeline.volume(src, vmin=R[0],vmax=R[1]) if False and not (cm == "Spectral"): ctf = ColorTransferFunction() ctf.range = R ctf.add_rgb_point(mind, 1,1,1) ctf.add_rgb_point(R[0], 1,1,1) ctf.add_rgb_point(R[1], 0,0,0) ctf.add_rgb_point(mind+ptpd, 0,0,0) v._volume_property.set_color(ctf) v._ctf = ctf v.update_ctf = True if False: from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() otf.add_point(mind, 0) otf.add_point(R[0], 0) #otf.add_point(R[0]+0.1*ptpd, 0.1) #otf.add_point(R[0]+0.2*ptpd, 0.3) #otf.add_point(R[0]+0.3*ptpd, 0.5) otf.add_point(R[0]+0.2*ptpd, 0.7) otf.add_point(R[1], 1.0) otf.add_point(mind+ptpd, 1.0) v._otf = otf v._volume_property.set_scalar_opacity(otf) v.update_ctf = True v.volume_mapper.cropping_region_planes = np.array([ 0., 1., 0., 1., 0., 1.]) v.volume_mapper.lock_sample_distance_to_input_spacing = True print "done"
def render_fmri(volume): data = np.zeros(mask.shape) data[mask] = volume data *= blurred_data fmri_src = viz3d.affine_img_src(data, fmri_affine, name='fMRI') vol = mlab.pipeline.volume(fmri_src) # Change the opacity function from enthought.tvtk.util.ctf import PiecewiseFunction width = .1 otf = PiecewiseFunction() otf.add_point(-FMRI_MAX, .2) otf.add_point(-width*FMRI_MAX, 0.) otf.add_point(width*FMRI_MAX, 0.) otf.add_point(FMRI_MAX, .2) vol._volume_property.set_scalar_opacity(otf) vol.update_ctf = True return fmri_src
# Use a GeometryFilter to cut out a slab geom = mlab.pipeline.user_defined(edges, filter='GeometryFilter') geom.filter.extent = [-0.8*extent, 0.8*extent, -0.2*extent, 0.2*extent, -0.8*extent, 0.8*extent, ] geom.filter.extent_clipping = True # Display connections in the selected slab as thick tubes. mlab.pipeline.surface(mlab.pipeline.tube(geom, tube_radius=0.25, tube_sides=10, ), colormap='Paired') # Display the scalar field with volume rendering. vol = mlab.pipeline.volume(delaunay) # Change the opacity transfer function from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() otf.add_point(0, 0) otf.add_point(N, 0.02) vol._otf = otf vol._volume_property.set_scalar_opacity(otf) # And now choose a view, and re-enable rendering. mlab.view(166, 80, 82) fig.scene.disable_render = False mlab.show()
def plot_charge_density_fog( path, UnitCell=False, opacity=[0, 1], view=[-90, 90], SaveFig=True, filepath="images/fog.png" ): with jasp(path) as calc: atoms = calc.get_atoms() x, y, z, cd = calc.get_charge_density() # making a black background mlab.figure(bgcolor=(0, 0, 0)) # plotting the atoms as spheres, for atom in atoms: mlab.points3d( atom.x, atom.y, atom.z, scale_factor=vdw_radii[atom.number] / 5.0, resolution=20, # a tuple is required for the color color=tuple(cpk_colors[atom.number]), scale_mode="none", ) if UnitCell == True: # draw the unit cell - there are 8 corners, and 12 connections a1, a2, a3 = atoms.get_cell() origin = [0, 0, 0] cell_matrix = [ [origin, a1], [origin, a2], [origin, a3], [a1, a1 + a2], [a1, a1 + a3], [a2, a2 + a1], [a2, a2 + a3], [a3, a1 + a3], [a3, a2 + a3], [a1 + a2, a1 + a2 + a3], [a2 + a3, a1 + a2 + a3], [a1 + a3, a1 + a3 + a2], ] for p1, p2 in cell_matrix: mlab.plot3d( [p1[0], p2[0]], # x-positions [p1[1], p2[1]], # y-positions [p1[2], p2[2]], # z-positions tube_radius=0.02, ) # plotting the charge density source = mlab.pipeline.scalar_field(x, y, z, cd) vmin = cd.min() vmax = cd.max() vol = mlab.pipeline.volume(source) # Changing the otf from enthought.tvtk.util.ctf import PiecewiseFunction otf = PiecewiseFunction() otf.add_point(vmin + 0.1 * (vmax - vmin), opacity[0]) otf.add_point(vmin + 0.8 * (vmax - vmin), opacity[1]) vol._otf = otf vol._volume_property.set_scalar_opacity(otf) # view adjusted by iteration mlab.view(azimuth=view[0], elevation=view[1], distance="auto") if SaveFig == True: mlab.savefig(filepath) mlab.show()
(ndimage.morphology.binary_fill_holes( ndimage.gaussian_filter( (anat_data > 4800).astype(np.float), 6) > 0.5 )).astype(np.float), 2) # The cortex shell vol = mlab.pipeline.volume(anat_src, color=(1, 1, 1)) # Change the opacity function from enthought.tvtk.util.ctf import PiecewiseFunction shell_size = .1 otf = PiecewiseFunction() otf.add_point(0, 0) otf.add_point((.5-shell_size)*anat_vmax, 0.) # If black background, use 0.2, if white, use .15 otf.add_point(.5*anat_vmax, 0.2) otf.add_point((.5+shell_size)*anat_vmax, 0.) otf.add_point(anat_vmax, 0) vol._volume_property.set_scalar_opacity(otf) vol.update_ctf = True mlab.view(25, 70, 310, (1.3, -16.1, 3.27)) mlab.savefig('glass_brain.png', size=(960/REDUCE, 768/REDUCE)) ################################################################################ # Render activation filenames = sorted(glob.glob( '/volatile/varoquau/data/data/subject1/functional/fMRI/session1/swfga*.hdr'