|
| 1 | +import graphblas as gb |
| 2 | +import networkx as nx |
| 3 | +from graphblas import Matrix, agg, select |
| 4 | +from graphblas.semiring import any_pair, plus_pair |
| 5 | +from networkx import average_clustering as _nx_average_clustering |
| 6 | +from networkx import clustering as _nx_clustering |
| 7 | +from networkx.utils import not_implemented_for |
| 8 | + |
| 9 | +from ._utils import graph_to_adjacency, list_to_mask, vector_to_dict |
| 10 | + |
| 11 | + |
| 12 | +def get_properties(G, names, *, L=None, U=None, degrees=None, has_self_edges=True): |
| 13 | + """Calculate properties of undirected graph""" |
| 14 | + if isinstance(names, str): |
| 15 | + # Separated by commas and/or spaces |
| 16 | + names = [name for name in names.replace(" ", ",").split(",") if name] |
| 17 | + rv = [] |
| 18 | + for name in names: |
| 19 | + if name == "L": |
| 20 | + if L is None: |
| 21 | + L = select.tril(G, -1).new(name="L") |
| 22 | + rv.append(L) |
| 23 | + elif name == "U": |
| 24 | + if U is None: |
| 25 | + U = select.triu(G, 1).new(name="U") |
| 26 | + rv.append(U) |
| 27 | + elif name == "degrees": |
| 28 | + if degrees is None: |
| 29 | + degrees = get_degrees(G, L=L, U=U, has_self_edges=has_self_edges) |
| 30 | + rv.append(degrees) |
| 31 | + elif name == "has_self_edges": |
| 32 | + # Compute if cheap |
| 33 | + if L is not None: |
| 34 | + has_self_edges = G.nvals > 2 * L.nvals |
| 35 | + elif U is not None: |
| 36 | + has_self_edges = G.nvals > 2 * U.nvals |
| 37 | + rv.append(has_self_edges) |
| 38 | + else: |
| 39 | + raise ValueError(f"Unknown property name: {name}") |
| 40 | + if len(rv) == 1: |
| 41 | + return rv[0] |
| 42 | + return rv |
| 43 | + |
| 44 | + |
| 45 | +def get_degrees(G, mask=None, *, L=None, U=None, has_self_edges=True): |
| 46 | + if L is not None: |
| 47 | + has_self_edges = G.nvals > 2 * L.nvals |
| 48 | + elif U is not None: |
| 49 | + has_self_edges = G.nvals > 2 * U.nvals |
| 50 | + if has_self_edges: |
| 51 | + if L is None or U is None: |
| 52 | + L, U = get_properties(G, "L U", L=L, U=U) |
| 53 | + degrees = ( |
| 54 | + L.reduce_rowwise(agg.count).new(mask=mask) + U.reduce_rowwise(agg.count).new(mask=mask) |
| 55 | + ).new(name="degrees") |
| 56 | + else: |
| 57 | + degrees = G.reduce_rowwise(agg.count).new(mask=mask, name="degrees") |
| 58 | + return degrees |
| 59 | + |
| 60 | + |
| 61 | +def single_triangle_core(G, index, *, L=None, has_self_edges=True): |
| 62 | + M = Matrix(bool, G.nrows, G.ncols) |
| 63 | + M[index, index] = True |
| 64 | + C = any_pair(G.T @ M.T).new(name="C") # select.coleq(G.T, index) |
| 65 | + has_self_edges = get_properties(G, "has_self_edges", L=L, has_self_edges=has_self_edges) |
| 66 | + if has_self_edges: |
| 67 | + del C[index, index] # Ignore self-edges |
| 68 | + R = C.T.new(name="R") |
| 69 | + if has_self_edges: |
| 70 | + # Pretty much all the time is spent here taking TRIL, which is used to ignore self-edges |
| 71 | + L = get_properties(G, "L", L=L) |
| 72 | + return plus_pair(L @ R.T).new(mask=C.S).reduce_scalar(allow_empty=False).value |
| 73 | + else: |
| 74 | + return plus_pair(G @ R.T).new(mask=C.S).reduce_scalar(allow_empty=False).value // 2 |
| 75 | + |
| 76 | + |
| 77 | +def triangles_core(G, mask=None, *, L=None, U=None): |
| 78 | + # Ignores self-edges |
| 79 | + L, U = get_properties(G, "L U", L=L, U=U) |
| 80 | + C = plus_pair(L @ L.T).new(mask=L.S) |
| 81 | + return ( |
| 82 | + C.reduce_rowwise().new(mask=mask) |
| 83 | + + C.reduce_columnwise().new(mask=mask) |
| 84 | + + plus_pair(U @ L.T).new(mask=U.S).reduce_rowwise().new(mask=mask) |
| 85 | + ).new(name="triangles") |
| 86 | + |
| 87 | + |
| 88 | +@not_implemented_for("directed") |
| 89 | +def triangles(G, nodes=None): |
| 90 | + if len(G) == 0: |
| 91 | + return {} |
| 92 | + A, key_to_id = graph_to_adjacency(G, dtype=bool) |
| 93 | + if nodes in G: |
| 94 | + return single_triangle_core(A, key_to_id[nodes]) |
| 95 | + mask, id_to_key = list_to_mask(nodes, key_to_id) |
| 96 | + result = triangles_core(A, mask=mask) |
| 97 | + return vector_to_dict(result, key_to_id, id_to_key, mask=mask, fillvalue=0) |
| 98 | + |
| 99 | + |
| 100 | +def total_triangles_core(G, *, L=None, U=None): |
| 101 | + # We use SandiaDot method, because it's usually the fastest on large graphs. |
| 102 | + # For smaller graphs, Sandia method is usually faster: plus_pair(L @ L).new(mask=L.S) |
| 103 | + L, U = get_properties(G, "L U", L=L, U=U) |
| 104 | + return plus_pair(L @ U.T).new(mask=L.S).reduce_scalar(allow_empty=False).value |
| 105 | + |
| 106 | + |
| 107 | +def transitivity_core(G, *, L=None, U=None, degrees=None): |
| 108 | + L, U = get_properties(G, "L U", L=L, U=U) |
| 109 | + numerator = total_triangles_core(G, L=L, U=U) |
| 110 | + if numerator == 0: |
| 111 | + return 0 |
| 112 | + degrees = get_properties(G, "degrees", L=L, U=U, degrees=degrees) |
| 113 | + denom = (degrees * (degrees - 1)).reduce().value |
| 114 | + return 6 * numerator / denom |
| 115 | + |
| 116 | + |
| 117 | +@not_implemented_for("directed") # Should we implement it for directed? |
| 118 | +def transitivity(G): |
| 119 | + if len(G) == 0: |
| 120 | + return 0 |
| 121 | + A = gb.io.from_networkx(G, weight=None, dtype=bool) |
| 122 | + return transitivity_core(A) |
| 123 | + |
| 124 | + |
| 125 | +def clustering_core(G, mask=None, *, L=None, U=None, degrees=None): |
| 126 | + L, U = get_properties(G, "L U", L=L, U=U) |
| 127 | + tri = triangles_core(G, mask=mask, L=L, U=U) |
| 128 | + degrees = get_degrees(G, mask=mask, L=L, U=U) |
| 129 | + denom = degrees * (degrees - 1) |
| 130 | + return (2 * tri / denom).new(name="clustering") |
| 131 | + |
| 132 | + |
| 133 | +def single_clustering_core(G, index, *, L=None, degrees=None, has_self_edges=True): |
| 134 | + has_self_edges = get_properties(G, "has_self_edges", L=L, has_self_edges=has_self_edges) |
| 135 | + tri = single_triangle_core(G, index, L=L, has_self_edges=has_self_edges) |
| 136 | + if tri == 0: |
| 137 | + return 0 |
| 138 | + if degrees is not None: |
| 139 | + degrees = degrees[index].value |
| 140 | + else: |
| 141 | + row = G[index, :].new() |
| 142 | + degrees = row.reduce(agg.count).value |
| 143 | + if has_self_edges and row[index].value is not None: |
| 144 | + degrees -= 1 |
| 145 | + denom = degrees * (degrees - 1) |
| 146 | + return 2 * tri / denom |
| 147 | + |
| 148 | + |
| 149 | +def clustering(G, nodes=None, weight=None): |
| 150 | + if len(G) == 0: |
| 151 | + return {} |
| 152 | + if isinstance(G, nx.DiGraph) or weight is not None: |
| 153 | + # TODO: Not yet implemented. Clustering implemented only for undirected and unweighted. |
| 154 | + return _nx_clustering(G, nodes=nodes, weight=weight) |
| 155 | + A, key_to_id = graph_to_adjacency(G, weight=weight) |
| 156 | + if nodes in G: |
| 157 | + return single_clustering_core(A, key_to_id[nodes]) |
| 158 | + mask, id_to_key = list_to_mask(nodes, key_to_id) |
| 159 | + result = clustering_core(A, mask=mask) |
| 160 | + return vector_to_dict(result, key_to_id, id_to_key, mask=mask, fillvalue=0.0) |
| 161 | + |
| 162 | + |
| 163 | +def average_clustering_core(G, mask=None, count_zeros=True, *, L=None, U=None, degrees=None): |
| 164 | + c = clustering_core(G, mask=mask, L=L, U=U, degrees=degrees) |
| 165 | + val = c.reduce(allow_empty=False).value |
| 166 | + if not count_zeros: |
| 167 | + return val / c.nvals |
| 168 | + elif mask is not None: |
| 169 | + return val / mask.parent.nvals |
| 170 | + else: |
| 171 | + return val / c.size |
| 172 | + |
| 173 | + |
| 174 | +def average_clustering(G, nodes=None, weight=None, count_zeros=True): |
| 175 | + if len(G) == 0 or isinstance(G, nx.DiGraph) or weight is not None: |
| 176 | + # TODO: Not yet implemented. Clustering implemented only for undirected and unweighted. |
| 177 | + return _nx_average_clustering(G, nodes=nodes, weight=weight, count_zeros=count_zeros) |
| 178 | + A, key_to_id = graph_to_adjacency(G, weight=weight) |
| 179 | + mask, _ = list_to_mask(nodes, key_to_id) |
| 180 | + return average_clustering_core(A, mask=mask, count_zeros=count_zeros) |
0 commit comments