Skip to content

Commit afaf68a

Browse files
committed
Merge pull request #65 from Eric89GXL/mlab-import
Import mayavi at the top of `surfer.viz` and remove backward-compat try layers
2 parents e241747 + 6759777 commit afaf68a

File tree

3 files changed

+13
-154
lines changed

3 files changed

+13
-154
lines changed

bin/pysurfer

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ if __name__ == '__main__':
101101

102102
# It's nice to have mlab in the namespace, but we'll import it
103103
# after the other stuff so getting usage is not interminable
104-
try:
105-
from mayavi import mlab
106-
except ImportError:
107-
from enthought.mayavi import mlab
104+
from mayavi import mlab
105+
assert mlab # make pyflakes happy
108106

109107
# Now clean up the namespace a bit
110108
del parser, args

surfer/tests/test_viz.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77

88
from surfer import Brain
99
from surfer import io
10-
try:
11-
from mayavi import mlab
12-
assert mlab
13-
except ImportError:
14-
from enthought.mayavi import mlab
10+
from mayavi import mlab
1511

1612
if 'SUBJECTS_DIR' not in os.environ:
1713
raise ValueError('Test suite relies on the definition of SUBJECTS_DIR')

surfer/viz.py

Lines changed: 10 additions & 145 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
@@ -228,24 +219,11 @@ def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen):
228219

229220
class _MlabGenerator(HasTraits):
230221
"""TraitsUI mlab figure generator"""
231-
try:
232-
from traitsui.api import View
233-
except ImportError:
234-
try:
235-
from traits.ui.api import View
236-
except ImportError:
237-
from enthought.traits.ui.api import View
238-
222+
from traitsui.api import View
239223
view = Instance(View)
240224

241225
def __init__(self, n_row, n_col, width, height, title, **traits):
242226
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-
249227
self.mlab_names = []
250228
self.n_row = n_row
251229
self.n_col = n_col
@@ -272,22 +250,7 @@ def _get_figs_view(self):
272250
return figures, self._v
273251

274252
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
283-
try:
284-
from traitsui.api import (View, Item, VGroup, HGroup)
285-
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)
253+
from traitsui.api import (View, Item, VGroup, HGroup)
291254
ind = 0
292255
va = []
293256
for ri in xrange(self.n_row):
@@ -447,12 +410,6 @@ def __init__(self, subject_id, hemi, surf, curv=True, title=None,
447410
# HELPERS
448411
def _toggle_render(self, state, views=None):
449412
"""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-
456413
figs = []
457414
[figs.extend(f) for f in self._figures]
458415
if views is None:
@@ -1440,11 +1397,6 @@ def set_distance(self, distance=None):
14401397
distance : float
14411398
The distance used.
14421399
"""
1443-
try:
1444-
from mayavi import mlab
1445-
assert mlab
1446-
except:
1447-
from enthought.mayavi import mlab
14481400
if distance is None:
14491401
distance = []
14501402
for ff in self._figures:
@@ -1679,12 +1631,6 @@ def hide_colorbar(self, row=-1, col=-1):
16791631

16801632
def close(self):
16811633
"""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-
16881634
for ri, ff in enumerate(self._figures):
16891635
for ci, f in enumerate(ff):
16901636
if f is not None:
@@ -1726,12 +1672,6 @@ def save_single_image(self, filename, row=-1, col=-1):
17261672
a Mayavi figure to plot instead of TraitsUI) if you intend to
17271673
script plotting commands.
17281674
"""
1729-
try:
1730-
from mayavi import mlab
1731-
assert mlab
1732-
except ImportError:
1733-
from enthought.mayavi import mlab
1734-
17351675
brain = self.brain_matrix[row, col]
17361676
ftype = filename[filename.rfind('.') + 1:]
17371677
good_ftypes = ['png', 'jpg', 'bmp', 'tiff', 'ps',
@@ -1833,11 +1773,6 @@ def screenshot_single(self, mode='rgb', antialiased=False, row=-1, col=-1):
18331773
a Mayavi figure to plot instead of TraitsUI) if you intend to
18341774
script plotting commands.
18351775
"""
1836-
try:
1837-
from mayavi import mlab
1838-
assert mlab
1839-
except ImportError:
1840-
from enthought.mayavi import mlab
18411776
brain = self.brain_matrix[row, col]
18421777
return mlab.screenshot(brain._f, mode, antialiased)
18431778

@@ -1965,12 +1900,6 @@ def save_montage(self, filename, order=['lat', 'ven', 'med'],
19651900
out : array
19661901
The montage image, useable with matplotlib.imshow().
19671902
"""
1968-
try:
1969-
from mayavi import mlab
1970-
assert mlab
1971-
except ImportError:
1972-
from enthought.mayavi import mlab
1973-
19741903
assert orientation in ['h', 'v']
19751904
if colorbar == 'auto':
19761905
colorbar = [len(order) // 2]
@@ -2067,11 +1996,6 @@ class _Hemisphere(object):
20671996
"""Object for visualizing one hemisphere with mlab"""
20681997
def __init__(self, subject_id, hemi, surf, figure, geo, curv, title,
20691998
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
20751999
if not hemi in ['lh', 'rh']:
20762000
raise ValueError('hemi must be either "lh" or "rh"')
20772001
# Set the identifying info
@@ -2109,12 +2033,6 @@ def __init__(self, subject_id, hemi, surf, figure, geo, curv, title,
21092033

21102034
def show_view(self, view=None, roll=None, distance=None):
21112035
"""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-
21182036
if isinstance(view, basestring):
21192037
try:
21202038
vd = self._xfm_view(view, 'd')
@@ -2229,11 +2147,6 @@ def add_data(self, array, mlab_plot, vertices, smooth_mat, min, max,
22292147
else:
22302148
raise ValueError("data has to be 1D or 2D")
22312149

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

22722185
def add_annotation(self, annot, ids, cmap):
22732186
"""Add an annotation file"""
2274-
try:
2275-
from mayavi import mlab
2276-
assert mlab
2277-
except ImportError:
2278-
from enthought.mayavi import mlab
2279-
22802187
# Create an mlab surface to visualize the annot
22812188
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
22822189
self._geo.y,
@@ -2295,12 +2202,6 @@ def add_annotation(self, annot, ids, cmap):
22952202

22962203
def add_label(self, label, label_name, color, alpha):
22972204
"""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-
23042205
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
23052206
self._geo.y,
23062207
self._geo.z,
@@ -2315,12 +2216,6 @@ def add_label(self, label, label_name, color, alpha):
23152216

23162217
def add_morphometry(self, morph_data, colormap, measure, min, max):
23172218
"""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-
23242219
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x,
23252220
self._geo.y,
23262221
self._geo.z,
@@ -2342,12 +2237,6 @@ def add_morphometry(self, morph_data, colormap, measure, min, max):
23422237

23432238
def add_foci(self, foci_coords, scale_factor, color, alpha, name):
23442239
"""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-
23512240
# Create the visualization
23522241
points = mlab.points3d(foci_coords[:, 0],
23532242
foci_coords[:, 1],
@@ -2361,12 +2250,6 @@ def add_foci(self, foci_coords, scale_factor, color, alpha, name):
23612250
def add_contour_overlay(self, scalar_data, min=None, max=None,
23622251
n_contours=7, line_width=1.5):
23632252
"""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-
23702253
# Set up the pipeline
23712254
mesh = mlab.pipeline.triangular_mesh_source(self._geo.x, self._geo.y,
23722255
self._geo.z,
@@ -2390,12 +2273,6 @@ def add_contour_overlay(self, scalar_data, min=None, max=None,
23902273

23912274
def add_text(self, x, y, text, name, color=None, opacity=1.0):
23922275
""" 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-
23992276
return mlab.text(x, y, text, name=name, color=color,
24002277
opacity=opacity, figure=self._f)
24012278

@@ -2498,11 +2375,6 @@ class OverlayDisplay():
24982375
"""Encapsulation of overlay viz plotting"""
24992376

25002377
def __init__(self, ol, figure):
2501-
try:
2502-
from mayavi import mlab
2503-
assert mlab
2504-
except ImportError:
2505-
from enthought.mayavi import mlab
25062378
args = [ol.geo.x, ol.geo.y, ol.geo.z, ol.geo.faces]
25072379
kwargs = dict(scalars=ol.mlab_data, figure=figure)
25082380
if ol.pos_lims is not None:
@@ -2558,14 +2430,7 @@ class TimeViewer(HasTraits):
25582430
brain(s) to control
25592431
"""
25602432
# Nested import of traisui for setup.py without X server
2561-
try:
2562-
from traitsui.api import (View, Item, VSplit, HSplit, Group)
2563-
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)
2433+
from traitsui.api import (View, Item, VSplit, HSplit, Group)
25692434
min_time = Int(0)
25702435
max_time = Int(1E9)
25712436
current_time = Range(low="min_time", high="max_time", value=0)

0 commit comments

Comments
 (0)