@classmethod
    def from_fsl(cls, xfm, func_nii, anat_nii):
        """Converts an fsl transform to a pycortex transform.

        Converts a transform computed using FSL's FLIRT to a transform ("xfm") object in pycortex.
        The transform must have been computed FROM the nifti volume specified in `func_nii` TO the 
        volume specified in `anat_nii` (See Notes below).

        Parameters
        ----------
        xfm : array
            4x4 transformation matrix, loaded from an FSL .mat file, for a transform computed 
            FROM the func_nii volume TO the anat_nii volume. Alternatively, a string file name
            for the FSL .mat file.
        anat_nii : str or nibabel.Nifti1Image
            nibabel image object (or path to nibabel-readable image) for anatomical volume from 
            which cortical surface was created
        func_nii : str or nibabel.Nifti1Image
            nibabel image object (or string path to nibabel-readable image) for (functional) data volume 
            to be projected onto cortical surface

        Returns
        -------
        xfm : cortex.xfm.Transform object
            A pycortex COORD transform. 

        Notes
        -----
        The transform is assumed to be computed FROM the functional data TO the anatomical data.
        In FSL speak, that means that the arguments to flirt should have been:
        flirt -in <func_nii> -ref <anat_nii> ...

        """
        ## -- Adapted from dipy.external.fsl.flirt2aff -- ##
        import nibabel
        import numpy.linalg as npl
        inv = npl.inv

        # Load transform from text file, if string is provided
        if isinstance(xfm,(str,unicode)):
            with open(xfm,'r') as fid:
                L = fid.readlines()
            xfm  = np.array([[np.float(s) for s in ll.split() if s] for ll in L])

        # Internally, pycortex computes the OPPOSITE transform: from anatomical volume to functional volume. 
        # Thus, assign anat to "infile" (starting point for transform)
        infile = anat_nii
        # Assign func to "reffile" (end point for transform)
        reffile = func_nii
        # and invert the usual direction (change from func>anat to anat>func)
        xfm = inv(xfm)

        try:
            inIm = nibabel.load(infile)
        except __HOLE__:
            inIm = infile
        
        refIm = nibabel.load(reffile)
        in_hdr = inIm.get_header()
        ref_hdr = refIm.get_header()
        # get_zooms gets the positive voxel sizes as returned in the header
        inspace = np.diag(in_hdr.get_zooms()[:3] + (1,))
        refspace = np.diag(ref_hdr.get_zooms()[:3] + (1,))
        # Since FSL does not use the full transform info in the nifti header, 
        # determine whether the transform indicates that the X axis should be 
        # flipped; if so, flip the X axis (for both infile and reffile)
        if npl.det(in_hdr.get_best_affine())>=0:
            inspace = np.dot(inspace, _x_flipper(in_hdr.get_data_shape()[0]))
        if npl.det(ref_hdr.get_best_affine())>=0:
            refspace = np.dot(refspace, _x_flipper(ref_hdr.get_data_shape()[0]))

        inAffine = inIm.get_affine()
        
        coord = np.dot(inv(refspace),np.dot(xfm,np.dot(inspace,inv(inAffine))))
        return cls(coord, refIm)
dataset/ETHPy150Open gallantlab/pycortex/cortex/xfm.py/Transform.from_fsl