本文整理汇总了Python中graph_tool.Graph.set_vertex_filter方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.set_vertex_filter方法的具体用法?Python Graph.set_vertex_filter怎么用?Python Graph.set_vertex_filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph_tool.Graph
的用法示例。
在下文中一共展示了Graph.set_vertex_filter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compose_graph
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import set_vertex_filter [as 别名]
def compose_graph(uid_pid_pairs):
# set up graph
g = Graph()
g.vp['pid'] = v_pid_p = g.new_vertex_property('string')
g.vp['count'] = v_count_p = g.new_vertex_property('int')
g.ep['count'] = e_count_p = g.new_edge_property('int')
pid_v_map = {}
uid_last_v_map = {}
vv_e_map = {}
for uid, pid in uid_pid_pairs:
# vertex
v = pid_v_map.get(pid)
if v is None:
v = g.add_vertex()
v_pid_p[v] = pid
v_count_p[v] = 0
pid_v_map[pid] = v
v_count_p[v] += 1
# edge
last_v = uid_last_v_map.get(uid)
uid_last_v_map[uid] = v
if last_v is None:
continue
vv = (last_v, v)
e = vv_e_map.get(vv)
if e is None:
e = g.add_edge(*vv)
e_count_p[e] = 0
vv_e_map[vv] = e
e_count_p[e] += 1
# calculate closeness
g.vp['closeness'] = v_closeness_p = g.new_vertex_property('float')
e_inverse_count_p = g.new_edge_property('int')
e_inverse_count_p.a = e_count_p.a.max()-e_count_p.a
debug('e_inverse_count_p.a: {}', e_inverse_count_p.a)
closeness(g, weight=e_inverse_count_p, vprop=v_closeness_p)
debug('v_closeness_p.a : {}', v_closeness_p.a)
v_closeness_p.a = nan_to_num(v_closeness_p.a)
debug('v_closeness_p.a : {}', v_closeness_p.a)
# fillter
g.vp['picked'] = v_picked_p = g.new_vertex_property('bool')
debug('v_count_p.a.mean() : {}', v_count_p.a.mean())
v_picked_p.a = v_count_p.a > v_count_p.a.mean()
debug('v_picked_p.a : {}', v_picked_p.a)
g.set_vertex_filter(v_picked_p)
g.set_vertex_filter(None)
return g
示例2: steiner_tree_greedy
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import set_vertex_filter [as 别名]
def steiner_tree_greedy(
g, root, infection_times, source, obs_nodes,
debug=False,
verbose=True):
# root = min(obs_nodes, key=infection_times.__getitem__)
sorted_obs = list(sorted(obs_nodes, key=infection_times.__getitem__))[1:]
tree_nodes = {root}
tree_edges = set()
for u in sorted_obs:
# connect u to the tree
vis = init_visitor(g, u)
if debug:
print('connect {} to tree'.format(u))
print('nodes connectable: {}'.format(tree_nodes))
forbidden_nodes = list(set(obs_nodes) - tree_nodes)
cpbfs_search(g, u, visitor=vis,
terminals=list(tree_nodes),
forbidden_nodes=forbidden_nodes,
count_threshold=1)
# add edge
reachable_nodes = set(np.nonzero(vis.dist > 0)[0]).intersection(tree_nodes)
if debug:
print('reachable_nodes: {}'.format(reachable_nodes))
assert len(reachable_nodes) > 0
sorted_ancestors = sorted(reachable_nodes, key=vis.dist.__getitem__)
ancestor = sorted_ancestors[0]
if debug:
print('ancestor: {}'.format(ancestor))
print('dist to reachable: {}'.format(vis.dist[sorted_ancestors]))
new_edges = extract_edges_from_pred(g, u, ancestor, vis.pred)
new_edges = {(v, u) for u, v in new_edges} # needs to reverse the order
if debug:
print('new_edges: {}'.format(new_edges))
tree_edges |= set(new_edges)
tree_nodes |= {v for e in new_edges for v in e}
t = Graph(directed=True)
for _ in range(g.num_vertices()):
t.add_vertex()
vfilt = t.new_vertex_property('bool')
vfilt.a = False
for v in tree_nodes:
vfilt[t.vertex(v)] = True
for u, v in tree_edges:
t.add_edge(t.vertex(u), t.vertex(v))
t.set_vertex_filter(vfilt)
return t
示例3: build_closure
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import set_vertex_filter [as 别名]
def build_closure(g, terminals,
debug=False,
verbose=False):
terminals = list(terminals)
# build closure
gc = Graph(directed=False)
for _ in range(g.num_vertices()):
gc.add_vertex()
edges_with_weight = set()
r2pred = {}
for r in terminals:
if debug:
print('root {}'.format(r))
vis = init_visitor(g, r)
pbfs_search(g, source=r, terminals=terminals, visitor=vis)
new_edges = set(get_edges(vis.dist, r, terminals))
if debug:
print('new edges {}'.format(new_edges))
edges_with_weight |= new_edges
r2pred[r] = vis.pred
for u, v, c in edges_with_weight:
gc.add_edge(u, v)
eweight = gc.new_edge_property('int')
weights = np.array([c for _, _, c in edges_with_weight])
eweight.set_2d_array(weights)
vfilt = gc.new_vertex_property('bool')
vfilt.a = False
for v in terminals:
vfilt[v] = True
gc.set_vertex_filter(vfilt)
return gc, eweight, r2pred