[Pkg-exppsy-pynifti] Image, design, usecases

Matthew Brett matthew.brett at gmail.com
Mon May 11 22:24:17 UTC 2009


Me again, a second time.

As a result of discussions with Fernando, how about:

import StringIO as StringIO
import numpy as np
data = np.zeros((2,3,4))
affine = np.eye(4)

img1 = Nifti1Image(data, affine, filespec='my_image')
assert img1.get_filename() == 'my_image.nii'
img1.save() # no error, files already set
str_io = StringIO()
files = {'image': str_io, 'header': str_io}

img2 = Nifti1Image(data, affine, filespec=files)
assert img2.get_filename() is None
img2.save() # no error, files already set

img3 = Nifti1Image(data, affine)
img3.save() # raises runtime error of some sort

img3.save('my_image') # OK
img3.save(files) # OK
img3.set_filespec('my_image')
assert img3.get_filename() == 'my_image.nii'
img3.save() # obviously OK

Underneath all this we might have a FileSpec object or function that
accepted filenames, dictionaries, tuples, and file-like objects in
some standard way, and that would deal with the Image.__init__ case,
and set_filespec, and save.

> The question to me is really whether we want to handle cases like
> 'somefile.hdr + crazyblob.exe', although that would technically be
> possible.

No, right, I'm not that interested in that rather opaque use-case, but
I am interested in the ability to pass in file-like objects.  I seem
to remember that's already been a feature request for pynifti - no?
And it's useful for testing.  And it opens up an interfacey way to
work with non-filesystem targets like databases.

How's the above look to y'all?

Matthew



More information about the Pkg-exppsy-pynifti mailing list