diff --git a/dipy/viz/colormap.py b/dipy/viz/colormap.py index fcb85378c7..9d50715216 100644 --- a/dipy/viz/colormap.py +++ b/dipy/viz/colormap.py @@ -117,7 +117,7 @@ def boys2rgb(v): hh5 = 6 * xy / 5.5 hh6 = (1 / 1.176) * .125 * (35 * z4 - 30 * z2 + 3) hh7 = 2.5 * x * (7 * z3 - 3 * z) / 3.737 - hh8 = 2.5 * y * (7 * z3 - 3 * z)/3.737 + hh8 = 2.5 * y * (7 * z3 - 3 * z) / 3.737 hh9 = ((x2 - y2) * 7.5 * (7 * z2 - 1)) / 15.85 hh10 = ((2 * xy) * (7.5 * (7 * z2 - 1))) / 15.85 hh11 = 105 * (4 * x3 * z - 3 * xz * (1 - z2)) / 59.32 @@ -149,7 +149,7 @@ def boys2rgb(v): X = X + hh8 * ss67 X = X + hh10 * cc89 - X = X + hh9 * ss89 + X = X + hh9 * ss89 Y = 0.0 @@ -227,7 +227,7 @@ def orient2rgb(v): if v.ndim == 2: orientn = np.sqrt(v[:, 0] ** 2 + v[:, 1] ** 2 + v[:, 2] ** 2) - orientn.shape = orientn.shape+(1,) + orientn.shape = orientn.shape + (1,) orient = np.abs(v / orientn) return orient diff --git a/dipy/viz/fvtk.py b/dipy/viz/fvtk.py index 57b64eac8f..58fca85b5c 100644 --- a/dipy/viz/fvtk.py +++ b/dipy/viz/fvtk.py @@ -1,8 +1,10 @@ ''' Fvtk module implements simple visualization functions using VTK. The main idea is the following: -A window can have one or more renderers. A renderer can have none, one or more actors. Examples of actors are a sphere, line, point etc. -You basically add actors in a renderer and in that way you can visualize the forementioned objects e.g. sphere, line ... +A window can have one or more renderers. A renderer can have none, +one or more actors. Examples of actors are a sphere, line, point etc. +You basically add actors in a renderer and in that way you can +visualize the forementioned objects e.g. sphere, line ... Examples --------- @@ -284,7 +286,8 @@ def volume(vol, voxsz=(1.0, 1.0, 1.0), affine=None, center_origin=1, As given by volumeimages. center_origin : int {0,1} It considers that the center of the volume is the - point ``(-vol.shape[0]/2.0+0.5,-vol.shape[1]/2.0+0.5,-vol.shape[2]/2.0+0.5)``. + point ``(-vol.shape[0]/2.0+0.5,-vol.shape[1]/2.0+0.5, + -vol.shape[2]/2.0+0.5)``. info : int {0,1} If 1 it prints out some info about the volume, the method and the dataset. @@ -292,7 +295,8 @@ def volume(vol, voxsz=(1.0, 1.0, 1.0), affine=None, center_origin=1, Use trilinear interpolation, default 1, gives smoother rendering. If you want faster interpolation use 0 (Nearest). maptype : int {0,1} - The maptype is a very important parameter which affects the raycasting algorithm in use for the rendering. + The maptype is a very important parameter which affects the + raycasting algorithm in use for the rendering. The options are: If 0 then vtkVolumeTextureMapper2D is used. If 1 then vtkVolumeRayCastFunction is used. @@ -437,8 +441,12 @@ def volume(vol, voxsz=(1.0, 1.0, 1.0), affine=None, center_origin=1, if affine is not None: aff = vtk.vtkMatrix4x4() - aff.DeepCopy((affine[0, 0], affine[0, 1], affine[0, 2], affine[0, 3], affine[1, 0], affine[1, 1], affine[1, 2], affine[1, 3], affine[2, 0], affine[ - 2, 1], affine[2, 2], affine[2, 3], affine[3, 0], affine[3, 1], affine[3, 2], affine[3, 3])) + aff.DeepCopy((affine[0, 0], affine[0, 1], affine[0, 2], + affine[0, 3], affine[1, 0], affine[1, 1], + affine[1, 2], affine[1, 3], affine[2, 0], + affine[2, 1], affine[2, 2], affine[2, 3], + affine[3, 0], affine[3, 1], affine[3, 2], + affine[3, 3])) # aff.DeepCopy((affine[0,0],affine[0,1],affine[0,2],0,affine[1,0],affine[1,1],affine[1,2],0,affine[2,0],affine[2,1],affine[2,2],0,affine[3,0],affine[3,1],affine[3,2],1)) # aff.DeepCopy((affine[0,0],affine[0,1],affine[0,2],127.5,affine[1,0],affine[1,1],affine[1,2],-127.5,affine[2,0],affine[2,1],affine[2,2],-127.5,affine[3,0],affine[3,1],affine[3,2],1)) @@ -472,7 +480,9 @@ def volume(vol, voxsz=(1.0, 1.0, 1.0), affine=None, center_origin=1, # changeFilter.SetInput(im) if center_origin: changeFilter.SetOutputOrigin( - -vol.shape[0] / 2.0 + 0.5, -vol.shape[1] / 2.0 + 0.5, -vol.shape[2] / 2.0 + 0.5) + -vol.shape[0] / 2.0 + 0.5, + -vol.shape[1] / 2.0 + 0.5, + -vol.shape[2] / 2.0 + 0.5) print('ChangeFilter ', changeFilter.GetOutputOrigin()) opacity = vtk.vtkPiecewiseFunction() @@ -673,7 +683,9 @@ def contour(vol, voxsz=(1.0, 1.0, 1.0), affine=None, levels=[50], return ass -lowercase_cm_name = {'blues':'Blues', 'accent':'Accent'} +lowercase_cm_name = {'blues': 'Blues', 'accent': 'Accent'} + + def create_colormap(v, name='jet', auto=True): """Create colors from a specific colormap and return it as an array of shape (N,3) where every row gives the corresponding @@ -907,14 +919,15 @@ def peaks(peaks_dirs, peaks_values=None, scale=2.2, colors=(1, 0, 0)): pv = 1. symm = np.vstack((-peaks_dirs[ijk][i] * pv + xyz, - peaks_dirs[ijk][i] * pv + xyz)) + peaks_dirs[ijk][i] * pv + xyz)) list_dirs.append(symm) return line(list_dirs, colors) -def tensor(evals, evecs, scalar_colors=None, sphere=None, scale=2.2, norm=True): +def tensor(evals, evecs, scalar_colors=None, + sphere=None, scale=2.2, norm=True): """Plot many tensors as ellipsoids simultaneously. Parameters diff --git a/dipy/viz/projections.py b/dipy/viz/projections.py index 91035b7ac7..43db3e5e64 100644 --- a/dipy/viz/projections.py +++ b/dipy/viz/projections.py @@ -8,16 +8,14 @@ import numpy as np import scipy.interpolate as interp from ..utils.optpkg import optional_package +import dipy.core.geometry as geo +from dipy.testing import doctest_skip_parser matplotlib, has_mpl, setup_module = optional_package("matplotlib") plt, _, _ = optional_package("matplotlib.pyplot") tri, _, _ = optional_package("matplotlib.tri") bm, has_basemap, _ = optional_package("mpl_toolkits.basemap") -import dipy.core.geometry as geo - -from dipy.testing import doctest_skip_parser - @doctest_skip_parser def sph_project(vertices, val, ax=None, vmin=None, vmax=None, cmap=None, @@ -45,7 +43,8 @@ def sph_project(vertices, val, ax=None, vmin=None, vmax=None, cmap=None, triang : Whether to display the plot triangulated as a pseudo-color plot. - boundary : Whether to draw the boundary around the projection in a black line + boundary : Whether to draw the boundary around the projection + in a black line Returns ------- @@ -76,10 +75,12 @@ def sph_project(vertices, val, ax=None, vmin=None, vmax=None, cmap=None, m.drawmapboundary() # Rotate the coordinate system so that you are looking from the north pole: - verts_rot = np.array(np.dot(np.matrix([[0,0,-1],[0,1,0],[1,0,0]]), vertices)) + verts_rot = np.array( + np.dot(np.matrix([[0, 0, -1], [0, 1, 0], [1, 0, 0]]), vertices)) - # To get the orthographic projection, when the first coordinate is positive: - neg_idx = np.where(verts_rot[0]>0) + # To get the orthographic projection, when the first coordinate is + # positive: + neg_idx = np.where(verts_rot[0] > 0) # rotate the entire bvector around to point in the other direction: verts_rot[:, neg_idx] *= -1 @@ -102,18 +103,18 @@ def sph_project(vertices, val, ax=None, vmin=None, vmax=None, cmap=None, else: cmap_data = cmap._segmentdata red_interp, blue_interp, green_interp = ( - interp.interp1d(np.array(cmap_data[gun])[:,0], - np.array(cmap_data[gun])[:,1]) for gun in - ['red', 'blue','green']) + interp.interp1d(np.array(cmap_data[gun])[:, 0], + np.array(cmap_data[gun])[:, 1]) for gun in + ['red', 'blue', 'green']) - r = (val - my_min)/float(my_max-my_min) + r = (val - my_min) / float(my_max - my_min) # Enforce the maximum and minumum boundaries, if there are values # outside those boundaries: - r[r<0]=0 - r[r>1]=1 + r[r < 0] = 0 + r[r > 1] = 1 - for this_x, this_y, this_r in zip(x,y,r): + for this_x, this_y, this_r in zip(x, y, r): red = red_interp(this_r) blue = blue_interp(this_r) green = green_interp(this_r) @@ -127,8 +128,7 @@ def sph_project(vertices, val, ax=None, vmin=None, vmax=None, cmap=None, pos = ax.get_position() l, b, w, h = pos.bounds # setup colorbar axes - cax = fig.add_axes([l+w+0.075, b, 0.05, h], frameon=False) - fig.colorbar(mappable, cax=cax) # draw colorbar + cax = fig.add_axes([l + w + 0.075, b, 0.05, h], frameon=False) + fig.colorbar(mappable, cax=cax) # draw colorbar return ax - diff --git a/dipy/viz/regtools.py b/dipy/viz/regtools.py index 1fcb358c4c..74a7efee7a 100644 --- a/dipy/viz/regtools.py +++ b/dipy/viz/regtools.py @@ -17,6 +17,7 @@ def _tile_plot(imgs, titles, **kwargs): return fig + def overlay_images(img0, img1, title0='', title_mid='', title1='', fname=None): r""" Plot two images one on top of the other using red and green channels. @@ -49,8 +50,8 @@ def overlay_images(img0, img1, title0='', title_mid='', title1='', fname=None): image is not saved. """ # Normalize the input images to [0,255] - img0 = 255*((img0 - img0.min()) / (img0.max() - img0.min())) - img1 = 255*((img1 - img1.min()) / (img1.max() - img1.min())) + img0 = 255 * ((img0 - img0.min()) / (img0.max() - img0.min())) + img1 = 255 * ((img1 - img1.min()) / (img1.max() - img1.min())) # Create the color images img0_red = np.zeros(shape=(img0.shape) + (3,), dtype=np.uint8) @@ -65,7 +66,7 @@ def overlay_images(img0, img1, title0='', title_mid='', title1='', fname=None): overlay[..., 1] = img1 fig = _tile_plot([img0_red, overlay, img1_green], - [title0, title_mid, title1]) + [title0, title_mid, title1]) # If a file name was given, save the figure if fname is not None: @@ -73,6 +74,7 @@ def overlay_images(img0, img1, title0='', title_mid='', title1='', fname=None): return fig + def draw_lattice_2d(nrows, ncols, delta): r"""Create a regular lattice of nrows x ncols squares. @@ -99,7 +101,7 @@ def draw_lattice_2d(nrows, ncols, delta): C = 1 + (delta + 1) * ncols """ lattice = np.ndarray((1 + (delta + 1) * nrows, - 1 + (delta + 1) * ncols), + 1 + (delta + 1) * ncols), dtype=np.float64) # Fill the lattice with "white" @@ -107,7 +109,7 @@ def draw_lattice_2d(nrows, ncols, delta): # Draw the horizontal lines in "black" for i in range(nrows + 1): - lattice[i*(delta + 1), :] = 0 + lattice[i * (delta + 1), :] = 0 # Draw the vertical lines in "black" for j in range(ncols + 1): @@ -278,7 +280,8 @@ def plot_slices(V, slice_indices=None, fname=None): V : array, shape (S, R, C) the 3D volume to extract the slices from slice_indices : array, shape (3,) (optional) - the indices of the sagital (slice_indices[0]), coronal (slice_indices[1]) + the indices of the sagital (slice_indices[0]), coronal + (slice_indices[1]) and axial (slice_indices[2]) slices to be displayed. If None, the middle slices along each direction are displayed. fname : string (optional) @@ -286,7 +289,7 @@ def plot_slices(V, slice_indices=None, fname=None): figure is not saved to disk. """ if slice_indices is None: - slice_indices = np.array(V.shape)//2 + slice_indices = np.array(V.shape) // 2 # Normalize the intensities to [0, 255] sh = V.shape @@ -299,8 +302,8 @@ def plot_slices(V, slice_indices=None, fname=None): sagittal = np.asarray(V[slice_indices[0], :, :]).astype(np.uint8).T fig = _tile_plot([axial, coronal, sagittal], - ['Axial', 'Coronal', 'Sagittal'], - cmap=plt.cm.gray, origin='lower') + ['Axial', 'Coronal', 'Sagittal'], + cmap=plt.cm.gray, origin='lower') # Save the figure if requested if fname is not None: @@ -308,6 +311,7 @@ def plot_slices(V, slice_indices=None, fname=None): return fig + def overlay_slices(L, R, slice_index=None, slice_type=1, ltitle='Left', rtitle='Right', fname=None): r"""Plot three overlaid slices from the given volumes. @@ -354,19 +358,19 @@ def overlay_slices(L, R, slice_index=None, slice_type=1, ltitle='Left', # the slices (note the transpositions) if slice_type is 0: if slice_index is None: - slice_index = sh[0]//2 + slice_index = sh[0] // 2 colorImage = np.zeros(shape=(sh[2], sh[1], 3), dtype=np.uint8) ll = np.asarray(L[slice_index, :, :]).astype(np.uint8).T rr = np.asarray(R[slice_index, :, :]).astype(np.uint8).T elif slice_type is 1: if slice_index is None: - slice_index = sh[1]//2 + slice_index = sh[1] // 2 colorImage = np.zeros(shape=(sh[2], sh[0], 3), dtype=np.uint8) ll = np.asarray(L[:, slice_index, :]).astype(np.uint8).T rr = np.asarray(R[:, slice_index, :]).astype(np.uint8).T elif slice_type is 2: if slice_index is None: - slice_index = sh[2]//2 + slice_index = sh[2] // 2 colorImage = np.zeros(shape=(sh[1], sh[0], 3), dtype=np.uint8) ll = np.asarray(L[:, :, slice_index]).astype(np.uint8).T rr = np.asarray(R[:, :, slice_index]).astype(np.uint8).T @@ -382,8 +386,8 @@ def overlay_slices(L, R, slice_index=None, slice_type=1, ltitle='Left', colorImage[..., 1] = rr * (rr > rr[0, 0]) fig = _tile_plot([ll, colorImage, rr], - [ltitle, 'Overlay', rtitle], - cmap=plt.cm.gray, origin='lower') + [ltitle, 'Overlay', rtitle], + cmap=plt.cm.gray, origin='lower') # Save the figure to disk, if requested if fname is not None: diff --git a/dipy/viz/widget.py b/dipy/viz/widget.py index d91724d71d..45cffc101d 100644 --- a/dipy/viz/widget.py +++ b/dipy/viz/widget.py @@ -18,7 +18,7 @@ def slider(iren, ren, callback, min_value=0, max_value=255, value=125, size=(50, 0), label_format="%0.0lf", color=(0.5, 0.5, 0.5), - selected_color = (0.9, 0.2, 0.1)): + selected_color=(0.9, 0.2, 0.1)): """ A 2D slider widget Parameters @@ -133,8 +133,10 @@ def get_value(self): def _place_widget(obj, event): slider.place(ren) - iren.GetRenderWindow().AddObserver(vtk.vtkCommand.StartEvent, _place_widget) - iren.GetRenderWindow().AddObserver(vtk.vtkCommand.ModifiedEvent, _place_widget) + iren.GetRenderWindow().AddObserver( + vtk.vtkCommand.StartEvent, _place_widget) + iren.GetRenderWindow().AddObserver( + vtk.vtkCommand.ModifiedEvent, _place_widget) return slider @@ -218,8 +220,10 @@ def place(self, renderer): def _place_widget(obj, event): button.place(ren) - iren.GetRenderWindow().AddObserver(vtk.vtkCommand.StartEvent, _place_widget) - iren.GetRenderWindow().AddObserver(vtk.vtkCommand.ModifiedEvent, _place_widget) + iren.GetRenderWindow().AddObserver( + vtk.vtkCommand.StartEvent, _place_widget) + iren.GetRenderWindow().AddObserver( + vtk.vtkCommand.ModifiedEvent, _place_widget) return button @@ -315,8 +319,10 @@ def place(self, renderer): def _place_widget(obj, event): text_widget.place(ren) - iren.GetRenderWindow().AddObserver(vtk.vtkCommand.StartEvent, _place_widget) - iren.GetRenderWindow().AddObserver(vtk.vtkCommand.ModifiedEvent, _place_widget) + iren.GetRenderWindow().AddObserver( + vtk.vtkCommand.StartEvent, _place_widget) + iren.GetRenderWindow().AddObserver( + vtk.vtkCommand.ModifiedEvent, _place_widget) text_widget.On()