Skip to content
Matthew Brett edited this page Mar 26, 2011 · 6 revisions

BIAP2 : Slicecopy

Please see https://github.com/nipy/nibabel/issues#issue/9 for motivation.

Sometimes we have a biiig images and we don't want to load the whole array into memory. In this case it is useful to be able to load as a proxy:

img = load('my_huge_image.nii')

and then take out individual slices, as in something very approximately like:

slice0 = img.get_slice(0)

This is fairly easy to implement, but, a question arises, and that is:

Should slice0 be a copy or a view?

As from the previous discussion - BIAP1 - an image may be a proxy or an array.

If the image is an array, the most natural thing to return is a view. That is, modifying slice0 will modify the underlying array in img.

If the image is a proxy, it would be self-defeating to return a view, because that would involve reading the whole image into memory, exactly what we are trying to avoid. So, for a proxied image, we'd nearly always want to return a copy.

There seem to be two options. The first is to always return a copy. The second is to pass a flag into the routine to signal a copy or a view, with copy=True being the default. The problem about the latter is that copy=False would, perhaps surprisingly, read the whole image into memory.

Clone this wiki locally