本文整理汇总了Python中igraph.Graph.degree方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.degree方法的具体用法?Python Graph.degree怎么用?Python Graph.degree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.degree方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ReactDic
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import degree [as 别名]
#.........这里部分代码省略.........
for p in self.plst:
p.srlst = list(set(p.rlst))
for r in p.srlst:
#print r
#print r.__dict__
r.plst.append(p)
for prim in p.primlst:
for i in range(2):
for c in prim[i]:
if c not in p.sclst:
p.sclst.append(c)
c.cplst.append(p)
def create_dgraph(self):
""" initializes self.graph: the total directed graph of reactions. It ereases any preceding graph. """
self.dgraph = Graph(0,directed = True)
cnlst = []
rnlst = []
rlinks = []
for rct in self.rlst:
rnlst.append(rct.name)
for i in rct.innames:
cnlst.append(i)
rlinks.append((i,rct.name))
for o in rct.outnames:
cnlst.append(o)
rlinks.append((rct.name,o))
if rct.reversible == True:
rnlst.append(rct.name+'_r')
for o in rct.innames:
rlinks.append((rct.name+'_r',o))
for i in rct.outnames:
rlinks.append((i,rct.name+'_r'))
cnlst = list(set(cnlst))
cinpathlst = [self.gsearch(cn).inpath if self.gsearch(cn) != None else [] for cn in cnlst]
rnlst = list(set(rnlst))
rinpathlst = [self.rsearch(rn).inpath if '_r' not in rn else self.rsearch(rn[0:len(rn)-2]) for rn in rnlst]
cnlst.extend(rnlst)
cinpathlst.extend(rinpathlst)
self.dgraph.add_vertices(cnlst)
self.dgraph.add_edges(rlinks)
self.dgraph.vs['inpath'] = cinpathlst
#print rlinks[len(rlinks)-3000:len(rlinks)-1]
print self.dgraph.summary()
def create_pgraphs(self,filterlst):
""" create the graphs of all the pathways and initialize the corresponding variable """
for p in self.plst:
p.dependence(self)
print '\nprimgraph'
for p in self.plst:
p.create_primgraph(filterlst)
print '\ntotgraph\n'
for p in self.plst:
p.create_totgraph(filterlst)
def create_filterlst(self,threshold):
""" create a filtering list nlst containing all the vertex in dgraph that have a degree higher than threshold """
lst = []
nlst = []
for v in range(self.dgraph.vcount()):
if self.dgraph.degree(v) > threshold:
lst.append(v)
nlst.append(self.dgraph.vs['name'][v])
print 'NUMBER OF ELEMENTS TO FILTER: '+str(len(lst))
print nlst
return nlst
def stoichmat(self):
""" outputs the full stoichiometric matrix of the system """
smat = []
clst = []
rlst = []
rev = []
for r in self.rlst:
if r.nobalance == False:
if r.name not in rlst:
rlst.append(r.name)
rev.append(r.reversible)
else:
print 'error in STOICHMAT: reaction\t',r.name,'\talready present!'
for cname in union(r.innames[:],r.outnames[:]):
if cname not in clst:
clst.append(cname)
for r in self.rlst:
if r.nobalance == False:
#create the stoichiometric vector
rclst = r.innames[:]
rclst.extend(r.outnames[:])
sv = [r.slst[[i for i,b in enumerate(rclst) if b == cname][0]] if cname in rclst else 0 for cname in clst]
smat.append(sv)
return [smat,clst,rlst,rev]
示例2: len
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import degree [as 别名]
if frm in g.vs['name'] and to not in g.vs['name']:
g.add_vertex(to,updated_ts=tweet_time)
g.add_edge(frm,to, updated_ts = tweet_time )
g.vs.find(frm)['updated_ts'] = tweet_time
if frm in g.vs['name'] and to in g.vs['name']:
g.vs.find(frm)['updated_ts'] = tweet_time
g.vs.find(to)['updated_ts'] = tweet_time
"""
if two exiting nodes are there, no need to create new edge. Just
update updated_ts to reflect the latest time
"""
if not g.are_connected((g.vs.find(frm)).index,(g.vs.find(to)).index):
g.add_edge(frm,to, updated_ts = tweet_time )
if len(g.degree()) > 1:
average = sum(g.degree())/len(g.degree())
else:
average = 0.00
ft2.write("{0:.2f}".format(average) + '\n')
line_generator.close()
ft2.close()
示例3: cluster_points
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import degree [as 别名]
a = [sum(bi) for bi in b]
G = np.diag(a)
L = G - b
checkmodularity, membership = cluster_points(L)
#----------------------------------------------------------------------------
#Actually, it is from VerticeIDtoNodeID.
OrderIDtoUserID = {}
for line in FromNodeIDtoVerticeID:
OrderIDtoUserID[FromNodeIDtoVerticeID[line]] = line
if checkmodularity > Modularity_Threshold:
#Get our results:
for i in range(0, len(membership)):
if membership[i] == 0:
OneGroup[OrderIDtoUserID[i]] = gr.degree(i)
else:
AnotherGroup[OrderIDtoUserID[i]] = gr.degree(i)
else:
for i in range(0, len(membership)):
OneGroup[OrderIDtoUserID[i]] = gr.degree(i)
#if checkmodularity > Modularity_Threshold:
# print 'modularity value is :' + str(checkmodularity)
# print OneGroup
# print AnotherGroup
#else:
# print 'modularity value is :' + str(checkmodularity)
# print OneGroup
示例4: doClustering
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import degree [as 别名]
#.........这里部分代码省略.........
# print FromNodeIDtoVerticeID
#Now we map long NodeID into VerticeID.
#-------------------------------------
count = 0
for line in FromNodeIDtoVerticeID:
count = count + 1
#print count
#-------------------------------------
for like in likes:
igraphEdgePair = (FromNodeIDtoVerticeID[like[0]],FromNodeIDtoVerticeID[like[1]])
gr.add_edges(igraphEdgePair)
#print summary(gr)
edgelist = gr.get_edgelist()
#Now we finish building edges.
length = len(NodeList)
# print 'nodelist: ' + str(length)
#Here we are dealing with special situations in final results.
if length == 0:
ClusterResultWithUserID = []
ClusterResultWithUserID.append('0')
return ClusterResultWithUserID
#print '-----------------'
#Now we are building adjacent matrix for later clustering.
b = np.arange(0,length*length,1)
for i in range(0,length*length):
b[i] = 0
#print b
b.shape = length,length
for i in range(0,len(edgelist)):
b[edgelist[i][0]][edgelist[i][1]] = b[edgelist[i][0]][edgelist[i][1]] + 1
b[edgelist[i][1]][edgelist[i][0]] = b[edgelist[i][1]][edgelist[i][0]] + 1
#Now we finished building adjacent matrix.
a = [sum(bi) for bi in b]
G = np.diag(a)
L = G - b
#---------------------------------------------------------------------------------------------------------------------------------
# for w in range(0,200):
# evals, Y, idx = cluster_points(L)
# membership = []
# for i in range(0,len(idx)):
# membership.append(str(idx[i]))
# membership[i] = int(membership[i])
#--------------------------------------------
checkmodularity = -100
savemembership = []
for w in range(0,200):
evals, Y, idx = cluster_points(L)
membership = []
for i in range(0,len(idx)):
membership.append(str(idx[i]))
membership[i] = int(membership[i])
if gr.modularity(membership) > checkmodularity:
checkmodularity = gr.modularity(membership)
savemembership = membership
#---------------------------------------------------------------------------------------------------------------------------------
checkmodularity = gr.modularity(membership)
OrderIDtoUserID = {}
#Actually, it is from VerticeIDtoNodeID.
for line in FromNodeIDtoVerticeID:
OrderIDtoUserID[FromNodeIDtoVerticeID[line]] = line
#print OrderIDtoUserID
#Use OrderIDtoUserID = {}
OneGroup = {}
AnotherGroup = {}
for i in range(0,len(membership)):
if membership[i] == 0:
OneGroup[OrderIDtoUserID[i]] = gr.degree(i)
else:
AnotherGroup[OrderIDtoUserID[i]] = gr.degree(i)
ClusterResultWithUserID = [OneGroup, AnotherGroup]
#???Teng also need str(checkmodularity)
return ClusterResultWithUserID
示例5: __init__
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import degree [as 别名]
class hash_tag_graph:
"""
hash tag graph class
"""
def __init__(self,window_duration=60,verbose=True):
"""
Initialize the class
:return: hash_tag_graph object
"""
self.t_window = window_duration
self.latest_time = 0
self.graph = Graph()
self.verbose=verbose
def trim(self):
"""
remove edges outside time window
:return: None
"""
# identify edges outside window
min_time = self.latest_time - self.t_window
edges_to_trim = self.graph.es.select(time_lt=min_time)
if self.verbose: print("Edges to trim: "+str(edges_to_trim))
# remove edges outside of t_window
self.graph.delete_edges(edges_to_trim)
# identify vertices with 0 degree to delete
vertices_to_trim = self.graph.vs.select(_degree=0)
if self.verbose: print("Vertices to trim: "+str(vertices_to_trim._name_index))
self.graph.delete_vertices(vertices_to_trim)
def add_tweet(self,hash_tag_tuple,epoch_time):
"""
Adds tweet to hash tag graph and updates graph such that it only contains tweets
withing window_duration of the latest in time tweet. If tweet is outside of the window_duration
than it is not added to the graph and nothing happens
:return:
"""
# Check if tweet is in order, inside the window duration, or outside
t_diff = self.latest_time - epoch_time > self.t_window
if t_diff <= self.t_window:
self.latest_time = max(epoch_time,self.latest_time)
current_vertices = self.graph.vs._name_index
if self.verbose:
print('Graph name index: '+str(current_vertices))
print('Graph name index type: '+str(type(current_vertices)))
# current vertivces will have none type when it is initilazed empty
if current_vertices is not None:
# Add hashtag to graph as vertex, if its already exists, nothing happens
for hash_tag in hash_tag_tuple:
# only add hashtag if it isn't already in the graph
if hash_tag not in current_vertices:
if self.verbose: print("Adding Vertex: "+str(hash_tag))
self.graph.add_vertex(name=hash_tag)
else:
# Add hashtag to graph as vertex, if its already exists, nothing happens
for hash_tag in hash_tag_tuple:
if self.verbose: print("Adding Vertex: "+str(hash_tag))
self.graph.add_vertex(name=hash_tag)
# Add edges with associated epoch time
for edge in combinations(hash_tag_tuple,r=2):
if self.verbose: print('Adding Edge Pair:'+str(edge)+" Time:"+str(epoch_time))
self.graph.add_edge(source=edge[0],target=edge[1],time=epoch_time)
self.trim()
# if tweet is outside of the time window than toss it
else:
return
return
def get_mean_degree(self):
"""
Compute the average degree
#.........这里部分代码省略.........