Skip to content

Commit 519e96c

Browse files
committed
Merge branch 'master' of git://github.com/nipy/nipype into fix/realign
2 parents 3ec370c + 6c23846 commit 519e96c

22 files changed

+627
-106
lines changed

nipype/interfaces/ants/registration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ class RegistrationInputSpec(ANTSCommandInputSpec):
378378
winsorize_lower_quantile = traits.Range(
379379
low=0.0, high=1.0, value=0.0, argstr='%s', usedefault=True, desc="The Lower quantile to clip image ranges")
380380

381+
verbose = traits.Bool(argstr='-v', default=False)
382+
381383

382384
class RegistrationOutputSpec(TraitedSpec):
383385
forward_transforms = traits.List(

nipype/interfaces/ants/tests/test_auto_Registration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def test_Registration_inputs():
104104
use_estimate_learning_rate_once=dict(),
105105
use_histogram_matching=dict(usedefault=True,
106106
),
107+
verbose=dict(argstr='-v',
108+
),
107109
winsorize_lower_quantile=dict(argstr='%s',
108110
usedefault=True,
109111
),

nipype/interfaces/freesurfer/base.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
from __future__ import print_function, division, unicode_literals, absolute_import
1818
from builtins import open, object, str
1919

20-
2120
import os
2221

22+
from ... import LooseVersion
2323
from ...utils.filemanip import fname_presuffix
2424
from ..base import (CommandLine, Directory,
2525
CommandLineInputSpec, isdefined,
2626
traits, TraitedSpec, File)
2727

2828
__docformat__ = 'restructuredtext'
2929

30+
3031
class Info(object):
3132
""" Freesurfer subject directory and version information.
3233
@@ -65,6 +66,41 @@ def version():
6566
fid.close()
6667
return version
6768

69+
@classmethod
70+
def looseversion(cls):
71+
""" Return a comparable version object
72+
73+
If no version found, use LooseVersion('0.0.0')
74+
"""
75+
ver = cls.version()
76+
if ver is None:
77+
return LooseVersion('0.0.0')
78+
79+
vinfo = ver.rstrip().split('-')
80+
try:
81+
int(vinfo[-1], 16)
82+
except ValueError:
83+
githash = ''
84+
else:
85+
githash = '.' + vinfo[-1]
86+
87+
# As of FreeSurfer v6.0.0, the final component is a githash
88+
if githash:
89+
if vinfo[3] == 'dev':
90+
# This will need updating when v6.0.1 comes out
91+
vstr = '6.0.0-dev' + githash
92+
elif vinfo[5][0] == 'v':
93+
vstr = vinfo[5][1:]
94+
else:
95+
raise RuntimeError('Unknown version string: ' + ver)
96+
# Retain pre-6.0.0 heuristics
97+
elif 'dev' in ver:
98+
vstr = vinfo[-1] + '-dev'
99+
else:
100+
vstr = ver.rstrip().split('-v')[-1]
101+
102+
return LooseVersion(vstr)
103+
68104
@classmethod
69105
def subjectsdir(cls):
70106
"""Check the global SUBJECTS_DIR
@@ -154,12 +190,9 @@ def _gen_fname(self, basename, fname=None, cwd=None, suffix='_fs',
154190

155191
@property
156192
def version(self):
157-
ver = Info.version()
158-
if ver:
159-
if 'dev' in ver:
160-
return ver.rstrip().split('-')[-1] + '.dev'
161-
else:
162-
return ver.rstrip().split('-v')[-1]
193+
ver = Info.looseversion()
194+
if ver > LooseVersion("0.0.0"):
195+
return ver.vstring
163196

164197

165198
class FSSurfaceCommand(FSCommand):
@@ -183,18 +216,19 @@ def _normalize_filenames(self):
183216
def _associated_file(in_file, out_name):
184217
"""Based on MRIsBuildFileName in freesurfer/utils/mrisurf.c
185218
219+
If no path information is provided for out_name, use path and
220+
hemisphere (if also unspecified) from in_file to determine the path
221+
of the associated file.
186222
Use in_file prefix to indicate hemisphere for out_name, rather than
187223
inspecting the surface data structure.
188-
Also, output to in_file's directory if path information not provided
189-
for out_name.
190224
"""
191225
path, base = os.path.split(out_name)
192226
if path == '':
193227
path, in_file = os.path.split(in_file)
194228
hemis = ('lh.', 'rh.')
195229
if in_file[:3] in hemis and base[:3] not in hemis:
196230
base = in_file[:3] + base
197-
return os.path.abspath(os.path.join(path, base))
231+
return os.path.join(path, base)
198232

199233

200234
class FSScriptCommand(FSCommand):

nipype/interfaces/freesurfer/preprocess.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@
3636
__docformat__ = 'restructuredtext'
3737
iflogger = logging.getLogger('interface')
3838

39-
FSVersion = "0"
40-
_ver = Info.version()
41-
if _ver:
42-
if 'dev' in _ver:
43-
FSVersion = _ver.rstrip().split('-')[-1] + '.dev'
44-
else:
45-
FSVersion = _ver.rstrip().split('-v')[-1]
39+
# Keeping this to avoid breaking external programs that depend on it, but
40+
# this should not be used internally
41+
FSVersion = Info.looseversion().vstring
4642

4743

4844
class ParseDICOMDirInputSpec(FSTraitedSpec):
@@ -724,7 +720,7 @@ class ReconAll(CommandLine):
724720
'mri/brainmask.auto.mgz',
725721
'mri/brainmask.mgz'], []),
726722
]
727-
if LooseVersion(FSVersion) < LooseVersion("6.0.0"):
723+
if Info.looseversion() < LooseVersion("6.0.0"):
728724
_autorecon2_steps = [
729725
('gcareg', ['mri/transforms/talairach.lta'], []),
730726
('canorm', ['mri/norm.mgz'], []),
@@ -1072,7 +1068,7 @@ class BBRegister(FSCommand):
10721068
"""
10731069

10741070
_cmd = 'bbregister'
1075-
if FSVersion and LooseVersion(FSVersion) < LooseVersion("6.0.0"):
1071+
if LooseVersion('0.0.0') < Info.looseversion() < LooseVersion("6.0.0"):
10761072
input_spec = BBRegisterInputSpec
10771073
else:
10781074
input_spec = BBRegisterInputSpec6
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# AUTO-GENERATED by tools/checkspecs.py on 2017.04.21
2+
# Modified 2017.04.21 by Chris Markiewicz
3+
from __future__ import unicode_literals
4+
import pytest
5+
6+
from ..base import FSSurfaceCommand
7+
from ... import freesurfer as fs
8+
from ...io import FreeSurferSource
9+
10+
11+
def test_FSSurfaceCommand_inputs():
12+
input_map = dict(args=dict(argstr='%s',
13+
),
14+
environ=dict(nohash=True,
15+
usedefault=True,
16+
),
17+
ignore_exception=dict(nohash=True,
18+
usedefault=True,
19+
),
20+
subjects_dir=dict(),
21+
terminal_output=dict(nohash=True,
22+
),
23+
)
24+
inputs = FSSurfaceCommand.input_spec()
25+
26+
for key, metadata in list(input_map.items()):
27+
for metakey, value in list(metadata.items()):
28+
assert getattr(inputs.traits()[key], metakey) == value
29+
30+
31+
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
32+
def test_associated_file():
33+
fssrc = FreeSurferSource(subjects_dir=fs.Info.subjectsdir(),
34+
subject_id='fsaverage', hemi='lh')
35+
36+
fsavginfo = fssrc.run().outputs.get()
37+
38+
# Pairs of white/pial files in the same directories
39+
for white, pial in [('lh.white', 'lh.pial'),
40+
('./lh.white', './lh.pial'),
41+
(fsavginfo['white'], fsavginfo['pial'])]:
42+
43+
# Unspecified paths, possibly with missing hemisphere information,
44+
# are equivalent to using the same directory and hemisphere
45+
for name in ('pial', 'lh.pial', pial):
46+
assert FSSurfaceCommand._associated_file(white, name) == pial
47+
48+
# With path information, no changes are made
49+
for name in ('./pial', './lh.pial', fsavginfo['pial']):
50+
assert FSSurfaceCommand._associated_file(white, name) == name

nipype/interfaces/freesurfer/tests/test_auto_FSSurfaceCommand.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

nipype/interfaces/freesurfer/tests/test_auto_MRIsExpand.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def test_MRIsExpand_inputs():
2323
mandatory=True,
2424
position=-3,
2525
),
26-
navgs=dict(argstr='-navgs %d %d',
27-
),
2826
nsurfaces=dict(argstr='-N %d',
2927
),
3028
out_name=dict(argstr='%s',

nipype/interfaces/freesurfer/tests/test_preprocess.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# -*- coding: utf-8 -*-
22
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
from builtins import str
45
import os
56

67
import pytest
78
from nipype.testing.fixtures import create_files_in_directory
89

910
from nipype.interfaces import freesurfer
10-
from nipype.interfaces.freesurfer.preprocess import FSVersion
11+
from nipype.interfaces.freesurfer import Info
1112
from nipype import LooseVersion
1213

1314

@@ -138,7 +139,7 @@ def test_bbregister(create_files_in_directory):
138139
bbr.inputs.contrast_type = 't2'
139140

140141
# Check that 'init' is mandatory in FS < 6, but not in 6+
141-
if LooseVersion(FSVersion) < LooseVersion("6.0.0"):
142+
if Info.looseversion() < LooseVersion("6.0.0"):
142143
with pytest.raises(ValueError):
143144
bbr.cmdline
144145
else:
@@ -154,3 +155,9 @@ def test_bbregister(create_files_in_directory):
154155
'--reg {base}_bbreg_fsaverage.dat '
155156
'--mov {full} --s fsaverage'.format(
156157
full=filelist[0], base=base))
158+
159+
def test_FSVersion():
160+
"""Check that FSVersion is a string that can be compared with LooseVersion
161+
"""
162+
assert isinstance(freesurfer.preprocess.FSVersion, str)
163+
assert LooseVersion(freesurfer.preprocess.FSVersion) >= LooseVersion("0")

nipype/interfaces/freesurfer/tests/test_utils.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
from nipype.testing.fixtures import (create_files_in_directory_plus_dummy_file,
99
create_surf_file_in_directory)
1010

11+
from nipype.pipeline import engine as pe
12+
from nipype.interfaces import freesurfer as fs
1113
from nipype.interfaces.base import TraitError
12-
import nipype.interfaces.freesurfer as fs
14+
from nipype.interfaces.io import FreeSurferSource
1315

1416

1517
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
@@ -159,3 +161,52 @@ def test_surfshots(create_files_in_directory_plus_dummy_file):
159161
os.environ["DISPLAY"] = hold_display
160162
except KeyError:
161163
pass
164+
165+
166+
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
167+
def test_mrisexpand(tmpdir):
168+
fssrc = FreeSurferSource(subjects_dir=fs.Info.subjectsdir(),
169+
subject_id='fsaverage', hemi='lh')
170+
171+
fsavginfo = fssrc.run().outputs.get()
172+
173+
# dt=60 to ensure very short runtime
174+
expand_if = fs.MRIsExpand(in_file=fsavginfo['smoothwm'],
175+
out_name='expandtmp',
176+
distance=1,
177+
dt=60)
178+
179+
expand_nd = pe.Node(
180+
fs.MRIsExpand(in_file=fsavginfo['smoothwm'],
181+
out_name='expandtmp',
182+
distance=1,
183+
dt=60),
184+
name='expand_node')
185+
186+
# Interfaces should have same command line at instantiation
187+
orig_cmdline = 'mris_expand -T 60 {} 1 expandtmp'.format(fsavginfo['smoothwm'])
188+
assert expand_if.cmdline == orig_cmdline
189+
assert expand_nd.interface.cmdline == orig_cmdline
190+
191+
# Run both interfaces
192+
if_res = expand_if.run()
193+
nd_res = expand_nd.run()
194+
195+
# Commandlines differ
196+
node_cmdline = 'mris_expand -T 60 -pial {cwd}/lh.pial {cwd}/lh.smoothwm ' \
197+
'1 expandtmp'.format(cwd=nd_res.runtime.cwd)
198+
assert if_res.runtime.cmdline == orig_cmdline
199+
assert nd_res.runtime.cmdline == node_cmdline
200+
201+
# Check output
202+
if_out_file = if_res.outputs.get()['out_file']
203+
nd_out_file = nd_res.outputs.get()['out_file']
204+
# Same filename
205+
assert op.basename(if_out_file) == op.basename(nd_out_file)
206+
# Interface places output in source directory
207+
assert op.dirname(if_out_file) == op.dirname(fsavginfo['smoothwm'])
208+
# Node places output in working directory
209+
assert op.dirname(nd_out_file) == nd_res.runtime.cwd
210+
211+
# Remove test surface
212+
os.unlink(if_out_file)

0 commit comments

Comments
 (0)