From 7f8e9d6c70513c920e84d2e05d4f9cd275fb7076 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Thu, 5 Sep 2019 16:34:37 -0400 Subject: [PATCH] FIX: Fix for deprecation --- mne/conftest.py | 1 + mne/fixes.py | 8 ++++++++ mne/morph.py | 18 ++++++++++-------- mne/source_space.py | 7 ++++--- mne/tests/test_source_estimate.py | 4 ++-- mne/viz/_3d.py | 8 ++++---- mne/viz/tests/test_3d.py | 4 ++-- 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/mne/conftest.py b/mne/conftest.py index ce26ece1285..bbbb798e3d3 100644 --- a/mne/conftest.py +++ b/mne/conftest.py @@ -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() diff --git a/mne/fixes.py b/mne/fixes.py index 3d397caa141..251f0138b0f 100644 --- a/mne/fixes.py +++ b/mne/fixes.py @@ -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. diff --git a/mne/morph.py b/mne/morph.py index 91928ed1164..9644379302a 100644 --- a/mne/morph.py +++ b/mne/morph.py @@ -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, @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/mne/source_space.py b/mne/source_space.py index 96a4279e9e8..110a22f3aa1 100644 --- a/mne/source_space.py +++ b/mne/source_space.py @@ -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, @@ -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']) @@ -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() @@ -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() diff --git a/mne/tests/test_source_estimate.py b/mne/tests/test_source_estimate.py index 51122dfeda3..cbb3f065186 100644 --- a/mne/tests/test_source_estimate.py +++ b/mne/tests/test_source_estimate.py @@ -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) @@ -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']) diff --git a/mne/viz/_3d.py b/mne/viz/_3d.py index e44129fbc9f..df38c3ab03d 100644 --- a/mne/viz/_3d.py +++ b/mne/viz/_3d.py @@ -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 @@ -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 @@ -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]]) @@ -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] diff --git a/mne/viz/tests/test_3d.py b/mne/viz/tests/test_3d.py index 22f43b34264..239c2021af0 100644 --- a/mne/viz/tests/test_3d.py +++ b/mne/viz/tests/test_3d.py @@ -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 @@ -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')