Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib optional #52

Merged
merged 10 commits into from Aug 12, 2012
3 changes: 1 addition & 2 deletions dipy/sims/voxel.py
Expand Up @@ -250,8 +250,7 @@ def multi_tensor_odf(odf_verts, mf, mevals=None, mevecs=None):
>>> from dipy.sims.voxel import multi_tensor_odf, all_tensor_evecs
>>> from dipy.data import get_sphere
>>> vertices, faces = get_sphere('symmetric724')
>>> mevals=np.array(([0.0015, 0.0003, 0.0003],
[0.0015, 0.0003, 0.0003]))
>>> mevals=np.array(([0.0015, 0.0003, 0.0003],[0.0015, 0.0003, 0.0003]))
>>> e0 = np.array([1, 0, 0.])
>>> e1 = np.array([0., 1, 0])
>>> mevecs=[all_tensor_evecs(e0), all_tensor_evecs(e1)]
Expand Down
33 changes: 31 additions & 2 deletions dipy/viz/__init__.py
@@ -1,4 +1,33 @@
# Init file for visualization package

from ._show_odfs import show_odfs
from projections import *

# We make the visualization requirements (mayavi, matplotlib) optional
# imports:

try:
# Mayavi appears in many guises:
try:
from enthought.mayavi import mlab
except ImportError:
from mayavi import mlab
has_mayavi = True
except ImportError:
e_s = "You do not have Mayavi installed. Some visualization functions"
e_s += " might not work."
print(e_s)
has_mayavi = False

try:
import matplotlib
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

if has_mayavi:
from ._show_odfs import show_odfs

if has_mpl:
from projections import *
55 changes: 33 additions & 22 deletions dipy/viz/projections.py
Expand Up @@ -8,14 +8,25 @@
import numpy as np
import scipy.interpolate as interp

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.tri as tri
try:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.tri as tri
has_mpl = True
hot = matplotlib.cm.hot
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
hot = None
plt = None

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):
cmap=hot, cbar=True, triang=False):

"""Draw a signal on a 2D projection of the sphere.

Expand Down Expand Up @@ -49,11 +60,11 @@ def sph_project(vertices, val, ax=None, vmin=None, vmax=None,
--------
>>> from dipy.data import get_sphere
>>> verts,faces=get_sphere('symmetric724')
>>> sph_project(verts,np.random.rand(len(verts)))
>>> ax = sph_project(verts,np.random.rand(len(verts)))

"""
if ax is None:
_, ax = plt.subplots(1)
if ax is None and plt is not None:
_, ax = plt.subplots(1)
fig = ax.get_figure()

x = vertices[:, 0]
Expand All @@ -75,22 +86,22 @@ def sph_project(vertices, val, ax=None, vmin=None, vmax=None,
r[r>1]=1

if triang:
triang = tri.Triangulation(x, y)
ax.tripcolor(triang, r, cmap=cmap)
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()])
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()])


ax.set_aspect('equal')
Expand Down