Skip to content

Commit 8827392

Browse files
committed
ENH: Detect or manually set repetition time
1 parent 07ab68e commit 8827392

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

nipype/algorithms/confounds.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ class CompCorInputSpec(BaseInterfaceInputSpec):
337337
False, usedefault=True,
338338
desc='Use cosine basis to remove low-frequency trends pre-component '
339339
'extraction')
340+
repetition_time = traits.Float(
341+
desc='Repetition time (TR) of series - derived from image header if '
342+
'unspecified')
340343
save_hpf_basis = traits.Either(
341344
traits.Bool, File, requires=['high_pass_filter'],
342345
desc='Save high pass filter basis as text file')
@@ -398,10 +401,10 @@ def _run_interface(self, runtime):
398401
mmap=NUMPY_MMAP)
399402

400403
if len(imgseries.shape) != 4:
401-
raise ValueError('tCompCor expected a 4-D nifti file. Input {} has '
402-
'{} dimensions (shape {})'.format(
403-
self.inputs.realigned_file, len(imgseries.shape),
404-
imgseries.shape))
404+
raise ValueError('{} expected a 4-D nifti file. Input {} has '
405+
'{} dimensions (shape {})'.format(
406+
self._header, self.inputs.realigned_file,
407+
len(imgseries.shape), imgseries.shape))
405408

406409
if len(mask_images) == 0:
407410
img = nb.Nifti1Image(np.ones(imgseries.shape[:3], dtype=np.bool),
@@ -411,9 +414,27 @@ def _run_interface(self, runtime):
411414

412415
mask_images = self._process_masks(mask_images, imgseries.get_data())
413416

417+
TR = 0
418+
if self.inputs.high_pass_filter:
419+
if isdefined(self.inputs.repetition_time):
420+
TR = self.inputs.repetition_time
421+
else:
422+
# Derive TR from NIfTI header, if possible
423+
try:
424+
TR = imgseries.header.get_zooms()[3]
425+
if self.inputs.get_xyzt_units()[1] == 'msec':
426+
TR /= 1000
427+
except AttributeError, IndexError:
428+
pass
429+
430+
if TR == 0:
431+
raise ValueError(
432+
'{} cannot detect repetition time from image - '
433+
'Set the repetition_time input'.format(self._header))
434+
414435
components, hpf_basis = compute_noise_components(
415436
imgseries.get_data(), mask_images, degree,
416-
self.inputs.num_components, self.inputs.high_pass_filter)
437+
self.inputs.num_components, self.inputs.high_pass_filter, TR)
417438

418439
components_file = os.path.join(os.getcwd(), self.inputs.components_file)
419440
np.savetxt(components_file, components, fmt=b"%.10f", delimiter='\t',
@@ -928,7 +949,7 @@ def combine_mask_files(mask_files, mask_method=None, mask_index=None):
928949

929950

930951
def compute_noise_components(imgseries, mask_images, degree, num_components,
931-
high_pass_filter=False):
952+
high_pass_filter=False, repetition_time=0):
932953
"""Compute the noise components from the imgseries for each mask
933954
934955
imgseries: a nibabel img
@@ -960,7 +981,8 @@ def compute_noise_components(imgseries, mask_images, degree, num_components,
960981
if high_pass_filter:
961982
# If degree == 0, remove mean in same pass
962983
voxel_timecourses, hpf_basis = cosine_filter(
963-
voxel_timecourses, 2.5, remove_mean=(degree == 0))
984+
voxel_timecourses, repetition_time,
985+
remove_mean=(degree == 0))
964986

965987
# from paper:
966988
# "The constant and linear trends of the columns in the matrix M were

0 commit comments

Comments
 (0)