Skip to content

ENH: Pacify DeprecationWarnings caused by nibabel 3 pre-release #3099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions nipype/algorithms/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the array coercion to _process_masks, since the data may not be used.


TR = 0
if self.inputs.pre_filter == "cosine":
Expand Down Expand Up @@ -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),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please specifically verify that the desired behavior here would be to compute noise components on float32 data... float64 would be very large, but this data is presumably floating point, so using the dataobj interface seems needlessly cumbersome.

mask_images,
components_criterion,
self.inputs.pre_filter,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -920,11 +919,13 @@ 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, mmap=NUMPY_MMAP) for filename in self.inputs.in_file
]
vollist = [nb.load(filename) 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)

Expand Down Expand Up @@ -985,7 +986,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)}
Expand Down Expand Up @@ -1032,8 +1033,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).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")
Expand Down Expand Up @@ -1278,20 +1279,22 @@ 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)
img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0
if mask is None:
mask = img.get_data() > 0
np.logical_or(mask, img.get_data() > 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]

if mask_method == "intersect":
mask = None
for filename in mask_files:
img = nb.load(filename, mmap=NUMPY_MMAP)
img = nb.load(filename)
img_as_mask = np.asanyarray(img.dataobj).astype("int32") > 0
if mask is None:
mask = img.get_data() > 0
np.logical_and(mask, img.get_data() > 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]

Expand Down Expand Up @@ -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 "
Expand Down
10 changes: 5 additions & 5 deletions nipype/algorithms/icc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To control memory usage:

Suggested change
nb.load(fname, mmap=NUMPY_MMAP).get_fdata()[maskdata].reshape(-1, 1)
nb.load(fname).get_fdata(dtype=np.float32)[maskdata].reshape(-1, 1)

for fname in sessions
]
for sessions in self.inputs.subjects_sessions
Expand All @@ -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")

Expand Down
3 changes: 1 addition & 2 deletions nipype/algorithms/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],))
Expand Down
65 changes: 30 additions & 35 deletions nipype/algorithms/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()))
)
cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1)
origdata1 = np.asanyarray(nii1.dataobj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like we're finding a mask of a volume of real values, so I would probably go with fdata. The slight potential memory advantage of dataobj doesn't justify ugliness IMO.

Suggested change
origdata1 = np.asanyarray(nii1.dataobj)
origdata1 = nii1.get_fdata(dtype=np.float32)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the next line is origdata1 != 0, which may not work for float.

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.logical_and(
nii2.get_data() != 0, np.logical_not(np.isnan(nii2.get_data()))
)
origdata2 = np.asanyarray(nii2.dataobj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again:

Suggested change
origdata2 = np.asanyarray(nii2.dataobj)
origdata2 = nii2.get_fdata(dtype=np.float32)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the next line is origdata2 != 0, which may not work for float.

origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2)
(labeled_data, n_labels) = label(origdata2)

cogs = np.ones((4, n_labels))
Expand All @@ -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)
Expand All @@ -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.logical_not(np.logical_or(origdata1 == 0, np.isnan(origdata1)))
origdata2 = nii2.get_data()
origdata2 = np.logical_not(np.logical_or(origdata2 == 0, np.isnan(origdata2)))
origdata1 = np.asanyarray(nii1.dataobj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
origdata1 = np.asanyarray(nii1.dataobj)
origdata1 = nii1.get_fdata(dtype=np.float32)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, now i'm confused why this function uses get_fdata, but the previous one (_eucl_mean) uses dataobj

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My criterion here was whether something appeared to be targeting integer values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roger!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the next line is origdata1 == 0, which may not work for float.

origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1)
origdata2 = np.asanyarray(nii2.dataobj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
origdata2 = np.asanyarray(nii2.dataobj)
origdata2 = nii2.get_fdata(dtype=np.float32)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the next line is origdata2 == 0, which may not work for float.

origdata2 = (np.rint(origdata2) != 0) & ~np.isnan(origdata2)

if isdefined(self.inputs.mask_volume):
maskdata = nb.load(self.inputs.mask_volume).get_data()
maskdata = np.logical_not(np.logical_or(maskdata == 0, np.isnan(maskdata)))
maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
maskdata = np.asanyarray(nb.load(self.inputs.mask_volume).dataobj)
maskdata = nb.load(self.inputs.mask_volume).get_fdata(dtype=np.float32)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the next line is maskdata == 0, which may not work for float.

maskdata = (np.rint(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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = 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:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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, \
Expand Down Expand Up @@ -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]
Expand All @@ -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) == 1
else:
mask2 = None

Expand Down
Loading