#NEWLINE# @ classmethod #NEWLINE# def from_fsl( cls , xfm , func_nii , anat_nii ) : #NEWLINE# #INDENT# 'Converts an fsl transform to a pycortex transform.\n\n        Converts a transform computed using FSL\'s FLIRT to a transform ("xfm") object in pycortex.\n        The transform must have been computed FROM the nifti volume specified in `func_nii` TO the \n        volume specified in `anat_nii` (See Notes below).\n\n        Parameters\n        ----------\n        xfm : array\n            4x4 transformation matrix, loaded from an FSL .mat file, for a transform computed \n            FROM the func_nii volume TO the anat_nii volume. Alternatively, a string file name\n            for the FSL .mat file.\n        anat_nii : str or nibabel.Nifti1Image\n            nibabel image object (or path to nibabel-readable image) for anatomical volume from \n            which cortical surface was created\n        func_nii : str or nibabel.Nifti1Image\n            nibabel image object (or string path to nibabel-readable image) for (functional) data volume \n            to be projected onto cortical surface\n\n        Returns\n        -------\n        xfm : cortex.xfm.Transform object\n            A pycortex COORD transform. \n\n        Notes\n        -----\n        The transform is assumed to be computed FROM the functional data TO the anatomical data.\n        In FSL speak, that means that the arguments to flirt should have been:\n        flirt -in <func_nii> -ref <anat_nii> ...\n\n        ' #NEWLINE# import nibabel #NEWLINE# import numpy.linalg as npl #NEWLINE# inv = npl . inv #NEWLINE# if isinstance ( xfm , ( str , unicode ) ) : #NEWLINE# #INDENT# with open ( xfm , 'r' ) as fid : #NEWLINE# #INDENT# L = fid . readlines ( ) #NEWLINE# #UNINDENT# xfm = np . array ( [ [ np . float ( s ) for s in ll . split ( ) if s ] for ll in L ] ) #NEWLINE# #UNINDENT# infile = anat_nii #NEWLINE# reffile = func_nii #NEWLINE# xfm = inv ( xfm ) #NEWLINE# try : #NEWLINE# #INDENT# inIm = nibabel . load ( infile ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# inIm = infile #NEWLINE# #UNINDENT# refIm = nibabel . load ( reffile ) #NEWLINE# in_hdr = inIm . get_header ( ) #NEWLINE# ref_hdr = refIm . get_header ( ) #NEWLINE# inspace = np . diag ( ( in_hdr . get_zooms ( ) [ : 3 ] + ( 1 , ) ) ) #NEWLINE# refspace = np . diag ( ( ref_hdr . get_zooms ( ) [ : 3 ] + ( 1 , ) ) ) #NEWLINE# if ( npl . det ( in_hdr . get_best_affine ( ) ) >= 0 ) : #NEWLINE# #INDENT# inspace = np . dot ( inspace , _x_flipper ( in_hdr . get_data_shape ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# if ( npl . det ( ref_hdr . get_best_affine ( ) ) >= 0 ) : #NEWLINE# #INDENT# refspace = np . dot ( refspace , _x_flipper ( ref_hdr . get_data_shape ( ) [ 0 ] ) ) #NEWLINE# #UNINDENT# inAffine = inIm . get_affine ( ) #NEWLINE# coord = np . dot ( inv ( refspace ) , np . dot ( xfm , np . dot ( inspace , inv ( inAffine ) ) ) ) #NEWLINE# return cls ( coord , refIm )