NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.volumeutils.apply_read_scaling

Next topic

nibabel.volumeutils.array_to_file

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

>>> from StringIO import StringIO #23dt : BytesIO
>>> bio = StringIO() #23dt : BytesIO
>>> arr = np.arange(6).reshape(1,2,3)
>>> _ = bio.write(arr.tostring('F')) # outputs int in python3
>>> arr2 = array_from_file((1,2,3), arr.dtype, bio)
>>> np.all(arr == arr2)
True
>>> bio = StringIO() #23dt : BytesIO
>>> _ = bio.write(' ' * 10) #23dt : bytes
>>> _ = bio.write(arr.tostring('F'))
>>> arr2 = array_from_file((1,2,3), arr.dtype, bio, 10)
>>> np.all(arr == arr2)
True