Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/node_classification/tests/test_local_and_global_consistency.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import pytest | |
2 | |
3 numpy = pytest.importorskip("numpy") | |
4 scipy = pytest.importorskip("scipy") | |
5 | |
6 | |
7 import networkx as nx | |
8 from networkx.algorithms import node_classification | |
9 | |
10 | |
11 class TestLocalAndGlobalConsistency: | |
12 def test_path_graph(self): | |
13 G = nx.path_graph(4) | |
14 label_name = "label" | |
15 G.nodes[0][label_name] = "A" | |
16 G.nodes[3][label_name] = "B" | |
17 predicted = node_classification.local_and_global_consistency( | |
18 G, label_name=label_name | |
19 ) | |
20 assert predicted[0] == "A" | |
21 assert predicted[1] == "A" | |
22 assert predicted[2] == "B" | |
23 assert predicted[3] == "B" | |
24 | |
25 def test_no_labels(self): | |
26 with pytest.raises(nx.NetworkXError): | |
27 G = nx.path_graph(4) | |
28 node_classification.local_and_global_consistency(G) | |
29 | |
30 def test_no_nodes(self): | |
31 with pytest.raises(nx.NetworkXError): | |
32 G = nx.Graph() | |
33 node_classification.local_and_global_consistency(G) | |
34 | |
35 def test_no_edges(self): | |
36 with pytest.raises(nx.NetworkXError): | |
37 G = nx.Graph() | |
38 G.add_node(1) | |
39 G.add_node(2) | |
40 node_classification.local_and_global_consistency(G) | |
41 | |
42 def test_digraph(self): | |
43 with pytest.raises(nx.NetworkXNotImplemented): | |
44 G = nx.DiGraph() | |
45 G.add_edge(0, 1) | |
46 G.add_edge(1, 2) | |
47 G.add_edge(2, 3) | |
48 label_name = "label" | |
49 G.nodes[0][label_name] = "A" | |
50 G.nodes[3][label_name] = "B" | |
51 node_classification.harmonic_function(G) | |
52 | |
53 def test_one_labeled_node(self): | |
54 G = nx.path_graph(4) | |
55 label_name = "label" | |
56 G.nodes[0][label_name] = "A" | |
57 predicted = node_classification.local_and_global_consistency( | |
58 G, label_name=label_name | |
59 ) | |
60 assert predicted[0] == "A" | |
61 assert predicted[1] == "A" | |
62 assert predicted[2] == "A" | |
63 assert predicted[3] == "A" | |
64 | |
65 def test_nodes_all_labeled(self): | |
66 G = nx.karate_club_graph() | |
67 label_name = "club" | |
68 predicted = node_classification.local_and_global_consistency( | |
69 G, alpha=0, label_name=label_name | |
70 ) | |
71 for i in range(len(G)): | |
72 assert predicted[i] == G.nodes[i][label_name] |