Example #1
0
def test_mutual_information():
    """ Test the function which returns the mutual information in two
    partitions

    XXX - This test is currently incomplete - it only checks the most basic
    case of MI(x, x)==1, but doesn't do any non-trivial checks.
    """

    # nnod_mod, av_degrees, nmods
    networks = [ [4, [2, 3], [2, 4, 6]],
                 [8, [4, 6], [4, 6, 8]],
                 [40, [20], [2]] ]

    for nnod_mod, av_degrees, nmods in networks:
        for nmod in nmods:
            nnod = nnod_mod*nmod
            for av_degree in av_degrees:
                #make a graph object
                g = mod.random_modular_graph(nnod, nmod, av_degree)

                #Compute the of nodes per module
                nnod_mod = nnod/nmod
                #Make a "correct" partition for the graph
                ppart = mod.perfect_partition(nmod,nnod_mod)

                #graph_out, mod_array =mod.simulated_annealing(g, temperature =
                #temperature,temp_scaling = temp_scaling, tmin=tmin)

                #test the perfect case for now: two of the same partition
                #returns 1
                mi_orig  = mod.mutual_information(ppart,ppart)
                yield npt.assert_equal(mi_orig,1)

                #move one node and test that mutual_information comes out
                #correctly
                graph_partition = mod.GraphPartition(g,ppart)
                graph_partition.node_update(0,0,1)

                mi = mod.mutual_information(ppart,graph_partition.index)
                yield npt.assert_array_less(mi, mi_orig)
                ## NOTE: CORRECTNESS NOT TESTED YET

                #merge modules and check that mutual information comes out
                #correctly/lower
                graph_partition2 = mod.GraphPartition(g,ppart)
                merged_module, e_new, a_new, d,t,m1,m2,x = graph_partition2.compute_module_merge(0,1)
                graph_partition2.apply_module_merge(m1,m2,merged_module,e_new,a_new)
                mi2 = mod.mutual_information(ppart,graph_partition2.index)
                yield npt.assert_array_less(mi2,mi_orig)
                ## NOTE: CORRECTNESS NOT TESTED YET

                #split modules and check that mutual information comes out
                #correclty/lower
                graph_partition3 = mod.GraphPartition(g,ppart)
                n1 = list(graph_partition3.index[0])[::2]
                n2 = list(graph_partition3.index[0])[1::2]
                split_modules,e_new,a_new,d,t,m,n1,n2 = graph_partition3.compute_module_split(0,n1,n2)
                graph_partition3.apply_module_split(m,n1,n2,split_modules,e_new,a_new)
                mi3 = mod.mutual_information(ppart,graph_partition3.index)
                yield npt.assert_array_less(mi3,mi_orig)
Example #2
0
def test_mutual_information_empty():
    """Validate that empty modules don't affect MI.
    """
    # Define two simple partitions that are off by one assignment
    a = {0:[0, 1], 1:[2, 3], 2:[4, 5]}
    b = {0:[0, 1], 1:[2, 3], 2:[4, 5], 3:[]}

    try:
        mod.mutual_information(a, b)
    except ValueError, e:
        nt.assert_equals(e.args[0], "Empty module in second partition.")
Example #3
0
def test_mutual_information_empty():
    """Validate that empty modules don't affect MI.
    """
    # Define two simple partitions that are off by one assignment
    a = {0: [0, 1], 1: [2, 3], 2: [4, 5]}
    b = {0: [0, 1], 1: [2, 3], 2: [4, 5], 3: []}

    try:
        mod.mutual_information(a, b)
    except ValueError, e:
        nt.assert_equals(e.args[0], "Empty module in second partition.")
Example #4
0
def test_mutual_information_simple():
    """MI computations with hand-validated values.
    """
    from math import log
    # Define two simple partitions that are off by one assignment
    a = {0:[0, 1], 1:[2, 3], 2:[4, 5]}
    b = {0:[0, 1], 1:[2, 3, 4], 2:[5]}
    N_true = np.array([ [2,0,0], [0,2,0], [0,1,1] ], dtype=float)
    N = mod.confusion_matrix(a, b)
    # test confusion matrix
    npt.assert_equal(N, N_true)
    # Now compute mi by hand
    num = -6*log(3)-4*log(2)
    den = -(3*log(2)+8*log(3)+log(6))
    mi_true = num/den
    mi = mod.mutual_information(a, b)
    npt.assert_almost_equal(mi, mi_true)
    # Let's now flip the labels and confirm that the computation is impervious
    # to module labels
    b2 = {2:[0, 1], 0:[2, 3, 4], 1:[5]}
    npt.assert_almost_equal(mod.mutual_information(b, b2), 1)
    npt.assert_almost_equal(mod.mutual_information(a, b2), mi)
Example #5
0
def test_mutual_information_simple():
    """MI computations with hand-validated values.
    """
    from math import log
    # Define two simple partitions that are off by one assignment
    a = {0: [0, 1], 1: [2, 3], 2: [4, 5]}
    b = {0: [0, 1], 1: [2, 3, 4], 2: [5]}
    N_true = np.array([[2, 0, 0], [0, 2, 0], [0, 1, 1]], dtype=float)
    N = mod.confusion_matrix(a, b)
    # test confusion matrix
    npt.assert_equal(N, N_true)
    # Now compute mi by hand
    num = -6 * log(3) - 4 * log(2)
    den = -(3 * log(2) + 8 * log(3) + log(6))
    mi_true = num / den
    mi = mod.mutual_information(a, b)
    npt.assert_almost_equal(mi, mi_true)
    # Let's now flip the labels and confirm that the computation is impervious
    # to module labels
    b2 = {2: [0, 1], 0: [2, 3, 4], 1: [5]}
    npt.assert_almost_equal(mod.mutual_information(b, b2), 1)
    npt.assert_almost_equal(mod.mutual_information(a, b2), mi)
Example #6
0
def SA():
    """ Test the simulated annealing script"""
    #nnod_mod, av_degrees, nmods
    #networks = [ [4, [2, 3], [2, 4, 6]]]#,
    #networks =  [ [8, [4, 6], [4, 6, 8]]]
    #networks = [[40, [20], [2]]]
    networks = [[32, [16], [4]]]
    #networks = [[64, [12], [6]]]
    btwn_fracs = [0]
    temperature = 10
    temp_scaling = 0.9995
    tmin = 1e-4
    nochange_ratio_min = 0.01

    #keep time

    for nnod_mod, av_degrees, nmods in networks:
        for nmod in nmods:
            nnod = nnod_mod * nmod
            for av_degree in av_degrees:
                for btwn_frac in btwn_fracs:
                    t1 = time.clock()
                    g = mod.random_modular_graph(nnod, nmod, av_degree,
                                                 btwn_frac)
                    #Compute the # of nodes per module
                    nnod_mod = nnod / nmod
                    #Make a "correct" partition for the graph
                    ppart = mod.perfect_partition(nmod, nnod_mod)

                    graph_out, energy_array, rej_array, temp_array = mod.simulated_annealing(
                        g,
                        temperature=temperature,
                        temp_scaling=temp_scaling,
                        tmin=tmin,
                        nochange_ratio_min=nochange_ratio_min)

                    print "perfect partition", ppart
                    print "SA partition", graph_out.index

                    t2 = time.clock()
                    print 'Elapsed time: ', float(t2 - t1) / 60, 'minutes'
                    print 'partition similarity: ', mod.mutual_information(
                        ppart, graph_out.index)
                    return graph_out, g, energy_array, rej_array, ppart, temp_array
Example #7
0
def SA():
    """ Test the simulated annealing script"""
    # nnod_mod, av_degrees, nmods
    # networks = [ [4, [2, 3], [2, 4, 6]]]#,
    # networks =  [ [8, [4, 6], [4, 6, 8]]]
    # networks = [[40, [20], [2]]]
    networks = [[32, [16], [4]]]
    # networks = [[64, [12], [6]]]
    btwn_fracs = [0]
    temperature = 10
    temp_scaling = 0.9995
    tmin = 1e-4
    nochange_ratio_min = 0.01

    # keep time

    for nnod_mod, av_degrees, nmods in networks:
        for nmod in nmods:
            nnod = nnod_mod * nmod
            for av_degree in av_degrees:
                for btwn_frac in btwn_fracs:
                    t1 = time.clock()
                    g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac)
                    # Compute the # of nodes per module
                    nnod_mod = nnod / nmod
                    # Make a "correct" partition for the graph
                    ppart = mod.perfect_partition(nmod, nnod_mod)

                    graph_out, energy_array, rej_array, temp_array = mod.simulated_annealing(
                        g,
                        temperature=temperature,
                        temp_scaling=temp_scaling,
                        tmin=tmin,
                        nochange_ratio_min=nochange_ratio_min,
                    )

                    print "perfect partition", ppart
                    print "SA partition", graph_out.index

                    t2 = time.clock()
                    print "Elapsed time: ", float(t2 - t1) / 60, "minutes"
                    print "partition similarity: ", mod.mutual_information(ppart, graph_out.index)
                    return graph_out, g, energy_array, rej_array, ppart, temp_array
Example #8
0
def test_sim_anneal_simple():
    """Very simple simulated_annealing test with a small network"""

    #
    nnod, nmod, av_degree, btwn_frac = 24, 3, 4, 0
    g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac)

    #Compute the # of nodes per module
    nnod_mod = nnod/nmod
    #Make a "correct" partition for the graph
    ppart = mod.perfect_partition(nmod,nnod_mod)

    temperature = 10
    temp_scaling = 0.95
    tmin=1

    graph_out, graph_dict = mod.simulated_annealing(g,
               temperature = temperature, temp_scaling = temp_scaling,
               tmin=tmin, extra_info = True, debug=True)

    # Ensure that there are no empty modules
    util.assert_no_empty_modules(graph_out.index)

    mi = mod.mutual_information(ppart, graph_out.index)
Example #9
0
def danon_benchmark():
    """This test comes from Danon et al 2005. It will create the line plot of
    Mututal Information vs. betweenness fraction to assess the performance of
    the simulated annealing algorithm."""
    networks = [[32, [16], [6]]]
    btwn_fracs = [float(i)/100 for i in range(0,80,3)]

    temperature = 0.1
    temp_scaling = 0.9995
    tmin=1e-4

    num_reps = range(1)
    mi_arr=np.empty((len(btwn_fracs),len(num_reps)))

    #keep time
    for rep in num_reps:
        t1 = time.clock()
        for nnod_mod, av_degrees, nmods in networks:
            for nmod in nmods:
                nnod = nnod_mod*nmod
                for av_degree in av_degrees:
                    x_mod = []
                    for ix,btwn_frac in enumerate(btwn_fracs):
                        print 'btwn_frac: ',btwn_frac
                        g = mod.random_modular_graph(nnod, nmod, av_degree,btwn_frac)
                        #Compute the # of nodes per module
                        nnod_mod = nnod/nmod
                        #Make a "correct" partition for the graph
                        ppart = mod.perfect_partition(nmod,nnod_mod)

                        graph_out, graph_dict =mod.simulated_annealing(g,
                        temperature = temperature,temp_scaling = temp_scaling,
                        tmin=tmin, extra_info = True)

                        #print "SA partition",graph_out.index
                        mi = mod.mutual_information(ppart,graph_out.index)
                        t2 = time.clock()
                        print 'Elapsed time: ', (float(t2-t1)/60), ' minutes'
                        print 'partition similarity: ',mi
                        mi_arr[ix,rep] = mi
                        ## plot_partition(g,graph_out.index,'mi: '+ str(mi),'danon_test_6mod'+str(btwn_frac)+'_graph.png')
                        x_mod.append(betweenness_to_modularity(g,ppart))


                    ## mi_arr_avg = np.mean(mi_arr,1)
                    ## plt.figure()
                    ## plt.plot(btwn_fracs,mi_arr_avg)
                    ## plt.xlabel('Betweenness fraction')
                    ## plt.ylabel('Mutual information')
                    ## plt.savefig('danon_test_6mod/danontest_btwn.png')

                    ## plt.figure()
                    ## plt.plot(x_mod,mi_arr_avg)
                    ## plt.xlabel('Modularity')
                    ## plt.ylabel('Mutual information')
                    ## plt.savefig('danon_test_6mod/danontest_mod.png')

    #plt.figure()
    #plt.plot(graph_dict['energy'], label = 'energy')
    #plt.plot(graph_dict['temperature'], label = 'temperature')
    #plt.xlabel('Iteration')

    return mi_arr
Example #10
0

def test_mutual_information_empty():
    """Validate that empty modules don't affect MI.
    """
    # Define two simple partitions that are off by one assignment
    a = {0:[0, 1], 1:[2, 3], 2:[4, 5]}
    b = {0:[0, 1], 1:[2, 3], 2:[4, 5], 3:[]}

    try:
        mod.mutual_information(a, b)
    except ValueError, e:
        nt.assert_equals(e.args[0], "Empty module in second partition.")

    try:
        mod.mutual_information(b, a)
    except ValueError, e:
        nt.assert_equals(e.args[0], "Empty module in first partition.")


def test_mutual_information():
    """ Test the function which returns the mutual information in two
    partitions

    XXX - This test is currently incomplete - it only checks the most basic
    case of MI(x, x)==1, but doesn't do any non-trivial checks.
    """

    # nnod_mod, av_degrees, nmods
    networks = [ [4, [2, 3], [2, 4, 6]],
                 [8, [4, 6], [4, 6, 8]],
Example #11
0
def test_mutual_information():
    """ Test the function which returns the mutual information in two
    partitions

    XXX - This test is currently incomplete - it only checks the most basic
    case of MI(x, x)==1, but doesn't do any non-trivial checks.
    """

    # nnod_mod, av_degrees, nmods
    networks = [[4, [2, 3], [2, 4, 6]], [8, [4, 6], [4, 6, 8]],
                [40, [20], [2]]]

    for nnod_mod, av_degrees, nmods in networks:
        for nmod in nmods:
            nnod = nnod_mod * nmod
            for av_degree in av_degrees:
                #make a graph object
                g = mod.random_modular_graph(nnod, nmod, av_degree)

                #Compute the of nodes per module
                nnod_mod = nnod / nmod
                #Make a "correct" partition for the graph
                ppart = mod.perfect_partition(nmod, nnod_mod)

                #graph_out, mod_array =mod.simulated_annealing(g, temperature =
                #temperature,temp_scaling = temp_scaling, tmin=tmin)

                #test the perfect case for now: two of the same partition
                #returns 1
                mi_orig = mod.mutual_information(ppart, ppart)
                npt.assert_equal(mi_orig, 1)

                #move one node and test that mutual_information comes out
                #correctly
                graph_partition = mod.GraphPartition(g, ppart)
                graph_partition.node_update(0, 0, 1)

                mi = mod.mutual_information(ppart, graph_partition.index)
                npt.assert_array_less(mi, mi_orig)
                ## NOTE: CORRECTNESS NOT TESTED YET

                #merge modules and check that mutual information comes out
                #correctly/lower
                graph_partition2 = mod.GraphPartition(g, ppart)
                merged_module, e_new, a_new, d, t, m1, m2, x = graph_partition2.compute_module_merge(
                    0, 1)
                graph_partition2.apply_module_merge(m1, m2, merged_module,
                                                    e_new, a_new)
                mi2 = mod.mutual_information(ppart, graph_partition2.index)
                npt.assert_array_less(mi2, mi_orig)
                ## NOTE: CORRECTNESS NOT TESTED YET

                #split modules and check that mutual information comes out
                #correclty/lower
                graph_partition3 = mod.GraphPartition(g, ppart)
                n1 = set(list(graph_partition3.index[0])[::2])
                n2 = set(list(graph_partition3.index[0])[1::2])

                split_modules, e_new, a_new, d, t, m, n1, n2 = graph_partition3.compute_module_split(
                    0, n1, n2)
                graph_partition3.apply_module_split(m, n1, n2, split_modules,
                                                    e_new, a_new)
                mi3 = mod.mutual_information(ppart, graph_partition3.index)
                npt.assert_array_less(mi3, mi_orig)
Example #12
0
def danon_benchmark():
    """This test comes from Danon et al 2005. It will create the line plot of
    Mututal Information vs. betweenness fraction to assess the performance of
    the simulated annealing algorithm."""
    networks = [[32, [16], [6]]]
    btwn_fracs = [float(i) / 100 for i in range(0, 80, 3)]

    temperature = 0.1
    temp_scaling = 0.9995
    tmin = 1e-4

    num_reps = range(1)
    mi_arr = np.empty((len(btwn_fracs), len(num_reps)))

    #keep time
    for rep in num_reps:
        t1 = time.clock()
        for nnod_mod, av_degrees, nmods in networks:
            for nmod in nmods:
                nnod = nnod_mod * nmod
                for av_degree in av_degrees:
                    x_mod = []
                    for ix, btwn_frac in enumerate(btwn_fracs):
                        print 'btwn_frac: ', btwn_frac
                        g = mod.random_modular_graph(nnod, nmod, av_degree,
                                                     btwn_frac)
                        #Compute the # of nodes per module
                        nnod_mod = nnod / nmod
                        #Make a "correct" partition for the graph
                        ppart = mod.perfect_partition(nmod, nnod_mod)

                        graph_out, graph_dict = mod.simulated_annealing(
                            g,
                            temperature=temperature,
                            temp_scaling=temp_scaling,
                            tmin=tmin,
                            extra_info=True)

                        #print "SA partition",graph_out.index
                        mi = mod.mutual_information(ppart, graph_out.index)
                        t2 = time.clock()
                        print 'Elapsed time: ', (float(t2 - t1) /
                                                 60), ' minutes'
                        print 'partition similarity: ', mi
                        mi_arr[ix, rep] = mi
                        ## plot_partition(g,graph_out.index,'mi: '+ str(mi),'danon_test_6mod'+str(btwn_frac)+'_graph.png')
                        x_mod.append(betweenness_to_modularity(g, ppart))

                    ## mi_arr_avg = np.mean(mi_arr,1)
                    ## plt.figure()
                    ## plt.plot(btwn_fracs,mi_arr_avg)
                    ## plt.xlabel('Betweenness fraction')
                    ## plt.ylabel('Mutual information')
                    ## plt.savefig('danon_test_6mod/danontest_btwn.png')

                    ## plt.figure()
                    ## plt.plot(x_mod,mi_arr_avg)
                    ## plt.xlabel('Modularity')
                    ## plt.ylabel('Mutual information')
                    ## plt.savefig('danon_test_6mod/danontest_mod.png')

    #plt.figure()
    #plt.plot(graph_dict['energy'], label = 'energy')
    #plt.plot(graph_dict['temperature'], label = 'temperature')
    #plt.xlabel('Iteration')

    return mi_arr
Example #13
0

def test_mutual_information_empty():
    """Validate that empty modules don't affect MI.
    """
    # Define two simple partitions that are off by one assignment
    a = {0: [0, 1], 1: [2, 3], 2: [4, 5]}
    b = {0: [0, 1], 1: [2, 3], 2: [4, 5], 3: []}

    try:
        mod.mutual_information(a, b)
    except ValueError, e:
        nt.assert_equals(e.args[0], "Empty module in second partition.")

    try:
        mod.mutual_information(b, a)
    except ValueError, e:
        nt.assert_equals(e.args[0], "Empty module in first partition.")


def test_mutual_information():
    """ Test the function which returns the mutual information in two
    partitions

    XXX - This test is currently incomplete - it only checks the most basic
    case of MI(x, x)==1, but doesn't do any non-trivial checks.
    """

    # nnod_mod, av_degrees, nmods
    networks = [[4, [2, 3], [2, 4, 6]], [8, [4, 6], [4, 6, 8]],
                [40, [20], [2]]]