@@ -115,10 +115,10 @@ class CiftiHandler(object):
115
115
116
116
Methods
117
117
-------
118
- * get_volume_data : Extract volume data
119
- * get_surface_data : Extract surface data for a given hemisphere
120
- * get_all_data : Convenience function for extracting surface and volume data
121
- * create_new_cifti : Create new CIFTI image from provided data
118
+ * `` get_volume_data`` : Extract volume data
119
+ * `` get_surface_data`` : Extract surface data for a given hemisphere
120
+ * `` get_all_data`` : Convenience function for extracting surface and volume data
121
+ * `` create_new_cifti`` : Create new CIFTI image from provided data
122
122
123
123
Example usage
124
124
-------------
@@ -401,20 +401,18 @@ class CiftiMasker(object):
401
401
402
402
Methods
403
403
-------
404
- * fit : Load mask
405
- * transform : Load dataset, applying mask
406
- * transform_multiple : Load multiple datasets, applying mask
407
- * fit_transform[_multiple] : Fit and transform in one go
408
- * inverse_transform : Create new CIFTI image from masked data
409
- * uncache : Clear cache
404
+ * ``transform`` : Load dataset, applying mask
405
+ * ``transform_multiple`` : Load multiple datasets, applying mask
406
+ * ``inverse_transform`` : Create new CIFTI image from masked data
407
+ * ``uncache`` : Clear cache
410
408
411
409
Example usage
412
410
-------------
413
411
Use masks from Freesurfer Desikan-Killiany atlas for example subject.
414
412
415
413
>>> maskfile = '/mnt/hcpdata/Facelab/100610/MNINonLinear/' \\
416
414
... 'fsaverage_LR32k/100610.aparc.32k_fs_LR.dlabel.nii'
417
- >>> masker = CiftiMasker(maskfile).fit()
415
+ >>> masker = CiftiMasker(maskfile)
418
416
419
417
Use the ``transform`` method to mask data. By default, this will mask by
420
418
the label with a numerical ID of 1 - this will work if the dlabel file
@@ -462,14 +460,14 @@ class CiftiMasker(object):
462
460
463
461
(900, 0)
464
462
465
- Instead of using a dlabel mask file, you can also mask by one of the
463
+ Instead of using a CIFTI mask file, you can also mask by one of the
466
464
labelled structures contained in the data CIFTI file. This is most useful
467
465
for extracting subcortical regions. You can use the full structure name,
468
466
or anything recognised by nibabel's ``to_cifti_brain_structure_name``
469
467
method. Here, we extract data for the left amygdala, comprising 900
470
468
timepoints and 315 voxels.
471
469
472
- >>> masker = CiftiMasker('left amygdala').fit()
470
+ >>> masker = CiftiMasker('left amygdala')
473
471
>>> ROI_data = masker.transform(infile)
474
472
>>> print(ROI_data.shape)
475
473
@@ -485,13 +483,27 @@ class CiftiMasker(object):
485
483
>>> new_img.to_filename('my_masked_data.dtseries.nii')
486
484
"""
487
485
def __init__ (self , mask_img ):
486
+ # Assign arg to class
488
487
self .mask_img = mask_img
489
- self ._is_fitted = False
490
488
491
- def _check_is_fitted (self ):
492
- if not self ._is_fitted :
493
- raise Exception ('This instance is not fitted yet. '
494
- 'Call .fit method first.' )
489
+ # Select CIFTI structure, or load from file, or pass through objects
490
+ try :
491
+ self .mask_struct = nib .cifti2 .BrainModelAxis \
492
+ .to_cifti_brain_structure_name (self .mask_img )
493
+ self ._mask_is_cifti_struct = True
494
+
495
+ except ValueError :
496
+ if isinstance (self .mask_img , CiftiHandler ):
497
+ self .mask_handler = copy .deepcopy (CiftiHandler )
498
+ self .mask_handler .full_surface = True
499
+ elif (isinstance (self .mask_img , str ) and os .path .isfile (self .mask_img )) \
500
+ or isinstance (self .mask_img , nib .Cifti2Image ):
501
+ self .mask_handler = CiftiHandler (self .mask_img , full_surface = True )
502
+ else :
503
+ raise ValueError ('Invalid mask image' )
504
+
505
+ self .mask_dict = self .mask_handler .get_all_data (dtype = int )
506
+ self ._mask_is_cifti_struct = False
495
507
496
508
def _resample_to_data (self , dict_ , data_handler , block = 'all' ):
497
509
"""
@@ -562,32 +574,6 @@ def _parse_labelID(self, labelID, mapN):
562
574
else :
563
575
raise TypeError ('Invalid label ID type' )
564
576
565
- def fit (self ):
566
- """
567
- Load mask
568
- """
569
- # Select CIFTI structure, or load from file, or pass through objects
570
- try :
571
- self .mask_struct = nib .cifti2 .BrainModelAxis \
572
- .to_cifti_brain_structure_name (self .mask_img )
573
- self ._mask_is_cifti_struct = True
574
- except ValueError :
575
- if isinstance (self .mask_img , CiftiHandler ):
576
- self .mask_handler = copy .deepcopy (CiftiHandler )
577
- self .mask_handler .full_surface = True
578
- elif (isinstance (self .mask_img , str ) and os .path .isfile (self .mask_img )) \
579
- or isinstance (self .mask_img , nib .Cifti2Image ):
580
- self .mask_handler = CiftiHandler (self .mask_img , full_surface = True )
581
- else :
582
- raise ValueError ('Invalid mask image' )
583
-
584
- self .mask_dict = self .mask_handler .get_all_data (dtype = int )
585
- self ._mask_is_cifti_struct = False
586
-
587
- # Return
588
- self ._is_fitted = True
589
- return self
590
-
591
577
def transform (self , img , mask_block = 'all' , labelID = 1 , mapN = 0 , dtype = None ):
592
578
"""
593
579
Load data from CIFTI and apply mask
@@ -624,9 +610,6 @@ def transform(self, img, mask_block='all', labelID=1, mapN=0, dtype=None):
624
610
data_array : ndarray
625
611
[nSamples x nGrayOrdinates] array of data values after masking
626
612
"""
627
- # Error check
628
- self ._check_is_fitted ()
629
-
630
613
# Open handler for data file
631
614
if isinstance (img , CiftiHandler ):
632
615
self .data_handler = copy .deepcopy (img )
@@ -692,14 +675,6 @@ def transform_multiple(self, imgs, vstack=False, *args, **kwargs):
692
675
data = np .vstack (data )
693
676
return data
694
677
695
- def fit_transform (self , * args , ** kwargs ):
696
- self .fit ()
697
- return self .transform (* args , ** kwargs )
698
-
699
- def fit_transform_multiple (self , * args , ** kwargs ):
700
- self .fit ()
701
- return self .transform_multiple (* args , ** kwargs )
702
-
703
678
def inverse_transform (self , data_array , mask_block = 'all' , labelID = 1 ,
704
679
mapN = 0 , dtype = None , template_img = None ,
705
680
return_as_cifti = True , * args , ** kwargs ):
@@ -714,8 +689,8 @@ def inverse_transform(self, data_array, mask_block='all', labelID=1,
714
689
array containing masked data.
715
690
716
691
mask_block : str {all | lh | rh | surface | volume}
717
- Which blocks from the CIFTI array to return data from . Should
718
- match value supplied to forward transform.
692
+ Which blocks in the CIFTI array to allocate data to . Should match
693
+ value supplied to forward transform.
719
694
720
695
labelID : int or str
721
696
ID or name of label to select if mask contains multiple labels.
@@ -748,9 +723,6 @@ def inverse_transform(self, data_array, mask_block='all', labelID=1,
748
723
Data reshaped to full set of grayordinates. Returned as Cifti2Image
749
724
object if return_as_cifti is True, otherwise returned as array.
750
725
"""
751
- # Error check
752
- self ._check_is_fitted ()
753
-
754
726
# Check dtype
755
727
if dtype is None :
756
728
dtype = data_array .dtype
@@ -804,7 +776,6 @@ def uncache(self):
804
776
"""
805
777
Clear mask and data from cache
806
778
"""
807
- self ._check_is_fitted ()
808
779
if not self ._mask_is_cifti_struct :
809
780
self .mask_handler .uncache ()
810
781
self .data_handler .uncache ()
0 commit comments