Beispiel #1
0
def test_graph1():
    """Test graph."""
    shape = (4, 4, 3)
    adj = np.array([[0, 1, 0], [1, 2, 0], [2, 3, 0], [0, 2, 1], [1, 2, 1],
                    [1, 3, 1], [2, 1, 1], [2, 3, 1], [0, 1, 2], [1, 2, 2],
                    [1, 3, 2]])
    revadj = adj.copy()
    ii = revadj[:, 0].copy()
    revadj[:, 0] = revadj[:, 1]
    revadj[:, 1] = ii

    # UNDIRECTED
    G = make_graph(adj, shape, sym=True, display=False)
    revG = make_graph(revadj, shape, sym=True, display=False)
    expect_cooc_mat_sym = np.array([[10, 13, 10], [13, 18, 12], [10, 12, 12]])
    cooc_mat_sym = compute_cooccurrence(G, revG)
    assert np.array_equal(expect_cooc_mat_sym, cooc_mat_sym)
    print ''

    # DIRECTED
    G = make_graph(adj, shape, sym=False, display=False)
    revG = make_graph(revadj, shape, sym=False, display=False)
    expect_cooc_mat_asym = np.array([[2, 4, 2], [3, 6, 2], [2, 4, 2]])
    cooc_mat_asym = compute_cooccurrence(G, revG)
    assert np.array_equal(expect_cooc_mat_asym, cooc_mat_asym)
def test_graph1_creation():
    shape = np.asarray([4, 4, 2], dtype=np.int32)
    adj = np.asarray([
        [0, 1, 0],
        [0, 2, 1],
        [1, 2, 0],
        [1, 2, 1],
        [1, 3, 1],
        [2, 3, 0],
        [2, 1, 1],
        [2, 3, 1],
    ],
                     dtype=np.int32)
    values = np.arange(adj.shape[0]) + 10.

    # create graph
    expect_G = np.asarray([[0., 1., 0., 0., 0., 0., 1., 0.],
                           [1., 0., 1., 0., 0., 0., 1., 1.],
                           [0., 1., 0., 1., 1., 1., 0., 1.],
                           [0., 0., 1., 0., 0., 1., 1., 0.]])
    G = make_graph(adj, shape, sym=True, save_csc=True)
    assert np.array_equal(G.csr.toarray(), G.csc.toarray())
    dirpath = join(abspath(expanduser(os.curdir)), '_undir')
    if not exists(dirpath):
        os.mkdir(dirpath)
    G.save_graph(dirpath)
    assert np.array_equal(G.indeg_vec, np.asarray([2, 3, 3, 2]))
    assert np.array_equal(expect_G, G.csr.toarray())

    # rebuild graph
    G = Graph.reconstruct(dirpath, shape, sym=True, save_csc=True)
    assert np.array_equal(expect_G, G.csr.toarray())
    if exists(dirpath):
        shutil.rmtree(dirpath)
        print('Removed: %s' % dirpath)
Beispiel #3
0
def test_graph2():
	sym = True
	adj = np.array([
		[0, 1, 0, 16],
		[2, 4, 0, 14],
		[4, 5, 0, 4],
		[0, 2, 1, 13],
		[2, 1, 1, 4],
		[3, 5, 1, 20],
		[1, 3, 2, 12],
		[3, 2, 2, 9],
		[4, 3, 2, 7]
	])
	shape = (6, 6, 3)
	G = make_graph(adj[:,:3], shape, values=adj[:,3], sym=sym, display=False)
	G.sources = np.repeat(np.arange(G.N), np.diff(G.csr.indptr))
	G.targets = G.csr.indices % G.N
	cost_vec = G.indeg_vec
	print "Original graph:\n", G

	# Successive shortest path algorithm
	s, p, o = 0, 2, 5
	expect = 2.88888888889
	mincostflow = succ_shortest_path(G, cost_vec, s, p, o)
	print mincostflow
	assert np.allclose(mincostflow.flow, expect)

	print 'Recovered max-flow edges (i, j, r, flow)..'
	adj = np.zeros((len(mincostflow.edges), 4))
	for i, (k, v) in enumerate(mincostflow.edges.iteritems()):
		adj[i, :] = np.array([k[0], k[1], k[2], v])
	adj = adj[np.lexsort((adj[:,2], adj[:,1], adj[:,0])),:]
	print adj
	print ''
Beispiel #4
0
def test_graph1():
    sym = True
    adj = np.array([[0, 2, 0, 10], [1, 2, 0, 30], [0, 1, 1, 20], [1, 3, 1, 10],
                    [2, 3, 1, 20]])
    shape = (4, 4, 2)
    G = make_graph(adj[:, :3], shape, values=adj[:, 3], sym=sym, display=False)
    G.sources = np.repeat(np.arange(G.N), np.diff(G.csr.indptr))
    G.targets = G.csr.indices % G.N
    cost_vec = G.indeg_vec
    print("Original graph:\n", G)

    # Successive shortest path algorithm
    s, p, o = 0, 1, 3
    expect = 6.42857142857
    mincostflow = succ_shortest_path(G, cost_vec, s, p, o)
    print(mincostflow)
    assert np.allclose(mincostflow.flow, expect)

    print('Recovered max-flow edges (i, j, r, flow)..')
    adj = np.zeros((len(mincostflow.edges), 4))
    for i, (k, v) in enumerate(mincostflow.edges.items()):
        adj[i, :] = np.array([k[0], k[1], k[2], v])
    adj = adj[np.lexsort((adj[:, 2], adj[:, 1], adj[:, 0])), :]
    print(adj)
    print('')
Beispiel #5
0
def test_graph2():
    """
	Co-occurrence count on a small graph, e.g. CNetS/IU graph.
	"""
    shape = (8, 8, 6)
    # s, o, p
    adj = np.array([[3, 5, 0], [4, 5, 0], [5, 3, 0], [5, 4, 0], [6, 7, 0],
                    [7, 6, 0], [3, 0, 1], [3, 1, 1], [3, 2, 1], [4, 0, 1],
                    [4, 1, 1], [5, 0, 1], [5, 1, 1], [5, 2, 1], [6, 0, 1],
                    [7, 0, 1], [3, 2, 2], [3, 6, 3], [3, 7, 3], [4, 6, 3],
                    [4, 7, 3], [1, 0, 4], [2, 0, 4], [0, 3, 5], [0, 4, 5],
                    [0, 5, 5], [0, 6, 5], [0, 7, 5], [1, 3, 5], [1, 4, 5],
                    [1, 5, 5], [2, 3, 5], [2, 4, 5], [2, 5, 5], [2, 6, 5],
                    [2, 7, 5]])
    revadj = adj.copy()
    ii = revadj[:, 0].copy()
    revadj[:, 0] = revadj[:, 1]
    revadj[:, 1] = ii

    # UNDIRECTED cooccurrence matrix
    G = make_graph(adj, shape, sym=True, display=False)
    revG = make_graph(revadj, shape, sym=True, display=False)
    expect_cooc_mat_sym = np.array([[8, 13, 1, 8, 0, 16],
                                    [13, 62, 5, 14, 15, 72],
                                    [1, 5, 2, 2, 1, 8], [8, 14, 2, 16, 0, 20],
                                    [0, 15, 1, 0, 6, 18],
                                    [16, 72, 8, 20, 18, 94]])
    cooc_mat_sym = compute_cooccurrence(G, revG)
    assert np.array_equal(expect_cooc_mat_sym, cooc_mat_sym)
    print '\n', '=' * 25

    # DIRECTED cooccurrence matrix
    expect_cooc_mat_asym = np.array([[8, 13, 1, 4, 0, 0], [0, 0, 0, 0, 5, 44],
                                     [0, 0, 0, 0, 1, 5], [4, 4, 0, 0, 0, 0],
                                     [0, 0, 0, 0, 0, 10],
                                     [16, 28, 3, 12, 0, 0]])
    G = make_graph(adj, shape, sym=False, display=False)
    revG = make_graph(revadj, shape, sym=False, display=False)
    cooc_mat_asym = compute_cooccurrence(G, revG)
    assert np.array_equal(expect_cooc_mat_asym, cooc_mat_asym)
def test_graph2_creation():
    # shape of an example graph.
    shape = (8, 8, 6)

    # adjacency
    adj = np.array([[3, 5, 0], [4, 5, 0], [5, 3, 0], [5, 4, 0], [6, 7, 0],
                    [7, 6, 0], [3, 0, 1], [3, 1, 1], [3, 2, 1], [4, 0, 1],
                    [4, 1, 1], [5, 0, 1], [5, 1, 1], [5, 2, 1], [6, 0, 1],
                    [7, 0, 1], [3, 2, 2], [3, 6, 3], [3, 7, 3], [4, 6, 3],
                    [4, 7, 3], [1, 0, 4], [2, 0, 4], [0, 3, 5], [0, 4, 5],
                    [0, 5, 5], [0, 6, 5], [0, 7, 5], [1, 3, 5], [1, 4, 5],
                    [1, 5, 5], [2, 3, 5], [2, 4, 5], [2, 5, 5], [2, 6, 5],
                    [2, 7, 5]])
    A = np.asarray([[[0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 1., 0., 0.],
                     [0., 0., 0., 0., 0., 1., 0., 0.],
                     [0., 0., 0., 1., 1., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 1.],
                     [0., 0., 0., 0., 0., 0., 1., 0.]],
                    [[0., 0., 0., 1., 1., 1., 1., 1.],
                     [0., 0., 0., 1., 1., 1., 0., 0.],
                     [0., 0., 0., 1., 0., 1., 0., 0.],
                     [1., 1., 1., 0., 0., 0., 0., 0.],
                     [1., 1., 0., 0., 0., 0., 0., 0.],
                     [1., 1., 1., 0., 0., 0., 0., 0.],
                     [1., 0., 0., 0., 0., 0., 0., 0.],
                     [1., 0., 0., 0., 0., 0., 0., 0.]],
                    [[0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 1., 0., 0., 0., 0.],
                     [0., 0., 1., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.]],
                    [[0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 1., 1.],
                     [0., 0., 0., 0., 0., 0., 1., 1.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 1., 1., 0., 0., 0.],
                     [0., 0., 0., 1., 1., 0., 0., 0.]],
                    [[0., 1., 1., 0., 0., 0., 0., 0.],
                     [1., 0., 0., 0., 0., 0., 0., 0.],
                     [1., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0.]],
                    [[0., 0., 0., 1., 1., 1., 1., 1.],
                     [0., 0., 0., 1., 1., 1., 0., 0.],
                     [0., 0., 0., 1., 1., 1., 1., 1.],
                     [1., 1., 1., 0., 0., 0., 0., 0.],
                     [1., 1., 1., 0., 0., 0., 0., 0.],
                     [1., 1., 1., 0., 0., 0., 0., 0.],
                     [1., 0., 1., 0., 0., 0., 0., 0.],
                     [1., 0., 1., 0., 0., 0., 0., 0.]]])
    G = make_graph(adj, shape, sym=True, save_csc=True)
    assert np.array_equal(G.csr.toarray(), G.csc.toarray())
    assert np.array_equal(G.indeg_vec, np.asarray([7, 4, 6, 6, 6, 5, 5, 5]))
    for k in xrange(shape[2]):
        assert np.array_equal(G.getslice(k).toarray(), A[k, :, :])
Beispiel #7
0
def test_graph2():
    sym = True
    adj = np.array([[0, 1, 0, 16], [2, 4, 0, 14], [4, 5, 0, 4], [0, 2, 1, 13],
                    [2, 1, 1, 4], [3, 5, 1, 20], [1, 3, 2, 12], [3, 2, 2, 9],
                    [4, 3, 2, 7]])
    shape = (6, 6, 3)
    G = make_graph(adj[:, :3], shape, values=adj[:, 3], sym=sym, display=False)
    print "Original graph:\n", G

    # set weights
    indegsim = weighted_degree(G.indeg_vec, weight='degree').reshape((1, G.N))
    indegsim = indegsim.ravel()
    targets = G.csr.indices % G.N
    specificity_wt = indegsim[targets]  # specificity
    G.csr.data = specificity_wt.copy()

    # back up
    data = G.csr.data.copy()
    indices = G.csr.indices.copy()
    indptr = G.csr.indptr.copy()

    # Closure
    expect = [[0, 0, 1, 0.20000000000000001, [0, 2, 1], [-1, 1, 1]],
              [0, 0, 2, 1.0, [0, 2], [-1, 1]],
              [0, 0, 3, 0.25, [0, 1, 3], [-1, 0, 2]],
              [0, 0, 4, 0.20000000000000001, [0, 2, 4], [-1, 1, 0]],
              [0, 0, 5, 0.125, [0, 1, 3, 5], [-1, 0, 2, 1]],
              [0, 1, 1, 1.0, [0, 1], [-1, 0]],
              [0, 1, 2, 0.25, [0, 1, 2], [-1, 0, 1]],
              [0, 1, 3, 0.25, [0, 1, 3], [-1, 0, 2]],
              [0, 1, 4, 0.20000000000000001, [0, 2, 4], [-1, 1, 0]],
              [0, 1, 5, 0.125, [0, 1, 3, 5], [-1, 0, 2, 1]],
              [0, 2, 1, 1.0, [0, 1], [-1, 0]], [0, 2, 2, 1.0, [0, 2], [-1, 1]],
              [0, 2, 3, 0.25, [0, 1, 3], [-1, 0, 2]],
              [0, 2, 4, 0.20000000000000001, [0, 2, 4], [-1, 1, 0]],
              [0, 2, 5, 0.125, [0, 1, 3, 5], [-1, 0, 2, 1]],
              [1, 0, 0, 0.20000000000000001, [1, 2, 0], [-1, 1, 1]],
              [1, 0, 2, 1.0, [1, 2], [-1, 1]], [1, 0, 3, 1.0, [1, 3], [-1, 2]],
              [1, 0, 4, 0.20000000000000001, [1, 3, 4], [-1, 2, 2]],
              [1, 0, 5, 0.20000000000000001, [1, 3, 5], [-1, 2, 1]],
              [1, 1, 0, 1.0, [1, 0], [-1, 0]],
              [1, 1, 2, 0.33333333333333331, [1, 0, 2], [-1, 0, 1]],
              [1, 1, 3, 1.0, [1, 3], [-1, 2]],
              [1, 1, 4, 0.20000000000000001, [1, 3, 4], [-1, 2, 2]],
              [1, 1, 5, 0.20000000000000001, [1, 3, 5], [-1, 2, 1]],
              [1, 2, 0, 1.0, [1, 0], [-1, 0]], [1, 2, 2, 1.0, [1, 2], [-1, 1]],
              [1, 2, 3, 0.20000000000000001, [1, 2, 3], [-1, 1, 2]],
              [1, 2, 4, 0.20000000000000001, [1, 3, 4], [-1, 2, 2]],
              [1, 2, 5, 0.20000000000000001, [1, 3, 5], [-1, 2, 1]],
              [2, 0, 0, 1.0, [2, 0], [-1, 1]], [2, 0, 1, 1.0, [2, 1], [-1, 1]],
              [2, 0, 3, 1.0, [2, 3], [-1, 2]],
              [2, 0, 4, 0.20000000000000001, [2, 3, 4], [-1, 2, 2]],
              [2, 0, 5, 0.25, [2, 4, 5], [-1, 0, 0]],
              [2, 1, 0, 0.25, [2, 1, 0], [-1, 1, 0]],
              [2, 1, 1, 0.33333333333333331, [2, 0, 1], [-1, 1, 0]],
              [2, 1, 3, 1.0, [2, 3], [-1, 2]], [2, 1, 4, 1.0, [2, 4], [-1, 0]],
              [2, 1, 5, 0.25, [2, 4, 5], [-1, 0, 0]],
              [2, 2, 0, 1.0, [2, 0], [-1, 1]], [2, 2, 1, 1.0, [2, 1], [-1, 1]],
              [2, 2, 3, 0.25, [2, 1, 3], [-1, 1, 2]],
              [2, 2, 4, 1.0, [2, 4], [-1, 0]],
              [2, 2, 5, 0.25, [2, 4, 5], [-1, 0, 0]],
              [3, 0, 0, 0.25, [3, 1, 0], [-1, 2, 0]],
              [3, 0, 1, 1.0, [3, 1], [-1, 2]], [3, 0, 2, 1.0, [3, 2], [-1, 2]],
              [3, 0, 4, 1.0, [3, 4], [-1, 2]], [3, 0, 5, 1.0, [3, 5], [-1, 1]],
              [3, 1, 0, 0.25, [3, 1, 0], [-1, 2, 0]],
              [3, 1, 1, 1.0, [3, 1], [-1, 2]], [3, 1, 2, 1.0, [3, 2], [-1, 2]],
              [3, 1, 4, 1.0, [3, 4], [-1, 2]],
              [3, 1, 5, 0.25, [3, 4, 5], [-1, 2, 0]],
              [3, 2, 0, 0.25, [3, 1, 0], [-1, 2, 0]],
              [3, 2, 1, 0.20000000000000001, [3, 2, 1], [-1, 2, 1]],
              [3, 2, 2, 0.25, [3, 4, 2], [-1, 2, 0]],
              [3, 2, 4, 0.33333333333333331, [3, 5, 4], [-1, 1, 0]],
              [3, 2, 5, 1.0, [3, 5], [-1, 1]],
              [4, 0, 0, 0.20000000000000001, [4, 2, 0], [-1, 0, 1]],
              [4, 0, 1, 0.20000000000000001, [4, 3, 1], [-1, 2, 2]],
              [4, 0, 2, 0.20000000000000001, [4, 3, 2], [-1, 2, 2]],
              [4, 0, 3, 1.0, [4, 3], [-1, 2]],
              [4, 0, 5, 0.20000000000000001, [4, 3, 5], [-1, 2, 1]],
              [4, 1, 0, 0.20000000000000001, [4, 2, 0], [-1, 0, 1]],
              [4, 1, 1, 0.20000000000000001, [4, 3, 1], [-1, 2, 2]],
              [4, 1, 2, 1.0, [4, 2], [-1, 0]], [4, 1, 3, 1.0, [4, 3], [-1, 2]],
              [4, 1, 5, 1.0, [4, 5], [-1, 0]],
              [4, 2, 0, 0.20000000000000001, [4, 2, 0], [-1, 0, 1]],
              [4, 2, 1, 0.20000000000000001, [4, 3, 1], [-1, 2, 2]],
              [4, 2, 2, 1.0, [4, 2], [-1, 0]],
              [4, 2, 3, 0.33333333333333331, [4, 5, 3], [-1, 0, 1]],
              [4, 2, 5, 1.0, [4, 5], [-1, 0]],
              [5, 0, 0, 0.125, [5, 4, 2, 0], [-1, 0, 0, 1]],
              [5, 0, 1, 0.20000000000000001, [5, 3, 1], [-1, 1, 2]],
              [5, 0, 2, 0.25, [5, 4, 2], [-1, 0, 0]],
              [5, 0, 3, 1.0, [5, 3], [-1, 1]],
              [5, 0, 4, 0.20000000000000001, [5, 3, 4], [-1, 1, 2]],
              [5, 1, 0, 0.125, [5, 4, 2, 0], [-1, 0, 0, 1]],
              [5, 1, 1, 0.20000000000000001, [5, 3, 1], [-1, 1, 2]],
              [5, 1, 2, 0.25, [5, 4, 2], [-1, 0, 0]],
              [5, 1, 3, 0.25, [5, 4, 3], [-1, 0, 2]],
              [5, 1, 4, 1.0, [5, 4], [-1, 0]],
              [5, 2, 0, 0.125, [5, 4, 2, 0], [-1, 0, 0, 1]],
              [5, 2, 1, 0.20000000000000001, [5, 3, 1], [-1, 1, 2]],
              [5, 2, 2, 0.25, [5, 4, 2], [-1, 0, 0]],
              [5, 2, 3, 1.0, [5, 3], [-1, 1]], [5, 2, 4, 1.0, [5, 4], [-1, 0]]]
    results = []
    itr = 0
    for s in xrange(G.N):
        for p in xrange(G.R):
            for o in xrange(G.N):
                if s == o:
                    continue
                G.csr.data[targets == o] = 1
                rp = relclosure(G, s, p, o, kind='metric', linkpred=True)
                tmp = [
                    rp.source, rp.relation, rp.target, rp.score, rp.path,
                    rp.relational_path
                ]
                results.append(tmp)
                assert allclose(expect[itr], tmp)
                itr += 1
                G.csr.data = data.copy()
                G.csr.indices = indices.copy()
                G.csr.indptr = indptr.copy()