From c1a69362fd0603268283c6bbf86aa3972fac683d Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 18 Nov 2019 10:17:42 -0500 Subject: [PATCH 01/15] ENH: Remove deprecated get_data() --- nipype/algorithms/metrics.py | 57 ++++++++++++++----------------- nipype/interfaces/image.py | 6 ++-- nipype/interfaces/nipy/model.py | 21 +++++------- nipype/interfaces/utility/base.py | 6 ++-- 4 files changed, 41 insertions(+), 49 deletions(-) diff --git a/nipype/algorithms/metrics.py b/nipype/algorithms/metrics.py index 7ee1ac5bfd..50cbe70161 100644 --- a/nipype/algorithms/metrics.py +++ b/nipype/algorithms/metrics.py @@ -89,10 +89,10 @@ def _get_coordinates(self, data, affine): def _eucl_min(self, nii1, nii2): from scipy.spatial.distance import cdist, euclidean - origdata1 = nii1.get_data().astype(np.bool) + origdata1 = np.asanyarray(nii1.dataobj).astype(np.bool) border1 = self._find_border(origdata1) - origdata2 = nii2.get_data().astype(np.bool) + origdata2 = np.asanyarray(nii2.dataobj).astype(np.bool) border2 = self._find_border(origdata2) set1_coordinates = self._get_coordinates(border1, nii1.affine) @@ -111,16 +111,14 @@ def _eucl_cog(self, nii1, nii2): from scipy.spatial.distance import cdist from scipy.ndimage.measurements import center_of_mass, label - origdata1 = np.logical_and( - nii1.get_data() != 0, np.logical_not(np.isnan(nii1.get_data())) - ) + origdata1 = np.asanyarray(nii1.dataobj) + origdata1 = (origdata1 != 0) & ~np.isnan(origdata1) cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1) cog_t = np.vstack((cog_t, np.array([1]))) cog_t_coor = np.dot(nii1.affine, cog_t)[:3, :] - origdata2 = np.logical_and( - nii2.get_data() != 0, np.logical_not(np.isnan(nii2.get_data())) - ) + origdata2 = np.asanyarray(nii2.dataobj) + origdata2 = (origdata2 != 0) & ~np.isnan(origdata2) (labeled_data, n_labels) = label(origdata2) cogs = np.ones((4, n_labels)) @@ -137,10 +135,10 @@ def _eucl_cog(self, nii1, nii2): def _eucl_mean(self, nii1, nii2, weighted=False): from scipy.spatial.distance import cdist - origdata1 = nii1.get_data().astype(np.bool) + origdata1 = np.asanyarray(nii1.dataobj).astype(np.bool) border1 = self._find_border(origdata1) - origdata2 = nii2.get_data().astype(np.bool) + origdata2 = np.asanyarray(nii2.dataobj).astype(np.bool) set1_coordinates = self._get_coordinates(border1, nii1.affine) set2_coordinates = self._get_coordinates(origdata2, nii2.affine) @@ -159,26 +157,26 @@ def _eucl_mean(self, nii1, nii2, weighted=False): plt.close() if weighted: - return np.average(min_dist_matrix, weights=nii2.get_data()[origdata2].flat) + return np.average(min_dist_matrix, weights=nii2.dataobj[origdata2].flat) else: return np.mean(min_dist_matrix) def _eucl_max(self, nii1, nii2): from scipy.spatial.distance import cdist - origdata1 = nii1.get_data() + origdata1 = np.asanyarray(nii1.dataobj) origdata1 = np.logical_not(np.logical_or(origdata1 == 0, np.isnan(origdata1))) - origdata2 = nii2.get_data() + origdata2 = np.asanyarray(nii2.dataobj) origdata2 = np.logical_not(np.logical_or(origdata2 == 0, np.isnan(origdata2))) if isdefined(self.inputs.mask_volume): - maskdata = nb.load(self.inputs.mask_volume).get_data() + maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj) maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata))) origdata1 = np.logical_and(maskdata, origdata1) origdata2 = np.logical_and(maskdata, origdata2) if origdata1.max() == 0 or origdata2.max() == 0: - return np.NaN + return np.nan border1 = self._find_border(origdata1) border2 = self._find_border(origdata2) @@ -302,19 +300,17 @@ def _run_interface(self, runtime): scale = 1.0 if self.inputs.vol_units == "mm": - voxvol = nii1.header.get_zooms() - for i in range(nii1.get_data().ndim - 1): - scale = scale * voxvol[i] + scale = np.prod(nii1.header.get_zooms()[:3]) - data1 = nii1.get_data() + data1 = np.asanyarray(nii1.dataobj) data1[np.logical_or(data1 < 0, np.isnan(data1))] = 0 max1 = int(data1.max()) data1 = data1.astype(np.min_scalar_type(max1)) - data2 = nii2.get_data().astype(np.min_scalar_type(max1)) + data2 = np.asanyarray(nii2.dataobj).astype(np.min_scalar_type(max1)) data2[np.logical_or(data1 < 0, np.isnan(data1))] = 0 if isdefined(self.inputs.mask_volume): - maskdata = nb.load(self.inputs.mask_volume).get_data() + maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj) maskdata = ~np.logical_or(maskdata == 0, np.isnan(maskdata)) data1[~maskdata] = 0 data2[~maskdata] = 0 @@ -445,8 +441,8 @@ class FuzzyOverlap(SimpleInterface): def _run_interface(self, runtime): # Load data - refdata = nb.concat_images(self.inputs.in_ref).get_data() - tstdata = nb.concat_images(self.inputs.in_tst).get_data() + refdata = np.asanyarray(nb.concat_images(self.inputs.in_ref).dataobj) + tstdata = np.asanyarray(nb.concat_images(self.inputs.in_tst).dataobj) # Data must have same shape if not refdata.shape == tstdata.shape: @@ -460,8 +456,7 @@ def _run_interface(self, runtime): # Load mask mask = np.ones_like(refdata, dtype=bool) if isdefined(self.inputs.in_mask): - mask = nb.load(self.inputs.in_mask).get_data() - mask = mask > 0 + mask = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) > 0 mask = np.repeat(mask[..., np.newaxis], ncomp, -1) assert mask.shape == refdata.shape @@ -565,8 +560,8 @@ class ErrorMap(BaseInterface): def _run_interface(self, runtime): # Get two numpy data matrices nii_ref = nb.load(self.inputs.in_ref) - ref_data = np.squeeze(nii_ref.get_data()) - tst_data = np.squeeze(nb.load(self.inputs.in_tst).get_data()) + ref_data = np.squeeze(nii_ref.dataobj) + tst_data = np.squeeze(nb.load(self.inputs.in_tst).dataobj) assert ref_data.ndim == tst_data.ndim # Load mask @@ -578,7 +573,7 @@ def _run_interface(self, runtime): mapshape = ref_data.shape[:-1] if isdefined(self.inputs.mask): - msk = nb.load(self.inputs.mask).get_data() + msk = np.asanyarray(nb.load(self.inputs.mask).dataobj) if mapshape != msk.shape: raise RuntimeError( "Mask should match volume shape, \ @@ -701,7 +696,7 @@ def _run_interface(self, runtime): vol1_nii = nb.load(self.inputs.volume1) vol2_nii = nb.load(self.inputs.volume2) - dims = vol1_nii.get_data().ndim + dims = len(vol1_nii.shape) if dims == 3 or dims == 2: vols1 = [vol1_nii] @@ -716,12 +711,12 @@ def _run_interface(self, runtime): ) if isdefined(self.inputs.mask1): - mask1 = nb.load(self.inputs.mask1).get_data() == 1 + mask1 = np.asanyarray(nb.load(self.inputs.mask1).dataobj) == 1 else: mask1 = None if isdefined(self.inputs.mask2): - mask2 = nb.load(self.inputs.mask2).get_data() == 1 + mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 2 else: mask2 = None diff --git a/nipype/interfaces/image.py b/nipype/interfaces/image.py index b3f3f433cd..a984efa159 100644 --- a/nipype/interfaces/image.py +++ b/nipype/interfaces/image.py @@ -68,8 +68,8 @@ def _run_interface(self, runtime): import nibabel as nb img = nb.load(self.inputs.in_file) - data = img.get_data() - ref_data = nb.load(self.inputs.ref_file).get_data() + data = img.get_fdata() + ref_data = nb.load(self.inputs.ref_file).get_fdata() in_mask = data > 0 ref_mask = ref_data > 0 @@ -232,7 +232,7 @@ def _as_reoriented_backport(img, ornt): if np.array_equal(ornt, [[0, 1], [1, 1], [2, 1]]): return img - t_arr = nb.apply_orientation(img.get_data(), ornt) + t_arr = nb.apply_orientation(img.dataobj, ornt) new_aff = img.affine.dot(inv_ornt_aff(ornt, img.shape)) reoriented = img.__class__(t_arr, new_aff, img.header) diff --git a/nipype/interfaces/nipy/model.py b/nipype/interfaces/nipy/model.py index c6287dac28..17f031d288 100644 --- a/nipype/interfaces/nipy/model.py +++ b/nipype/interfaces/nipy/model.py @@ -115,21 +115,19 @@ def _run_interface(self, runtime): if isinstance(functional_runs, (str, bytes)): functional_runs = [functional_runs] nii = nb.load(functional_runs[0]) - data = nii.get_data() + data = nii.get_fdata(caching='unchanged') if isdefined(self.inputs.mask): - mask = nb.load(self.inputs.mask).get_data() > 0 + mask = np.asanyarray(nb.load(self.inputs.mask).dataobj) > 0 else: mask = np.ones(nii.shape[:3]) == 1 - timeseries = data.copy()[mask, :] + timeseries = data[mask, :] del data for functional_run in functional_runs[1:]: - nii = nb.load(functional_run, mmap=NUMPY_MMAP) - data = nii.get_data() - npdata = data.copy() - del data + nii = nb.load(functional_run, mmap=False) + npdata = np.asarray(nii.dataobj) timeseries = np.concatenate((timeseries, npdata[mask, :]), axis=1) del npdata @@ -326,15 +324,14 @@ def _run_interface(self, runtime): beta_nii = nb.load(self.inputs.beta) if isdefined(self.inputs.mask): - mask = nb.load(self.inputs.mask).get_data() > 0 + mask = np.asanyarray(nb.load(self.inputs.mask).dataobj) > 0 else: mask = np.ones(beta_nii.shape[:3]) == 1 glm = GLM.GeneralLinearModel() - nii = nb.load(self.inputs.beta) - glm.beta = beta_nii.get_data().copy()[mask, :].T + glm.beta = np.array(beta_nii.dataobj)[mask, :].T glm.nvbeta = self.inputs.nvbeta - glm.s2 = nb.load(self.inputs.s2).get_data().copy()[mask] + glm.s2 = np.array(nb.load(self.inputs.s2).dataobj)[mask] glm.dof = self.inputs.dof glm._axis = self.inputs.axis glm._constants = self.inputs.constants @@ -358,7 +355,7 @@ def _run_interface(self, runtime): stat_map = np.zeros(mask.shape) stat_map[mask] = est_contrast.stat().T stat_map_file = os.path.abspath(name + "_stat_map.nii") - nb.save(nb.Nifti1Image(stat_map, nii.affine), stat_map_file) + nb.save(nb.Nifti1Image(stat_map, beta_nii.affine), stat_map_file) self._stat_maps.append(stat_map_file) p_map = np.zeros(mask.shape) diff --git a/nipype/interfaces/utility/base.py b/nipype/interfaces/utility/base.py index 9ff13011b7..5a59d95bad 100644 --- a/nipype/interfaces/utility/base.py +++ b/nipype/interfaces/utility/base.py @@ -423,9 +423,9 @@ class AssertEqual(BaseInterface): def _run_interface(self, runtime): import nibabel as nb - data1 = nb.load(self.inputs.volume1).get_data() - data2 = nb.load(self.inputs.volume2).get_data() + data1 = np.asanyarray(nb.load(self.inputs.volume1)) + data2 = np.asanyarray(nb.load(self.inputs.volume2)) - if not np.all(data1 == data2): + if not np.array_equal(data1, data2): raise RuntimeError("Input images are not exactly equal") return runtime From 36732e5329e9404d3dd455a3d633eb3887d612ed Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 18 Nov 2019 16:10:42 -0500 Subject: [PATCH 02/15] RF: Purge get_data from nipy, dipy interfaces --- nipype/interfaces/dipy/anisotropic_power.py | 4 ++-- nipype/interfaces/dipy/preprocess.py | 10 +++++----- nipype/interfaces/dipy/reconstruction.py | 20 +++++++++++--------- nipype/interfaces/dipy/simulate.py | 10 +++++----- nipype/interfaces/dipy/tensors.py | 6 +++--- nipype/interfaces/dipy/tracks.py | 6 +++--- nipype/interfaces/nipy/preprocess.py | 5 ++--- nipype/interfaces/nipy/utils.py | 4 ++-- 8 files changed, 33 insertions(+), 32 deletions(-) diff --git a/nipype/interfaces/dipy/anisotropic_power.py b/nipype/interfaces/dipy/anisotropic_power.py index 3982ed7106..06e25fb132 100644 --- a/nipype/interfaces/dipy/anisotropic_power.py +++ b/nipype/interfaces/dipy/anisotropic_power.py @@ -43,11 +43,11 @@ def _run_interface(self, runtime): gtab = self._get_gradient_table() img = nb.load(self.inputs.in_file) - data = img.get_data() + data = np.asanyarray(img.dataobj) affine = img.affine mask = None if isdefined(self.inputs.mask_file): - mask = nb.load(self.inputs.mask_file).get_data() + mask = np.asanyarray(nb.load(self.inputs.mask_file).dataobj) # Fit it model = shm.QballModel(gtab, 8) diff --git a/nipype/interfaces/dipy/preprocess.py b/nipype/interfaces/dipy/preprocess.py index 97d43e9220..3e89d1b88a 100644 --- a/nipype/interfaces/dipy/preprocess.py +++ b/nipype/interfaces/dipy/preprocess.py @@ -166,7 +166,7 @@ def _run_interface(self, runtime): settings = dict(mask=None, rician=(self.inputs.noise_model == "rician")) if isdefined(self.inputs.in_mask): - settings["mask"] = nb.load(self.inputs.in_mask).get_data() + settings["mask"] = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) if isdefined(self.inputs.patch_radius): settings["patch_radius"] = self.inputs.patch_radius @@ -180,10 +180,10 @@ def _run_interface(self, runtime): signal_mask = None if isdefined(self.inputs.signal_mask): - signal_mask = nb.load(self.inputs.signal_mask).get_data() + signal_mask = np.asanyarray(nb.load(self.inputs.signal_mask).dataobj) noise_mask = None if isdefined(self.inputs.noise_mask): - noise_mask = nb.load(self.inputs.noise_mask).get_data() + noise_mask = np.asanyarray(nb.load(self.inputs.noise_mask).dataobj) _, s = nlmeans_proxy( self.inputs.in_file, @@ -224,7 +224,7 @@ def resample_proxy(in_file, order=3, new_zooms=None, out_file=None): img = nb.load(in_file, mmap=NUMPY_MMAP) hdr = img.header.copy() - data = img.get_data().astype(np.float32) + data = img.get_fdata(dtype=np.float32) affine = img.affine im_zooms = hdr.get_zooms()[:3] @@ -264,7 +264,7 @@ def nlmeans_proxy(in_file, settings, snr=None, smask=None, nmask=None, out_file= img = nb.load(in_file, mmap=NUMPY_MMAP) hdr = img.header - data = img.get_data() + data = img.get_fdata() aff = img.affine if data.ndim < 4: diff --git a/nipype/interfaces/dipy/reconstruction.py b/nipype/interfaces/dipy/reconstruction.py index ae60aab143..27a9e632ad 100644 --- a/nipype/interfaces/dipy/reconstruction.py +++ b/nipype/interfaces/dipy/reconstruction.py @@ -92,17 +92,19 @@ def _run_interface(self, runtime): img = nb.load(self.inputs.in_file) hdr = img.header.copy() affine = img.affine - data = img.get_data() + data = img.get_fdata() gtab = self._get_gradient_table() if isdefined(self.inputs.in_mask): - msk = nb.load(self.inputs.in_mask).get_data().astype(np.uint8) + msk = np.asanyarray(nb.load(self.inputs.in_mask).dataobj).astype(np.uint8) else: msk = np.ones(data.shape[:3], dtype=np.uint8) try_b0 = True if isdefined(self.inputs.noise_mask): - noise_msk = nb.load(self.inputs.noise_mask).get_data().reshape(-1) + noise_msk = ( + nb.load(self.inputs.noise_mask).get_fdata(dtype=np.float32).reshape(-1) + ) noise_msk[noise_msk > 0.5] = 1 noise_msk[noise_msk < 1.0] = 0 noise_msk = noise_msk.astype(np.uint8) @@ -232,16 +234,16 @@ def _run_interface(self, runtime): affine = img.affine if isdefined(self.inputs.in_mask): - msk = nb.load(self.inputs.in_mask).get_data() + msk = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) msk[msk > 0] = 1 msk[msk < 0] = 0 else: msk = np.ones(imref.shape) - data = img.get_data().astype(np.float32) + data = img.get_fdata(dtype=np.float32) gtab = self._get_gradient_table() - evals = np.nan_to_num(nb.load(self.inputs.in_evals).get_data()) + evals = np.nan_to_num(nb.load(self.inputs.in_evals).dataobj) FA = np.nan_to_num(fractional_anisotropy(evals)) * msk indices = np.where(FA > self.inputs.fa_thresh) S0s = data[indices][:, np.nonzero(gtab.b0s_mask)[0]] @@ -260,7 +262,7 @@ def _run_interface(self, runtime): indices = np.logical_or( FA >= 0.4, (np.logical_and(FA >= 0.15, MD >= 0.0011)) ) - data = nb.load(self.inputs.in_file).get_data() + data = np.asanyarray(nb.load(self.inputs.in_file).dataobj) response = recursive_response( gtab, data, @@ -359,11 +361,11 @@ def _run_interface(self, runtime): imref = nb.four_to_three(img)[0] if isdefined(self.inputs.in_mask): - msk = nb.load(self.inputs.in_mask).get_data() + msk = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) else: msk = np.ones(imref.shape) - data = img.get_data().astype(np.float32) + data = img.get_fdata(dtype=np.float32) gtab = self._get_gradient_table() resp_file = np.loadtxt(self.inputs.response) diff --git a/nipype/interfaces/dipy/simulate.py b/nipype/interfaces/dipy/simulate.py index d9f0ed1023..a14e979b45 100644 --- a/nipype/interfaces/dipy/simulate.py +++ b/nipype/interfaces/dipy/simulate.py @@ -143,7 +143,7 @@ def _run_interface(self, runtime): vfs = np.squeeze( nb.concat_images( [nb.load(f, mmap=NUMPY_MMAP) for f in self.inputs.in_vfms] - ).get_data() + ).dataobj ) if nballs == 1: vfs = vfs[..., np.newaxis] @@ -151,7 +151,7 @@ def _run_interface(self, runtime): # Generate a mask if isdefined(self.inputs.in_mask): - msk = nb.load(self.inputs.in_mask).get_data() + msk = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) msk[msk > 0.0] = 1.0 msk[msk < 1.0] = 0.0 else: @@ -165,7 +165,7 @@ def _run_interface(self, runtime): ffsim = nb.concat_images( [nb.load(f, mmap=NUMPY_MMAP) for f in self.inputs.in_frac] ) - ffs = np.nan_to_num(np.squeeze(ffsim.get_data())) # fiber fractions + ffs = np.nan_to_num(np.squeeze(ffsim.dataobj)) # fiber fractions ffs = np.clip(ffs, 0.0, 1.0) if nsticks == 1: ffs = ffs[..., np.newaxis] @@ -207,7 +207,7 @@ def _run_interface(self, runtime): dirs = None for i in range(nsticks): f = self.inputs.in_dirs[i] - fd = np.nan_to_num(nb.load(f, mmap=NUMPY_MMAP).get_data()) + fd = np.nan_to_num(nb.load(f, mmap=NUMPY_MMAP).dataobj) w = np.linalg.norm(fd, axis=3)[..., np.newaxis] w[w < np.finfo(float).eps] = 1.0 fd /= w @@ -230,7 +230,7 @@ def _run_interface(self, runtime): mevals = [sf_evals] * nsticks + [[ba_evals[d]] * 3 for d in range(nballs)] - b0 = b0_im.get_data()[msk > 0] + b0 = b0_im.get_fdata()[msk > 0] args = [] for i in range(nvox): args.append( diff --git a/nipype/interfaces/dipy/tensors.py b/nipype/interfaces/dipy/tensors.py index 2f9ad95f5b..e8f2029881 100644 --- a/nipype/interfaces/dipy/tensors.py +++ b/nipype/interfaces/dipy/tensors.py @@ -47,11 +47,11 @@ def _run_interface(self, runtime): gtab = self._get_gradient_table() img = nb.load(self.inputs.in_file) - data = img.get_data() + data = img.get_fdata() affine = img.affine mask = None if isdefined(self.inputs.mask_file): - mask = nb.load(self.inputs.mask_file).get_data() + mask = np.asanyarray(nb.load(self.inputs.mask_file).dataobj) # Fit it tenmodel = dti.TensorModel(gtab) @@ -120,7 +120,7 @@ def _run_interface(self, runtime): # Load the 4D image files img = nb.load(self.inputs.in_file) - data = img.get_data() + data = img.get_fdata() affine = img.affine # Load the gradient strengths and directions diff --git a/nipype/interfaces/dipy/tracks.py b/nipype/interfaces/dipy/tracks.py index 947bf22121..9ac9e0b59c 100644 --- a/nipype/interfaces/dipy/tracks.py +++ b/nipype/interfaces/dipy/tracks.py @@ -233,7 +233,7 @@ def _run_interface(self, runtime): imref = nb.four_to_three(img)[0] affine = img.affine - data = img.get_data().astype(np.float32) + data = img.get_fdata(dtype=np.float32) hdr = imref.header.copy() hdr.set_data_dtype(np.float32) hdr["data_type"] = 16 @@ -274,7 +274,7 @@ def _run_interface(self, runtime): IFLOGGER.info("Performing tractography") if isdefined(self.inputs.tracking_mask): - msk = nb.load(self.inputs.tracking_mask).get_data() + msk = np.asanyarray(nb.load(self.inputs.tracking_mask).dataobj) msk[msk > 0] = 1 msk[msk < 0] = 0 else: @@ -287,7 +287,7 @@ def _run_interface(self, runtime): seeds = np.loadtxt(self.inputs.seed_coord) elif isdefined(self.inputs.seed_mask): - seedmsk = nb.load(self.inputs.seed_mask).get_data() + seedmsk = np.asanyarray(nb.load(self.inputs.seed_mask).dataobj) assert seedmsk.shape == data.shape[:3] seedmsk[seedmsk > 0] = 1 seedmsk[seedmsk < 1] = 0 diff --git a/nipype/interfaces/nipy/preprocess.py b/nipype/interfaces/nipy/preprocess.py index 3da52fbd04..b22837d6a3 100644 --- a/nipype/interfaces/nipy/preprocess.py +++ b/nipype/interfaces/nipy/preprocess.py @@ -57,8 +57,7 @@ def _run_interface(self, runtime): value = getattr(self.inputs, key) if isdefined(value): if key in ["mean_volume", "reference_volume"]: - nii = nb.load(value, mmap=NUMPY_MMAP) - value = nii.get_data() + value = np.asanyarray(nb.load(value, mmap=NUMPY_MMAP).dataobj) args[key] = value brain_mask = compute_mask(**args) @@ -264,7 +263,7 @@ def _run_interface(self, runtime): s = slice(self.inputs.begin_index, nii.shape[3]) else: s = slice(self.inputs.begin_index, self.inputs.end_index) - nii2 = nb.Nifti1Image(nii.get_data()[..., s], nii.affine, nii.header) + nii2 = nb.Nifti1Image(nii.dataobj[..., s], nii.affine, nii.header) nb.save(nii2, out_file) return runtime diff --git a/nipype/interfaces/nipy/utils.py b/nipype/interfaces/nipy/utils.py index 23be89b4f5..ec9893453a 100644 --- a/nipype/interfaces/nipy/utils.py +++ b/nipype/interfaces/nipy/utils.py @@ -74,12 +74,12 @@ def _run_interface(self, runtime): vol2_nii = nb.load(self.inputs.volume2) if isdefined(self.inputs.mask1): - mask1 = nb.load(self.inputs.mask1).get_data() == 1 + mask1 = np.asanyarray(nb.load(self.inputs.mask1).dataobj) == 1 else: mask1 = None if isdefined(self.inputs.mask2): - mask2 = nb.load(self.inputs.mask2).get_data() == 1 + mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 2 else: mask2 = None From 77aa4d92dbe4addd52aef8fbed74f2127ef4babf Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 11:21:39 -0500 Subject: [PATCH 03/15] RF: Purge get_data() from algorithms.misc --- nipype/algorithms/misc.py | 56 ++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index 723b4d83d4..7c35f6c5f8 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -94,7 +94,7 @@ def _gen_output_filename(self): def _get_brodmann_area(self): nii = nb.load(self.inputs.atlas) - origdata = nii.get_data() + origdata = np.asanyarray(nii.dataobj) newdata = np.zeros(origdata.shape) if not isinstance(self.inputs.labels, list): @@ -153,7 +153,7 @@ class SimpleThreshold(BaseInterface): def _run_interface(self, runtime): for fname in self.inputs.volumes: img = nb.load(fname, mmap=NUMPY_MMAP) - data = np.array(img.get_data()) + data = img.get_fdata() active_map = data > self.inputs.threshold @@ -216,7 +216,7 @@ def _run_interface(self, runtime): affine = np.dot(self.inputs.transformation_matrix, affine) nb.save( - nb.Nifti1Image(img.get_data(), affine, img.header), + nb.Nifti1Image(img.dataobj, affine, img.header), self._gen_output_filename(fname), ) @@ -1014,11 +1014,11 @@ class AddNoise(BaseInterface): def _run_interface(self, runtime): in_image = nb.load(self.inputs.in_file) - in_data = in_image.get_data() + in_data = in_image.get_fdata() snr = self.inputs.snr if isdefined(self.inputs.in_mask): - in_mask = nb.load(self.inputs.in_mask).get_data() + in_mask = np.asanyarray(nb.load(self.inputs.in_mask).dataobj) else: in_mask = np.ones_like(in_data) @@ -1272,27 +1272,25 @@ def normalize_tpms(in_files, in_mask=None, out_files=None): imgs = [nb.load(fim, mmap=NUMPY_MMAP) for fim in in_files] if len(in_files) == 1: - img_data = imgs[0].get_data() + img_data = imgs[0].get_fdata(dtype=np.float32) img_data[img_data > 0.0] = 1.0 hdr = imgs[0].header.copy() - hdr["data_type"] = 16 hdr.set_data_dtype(np.float32) - nb.save( - nb.Nifti1Image(img_data.astype(np.float32), imgs[0].affine, hdr), - out_files[0], - ) + nb.save(nb.Nifti1Image(img_data, imgs[0].affine, hdr), out_files[0]) return out_files[0] - img_data = np.array([im.get_data() for im in imgs]).astype(np.float32) + img_data = np.stack( + [im.get_fdata(caching="unchanged", dtype=np.float32) for im in imgs] + ) # img_data[img_data>1.0] = 1.0 img_data[img_data < 0.0] = 0.0 weights = np.sum(img_data, axis=0) - msk = np.ones_like(imgs[0].get_data()) + msk = np.ones(imgs[0].shape) msk[weights <= 0] = 0 if in_mask is not None: - msk = nb.load(in_mask, mmap=NUMPY_MMAP).get_data() + msk = np.asanyarray(nb.load(in_mask, mmap=NUMPY_MMAP).dataobj) msk[msk <= 0] = 0 msk[msk > 0] = 1 @@ -1302,7 +1300,6 @@ def normalize_tpms(in_files, in_mask=None, out_files=None): data = np.ma.masked_equal(img_data[i], 0) probmap = data / weights hdr = imgs[i].header.copy() - hdr["data_type"] = 16 hdr.set_data_dtype("float32") nb.save( nb.Nifti1Image(probmap.astype(np.float32), imgs[i].affine, hdr), out_file @@ -1331,7 +1328,7 @@ def split_rois(in_file, mask=None, roishape=None): droishape = (roishape[0], roishape[1], roishape[2], nvols) if mask is not None: - mask = nb.load(mask, mmap=NUMPY_MMAP).get_data() + mask = np.asanyarray(nb.load(mask, mmap=NUMPY_MMAP).dataobj) mask[mask > 0] = 1 mask[mask < 1] = 0 else: @@ -1342,7 +1339,7 @@ def split_rois(in_file, mask=None, roishape=None): els = np.sum(mask) nrois = int(ceil(els / float(roisize))) - data = im.get_data().reshape((mask.size, -1)) + data = np.asanyarray(im.dataobj).reshape((mask.size, -1)) data = np.squeeze(data.take(nzels, axis=0)) nvols = data.shape[-1] @@ -1420,10 +1417,10 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): rsh = ref.shape del ref npix = rsh[0] * rsh[1] * rsh[2] - fcdata = nb.load(in_files[0]).get_data() + fcimg = nb.load(in_files[0]) - if fcdata.ndim == 4: - ndirs = fcdata.shape[-1] + if len(fcimg.shape) == 4: + ndirs = fcimg.shape[-1] else: ndirs = 1 newshape = (rsh[0], rsh[1], rsh[2], ndirs) @@ -1431,11 +1428,13 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): hdr.set_xyzt_units("mm", "sec") if ndirs < 300: - data = np.zeros((npix, ndirs)) + data = np.zeros((npix, ndirs), dtype=dtype) for cname, iname in zip(in_files, in_idxs): f = np.load(iname) idxs = np.squeeze(f["arr_0"]) - cdata = nb.load(cname, mmap=NUMPY_MMAP).get_data().reshape(-1, ndirs) + cdata = np.asanyarray(nb.load(cname, mmap=NUMPY_MMAP).dataobj).reshape( + -1, ndirs + ) nels = len(idxs) idata = (idxs,) try: @@ -1450,10 +1449,7 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): ) raise - hdr.set_data_shape(newshape) - nb.Nifti1Image(data.reshape(newshape).astype(dtype), aff, hdr).to_filename( - out_file - ) + nb.Nifti1Image(data.reshape(newshape), aff, hdr).to_filename(out_file) else: hdr.set_data_shape(rsh[:3]) @@ -1468,10 +1464,10 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): idxs = np.squeeze(f["arr_0"]) for d, fname in enumerate(nii): - data = nb.load(fname, mmap=NUMPY_MMAP).get_data().reshape(-1) - cdata = ( - nb.load(cname, mmap=NUMPY_MMAP).get_data().reshape(-1, ndirs)[:, d] + data = np.asanyarray(nb.load(fname, mmap=NUMPY_MMAP).dataobj).reshape( + -1 ) + cdata = nb.load(cname, mmap=NUMPY_MMAP).dataobj[..., d].reshape(-1) nels = len(idxs) idata = (idxs,) data[idata] = cdata[0:nels] @@ -1552,7 +1548,7 @@ def _run_interface(self, runtime): self._median_files = [] for idx, fname in enumerate(ensure_list(self.inputs.in_files)): img = nb.load(fname, mmap=NUMPY_MMAP) - data = np.median(img.get_data(), axis=3) + data = np.median(img.get_fdata(), axis=3) if self.inputs.median_per_file: self._median_files.append(self._write_nifti(img, data, idx)) else: From 4b618ec4deb160dc06ccea14ed2d0860a05513e2 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 12:03:33 -0500 Subject: [PATCH 04/15] TEST: Purge get_data() from algorithms.tests.* --- nipype/algorithms/tests/test_CompCor.py | 11 +++++------ nipype/algorithms/tests/test_normalize_tpms.py | 8 ++++---- nipype/algorithms/tests/test_splitmerge.py | 7 ++++--- nipype/algorithms/tests/test_stats.py | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nipype/algorithms/tests/test_CompCor.py b/nipype/algorithms/tests/test_CompCor.py index 762d8a0889..314b9ed9bf 100644 --- a/nipype/algorithms/tests/test_CompCor.py +++ b/nipype/algorithms/tests/test_CompCor.py @@ -124,7 +124,7 @@ def test_tcompcor_no_percentile(self): ccinterface = TCompCor(num_components=6, realigned_file=self.realigned_file) ccinterface.run() - mask = nb.load("mask_000.nii.gz").get_data() + mask = nb.load("mask_000.nii.gz").dataobj num_nonmasked_voxels = np.count_nonzero(mask) assert num_nonmasked_voxels == 1 @@ -153,7 +153,7 @@ def test_tcompcor_asymmetric_dim(self): ) TCompCor(realigned_file=asymmetric_data).run() - assert nb.load("mask_000.nii.gz").get_data().shape == asymmetric_shape[:3] + assert nb.load("mask_000.nii.gz").shape == asymmetric_shape[:3] def test_compcor_bad_input_shapes(self): # dim 0 is < dim 0 of self.mask_files (2) @@ -183,12 +183,12 @@ def test_tcompcor_merge_intersect_masks(self): ).run() if method == "union": assert np.array_equal( - nb.load("mask_000.nii.gz").get_data(), + nb.load("mask_000.nii.gz").dataobj, ([[[0, 0], [0, 0]], [[0, 0], [1, 0]]]), ) if method == "intersect": assert np.array_equal( - nb.load("mask_000.nii.gz").get_data(), + nb.load("mask_000.nii.gz").dataobj, ([[[0, 0], [0, 0]], [[0, 1], [0, 0]]]), ) @@ -197,8 +197,7 @@ def test_tcompcor_index_mask(self): realigned_file=self.realigned_file, mask_files=self.mask_files, mask_index=1 ).run() assert np.array_equal( - nb.load("mask_000.nii.gz").get_data(), - ([[[0, 0], [0, 0]], [[0, 1], [0, 0]]]), + nb.load("mask_000.nii.gz").dataobj, ([[[0, 0], [0, 0]], [[0, 1], [0, 0]]]) ) def test_tcompcor_multi_mask_no_index(self): diff --git a/nipype/algorithms/tests/test_normalize_tpms.py b/nipype/algorithms/tests/test_normalize_tpms.py index 7ff482f23f..fac9ea7286 100644 --- a/nipype/algorithms/tests/test_normalize_tpms.py +++ b/nipype/algorithms/tests/test_normalize_tpms.py @@ -19,7 +19,7 @@ def test_normalize_tpms(tmpdir): in_mask = example_data("tpms_msk.nii.gz") - mskdata = nb.load(in_mask, mmap=NUMPY_MMAP).get_data() + mskdata = np.asanyarray(nb.load(in_mask, mmap=NUMPY_MMAP).dataobj) mskdata[mskdata > 0.0] = 1.0 mapdata = [] @@ -32,8 +32,8 @@ def test_normalize_tpms(tmpdir): out_files.append(tmpdir.join("normtpm_%02d.nii.gz" % i).strpath) im = nb.load(mapname, mmap=NUMPY_MMAP) - data = im.get_data() - mapdata.append(data.copy()) + data = im.get_fdata() + mapdata.append(data) nb.Nifti1Image(2.0 * (data * mskdata), im.affine, im.header).to_filename( filename @@ -45,7 +45,7 @@ def test_normalize_tpms(tmpdir): sumdata = np.zeros_like(mskdata) for i, tstfname in enumerate(out_files): - normdata = nb.load(tstfname, mmap=NUMPY_MMAP).get_data() + normdata = nb.load(tstfname, mmap=NUMPY_MMAP).get_fdata() sumdata += normdata assert np.all(normdata[mskdata == 0] == 0) assert np.allclose(normdata, mapdata[i]) diff --git a/nipype/algorithms/tests/test_splitmerge.py b/nipype/algorithms/tests/test_splitmerge.py index 96e60c6cbb..4305eeede0 100644 --- a/nipype/algorithms/tests/test_splitmerge.py +++ b/nipype/algorithms/tests/test_splitmerge.py @@ -15,8 +15,9 @@ def test_split_and_merge(tmpdir): in_mask = example_data("tpms_msk.nii.gz") dwfile = tmpdir.join("dwi.nii.gz").strpath - mskdata = nb.load(in_mask, mmap=NUMPY_MMAP).get_data() - aff = nb.load(in_mask, mmap=NUMPY_MMAP).affine + mask_img = nb.load(in_mask, mmap=NUMPY_MMAP) + mskdata = np.asanyarray(mask_img.dataobj) + aff = mask_img.affine dwshape = (mskdata.shape[0], mskdata.shape[1], mskdata.shape[2], 6) dwdata = np.random.normal(size=dwshape) @@ -25,7 +26,7 @@ def test_split_and_merge(tmpdir): resdw, resmsk, resid = split_rois(dwfile, in_mask, roishape=(20, 20, 2)) merged = merge_rois(resdw, resid, in_mask) - dwmerged = nb.load(merged, mmap=NUMPY_MMAP).get_data() + dwmerged = nb.load(merged, mmap=NUMPY_MMAP).get_fdata(dtype=np.float32) dwmasked = dwdata * mskdata[:, :, :, np.newaxis] diff --git a/nipype/algorithms/tests/test_stats.py b/nipype/algorithms/tests/test_stats.py index 29305e7a7a..752fadf307 100644 --- a/nipype/algorithms/tests/test_stats.py +++ b/nipype/algorithms/tests/test_stats.py @@ -19,7 +19,7 @@ def test_ActivationCount(tmpdir): diff = nb.load(res.outputs.out_file) pos = nb.load(res.outputs.acm_pos) neg = nb.load(res.outputs.acm_neg) - assert np.allclose(diff.get_data(), pos.get_data() - neg.get_data()) + assert np.allclose(diff.get_fdata(), pos.get_fdata() - neg.get_fdata()) @pytest.mark.parametrize( @@ -43,8 +43,8 @@ def test_ActivationCount_normaldistr(tmpdir, threshold, above_thresh): pos = nb.load(res.outputs.acm_pos) neg = nb.load(res.outputs.acm_neg) assert np.isclose( - pos.get_data().mean(), above_thresh * 1.0e-2, rtol=0.1, atol=1.0e-4 + pos.get_fdata().mean(), above_thresh * 1.0e-2, rtol=0.1, atol=1.0e-4 ) assert np.isclose( - neg.get_data().mean(), above_thresh * 1.0e-2, rtol=0.1, atol=1.0e-4 + neg.get_fdata().mean(), above_thresh * 1.0e-2, rtol=0.1, atol=1.0e-4 ) From f737c267942d27276579ecd2de980001d549cae1 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 12:07:34 -0500 Subject: [PATCH 05/15] RF: Purge get_data from algorithms.confounds Selecting dtype=float32 for loading full time series --- nipype/algorithms/confounds.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/nipype/algorithms/confounds.py b/nipype/algorithms/confounds.py index 428812b842..9478ddbbbf 100644 --- a/nipype/algorithms/confounds.py +++ b/nipype/algorithms/confounds.py @@ -623,12 +623,10 @@ def _run_interface(self, runtime): skip_vols = self.inputs.ignore_initial_volumes if skip_vols: imgseries = imgseries.__class__( - imgseries.get_data()[..., skip_vols:], - imgseries.affine, - imgseries.header, + imgseries.dataobj[..., skip_vols:], imgseries.affine, imgseries.header ) - mask_images = self._process_masks(mask_images, imgseries.get_data()) + mask_images = self._process_masks(mask_images, imgseries.dataobj) TR = 0 if self.inputs.pre_filter == "cosine": @@ -664,7 +662,7 @@ def _run_interface(self, runtime): ) components, filter_basis, metadata = compute_noise_components( - imgseries.get_data(), + imgseries.get_fdata(dtype=np.float32), mask_images, components_criterion, self.inputs.pre_filter, @@ -837,8 +835,9 @@ def __init__(self, *args, **kwargs): def _process_masks(self, mask_images, timeseries=None): out_images = [] self._mask_files = [] + timeseries = np.asanyarray(timeseries) for i, img in enumerate(mask_images): - mask = img.get_data().astype(np.bool) + mask = np.asanyarray(img.dataobj).astype(np.bool) imgseries = timeseries[mask, :] imgseries = regress_poly(2, imgseries)[0] tSTD = _compute_tSTD(imgseries, 0, axis=-1) @@ -924,7 +923,11 @@ def _run_interface(self, runtime): nb.load(filename, mmap=NUMPY_MMAP) for filename in self.inputs.in_file ] data = np.concatenate( - [vol.get_data().reshape(vol.shape[:3] + (-1,)) for vol in vollist], axis=3 + [ + vol.get_fdata(dtype=np.float32).reshape(vol.shape[:3] + (-1,)) + for vol in vollist + ], + axis=3, ) data = np.nan_to_num(data) @@ -985,7 +988,7 @@ class NonSteadyStateDetector(BaseInterface): def _run_interface(self, runtime): in_nii = nb.load(self.inputs.in_file) global_signal = ( - in_nii.get_data()[:, :, :, :50].mean(axis=0).mean(axis=0).mean(axis=0) + in_nii.dataobj[:, :, :, :50].mean(axis=0).mean(axis=0).mean(axis=0) ) self._results = {"n_volumes_to_discard": is_outlier(global_signal)} @@ -1032,8 +1035,8 @@ def compute_dvars( from nitime.algorithms import AR_est_YW import warnings - func = nb.load(in_file, mmap=NUMPY_MMAP).get_data().astype(np.float32) - mask = nb.load(in_mask, mmap=NUMPY_MMAP).get_data().astype(np.uint8) + func = nb.load(in_file, mmap=NUMPY_MMAP).get_fdata(dtype=np.float32) + mask = np.asanyarray(nb.load(in_mask, mmap=NUMPY_MMAP).dataobj).astype(np.uint8) if len(func.shape) != 4: raise RuntimeError("Input fMRI dataset should be 4-dimensional") @@ -1280,8 +1283,8 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None): for filename in mask_files: img = nb.load(filename, mmap=NUMPY_MMAP) if mask is None: - mask = img.get_data() > 0 - np.logical_or(mask, img.get_data() > 0, mask) + mask = img.get_fdata() > 0 + np.logical_or(mask, img.get_fdata() > 0, mask) img = nb.Nifti1Image(mask, img.affine, header=img.header) return [img] @@ -1290,8 +1293,8 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None): for filename in mask_files: img = nb.load(filename, mmap=NUMPY_MMAP) if mask is None: - mask = img.get_data() > 0 - np.logical_and(mask, img.get_data() > 0, mask) + mask = img.get_fdata() > 0 + np.logical_and(mask, img.get_fdata() > 0, mask) img = nb.Nifti1Image(mask, img.affine, header=img.header) return [img] @@ -1374,7 +1377,7 @@ def compute_noise_components( md_retained = [] for name, img in zip(mask_names, mask_images): - mask = nb.squeeze_image(img).get_data().astype(np.bool) + mask = np.asanyarray(nb.squeeze_image(img).dataobj).astype(np.bool) if imgseries.shape[:3] != mask.shape: raise ValueError( "Inputs for CompCor, timeseries and mask, do not have " From 68382d2e3b807f2ee0ecf04d4eaf27b4357e2a9b Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 12:35:22 -0500 Subject: [PATCH 06/15] RF: Finish purging get_data() from algorithms --- nipype/algorithms/icc.py | 10 +++++----- nipype/algorithms/mesh.py | 3 +-- nipype/algorithms/rapidart.py | 4 ++-- nipype/algorithms/stats.py | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/nipype/algorithms/icc.py b/nipype/algorithms/icc.py index a3ae57edf8..cd73caa0ca 100644 --- a/nipype/algorithms/icc.py +++ b/nipype/algorithms/icc.py @@ -41,12 +41,12 @@ class ICC(BaseInterface): output_spec = ICCOutputSpec def _run_interface(self, runtime): - maskdata = nb.load(self.inputs.mask).get_data() + maskdata = nb.load(self.inputs.mask).get_fdata() maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata))) session_datas = [ [ - nb.load(fname, mmap=NUMPY_MMAP).get_data()[maskdata].reshape(-1, 1) + nb.load(fname, mmap=NUMPY_MMAP).get_fdata()[maskdata].reshape(-1, 1) for fname in sessions ] for sessions in self.inputs.subjects_sessions @@ -66,17 +66,17 @@ def _run_interface(self, runtime): nim = nb.load(self.inputs.subjects_sessions[0][0]) new_data = np.zeros(nim.shape) - new_data[maskdata] = icc.reshape(-1,) + new_data[maskdata] = icc.reshape(-1) new_img = nb.Nifti1Image(new_data, nim.affine, nim.header) nb.save(new_img, "icc_map.nii") new_data = np.zeros(nim.shape) - new_data[maskdata] = session_var.reshape(-1,) + new_data[maskdata] = session_var.reshape(-1) new_img = nb.Nifti1Image(new_data, nim.affine, nim.header) nb.save(new_img, "session_var_map.nii") new_data = np.zeros(nim.shape) - new_data[maskdata] = subject_var.reshape(-1,) + new_data[maskdata] = subject_var.reshape(-1) new_img = nb.Nifti1Image(new_data, nim.affine, nim.header) nb.save(new_img, "subject_var_map.nii") diff --git a/nipype/algorithms/mesh.py b/nipype/algorithms/mesh.py index 3732f88548..ab70237030 100644 --- a/nipype/algorithms/mesh.py +++ b/nipype/algorithms/mesh.py @@ -113,9 +113,8 @@ def _run_interface(self, runtime): warps = [] for axis in warp_dims: - wdata = axis.get_data() + wdata = axis.dataobj # four_to_three ensures this is an array if np.any(wdata != 0): - warp = ndimage.map_coordinates(wdata, voxpoints.transpose()) else: warp = np.zeros((points.shape[0],)) diff --git a/nipype/algorithms/rapidart.py b/nipype/algorithms/rapidart.py index 3f02ca8d29..bb03ee9f7d 100644 --- a/nipype/algorithms/rapidart.py +++ b/nipype/algorithms/rapidart.py @@ -496,7 +496,7 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None): # compute global intensity signal (x, y, z, timepoints) = nim.shape - data = nim.get_data() + data = nim.get_fdata(dtype=np.float32) affine = nim.affine g = np.zeros((timepoints, 1)) masktype = self.inputs.mask_type @@ -526,7 +526,7 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None): g[t0] = np.nansum(vol * mask_tmp) / np.nansum(mask_tmp) elif masktype == "file": # uses a mask image to determine intensity maskimg = load(self.inputs.mask_file, mmap=NUMPY_MMAP) - mask = maskimg.get_data() + mask = maskimg.get_fdata() affine = maskimg.affine mask = mask > 0.5 for t0 in range(timepoints): diff --git a/nipype/algorithms/stats.py b/nipype/algorithms/stats.py index 2a8b00f614..29ce8d6be4 100644 --- a/nipype/algorithms/stats.py +++ b/nipype/algorithms/stats.py @@ -50,7 +50,7 @@ class ActivationCount(SimpleInterface): output_spec = ActivationCountOutputSpec def _run_interface(self, runtime): - allmaps = nb.concat_images(self.inputs.in_files).get_data() + allmaps = nb.concat_images(self.inputs.in_files).dataobj acm_pos = np.mean(allmaps > self.inputs.threshold, axis=3, dtype=np.float32) acm_neg = np.mean( allmaps < -1.0 * self.inputs.threshold, axis=3, dtype=np.float32 From 530143e6caa23f323589fa398d5b1f2c71338918 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 13:06:19 -0500 Subject: [PATCH 07/15] RF: Purge get_data() from interfaces.cmtk --- nipype/interfaces/cmtk/cmtk.py | 13 +++++++------ nipype/interfaces/cmtk/parcellation.py | 12 ++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/nipype/interfaces/cmtk/cmtk.py b/nipype/interfaces/cmtk/cmtk.py index c7b34aeae7..594f33a9d4 100644 --- a/nipype/interfaces/cmtk/cmtk.py +++ b/nipype/interfaces/cmtk/cmtk.py @@ -201,7 +201,8 @@ def cmat( stats["orig_n_fib"] = len(fib) roi = nb.load(roi_file, mmap=NUMPY_MMAP) - roiData = roi.get_data() + # Preserve on-disk type unless scaled + roiData = np.asanyarray(roi.dataobj) roiVoxelSize = roi.header.get_zooms() (endpoints, endpointsmm) = create_endpoints_array(fib, roiVoxelSize) @@ -432,9 +433,7 @@ def cmat( fiberlabels_fname = op.abspath(endpoint_name + "_filtered_fiberslabel.npy") iflogger.info("Storing all fiber labels (with orphans) as %s", fiberlabels_fname) - np.save( - fiberlabels_fname, np.array(fiberlabels, dtype=np.int32), - ) + np.save(fiberlabels_fname, np.array(fiberlabels, dtype=np.int32)) fiberlabels_noorphans_fname = op.abspath(endpoint_name + "_final_fiberslabels.npy") iflogger.info( @@ -840,7 +839,8 @@ def _run_interface(self, runtime): aparcpath, aparcname, aparcext = split_filename(aparc_aseg_file) iflogger.info("Using Aparc+Aseg file: %s", aparcname + aparcext) niiAPARCimg = nb.load(aparc_aseg_file, mmap=NUMPY_MMAP) - niiAPARCdata = niiAPARCimg.get_data() + # Preserve on-disk type + niiAPARCdata = np.asanyarray(niiAPARCimg.dataobj) niiDataLabels = np.unique(niiAPARCdata) numDataLabels = np.size(niiDataLabels) iflogger.info("Number of labels in image: %s", numDataLabels) @@ -1057,7 +1057,8 @@ def create_nodes(roi_file, resolution_network_file, out_filename): G = nx.Graph() gp = nx.read_graphml(resolution_network_file) roi_image = nb.load(roi_file, mmap=NUMPY_MMAP) - roiData = roi_image.get_data() + # Preserve on-disk type unless scaled + roiData = np.asanyarray(roi_image.dataobj) for u, d in gp.nodes(data=True): G.add_node(int(u), **d) xyz = tuple( diff --git a/nipype/interfaces/cmtk/parcellation.py b/nipype/interfaces/cmtk/parcellation.py index 60ef0445b3..c7397b2133 100644 --- a/nipype/interfaces/cmtk/parcellation.py +++ b/nipype/interfaces/cmtk/parcellation.py @@ -345,7 +345,7 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name, dilation): parval = cmp_config._get_lausanne_parcellation("Lausanne2008")[parcellation_name] pgpath = parval["node_information_graphml"] aseg = nb.load(op.join(fs_dir, "mri", "aseg.nii.gz")) - asegd = aseg.get_data() + asegd = np.asanyarray(aseg.dataobj) # identify cortical voxels, right (3) and left (42) hemispheres idxr = np.where(asegd == 3) @@ -420,7 +420,7 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name, dilation): runCmd(mri_cmd, log) tmp = nb.load(op.join(output_dir, "tmp.nii.gz")) - tmpd = tmp.get_data() + tmpd = np.asanyarray(tmp.dataobj) # find voxel and set them to intensityvalue in rois idx = np.where(tmpd == 1) @@ -478,7 +478,7 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name): ] # load ribbon as basis for white matter mask fsmask = nb.load(op.join(fs_dir, "mri", "ribbon.nii.gz")) - fsmaskd = fsmask.get_data() + fsmaskd = np.asanyarray(fsmask.dataobj) wmmask = np.zeros(fsmaskd.shape) # extract right and left white matter @@ -490,7 +490,7 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name): # remove subcortical nuclei from white matter mask aseg = nb.load(op.join(fs_dir, "mri", "aseg.nii.gz")) - asegd = aseg.get_data() + asegd = np.asanyarray(aseg.dataobj) # need binary erosion function imerode = nd.binary_erosion @@ -583,7 +583,7 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name): # ADD voxels from 'cc_unknown.nii.gz' dataset ccun = nb.load(op.join(fs_dir, "label", "cc_unknown.nii.gz")) - ccund = ccun.get_data() + ccund = np.asanyarray(ccun.dataobj) idx = np.where(ccund != 0) iflogger.info("Add corpus callosum and unknown to wm mask") wmmask[idx] = 1 @@ -594,7 +594,7 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name): parcellation_name, ) roi = nb.load(op.join(op.curdir, "ROI_%s.nii.gz" % parcellation_name)) - roid = roi.get_data() + roid = np.asanyarray(roi.dataobj) assert roid.shape[0] == wmmask.shape[0] pg = nx.read_graphml(pgpath) for brk, brv in pg.nodes(data=True): From 7fd0b80c2763757f3684572ebcc5828adf4658f5 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 13:17:08 -0500 Subject: [PATCH 08/15] RF: Purge get_data() from interfaces.nilearn --- nipype/interfaces/nilearn.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/nipype/interfaces/nilearn.py b/nipype/interfaces/nilearn.py index 38ecb84a3a..82da210fac 100644 --- a/nipype/interfaces/nilearn.py +++ b/nipype/interfaces/nilearn.py @@ -124,15 +124,15 @@ def _process_inputs(self): maskers = [] # determine form of label files, choose appropriate nilearn masker - if np.amax(label_data.get_data()) > 1: # 3d label file - n_labels = np.amax(label_data.get_data()) + if np.amax(label_data.dataobj) > 1: # 3d label file + n_labels = np.amax(label_data.dataobj) maskers.append(nl.NiftiLabelsMasker(label_data)) else: # 4d labels - n_labels = label_data.get_data().shape[3] + n_labels = label_data.shape[3] if self.inputs.incl_shared_variance: # independent computation for img in nli.iter_img(label_data): maskers.append( - nl.NiftiMapsMasker(self._4d(img.get_data(), img.affine)) + nl.NiftiMapsMasker(self._4d(img.dataobj, img.affine)) ) else: # one computation fitting all maskers.append(nl.NiftiMapsMasker(label_data)) @@ -155,9 +155,7 @@ def _process_inputs(self): ) if self.inputs.include_global: - global_label_data = label_data.get_data().sum( - axis=3 - ) # sum across all regions + global_label_data = label_data.dataobj.sum(axis=3) # sum across all regions global_label_data = ( np.rint(global_label_data).astype(int).clip(0, 1) ) # binarize From de8785b047693889431aa35655b1aaf77d7023cd Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 25 Nov 2019 13:21:09 -0500 Subject: [PATCH 09/15] TEST: Purge final get_data() calls from some tests --- nipype/interfaces/freesurfer/tests/test_model.py | 8 ++++---- nipype/interfaces/tests/test_image.py | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/nipype/interfaces/freesurfer/tests/test_model.py b/nipype/interfaces/freesurfer/tests/test_model.py index 0fe0065af5..5d68fe7e3c 100644 --- a/nipype/interfaces/freesurfer/tests/test_model.py +++ b/nipype/interfaces/freesurfer/tests/test_model.py @@ -32,12 +32,12 @@ def test_concatenate(tmpdir): # Test default behavior res = model.Concatenate(in_files=[in1, in2]).run() assert res.outputs.concatenated_file == tmpdir.join("concat_output.nii.gz").strpath - assert np.allclose(nb.load("concat_output.nii.gz").get_data(), out_data) + assert np.allclose(nb.load("concat_output.nii.gz").get_fdata(), out_data) # Test specified concatenated_file res = model.Concatenate(in_files=[in1, in2], concatenated_file=out).run() assert res.outputs.concatenated_file == tmpdir.join(out).strpath - assert np.allclose(nb.load(out, mmap=NUMPY_MMAP).get_data(), out_data) + assert np.allclose(nb.load(out, mmap=NUMPY_MMAP).get_fdata(), out_data) # Test in workflow wf = pe.Workflow("test_concatenate", base_dir=tmpdir.strpath) @@ -47,7 +47,7 @@ def test_concatenate(tmpdir): wf.add_nodes([concat]) wf.run() assert np.allclose( - nb.load(tmpdir.join("test_concatenate", "concat", out).strpath).get_data(), + nb.load(tmpdir.join("test_concatenate", "concat", out).strpath).get_fdata(), out_data, ) @@ -55,4 +55,4 @@ def test_concatenate(tmpdir): res = model.Concatenate( in_files=[in1, in2], concatenated_file=out, stats="mean" ).run() - assert np.allclose(nb.load(out, mmap=NUMPY_MMAP).get_data(), mean_data) + assert np.allclose(nb.load(out, mmap=NUMPY_MMAP).get_fdata(), mean_data) diff --git a/nipype/interfaces/tests/test_image.py b/nipype/interfaces/tests/test_image.py index b05d55b547..d1ed2578a6 100644 --- a/nipype/interfaces/tests/test_image.py +++ b/nipype/interfaces/tests/test_image.py @@ -50,7 +50,9 @@ def test_reorientation_backport(): # Reorientation changes affine and data array assert not np.allclose(img.affine, reoriented_a.affine) - assert not (flips_only and np.allclose(img.get_data(), reoriented_a.get_data())) + assert not ( + flips_only and np.allclose(img.get_fdata(), reoriented_a.get_fdata()) + ) # Dimension info changes iff axes are reordered assert flips_only == np.array_equal( img.header.get_dim_info(), reoriented_a.header.get_dim_info() @@ -58,7 +60,7 @@ def test_reorientation_backport(): # Both approaches produce equivalent images assert np.allclose(reoriented_a.affine, reoriented_b.affine) - assert np.array_equal(reoriented_a.get_data(), reoriented_b.get_data()) + assert np.array_equal(reoriented_a.get_fdata(), reoriented_b.get_fdata()) assert np.array_equal( reoriented_a.header.get_dim_info(), reoriented_b.header.get_dim_info() ) From c613c7d60b938b7ac194c4f0b61991919248a837 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Sat, 14 Dec 2019 21:39:34 -0500 Subject: [PATCH 10/15] RF: Drop NUMPY_MMAP from surrounding code --- nipype/algorithms/confounds.py | 10 +++++----- nipype/algorithms/misc.py | 16 ++++++++-------- nipype/algorithms/rapidart.py | 2 +- nipype/algorithms/tests/test_normalize_tpms.py | 6 +++--- nipype/algorithms/tests/test_splitmerge.py | 4 ++-- nipype/interfaces/cmtk/cmtk.py | 6 +++--- nipype/interfaces/dipy/simulate.py | 6 +++--- nipype/interfaces/freesurfer/tests/test_model.py | 4 ++-- nipype/interfaces/nipy/preprocess.py | 2 +- 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/nipype/algorithms/confounds.py b/nipype/algorithms/confounds.py index 9478ddbbbf..a298f9fb28 100644 --- a/nipype/algorithms/confounds.py +++ b/nipype/algorithms/confounds.py @@ -920,7 +920,7 @@ def _run_interface(self, runtime): img = nb.load(self.inputs.in_file[0], mmap=NUMPY_MMAP) header = img.header.copy() vollist = [ - nb.load(filename, mmap=NUMPY_MMAP) for filename in self.inputs.in_file + nb.load(filename) for filename in self.inputs.in_file ] data = np.concatenate( [ @@ -1035,8 +1035,8 @@ def compute_dvars( from nitime.algorithms import AR_est_YW import warnings - func = nb.load(in_file, mmap=NUMPY_MMAP).get_fdata(dtype=np.float32) - mask = np.asanyarray(nb.load(in_mask, mmap=NUMPY_MMAP).dataobj).astype(np.uint8) + func = nb.load(in_file).get_fdata(dtype=np.float32) + mask = np.asanyarray(nb.load(in_mask).dataobj).astype(np.uint8) if len(func.shape) != 4: raise RuntimeError("Input fMRI dataset should be 4-dimensional") @@ -1281,7 +1281,7 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None): if mask_method == "union": mask = None for filename in mask_files: - img = nb.load(filename, mmap=NUMPY_MMAP) + img = nb.load(filename) if mask is None: mask = img.get_fdata() > 0 np.logical_or(mask, img.get_fdata() > 0, mask) @@ -1291,7 +1291,7 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None): if mask_method == "intersect": mask = None for filename in mask_files: - img = nb.load(filename, mmap=NUMPY_MMAP) + img = nb.load(filename) if mask is None: mask = img.get_fdata() > 0 np.logical_and(mask, img.get_fdata() > 0, mask) diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index 7c35f6c5f8..fd3f000395 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -152,7 +152,7 @@ class SimpleThreshold(BaseInterface): def _run_interface(self, runtime): for fname in self.inputs.volumes: - img = nb.load(fname, mmap=NUMPY_MMAP) + img = nb.load(fname) data = img.get_fdata() active_map = data > self.inputs.threshold @@ -1269,7 +1269,7 @@ def normalize_tpms(in_files, in_mask=None, out_files=None): out_file = op.abspath("%s_norm_%02d%s" % (fname, i, fext)) out_files += [out_file] - imgs = [nb.load(fim, mmap=NUMPY_MMAP) for fim in in_files] + imgs = [nb.load(fim) for fim in in_files] if len(in_files) == 1: img_data = imgs[0].get_fdata(dtype=np.float32) @@ -1290,7 +1290,7 @@ def normalize_tpms(in_files, in_mask=None, out_files=None): msk[weights <= 0] = 0 if in_mask is not None: - msk = np.asanyarray(nb.load(in_mask, mmap=NUMPY_MMAP).dataobj) + msk = np.asanyarray(nb.load(in_mask).dataobj) msk[msk <= 0] = 0 msk[msk > 0] = 1 @@ -1328,7 +1328,7 @@ def split_rois(in_file, mask=None, roishape=None): droishape = (roishape[0], roishape[1], roishape[2], nvols) if mask is not None: - mask = np.asanyarray(nb.load(mask, mmap=NUMPY_MMAP).dataobj) + mask = np.asanyarray(nb.load(mask).dataobj) mask[mask > 0] = 1 mask[mask < 1] = 0 else: @@ -1432,7 +1432,7 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): for cname, iname in zip(in_files, in_idxs): f = np.load(iname) idxs = np.squeeze(f["arr_0"]) - cdata = np.asanyarray(nb.load(cname, mmap=NUMPY_MMAP).dataobj).reshape( + cdata = np.asanyarray(nb.load(cname).dataobj).reshape( -1, ndirs ) nels = len(idxs) @@ -1464,10 +1464,10 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): idxs = np.squeeze(f["arr_0"]) for d, fname in enumerate(nii): - data = np.asanyarray(nb.load(fname, mmap=NUMPY_MMAP).dataobj).reshape( + data = np.asanyarray(nb.load(fname).dataobj).reshape( -1 ) - cdata = nb.load(cname, mmap=NUMPY_MMAP).dataobj[..., d].reshape(-1) + cdata = nb.load(cname).dataobj[..., d].reshape(-1) nels = len(idxs) idata = (idxs,) data[idata] = cdata[0:nels] @@ -1547,7 +1547,7 @@ def _run_interface(self, runtime): total = None self._median_files = [] for idx, fname in enumerate(ensure_list(self.inputs.in_files)): - img = nb.load(fname, mmap=NUMPY_MMAP) + img = nb.load(fname) data = np.median(img.get_fdata(), axis=3) if self.inputs.median_per_file: self._median_files.append(self._write_nifti(img, data, idx)) diff --git a/nipype/algorithms/rapidart.py b/nipype/algorithms/rapidart.py index bb03ee9f7d..5d7f185344 100644 --- a/nipype/algorithms/rapidart.py +++ b/nipype/algorithms/rapidart.py @@ -525,7 +525,7 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None): mask[:, :, :, t0] = mask_tmp g[t0] = np.nansum(vol * mask_tmp) / np.nansum(mask_tmp) elif masktype == "file": # uses a mask image to determine intensity - maskimg = load(self.inputs.mask_file, mmap=NUMPY_MMAP) + maskimg = load(self.inputs.mask_file) mask = maskimg.get_fdata() affine = maskimg.affine mask = mask > 0.5 diff --git a/nipype/algorithms/tests/test_normalize_tpms.py b/nipype/algorithms/tests/test_normalize_tpms.py index fac9ea7286..c9b206361d 100644 --- a/nipype/algorithms/tests/test_normalize_tpms.py +++ b/nipype/algorithms/tests/test_normalize_tpms.py @@ -19,7 +19,7 @@ def test_normalize_tpms(tmpdir): in_mask = example_data("tpms_msk.nii.gz") - mskdata = np.asanyarray(nb.load(in_mask, mmap=NUMPY_MMAP).dataobj) + mskdata = np.asanyarray(nb.load(in_mask).dataobj) mskdata[mskdata > 0.0] = 1.0 mapdata = [] @@ -31,7 +31,7 @@ def test_normalize_tpms(tmpdir): filename = tmpdir.join("modtpm_%02d.nii.gz" % i).strpath out_files.append(tmpdir.join("normtpm_%02d.nii.gz" % i).strpath) - im = nb.load(mapname, mmap=NUMPY_MMAP) + im = nb.load(mapname) data = im.get_fdata() mapdata.append(data) @@ -45,7 +45,7 @@ def test_normalize_tpms(tmpdir): sumdata = np.zeros_like(mskdata) for i, tstfname in enumerate(out_files): - normdata = nb.load(tstfname, mmap=NUMPY_MMAP).get_fdata() + normdata = nb.load(tstfname).get_fdata() sumdata += normdata assert np.all(normdata[mskdata == 0] == 0) assert np.allclose(normdata, mapdata[i]) diff --git a/nipype/algorithms/tests/test_splitmerge.py b/nipype/algorithms/tests/test_splitmerge.py index 4305eeede0..5b06484e2c 100644 --- a/nipype/algorithms/tests/test_splitmerge.py +++ b/nipype/algorithms/tests/test_splitmerge.py @@ -15,7 +15,7 @@ def test_split_and_merge(tmpdir): in_mask = example_data("tpms_msk.nii.gz") dwfile = tmpdir.join("dwi.nii.gz").strpath - mask_img = nb.load(in_mask, mmap=NUMPY_MMAP) + mask_img = nb.load(in_mask) mskdata = np.asanyarray(mask_img.dataobj) aff = mask_img.affine @@ -26,7 +26,7 @@ def test_split_and_merge(tmpdir): resdw, resmsk, resid = split_rois(dwfile, in_mask, roishape=(20, 20, 2)) merged = merge_rois(resdw, resid, in_mask) - dwmerged = nb.load(merged, mmap=NUMPY_MMAP).get_fdata(dtype=np.float32) + dwmerged = nb.load(merged).get_fdata(dtype=np.float32) dwmasked = dwdata * mskdata[:, :, :, np.newaxis] diff --git a/nipype/interfaces/cmtk/cmtk.py b/nipype/interfaces/cmtk/cmtk.py index 594f33a9d4..6622b49ff6 100644 --- a/nipype/interfaces/cmtk/cmtk.py +++ b/nipype/interfaces/cmtk/cmtk.py @@ -200,7 +200,7 @@ def cmat( fib, hdr = nb.trackvis.read(track_file, False) stats["orig_n_fib"] = len(fib) - roi = nb.load(roi_file, mmap=NUMPY_MMAP) + roi = nb.load(roi_file) # Preserve on-disk type unless scaled roiData = np.asanyarray(roi.dataobj) roiVoxelSize = roi.header.get_zooms() @@ -838,7 +838,7 @@ def _run_interface(self, runtime): aparc_aseg_file = self.inputs.aparc_aseg_file aparcpath, aparcname, aparcext = split_filename(aparc_aseg_file) iflogger.info("Using Aparc+Aseg file: %s", aparcname + aparcext) - niiAPARCimg = nb.load(aparc_aseg_file, mmap=NUMPY_MMAP) + niiAPARCimg = nb.load(aparc_aseg_file) # Preserve on-disk type niiAPARCdata = np.asanyarray(niiAPARCimg.dataobj) niiDataLabels = np.unique(niiAPARCdata) @@ -1056,7 +1056,7 @@ def _gen_outfilename(self, ext): def create_nodes(roi_file, resolution_network_file, out_filename): G = nx.Graph() gp = nx.read_graphml(resolution_network_file) - roi_image = nb.load(roi_file, mmap=NUMPY_MMAP) + roi_image = nb.load(roi_file) # Preserve on-disk type unless scaled roiData = np.asanyarray(roi_image.dataobj) for u, d in gp.nodes(data=True): diff --git a/nipype/interfaces/dipy/simulate.py b/nipype/interfaces/dipy/simulate.py index a14e979b45..2ad2fe38ed 100644 --- a/nipype/interfaces/dipy/simulate.py +++ b/nipype/interfaces/dipy/simulate.py @@ -142,7 +142,7 @@ def _run_interface(self, runtime): nballs = len(self.inputs.in_vfms) vfs = np.squeeze( nb.concat_images( - [nb.load(f, mmap=NUMPY_MMAP) for f in self.inputs.in_vfms] + self.inputs.in_vfms ).dataobj ) if nballs == 1: @@ -163,7 +163,7 @@ def _run_interface(self, runtime): # Fiber fractions ffsim = nb.concat_images( - [nb.load(f, mmap=NUMPY_MMAP) for f in self.inputs.in_frac] + self.inputs.in_frac ) ffs = np.nan_to_num(np.squeeze(ffsim.dataobj)) # fiber fractions ffs = np.clip(ffs, 0.0, 1.0) @@ -207,7 +207,7 @@ def _run_interface(self, runtime): dirs = None for i in range(nsticks): f = self.inputs.in_dirs[i] - fd = np.nan_to_num(nb.load(f, mmap=NUMPY_MMAP).dataobj) + fd = np.nan_to_num(nb.load(f).dataobj) w = np.linalg.norm(fd, axis=3)[..., np.newaxis] w[w < np.finfo(float).eps] = 1.0 fd /= w diff --git a/nipype/interfaces/freesurfer/tests/test_model.py b/nipype/interfaces/freesurfer/tests/test_model.py index 5d68fe7e3c..8d30ebb5dc 100644 --- a/nipype/interfaces/freesurfer/tests/test_model.py +++ b/nipype/interfaces/freesurfer/tests/test_model.py @@ -37,7 +37,7 @@ def test_concatenate(tmpdir): # Test specified concatenated_file res = model.Concatenate(in_files=[in1, in2], concatenated_file=out).run() assert res.outputs.concatenated_file == tmpdir.join(out).strpath - assert np.allclose(nb.load(out, mmap=NUMPY_MMAP).get_fdata(), out_data) + assert np.allclose(nb.load(out).get_fdata(), out_data) # Test in workflow wf = pe.Workflow("test_concatenate", base_dir=tmpdir.strpath) @@ -55,4 +55,4 @@ def test_concatenate(tmpdir): res = model.Concatenate( in_files=[in1, in2], concatenated_file=out, stats="mean" ).run() - assert np.allclose(nb.load(out, mmap=NUMPY_MMAP).get_fdata(), mean_data) + assert np.allclose(nb.load(out).get_fdata(), mean_data) diff --git a/nipype/interfaces/nipy/preprocess.py b/nipype/interfaces/nipy/preprocess.py index b22837d6a3..278147df01 100644 --- a/nipype/interfaces/nipy/preprocess.py +++ b/nipype/interfaces/nipy/preprocess.py @@ -57,7 +57,7 @@ def _run_interface(self, runtime): value = getattr(self.inputs, key) if isdefined(value): if key in ["mean_volume", "reference_volume"]: - value = np.asanyarray(nb.load(value, mmap=NUMPY_MMAP).dataobj) + value = np.asanyarray(nb.load(value).dataobj) args[key] = value brain_mask = compute_mask(**args) From 8e442237f24b10a4cd6a5bf2dee41e2c3ac06b88 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Sat, 14 Dec 2019 21:42:37 -0500 Subject: [PATCH 11/15] FIX: Value 1 replaced with 2 by accident --- nipype/algorithms/metrics.py | 2 +- nipype/interfaces/nipy/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nipype/algorithms/metrics.py b/nipype/algorithms/metrics.py index 50cbe70161..64636246db 100644 --- a/nipype/algorithms/metrics.py +++ b/nipype/algorithms/metrics.py @@ -716,7 +716,7 @@ def _run_interface(self, runtime): mask1 = None if isdefined(self.inputs.mask2): - mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 2 + mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 1 else: mask2 = None diff --git a/nipype/interfaces/nipy/utils.py b/nipype/interfaces/nipy/utils.py index ec9893453a..3dafbbc07c 100644 --- a/nipype/interfaces/nipy/utils.py +++ b/nipype/interfaces/nipy/utils.py @@ -79,7 +79,7 @@ def _run_interface(self, runtime): mask1 = None if isdefined(self.inputs.mask2): - mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 2 + mask2 = np.asanyarray(nb.load(self.inputs.mask2).dataobj) == 1 else: mask2 = None From 97bdbd5f48ab242de5288ba4715192a27619a803 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Thu, 19 Dec 2019 15:51:11 -0500 Subject: [PATCH 12/15] STY: Black --- nipype/algorithms/confounds.py | 4 +--- nipype/algorithms/misc.py | 8 ++------ nipype/interfaces/dipy/simulate.py | 10 ++-------- nipype/interfaces/nipy/model.py | 2 +- nipype/utils/config.py | 4 +++- 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/nipype/algorithms/confounds.py b/nipype/algorithms/confounds.py index a298f9fb28..91839dcf07 100644 --- a/nipype/algorithms/confounds.py +++ b/nipype/algorithms/confounds.py @@ -919,9 +919,7 @@ class TSNR(BaseInterface): def _run_interface(self, runtime): img = nb.load(self.inputs.in_file[0], mmap=NUMPY_MMAP) header = img.header.copy() - vollist = [ - nb.load(filename) for filename in self.inputs.in_file - ] + vollist = [nb.load(filename) for filename in self.inputs.in_file] data = np.concatenate( [ vol.get_fdata(dtype=np.float32).reshape(vol.shape[:3] + (-1,)) diff --git a/nipype/algorithms/misc.py b/nipype/algorithms/misc.py index fd3f000395..b472039075 100644 --- a/nipype/algorithms/misc.py +++ b/nipype/algorithms/misc.py @@ -1432,9 +1432,7 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): for cname, iname in zip(in_files, in_idxs): f = np.load(iname) idxs = np.squeeze(f["arr_0"]) - cdata = np.asanyarray(nb.load(cname).dataobj).reshape( - -1, ndirs - ) + cdata = np.asanyarray(nb.load(cname).dataobj).reshape(-1, ndirs) nels = len(idxs) idata = (idxs,) try: @@ -1464,9 +1462,7 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None): idxs = np.squeeze(f["arr_0"]) for d, fname in enumerate(nii): - data = np.asanyarray(nb.load(fname).dataobj).reshape( - -1 - ) + data = np.asanyarray(nb.load(fname).dataobj).reshape(-1) cdata = nb.load(cname).dataobj[..., d].reshape(-1) nels = len(idxs) idata = (idxs,) diff --git a/nipype/interfaces/dipy/simulate.py b/nipype/interfaces/dipy/simulate.py index 2ad2fe38ed..eb21be1780 100644 --- a/nipype/interfaces/dipy/simulate.py +++ b/nipype/interfaces/dipy/simulate.py @@ -140,11 +140,7 @@ def _run_interface(self, runtime): # Volume fractions of isotropic compartments nballs = len(self.inputs.in_vfms) - vfs = np.squeeze( - nb.concat_images( - self.inputs.in_vfms - ).dataobj - ) + vfs = np.squeeze(nb.concat_images(self.inputs.in_vfms).dataobj) if nballs == 1: vfs = vfs[..., np.newaxis] total_vf = np.sum(vfs, axis=3) @@ -162,9 +158,7 @@ def _run_interface(self, runtime): nvox = len(msk[msk > 0]) # Fiber fractions - ffsim = nb.concat_images( - self.inputs.in_frac - ) + ffsim = nb.concat_images(self.inputs.in_frac) ffs = np.nan_to_num(np.squeeze(ffsim.dataobj)) # fiber fractions ffs = np.clip(ffs, 0.0, 1.0) if nsticks == 1: diff --git a/nipype/interfaces/nipy/model.py b/nipype/interfaces/nipy/model.py index 17f031d288..887724c4e7 100644 --- a/nipype/interfaces/nipy/model.py +++ b/nipype/interfaces/nipy/model.py @@ -115,7 +115,7 @@ def _run_interface(self, runtime): if isinstance(functional_runs, (str, bytes)): functional_runs = [functional_runs] nii = nb.load(functional_runs[0]) - data = nii.get_fdata(caching='unchanged') + data = nii.get_fdata(caching="unchanged") if isdefined(self.inputs.mask): mask = np.asanyarray(nb.load(self.inputs.mask).dataobj) > 0 diff --git a/nipype/utils/config.py b/nipype/utils/config.py index 5ba52bfb04..999ee76307 100644 --- a/nipype/utils/config.py +++ b/nipype/utils/config.py @@ -91,7 +91,9 @@ def __init__(self, *args, **kwargs): self._config = configparser.ConfigParser() self._cwd = None - config_dir = os.path.expanduser(os.getenv("NIPYPE_CONFIG_DIR", default="~/.nipype")) + config_dir = os.path.expanduser( + os.getenv("NIPYPE_CONFIG_DIR", default="~/.nipype") + ) self.data_file = os.path.join(config_dir, "nipype.json") self.set_default_config() From a5c8e0f0e08d81ed4ef76df7e5a32f20e079fbc9 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Thu, 19 Dec 2019 15:51:22 -0500 Subject: [PATCH 13/15] FIX: Improved mask estimation --- nipype/algorithms/confounds.py | 10 ++++++---- nipype/algorithms/metrics.py | 12 ++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/nipype/algorithms/confounds.py b/nipype/algorithms/confounds.py index 91839dcf07..3bbf4632f4 100644 --- a/nipype/algorithms/confounds.py +++ b/nipype/algorithms/confounds.py @@ -1280,9 +1280,10 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None): mask = None for filename in mask_files: img = nb.load(filename) + img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0 if mask is None: - mask = img.get_fdata() > 0 - np.logical_or(mask, img.get_fdata() > 0, mask) + mask = img_as_mask + np.logical_or(mask, img_as_mask, mask) img = nb.Nifti1Image(mask, img.affine, header=img.header) return [img] @@ -1290,9 +1291,10 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None): mask = None for filename in mask_files: img = nb.load(filename) + img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0 if mask is None: - mask = img.get_fdata() > 0 - np.logical_and(mask, img.get_fdata() > 0, mask) + mask = img_as_mask + np.logical_and(mask, img_as_mask, mask) img = nb.Nifti1Image(mask, img.affine, header=img.header) return [img] diff --git a/nipype/algorithms/metrics.py b/nipype/algorithms/metrics.py index 64636246db..ab13dca963 100644 --- a/nipype/algorithms/metrics.py +++ b/nipype/algorithms/metrics.py @@ -112,13 +112,13 @@ def _eucl_cog(self, nii1, nii2): from scipy.ndimage.measurements import center_of_mass, label origdata1 = np.asanyarray(nii1.dataobj) - origdata1 = (origdata1 != 0) & ~np.isnan(origdata1) - cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1) + origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1) + cog_t = np.array(center_of_mass(origdata1)).reshape(-1, 1) cog_t = np.vstack((cog_t, np.array([1]))) cog_t_coor = np.dot(nii1.affine, cog_t)[:3, :] origdata2 = np.asanyarray(nii2.dataobj) - origdata2 = (origdata2 != 0) & ~np.isnan(origdata2) + origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2) (labeled_data, n_labels) = label(origdata2) cogs = np.ones((4, n_labels)) @@ -165,13 +165,13 @@ def _eucl_max(self, nii1, nii2): from scipy.spatial.distance import cdist origdata1 = np.asanyarray(nii1.dataobj) - origdata1 = np.logical_not(np.logical_or(origdata1 == 0, np.isnan(origdata1))) + origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1) origdata2 = np.asanyarray(nii2.dataobj) - origdata2 = np.logical_not(np.logical_or(origdata2 == 0, np.isnan(origdata2))) + origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2) if isdefined(self.inputs.mask_volume): maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj) - maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata))) + maskdata = (np.rint(maskdata) != 0) & ~np.isnan(maskdata) origdata1 = np.logical_and(maskdata, origdata1) origdata2 = np.logical_and(maskdata, origdata2) From c87c6bf1bf3a17562f1bfa8dd6a3d47a051177fc Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Thu, 19 Dec 2019 15:52:34 -0500 Subject: [PATCH 14/15] RF: Simplify data retrieval, avoid needless upcast --- nipype/algorithms/metrics.py | 4 ++-- nipype/algorithms/rapidart.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nipype/algorithms/metrics.py b/nipype/algorithms/metrics.py index ab13dca963..611ec57af5 100644 --- a/nipype/algorithms/metrics.py +++ b/nipype/algorithms/metrics.py @@ -441,8 +441,8 @@ class FuzzyOverlap(SimpleInterface): def _run_interface(self, runtime): # Load data - refdata = np.asanyarray(nb.concat_images(self.inputs.in_ref).dataobj) - tstdata = np.asanyarray(nb.concat_images(self.inputs.in_tst).dataobj) + refdata = nb.concat_images(self.inputs.in_ref).dataobj + tstdata = nb.concat_images(self.inputs.in_tst).dataobj # Data must have same shape if not refdata.shape == tstdata.shape: diff --git a/nipype/algorithms/rapidart.py b/nipype/algorithms/rapidart.py index 5d7f185344..a13d412481 100644 --- a/nipype/algorithms/rapidart.py +++ b/nipype/algorithms/rapidart.py @@ -526,7 +526,7 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None): g[t0] = np.nansum(vol * mask_tmp) / np.nansum(mask_tmp) elif masktype == "file": # uses a mask image to determine intensity maskimg = load(self.inputs.mask_file) - mask = maskimg.get_fdata() + mask = maskimg.get_fdata(dtype=np.float32) affine = maskimg.affine mask = mask > 0.5 for t0 in range(timepoints): From fcbaad87be01a767702fdcfc451089e019c95c05 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Thu, 19 Dec 2019 16:06:00 -0500 Subject: [PATCH 15/15] FIX: Missing and unused imports --- nipype/interfaces/cmtk/cmtk.py | 1 - nipype/interfaces/dipy/anisotropic_power.py | 1 + nipype/interfaces/dipy/simulate.py | 1 - nipype/interfaces/dipy/tensors.py | 1 + nipype/interfaces/nipy/model.py | 2 -- nipype/interfaces/nipy/preprocess.py | 1 - nipype/interfaces/nipy/utils.py | 1 + 7 files changed, 3 insertions(+), 5 deletions(-) diff --git a/nipype/interfaces/cmtk/cmtk.py b/nipype/interfaces/cmtk/cmtk.py index 6622b49ff6..237b092f35 100644 --- a/nipype/interfaces/cmtk/cmtk.py +++ b/nipype/interfaces/cmtk/cmtk.py @@ -10,7 +10,6 @@ from ... import logging from ...utils.filemanip import split_filename -from ...utils import NUMPY_MMAP from ..base import ( BaseInterface, diff --git a/nipype/interfaces/dipy/anisotropic_power.py b/nipype/interfaces/dipy/anisotropic_power.py index 06e25fb132..7ad82fb678 100644 --- a/nipype/interfaces/dipy/anisotropic_power.py +++ b/nipype/interfaces/dipy/anisotropic_power.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import numpy as np import nibabel as nb from ... import logging diff --git a/nipype/interfaces/dipy/simulate.py b/nipype/interfaces/dipy/simulate.py index eb21be1780..e1867342bb 100644 --- a/nipype/interfaces/dipy/simulate.py +++ b/nipype/interfaces/dipy/simulate.py @@ -6,7 +6,6 @@ import nibabel as nb from ... import logging -from ...utils import NUMPY_MMAP from ..base import ( traits, TraitedSpec, diff --git a/nipype/interfaces/dipy/tensors.py b/nipype/interfaces/dipy/tensors.py index e8f2029881..6f77d9a47e 100644 --- a/nipype/interfaces/dipy/tensors.py +++ b/nipype/interfaces/dipy/tensors.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import numpy as np import nibabel as nb from ... import logging diff --git a/nipype/interfaces/nipy/model.py b/nipype/interfaces/nipy/model.py index 887724c4e7..b931947c19 100644 --- a/nipype/interfaces/nipy/model.py +++ b/nipype/interfaces/nipy/model.py @@ -1,8 +1,6 @@ # -*- coding: utf-8 -*- import os -from ...utils import NUMPY_MMAP - from .base import NipyBaseInterface from ..base import ( TraitedSpec, diff --git a/nipype/interfaces/nipy/preprocess.py b/nipype/interfaces/nipy/preprocess.py index 278147df01..115b7f93ad 100644 --- a/nipype/interfaces/nipy/preprocess.py +++ b/nipype/interfaces/nipy/preprocess.py @@ -4,7 +4,6 @@ import nibabel as nb import numpy as np -from ...utils import NUMPY_MMAP from ...utils.filemanip import split_filename, fname_presuffix from .base import NipyBaseInterface, have_nipy diff --git a/nipype/interfaces/nipy/utils.py b/nipype/interfaces/nipy/utils.py index 3dafbbc07c..08eb80e0b2 100644 --- a/nipype/interfaces/nipy/utils.py +++ b/nipype/interfaces/nipy/utils.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import warnings +import numpy as np import nibabel as nb from .base import NipyBaseInterface, have_nipy