src/estimagic/parameters/block_trees.py
def matrix_to_block_tree(matrix, tree1, tree2):
    """Convert a matrix to block-tree.

    A block tree most often arises when one applies an operation to a function that maps
    between two trees. Two main examples are the Jacobian of the function
    f : tree1 -> tree2, which results in a block tree structure, or the covariance
    matrix of a tree, in which case tree1 = tree2.

    Args:
        matrix (numpy.ndarray): 2-d matrix represenation of the block tree.
        tree1: A pytree.
        tree2: A pytree.

    Returns:
        block_tree: A (block) pytree.

    """
    _check_dimensions(matrix, tree1, tree2)

    flat1, treedef1 = tree_flatten(tree1)
    flat2, treedef2 = tree_flatten(tree2)

    flat1_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in flat1]
    flat2_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in flat2]

    shapes1 = [np.shape(a) for a in flat1_np]
    shapes2 = [np.shape(a) for a in flat2_np]

    block_bounds1 = np.cumsum([int(np.product(s)) for s in shapes1[:-1]])
    block_bounds2 = np.cumsum([int(np.product(s)) for s in shapes2[:-1]])

    blocks = []
    for leaf1, s1, submat in zip(
        flat1, shapes1, np.split(matrix, block_bounds1, axis=0)
    ):
        row = []
        for leaf2, s2, block_values in zip(
            flat2, shapes2, np.split(submat, block_bounds2, axis=1)
        ):
            raw_block = block_values.reshape((*s1, *s2))
            block = _convert_raw_block_to_pandas(raw_block, leaf1, leaf2)
            row.append(block)

        blocks.append(row)

    block_tree = tree_unflatten(
        treedef1, [tree_unflatten(treedef2, row) for row in blocks]
    )

    return block_tree
Convert a matrix to block-tree.

    A block tree most often arises when one applies an operation to a function that maps
    between two trees. Two main examples are the Jacobian of the function
    f : tree1 -> tree2, which results in a block tree structure, or the covariance
    matrix of a tree, in which case tree1 = tree2.

    Args:
        matrix (numpy.ndarray): 2-d matrix represenation of the block tree.
        tree1: A pytree.
        tree2: A pytree.

    Returns:
        block_tree: A (block) pytree.

    
Convert a matrix to block-tree.
A block tree most often arises when one applies an operation to a function that maps
between two trees. Two main examples are the Jacobian of the function
f : tree1 -> tree2, which results in a block tree structure, or the covariance
matrix of a tree, in which case tree1 = tree2.
Convert a matrix to block - tree . A block tree most often arises when one applies an operation to a function that maps between two trees . Two main examples are the Jacobian of the function f : tree1 - > tree2 which results in a block tree structure or the covariance matrix of a tree in which case tree1 = tree2 .
def matrix_to_block_tree(matrix, tree1, tree2):
    _check_dimensions(matrix, tree1, tree2)
    flat1, treedef1 = tree_flatten(tree1)
    flat2, treedef2 = tree_flatten(tree2)
    flat1_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in flat1]
    flat2_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in flat2]
    shapes1 = [np.shape(a) for a in flat1_np]
    shapes2 = [np.shape(a) for a in flat2_np]
    block_bounds1 = np.cumsum([int(np.product(s)) for s in shapes1[:-1]])
    block_bounds2 = np.cumsum([int(np.product(s)) for s in shapes2[:-1]])
    blocks = []
    for leaf1, s1, submat in zip(
        flat1, shapes1, np.split(matrix, block_bounds1, axis=0)
    ):
        row = []
        for leaf2, s2, block_values in zip(
            flat2, shapes2, np.split(submat, block_bounds2, axis=1)
        ):
            raw_block = block_values.reshape((*s1, *s2))
            block = _convert_raw_block_to_pandas(raw_block, leaf1, leaf2)
            row.append(block)
        blocks.append(row)
    block_tree = tree_unflatten(
        treedef1, [tree_unflatten(treedef2, row) for row in blocks]
    )
    return block_tree
def matrix_to_block_tree ( matrix , tree1 , tree2 ) : _check_dimensions ( matrix , tree1 , tree2 ) flat1 , treedef1 = tree_flatten ( tree1 ) flat2 , treedef2 = tree_flatten ( tree2 ) flat1_np = [ _convert_pandas_objects_to_numpy ( leaf ) for leaf in flat1 ] flat2_np = [ _convert_pandas_objects_to_numpy ( leaf ) for leaf in flat2 ] shapes1 = [ np . shape ( a ) for a in flat1_np ] shapes2 = [ np . shape ( a ) for a in flat2_np ] block_bounds1 = np . cumsum ( [ int ( np . product ( s ) ) for s in shapes1 [ : - 1 ] ] ) block_bounds2 = np . cumsum ( [ int ( np . product ( s ) ) for s in shapes2 [ : - 1 ] ] ) blocks = [ ] for leaf1 , s1 , submat in zip ( flat1 , shapes1 , np . split ( matrix , block_bounds1 , axis = 0 ) ) : row = [ ] for leaf2 , s2 , block_values in zip ( flat2 , shapes2 , np . split ( submat , block_bounds2 , axis = 1 ) ) : raw_block = block_values . reshape ( ( * s1 , * s2 ) ) block = _convert_raw_block_to_pandas ( raw_block , leaf1 , leaf2 ) row . append ( block ) blocks . append ( row ) block_tree = tree_unflatten ( treedef1 , [ tree_unflatten ( treedef2 , row ) for row in blocks ] ) return block_tree
Convert a matrix to block-tree.
Convert a matrix to block - tree .
"""Convert a matrix to block-tree.

    A block tree most often arises when one applies an operation to a function that maps
    between two trees. Two main examples are the Jacobian of the function
    f : tree1 -> tree2, which results in a block tree structure, or the covariance
    matrix of a tree, in which case tree1 = tree2.

    Args:
        matrix (numpy.ndarray): 2-d matrix represenation of the block tree.
        tree1: A pytree.
        tree2: A pytree.

    Returns:
        block_tree: A (block) pytree.

    """
A (block) pytree.
A ( block ) pytree .
block_tree
matrix
2-d matrix represenation of the block tree.
2 - d matrix represenation of the block tree .
tree1
None
tree2
None