sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.22k stars 425 forks source link

Deprecate sorting of Graph vertices and edges by default #22349

Closed jdemeyer closed 1 year ago

jdemeyer commented 7 years ago

Why does Graph.vertices() need to sort the list of vertices? If the user wants a sorted list, they can always call sorted() anyway. The same for Graph.edges().

Sorting is broken badly now that #22029 is merged.

CC: @sagetrac-Stefan @jm58660 @jhpalmieri

Component: graph theory

Keywords: richcmp

Author: John Palmieri, David Coudert

Branch/Commit: fe93716

Reviewer: David Coudert, John Palmieri

Issue created by migration from https://trac.sagemath.org/ticket/22349

jhpalmieri commented 2 years ago

Changed branch from u/jdemeyer/graph_vertices___should_not_be_sorted to u/jhpalmieri/graph_vertices___should_not_be_sorted

jhpalmieri commented 2 years ago

Changed commit from aa0691f to b6b6c34

jhpalmieri commented 2 years ago
comment:62

Here is a start. This does what I proposed in comment:60, and all tests pass. I was not that careful in choosing sort=True or sort=False, and so I would not surprised if we could use sort=False in more places. Feel free to modify the branch.


New commits:

27a6483trac 22349: vertex sorting
b6b6c34trac 22349: edge sorting
jhpalmieri commented 2 years ago
comment:63

The key changes are in graphs/generic_graph.py:

@@ -10972,15 +10974,20 @@ class GenericGraph(GenericGraph_pyx):
         for u in self._backend.iterator_nbrs(vertex):
             yield u

-    def vertices(self, sort=True, key=None, degree=None, vertex_property=None):
+    def vertices(self, sort=None, key=None, degree=None, vertex_property=None):
         r"""
         Return a list of the vertices.

         INPUT:

-        - ``sort`` -- boolean (default: ``True``); if ``True``, vertices are
+        - ``sort`` -- boolean (default: ``None``); if ``True``, vertices are
           sorted according to the default ordering

+          As of :trac:`22349`, this argument must be explicitly
+          specified (unless a ``key`` is given); otherwise a warning
+          is printed and ``sort=True`` is used. The default will
+          eventually be changed to ``False``.
+
         - ``key`` -- a function (default: ``None``); a function that takes a
           vertex as its one argument and returns a value that can be used for
           comparisons in the sorting algorithm (we must have ``sort=True``)

and

@@ -11065,9 +11072,23 @@ class GenericGraph(GenericGraph_pyx):
             Traceback (most recent call last):
             ...
             ValueError: sort keyword is False, yet a key function is given
+
+        Deprecation warning for ``sort=None`` (:trac:`22349`)::
+
+            sage: G = graphs.HouseGraph()
+            sage: G.vertices()
+            doctest:...: DeprecationWarning: parameter 'sort' will be set to False by default in the future
+            See http://trac.sagemath.org/22349 for details.
+            [0, 1, 2, 3, 4]
         """
+        if sort is None:
+            if key is None:
+                deprecation(22349, "parameter 'sort' will be set to False by default in the future")
+            sort = True
+
         if (not sort) and key:
             raise ValueError('sort keyword is False, yet a key function is given')
+
         if sort:
             return sorted(self.vertex_iterator(degree=degree, vertex_property=vertex_property), key=key)
         return list(self.vertex_iterator(degree=degree, vertex_property=vertex_property))

and similar for edges (although edges already printed a warning if None was used, but None was not the default, so it had to be called explicitly as g.edges(sort=None) to trigger the warning before).

dcoudert commented 2 years ago
comment:64

I'm impressed !

I'm currently doing some tests and will push later some corrections.

dcoudert commented 2 years ago
comment:65

I did a few changes in graphs and many tests. I have not detected any problem yet, but others may raise some issues.

This ticket highlights many places were the usage of graphs must be improved. I did some (minor) corrections in src/sage/schemes/curves/zariski_vankampen.py as example, but more can be done in this file. Typically, it is better to use for u in G: than for u in G.vertices(sort=False):. The ordering of the vertices is the same, but we avoid building the list of vertices. Tests like u in G.vertices(sort=False) should be replaced by u in G. etc. In future tickets, we should address (very bad) code like this in src/sage/geometry/hyperplane_arrangement/library.py

-        for e in G.edges():
-            i = G.vertices().index(e[0])
-            j = G.vertices().index(e[1])
+        for e in G.edges(sort=True):
+            i = G.vertices(sort=True).index(e[0])
+            j = G.vertices(sort=True).index(e[1])

Clearly, we need to define a mapping from vertices to index.


New commits:

fee74b6trac #22349: some corrections
dcoudert commented 2 years ago

Changed branch from u/jhpalmieri/graph_vertices___should_not_be_sorted to public/graphs/22349_deprecate_sorting_vertices_and_edges

dcoudert commented 2 years ago

Changed commit from b6b6c34 to fee74b6

jhpalmieri commented 2 years ago
comment:67

Great! I'm happy with your changes. We can try to get it merged soon, although with this many files affected, there might very well be merge conflicts.

jhpalmieri commented 2 years ago

Reviewer: David Coudert, John Palmieri

jhpalmieri commented 2 years ago

Author: John Palmieri, David Coudert

jhpalmieri commented 2 years ago
comment:68

Positive review from me for your contributions.

dcoudert commented 2 years ago
comment:69

We will certainly get merge conflicts (due to the many tickets improving the coding style for instance), but let's try.

Positive review from me too.

vbraun commented 2 years ago
comment:70

Merge failure on top of:

83a3df1c99 Trac #34131: some doc polishing in symbolic

dee5229b00 Trac #34130: some doc polishing in groups

fd80fa111f Trac #34126: some doc polishing in categories

718d356f1c Trac #34125: some doc polishing in modular

49390d8bcf Trac #34124: some doc polishing in combinat

af94c25b54 Trac #34096: Fix typo: enviroment -> environment

d04c189c8e Trac #34050: add some space around == in paths and rational

1634bb6946 Trac #34047: Typo in _repr_Expression in symbolic/expression.pyx

dc8086fd57 Trac #34154: Fix repeated word typos

17b1c177aa Trac #34148: fix and activate pycodestyle E306

785bdbd0ec Trac #34136: modernize super() in rings (part one)

9882001355 Trac #34129: Dummy packages should write proper log files

dbc6b9e161 Trac #34109: pep8 for sagedoc.py

5e6a3318c8 Trac #34103: randomize infinite recursion

17ba76f00b Trac #34093: Wrong evaluation of elements of CBF[x] on polynomials from other rings

4657052737 Trac #34122: Bug in is_planar() method for directed graphs

c9cfb5395f Trac #34087: inverse reciprocal trig functions not (back)translated to/from Mathematica

1d6d055563 Trac #34076: pycodestyle cleanup in src/sage/graphs/genus.pyx

043d862b5d Trac #34071: pycodestyle cleanup in asteroidal_triples.pyx, chrompoly.pyx, cliquer.pyx, convexity_properties.pyx

b9b25dc735 Trac #34070: pycodestyle cleanup in src/sage/graphs/centrality.pyx

4ef2c6597d Trac #34069: pycodestyle cleanup in src/sage/graphs/comparability.pyx

5b1146766b Trac #34066: Tachyon help message

798adaa5a3 Trac #34065: pycodestyle cleanup in src/sage/graphs/bliss.pyx

ce62be23a2 Trac #34063: pycodestyle cleanup in src/sage/graphs/base/

a0eadb3e40 Trac #34059: Fix trivial case in conversion of list to number field element

fea0ac50a6 Trac #34057: changes about avoiding double dieses

9eefd5c27e Trac #34145: modernize super in geometry/

01e117dfd8 Trac #34137: modernize super in categories/

6c79334402 Trac #33819: build.yml: In workflow_dispatch, make container and base tag selectable; add doc

6a64ab8d00 Trac #33760: do not update _pos, _pos3d, _embedding on vertex/edge deletions

dbf091dbbb Trac #34139: fix the linter

625ac58151 Updated SageMath version to 9.7.beta5

merge was not clean: conflicts in src/sage/groups/perm_gps/partn_ref/refinement_graphs.pyx

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from fee74b6 to 1e1ea24

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

39d156bsome doc polishing in groups
40b5e2amore fixes
68ea9dcMerge branch 'u/chapoton/34130' in 9.7.b5
1ec4bdftrac 22349: vertex sorting
a9ecbc5trac 22349: edge sorting
1e1ea24trac #22349: some corrections
jhpalmieri commented 2 years ago
comment:72

Rebased over #34130.

vbraun commented 2 years ago
comment:73

Merge failure on top of:

7f7149489c Updated SageMath version to 9.7.beta6

merge was not clean: conflicts in src/sage/combinat/yang_baxter_graph.py

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

4362caeMerge branch 'develop' into t/22349/public/graphs/22349_deprecate_sorting_vertices_and_edges
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 2 years ago

Changed commit from 1e1ea24 to 4362cae

jhpalmieri commented 2 years ago
comment:75

Merged 9.7.beta6.

dcoudert commented 2 years ago
comment:76

Let's hope this one can be merged soon to avoid new conflicts.

vbraun commented 1 year ago
comment:77

see patchbot

jhpalmieri commented 1 year ago
comment:78

Replying to @vbraun:

see patchbot

Which part? I believe the "deprecation number" warning is from this change:

@@ -12229,7 +12255,8 @@ class GenericGraph(GenericGraph_pyx):
             [(0, 1, None), (0, 2, None), (1, 3, None), (2, 3, None), (2, 4, None), (3, 4, None)]
         """
         if sort is None:
-            deprecation(27408, "parameter 'sort' will be set to False by default in the future")
+            if key is None:
+                deprecation(27408, "parameter 'sort' will be set to False by default in the future")
             sort = True

         if vertices is not None and vertices in self:

and that seems appropriate: no reason to change the ticket number. (That's the only match for "27408" in the changes for this ticket.)

The pyflakes warnings are not related to this ticket: they are in files touched by this ticket but not where we've modified them. (In addition, I don't understand the warning about simplicial_complex.py)

tscrim commented 1 year ago
comment:79

It is more fundamental: there is a test failing for src/sage/graphs/edge_connectivity.pyx.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 1 year ago

Changed commit from 4362cae to fe93716

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 1 year ago

Branch pushed to git repo; I updated commit sha1. New commits:

fe93716trac 22349: fix one more doctest
jhpalmieri commented 1 year ago
comment:82

Doctest fixed.

vbraun commented 1 year ago

Changed branch from public/graphs/22349_deprecate_sorting_vertices_and_edges to fe93716

tornaria commented 1 year ago

How long it should take until the deprecation warning is removed?

kcrisman commented 1 year ago

How long it should take until the deprecation warning is removed?

I hear you - see e.g. here. I think there is still a waiting period for removing them, though - a year, was it? Maybe a new ticket/issue should be opened for that.