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:
objectAn 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,dtypeThe 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.
factorThe resizing factor (see notes below). Larger values tend to lead to more wasted memory, but more computationally efficient code.
use_numpy_resize,refcheckNormally, 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
datawhich is a Numpy array.Some numpy methods are implemented and can work directly on the array object, including
len(arr),arr[...]andarr[...]=.... In other cases, use thedataattribute.Notes
The dynamic array returns a
dataattribute which is a view on the larger_dataattribute. When a resize operation is performed, and a specific dimension is enlarged beyond the size in the_dataattribute, the size is increased to the larger ofcursize*factorandnewsize. 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. same number of dimensions.
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.
-
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, whereasshrinkwill 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.