Returns a EasyGraph Graph or DiGraph from a PyGraphviz graph. Parameters ---------- A : PyGraphviz AGraph A graph created with PyGraphviz create_using : EasyGraph graph constructor, optional (default=None) Graph type to create. If graph instance, then cleared before po
(A, create_using=None)
| 5 | |
| 6 | |
| 7 | def from_agraph(A, create_using=None): |
| 8 | """Returns a EasyGraph Graph or DiGraph from a PyGraphviz graph. |
| 9 | |
| 10 | Parameters |
| 11 | ---------- |
| 12 | A : PyGraphviz AGraph |
| 13 | A graph created with PyGraphviz |
| 14 | |
| 15 | create_using : EasyGraph graph constructor, optional (default=None) |
| 16 | Graph type to create. If graph instance, then cleared before populated. |
| 17 | If `None`, then the appropriate Graph type is inferred from `A`. |
| 18 | |
| 19 | Examples |
| 20 | -------- |
| 21 | >>> K5 = eg.complete_graph(5) |
| 22 | >>> A = eg.to_agraph(K5) |
| 23 | >>> G = eg.from_agraph(A) |
| 24 | |
| 25 | Notes |
| 26 | ----- |
| 27 | The Graph G will have a dictionary G.graph_attr containing |
| 28 | the default graphviz attributes for graphs, nodes and edges. |
| 29 | |
| 30 | Default node attributes will be in the dictionary G.node_attr |
| 31 | which is keyed by node. |
| 32 | |
| 33 | Edge attributes will be returned as edge data in G. With |
| 34 | edge_attr=False the edge data will be the Graphviz edge weight |
| 35 | attribute or the value 1 if no edge weight attribute is found. |
| 36 | |
| 37 | """ |
| 38 | if create_using is None: |
| 39 | if A.is_directed(): |
| 40 | if A.is_strict(): |
| 41 | create_using = eg.DiGraph |
| 42 | else: |
| 43 | create_using = eg.MultiDiGraph |
| 44 | else: |
| 45 | if A.is_strict(): |
| 46 | create_using = eg.Graph |
| 47 | else: |
| 48 | create_using = eg.MultiGraph |
| 49 | |
| 50 | # assign defaults |
| 51 | N = eg.empty_graph(0, create_using) |
| 52 | if A.name is not None: |
| 53 | N.name = A.name |
| 54 | |
| 55 | # add graph attributes |
| 56 | N.graph.update(A.graph_attr) |
| 57 | |
| 58 | # add nodes, attributes to N.node_attr |
| 59 | for n in A.nodes(): |
| 60 | str_attr = {str(k): v for k, v in n.attr.items()} |
| 61 | N.add_node(str(n), **str_attr) |
| 62 | |
| 63 | # add edges, assign edge data as dictionary of attributes |
| 64 | for e in A.edges(): |
no test coverage detected