NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.volumeutils.allopen

Next topic

nibabel.volumeutils.array_to_file

This Page

Reggie -- the one

nibabel.volumeutils.array_from_file

nibabel.volumeutils.array_from_file(shape, in_dtype, infile, offset=0, order='F')

Get array from file with specified shape, dtype and file offset

Parameters :

shape : sequence

sequence specifying output array shape

in_dtype : numpy dtype

fully specified numpy dtype, including correct endianness

infile : file-like

open file-like object implementing at least read() and seek()

offset : int, optional

offset in bytes into infile to start reading array data. Default is 0

order : {‘F’, ‘C’} string

order in which to write data. Default is ‘F’ (fortran order).

Returns :

arr : array-like

array like object that can be sliced, containing data

Examples

>>> import StringIO
>>> str_io = StringIO.StringIO()
>>> arr = np.arange(6).reshape(1,2,3)
>>> str_io.write(arr.tostring('F'))
>>> arr2 = array_from_file((1,2,3), arr.dtype, str_io)
>>> np.all(arr == arr2)
True
>>> str_io = StringIO.StringIO()
>>> str_io.write(' ' * 10)
>>> str_io.write(arr.tostring('F'))
>>> arr2 = array_from_file((1,2,3), arr.dtype, str_io, 10)
>>> np.all(arr == arr2)
True