#4212 include analytics to hits and pagerank #4178

Open popokatapepel popokatapepel
Coverage Reach
algorithms/flow/networksimplex.py algorithms/flow/preflowpush.py algorithms/flow/boykovkolmogorov.py algorithms/flow/capacityscaling.py algorithms/flow/shortestaugmentingpath.py algorithms/flow/edmondskarp.py algorithms/flow/utils.py algorithms/flow/dinitz_alg.py algorithms/flow/maxflow.py algorithms/flow/gomory_hu.py algorithms/flow/mincost.py algorithms/flow/__init__.py algorithms/isomorphism/ismags.py algorithms/isomorphism/isomorphvf2.py algorithms/isomorphism/matchhelpers.py algorithms/isomorphism/temporalisomorphvf2.py algorithms/isomorphism/tree_isomorphism.py algorithms/isomorphism/isomorph.py algorithms/isomorphism/vf2userfunc.py algorithms/isomorphism/__init__.py algorithms/centrality/betweenness.py algorithms/centrality/flow_matrix.py algorithms/centrality/betweenness_subset.py algorithms/centrality/load.py algorithms/centrality/current_flow_betweenness.py algorithms/centrality/subgraph_alg.py algorithms/centrality/closeness.py algorithms/centrality/katz.py algorithms/centrality/group.py algorithms/centrality/current_flow_betweenness_subset.py algorithms/centrality/reaching.py algorithms/centrality/trophic.py algorithms/centrality/eigenvector.py algorithms/centrality/dispersion.py algorithms/centrality/percolation.py algorithms/centrality/voterank_alg.py algorithms/centrality/second_order.py algorithms/centrality/current_flow_closeness.py algorithms/centrality/degree_alg.py algorithms/centrality/__init__.py algorithms/centrality/harmonic.py algorithms/connectivity/edge_augmentation.py algorithms/connectivity/connectivity.py algorithms/connectivity/edge_kcomponents.py algorithms/connectivity/cuts.py algorithms/connectivity/kcomponents.py algorithms/connectivity/kcutsets.py algorithms/connectivity/disjoint_paths.py algorithms/connectivity/stoerwagner.py algorithms/connectivity/utils.py algorithms/connectivity/__init__.py algorithms/bipartite/generators.py algorithms/bipartite/matching.py algorithms/bipartite/projection.py algorithms/bipartite/edgelist.py algorithms/bipartite/basic.py algorithms/bipartite/cluster.py algorithms/bipartite/centrality.py algorithms/bipartite/matrix.py algorithms/bipartite/spectral.py algorithms/bipartite/redundancy.py algorithms/bipartite/covering.py algorithms/bipartite/__init__.py algorithms/shortest_paths/weighted.py algorithms/shortest_paths/unweighted.py algorithms/shortest_paths/generic.py algorithms/shortest_paths/astar.py algorithms/shortest_paths/dense.py algorithms/shortest_paths/__init__.py algorithms/tree/branchings.py algorithms/tree/mst.py algorithms/tree/coding.py algorithms/tree/operations.py algorithms/tree/decomposition.py algorithms/tree/recognition.py algorithms/tree/__init__.py algorithms/community/modularity_max.py algorithms/community/lukes.py algorithms/community/asyn_fluid.py algorithms/community/quality.py algorithms/community/label_propagation.py algorithms/community/kernighan_lin.py algorithms/community/kclique.py algorithms/community/centrality.py algorithms/community/__init__.py algorithms/community/community_utils.py algorithms/approximation/kcomponents.py algorithms/approximation/connectivity.py algorithms/approximation/treewidth.py algorithms/approximation/clique.py algorithms/approximation/steinertree.py algorithms/approximation/dominating_set.py algorithms/approximation/clustering_coefficient.py algorithms/approximation/ramsey.py algorithms/approximation/__init__.py algorithms/approximation/vertex_cover.py algorithms/approximation/independent_set.py algorithms/approximation/matching.py algorithms/planarity.py algorithms/threshold.py algorithms/coloring/equitable_coloring.py algorithms/coloring/greedy_coloring_with_interchange.py algorithms/coloring/greedy_coloring.py algorithms/coloring/__init__.py algorithms/matching.py algorithms/simple_paths.py algorithms/operators/product.py algorithms/operators/binary.py algorithms/operators/all.py algorithms/operators/unary.py algorithms/operators/__init__.py algorithms/traversal/depth_first_search.py algorithms/traversal/breadth_first_search.py algorithms/traversal/edgedfs.py algorithms/traversal/edgebfs.py algorithms/traversal/beamsearch.py algorithms/traversal/__init__.py algorithms/components/strongly_connected.py algorithms/components/biconnected.py algorithms/components/connected.py algorithms/components/weakly_connected.py algorithms/components/attracting.py algorithms/components/semiconnected.py algorithms/components/__init__.py algorithms/similarity.py algorithms/dag.py algorithms/link_analysis/pagerank_alg.py algorithms/link_analysis/hits_alg.py algorithms/link_analysis/__init__.py algorithms/cycles.py algorithms/planar_drawing.py algorithms/clique.py algorithms/distance_measures.py algorithms/assortativity/mixing.py algorithms/assortativity/connectivity.py algorithms/assortativity/correlation.py algorithms/assortativity/pairs.py algorithms/assortativity/neighbor_degree.py algorithms/assortativity/__init__.py algorithms/lowest_common_ancestors.py algorithms/graphical.py algorithms/smallworld.py algorithms/chordal.py algorithms/cluster.py algorithms/__init__.py algorithms/euler.py algorithms/swap.py algorithms/core.py algorithms/triads.py algorithms/node_classification/hmn.py algorithms/node_classification/lgc.py algorithms/node_classification/utils.py algorithms/node_classification/__init__.py algorithms/regular.py algorithms/sparsifiers.py algorithms/link_prediction.py algorithms/minors.py algorithms/hybrid.py algorithms/tournament.py algorithms/structuralholes.py algorithms/asteroidal.py algorithms/bridges.py algorithms/cuts.py algorithms/dominance.py algorithms/distance_regular.py algorithms/communicability_alg.py algorithms/graph_hashing.py algorithms/chains.py algorithms/richclub.py algorithms/d_separation.py algorithms/reciprocity.py algorithms/efficiency_measures.py algorithms/covering.py algorithms/dominating.py algorithms/mis.py algorithms/non_randomness.py algorithms/boundary.py algorithms/wiener.py algorithms/moral.py algorithms/voronoi.py algorithms/vitality.py algorithms/isolate.py algorithms/hierarchy.py algorithms/smetric.py generators/random_graphs.py generators/social.py generators/degree_seq.py generators/community.py generators/joint_degree_seq.py generators/classic.py generators/internet_as_graphs.py generators/line.py generators/geometric.py generators/small.py generators/directed.py generators/lattice.py generators/nonisomorphic_trees.py generators/harary_graph.py generators/spectral_graph_forge.py generators/duplication.py generators/expanders.py generators/trees.py generators/atlas.py generators/sudoku.py generators/random_clustered.py generators/intersection.py generators/__init__.py generators/mycielski.py generators/interval_graph.py generators/ego.py generators/stochastic.py generators/cographs.py generators/triads.py readwrite/gexf.py readwrite/gml.py readwrite/graphml.py readwrite/json_graph/cytoscape.py readwrite/json_graph/adjacency.py readwrite/json_graph/node_link.py readwrite/json_graph/tree.py readwrite/json_graph/jit.py readwrite/json_graph/__init__.py readwrite/nx_shp.py readwrite/pajek.py readwrite/sparse6.py readwrite/multiline_adjlist.py readwrite/graph6.py readwrite/edgelist.py readwrite/adjlist.py readwrite/leda.py readwrite/p2g.py readwrite/nx_yaml.py readwrite/__init__.py readwrite/gpickle.py classes/reportviews.py classes/function.py classes/graph.py classes/coreviews.py classes/digraph.py classes/multigraph.py classes/multidigraph.py classes/graphviews.py classes/filters.py classes/ordered.py classes/__init__.py drawing/layout.py drawing/nx_pylab.py drawing/nx_agraph.py drawing/nx_pydot.py drawing/__init__.py utils/misc.py utils/heaps.py utils/decorators.py utils/mapped_queue.py utils/random_sequence.py utils/union_find.py utils/rcm.py utils/contextmanagers.py utils/__init__.py linalg/algebraicconnectivity.py linalg/attrmatrix.py linalg/laplacianmatrix.py linalg/graphmatrix.py linalg/modularitymatrix.py linalg/spectrum.py linalg/bethehessianmatrix.py linalg/__init__.py convert_matrix.py convert.py conftest.py relabel.py testing/utils.py testing/__init__.py __init__.py exception.py

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.


@@ -5,6 +5,15 @@
Loading
5 5
__all__ = ["pagerank", "pagerank_numpy", "pagerank_scipy", "google_matrix"]
6 6
7 7
8 +
class PageRankResult(dict):
9 +
    def __init__(self, pagerank_score, analytics_info) -> None:
10 +
        super().__init__(pagerank_score)
11 +
        self.pagerank_iterations = analytics_info["x"]
12 +
        self.convergence = analytics_info["err"]
13 +
        self.iterations = analytics_info["iterations"]
14 +
        self.return_message = analytics_info["return_message"]
15 +
16 +
8 17
@not_implemented_for("multigraph")
9 18
def pagerank(
10 19
    G,
@@ -15,6 +24,7 @@
Loading
15 24
    nstart=None,
16 25
    weight="weight",
17 26
    dangling=None,
27 +
    analytics=False,
18 28
):
19 29
    """Returns the PageRank of the nodes in the graph.
20 30
@@ -59,10 +69,14 @@
Loading
59 69
      matrix (see notes under google_matrix). It may be common to have the
60 70
      dangling dict to be the same as the personalization dict.
61 71
72 +
    analytics : bool (default=False)
73 +
        Store the pagerank scores and error deltas of each Iteration.
74 +
62 75
    Returns
63 76
    -------
64 77
    pagerank : dictionary
65 78
       Dictionary of nodes with PageRank as value
79 +
       with further analytics information if specified.
66 80
67 81
    Examples
68 82
    --------
@@ -92,7 +106,7 @@
Loading
92 106
    PowerIterationFailedConvergence
93 107
        If the algorithm fails to converge to the specified tolerance
94 108
        within the specified number of iterations of the power iteration
95 -
        method.
109 +
        method and analytics is disabled.
96 110
97 111
    References
98 112
    ----------
@@ -140,6 +154,7 @@
Loading
140 154
    dangling_nodes = [n for n in W if W.out_degree(n, weight=weight) == 0.0]
141 155
142 156
    # power iteration: make up to max_iter iterations
157 +
    analytics_info = dict(x=[], err=[], iterations=0)
143 158
    for _ in range(max_iter):
144 159
        xlast = x
145 160
        x = dict.fromkeys(xlast.keys(), 0)
@@ -152,9 +167,21 @@
Loading
152 167
            x[n] += danglesum * dangling_weights.get(n, 0) + (1.0 - alpha) * p.get(n, 0)
153 168
        # check convergence, l1 norm
154 169
        err = sum([abs(x[n] - xlast[n]) for n in x])
170 +
        if analytics:
171 +
            analytics_info["x"].append(x)
172 +
            analytics_info["err"].append(err)
173 +
        analytics_info["iterations"] += 1
155 174
        if err < N * tol:
156 -
            return x
157 -
    raise nx.PowerIterationFailedConvergence(max_iter)
175 +
            analytics_info[
176 +
                "return_message"
177 +
            ] = f"iteration converged within {analytics_info['iterations']} iterations"
178 +
            return PageRankResult(x, analytics_info)
179 +
    if not analytics:
180 +
        raise nx.PowerIterationFailedConvergence(max_iter)
181 +
    analytics_info[
182 +
        "return_message"
183 +
    ] = f"power iteration failed to converge within {max_iter} iterations"
184 +
    return PageRankResult(dict(), analytics_info)
158 185
159 186
160 187
def google_matrix(

@@ -5,7 +5,19 @@
Loading
5 5
__all__ = ["hits", "hits_numpy", "hits_scipy", "authority_matrix", "hub_matrix"]
6 6
7 7
8 -
def hits(G, max_iter=100, tol=1.0e-8, nstart=None, normalized=True):
8 +
class HitsResult(tuple):
9 +
    def __new__(cls, hub_score, authority_score, analytics_info) -> tuple:
10 +
        return super().__new__(cls, (hub_score, authority_score))
11 +
12 +
    def __init__(self, hub_score, authority_score, analytics_info) -> None:
13 +
        self.hub_iterations = analytics_info["h"]
14 +
        self.authority_iterations = analytics_info["a"]
15 +
        self.convergence = analytics_info["err"]
16 +
        self.iterations = analytics_info["iterations"]
17 +
        self.return_message = analytics_info["return_message"]
18 +
19 +
20 +
def hits(G, max_iter=100, tol=1.0e-8, nstart=None, normalized=True, analytics=False):
9 21
    """Returns HITS hubs and authorities values for nodes.
10 22
11 23
    The HITS algorithm computes two numbers for a node.
@@ -29,18 +41,22 @@
Loading
29 41
    normalized : bool (default=True)
30 42
       Normalize results by the sum of all of the values.
31 43
44 +
    analytics : bool (default=False)
45 +
        Store the authority and hub scores and error delta of each Iteration.
46 +
        Iteration values are not normalized.
47 +
32 48
    Returns
33 49
    -------
34 50
    (hubs,authorities) : two-tuple of dictionaries
35 51
       Two dictionaries keyed by node containing the hub and authority
36 -
       values.
52 +
       values. With further analytics information if specified.
37 53
38 54
    Raises
39 55
    ------
40 56
    PowerIterationFailedConvergence
41 57
        If the algorithm fails to converge to the specified tolerance
42 58
        within the specified number of iterations of the power iteration
43 -
        method.
59 +
        method and analytics is disabled.
44 60
45 61
    Examples
46 62
    --------
@@ -82,6 +98,7 @@
Loading
82 98
        s = 1.0 / sum(h.values())
83 99
        for k in h:
84 100
            h[k] *= s
101 +
    analytics_info = dict(h=[], a=[], err=[], iterations=0)
85 102
    for _ in range(max_iter):  # power iteration: make up to max_iter iterations
86 103
        hlast = h
87 104
        h = dict.fromkeys(hlast.keys(), 0)
@@ -105,18 +122,31 @@
Loading
105 122
            a[n] *= s
106 123
        # check convergence, l1 norm
107 124
        err = sum([abs(h[n] - hlast[n]) for n in h])
125 +
        if analytics:
126 +
            analytics_info["a"].append(a)
127 +
            analytics_info["h"].append(h)
128 +
            analytics_info["err"].append(err)
129 +
        analytics_info["iterations"] += 1
108 130
        if err < tol:
131 +
            analytics_info[
132 +
                "return_message"
133 +
            ] = f"iteration converged within {analytics_info['iterations']} iterations"
109 134
            break
110 135
    else:
111 -
        raise nx.PowerIterationFailedConvergence(max_iter)
136 +
        if not analytics:
137 +
            raise nx.PowerIterationFailedConvergence(max_iter)
138 +
        analytics_info[
139 +
            "return_message"
140 +
        ] = f"power iteration failed to converge within {max_iter} iterations"
141 +
        return HitsResult(dict(), dict(), analytics_info)
112 142
    if normalized:
113 143
        s = 1.0 / sum(a.values())
114 144
        for n in a:
115 145
            a[n] *= s
116 146
        s = 1.0 / sum(h.values())
117 147
        for n in h:
118 148
            h[n] *= s
119 -
    return h, a
149 +
    return HitsResult(h, a, analytics_info)
120 150
121 151
122 152
def authority_matrix(G, nodelist=None):

Learn more Showing 27 files with coverage changes found.

Changes in networkx/algorithms/triads.py
-1
+1
Loading file...
Changes in networkx/classes/graph.py
-5
Loading file...
Changes in networkx/readwrite/json_graph/cytoscape.py
New
Loading file...
Changes in networkx/readwrite/graphml.py
New
Loading file...
Changes in networkx/algorithms/cluster.py
+2
Loading file...
Changes in networkx/classes/digraph.py
-6
Loading file...
Changes in networkx/algorithms/shortest_paths/weighted.py
-1
-1
+2
Loading file...
Changes in networkx/convert.py
-1
Loading file...
Changes in networkx/drawing/layout.py
-1
-3
Loading file...
Changes in networkx/algorithms/bipartite/matching.py
-1
-2
Loading file...
Changes in networkx/generators/joint_degree_seq.py
-2
-2
+4
Loading file...
Changes in networkx/algorithms/centrality/current_flow_betweenness.py
-1
-2
Loading file...
Changes in networkx/algorithms/centrality/katz.py
-1
-2
Loading file...
Changes in networkx/algorithms/link_analysis/hits_alg.py
-4
+3
+1
Loading file...
Changes in networkx/algorithms/shortest_paths/dense.py
-1
-2
Loading file...
Changes in networkx/linalg/attrmatrix.py
-2
-4
Loading file...
Changes in networkx/algorithms/centrality/current_flow_betweenness_subset.py
-2
-4
Loading file...
Changes in networkx/algorithms/isomorphism/matchhelpers.py
-3
-9
Loading file...
Changes in networkx/algorithms/centrality/second_order.py
-1
-2
Loading file...
Changes in networkx/linalg/algebraicconnectivity.py
-3
-2
-14
Loading file...
Changes in networkx/algorithms/centrality/trophic.py
-2
-4
Loading file...
Changes in networkx/algorithms/non_randomness.py
-1
-3
Loading file...
Changes in networkx/algorithms/node_classification/hmn.py
-2
-4
Loading file...
Changes in networkx/algorithms/bipartite/spectral.py
-1
-2
Loading file...
Changes in networkx/algorithms/node_classification/lgc.py
-2
-4
Loading file...
Changes in networkx/drawing/nx_pylab.py
-7
-32
Loading file...
Changes in networkx/algorithms/assortativity/correlation.py
-4
-6
Loading file...
Files Coverage
networkx 0.40% 93.90%
Project Totals (275 files) 93.90%
Loading