本文整理汇总了Python中igraph.Graph.clusters方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.clusters方法的具体用法?Python Graph.clusters怎么用?Python Graph.clusters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.clusters方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_igraph_graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import clusters [as 别名]
def get_igraph_graph(network):
print 'load %s users into igraph' % len(network)
g = Graph(directed=True)
keys_set = set(network.keys())
g.add_vertices(network.keys())
print 'iterative load into igraph'
edges = []
for source in network:
for target in network[source].intersection(keys_set):
edges.append((source, target))
g.add_edges(edges)
g = g.simplify()
print 'make sure graph is connected'
connected_clusters = g.clusters()
connected_cluster_lengths = [len(x) for x in connected_clusters]
connected_cluster_max_idx = connected_cluster_lengths.index(max(connected_cluster_lengths))
g = connected_clusters.subgraph(connected_cluster_max_idx)
if g.is_connected():
print 'graph is connected'
else:
print 'graph is not connected'
return g
示例2: reduce_and_save_communities
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import clusters [as 别名]
def reduce_and_save_communities(root_user, distance=10, return_graph_for_inspection=False):
print 'starting reduce_and_save_communities'
print 'root_user: %s, following_in_our_db: %s, distance: %s' % (
root_user.screen_name, len(root_user.following), distance)
network = TwitterUser.get_rooted_network(root_user, postgres_handle, distance=distance)
print 'load %s users into igraph' % len(network)
g = Graph(directed=True)
keys_set = set(network.keys())
g.add_vertices(network.keys())
g.vs["id"] = network.keys() #need this for pajek format
print 'iterative load into igraph'
edges = []
for source in network:
for target in network[source].intersection(keys_set):
edges.append((source, target))
g.add_edges(edges)
g = g.simplify()
print 'make sure graph is connected'
connected_clusters = g.clusters()
connected_cluster_lengths = [len(x) for x in connected_clusters]
connected_cluster_max_idx = connected_cluster_lengths.index(max(connected_cluster_lengths))
g = connected_clusters.subgraph(connected_cluster_max_idx)
if g.is_connected():
print 'graph is connected'
else:
print 'graph is not connected'
if return_graph_for_inspection:
return g
print 'write to pajek format'
root_file_name = root_user.screen_name
f = open('io/%s.net' % root_file_name, 'w')
g.write(f, format='pajek')
print 'run infomap'
#infomap_command = 'infomap_dir/infomap 345234 io/%s.net 10'
#infomap_command = 'conf-infomap_dir/conf-infomap 344 io/%s.net 10 10 0.50'
infomap_command = 'infohiermap_dir/infohiermap 345234 io/%s.net 30'
os.system(infomap_command % root_file_name)
print 'read into memory'
f = open('io/%s.smap' % root_file_name)
section_header = ''
communities = defaultdict(lambda: ([], [], []))
for line in f:
if line.startswith('*Modules'):
section_header = 'Modules'
continue
if line.startswith('*Insignificants'):
section_header = 'Insignificants'
continue
if line.startswith('*Nodes'):
section_header = 'Nodes'
continue
if line.startswith('*Links'):
section_header = 'Links'
continue
if section_header == 'Modules':
#looks like this:
#1 "26000689,..." 0.130147 0.0308866
#The names under *Modules are derived from the node with the highest
#flow volume within the module, and 0.25 0.0395432 represent, respectively,
#the aggregated flow volume of all nodes within the module and the per
#step exit flow from the module.
continue
if section_header == 'Nodes':
#looks like this:
#1:10 "2335431" 0.00365772
#or w/ a semicolon instead, semicolon means not significant
#see http://www.tp.umu.se/~rosvall/code.html
if ';' in line:
continue
community_idx = line.split(':')[0]
node_id = line.split('"')[1]
final_volume = float(line.split(' ')[2])
communities[community_idx][1].append(node_id)
communities[community_idx][2].append(final_volume)
if section_header == 'Links':
#community_edges
#looks like this:
#1 4 0.0395432
community_idx = line.split(' ')[0]
target_community_idx = line.split(' ')[1]
edge_weight = line.split(' ')[2]
communities[community_idx][0].append('%s:%s' % (target_community_idx, edge_weight))