8000 NF: Conformation function and CLI tool to apply shape, orientation and zooms by kaczmarj · Pull Request #853 · nipy/nibabel · GitHub
[go: up one dir, main page]

Skip to content

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 30 commits into from
Apr 19, 2020
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b897935
add `conform` and `_transform_range` funcs
Dec 11, 2019
76e9aed
Merge remote-tracking branch 'upstream/master' into add/fs-conform
Dec 11, 2019
9895eb0
add documentation and fix style
Dec 12, 2019
49e4ada
clean up documentation + only conform 3D
Dec 13, 2019
57c3648
add tests for `conform` and `_transform_range`
Dec 13, 2019
a681bdd
only test `conform` if scipy installed
Dec 13, 2019
4e62b7c
add `nib-conform` console script
kaczmarj Apr 8, 2020
e19b022
tighten scope of conform function
kaczmarj Apr 8, 2020
12ea136
Merge remote-tracking branch 'upstream/master' into add/fs-conform
kaczmarj Apr 8, 2020
89eedc5
add `nib-conform`
kaczmarj Apr 8, 2020
348f838
use proper labels for orientation
kaczmarj Apr 8, 2020
9491806
add non-3d tests
kaczmarj Apr 8, 2020
a9ce73b
fix style
kaczmarj Apr 8, 2020
3911610
make voxel size and out shape int type
kaczmarj Apr 8, 2020
0d8843b
add tests for `nib-conform` command
kaczmarj Apr 8, 2020
3e4da11
skip tests if scipy not available
kaczmarj Apr 8, 2020
8b712ca
use `nb.save(img, filename)` instead of `img.save(...)`
kaczmarj Apr 8, 2020
67ace2f
keep input class by default in `conform`
kaczmarj Apr 8, 2020
527400d
do not error on non-3d inputs
kaczmarj Apr 8, 2020
6e19298
clean up code
kaczmarj Apr 8, 2020
07fa254
correct the re-orientation of the output image in `conform`
kaczmarj Apr 8, 2020
00825c7
make `to_img` the same image/header classes as input image
kaczmarj Apr 8, 2020
4ca32ba
make pep8 gods happy
kaczmarj Apr 8, 2020
a536ed3
test for errors on non-3d inputs and arguments
kaczmarj Apr 8, 2020
3af4bd8
test that file is not overwritten without `--force`
kaczmarj Apr 8, 2020
3658170
remove bin/nib-conform because it is unused
kaczmarj Apr 8, 2020
eb097f4
copy header from input image in `conform`
kaczmarj Apr 11, 2020
241f58f
NF: Add nibabel.affines.rescale_affine function
effigies Apr 15, 2020
f77fbb5
RF: Update conformation to reorient, rescale and resample
effigies Apr 15, 2020
2177a59
Merge pull request #1 from effigies/add/fs-conform
kaczmarj Apr 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add documentation and fix style
  • Loading branch information
Jakub Kaczmarzyk committed Dec 12, 2019
commit 9895eb09f78275b762b8a046b02a71e668e02c7c
68 changes: 65 additions & 3 deletions nibabel/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,74 @@ def smooth_image(img,


def _transform_range(x, new_min, new_max):
"""Transform data to a new range, while maintaining ratios.

Parameters
----------
x : array-like
The data to transform.
new_min, new_max 8000 : scalar
The minimum and maximum of the output array.

Returns
-------
transformed : array-like
A copy of the transformed data.

Examples
--------
>>> _transform_range([2, 4, 6], -1, 1)
array([-1., 0., 1.])
"""
x = np.asarray(x)
x_min, x_max = x.min(), x.max()
return (((x - x_min) * (new_max - new_min)) / (x_max - x_min)) + new_min


def conform(from_img, out_shape=(256, 256, 256),
voxel_size=(1.0, 1.0, 1.0), order=3, cval=0.0, out_class=Nifti1Image):
def conform(from_img,
out_shape=(256, 256, 256),
voxel_size=(1.0, 1.0, 1.0),
order=3,
cval=0.0,
out_class=Nifti1Image):
""" Resample image to ``out_shape`` with voxels of size ``voxel_size``.

Using the default arguments, this function is meant to replicate most parts
of FreeSurfer's ``mri_convert --conform`` command. The output image is also
reoriented to RAS.

Parameters
----------
from_img : object
Object having attributes ``dataobj``, ``affine``, ``header`` and
``shape``. If `out_class` is not None, ``img.__class__`` should be able
to construct an image from data, affine and header.
out_shape : sequence, optional
The shape of the output volume. Default is (256, 256, 256).
voxel_size : sequence, optional
The size in millimeters of the voxels in the resampled output. Default
is 1mm isotropic.
order : int, optional
The order of the spline interpolation, default is 3. The order has to
be in the range 0-5 (see ``scipy.ndimage.affine_transform``)
mode : str, optional
Points outside the boundaries of the input are filled according
to the given mode ('constant', 'nearest', 'reflect' or 'wrap').
Default is 'constant' 9848 ; (see ``scipy.ndimage.affine_transform``)
cval : scalar, optional
Value used for points outside the boundaries of the input if
``mode='constant'``. Default is 0.0 (see
``scipy.ndimage.affine_transform``)
out_class : None or SpatialImage class, optional
Class of output image. If None, use ``from_img.__class__``.

Returns
-------
out_img : object
Image of instance specified by `out_class`, containing data output from
resampling `from_img` into axes aligned to the output space of
``from_img.affine``
"""
# Create fake image of the image we want to resample to.
hdr = Nifti1Header()
hdr.set_data_shape(out_shape)
Expand All @@ -329,7 +390,8 @@ def conform(from_img, out_shape=(256, 256, 256),
to_img = Nifti1Image(np.empty(out_shape), affine=dst_aff, header=hdr)
# Resample input image.
out_img = resample_from_to(
from_img=from_img, to_vox_map=to_img, order=order, mode="constant", cval=cval, out_class=out_class)
from_img=from_img, to_vox_map=to_img, order=order, mode="constant",
cval=cval, out_class=out_class)
# Cast to uint8.
data = out_img.get_fdata()
data = _transform_range(data, new_min=0.0, new_max=255.0)
Expand Down
0