Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • paulmc/fslpy
  • ndcn0236/fslpy
  • seanf/fslpy
3 results
Show changes
Showing
with 962 additions and 177 deletions
#!/usr/bin/env python
#
# __init__.py - The fsl.scripts package.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""The ``fsl.scripts`` package contains all of the executable scripts provided
by ``fslpy``.
"""
......@@ -19,20 +19,9 @@ import warnings
import logging
import numpy as np
# if h5py <= 2.7.1 is installed,
# it will be imported via nibabel,
# and will cause a numpy warning
# to be emitted.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.image as fslimage
# If wx is not present, then fsl.utils.platform
# will complain that it is not present.
logging.getLogger('fsl.utils.platform').setLevel(logging.ERROR)
import fsl.data.atlases as fslatlases # noqa
import fsl.version as fslversion # noqa
import fsl.data.image as fslimage
import fsl.data.atlases as fslatlases
import fsl.version as fslversion
log = logging.getLogger(__name__)
......@@ -381,7 +370,7 @@ def maskQuery(atlas, masks, *args, **kwargs):
labels = []
props = []
zprops = atlas.maskProportions(mask)
zprops = atlas.maskValues(mask)
for i in range(len(zprops)):
if zprops[i] > 0:
......@@ -405,7 +394,7 @@ def coordQuery(atlas, coords, voxel, *args, **kwargs):
if isinstance(atlas, fslatlases.ProbabilisticAtlas):
props = atlas.proportions(coord, voxel=voxel)
props = atlas.values(coord, voxel=voxel)
labels = []
nzprops = []
......@@ -569,10 +558,10 @@ def parseArgs(args):
usages = {
'main' : 'usage: atlasq [-h] command [options]',
'ohi' : textwrap.dedent("""
usage: atlasq ohi -h
atlasq ohi --dumpatlases
atlasq ohi -a atlas -c X,Y,Z
atlasq ohi -a atlas -m mask
usage: atlasquery -h
atlasquery --dumpatlases
atlasquery -a atlas -c X,Y,Z
atlasquery -a atlas -m mask
""").strip(),
'list' : 'usage: atlasq list [-e]',
'summary' : 'usage: atlasq summary atlas',
......
#!/usr/bin/env python
#
# fsl_abspath.py - Make a path absolute
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""The fsl_abspath command - makes relative paths absolute.
"""
import os.path as op
import sys
usage = """
usage: fsl_abspath path
""".strip()
def main(argv=None):
"""fsl_abspath - make a relative path absolute. """
if argv is None:
argv = sys.argv[1:]
if len(argv) != 1:
print(usage)
return 1
print(op.realpath(argv[0]))
return 0
if __name__ == '__main__':
sys.exit(main())
#!/usr/bin/env python
#
# fsl_apply_x5.py - Apply an X5 transformation to an image.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""The ``fsl_apply_x5`` script can be used to apply an X5 transformation file
to resample an image.
"""
import functools as ft
import sys
import argparse
import fsl.transform.x5 as x5
import fsl.transform.nonlinear as nonlinear
import fsl.utils.parse_data as parse_data
import fsl.utils.image.resample as resample
import fsl.data.image as fslimage
def parseArgs(args):
"""Parses command-line arguments.
:arg args: Sequence of command-line arguments. If ``None``, ``sys.argv``
is used
:returns: An ``argparse.Namespace`` object containing parsed arguments
"""
parser = argparse.ArgumentParser('fsl_apply_x5')
flags = {
'input' : ('input',),
'xform' : ('xform',),
'output' : ('output',),
'interp' : ('-i', '--interp'),
'ref' : ('-r', '--ref'),
}
helps = {
'input' : 'Input image',
'xform' : 'X5 transformation file',
'output' : 'Output image',
'interp' : 'Interpolation (default: linear)',
'ref' : 'Alternate reference image (default: '
'reference specified in X5 file)',
}
opts = {
'input' : dict(help=helps['input'],
type=parse_data.Image),
'xform' : dict(help=helps['xform']),
'output' : dict(help=helps['output'],
type=parse_data.ImageOut),
'interp' : dict(help=helps['interp'],
choices=('nearest', 'linear', 'cubic'),
default='linear'),
'ref' : dict(help=helps['ref'],
type=parse_data.Image),
}
parser.add_argument(*flags['input'], **opts['input'])
parser.add_argument(*flags['xform'], **opts['xform'])
parser.add_argument(*flags['output'], **opts['output'])
parser.add_argument(*flags['interp'], **opts['interp'])
parser.add_argument(*flags['ref'], **opts['ref'])
if len(args) == 0:
parser.print_help()
sys.exit(0)
args = parser.parse_args(args)
if args.interp == 'nearest': args.interp = 0
elif args.interp == 'linear': args.interp = 1
elif args.interp == 'cubic': args.interp = 3
return args
def applyLinear(args):
"""Applies a linear X5 transformation file to the input.
:arg args: ``argparse.Namespace`` object
:returns: The transformed input as an :class:`.Image` object
"""
input = args.input
xform, src, ref = x5.readLinearX5(args.xform)
if args.ref is not None:
ref = args.ref
res, xform = resample.resampleToReference(input,
ref,
matrix=xform,
order=args.interp)
return fslimage.Image(res, xform=xform, header=ref.header)
def applyNonlinear(args):
"""Applies a non-linear X5 transformation file to the input.
:arg args: ``argparse.Namespace`` object
:returns: The transformed input as an :class:`.Image` object
"""
field = x5.readNonLinearX5(args.xform)
if args.ref is None: ref = field.ref
else: ref = args.ref
result = nonlinear.applyDeformation(args.input,
field,
ref=ref,
order=args.interp,
mode='constant')
return fslimage.Image(result, header=ref.header)
def main(args=None):
"""Entry point. Parse command-line arguments, then calls
:func:`applyLinear` or :func:`applyNonlinear` depending on the x5 file
type.
"""
if args is None:
args = sys.argv[1:]
print()
print('Warning: this version of fsl_apply_x5 is a development release.\n'
'Interface, behaviour, and input/output formats of future versions\n'
'may differ substantially from this version.')
print()
args = parseArgs(args)
if x5.inferType(args.xform) == 'linear':
result = applyLinear(args)
else:
result = applyNonlinear(args)
result.save(args.output)
if __name__ == '__main__':
sys.exit(main())
#!/usr/bin/env python
#
# fsl_convert_x5.py -
# fsl_convert_x5.py - Convert between FSL and X5 transformation files.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This script can be used to convert between FSL and X5 linear and non-linear
transformation file formats.
"""
import os.path as op
import sys
import shutil
import logging
import argparse
import os.path as op
import functools as ft
import textwrap as tw
import sys
import shutil
import logging
import argparse
import fsl.data.image as fslimage
import fsl.transform as transform
import fsl.data.image as fslimage
import fsl.utils.parse_data as parse_data
import fsl.transform.flirt as flirt
import fsl.transform.fnirt as fnirt
import fsl.transform.x5 as x5
log = logging.getLogger(__name__)
def parseArgs(args):
"""Create an argument parser and parse the given ``args``.
:arg args: Sequence of commane line arguments.
:return: An ``argparse.Namespace`` object containing parsed arguments.
"""
helps = {
'input' : 'Input file',
'output' : 'Output file',
'source' : 'Source image',
'reference' : 'Reference image',
'input_format' : 'Input format - if not provided, the input format '
'is automatically inferred from the input file '
'name.',
'output_format' : 'Output format - if not provided, the output format '
'is automatically inferred from the output file '
'name.',
}
epilog = tw.dedent("""
If the input and output file formats are the same, the input file
is simply copied to the output file.
""").strip()
parser = argparse.ArgumentParser('fsl_convert_x5')
subparsers = parser.add_subparsers(dest='ctype')
flirt = subparsers.add_parser('flirt')
fnirt = subparsers.add_parser('fnirt')
flirt.add_argument('input')
flirt.add_argument('output')
flirt.add_argument('-s', '--source')
flirt.add_argument('-r', '--reference')
flirt.add_argument('-if', '--input_format', choices=('x5', 'mat'))
flirt.add_argument('-of', '--output_format', choices=('x5', 'mat'))
intype = fnirt.add_mutually_exclusive_group()
outtype = fnirt.add_mutually_exclusive_group()
fnirt .add_argument('input')
fnirt .add_argument('output')
fnirt .add_argument('-s', '--source')
fnirt .add_argument('-r', '--reference')
fnirt .add_argument('-if', '--input_format', choices=('x5', 'nii'))
fnirt .add_argument('-of', '--output_format', choices=('x5', 'nii'))
intype .add_argument('-ai', '--absin', action='store_const', const='absolute', dest='inDispType')
intype .add_argument('-ri', '--relin', action='store_const', const='relative', dest='inDispType')
outtype.add_argument('-ao', '--absout', action='store_const', const='absolute', dest='outDispType')
outtype.add_argument('-ro', '--relout', action='store_const', const='relative', dest='outDispType')
args = parser.parse_args(args)
if args.ctype is None:
flirt = subparsers.add_parser('flirt', epilog=epilog)
fnirt = subparsers.add_parser('fnirt', epilog=epilog)
imgtype = parse_data.Image
flirt.add_argument('input', help=helps['input'])
flirt.add_argument('output', help=helps['output'])
flirt.add_argument('-s', '--source', help=helps['source'],
type=imgtype)
flirt.add_argument('-r', '--reference', help=helps['reference'],
type=imgtype)
flirt.add_argument('-if', '--input_format', help=helps['input_format'],
choices=('x5', 'mat'))
flirt.add_argument('-of', '--output_format', help=helps['output_format'],
choices=('x5', 'mat'))
fnirt .add_argument('input', help=helps['input'])
fnirt .add_argument('output', help=helps['output'])
fnirt .add_argument('-s', '--source', help=helps['source'],
type=imgtype)
fnirt .add_argument('-r', '--reference', help=helps['reference'],
type=imgtype)
fnirt .add_argument('-if', '--input_format', help=helps['input_format'],
choices=('x5', 'nii'))
fnirt .add_argument('-of', '--output_format', help=helps['output_format'],
choices=('x5', 'nii'))
if len(args) == 0:
parser.print_help()
sys.exit(0)
def getfmt(arg, fname):
if len(args) == 1:
if args[0] == 'flirt':
flirt.print_help()
sys.exit(0)
elif args[0] == 'fnirt':
fnirt.print_help()
sys.exit(0)
args = parser.parse_args(args)
# If input/output formats were not
# specified, infer them from the
# file names
def getfmt(arg, fname, exist):
ext = op.splitext(fname)[1]
if ext in ('.mat', '.x5'):
return ext[1:]
if fslimage.looksLikeImage(fslimage.fixExt(fname)):
fname = fslimage.addExt(fname, mustExist=exist)
if fslimage.looksLikeImage(fname):
return 'nii'
parser.error('Could not infer format from '
'filename: {}'.format(args.input))
if args.input_format is None: args.input_format = getfmt('input', args.input)
if args.output_format is None: args.output_format = getfmt('output', args.output)
if args.input_format is None:
args.input_format = getfmt('input', args.input, True)
if args.output_format is None:
args.output_format = getfmt('output', args.output, False)
# The source and reference arguments are
# required if the input is a FLIRT matrix
# or a FNIRT displacement/coefficient field.
if args.input_format in ('mat', 'nii') and \
(args.source is None or args.reference is None):
parser.error('You must specify a source and reference '
'when the input is not an X5 file!')
return args
def flirtToX5(args):
src = fslimage.Image(args.source, loadData=False)
ref = fslimage.Image(args.reference, loadData=False)
xform = transform.readFlirt(args.input)
xform = transform.fromFlirt(xform, src, ref, 'world', 'world')
transform.writeLinearX5(args.output, xform, src, ref)
"""Convert a linear FLIRT transformation matrix to an X5 transformation
file.
"""
src = args.source
ref = args.reference
xform = flirt.readFlirt(args.input)
xform = flirt.fromFlirt(xform, src, ref, 'world', 'world')
x5.writeLinearX5(args.output, xform, src, ref)
def X5ToFlirt(args):
xform, src, ref = transform.readLinearX5(args.input)
xform = transform.toFlirt(xform, src, ref, 'world', 'world')
transform.writeFlirt(xform, args.output)
"""Convert a linear X5 transformation file to a FLIRT matrix. """
xform, src, ref = x5.readLinearX5(args.input)
xform = flirt.toFlirt(xform, src, ref, 'world', 'world')
flirt.writeFlirt(xform, args.output)
def fnirtToX5(args):
src = fslimage.Image(args.source, loadData=False)
ref = fslimage.Image(args.reference, loadData=False)
field = transform.readFnirt(args.input,
src=src,
ref=ref,
dispType=args.inDispType)
field = transform.fromFnirt(field, 'world', 'world')
transform.writeNonLinearX5(args.output, field)
"""Convert a non-linear FNIRT transformation into an X5 transformation
file.
"""
src = args.source
ref = args.reference
field = fnirt.readFnirt(args.input, src=src, ref=ref)
field = fnirt.fromFnirt(field, 'world', 'world')
x5.writeNonLinearX5(args.output, field)
def X5ToFnirt(args):
field = transform.readNonLinearX5(args.input)
field = transform.toFnirt(field, 'world', 'world')
transform.writeFnirt(field, args.output)
"""Convert a non-linear X5 transformation file to a FNIRT-style
transformation file.
"""
field = x5.readNonLinearX5(args.input)
field = fnirt.toFnirt(field)
field.save(args.output)
def doFlirt(args):
"""Converts a linear FIRT transformation file to or from the X5 format. """
infmt = args.input_format
outfmt = args.output_format
......@@ -110,6 +174,9 @@ def doFlirt(args):
def doFnirt(args):
"""Converts a non-linear FNIRT transformation file to or from the X5
format.
"""
infmt = args.input_format
outfmt = args.output_format
......@@ -119,15 +186,28 @@ def doFnirt(args):
def main(args=None):
"""Entry point. Calls :func:`doFlirt` or :func:`doFnirt` depending on
the sub-command specified in the arguments.
:arg args: Sequence of command-line arguments. If not provided,
``sys.argv`` is used.
"""
if args is None:
args = sys.argv[1:]
args = parseArgs(args)
ctype = args.ctype
print()
print('Warning: this version of fsl_convert_x5 is a development release.\n'
'Interface, behaviour, and input/output formats of future versions\n'
'may differ substantially from this version.')
print()
args = parseArgs(args)
if args.ctype == 'flirt': doFlirt(args)
elif args.ctype == 'fnirt': doFnirt(args)
if ctype == 'flirt': doFlirt(args)
elif ctype == 'fnirt': doFnirt(args)
return 0
if __name__ == '__main__':
......
......@@ -9,8 +9,6 @@ time series from a MELODIC ``.ica`` directory.
"""
from __future__ import print_function
import os.path as op
import sys
import argparse
......@@ -18,12 +16,8 @@ import warnings
import numpy as np
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.fixlabels as fixlabels
import fsl.data.melodicanalysis as melanalysis
import fsl.data.fixlabels as fixlabels
import fsl.data.melodicanalysis as melanalysis
DTYPE = np.float64
......
......@@ -12,19 +12,13 @@ The :func:`main` function is essentially a wrapper around the
"""
from __future__ import print_function
import os.path as op
import sys
import warnings
import logging
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.utils.imcp as imcp
import fsl.data.image as fslimage
import fsl.utils.imcp as imcp
import fsl.data.image as fslimage
usage = """Usage:
......@@ -59,6 +53,11 @@ def main(argv=None):
print(usage)
return 1
# When converting to NIFTI2, nibabel
# emits an annoying message via log.warning:
# sizeof_hdr should be 540; set sizeof_hdr to 540
logging.getLogger('nibabel').setLevel(logging.ERROR)
try:
srcs = [fslimage.fixExt(s) for s in srcs]
srcs = fslpath.removeDuplicates(
......
......@@ -9,17 +9,11 @@ NIFTI/ANALYZE image files.
"""
from __future__ import print_function
import itertools as it
import glob
import sys
import warnings
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.data.image as fslimage
usage = """
Usage: imglob [-extension/extensions] <list of names>
......@@ -27,8 +21,17 @@ Usage: imglob [-extension/extensions] <list of names>
-extensions for image list with full extensions
""".strip()
exts = fslimage.ALLOWED_EXTENSIONS
groups = fslimage.FILE_GROUPS
# The lists below are defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii', '.img', '.hdr', '.img.gz', '.hdr.gz']
"""List of supported image file extensions. """
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
def imglob(paths, output=None):
......@@ -61,12 +64,38 @@ def imglob(paths, output=None):
imgfiles = []
# Expand any wildcard paths if provided.
# Depending on the way that imglob is
# invoked, this may not get done by the
# calling shell.
#
# We also have to handle incomplete
# wildcards, e.g. if the user provides
# "img_??", we need to add possible
# file suffixes before it can be
# expanded.
expanded = []
for path in paths:
if any(c in path for c in '*?[]'):
if fslpath.hasExt(path, exts):
globs = [path]
else:
globs = [f'{path}{ext}' for ext in exts]
globs = [glob.glob(g) for g in globs]
expanded.extend(it.chain(*globs))
else:
expanded.append(path)
paths = expanded
# Build a list of all image files (both
# hdr and img and otherwise) that match
for path in paths:
try:
path = fslimage.removeExt(path)
imgfiles.extend(fslimage.addExt(path, unambiguous=False))
path = fslpath.removeExt(path, allowedExts=exts)
imgfiles.extend(fslpath.addExt(path,
allowedExts=exts,
unambiguous=False))
except fslpath.PathError:
continue
......
#!/usr/bin/env python
#
# imln.py - Create symbolic links to image files.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module defines the ``imln`` application, for creating sym-links
to NIFTI image files.
.. note:: When creating links to relative paths, ln requires that the path is
relative to the link location, rather than the invocation
location. This is *not* currently supported by imln, and possibly
never will be.
"""
import os.path as op
import os
import sys
import fsl.utils.path as fslpath
# The lists below are defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are supported by ``imtest``.
"""
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
usage = """
Usage: imln <file1> <file2>
Makes a link (called file2) to file1
NB: filenames can be basenames or include an extension
""".strip()
def main(argv=None):
"""``imln`` - create sym-links to images. """
if argv is None:
argv = sys.argv[1:]
if len(argv) != 2:
print(usage)
return 1
target, linkbase = argv
target = fslpath.removeExt(target, exts)
linkbase = fslpath.removeExt(linkbase, exts)
# Target must exist, so we can
# infer the correct extension(s).
# Error on incomplete file groups
# (e.g. a.img without a.hdr).
try:
targets = fslpath.getFileGroup(target,
allowedExts=exts,
fileGroups=groups,
unambiguous=True)
except Exception as e:
print(f'Error: {e}')
return 1
for target in targets:
if not op.exists(target):
continue
ext = fslpath.getExt(target, exts)
link = f'{linkbase}{ext}'
try:
# emulate old imln behaviour - if
# link already exists, it is removed
if op.exists(link):
os.remove(link)
os.symlink(target, link)
except Exception as e:
print(f'Error: {e}')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())
......@@ -13,19 +13,13 @@ The :func:`main` function is essentially a wrapper around the
"""
from __future__ import print_function
import os.path as op
import sys
import warnings
import logging
import fsl.utils.path as fslpath
# See atlasq.py for explanation
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
import fsl.utils.imcp as imcp
import fsl.data.image as fslimage
import fsl.utils.imcp as imcp
import fsl.data.image as fslimage
usage = """Usage:
......@@ -60,6 +54,11 @@ def main(argv=None):
print(usage)
return 1
# When converting to NIFTI2, nibabel
# emits an annoying message via log.warning:
# sizeof_hdr should be 540; set sizeof_hdr to 540
logging.getLogger('nibabel').setLevel(logging.ERROR)
try:
srcs = [fslimage.fixExt(s) for s in srcs]
srcs = fslpath.removeDuplicates(
......
#!/usr/bin/env python
#
# imrm.py - Remove image files.
#
# Author: Paul McCarthy <paulmc@fmrib.ox.ac.uk>
#
"""This module defines the ``imrm`` application, for removing NIFTI image
files.
"""
import os.path as op
import os
import sys
import fsl.scripts.imglob as imglob
usage = """Usage: imrm <list of image names to remove>
NB: filenames can be basenames or not
""".strip()
def main(argv=None):
"""Removes all images which are specified on the command line. """
if argv is None:
argv = sys.argv[1:]
if len(argv) < 1:
print(usage)
return 1
paths = imglob.imglob(argv, 'all')
for path in paths:
if op.exists(path):
os.remove(path)
return 0
if __name__ == '__main__':
sys.exit(main())
#!/usr/bin/env python
#
# imtest.py - Test whether an image file exists or not.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""The ``imtest`` script can be used to test whether an image file exists or
not, without having to know the file suffix (.nii, .nii.gz, etc).
"""
import os.path as op
import sys
import fsl.utils.path as fslpath
# The lists below are defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are supported by ``imtest``.
"""
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
def imtest(path):
"""Returns ``True`` if the given image path exists, False otherwise. """
path = fslpath.removeExt(path, exts)
path = op.realpath(path)
# getFileGroup will raise an error
# if the image (including all
# components - i.e. header and
# image) does not exist
try:
fslpath.getFileGroup(path,
allowedExts=exts,
fileGroups=groups,
unambiguous=True)
return True
except fslpath.PathError:
return False
def main(argv=None):
"""Test if an image path exists, and prints ``'1'`` if it does or ``'0'``
if it doesn't.
"""
if argv is None:
argv = sys.argv[1:]
# emulate old fslio/imtest - always return 0
if len(argv) != 1:
print('0')
return 0
if imtest(argv[0]):
print('1')
else:
print('0')
return 0
if __name__ == '__main__':
sys.exit(main())
#!/usr/bin/env python
#
# remove_ext.py - Remove file extensions from NIFTI image paths
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import sys
import fsl.utils.path as fslpath
usage = """Usage: remove_ext <list of image paths to remove extension from>
""".strip()
# This list is defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
'.img', '.hdr',
'.img.gz', '.hdr.gz',
'.mnc', '.mnc.gz']
"""List of file extensions that are removed by ``remove_ext``. """
def main(argv=None):
"""Removes file extensions from all paths which are specified on the
command line.
"""
if argv is None:
argv = sys.argv[1:]
if len(argv) < 1:
print(usage)
return 1
removed = []
for path in argv:
removed.append(fslpath.removeExt(path, exts))
print(' '.join(removed))
return 0
if __name__ == '__main__':
sys.exit(main())
......@@ -10,6 +10,7 @@
import os
import sys
import glob
import hashlib
import shutil
import fnmatch
import logging
......@@ -20,14 +21,12 @@ import os.path as op
import numpy as np
import nibabel as nib
from six import StringIO
from io import StringIO
try: from unittest import mock
except ImportError: import mock
from unittest import mock
import fsl.data.image as fslimage
from fsl.utils.tempdir import tempdir
from fsl.utils.tempdir import tempdir
from fsl.utils.platform import platform as fslplatform
......@@ -50,7 +49,10 @@ def mockFSLDIR(**kwargs):
if not op.isdir(subdir):
os.makedirs(subdir)
for fname in files:
touch(op.join(subdir, fname))
fname = op.join(subdir, fname)
touch(fname)
if subdir == bindir:
os.chmod(fname, 0o755)
fslplatform.fsldir = fsldir
fslplatform.fsldevdir = None
......@@ -68,7 +70,7 @@ def touch(fname):
pass
class CaptureStdout(object):
class CaptureStdout:
"""Context manager which captures stdout and stderr. """
def __init__(self):
......@@ -87,6 +89,7 @@ class CaptureStdout(object):
sys.stdout = self.__mock_stdout
sys.stderr = self.__mock_stderr
return self
def __exit__(self, *args, **kwargs):
......@@ -145,6 +148,8 @@ def testdir(contents=None, suffix=""):
shutil.rmtree(self.testdir)
return ctx(contents)
testdir.__test__ = False
def make_dummy_files(paths):
"""Creates dummy files for all of the given paths. """
......@@ -284,7 +289,8 @@ def make_mock_feat_analysis(featdir,
copes=True,
zstats=True,
residuals=True,
clustMasks=True):
clustMasks=True,
zfstats=True):
if xform is None:
xform = np.eye(4)
......@@ -317,6 +323,7 @@ def make_mock_feat_analysis(featdir,
data = np.ravel_multi_index(data, shape)
data = data.reshape(list(shape) + [1]).repeat(timepoints, axis=3)
data[..., :] += range(i, i + timepoints)
data = data.astype(np.int32)
img = nib.nifti1.Nifti1Image(data, xform)
......@@ -341,6 +348,11 @@ def make_mock_feat_analysis(featdir,
otherFiles .extend(files)
otherShapes.extend([shape] * len(files))
if zfstats:
files = glob.glob(op.join(featdir, 'stats', 'zfstat*nii.gz'))
otherFiles .extend(files)
otherShapes.extend([shape] * len(files))
if residuals:
files = glob.glob(op.join(featdir, 'stats', 'res4d.nii.gz'))
otherFiles .extend(files)
......@@ -428,3 +440,10 @@ def make_random_mask(filename, shape, xform, premask=None, minones=1):
img.save(filename)
return img
def sha256(filename):
hashobj = hashlib.sha256()
with open(filename, 'rb') as f:
hashobj.update(f.read())
return hashobj.hexdigest()
......@@ -14,8 +14,8 @@ import pytest
import fsl.utils.assertions as assertions
import fsl.utils.tempdir as tempdir
from . import make_random_image
from . import testdir
from fsl.tests import make_random_image
from fsl.tests import testdir
def test_assertFileExists():
......@@ -160,14 +160,14 @@ def test_assertIsMelodicDir():
('analysis.ica', [ 'melodic_mix', 'melodic_FTmix'], False),
('analysis.ica', ['melodic_IC.nii.gz', 'melodic_FTmix'], False),
('analysis.ica', ['melodic_IC.nii.gz', 'melodic_mix'], False),
('analysis', ['melodic_IC.nii.gz', 'melodic_mix', 'melodic_FTmix'], False),
('analysis', ['melodic_oIC.nii.gz', 'melodic_mix', 'melodic_FTmix'], False),
('analysis', ['melodic_IC.nii.gz', 'melodic_mix', 'melodic_FTmix'], True),
('analysis', [ 'melodic_mix', 'melodic_FTmix'], False),
]
for dirname, paths, expected in tests:
with testdir(paths, dirname):
if expected:
assertions.assertIsMelodicDir(dirname)
assertions.assertIsMelodicDir('.')
else:
with pytest.raises(AssertionError):
assertions.assertIsMelodicDir(dirname)
......
......@@ -13,14 +13,15 @@ import os
import os.path as op
import numpy as np
import mock
from unittest import mock
import pytest
import tests
import fsl.tests as tests
import fsl.utils.image.resample as resample
import fsl.data.atlases as atlases
import fsl.data.image as fslimage
import fsl.transform as transform
import fsl.transform.affine as affine
datadir = op.join(op.dirname(__file__), 'testdata')
......@@ -40,7 +41,8 @@ dummy_atlas_desc = """<?xml version="1.0" encoding="ISO-8859-1"?>
<header>
<name>{name}</name>
<shortname>{shortname}</shortname>
<type>Label</type>
<type>{atlastype}</type>
{extraheader}
<images>
<imagefile>/{shortname}/{filename}</imagefile>
<summaryimagefile>/{shortname}/My{filename}</summaryimagefile>
......@@ -52,7 +54,8 @@ dummy_atlas_desc = """<?xml version="1.0" encoding="ISO-8859-1"?>
</data>
</atlas>
"""
def _make_dummy_atlas(savedir, name, shortName, filename):
def _make_dummy_atlas(
savedir, name, shortName, filename, atlastype='Label', extraheader=''):
mladir = op.join(savedir, shortName)
mlaxmlfile = op.join(savedir, '{}.xml'.format(shortName))
mlaimgfile = op.join(savedir, shortName, '{}.nii.gz'.format(filename))
......@@ -70,7 +73,9 @@ def _make_dummy_atlas(savedir, name, shortName, filename):
desc = dummy_atlas_desc.format(
name=name,
shortname=shortName,
filename=filename)
filename=filename,
atlastype=atlastype,
extraheader=extraheader)
f.write(desc)
return mlaxmlfile
......@@ -142,6 +147,28 @@ def test_AtlasDescription():
registry.getAtlasDescription('non-existent-atlas')
def test_StatisticHeader():
with tests.testdir() as testdir:
hdr = '<statistic>T</statistic>' \
'<units></units>' \
'<precision>3</precision>' \
'<upper>75</upper>'
xmlfile = _make_dummy_atlas(testdir,
'statlas',
'STA',
'StAtlas',
atlastype='Statistic',
extraheader=hdr)
desc = atlases.AtlasDescription(xmlfile, 'StAtlas')
assert desc.atlasType == 'statistic'
assert desc.statistic == 'T'
assert desc.units == ''
assert desc.precision == 3
assert desc.lower == 0
assert desc.upper == 75
def test_add_remove_atlas():
with tests.testdir() as testdir:
......@@ -225,8 +252,7 @@ def test_load_atlas():
reg = atlases.registry
reg.rescanAtlases()
probatlas = reg.loadAtlas('harvardoxford-cortical',
calcRange=False, loadData=False)
probatlas = reg.loadAtlas('harvardoxford-cortical')
probsumatlas = reg.loadAtlas('harvardoxford-cortical', loadSummary=True)
lblatlas = reg.loadAtlas('talairach')
......@@ -250,6 +276,9 @@ def test_get():
assert (target == atlas.get(index=label.index).data).all()
assert (target == atlas.get(value=label.value).data).all()
assert (target == atlas.get(name=label.name).data).all()
if atlas is lblatlas:
target = target * label.value
assert (target == atlas.get(value=label.value, binary=False).data).all()
def test_find():
......@@ -257,8 +286,7 @@ def test_find():
reg = atlases.registry
reg.rescanAtlases()
probatlas = reg.loadAtlas('harvardoxford-cortical',
calcRange=False, loadData=False)
probatlas = reg.loadAtlas('harvardoxford-cortical')
probsumatlas = reg.loadAtlas('harvardoxford-cortical', loadSummary=True)
lblatlas = reg.loadAtlas('talairach')
......@@ -305,8 +333,7 @@ def test_prepareMask():
reg = atlases.registry
reg.rescanAtlases()
probatlas = reg.loadAtlas('harvardoxford-cortical',
loadData=False, calcRange=False)
probatlas = reg.loadAtlas('harvardoxford-cortical')
probsumatlas = reg.loadAtlas('harvardoxford-cortical', loadSummary=True)
lblatlas = reg.loadAtlas('talairach')
......@@ -326,7 +353,7 @@ def test_prepareMask():
np.random.random(list(ashape) + [2]))
wrongspace = fslimage.Image(
np.random.random((20, 20, 20)),
xform=transform.concat(atlas.voxToWorldMat, np.diag([2, 2, 2, 1])))
xform=affine.concat(atlas.voxToWorldMat, np.diag([2, 2, 2, 1])))
with pytest.raises(atlases.MaskError):
atlas.prepareMask(wrongdims)
......
......@@ -13,11 +13,11 @@ import pytest
import fsl.data.atlases as fslatlases
import fsl.data.image as fslimage
import fsl.transform as transform
import fsl.transform.affine as affine
import fsl.utils.image.resample as resample
import fsl.utils.cache as cache
from . import (testdir, make_random_mask)
from fsl.tests import (testdir, make_random_mask)
pytestmark = pytest.mark.fsltest
......@@ -41,17 +41,16 @@ _atlases = cache.Cache()
def _get_atlas(atlasID, res, summary=False):
atlas = _atlases.get((atlasID, res, summary), default=None)
if atlas is None:
atlas = fslatlases.loadAtlas(atlasID,
loadSummary=summary,
resolution=res)
# We need some atlases to be loaded into memory,
# so we can use boolean-mask-based indexing
if summary or atlasID in ('talairach', 'striatum-structural',
'jhu-labels', 'smatt'):
kwargs = {}
else:
kwargs = {'loadData' : False,
'calcRange' : False}
atlas.data
atlas = fslatlases.loadAtlas(atlasID,
loadSummary=summary,
resolution=res,
**kwargs)
_atlases.put((atlasID, res, summary), atlas)
return atlas
......@@ -85,7 +84,7 @@ def _get_zero_mask(aimg):
elif isinstance(aimg, fslatlases.ProbabilisticAtlas):
# Keep memory usage down
zmask = np.ones(aimg.shape[:3], dtype=np.bool)
zmask = np.ones(aimg.shape[:3], dtype=bool)
for vol in range(aimg.shape[-1]):
zmask = np.logical_and(zmask, aimg[..., vol] == 0)
......@@ -155,7 +154,7 @@ def _gen_coord_voxel_query(atlas, qtype, qin, **kwargs):
dlo = (0, 0, 0)
dhi = atlas.shape
else:
dlo, dhi = transform.axisBounds(atlas.shape, atlas.voxToWorldMat)
dlo, dhi = affine.axisBounds(atlas.shape, atlas.voxToWorldMat)
dlen = [hi - lo for lo, hi in zip(dlo, dhi)]
......@@ -190,7 +189,7 @@ def _gen_coord_voxel_query(atlas, qtype, qin, **kwargs):
coords = np.array(coords, dtype=dtype)
if not voxel:
coords = transform.transform(coords, atlas.voxToWorldMat)
coords = affine.transform(coords, atlas.voxToWorldMat)
return tuple([dtype(c) for c in coords])
......@@ -200,7 +199,7 @@ def _eval_coord_voxel_query(atlas, query, qtype, qin):
voxel = qtype == 'voxel'
if voxel: vx, vy, vz = query
else: vx, vy, vz = transform.transform(query, atlas.worldToVoxMat)
else: vx, vy, vz = affine.transform(query, atlas.worldToVoxMat)
vx, vy, vz = [int(round(v)) for v in [vx, vy, vz]]
......@@ -218,8 +217,8 @@ def _eval_coord_voxel_query(atlas, query, qtype, qin):
elif qin == 'out':
expval = []
assert atlas.proportions( query, voxel=voxel) == expval
assert atlas.coordProportions(query, voxel=voxel) == expval
assert atlas.values( query, voxel=voxel) == expval
assert atlas.coordValues(query, voxel=voxel) == expval
if isinstance(atlas, fslatlases.LabelAtlas): evalLabel()
elif isinstance(atlas, fslatlases.ProbabilisticAtlas): evalProb()
......@@ -284,7 +283,7 @@ def _eval_mask_query(atlas, query, qtype, qin):
rmask = resample.resample(
mask, atlas.shape[:3], dtype=np.float32, order=0)[0]
rmask = np.array(rmask, dtype=np.bool)
rmask = np.array(rmask, dtype=bool)
def evalLabel():
......@@ -343,13 +342,13 @@ def _eval_mask_query(atlas, query, qtype, qin):
if qin == 'out':
with pytest.raises(fslatlases.MaskError):
atlas.maskProportions(mask)
atlas.maskValues(mask)
with pytest.raises(fslatlases.MaskError):
atlas.proportions( mask)
atlas.values( mask)
return
props = atlas. proportions(mask)
props2 = atlas.maskProportions(mask)
props = atlas. values(mask)
props2 = atlas.maskValues(mask)
assert np.all(np.isclose(props, props2))
......
#!/usr/bin/env python
#
# test_bids.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
import json
import os.path as op
import itertools as it
from pathlib import Path
import pytest
from fsl.utils.tempdir import tempdir
import fsl.utils.bids as fslbids
def test_parseFilename():
badtests = ['bad_file.txt']
for test in badtests:
with pytest.raises(ValueError):
fslbids.parseFilename(test)
tests = [
('sub-01_ses-01_t1w.nii.gz',
({'sub' : '01', 'ses' : '01'}, 't1w')),
('a-1_b-2_c-3_d-4_e.json',
({'a' : '1', 'b' : '2', 'c' : '3', 'd' : '4'}, 'e')),
]
for filename, expect in tests:
assert fslbids.parseFilename(filename) == expect
def test_isBIDSDir():
with tempdir():
assert not fslbids.isBIDSDir('.')
with tempdir():
Path('dataset_description.json').touch()
assert fslbids.isBIDSDir('.')
def test_inBIDSDir():
with tempdir():
Path('a/b/c').mkdir(parents=True)
Path('dataset_description.json').touch()
assert fslbids.inBIDSDir(Path('.'))
assert fslbids.inBIDSDir(Path('a'))
assert fslbids.inBIDSDir(Path('a/b'))
assert fslbids.inBIDSDir(Path('a/b/c'))
with tempdir():
Path('a/b/c').mkdir(parents=True)
assert not fslbids.inBIDSDir(Path('.'))
assert not fslbids.inBIDSDir(Path('a'))
assert not fslbids.inBIDSDir(Path('a/b'))
assert not fslbids.inBIDSDir(Path('a/b/c'))
def test_isBIDSFile():
goodfiles = [
Path('sub-01_ses-01_t1w.nii.gz'),
Path('sub-01_ses-01_t1w.nii'),
Path('sub-01_ses-01_t1w.json'),
Path('a-1_b-2_c-3_d-4_e.nii.gz'),
Path('sub-01_ses-01_t1w.txt'),
]
badfiles = [
Path('sub-01_ses-01.nii.gz'),
Path('sub-01_ses-01_t1w'),
Path('sub-01_ses-01_t1w.'),
Path('sub_ses-01_t1w.nii.gz'),
Path('sub-01_ses_t1w.nii.gz'),
]
with tempdir():
Path('dataset_description.json').touch()
for f in goodfiles: assert fslbids.isBIDSFile(f)
for f in badfiles: assert not fslbids.isBIDSFile(f)
with tempdir():
for f in it.chain(goodfiles, badfiles):
assert not fslbids.isBIDSFile(f)
def test_loadMetadata():
dd = Path('dataset_description.json')
t1 = Path('sub-01/func/sub-01_task-stim_bold.nii.gz')
json1 = Path('sub-01/func/sub-01_task-stim_bold.json')
json2 = Path('sub-01/sub-01_bold.json')
json3 = Path('sub-01_t1w.json')
json4 = Path('sub-01/task-stim_bold.json')
meta1 = {'a' : '1', 'b' : '2'}
meta2 = {'a' : '10', 'c' : '3'}
meta3 = {'a' : '109', 'b' : '99'}
meta4 = {'c' : '9', 'd' : '5'}
with tempdir():
dd.touch()
Path(op.dirname(t1)).mkdir(parents=True)
t1.touch()
assert fslbids.loadMetadata(t1) == {}
json1.write_text(json.dumps(meta1))
assert fslbids.loadMetadata(t1) == meta1
json2.write_text(json.dumps(meta2))
assert fslbids.loadMetadata(t1) == {**meta2, **meta1}
json3.write_text(json.dumps(meta3))
assert fslbids.loadMetadata(t1) == {**meta2, **meta1}
json4.write_text(json.dumps(meta4))
assert fslbids.loadMetadata(t1) == {**meta4, **meta2, **meta1}
def test_loadMetadata_control_characters():
dd = Path('dataset_description.json')
t1 = Path('sub-01/func/sub-01_task-stim_bold.nii.gz')
json1 = Path('sub-01/func/sub-01_task-stim_bold.json')
meta1 = {"a" : "1", "b" : "2\x19\x20"}
smeta1 = '{"a" : "1", "b" : "2\x19\x20"}'
with tempdir():
dd.touch()
Path(op.dirname(t1)).mkdir(parents=True)
t1.touch()
assert fslbids.loadMetadata(t1) == {}
json1.write_text(smeta1)
assert fslbids.loadMetadata(t1) == meta1
def test_loadMetadata_symlinked():
ddreal = Path('a')
t1real = Path('b')
j1real = Path('c')
j2real = Path('d')
j3real = Path('e')
j4real = Path('f')
dd = Path('data/dataset_description.json')
t1 = Path('data/sub-01/func/sub-01_task-stim_bold.nii.gz')
json1 = Path('data/sub-01/func/sub-01_task-stim_bold.json')
json2 = Path('data/sub-01/sub-01_bold.json')
json3 = Path('data/sub-01_t1w.json')
json4 = Path('data/sub-01/task-stim_bold.json')
meta1 = {'a' : '1', 'b' : '2'}
meta2 = {'a' : '10', 'c' : '3'}
meta3 = {'a' : '109', 'b' : '99'}
meta4 = {'c' : '9', 'd' : '5'}
with tempdir():
ddreal.touch()
t1real.touch()
j1real.write_text(json.dumps(meta1))
j2real.write_text(json.dumps(meta2))
j3real.write_text(json.dumps(meta3))
j4real.write_text(json.dumps(meta4))
Path(op.dirname(t1)).mkdir(parents=True)
dd .symlink_to(op.join('..', ddreal))
t1 .symlink_to(op.join('..', '..', '..', t1real))
json1.symlink_to(op.join('..', '..', '..', j1real))
json2.symlink_to(op.join('..', '..', j2real))
json3.symlink_to(op.join('..', j3real))
json4.symlink_to(op.join('..', '..', j4real))
assert fslbids.loadMetadata(t1) == {**meta4, **meta2, **meta1}
File moved
......@@ -113,7 +113,55 @@ def test_expiry():
with pytest.raises(cache.Expired):
c.get(0)
with pytest.raises(cache.Expired):
c.get(1)
assert c.get(1, default='default') == 'default'
# And that the cache is empty
assert len(c) == 0
def test_lru():
c = cache.Cache(maxsize=3, lru=True)
c[0] = '0'
c[1] = '1'
c[2] = '2'
c[3] = '3'
# normal behaviour - first inserted
# is dropped
with pytest.raises(KeyError):
assert c.get(0)
# lru behaviour - oldest accessed is
# dropped
c[1]
c[4] = '4'
with pytest.raises(KeyError):
c[2]
c[1]
c[3]
c[4]
assert len(c) == 3
def test_accessors():
c = cache.Cache(maxsize=3)
c[0] = '0'
c[1] = '1'
c[2] = '2'
c[3] = '3'
assert list(c.keys()) == [ 1, 2, 3]
assert list(c.values()) == ['1', '2', '3']
assert list(c.items()) == [(1, '1'), (2, '2'), (3, '3')]
assert 0 not in c
assert 1 in c
assert 2 in c
assert 3 in c