Skip to content

Commit

Permalink
FIX: Fix for deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Sep 5, 2019
1 parent 79b52e2 commit 7f8e9d6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions mne/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def pytest_configure(config):
ignore:numpy.ufunc size changed:RuntimeWarning
ignore:.*mne-realtime.*:DeprecationWarning
ignore:.*imp.*:DeprecationWarning
always:.*get_data.* is deprecated in favor of.*:DeprecationWarning
""" # noqa: E501
for warning_line in warning_lines.split('\n'):
warning_line = warning_line.strip()
Expand Down
8 changes: 8 additions & 0 deletions mne/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ def minimum_phase(h):
###############################################################################
# Misc utilities

# Deal with nibabel 2.5 img.get_data() deprecation
def _get_img_fdata(img):
try:
return img.get_fdata()
except AttributeError:
return img.get_data().astype(float)


def _read_volume_info(fobj):
"""An implementation of nibabel.freesurfer.io._read_volume_info, since old
versions of nibabel (<=2.1.0) don't have it.
Expand Down
18 changes: 10 additions & 8 deletions mne/morph.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
from scipy import sparse

from .fixes import _get_img_fdata
from .parallel import parallel_func
from .source_estimate import (VolSourceEstimate, SourceEstimate,
VolVectorSourceEstimate, VectorSourceEstimate,
Expand Down Expand Up @@ -524,7 +525,7 @@ def _morphed_stc_as_volume(morph, stc, mri_resolution=False, mri_space=True,
if new_zooms is not None:
from dipy.align.reslice import reslice
new_zooms = new_zooms[:3]
img, affine = reslice(img.get_data(),
img, affine = reslice(_get_img_fdata(img),
img.affine, # MRI to world registration
zooms, # old voxel size in mm
new_zooms) # new voxel size in mm
Expand Down Expand Up @@ -677,7 +678,8 @@ def _interpolate_data(stc, morph, mri_resolution=True, mri_space=True,
if voxel_size_defined:
# reslice mri
img, img_affine = reslice(
img.get_data(), img.affine, _get_zooms_orig(morph), voxel_size)
_get_img_fdata(img), img.affine, _get_zooms_orig(morph),
voxel_size)
with warnings.catch_warnings(): # nibabel<->numpy warning
img = NiftiImage(img, img_affine, header=header)

Expand Down Expand Up @@ -710,25 +712,25 @@ def _compute_morph_sdr(mri_from, mri_to, niter_affine=(100, 100, 10),

# reslice mri_from
mri_from_res, mri_from_res_affine = reslice(
mri_from.get_data(), mri_from.affine, mri_from.header.get_zooms()[:3],
zooms)
_get_img_fdata(mri_from), mri_from.affine,
mri_from.header.get_zooms()[:3], zooms)

with warnings.catch_warnings(): # nibabel<->numpy warning
mri_from = nib.Nifti1Image(mri_from_res, mri_from_res_affine)

# reslice mri_to
mri_to_res, mri_to_res_affine = reslice(
mri_to.get_data(), mri_to.affine, mri_to.header.get_zooms()[:3],
_get_img_fdata(mri_to), mri_to.affine, mri_to.header.get_zooms()[:3],
zooms)

with warnings.catch_warnings(): # nibabel<->numpy warning
mri_to = nib.Nifti1Image(mri_to_res, mri_to_res_affine)

affine = mri_to.affine
mri_to = np.array(mri_to.dataobj, float) # to ndarray
mri_to = _get_img_fdata(mri_to) # to ndarray
mri_to /= mri_to.max()
mri_from_affine = mri_from.affine # get mri_from to world transform
mri_from = np.array(mri_from.dataobj, float) # to ndarray
mri_from = _get_img_fdata(mri_from) # to ndarray
mri_from /= mri_from.max() # normalize

# compute center of mass
Expand Down Expand Up @@ -1148,7 +1150,7 @@ def _morph_one(stc_one):

# reslice to match morph
img_to, img_to_affine = reslice(
img_to.get_data(), morph.affine, _get_zooms_orig(morph),
_get_img_fdata(img_to), morph.affine, _get_zooms_orig(morph),
morph.zooms)

# morph data
Expand Down
7 changes: 4 additions & 3 deletions mne/source_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
write_float_matrix, write_int_matrix,
write_coord_trans, start_file, end_file, write_id)
from .bem import read_bem_surfaces, ConductorModel
from .fixes import _get_img_fdata
from .surface import (read_surface, _create_surf_spacing, _get_ico_surface,
_tessellate_sphere_surf, _get_surf_neighbors,
_normalize_vectors, _triangle_neighbors, mesh_dist,
Expand Down Expand Up @@ -354,7 +355,7 @@ def export_volume(self, fname, include_surfaces=True,
# get the inuse array
if mri_resolution:
# read the mri file used to generate volumes
aseg_data = nib.load(vs['mri_file']).get_data()
aseg_data = _get_img_fdata(nib.load(vs['mri_file']))
# get the voxel space shape
shape3d = (vs['mri_height'], vs['mri_depth'],
vs['mri_width'])
Expand Down Expand Up @@ -1829,7 +1830,7 @@ def _get_volume_label_mask(mri, volume_label, rr):

# Read the segmentation data using nibabel
mgz = nib.load(mri)
mgz_data = mgz.get_data()
mgz_data = _get_img_fdata(mgz)

# Get the numeric index for this volume label
lut = _get_lut()
Expand Down Expand Up @@ -2513,7 +2514,7 @@ def get_volume_labels_from_aseg(mgz_fname, return_colors=False):
import nibabel as nib

# Read the mgz file using nibabel
mgz_data = nib.load(mgz_fname).get_data()
mgz_data = _get_img_fdata(nib.load(mgz_fname))

# Get the unique label names
lut = _get_lut()
Expand Down
4 changes: 2 additions & 2 deletions mne/tests/test_source_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
spatial_src_connectivity, spatial_tris_connectivity,
SourceSpaces, VolVectorSourceEstimate)
from mne.datasets import testing
from mne.fixes import fft
from mne.fixes import fft, _get_img_fdata
from mne.source_estimate import grade_to_tris, _get_vol_mask
from mne.minimum_norm import (read_inverse_operator, apply_inverse,
apply_inverse_epochs)
Expand Down Expand Up @@ -948,7 +948,7 @@ def test_vol_mask():
data = (1 + np.arange(n_vertices))[:, np.newaxis]
stc_tmp = VolSourceEstimate(data, vertices, tmin=0., tstep=1.)
img = stc_tmp.as_volume(src, mri_resolution=False)
img_data = img.get_data()[:, :, :, 0].T
img_data = _get_img_fdata(img)[:, :, :, 0].T
mask_nib = (img_data != 0)
assert_array_equal(img_data[mask_nib], data[:, 0])
assert_array_equal(np.where(mask_nib.ravel())[0], src[0]['vertno'])
Expand Down
8 changes: 4 additions & 4 deletions mne/viz/_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from scipy import linalg, sparse

from ..defaults import DEFAULTS
from ..fixes import einsum, _crop_colorbar
from ..fixes import einsum, _crop_colorbar, _get_img_fdata
from ..io import _loc_to_coil_trans
from ..io.pick import pick_types, _picks_to_idx
from ..io.constants import FIFF
Expand Down Expand Up @@ -431,7 +431,7 @@ def _plot_mri_contours(mri_fname, surf_fnames, orientation='coronal',

# Load the T1 data
nim = nib.load(mri_fname)
data = nim.get_data()
data = _get_img_fdata(nim)
try:
affine = nim.affine
except AttributeError: # old nibabel
Expand Down Expand Up @@ -1928,7 +1928,7 @@ def _click_to_cut_coords(event, params):
cut_coords = np.array((x, y, z))

if params['mode'] == 'glass_brain': # find idx for MIP
img_data = np.abs(params['img_idx'].get_data())
img_data = np.abs(_get_img_fdata(params['img_idx']))
ijk = _cut_coords_to_ijk(cut_coords, params['img_idx'])
if ax == 'x':
ijk[0] = np.argmax(img_data[:, ijk[1], ijk[2]])
Expand Down Expand Up @@ -2762,7 +2762,7 @@ def _plot_dipole_mri_orthoview(dipole, trans, subject, subjects_dir=None,
coord_frame=coord_frame)

zooms = t1.header.get_zooms()
data = t1.get_data()
data = _get_img_fdata(t1)
dims = len(data) # Symmetric size assumed.
dd = dims / 2.
dd *= t1.header.get_zooms()[0]
Expand Down
4 changes: 2 additions & 2 deletions mne/viz/tests/test_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def test_plot_volume_source_estimates_morph():
n_time = 2
data = np.random.RandomState(0).rand(n_verts, n_time)
stc = VolSourceEstimate(data, vertices, 1, 1)
morph = compute_source_morph(sample_src, 'sample', 'fsaverage', zooms=10,
morph = compute_source_morph(sample_src, 'sample', 'fsaverage', zooms=5,
subjects_dir=subjects_dir)
initial_pos = (-0.05, -0.01, -0.006)
with pytest.warns(None): # sometimes get scalars/index warning
Expand All @@ -538,7 +538,7 @@ def test_plot_volume_source_estimates_morph():
initial_pos=initial_pos, verbose=True)
log = log.getvalue()
assert 't = 1.000 s' in log
assert '(-52.0, -8.0, -2.0) mm' in log
assert '(-52.0, -8.0, -7.0) mm' in log

with pytest.raises(ValueError, match='Allowed values are'):
stc.plot(sample_src, 'sample', subjects_dir, mode='abcd')
Expand Down

0 comments on commit 7f8e9d6

Please sign in to comment.