Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Mathias Goncalves <[email protected]>
Co-authored-by: Chris Markiewicz <[email protected]>
  • Loading branch information
3 people committed May 2, 2023
1 parent f03d54a commit f01cee1
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions sdcflows/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@
# https://www.nipreps.org/community/licensing/
#
"""Image processing tools."""
import nibabel as nb


def deoblique_and_zooms(in_reference, oblique, factor=4, padding=1):
def deoblique_and_zooms(
in_reference: nb.spatialimages.SpatialImage,
oblique: nb.spatialimages.SpatialImage,
factor: int = 4,
padding: int = 1,
factor_tol: float = 1e-4,
):
"""
Generate a sampling reference aligned with in_reference fully covering oblique.
Expand All @@ -39,6 +46,8 @@ def deoblique_and_zooms(in_reference, oblique, factor=4, padding=1):
padding : :obj:`int`
Number of additional voxels around the most extreme positions of the projection of
oblique on to the reference.
factor_tol : :obj:`float`
Absolute tolerance to determine whether factor is one.
"""
from itertools import product
Expand All @@ -54,24 +63,23 @@ def deoblique_and_zooms(in_reference, oblique, factor=4, padding=1):
_, qcode = in_reference.get_qform(coded=True)

# Calculate the 8 most extreme coordinates of oblique in in_reference space
extent = apply_affine(
oblique.affine,
np.array(list(product((0, 1), repeat=3))) * (np.array(oblique.shape[:3]) - 1),
corners = np.array(list(product((0, 1), repeat=3))) * (
np.array(oblique.shape[:3]) - 1
)
extent_ijk = apply_affine(np.linalg.inv(affine), extent)
extent_ijk = apply_affine(np.linalg.inv(affine) @ oblique.affine, corners)

underflow = np.clip(extent_ijk.min(0) - padding, None, 0).astype(int)
overflow = np.ceil(
np.clip(extent_ijk.max(0) + padding + 1 - ref_shape, 0, None)
).astype(int)
if np.any(underflow < 0) or np.any(overflow > 0):
# Add under/overflow voxels
ref_shape += np.abs(underflow) + overflow
ref_shape += overflow - underflow
# Consistently update origin
affine[:-1, -1] = apply_affine(affine, underflow)

# Make grid denser
if abs(1.0 - factor) > 1e-4:
if abs(1.0 - factor) > factor_tol:
new_shape = np.rint(ref_shape * factor)
affine = rescale_affine(affine, ref_shape, ref_zooms / factor, new_shape)
ref_shape = new_shape
Expand All @@ -81,7 +89,7 @@ def deoblique_and_zooms(in_reference, oblique, factor=4, padding=1):
hdr.set_qform(affine, qcode)

return in_reference.__class__(
np.zeros(ref_shape.astype(int), dtype=hdr.get_data_dtype()),
nb.fileslice.strided_scalar(ref_shape.astype(int)),
affine,
hdr,
)
Expand Down

0 comments on commit f01cee1

Please sign in to comment.