@@ -1249,10 +1249,24 @@ def merge_rois(in_files, in_idxs, in_ref,
1249
1249
import nibabel as nb
1250
1250
import numpy as np
1251
1251
import os .path as op
1252
+ import subprocess as sp
1252
1253
1253
1254
if out_file is None :
1254
1255
out_file = op .abspath ('merged.nii.gz' )
1255
1256
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
+
1256
1270
ref = nb .load (in_ref )
1257
1271
aff = ref .get_affine ()
1258
1272
hdr = ref .get_header ().copy ()
@@ -1261,23 +1275,47 @@ def merge_rois(in_files, in_idxs, in_ref,
1261
1275
npix = rsh [0 ] * rsh [1 ] * rsh [2 ]
1262
1276
ndirs = nb .load (in_files [0 ]).get_shape ()[- 1 ]
1263
1277
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
-
1276
1278
hdr .set_data_dtype (dtype )
1277
1279
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 )
1281
1319
1282
1320
return out_file
1283
1321
0 commit comments