本文整理汇总了Python中igraph.Graph.neighbors方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.neighbors方法的具体用法?Python Graph.neighbors怎么用?Python Graph.neighbors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.neighbors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MozGraph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import neighbors [as 别名]
class MozGraph(object):
def __init__(self, month, session):
self.g = Graph(directed=True)
self.session = session
self.start = month.first
self.stop = month.last
# Mapping ids of debuggers and bugs to their associated vertices
self.dbid_to_vertex = {}
self.bid_to_vertex = {}
self._populate_irc()
self._populate_bugevents()
def __getitem__(self, item):
"""XXX: This isn't really parallel with the implementation of getitem
in MozIRCGraph (which came earlier). Anyways, takes a Bug or Debugger.
"""
if isinstance(item, Debugger):
return self.dbid_to_vertex[item.id]
elif isinstance(item, Bug):
return self.bid_to_vertex[item.bzid]
else:
raise ValueError("Expected a debugger or bug")
@classmethod
def all(cls, session):
"""A convenience method returning an iterator of all MozGraphs, (ranging
over all Months in the given session).
"""
for month in session.query(Month):
yield cls.load(month, session)
@staticmethod
def fname(month):
name = "month" + str(month.id)
return os.path.join(ADJ_DIR, name)
def _populate_irc(self):
for chat in self._relevant_chats():
self.add_chat(chat)
def _relevant_chats(self):
return self.session.query(Chat).filter(self.start <= Chat.date).\
filter(Chat.date <= self.stop)
def add_chat(self, chat):
"""Encode a chat between two debuggers in this graph.
"""
seme = self.add_debugger(chat.db1)
uke = self.add_debugger(chat.db2)
self.add_edge(seme, uke, chat.n)
def add_debugger(self, debugger):
"""Add this debugger to the graph if it's not already present, and return it.
"""
if debugger.id not in self.dbid_to_vertex:
index = len(self.g.vs)
self.g.add_vertex(id=debugger.id)
self.dbid_to_vertex[debugger.id] = self.g.vs[index]
return self.dbid_to_vertex[debugger.id]
def add_edge(self, v1, v2, weight=1):
self.g.add_edge(v1, v2, weight=weight)
def _populate_bugevents(self):
for bugevent in self._relevant_bugevents():
self.add_bugevent(bugevent)
def _relevant_bugevents(self):
return self.session.query(BugEvent).filter(self.start <= BugEvent.date).\
filter(BugEvent.date <= self.stop)
def add_bugevent(self, bugevent):
debugger = self.add_debugger(bugevent.debugger)
bug = self.add_bug(bugevent.bug)
self.add_edge(debugger, bug)
def add_bug(self, bug):
if bug.id not in self.bid_to_vertex:
index = len(self.g.vs)
self.g.add_vertex(id=bug.bzid)
self.bid_to_vertex[bug.bzid] = self.g.vs[index]
return self.bid_to_vertex[bug.bzid]
def effective_size(self, vertex):
"""A variable that igraph doesn't come with, and we have to implement ourselves.
Effective size of a node is the number of alters the node has,
minus the average number of ties that each alter has to other alters:
n - 2t/2,
where n is the number of alters, and t is the number of ties among them.
"""
if not isinstance(vertex, int):
vertex = vertex.index
neighbours = set(self.g.neighbors(vertex))
total_metaneighbours = 0
for neigh in neighbours:
neighbours_squared = set(self.g.neighbors(neigh))
total_metaneighbours += len(set.intersection(neighbours, neighbours_squared))
#.........这里部分代码省略.........
示例2: DAGApp
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import neighbors [as 别名]
#.........这里部分代码省略.........
"""
if mode == 'object':
return [self._kernels[idx_] for idx_ in self._g.vs.indices]
else:
return self._g.vs.indices
def get_kernel(self, kernel_idx):
"""Get the kernel object
Parameters
----------
kernel_idx : int
The kernel index
Returns
-------
:class:`~lumos.model.Kernel`
The kernel object.
"""
return self._kernels[kernel_idx]
def _prep_baseline(self):
self._depth_sorted = [[v_.index
for v_ in self._g.vs.select(depth_eq=d_)]
for d_ in range(1, self._max_depth + 1)]
finish_time = dict.fromkeys(self._g.vs.indices, 0)
for l, node_list in enumerate(self._depth_sorted):
if l == 0:
for node in node_list:
finish_time[node] = self._length[node]
else:
for node in node_list:
start = max([finish_time[n_] for n_ in
self._g.neighbors(node,
mode=GRAPH_IN)])
finish_time[node] = start + self._length[node]
self._baseline_runtime = max(finish_time.values())
def get_kernel_depth(self, kernel_idx):
return self._g.vs[kernel_idx]['depth']
def get_all_kernel_depth(self):
return self._g.vs['depth']
def kernels_topo_sort(self):
"""sort kernels in a topological order.
Returns
-------
list
kernel indexes in a topological sort order
"""
return self._g.topological_sorting()
def get_precedent_kernel(self, kernel_idx):
"""Get the precedent (pre-requisite) kernels.
Returns
-------
list
A list of kernel indexes that precedent the given kernel. None
if no precedent kernels exist, e.g. the starting kernel.
"""
return self._g.neighbors(kernel_idx, mode=GRAPH_IN)
def kernels_depth_sort(self):