File: basic_io.rst

package info (click to toggle)
nipy 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,352 kB
  • sloc: python: 39,115; ansic: 30,931; makefile: 210; sh: 93
file content (90 lines) | stat: -rw-r--r-- 2,343 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
78
79
80
81
82
83
84
85
86
87
88
89
90
.. basic_data_io:

===============
 Basic Data IO
===============

Accessing images using nipy:

While Nifti_ is the primary file format Analyze images (with associated .mat
file), and MINC files can also be read.

Load Image from File
====================

Get a filename for an example file.  ``anatfile`` gives a filename for a small
testing image in the nipy distribution:

>>> from nipy.testing import anatfile

Load the file from disk:

>>> from nipy import load_image
>>> myimg = load_image(anatfile)
>>> myimg.shape
(33, 41, 25)
>>> myimg.affine
array([[ -2.,   0.,   0.,  32.],
       [  0.,   2.,   0., -40.],
       [  0.,   0.,   2., -16.],
       [  0.,   0.,   0.,   1.]])

Access Data into an Array
=========================

This allows the user to access data as a numpy array.

>>> mydata = myimg.get_fdata()
>>> mydata.shape
(33, 41, 25)
>>> mydata.ndim
3

Save image to a File
====================

>>> from nipy import save_image
>>> newimg = save_image(myimg, 'newmyfile.nii')

Create Image from an Array
===========================

This will have a generic affine-type CoordinateMap with unit voxel sizes.

>>> import numpy as np
>>> from nipy.core.api import Image, vox2mni
>>> rawarray = np.zeros((43,128,128))
>>> arr_img = Image(rawarray, vox2mni(np.eye(4)))
>>> arr_img.shape
(43, 128, 128)

Coordinate map
==============

Images have a Coordinate Map.

The Coordinate Map contains information defining the input (domain) and output
(range) Coordinate Systems of the image, and the mapping between the two
Coordinate systems.  The *input* coordinate system is the *voxel* coordinate
system, and the *output* coordinate system is the *world* coordinate system.

>>> newimg.coordmap
AffineTransform(
   function_domain=CoordinateSystem(coord_names=('i', 'j', 'k'), name='voxels', coord_dtype=float64),
   function_range=CoordinateSystem(coord_names=('aligned-x=L->R', 'aligned-y=P->A', 'aligned-z=I->S'), name='aligned', coord_dtype=float64),
   affine=array([[ -2.,   0.,   0.,  32.],
                 [  0.,   2.,   0., -40.],
                 [  0.,   0.,   2., -16.],
                 [  0.,   0.,   0.,   1.]])
)

See :ref:`coordinate_map` for more detail.

.. testcleanup::

    # Delete the file we wrote out further up the page.
    import os

    os.unlink('newmyfile.nii')

.. include:: ../links_names.txt