nipy/nibabel — pull request #211 by matthew-brett (2013-11-15 17:02:59+00:00)
Title: MRG: add proxy slicing

Long awaited API to be able to do arbitrary slicing on array proxies from images.

Meaning, you can slice data from images without loading the whole image from disk.

Of course this can save memory, but it can also be much faster. For an
uncompressed nifti image:

In [17]: timeit img = nib.load(fname1); arr = img.dataobj[..., 120]
1000 loops, best of 3: 1.7 ms per loop

In [19]: timeit img = nib.load(fname1); arr = img.get_data()[..., 120]
1 loops, best of 3: 713 ms per loop

For a gzipped image, the savings depend on the position in the file:

In [8]: timeit img = nib.load(fname); arr = img.dataobj[..., 2]
10 loops, best of 3: 30.7 ms per loop

In [7]: timeit img = nib.load(fname); arr = img.get_data()[..., 2]
1 loops, best of 3: 1.32 s per loop

In [12]: timeit img = nib.load(fname); arr = img.dataobj[..., 40]
1 loops, best of 3: 466 ms per loop

In [11]: timeit img = nib.load(fname); arr = img.get_data()[..., 40]
1 loops, best of 3: 1.31 s per loop

In [13]: timeit img = nib.load(fname); arr = img.dataobj[..., 100]
1 loops, best of 3: 1.15 s per loop

In [14]: timeit img = nib.load(fname); arr = img.dataobj[..., 120]
1 loops, best of 3: 1.38 s per loop