-
Notifications
You must be signed in to change notification settings - Fork 533
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
Changes from all commits
c1a6936
36732e5
77aa4d9
4b618ec
f737c26
68382d2
530143e
7fd0b80
de8785b
c613c7d
8e44223
97bdbd5
a5c8e0f
c87c6bf
fcbaad8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
effigies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
self._results = {"n_volumes_to_discard": is_outlier(global_signal)} | ||
|
@@ -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") | ||
|
@@ -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] | ||
|
||
|
@@ -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 " | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To control memory usage:
Suggested change
|
||||||
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") | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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())) | ||||||
) | ||||||
cog_t = np.array(center_of_mass(origdata1.copy())).reshape(-1, 1) | ||||||
origdata1 = np.asanyarray(nii1.dataobj) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the next line is |
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the next line is |
||||||
origdata2 = (np.rint(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.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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, now i'm confused why this function uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My criterion here was whether something appeared to be targeting integer values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. roger! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the next line is |
||||||
origdata1 = (np.rint(origdata1) != 0) & ~np.isnan(origdata1) | ||||||
origdata2 = np.asanyarray(nii2.dataobj) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the next line is |
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the next line is |
||||||
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) | ||||||
|
@@ -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]) | ||||||
effigies marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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 = 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: | ||||||
|
@@ -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) == 1 | ||||||
else: | ||||||
mask2 = None | ||||||
|
||||||
|
There was a problem hiding this comment.
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.