@@ -0,0 +1,2483 @@
+%22%22%22 Common utilities used across active sampling modules in dora%0A%22%22%22%0Aimport numpy as np%0Aimport time%0A%0A%0Aclass ArrayBuffer():%0A    %22%22%22%0A    ArrayBuffer Class%0A%0A    Provides an efficient structure for numpy arrays growing on the first axis.%0A    Implemented with an automatically resising buffer, providing matrices as%0A    views to this data.%0A%0A    Attributes:%0A%0A    %22%22%22%0A    def __init__(self, dims, dtype=float):%0A        %22%22%22%0A        Initialise Buffer%0A%0A        .. note:: Dimensionality of the second axis must be provided a priori.%0A%0A        Parameters%0A        ----------%0A        dims : int%0A            Second dimension of growing array - shape will by (., d)%0A%0A        dtype : type (optional)%0A            Data format of numpy array buffer%0A%0A        %22%22%22%0A        assert(isinstance(dims, int))%0A        self.__buffer = np.zeros((0, dims), dtype)%0A        self.__count = 0%0A%0A    def append(self, value):%0A        %22%22%22%0A        Adds a length d vector to the buffer, extending its first axis by%0A        adding a 1xdims row.%0A%0A        Parameters%0A        ----------%0A        value : array_like%0A            A one dimensional array of length dims consistent with the buffer.%0A        %22%22%22%0A        value = np.asarray(value)%0A        assert(value.ndim == 1)%0A        self.__count += 1%0A        if self.__count %3E= self.__buffer.shape%5B0%5D:%0A            growth_factor = 2.0%0A            newsize = np.floor(growth_factor*self.__buffer.shape%5B0%5D + 2.0)%0A            # make a new buffer - cant change old one in case it is referenced%0A            self.__buffer = np.resize(self.__buffer,%0A                                      (newsize, self.__buffer.shape%5B1%5D))%0A%0A        self.__buffer%5Bself.__count-1%5D = value%0A        return self.__buffer%5B:self.__count%5D%0A%0A%0Adef demo():%0A    d = 20%0A    n_stack = 10000%0A    buf = ArrayBuffer(d)%0A%0A    st = time.time()%0A    for i in range(n_stack):%0A        a = buf.append(np.random.random(d))  # NOQA we dont do anything with a%0A%0A    ft = time.time()%0A    print('Efficient buffer took %7B0:.5f%7D seconds'.format(ft-st))%0A%0A    st = time.time()%0A    b = np.zeros((0, d))%0A    for i in range(n_stack):%0A        b = np.vstack((b, np.random.random(d)))%0A    ft = time.time()%0A    print('Repeated Vstack took %7B0:.5f%7D seconds'.format(ft-st))%0A%0A    st = time.time()%0A    b_buf = %5B%5D%0A    for i in range(n_stack):%0A        b_buf.append(np.random.random(d))%0A        b = np.array(b_buf)%0A    ft = time.time()%0A    print('List buffering and casting took %7B0:.5f%7D seconds'.format(ft-st))%0A%0A    return%0A%0A%0Aif __name__ == '__main__':%0A    demo()%0A

Add numpy array buffer