From 56d7bda2dd2a59b066642f296638d66afae26926 Mon Sep 17 00:00:00 2001 From: arokem Date: Sat, 4 Aug 2012 11:27:56 -0700 Subject: [PATCH] RF: Make the function definitions depend on has_mpl. --- dipy/viz/projections.py | 147 +++++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 71 deletions(-) diff --git a/dipy/viz/projections.py b/dipy/viz/projections.py index eb76d0e240..15757a448d 100644 --- a/dipy/viz/projections.py +++ b/dipy/viz/projections.py @@ -12,105 +12,110 @@ import matplotlib import matplotlib.pyplot as plt import matplotlib.tri as tri + has_mpl = True except ImportError: e_s = "You do not have Matplotlib installed. Some visualization functions" e_s += " might not work for you." print(e_s) + has_mpl=False import dipy.core.geometry as geo -def sph_project(vertices, val, ax=None, vmin=None, vmax=None, - cmap=matplotlib.cm.hot, cbar=True, triang=False): +# Only define these functions if matplotlib is present, otherwise, this module +# returns 'empty': +if has_mpl: + def sph_project(vertices, val, ax=None, vmin=None, vmax=None, + cmap=matplotlib.cm.hot, cbar=True, triang=False): - """Draw a signal on a 2D projection of the sphere. + """Draw a signal on a 2D projection of the sphere. - Parameters - ---------- + Parameters + ---------- - vertices : (N,3) ndarray - unit vector points of the sphere + vertices : (N,3) ndarray + unit vector points of the sphere - val: (N) ndarray - Function values. + val: (N) ndarray + Function values. - ax : mpl axis, optional - If specified, draw onto this existing axis instead. + ax : mpl axis, optional + If specified, draw onto this existing axis instead. - vmin, vmax: floats - Values to cut the z + vmin, vmax: floats + Values to cut the z - cmap: mpl colormap + cmap: mpl colormap - cbar: Whether to add the color-bar to the figure + cbar: Whether to add the color-bar to the figure - triang: Whether to display the plot triangulated as a pseudo-color plot. + triang: Whether to display the plot triangulated as a pseudo-color plot. - Returns - ------- - fig : figure - Matplotlib figure + Returns + ------- + fig : figure + Matplotlib figure - Examples - -------- - >>> from dipy.data import get_sphere - >>> verts,faces=get_sphere('symmetric724') - >>> sph_project(verts,np.random.rand(len(verts))) + Examples + -------- + >>> from dipy.data import get_sphere + >>> verts,faces=get_sphere('symmetric724') + >>> sph_project(verts,np.random.rand(len(verts))) - """ - if ax is None: - _, ax = plt.subplots(1) - fig = ax.get_figure() + """ + if ax is None: + _, ax = plt.subplots(1) + fig = ax.get_figure() - x = vertices[:, 0] - y = vertices[:, 1] + x = vertices[:, 0] + y = vertices[:, 1] - my_min = np.nanmin(val) - if vmin is not None: - my_min = vmin + my_min = np.nanmin(val) + if vmin is not None: + my_min = vmin - my_max = np.nanmax(val) - if vmax is not None: - my_max = vmax + my_max = np.nanmax(val) + if vmax is not None: + my_max = vmax - 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 + # Enforce the maximum and minumum boundaries, if there are values + # outside those boundaries: + r[r<0]=0 + r[r>1]=1 - if triang: - triang = tri.Triangulation(x, y) - ax.tripcolor(triang, r, cmap=cmap) - 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']) + if triang: + triang = tri.Triangulation(x, y) + ax.tripcolor(triang, r, cmap=cmap) + 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']) - 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) - ax.plot(this_x, this_y, 'o', - c=[red.item(), green.item(), blue.item()]) + 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) + ax.plot(this_x, this_y, 'o', + c=[red.item(), green.item(), blue.item()]) - ax.set_aspect('equal') - ax.set_axis_off() - if cbar: - mappable = matplotlib.cm.ScalarMappable(cmap=cmap) - mappable.set_array([my_min, my_max]) - # setup colorbar axes instance. - 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 + ax.set_aspect('equal') + ax.set_axis_off() + if cbar: + mappable = matplotlib.cm.ScalarMappable(cmap=cmap) + mappable.set_array([my_min, my_max]) + # setup colorbar axes instance. + 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 - ax.set_xlim([-1.1, 1.1]) - ax.set_ylim([-1.1, 1.1]) + ax.set_xlim([-1.1, 1.1]) + ax.set_ylim([-1.1, 1.1]) - return ax + return ax