本文整理匯總了Python中networkx.read_graphml方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.read_graphml方法的具體用法?Python networkx.read_graphml怎麽用?Python networkx.read_graphml使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.read_graphml方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: read_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
def read_graph(self, subgraph_file=None):
if subgraph_file is None:
subraph_file = self.context_graph_file
logging.info("Writing graph.")
# write the graph out
file_format = subgraph_file.split(".")[-1]
if file_format == "graphml":
return nx.read_graphml(subgraph_file)
elif file_format == "gml":
return nx.read_gml(subgraph_file)
elif file_format == "gexf":
return nx.read_gexf(subgraph_file)
elif file_format == "net":
return nx.read_pajek(subgraph_file)
elif file_format == "yaml":
return nx.read_yaml(subgraph_file)
elif file_format == "gpickle":
return nx.read_gpickle(subgraph_file)
else:
logging.warning("File format not found, returning empty graph.")
return nx.MultiDiGraph()
示例2: load_graphml
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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
示例3: read_graphml_with_position
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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
示例4: read_graphml_with_position
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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
示例5: test_read_attribute_graphml
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例6: test_default_attribute
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例7: test_unicode
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例8: load_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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
示例9: test_read_attribute_graphml
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例10: test_directed_edge_in_undirected
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例11: test_undirected_edge_in_directed
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例12: test_multigraph_keys
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例13: test_write_read_attribute_numeric_type_graphml
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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])
示例14: test_more_multigraph_keys
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)
示例15: test_numpy_float32
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import read_graphml [as 別名]
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)