diff --git a/surfer/io.py b/surfer/io.py index 8ddcf69..efda923 100644 --- a/surfer/io.py +++ b/surfer/io.py @@ -29,7 +29,7 @@ def read_scalar_data(filepath): flat numpy array of scalar data """ try: - scalar_data = nib.load(filepath).get_data() + scalar_data = np.asanyarray(nib.load(filepath).dataobj) scalar_data = np.ravel(scalar_data, order="F") return scalar_data diff --git a/surfer/tests/test_viz.py b/surfer/tests/test_viz.py index 46dfce6..5bb6b18 100644 --- a/surfer/tests/test_viz.py +++ b/surfer/tests/test_viz.py @@ -2,7 +2,6 @@ import os.path as op from os.path import join as pjoin import sys -import warnings import pytest from mayavi import mlab @@ -16,8 +15,6 @@ from surfer.utils import (requires_fsaverage, requires_imageio, requires_fs, _get_extra) -warnings.simplefilter('always') - subject_id = 'fsaverage' std_args = [subject_id, 'lh', 'inflated'] data_dir = pjoin(op.dirname(__file__), '..', '..', 'examples', 'example_data') @@ -84,28 +81,35 @@ def test_image(tmpdir): brain = Brain(subject_id, 'both', surf=surf, size=100) brain.add_overlay(overlay_fname, hemi='lh', min=5, max=20, sign="pos") brain.save_imageset(tmp_name, ['med', 'lat'], 'jpg') - brain.close() - - brain = Brain(*std_args, size=100) brain.save_image(tmp_name) brain.save_image(tmp_name, 'rgba', True) brain.screenshot() - if os.getenv('TRAVIS', '') != 'true': - # for some reason these fail on Travis sometimes - brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='v') - brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='h') - brain.save_montage(tmp_name, [['l', 'v'], ['m', 'f']]) + brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='v') + brain.save_montage(tmp_name, ['l', 'v', 'm'], orientation='h') + brain.save_montage(tmp_name, [['l', 'v'], ['m', 'f']]) brain.close() +@requires_fsaverage() +def test_brain_separate(): + """Test that Brain does not reuse existing figures by default.""" + _set_backend('auto') + brain = Brain(*std_args) + assert brain.brain_matrix.size == 1 + brain_2 = Brain(*std_args) + assert brain_2.brain_matrix.size == 1 + assert brain._figures[0][0] is not brain_2._figures[0][0] + brain_3 = Brain(*std_args, figure=brain._figures[0][0]) + assert brain._figures[0][0] is brain_3._figures[0][0] + + @requires_fsaverage() def test_brains(): """Test plotting of Brain with different arguments.""" # testing backend breaks when passing in a figure, so we use 'auto' here # (shouldn't affect usability, but it makes testing more annoying) _set_backend('auto') - with warnings.catch_warnings(record=True): # traits for mlab.figure() - mlab.figure(101) + mlab.figure(101) surfs = ['inflated', 'white', 'white', 'white', 'white', 'white', 'white'] hemis = ['lh', 'rh', 'both', 'both', 'rh', 'both', 'both'] titles = [None, 'Hello', 'Good bye!', 'lut test', @@ -118,8 +122,7 @@ def test_brains(): (0.2, 0.2, 0.2), "black", "0.75"] foregrounds = ["black", "white", "0.75", "red", (0.2, 0.2, 0.2), "blue", "black"] - with warnings.catch_warnings(record=True): # traits for mlab.figure() - figs = [101, mlab.figure(), None, None, mlab.figure(), None, None] + figs = [101, mlab.figure(), None, None, mlab.figure(), None, None] subj_dir = utils._get_subjects_dir() subj_dirs = [None, subj_dir, subj_dir, subj_dir, subj_dir, subj_dir, subj_dir] @@ -458,9 +461,8 @@ def test_probabilistic_labels(): prob_field[ids] = probs brain.add_data(prob_field, thresh=1e-5) - with warnings.catch_warnings(record=True): - brain.data["colorbar"].number_of_colors = 10 - brain.data["colorbar"].number_of_labels = 11 + brain.data["colorbar"].number_of_colors = 10 + brain.data["colorbar"].number_of_labels = 11 brain.close() diff --git a/surfer/viz.py b/surfer/viz.py index 094fc37..f600d69 100644 --- a/surfer/viz.py +++ b/surfer/viz.py @@ -217,8 +217,8 @@ def _make_viewer(figure, n_row, n_col, title, scene_size, offscreen, # Triage: don't make TraitsUI if we don't have to if n_row == 1 and n_col == 1: with warnings.catch_warnings(record=True): # traits - figure = mlab.figure(title, size=(w, h)) - mlab.clf(figure) + figure = mlab.figure(size=(w, h)) + figure.name = title # should set the figure title figures = [[figure]] _v = None else: @@ -2293,13 +2293,20 @@ def close(self): for ri, ff in enumerate(self._figures): for ci, f in enumerate(ff): if f is not None: - mlab.close(f) + try: + mlab.close(f) + except Exception: + pass self._figures[ri][ci] = None + _force_render([]) # should we tear down other variables? if getattr(self, '_v', None) is not None: - self._v.dispose() + try: + self._v.dispose() + except Exception: + pass self._v = None def __del__(self):