# Ticket #12806: trac_12806_doctest_fixes.patch

File trac_12806_doctest_fixes.patch, 14.5 KB (added by jlopez, 8 years ago)
• ## sage/graphs/digraph.py

```# HG changeset patch
# User Javier López Peña <vengoroso@gmail.com>
# Date 1339690589 -3600
# Node ID 62b637ba49ecf5ea9084e034a85700d99acde03d
# Parent  04c5f242405074de0df5af4fe35af4f5bea417ba
Trac 12806: doctest failures with new NetworkX 1.6 spkg

diff --git a/sage/graphs/digraph.py b/sage/graphs/digraph.py```
 a def topological_sort(self, implementation = "default"): """ Returns a topological sort of the digraph if it is acyclic, and raises a TypeError if the digraph contains a directed cycle. TypeError if the digraph contains a directed cycle. As topological sorts are not necessarily unique, different implementations may yield different results. A topological sort is an ordering of the vertices of the digraph such that each vertex comes before all of its successors. That is, if `u` INPUT: - ``implementation`` -- Use the default Cython implementation (``implementation = default``) or the NetworkX library (``implementation = "NetworkX"``) (``implementation = default``), the default NetworkX library (``implementation = "NetworkX"``) or the recursive NetworkX implementation (``implementation = "recursive") .. SEEALSO:: sage: D.topological_sort(implementation = "NetworkX") [4, 5, 6, 9, 0, 1, 2, 3, 7, 8, 10] Using the NetworkX recursive implementation :: sage: D.topological_sort(implementation = "recursive") [4, 5, 6, 9, 0, 3, 2, 7, 1, 8, 10] :: .. note:: There is a recursive version of this in NetworkX, but it has problems:: There is a recursive version of this in NetworkX, it used to have problems in earlier versions but they have since been fixed:: sage: import networkx sage: D = DiGraph({ 0:[1,2,3], 4:[2,5], 1:[8], 2:[7], 3:[7], 5:[6,7], 7:[8], 6:[9], 8:[10], 9:[10] }) sage: N = D.networkx_graph() sage: networkx.topological_sort(N) [4, 5, 6, 9, 0, 1, 2, 3, 7, 8, 10] sage: networkx.topological_sort_recursive(N) is None True sage: networkx.topological_sort_recursive(N) [4, 5, 6, 9, 0, 3, 2, 7, 1, 8, 10] TESTS: else: raise TypeError('Digraph is not acyclic-- there is no topological sort.') elif implementation == "NetworkX": elif implementation == "NetworkX" or implementation == "recursive": import networkx S = networkx.topological_sort(self.networkx_graph(copy=False)) if implementation == "NetworkX": S = networkx.topological_sort(self.networkx_graph(copy=False)) else: S = networkx.topological_sort_recursive(self.networkx_graph(copy=False)) if S is None: raise TypeError('Digraph is not acyclic-- there is no topological sort.') else:
• ## sage/graphs/generic_graph.py

`diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py`
 a import networkx return networkx.average_clustering(self.networkx_graph(copy=False)) def clustering_coeff(self, nbunch=None, weights=False): r""" Returns the clustering coefficient for each vertex in nbunch as a def clustering_coeff(self, nodes=None, weight=False, return_vertex_weights=True): r""" Returns the clustering coefficient for each vertex in `nodes` as a dictionary keyed by vertex. The clustering coefficient of a graph is the fraction of possible triangles that are triangles, `c_i = triangles_i / (k_i\*(k_i-1)/2)` where `k_i` is the degree of vertex `i`, [1]. A coefficient for the whole graph is the average of the `c_i`. For an unweighted graph, the clustering coefficient of a node `i` is the fraction of possible triangles containing `i` that exist. `c_i = 2\*T(i) / (k_i\*(k_i-1))` where T(i)` the number of triangles through `i` and `k_i` is the degree of vertex `i` [1]. For weighted graphs the clustering is defined as the geometric average of the subgraph edge weights [1], normalized by the maximum weight in the network. The value of `c_i` is assigned to 0 if `k_i < 2`. A coefficient for the whole graph is the average of the `c_i`. Transitivity is the fraction of all possible triangles which are triangles, T = 3\*triangles/triads, [1]. INPUT: -  ``nbunch`` - the vertices to inspect (default -  ``nodes`` - the vertices to inspect (default None returns data on all vertices in graph) -  ``weights`` - default is False. If both with_labels and weights are True, then returns a clustering coefficient dict and a dict of weights based on degree. Weights are the fraction of connected triples in the graph that include the keyed vertex. -  ``weight`` - string or boolean default is False. If it is a string it used the indicated edge property as weight. `weight = True` is equivalent to `weight = weight` - ``return_vertex_weights`` is a boolean ensuring backwards compatibility with deprecated features of NetworkX 1.2. It should be set to False for all production code. REFERENCE: [0.3333333333333333, 0.3333333333333333, 0.0, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.0, 0.3333333333333333, 0.3333333333333333, 0.0] sage: (graphs.FruchtGraph()).clustering_coeff() {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0, 3: 0.3333333333333333, 4: 0.3333333333333333, 5: 0.3333333333333333, 6: 0.3333333333333333, 7: 0.3333333333333333, 8: 0.0, 9: 0.3333333333333333, 10: 0.3333333333333333, 11: 0.0} sage: (graphs.FruchtGraph()).clustering_coeff(weights=True) sage: (graphs.FruchtGraph()).clustering_coeff(weight=True, return_vertex_weights=False) {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0, 3: 0.3333333333333333, 4: 0.3333333333333333, 5: 0.3333333333333333, 6: 0.3333333333333333, 7: 0.3333333333333333, 8: 0.0, 9: 0.3333333333333333, 10: 0.3333333333333333, 11: 0.0} sage: (graphs.FruchtGraph()).clustering_coeff(nodes=[0,1,2]) {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0} sage: (graphs.FruchtGraph()).clustering_coeff(nodes=[0,1,2],weight=True,  return_vertex_weights=False) {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0} TESTS:: Failing doctest due to API change, use for backwards compatibility Remove when return_vertex_weights is deprecated sage: (graphs.FruchtGraph()).clustering_coeff(weight=True, return_vertex_weights=True) doctest:...: DeprecationWarning: The option 'return_vertex_weights' has been deprecated. Only offered for backwards compatibility with NetworkX 1.2. ({0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0, 3: 0.3333333333333333, 4: 0.3333333333333333, 5: 0.3333333333333333, 6: 0.3333333333333333, 7: 0.3333333333333333, 8: 0.0, 9: 0.3333333333333333, 10: 0.3333333333333333, 11: 0.0}, {0: 0.08333333333333333, 1: 0.08333333333333333, 2: 0.08333333333333333, 3: 0.08333333333333333, 4: 0.08333333333333333, 5: 0.08333333333333333, 6: 0.08333333333333333, 7: 0.08333333333333333, 8: 0.08333333333333333, 9: 0.08333333333333333, 10: 0.08333333333333333, 11: 0.08333333333333333}) sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2]) {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0} sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2],weights=True) sage: (graphs.FruchtGraph()).clustering_coeff(nodes=[0,1,2], weight=True, return_vertex_weight=True) doctest:...: DeprecationWarning: The option 'return_vertex_weights' has been deprecated. Only offered for backwards compatibility with NetworkX 1.2. ({0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0}, {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.3333333333333333}) """ import networkx return networkx.clustering(self.networkx_graph(copy=False), nbunch, weights) if weight and return_vertex_weights: # Running in compatibility mode with deprecated NetworkX 1.2 features # All this code should be removed when the deprecation warning expires from sage.misc.misc import deprecation deprecation("The option 'return_vertex_weights' has been " +\ "deprecated. Only offered for backwards" +\ " compatibility with NetworkX 1.2.") G = self.networkx_graph(copy=False) if G.is_directed(): raise NetworkXError("Clustering algorithms are not defined for directed graphs.") clusterc={} weights={} for v,d,t in networkx.cluster._triangles_and_degree_iter(G,nodes): weights[v]=float(d*(d-1)) if t==0: clusterc[v]=0.0 else: clusterc[v]=t/float(d*(d-1)) scale=1./sum(weights.itervalues()) for v,w in weights.iteritems(): weights[v]=w*scale return clusterc,weights else: return networkx.clustering(self.networkx_graph(copy=False), nodes, weight=weight) def cluster_transitivity(self): r"""
• ## sage/graphs/graph.py

`diff --git a/sage/graphs/graph.py b/sage/graphs/graph.py`
 a ### Centrality def centrality_betweenness(self, normalized=True): def centrality_betweenness(self, k=None, normalized=True, weight=None, endpoints=False, seed=None): r""" Returns the betweenness centrality (fraction of number of shortest paths that go through each vertex) as a dictionary keyed by -  ``normalized`` - boolean (default True) - if set to False, result is not normalized. - ``k`` - integer or None (default None) - if set to an integer, use ``k`` node samples to estimate betweenness. Higher values give better approximations. - ``weight`` - None or string. If set to a string, use that attribute of the nodes as weight. ``weight = True`` is equivalent to ``weight = "weight"`` - ``endpoints`` - Boolean. If set to True it includes the endpoints in the shortest paths count REFERENCE: {0: 0.16666666666666666, 1: 0.16666666666666666, 2: 0.0, 3: 0.0} """ import networkx return networkx.betweenness_centrality(self.networkx_graph(copy=False), normalized) return networkx.betweenness_centrality(self.networkx_graph(copy=False), k=k,normalized = normalized, weight=weight, endpoints=endpoints, seed=seed ) def centrality_degree(self, v=None): r"""
• ## sage/graphs/graph_generators.py

`diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py`
 a TESTS: We only consider balanced trees whose root node has degree `r \geq 2`:: sage: graphs.BalancedTree(1, randint(1, 10^6)) Traceback (most recent call last): ... NetworkXError: Invalid graph description, r should be >=2 sage: graphs.BalancedTree(randint(-10^6, 1), randint(1, 10^6)) Traceback (most recent call last): ... NetworkXError: Invalid graph description, r should be >=2 The tree must have height `h \geq 1`:: sage: graphs.BalancedTree(randint(2, 10^6), 0) Traceback (most recent call last): ... NetworkXError: Invalid graph description, h should be >=1 sage: graphs.BalancedTree(randint(2, 10^6), randint(-10^6, 0)) Traceback (most recent call last): ... NetworkXError: Invalid graph description, h should be >=1 sage: graphs.BalancedTree(randint(-10^6, 1), randint(-10^6, 0)) Traceback (most recent call last): ... NetworkXError: Invalid graph description, r should be >=2 Normally we would only consider balanced trees whose root node has degree `r \geq 2`, but the construction degenerates gracefully:: sage: graphs.BalancedTree(1, 10) Balanced tree: Graph on 2 vertices sage: graphs.BalancedTree(-1, 10) Balanced tree: Graph on 1 vertex Similarly, we usually want the tree must have height `h \geq 1` but the algorithm also degenerates gracefully here:: sage: graphs.BalancedTree(3, 0) Balanced tree: Graph on 1 vertex sage: graphs.BalancedTree(5, -2) Balanced tree: Graph on 0 vertices sage: graphs.BalancedTree(-2,-2) Balanced tree: Graph on 0 vertices """ import networkx return graph.Graph(networkx.balanced_tree(r, h), name="Balanced tree") :: sage: graphs.RandomHolmeKim(8, 2, 0.5).edges(labels=False) [(0, 2), (0, 4), (1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (3, 6), (3, 7), (4, 5), (4, 6)] [(0, 2), (0, 5), (1, 2), (1, 3), (2, 3), (2, 4), (2, 6), (2, 7), (3, 4), (3, 6), (3, 7), (4, 5)] :: sage: G = graphs.DegreeSequenceExpected([1,2,3,2,3]) sage: G.edges(labels=False) [(0, 2), (1, 1), (1, 3), (2, 2), (2, 4), (3, 3)] [(0, 2), (0, 3), (1, 1), (1, 4), (2, 3), (2, 4), (3, 4), (4, 4)] sage: G.show()  # long time REFERENCE: