def test_applyArgStyle_argmap_valmap(): argmap = {'a1': 'a', 'a2': 'b'} valmap = { 'a': wutils.SHOW_IF_TRUE, 'b': wutils.HIDE_IF_TRUE, } # kwargs, expected tests = [ ({}, ['']), ({ 'a1': False, }, ['']), ({ 'a1': True, }, ['-a']), ({ 'a2': False }, ['-b']), ({ 'a2': True }, ['']), ({ 'a1': False, 'a2': True }, ['']), ({ 'a1': True, 'a2': True }, ['-a']), ({ 'a1': False, 'a2': False }, ['-b']), ({ 'a1': False, 'a2': True }, ['']), ({ 'a1': True, 'a2': False }, ['-a -b', '-b -a']), ({ 'a1': True, 'a2': True }, ['-a']), ] for kwargs, expected in tests: expected = [shlex.split(e) for e in expected] assert wutils.applyArgStyle('-', argmap=argmap, valmap=valmap, **kwargs) in expected
def test_applyArgStyle_default(): kwargs = { 'arg1': 'val', 'arg2': ['val1', 'val2'], 'a': 'val', 'b': ['val1', 'val2'], } exp = ['--arg1=val', '--arg2=val1,val2', '-a', 'val', '-b', 'val1', 'val2'] assert wutils.applyArgStyle(**kwargs) == exp
def test_applyArgStyle(): kwargs = { 'name': 'val', 'name2': ['val1', 'val2'], 'name3': 'val1 val2', } # these combinations of style+valsep should # raise an error with pytest.raises(ValueError): wutils.applyArgStyle(style='-=', valsep=' ', **kwargs) with pytest.raises(ValueError): wutils.applyArgStyle(style='--=', valsep=' ', **kwargs) # unsupported style/valsep with pytest.raises(ValueError): wutils.applyArgStyle('?', **kwargs) with pytest.raises(ValueError): wutils.applyArgStyle('-', valsep='b', **kwargs) # style, valsep, expected_result. tests = [ ('-', ' ', ['-name', 'val', '-name2', 'val1', 'val2', '-name3', 'val1 val2']), ('-', '"', ['-name', 'val', '-name2', 'val1 val2', '-name3', 'val1 val2']), ('-', ',', ['-name', 'val', '-name2', 'val1,val2', '-name3', 'val1 val2']), ('--', ' ', ['--name', 'val', '--name2', 'val1', 'val2', '--name3', 'val1 val2']), ('--', '"', ['--name', 'val', '--name2', 'val1 val2', '--name3', 'val1 val2']), ('--', ',', ['--name', 'val', '--name2', 'val1,val2', '--name3', 'val1 val2']), ('-=', '"', ['-name=val', '-name2=val1 val2', '-name3=val1 val2']), ('-=', ',', ['-name=val', '-name2=val1,val2', '-name3=val1 val2']), ('--=', '"', ['--name=val', '--name2=val1 val2', '--name3=val1 val2']), ('--=', ',', ['--name=val', '--name2=val1,val2', '--name3=val1 val2']), ] for style, valsep, exp in tests: result = wutils.applyArgStyle(style, valsep=valsep, **kwargs) assert result == exp
def test_applyArgStyle_charstyle(): kwargs = { 'n': 'val', 'm': ['val1', 'val2'], 'o': 'val1 val2', } # these combinations of charstyle+ # charsep should raise an error with pytest.raises(ValueError): wutils.applyArgStyle(charstyle='--=', charsep=' ', **kwargs) with pytest.raises(ValueError): wutils.applyArgStyle(charstyle='-=', charsep=' ', **kwargs) # unsupported chrastyle/charsep with pytest.raises(ValueError): wutils.applyArgStyle(charstyle='?', **kwargs) with pytest.raises(ValueError): wutils.applyArgStyle('-', charsep='b', **kwargs) # style, valsep, charstyle, charsep, expected_result. # Order of arguments is not guaranteed tests = [ ('-', ' ', ['-n', 'val', '-m', 'val1', 'val2', '-o', 'val1 val2']), ('-', '"', ['-n', 'val', '-m', 'val1 val2', '-o', 'val1 val2']), ('-', ',', ['-n', 'val', '-m', 'val1,val2', '-o', 'val1 val2']), ('--', ' ', ['--n', 'val', '--m', 'val1', 'val2', '--o', 'val1 val2']), ('--', '"', ['--n', 'val', '--m', 'val1 val2', '--o', 'val1 val2']), ('--', ',', ['--n', 'val', '--m', 'val1,val2', '--o', 'val1 val2']), ('-=', '"', ['-n=val', '-m=val1 val2', '-o=val1 val2']), ('-=', ',', ['-n=val', '-m=val1,val2', '-o=val1 val2']), ('--=', '"', ['--n=val', '--m=val1 val2', '--o=val1 val2']), ('--=', ',', ['--n=val', '--m=val1,val2', '--o=val1 val2']), ] for style, valsep, exp in tests: result = wutils.applyArgStyle(charstyle=style, charsep=valsep, **kwargs) assert result == exp
def test_applyArgStyle_argmap(): kwargs = { 'name1': 'val1', 'name2': 'val2', } argmap = { 'name1': 'n', 'name2': 'm', } # order not guaranteed exp = [shlex.split('-n val1 -m val2'), shlex.split('-m val2 -n val1')] assert wutils.applyArgStyle('-', argmap=argmap, **kwargs) in exp
def test_applyArgStyle(): kwargs = { 'name': 'val', 'name2': ['val1', 'val2'], } # these combinations of style+valsep should # raise an error with pytest.raises(ValueError): wutils.applyArgStyle(style='-=', valsep=' ', **kwargs) with pytest.raises(ValueError): wutils.applyArgStyle(style='--=', valsep=' ', **kwargs) # unsupported style/valsep with pytest.raises(ValueError): wutils.applyArgStyle('?', **kwargs) with pytest.raises(ValueError): wutils.applyArgStyle('-', valsep='b', **kwargs) # style, valsep, expected_result. # Order of arguments is not guaranteed tests = [ ('-', ' ', [' -name val', '-name2 val1 val2']), ('-', '"', [' -name val', '-name2 "val1 val2"']), ('-', ',', [' -name val', '-name2 val1,val2']), ('--', ' ', ['--name val', '--name2 val1 val2']), ('--', '"', ['--name val', '--name2 "val1 val2"']), ('--', ',', ['--name val', '--name2 val1,val2']), ('-=', '"', [' -name=val', '-name2="val1 val2"']), ('-=', ',', [' -name=val', '-name2=val1,val2']), ('--=', '"', ['--name=val', '--name2="val1 val2"']), ('--=', ',', ['--name=val', '--name2=val1,val2']), ] for style, valsep, exp in tests: exp = [shlex.split(e) for e in exp] result = wutils.applyArgStyle(style, valsep=valsep, **kwargs) assert result in (exp[0] + exp[1], exp[1] + exp[0])
def mvntool(mvn, param, **kwargs): """ Wrapper for MVNTOOL. :param mvn: Input MVN data :param param: Parameter index (or name, but only if param-list is specified) For other arguments, see command line tool for now """ asrt.assertIsNifti(mvn) valmap = { 'write' : wutils.SHOW_IF_TRUE, 'new' : wutils.SHOW_IF_TRUE, } cmd = ['mvntool', '--input={}'.format(mvn), '--param={}'.format(param)] cmd += wutils.applyArgStyle('--=', valmap=valmap, **kwargs) return cmd
def fnirtfileutils(src, **kwargs): """Wrapper for the ``fnirt`` command. Compulsory arguments (You MUST set one or more of): -i,--in filename of input coefficient volume (to be converted) Optional arguments (You may optionally specify one or more of): -r,--ref filename for reference volume -o,--out filename for output (field/coef) volume - uses relative warp convention -f,--outformat Output format [field spline], default=field -w,--warpres Warp resolution (mm), only relevant when --outformat=spline -k,--knotspace Knot-spacing (voxels), only relevant when --outformat=spline -j,--jac filename for output (jacobian map) volume -a,--withaff If set, the affine transform is included in the field/jacobian -v,--verbose switch on diagnostic messages -h,--help display this message """ asrt.assertIsNifti(src) cmd = ['fnirtfileutils', '--in={}'.format(src)] cmd += wutils.applyArgStyle('--=', **kwargs) return cmd
def epi_reg(**kwargs): """Wrapper for the ``epi_reg`` command. Required options: :arg epi: EPI image :arg t1: T1 weighted image (wholehead) :arg t1_brain: T1 weighted image (brain extracted) :arg out: Output image name or LOAD to return in-memory image Additional options: :arg fmap: fieldmap image (in rad/s) :arg fmapmag: fieldmap magnitude image - wholehead extracted :arg fmapmagbrain: fieldmap magnitude image - brain extracted :arg gdc: Gradient-distortion corection warpfield :arg wmseg: white matter segmentation of T1 image :arg echospacing: Effective EPI echo spacing (sometimes called dwell time) - in seconds :arg pedir: Phase encoding direction, dir = x/y/z/-x/-y/-z :arg weight: weighting image (in T1 space) :arg nofmapreg: do not perform registration of fmap to T1 (use if fmap already registered) """ valmap = { 'nofmapreg': wutils.SHOW_IF_TRUE, } cmd = [ 'epi_reg', ] cmd += wutils.applyArgStyle('--=', valmap=valmap, singlechar_args=True, **kwargs) return cmd
def fslStats(inputFile, voxel, **kwargs): cmd = ['fslstats', inputFile, "-k", voxel, "-m"] return cmd + applyArgStyle('-', valmap={"v": SHOW_IF_TRUE}, **kwargs)