Skip to content
Snippets Groups Projects
Commit 3cca2af2 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Merge branch 'enh/guess_type' into 'master'

Enh/guess type

See merge request fsl/fslpy!90
parents 6532e0f2 08d8b468
No related branches found
No related tags found
No related merge requests found
Pipeline #3001 canceled
......@@ -20,6 +20,7 @@
fsl.data.melodicimage
fsl.data.mesh
fsl.data.mghimage
fsl.data.utils
fsl.data.vest
fsl.data.volumelabels
fsl.data.vtk
......
``fsl.data.utils``
==================
.. automodule:: fsl.data.utils
:members:
:undoc-members:
:show-inheritance:
......@@ -7,3 +7,6 @@
"""This module contains various data types and associated I/O routines,
models, constants, and other data-like things used throughout ``fslpy``.
"""
from .utils import guessType # noqa
#!/usr/bin/env python
#
# utils.py - Miscellaneous utility functions
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module is home to some miscellaneous utility functions for working
with the data types defined in the :mod:`fsl.data` package.
"""
import os.path as op
def guessType(path):
"""A convenience function which, given the name of a file or directory,
attempts to figure out a suitable data 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.utils.path as fslpath
import fsl.data.image as fslimage
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
......@@ -358,6 +358,56 @@ def make_mock_feat_analysis(featdir,
return featdir
def make_mock_melodic_analysis(basedir, shape4D, ntimepoints, xform=None):
if xform is None:
xform = np.eye(4)
ncomps = shape4D[-1]
halftime = int(np.floor(ntimepoints / 2))
os.makedirs(basedir)
make_random_image(op.join(basedir, 'melodic_IC.nii.gz'),
dims=shape4D,
xform=xform)
mix = np.random.randint(1, 255, (ntimepoints, ncomps))
ftmix = np.random.randint(1, 255, (halftime, ncomps))
np.savetxt(op.join(basedir, 'melodic_mix'), mix)
np.savetxt(op.join(basedir, 'melodic_FTmix'), ftmix)
def make_mock_dtifit_analysis(basedir, shape3D, basename='dti', xform=None, tensor=False):
if xform is None:
xform = np.eye(4)
os.makedirs(basedir)
shape4D = tuple(shape3D) + (3,)
def mk(ident, shp):
make_random_image(
op.join(basedir, '{}_{}.nii.gz'.format(basename, ident)),
shp,
xform)
mk('V1', shape4D)
mk('V2', shape4D)
mk('V3', shape4D)
mk('L1', shape3D)
mk('L2', shape3D)
mk('L3', shape3D)
mk('S0', shape3D)
mk('MD', shape3D)
mk('MO', shape3D)
mk('FA', shape3D)
if tensor:
mk('tensor', tuple(shape3D) + (6,))
def make_random_mask(filename, shape, xform, premask=None, minones=1):
"""Make a random binary mask image. """
......
#!/usr/bin/env python
#
# test_fsl_data_utils.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import shutil
import os
import os.path as op
import fsl.utils.tempdir as tempdir
import fsl.data.utils as dutils
import fsl.utils.path as fslpath
import fsl.data.image as fslimage
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
from . import (touch,
make_mock_feat_analysis,
make_mock_melodic_analysis,
make_mock_dtifit_analysis)
def test_guessType():
def asrt(path, cls):
restype, respath = dutils.guessType(path)
assert restype is cls
if path.startswith('fsleyes://'):
path = path[10:]
# image path might not have an extension
try:
path = fslimage.addExt(path, mustExist=True)
except fslimage.PathError:
pass
assert respath == op.abspath(path)
with tempdir.tempdir() as td:
touch('foo.nii')
asrt('foo', fslimage.Image)
asrt('foo.nii', fslimage.Image)
asrt('fsleyes://foo', fslimage.Image)
asrt('fsleyes://foo.nii', fslimage.Image)
os.remove('foo.nii')
touch('foo.vtk')
asrt('foo.vtk', fslvtk.VTKMesh)
os.remove('foo.vtk')
touch('foo.surf.gii')
asrt('foo.surf.gii', fslgifti.GiftiMesh)
os.remove('foo.surf.gii')
touch('lh.pial')
asrt('lh.pial', fslfs.FreesurferMesh)
os.remove('lh.pial')
touch('foo.mgz')
asrt('foo.mgz', fslmgh.MGHImage)
os.remove('foo.mgz')
make_mock_melodic_analysis('filtered_func_data.ica',
(10, 10, 10, 10),
20)
asrt('filtered_func_data.ica/melodic_IC', melimage.MelodicImage)
asrt('filtered_func_data.ica/melodic_IC.nii.gz', melimage.MelodicImage)
asrt('filtered_func_data.ica', melimage.MelodicImage)
asrt('filtered_func_data.ica/', melimage.MelodicImage)
shutil.rmtree('filtered_func_data.ica')
featdir = op.join(op.dirname(__file__),
'testdata', 'test_feat', '1stlevel_1.feat')
make_mock_feat_analysis(featdir,
td,
(10, 10, 10, 10))
asrt('1stlevel_1.feat/filtered_func_data', featimage.FEATImage)
asrt('1stlevel_1.feat/filtered_func_data.nii.gz', featimage.FEATImage)
asrt('1stlevel_1.feat', featimage.FEATImage)
make_mock_dtifit_analysis('dti', (10, 10, 10))
asrt('dti', dtifit.DTIFitTensor)
shutil.rmtree('dti')
asrt('noexist', None)
touch('norecognise')
asrt('norecognise', None)
touch('norecognise.txt')
asrt('norecognise.txt', None)
os.remove('norecognise')
os.remove('norecognise.txt')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment