hcNetworks.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:synchrony 作者: cknd 项目源码 文件源码
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")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号