Skip to content

[REF] removed all uses of numpy_mmap #3121

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 2 commits into from
Dec 30, 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
9 changes: 3 additions & 6 deletions examples/dmri_camino_dti.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,27 @@

def get_vox_dims(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
if isinstance(volume, list):
volume = volume[0]
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
hdr = nii.header
voxdims = hdr.get_zooms()
return [float(voxdims[0]), float(voxdims[1]), float(voxdims[2])]


def get_data_dims(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
if isinstance(volume, list):
volume = volume[0]
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
hdr = nii.header
datadims = hdr.get_data_shape()
return [int(datadims[0]), int(datadims[1]), int(datadims[2])]


def get_affine(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
return nii.affine


Expand Down
9 changes: 3 additions & 6 deletions examples/dmri_connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,27 @@

def get_vox_dims(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
if isinstance(volume, list):
volume = volume[0]
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
hdr = nii.header
voxdims = hdr.get_zooms()
return [float(voxdims[0]), float(voxdims[1]), float(voxdims[2])]


def get_data_dims(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
if isinstance(volume, list):
volume = volume[0]
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
hdr = nii.header
datadims = hdr.get_data_shape()
return [int(datadims[0]), int(datadims[1]), int(datadims[2])]


def get_affine(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
return nii.affine


Expand Down
1 change: 0 additions & 1 deletion examples/fmri_ants_openfmri.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from nipype.workflows.fmri.fsl import (create_featreg_preproc,
create_modelfit_workflow,
create_fixed_effects_flow)
from nipype.utils import NUMPY_MMAP

config.enable_provenance()
version = 0
Expand Down
3 changes: 1 addition & 2 deletions examples/fmri_fsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ def pickfirst(files):

def getmiddlevolume(func):
from nibabel import load
from nipype.utils import NUMPY_MMAP
funcfile = func
if isinstance(func, list):
funcfile = func[0]
_, _, _, timepoints = load(funcfile, mmap=NUMPY_MMAP).shape
_, _, _, timepoints = load(funcfile).shape
return int(timepoints / 2) - 1


Expand Down
3 changes: 1 addition & 2 deletions examples/fmri_spm_auditory.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@

def get_vox_dims(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
if isinstance(volume, list):
volume = volume[0]
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
hdr = nii.header
voxdims = hdr.get_zooms()
return [float(voxdims[0]), float(voxdims[1]), float(voxdims[2])]
Expand Down
3 changes: 1 addition & 2 deletions examples/fmri_spm_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@

def get_vox_dims(volume):
import nibabel as nb
from nipype.utils import NUMPY_MMAP
if isinstance(volume, list):
volume = volume[0]
nii = nb.load(volume, mmap=NUMPY_MMAP)
nii = nb.load(volume)
hdr = nii.header
voxdims = hdr.get_zooms()
return [float(voxdims[0]), float(voxdims[1]), float(voxdims[2])]
Expand Down
21 changes: 8 additions & 13 deletions examples/rsfmri_vol_surface_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ def median(in_files):
"""
import numpy as np
import nibabel as nb
from nipype.utils import NUMPY_MMAP
average = None
for idx, filename in enumerate(filename_to_list(in_files)):
img = nb.load(filename, mmap=NUMPY_MMAP)
img = nb.load(filename)
data = np.median(img.get_data(), axis=3)
if average is None:
average = data
Expand All @@ -146,12 +145,11 @@ def bandpass_filter(files, lowpass_freq, highpass_freq, fs):
from nipype.utils.filemanip import split_filename, list_to_filename
import numpy as np
import nibabel as nb
from nipype.utils import NUMPY_MMAP
out_files = []
for filename in filename_to_list(files):
path, name, ext = split_filename(filename)
out_file = os.path.join(os.getcwd(), name + '_bp' + ext)
img = nb.load(filename, mmap=NUMPY_MMAP)
img = nb.load(filename)
timepoints = img.shape[-1]
F = np.zeros((timepoints))
lowidx = int(timepoints / 2) + 1
Expand Down Expand Up @@ -264,12 +262,11 @@ def extract_noise_components(realigned_file,
from scipy.linalg.decomp_svd import svd
import numpy as np
import nibabel as nb
from nipype.utils import NUMPY_MMAP
import os
imgseries = nb.load(realigned_file, mmap=NUMPY_MMAP)
imgseries = nb.load(realigned_file)
components = None
for filename in filename_to_list(mask_file):
mask = nb.load(filename, mmap=NUMPY_MMAP).get_data()
mask = nb.load(filename).get_data()
if len(np.nonzero(mask > 0)[0]) == 0:
continue
voxel_timecourses = imgseries.get_data()[mask > 0]
Expand Down Expand Up @@ -334,11 +331,10 @@ def extract_subrois(timeseries_file, label_file, indices):
"""
from nipype.utils.filemanip import split_filename
import nibabel as nb
from nipype.utils import NUMPY_MMAP
import os
img = nb.load(timeseries_file, mmap=NUMPY_MMAP)
img = nb.load(timeseries_file)
data = img.get_data()
roiimg = nb.load(label_file, mmap=NUMPY_MMAP)
roiimg = nb.load(label_file)
rois = roiimg.get_data()
prefix = split_filename(timeseries_file)[1]
out_ts_file = os.path.join(os.getcwd(), '%s_subcortical_ts.txt' % prefix)
Expand All @@ -359,9 +355,8 @@ def combine_hemi(left, right):
"""
import os
import numpy as np
from nipype.utils import NUMPY_MMAP
lh_data = nb.load(left, mmap=NUMPY_MMAP).get_data()
rh_data = nb.load(right, mmap=NUMPY_MMAP).get_data()
lh_data = nb.load(left).get_data()
rh_data = nb.load(right).get_data()

indices = np.vstack((1000000 + np.arange(0, lh_data.shape[0])[:, None],
2000000 + np.arange(0, rh_data.shape[0])[:, None]))
Expand Down
13 changes: 6 additions & 7 deletions examples/rsfmri_vol_surface_preprocessing_nipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import numpy as np
import scipy as sp
import nibabel as nb
from nipype.utils.config import NUMPY_MMAP

"""
A list of modules and functions to import inside of nodes
Expand Down Expand Up @@ -129,7 +128,7 @@ def median(in_files):
"""
average = None
for idx, filename in enumerate(filename_to_list(in_files)):
img = nb.load(filename, mmap=NUMPY_MMAP)
img = nb.load(filename)
data = np.median(img.get_data(), axis=3)
if average is None:
average = data
Expand All @@ -156,7 +155,7 @@ def bandpass_filter(files, lowpass_freq, highpass_freq, fs):
for filename in filename_to_list(files):
path, name, ext = split_filename(filename)
out_file = os.path.join(os.getcwd(), name + '_bp' + ext)
img = nb.load(filename, mmap=NUMPY_MMAP)
img = nb.load(filename)
timepoints = img.shape[-1]
F = np.zeros((timepoints))
lowidx = int(timepoints / 2) + 1
Expand Down Expand Up @@ -282,9 +281,9 @@ def extract_subrois(timeseries_file, label_file, indices):
The first four columns are: freesurfer index, i, j, k positions in the
label file
"""
img = nb.load(timeseries_file, mmap=NUMPY_MMAP)
img = nb.load(timeseries_file)
data = img.get_data()
roiimg = nb.load(label_file, mmap=NUMPY_MMAP)
roiimg = nb.load(label_file)
rois = roiimg.get_data()
prefix = split_filename(timeseries_file)[1]
out_ts_file = os.path.join(os.getcwd(), '%s_subcortical_ts.txt' % prefix)
Expand All @@ -303,8 +302,8 @@ def extract_subrois(timeseries_file, label_file, indices):
def combine_hemi(left, right):
"""Combine left and right hemisphere time series into a single text file
"""
lh_data = nb.load(left, mmap=NUMPY_MMAP).get_data()
rh_data = nb.load(right, mmap=NUMPY_MMAP).get_data()
lh_data = nb.load(left).get_data()
rh_data = nb.load(right).get_data()

indices = np.vstack((1000000 + np.arange(0, lh_data.shape[0])[:, None],
2000000 + np.arange(0, rh_data.shape[0])[:, None]))
Expand Down
9 changes: 4 additions & 5 deletions nipype/algorithms/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
OutputMultiPath,
SimpleInterface,
)
from ..utils import NUMPY_MMAP
from ..utils.misc import normalize_mc_params

IFLOGGER = logging.getLogger("nipype.interface")
Expand Down Expand Up @@ -599,7 +598,7 @@ def _run_interface(self, runtime):
else 0
)

imgseries = nb.load(self.inputs.realigned_file, mmap=NUMPY_MMAP)
imgseries = nb.load(self.inputs.realigned_file)

if len(imgseries.shape) != 4:
raise ValueError(
Expand Down Expand Up @@ -917,7 +916,7 @@ class TSNR(BaseInterface):
output_spec = TSNROutputSpec

def _run_interface(self, runtime):
img = nb.load(self.inputs.in_file[0], mmap=NUMPY_MMAP)
img = nb.load(self.inputs.in_file[0])
header = img.header.copy()
vollist = [nb.load(filename) for filename in self.inputs.in_file]
data = np.concatenate(
Expand Down Expand Up @@ -1263,7 +1262,7 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None):
)
)
if mask_index < len(mask_files):
mask = nb.load(mask_files[mask_index], mmap=NUMPY_MMAP)
mask = nb.load(mask_files[mask_index])
return [mask]
raise ValueError(
("mask_index {0} must be less than number of mask " "files {1}").format(
Expand All @@ -1273,7 +1272,7 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None):
masks = []
if mask_method == "none":
for filename in mask_files:
masks.append(nb.load(filename, mmap=NUMPY_MMAP))
masks.append(nb.load(filename))
return masks

if mask_method == "union":
Expand Down
3 changes: 1 addition & 2 deletions nipype/algorithms/icc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
traits,
File,
)
from ..utils import NUMPY_MMAP


class ICCInputSpec(BaseInterfaceInputSpec):
Expand Down Expand Up @@ -46,7 +45,7 @@ def _run_interface(self, runtime):

session_datas = [
[
nb.load(fname, mmap=NUMPY_MMAP).get_fdata()[maskdata].reshape(-1, 1)
nb.load(fname).get_fdata()[maskdata].reshape(-1, 1)
for fname in sessions
]
for sessions in self.inputs.subjects_sessions
Expand Down
9 changes: 4 additions & 5 deletions nipype/algorithms/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
Undefined,
)
from ..utils.filemanip import fname_presuffix, split_filename, ensure_list
from ..utils import NUMPY_MMAP

from . import confounds

Expand Down Expand Up @@ -210,7 +209,7 @@ def _gen_output_filename(self, name):

def _run_interface(self, runtime):
for fname in self.inputs.volumes:
img = nb.load(fname, mmap=NUMPY_MMAP)
img = nb.load(fname)

affine = img.affine
affine = np.dot(self.inputs.transformation_matrix, affine)
Expand Down Expand Up @@ -1320,7 +1319,7 @@ def split_rois(in_file, mask=None, roishape=None):
if roishape is None:
roishape = (10, 10, 1)

im = nb.load(in_file, mmap=NUMPY_MMAP)
im = nb.load(in_file)
imshape = im.shape
dshape = imshape[:3]
nvols = imshape[-1]
Expand Down Expand Up @@ -1411,7 +1410,7 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None):
except:
pass

ref = nb.load(in_ref, mmap=NUMPY_MMAP)
ref = nb.load(in_ref)
aff = ref.affine
hdr = ref.header.copy()
rsh = ref.shape
Expand Down Expand Up @@ -1469,7 +1468,7 @@ def merge_rois(in_files, in_idxs, in_ref, dtype=None, out_file=None):
data[idata] = cdata[0:nels]
nb.Nifti1Image(data.reshape(rsh[:3]), aff, hdr).to_filename(fname)

imgs = [nb.load(im, mmap=NUMPY_MMAP) for im in nii]
imgs = [nb.load(im) for im in nii]
allim = nb.concat_images(imgs)
allim.to_filename(out_file)

Expand Down
7 changes: 3 additions & 4 deletions nipype/algorithms/modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from nibabel import load
import numpy as np

from ..utils import NUMPY_MMAP
from ..interfaces.base import (
BaseInterface,
TraitedSpec,
Expand Down Expand Up @@ -474,7 +473,7 @@ def _generate_standard_design(
for i, out in enumerate(outliers):
numscans = 0
for f in ensure_list(sessinfo[i]["scans"]):
shape = load(f, mmap=NUMPY_MMAP).shape
shape = load(f).shape
if len(shape) == 3 or shape[3] == 1:
iflogger.warning(
"You are using 3D instead of 4D "
Expand Down Expand Up @@ -604,7 +603,7 @@ def _concatenate_info(self, infolist):
if isinstance(f, list):
numscans = len(f)
elif isinstance(f, (str, bytes)):
img = load(f, mmap=NUMPY_MMAP)
img = load(f)
numscans = img.shape[3]
else:
raise Exception("Functional input not specified correctly")
Expand Down Expand Up @@ -984,7 +983,7 @@ def _generate_clustered_design(self, infolist):
infoout[i].onsets = None
infoout[i].durations = None
if info.conditions:
img = load(self.inputs.functional_runs[i], mmap=NUMPY_MMAP)
img = load(self.inputs.functional_runs[i])
nscans = img.shape[3]
reg, regnames = self._cond_to_regress(info, nscans)
if hasattr(infoout[i], "regressors") and infoout[i].regressors:
Expand Down
7 changes: 3 additions & 4 deletions nipype/algorithms/rapidart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from nibabel import load, funcs, Nifti1Image
import numpy as np

from ..utils import NUMPY_MMAP
from ..interfaces.base import (
BaseInterface,
traits,
Expand Down Expand Up @@ -485,12 +484,12 @@ def _detect_outliers_core(self, imgfile, motionfile, runidx, cwd=None):

# read in functional image
if isinstance(imgfile, (str, bytes)):
nim = load(imgfile, mmap=NUMPY_MMAP)
nim = load(imgfile)
elif isinstance(imgfile, list):
if len(imgfile) == 1:
nim = load(imgfile[0], mmap=NUMPY_MMAP)
nim = load(imgfile[0])
else:
images = [load(f, mmap=NUMPY_MMAP) for f in imgfile]
images = [load(f) for f in imgfile]
nim = funcs.concat_images(images)

# compute global intensity signal
Expand Down
Loading