Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph to_directed can have side effect when plotting #22424

Closed
kcrisman opened this issue Feb 23, 2017 · 29 comments
Closed

graph to_directed can have side effect when plotting #22424

kcrisman opened this issue Feb 23, 2017 · 29 comments

Comments

@kcrisman
Copy link
Member

According to this ask.sagemath question, we have a very bizarre behavior for certain graphs:

G1=graphs.RandomGNP(5,0.5)
G1.plot(save_pos=True)
G2=G1.to_directed()
G2.delete_vertex(0)
G2.add_vertex(5)
G2.plot()
G1.plot()

gives

    485                 self._plot_components['vertex_labels'].append(text(str(v),
--> 486                     self._pos[v], rgbcolor=(0,0,0), zorder=8))

KeyError: 0

As if the zero vertex had been removed from G1. Plotting only one of them does NOT cause the error, and doing anything else seems fine. Reversing the order of the plotting gives KeyError: 5 as if the 5 vertex had never been added - in both cases it is the second plot which gives fits.

See comments below.

Component: graph theory

Author: David Coudert

Branch/Commit: 64fc436

Reviewer: Jeroen Demeyer

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

@kcrisman kcrisman added this to the sage-7.6 milestone Feb 23, 2017
@dcoudert
Copy link
Contributor

Commit: 34abce2

@dcoudert

This comment has been minimized.

@dcoudert
Copy link
Contributor

Author: David Coudert

@dcoudert
Copy link
Contributor

comment:1

In the to_directedmethod, we have:

        D = DiGraph(name           = self.name(),
                    pos            = self._pos,
                    multiedges     = self.allows_multiple_edges(),
                    loops          = self.allows_loops(),
                    implementation = implementation,
                    data_structure = (data_structure if data_structure!="static_sparse"
                                      else "sparse")) # we need a mutable copy

Hence, the dictionary _pos storing the positions of the vertices is the same.

This patch solves this issue passing a copy of the position dictionary.


New commits:

34abce2trac #22424: fix reported bug

@dcoudert
Copy link
Contributor

Branch: u/dcoudert/22424

@kcrisman
Copy link
Member Author

comment:2

Nice quick find. This makes perfect sense - and I see why it didn't show up before! Thanks.

You'll want to add some sort of doctest to verify that this is fixed.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 24, 2017

Changed commit from 34abce2 to 840b584

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 24, 2017

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

840b584trac #22424: doctest

@dcoudert
Copy link
Contributor

comment:4

The problem reported in this patch is reproducible as:

sage: G1=graphs.Grid2RandomGNP(5,0.5)
sage: gp1 = G1.graphplot(save_pos=True)
sage: G2=G1.to_directed()
sage: G2.delete_vertex(0)
sage: G2.add_vertex(5)
sage: gp2 = G2.graphplot()
sage: gp1 = G1.graphplot()

So I added this test to the ticket rather than a show or plot call.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 27, 2017

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

f0af172trac #22424: Merged with 7.6.beta5
abbd053trac #22424: corrected doctest

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 27, 2017

Changed commit from 840b584 to abbd053

@dcoudert
Copy link
Contributor

comment:6

Ready for review

@jdemeyer
Copy link

comment:7

Passing a copy to the DiGraph constructor is not the right fix.

Instead, DiGraph should not modify its input!

@jdemeyer
Copy link

comment:8

In other words: do not work around the bug but fix the bug instead.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 27, 2017

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

2d49d35trac #22424: fix the bug

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 27, 2017

Changed commit from abbd053 to 2d49d35

@dcoudert
Copy link
Contributor

comment:10

This should be a better way for both graphs and digraphs.

@dcoudert
Copy link
Contributor

dcoudert commented Mar 3, 2017

comment:12

Anyone to review this patch?

@jdemeyer
Copy link

jdemeyer commented Mar 3, 2017

comment:13

Two details:

  1. there is some trailing whitespace in this patch.

  2. why did you remove the plotting doctest in the last commit? Even if you changed the fix, you should keep that test.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 4, 2017

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

f0ebd9ctrac #22424: remove trailing white space and and doctest

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 4, 2017

Changed commit from 2d49d35 to f0ebd9c

@dcoudert
Copy link
Contributor

dcoudert commented Mar 4, 2017

comment:15

I have put back the plotting doctest and removed the trailing white space (always hard to find).

@jdemeyer
Copy link

jdemeyer commented Mar 6, 2017

comment:16

There is still trailing whitespace here:

+        The position dictionary is not the input one (:trac:`22424`)::
+
+            sage: my_pos = {0:(0,0), 1:(1,1)} <-----------------------------
+            sage: G = Graph([[0,1], [(0,1)]], pos=my_pos)
+            sage: my_pos == G._pos
+            True
+            sage: my_pos is G._pos
+            False

@jdemeyer
Copy link

jdemeyer commented Mar 6, 2017

Reviewer: Jeroen Demeyer

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 6, 2017

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

ec12c65trac #22424: Merged with 7.6.beta6
64fc436trac #22424: bloody trailing white space

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 6, 2017

Changed commit from f0ebd9c to 64fc436

@dcoudert
Copy link
Contributor

dcoudert commented Mar 6, 2017

comment:18

Sorry, removed.

@dcoudert
Copy link
Contributor

dcoudert commented Mar 6, 2017

comment:20

Thank you.

@vbraun
Copy link
Member

vbraun commented Mar 8, 2017

Changed branch from u/dcoudert/22424 to 64fc436

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants