%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
import numpy as np
import matplotlib.pyplot as plt
import os
MNI_PATH = 'mni_icbm152_nlin_asym_09a'
os.listdir(MNI_PATH)
['mni_icbm152_csf_tal_nlin_asym_09a.nii', 'mni_icbm152_gm_tal_nlin_asym_09a.nii', 'mni_icbm152_pd_tal_nlin_asym_09a.nii', 'mni_icbm152_t1_tal_nlin_asym_09a.nii', 'mni_icbm152_t1_tal_nlin_asym_09a_eye_mask.nii', 'mni_icbm152_t1_tal_nlin_asym_09a_face_mask.nii', 'mni_icbm152_t1_tal_nlin_asym_09a_mask.nii', 'mni_icbm152_t2_tal_nlin_asym_09a.nii', 'mni_icbm152_wm_tal_nlin_asym_09a.nii']
t1_fname = os.path.join(MNI_PATH, 'mni_icbm152_t1_tal_nlin_asym_09a.nii')
import antsutils
import nipy
import nipy.core.api as nca
import nipy.algorithms.resample as nar
/Users/mb312/usr/local/lib/python2.6/site-packages/nose-1.1.3.dev-py2.6.egg/nose/plugins/manager.py:405: UserWarning: Module nipy was already imported from /Users/mb312/usr/local/lib/python2.6/site-packages/nipy/__init__.pyc, but /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages is being added to sys.path import pkg_resources
bb = [[-78, 78], [-112, 76], [-50, 85]] # SPM default
vox = [2, 2, 2]
out_shape, out_affine = antsutils.bb2imgdef(bb, vox)
out_shape
(79, 95, 69)
Resample to the desired bounding box
out_cmap = nca.vox2mni(out_affine)
inter_map = np.eye(4)
big_template = nipy.load_image(t1_fname)
small_template = nar.resample(big_template, out_cmap, inter_map, out_shape)
small_template.shape
(79, 95, 69)
small_template.affine
array([[ -2., -0., -0., 78.], [ 0., 2., 0., -112.], [ 0., 0., 2., -50.], [ 0., 0., 0., 1.]])
plt.imshow(big_template.get_data()[:, :, 90], cmap="gray")
<matplotlib.image.AxesImage at 0x5d12510>
big_to_small = nca.compose(small_template.coordmap.inverse(), big_template.coordmap)
big_to_small
AffineTransform( function_domain=CoordinateSystem(coord_names=('i', 'j', 'k'), name='voxels', coord_dtype=float64), function_range=CoordinateSystem(coord_names=('i', 'j', 'k'), name='voxels', coord_dtype=float64), affine=array([[ -0.5, 0. , 0. , 88. ], [ 0. , 0.5, 0. , -11. ], [ 0. , 0. , 0.5, -11. ], [ 0. , 0. , 0. , 1. ]]) )
Which slice corresponds to 90 in the small template?
big_to_small([0, 0, 90])
array([ 88., -11., 34.])
plt.imshow(small_template.get_data()[:, :, 34], cmap="gray")
<matplotlib.image.AxesImage at 0x5d50f10>
nipy.save_image(small_template, 'small_template.nii.gz')
/Users/mb312/usr/local/lib/python2.6/site-packages/nipy/io/files.py:137: FutureWarning: Default `strict` currently False; this will change to True in a future version of nipy ni_img = nipy2nifti(img, data_dtype = io_dtype)
Image( data=array([[[ 9.255049, 9.062705, ..., 6.277446, 5.843554], [ 9.120856, 9.381788, ..., 6.662134, 6.557761], ..., [ 8.850978, 8.625831, ..., 7.837072, 7.462821], [ 8.836068, 8.506548, ..., 7.717789, 7.662621]], [[ 8.815193, 8.937458, ..., 5.93898 , 5.971783], [ 8.909128, 8.865888, ..., 6.720284, 6.717302], ..., [ 8.660125, 8.421559, ..., 7.692441, 7.57614 ], [ 8.76748 , 8.585573, ..., 7.483696, 7.820671]], ..., [[ 8.94044 , 8.551279, ..., 6.753087, 6.407166], [ 8.764498, 8.843523, ..., 7.155668, 6.858951], ..., [ 7.544828, 7.978721, ..., 7.586578, 7.349502], [ 7.783395, 7.602979, ..., 7.03042 , 7.373359]], [[ 8.913602, 9.117874, ..., 6.571181, 6.521976], [ 9.193917, 9.052268, ..., 7.052786, 7.146721], ..., [ 8.112914, 7.902678, ..., 7.370377, 7.154176], [ 7.796814, 7.932498, ..., 7.641746, 7.483696]]]), coordmap=AffineTransform( function_domain=CoordinateSystem(coord_names=('i', 'j', 'k'), name='voxels', coord_dtype=float64), function_range=CoordinateSystem(coord_names=('mni-x=L->R', 'mni-y=P->A', 'mni-z=I->S'), name='mni', coord_dtype=float64), affine=array([[ -2., -0., -0., 78.], [ 0., 2., 0., -112.], [ 0., 0., 2., -50.], [ 0., 0., 0., 1.]]) ))