#19477: Graph(filename)
-------------------------+-------------------------------------------------
Reporter: | Owner:
ncohen | Status: needs_review
Type: | Milestone: sage-6.10
enhancement | Resolution:
Priority: major | Merged in:
Component: graph | Reviewers:
theory | Work issues:
Keywords: | Commit:
Authors: | 836cef67d31fe19b405fd82a3f8a769acfa53280
Nathann Cohen | Stopgaps:
Report Upstream: N/A |
Branch: |
u/ncohen/19477 |
Dependencies: |
#19390 |
-------------------------+-------------------------------------------------
Comment (by dcoudert):
Hello Nathann,
I know you will cry, but your method to read edgelist graphs is not
generic enough and I suggest to use the method of networkx instead. Why:
{{{
sage: G = Graph()
sage: G.add_edge(4,5, {'color':3, 'weight':2.5})
sage: f = tmp_filename(ext='.edgelist')
sage: G.export_to_file(f,format='edgelist')
sage: import networkx
sage: gx = networkx.read_edgelist(f)
sage: h = Graph(gx)
sage: h.edges()
[(u'4', u'5', {'color': 3, 'weight': 2.5})]
sage: G.edges()
[(4, 5, {'color': 3, 'weight': 2.50000000000000})]
sage: line = "4 5 {'color':3, 'weight':2.5}"
sage: line.strip().split()
['4', '5', "{'color':3,", "'weight':2.5}"]
}}}
I agree this is ugly, but it's possible.
In fact, if you assume that a graph is either unweighted (with data on
edges) or weighted (with a unique number on the edges) then the method is
similar to `networkx.read_weighted_edgelist`.
The cases with loops and/or multiple edges are of course even more
complicated, and I don't know how to handle them in a easy manner.
{{{
sage: G.allow_multiple_edges(True)
sage: G.allow_loops(True)
sage: G.add_edge(4,5, {'color':42, 'weight':99})
sage: G.add_edge(3,3,12345)
sage: G.edges()
[(3, 3, 12345),
(4, 5, {'color': 3, 'weight': 2.50000000000000}),
(4, 5, {'color': 42, 'weight': 99})]
sage: G.export_to_file(f,format='edgelist')
sage: gx = networkx.read_edgelist(f)
sage: h = Graph(gx)
sage: h.edges()
[(u'4', u'5', {'weight': {'color': 42, 'weight': 99}})]
sage: gx.edges()
[(u'3', u'3'), (u'5', u'4')]
sage: gx.get_edge_data(4,5)
sage: gx.get_edge_data(u'5', u'4')
{'weight': {'color': 42, 'weight': 99}}
}}}
David.
--
Ticket URL: <http://trac.sagemath.org/ticket/19477#comment:2>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.