Skip to content

Commit 16eafc0

Browse files
committed
Update SPM interfaces to use the new PackageInfo
1 parent ead00d7 commit 16eafc0

File tree

6 files changed

+109
-67
lines changed

6 files changed

+109
-67
lines changed

nipype/interfaces/freesurfer/tests/test_FSSurfaceCommand.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ def test_FSSurfaceCommand_inputs():
2929

3030

3131
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
32-
def test_associated_file():
32+
def test_associated_file(tmpdir):
3333
fssrc = FreeSurferSource(subjects_dir=fs.Info.subjectsdir(),
3434
subject_id='fsaverage', hemi='lh')
35+
fssrc.base_dir = tmpdir.strpath
36+
fssrc.resource_monitor = False
3537

3638
fsavginfo = fssrc.run().outputs.get()
3739

nipype/interfaces/spm/base.py

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
# Local imports
3030
from ... import logging
3131
from ...utils import spm_docs as sd, NUMPY_MMAP
32-
from ..base import (BaseInterface, traits, isdefined, InputMultiPath,
33-
BaseInterfaceInputSpec, Directory, Undefined, ImageFile)
32+
from ..base import (
33+
BaseInterface, traits, isdefined, InputMultiPath,
34+
BaseInterfaceInputSpec, Directory, Undefined,
35+
ImageFile, PackageInfo
36+
)
3437
from ..matlab import MatlabCommand
3538
from ...external.due import due, Doi, BibTeX
3639

@@ -123,12 +126,34 @@ def scans_for_fnames(fnames, keep4d=False, separate_sessions=False):
123126
return flist
124127

125128

126-
class Info(object):
129+
class Info(PackageInfo):
127130
"""Handles SPM version information
128131
"""
129-
@staticmethod
130-
def version(matlab_cmd=None, paths=None, use_mcr=None):
131-
"""Returns the path to the SPM directory in the Matlab path
132+
_path = None
133+
_name = None
134+
135+
@classmethod
136+
def path(klass, matlab_cmd=None, paths=None, use_mcr=None):
137+
if klass._path:
138+
return klass._path
139+
return klass.getinfo(matlab_cmd, paths, use_mcr)['path']
140+
141+
@classmethod
142+
def version(klass, matlab_cmd=None, paths=None, use_mcr=None):
143+
if klass._version:
144+
return klass._version
145+
return klass.getinfo(matlab_cmd, paths, use_mcr)['release']
146+
147+
@classmethod
148+
def name(klass, matlab_cmd=None, paths=None, use_mcr=None):
149+
if klass._name:
150+
return klass._name
151+
return klass.getinfo(matlab_cmd, paths, use_mcr)['name']
152+
153+
@classmethod
154+
def getinfo(klass, matlab_cmd=None, paths=None, use_mcr=None):
155+
"""
156+
Returns the path to the SPM directory in the Matlab path
132157
If path not found, returns None.
133158
134159
Parameters
@@ -152,6 +177,13 @@ def version(matlab_cmd=None, paths=None, use_mcr=None):
152177
returns None of path not found
153178
"""
154179

180+
if klass._name and klass._path and klass._version:
181+
return {
182+
'name': klass._name,
183+
'path': klass._path,
184+
'release': klass._version
185+
}
186+
155187
use_mcr = use_mcr or 'FORCE_SPMMCR' in os.environ
156188
matlab_cmd = ((use_mcr and os.getenv('SPMMCRCMD')) or
157189
os.getenv('MATLABCMD') or
@@ -184,13 +216,17 @@ def version(matlab_cmd=None, paths=None, use_mcr=None):
184216
# No Matlab -- no spm
185217
logger.debug('%s', e)
186218
return None
187-
else:
188-
out = sd._strip_header(out.runtime.stdout)
189-
out_dict = {}
190-
for part in out.split('|'):
191-
key, val = part.split(':')
192-
out_dict[key] = val
193-
return out_dict
219+
220+
out = sd._strip_header(out.runtime.stdout)
221+
out_dict = {}
222+
for part in out.split('|'):
223+
key, val = part.split(':')
224+
out_dict[key] = val
225+
226+
klass._version = out_dict['release']
227+
klass._path = out_dict['path']
228+
klass._name = out_dict['name']
229+
return out_dict
194230

195231

196232
def no_spm():
@@ -288,13 +324,15 @@ def _matlab_cmd_update(self):
288324

289325
@property
290326
def version(self):
291-
version_dict = Info.version(matlab_cmd=self.inputs.matlab_cmd,
292-
paths=self.inputs.paths,
293-
use_mcr=self.inputs.use_mcr)
294-
if version_dict:
295-
return '.'.join((version_dict['name'].split('SPM')[-1],
296-
version_dict['release']))
297-
return version_dict
327+
info_dict = Info.getinfo(
328+
matlab_cmd=self.inputs.matlab_cmd,
329+
paths=self.inputs.paths,
330+
use_mcr=self.inputs.use_mcr
331+
)
332+
if info_dict:
333+
return '%s.%s' % (
334+
info_dict['name'].split('SPM')[-1],
335+
info_dict['release'])
298336

299337
@property
300338
def jobtype(self):

nipype/interfaces/spm/tests/test_base.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616
from nipype.interfaces.spm.base import SPMCommandInputSpec
1717
from nipype.interfaces.base import traits
1818

19-
try:
20-
matlab_cmd = os.environ['MATLABCMD']
21-
except:
22-
matlab_cmd = 'matlab'
23-
24-
mlab.MatlabCommand.set_default_matlab_cmd(matlab_cmd)
19+
mlab.MatlabCommand.set_default_matlab_cmd(
20+
os.getenv('MATLABCMD', 'matlab'))
2521

2622

2723
def test_scan_for_fnames(create_files_in_directory):
@@ -35,10 +31,10 @@ def test_scan_for_fnames(create_files_in_directory):
3531
if not save_time:
3632
@pytest.mark.skipif(no_spm(), reason="spm is not installed")
3733
def test_spm_path():
38-
spm_path = spm.Info.version()['path']
34+
spm_path = spm.Info.path()
3935
if spm_path is not None:
4036
assert isinstance(spm_path, (str, bytes))
41-
assert 'spm' in spm_path
37+
assert 'spm' in spm_path.lower()
4238

4339

4440
def test_use_mfile():

nipype/interfaces/spm/tests/test_model.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
import nipype.interfaces.spm.model as spm
77
import nipype.interfaces.matlab as mlab
88

9-
try:
10-
matlab_cmd = os.environ['MATLABCMD']
11-
except:
12-
matlab_cmd = 'matlab'
13-
14-
mlab.MatlabCommand.set_default_matlab_cmd(matlab_cmd)
9+
mlab.MatlabCommand.set_default_matlab_cmd(
10+
os.getenv('MATLABCMD', 'matlab'))
1511

1612

1713
def test_level1design():

nipype/interfaces/spm/tests/test_preprocess.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@
1010
from nipype.interfaces.spm import no_spm
1111
import nipype.interfaces.matlab as mlab
1212

13-
try:
14-
matlab_cmd = os.environ['MATLABCMD']
15-
except:
16-
matlab_cmd = 'matlab'
17-
18-
mlab.MatlabCommand.set_default_matlab_cmd(matlab_cmd)
13+
mlab.MatlabCommand.set_default_matlab_cmd(
14+
os.getenv('MATLABCMD', 'matlab'))
1915

2016

2117
def test_slicetiming():
@@ -88,7 +84,7 @@ def test_normalize12_list_outputs(create_files_in_directory):
8884

8985
@pytest.mark.skipif(no_spm(), reason="spm is not installed")
9086
def test_segment():
91-
if spm.Info.version()['name'] == "SPM12":
87+
if spm.Info.name() == "SPM12":
9288
assert spm.Segment()._jobtype == 'tools'
9389
assert spm.Segment()._jobname == 'oldseg'
9490
else:
@@ -98,7 +94,7 @@ def test_segment():
9894

9995
@pytest.mark.skipif(no_spm(), reason="spm is not installed")
10096
def test_newsegment():
101-
if spm.Info.version()['name'] == "SPM12":
97+
if spm.Info.name() == "SPM12":
10298
assert spm.NewSegment()._jobtype == 'spatial'
10399
assert spm.NewSegment()._jobname == 'preproc'
104100
else:

nipype/workflows/fmri/spm/preprocess.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from ....interfaces import spm as spm
99
from ....interfaces import utility as niu
1010
from ....pipeline import engine as pe
11-
from ....interfaces.matlab import no_matlab
1211
from ...smri.freesurfer.utils import create_getmask_flow
1312

1413
from .... import logging
@@ -141,7 +140,8 @@ def create_vbm_preproc(name='vbmpreproc'):
141140
142141
>>> preproc = create_vbm_preproc()
143142
>>> preproc.inputs.inputspec.fwhm = 8
144-
>>> preproc.inputs.inputspec.structural_files = [os.path.abspath('s1.nii'), os.path.abspath('s3.nii')]
143+
>>> preproc.inputs.inputspec.structural_files = [
144+
... os.path.abspath('s1.nii'), os.path.abspath('s3.nii')]
145145
>>> preproc.inputs.inputspec.template_prefix = 'Template'
146146
>>> preproc.run() # doctest: +SKIP
147147
@@ -185,7 +185,9 @@ def getclass1images(class_images):
185185
class1images.extend(session[0])
186186
return class1images
187187

188-
workflow.connect(dartel_template, ('segment.native_class_images', getclass1images), norm2mni, 'apply_to_files')
188+
workflow.connect(
189+
dartel_template, ('segment.native_class_images', getclass1images),
190+
norm2mni, 'apply_to_files')
189191
workflow.connect(inputnode, 'fwhm', norm2mni, 'fwhm')
190192

191193
def compute_icv(class_images):
@@ -217,10 +219,11 @@ def compute_icv(class_images):
217219
"icv"
218220
]),
219221
name="outputspec")
220-
workflow.connect([(dartel_template, outputnode, [('outputspec.template_file', 'template_file')]),
221-
(norm2mni, outputnode, [("normalized_files", "normalized_files")]),
222-
(calc_icv, outputnode, [("icv", "icv")]),
223-
])
222+
workflow.connect([
223+
(dartel_template, outputnode, [('outputspec.template_file', 'template_file')]),
224+
(norm2mni, outputnode, [("normalized_files", "normalized_files")]),
225+
(calc_icv, outputnode, [("icv", "icv")]),
226+
])
224227

225228
return workflow
226229

@@ -233,7 +236,8 @@ def create_DARTEL_template(name='dartel_template'):
233236
-------
234237
235238
>>> preproc = create_DARTEL_template()
236-
>>> preproc.inputs.inputspec.structural_files = [os.path.abspath('s1.nii'), os.path.abspath('s3.nii')]
239+
>>> preproc.inputs.inputspec.structural_files = [
240+
... os.path.abspath('s1.nii'), os.path.abspath('s3.nii')]
237241
>>> preproc.inputs.inputspec.template_prefix = 'Template'
238242
>>> preproc.run() # doctest: +SKIP
239243
@@ -259,24 +263,34 @@ def create_DARTEL_template(name='dartel_template'):
259263
name='segment')
260264
workflow.connect(inputnode, 'structural_files', segment, 'channel_files')
261265

262-
version = spm.Info.version()
263-
if version:
264-
spm_path = version['path']
265-
if version['name'] == 'SPM8':
266-
tissue1 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 1), 2, (True, True), (False, False))
267-
tissue2 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 2), 2, (True, True), (False, False))
268-
tissue3 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 3), 2, (True, False), (False, False))
269-
tissue4 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 4), 3, (False, False), (False, False))
270-
tissue5 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 5), 4, (False, False), (False, False))
271-
tissue6 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 6), 2, (False, False), (False, False))
272-
elif version['name'] == 'SPM12':
273-
spm_path = version['path']
266+
spm_info = spm.Info.getinfo()
267+
if spm_info:
268+
spm_path = spm_info['path']
269+
if spm_info['name'] == 'SPM8':
270+
tissue1 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 1),
271+
2, (True, True), (False, False))
272+
tissue2 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 2),
273+
2, (True, True), (False, False))
274+
tissue3 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 3),
275+
2, (True, False), (False, False))
276+
tissue4 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 4),
277+
3, (False, False), (False, False))
278+
tissue5 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 5),
279+
4, (False, False), (False, False))
280+
tissue6 = ((os.path.join(spm_path, 'toolbox/Seg/TPM.nii'), 6),
281+
2, (False, False), (False, False))
282+
elif spm_info['name'] == 'SPM12':
283+
spm_path = spm_info['path']
274284
tissue1 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 1), 1, (True, True), (False, False))
275285
tissue2 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 2), 1, (True, True), (False, False))
276-
tissue3 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 3), 2, (True, False), (False, False))
277-
tissue4 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 4), 3, (False, False), (False, False))
278-
tissue5 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 5), 4, (False, False), (False, False))
279-
tissue6 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 6), 2, (False, False), (False, False))
286+
tissue3 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 3),
287+
2, (True, False), (False, False))
288+
tissue4 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 4),
289+
3, (False, False), (False, False))
290+
tissue5 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 5),
291+
4, (False, False), (False, False))
292+
tissue6 = ((os.path.join(spm_path, 'tpm/TPM.nii'), 6),
293+
2, (False, False), (False, False))
280294
else:
281295
logger.critical('Unsupported version of SPM')
282296

0 commit comments

Comments
 (0)