本文整理汇总了Python中networkx.MultiGraph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.MultiGraph方法的具体用法?Python networkx.MultiGraph怎么用?Python networkx.MultiGraph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.MultiGraph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gdf_to_nx
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def gdf_to_nx(gdf_network, approach="primal", length="mm_len"):
"""
Convert LineString GeoDataFrame to networkx.MultiGraph
Parameters
----------
gdf_network : GeoDataFrame
GeoDataFrame containing objects to convert
approach : str, default 'primal'
Decide wheter genereate ``'primal'`` or ``'dual'`` graph.
length : str, default mm_len
name of attribute of segment length (geographical) which will be saved to graph
Returns
-------
networkx.Graph
Graph
"""
gdf_network = gdf_network.copy()
if "key" in gdf_network.columns:
gdf_network.rename(columns={"key": "__key"}, inplace=True)
# generate graph from GeoDataFrame of LineStrings
net = nx.MultiGraph()
net.graph["crs"] = gdf_network.crs
gdf_network[length] = gdf_network.geometry.length
fields = list(gdf_network.columns)
if approach == "primal":
_generate_primal(net, gdf_network, fields)
elif approach == "dual":
_generate_dual(net, gdf_network, fields)
else:
raise ValueError(
"Approach {} is not supported. Use 'primal' or 'dual'.".format(approach)
)
return net
示例2: format_graph_for_json
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def format_graph_for_json(graph, raise_errors=True):
"""
Currently, only supported types for graph are Graph, DiGraph, MultiGraph, and MultiDiGraph. graph must
be an instance of one of these types, not a class that inherits from one.
"""
graph_dict = nx.to_dict_of_dicts(graph)
graph_type = ''
for key, value in accepted_types.items():
if type(graph) == key:
graph_type = value
break
if graph_type == '':
if raise_errors:
raise TypeError('parameter graph is not of the accepted types.graph is of'
'type {}'.format(str(type(graph))))
else:
graph_type = 'other'
return {'graph_type': graph_type, 'graph_dict': graph_dict}
示例3: merge
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def merge(self, ontologies):
"""
Merges specified ontology into current ontology
"""
if self.xref_graph is None:
self.xref_graph = nx.MultiGraph()
logger.info("Merging source: {} xrefs: {}".format(self, len(self.xref_graph.edges())))
for ont in ontologies:
logger.info("Merging {} into {}".format(ont, self))
g = self.get_graph()
srcg = ont.get_graph()
for n in srcg.nodes():
g.add_node(n, **srcg.node[n])
for (o,s,m) in srcg.edges(data=True):
g.add_edge(o,s,**m)
if ont.xref_graph is not None:
for (o,s,m) in ont.xref_graph.edges(data=True):
self.xref_graph.add_edge(o,s,**m)
示例4: generate_predicted_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def generate_predicted_graph(self, dpids, switch_links, host_links, host_information):
"""Creates the predicted network graph"""
self.predicted_network_graph = networkx.MultiGraph()
for dpid in dpids:
self.predicted_network_graph.add_node(dpid)
for link in switch_links:
u, v = link
self.predicted_network_graph.add_edge(self.dpids[u], self.dpids[v])
self.host_name_to_index = {}
for host_id, host_info in host_information.items():
host_name = host_info['host'].name
self.host_name_to_index[host_name] = host_id
self.predicted_network_graph.add_node(host_name)
links = host_links[host_id]
for link in links:
self.predicted_network_graph.add_edge(host_name, self.dpids[link])
示例5: read_dot
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def read_dot(path):
"""Return a NetworkX MultiGraph or MultiDiGraph from a dot file on path.
Parameters
----------
path : filename or file handle
Returns
-------
G : NetworkX multigraph
A MultiGraph or MultiDiGraph.
Notes
-----
Use G = nx.Graph(read_dot(path)) to return a Graph instead of a MultiGraph.
"""
import pydotplus
data = path.read()
P = pydotplus.graph_from_dot_data(data)
return from_pydot(P)
示例6: test_set_edge_attributes_multi
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def test_set_edge_attributes_multi():
graphs = [nx.MultiGraph(), nx.MultiDiGraph()]
for G in graphs:
G = nx.path_graph(3, create_using=G)
# Test single value
attr = 'hello'
vals = 3
nx.set_edge_attributes(G, attr, vals)
assert_equal(G[0][1][0][attr], vals)
assert_equal(G[1][2][0][attr], vals)
# Test multiple values
attr = 'hi'
edges = [(0,1,0), (1,2,0)]
vals = dict(zip(edges, range(len(edges))))
nx.set_edge_attributes(G, attr, vals)
assert_equal(G[0][1][0][attr], 0)
assert_equal(G[1][2][0][attr], 1)
示例7: test_get_edge_attributes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def test_get_edge_attributes():
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
for G in graphs:
G = nx.path_graph(3, create_using=G)
attr = 'hello'
vals = 100
nx.set_edge_attributes(G, attr, vals)
attrs = nx.get_edge_attributes(G, attr)
assert_equal(len(attrs), 2)
if G.is_multigraph():
keys = [(0,1,0), (1,2,0)]
else:
keys = [(0,1), (1,2)]
for key in keys:
assert_equal(attrs[key], 100)
示例8: __len__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def __len__(self):
"""Return the number of nodes. Use the expression 'len(G)'.
Returns
-------
nnodes : int
The number of nodes in the graph.
Examples
--------
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> len(G)
4
"""
return len(self.node)
示例9: number_of_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def number_of_nodes(self):
"""Return the number of nodes in the graph.
Returns
-------
nnodes : int
The number of nodes in the graph.
See Also
--------
order, __len__ which are identical
Examples
--------
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> len(G)
3
"""
return len(self.node)
示例10: has_node
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def has_node(self, n):
"""Return True if the graph contains the node n.
Parameters
----------
n : node
Examples
--------
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.has_node(0)
True
It is more readable and simpler to use
>>> 0 in G
True
"""
try:
return n in self.node
except TypeError:
return False
示例11: neighbors_iter
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def neighbors_iter(self, n):
"""Return an iterator over all neighbors of node n.
Examples
--------
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> [n for n in G.neighbors_iter(0)]
[1]
Notes
-----
It is faster to use the idiom "in G[0]", e.g.
>>> G = nx.path_graph(4)
>>> [n for n in G[0]]
[1]
"""
try:
return iter(self.adj[n])
except KeyError:
raise NetworkXError("The node %s is not in the graph."%(n,))
示例12: adjacency_list
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def adjacency_list(self):
"""Return an adjacency list representation of the graph.
The output adjacency list is in the order of G.nodes().
For directed graphs, only outgoing adjacencies are included.
Returns
-------
adj_list : lists of lists
The adjacency structure of the graph as a list of lists.
See Also
--------
adjacency_iter
Examples
--------
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.adjacency_list() # in order given by G.nodes()
[[1], [0, 2], [1, 3], [2]]
"""
return list(map(list,iter(self.adj.values())))
示例13: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def setUp(self):
super(TestGraphCanvasMultiGraph, self).setUp()
# Add in some extra edges
G = nx.MultiGraph(self.input_G)
G.add_edge('a','c')
G.add_edge('out',12)
G.add_edge('out',12)
G.add_edge('out',12)
self.input_G = G.copy()
# Viewer under test
self.a = nxv.GraphCanvas(G)
示例14: __init__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def __init__(self, pairs):
##self.graph = nx.MultiGraph()
self.graph = nx.Graph()
self.graph.add_edges_from(pairs)
self.lattice_points = self.graph.nodes()
self.pairs = self.graph.edges()
self.interaction = -1
self.field = 0
self.energy = None
示例15: create_multi_exchange_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import MultiGraph [as 别名]
def create_multi_exchange_graph(exchanges: list, digraph=False):
"""
Returns a MultiGraph representing the markets for each exchange in exchanges. Each edge represents a market.
Note: does not add edge weights using the ticker's ask and bid prices.
exchange.load_markets() must have been called for each exchange in exchanges. Will throw a ccxt error if it has not.
todo: check which error.
"""
if digraph:
graph = nx.MultiDiGraph()
else:
graph = nx.MultiGraph()
for exchange in exchanges:
for market_name in exchange.symbols:
try:
base_currency, quote_currency = market_name.split('/')
# if ccxt returns a market in incorrect format (e.g FX_BTC_JPY on BitFlyer)
except ValueError:
continue
graph.add_edge(base_currency,
quote_currency,
market_name=market_name,
exchange_name=exchange.name.lower())
if digraph:
graph.add_edge(quote_currency,
base_currency,
market_name=market_name,
exchange_name=exchange.name.lower())
return graph