forked from nipy/nibabel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: add tests against SPM resampling
Build some test images by resampling with SPM, and test our resampling against SPM's resampling.
- Loading branch information
1 parent
e1795b9
commit ca44287
Showing
9 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
anat_moved.nii | ||
resampled_functional.nii |
Binary file not shown.
Binary file not shown.
This file contains 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,22 @@ | ||
""" Make anatomical image with altered affine | ||
* Add some rotations and translations to affine; | ||
* Save as ``.nii`` file so SPM can read it. | ||
See ``resample_using_spm.m`` for processing of this generated image by SPM. | ||
""" | ||
|
||
import numpy as np | ||
|
||
import nibabel as nib | ||
from nibabel.eulerangles import euler2mat | ||
from nibabel.affines import from_matvec | ||
|
||
img = nib.load('anatomical.nii') | ||
some_rotations = euler2mat(0.1, 0.2, 0.3) | ||
extra_affine = from_matvec(some_rotations, [3, 4, 5]) | ||
moved_anat = nib.Nifti1Image(img.dataobj, | ||
extra_affine.dot(img.affine), | ||
img.header) | ||
moved_anat.set_data_dtype(np.float32) | ||
nib.save(moved_anat, 'anat_moved.nii') |
Binary file not shown.
This file contains 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,15 @@ | ||
% Script uses SPM to resample moved anatomical image. | ||
% | ||
% Run `python make_moved_anat.py` to generate file to work on. | ||
% | ||
% Run from the directory containing this file. | ||
% Works with Octave or MATLAB. | ||
% Needs SPM (5, 8 or 12) on the MATLAB path. | ||
P = {'functional.nii', 'anat_moved.nii'}; | ||
% Resample without masking | ||
flags = struct('mask', false, 'mean', false, ... | ||
'interp', 1, 'which', 1, ... | ||
'prefix', 'resampled_'); | ||
spm_reslice(P, flags); | ||
% Reorient to canonical orientation at 4mm resolution, polynomial interpolation | ||
to_canonical({'anat_moved.nii'}, 4, 'reoriented_', 1); |
Binary file not shown.
This file contains 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,61 @@ | ||
function to_canonical(imgs, vox_sizes, prefix, hold) | ||
% Resample images to canonical (transverse) orientation with given voxel sizes | ||
% | ||
% Inspired by ``reorient.m`` by John Ashburner: | ||
% http://blogs.warwick.ac.uk/files/nichols/reorient.m | ||
% | ||
% Parameters | ||
% ---------- | ||
% imgs : char or cell array or struct array | ||
% Images to resample to canonical orientation. | ||
% vox_sizes : vector (3, 1), optional | ||
% Voxel sizes for output image. | ||
% prefix : char, optional | ||
% Prefix for output resampled images, default = 'r' | ||
% hold : float, optional | ||
% Hold (resampling method) value, default = 3. | ||
|
||
if ~isstruct(imgs) | ||
imgs = spm_vol(imgs); | ||
end | ||
if nargin < 2 | ||
vox_sizes = [1 1 1]; | ||
elseif numel(vox_sizes) == 1 | ||
vox_sizes = [vox_sizes vox_sizes vox_sizes]; | ||
end | ||
vox_sizes = vox_sizes(:); | ||
if nargin < 3 | ||
prefix = 'r'; | ||
end | ||
if nargin < 4 | ||
hold = 3; | ||
end | ||
|
||
for vol_no = 1:numel(imgs) | ||
vol = imgs{vol_no}(1); | ||
% From: | ||
% http://stackoverflow.com/questions/4165859/generate-all-possible-combinations-of-the-elements-of-some-vectors-cartesian-pr | ||
sets = {[1, vol.dim(1)], [1, vol.dim(2)], [1, vol.dim(3)]}; | ||
[x y z] = ndgrid(sets{:}); | ||
corners = [x(:) y(:) z(:)]; | ||
corner_coords = [corners ones(length(corners), 1)]'; | ||
corner_mm = vol.mat * corner_coords; | ||
min_xyz = min(corner_mm(1:3, :), [], 2); | ||
max_xyz = max(corner_mm(1:3, :), [], 2); | ||
% Make output volume | ||
out_vol = vol; | ||
out_vol.private = []; | ||
out_vol.mat = diag([vox_sizes' 1]); | ||
out_vol.mat(1:3, 4) = min_xyz - vox_sizes; | ||
out_vol.dim(1:3) = ceil((max_xyz - min_xyz) ./ vox_sizes) + 1; | ||
[dpath, froot, ext] = fileparts(vol.fname); | ||
out_vol.fname = fullfile(dpath, [prefix froot ext]); | ||
out_vol = spm_create_vol(out_vol); | ||
% Resample original volume at output volume grid | ||
plane_size = out_vol.dim(1:2); | ||
for slice_no = 1:out_vol.dim(3) | ||
resamp_affine = inv(spm_matrix([0 0 -slice_no]) * inv(out_vol.mat) * vol.mat); | ||
slice_vals = spm_slice_vol(vol, resamp_affine, plane_size, hold); | ||
out_vol = spm_write_plane(out_vol, slice_vals, slice_no); | ||
end | ||
end |
This file contains 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