コード例 #1
0
def test_similarity():
    rdf_space = Space('tests/example.n3')
    # Overriding _ut
    rdf_space._ut = np.array([[0,1,0,0],[1,0,0,0],[0,1,0,0],[1,1,1,1]], dtype=float).T
    # Overriding uri_index
    rdf_space.uri_index = {'http://0': 0, 'http://1': 1, 'http://2': 2, 'http://3': 3}

    assert_equal(rdf_space.similarity('http://0', 'http://0'), 1.0)
    assert_equal(rdf_space.similarity('http://0', 'http://1'), 0)
    assert_equal(rdf_space.similarity('http://0', 'http://2'), 1.0)
    assert_equal(rdf_space.similarity('http://0', 'http://3'), 0.5)
コード例 #2
0
def test_centrality():
    rdf_space = Space('tests/example.n3')
    # Overriding _ut
    rdf_space._ut = np.array([[-1,1,0,0],[1,0,0,0],[2,1,0,0],[3,1,1,1]], dtype=float).T
    # Overriding uri_index
    rdf_space._uri_index = {'http://0': 0, 'http://1': 1, 'http://2': 2, 'http://3': 3}

    assert_equal(rdf_space.centrality('http://0'), 1)
    assert_equal(rdf_space.centrality('http://1'), 1)
    assert_equal(rdf_space.centrality('http://2'), 2)
    assert_equal(rdf_space.centrality('http://3'), 3)
コード例 #3
0
def test_centroid():
    rdf_space = Space('tests/example.n3')
    # Overriding _ut
    rdf_space._ut = np.array([[0,1,0,0],[1,0,0,0],[0,1,0,0],[1,1,1,1]], dtype=float).T
    # Overriding uri_index
    rdf_space.uri_index = {'http://0': 0, 'http://1': 1, 'http://2': 2, 'http://3': 3}

    centroid = rdf_space.centroid(['http://0', 'http://1', 'http://2', 'http://3'])
    assert_array_equal(centroid, np.array([0.5, 0.75, 0.25, 0.25]))
    centroid = rdf_space.centroid(['http://0', 'http://3'])
    assert_array_equal(centroid, np.array([0.5, 1, 0.5, 0.5]))
    centroid = rdf_space.centroid(['http://0', 'http://1'])
    assert_array_equal(centroid, np.array([0.5, 0.5, 0, 0]))
    centroid = rdf_space.centroid(['http://0', 'http://6'])
    assert_array_equal(centroid, np.array([0, 1, 0, 0]))
    centroid = rdf_space.centroid([])
    assert_array_equal(centroid, None)
コード例 #4
0
def test_save_and_load():
    rdf_space = Space('tests/example.n3')
    rdf_space._ut = np.random.rand(5, 5)
    rdf_space._ut_shape = (5, 5)
    rdf_space._s = np.random.rand(5, 5)
    rdf_space._s_shape = (5, 5)
    rdf_space._vt = np.random.rand(5, 5)
    rdf_space._vt_shape = (5, 5)
    adj = rdf_space._adjacency
    rdf_space.save('tests/example-space')
    assert_array_equal(rdf_space._adjacency, adj)

    space = Space.load('tests/example-space')
    assert_equal(space.uri_index, rdf_space.uri_index)
    assert_equal(space._ut[2,3], rdf_space._ut[2,3])
    assert_equal(space._s[2,2], rdf_space._s[2,2])
    assert_equal(space._vt[2,3], rdf_space._vt[2,3])
    assert_equal(space._adjacency, None)

    try:
        space = Space.load('foo')
        assert_true(false)
    except Exception as e:
        assert_equal(e.args[0], 'No such directory')