Skip to content

Commit 35d980d

Browse files
committed
ENH: Cleaner mlab imports
1 parent e241747 commit 35d980d

File tree

1 file changed

+10
-135
lines changed

1 file changed

+10
-135
lines changed

surfer/viz.py

Lines changed: 10 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
import nibabel as nib
1010

11+
from mayavi import mlab
12+
from mayavi.tools.mlab_scene_model import MlabSceneModel
13+
from mayavi.core.ui.api import SceneEditor
14+
from mayavi.core.ui.mayavi_scene import MayaviScene
15+
1116
from . import io
1217
from . import utils
1318
from .io import Surface, _get_subjects_dir
@@ -18,12 +23,8 @@
1823
logging.basicConfig() # suppress "No handlers found for logger" error
1924
logger = logging.getLogger('surfer')
2025

21-
try:
22-
from traits.api import (HasTraits, Range, Int, Float,
23-
Bool, Enum, on_trait_change, Instance)
24-
except ImportError:
25-
from enthought.traits.api import (HasTraits, Range, Int, Float,
26-
Bool, Enum, on_trait_change, Instance)
26+
from traits.api import (HasTraits, Range, Int, Float,
27+
Bool, Enum, on_trait_change, Instance)
2728

2829
lh_viewdict = {'lateral': {'v': (180., 90.), 'r': 90.},
2930
'medial': {'v': (0., 90.), 'r': -90.},
@@ -159,11 +160,6 @@ def _prepare_data(data):
159160

160161
def _force_render(figures, backend):
161162
"""Ensure plots are updated before properties are used"""
162-
try:
163-
from mayavi import mlab
164-
assert mlab
165-
except:
166-
from enthought.mayavi import mlab
167163
if not isinstance(figures, list):
168164
figures = [[figures]]
169165
for ff in figures:
@@ -188,11 +184,6 @@ def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen):
188184
is returned to the command line. With the multi-view, TraitsUI
189185
unfortunately has no such support, so we only use it if needed.
190186
"""
191-
try:
192-
from mayavi import mlab
193-
assert mlab
194-
except:
195-
from enthought.mayavi import mlab
196187
if figure is None:
197188
# spawn scenes
198189
h, w = scene_size
@@ -231,21 +222,12 @@ class _MlabGenerator(HasTraits):
231222
try:
232223
from traitsui.api import View
233224
except ImportError:
234-
try:
235-
from traits.ui.api import View
236-
except ImportError:
237-
from enthought.traits.ui.api import View
225+
from traits.ui.api import View
238226

239227
view = Instance(View)
240228

241229
def __init__(self, n_row, n_col, width, height, title, **traits):
242230
HasTraits.__init__(self, **traits)
243-
try:
244-
from mayavi.tools.mlab_scene_model import MlabSceneModel
245-
assert MlabSceneModel
246-
except:
247-
from enthought.mayavi.tools.mlab_scene_model import MlabSceneModel
248-
249231
self.mlab_names = []
250232
self.n_row = n_row
251233
self.n_col = n_col
@@ -272,22 +254,10 @@ def _get_figs_view(self):
272254
return figures, self._v
273255

274256
def _get_gen_view(self):
275-
try:
276-
from mayavi.core.ui.api import SceneEditor
277-
from mayavi.core.ui.mayavi_scene import MayaviScene
278-
assert SceneEditor
279-
assert MayaviScene
280-
except:
281-
from enthought.mayavi.core.ui.api import SceneEditor
282-
from enthought.mayavi.core.ui.mayavi_scene import MayaviScene
283257
try:
284258
from traitsui.api import (View, Item, VGroup, HGroup)
285259
except ImportError:
286-
try:
287-
from traits.ui.api import (View, Item, VGroup, HGroup)
288-
except ImportError:
289-
from enthought.traits.ui.api import (View, Item,
290-
VGroup, HGroup)
260+
from traits.ui.api import (View, Item, VGroup, HGroup)
291261
ind = 0
292262
va = []
293263
for ri in xrange(self.n_row):
@@ -447,12 +417,6 @@ def __init__(self, subject_id, hemi, surf, curv=True, title=None,
447417
# HELPERS
448418
def _toggle_render(self, state, views=None):
449419
"""Turn rendering on (True) or off (False)"""
450-
try:
451-
from mayavi import mlab
452-
assert mlab
453-
except ImportError:
454-
from enthought.mayavi import mlab
455-
456420
figs = []
457421
[figs.extend(f) for f in self._figures]
458422
if views is None:
@@ -1440,11 +1404,6 @@ def set_distance(self, distance=None):
14401404
distance : float
14411405
The distance used.
14421406
"""
1443-
try:
1444-
from mayavi import mlab
1445-
assert mlab
1446-
except:
1447-
from enthought.mayavi import mlab
14481407
if distance is None:
14491408
distance = []
14501409
for ff in self._figures:
@@ -1679,12 +1638,6 @@ def hide_colorbar(self, row=-1, col=-1):
16791638

16801639
def close(self):
16811640
"""Close all figures and cleanup data structure."""
1682-
try:
1683-
from mayavi import mlab
1684-
assert mlab
1685-
except ImportError:
1686-
from enthought.mayavi import mlab
1687-
16881641
for ri, ff in enumerate(self._figures):
16891642
for ci, f in enumerate(ff):
16901643
if f is not None:
@@ -1726,12 +1679,6 @@ def save_single_image(self, filename, row=-1, col=-1):
17261679
a Mayavi figure to plot instead of TraitsUI) if you intend to
17271680
script plotting commands.
17281681
"""
1729-
try:
1730-
from mayavi import mlab
1731-
assert mlab
1732-
except ImportError:
1733-
from enthought.mayavi import mlab
1734-
17351682
brain = self.brain_matrix[row, col]
17361683
ftype = filename[filename.rfind('.') + 1:]
17371684
good_ftypes = ['png', 'jpg', 'bmp', 'tiff', 'ps',
@@ -1833,11 +1780,6 @@ def screenshot_single(self, mode='rgb', antialiased=False, row=-1, col=-1):
18331780
a Mayavi figure to plot instead of TraitsUI) if you intend to
18341781
script plotting commands.
18351782
"""
1836-
try:
1837-
from mayavi import mlab
1838-
assert mlab
1839-
except ImportError:
1840-
from enthought.mayavi import mlab
18411783
brain = self.brain_matrix[row, col]
18421784
return mlab.screenshot(brain._f, mode, antialiased)
18431785

@@ -1965,12 +1907,6 @@ def save_montage(self, filename, order=['lat', 'ven', 'med'],
19651907
out : array
19661908
The montage image, useable with matplotlib.imshow().
19671909
"""
1968-
try:
1969-
from mayavi import mlab
1970-
assert mlab
1971-
except ImportError:
1972-
from enthought.mayavi import mlab
1973-
19741910
assert orientation in ['h', 'v']
19751911
if colorbar == 'auto':
19761912
colorbar = [len(order) // 2]
@@ -2067,11 +2003,6 @@ class _Hemisphere(object):
20672003
"""Object for visualizing one hemisphere with mlab"""
20682004
def __init__(self, subject_id, hemi, surf, figure, geo, curv, title,
20692005
config_opts, subjects_dir, bg_color, offset, backend):
2070-
try:
2071-
from mayavi import mlab
2072-
assert mlab
2073-
except ImportError:
2074-
from enthought.mayavi import mlab
20752006
if not hemi in ['lh', 'rh']:
20762007
raise ValueError('hemi must be either "lh" or "rh"')
20772008
# Set the identifying info
@@ -2109,12 +2040,6 @@ def __init__(self, subject_id, hemi, surf, figure, geo, curv, title,
21092040

21102041
def show_view(self, view=None, roll=None, distance=None):
21112042
"""Orient camera to display view"""
2112-
try:
2113-
from mayavi import mlab
2114-
assert mlab
2115-
except ImportError:
2116-
from enthought.mayavi import mlab
2117-
21182043
if isinstance(view, basestring):
21192044
try:
21202045
vd = self._xfm_view(view, 'd')
@@ -2229,11 +2154,6 @@ def add_data(self, array, mlab_plot, vertices, smooth_mat, min, max,
22292154
else:
22302155
raise ValueError("data has to be 1D or 2D")
22312156

2232-
try:
2233-
from mayavi import mlab
2234-
assert mlab
2235-
except ImportError:
2236-
from enthought.mayavi import mlab
22372157
# Set up the visualization pipeline
22382158
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
22392159
self._geo.y,
@@ -2271,12 +2191,6 @@ def add_data(self, array, mlab_plot, vertices, smooth_mat, min, max,
22712191

22722192
def add_annotation(self, annot, ids, cmap):
22732193
"""Add an annotation file"""
2274-
try:
2275-
from mayavi import mlab
2276-
assert mlab
2277-
except ImportError:
2278-
from enthought.mayavi import mlab
2279-
22802194
# Create an mlab surface to visualize the annot
22812195
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
22822196
self._geo.y,
@@ -2295,12 +2209,6 @@ def add_annotation(self, annot, ids, cmap):
22952209

22962210
def add_label(self, label, label_name, color, alpha):
22972211
"""Add an ROI label to the image"""
2298-
try:
2299-
from mayavi import mlab
2300-
assert mlab
2301-
except ImportError:
2302-
from enthought.mayavi import mlab
2303-
23042212
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
23052213
self._geo.y,
23062214
self._geo.z,
@@ -2315,12 +2223,6 @@ def add_label(self, label, label_name, color, alpha):
23152223

23162224
def add_morphometry(self, morph_data, colormap, measure, min, max):
23172225
"""Add a morphometry overlay to the image"""
2318-
try:
2319-
from mayavi import mlab
2320-
assert mlab
2321-
except ImportError:
2322-
from enthought.mayavi import mlab
2323-
23242226
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
23252227
self._geo.y,
23262228
self._geo.z,
@@ -2342,12 +2244,6 @@ def add_morphometry(self, morph_data, colormap, measure, min, max):
23422244

23432245
def add_foci(self, foci_coords, scale_factor, color, alpha, name):
23442246
"""Add spherical foci, possibly mapping to displayed surf"""
2345-
try:
2346-
from mayavi import mlab
2347-
assert mlab
2348-
except ImportError:
2349-
from enthought.mayavi import mlab
2350-
23512247
# Create the visualization
23522248
points = mlab.points3d(foci_coords[:, 0],
23532249
foci_coords[:, 1],
@@ -2361,12 +2257,6 @@ def add_foci(self, foci_coords, scale_factor, color, alpha, name):
23612257
def add_contour_overlay(self, scalar_data, min=None, max=None,
23622258
n_contours=7, line_width=1.5):
23632259
"""Add a topographic contour overlay of the positive data"""
2364-
try:
2365-
from mayavi import mlab
2366-
assert mlab
2367-
except ImportError:
2368-
from enthought.mayavi import mlab
2369-
23702260
# Set up the pipeline
23712261
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x, self._geo.y,
23722262
self._geo.z,
@@ -2390,12 +2280,6 @@ def add_contour_overlay(self, scalar_data, min=None, max=None,
23902280

23912281
def add_text(self, x, y, text, name, color=None, opacity=1.0):
23922282
""" Add a text to the visualization"""
2393-
try:
2394-
from mayavi import mlab
2395-
assert mlab
2396-
except ImportError:
2397-
from enthought.mayavi import mlab
2398-
23992283
return mlab.text(x, y, text, name=name, color=color,
24002284
opacity=opacity, figure=self._f)
24012285

@@ -2498,11 +2382,6 @@ class OverlayDisplay():
24982382
"""Encapsulation of overlay viz plotting"""
24992383

25002384
def __init__(self, ol, figure):
2501-
try:
2502-
from mayavi import mlab
2503-
assert mlab
2504-
except ImportError:
2505-
from enthought.mayavi import mlab
25062385
args = [ol.geo.x, ol.geo.y, ol.geo.z, ol.geo.faces]
25072386
kwargs = dict(scalars=ol.mlab_data, figure=figure)
25082387
if ol.pos_lims is not None:
@@ -2561,11 +2440,7 @@ class TimeViewer(HasTraits):
25612440
try:
25622441
from traitsui.api import (View, Item, VSplit, HSplit, Group)
25632442
except ImportError:
2564-
try:
2565-
from traits.ui.api import (View, Item, VSplit, HSplit, Group)
2566-
except ImportError:
2567-
from enthought.traits.ui.api import (View, Item, VSplit,
2568-
HSplit, Group)
2443+
from traits.ui.api import (View, Item, VSplit, HSplit, Group)
25692444
min_time = Int(0)
25702445
max_time = Int(1E9)
25712446
current_time = Range(low="min_time", high="max_time", value=0)

0 commit comments

Comments
 (0)