Skip to content

edgespace

Functions:

  • sq2flat –

    Transform square (symmetric/triu) array(s) into cannonical 1d edge array(s).

  • flat2sq –

    Transform flat array of edge values to a square/symmetric "adjacency" representation

  • binary_feature_edge_cliques –

    returns a generator over the rows of X, projecting node->edgespace

sq2flat

sq2flat(
    A: Shaped[COO | sparray | ndarray, "*batch n n"]
) -> Shaped[COO | sparray | ndarray, "*batch e"]

Transform square (symmetric/triu) array(s) into cannonical 1d edge array(s).

Preserves sparsity using triangular number identities (each m,n pair has a unique linear index).

Currently dispatches for numpy.ndarray, scipy sparse, and pydata's sparse.COO.

Parameters:

  • A (Shaped[COO | sparray | ndarray, '*batch n n']) –

    Square array to unroll

Returns:

  • Shaped[COO | sparray | ndarray, '*batch e'] –

    flattened array of upper-triangle values

Source code in src/affinis/edgespace.py
@dispatch.abstract
def sq2flat(  # type:ignore
    A: Shaped[COO|sparray|np.ndarray, "*batch n n"]
) -> Shaped[COO|sparray|np.ndarray, "*batch e"]: 
    """Transform square (symmetric/triu) array(s) into cannonical 1d edge array(s).

    Preserves sparsity using triangular number identities
    (each m,n pair has a unique linear index).

    Currently dispatches for numpy.ndarray, scipy sparse, and pydata's `sparse.COO`.

    Args:
        A: Square array to unroll

    Returns:
        flattened array of upper-triangle values
    """
    ...

flat2sq

flat2sq(
    A: Shaped[COO | sparray | ndarray, e]
) -> Shaped[COO | sparray | ndarray, "n n"]

Transform flat array of edge values to a square/symmetric "adjacency" representation

Preserves sparsity using triangular number identities (each edge index corresponds to a unique (m,n) pair of indices).

Parameters:

  • e –

    flat array (triangular-number-sized)

Returns:

  • Shaped[COO | sparray | ndarray, 'n n'] –

    Square, symmetric array

Source code in src/affinis/edgespace.py
@dispatch.abstract
def flat2sq(  #type:ignore
    A: Shaped[COO|sparray|np.ndarray, "e"]
) -> Shaped[COO|sparray|np.ndarray, "n n"]: 
    """ Transform flat array of edge values to a square/symmetric "adjacency" representation

    Preserves sparsity using triangular number identities
    (each edge index corresponds to a unique (m,n) pair of indices).

    Args:
        e: flat array (triangular-number-sized)

    Returns:
        Square, symmetric array 
    """
    ...

binary_feature_edge_cliques

binary_feature_edge_cliques(X: FeatMat)

returns a generator over the rows of X, projecting node->edgespace

Using the standard edgespace projection from [[affinis.edgespace.sq2flat]], we can interpret each observation of X as a clique (all pairwise interactions of the nodes in X are also active).

This turns every row of X into a 2D adjacency matrix, and then unrolls each slice's upper triangle to a (longer) 1D array.

Source code in src/affinis/edgespace.py
def binary_feature_edge_cliques(X:FeatMat):
    """returns a generator over the rows of X, projecting node->edgespace

    Using the standard edgespace projection from [[affinis.edgespace.sq2flat]],
    we can interpret each observation of X as a clique (all pairwise interactions
    of the nodes in X are also active).

    This turns every row of X into a 2D adjacency matrix, and then unrolls each
    slice's upper triangle to a (longer) 1D array. 

    """

    X_edgespace = sq2flat(einsum("bi,bo->bio", X, X)).tocsr()
    yield from csr_rows_idx(X_edgespace)

sq_e_ij

sq_e_ij(n: int, e: Idx) -> tuple[Idx, Idx]

Closed-form expression to map edge-to-node-pair indices.

Get a row-column location in the upper-triangle of a symmetric matrix, given the linear index of it's unrolled vector representation.

Parameters:

  • n (int) –

    dimension of square/symmetric matrix

  • e (Idx) –

    index of edge(s) from vector-representation

Returns:

  • tuple[Idx, Idx] –

    pair of row(s),column(s) indices in corresponding matrix.

Source code in src/affinis/edgespace.py
def sq_e_ij(n: int, e: Idx) -> tuple[Idx, Idx]:
    """Closed-form expression to map edge-to-node-pair indices.

    Get a row-column location in the upper-triangle of a symmetric
    matrix, given the linear index of it's unrolled vector representation.

    Parameters:
        n: dimension of square/symmetric matrix
        e: index of edge(s) from vector-representation

    Returns:
        pair of row(s),column(s) indices in corresponding matrix.
    """
    i = n - 2 - np.floor(np.sqrt(-8 * e + 4 * n * (n - 1) - 7) / 2.0 - 0.5)
    j = e + i + 1 - n * (n - 1) / 2 + (n - i) * ((n - i) - 1) / 2
    return i.astype(int), j.astype(int)

sq_ij_e

sq_ij_e(n: int, ij: tuple[Idx, Idx]) -> Idx

Closed-form expression to map edge-to-node-pair indices.

Get an edge index from a row-column index pai upper-triangle of a symmetric matrix.

Parameters:

  • n (int) –

    dimension of square/symmetric matrix

  • ij (tuple[Idx, Idx]) –

    pair of row(s),column(s) indices in corresponding matrix.

Returns:

  • Idx –

    index of edge(s) from vector-representation

Source code in src/affinis/edgespace.py
def sq_ij_e(n: int, ij: tuple[Idx, Idx]) -> Idx:
    """Closed-form expression to map edge-to-node-pair indices.

    Get an edge index from a row-column index pai  upper-triangle of a
    symmetric matrix.

    Parameters:
        n: dimension of square/symmetric matrix
        ij: pair of row(s),column(s) indices in corresponding matrix.

    Returns:
        index of edge(s) from vector-representation
    """
    i, j = ij
    e = (n * (n - 1) / 2) - (n - i) * ((n - i) - 1) / 2 + j - i - 1
    return e.astype(int)

csr_rows_idx

csr_rows_idx(matrix)

Return column indices for data in matrix, per row (empty array if none)

Source code in src/affinis/edgespace.py
def csr_rows_idx(matrix):
    """Return column indices for data in matrix, per row (empty array if none)"""
    rows = matrix.shape[0]
    for index in range(rows):
        indptr_start = matrix.indptr[index]
        indptr_end = matrix.indptr[index + 1]
        # values = matrix.data[indptr_start:indptr_end]
        indices = matrix.indices[indptr_start:indptr_end]
        # func(indices, values)
        yield indices