Example #1
0
def test_adjust_partition():
    e = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'),
                   skiprows=3, dtype=int)[:, :2] - 1
    g = nx.Graph()
    g.add_edges_from(e)

    p0 = mod.newman_partition(g)
Example #2
0
def test_adjust_partition():
    e = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'),
                   skiprows=3,
                   dtype=int)[:, :2] - 1
    g = nx.Graph()
    g.add_edges_from(e)

    p0 = mod.newman_partition(g)
def graph_analysis (graph):
    part = modularity.newman_partition(graph)
    print part.modularity()
    part = util.dictset_to_listset(part.index)
    part = weighted_modularity.WeightedPartition(graph,part)
    pc = np.array(participation_coefficient(part,catch_edgeless_node=False).values())
    wmd = np.array(within_community_degree(part,catch_edgeless_node=False).values())
    return pc,wmd
Example #4
0
def test_adjust_partition():
    e = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'),
                   skiprows=3, dtype=int)[:, :2] - 1
    g = nx.Graph()
    g.add_edges_from(e)

    p0 = mod.newman_partition(g)
    p1 = mod.adjust_partition(g, p0, max_iter=6)

    npt.assert_(p0 > 0.38)
    npt.assert_(p1 > 0.42)
Example #5
0
def test_adjust_partition():
    e = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'),
                   skiprows=3,
                   dtype=int)[:, :2] - 1
    g = nx.Graph()
    g.add_edges_from(e)

    p0 = mod.newman_partition(g)
    p1 = mod.adjust_partition(g, p0, max_iter=6)

    npt.assert_(p0 > 0.38)
    npt.assert_(p1 > 0.42)
Example #6
0
def test_newman_partition():
    """ Test Newman Partition function """
    tmpmat = np.random.random((10, 10))
    tmpmat[tmpmat < .5] = 0
    graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
    npt.assert_raises(ValueError, mod.newman_partition, graph)
    tmpmat[:] = 0
    # test that no edges raises error (from GraphPartition)
    graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
    npt.assert_raises(ValueError, mod.newman_partition, graph)
    tmpmat[:] = 1
    util.fill_diagonal(tmpmat, 0)
    graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
    part = mod.newman_partition(graph)
    ## if all edges are connected expect only one partition
    expected_part = {0: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])}
    nt.assert_equal(part.index, expected_part)
Example #7
0
def test_newman_partition():
    """ Test Newman Partition function """
    tmpmat = np.random.random((10, 10))
    tmpmat[tmpmat < 0.5] = 0
    graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
    npt.assert_raises(ValueError, mod.newman_partition, graph)
    tmpmat[:] = 0
    # test that no edges raises error (from GraphPartition)
    graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
    npt.assert_raises(ValueError, mod.newman_partition, graph)
    tmpmat[:] = 1
    util.fill_diagonal(tmpmat, 0)
    graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
    part = mod.newman_partition(graph)
    ## if all edges are connected expect only one partition
    expected_part = {0: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])}
    nt.assert_equal(part.index, expected_part)
Example #8
0
def main(argv=sys.argv):

    filename = argv[
        1]  # file name convention: cmat_<project>_<atlas>_<subgroup>_S<subject#>_B<block#>_<cost>.npy
    failedcmd = argv[2]  # newman or adjust

    # Load saved cmat generated from NewmanMod.py
    cmat_1 = np.load(filename)

    # newman.partition fails if there are Inf/nan's in cmat_1
    # this occurs on the diagonal (Z-transformed autocorrelation == Inf)
    # so we'll convert the diagonal to 0
    # NOTE: this manipulation to cmat_1 also changes modularity values
    # calculated with simulated annealing - we don't understand why this happens

    temp = np.isnan(cmat_1)
    cmat_1[temp] = 0

    G1 = nx.Graph(weighted=False)
    G1 = nx.from_numpy_matrix(cmat_1, G1)

    P1 = md.newman_partition(G1)
    if failedcmd == 'adjust':
        P1 = md.adjust_partition(G1, P1)

    print('Modularity: ', P1.modularity())

    #save partition information (nodelist is not relabeled here)
    pname = 'part_norelabel_' + '_'.join(filename.split('_')[1:])
    pout = open(pname, 'w')
    pickle.dump(P1.index, pout)
    pout.close()
    print('Wrote ', pname)

    #clear variables
    del P1, cmat_1, G1
Example #9
0
def calc_spectral_modularity(mat, cost):
    """ Use brainx to 
    generate binary matrix theshold based on cost
    run newman spectral modularity
    
    Parameters
    ----------
    mat : numpy matrix
        symmetric adjacency matrix, should not contain nan
    cost: float
        cost used to binarize adjacency matrix
        
    Returns
    -------
    part : GraphPartition
        part.index provides access to found communities
    true_cost : float
        the actual cost associated with thresholded adj matrix
    """
    mask, real_cost = util.threshold_adjacency_matrix(mat, cost)
    true_cost = util.find_true_cost(mask)
    graph = nx.from_numpy_matrix(mask)
    part = modularity.newman_partition(graph)
    return part, true_cost 
Example #10
0
def main(argv = sys.argv):

    filename = argv[1] # file name convention: cmat_<project>_<atlas>_<subgroup>_S<subject#>_B<block#>_<cost>.npy
    failedcmd = argv[2] # newman or adjust
    

    # Load saved cmat generated from NewmanMod.py
    cmat_1 = np.load(filename)

    # newman.partition fails if there are Inf/nan's in cmat_1 
    # this occurs on the diagonal (Z-transformed autocorrelation == Inf)
    # so we'll convert the diagonal to 0
    # NOTE: this manipulation to cmat_1 also changes modularity values 
    # calculated with simulated annealing - we don't understand why this happens

    temp=np.isnan(cmat_1)
    cmat_1[temp]=0

    G1 = nx.Graph(weighted = False)
    G1 = nx.from_numpy_matrix(cmat_1,G1)

    P1 = md.newman_partition(G1)
    if failedcmd == 'adjust':
        P1 = md.adjust_partition(G1,P1)

    print('Modularity: ', P1.modularity())

    #save partition information (nodelist is not relabeled here)
    pname = 'part_norelabel_' + '_'.join(filename.split('_')[1:])
    pout = open(pname,'w')
    pickle.dump(P1.index,pout)
    pout.close()
    print('Wrote ',pname)

    #clear variables
    del P1, cmat_1, G1
Example #11
0
#!/usr/bin/env python
"""This code is crashing with:

IndexError: index 32 is out of bounds for axis 1 with size 25
"""

import cPickle as pickle
from brainx import modularity as md

G3 = pickle.load(open("G3.pck", "rb"))
P3 = md.newman_partition(G3)