def createroiidxs(network,distance):
"""
Choose two central nodes, some distance apart, and return their (i,j) indices.
Args:
network: networkx graph
distance: how far apart the two nodes should be.
Returns:
A tuple of two (i,j) indices / node labels
"""
nodes,centralities = zip(*nx.closeness_centrality(network).items())
# sort nodes from most central to least central:
centr_arxs = np.argsort(centralities)
nodes_sorted = [n for n in reversed(np.array(nodes)[centr_arxs])]
k = 0
while k<len(nodes_sorted):
# pick some node in the middle of the graph (high centrality)
middlenode = tuple(nodes_sorted[k])
# now pick the most central node that meets the given distance criterion.
# [since we dont want to end up near the boundaries)
for n in nodes_sorted:
if nx.shortest_path_length(network,middlenode,tuple(n)) == distance:
return middlenode,tuple(n)
# if that didnt work, try starting with a different, less central middlenode.
k = k+1
raise Exception("speficied distance to high for this network")
评论列表
文章目录