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

MRG, FIX: Safer exit #285

Merged
merged 7 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion surfer/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 20 additions & 18 deletions surfer/tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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]
Expand Down Expand Up @@ -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()


Expand Down
15 changes: 11 additions & 4 deletions surfer/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down