|
5 | 5 |
|
6 | 6 | pytest.importorskip("tensorflow") |
7 | 7 | from ge import LINE |
| 8 | +from ge.models import line as line_module |
| 9 | +from ge.utils import preprocess_nxgraph |
8 | 10 |
|
9 | 11 | TEST_GRAPH_PATH = Path(__file__).resolve().parent / "Wiki_edgelist.txt" |
10 | 12 |
|
11 | 13 |
|
| 14 | +def test_LINE_sampling_tables_use_normalized_probabilities(monkeypatch): |
| 15 | + graph = nx.DiGraph() |
| 16 | + graph.add_edge("a", "b", weight=2) |
| 17 | + graph.add_edge("a", "c", weight=6) |
| 18 | + |
| 19 | + model = LINE.__new__(LINE) |
| 20 | + model.graph = graph |
| 21 | + model.idx2node, model.node2idx = preprocess_nxgraph(graph) |
| 22 | + model.node_size = graph.number_of_nodes() |
| 23 | + |
| 24 | + sampling_tables = [] |
| 25 | + |
| 26 | + def record_sampling_table(area_ratio): |
| 27 | + sampling_tables.append(list(area_ratio)) |
| 28 | + return [1] * len(area_ratio), [0] * len(area_ratio) |
| 29 | + |
| 30 | + monkeypatch.setattr(line_module, "create_alias_table", record_sampling_table) |
| 31 | + |
| 32 | + LINE._gen_sampling_table(model) |
| 33 | + |
| 34 | + node_probs, edge_probs = sampling_tables |
| 35 | + expected_edge_probs = [ |
| 36 | + graph[edge[0]][edge[1]]["weight"] / 8 for edge in graph.edges() |
| 37 | + ] |
| 38 | + |
| 39 | + assert sum(node_probs) == pytest.approx(1) |
| 40 | + assert sum(edge_probs) == pytest.approx(1) |
| 41 | + assert edge_probs == pytest.approx(expected_edge_probs) |
| 42 | + |
| 43 | + |
12 | 44 | def test_LINE(): |
13 | 45 | graph = nx.read_edgelist( |
14 | 46 | str(TEST_GRAPH_PATH), |
|
0 commit comments