Skip to content

Commit 21e7183

Browse files
authored
Merge pull request #285 from larsoner/safer
MRG, FIX: Safer exit
2 parents 3692eb4 + 5aaf043 commit 21e7183

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

surfer/io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def read_scalar_data(filepath):
2929
flat numpy array of scalar data
3030
"""
3131
try:
32-
scalar_data = nib.load(filepath).get_data()
32+
scalar_data = np.asanyarray(nib.load(filepath).dataobj)
3333
scalar_data = np.ravel(scalar_data, order="F")
3434
return scalar_data
3535

surfer/tests/test_viz.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os.path as op
33
from os.path import join as pjoin
44
import sys
5-
import warnings
65

76
import pytest
87
from mayavi import mlab
@@ -16,8 +15,6 @@
1615
from surfer.utils import (requires_fsaverage, requires_imageio, requires_fs,
1716
_get_extra)
1817

19-
warnings.simplefilter('always')
20-
2118
subject_id = 'fsaverage'
2219
std_args = [subject_id, 'lh', 'inflated']
2320
data_dir = pjoin(op.dirname(__file__), '..', '..', 'examples', 'example_data')
@@ -84,28 +81,35 @@ def test_image(tmpdir):
8481
brain = Brain(subject_id, 'both', surf=surf, size=100)
8582
brain.add_overlay(overlay_fname, hemi='lh', min=5, max=20, sign="pos")
8683
brain.save_imageset(tmp_name, ['med', 'lat'], 'jpg')
87-
brain.close()
88-
89-
brain = Brain(*std_args, size=100)
9084
brain.save_image(tmp_name)
9185
brain.save_image(tmp_name, 'rgba', True)
9286
brain.screenshot()
93-
if os.getenv('TRAVIS', '') != 'true':
94-
# for some reason these fail on Travis sometimes
95-
brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='v')
96-
brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='h')
97-
brain.save_montage(tmp_name, [['l', 'v'], ['m', 'f']])
87+
brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='v')
88+
brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='h')
89+
brain.save_montage(tmp_name, [['l', 'v'], ['m', 'f']])
9890
brain.close()
9991

10092

93+
@requires_fsaverage()
94+
def test_brain_separate():
95+
"""Test that Brain does not reuse existing figures by default."""
96+
_set_backend('auto')
97+
brain = Brain(*std_args)
98+
assert brain.brain_matrix.size == 1
99+
brain_2 = Brain(*std_args)
100+
assert brain_2.brain_matrix.size == 1
101+
assert brain._figures[0][0] is not brain_2._figures[0][0]
102+
brain_3 = Brain(*std_args, figure=brain._figures[0][0])
103+
assert brain._figures[0][0] is brain_3._figures[0][0]
104+
105+
101106
@requires_fsaverage()
102107
def test_brains():
103108
"""Test plotting of Brain with different arguments."""
104109
# testing backend breaks when passing in a figure, so we use 'auto' here
105110
# (shouldn't affect usability, but it makes testing more annoying)
106111
_set_backend('auto')
107-
with warnings.catch_warnings(record=True): # traits for mlab.figure()
108-
mlab.figure(101)
112+
mlab.figure(101)
109113
surfs = ['inflated', 'white', 'white', 'white', 'white', 'white', 'white']
110114
hemis = ['lh', 'rh', 'both', 'both', 'rh', 'both', 'both']
111115
titles = [None, 'Hello', 'Good bye!', 'lut test',
@@ -118,8 +122,7 @@ def test_brains():
118122
(0.2, 0.2, 0.2), "black", "0.75"]
119123
foregrounds = ["black", "white", "0.75", "red",
120124
(0.2, 0.2, 0.2), "blue", "black"]
121-
with warnings.catch_warnings(record=True): # traits for mlab.figure()
122-
figs = [101, mlab.figure(), None, None, mlab.figure(), None, None]
125+
figs = [101, mlab.figure(), None, None, mlab.figure(), None, None]
123126
subj_dir = utils._get_subjects_dir()
124127
subj_dirs = [None, subj_dir, subj_dir, subj_dir,
125128
subj_dir, subj_dir, subj_dir]
@@ -458,9 +461,8 @@ def test_probabilistic_labels():
458461
prob_field[ids] = probs
459462
brain.add_data(prob_field, thresh=1e-5)
460463

461-
with warnings.catch_warnings(record=True):
462-
brain.data["colorbar"].number_of_colors = 10
463-
brain.data["colorbar"].number_of_labels = 11
464+
brain.data["colorbar"].number_of_colors = 10
465+
brain.data["colorbar"].number_of_labels = 11
464466
brain.close()
465467

466468

surfer/viz.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen,
217217
# Triage: don't make TraitsUI if we don't have to
218218
if n_row == 1 and n_col == 1:
219219
with warnings.catch_warnings(record=True): # traits
220-
figure = mlab.figure(title, size=(w, h))
221-
mlab.clf(figure)
220+
figure = mlab.figure(size=(w, h))
221+
figure.name = title # should set the figure title
222222
figures = [[figure]]
223223
_v = None
224224
else:
@@ -2293,13 +2293,20 @@ def close(self):
22932293
for ri, ff in enumerate(self._figures):
22942294
for ci, f in enumerate(ff):
22952295
if f is not None:
2296-
mlab.close(f)
2296+
try:
2297+
mlab.close(f)
2298+
except Exception:
2299+
pass
22972300
self._figures[ri][ci] = None
2301+
22982302
_force_render([])
22992303

23002304
# should we tear down other variables?
23012305
if getattr(self, '_v', None) is not None:
2302-
self._v.dispose()
2306+
try:
2307+
self._v.dispose()
2308+
except Exception:
2309+
pass
23032310
self._v = None
23042311

23052312
def __del__(self):

0 commit comments

Comments
 (0)