Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/node_classification/tests/test_harmonic_function.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 import networkx as nx | |
7 from networkx.algorithms import node_classification | |
8 | |
9 | |
10 class TestHarmonicFunction: | |
11 def test_path_graph(self): | |
12 G = nx.path_graph(4) | |
13 label_name = "label" | |
14 G.nodes[0][label_name] = "A" | |
15 G.nodes[3][label_name] = "B" | |
16 predicted = node_classification.harmonic_function(G, label_name=label_name) | |
17 assert predicted[0] == "A" | |
18 assert predicted[1] == "A" | |
19 assert predicted[2] == "B" | |
20 assert predicted[3] == "B" | |
21 | |
22 def test_no_labels(self): | |
23 with pytest.raises(nx.NetworkXError): | |
24 G = nx.path_graph(4) | |
25 node_classification.harmonic_function(G) | |
26 | |
27 def test_no_nodes(self): | |
28 with pytest.raises(nx.NetworkXError): | |
29 G = nx.Graph() | |
30 node_classification.harmonic_function(G) | |
31 | |
32 def test_no_edges(self): | |
33 with pytest.raises(nx.NetworkXError): | |
34 G = nx.Graph() | |
35 G.add_node(1) | |
36 G.add_node(2) | |
37 node_classification.harmonic_function(G) | |
38 | |
39 def test_digraph(self): | |
40 with pytest.raises(nx.NetworkXNotImplemented): | |
41 G = nx.DiGraph() | |
42 G.add_edge(0, 1) | |
43 G.add_edge(1, 2) | |
44 G.add_edge(2, 3) | |
45 label_name = "label" | |
46 G.nodes[0][label_name] = "A" | |
47 G.nodes[3][label_name] = "B" | |
48 node_classification.harmonic_function(G) | |
49 | |
50 def test_one_labeled_node(self): | |
51 G = nx.path_graph(4) | |
52 label_name = "label" | |
53 G.nodes[0][label_name] = "A" | |
54 predicted = node_classification.harmonic_function(G, label_name=label_name) | |
55 assert predicted[0] == "A" | |
56 assert predicted[1] == "A" | |
57 assert predicted[2] == "A" | |
58 assert predicted[3] == "A" | |
59 | |
60 def test_nodes_all_labeled(self): | |
61 G = nx.karate_club_graph() | |
62 label_name = "club" | |
63 predicted = node_classification.harmonic_function(G, label_name=label_name) | |
64 for i in range(len(G)): | |
65 assert predicted[i] == G.nodes[i][label_name] | |
66 | |
67 def test_labeled_nodes_are_not_changed(self): | |
68 G = nx.karate_club_graph() | |
69 label_name = "club" | |
70 label_removed = {0, 1, 2, 3, 4, 5, 6, 7} | |
71 for i in label_removed: | |
72 del G.nodes[i][label_name] | |
73 predicted = node_classification.harmonic_function(G, label_name=label_name) | |
74 label_not_removed = set(list(range(len(G)))) - label_removed | |
75 for i in label_not_removed: | |
76 assert predicted[i] == G.nodes[i][label_name] |