def add_volume(self, volume, cmap='afmhot_r', alpha=1, add_colorbar=True, **kwargs): """ Renders intensitdata from a 3D numpy array as a lego volumetric actor. :param volume: np 3D array with number of dimensions = those of the 100um reference space. :param cmap: str with name of colormap to use :param alpha: float, transparency :param add_colorbar: if True a colorbar is added to show the values of the colormap """ # Parse kwargs line_width = kwargs.pop('line_width', 1) if cmap == 'random' or not cmap or cmap is None: cmap = get_random_colormap() # Get vmin and vmax threshold for visualisation vmin = kwargs.pop('vmin', 0.000001) vmax = kwargs.pop('vmax', np.nanmax(volume)) # Check values if np.max(volume) > vmax: print( "While rendering mapped projection some of the values are above the vmax threshold." + "They will not be displayed." + f" vmax was {vmax} but found value {round(np.max(volume), 5)}." ) if vmin > vmax: raise ValueError( f'The vmin threhsold [{vmin}] cannot be larger than the vmax threshold [{vmax}' ) if vmin < 0: vmin = 0 # Get 'lego' actor vol = Volume(volume) lego = vol.legosurface(vmin=vmin, vmax=vmax, cmap=cmap) # Scale and color actor lego.alpha(alpha).lw(line_width).scale(self.voxel_size) lego.cmap = cmap # Add colorbar if add_colorbar: lego.addScalarBar(vmin=vmin, vmax=vmax, horizontal=1, c='k', pos=(0.05, 0.05), titleFontSize=40) # Add to scene actor = self.scene.add_vtkactor(lego) return actor
iteration = 4 size = 3**iteration voxels = np.ones((size, size, size)) def iterate(length, x, y, z): nl = length // 3 if nl < 1: return margin = (nl - 1) // 2 voxels[z - margin:z + margin + 1, y - margin:y + margin + 1, :] = 0 voxels[z - margin:z + margin + 1, :, x - margin:x + margin + 1] = 0 voxels[:, y - margin:y + margin + 1, x - margin:x + margin + 1] = 0 for ix, iy, iz in np.ndindex((3, 3, 3)): if (1 if ix != 1 else 0) + (1 if iy != 1 else 0) + (1 if iz != 1 else 0) != 2: iterate(nl, x + (ix - 1) * nl, y + (iy - 1) * nl, z + (iz - 1) * nl) iterate(size, size // 2, size // 2, size // 2) print('voxels min, max =', np.min(voxels), np.max(voxels)) vol = Volume(voxels) lego = vol.legosurface(-0.1, 1.1, cmap='afmhot_r') show(vol, lego, N=2, bg='w')