2727from scipy .stats import zscore
2828
2929from nitransforms .base import TransformBase
30+ from nitransforms .linear import Affine
3031
3132
3233DEFAULT_FD_RADIUS = 50.0
@@ -82,7 +83,8 @@ def compute_fd_from_motion(
8283
8384def compute_fd_from_transform (
8485 img : nb .spatialimages .SpatialImage ,
85- test_xfm : TransformBase ,
86+ xfm : TransformBase ,
87+ xfm_prev : TransformBase | None = None ,
8688 radius : float = DEFAULT_FD_RADIUS ,
8789 n_vertices : int = 8 ,
8890) -> float :
@@ -97,8 +99,11 @@ def compute_fd_from_transform(
9799 ----------
98100 img : :obj:`~nibabel.spatialimages.SpatialImage`
99101 The reference image. Used to extract the center coordinates.
100- test_xfm : :obj:`~nitransforms.base.TransformBase`
102+ xfm : :obj:`~nitransforms.base.TransformBase`
101103 The transformation to test. Applied to coordinates around the image center.
104+ xfm_prev : :obj:`~nitransforms.base.TransformBase`, optional
105+ A previous transformation to compare with. If ``None``, the identity
106+ transformation is assumed (no transformation).
102107 radius : :obj:`float`, optional
103108 The radius (in mm) of the spherical neighborhood around the center of the image.
104109 n_vertices : :obj:`int`, optional
@@ -110,6 +115,8 @@ def compute_fd_from_transform(
110115 The average framewise displacement (FD) for the test transformation.
111116
112117 """
118+ xfm_prev = Affine () if xfm_prev is None else xfm_prev
119+
113120 affine = img .affine
114121 # Compute the center of the image in voxel space
115122 center_ijk = 0.5 * (np .array (img .shape [:3 ]) - 1 )
@@ -118,13 +125,13 @@ def compute_fd_from_transform(
118125 # Generate coordinates of points at radius distance from center
119126 fd_coords = sample_unit_sphere (n_points = n_vertices ) * radius + center_xyz
120127 # Compute the average displacement from the test transformation
121- return np .mean (np .linalg .norm (test_xfm .map (fd_coords ) - fd_coords , axis = - 1 ))
128+ return np .mean (np .linalg .norm (xfm .map (fd_coords ) - xfm_prev . map ( fd_coords ) , axis = - 1 ))
122129
123130
124131def displacements_within_mask (
125132 mask_img : nb .spatialimages .SpatialImage ,
126- test_xfm : TransformBase ,
127- reference_xfm : TransformBase | None = None ,
133+ xfm : TransformBase ,
134+ xfm_prev : TransformBase | None = None ,
128135) -> np .ndarray :
129136 """
130137 Compute the distance between voxel coordinates mapped through two transforms.
@@ -134,11 +141,11 @@ def displacements_within_mask(
134141 mask_img : :obj:`~nibabel.spatialimages.SpatialImage`
135142 A mask image that defines the region of interest. Voxel coordinates
136143 within the mask are transformed.
137- test_xfm : :obj:`~nitransforms.base.TransformBase`
144+ xfm : :obj:`~nitransforms.base.TransformBase`
138145 The transformation to test. This transformation is applied to the
139146 voxel coordinates.
140- reference_xfm : :obj:`~nitransforms.base.TransformBase`, optional
141- A reference transformation to compare with. If ``None``, the identity
147+ xfm_prev : :obj:`~nitransforms.base.TransformBase`, optional
148+ A previous ( reference) transformation to compare with. If ``None``, the identity
142149 transformation is assumed (no transformation).
143150
144151 Returns
@@ -155,10 +162,10 @@ def displacements_within_mask(
155162 np .argwhere (maskdata ),
156163 )
157164 # Apply the test transformation
158- targets = test_xfm .map (xyz )
165+ targets = xfm .map (xyz )
159166
160167 # Compute the difference (displacement) between the test and reference transformations
161- diffs = targets - xyz if reference_xfm is None else targets - reference_xfm .map (xyz )
168+ diffs = targets - xyz if xfm_prev is None else targets - xfm_prev .map (xyz )
162169 return np .linalg .norm (diffs , axis = - 1 )
163170
164171
0 commit comments