Skip to content

Commit

Permalink
Merge pull request #221 from larsoner/imsave
Browse files Browse the repository at this point in the history
MRG: Fix imsave
  • Loading branch information
mwaskom authored Nov 28, 2017
2 parents 9570efb + d0493e4 commit 234f7b5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 31 deletions.
23 changes: 18 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
language: c
sudo: false
# This list of requirements is way overkill, but it helps ensure we
# actually load a usable config (it works for MNE-Python)

env:
global: PYTHON_VERSION=2.7
DISPLAY=:99.0
CONDA_DEPENDENCIES="numpy scipy matplotlib mayavi flake8 pytest pytest-cov nose coverage"
PIP_DEPENDENCIES="codecov nibabel imageio"

matrix:
include:
# Full (Linux, 3.6)
- os: linux
env: CONDA_ENVIRONMENT="environment.yml"
addons:
apt:
packages:
- mencoder

# Full (Linux, 2.7)
- os: linux
env: CONDA_DEPENDENCIES="numpy scipy matplotlib mayavi flake8 pytest pytest-cov nose coverage"
PIP_DEPENDENCIES="codecov nibabel imageio"
addons:
apt:
packages:
- mencoder

# Minimal (no mencoder)
- os: linux
env: CONDA_DEPENDENCIES="numpy scipy matplotlib mayavi flake8 pytest pytest-cov nose coverage"
PIP_DEPENDENCIES="codecov nibabel"

# OSX
- os: osx
env: CONDA_DEPENDENCIES="numpy scipy matplotlib mayavi flake8 pytest pytest-cov nose coverage"
PIP_DEPENDENCIES="codecov nibabel imageio"

# Setup anaconda
before_install:
Expand All @@ -46,7 +57,9 @@ before_script:
- unzip ../fsaverage_min.zip
- cd ..
- export SUBJECTS_DIR="${PWD}/subjects"
- python -c "import imageio; imageio.plugins.ffmpeg.download()"
- if [[ $PIP_DEPENDENCIES == *"imageio"* ]] || [ ! -z "$CONDA_ENVIRONMENT" ]; then
python -c "import imageio; imageio.plugins.ffmpeg.download()";
fi

script:
- cd ${SRC_DIR}
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ install:
- "conda install ffmpeg -c conda-forge"
- "SET SUBJECTS_DIR=%CD%\\subjects"
- "ls %CD%\\subjects"
- "pip install --upgrade git+https://github.com/enthought/mayavi.git"

build: false # Not a C# project, build stuff at the test step instead.

Expand Down
1 change: 0 additions & 1 deletion doc/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ To use PySurfer, you will need to have the following Python packages:

* numpy_
* scipy_
* ipython_
* nibabel_
* mayavi_
* matplotlib_
Expand Down
29 changes: 29 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: mne
channels:
- defaults
- clinicalgraphics # Needed for VTK
dependencies:
- python=3.6.1
- pip
- mkl
- numpy
- scipy
- matplotlib
- pyqt=5
- vtk>=7
- nose
- coverage
- pytest
- pytest-cov
- flake8
- pygments
- pip:
- "git+https://github.com/enthought/traits.git@a7a83182048c08923953e302658b51b68c802132"
- "git+https://github.com/enthought/pyface.git@13a064de48adda3c880350545717d8cf8929afad"
- "git+https://github.com/enthought/traitsui.git@ee8ef0a34dfc1db18a8e2c0301cc18d96b7a3e2f"
- "git+https://github.com/enthought/mayavi.git"
- nibabel
- pytest-sugar
- pytest-faulthandler
- imageio
- codecov
20 changes: 13 additions & 7 deletions surfer/tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,26 @@ def has_freesurfer():

def _set_backend(backend=None):
"""Use testing backend for Windows."""
only_test = (sys.platform == 'win32' or
(os.getenv('TRAVIS', 'false') == 'true' and
sys.version[0] == '3'))
if backend is None:
backend = 'test' if sys.platform == 'win32' else 'auto'
backend = 'test' if only_test else 'auto'
else:
if backend != 'test' and sys.platform == 'win32':
raise SkipTest('non-testing backend crashes on Windows')
if only_test:
raise SkipTest('non-testing backend crashes on Windows and '
'Travis Py3k')
mlab.options.backend = backend


@requires_fsaverage
def test_offscreen():
"""Test offscreen rendering."""
if sys.platform == 'darwin' and os.getenv('TRAVIS', 'false') == 'true':
raise SkipTest('Offscreen Travis tests fail on OSX')
if os.getenv('TRAVIS', 'false') == 'true':
if sys.platform == 'darwin':
raise SkipTest('Offscreen Travis tests fail on OSX')
if sys.version[0] == '3':
raise SkipTest('Offscreen Travis tests fail on Py3k')
_set_backend()
brain = Brain(*std_args, offscreen=True)
# Sometimes the first screenshot is rendered with a different
Expand All @@ -77,8 +84,7 @@ def test_image():
brain.save_image(tmp_name)
brain.save_image(tmp_name, 'rgba', True)
brain.screenshot()
if not (sys.platform.startswith('linux') and
os.getenv('TRAVIS', 'false') == 'true'):
if not os.getenv('TRAVIS', 'false') == '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')
Expand Down
2 changes: 1 addition & 1 deletion surfer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def dec(*args, **kwargs):
# set it back if we get an exception
try:
ret = function(*args, **kwargs)
except:
except Exception:
set_log_level(old_level)
raise
set_log_level(old_level)
Expand Down
32 changes: 15 additions & 17 deletions surfer/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,16 @@ def save_single_image(self, filename, row=-1, col=-1):
if mlab.options.backend != 'test':
mlab.savefig(filename, figure=brain._f)

def _screenshot_figure(self, mode='rgb', antialiased=False, dpi=100):
"""Create a matplolib figure from the current screenshot."""
# adapted from matplotlib.image.imsave
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
fig = Figure(dpi=dpi, frameon=False) # DPI only used for metadata
FigureCanvasAgg(fig)
fig.figimage(self.screenshot(mode, antialiased), resize=True)
return fig

def save_image(self, filename, mode='rgb', antialiased=False):
"""Save view from all panels to disk
Expand All @@ -2286,8 +2296,7 @@ def save_image(self, filename, mode='rgb', antialiased=False):
a Mayavi figure to plot instead of TraitsUI) if you intend to
script plotting commands.
"""
from scipy import misc
misc.imsave(filename, self.screenshot(mode, antialiased))
self._screenshot_figure(mode, antialiased).savefig(filename)

def screenshot(self, mode='rgb', antialiased=False):
"""Generate a screenshot of current view.
Expand Down Expand Up @@ -2643,7 +2652,7 @@ def save_movie(self, fname, time_dilation=4., tmin=None, tmax=None,
raise ValueError("tmax=%r is greater than the latest time point "
"(%r)" % (tmax, self._times[-1]))
n_frames = floor((tmax - tmin) * time_dilation * framerate)
times = np.arange(n_frames)
times = np.arange(n_frames, dtype=float)
times /= framerate * time_dilation
times += tmin
interp_func = interp1d(self._times, np.arange(self.n_times))
Expand Down Expand Up @@ -3067,10 +3076,6 @@ def _min_diff(self, beg, end):

def _add_scalar_data(self, data):
"""Add scalar values to dataset"""
if mlab.options.backend == 'test':
# required SetActiveAttribute filter attributes are not set under
# the testing backend
return 0, self._geo_mesh
array_id = self._mesh_dataset.point_data.add_array(data)
self._mesh_dataset.point_data.get_array(array_id).name = array_id
self._mesh_dataset.point_data.update()
Expand All @@ -3088,10 +3093,6 @@ def _add_scalar_data(self, data):

def _remove_scalar_data(self, array_id):
"""Removes scalar data"""
if mlab.options.backend == 'test':
# required SetActiveAttribute filter attributes are not set under
# the testing backend
return
self._mesh_clones.pop(array_id).remove()
self._mesh_dataset.point_data.remove_array(array_id)

Expand All @@ -3108,8 +3109,7 @@ def _add_vector_data(self, vectors, vector_values, fmin, fmid, fmax,

# Enable backface culling
quiver.actor.property.backface_culling = False
if mlab.options.backend != 'test':
quiver.mlab_source.update()
quiver.mlab_source.update()

# Compute scaling for the glyphs
quiver.glyph.glyph.scale_factor = (scale_factor_norm *
Expand Down Expand Up @@ -3336,8 +3336,7 @@ def set_data(self, layer_id, values, vectors=None, vector_values=None):
self._mesh_dataset.point_data.get_array(
data['array_id']).from_array(values)
# avoid "AttributeError: 'Scene' object has no attribute 'update'"
if mlab.options.backend != 'test':
data['mesh'].update()
data['mesh'].update()
if vectors is not None:
q = data['glyphs']

Expand All @@ -3349,8 +3348,7 @@ def set_data(self, layer_id, values, vectors=None, vector_values=None):
# Update glyphs
q.mlab_source.vectors = vectors
q.mlab_source.scalars = vector_values
if mlab.options.backend != 'test':
q.mlab_source.update()
q.mlab_source.update()

# Update changed parameters, and glyph scaling
q.glyph.glyph.scale_factor = (data['scale_factor_norm'] *
Expand Down

0 comments on commit 234f7b5

Please sign in to comment.