修訂. | b6ad982fc39c9b798c79d48c23d2e7522780b43c |
---|---|
大小 | 2,014 bytes |
時間 | 2010-04-22 00:57:19 |
作者 | lorenzo |
Log Message | I added a small script capable of reading (and understanding) a network
|
#! /usr/bin/env python
import scipy as s
import numpy as n
import igraph as ig
import string
from igraph import UniqueIdGenerator, Graph
from collections import defaultdict
def array_to_dict_iter(array):
for row in array:
yield dict(source=row[0], target=row[1])
filename="input_graph.dat"
f = open(filename)
raw_data = [map(int, string.split(line)) for line in f.readlines()]
f.close()
raw_data = s.array(raw_data, dtype="int64")
vertex_ids = UniqueIdGenerator()
weights = defaultdict(int)
for row in raw_data:
source, target = row
edge = vertex_ids[source], vertex_ids[target]
if edge[0] > edge[1]:
edge = edge[1], edge[0]
weights[edge] += 1
g2 = Graph(weights.keys())
g2.es["weight"] = weights.values()
g2=g2.simplify()
clustering2=g2.clusters()
print("g2.degree() is, ", g2.degree())
print("clustering2.sizes is, ", clustering2.sizes())
print("clustering2.membership is, ", clustering2.membership)
########################################################################################
########################################################################################
########################################################################################
seed=101843068
g = ig.Graph.DictList(vertices=None, edges=array_to_dict_iter(raw_data))
g=g.simplify()
print("####################################")
print("g.degree is, ", g.degree())
clustering=g.clusters()
print("clustering.sizes is, ", clustering.sizes())
print("clustering.membership is, ", clustering.membership)
c_sizes=s.asarray(clustering.sizes())
print(g.vs["name"])
name_list=g.vs["name"]
name_list=s.asarray(name_list)
sel=s.where(name_list==seed)[0]
print("sel is, ", sel)
#Now the point is to get the size of the CC the seed belongs to
seed_membership=clustering.membership[sel]
print("the CC the seed belongs to is, " , seed_membership)
print("the size of the CC the seed belongs to is, ", c_sizes[seed_membership])
print "So far so good"