DynamicArray class

(Shortest import: from brian2.memory.dynamicarray import DynamicArray)

class brian2.memory.dynamicarray.DynamicArray(shape, dtype=<class 'float'>, factor=2, use_numpy_resize=False, refcheck=True)[source]

Bases: object

An N-dimensional dynamic array class

The array can be resized in any dimension, and the class will handle allocating a new block of data and copying when necessary.

Warning

The data will NOT be contiguous for >1D arrays. To ensure this, you will either need to use 1D arrays, or to copy the data, or use the shrink method with the current size (although note that in both cases you negate the memory and efficiency benefits of the dynamic array).

Initialisation arguments:

shape, dtype
The shape and dtype of the array to initialise, as in Numpy. For 1D arrays, shape can be a single int, for ND arrays it should be a tuple.
factor
The resizing factor (see notes below). Larger values tend to lead to more wasted memory, but more computationally efficient code.
use_numpy_resize, refcheck
Normally, when you resize the array it creates a new array and copies the data. Sometimes, it is possible to resize an array without a copy, and if this option is set it will attempt to do this. However, this can cause memory problems if you are not careful so the option is off by default. You need to ensure that you do not create slices of the array so that no references to the memory exist other than the main array object. If you are sure you know what you’re doing, you can switch this reference check off. Note that resizing in this way is only done if you resize in the first dimension.

The array is initialised with zeros. The data is stored in the attribute data which is a Numpy array.

Some numpy methods are implemented and can work directly on the array object, including len(arr), arr[...] and arr[...]=.... In other cases, use the data attribute.

Notes

The dynamic array returns a data attribute which is a view on the larger _data attribute. When a resize operation is performed, and a specific dimension is enlarged beyond the size in the _data attribute, the size is increased to the larger of cursize*factor and newsize. This ensures that the amortized cost of increasing the size of the array is O(1).

Examples

>>> x = DynamicArray((2, 3), dtype=int)
>>> x[:] = 1
>>> x.resize((3, 3))
>>> x[:] += 1
>>> x.resize((3, 4))
>>> x[:] += 1
>>> x.resize((4, 4))
>>> x[:] += 1
>>> x.data[:] = x.data**2
>>> x.data
array([[16, 16, 16,  4],
       [16, 16, 16,  4],
       [ 9,  9,  9,  4],
       [ 1,  1,  1,  1]])

Methods

resize(newshape) Resizes the data to the new shape, which can be a different size to the current data, but should have the same rank, i.e.
resize_along_first(newshape)
shrink(newshape) Reduces the data to the given shape, which should be smaller than the current shape.

Details

resize(newshape)[source]

Resizes the data to the new shape, which can be a different size to the current data, but should have the same rank, i.e. same number of dimensions.

resize_along_first(newshape)[source]
shrink(newshape)[source]

Reduces the data to the given shape, which should be smaller than the current shape. resize() can also be used with smaller values, but it will not shrink the allocated memory, whereas shrink will reallocate the memory. This method should only be used infrequently, as if it is used frequently it will negate the computational efficiency benefits of the DynamicArray.