Example #1
0
File: util.py Project: k9triz/breze
def n_pars_by_partition(partition):
    n = 0
    for _, shape in dictlist.leafs(partition):
        shape = (shape,) if isinstance(shape, int) else shape
        n += np.prod(shape)

    return int(n)
Example #2
0
File: util.py Project: 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
Example #3
0
def test_dictlist_dfs():
    tree = make_dictlist()
    contents = sorted(list(dictlist.leafs(tree)))

    assert contents[0] == (('bla',), 2)
    assert contents[1] == (('blo', 0), (2, 3))
    assert contents[2] == (('blo', 1), 5)
    assert contents[3] == (('blu', 'far', 0), 1)
    assert contents[4] == (('blu', 'far', 1), 2)
    assert contents[5] == (('blu', 'foo'), (2, 4))
    assert contents[6] == (('blubb',), (2, 3))
Example #4
0
File: util.py Project: 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