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

Cleaned up fnirt module, little changes to bet/flirt

parent e2bc373b
No related branches found
No related tags found
No related merge requests found
...@@ -16,10 +16,8 @@ from . import wrapperutils as wutils ...@@ -16,10 +16,8 @@ from . import wrapperutils as wutils
@wutils.fileOrImage('input', 'output') @wutils.fileOrImage('input', 'output')
def bet(input, output, **kwargs): def bet(input, output, **kwargs):
"""Delete non-brain tissue from an image of the whole head. """Wrapper for the ``bet`` command.
:arg input: Image to be brain-extracted
:arg output: Output image
:arg mask: Generate a brain mask :arg mask: Generate a brain mask
:arg seg: If ``False``, a brain extracted image is not :arg seg: If ``False``, a brain extracted image is not
generated. generated.
...@@ -45,7 +43,7 @@ def bet(input, output, **kwargs): ...@@ -45,7 +43,7 @@ def bet(input, output, **kwargs):
} }
cmd = ['bet', input, output] cmd = ['bet', input, output]
cmd += wutils.applyArgStyle('-', argmap, valmap, **kwargs) cmd += wutils.applyArgStyle('-', argmap=argmap, valmap=valmap, **kwargs)
return run.runfsl(*cmd) return run.runfsl(*cmd)
......
...@@ -28,7 +28,7 @@ from . import wrapperutils as wutils ...@@ -28,7 +28,7 @@ from . import wrapperutils as wutils
@wutils.fileOrImage('src', 'ref', 'out', 'wmseg', 'fieldmap', 'fieldmapmask') @wutils.fileOrImage('src', 'ref', 'out', 'wmseg', 'fieldmap', 'fieldmapmask')
@wutils.fileOrArray('init', 'omat', 'wmcoords', 'wmnorms') @wutils.fileOrArray('init', 'omat', 'wmcoords', 'wmnorms')
def flirt(src, ref, **kwargs): def flirt(src, ref, **kwargs):
"""Wrapper around the ``flirt`` command. """ """Wrapper for the ``flirt`` command. """
asrt.assertIsNifti(src, ref) asrt.assertIsNifti(src, ref)
...@@ -43,8 +43,6 @@ def flirt(src, ref, **kwargs): ...@@ -43,8 +43,6 @@ def flirt(src, ref, **kwargs):
'noresampblur' : wutils.SHOW_IF_TRUE, 'noresampblur' : wutils.SHOW_IF_TRUE,
'2D' : wutils.SHOW_IF_TRUE, '2D' : wutils.SHOW_IF_TRUE,
'v' : wutils.SHOW_IF_TRUE, 'v' : wutils.SHOW_IF_TRUE,
'version' : wutils.SHOW_IF_TRUE,
'help' : wutils.SHOW_IF_TRUE,
} }
cmd = ['flirt', '-in', src, '-ref', ref] cmd = ['flirt', '-in', src, '-ref', ref]
...@@ -93,7 +91,7 @@ def concatxfm(inmat1, inmat2, outmat): ...@@ -93,7 +91,7 @@ def concatxfm(inmat1, inmat2, outmat):
@wutils.fileOrImage('infile', 'out', 'reffile') @wutils.fileOrImage('infile', 'out', 'reffile')
@wutils.fileOrArray('init') @wutils.fileOrArray('init')
def mcflirt(infile, **kwargs): def mcflirt(infile, **kwargs):
"""Wrapper around the ``mcflirt`` command.""" """Wrapper for the ``mcflirt`` command."""
asrt.assertIsNifti(infile) asrt.assertIsNifti(infile)
......
...@@ -19,182 +19,90 @@ non-linear registration of 3D images. ...@@ -19,182 +19,90 @@ non-linear registration of 3D images.
""" """
import os.path as op
import glob
import fsl.utils.run as run import fsl.utils.run as run
import fsl.utils.assertions as asrt import fsl.utils.assertions as asrt
from . import wrapperutils as wutils
@wutils.fileOrImage('src', 'ref', 'inwarp', 'cout', 'iout', 'fout', 'jout',
'refout', 'refmask', 'inmask')
@wutils.fileOrArray('aff')
def fnirt(src, ref, **kwargs):
"""Wrapper for the ``fnirt`` command."""
def fnirt(src, ref, aff=None, imprefm=None, impinm=None, applyrefmask=None, asrt.assertIsNifti(src, ref)
applyinmask=None, subsamp=None, miter=None, infwhm=None,
reffwhm=None, lmbda=None, estint=None, warpres=None, ssqlambda=None, cmd = ['fnirt', '--in={}'.format(src), '--ref={}'.format(ref)]
regmod=None, intmod=None, intorder=None, biasres=None, cmd += wutils.applyArgStyle('--=', **kwargs)
biaslambda=None, refderiv=None, cout=None, intout=None, refout=None,
iout=None, interp=None, inwarp=None, minmet=None, verbose=False,
intin=None, jout=None):
"""Do nonlinear image registration."""
cmd = 'fnirt --in={0} --ref={1}'.format(src, ref)
if aff is not None:
cmd += " --aff={0}".format(aff)
if imprefm is not None:
cmd += " --imprefm={0}".format(imprefm)
if impinm is not None:
cmd += " --impinm={0}".format(impinm)
if applyrefmask is not None:
cmd += " --applyrefmask={0}".format(applyrefmask)
if applyinmask is not None:
cmd += " --applyinmask={0}".format(applyinmask)
if subsamp is not None:
cmd += " --subsamp={0}".format(subsamp)
if miter is not None:
cmd += " --miter={0}".format(miter)
if infwhm is not None:
cmd += " --infwhm={0}".format(infwhm)
if reffwhm is not None:
cmd += " --reffwhm={0}".format(reffwhm)
if lmbda is not None:
cmd += " --lambda={0}".format(lmbda)
if estint is not None:
cmd += " --estint={0}".format(estint)
if warpres is not None:
cmd += " --warpres={0}".format(warpres)
if ssqlambda is not None:
cmd += " --ssqlambda={0}".format(ssqlambda)
if regmod is not None:
cmd += " --regmod={0}".format(regmod)
if intmod is not None:
cmd += " --intmod={0}".format(intmod)
if intorder is not None:
cmd += " --intorder={0}".format(intorder)
if biasres is not None:
cmd += " --biasres={0}".format(biasres)
if biaslambda is not None:
cmd += " --biaslambda={0}".format(biaslambda)
if refderiv is not None:
cmd += " --refderiv={0}".format(refderiv)
if cout is not None:
cmd += " --cout={0}".format(cout)
if intout is not None:
cmd += " --intout={0}".format(intout)
if refout is not None:
cmd += " --refout={0}".format(refout)
if iout is not None:
cmd += " --iout={0}".format(iout)
if interp is not None:
cmd += " --interp={0}".format(interp)
if inwarp is not None:
cmd += " --inwarp={0}".format(inwarp)
if minmet is not None:
cmd += " --minmet={0}".format(minmet)
if intin is not None:
cmd += " --intin={0}".format(intin)
if jout is not None:
cmd += " --jout={0}".format(jout)
if verbose:
cmd += " --verbose"
return run.runfsl(cmd) return run.runfsl(cmd)
def applywarp(src, ref, out, warp=None, premat=None, prematdir=None, @wutils.fileOrImage('src', 'ref', 'out', 'warp', 'mask')
postmat=None, postmatdir=None, interp="spline", @wutils.fileOrArray('premat', 'postmat')
paddingsize=None, abs=False, rel=False): def applywarp(src, ref, out, warp, **kwargs):
"""Tool for applying FSL warps. """Wrapper for the ``applywarp`` command. """
The ``prematdir`` and ``postmatdir`` arguments can be used when warping a valmap = {
4D image. You can specify a directory containing multiple affines, named 'abs' : wutils.SHOW_IF_TRUE,
``MAT_????`` (as output by e.g. ``mcflirt``). Each file will be applied to 'rel' : wutils.SHOW_IF_TRUE,
one volume (in order) of the image. 'super' : wutils.SHOW_IF_TRUE,
""" 'debug' : wutils.SHOW_IF_TRUE,
assert (warp or premat or postmat or prematdir or postmatdir), \ 'verbose' : wutils.SHOW_IF_TRUE,
"either a warp or mat (premat, postmat or prematdir) must be supplied" }
assert not (premat and prematdir), \
"cannot use premat and prematdir arguments together" cmd = ['applywarp',
assert not (postmat and postmatdir), \ '--in={}'.format(src),
"cannot use postmat and postmatdir arguments together" '--ref={}'.format(ref),
'--out={}'.format(out),
def catmats(matdir, out): '--warp={}'.format(warp)]
"""Concatenate FSL trasformations files into a single file."""
mats = sorted(glob.glob(op.join(matdir, "MAT_????"))) cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs)
with open(out, 'w') as outfile:
for fname in mats:
with open(fname) as infile:
outfile.write(infile.read())
cmd = "--in={0} --ref={1} --out={2} --interp={3}".format(src, ref, out,
interp)
cmd = "applywarp " + cmd
if prematdir:
premat = op.join(prematdir, 'allmats.txt')
catmats(prematdir, premat)
if postmatdir:
postmat = op.join(postmatdir, 'allmats.txt')
catmats(postmatdir, postmat)
if warp:
cmd += " --warp={0}".format(warp)
if premat:
cmd += " --premat={0}".format(premat)
if postmat:
cmd += " --postmat={0}".format(postmat)
if paddingsize:
cmd += " --paddingsize={0}".format(paddingsize)
if abs:
cmd += " --abs"
if rel:
cmd += " --rel"
return run.runfsl(cmd) return run.runfsl(cmd)
def invwarp(inwarp, ref, outwarp): @wutils.fileOrImage('warp', 'ref', 'out')
"""Tool for inverting FSL warps.""" def invwarp(warp, ref, out, **kwargs):
"""Wrapper for the ``invwarp`` command."""
valmap = {
'abs' : wutils.SHOW_IF_TRUE,
'rel' : wutils.SHOW_IF_TRUE,
'noconstraint' : wutils.SHOW_IF_TRUE,
'debug' : wutils.SHOW_IF_TRUE,
'verbose' : wutils.SHOW_IF_TRUE,
}
asrt.assertIsNifti(warp, ref, out)
asrt.assertFileExists(inwarp, ref) cmd = ['invwarp',
asrt.assertIsNifti(inwarp, ref, outwarp) '--warp={}'.format(warp),
'--ref={}'.format(ref),
'--out={}'.format(out)]
cmd = 'invwarp' cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs)
cmd += ' --warp={}'.format(inwarp)
cmd += ' --out={}'.format(outwarp)
cmd += ' --ref={}'.format(ref)
return run.runfsl(cmd) return run.runfsl(cmd)
def convertwarp(out, ref, warp1=None, warp2=None, premat=None, midmat=None, @wutils.fileOrImage('out', 'ref', 'warp1', 'warp2', 'shiftmap')
postmat=None, shiftmap=None, shiftdir=None, absout=False, @wutils.fileOrArray('premat', 'midmat', 'postmat')
abs=False, rel=False, relout=False): def convertwarp(out, ref, **kwargs):
"""Tool for converting FSL warps.""" """Wrapper for the ``convertwarp`` command."""
assert (warp1 or warp2 or premat or midmat or postmat), \ valmap = {
"either a warp (warp1 or warp2) or mat (premat, midmat, or " + \ 'abs' : wutils.SHOW_IF_TRUE,
"postmat) must be supplied" 'rel' : wutils.SHOW_IF_TRUE,
'absout' : wutils.SHOW_IF_TRUE,
cmd = "convertwarp --ref={0} --out={1}".format(ref, out) 'relout' : wutils.SHOW_IF_TRUE,
if warp1: 'jacobian' : wutils.SHOW_IF_TRUE,
cmd = cmd + " --warp1={0}".format(warp1) 'jstats' : wutils.SHOW_IF_TRUE,
if warp2: 'constrainj' : wutils.SHOW_IF_TRUE,
cmd = cmd + " --warp2={0}".format(warp2) 'verbose' : wutils.SHOW_IF_TRUE,
if premat: }
cmd = cmd + " --premat={0}".format(premat)
if midmat: cmd = ['convertwarp', '--ref={}'.format(ref), '--out={}'.format(out)]
cmd = cmd + " --midmat={0}".format(midmat) cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs)
if postmat:
cmd = cmd + " --postmat={0}".format(postmat)
if shiftmap:
cmd = cmd + " --shiftmap={0}".format(shiftmap)
if shiftdir:
cmd = cmd + " --shiftdir={0}".format(shiftdir)
if absout:
cmd = cmd + " --absout"
if relout:
cmd = cmd + " --relout"
if abs:
cmd = cmd + " --abs"
if rel:
cmd = cmd + " --rel"
return run.runfsl(cmd) return run.runfsl(cmd)
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