Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / generators / tests / test_joint_degree_seq.py @ 5cef0f13

History | View | Annotate | Download (2.43 KB)

1
import time
2
from nose.tools import *
3
from networkx.generators.joint_degree_seq import is_valid_joint_degree, joint_degree_graph
4
from networkx.algorithms.assortativity import degree_mixing_dict
5
from networkx.generators import powerlaw_cluster_graph
6

    
7

    
8
def test_is_valid_joint_degree():
9
    ''' Tests for conditions that invalidate a joint degree dict '''
10

    
11
    # valid joint degree that satisfies all five conditions
12
    joint_degrees = {1: {4: 1},
13
                     2: {2: 2, 3: 2, 4: 2},
14
                     3: {2: 2, 4: 1},
15
                     4: {1: 1, 2: 2, 3: 1}}
16
    assert_true(is_valid_joint_degree(joint_degrees))
17

    
18
    # test condition 1
19
    # joint_degrees_1[1][4] not integer
20
    joint_degrees_1 = {1: {4: 1.5},
21
                       2: {2: 2, 3: 2, 4: 2},
22
                       3: {2: 2, 4: 1},
23
                       4: {1: 1.5, 2: 2, 3: 1}}
24
    assert_false(is_valid_joint_degree(joint_degrees_1))
25

    
26
    # test condition 2
27
    # degree_count[2] = sum(joint_degrees_2[2][j)/2, is not an int
28
    # degree_count[4] = sum(joint_degrees_2[4][j)/4, is not an int
29
    joint_degrees_2 = {1: {4: 1},
30
                       2: {2: 2, 3: 2, 4: 3},
31
                       3: {2: 2, 4: 1},
32
                       4: {1: 1, 2: 3, 3: 1}}
33
    assert_false(is_valid_joint_degree(joint_degrees_2))
34

    
35
    # test conditions 3 and 4
36
    # joint_degrees_3[1][4]>degree_count[1]*degree_count[4]
37
    joint_degrees_3 = {1: {4: 2},
38
                       2: {2: 2, 3: 2, 4: 2},
39
                       3: {2: 2, 4: 1},
40
                       4: {1: 2, 2: 2, 3: 1}}
41
    assert_false(is_valid_joint_degree(joint_degrees_3))
42

    
43
    # test condition 5
44
    # joint_degrees_5[1][1] not even
45
    joint_degrees_5 = {1: {1: 9}}
46
    assert_false(is_valid_joint_degree(joint_degrees_5))
47

    
48

    
49
def test_joint_degree_graph(ntimes=10):
50
    for _ in range(ntimes):
51
        seed = int(time.time())
52

    
53
        n, m, p = 20, 10, 1
54
        # generate random graph with model powerlaw_cluster and calculate
55
        # its joint degree
56
        g = powerlaw_cluster_graph(n, m, p, seed=seed)
57
        joint_degrees_g = degree_mixing_dict(g, normalized=False)
58

    
59
        # generate simple undirected graph with given joint degree
60
        # joint_degrees_g
61
        G = joint_degree_graph(joint_degrees_g)
62
        joint_degrees_G = degree_mixing_dict(G, normalized=False)
63

    
64
        # assert that the given joint degree is equal to the generated
65
        # graph's joint degree
66
        assert_true(joint_degrees_g == joint_degrees_G)