SPM and Python¶
Differences between MATLAB and Python¶
Data structures available in MATLAB behave differently from those often used in Python.
|
MATLAB |
Python |
|---|---|
|
1-indexing; |
0-indexing; |
|
Arrays and scalars are instances of |
Python defines builtin scalar types |
|
The size of an array, returned by |
The shape of a NumPy array is one of its |
|
Arrays have an infinite number of singleton |
Dimensions are explicit: arrays with |
|
When an operator acts on arrays with |
When an operator acts on arrays with |
|
Indexing multidimensional arrays is |
Indexing multidimensional arrays is |
|
By default, literals have type |
Literals that have a decimal separator are of |
|
Cells (which can contain elements of any |
Lists can contain elements of any type, and |
|
Similarly, structures can be structure |
Dictionaries are always scalar. |
|
The fields of a structure are accessed using |
Dictionaries are accessed with the indexing |
|
MATLAB structures can be implicitly defined |
The SPM-Python type system¶
Internally, SPM makes advanced use of MATLAB types; it behaves differently whether its inputs are cells of structures or structures or cells, and dimensions have a semantic meaning. This makes communicating between SPM and Python challenging. To enable this communication, we have implemented a dedicated Python type system that can easily be converted to (and from) MATLAB types. Out aim, when designing this type system, was to create a type system with a pythonic feels, and that would not throw off Python users. At the same type, we have tried to reach a one-to-one mapping between this new type system and MATLAB types, and support an extended set of features that ease the translation of MATLAB scripts into Python. Therefore:
- We implement
Array,CellandStructtypes that can all behave as arrays, as well as scalars (Array,Struct) or one dimensional lists (Cell). - All types are based off (and inherit)
np.ndarray. - We use numpy-like indexing (0-indexing, implicit dimension indexing, broadcasting to the left).
- We support implicit resizing when indexing out-of-bounds.
- Structure fields can be indexed using either dot (
x.field) or bracket (x["field"]) indexing. - A scalar (0-dimensional)
Structimplements theMappingprotocol. - A row (1-dimensional)
Cellimplements theListprotocol.
Examples¶
The Array class is mostly inter-changeable with the numpy.ndarray class,
and pure NumPy arrays can be passed to SPM functions that expect a numeric
array. The Array class adds implicit indexing, such that the following
(self-explanatory) snippet works:
>>> x = Array([0]) # Define an array with shape [0] -- it is empty
>>> x[1] = 1 # Assign value 1 at index 3. Intermediate indices are filled with zeros
>>> print(x)
[0.0, 1.0]
>>> print(repr(x))
Array([0., 1.])
Failure
Note the, contrary to matlab, only one-dimensional implicit indexing is supported. It is therefore not possible to do
Implicit indexing is also supported by types Cell and Struct:
>>> c = Cell()
>>> c[1] = 1
>>> print(c)
[[], 1]
>>> print(repr(c))
Cell([Array([]), 1])
>>> s = Struct()
>>> s[1].field = 1
>>> print(s)
[{'field': Array([])}, {'field': 1}]
>>> print(repr(s))
Struct([{'field': Array([])}, {'field': 1}])
Implicit indexing can be deeply nested, a feature that is commonly used in MATLAB to write SPM batch jobs. For example this SPM-MATLAB batch line
would be written
realign_estimate_reslice = Struct()
realign_estimate_reslice.matlabbatch(0).spm.spatial.realign.estwrite.eoptions.quality = 0.9
in SPM-Python. Note that we use round brackets (matlabbatch(0).spm)
instead of square brackets (matlabbatch[0].spm) to specify that matlabbatch
should be a cell that contains a struct, and not a struct array.
In order to implement this nesting of implicitly indexed structured,
we had to delay the creation of each object in the hierarchy, as the final
type of an object is only known after it has been indexed. To do this,
we introduced a delayed type that is originally generic, and metamorphoses
itself into a Struct or Cell or Array as it is being used. Our logic
is documented in the DelayedArray section.
Array¶
Array implements numeric arrays, compatible with matlab arrays.
# Instantiate from size
Array(N, M, ...)
Array([N, M, ...])
Array.from_shape([N, M, ...])
# Instantiate from existing numeric array
Array(other_array)
Array.from_any(other_array)
# Other options
Array(..., dtype=None, order=None, *, copy=None, owndata=None)
Warning
Lists or vectors of integers can be interpreted as shapes or as
numeric arrays to copy. They are interpreted as shapes by the
Array constructor. To ensure that they are interpreted as
arrays to copy, use Array.from_any.
@classmethod
def from_shape(cls: Array, shape=tuple(), **kwargs) -> Array:
"""
Build an array of a given shape.
Parameters
----------
shape : list[int]
Shape of new array.
Other Parameters
----------------
dtype : np.dtype | None, default='double'
Target data type.
order : {"C", "F"} | None, default=None
Memory layout.
* "C" : row-major (C-style);
* "F" : column-major (Fortran-style).
Returns
-------
array : Array
New array.
"""
...
@classmethod
def from_any(cls: Array, other, **kwargs) -> Array:
"""
Convert an array-like object to a numeric array.
Parameters
----------
other : ArrayLike
object to convert.
Other Parameters
----------------
dtype : np.dtype | None, default=None
Target data type. Guessed if `None`.
order : {"C", "F", "A", "K"} | None, default=None
Memory layout.
* `"C"` : row-major (C-style);
* `"F"` : column-major (Fortran-style);
* `"A"` : (any) `"F"` if a is Fortran contiguous, `"C"` otherwise;
* `"K"` : (keep) preserve input order;
* `None`: preserve input order if possible, `"C"` otherwise.
copy : bool | None, default=None
Whether to copy the underlying data.
* `True` : the object is copied;
* `None` : the the object is copied only if needed;
* `False`: raises a `ValueError` if a copy cannot be avoided.
owndata : bool, default=None
If `True`, ensures that the returned `Array` owns its data.
This may trigger an additional copy.
Returns
-------
array : Array
Converted array.
"""
...
@classmethod
def from_cell(cls: Array, other: Cell, **kwargs) -> Array:
"""
Convert a `Cell` to a numeric `Array`.
Parameters
----------
other : Cell
Cell to convert.
Other Parameters
----------------
dtype : np.dtype | None, default=None
Target data type. Guessed if `None`.
order : {"C", "F", "A", "K"} | None, default="K"
Memory layout.
* `"C"` : row-major (C-style);
* `"F"` : column-major (Fortran-style);
* `"A"` : (any) `"F"` if a is Fortran contiguous, `"C"` otherwise;
* `"K"` : (keep) preserve input order.
owndata : bool, default=None
If `True`, ensures that the returned `Array` owns its data.
This may trigger an additional copy.
Returns
-------
array : Array
Converted array.
"""
...
SparseArray¶
SparseArray implements sparse arrays.
- If
scipyis available, it uses ascipy.sparse.csc_arraybackend. - Otherwise, it uses a dense backend.
# Instantiate from size
SparseArray(N, M, ...)
SparseArray([N, M, ...])
SparseArray.from_shape([N, M, ...])
# Instantiate from existing sparse or dense array
SparseArray(other_array)
SparseArray.from_any(other_array)
# Other options
SparseArray(..., dtype=None, *, copy=None)
Warning
Lists or vectors of integers can be interpreted as shapes
or as dense arrays to copy. They are interpreted as shapes
by the SparseArray constructor. To ensure that they are
interpreted as dense arrays to copy, usse SparseArray.from_any.
@classmethod
def from_coo(cls: SparseArray, values, indices, shape=None, **kw) -> SparseArray:
"""
Build a sparse array from indices and values.
Parameters
----------
values : (N,) ArrayLike
Values to set at each index.
indices : (D, N) ArrayLike
Indices of nonzero elements.
shape : list[int] | None
Shape of the array.
dtype : np.dtype | None
Target data type. Same as `values` by default.
Returns
-------
array : SparseArray
New array.
"""
...
@classmethod
def from_shape(cls: SparseArray, shape=tuple(), **kwargs) -> SparseArray:
"""
Build an array of a given shape.
Parameters
----------
shape : list[int]
Shape of the new array.
Other Parameters
----------------
dtype : np.dtype | None, default='double'
Target data type.
Returns
-------
array : SparseArray
New array.
"""
...
@classmethod
def from_any(cls: SparseArray, other, **kwargs) -> SparseArray:
"""
Convert an array-like object to a numeric array.
Parameters
----------
other : ArrayLike
object to convert.
Other Parameters
----------------
dtype : np.dtype | None, default=None
Target data type. Guessed if `None`.
copy : bool | None, default=None
Whether to copy the underlying data.
* `True` : the object is copied;
* `None` : the the object is copied only if needed;
* `False`: raises a `ValueError` if a copy cannot be avoided.
Returns
-------
array : SparseArray
Converted array.
"""
...
Cell¶
Cell implements cell arrays, compatible with matlab cells.
# Instantiate from size
Cell(N, M, ...)
Cell([N, M, ...])
Cell.from_shape([N, M, ...])
# Instantiate from existing (cell-like) array (implicitely)
Cell(cell_like)
Cell.from_any(cell_like)
# Other options
Cell(..., order=None, *, copy=None, owndata=None, deepcat=False)
A cell is a MutableSequence and therefore (mostly) behaves like a list.
It implements the following methods (which all operate along the
1st dimension, if the cell is a cell array):
append: Append object to the end of the cellclear: Empty the cellcount: Number of occurrences of a valueextend: Extend list by appending elements from the iterableindex: First index of a valueinsert: Insert object before indexpop: Remove and return item at indexremove: Remove first occurrence of valuereverse: Reverse the cell in-placesort: Sort the list in ascending order in-place
The magic operators + and * also operate as in lists:
a + b: Concatenateaandba += b: Append iterablebtoaa * n: Concatenatenrepeats ofaa *= n: Appendnrepeats ofatoa
Finally, elements (along the first dimension) can be deleted using
del cell[index].
Warning
Lists or vectors of integers can be interpreted as shapes or as
cell-like objects to copy. They are interpreted as shapes by the
Cell constructor. To ensure that they are interpreted as
arrays to co~~py, use Cell.from_any.
@classmethod
def from_shape(cls: Cell, shape=tuple(), **kwargs) -> Cell:
"""
Build a cell array of a given size.
Parameters
----------
shape : list[int]
Input shape.
Other Parameters
----------------
order : {"C", "F"} | None, default="C"
Memory layout.
* `"C"` : row-major (C-style);
* `"F"` : column-major (Fortran-style).
Returns
-------
cell : Cell
New cell array.
"""
...
@classmethod
def from_any(cls: Cell, other, **kwargs) -> Cell:
"""
Convert a (nested) list-like object to a cell.
Parameters
----------
other : CellLike
object to convert.
Other Parameters
----------------
deepcat : bool, default=False
Convert cells of cells into cell arrays.
order : {"C", "F", "A", "K"} | None, default=None
Memory layout.
* `"C"` : row-major (C-style);
* `"F"` : column-major (Fortran-style);
* `"A"` : (any) `"F"` if a is Fortran contiguous, `"C"` otherwise;
* `"K"` : (keep) preserve input order;
* `None`: preserve input order if possible, `"C"` otherwise.
copy : bool | None, default=None
Whether to copy the underlying data.
* `True` : the object is copied;
* `None` : the the object is copied only if needed;
* `False`: raises a `ValueError` if a copy cannot be avoided.
owndata : bool, default=False
If `True`, ensures that the returned `Cell` owns its data.
This may trigger an additional copy.
Returns
-------
cell : Cell
Converted cell.
"""
...
Struct¶
Struct implement structure/dictionary arrays, compatible with matlab structs.
# Instantiate from size
Struct(N, M, ...)
Struct([N, M, ...])
Struct.from_shape([N, M, ...])
# Instantiate from existing struct array
# (or list/cell of dictionaries)
Struct(struct_like)
Struct.from_any(struct_like)
# Instantiate from dictionary
Struct(a=x, b=y)
Struct({"a": x, "b": y})
Struct.from_any({"a": x, "b": y})
The following field names correspond to existing attributes or
methods of Struct objects and are therefore protected. They can
still be used as field names, but only through the dictionary syntax
(s["shape"]), not the dot syntax (s.shape):
ndim -> int: number of dimensionsshape -> list[int]: array shapesize -> int: number of elementsreshape() -> Struct: struct array with a different shapekeys() -> list[str]: field namesvalues() -> list: values (per key)items() -> [str, list]: (key, value) pairsget() -> list: value (per element)setdefault(): sets default value for field nameupdate(): update fields from dictionary-likeas_num -> raise: interpret object as a numeric arrayas_cell -> raise: interpret object as a cell arrayas_struct -> Struct: interpret object as a struct arrayas_dict() -> dict: convert to plain dictionaryfrom_shape() -> Struct: build a new empty structfrom_any() -> Struct: build a new struct by (shallow) copyfrom_cell() -> Struct: build a new struct by (shallow) copy
The following field names are protected because they have a special meaning in the python language. They can still be used as field names through the dictionary syntax:
as |
assert |
break |
class |
continue |
def |
del |
elif |
else |
except |
False |
finally |
for |
from |
global |
if |
import |
in |
is |
lambda |
None |
nonlocal |
not |
or |
pass |
raise |
return |
True |
try |
while |
with |
yield |
@classmethod
def from_shape(cls: Struct, shape=tuple(), **kwargs) -> Struct:
"""
Build a struct array of a given size.
Parameters
----------
shape : list[int]
Input shape.
Other Parameters
----------------
order : {"C", "F"} | None, default="C"
Memory layout.
* `"C"` : row-major (C-style);
* `"F"` : column-major (Fortran-style).
Returns
-------
struct : Struct
New struct array.
"""
...
@classmethod
def from_any(cls: Struct, other, **kwargs) -> Struct:
"""
* Convert a dict-like object to struct; or
* Convert an array of dict-like objects to a struct array.
Parameters
----------
other : DictLike | ArrayLike[DictLike]
object to convert.
Other Parameters
----------------
order : {"C", "F", "A", "K"} | None, default=None
Memory layout.
* `"C"` : row-major (C-style);
* `"F"` : column-major (Fortran-style);
* `"A"` : (any) `"F"` if a is Fortran contiguous, `"C"` otherwise;
* `"K"` : (keep) preserve input order;
* `None`: preserve input order if possible, `"C"` otherwise.
copy : bool | None, default=None
Whether to copy the underlying data.
* `True` : the object is copied;
* `None` : the the object is copied only if needed;
* `False`: raises a `ValueError` if a copy cannot be avoided.
owndata : bool, default=None
If `True`, ensures that the returned `Struct` owns its data.
This may trigger an additional copy.
Returns
-------
struct : Struct
Converted structure.
"""
...
AnyDelayedArray¶
A AnyDelayedArray is an object that we return when we don’t know how
an indexed element will be used yet.
It decides whether it is a Struct, Cell or Array based on the type of indexing that is used.
In Matlab:
a(x,y) = numindicates thatais a numeric array;a(x,y) = cellindicates thatais a cell array;a{x,y} = anyindicates thatais a cell array;a(x,y).f = anyindicates thatais a struct array;a.f = anyindicates thatais a struct.
These indexing operations can be chained, so in
a(x).b.c{y}.d(z) = 2:
ais a struct array;bis a struct;cis a cell;c{y}is a structdis a numeric array.
In Python, there is only one type of indexing ([]). This is a problem as
we cannot differentiate a{x}.b = y — where a is a cell that contains
a struct — from a(x).b = y — where a is a struct array.
One solution may be to abuse the “call” operator (), so that it returns a
cell. This would work in some situations (a[x].b = y is a struct array,
whereas a(x).b = y is a cell of struct). However, the statement
a(x) = y (which would correspond to matlab’s a{x} = y) is not valid
python syntax. Furthermore, it would induce a new problem, as cells could
not be differentiated from function handles, in some cases.
Instead, the use of brackets automatically transforms the object into either:
- a
Struct(in all “get” cases, and in the “set” contexta[x] = y, whenyis either adictor aStruct); or - an
Array(in the “set” contexta[x] = y, whenyis neither adictnor aStruct).
Alternatively, if the user wishes to specify which type the object should
take, we implement the properties as_cell, as_struct and as_num.
Therefore:
a[x,y] = num:ais a numeric array;a[x,y] = struct:ais a numeric array;a[x,y].f = any:ais a struct array;a(x,y).f = any:ais a cell array containing a struct;a.f = any:ais a struct.
And explicitly:
a.as_cell[x,y] = any:ais a cell array;a.as_struct[x,y].f = any:ais a struct array;a.as_cell[x,y].f = any:ais a cell array containing a struct;a.as_num[x,y] = num:ais a numeric array.