Skip to content

Commit

Permalink
✅ Update calculate_FD_J test for #1624 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shnizzedy committed Oct 12, 2023
1 parent 1a6735d commit a765d4c
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions CPAC/generate_motion_statistics/generate_motion_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from CPAC.pipeline import nipype_pipeline_engine as pe
from CPAC.utils.interfaces.function import Function
from CPAC.utils.pytest import skipif
from CPAC.utils.typing import LITERAL
from CPAC.utils.typing import LITERAL, TUPLE


def motion_power_statistics(name='motion_stats',
Expand Down Expand Up @@ -375,7 +375,8 @@ def calculate_FD_P(in_file):
@skipif(sys.version_info < (3, 10),
reason="Test requires Python 3.10 or higher")
def calculate_FD_J(in_file: str, calc_from: LITERAL['affine', 'rms'],
center: Optional[np.ndarray] = None) -> str:
center: Optional[np.ndarray] = None
) -> TUPLE[str, np.ndarray]:
"""
Method to calculate framewise displacement as per Jenkinson et al. 2002
Expand All @@ -387,16 +388,22 @@ def calculate_FD_J(in_file: str, calc_from: LITERAL['affine', 'rms'],
calc_from is 'rms'.
calc_from : string
one of {'affine', 'rms'}
center : ndarray, optional
center : ~numpy.ndarray, optional
optional volume center for the from-affine calculation
Returns
-------
out_file : string
Frame-wise displacement file path
Framewise displacement file path
fdj : ~numpy.ndarray
Framewise displacement array
Examples
--------
The file and array output by this function and the "rels_rms"
property of the pickled test data (offset by a leading zero)
should all be equal (rounded to the neareast 0.001):
>>> import gzip, os, pickle
>>> from unittest import mock
>>> import numpy as np
Expand All @@ -406,10 +413,16 @@ def calculate_FD_J(in_file: str, calc_from: LITERAL['affine', 'rms'],
>>> with mock.patch('nibabel.load',
... return_value=test_data.img), mock.patch(
... 'numpy.genfromtxt', return_value=test_data.affine):
... fdj_file = calculate_FD_J(test_data.affine, calc_from='affine',
... center=find_volume_center(test_data.img))
>>> all(np.isclose(np.genfromtxt(fdj_file),
... np.insert(test_data.rels_rms, 0, 0), atol=0.001))
... fdj_file, fdj = calculate_FD_J(
... test_data.affine, calc_from='affine',
... center=find_volume_center(test_data.img))
>>> fdj_from_file = np.genfromtxt(fdj_file)
>>> fdj_test_data = np.insert(test_data.rels_rms, 0, 0)
>>> all(np.isclose(fdj, fdj_from_file, atol=0.001))
True
>>> all(np.isclose(fdj, fdj_test_data, atol=0.001))
True
>>> all(np.isclose(fdj_from_file, fdj_test_data, atol=0.001))
True
>>> os.unlink(fdj_file)
"""
Expand Down

0 comments on commit a765d4c

Please sign in to comment.