Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / community / tests / test_quality.py @ 5cef0f13

History | View | Annotate | Download (2.55 KB)

1
# test_quality.py - unit tests for the quality module
2
#
3
# Copyright 2015 NetworkX developers.
4
#
5
# This file is part of NetworkX.
6
#
7
# NetworkX is distributed under a BSD license; see LICENSE.txt for more
8
# information.
9
"""Unit tests for the :mod:`networkx.algorithms.community.quality`
10
module.
11

12
"""
13
from __future__ import division
14

    
15
from nose.tools import assert_equal
16
from nose.tools import assert_almost_equal
17

    
18
import networkx as nx
19
from networkx import barbell_graph
20
from networkx.algorithms.community import coverage
21
from networkx.algorithms.community import modularity
22
from networkx.algorithms.community import performance
23
from networkx.algorithms.community.quality import inter_community_edges
24

    
25

    
26
class TestPerformance(object):
27
    """Unit tests for the :func:`performance` function."""
28

    
29
    def test_bad_partition(self):
30
        """Tests that a poor partition has a low performance measure."""
31
        G = barbell_graph(3, 0)
32
        partition = [{0, 1, 4}, {2, 3, 5}]
33
        assert_almost_equal(8 / 15, performance(G, partition))
34

    
35
    def test_good_partition(self):
36
        """Tests that a good partition has a high performance measure.
37

38
        """
39
        G = barbell_graph(3, 0)
40
        partition = [{0, 1, 2}, {3, 4, 5}]
41
        assert_almost_equal(14 / 15, performance(G, partition))
42

    
43

    
44
class TestCoverage(object):
45
    """Unit tests for the :func:`coverage` function."""
46

    
47
    def test_bad_partition(self):
48
        """Tests that a poor partition has a low coverage measure."""
49
        G = barbell_graph(3, 0)
50
        partition = [{0, 1, 4}, {2, 3, 5}]
51
        assert_almost_equal(3 / 7, coverage(G, partition))
52

    
53
    def test_good_partition(self):
54
        """Tests that a good partition has a high coverage measure."""
55
        G = barbell_graph(3, 0)
56
        partition = [{0, 1, 2}, {3, 4, 5}]
57
        assert_almost_equal(6 / 7, coverage(G, partition))
58

    
59

    
60
def test_modularity():
61
    G = nx.barbell_graph(3, 0)
62
    C = [{0, 1, 4}, {2, 3, 5}]
63
    assert_almost_equal(-16 / (14 ** 2), modularity(G, C))
64
    C = [{0, 1, 2}, {3, 4, 5}]
65
    assert_almost_equal((35 * 2) / (14 ** 2), modularity(G, C))
66

    
67

    
68
def test_inter_community_edges_with_digraphs():
69
    G = nx.complete_graph(2, create_using=nx.DiGraph())
70
    partition = [{0}, {1}]
71
    assert_equal(inter_community_edges(G, partition), 2)
72

    
73
    G = nx.complete_graph(10, create_using=nx.DiGraph())
74
    partition = [{0}, {1, 2}, {3, 4, 5}, {6, 7, 8, 9}]
75
    assert_equal(inter_community_edges(G, partition), 70)
76

    
77
    G = nx.cycle_graph(4, create_using=nx.DiGraph())
78
    partition = [{0, 1}, {2, 3}]
79
    assert_equal(inter_community_edges(G, partition), 2)