[Neuroimaging] indexed access to gziped files

Matthew Brett matthew.brett at gmail.com
Mon Mar 14 13:50:10 EDT 2016


Hi,

On Mon, Mar 14, 2016 at 3:51 AM, paul mccarthy <pauldmccarthy at gmail.com> wrote:
> Hi all,
>
>
>> This isn't so if you create an image via the fileobject itself.
>
>
> Matthew, is this currently possible in nibabel? I had a quick play, and poke
> through the code, but I couldn't get anything to work - it looks like there
> is no "from_fileobj" method defined in the Nifti1Image class (or any of its
> bases).

There isn't a `from_fileobj` because some images need more than one
file (like nifti .img / .hdr pairs).

It might be worth adding `from_fileobj` to image types that do need
only one file (like .nii files) - I can't think of any big problems
with that offhand.

At the moment, you have do do this dance:

In [1]: import nibabel as nib
In [2]: fobj = open('my_mri.nii', 'rb')
In [3]: fm = nib.Nifti1Image.make_file_map()
In [6]: fm['image'].fileobj = fobj
In [7]: img = nib.Nifti1Image.from_file_map(fm)
In [8]: img.shape
Out[8]: (2, 3, 4, 4)

> If this is (or will be possible), then then the problem is solved, isn't it?
> Users of nibabel can just create IndexedGzipFile instances themselves, and
> pass the handle to nibabel. No need for nibabel to be dependent upon
> indexed_gzip - the choice would be up to the caller. Or am I missing
> something here?

Sure - that could work, and be easier with a `from_fileobj` method.
But it would involve the user having to use some boilerplate rather
than having it happen automatically via `nib.load`.

Did you have a chance to look into Cython for the wrapping problem?

Cheers,

Matthew


More information about the Neuroimaging mailing list