vector_structure¶
Lightweight utilities for working with structured flat vectors and block matrices in NumPy.
- class vector_structure.VectorStructure(sizes: Sequence[tuple[BlockName, int]])¶
Helper class to describe block vectors and block matrices.
- cuts¶
dict[BlockName, slice] – the indices for each named block
- size¶
int – the total size of the described vector
- __getitem__(idx: BlockName | tuple[BlockName, ...] | slice) slice¶
Get the indices for one or multiple blocks.
Example
>>> from vector_structure import VectorStructure >>> x = VectorStructure([("x", 3), ("y", 2), ("z", 1)]) >>> li = [1, 2, 3, 4, 5, 6] >>> li[x["y"]] [4, 5] >>> li[x["y":"z"]] [4, 5, 6] >>> li[x["x","y"]] [1, 2, 3, 4, 5]
Note
when indexing by slice, both the start and the stop are included, i.e. a[“a”:”c”] = a[“a”, “b”, “c”]. This is different from the Python convention, but similar to Pandas.
- __init__(sizes: Sequence[tuple[BlockName, int]])¶
- __repr__()¶
Return repr(self).
- __weakref__¶
list of weak references to the object (if defined)
- as_dict(arr: SpecificArrayLike) dict[BlockName, SpecificArrayLike]¶
Split the array into pieces according to the vector structure.
Inverse operation to
from_dict().Example
>>> from vector_structure import VectorStructure >>> x = VectorStructure([("x", 3), ("y", 1)]) >>> x.as_dict([1, 2, 3, 4]) {'x': [1, 2, 3], 'y': [4]}
- block_size(idx: BlockName | tuple[BlockName, ...] | slice) int¶
Return the size of one or more blocks.
- Parameters:
idx – Index. See
__getitem__().
This is just syntactic sugar for simple_slice_len(self[idxs])
- vector_structure.simple_slice_len(sl: slice) int¶
Compute the size of a ‘simple’ slice.
A ‘simple’ slice is a slice with (step=None or step=1) and start >= 0 and stop >= 0. VectorStructure[…] always returns simple slices. This function is just syntactic sugar for sl.stop - sl.start.
- Parameters:
sl – “Simple” slice
- Raises:
ValueError – If the slice is not a “simple” slice.
- Returns:
The length of the slice, i.e. len(li[sl]) for a container li.