-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Integrate downsampling in BSplineApprox
when the input is high-res
#301
Changes from all commits
b7dd98b
9893475
4b0a53c
41c5788
f25604d
995a898
80bb746
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,42 @@ | |
"""Image processing tools.""" | ||
|
||
|
||
def resample_to_zooms(in_file, zooms, order=3, prefilter=True): | ||
oesteban marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Resample the input data to a new grid with the requested zooms.""" | ||
from pathlib import Path | ||
import numpy as np | ||
import nibabel as nb | ||
from nibabel.affines import rescale_affine | ||
from nitransforms.linear import Affine | ||
|
||
if isinstance(in_file, (str, Path)): | ||
in_file = nb.load(in_file) | ||
|
||
# Prepare output x-forms | ||
sform, scode = in_file.get_sform(coded=True) | ||
qform, qcode = in_file.get_qform(coded=True) | ||
|
||
hdr = in_file.header.copy() | ||
zooms = np.array(zooms) | ||
|
||
pre_zooms = np.array(in_file.header.get_zooms()[:3]) | ||
# Could use `np.ceil` if we prefer | ||
new_shape = np.rint(np.array(in_file.shape[:3]) * pre_zooms / zooms) | ||
affine = rescale_affine(in_file.affine, in_file.shape[:3], zooms, new_shape) | ||
|
||
# Generate new reference | ||
hdr.set_sform(affine, scode) | ||
hdr.set_qform(affine, qcode) | ||
newref = in_file.__class__( | ||
np.zeros(new_shape.astype(int), dtype=hdr.get_data_dtype()), | ||
affine, | ||
hdr, | ||
) | ||
|
||
# Resample via identity transform | ||
return Affine(reference=newref).apply(in_file, order=order, prefilter=prefilter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at scipy.ndimage.map_coordinates, I don't think prefilter filters more or less based on the down-sampling factor. I suspect for the factors we'll be dealing with (mostly <2, almost all <3) it's probably fine, but I don't really know how to evaluate this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, that's the b-spline filtering, which blurs with the width of the B-Spline basis (cubic, then 4 voxels). I believe that is enough for the purpose of this interpolation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of SyN, ANTs is already giving you a displacements field that has been smoothed to a certain kernel width (the third parameter of -t [Syn]). We are not in a more general case where you have a signal with additive noise and you want to subsample it safely. So I'm quite convinced no extra smoothing should be added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (to answer the question, you are right, it is independent of the size ratio) -- but that should be okay, IMHO, for the reasons above. |
||
|
||
|
||
def ensure_positive_cosines(img): | ||
""" | ||
Reorient axes polarity to have all positive direction cosines. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for clarity, I think we want to rename from
zooms_min
, since it now diverges formself.inputs.zooms_min
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would you suggest? (I have no particular preference)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
target_zooms
made sense to me.