File: image_fromarray.py

package info (click to toggle)
nipy 0.1.2%2B20100526-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,992 kB
  • ctags: 13,434
  • sloc: python: 47,720; ansic: 41,334; makefile: 197
file content (77 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""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 import make_datasource

# Make the templates datasource
templates = make_datasource('nipy', 'templates')

# Load an image to get the array and affine
filename = templates.get_filename('ICBM152', '2mm', 'T1.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.  Users 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)