Skip to content

Commit 5ee0ffb

Browse files
committed
Interface Merge for large files
1 parent f177e3f commit 5ee0ffb

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

nipype/algorithms/misc.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,10 +1249,24 @@ def merge_rois(in_files, in_idxs, in_ref,
12491249
import nibabel as nb
12501250
import numpy as np
12511251
import os.path as op
1252+
import subprocess as sp
12521253

12531254
if out_file is None:
12541255
out_file = op.abspath('merged.nii.gz')
12551256

1257+
if dtype is None:
1258+
dtype = np.float32
1259+
1260+
# if file is compressed, uncompress using os
1261+
# to avoid memory errors
1262+
if op.splitext(in_ref)[1] == '.gz':
1263+
try:
1264+
iflogger.info('uncompress %i' % in_ref)
1265+
sp.check_call(['gunzip', in_ref], stdout=sp.PIPE, shell=True)
1266+
in_ref = op.splitext(in_ref)[0]
1267+
except:
1268+
pass
1269+
12561270
ref = nb.load(in_ref)
12571271
aff = ref.get_affine()
12581272
hdr = ref.get_header().copy()
@@ -1261,23 +1275,47 @@ def merge_rois(in_files, in_idxs, in_ref,
12611275
npix = rsh[0] * rsh[1] * rsh[2]
12621276
ndirs = nb.load(in_files[0]).get_shape()[-1]
12631277
newshape = (rsh[0], rsh[1], rsh[2], ndirs)
1264-
data = np.zeros((npix, ndirs), dtype=dtype)
1265-
for cname, iname in zip(in_files, in_idxs):
1266-
with np.load(iname) as f:
1267-
idxs = np.squeeze(f['arr_0'])
1268-
cdata = nb.load(cname).get_data().reshape(-1, ndirs)
1269-
nels = len(idxs)
1270-
idata = (idxs, )
1271-
data[idata, ...] = cdata[0:nels, ...]
1272-
1273-
if dtype is None:
1274-
dtype = np.float32
1275-
12761278
hdr.set_data_dtype(dtype)
12771279
hdr.set_xyzt_units('mm', 'sec')
1278-
hdr.set_data_shape(newshape)
1279-
nb.Nifti1Image(data.reshape(newshape).astype(dtype),
1280-
aff, hdr).to_filename(out_file)
1280+
1281+
if ndirs < 300:
1282+
data = np.zeros((npix, ndirs))
1283+
for cname, iname in zip(in_files, in_idxs):
1284+
with np.load(iname) as f:
1285+
idxs = np.squeeze(f['arr_0'])
1286+
cdata = nb.load(cname).get_data().reshape(-1, ndirs)
1287+
nels = len(idxs)
1288+
idata = (idxs, )
1289+
data[idata, ...] = cdata[0:nels, ...]
1290+
hdr.set_data_shape(newshape)
1291+
1292+
nb.Nifti1Image(data.reshape(newshape).astype(dtype),
1293+
aff, hdr).to_filename(out_file)
1294+
1295+
1296+
else:
1297+
hdr.set_data_shape(rsh[:3])
1298+
nii = []
1299+
for d in xrange(ndirs):
1300+
fname = op.abspath('vol%06d.nii' % d)
1301+
nb.Nifti1Image(np.zeros(rsh[:3]), aff, hdr).to_filename(fname)
1302+
nii.append(fname)
1303+
1304+
for cname, iname in zip(in_files, in_idxs):
1305+
with np.load(iname) as f:
1306+
idxs = np.squeeze(f['arr_0'])
1307+
1308+
for d, fname in enumerate(nii):
1309+
data = nb.load(fname).get_data().reshape(-1)
1310+
cdata = nb.load(cname).get_data().reshape(-1, ndirs)[:, d]
1311+
nels = len(idxs)
1312+
idata = (idxs, )
1313+
data[idata] = cdata[0:nels]
1314+
nb.Nifti1Image(data.reshape(rsh[:3]), aff, hdr).to_filename(fname)
1315+
1316+
imgs = [nb.load(im) for im in nii]
1317+
allim = nb.concat_images(imgs)
1318+
allim.to_filename(out_file)
12811319

12821320
return out_file
12831321

0 commit comments

Comments
 (0)