Ticket #12806: trac_12806_doctest_fixes.patch

File trac_12806_doctest_fixes.patch, 14.5 KB (added by jlopez, 7 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 b  
    25462546    def topological_sort(self, implementation = "default"):
    25472547        """
    25482548        Returns a topological sort of the digraph if it is acyclic, and raises a
    2549         TypeError if the digraph contains a directed cycle.
     2549        TypeError if the digraph contains a directed cycle. As topological
     2550        sorts are not necessarily unique, different implementations may yield
     2551        different results.
    25502552       
    25512553        A topological sort is an ordering of the vertices of the digraph such
    25522554        that each vertex comes before all of its successors. That is, if `u`
     
    25562558        INPUT:
    25572559
    25582560        - ``implementation`` -- Use the default Cython implementation
    2559           (``implementation = default``) or the NetworkX library
    2560           (``implementation = "NetworkX"``)
     2561          (``implementation = default``), the default NetworkX library
     2562          (``implementation = "NetworkX"``) or the recursive NetworkX
     2563          implementation (``implementation = "recursive")
    25612564
    25622565        .. SEEALSO::
    25632566
     
    25822585
    25832586            sage: D.topological_sort(implementation = "NetworkX")
    25842587            [4, 5, 6, 9, 0, 1, 2, 3, 7, 8, 10]
     2588           
     2589        Using the NetworkX recursive implementation ::
     2590
     2591            sage: D.topological_sort(implementation = "recursive")
     2592            [4, 5, 6, 9, 0, 3, 2, 7, 1, 8, 10]
    25852593
    25862594        ::
    25872595       
     
    25932601       
    25942602        .. note::
    25952603
    2596            There is a recursive version of this in NetworkX, but it has
    2597            problems::
     2604           There is a recursive version of this in NetworkX, it used to have problems in earlier versions but they have since been fixed::
    25982605       
    25992606              sage: import networkx
    26002607              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] })
    26012608              sage: N = D.networkx_graph()
    26022609              sage: networkx.topological_sort(N)
    26032610              [4, 5, 6, 9, 0, 1, 2, 3, 7, 8, 10]
    2604               sage: networkx.topological_sort_recursive(N) is None
    2605               True
     2611              sage: networkx.topological_sort_recursive(N)
     2612              [4, 5, 6, 9, 0, 3, 2, 7, 1, 8, 10]
    26062613
    26072614        TESTS:
    26082615
     
    26212628            else:
    26222629                raise TypeError('Digraph is not acyclic-- there is no topological sort.')
    26232630               
    2624         elif implementation == "NetworkX":
     2631        elif implementation == "NetworkX" or implementation == "recursive":
    26252632            import networkx
    2626             S = networkx.topological_sort(self.networkx_graph(copy=False))
     2633            if implementation == "NetworkX":
     2634                S = networkx.topological_sort(self.networkx_graph(copy=False))
     2635            else:
     2636                S = networkx.topological_sort_recursive(self.networkx_graph(copy=False))
    26272637            if S is None:
    26282638                raise TypeError('Digraph is not acyclic-- there is no topological sort.')
    26292639            else:
  • sage/graphs/generic_graph.py

    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b  
    1082110821        import networkx
    1082210822        return networkx.average_clustering(self.networkx_graph(copy=False))
    1082310823       
    10824     def clustering_coeff(self, nbunch=None, weights=False):
    10825         r"""
    10826         Returns the clustering coefficient for each vertex in nbunch as a
     10824    def clustering_coeff(self, nodes=None, weight=False, return_vertex_weights=True):
     10825        r"""
     10826        Returns the clustering coefficient for each vertex in `nodes` as a
    1082710827        dictionary keyed by vertex.
    1082810828       
    10829         The clustering coefficient of a graph is the fraction of possible
    10830         triangles that are triangles, `c_i = triangles_i /
    10831         (k_i\*(k_i-1)/2)` where `k_i` is the degree of vertex `i`, [1]. A
    10832         coefficient for the whole graph is the average of the `c_i`.
     10829        For an unweighted graph, the clustering coefficient of a node `i` is
     10830        the fraction of possible triangles containing `i` that exist.
     10831        `c_i = 2\*T(i) / (k_i\*(k_i-1))` where T(i)` the number of triangles
     10832        through `i` and `k_i` is the degree of vertex `i` [1].
     10833       
     10834        For weighted graphs the clustering is defined as the geometric average
     10835        of the subgraph edge weights [1], normalized by the maximum weight in
     10836        the network.
     10837       
     10838        The value of `c_i` is assigned to 0 if `k_i < 2`.
     10839       
     10840        A coefficient for the whole graph is the average of the `c_i`.
     10841       
    1083310842        Transitivity is the fraction of all possible triangles which are
    1083410843        triangles, T = 3\*triangles/triads, [1].
    1083510844       
    1083610845        INPUT:
    1083710846       
    10838         -  ``nbunch`` - the vertices to inspect (default
     10847        -  ``nodes`` - the vertices to inspect (default
    1083910848           None returns data on all vertices in graph)
    1084010849       
    10841         -  ``weights`` - default is False. If both
    10842            with_labels and weights are True, then returns a clustering
    10843            coefficient dict and a dict of weights based on degree. Weights are
    10844            the fraction of connected triples in the graph that include the
    10845            keyed vertex.
    10846        
     10850        -  ``weight`` - string or boolean default is False. If it is a string
     10851        it used the indicated edge property as weight. `weight = True` is
     10852        equivalent to `weight = weight`
     10853       
     10854        - ``return_vertex_weights`` is a boolean ensuring backwards
     10855        compatibility with deprecated features of NetworkX 1.2. It
     10856        should be set to False for all production code.
    1084710857       
    1084810858        REFERENCE:
    1084910859
     
    1085710867            [0.3333333333333333, 0.3333333333333333, 0.0, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.0, 0.3333333333333333, 0.3333333333333333, 0.0]
    1085810868            sage: (graphs.FruchtGraph()).clustering_coeff()
    1085910869            {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}
    10860             sage: (graphs.FruchtGraph()).clustering_coeff(weights=True)
     10870
     10871            sage: (graphs.FruchtGraph()).clustering_coeff(weight=True, return_vertex_weights=False)
     10872            {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}
     10873            sage: (graphs.FruchtGraph()).clustering_coeff(nodes=[0,1,2])
     10874            {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0}
     10875
     10876            sage: (graphs.FruchtGraph()).clustering_coeff(nodes=[0,1,2],weight=True,  return_vertex_weights=False)
     10877            {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0}
     10878
     10879        TESTS::
     10880
     10881            Failing doctest due to API change, use for backwards compatibility
     10882            Remove when return_vertex_weights is deprecated
     10883            sage: (graphs.FruchtGraph()).clustering_coeff(weight=True, return_vertex_weights=True)
     10884            doctest:...: DeprecationWarning: The option 'return_vertex_weights' has been deprecated. Only offered for backwards compatibility with NetworkX 1.2.
    1086110885            ({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})
    10862             sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2])
    10863             {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0}
    10864             sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2],weights=True)
     10886
     10887            sage: (graphs.FruchtGraph()).clustering_coeff(nodes=[0,1,2], weight=True, return_vertex_weight=True)
     10888            doctest:...: DeprecationWarning: The option 'return_vertex_weights' has been deprecated. Only offered for backwards compatibility with NetworkX 1.2.
    1086510889            ({0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.0}, {0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.3333333333333333})
     10890
    1086610891        """
    1086710892        import networkx
    10868         return networkx.clustering(self.networkx_graph(copy=False), nbunch, weights)
     10893        if weight and return_vertex_weights:
     10894            # Running in compatibility mode with deprecated NetworkX 1.2 features
     10895            # All this code should be removed when the deprecation warning expires
     10896            from sage.misc.misc import deprecation
     10897            deprecation("The option 'return_vertex_weights' has been " +\
     10898                        "deprecated. Only offered for backwards" +\
     10899                        " compatibility with NetworkX 1.2.")
     10900            G = self.networkx_graph(copy=False)
     10901            if G.is_directed():
     10902                raise NetworkXError("Clustering algorithms are not defined for directed graphs.")
     10903            clusterc={}
     10904            weights={}
     10905            for v,d,t in networkx.cluster._triangles_and_degree_iter(G,nodes):
     10906                weights[v]=float(d*(d-1))
     10907                if t==0:
     10908                    clusterc[v]=0.0
     10909                else:
     10910                    clusterc[v]=t/float(d*(d-1))
     10911            scale=1./sum(weights.itervalues())
     10912            for v,w in weights.iteritems():
     10913                weights[v]=w*scale
     10914            return clusterc,weights
     10915           
     10916        else:
     10917            return networkx.clustering(self.networkx_graph(copy=False), nodes, weight=weight)
    1086910918       
    1087010919    def cluster_transitivity(self):
    1087110920        r"""
  • sage/graphs/graph.py

    diff --git a/sage/graphs/graph.py b/sage/graphs/graph.py
    a b  
    32093209
    32103210    ### Centrality
    32113211
    3212     def centrality_betweenness(self, normalized=True):
     3212    def centrality_betweenness(self, k=None, normalized=True, weight=None, endpoints=False, seed=None):
    32133213        r"""
    32143214        Returns the betweenness centrality (fraction of number of shortest
    32153215        paths that go through each vertex) as a dictionary keyed by
     
    32283228        -  ``normalized`` - boolean (default True) - if set to
    32293229           False, result is not normalized.
    32303230       
     3231        - ``k`` - integer or None (default None) - if set to an integer, use ``k`` node samples to estimate betweenness. Higher values give better approximations.
     3232       
     3233        - ``weight`` - None or string. If set to a string, use that attribute of the nodes as weight. ``weight = True`` is equivalent to ``weight = "weight"``
     3234       
     3235        - ``endpoints`` - Boolean. If set to True it includes the endpoints in the shortest paths count
    32313236       
    32323237        REFERENCE:
    32333238
     
    32493254            {0: 0.16666666666666666, 1: 0.16666666666666666, 2: 0.0, 3: 0.0}
    32503255        """
    32513256        import networkx
    3252         return networkx.betweenness_centrality(self.networkx_graph(copy=False), normalized)
     3257        return networkx.betweenness_centrality(self.networkx_graph(copy=False), k=k,normalized = normalized, weight=weight, endpoints=endpoints, seed=seed )
    32533258       
    32543259    def centrality_degree(self, v=None):
    32553260        r"""
  • sage/graphs/graph_generators.py

    diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
    a b  
    42214221
    42224222        TESTS:
    42234223
    4224         We only consider balanced trees whose root node has degree `r \geq 2`::
    4225 
    4226             sage: graphs.BalancedTree(1, randint(1, 10^6))
    4227             Traceback (most recent call last):
    4228             ...
    4229             NetworkXError: Invalid graph description, r should be >=2
    4230             sage: graphs.BalancedTree(randint(-10^6, 1), randint(1, 10^6))
    4231             Traceback (most recent call last):
    4232             ...
    4233             NetworkXError: Invalid graph description, r should be >=2
    4234 
    4235         The tree must have height `h \geq 1`::
    4236 
    4237             sage: graphs.BalancedTree(randint(2, 10^6), 0)
    4238             Traceback (most recent call last):
    4239             ...
    4240             NetworkXError: Invalid graph description, h should be >=1
    4241             sage: graphs.BalancedTree(randint(2, 10^6), randint(-10^6, 0))
    4242             Traceback (most recent call last):
    4243             ...
    4244             NetworkXError: Invalid graph description, h should be >=1
    4245             sage: graphs.BalancedTree(randint(-10^6, 1), randint(-10^6, 0))
    4246             Traceback (most recent call last):
    4247             ...
    4248             NetworkXError: Invalid graph description, r should be >=2
     4224         Normally we would only consider balanced trees whose root node
     4225         has degree `r \geq 2`, but the construction degenerates gracefully::
     4226
     4227            sage: graphs.BalancedTree(1, 10)
     4228            Balanced tree: Graph on 2 vertices
     4229           
     4230            sage: graphs.BalancedTree(-1, 10)
     4231            Balanced tree: Graph on 1 vertex
     4232
     4233        Similarly, we usually want the tree must have height `h \geq 1`
     4234        but the algorithm also degenerates gracefully here::
     4235
     4236            sage: graphs.BalancedTree(3, 0)
     4237            Balanced tree: Graph on 1 vertex
     4238
     4239            sage: graphs.BalancedTree(5, -2)
     4240            Balanced tree: Graph on 0 vertices
     4241
     4242            sage: graphs.BalancedTree(-2,-2)
     4243            Balanced tree: Graph on 0 vertices
    42494244        """
    42504245        import networkx
    42514246        return graph.Graph(networkx.balanced_tree(r, h), name="Balanced tree")
     
    57425737        ::
    57435738       
    57445739            sage: graphs.RandomHolmeKim(8, 2, 0.5).edges(labels=False)
    5745             [(0, 2), (0, 4), (1, 2), (1, 3), (2, 3), (3, 4), (3, 5), (3, 6), (3, 7), (4, 5), (4, 6)]
     5740            [(0, 2), (0, 5), (1, 2), (1, 3), (2, 3), (2, 4), (2, 6), (2, 7), (3, 4), (3, 6), (3, 7), (4, 5)]
    57465741       
    57475742        ::
    57485743       
     
    67836778       
    67846779            sage: G = graphs.DegreeSequenceExpected([1,2,3,2,3])
    67856780            sage: G.edges(labels=False)
    6786             [(0, 2), (1, 1), (1, 3), (2, 2), (2, 4), (3, 3)]
     6781            [(0, 2), (0, 3), (1, 1), (1, 4), (2, 3), (2, 4), (3, 4), (4, 4)]
    67876782            sage: G.show()  # long time
    67886783       
    67896784        REFERENCE: