Skip to content

Commit f6772b2

Browse files
committed
Add optional volume center to FD-J calculation
1 parent 7b13bce commit f6772b2

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

CPAC/generate_motion_statistics/generate_motion_statistics.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,21 +324,31 @@ def calculate_FD_P(in_file):
324324
return out_file
325325

326326

327-
def calculate_FD_J(in_file, motion_correct_tool='3dvolreg'):
327+
def calculate_FD_J(in_file, motion_correct_tool='3dvolreg', center=None):
328328
"""
329329
Method to calculate framewise displacement as per Jenkinson et al. 2002
330330
331331
Parameters
332332
----------
333333
in_file : string
334-
matrix transformations from volume alignment file path
334+
matrix transformations from volume alignment file path if
335+
motion_correct_tool is '3dvolreg', or FDRMS (*_rel.rms) output if
336+
motion_correct_tool is 'mcflirt'.
337+
motion_correct_tool : string
338+
motion correction tool used, '3dvolreg' or 'mcflirt'.
339+
center : ndarray
340+
optional volume center for the calculation.
335341
336342
Returns
337343
-------
338344
out_file : string
339345
Frame-wise displacement file path
340346
341347
"""
348+
if center is None:
349+
center = np.zeros((3, 1))
350+
else:
351+
center = np.asarray(center).reshape((3, 1))
342352

343353
if motion_correct_tool == '3dvolreg':
344354
pm_ = np.genfromtxt(in_file)
@@ -359,7 +369,7 @@ def calculate_FD_J(in_file, motion_correct_tool='3dvolreg'):
359369

360370
M = np.dot(T_rb, np.linalg.inv(T_rb_prev)) - np.eye(4)
361371
A = M[0:3, 0:3]
362-
b = M[0:3, 3]
372+
b = M[0:3, 3:4] + A @ center
363373

364374
fd[i] = np.sqrt(
365375
(rmax * rmax / 5) * np.trace(np.dot(A.T, A)) + np.dot(b.T, b)
@@ -370,6 +380,9 @@ def calculate_FD_J(in_file, motion_correct_tool='3dvolreg'):
370380
elif motion_correct_tool == 'mcflirt':
371381
rel_rms = np.loadtxt(in_file)
372382
fd = np.append(0, rel_rms)
383+
384+
else:
385+
raise ValueError(f"motion_correct_tool {motion_correct_tool} not supported")
373386

374387
out_file = os.path.join(os.getcwd(), 'FD_J.1D')
375388
np.savetxt(out_file, fd, fmt='%.8f')

0 commit comments

Comments
 (0)