Table Of Contents

Previous topic

Tutorials

Next topic

Basics of the Coordinate Map

This Page

Basic Data IO

Accessing images using nipy:

Nifti is the primary file format

Load Image from File

from nipy.core.api import load_image
infile = 'myimage.nii'
myimg = load_image(infile)

Access Data into an Array

This allows user to access data in a numpy array.

Note

This is the correct way to access the data as it applies the proper intensity scaling to the image as defined in the header

from nipy.core.api import load_image
import numpy as np
myimg = load_file('myfile')
mydata = np.asarray(myimg)
mydata.shape

Save image to a File

from nipy.core.api import load_image,save_image
import numpy as np
myimg = load_file('myfile.nii')
newimg = save_file(myimg,'newmyfile.nii')

Create Image from an Array

This will have a generic CoordinateMap with Unit step sizes

from nipy.core.api import fromarray, save_image
import numpy as np
rawarray = np.zeros(43,128,128)
innames='kij'
outnames='zyx'
newimg = fromarray(rawarray, innames, outnames)

Images have a Coordinate Map.

The Coordinate Map contains information defining the input and output Coordinate Systems of the image, and the mapping between the two Coordinate systems.

Basics of the Coordinate Map

Here is an examples file image_fromarray.py that shows the use of io and Coordinate Maps.

"""Create a nifti image from a numpy array and an affine transform."""

import os

import numpy as np

from nipy.core.api import fromarray, Affine
from nipy.io.api import save_image, load_image
from nipy.utils.tests.data import repository

# Load an image to get the array and affine
filename = str(repository._fullpath('avg152T1.nii.gz'))
assert os.path.exists(filename)

# Use one of our test files to get an array and affine (as numpy array) from.
img = load_image(filename)
arr = np.asarray(img)
affine_array = img.coordmap.affine.copy()

################################################################################
# START HERE
################################################################################


# 1) Create a CoordinateMap from the affine transform which specifies
# the mapping from input to output coordinates.

# Specify the axis order of the input coordinates
input_coords = ['k', 'j', 'i']
output_coords = ['z','y','x']
#or
innames = ('kji')
outnames = ('zyx')
# either way works

# Build a CoordinateMap to create the image with
affine_coordmap = Affine.from_params(innames, outnames, affine_array)

# 2) Create a nipy image from the array and CoordinateMap

# Create new image
newimg = fromarray(arr, innames=innames, outnames=outnames, 
                                         coordmap=affine_coordmap)

################################################################################
# END HERE, for testing purposes only.
################################################################################
# Imports used just for development and testing.  User's typically
# would not uses these when creating an image.
from tempfile import mkstemp
from nipy.testing import assert_equal

# We use a temporary file for this example so as to not create junk
# files in the nipy directory.
fd, name = mkstemp(suffix='.nii.gz')
tmpfile = open(name)

# Save the nipy image to the specified filename
save_image(newimg, tmpfile.name)


# Reload and verify the affine was saved correctly.
tmpimg = load_image(tmpfile.name)
assert_equal(tmpimg.affine, affine_coordmap.affine)
assert_equal(np.mean(tmpimg), np.mean(img))
assert_equal(np.std(tmpimg), np.std(img))
assert_equal(np.asarray(tmpimg), np.asarray(img))

# cleanup our tempfile
tmpfile.close()
os.unlink(name)