Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / node_classification / tests / test_local_and_global_consistency.py @ 5cef0f13

History | View | Annotate | Download (2.39 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
from nose import SkipTest
4
import networkx as nx
5
from networkx.algorithms import node_classification
6

    
7

    
8
class TestLocalAndGlobalConsistency:
9

    
10
    @classmethod
11
    def setupClass(cls):
12
        global numpy
13
        global scipy
14
        try:
15
            import numpy
16
        except ImportError:
17
            raise SkipTest('NumPy not available.')
18
        try:
19
            import scipy
20
        except ImportError:
21
            raise SkipTest('SciPy not available.')
22

    
23
    def test_path_graph(self):
24
        G = nx.path_graph(4)
25
        label_name = 'label'
26
        G.node[0][label_name] = 'A'
27
        G.node[3][label_name] = 'B'
28
        predicted = node_classification.local_and_global_consistency(
29
            G, label_name=label_name)
30
        assert_equal(predicted[0], 'A')
31
        assert_equal(predicted[1], 'A')
32
        assert_equal(predicted[2], 'B')
33
        assert_equal(predicted[3], 'B')
34

    
35
    @raises(nx.NetworkXError)
36
    def test_no_labels(self):
37
        G = nx.path_graph(4)
38
        node_classification.local_and_global_consistency(G)
39

    
40
    @raises(nx.NetworkXError)
41
    def test_no_nodes(self):
42
        G = nx.Graph()
43
        node_classification.local_and_global_consistency(G)
44

    
45
    @raises(nx.NetworkXError)
46
    def test_no_edges(self):
47
        G = nx.Graph()
48
        G.add_node(1)
49
        G.add_node(2)
50
        node_classification.local_and_global_consistency(G)
51

    
52
    @raises(nx.NetworkXNotImplemented)
53
    def test_digraph(self):
54
        G = nx.DiGraph()
55
        G.add_edge(0, 1)
56
        G.add_edge(1, 2)
57
        G.add_edge(2, 3)
58
        label_name = 'label'
59
        G.node[0][label_name] = 'A'
60
        G.node[3][label_name] = 'B'
61
        node_classification.harmonic_function(G)
62

    
63
    def test_one_labeled_node(self):
64
        G = nx.path_graph(4)
65
        label_name = 'label'
66
        G.node[0][label_name] = 'A'
67
        predicted = node_classification.local_and_global_consistency(
68
            G, label_name=label_name)
69
        assert_equal(predicted[0], 'A')
70
        assert_equal(predicted[1], 'A')
71
        assert_equal(predicted[2], 'A')
72
        assert_equal(predicted[3], 'A')
73

    
74
    def test_nodes_all_labeled(self):
75
        G = nx.karate_club_graph()
76
        label_name = 'club'
77
        predicted = node_classification.local_and_global_consistency(
78
            G, alpha=0, label_name=label_name)
79
        for i in range(len(G)):
80
            assert_equal(predicted[i], G.node[i][label_name])