Skip to content

Commit ff2160a

Browse files
committed
refactor
1 parent 2e2dac1 commit ff2160a

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

nipype/algorithms/confounds.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
from ..interfaces.base import (traits, TraitedSpec, BaseInterface,
2828
BaseInterfaceInputSpec, File, isdefined,
2929
InputMultiPath)
30-
from nipype.utils import NUMPY_MMAP
30+
from ..utils import NUMPY_MMAP
31+
from ..utils.misc import normalize_mc_params
3132

3233
IFLOG = logging.getLogger('interface')
3334

@@ -195,8 +196,8 @@ def _list_outputs(self):
195196

196197

197198
class FramewiseDisplacementInputSpec(BaseInterfaceInputSpec):
198-
in_file = File(exists=True, mandatory=True, desc='motion parameters as written by FSL MCFLIRT or AFNI 3dvolreg')
199-
parameter_source = traits.Enum("FSL", "AFNI",
199+
in_file = File(exists=True, mandatory=True, desc='motion parameters')
200+
parameter_source = traits.Enum("FSL", "AFNI", "SPM", "FSFAST",
200201
desc="Source of movement parameters",
201202
mandatory=True)
202203
radius = traits.Float(50, usedefault=True,
@@ -253,10 +254,11 @@ class FramewiseDisplacement(BaseInterface):
253254

254255
def _run_interface(self, runtime):
255256
mpars = np.loadtxt(self.inputs.in_file) # mpars is N_t x 6
256-
diff = mpars[:-1, :] - mpars[1:, :]
257-
diff[:, :3] *= self.inputs.radius
258-
if self.inputs.parameter_source == "AFNI":
259-
diff[:, :3] *= (np.pi / 180)
257+
mpars = np.apply_along_axis(func1d=normalize_mc_params,
258+
axis=1, arr=mpars,
259+
source=self.inputs.parameter_source)
260+
diff = mpars[:-1, :6] - mpars[1:, :6]
261+
diff[:, 3:6] *= self.inputs.radius
260262
fd_res = np.abs(diff).sum(axis=1)
261263

262264
self._results = {

nipype/algorithms/rapidart.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
OutputMultiPath, TraitedSpec, File,
3535
BaseInterfaceInputSpec, isdefined)
3636
from ..utils.filemanip import filename_to_list, save_json, split_filename
37-
from ..utils.misc import find_indices
37+
from ..utils.misc import find_indices, normalize_mc_params
3838
from .. import logging, config
3939
iflogger = logging.getLogger('interface')
4040

@@ -46,15 +46,12 @@ def _get_affine_matrix(params, source):
4646
source : the package that generated the parameters
4747
supports SPM, AFNI, FSFAST, FSL, NIPY
4848
"""
49-
if source == 'FSL':
50-
params = params[[3, 4, 5, 0, 1, 2]]
51-
elif source in ('AFNI', 'FSFAST'):
52-
params = params[np.asarray([4, 5, 3, 1, 2, 0]) + (len(params) > 6)]
53-
params[3:] = params[3:] * np.pi / 180.
5449
if source == 'NIPY':
5550
# nipy does not store typical euler angles, use nipy to convert
5651
from nipy.algorithms.registration import to_matrix44
5752
return to_matrix44(params)
53+
54+
params = normalize_mc_params(params, source)
5855
# process for FSL, SPM, AFNI and FSFAST
5956
rotfunc = lambda x: np.array([[np.cos(x), np.sin(x)],
6057
[-np.sin(x), np.cos(x)]])

nipype/utils/misc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,15 @@ def unflatten(in_list, prev_structure):
243243
for item in prev_structure:
244244
out.append(unflatten(in_list, item))
245245
return out
246+
247+
248+
def normalize_mc_params(params, source):
249+
"""
250+
Normalize a single row of motion parameters to the SPM format.
251+
"""
252+
if source == 'FSL':
253+
params = params[[3, 4, 5, 0, 1, 2]]
254+
elif source in ('AFNI', 'FSFAST'):
255+
params = params[np.asarray([4, 5, 3, 1, 2, 0]) + (len(params) > 6)]
256+
params[3:] = params[3:] * np.pi / 180.
257+
return params

0 commit comments

Comments
 (0)