def test_dynamic_array_1d_shrink(): for numpy_resize in [True, False]: da = DynamicArray1D(10, use_numpy_resize=numpy_resize) da[:] = np.arange(10) da.shrink(5) assert len(da) == 5 assert all(da[:] == np.arange(5)) # After using shrink, the underlying array should have changed assert len(da._data) == 5
def test_dynamic_array_2d_access(): da = DynamicArray1D((10, 20)) da[:, :] = np.arange(200).reshape((10, 20)) assert da[5, 10] == 5 * 20 + 10 assert da.shape == (10, 20) assert len(str(da)) assert len(repr(da)) da[:] += 1 da.data[:] += 1 assert_equal(da[:, :], np.arange(200).reshape((10, 20)) + 2)
def create_all_to_all(N, dt): ''' Return a tuple containing `synapses` and `delays` in the form that is needed for the `SpikeQueue` initializer. Every synapse has a delay depending on the presynaptic neuron. ''' data = np.repeat(np.arange(N, dtype=np.int32), N) delays = DynamicArray1D(data.shape, dtype=np.float64) delays[:] = data * dt synapses = data return synapses, delays
def add_array(self, var): # This creates the actual numpy arrays (or DynamicArrayVariable objects) if isinstance(var, DynamicArrayVariable): if var.ndim == 1: arr = DynamicArray1D(var.size, dtype=var.dtype) else: arr = DynamicArray(var.size, dtype=var.dtype) else: arr = np.empty(var.size, dtype=var.dtype) self.arrays[var] = arr
def test_dynamic_array_1d_access(): da = DynamicArray1D(10) da[:] = np.arange(10) assert da[7] == 7 assert len(da) == 10 assert da.shape == (10, ) assert len(str(da)) assert len(repr(da)) da[:] += 1 da.data[:] += 1 assert all(da[:] == (np.arange(10) + 2))
def test_dynamic_array_1d_resize_down_up(): for numpy_resize in [True, False]: da = DynamicArray1D(10, use_numpy_resize=numpy_resize) da[:] = np.arange(10) da.resize(5) assert len(da) == 5 assert da.shape == (5, ) assert all(da[:5] == np.arange(5)) da.resize(10) assert len(da) == 10 assert da.shape == (10, ) assert all(da[:5] == np.arange(5)) assert all(da[5:] == 0)