test_closure.py 文件源码

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

项目:knowledge_linker 作者: glciampaglia 项目源码 文件源码
def test_closure_and_cclosure_against_networkx():
    """ Test 'clusure' and 'cclosure' on 'metric' againt the NetworkX shortest_path (Rion's testing) """

    G = nx.Graph()
    G.add_nodes_from([0,1,2,3,4])
    G.add_edges_from([(0,1), (1,2), (2,3), (3,4)], weight=0.1)
    G.add_edges_from([(0,4)], weight=0.8)

    # Extract Adjacency Matrix from G
    A = nx.adjacency_matrix(G)
    # Transform distance into a similarity
    x = np.ravel(A[A > 0])
    A[A > 0] = (1.0 / (x + 1.0))

    for n1, n2 in combinations(G.nodes(),2):

        # Tests all three methods of computing all shortest paths ('closure','cclosure', and 'nx.all_shortest_paths')
        c_dist, c_paths = clo.closure(A, source=n1, target=n2, kind='metric')
        c_paths = [n for n in c_paths] # convers numbers to letters

        cc_dist, cc_paths = clo.cclosure(A, source=n1, target=n2, retpath=1, kind='metric')
        cc_paths = [n for n in cc_paths] if cc_paths is not None else ''

        nx_paths = list(nx.all_shortest_paths(G, source=n1, target=n2, weight='weight'))[0]

        assert nx_paths == c_paths, "NetworkX and Python 'closure' differ"
        assert nx_paths == cc_paths, "NetworkX and Cython 'cclosure' differ"
        assert c_paths == cc_paths, "Python 'closure' and Cython 'cclosure' differ"
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号