Skip to content

Commit 3a54409

Browse files
committed
RF: NipyBaseInterface
1 parent 3b25528 commit 3a54409

File tree

4 files changed

+43
-51
lines changed

4 files changed

+43
-51
lines changed

nipype/interfaces/nipy/base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
""" Base interface for nipy """
5+
6+
from ..base import LibraryBaseInterface
7+
from ...utils.misc import package_check
8+
9+
# Originally set in model, preprocess and utils
10+
# Set here to be imported, in case anybody depends on its presence
11+
# Remove in 2.0
12+
have_nipy = True
13+
try:
14+
package_check('nipy')
15+
except ImportError:
16+
have_nipy = False
17+
18+
19+
class NipyBaseInterface(LibraryBaseInterface):
20+
_pkg = 'nipy'

nipype/interfaces/nipy/model.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,12 @@
88
import nibabel as nb
99
import numpy as np
1010

11-
from ...utils.misc import package_check
1211
from ...utils import NUMPY_MMAP
1312

14-
from ..base import (BaseInterface, TraitedSpec, traits, File, OutputMultiPath,
13+
from .base import NipyBaseInterface, have_nipy
14+
from ..base import (TraitedSpec, traits, File, OutputMultiPath,
1515
BaseInterfaceInputSpec, isdefined)
1616

17-
have_nipy = True
18-
try:
19-
package_check('nipy')
20-
except Exception as e:
21-
have_nipy = False
22-
else:
23-
import nipy.modalities.fmri.design_matrix as dm
24-
import nipy.modalities.fmri.glm as GLM
25-
26-
if have_nipy:
27-
try:
28-
BlockParadigm = dm.BlockParadigm
29-
except AttributeError:
30-
from nipy.modalities.fmri.experimental_paradigm import BlockParadigm
31-
3217

3318
class FitGLMInputSpec(BaseInterfaceInputSpec):
3419
session_info = traits.List(
@@ -94,14 +79,20 @@ class FitGLMOutputSpec(TraitedSpec):
9479
a = File(exists=True)
9580

9681

97-
class FitGLM(BaseInterface):
82+
class FitGLM(NipyBaseInterface):
9883
'''
9984
Fit GLM model based on the specified design. Supports only single or concatenated runs.
10085
'''
10186
input_spec = FitGLMInputSpec
10287
output_spec = FitGLMOutputSpec
10388

10489
def _run_interface(self, runtime):
90+
import nipy.modalities.fmri.glm as GLM
91+
import nipy.modalities.fmri.design_matrix as dm
92+
try:
93+
BlockParadigm = dm.BlockParadigm
94+
except AttributeError:
95+
from nipy.modalities.fmri.experimental_paradigm import BlockParadigm
10596

10697
session_info = self.inputs.session_info
10798

@@ -283,14 +274,15 @@ class EstimateContrastOutputSpec(TraitedSpec):
283274
p_maps = OutputMultiPath(File(exists=True))
284275

285276

286-
class EstimateContrast(BaseInterface):
277+
class EstimateContrast(NipyBaseInterface):
287278
'''
288279
Estimate contrast of a fitted model.
289280
'''
290281
input_spec = EstimateContrastInputSpec
291282
output_spec = EstimateContrastOutputSpec
292283

293284
def _run_interface(self, runtime):
285+
import nipy.modalities.fmri.glm as GLM
294286

295287
beta_nii = nb.load(self.inputs.beta)
296288
if isdefined(self.inputs.mask):

nipype/interfaces/nipy/preprocess.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,12 @@
88
import nibabel as nb
99
import numpy as np
1010

11-
from ...utils.misc import package_check
1211
from ...utils import NUMPY_MMAP
13-
1412
from ...utils.filemanip import split_filename, fname_presuffix
15-
from ..base import (TraitedSpec, BaseInterface, traits, BaseInterfaceInputSpec,
16-
isdefined, File, InputMultiPath, OutputMultiPath)
1713

18-
have_nipy = True
19-
try:
20-
package_check('nipy')
21-
except Exception as e:
22-
have_nipy = False
23-
else:
24-
import nipy
25-
from nipy import save_image, load_image
26-
nipy_version = nipy.__version__
14+
from .base import NipyBaseInterface, have_nipy
15+
from ..base import (TraitedSpec, traits, BaseInterfaceInputSpec,
16+
isdefined, File, InputMultiPath, OutputMultiPath)
2717

2818

2919
class ComputeMaskInputSpec(BaseInterfaceInputSpec):
@@ -44,7 +34,7 @@ class ComputeMaskOutputSpec(TraitedSpec):
4434
brain_mask = File(exists=True)
4535

4636

47-
class ComputeMask(BaseInterface):
37+
class ComputeMask(NipyBaseInterface):
4838
input_spec = ComputeMaskInputSpec
4939
output_spec = ComputeMaskOutputSpec
5040

@@ -118,7 +108,7 @@ class SpaceTimeRealignerOutputSpec(TraitedSpec):
118108
"euler angles"))
119109

120110

121-
class SpaceTimeRealigner(BaseInterface):
111+
class SpaceTimeRealigner(NipyBaseInterface):
122112
"""Simultaneous motion and slice timing correction algorithm
123113
124114
If slice_times is not specified, this algorithm performs spatial motion
@@ -157,11 +147,8 @@ class SpaceTimeRealigner(BaseInterface):
157147
output_spec = SpaceTimeRealignerOutputSpec
158148
keywords = ['slice timing', 'motion correction']
159149

160-
@property
161-
def version(self):
162-
return nipy_version
163-
164150
def _run_interface(self, runtime):
151+
from nipy import save_image, load_image
165152
all_ims = [load_image(fname) for fname in self.inputs.in_file]
166153

167154
if not isdefined(self.inputs.slice_times):
@@ -233,7 +220,7 @@ class TrimOutputSpec(TraitedSpec):
233220
out_file = File(exists=True)
234221

235222

236-
class Trim(BaseInterface):
223+
class Trim(NipyBaseInterface):
237224
""" Simple interface to trim a few volumes from a 4d fmri nifti file
238225
239226
Examples

nipype/interfaces/nipy/utils.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,10 @@
55
import warnings
66
import nibabel as nb
77

8-
from ...utils.misc import package_check
9-
from ..base import (TraitedSpec, BaseInterface, traits, BaseInterfaceInputSpec,
8+
from .base import NipyBaseInterface, have_nipy
9+
from ..base import (TraitedSpec, traits, BaseInterfaceInputSpec,
1010
File, isdefined)
1111

12-
have_nipy = True
13-
try:
14-
package_check('nipy')
15-
except Exception as e:
16-
have_nipy = False
17-
else:
18-
from nipy.algorithms.registration.histogram_registration import HistogramRegistration
19-
from nipy.algorithms.registration.affine import Affine
20-
2112

2213
class SimilarityInputSpec(BaseInterfaceInputSpec):
2314
volume1 = File(exists=True, desc="3D volume", mandatory=True)
@@ -42,7 +33,7 @@ class SimilarityOutputSpec(TraitedSpec):
4233
similarity = traits.Float(desc="Similarity between volume 1 and 2")
4334

4435

45-
class Similarity(BaseInterface):
36+
class Similarity(NipyBaseInterface):
4637
"""Calculates similarity between two 3D volumes. Both volumes have to be in
4738
the same coordinate system, same space within that coordinate system and
4839
with the same voxel dimensions.
@@ -72,6 +63,8 @@ def __init__(self, **inputs):
7263
super(Similarity, self).__init__(**inputs)
7364

7465
def _run_interface(self, runtime):
66+
from nipy.algorithms.registration.histogram_registration import HistogramRegistration
67+
from nipy.algorithms.registration.affine import Affine
7568

7669
vol1_nii = nb.load(self.inputs.volume1)
7770
vol2_nii = nb.load(self.inputs.volume2)

0 commit comments

Comments
 (0)