Opened 3 years ago
Closed 3 years ago
#25120 closed defect (fixed)
dot2tex edge coloring is broken
Reported by:  vdelecroix  Owned by:  

Priority:  major  Milestone:  sage8.2 
Component:  graphics  Keywords:  thursdaysbdx 
Cc:  slabbe, nthiery  Merged in:  
Authors:  Vincent Delecroix  Reviewers:  Sébastien Labbé 
Report Upstream:  N/A  Work issues:  
Branch:  b4176d5 (Commits)  Commit:  b4176d59c0c369e6ac246d05ed8b3377e9109e97 
Dependencies:  Stopgaps: 
Description
From the documentation
sage: sage.graphs.graph_latex.GraphLatex.set_option? ...  ``edge_colors``  a dictionary whose keys are edges of the graph and whose values are colors. ...
Though
sage: G = Graph([(0,1)]) sage: G.set_latex_options(format='dot2tex', edge_colors = {(0,1): 'red'}) sage: view(G)  AssertionError Traceback (most recent call last) <ipythoninput46e13ee6ead400> in <module>() > 1 view(G) /opt/sage/local/lib/python2.7/sitepackages/sage/misc/latex.pyc in view(objects, title, debug, sep, tiny, pdflatex, engine, viewer, tightpage, margin, mode, combine_all, **kwds) 2201 latex_options = {} 2202 > 2203 s = _latex_file_(objects, title=title, sep=sep, tiny=tiny, debug=debug, **latex_options) 2204 if engine is None: 2205 engine = _Latex_prefs._option["engine"] /opt/sage/local/lib/python2.7/sitepackages/sage/misc/latex.pyc in _latex_file_(objects, title, debug, sep, tiny, math_left, math_right, extra_preamble) 1767 for i in range(len(objects)): 1768 x = objects[i] > 1769 L = latex(x) 1770 if '\\begin{pgfpicture}' in L: 1771 # Resize the pgf figure to the text width if larger. /opt/sage/local/lib/python2.7/sitepackages/sage/misc/latex.pyc in __call__(self, x, combine_all) 921 """ 922 if has_latex_attr(x): > 923 return LatexExpr(x._latex_()) 924 try: 925 f = latex_table[type(x)] /opt/sage/local/lib/python2.7/sitepackages/sage/graphs/generic_graph.pyc in _latex_(self) 768 setup_latex_preamble() 769 > 770 return self.latex_options().latex() 771 772 def _matrix_(self, R=None): /opt/sage/local/lib/python2.7/sitepackages/sage/graphs/graph_latex.pyc in latex(self) 1302 return self.tkz_picture() 1303 elif format == "dot2tex": > 1304 return self.dot2tex_picture() 1305 1306 def dot2tex_picture(self): /opt/sage/local/lib/python2.7/sitepackages/sage/graphs/graph_latex.pyc in dot2tex_picture(self) 1356 options = self.__graphlatex_options.copy() 1357 options.update(self._options) > 1358 dotdata = self._graph.graphviz_string(labels="latex", **options) 1359 import dot2tex 1360 return dot2tex.dot2tex(dotdata, /opt/sage/local/lib/python2.7/sitepackages/sage/misc/decorators.pyc in wrapper(*args, **kwds) 562 options['__original_opts'] = kwds 563 options.update(kwds) > 564 return func(*args, **options) 565 566 #Add the options specified by @options to the signature of the wrapped /opt/sage/local/lib/python2.7/sitepackages/sage/graphs/generic_graph.pyc in graphviz_string(self, **options) 20705 for edge in options['edge_colors'][color]: 20706 assert isinstance(edge, (list, tuple)) and len(edge) >= 2 and len(edge) <= 3,\ > 20707 "%s is not a valid format for edge"%(edge) 20708 u = edge[0] 20709 v = edge[1] AssertionError: r is not a valid format for edge
The confusion comes from the fact that the convention for edge_colors
with .plot()
is the reverse
sage: G.plot? ... * "edge_colors"  a dictionary specifying edge colors: each key is a color recognized by matplotlib, and each entry is a list of edges. ...
So is the case for graphviz_string
sage: G.graphviz_string? ... * "edge_colors"  (default: None) a dictionary whose keys are colors and values are list of edges. } ...
Change History (8)
comment:1 Changed 3 years ago by
 Branch set to u/vdelecroix/25120
 Commit set to 11e8cb907259ab545d51dbd2589ef839ec279ada
 Status changed from new to needs_review
comment:2 Changed 3 years ago by
 Commit changed from 11e8cb907259ab545d51dbd2589ef839ec279ada to b4176d59c0c369e6ac246d05ed8b3377e9109e97
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
b4176d5  dirty fix for edge colors with dot2tex

comment:3 Changed 3 years ago by
Hi Vincent,
Ouch, this clash of option format is annoying indeed. I went through the code and patch, and not sure what's the best way out. Especially since I am pretty sure there is code around using set_latex_options
with edge_options
already in the graphviz_string format.
I am available for a phone brainstorm this morning if you are available.
comment:4 Changed 3 years ago by
What if we rather change the documentation of sage.graphs.graph_latex.GraphLatex.set_option
so that it takes a dict colors > list of edges
like the others. Then, this ticket would do some modification of the tkz_picture
code to revert the dict the other way around?
This way, the dict edge_colors
would always have the same form as input.
comment:5 Changed 3 years ago by
This clash of option was there and will remain after the ticket. I will not try to fix it. Would it be possible to review this ticket (which does fix an issue) without discussing the option clash?
comment:6 Changed 3 years ago by
Indeed, I realised soon after that my suggestion would have been backward incompatible.
Once stuff are in Sage we can not change their interface anymore. This is why I am keeping my TikzPicture
in my personnal package for now, because I am still sometimes changing its interface.
To make sure all the test are tested in the file, I did:
sage t showskipped optional=dot2tex,sage,graphviz src/sage/graphs/graph_latex.py
Thanks for digging this problem and fixing this issue. I may have stumble over this many years ago. But, once I realized that color_by_labels=True
was working and I have always been using this way and forgot about the problem you are fixing.
comment:7 Changed 3 years ago by
 Keywords thursdaysbdx added
 Reviewers set to Sébastien Labbé
 Status changed from needs_review to positive_review
comment:8 Changed 3 years ago by
 Branch changed from u/vdelecroix/25120 to b4176d59c0c369e6ac246d05ed8b3377e9109e97
 Resolution set to fixed
 Status changed from positive_review to closed
Any better idea welcome!
New commits:
_invert_unit > inverse_of_unit
dirty fix for edge colors with dot2tex