Python networkx.read_graphml() Examples
The following are 30
code examples of networkx.read_graphml().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: buildupgraph.py From complex_network with GNU General Public License v2.0 | 7 votes |
def read_graphml_with_position(filename): """Read a graph in GraphML format with position """ G = nx.read_graphml(filename) # rearrage node attributes x, y as position for networkx pos = dict() # A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2. node_and_x = nx.get_node_attributes(G, 'x') node_and_y = nx.get_node_attributes(G, 'y') for node in node_and_x: x = node_and_x[node] y = node_and_y[node] pos[node] = (x, y) # add node attribute `pos` to G nx.set_node_attributes(G, 'pos', pos) return G
Example #2
Source File: graphviz.py From complex_network with GNU General Public License v2.0 | 7 votes |
def read_graphml_with_position(cls, filename): """Read a graph in GraphML format with position """ G = nx.read_graphml(filename) # rearrage node attributes x, y as position for networkx pos = dict() # A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2. node_and_x = nx.get_node_attributes(G, 'x') node_and_y = nx.get_node_attributes(G, 'y') for node in node_and_x: x = node_and_x[node] y = node_and_y[node] pos[node] = (x, y) # add node attribute `pos` to G nx.set_node_attributes(G, 'pos', pos) return G
Example #3
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_unicode_attributes(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) node_type = str except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) node_type = unicode G.add_edge(name1, 'Radiohead', foo=name2) fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname, node_type=node_type) assert_equal(G._adj, H._adj) os.close(fd) os.unlink(fname)
Example #4
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_more_multigraph_keys(self): """Writing keys as edge id attributes means keys become strings. The original keys are stored as data, so read them back in if `make_str(key) == edge_id` This allows the adjacency to remain the same. """ G = nx.MultiGraph() G.add_edges_from([('a', 'b', 2), ('a', 'b', 3)]) fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname) assert_true(H.is_multigraph()) assert_edges_equal(G.edges(keys=True), H.edges(keys=True)) assert_equal(G._adj, H._adj) os.close(fd) os.unlink(fname)
Example #5
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_write_read_attribute_numeric_type_graphml(self): from xml.etree.ElementTree import parse G = self.attribute_numeric_type_graph fh = io.BytesIO() self.writer(G, fh, infer_numeric_types=True) fh.seek(0) H = nx.read_graphml(fh) fh.seek(0) assert_nodes_equal(G.nodes(), H.nodes()) assert_edges_equal(G.edges(), H.edges()) assert_edges_equal(G.edges(data=True), H.edges(data=True)) self.attribute_numeric_type_fh.seek(0) xml = parse(fh) # Children are the key elements, and the graph element children = xml.getroot().getchildren() assert_equal(len(children), 3) keys = [child.items() for child in children[:2]] assert_equal(len(keys), 2) assert_in(('attr.type', 'double'), keys[0]) assert_in(('attr.type', 'double'), keys[1])
Example #6
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_numpy_float32(self): try: import numpy as np except: return wt = np.float32(3.4) G = nx.Graph([(1, 2, {'weight': wt})]) fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname, node_type=int) assert_equal(G.edges, H.edges) wtG = G[1][2]['weight'] wtH = H[1][2]['weight'] assert_almost_equal(wtG, wtH, places=6) assert_equal(type(wtG), np.float32) assert_equal(type(wtH), float) os.close(fd) os.unlink(fname)
Example #7
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_unicode_attributes(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) node_type = str except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) node_type = unicode G.add_edge(name1, 'Radiohead', foo=name2) fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname, node_type=node_type) assert_equal(G._adj, H._adj) os.close(fd) os.unlink(fname)
Example #8
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_multigraph_keys(self): # Test that reading multigraphs uses edge id attributes as keys s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G" edgedefault="directed"> <node id="n0"/> <node id="n1"/> <edge id="e0" source="n0" target="n1"/> <edge id="e1" source="n0" target="n1"/> </graph> </graphml> """ fh = io.BytesIO(s.encode('UTF-8')) G = nx.read_graphml(fh) expected = [("n0", "n1", "e0"), ("n0", "n1", "e1")] assert_equal(sorted(G.edges(keys=True)), expected) fh.seek(0) H = nx.parse_graphml(s) assert_equal(sorted(H.edges(keys=True)), expected)
Example #9
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_unicode_escape(self): # test for handling json escaped stings in python 2 Issue #1880 import json a = dict(a='{"a": "123"}') # an object with many chars to escape try: # Python 3.x chr(2344) sa = json.dumps(a) except ValueError: # Python 2.6+ sa = unicode(json.dumps(a)) G = nx.Graph() G.graph['test'] = sa fh = io.BytesIO() self.writer(G, fh) fh.seek(0) H = nx.read_graphml(fh) assert_equal(G.graph['test'], H.graph['test'])
Example #10
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_undirected_edge_in_directed(self): s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G" edgedefault='directed'> <node id="n0"/> <node id="n1"/> <node id="n2"/> <edge source="n0" target="n1"/> <edge source="n1" target="n2" directed='false'/> </graph> </graphml>""" fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError, nx.read_graphml, fh) assert_raises(nx.NetworkXError, nx.parse_graphml, s)
Example #11
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_directed_edge_in_undirected(self): s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G"> <node id="n0"/> <node id="n1"/> <node id="n2"/> <edge source="n0" target="n1"/> <edge source="n1" target="n2" directed='true'/> </graph> </graphml>""" fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError, nx.read_graphml, fh) assert_raises(nx.NetworkXError, nx.parse_graphml, s)
Example #12
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_read_attribute_graphml(self): G = self.attribute_graph H = nx.read_graphml(self.attribute_fh) assert_nodes_equal(G.nodes(True), sorted(H.nodes(data=True))) ge = sorted(G.edges(data=True)) he = sorted(H.edges(data=True)) for a, b in zip(ge, he): assert_equal(a, b) self.attribute_fh.seek(0) I = nx.parse_graphml(self.attribute_data) assert_equal(sorted(G.nodes(True)), sorted(I.nodes(data=True))) ge = sorted(G.edges(data=True)) he = sorted(I.edges(data=True)) for a, b in zip(ge, he): assert_equal(a, b)
Example #13
Source File: chempiler_setup.py From ChemputerSoftware with BSD 3-Clause "New" or "Revised" License | 6 votes |
def load_graph(self, file): """ Loads a .graphml file containing the architecture, discards unnecessary information and relabels the nodes. :param file: GraphML file containing the architecture :return: sanitised graph object """ graph = nx.read_graphml(file) mapping = {} for node in graph.nodes(): label = graph.node[node].pop("label", "NaN") mapping[node] = label graph = nx.relabel_nodes(graph, mapping) for node in graph.nodes(): graph.node[node].pop("x", None) graph.node[node].pop("y", None) return graph
Example #14
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_read_attribute_graphml(self): G = self.attribute_graph H = nx.read_graphml(self.attribute_fh) assert_nodes_equal(G.nodes(True), sorted(H.nodes(data=True))) ge = sorted(G.edges(data=True)) he = sorted(H.edges(data=True)) for a, b in zip(ge, he): assert_equal(a, b) self.attribute_fh.seek(0) I = nx.parse_graphml(self.attribute_data) assert_equal(sorted(G.nodes(True)), sorted(I.nodes(data=True))) ge = sorted(G.edges(data=True)) he = sorted(I.edges(data=True)) for a, b in zip(ge, he): assert_equal(a, b)
Example #15
Source File: test_graphml.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_unicode(self): G = nx.Graph() try: # Python 3.x name1 = chr(2344) + chr(123) + chr(6543) name2 = chr(5543) + chr(1543) + chr(324) node_type=str except ValueError: # Python 2.6+ name1 = unichr(2344) + unichr(123) + unichr(6543) name2 = unichr(5543) + unichr(1543) + unichr(324) node_type=unicode G.add_edge(name1, 'Radiohead', attr_dict={'foo': name2}) fd, fname = tempfile.mkstemp() nx.write_graphml(G, fname) H = nx.read_graphml(fname,node_type=node_type) assert_equal(G.adj, H.adj) os.close(fd) os.unlink(fname)
Example #16
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_directed_edge_in_undirected(self): s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G"> <node id="n0"/> <node id="n1"/> <node id="n2"/> <edge source="n0" target="n1"/> <edge source="n1" target="n2" directed='true'/> </graph> </graphml>""" fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError, nx.read_graphml, fh) assert_raises(nx.NetworkXError, nx.parse_graphml, s)
Example #17
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_undirected_edge_in_directed(self): s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G" edgedefault='directed'> <node id="n0"/> <node id="n1"/> <node id="n2"/> <edge source="n0" target="n1"/> <edge source="n1" target="n2" directed='false'/> </graph> </graphml>""" fh = io.BytesIO(s.encode('UTF-8')) assert_raises(nx.NetworkXError, nx.read_graphml, fh) assert_raises(nx.NetworkXError, nx.parse_graphml, s)
Example #18
Source File: test_graphml.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_default_attribute(self): G=nx.Graph() G.add_node(1,label=1,color='green') G.add_path([0,1,2,3]) G.add_edge(1,2,weight=3) G.graph['node_default']={'color':'yellow'} G.graph['edge_default']={'weight':7} fh = io.BytesIO() nx.write_graphml(G,fh) fh.seek(0) H=nx.read_graphml(fh,node_type=int) assert_equal(sorted(G.nodes()),sorted(H.nodes())) assert_equal( sorted(sorted(e) for e in G.edges()), sorted(sorted(e) for e in H.edges())) assert_equal(G.graph,H.graph)
Example #19
Source File: topology.py From YAFS with MIT License | 6 votes |
def load_graphml(self,filename): warnings.warn("The load_graphml function is deprecated and " "will be removed in version 2.0.0. " "Use NX.READ_GRAPHML function instead.", FutureWarning, stacklevel=8 ) self.G = nx.read_graphml(filename) attEdges = {} for k in self.G.edges(): attEdges[k] = {"BW": 1, "PR": 1} nx.set_edge_attributes(self.G, values=attEdges) attNodes = {} for k in self.G.nodes(): attNodes[k] = {"IPT": 1} nx.set_node_attributes(self.G, values=attNodes) for k in self.G.nodes(): self.nodeAttributes[k] = self.G.node[k] #it has "id" att. TODO IMPROVE
Example #20
Source File: test_graphml.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_read_attribute_graphml(self): G=self.attribute_graph H=nx.read_graphml(self.attribute_fh) assert_equal(sorted(G.nodes(True)),sorted(H.nodes(data=True))) ge=sorted(G.edges(data=True)) he=sorted(H.edges(data=True)) for a,b in zip(ge,he): assert_equal(a,b) self.attribute_fh.seek(0) I=nx.parse_graphml(self.attribute_data) assert_equal(sorted(G.nodes(True)),sorted(I.nodes(data=True))) ge=sorted(G.edges(data=True)) he=sorted(I.edges(data=True)) for a,b in zip(ge,he): assert_equal(a,b)
Example #21
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_multigraph_keys(self): # Test that reading multigraphs uses edge id attributes as keys s = """<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G" edgedefault="directed"> <node id="n0"/> <node id="n1"/> <edge id="e0" source="n0" target="n1"/> <edge id="e1" source="n0" target="n1"/> </graph> </graphml> """ fh = io.BytesIO(s.encode('UTF-8')) G = nx.read_graphml(fh) expected = [("n0", "n1", "e0"), ("n0", "n1", "e1")] assert_equal(sorted(G.edges(keys=True)), expected) fh.seek(0) H = nx.parse_graphml(s) assert_equal(sorted(H.edges(keys=True)), expected)
Example #22
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_write_read_attribute_numeric_type_graphml(self): from xml.etree.ElementTree import parse G = self.attribute_numeric_type_graph fh = io.BytesIO() self.writer(G, fh, infer_numeric_types=True) fh.seek(0) H = nx.read_graphml(fh) fh.seek(0) assert_nodes_equal(G.nodes(), H.nodes()) assert_edges_equal(G.edges(), H.edges()) assert_edges_equal(G.edges(data=True), H.edges(data=True)) self.attribute_numeric_type_fh.seek(0) xml = parse(fh) # Children are the key elements, and the graph element children = xml.getroot().getchildren() assert_equal(len(children), 3) keys = [child.items() for child in children[:2]] assert_equal(len(keys), 2) assert_in(('attr.type', 'double'), keys[0]) assert_in(('attr.type', 'double'), keys[1])
Example #23
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_more_multigraph_keys(self): """Writing keys as edge id attributes means keys become strings. The original keys are stored as data, so read them back in if `make_str(key) == edge_id` This allows the adjacency to remain the same. """ G = nx.MultiGraph() G.add_edges_from([('a', 'b', 2), ('a', 'b', 3)]) fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname) assert_true(H.is_multigraph()) assert_edges_equal(G.edges(keys=True), H.edges(keys=True)) assert_equal(G._adj, H._adj) os.close(fd) os.unlink(fname)
Example #24
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_multigraph_to_graph(self): # test converting multigraph to graph if no parallel edges found G = nx.MultiGraph() G.add_edges_from([('a', 'b', 2), ('b', 'c', 3)]) # no multiedges fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname) assert_false(H.is_multigraph()) os.close(fd) os.unlink(fname)
Example #25
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_preserve_multi_edge_data(self): """ Test that data and keys of edges are preserved on consequent write and reads """ G = nx.MultiGraph() G.add_node(1) G.add_node(2) G.add_edges_from([ # edges with no data, no keys: (1, 2), # edges with only data: (1, 2, dict(key='data_key1')), (1, 2, dict(id='data_id2')), (1, 2, dict(key='data_key3', id='data_id3')), # edges with both data and keys: (1, 2, 103, dict(key='data_key4')), (1, 2, 104, dict(id='data_id5')), (1, 2, 105, dict(key='data_key6', id='data_id7')), ]) fh = io.BytesIO() nx.write_graphml(G, fh) fh.seek(0) H = nx.read_graphml(fh, node_type=int) assert_edges_equal( G.edges(data=True, keys=True), H.edges(data=True, keys=True) ) assert_equal(G._adj, H._adj)
Example #26
Source File: test_graphml.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_numpy_float(self): try: import numpy as np except: return wt = np.float(3.4) G = nx.Graph([(1, 2, {'weight': wt})]) fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname, node_type=int) assert_equal(G._adj, H._adj) os.close(fd) os.unlink(fname)
Example #27
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_multigraph_to_graph(self): # test converting multigraph to graph if no parallel edges found G = nx.MultiGraph() G.add_edges_from([('a', 'b', 2), ('b', 'c', 3)]) # no multiedges fd, fname = tempfile.mkstemp() self.writer(G, fname) H = nx.read_graphml(fname) assert_false(H.is_multigraph()) os.close(fd) os.unlink(fname)
Example #28
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_default_attribute(self): G = nx.Graph(name="Fred") G.add_node(1, label=1, color='green') nx.add_path(G, [0, 1, 2, 3]) G.add_edge(1, 2, weight=3) G.graph['node_default'] = {'color': 'yellow'} G.graph['edge_default'] = {'weight': 7} fh = io.BytesIO() self.writer(G, fh) fh.seek(0) H = nx.read_graphml(fh, node_type=int) assert_nodes_equal(G.nodes(), H.nodes()) assert_edges_equal(G.edges(), H.edges()) assert_equal(G.graph, H.graph)
Example #29
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_preserve_multi_edge_data(self): """ Test that data and keys of edges are preserved on consequent write and reads """ G = nx.MultiGraph() G.add_node(1) G.add_node(2) G.add_edges_from([ # edges with no data, no keys: (1, 2), # edges with only data: (1, 2, dict(key='data_key1')), (1, 2, dict(id='data_id2')), (1, 2, dict(key='data_key3', id='data_id3')), # edges with both data and keys: (1, 2, 103, dict(key='data_key4')), (1, 2, 104, dict(id='data_id5')), (1, 2, 105, dict(key='data_key6', id='data_id7')), ]) fh = io.BytesIO() nx.write_graphml(G, fh) fh.seek(0) H = nx.read_graphml(fh, node_type=int) assert_edges_equal( G.edges(data=True, keys=True), H.edges(data=True, keys=True) ) assert_equal(G._adj, H._adj)
Example #30
Source File: test_graphml.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_read_simple_directed_graphml(self): G = self.simple_directed_graph H = nx.read_graphml(self.simple_directed_fh) assert_equal(sorted(G.nodes()), sorted(H.nodes())) assert_equal(sorted(G.edges()), sorted(H.edges())) assert_equal(sorted(G.edges(data=True)), sorted(H.edges(data=True))) self.simple_directed_fh.seek(0) I = nx.parse_graphml(self.simple_directed_data) assert_equal(sorted(G.nodes()), sorted(I.nodes())) assert_equal(sorted(G.edges()), sorted(I.edges())) assert_equal(sorted(G.edges(data=True)), sorted(I.edges(data=True)))