Beispiel #1
0
def test_random_modular_graph_between_fraction():
    """Test for graphs with non-zero between_fraction"""
    # We need to measure the degree within/between modules
    nnods = 120, 240, 360
    nmods = 2, 3, 4
    av_degrees = 8, 10, 16
    btwn_fractions = 0, 0.1, 0.3, 0.5

    for nnod in nnods:
        for nmod in nmods:
            for av_degree in av_degrees:
                for btwn_fraction in btwn_fractions:
                    g = mod.random_modular_graph(nnod, nmod, av_degree,
                                                 btwn_fraction)

                    # First, check the average degree.
                    av_degree_actual = np.mean(list(g.degree().values()))
                    # Since we are generating random graphs, the actual average
                    # degree we get may be off from the reuqested one by a bit.
                    # We allow it to be off by up to 1.
                    #print 'av deg:',av_degree, av_degree_actual  # dbg
                    nt.assert_true(
                        abs(av_degree - av_degree_actual) < 1.5,
                        """av deg: %.2f  av deg actual: %.2f -
                          This is a stochastic test - repeat to confirm.""" %
                        (av_degree, av_degree_actual))

                    # Now, check the between fraction
                    mat = nx.adj_matrix(g)

                    #compute the total number of edges in the real graph
                    nedg = nx.number_of_edges(g)

                    # sanity checks:
                    if nnod % nmod:
                        raise ValueError("nmod must divide nnod evenly")

                    #Compute the of nodes per module
                    nnod_mod = nnod / nmod

                    #compute what the values are in the real graph
                    blocks = [np.ones((nnod_mod, nnod_mod))] * nmod
                    mask = util.diag_stack(blocks)
                    mask[mask == 0] = 2
                    mask = np.triu(mask, 1)
                    btwn_real = np.sum(mat[mask == 2].flatten())
                    btwn_real_frac = btwn_real / nedg

                    #compare to what the actual values are
                    nt.assert_almost_equal(
                        btwn_fraction, btwn_real_frac, 1,
                        "This is a stochastic test, repeat to confirm failure")

                    nt.assert_true(
                        abs(btwn_fraction - btwn_real_frac) < 0.1,
                        """btwn fraction: %.2f  real btwn frac: %.2f -
                          This is a stochastic test - repeat to confirm.""" %
                        (btwn_fraction, btwn_real_frac))
Beispiel #2
0
def test_random_modular_graph_between_fraction():
    """Test for graphs with non-zero between_fraction"""
    # We need to measure the degree within/between modules
    nnods = 120, 240, 360
    nmods = 2, 3, 4
    av_degrees = 8, 10, 16
    btwn_fractions = 0, 0.1, 0.3, 0.5

    for nnod in nnods:
        for nmod in nmods:
            for av_degree in av_degrees:
                for btwn_fraction in btwn_fractions:
                    g = mod.random_modular_graph(nnod, nmod, av_degree,
                                                 btwn_fraction)

                    # First, check the average degree.
                    av_degree_actual = np.mean(list(g.degree().values()))
                    # Since we are generating random graphs, the actual average
                    # degree we get may be off from the reuqested one by a bit.
                    # We allow it to be off by up to 1.
                    #print 'av deg:',av_degree, av_degree_actual  # dbg
                    nt.assert_true (abs(av_degree-av_degree_actual)<1.5,
                          """av deg: %.2f  av deg actual: %.2f -
                          This is a stochastic test - repeat to confirm.""" %
                                          (av_degree, av_degree_actual))

                    # Now, check the between fraction
                    mat = nx.adj_matrix(g)

                    #compute the total number of edges in the real graph
                    nedg = nx.number_of_edges(g)

                     # sanity checks:
                    if nnod%nmod:
                        raise ValueError("nmod must divide nnod evenly")

                    #Compute the of nodes per module
                    nnod_mod = nnod/nmod

                    #compute what the values are in the real graph
                    blocks = [np.ones((nnod_mod, nnod_mod))] * nmod
                    mask = util.diag_stack(blocks)
                    mask[mask==0] = 2
                    mask = np.triu(mask,1)
                    btwn_real = np.sum(mat[mask == 2].flatten())
                    btwn_real_frac = btwn_real / nedg

                    #compare to what the actual values are
                    nt.assert_almost_equal(btwn_fraction,
                                           btwn_real_frac, 1,
                    "This is a stochastic test, repeat to confirm failure")

                    nt.assert_true(abs(btwn_fraction - btwn_real_frac) < 0.1,
                          """btwn fraction: %.2f  real btwn frac: %.2f -
                          This is a stochastic test - repeat to confirm.""" %
                                          (btwn_fraction, btwn_real_frac))
Beispiel #3
0
def test_diag_stack():
    """Manual verification of simple stacking."""
    a = np.empty((2,2))
    a.fill(1)
    b = np.empty((3,3))
    b.fill(2)
    c = np.empty((2,3))
    c.fill(3)

    d = util.diag_stack((a,b,c))

    d_true = np.array([[ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  2.,  2.,  2.,  0.,  0.,  0.],
       [ 0.,  0.,  2.,  2.,  2.,  0.,  0.,  0.],
       [ 0.,  0.,  2.,  2.,  2.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  3.,  3.,  3.],
       [ 0.,  0.,  0.,  0.,  0.,  3.,  3.,  3.]])

    npt.assert_equal(d, d_true)
Beispiel #4
0
def test_diag_stack():
    """Manual verification of simple stacking."""
    a = np.empty((2, 2))
    a.fill(1)
    b = np.empty((3, 3))
    b.fill(2)
    c = np.empty((2, 3))
    c.fill(3)

    d = util.diag_stack((a, b, c))

    d_true = np.array([[1., 1., 0., 0., 0., 0., 0., 0.],
                       [1., 1., 0., 0., 0., 0., 0., 0.],
                       [0., 0., 2., 2., 2., 0., 0., 0.],
                       [0., 0., 2., 2., 2., 0., 0., 0.],
                       [0., 0., 2., 2., 2., 0., 0., 0.],
                       [0., 0., 0., 0., 0., 3., 3., 3.],
                       [0., 0., 0., 0., 0., 3., 3., 3.]])

    npt.assert_equal(d, d_true)