def calculate_katz_centrality(graph):
"""
Compute the katz centrality for nodes.
"""
# if not graph.is_directed():
# raise nx.NetworkXError( \
# "katz_centrality() not defined for undirected graphs.")
print "\n\tCalculating Katz Centrality..."
print "\tWarning: This might take a long time larger pedigrees."
g = graph
A = nx.adjacency_matrix(g)
from scipy import linalg as LA
max_eign = float(np.real(max(LA.eigvals(A.todense()))))
print "\t-Max.Eigenvalue(A) ", round(max_eign, 3)
kt = nx.katz_centrality(g, tol=1.0e-4, alpha=1/max_eign-0.01, beta=1.0, max_iter=999999)
nx.set_node_attributes(g, 'katz', kt)
katz_sorted = sorted(kt.items(), key=itemgetter(1), reverse=True)
for key, value in katz_sorted[0:10]:
print "\t > ", key, round(value, 4)
return g, kt
python类set_node_attributes()的实例源码
def find_partition(graph):
# code and lib from http://perso.crans.org/aynaud/communities/
# must be an undirected graph
g = graph
partition = community.best_partition(g)
print "Partitions found: ", len(set(partition.values()))
# to show members of each partition:
for i in set(partition.values()):
members = [nodes for nodes in partition.keys() if partition[nodes] == i]
print i, len(members)
# if i==0:
# # write out the subgraph
# community_graph = graph.subgraph(members)
# #draw_graph(community_graph)
# #nx.write_edgelist(community_graph, "community.edgelist", data=False)
# #for member in members:
# # print member, i
# print "Partition for node johncoogan: ", partition[node_id]
nx.set_node_attributes(g, 'partition', partition)
return g, partition
def nbest_centrality(G, metric, n=10, attr="centrality", **kwargs):
# Compute the centrality scores for each vertex
scores = metric(G, **kwargs)
# Set the score as a property on each node
nx.set_node_attributes(G, attr, scores)
# Filter scores (do not include in book)
ntypes = nx.get_node_attributes(G, 'type')
phrases = [
item for item in scores.items()
if ntypes.get(item[0], None) == "keyphrase"
]
# Find the top n scores and print them along with their index
topn = heapq.nlargest(n, phrases, key=itemgetter(1))
for idx, item in enumerate(topn):
print("{}. {}: {:0.4f}".format(idx+1, *item))
return G
def __configure_response_function(self):
"""
Configure the response function
"""
if self.symbolic_:
return
rf = self.params["response_function"]
F = {}
for rf_case in rf:
for node in rf_case["nodes"]:
F[str(node)] = RF(rf_case)
nx.set_node_attributes(self.G,name="rf", values=F)
return
##########################################################
# HELPER
##########################################################
def search(self, selected_affils, conf_name, year, exclude_papers=[], rtype="affil", force=False):
"""
Checks if the graph model already exists, otherwise creates one and
runs the ranking on the nodes.
"""
graph = build_graph(conf_name,
year,
self.params['H'],
self.params['min_topic_lift'],
self.params['min_ngram_lift'],
exclude_papers, force, load=True, save=self.save)
# Store number of nodes for checking later
self.nnodes = graph.number_of_nodes()
# Rank nodes using subgraph
scores = rank_nodes(graph, return_type=rtype, **self.params)
# Adds the score to the nodes and writes to disk. A stupid cast
# is required because write_gexf can't handle np.float64
scores = {nid: float(score) for nid, score in scores.items()}
nx.set_node_attributes(graph, "score", scores)
# nx.write_gexf(graph, utils.get_graph_file_name(model_folder, query))
# Returns the top values of the type of node of interest
results = get_top_nodes(graph, scores.items(), limit=selected_affils, return_type=rtype)
# Add to class object for future access
self.graph = graph
return results
def search(self, query, exclude=[], limit=20, rtype="paper", force=False):
"""
Checks if the graph model already exists, otherwise creates one and
runs the ranking on the nodes.
"""
graph = build_graph(query,
self.params['K'],
self.params['H'],
self.params['min_topic_lift'],
self.params['min_ngram_lift'],
exclude, force, load=True, save=self.save)
# Store number of nodes for checking later
self.nnodes = graph.number_of_nodes()
# Rank nodes using subgraph
scores = ranker.rank_nodes(graph, limit=limit, return_type=rtype, **self.params)
# Adds the score to the nodes and writes to disk. A stupid cast
# is required because write_gexf can't handle np.float64
scores = {nid: float(score) for nid, score in scores.items()}
nx.set_node_attributes(graph, "score", scores)
# nx.write_gexf(graph, utils.get_graph_file_name(model_folder, query))
# Returns the top values of the type of node of interest
results = get_top_nodes(graph, scores.items(), limit=limit, return_type=rtype)
# Add to class object for future access
self.graph = graph
return [str(pub_id) for _nid, pub_id, _score in results]
def build_function_graph(self, analysis_unit_dict):
''' BUILDS DIRECTED FUNCTION GRAPH
input: a dictionary of functions from this dump file
output: none. Side effect creates a graph linked to this object
'''
if self.debug_verbose:
print inspect.stack()[0][3]
# BUILD CALL GRAPH
self.function_graph = nx.DiGraph()
G = self.function_graph
for k, function_dict in analysis_unit_dict.iteritems():
if function_dict['function']: # MIGHT BE NONE WHEN FUNCTION IS CLASS CONSTRUCTOR (?)
node = function_dict['function'].Id # Id of the Function
#if not G.has_node(node):
G.add_node(node) #, function_dict_key=k}) # FUNCTION CPP OBJECT IS NODE
#else:
all_attr = nx.get_node_attributes(G, 'function_id')
all_attr[node] = k
nx.set_node_attributes(G, 'function_id', all_attr)
#function_dict['is_visited'] = False
self.add_edges_to_function_graph(function_dict, G, node)
self.function_graph = G
def set_node_attributes(self,side,name,values):
'''
Set node of type side attributes from dictionary of nodes and values.
Only sets one attribute at a time.
Parameters
----------
name : string
Attribute name
side : int or str
Tags for each side of the bipartite network.
values: dict
Dictionary of attribute values keyed by node.
Nodes that do not belong to side will not be considered.
'''
self._check_side(side)
ns = set(values.keys()).intersection(set(self.nodes(side)))
vals = {val:values[val] for val in ns}
set_node_attributes(self,name,vals)
def refine_to_chain(g, from_attr, to_attr):
'''can be used to refine basic blocks into blocks - the dual of contract_chains()
assume g.node[n][attr] is a list
returns a graph whose nodes are the refinement of the lists into paths
the elements of the lists are held as to_attr
the nodes become tuples (node_index, list_index)'''
paths = []
for n in g.nodes_iter():
block = g.node[n][from_attr]
size = len(block)
path = nx.path_graph(size, create_using=nx.DiGraph())
nx.relabel_nodes(path, mapping={x:(n, x) for x in path.nodes()}, copy=False)
path.add_edges_from(((n, size - 1), (s, 0)) for s in g.successors_iter(n))
paths.append(path)
values = {(n, x): block
for n in g.nodes_iter()
for x, block in enumerate(g.node[n][from_attr])}
res = nx.compose_all(paths)
nx.set_node_attributes(res, to_attr, values)
return res
def dataflow(g:'graph', start:'node', start_value, Analysis=ConsProp):
import networkx as nx
import graph_utils as gu
gu.node_data_map_inplace(g,
f=lambda n, d: Analysis.single_block_update,
attr='transfer_function')
nx.set_node_attributes(g, 'outb', {v: Analysis.initial() for v in g.nodes()})
nx.set_node_attributes(g, 'inb', {v: Analysis.initial() for v in g.nodes()})
g.node[start]['inb'] = Analysis.initial()
wl = set(g.nodes())
while wl:
u = wl.pop()
udata = g.node[u]
inb = udata['inb']
Analysis.join(inb, [g.node[x]['outb'] for x in g.predecessors(u)])
outb = udata['transfer_function'](udata[BLOCKNAME], inb)
if outb != udata['outb']:
udata['outb'] = outb
wl.update(g.successors(u))
def calculate_degree(graph):
print "\n\tCalculating node degree...\n"
g = graph
deg = nx.degree(g)
nx.set_node_attributes(g, 'degree', deg)
return g, deg
def calculate_odegree(graph):
# will only work on DiGraph (directed graph)
print "\n\tCalculating Outdegree..."
g = graph
odeg = g.out_degree()
nx.set_node_attributes(g, 'odegree', odeg)
outdeg_sorted = sorted(odeg.items(), key=itemgetter(1), reverse=True)
for key, value in outdeg_sorted[0:10]:
print "\t > ", key, value
return g, odeg
def calculate_indegree(graph):
# will only work on DiGraph (directed graph)
print "\tCalculating Indegree..."
g = graph
indeg = g.in_degree()
nx.set_node_attributes(g, 'indegree', indeg)
indeg_sorted = sorted(indeg.items(), key=itemgetter(1), reverse=True)
for key, value in indeg_sorted[0:10]:
print "\t > ", key, value
return g, indeg
def calculate_outdegree(graph):
# will only work on DiGraph (directed graph)
print "\n\tCalculating Outdegree Centrality..."
g = graph
outdeg = out_degree_centrality(g)
nx.set_node_attributes(g, 'outdegree', outdeg)
outdeg_sorted = sorted(outdeg.items(), key=itemgetter(1), reverse=True)
for key, value in outdeg_sorted[0:10]:
print "\t > ", key, round(value, 4)
return g, outdeg
def calculate_closeness(graph):
print "\n\tCalculating Closeness Centrality..."
g = graph
clo = nx.closeness_centrality(g)
nx.set_node_attributes(g, 'closeness', clo)
degclos_sorted = sorted(clo.items(), key=itemgetter(1), reverse=True)
for key, value in degclos_sorted[0:10]:
print "\t > ", key, round(value, 4)
return g, clo
def calculate_eigenvector_centrality(graph):
print "\n\tCalculating Eigenvector Centrality..."
g = graph
ec = nx.eigenvector_centrality_numpy(g)
nx.set_node_attributes(g, 'eigenvector', ec)
degeign_sorted = sorted(ec.items(), key=itemgetter(1), reverse=True)
for key, value in degeign_sorted[0:10]:
print "\t > ", key, round(value, 4)
return g, ec
def calculate_degree_centrality(graph):
print "\nCalculating Degree Centrality..."
g = graph
dc = nx.degree_centrality(g)
nx.set_node_attributes(g, 'degree_cent', dc)
degcent_sorted = sorted(dc.items(), key=itemgetter(1), reverse=True)
for key, value in degcent_sorted[0:10]:
print " > ", key, round(value, 4)
return graph, dc
def voronoi_partition(G, outline):
"""
For 2D-embedded graph `G`, within the boundary given by the shapely polygon
`outline`, returns `G` with the Voronoi cell region as an additional node
attribute.
"""
#following line from vresutils.graph caused a bug
#G = polygon_subgraph(G, outline, copy=False)
points = list(vresutils.graph.get_node_attributes(G, 'pos').values())
regions = vresutils.graph.voronoi_partition_pts(points, outline, no_multipolygons=True)
nx.set_node_attributes(G, 'region', dict(zip(G.nodes(), regions)))
return G
def gdfs_to_graph(gdf_nodes, gdf_edges):
"""
Convert node and edge GeoDataFrames into a graph
Parameters
----------
gdf_nodes : GeoDataFrame
gdf_edges : GeoDataFrame
Returns
-------
networkx multidigraph
"""
G = nx.MultiDiGraph()
G.graph['crs'] = gdf_nodes.crs
G.graph['name'] = gdf_nodes.gdf_name.rstrip('_nodes')
# add the nodes and their attributes to the graph
G.add_nodes_from(gdf_nodes.index)
attributes = gdf_nodes.to_dict()
for attribute_name in gdf_nodes.columns:
# only add this attribute to nodes which have a non-null value for it
attribute_values = {k:v for k, v in attributes[attribute_name].items() if pd.notnull(v)}
nx.set_node_attributes(G, name=attribute_name, values=attribute_values)
# add the edges and attributes that are not u, v, key (as they're added
# separately) or null
for _, row in gdf_edges.iterrows():
attrs = {}
for label, value in row.iteritems():
if (label not in ['u', 'v', 'key']) and (isinstance(value, list) or pd.notnull(value)):
attrs[label] = value
G.add_edge(u=row['u'], v=row['v'], key=row['key'], **attrs)
return G
def _project_AA(self,side):
"""
Builds the projection of the bipartite network on to the chosen side.
The projection is done using the ADAMIC-ADAR index.
Parameters
----------
side : int or str
Tags for each side of the bipartite network.
"""
self._check_side(side)
aside = self.side if side == self.aside else self.aside
net = self.edges(as_df=True)[[side,aside]]
AA = merge(net,net,how='inner',left_on=aside,right_on=aside)
nodes = self.nodes(side,as_df=True)[[side]].reset_index().rename(columns={'index':side+'_index'})
AA = merge(AA,nodes.rename(columns={side:side+'_x',side+'_index':side+'_index_x'}),how='left',right_on=side+'_x',left_on=side+'_x')
AA = merge(AA,nodes.rename(columns={side:side+'_y',side+'_index':side+'_index_y'}),how='left',right_on=side+'_y',left_on=side+'_y')
AA = AA[AA[side+'_index_x']>AA[side+'_index_y']].drop([side+'_index_x',side+'_index_y'],1)
AA = merge(AA,self.degree(aside,as_df=True))
AA['AA'] = 1./log(AA['degree'])
AA = AA[[side+'_x',side+'_y','AA']].groupby([side+'_x',side+'_y']).sum().reset_index()
self.P[side] = gGraph(node_id=side)
self.P[side].add_weighted_edges_from([val[1:] for val in AA.itertuples()])
nodes = merge(self.P[side].nodes(as_df=True),self.nodes(side,as_df=True),how='left')
properties = nodes.columns.values.tolist()
properties.remove(side)
for prop in properties:
values = dict(zip(nodes[side].values,nodes[prop].values))
set_node_attributes(self.P[side],prop,values)
def _project_NK(self,side):
"""
Builds the projection of the bipartite network on to the chosen side.
The projection is done using the NK conditional probability.
Parameters
----------
side : int or str
Tags for each side of the bipartite network.
"""
self._check_side(side)
aside = self.side if side == self.aside else self.aside
E = self.recombination_ease(aside)
net = self.edges(as_df=True)[[side,aside]]
nodes = merge(E,net).groupby(side).sum()[['E']].reset_index().reset_index().rename(columns={'index':side+'_index'})
dis = merge(E,merge(net,net,how='inner',left_on=aside,right_on=aside))
dis = dis.groupby([side+'_x',side+'_y']).sum()[['E']].reset_index().rename(columns={'E':'E_both'})
dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_x').drop(side,1)
dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_y').drop(side,1)
dis = dis[dis[side+'_index_x']>dis[side+'_index_y']].drop([side+'_index_x',side+'_index_y'],1)
dis['p_x'] = dis['E_both']/dis['E_x'].astype(float)
dis['p_y'] = dis['E_both']/dis['E_y'].astype(float)
dis['fi'] = dis[['p_x','p_y']].min(1)
dis = dis[[side+'_x',side+'_y','fi']]
self.P[side] = gGraph(node_id=side)
self.P[side].add_weighted_edges_from([val[1:] for val in dis.itertuples()])
nodes = merge(self.P[side].nodes(as_df=True),self.nodes(side,as_df=True),how='left')
properties = nodes.columns.values.tolist()
properties.remove(side)
for prop in properties:
values = dict(zip(nodes[side].values,nodes[prop].values))
set_node_attributes(self.P[side],prop,values)
def _get_projection_pos(self,side,P,C=None):
'''This function requires graph_tool'''
pos = get_pos(P[[side+'_x',side+'_y']],node_id=side,comms=True,progress=False,C=C)
X = {}
Y = {}
Cc = {}
for i,x,y,c in pos.values:
X[i]=x
Y[i]=y
Cc[i]=c
self.set_node_attributes(side,'x',X)
self.set_node_attributes(side,'y',Y)
self.set_node_attributes(side,'c',Cc)
return pos
def recombination_ease(self,side):
nodes = self.nodes(side,as_df=True)
if 'E' not in nodes.columns:
aside = self.side if side == self.aside else self.aside
net = self.edges(as_df=True)[[side,aside]]
dis = merge(net,net,how='inner',left_on=aside,right_on=aside)[[side+'_x',side+'_y']].drop_duplicates()
dis = dis[dis[side+'_x']!=dis[side+'_y']]
E = merge(dis.groupby(side+'_x').count().reset_index().rename(columns={side+'_y':'n_c'}).rename(columns={side+'_x':side}),net.groupby(side).count().reset_index().rename(columns={aside:'n_p'}),how='outer').fillna(0)
E['E'] = E['n_c']/E['n_p']
E = E[[side,'E']]
self.set_node_attributes(side,'E',dict(E.values))
return E
else:
return nodes[[side,'E']]
def _prepare_overlaps(exons):
"""Compute splicegraph prior the computation of overlaps"""
splice_graph = nx.DiGraph()
exon_df = exons_to_df(exons)
exon2coord = exon_to_coordinates(exons)
splice_graph.add_nodes_from(exon2coord.keys())
nx.set_node_attributes(
G=splice_graph,
name='coordinates',
values = exon2coord
)
transcript2path = transcript_to_path(exon_df)
for path in transcript2path.values():
splice_graph.add_path(path)
return splice_graph
cluster_coefficient.py 文件源码
项目:Python-Data-Analytics-and-Visualization
作者: PacktPublishing
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def calculate_centrality(G):
degc = nx.degree_centrality(G)
nx.set_node_attributes(G,'degree_cent', degc)
degc_sorted = sorted(degc.items(), key=valuegetter(1), reverse=True)
for key, value in degc_sorted[0:10]:
print "Degree Centrailty:", key, value
return G, degc
def add_junction(self, name, base_demand=0.0, demand_pattern=None, elevation=0.0, coordinates=None):
"""
Adds a junction to the water network model.
Parameters
-------------------
name : string
Name of the junction.
base_demand : float
Base demand at the junction.
demand_pattern : string or Pattern
Name of the demand pattern or the actual Pattern object
elevation : float
Elevation of the junction.
coordinates : tuple of floats
X-Y coordinates of the node location.
"""
base_demand = float(base_demand)
elevation = float(elevation)
if not isinstance(demand_pattern, Pattern):
demand_pattern = self.get_pattern(demand_pattern)
junction = Junction(name, base_demand, demand_pattern, elevation)
self._nodes[name] = junction
self._junctions[name] = junction
self._graph.add_node(name)
if coordinates is not None:
self.set_node_coordinates(name, coordinates)
nx.set_node_attributes(self._graph, name='type', values={name:'junction'})
self._num_junctions += 1
def add_reservoir(self, name, base_head=0.0, head_pattern=None, coordinates=None):
"""
Adds a reservoir to the water network model.
Parameters
----------
name : string
Name of the reservoir.
base_head : float, optional
Base head at the reservoir.
head_pattern : string or Pattern
Name of the head pattern or the actual Pattern object
coordinates : tuple of floats, optional
X-Y coordinates of the node location.
"""
base_head = float(base_head)
if head_pattern and isinstance(head_pattern, six.string_types):
head_pattern = self.get_pattern(head_pattern)
reservoir = Reservoir(name, base_head, head_pattern)
self._nodes[name] = reservoir
self._reservoirs[name] = reservoir
self._graph.add_node(name)
if coordinates is not None:
self.set_node_coordinates(name, coordinates)
nx.set_node_attributes(self._graph, name='type', values={name:'reservoir'})
self._num_reservoirs += 1
def set_node_coordinates(self, name, coordinates):
"""
Sets the node coordinates in the networkx graph.
Parameters
----------
name : string
Name of the node.
coordinates : tuple
X-Y coordinates.
"""
nx.set_node_attributes(self._graph, name='pos', values={name: coordinates})
def update_stackdepth(cfg):
'''The stack depth is supposed to be independent of path.
So dijkstra on the undirected graph suffices (and maybe too strong. we don't need minimality)
The `undirected` part is just because we want to work
with unreachable code too.
'''
bidi = gu.copy_to_bidirectional(cfg, weight='stack_effect')
depths = nx.single_source_dijkstra_path_length(bidi, source=0, weight='stack_effect')
nx.set_node_attributes(cfg, 'stack_depth', depths)
return cfg
def make_bcode_block_cfg(instructions):
dbs = {b.offset: b for b in instructions}
cfg = nx.DiGraph([(b.offset, dbs[j].offset, {'stack_effect': stack_effect})
for b in dbs.values()
for (j, stack_effect) in b.next_list() if dbs.get(j)])
nx.set_node_attributes(cfg, name='BCode', values=dbs)
update_stackdepth(cfg)
# each node will hold a block of dictionaries - bcode and stack_depth
basic_block_cfg = gu.contract_chains(cfg, blockname=BLOCKNAME)
return basic_block_cfg