本文整理匯總了Python中networkx.exception.NetworkXError方法的典型用法代碼示例。如果您正苦於以下問題:Python exception.NetworkXError方法的具體用法?Python exception.NetworkXError怎麽用?Python exception.NetworkXError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx.exception
的用法示例。
在下文中一共展示了exception.NetworkXError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: remove_component
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def remove_component(self, component):
"""
Removes the specified component from the factor graph.
:param component: The component to remove.
:type component: ModelComponent
"""
if not isinstance(component, ModelComponent):
raise ModelSpecificationError(
"Attempted to remove an object that isn't a ModelComponent.")
try:
self.components_graph.remove_node(component) # implicitly removes edges
except NetworkXError as e:
raise ModelSpecificationError("Attempted to remove a node " + str(component) + " that isn't in the graph.")
if component.name is not None:
try:
if getattr(self, component.name) is component:
delattr(self, component.name)
except AttributeError:
pass
component.graph = None
示例2: neighbors_iter
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [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,))
示例3: neighbors_iter
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [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,))
示例4: neighbors
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def neighbors(self, n):
"""Return a list of the nodes connected to the node n in
the dense graph.
Parameters
----------
n : node
A node in the graph
Returns
-------
nlist : list
A list of nodes that are adjacent to n.
Raises
------
NetworkXError
If the node n is not in the graph.
"""
try:
return list(set(self.adj) - set(self.adj[n]) - set([n]))
except KeyError:
raise NetworkXError("The node %s is not in the graph."%(n,))
示例5: predecessors
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def predecessors(self, n):
"""Returns an iterator over predecessor nodes of n.
A predecessor of n is a node m such that there exists a directed
edge from m to n.
Parameters
----------
n : node
A node in the graph
Raises
-------
NetworkXError
If n is not in the graph.
See Also
--------
successors
"""
try:
return iter(self._pred[n])
except KeyError:
raise NetworkXError("The node %s is not in the digraph." % (n,))
示例6: dorogovtsev_goltsev_mendes_graph
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def dorogovtsev_goltsev_mendes_graph(n, create_using=None):
"""Returns the hierarchically constructed Dorogovtsev-Goltsev-Mendes graph.
n is the generation.
See: arXiv:/cond-mat/0112143 by Dorogovtsev, Goltsev and Mendes.
"""
G = empty_graph(0, create_using)
if G.is_directed():
raise NetworkXError("Directed Graph not supported")
if G.is_multigraph():
raise NetworkXError("Multigraph not supported")
G.add_edge(0, 1)
if n == 0:
return G
new_node = 2 # next node to be added
for i in range(1, n + 1): # iterate over number of generations.
last_generation_edges = list(G.edges())
number_of_edges_in_last_generation = len(last_generation_edges)
for j in range(0, number_of_edges_in_last_generation):
G.add_edge(new_node, last_generation_edges[j][0])
G.add_edge(new_node, last_generation_edges[j][1])
new_node += 1
return G
示例7: ladder_graph
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def ladder_graph(n, create_using=None):
"""Returns the Ladder graph of length n.
This is two paths of n nodes, with
each pair connected by a single edge.
Node labels are the integers 0 to 2*n - 1.
"""
G = empty_graph(2 * n, create_using)
if G.is_directed():
raise NetworkXError("Directed Graph not supported")
G.add_edges_from(pairwise(range(n)))
G.add_edges_from(pairwise(range(n, 2 * n)))
G.add_edges_from((v, v + n) for v in range(n))
return G
示例8: __init__
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def __init__(self, graph):
if not graph.is_multigraph():
msg = 'Wrong View class. Use DiGraphView.'
raise NetworkXError(msg)
self._graph = graph
self.root_graph = graph
while hasattr(self.root_graph, '_graph'):
self.root_graph = self.root_graph._graph
self.graph = graph.graph
self._node = graph._node
if graph.is_directed():
self._pred = graph._pred
self._succ = graph._succ
else:
self._pred = graph._adj
self._succ = graph._adj
self._adj = self._succ
示例9: data_to_graph6
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def data_to_graph6(data):
"""Convert 6-bit integer sequence to graph6 character sequence."""
if len(data) > 0 and (min(data) < 0 or max(data) > 63):
raise NetworkXError("graph6 data units must be within 0..63")
return ''.join([chr(d+63) for d in data])
示例10: n_to_data
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def n_to_data(n):
"""Convert an integer to one-, four- or eight-unit graph6 sequence."""
if n < 0:
raise NetworkXError("Numbers in graph6 format must be non-negative.")
if n <= 62:
return [n]
if n <= 258047:
return [63, (n>>12) & 0x3f, (n>>6) & 0x3f, n & 0x3f]
if n <= 68719476735:
return [63, 63,
(n>>30) & 0x3f, (n>>24) & 0x3f, (n>>18) & 0x3f,
(n>>12) & 0x3f, (n>>6) & 0x3f, n & 0x3f]
raise NetworkXError("Numbers above 68719476735 are not supported by graph6")
示例11: write_sparse6
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def write_sparse6(G, path, nodes=None, header=True):
"""Write graph G to given path in sparse6 format.
Parameters
----------
G : Graph (undirected)
path : file or string
File or filename to write
nodes: list or iterable
Nodes are labeled 0...n-1 in the order provided. If None the ordering
given by G.nodes() is used.
header: bool
If True add '>>sparse6<<' string to head of data
Raises
------
NetworkXError
If the graph is directed
Examples
--------
>>> G = nx.Graph([(0, 1), (0, 1), (0, 1)])
>>> nx.write_sparse6(G, 'test.s6')
See Also
--------
read_sparse6, parse_sparse6, generate_sparse6
Notes
-----
The format does not support edge or node labels.
References
----------
Sparse6 specification:
http://cs.anu.edu.au/~bdm/data/formats.txt for details.
"""
path.write(generate_sparse6(G, nodes=nodes, header=header))
path.write('\n')
示例12: remove_edge
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def remove_edge(self, u, v):
"""Remove the edge between u and v.
Parameters
----------
u, v : nodes
Remove the edge between nodes u and v.
Raises
------
NetworkXError
If there is not an edge between u and v.
See Also
--------
remove_edges_from : remove a collection of edges
Examples
--------
>>> G = nx.Graph() # or DiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.remove_edge(0,1)
>>> e = (1,2)
>>> G.remove_edge(*e) # unpacks e from an edge tuple
>>> e = (2,3,{'weight':7}) # an edge with attribute data
>>> G.remove_edge(*e[:2]) # select first part of edge tuple
"""
try:
del self.succ[u][v]
del self.pred[v][u]
except KeyError:
raise NetworkXError("The edge %s-%s not in graph."%(u,v))
示例13: successors_iter
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def successors_iter(self,n):
"""Return an iterator over successor nodes of n.
neighbors_iter() and successors_iter() are the same.
"""
try:
return iter(self.succ[n])
except KeyError:
raise NetworkXError("The node %s is not in the digraph."%(n,))
示例14: predecessors_iter
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def predecessors_iter(self,n):
"""Return an iterator over predecessor nodes of n."""
try:
return iter(self.pred[n])
except KeyError:
raise NetworkXError("The node %s is not in the digraph."%(n,))
示例15: remove_edge
# 需要導入模塊: from networkx import exception [as 別名]
# 或者: from networkx.exception import NetworkXError [as 別名]
def remove_edge(self, u, v):
"""Remove the edge between u and v.
Parameters
----------
u, v : nodes
Remove the edge between nodes u and v.
Raises
------
NetworkXError
If there is not an edge between u and v.
See Also
--------
remove_edges_from : remove a collection of edges
Examples
--------
>>> G = nx.Graph() # or DiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.remove_edge(0,1)
>>> e = (1,2)
>>> G.remove_edge(*e) # unpacks e from an edge tuple
>>> e = (2,3,{'weight':7}) # an edge with attribute data
>>> G.remove_edge(*e[:2]) # select first part of edge tuple
"""
try:
del self.adj[u][v]
if u != v: # self-loop needs only one entry removed
del self.adj[v][u]
except KeyError:
raise NetworkXError("The edge %s-%s is not in the graph" % (u, v))