-
Notifications
You must be signed in to change notification settings - Fork 262
NF: Conformation function and CLI tool to apply shape, orientation and zooms #853
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
Merged
Merged
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b897935
add `conform` and `_transform_range` funcs
76e9aed
Merge remote-tracking branch 'upstream/master' into add/fs-conform
9895eb0
add documentation and fix style
49e4ada
clean up documentation + only conform 3D
57c3648
add tests for `conform` and `_transform_range`
a681bdd
only test `conform` if scipy installed
4e62b7c
add `nib-conform` console script
kaczmarj e19b022
tighten scope of conform function
kaczmarj 12ea136
Merge remote-tracking branch 'upstream/master' into add/fs-conform
kaczmarj 89eedc5
add `nib-conform`
kaczmarj 348f838
use proper labels for orientation
kaczmarj 9491806
add non-3d tests
kaczmarj a9ce73b
fix style
kaczmarj 3911610
make voxel size and out shape int type
kaczmarj 0d8843b
add tests for `nib-conform` command
kaczmarj 3e4da11
skip tests if scipy not available
kaczmarj 8b712ca
use `nb.save(img, filename)` instead of `img.save(...)`
kaczmarj 67ace2f
keep input class by default in `conform`
kaczmarj 527400d
do not error on non-3d inputs
kaczmarj 6e19298
clean up code
kaczmarj 07fa254
correct the re-orientation of the output image in `conform`
kaczmarj 00825c7
make `to_img` the same image/header classes as input image
kaczmarj 4ca32ba
make pep8 gods happy
kaczmarj a536ed3
test for errors on non-3d inputs and arguments
kaczmarj 3af4bd8
test that file is not overwritten without `--force`
kaczmarj 3658170
remove bin/nib-conform because it is unused
kaczmarj eb097f4
copy header from input image in `conform`
kaczmarj 241f58f
NF: Add nibabel.affines.rescale_affine function
effigies f77fbb5
RF: Update conformation to reorient, rescale and resample
effigies 2177a59
Merge pull request #1 from effigies/add/fs-conform
kaczmarj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!python | ||
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
# | ||
# See COPYING file distributed along with the NiBabel package for the | ||
# copyright and license terms. | ||
# | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
""" | ||
Conform neuroimaging volume to arbitrary shape and voxel size. | ||
""" | ||
|
||
import argparse | ||
from pathlib import Path | ||
|
||
from nibabel import __version__ | ||
from nibabel.loadsave import load, save | ||
from nibabel.processing import conform | ||
|
||
|
||
def _get_parser(): | ||
"""Return command-line argument parser.""" | ||
p = argparse.ArgumentParser(description=__doc__) | ||
p.add_argument("infile", | ||
help="Neuroimaging volume to conform.") | ||
p.add_argument("outfile", | ||
help="Name of output file.") | ||
p.add_argument("--out-shape", nargs=3, default=(256, 256, 256), type=int, | ||
help="Shape of the conformed output.") | ||
p.add_argument("--voxel-size", nargs=3, default=(1, 1, 1), type=int, | ||
help="Voxel size in millimeters of the conformed output.") | ||
p.add_argument("--orientation", default="RAS", | ||
help="Orientation of the conformed output.") | ||
p.add_argument("-f", "--force", action="store_true", | ||
help="Overwrite existing output files.") | ||
p.add_argument("-V", "--version", action="version", version="{} {}".format(p.prog, __version__)) | ||
|
||
return p | ||
|
||
|
||
def main(args=None): | ||
"""Main program function.""" | ||
parser = _get_parser() | ||
opts = parser.parse_args(args) | ||
from_img = load(opts.infile) | ||
|
||
if not opts.force and Path(opts.outfile).exists(): | ||
raise FileExistsError("Output file exists: {}".format(opts.outfile)) | ||
|
||
out_img = conform( | ||
from_img=from_img, | ||
out_shape=opts.out_shape, | ||
voxel_size=opts.voxel_size, | ||
order=3, | ||
cval=0.0, | ||
orientation=opts.orientation) | ||
|
||
save(out_img, opts.outfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!python | ||
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
# | ||
# See COPYING file distributed along with the NiBabel package for the | ||
# copyright and license terms. | ||
# | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
|
||
import unittest | ||
|
||
import pytest | ||
|
||
import nibabel as nib | ||
from nibabel.testing import test_data | ||
from nibabel.cmdline.conform import main | ||
from nibabel.optpkg import optional_package | ||
|
||
_, have_scipy, _ = optional_package('scipy.ndimage') | ||
needs_scipy = unittest.skipUnless(have_scipy, 'These tests need scipy') | ||
|
||
|
||
@needs_scipy | ||
def test_default(tmpdir): | ||
infile = test_data(fname="anatomical.nii") | ||
outfile = tmpdir / "output.nii.gz" | ||
main([str(infile), str(outfile)]) | ||
assert outfile.isfile() | ||
c = nib.load(outfile) | ||
assert c.shape == (256, 256, 256) | ||
assert c.header.get_zooms() == (1, 1, 1) | ||
assert nib.orientations.aff2axcodes(c.affine) == ('R', 'A', 'S') | ||
|
||
with pytest.raises(FileExistsError): | ||
main([str(infile), str(outfile)]) | ||
|
||
main([str(infile), str(outfile), "--force"]) | ||
assert outfile.isfile() | ||
|
||
|
||
@needs_scipy | ||
def test_nondefault(tmpdir): | ||
infile = test_data(fname="anatomical.nii") | ||
outfile = tmpdir / "output.nii.gz" | ||
out_shape = (100, 100, 150) | ||
voxel_size = (1, 2, 4) | ||
orientation = "LAS" | ||
args = "{} {} --out-shape {} --voxel-size {} --orientation {}".format( | ||
infile, outfile, " ".join(map(str, out_shape)), " ".join(map(str, voxel_size)), orientation) | ||
main(args.split()) | ||
assert outfile.isfile() | ||
c = nib.load(outfile) | ||
assert c.shape == out_shape | ||
assert c.header.get_zooms() == voxel_size | ||
assert nib.orientations.aff2axcodes(c.affine) == tuple(orientation) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Consider the following image:
This is already RAS, 1mm isotropic and 256^3 in dimension. However, only the first octant (bounded by
(0, 0, 0), (0, 0, 128), ... ( 128, 128, 128)
) will be shared withto_img
. When we resample, I assume that we'll get a heavily cropped image.Assuming I'm correct, it might be better to construct the affine such that the same point is in the center of the image.
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.
I have a 256^3 image with 1^3 voxels in RAS, and I ran it through
nib-conform
to change it to 128^3 with 1^3 voxels. It is cropped, but the center of the original is the center of the output. Does this answer what you were getting at?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.
Nope, try the following:
This will place the (0, 0, 0) RAS point in the corner of the image. Conform
corner.nii.gz
and you'll see what I'm talking about.Uh oh!
There was an error while loading. Please reload this page.
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.
OK, did that, and indeed the conformed
corner.nii.gz
is heavily cropped. How can the affine be constructed to maintain the same center?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.
Need to solve for:
Noting that the affine is
And
t
is the thing you can change.If you've got time now, go for it, but I can work out the formula this afternoon.
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.
Let me take a shot at it this afternoon
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.
OK, this is way above level of understanding... sorry @effigies. i'll have to take linear algebra in grad school
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.
it looks like this is the freesurfer implementation of the solution we're looking for https://github.com/freesurfer/freesurfer/blob/ea988613231c850785b13632205467613c88d89d/utils/mri.cpp#L415-L523