Ejemplo n.º 1
0
def test_getDTIFitDataPrefix_and_isDTIFitPath():
    def make_dtifit_dir(dir, prefix, suffixes):
        for s in suffixes:
            path = op.join(dir, '{}{}'.format(prefix, s))
            with open(path, 'wt') as f:
                f.write(path)

    prefixes = ['dti', 'blob', 'random-prefix', '01234']
    suffixes = [
        '_V1.nii', '_V2.nii', '_V3.nii', '_L1.nii', '_L2.nii', '_L3.nii'
    ]
    badSuffixes = [
        '_V1.txt', '_V2.nii', '_V3.nii', '_L1.txt', '_L2.tar', '_L3.nii'
    ]

    # Valid dtifit directories
    with tests.testdir() as testdir:
        for p in prefixes:

            tests.cleardir(testdir)
            make_dtifit_dir(testdir, p, suffixes)
            assert dtifit.getDTIFitDataPrefix(testdir) == p
            assert dtifit.isDTIFitPath(testdir)

    # Invalid dtifit dir - one file missing
    with tests.testdir() as testdir:
        make_dtifit_dir(testdir, 'dti', suffixes[:-1])
        assert dtifit.getDTIFitDataPrefix(testdir) is None
        assert not dtifit.isDTIFitPath(testdir)

    # Invalid dtifit dir - non-nifti files
    with tests.testdir() as testdir:
        make_dtifit_dir(testdir, 'dti', badSuffixes)
        assert dtifit.getDTIFitDataPrefix(testdir) is None
        assert not dtifit.isDTIFitPath(testdir)

    # Ambiguous dtifit dir - multiple
    # potential prefixes. Should return
    # the first (alphabetical) one.
    with tests.testdir() as testdir:
        make_dtifit_dir(testdir, 'dti1', suffixes)
        make_dtifit_dir(testdir, 'dti2', suffixes)
        assert dtifit.getDTIFitDataPrefix(testdir) == 'dti1'
        assert dtifit.isDTIFitPath(testdir)
Ejemplo n.º 2
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
Ejemplo n.º 3
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