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

    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:
        block_tree: A (block) pytree, must match dimensions of tree1 and tree2.
        tree1: A pytree.
        tree2: A pytree.

    Returns:
        matrix (np.ndarray): 2d array containing information stored in block_tree.

    """
    if len(block_tree) != len(tree1):
        raise ValueError("First dimension of block_tree does not match that of tree1.")

    selector_first_element = list(block_tree)[0] if isinstance(block_tree, dict) else 0
    if len(block_tree[selector_first_element]) != len(tree2):
        raise ValueError("Second dimension of block_tree does not match that of tree2.")

    flat1 = tree_leaves(tree1)
    flat2 = tree_leaves(tree2)

    flat_block_tree = tree_leaves(block_tree)

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

    size1 = [np.size(a) for a in flat1_np]
    size2 = [np.size(a) for a in flat2_np]

    n_blocks1 = len(size1)
    n_blocks2 = len(size2)

    block_rows_raw = [
        flat_block_tree[n_blocks2 * i : n_blocks2 * (i + 1)] for i in range(n_blocks1)
    ]

    block_rows = []
    for s1, row in zip(size1, block_rows_raw):
        shapes = [(s1, s2) for s2 in size2]
        row_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in row]
        row_reshaped = _reshape_list_to_2d(row_np, shapes)
        row_concatenated = np.concatenate(row_reshaped, axis=1)
        block_rows.append(row_concatenated)

    matrix = np.concatenate(block_rows, axis=0)

    _check_dimensions(matrix, tree1, tree2)
    return matrix
Convert a block tree to a matrix.

    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:
        block_tree: A (block) pytree, must match dimensions of tree1 and tree2.
        tree1: A pytree.
        tree2: A pytree.

    Returns:
        matrix (np.ndarray): 2d array containing information stored in block_tree.

    
Convert a block tree to a matrix.
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 block tree to a matrix . 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 block_tree_to_matrix(block_tree, tree1, tree2):
    if len(block_tree) != len(tree1):
        raise ValueError("First dimension of block_tree does not match that of tree1.")
    selector_first_element = list(block_tree)[0] if isinstance(block_tree, dict) else 0
    if len(block_tree[selector_first_element]) != len(tree2):
        raise ValueError("Second dimension of block_tree does not match that of tree2.")
    flat1 = tree_leaves(tree1)
    flat2 = tree_leaves(tree2)
    flat_block_tree = tree_leaves(block_tree)
    flat1_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in flat1]
    flat2_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in flat2]
    size1 = [np.size(a) for a in flat1_np]
    size2 = [np.size(a) for a in flat2_np]
    n_blocks1 = len(size1)
    n_blocks2 = len(size2)
    block_rows_raw = [
        flat_block_tree[n_blocks2 * i : n_blocks2 * (i + 1)] for i in range(n_blocks1)
    ]
    block_rows = []
    for s1, row in zip(size1, block_rows_raw):
        shapes = [(s1, s2) for s2 in size2]
        row_np = [_convert_pandas_objects_to_numpy(leaf) for leaf in row]
        row_reshaped = _reshape_list_to_2d(row_np, shapes)
        row_concatenated = np.concatenate(row_reshaped, axis=1)
        block_rows.append(row_concatenated)
    matrix = np.concatenate(block_rows, axis=0)
    _check_dimensions(matrix, tree1, tree2)
    return matrix
def block_tree_to_matrix ( block_tree , tree1 , tree2 ) : if len ( block_tree ) != len ( tree1 ) : raise ValueError ( "First dimension of block_tree does not match that of tree1." ) selector_first_element = list ( block_tree ) [ 0 ] if isinstance ( block_tree , dict ) else 0 if len ( block_tree [ selector_first_element ] ) != len ( tree2 ) : raise ValueError ( "Second dimension of block_tree does not match that of tree2." ) flat1 = tree_leaves ( tree1 ) flat2 = tree_leaves ( tree2 ) flat_block_tree = tree_leaves ( block_tree ) flat1_np = [ _convert_pandas_objects_to_numpy ( leaf ) for leaf in flat1 ] flat2_np = [ _convert_pandas_objects_to_numpy ( leaf ) for leaf in flat2 ] size1 = [ np . size ( a ) for a in flat1_np ] size2 = [ np . size ( a ) for a in flat2_np ] n_blocks1 = len ( size1 ) n_blocks2 = len ( size2 ) block_rows_raw = [ flat_block_tree [ n_blocks2 * i : n_blocks2 * ( i + 1 ) ] for i in range ( n_blocks1 ) ] block_rows = [ ] for s1 , row in zip ( size1 , block_rows_raw ) : shapes = [ ( s1 , s2 ) for s2 in size2 ] row_np = [ _convert_pandas_objects_to_numpy ( leaf ) for leaf in row ] row_reshaped = _reshape_list_to_2d ( row_np , shapes ) row_concatenated = np . concatenate ( row_reshaped , axis = 1 ) block_rows . append ( row_concatenated ) matrix = np . concatenate ( block_rows , axis = 0 ) _check_dimensions ( matrix , tree1 , tree2 ) return matrix
Convert a block tree to a matrix.
Convert a block tree to a matrix .
"""Convert a block tree to a matrix.

    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:
        block_tree: A (block) pytree, must match dimensions of tree1 and tree2.
        tree1: A pytree.
        tree2: A pytree.

    Returns:
        matrix (np.ndarray): 2d array containing information stored in block_tree.

    """
matrix (np.ndarray): 2d array containing information stored in block_tree.
matrix ( np . ndarray ) : 2d array containing information stored in block_tree .
block_tree
A (block) pytree, must match dimensions of tree1 and tree2.
A ( block ) pytree must match dimensions of tree1 and tree2 .
tree1
None
tree2
None