Skip to content

Commit ad88ffc

Browse files
committed
feat: Add FramewiseDisplacement interface that expects headers
1 parent de5ee61 commit ad88ffc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

fmriprep/interfaces/confounds.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,39 @@ def _run_interface(self, runtime):
144144
return runtime
145145

146146

147+
class _FramewiseDisplacementInputSpec(BaseInterfaceInputSpec):
148+
in_file = File(exists=True, desc='Head motion transform file')
149+
radius = traits.Float(50, usedefault=True, desc='Radius of the head in mm')
150+
151+
152+
class _FramewiseDisplacementOutputSpec(TraitedSpec):
153+
out_file = File(desc='Output framewise displacement file')
154+
155+
156+
class FramewiseDisplacement(SimpleInterface):
157+
"""Calculate framewise displacement estimates."""
158+
159+
input_spec = _FramewiseDisplacementInputSpec
160+
output_spec = _FramewiseDisplacementOutputSpec
161+
162+
def _run_interface(self, runtime):
163+
self._results['out_file'] = fname_presuffix(
164+
self.inputs.in_file, suffix='_fd.tsv', newpath=runtime.cwd
165+
)
166+
167+
motion = pd.read_csv(self.inputs.in_file, delimiter='\t')
168+
169+
# Filter and ensure we have all parameters
170+
diff = motion[['trans_x', 'trans_y', 'trans_z', 'rot_x', 'rot_y', 'rot_z']].diff()
171+
diff[['rot_x', 'rot_y', 'rot_z']] *= self.inputs.radius
172+
173+
fd = pd.DataFrame(diff.abs().sum(axis=1), columns=['FramewiseDisplacement'])
174+
175+
fd.to_csv(self._results['out_file'], sep='\t', index=False)
176+
177+
return runtime
178+
179+
147180
class _FilterDroppedInputSpec(BaseInterfaceInputSpec):
148181
in_file = File(exists=True, desc='input CompCor metadata')
149182

0 commit comments

Comments
 (0)