@@ -302,6 +302,9 @@ def _list_outputs(self):
302
302
class CompCorInputSpec (BaseInterfaceInputSpec ):
303
303
realigned_file = File (exists = True , mandatory = True ,
304
304
desc = 'already realigned brain image (4D)' )
305
+ mask_file = InputMultiPath (File (exists = True , deprecated = '0.13' ,
306
+ new_name = 'mask_files' ,
307
+ desc = 'One or more mask files that determines ROI (3D)' ))
305
308
mask_files = InputMultiPath (File (exists = True ,
306
309
desc = 'One or more mask files that determines ROI (3D)' ))
307
310
merge_method = traits .Enum ('union' , 'intersect' , 'none' , xor = ['mask_index' ],
@@ -355,19 +358,21 @@ class CompCor(BaseInterface):
355
358
'tags' : ['method' , 'implementation' ]
356
359
}]
357
360
358
- def _run_interface (self , runtime ):
361
+ def _run_interface (self , runtime , tCompCor_mask = False ):
362
+
359
363
imgseries = nb .load (self .inputs .realigned_file ,
360
364
mmap = NUMPY_MMAP ).get_data ()
361
365
components = None
362
366
363
- if isdefined (self .inputs .mask_index ) and self . inputs . mask_index :
367
+ if isdefined (self .inputs .mask_index ):
364
368
if self .inputs .mask_index < len (self .inputs .mask_files ):
365
369
self .inputs .mask_files = [
366
370
self .inputs .mask_files [self .inputs .mask_index ]]
367
371
else :
368
- RuntimeWarning ('Mask index exceeded number of masks, using mask'
369
- ' {} instead' ).format (self .inputs .mask_files [0 ])
370
372
self .inputs .mask_files = self .inputs .mask_files [0 ]
373
+ if not tCompCor_mask :
374
+ RuntimeWarning ('Mask index exceeded number of masks, using '
375
+ 'mask {} instead' .format (self .inputs .mask_files [0 ]))
371
376
372
377
for mask_file in self .inputs .mask_files :
373
378
mask = nb .load (mask_file , mmap = NUMPY_MMAP ).get_data ()
@@ -380,16 +385,16 @@ def _run_interface(self, runtime):
380
385
imgseries .shape [:3 ], mask .shape ))
381
386
382
387
if (isdefined (self .inputs .merge_method ) and
383
- self .merge_method != 'none' and
384
- len (self .inputs .mask_files > 1 ) ):
388
+ self .inputs . merge_method != 'none' and
389
+ len (self .inputs .mask_files ) > 1 ):
385
390
if mask_file == self .inputs .mask_files [0 ]:
386
391
new_mask = mask
387
392
continue
388
- else :
389
- if self .inputs .merge_method == 'union' :
390
- new_mask = np .logical_or (new_mask , mask ).astype (int )
391
- elif self .inputs .merge_method == 'intersect' :
392
- new_mask = np .logical_and (new_mask , mask ).astype (int )
393
+ else :
394
+ if self .inputs .merge_method == 'union' :
395
+ new_mask = np .logical_or (new_mask , mask ).astype (int )
396
+ elif self .inputs .merge_method == 'intersect' :
397
+ new_mask = np .logical_and (new_mask , mask ).astype (int )
393
398
394
399
if mask_file != self .inputs .mask_files [- 1 ]:
395
400
continue
@@ -500,9 +505,12 @@ class TCompCor(CompCor):
500
505
output_spec = TCompCorOutputSpec
501
506
502
507
def _run_interface (self , runtime ):
508
+
503
509
_out_masks = []
504
- imgseries = nb .load (self .inputs .realigned_file ,
505
- mmap = NUMPY_MMAP ).get_data ()
510
+ img = nb .load (self .inputs .realigned_file , mmap = NUMPY_MMAP )
511
+ imgseries = img .get_data ()
512
+ aff = img .affine
513
+
506
514
507
515
if imgseries .ndim != 4 :
508
516
raise ValueError ('tCompCor expected a 4-D nifti file. Input {} has '
@@ -511,29 +519,30 @@ def _run_interface(self, runtime):
511
519
imgseries .shape ))
512
520
513
521
if isdefined (self .inputs .mask_files ):
514
- if isdefined (self .inputs .mask_index ) and self . inputs . mask_index :
522
+ if isdefined (self .inputs .mask_index ):
515
523
if self .inputs .mask_index < len (self .inputs .mask_files ):
516
524
self .inputs .mask_files = [
517
525
self .inputs .mask_files [self .inputs .mask_index ]]
518
526
else :
519
527
RuntimeWarning ('Mask index exceeded number of masks, using '
520
- 'mask {} instead' ) .format (self .inputs .mask_files [0 ])
528
+ 'mask {} instead' .format (self .inputs .mask_files [0 ]) )
521
529
self .inputs .mask_files = self .inputs .mask_files [0 ]
522
530
523
531
for i , mask_file in enumerate (self .inputs .mask_files , 1 ):
524
532
in_mask = nb .load (mask_file , mmap = NUMPY_MMAP ).get_data ()
525
533
if (isdefined (self .inputs .merge_method ) and
526
- self .merge_method != 'none' and
527
- len (self .inputs .mask_files > 1 ) ):
534
+ self .inputs . merge_method != 'none' and
535
+ len (self .inputs .mask_files ) > 1 ):
528
536
if mask_file == self .inputs .mask_files [0 ]:
529
537
new_mask = in_mask
530
538
continue
531
- else :
532
- if self .inputs .merge_method == 'union' :
533
- new_mask = np .logical_or (new_mask , in_mask ).astype (int )
534
- elif self .inputs .merge_method == 'intersect' :
535
- new_mask = np .logical_and (new_mask , in_mask ).astype (int )
536
-
539
+ else :
540
+ if self .inputs .merge_method == 'union' :
541
+ new_mask = np .logical_or (new_mask ,
542
+ in_mask ).astype (int )
543
+ elif self .inputs .merge_method == 'intersect' :
544
+ new_mask = np .logical_and (new_mask ,
545
+ in_mask ).astype (int )
537
546
if mask_file != self .inputs .mask_files [- 1 ]:
538
547
continue
539
548
else : # merge complete
@@ -559,9 +568,11 @@ def _run_interface(self, runtime):
559
568
mask_data = np .zeros_like (in_mask )
560
569
mask_data [in_mask != 0 ] = mask
561
570
# save mask
562
- mask_file = os .path .abspath ('mask{}.nii' .format (i ))
563
- nb .Nifti1Image (mask_data , nb .load (
564
- self .inputs .realigned_file ).affine ).to_filename (mask_file )
571
+ if self .inputs .merge_method == 'none' :
572
+ mask_file = os .path .abspath ('mask{}.nii' .format (i ))
573
+ else :
574
+ mask_file = os .path .abspath ('mask.nii' )
575
+ nb .Nifti1Image (mask_data , aff ).to_filename (mask_file )
565
576
IFLOG .debug ('tCompcor computed and saved mask of shape {} to '
566
577
'mask_file {}' .format (mask .shape , mask_file ))
567
578
_out_masks .append (mask_file )
@@ -577,16 +588,14 @@ def _run_interface(self, runtime):
577
588
578
589
# save mask
579
590
mask_file = os .path .abspath ('mask.nii' )
580
- nb .Nifti1Image (mask_data ,
581
- nb .load (self .inputs .realigned_file ).affine ).to_filename (mask_file )
582
- IFLOG .debug ('tCompcor computed and saved mask of shape {} to mask_file '
583
- '{}' .format (mask .shape , mask_file ))
591
+ nb .Nifti1Image (mask_data , aff ).to_filename (mask_file )
592
+ IFLOG .debug ('tCompcor computed and saved mask of shape {} to '
593
+ 'mask_file {}' .format (mask .shape , mask_file ))
584
594
_out_masks .append (mask_file )
585
595
self ._set_header ('tCompCor' )
586
596
587
597
self .inputs .mask_files = _out_masks
588
- print (self .inputs .mask_files )
589
- super (TCompCor , self )._run_interface (runtime )
598
+ super (TCompCor , self )._run_interface (runtime , tCompCor_mask = True )
590
599
return runtime
591
600
592
601
def _list_outputs (self ):
0 commit comments