arrayproxy

Array proxy base class

The proxy API is - at minimum:

  • The object has a read-only attributeshape

  • read onlyis_proxy attribute / property set to True

  • the object returns the data array fromnp.asarray(prox)

  • returns array slice fromprox[<slice_spec>] where<slice_spec> is anyndarray slice specification that does not use numpy ‘advanced indexing’.

  • modifying no object outsideobj will affect the result ofnp.asarray(obj). Specifically:

    • Changes in position (obj.tell()) of passed file-like objects willnot affect the output of fromnp.asarray(proxy).

    • if you pass a header into the __init__, then modifying the originalheader will not affect the result of the array return.

Seenibabel.tests.test_proxy_api for proxy API conformance checks.

ArrayLike(*args, **kwargs)

Protocol for numpy ndarray-like objects

ArrayProxy(file_like, spec, *[, mmap, ...])

Class to act as proxy for the array that can be read from a file

get_obj_dtype(obj)

Get the effective dtype of an array-like object

is_proxy(obj)

Return True ifobj is an array proxy

reshape_dataobj(obj, shape)

Useobj reshape method if possible, else numpy reshape function

ArrayLike

classnibabel.arrayproxy.ArrayLike(*args,**kwargs)

Bases:Protocol

Protocol for numpy ndarray-like objects

This is more stringent thannumpy.typing.ArrayLike, but guaranteesaccess to shape, ndim and slicing.

__init__(*args,**kwargs)
propertyndim:int
shape:tuple[int,...]

ArrayProxy

classnibabel.arrayproxy.ArrayProxy(file_like,spec,*,mmap=True,order=None,keep_file_open=None)

Bases:ArrayLike

Class to act as proxy for the array that can be read from a file

The array proxy allows us to freeze the passed fileobj and header such thatit returns the expected data array.

This implementation assumes a contiguous array in the file object, with oneof the numpy dtypes, starting at a given file positionoffset withsingleslope andintercept scaling to produce output values.

The class__init__ requires a spec which defines how the data will beread and rescaled. The spec may be a tuple of length 2 - 5, containing theshape, storage dtype, offset, slope and intercept, or aheader objectwith methods:

  • get_data_shape

  • get_data_dtype

  • get_data_offset

  • get_slope_inter

A header should also have a ‘copy’ method. This requirement will go awaywhen the deprecated ‘header’ property goes away.

This implementation allows us to deal with Analyze and its variants,including Nifti1, and with the MGH format.

Other image types might need more specific classes to implement the API.Seenibabel.minc1,nibabel.ecat andnibabel.parrec forexamples.

Initialize array proxy instance

Parameters:
file_likeobject

File-like object or filename. If file-like object, should implementat leastread andseek.

specobject or tuple

Tuple must have length 2-5, with the following values:

  1. shape: tuple - tuple of ints describing shape of data;

  2. storage_dtype: dtype specifier - dtype of array inside proxiedfile, or input tonumpy.dtype to specify array dtype;

  3. offset: int - offset, in bytes, of data array from start of file(default: 0);

  4. slope: float - scaling factor for resulting data (default: 1.0);

  5. inter: float - intercept for rescaled data (default: 0.0).

OR

Header object implementingget_data_shape,get_data_dtype,get_data_offset,get_slope_inter

mmap{True, False, ‘c’, ‘r’}, optional, keyword only

mmap controls the use of numpy memory mapping for reading data.If False, do not try numpymemmap for data array. If one of{‘c’, ‘r’}, try numpy memmap withmode=mmap. Ammap value ofTrue gives the same behavior asmmap='c'. Iffile_likecannot be memory-mapped, ignoremmap value and read array fromfile.

order{None, ‘F’, ‘C’}, optional, keyword only

order controls the order of the data array layout. Fortran-style,column-major order may be indicated with ‘F’, and C-style, row-majororder may be indicated with ‘C’. None gives the default order, thatcomes from the_default_order class variable.

keep_file_open{ None, True, False }, optional, keyword only

keep_file_open controls whether a new file handle is createdevery time the image is accessed, or a single file handle iscreated and used for the lifetime of thisArrayProxy. IfTrue, a single file handle is created and used. IfFalse,a new file handle is created every time the image is accessed.Iffile_like is an open file handle, this setting has noeffect. The default value (None) will result in the value ofKEEP_FILE_OPEN_DEFAULT being used.

__init__(file_like,spec,*,mmap=True,order=None,keep_file_open=None)

Initialize array proxy instance

Parameters:
file_likeobject

File-like object or filename. If file-like object, should implementat leastread andseek.

specobject or tuple

Tuple must have length 2-5, with the following values:

  1. shape: tuple - tuple of ints describing shape of data;

  2. storage_dtype: dtype specifier - dtype of array inside proxiedfile, or input tonumpy.dtype to specify array dtype;

  3. offset: int - offset, in bytes, of data array from start of file(default: 0);

  4. slope: float - scaling factor for resulting data (default: 1.0);

  5. inter: float - intercept for rescaled data (default: 0.0).

OR

Header object implementingget_data_shape,get_data_dtype,get_data_offset,get_slope_inter

mmap{True, False, ‘c’, ‘r’}, optional, keyword only

mmap controls the use of numpy memory mapping for reading data.If False, do not try numpymemmap for data array. If one of{‘c’, ‘r’}, try numpy memmap withmode=mmap. Ammap value ofTrue gives the same behavior asmmap='c'. Iffile_likecannot be memory-mapped, ignoremmap value and read array fromfile.

order{None, ‘F’, ‘C’}, optional, keyword only

order controls the order of the data array layout. Fortran-style,column-major order may be indicated with ‘F’, and C-style, row-majororder may be indicated with ‘C’. None gives the default order, thatcomes from the_default_order class variable.

keep_file_open{ None, True, False }, optional, keyword only

keep_file_open controls whether a new file handle is createdevery time the image is accessed, or a single file handle iscreated and used for the lifetime of thisArrayProxy. IfTrue, a single file handle is created and used. IfFalse,a new file handle is created every time the image is accessed.Iffile_like is an open file handle, this setting has noeffect. The default value (None) will result in the value ofKEEP_FILE_OPEN_DEFAULT being used.

copy()Self

Create a new ArrayProxy for the same file and parameters

If the proxied file is an open file handle, the new ArrayProxywill share a lock with the old one.

propertydtype
get_unscaled()

Read data from file

This is an optional part of the proxy API

propertyinter
propertyis_proxy
propertyndim
propertyoffset
reshape(shape)

Return an ArrayProxy with a new shape, without modifying data

propertyshape
propertyslope

get_obj_dtype

nibabel.arrayproxy.get_obj_dtype(obj)

Get the effective dtype of an array-like object

is_proxy

nibabel.arrayproxy.is_proxy(obj)

Return True ifobj is an array proxy

reshape_dataobj

nibabel.arrayproxy.reshape_dataobj(obj,shape)

Useobj reshape method if possible, else numpy reshape function