示例#1
0
文件: util.py 项目: slucia/breze
def array_partition_views(array, partition):
    """Return a dictlist with different items corresponding to different sub
    views into an array.

    This function can be used to use different parts of a one-dimensional
    array as a collection of arrays with differing without the need to
    reallocate. E.g the first half of an array might correspond to a matrix,
    the next entry to a scalar and the rest to a vector.

    The function takes two arguments. The first, ``array`` is supposed to
    resemble the flat array, while the second is a dictlist partitioning the
    array into views of given shapes.

    Here, the leaves of ``partition`` will be treated as shapes, of which a
    view into ``array`` should be contained in the result with the same path.


    Parameters
    ----------

    array : np.array or gp.garray
        numpy or gnumpy array of ndim 1.

    partition : dictlist
        Dictlist -- that is a a dictionary containing either lists or dicts as
        its elements, except leave nodes. These should be either tuples or ints
        to represent shapes of arays.


    Examples
    --------

    >>> from breze.arch.util import array_partition_views
    >>> partition = {'first': (2,),
    ...              'second': (2, 3),}
    >>> flat = np.arange(8)
    >>> views = array_partition_views(flat, partition)
    >>> views['first']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([0, 1])
    >>> views['second']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([[2, 3, 4],
               [5, 6, 7]])
    """
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item,) if isinstance(item, (int, long)) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
示例#2
0
文件: util.py 项目: v-chuqin/breze
def array_partition_views(array, partition):
    """Return a dictlist with different items corresponding to different sub
    views into an array.

    This function can be used to use different parts of a one-dimensional
    array as a collection of arrays with differing without the need to
    reallocate. E.g the first half of an array might correspond to a matrix,
    the next entry to a scalar and the rest to a vector.

    The function takes two arguments. The first, ``array`` is supposed to
    resemble the flat array, while the second is a dictlist partitioning the
    array into views of given shapes.

    Here, the leaves of ``partition`` will be treated as shapes, of which a
    view into ``array`` should be contained in the result with the same path.


    Parameters
    ----------

    array : np.array or gp.garray
        numpy or gnumpy array of ndim 1.

    partition : dictlist
        Dictlist -- that is a a dictionary containing either lists or dicts as
        its elements, except leave nodes. These should be either tuples or ints
        to represent shapes of arays.


    Examples
    --------

    >>> from breze.arch.util import array_partition_views
    >>> partition = {'first': (2,),
    ...              'second': (2, 3),}
    >>> flat = np.arange(8)
    >>> views = array_partition_views(flat, partition)
    >>> views['first']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([0, 1])
    >>> views['second']
    ... # doctest: +NORMALIZE_WHITESPACE
        array([[2, 3, 4],
               [5, 6, 7]])
    """
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item, ) if isinstance(item, (int, long)) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
示例#3
0
文件: util.py 项目: k9triz/breze
def array_partition_views(array, partition):
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item,) if isinstance(item, (int, long)) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
示例#4
0
文件: util.py 项目: gabobert/breze
def array_partition_views(array, partition):
    views = dictlist.copy(partition)
    pathsshapes = sorted(list(dictlist.leafs(partition)))

    n_used = 0
    for path, shape in pathsshapes:
        item = dictlist.get(partition, path)
        shape = (item, ) if isinstance(item, int) else item
        size = int(np.prod(shape))
        dictlist.set_(views, path, array[n_used:n_used + size].reshape(shape))
        n_used += size

    return views
示例#5
0
文件: util.py 项目: k9triz/breze
    def _lookup(self, container, ident):
        tensor_types = (theano.tensor.basic.TensorVariable,
                        theano.sandbox.cuda.var.CudaNdarrayVariable)

        if isinstance(ident, tensor_types):
            res = ident
        elif isinstance(ident, tuple):
            res = dictlist.get(container, ident)
        elif isinstance(ident, str):
            res = container[ident]
        else:
            raise ValueError('unrecognized way of pointing to expr')

        return res
示例#6
0
文件: util.py 项目: v-chuqin/breze
    def _lookup(self, container, ident):
        tensor_types = (theano.tensor.basic.TensorVariable,
                        theano.sandbox.cuda.var.CudaNdarrayVariable)

        if isinstance(ident, tensor_types):
            res = ident
        elif isinstance(ident, tuple):
            res = dictlist.get(container, ident)
        elif isinstance(ident, str):
            res = container[ident]
        else:
            raise ValueError('unrecognized way of pointing to expr')

        return res
示例#7
0
def test_dictlist_get():
    tree = make_dictlist()
    assert dictlist.get(tree, ('blu', 'far', 0)) == 1
    assert dictlist.get(tree, ('blu', 'foo')) == (2, 4)
示例#8
0
def test_dictlist_get():
    tree = make_dictlist()
    assert dictlist.get(tree, ('blu', 'far', 0)) == 1
    assert dictlist.get(tree, ('blu', 'foo')) == (2, 4)