Skip to content

Commit c503c32

Browse files
authored
Merge pull request #3099 from effigies/mnt/nibabel3
ENH: Pacify DeprecationWarnings caused by nibabel 3 pre-release
2 parents b5cb2aa + fcbaad8 commit c503c32

28 files changed

+190
-208
lines changed

nipype/algorithms/confounds.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,10 @@ def _run_interface(self, runtime):
623623
skip_vols = self.inputs.ignore_initial_volumes
624624
if skip_vols:
625625
imgseries = imgseries.__class__(
626-
imgseries.get_data()[..., skip_vols:],
627-
imgseries.affine,
628-
imgseries.header,
626+
imgseries.dataobj[..., skip_vols:], imgseries.affine, imgseries.header
629627
)
630628

631-
mask_images = self._process_masks(mask_images, imgseries.get_data())
629+
mask_images = self._process_masks(mask_images, imgseries.dataobj)
632630

633631
TR = 0
634632
if self.inputs.pre_filter == "cosine":
@@ -664,7 +662,7 @@ def _run_interface(self, runtime):
664662
)
665663

666664
components, filter_basis, metadata = compute_noise_components(
667-
imgseries.get_data(),
665+
imgseries.get_fdata(dtype=np.float32),
668666
mask_images,
669667
components_criterion,
670668
self.inputs.pre_filter,
@@ -837,8 +835,9 @@ def __init__(self, *args, **kwargs):
837835
def _process_masks(self, mask_images, timeseries=None):
838836
out_images = []
839837
self._mask_files = []
838+
timeseries = np.asanyarray(timeseries)
840839
for i, img in enumerate(mask_images):
841-
mask = img.get_data().astype(np.bool)
840+
mask = np.asanyarray(img.dataobj).astype(np.bool)
842841
imgseries = timeseries[mask, :]
843842
imgseries = regress_poly(2, imgseries)[0]
844843
tSTD = _compute_tSTD(imgseries, 0, axis=-1)
@@ -920,11 +919,13 @@ class TSNR(BaseInterface):
920919
def _run_interface(self, runtime):
921920
img = nb.load(self.inputs.in_file[0], mmap=NUMPY_MMAP)
922921
header = img.header.copy()
923-
vollist = [
924-
nb.load(filename, mmap=NUMPY_MMAP) for filename in self.inputs.in_file
925-
]
922+
vollist = [nb.load(filename) for filename in self.inputs.in_file]
926923
data = np.concatenate(
927-
[vol.get_data().reshape(vol.shape[:3] + (-1,)) for vol in vollist], axis=3
924+
[
925+
vol.get_fdata(dtype=np.float32).reshape(vol.shape[:3] + (-1,))
926+
for vol in vollist
927+
],
928+
axis=3,
928929
)
929930
data = np.nan_to_num(data)
930931

@@ -985,7 +986,7 @@ class NonSteadyStateDetector(BaseInterface):
985986
def _run_interface(self, runtime):
986987
in_nii = nb.load(self.inputs.in_file)
987988
global_signal = (
988-
in_nii.get_data()[:, :, :, :50].mean(axis=0).mean(axis=0).mean(axis=0)
989+
in_nii.dataobj[:, :, :, :50].mean(axis=0).mean(axis=0).mean(axis=0)
989990
)
990991

991992
self._results = {"n_volumes_to_discard": is_outlier(global_signal)}
@@ -1032,8 +1033,8 @@ def compute_dvars(
10321033
from nitime.algorithms import AR_est_YW
10331034
import warnings
10341035

1035-
func = nb.load(in_file, mmap=NUMPY_MMAP).get_data().astype(np.float32)
1036-
mask = nb.load(in_mask, mmap=NUMPY_MMAP).get_data().astype(np.uint8)
1036+
func = nb.load(in_file).get_fdata(dtype=np.float32)
1037+
mask = np.asanyarray(nb.load(in_mask).dataobj).astype(np.uint8)
10371038

10381039
if len(func.shape) != 4:
10391040
raise RuntimeError("Input fMRI dataset should be 4-dimensional")
@@ -1278,20 +1279,22 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None):
12781279
if mask_method == "union":
12791280
mask = None
12801281
for filename in mask_files:
1281-
img = nb.load(filename, mmap=NUMPY_MMAP)
1282+
img = nb.load(filename)
1283+
img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0
12821284
if mask is None:
1283-
mask = img.get_data() > 0
1284-
np.logical_or(mask, img.get_data() > 0, mask)
1285+
mask = img_as_mask
1286+
np.logical_or(mask, img_as_mask, mask)
12851287
img = nb.Nifti1Image(mask, img.affine, header=img.header)
12861288
return [img]
12871289

12881290
if mask_method == "intersect":
12891291
mask = None
12901292
for filename in mask_files:
1291-
img = nb.load(filename, mmap=NUMPY_MMAP)
1293+
img = nb.load(filename)
1294+
img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0
12921295
if mask is None:
1293-
mask = img.get_data() > 0
1294-
np.logical_and(mask, img.get_data() > 0, mask)
1296+
mask = img_as_mask
1297+
np.logical_and(mask, img_as_mask, mask)
12951298
img = nb.Nifti1Image(mask, img.affine, header=img.header)
12961299
return [img]
12971300

@@ -1374,7 +1377,7 @@ def compute_noise_components(
13741377
md_retained = []
13751378

13761379
for name, img in zip(mask_names, mask_images):
1377-
mask = nb.squeeze_image(img).get_data().astype(np.bool)
1380+
mask = np.asanyarray(nb.squeeze_image(img).dataobj).astype(np.bool)
13781381
if imgseries.shape[:3] != mask.shape:
13791382
raise ValueError(
13801383
"Inputs for CompCor, timeseries and mask, do not have "

nipype/algorithms/icc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class ICC(BaseInterface):
4141
output_spec = ICCOutputSpec
4242

4343
def _run_interface(self, runtime):
44-
maskdata = nb.load(self.inputs.mask).get_data()
44+
maskdata = nb.load(self.inputs.mask).get_fdata()
4545
maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata)))
4646

4747
session_datas = [
4848
[
49-
nb.load(fname, mmap=NUMPY_MMAP).get_data()[maskdata].reshape(-1, 1)
49+
nb.load(fname, mmap=NUMPY_MMAP).get_fdata()[maskdata].reshape(-1, 1)
5050
for fname in sessions
5151
]
5252
for sessions in self.inputs.subjects_sessions
@@ -66,17 +66,17 @@ def _run_interface(self, runtime):
6666

6767
nim = nb.load(self.inputs.subjects_sessions[0][0])
6868
new_data = np.zeros(nim.shape)
69-
new_data[maskdata] = icc.reshape(-1,)
69+
new_data[maskdata] = icc.reshape(-1)
7070
new_img = nb.Nifti1Image(new_data, nim.affine, nim.header)
7171
nb.save(new_img, "icc_map.nii")
7272

7373
new_data = np.zeros(nim.shape)
74-
new_data[maskdata] = session_var.reshape(-1,)
74+
new_data[maskdata] = session_var.reshape(-1)
7575
new_img = nb.Nifti1Image(new_data, nim.affine, nim.header)
7676
nb.save(new_img, "session_var_map.nii")
7777

7878
new_data = np.zeros(nim.shape)
79-
new_data[maskdata] = subject_var.reshape(-1,)
79+
new_data[maskdata] = subject_var.reshape(-1)
8080
new_img = nb.Nifti1Image(new_data, nim.affine, nim.header)
8181
nb.save(new_img, "subject_var_map.nii")
8282

nipype/algorithms/mesh.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ def _run_interface(self, runtime):
113113

114114
warps = []
115115
for axis in warp_dims:
116-
wdata = axis.get_data()
116+
wdata = axis.dataobj # four_to_three ensures this is an array
117117
if np.any(wdata != 0):
118-
119118
warp = ndimage.map_coordinates(wdata, voxpoints.transpose())
120119
else:
121120
warp = np.zeros((points.shape[0],))

nipype/algorithms/metrics.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ def _get_coordinates(self, data, affine):
8989
def _eucl_min(self, nii1, nii2):
9090
from scipy.spatial.distance import cdist, euclidean
9191

92-
origdata1 = nii1.get_data().astype(np.bool)
92+
origdata1 = np.asanyarray(nii1.dataobj).astype(np.bool)
9393
border1 = self._find_border(origdata1)
9494

95-
origdata2 = nii2.get_data().astype(np.bool)
95+
origdata2 = np.asanyarray(nii2.dataobj).astype(np.bool)
9696
border2 = self._find_border(origdata2)
9797

9898
set1_coordinates = self._get_coordinates(border1, nii1.affine)
@@ -111,16 +111,14 @@ def _eucl_cog(self, nii1, nii2):
111111
from scipy.spatial.distance import cdist
112112
from scipy.ndimage.measurements import center_of_mass, label
113113

114-
origdata1 = np.logical_and(
115-
nii1.get_data() != 0, np.logical_not(np.isnan(nii1.get_data()))
116-
)
117-
cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1)
114+
origdata1 = np.asanyarray(nii1.dataobj)
115+
origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1)
116+
cog_t = np.array(center_of_mass(origdata1)).reshape(-1, 1)
118117
cog_t = np.vstack((cog_t, np.array([1])))
119118
cog_t_coor = np.dot(nii1.affine, cog_t)[:3, :]
120119

121-
origdata2 = np.logical_and(
122-
nii2.get_data() != 0, np.logical_not(np.isnan(nii2.get_data()))
123-
)
120+
origdata2 = np.asanyarray(nii2.dataobj)
121+
origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2)
124122
(labeled_data, n_labels) = label(origdata2)
125123

126124
cogs = np.ones((4, n_labels))
@@ -137,10 +135,10 @@ def _eucl_cog(self, nii1, nii2):
137135
def _eucl_mean(self, nii1, nii2, weighted=False):
138136
from scipy.spatial.distance import cdist
139137

140-
origdata1 = nii1.get_data().astype(np.bool)
138+
origdata1 = np.asanyarray(nii1.dataobj).astype(np.bool)
141139
border1 = self._find_border(origdata1)
142140

143-
origdata2 = nii2.get_data().astype(np.bool)
141+
origdata2 = np.asanyarray(nii2.dataobj).astype(np.bool)
144142

145143
set1_coordinates = self._get_coordinates(border1, nii1.affine)
146144
set2_coordinates = self._get_coordinates(origdata2, nii2.affine)
@@ -159,26 +157,26 @@ def _eucl_mean(self, nii1, nii2, weighted=False):
159157
plt.close()
160158

161159
if weighted:
162-
return np.average(min_dist_matrix, weights=nii2.get_data()[origdata2].flat)
160+
return np.average(min_dist_matrix, weights=nii2.dataobj[origdata2].flat)
163161
else:
164162
return np.mean(min_dist_matrix)
165163

166164
def _eucl_max(self, nii1, nii2):
167165
from scipy.spatial.distance import cdist
168166

169-
origdata1 = nii1.get_data()
170-
origdata1 = np.logical_not(np.logical_or(origdata1 == 0, np.isnan(origdata1)))
171-
origdata2 = nii2.get_data()
172-
origdata2 = np.logical_not(np.logical_or(origdata2 == 0, np.isnan(origdata2)))
167+
origdata1 = np.asanyarray(nii1.dataobj)
168+
origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1)
169+
origdata2 = np.asanyarray(nii2.dataobj)
170+
origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2)
173171

174172
if isdefined(self.inputs.mask_volume):
175-
maskdata = nb.load(self.inputs.mask_volume).get_data()
176-
maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata)))
173+
maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj)
174+
maskdata = (np.rint(maskdata) != 0) & ~np.isnan(maskdata)
177175
origdata1 = np.logical_and(maskdata, origdata1)
178176
origdata2 = np.logical_and(maskdata, origdata2)
179177

180178
if origdata1.max() == 0 or origdata2.max() == 0:
181-
return np.NaN
179+
return np.nan
182180

183181
border1 = self._find_border(origdata1)
184182
border2 = self._find_border(origdata2)
@@ -302,19 +300,17 @@ def _run_interface(self, runtime):
302300
scale = 1.0
303301

304302
if self.inputs.vol_units == "mm":
305-
voxvol = nii1.header.get_zooms()
306-
for i in range(nii1.get_data().ndim - 1):
307-
scale = scale * voxvol[i]
303+
scale = np.prod(nii1.header.get_zooms()[:3])
308304

309-
data1 = nii1.get_data()
305+
data1 = np.asanyarray(nii1.dataobj)
310306
data1[np.logical_or(data1 < 0, np.isnan(data1))] = 0
311307
max1 = int(data1.max())
312308
data1 = data1.astype(np.min_scalar_type(max1))
313-
data2 = nii2.get_data().astype(np.min_scalar_type(max1))
309+
data2 = np.asanyarray(nii2.dataobj).astype(np.min_scalar_type(max1))
314310
data2[np.logical_or(data1 < 0, np.isnan(data1))] = 0
315311

316312
if isdefined(self.inputs.mask_volume):
317-
maskdata = nb.load(self.inputs.mask_volume).get_data()
313+
maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj)
318314
maskdata = ~np.logical_or(maskdata == 0, np.isnan(maskdata))
319315
data1[~maskdata] = 0
320316
data2[~maskdata] = 0
@@ -445,8 +441,8 @@ class FuzzyOverlap(SimpleInterface):
445441

446442
def _run_interface(self, runtime):
447443
# Load data
448-
refdata = nb.concat_images(self.inputs.in_ref).get_data()
449-
tstdata = nb.concat_images(self.inputs.in_tst).get_data()
444+
refdata = nb.concat_images(self.inputs.in_ref).dataobj
445+
tstdata = nb.concat_images(self.inputs.in_tst).dataobj
450446

451447
# Data must have same shape
452448
if not refdata.shape == tstdata.shape:
@@ -460,8 +456,7 @@ def _run_interface(self, runtime):
460456
# Load mask
461457
mask = np.ones_like(refdata, dtype=bool)
462458
if isdefined(self.inputs.in_mask):
463-
mask = nb.load(self.inputs.in_mask).get_data()
464-
mask = mask > 0
459+
mask = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) > 0
465460
mask = np.repeat(mask[..., np.newaxis], ncomp, -1)
466461
assert mask.shape == refdata.shape
467462

@@ -565,8 +560,8 @@ class ErrorMap(BaseInterface):
565560
def _run_interface(self, runtime):
566561
# Get two numpy data matrices
567562
nii_ref = nb.load(self.inputs.in_ref)
568-
ref_data = np.squeeze(nii_ref.get_data())
569-
tst_data = np.squeeze(nb.load(self.inputs.in_tst).get_data())
563+
ref_data = np.squeeze(nii_ref.dataobj)
564+
tst_data = np.squeeze(nb.load(self.inputs.in_tst).dataobj)
570565
assert ref_data.ndim == tst_data.ndim
571566

572567
# Load mask
@@ -578,7 +573,7 @@ def _run_interface(self, runtime):
578573
mapshape = ref_data.shape[:-1]
579574

580575
if isdefined(self.inputs.mask):
581-
msk = nb.load(self.inputs.mask).get_data()
576+
msk = np.asanyarray(nb.load(self.inputs.mask).dataobj)
582577
if mapshape != msk.shape:
583578
raise RuntimeError(
584579
"Mask should match volume shape, \
@@ -701,7 +696,7 @@ def _run_interface(self, runtime):
701696
vol1_nii = nb.load(self.inputs.volume1)
702697
vol2_nii = nb.load(self.inputs.volume2)
703698

704-
dims = vol1_nii.get_data().ndim
699+
dims = len(vol1_nii.shape)
705700

706701
if dims == 3 or dims == 2:
707702
vols1 = [vol1_nii]
@@ -716,12 +711,12 @@ def _run_interface(self, runtime):
716711
)
717712

718713
if isdefined(self.inputs.mask1):
719-
mask1 = nb.load(self.inputs.mask1).get_data() == 1
714+
mask1 = np.asanyarray(nb.load(self.inputs.mask1).dataobj) == 1
720715
else:
721716
mask1 = None
722717

723718
if isdefined(self.inputs.mask2):
724-
mask2 = nb.load(self.inputs.mask2).get_data() == 1
719+
mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 1
725720
else:
726721
mask2 = None
727722

0 commit comments

Comments
 (0)