(
    klass, matrix, mode=ADJ_DIRECTED, attr="weight", loops=True
)
return klass(
        nvert, edges=edges, directed=(mode == "directed"), edge_attrs={attr: weights}
    )
Construct graph from sparse matrix, weighted

    NOTE: Of course, you cannot emcompass a fully general weighted multigraph
    with a single adjacency matrix, so we don't try to do it here either.
Construct graph from sparse matrix, weighted
Construct graph from sparse matrix weighted
def _graph_from_weighted_sparse_matrix(
    klass, matrix, mode=ADJ_DIRECTED, attr="weight", loops=True
):
    """Construct graph from sparse matrix, weighted

    NOTE: Of course, you cannot emcompass a fully general weighted multigraph
    with a single adjacency matrix, so we don't try to do it here either.
    """
    # This function assumes there is scipy and the matrix is a scipy sparse
    # matrix. The caller should make sure those conditions are met.
    from scipy import sparse

    if not isinstance(matrix, sparse.coo_matrix):
        matrix = matrix.tocoo()

    nvert = max(matrix.shape)
    if min(matrix.shape) != nvert:
        raise ValueError("Matrix must be square")

    # Shorthand notation
    m = matrix

    mode = _convert_mode_argument(mode)

    if mode == "directed":
        if not loops:
            edges, weights = [], []
            for i, j, n in zip(m.row, m.col, m.data):
                if i != j:
                    edges.append((i, j))
                    weights.append(n)
        else:
            edges = list(zip(m.row, m.col))
            weights = list(m.data)

    elif mode in ("max", "plus"):
        fun = max if mode == "max" else add
        nedges = {}
        for i, j, n in zip(m.row, m.col, m.data):
            if i == j and not loops:
                continue
            pair = (i, j) if i < j else (j, i)
            nedges[pair] = fun(nedges.get(pair, 0), n)

        edges, weights = zip(*nedges.items())

    elif mode == "min":
        tmp = {(i, j): n for i, j, n in zip(m.row, m.col, m.data)}

        nedges = {}
        for pair, weight in tmp.items():
            i, j = pair
            if i == j and loops:
                nedges[pair] = weight
            elif i < j:
                nedges[pair] = min(weight, tmp.get((j, i), 0))

        edges, weights = [], []
        for pair in sorted(nedges.keys()):
            weight = nedges[pair]
            if weight != 0:
                edges.append(pair)
                weights.append(nedges[pair])

    elif mode == "upper":
        edges, weights = [], []
        for i, j, n in zip(m.row, m.col, m.data):
            if j > i or (loops and j == i):
                edges.append((i, j))
                weights.append(n)

    elif mode == "lower":
        edges, weights = [], []
        for i, j, n in zip(m.row, m.col, m.data):
            if j < i or (loops and j == i):
                edges.append((i, j))
                weights.append(n)

    else:
        raise ValueError("invalid mode")

    return klass(
        nvert, edges=edges, directed=(mode == "directed"), edge_attrs={attr: weights}
    )
def _graph_from_weighted_sparse_matrix ( klass , matrix , mode = ADJ_DIRECTED , attr = "weight" , loops = True ) : # This function assumes there is scipy and the matrix is a scipy sparse # matrix. The caller should make sure those conditions are met. from scipy import sparse if not isinstance ( matrix , sparse . coo_matrix ) : matrix = matrix . tocoo ( ) nvert = max ( matrix . shape ) if min ( matrix . shape ) != nvert : raise ValueError ( "Matrix must be square" ) # Shorthand notation m = matrix mode = _convert_mode_argument ( mode ) if mode == "directed" : if not loops : edges , weights = [ ] , [ ] for i , j , n in zip ( m . row , m . col , m . data ) : if i != j : edges . append ( ( i , j ) ) weights . append ( n ) else : edges = list ( zip ( m . row , m . col ) ) weights = list ( m . data ) elif mode in ( "max" , "plus" ) : fun = max if mode == "max" else add nedges = { } for i , j , n in zip ( m . row , m . col , m . data ) : if i == j and not loops : continue pair = ( i , j ) if i < j else ( j , i ) nedges [ pair ] = fun ( nedges . get ( pair , 0 ) , n ) edges , weights = zip ( * nedges . items ( ) ) elif mode == "min" : tmp = { ( i , j ) : n for i , j , n in zip ( m . row , m . col , m . data ) } nedges = { } for pair , weight in tmp . items ( ) : i , j = pair if i == j and loops : nedges [ pair ] = weight elif i < j : nedges [ pair ] = min ( weight , tmp . get ( ( j , i ) , 0 ) ) edges , weights = [ ] , [ ] for pair in sorted ( nedges . keys ( ) ) : weight = nedges [ pair ] if weight != 0 : edges . append ( pair ) weights . append ( nedges [ pair ] ) elif mode == "upper" : edges , weights = [ ] , [ ] for i , j , n in zip ( m . row , m . col , m . data ) : if j > i or ( loops and j == i ) : edges . append ( ( i , j ) ) weights . append ( n ) elif mode == "lower" : edges , weights = [ ] , [ ] for i , j , n in zip ( m . row , m . col , m . data ) : if j < i or ( loops and j == i ) : edges . append ( ( i , j ) ) weights . append ( n ) else : raise ValueError ( "invalid mode" ) return klass ( nvert , edges = edges , directed = ( mode == "directed" ) , edge_attrs = { attr : weights } )