def test_dynamic_array_2d_shrink(): for numpy_resize in [True, False]: da = DynamicArray((10, 20), use_numpy_resize=numpy_resize) da[:, :] = np.arange(200).reshape((10, 20)) da.shrink((5, 15)) assert da.shape == (5, 15) # After using shrink, the underlying array should have changed assert da._data.shape == (5, 15) assert_equal(da[:, :], np.arange(15).reshape((1, 15)) + 20*np.arange(5).reshape((5, 1)))
def reinit(self): self._values = dict((v, DynamicArray((0, len(self.indices)), use_numpy_resize=True, dtype=self.source.variables[v].dtype)) for v in self.record_variables) self._t = DynamicArray1D(0, use_numpy_resize=True, dtype=brian_prefs['core.default_scalar_dtype'])
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_2d_resize_up_down(): for numpy_resize in [True, False]: da = DynamicArray((10, 20), use_numpy_resize=numpy_resize) da[:, :] = np.arange(200).reshape((10, 20)) da.resize((15, 20)) assert da.shape == (15, 20) assert_equal(da[10:, :], np.zeros((5, 20))) assert_equal(da[:10, :], np.arange(200).reshape((10, 20))) da.resize((15, 25)) assert da.shape == (15, 25) assert_equal(da[:10, 20:], np.zeros((10, 5))) assert_equal(da[:10, :20], np.arange(200).reshape((10, 20))) da.resize((10, 20)) assert da.shape == (10, 20) assert_equal(da[:, :], np.arange(200).reshape((10, 20)))
def test_dynamic_array_2d_resize_down_up(): for numpy_resize in [True, False]: da = DynamicArray((10, 20), use_numpy_resize=numpy_resize) da[:, :] = np.arange(200).reshape((10, 20)) da.resize((5, 20)) assert da.shape == (5, 20) assert_equal(da, np.arange(100).reshape((5, 20))) da.resize((5, 15)) assert da.shape == (5, 15) for row_idx, row in enumerate(da): assert_equal(row, 20 * row_idx + np.arange(15)) da.resize((10, 20)) assert da.shape == (10, 20) for row_idx, row in enumerate(da[:5, :15]): assert_equal(row, 20 * row_idx + np.arange(15)) assert_equal(da[5:, 15:], 0)
def test_dynamic_array_2d_resize(): for numpy_resize in [True, False]: da = DynamicArray((10, 20), use_numpy_resize=numpy_resize) da[:, :] = np.arange(200).reshape((10, 20)) da.resize((15, 20)) assert da.shape == (15, 20) assert_equal(da[10:, :], np.zeros((5, 20))) assert_equal(da[:10, :], np.arange(200).reshape((10, 20))) da.resize((15, 25)) assert da.shape == (15, 25) assert_equal(da[:10, 20:], np.zeros((10, 5))) assert_equal(da[:10, :20], np.arange(200).reshape((10, 20))) da.resize((10, 20)) assert da.shape == (10, 20) assert_equal(da[:, :], np.arange(200).reshape((10, 20)))