Skip to content

Commit cd6b07a

Browse files
committed
ENH: Allow BIDS-style slice timings to be passed directly to TShift
1 parent 7fa55ae commit cd6b07a

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

nipype/interfaces/afni/preprocess.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
AFNICommandOutputSpec, AFNIPythonCommandInputSpec,
2121
AFNIPythonCommand, Info, no_afni)
2222

23+
from ...import logging
24+
iflogger = logging.getLogger('interface')
25+
2326

2427
class CentralityInputSpec(AFNICommandInputSpec):
2528
"""Common input spec class for all centrality-related commands
@@ -2565,9 +2568,10 @@ class TProject(AFNICommand):
25652568
output_spec = AFNICommandOutputSpec
25662569

25672570

2571+
25682572
class TShiftInputSpec(AFNICommandInputSpec):
25692573
in_file = File(
2570-
desc='input file to 3dTShift',
2574+
desc='input file to 3dTshift',
25712575
argstr='%s',
25722576
position=-1,
25732577
mandatory=True,
@@ -2594,12 +2598,26 @@ class TShiftInputSpec(AFNICommandInputSpec):
25942598
desc='ignore the first set of points specified', argstr='-ignore %s')
25952599
interp = traits.Enum(
25962600
('Fourier', 'linear', 'cubic', 'quintic', 'heptic'),
2597-
desc='different interpolation methods (see 3dTShift for details) '
2601+
desc='different interpolation methods (see 3dTshift for details) '
25982602
'default = Fourier',
25992603
argstr='-%s')
2600-
tpattern = Str(
2604+
tpattern = traits.Enum(
2605+
'alt+z', 'altplus', # Synonyms
2606+
'alt+z2',
2607+
'alt-z', 'altminus', # Synonyms
2608+
'alt-z2',
2609+
'seq+z', 'seqplus', # Synonyms
2610+
'seq-z', 'seqminus', # Synonyms
2611+
Str, # For backwards compatibility
26012612
desc='use specified slice time pattern rather than one in header',
2602-
argstr='-tpattern %s')
2613+
argstr='-tpattern %s',
2614+
xor=['slice_timing'])
2615+
slice_timing = traits.Either(
2616+
File(exists=True),
2617+
traits.List(traits.Float),
2618+
desc='time offsets from the volume acquisition onset for each slice',
2619+
argstr='-tpattern @%s',
2620+
xor=['tpattern'])
26032621
rlt = traits.Bool(
26042622
desc='Before shifting, remove the mean and linear trend',
26052623
argstr='-rlt')
@@ -2628,11 +2646,41 @@ class TShift(AFNICommand):
26282646
'3dTshift -prefix functional_tshift -tpattern alt+z -tzero 0.0 functional.nii'
26292647
>>> res = tshift.run() # doctest: +SKIP
26302648
2649+
Slice timings may be explicitly specified:
2650+
2651+
>>> TR = 2.5
2652+
>>> tshift = afni.TShift()
2653+
>>> tshift.inputs.in_file = 'functional.nii'
2654+
>>> tshift.inputs.tzero = 0.0
2655+
>>> tshift.inputs.tr = '%.1fs' % TR
2656+
>>> tshift.inputs.slice_timing = list(np.arange(40) / TR)
2657+
>>> tshift.cmdline
2658+
'3dTshift -prefix functional_tshift -tpattern @slice_timing.1D -TR 2.5s -tzero 0.0 functional.nii'
2659+
2660+
This will create the ``slice_timing.1D`` file in the working directory.
2661+
You may wish to remove this after running:
2662+
2663+
>>> os.unlink('slice_timing.1D')
2664+
26312665
"""
26322666
_cmd = '3dTshift'
26332667
input_spec = TShiftInputSpec
26342668
output_spec = AFNICommandOutputSpec
26352669

2670+
def _format_arg(self, name, trait_spec, value):
2671+
if name == 'tpattern' and value.startswith('@'):
2672+
iflogger.warning('Passing a file prefixed by "@" will be deprecated'
2673+
'; please use the `slice_timing` input')
2674+
elif name == 'slice_timing' and isinstance(value, list):
2675+
value = self._write_slice_timing()
2676+
return super(TShift, self)._format_arg(name, trait_spec, value)
2677+
2678+
def _write_slice_timing(self):
2679+
fname = 'slice_timing.1D'
2680+
with open(fname, 'w') as fobj:
2681+
fobj.write('\t'.join(map(str, self.inputs.slice_timing)))
2682+
return fname
2683+
26362684

26372685
class VolregInputSpec(AFNICommandInputSpec):
26382686
in_file = File(

0 commit comments

Comments
 (0)