Exemple #1
0
def guessDataSourceType(path):
    """A convenience function which, given the name of a file or directory,
    figures out a suitable overlay type.

    Returns a tuple containing two values - a type which should be able to
    load the path, and the path itself, possibly adjusted. If the type
    is unrecognised, the first tuple value will be ``None``.
    """

    import fsl.data.mesh as fslmesh
    import fsl.data.gifti as fslgifti
    import fsl.data.featimage as featimage
    import fsl.data.melodicimage as melimage
    import fsl.data.dtifit as dtifit
    import fsl.data.melodicanalysis as melanalysis
    import fsl.data.featanalysis as featanalysis

    # Support files opened via fsleyes:// URL
    if path.startswith('fsleyes://'):
        path = path[10:]

    path = op.abspath(path)

    # VTK files are easy
    if path.endswith('.vtk'):
        return fslmesh.TriangleMesh, path

    # So are GIFTIS
    if path.endswith('.gii'):
        return fslgifti.GiftiSurface, path

    # Analysis directory?
    if op.isdir(path):
        if melanalysis.isMelodicDir(path):
            return melimage.MelodicImage, path

        elif featanalysis.isFEATDir(path):
            return featimage.FEATImage, path

        elif dtifit.isDTIFitPath(path):
            return dtifit.DTIFitTensor, path

    # Assume it's a NIFTI image
    try:
        path = fslimage.addExt(path, mustExist=True)
    except fslimage.PathError:
        return None, path

    if melanalysis.isMelodicImage(path): return melimage.MelodicImage, path
    elif featanalysis.isFEATImage(path): return featimage.FEATImage, path
    else: return fslimage.Image, path

    # Otherwise, I don't
    # know what to do
    return None, path
def test_isMelodicDir():
    paths = ['analysis.ica/melodic_IC.nii.gz',
             'analysis.ica/melodic_mix',
             'analysis.ica/melodic_FTmix']

    with tests.testdir(paths) as testdir:
        meldir = op.join(testdir, 'analysis.ica')
        assert mela.isMelodicDir(meldir)

    # Directory must end in .ica
    with tests.testdir([p.replace('.ica', '.blob') for p in paths]) as testdir:
        meldir = op.join(testdir, 'analysis.blob')
        assert not mela.isMelodicDir(meldir)

    # Directory must exist!
    assert not mela.isMelodicDir('non-existent.ica')

    # Directory must contain all of the above files
    perms = it.chain(it.combinations(paths, 1),
                     it.combinations(paths, 2))
    for p in perms:
        with tests.testdir(p) as testdir:
            meldir = op.join(testdir, 'analysis.ica')
            assert not mela.isMelodicDir(meldir)
def guessFlirtFiles(path):
    """Given a ``path`` to a NIFTI image file, tries to guess an appropriate
    FLIRT transformation matrix file and reference image. The guess is based
    on the path location (e.g. if it is a FEAT or MELODIC image).

    Returns a tuple containing paths to the matrix file and reference image,
    or ``(None, None)`` if a guess couldn't be made.
    """

    import fsl.data.featanalysis as featanalysis
    import fsl.data.melodicanalysis as melodicanalysis

    if path is None:
        return None, None

    filename = op.basename(path)
    dirname = op.dirname(path)
    regDir = None
    srcRefMap = {}

    func2struc = 'example_func2highres.mat'
    struc2std = 'highres2standard.mat'

    featDir = featanalysis.getAnalysisDir(dirname)
    melDir = melodicanalysis.getAnalysisDir(dirname)

    # TODO more heuristics
    if featDir is not None:

        regDir = op.join(featDir, 'reg')
        srcRefMap = {
            'example_func': ('highres', func2struc),
            'filtered_func_data': ('highres', func2struc),
            'mean_func': ('highres', func2struc),
            'thresh_zstat': ('highres', func2struc),
            op.join('stats', 'zstat'): ('highres', func2struc),
            op.join('stats', 'tstat'): ('highres', func2struc),
            op.join('stats', 'cope'): ('highres', func2struc),
            op.join('stats', 'pe'): ('highres', func2struc),
            'highres': ('standard', struc2std),
            'highres_head': ('standard', struc2std),
            'struc': ('standard', struc2std),
            'struct': ('standard', struc2std),
            'struct_brain': ('standard', struc2std),
            'struc_brain': ('standard', struc2std),
        }

    elif melodicanalysis.isMelodicDir(dirname):

        if melDir.startswith('filtered_func_data'):
            regDir = op.join(melDir, '..', 'reg')
        else:
            regDir = op.join(melDir, 'reg')

        srcRefMap = {
            'filtered_func_data': ('highres', func2struc),
            'melodic_IC': ('highres', func2struc),
            'example_func': ('highres', func2struc),
            'mean_func': ('highres', func2struc),
            'highres': ('standard', struc2std),
            'highres_head': ('standard', struc2std),
            'struc': ('standard', struc2std),
            'struct': ('standard', struc2std),
            'struct_brain': ('standard', struc2std),
            'struc_brain': ('standard', struc2std),
        }

    matFile = None
    refFile = None

    for src, (ref, mat) in srcRefMap.items():

        if not filename.startswith(src):
            continue

        mat = op.join(regDir, mat)
        ref = op.join(regDir, ref)

        try:
            ref = fslimage.addExt(ref)
        except Exception:
            continue

        if op.exists(mat):
            matFile = mat
            refFile = ref
            break

    return matFile, refFile
Exemple #4
0
def guessDataSourceType(path):
    """A convenience function which, given the name of a file or directory,
    figures out a suitable overlay type.

    Returns a tuple containing two values - a type which should be able to
    load the path, and the path itself, possibly adjusted. If the type
    is unrecognised, the first tuple value will be ``None``.
    """

    import fsl.data.vtk as fslvtk
    import fsl.data.gifti as fslgifti
    import fsl.data.freesurfer as fslfs
    import fsl.data.mghimage as fslmgh
    import fsl.data.featimage as featimage
    import fsl.data.melodicimage as melimage
    import fsl.data.dtifit as dtifit
    import fsl.data.melodicanalysis as melanalysis
    import fsl.data.featanalysis as featanalysis

    # Support files opened via fsleyes:// URL
    if path.startswith('fsleyes://'):
        path = path[10:]

    path = op.abspath(path)

    # Accept images sans-extension
    try:
        path = fslimage.addExt(path, mustExist=True)
    except fslimage.PathError:
        pass

    if op.isfile(path):

        # Some types are easy - just check the extensions
        if fslpath.hasExt(path, fslvtk.ALLOWED_EXTENSIONS):
            return fslvtk.VTKMesh, path
        elif fslpath.hasExt(path, fslgifti.ALLOWED_EXTENSIONS):
            return fslgifti.GiftiMesh, path
        elif fslfs.isGeometryFile(path):
            return fslfs.FreesurferMesh, path
        elif fslpath.hasExt(path, fslmgh.ALLOWED_EXTENSIONS):
            return fslmgh.MGHImage, path

        # Other specialised image types
        elif melanalysis.isMelodicImage(path):
            return melimage.MelodicImage, path
        elif featanalysis.isFEATImage(path):
            return featimage.FEATImage, path
        elif fslimage.looksLikeImage(path):
            return fslimage.Image, path

    # Analysis directory?
    elif op.isdir(path):
        if melanalysis.isMelodicDir(path):
            return melimage.MelodicImage, path
        elif featanalysis.isFEATDir(path):
            return featimage.FEATImage, path
        elif dtifit.isDTIFitPath(path):
            return dtifit.DTIFitTensor, path

    # Otherwise, I don't
    # know what to do
    return None, path