示例#1
0
def test_from_matrix():
    A = sp.csr_matrix(
        np.array([[0, 2, 0, 3], [2, 0, 1, 1], [0, 1, 0, 0], [3, 1, 0, 0]]))
    graph = preprocess.graph.Graph(A)

    expected_edges = torch.tensor([(0, 1), (0, 3), (1, 2), (1, 3)])
    assert graph.n_items == 4
    assert graph.n_edges == 4

    testing.assert_all_equal(expected_edges, graph.edges)
    testing.assert_all_equal(np.array([2.0, 3.0, 1.0, 1.0]), graph.distances)
示例#2
0
def test_graph_from_torch():
    X = torch.ones(16).reshape(4, 4)
    X.fill_diagonal_(0.0)
    graph = preprocess.graph.Graph(X)
    assert graph.n_items == 4
    assert graph.n_edges == 6
    testing.assert_all_equal(torch.ones(graph.n_edges), graph.distances)

    expected_edges = torch.tensor([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3],
                                   [2, 3]])
    testing.assert_all_equal(expected_edges, graph.edges)
示例#3
0
def test_from_edges(device):
    edges = torch.tensor([[0, 1], [1, 2], [2, 3], [3, 0]], device=device)
    graph = preprocess.Graph.from_edges(edges)
    assert graph.n_items == 4
    assert graph.n_edges == edges.shape[0]

    expected_edges = torch.tensor([[0, 1], [0, 3], [1, 2], [2, 3]])
    testing.assert_all_equal(expected_edges, graph.edges)

    expected_distances = torch.ones(graph.n_edges)
    testing.assert_all_equal(expected_distances, graph.distances)
示例#4
0
def test_graph_from_csc_matrix():
    X = np.ones(16).reshape(4, 4)
    np.fill_diagonal(X, 0.0)
    X = sp.csc_matrix(X)
    with testing.disable_logging():
        # suppress a warning re: non-CSR matrix.
        graph = preprocess.graph.Graph(X)
    assert graph.n_items == 4
    assert graph.n_edges == 6
    testing.assert_all_equal(torch.ones(graph.n_edges), graph.distances)

    expected_edges = torch.tensor([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3],
                                   [2, 3]])
    testing.assert_all_equal(expected_edges, graph.edges)
示例#5
0
def test_all_distances_numpy(device):
    del device
    np.random.seed(0)
    data_matrix = np.random.randn(4, 2)
    graph = preprocess.data_matrix.distances(data_matrix)

    assert graph.n_items == data_matrix.shape[0]
    assert graph.n_edges == 6
    testing.assert_all_equal(
        graph.edges,
        torch.tensor([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]),
    )

    for e, d in zip(graph.edges, graph.distances):
        e = e.cpu().numpy()
        d = d.item()
        true_distance = np.linalg.norm(data_matrix[e[0]] - data_matrix[e[1]])
        testing.assert_allclose(true_distance, d)
示例#6
0
def test_all_distances_torch(device):
    np.random.seed(0)
    data_matrix = torch.tensor(np.random.randn(4, 2),
                               dtype=torch.float,
                               device=device)
    graph = preprocess.data_matrix.distances(data_matrix)

    assert graph.n_items == data_matrix.shape[0]
    assert graph.n_edges == 6
    testing.assert_all_equal(
        graph.edges,
        torch.tensor([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]),
    )

    for e, d in zip(graph.edges, graph.distances):
        e = e
        d = d
        true_distance = (data_matrix[e[0]] - data_matrix[e[1]]).norm()
        testing.assert_allclose(true_distance, d)
示例#7
0
def test_neighbors(device):
    edges = torch.tensor([[0, 1], [1, 2], [2, 3], [3, 0]], device=device)
    graph = preprocess.Graph.from_edges(edges)

    neighbors = graph.neighbors(0)
    testing.assert_all_equal(np.array([1, 3]), neighbors)

    neighbors = graph.neighbors(1)
    testing.assert_all_equal(np.array([0, 2]), neighbors)

    neighbors = graph.neighbors(2)
    testing.assert_all_equal(np.array([1, 3]), neighbors)

    neighbors = graph.neighbors(3)
    testing.assert_all_equal(np.array([0, 2]), neighbors)
示例#8
0
def test_distances(device):
    edges = torch.tensor([[0, 1], [1, 2], [2, 3], [3, 0]], device=device)
    graph = preprocess.Graph.from_edges(edges,
                                        torch.arange(edges.shape[0]) + 1)

    distances = graph.neighbor_distances(0)
    testing.assert_all_equal(np.array([1.0, 4.0]), distances)

    distances = graph.neighbor_distances(1)
    testing.assert_all_equal(np.array([1.0, 2.0]), distances)

    distances = graph.neighbor_distances(2)
    testing.assert_all_equal(np.array([2.0, 3.0]), distances)

    distances = graph.neighbor_distances(3)
    testing.assert_all_equal(np.array([4.0, 3.0]), distances)
示例#9
0
def test_distances_repeated_edges_summed(device):
    edges = torch.tensor([[0, 1], [1, 2], [2, 3], [3, 0], [0, 3]],
                         device=device)
    graph = preprocess.Graph.from_edges(edges,
                                        torch.arange(edges.shape[0]) + 1)

    # edge (0, 3) repeated, so distance is 4 + 5 = 9
    distances = graph.neighbor_distances(0)
    testing.assert_all_equal(np.array([1.0, 9.0]), distances)

    distances = graph.neighbor_distances(1)
    testing.assert_all_equal(np.array([1.0, 2.0]), distances)

    distances = graph.neighbor_distances(2)
    testing.assert_all_equal(np.array([2.0, 3.0]), distances)

    distances = graph.neighbor_distances(3)
    testing.assert_all_equal(np.array([9.0, 3.0]), distances)
示例#10
0
def test_adjacency_matrix(device):
    edges = torch.tensor([[0, 1], [1, 2], [2, 3], [3, 0]], device=device)
    graph = preprocess.Graph.from_edges(edges)
    expected = np.array([[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1],
                         [1, 0, 1, 0]])
    testing.assert_all_equal(expected, graph.A.todense())