Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.04 KB)

1
#!/usr/bin/env python
2
from nose.tools import assert_true, assert_raises
3
import random
4

    
5
from networkx import random_reference, lattice_reference, sigma, omega
6
import networkx as nx
7

    
8
rng = random.Random(0)
9
rng = 42
10

    
11

    
12
def test_random_reference():
13
    G = nx.connected_watts_strogatz_graph(50, 6, 0.1, seed=rng)
14
    Gr = random_reference(G, niter=1, seed=rng)
15
    C = nx.average_clustering(G)
16
    Cr = nx.average_clustering(Gr)
17
    assert_true(C > Cr)
18

    
19
    assert_raises(nx.NetworkXError, random_reference, nx.Graph())
20
    assert_raises(nx.NetworkXNotImplemented, random_reference, nx.DiGraph())
21

    
22
    H = nx.Graph(((0, 1), (2, 3)))
23
    Hl = random_reference(H, niter=1, seed=rng)
24

    
25

    
26
def test_lattice_reference():
27
    G = nx.connected_watts_strogatz_graph(50, 6, 1, seed=rng)
28
    Gl = lattice_reference(G, niter=1, seed=rng)
29
    L = nx.average_shortest_path_length(G)
30
    Ll = nx.average_shortest_path_length(Gl)
31
    assert_true(Ll > L)
32

    
33
    assert_raises(nx.NetworkXError, lattice_reference, nx.Graph())
34
    assert_raises(nx.NetworkXNotImplemented, lattice_reference, nx.DiGraph())
35

    
36
    H = nx.Graph(((0, 1), (2, 3)))
37
    Hl = lattice_reference(H, niter=1)
38

    
39

    
40
def test_sigma():
41
    Gs = nx.connected_watts_strogatz_graph(50, 6, 0.1, seed=rng)
42
    Gr = nx.connected_watts_strogatz_graph(50, 6, 1, seed=rng)
43
    sigmas = sigma(Gs, niter=1, nrand=2, seed=rng)
44
    sigmar = sigma(Gr, niter=1, nrand=2, seed=rng)
45
    assert_true(sigmar < sigmas)
46

    
47

    
48
def test_omega():
49
    Gl = nx.connected_watts_strogatz_graph(50, 6, 0, seed=rng)
50
    Gr = nx.connected_watts_strogatz_graph(50, 6, 1, seed=rng)
51
    Gs = nx.connected_watts_strogatz_graph(50, 6, 0.1, seed=rng)
52
    omegal = omega(Gl, niter=1, nrand=1, seed=rng)
53
    omegar = omega(Gr, niter=1, nrand=1, seed=rng)
54
    omegas = omega(Gs, niter=1, nrand=1, seed=rng)
55
    print("omegas, omegal, omegar")
56
    print(omegas, omegal, omegar)
57
    assert_true(omegal < omegas and omegas < omegar)
58

    
59

    
60
# fixture for nose tests
61
def setup_module(module):
62
    from nose import SkipTest
63
    try:
64
        import numpy
65
    except:
66
        raise SkipTest("NumPy not available")