Пример #1
0
    def __init__(self, g, features, n_machines, radius, activation, device):
        super().__init__()
        adj = nx.adj_matrix(g)
        p = my.adj2p(sp.sparse.triu(adj))
        adj = tf.cast(my.sparse_sp2tf(adj), tf.float32)
        deg = tf.expand_dims(tf.sparse_reduce_sum(adj, 1), 1)

        lg = nx.line_graph(g)
        adj_lg = tf.cast(my.sparse_sp2tf(nx.adj_matrix(lg)), tf.float32)
        deg_lg = tf.expand_dims(tf.sparse_reduce_sum(adj_lg, 1), 1)

        for i, (m, n) in enumerate(zip(features[:-1], features[1:])):
            setattr(
                self, 'layer%d' % i,
                GNNLayer(m, n, adj, deg, adj_lg, deg_lg, p, radius,
                         activation))

        self.n_layers = i + 1

        self.dense = tf.keras.layers.Dense(input_shape=(n, ), units=n_machines)

        self.device = device

        with tf.device(device):
            x = deg
            x -= tf.reduce_mean(x)
            x /= tf.sqrt(tf.reduce_mean(tf.square(x)))

            y = deg_lg
            y -= tf.reduce_mean(y)
            y /= tf.sqrt(tf.reduce_mean(tf.square(y)))

            self.x, self.y = x, y
Пример #2
0
def features(G,G1):
    A = nx.adj_matrix(G)
    n = len(A)
    A1 = nx.adj_matrix(G1)
    
    D = A1[:n,:n]-A
    
    pos = 0
    neg = 0
    
    iz = range(n)
    jz = range(n)
    
    shuffle(iz)
    shuffle(jz)
    
    for i in iz:
        for j in jz:
            
            if D[i,j] == 1:
                pos +=1
                train += [ [dot(A.A[i] , A.A[j]) / norm(A.A[i])* norm(A.A[j]),M[i,j],FF[i,j]]]
                target += [D[i,j]]
            elif neg < c:
                neg +=1
                train += [[dot(A.A[i] , A.A[j]) / norm(A.A[i])* norm(A.A[j]),M[i,j],FF[i,j]]]
                target += [D[i,j]]
    
    return train, target
Пример #3
0
def load_data(dataset_dir, dataset):
    feats = np.load(dataset_dir + f"/{dataset}-feats.npy")
    npz_path = Path(f'{dataset_dir}/{dataset}.npz')
    if dataset in ['cora', 'citeseer', 'pubmed']:
        # if not npz_path.exists():
        G = json_graph.node_link_graph(
            json.load(open(dataset_dir + "/{}-G.json".format(dataset))))
        original_adj = nx.adj_matrix(G)
        # sparse.save_npz(str(npz_path), original_adj)
        labels = json.load(
            open(dataset_dir + "/{}-class_map.json".format(dataset)))
        train_ids = [
            n for n in G.nodes()
            if not G.nodes[n]['val'] and not G.nodes[n]['test']
        ]
        test_ids = [n for n in G.nodes() if G.nodes[n]['test']]
        train_labels = [labels[str(i)] for i in train_ids]
        test_labels = [labels[str(i)] for i in test_ids]
        labels = list(labels.values())
    elif dataset in ['reddit', 'Amazon2M']:
        if not npz_path.exists():
            G = read_gpickle(dataset_dir + f'/{dataset}.gpickle')
            original_adj = nx.adj_matrix(G)
            sparse.save_npz(str(npz_path), original_adj)
        else:
            original_adj = sparse.load_npz(str(npz_path))
        train_ids = np.load(dataset_dir + f'/{dataset}_train_data.npy')
        test_ids = np.load(dataset_dir + f'/{dataset}_test_data.npy')
        labels = np.load(dataset_dir + f'/{dataset}_labels.npy')
        train_labels = labels[train_ids]
        test_labels = labels[test_ids]
    return original_adj, labels, train_ids, test_ids, train_labels, test_labels, feats
Пример #4
0
 def compare(self, g1, g2, alpha, verbose=False):
     """Compute the kernel value (similarity) between two graphs. 
     
     Parameters
     ----------
     g1 : networkx.Graph
         First graph.
     g2 : networkx.Graph
         Second graph.
     alpha : interger < 1
         A rule of thumb for setting it is to take the largest power of 10
         which is samller than 1/d^2, being d the largest degree in the 
         dataset of graphs.    
         
     Returns
     -------        
     k : The similarity value between g1 and g2.
     """
     am1 = nx.adj_matrix(g1)
     am2 = nx.adj_matrix(g2)
     x = np.zeros((len(am1),len(am2)))
     A = self.smt_filter(x,am1,am2,alpha)
     b = np.ones(len(am1)*len(am2))
     tol = 1e-6
     maxit = 20
     pcg(A,b,x,tol,maxit)
     return np.sum(x)
Пример #5
0
def hill_climbing(data, graph, **kwargs):
    """Hill Climbing optimization: a greedy exploration algorithm."""
    nodelist = list(data.columns)
    data = scale(data.as_matrix()).astype('float32')
    tested_candidates = [nx.adj_matrix(graph, nodelist=nodelist, weight=None)]
    best_score = parallel_graph_evaluation(data, tested_candidates[0].todense(), ** kwargs)
    best_candidate = graph
    can_improve = True
    while can_improve:
        can_improve = False
        for (i, j) in best_candidate.edges():
            test_graph = deepcopy(best_candidate)
            test_graph.add_edge(j, i, weight=test_graph[i][j]['weight'])
            test_graph.remove_edge(i, j)
            tadjmat = nx.adj_matrix(test_graph, nodelist=nodelist, weight=None)
            if (nx.is_directed_acyclic_graph(test_graph) and not any([(tadjmat != cand).nnz ==
                                                                      0 for cand in tested_candidates])):
                tested_candidates.append(tadjmat)
                score = parallel_graph_evaluation(data, tadjmat.todense(), **kwargs)
                if score < best_score:
                    can_improve = True
                    best_candidate = test_graph
                    best_score = score
                    break
    return best_candidate
Пример #6
0
def hill_climbing(data, graph, **kwargs):
    """Hill Climbing optimization: a greedy exploration algorithm."""
    if isinstance(data, th.utils.data.Dataset):
        nodelist = data.get_names()
    elif isinstance(data, pd.DataFrame):
        nodelist = list(data.columns)
    else:
        raise TypeError('Data type not understood')
    tested_candidates = [nx.adj_matrix(graph, nodelist=nodelist, weight=None)]
    best_score = parallel_graph_evaluation(data,
                                           tested_candidates[0].todense(),
                                           **kwargs)
    best_candidate = graph
    can_improve = True
    while can_improve:
        can_improve = False
        for (i, j) in best_candidate.edges():
            test_graph = deepcopy(best_candidate)
            test_graph.add_edge(j, i, weight=test_graph[i][j]['weight'])
            test_graph.remove_edge(i, j)
            tadjmat = nx.adj_matrix(test_graph, nodelist=nodelist, weight=None)
            if (nx.is_directed_acyclic_graph(test_graph)
                    and not any([(tadjmat != cand).nnz == 0
                                 for cand in tested_candidates])):
                tested_candidates.append(tadjmat)
                score = parallel_graph_evaluation(data, tadjmat.todense(),
                                                  **kwargs)
                if score < best_score:
                    can_improve = True
                    best_candidate = test_graph
                    best_score = score
                    break
    return best_candidate
Пример #7
0
def exploratory_hill_climbing(data,
                              graph,
                              proba=0.1,
                              decay=0.95,
                              max_trials=20,
                              **kwargs):
    """Hill climbing with a bit more exploration."""
    tested_candidates = [nx.adj_matrix(graph, weight=None)]
    best_score = parallel_graph_evaluation(data, graph, **kwargs)
    best_candidate = graph
    can_improve = True
    while can_improve:
        can_improve = False
        for (i, j) in best_candidate.edges():
            test_graph = deepcopy(best_candidate)
            test_graph.remove_edge(i, j)
            test_graph.add_edge(j, i)
            tadjmat = nx.adj_matrix(test_graph, weight=None)
            if (nx.is_directed_acyclic_graph(test_graph)
                    and tadjmat not in tested_candidates):
                tested_candidates.append(tadjmat)
                score = parallel_graph_evaluation(data, test_graph, **kwargs)
                if score < best_score:
                    can_improve = True
                    best_candidate = test_graph
                    best_score = score
                    break
    return best_candidate
def weighted_lambda_adjacency_diff(net1, net2, weighted='weight'):
    """
    """
    net1_eigs = np.linalg.eigvalsh(
        nx.adj_matrix(net1, weight=weighted).toarray())
    net2_eigs = np.linalg.eigvalsh(
        nx.adj_matrix(net2, weight=weighted).toarray())
    return lambda_diff(net1_eigs, net2_eigs)
Пример #9
0
def GenerateGossipMatrix(NoAgents,Topology):
    if(Topology == "Grid"):
        Graph_Adj = nx.adj_matrix(nx.grid_graph([int(np.sqrt(NoAgents)),int(np.sqrt(NoAgents))],periodic=True)).toarray()
    elif(Topology=="Cycle"):
        Graph_Adj = nx.adj_matrix(nx.grid_graph([NoAgents],periodic=True)).toarray()
    elif(Topology=="Expander"):
        Graph_Adj = nx.adj_matrix(nx.margulis_gabber_galil_graph(int(np.sqrt(NoAgents)))).toarray()
    
    Graph_Gossip = np.identity(NoAgents) -  (np.diag(Graph_Adj.sum(axis=0)) - Graph_Adj )/(Graph_Adj.sum(axis=0).max() + 1)
    if( np.sum(Graph_Gossip.sum(0) != 1.) > 0 or np.sum(Graph_Gossip.sum(1) != 1.)  ):
        print("Gossip Matrix not doubly stochastic! ")
    return(Graph_Gossip)
def weighted_lambda_normalized_adjacency_diff(net1, net2, weighted='weight'):
    adj1 = nx.adj_matrix(net1, weight=weighted).toarray()
    d1 = np.diag(1 / np.sqrt(adj1.sum(axis=1)))
    adj1 = d1.dot(adj1).dot(d1)
    adj1[np.isnan(adj1)] = 0
    adj2 = nx.adj_matrix(net2, weight=weighted).toarray()
    d2 = np.diag(1 / np.sqrt(adj2.sum(axis=1)))
    adj2 = d2.dot(adj2).dot(d2)
    adj2[np.isnan(adj2)] = 0

    net1_eigs = np.linalg.eigvalsh(adj1)
    net2_eigs = np.linalg.eigvalsh(adj2)
    return lambda_diff(net1_eigs, net2_eigs)
Пример #11
0
def magic_formula(gdict, wdict):

    g = next(iter(gdict.values()))
    a_shape = nx.adj_matrix(g).shape
    ones = np.ones(shape=a_shape)

    prob_no_contact = ones
    for name, g in gdict.items():
        a = nx.adj_matrix(g)
        a = wdict[name] * a
        prob_no_contact = np.multiply(prob_no_contact, (ones - a))

    # probability of contact (whatever layer)
    return 1 - prob_no_contact
Пример #12
0
 def test_adjacency_matrix(self):
     "Conversion to adjacency matrix"
     npt.assert_equal(nx.adj_matrix(self.G).todense(), self.A)
     npt.assert_equal(nx.adj_matrix(self.MG).todense(), self.A)
     npt.assert_equal(nx.adj_matrix(self.MG2).todense(), self.MG2A)
     npt.assert_equal(nx.adj_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2])
     npt.assert_equal(nx.adj_matrix(self.WG).todense(), self.WA)
     npt.assert_equal(nx.adj_matrix(self.WG, weight=None).todense(), self.A)
     npt.assert_equal(nx.adj_matrix(self.MG2, weight=None).todense(), self.MG2A)
     npt.assert_equal(nx.adj_matrix(self.WG, weight='other').todense(), 0.6 * self.WA)
     npt.assert_equal(nx.adj_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), self.no_edges_A)
Пример #13
0
 def test_adjacency_matrix(self):
     "Conversion to adjacency matrix"
     assert_equal(nx.adj_matrix(self.G).todense(), self.A)
     assert_equal(nx.adj_matrix(self.MG).todense(), self.A)
     assert_equal(nx.adj_matrix(self.MG2).todense(), self.MG2A)
     assert_equal(nx.adj_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2])
     assert_equal(nx.adj_matrix(self.WG).todense(), self.WA)
     assert_equal(nx.adj_matrix(self.WG, weight=None).todense(), self.A)
     assert_equal(nx.adj_matrix(self.MG2, weight=None).todense(), self.MG2A)
     assert_equal(nx.adj_matrix(self.WG, weight='other').todense(), 0.6 * self.WA)
     assert_equal(nx.adj_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), self.no_edges_A)
Пример #14
0
            def get_sample(metrictype):

                g = nx.erdos_renyi_graph(V, 0.2)

                ## node rank on the basis of egr/weighted spectrum
                if metrictype == 'egr':
                    ranks = self.get_egrnoderank(g)
                elif metrictype == 'weightedspectrum':
                    ranks = self.get_wghtspectnoderank(g)

                # ranks = np.array([2 if ind >= int(0.75*V) else 1 if (ind > int(0.25*V)) and (ind < int(0.75*V)) else 0 for ind in temp_ranks])
                ranks = (ranks - min(ranks)) / (max(ranks) - min(ranks))

                # ranks = np.array([1/(1+np.exp(-1*ind)) for ind in temp_ranks])

                # input node feature with degree

                degfeat = (np.sum(nx.adj_matrix(g).todense(), axis=1))

                x = np.reshape(np.arange(V), (V, 1))
                Idenfeat = enc.fit_transform(x).toarray()

                feat = np.concatenate((Idenfeat, degfeat), axis=1)

                # return temp_input, ranks, feat, g
                return ranks, feat, g
Пример #15
0
    def test_from_numpy_matrix_type(self):
        A = np.matrix([[1]])
        G = nx.from_numpy_matrix(A)
        assert_equal(type(G[0][0]['weight']), int)

        A = np.matrix([[1]]).astype(np.float)
        G = nx.from_numpy_matrix(A)
        assert_equal(type(G[0][0]['weight']), float)

        A = np.matrix([[1]]).astype(np.str)
        G = nx.from_numpy_matrix(A)
        assert_equal(type(G[0][0]['weight']), str)

        A = np.matrix([[1]]).astype(np.bool)
        G = nx.from_numpy_matrix(A)
        assert_equal(type(G[0][0]['weight']), bool)

        A = np.matrix([[1]]).astype(np.complex)
        G = nx.from_numpy_matrix(A)
        assert_equal(type(G[0][0]['weight']), complex)

        A = np.matrix([[1]]).astype(np.object)
        assert_raises(TypeError, nx.from_numpy_matrix, A)

        G = nx.cycle_graph(3)
        A = nx.adj_matrix(G).todense()
        H = nx.from_numpy_matrix(A)
        assert_true(all(type(m) == int and type(n) == int for m, n in H.edges()))
        H = nx.from_numpy_array(A)
        assert_true(all(type(m) == int and type(n) == int for m, n in H.edges()))
Пример #16
0
def main(argv):
    
    graph_fn="./data/7.txt"
    G = nx.Graph()  #let's create the graph first
    buildG(G, graph_fn)

    print G.nodes()
    print G.number_of_nodes()
    
    n = G.number_of_nodes()    #|V|
    A = nx.adj_matrix(G)    #adjacenct matrix

    m_ = 0.0    #the weighted version for number of edges
    for i in range(0,n):
        for j in range(0,n):
            m_ += A[i,j]
    m_ = m_/2.0
    print "m: %f" % m_

    #calculate the weighted degree for each node
    Orig_deg = {}
    Orig_deg = UpdateDeg(A, G.nodes())

    #run Newman alg
    runGirvanNewman(G, Orig_deg, m_)
Пример #17
0
def cluster(matrix):
    G = nx.Graph()
    for i in xrange(len(matrix)):
        for j in xrange(len(matrix)):
            if matrix[i][j] != 0:
                G.add_edge(i, j, weight=1/matrix[i][j])

    n = G.number_of_nodes()    #|V|
    A = nx.adj_matrix(G)
    m_ = 0.0    #the weighted version for number of edges
    for i in range(0,n):
        for j in range(0,n):
            m_ += A[i,j]
    m_ = m_/2.0

#calculate the weighted degree for each node
    Orig_deg = {}
    UpdateDeg(Orig_deg, A)

#let's find the best split of the graph
    BestQ = 0.0
    Q = 0.0
    Bestcomps = None
    while True:    
        CmtyGirvanNewmanStep(G)
        Q = _GirvanNewmanGetModularity(G, Orig_deg);
        if Q > BestQ:
            BestQ = Q
            Bestcomps = nx.connected_components(G)    #Best Split
        if G.number_of_edges() == 0:
            break
    return Bestcomps
Пример #18
0
def main(argv):
    
    # graph_fn="./data/7.txt"
    # G = nx.Graph()  #let's create the graph first
    # buildG(G, graph_fn)
    k=5
    G=nx.planted_partition_graph(k,10,0.8,0.02)
    # G.clear()
    bg(G)
    from test import da
    da(G)

    print G.nodes()
    print G.number_of_nodes()
    g=G.copy()
    n = G.number_of_nodes()    #|V|
    A = nx.adj_matrix(G)    #adjacenct matrix

    m_ = 0.0    #the weighted version for number of edges
    for i in range(0,n):
        for j in range(0,n):
            m_ += A[i,j]
    m_ = m_/2.0
    print "m: %f" % m_

    #calculate the weighted degree for each node
    Orig_deg = {}
    Orig_deg = UpdateDeg(A, G.nodes())

    #run Newman alg
    res=runGirvanNewman(G, Orig_deg, m_)
    print res
    shs(g,res)
Пример #19
0
def MCL_cluster(G,ex,r,tol,threshold):
    """
    Computes a clustering of graph G using the MCL algorithm 
    with power parameter ex and inflation parameter r
    The algorithm runs until the relative decrease in norm 
    is lower than tol or after 10,000 iterations
    Returns an array whose values are greater than threshold
    Leaves the graph G unchanged
    """

    M = np.array(nx.adj_matrix(G.copy()))
    M = inflate(M,1)

    norm_old = 0
    norm_new = np.linalg.norm(M)
    it = -1
    itermax = 10000
    while it < itermax:
        it += 1
        norm_old = norm_new
        M = M**ex
        M = inflate(M,r)
        norm_new = np.linalg.norm(M)
        if __name__ == '__main__':
            # debugging
            print "iteration %s" %it
            print "prop. decrease %s" %(abs(norm_old-norm_new)/norm_old)
        if abs(norm_old-norm_new)/norm_old < tol:
            print it
            break
    M[M < threshold] = 0
    return M
Пример #20
0
def simulate_affiliation_dpe():
    nrange = [400] #50*2**np.arange(3)
    drange = np.arange(1,5)
    
    embed = [Embed.dot_product_embed,
             Embed.dot_product_embed_unscaled,
             Embed.normalized_laplacian_embed,
             Embed.normalized_laplacian_embed_scaled]
    
    k = 2
    p = .15
    q = .1
    
    for n in nrange:
        G = rg.affiliation_model(n, k, p, q)
        for d in drange:
            print n*k,d,
            for e in embed:
                Embed.cluster_vertices_kmeans(G, e, d, k, 'kmeans')
                print num_diffs_w_perms_graph(G, 'block', 'kmeans'),
                
            print
    
    plot.matshow(nx.adj_matrix(G))
    plot.show()
Пример #21
0
    def __init__(self, G):
        '''
        Creates a TwoClubProblem for the given graph.

        Parameters
        ----------
        G : networkx.Graph
            The graph to find the 2-clubs of.
        '''

        n = nx.number_of_nodes(G)
        self.drivers, _ = find_drivers_id(G)
        Adj = nx.adj_matrix(G)

        # Create the individual adjacency matrices
        self.A = dict()
        for i in xrange(n):
            self.A[i] = Adj[i,:].transpose() * Adj[i,:]

        # Connectivity matrix
        C = Adj + Adj * Adj
        del Adj

        # First info vector
        info = [0 for i in xrange(n)]
        self.first_node = TwoClubNode(C, info, False)
Пример #22
0
def rand_spanning_tree(N, rand_weights=False):
    '''Creats a random minimal tree on N nodes

    Args:
        N (int): Number of nodes

    Returns:
        A NxN numpy array representing the adjacency matrix of the graph.

    '''

    # Create Random Graph
    A_rand = rand.rand(N, N)
    G_rand = nx.Graph()
    G_rand.add_nodes_from(xrange(N))
    for i in xrange(N):
        for j in xrange(i+1):
            G_rand.add_edge(i, j, weight=A_rand[i, j])
    # Find minimal spanning tree
    spanning_tree = nx.minimum_spanning_tree(G_rand)
    # Create adjacency matrix
    final_graph = nx.adj_matrix(spanning_tree).toarray()
    final_graph[final_graph > 0] = 1
    # Randomize weights if requested
    if rand_weights:
        R = np.tril(rand.rand(N, N))
        R = R + np.transpose(R)
        final_graph = final_graph * R
    return final_graph
Пример #23
0
def barabasi_albert(N, M, seed, verbose=True):
    '''Create random graph using Barabási-Albert preferential attachment model.

    A graph of N nodes is grown by attaching new nodes each with M edges that
    are preferentially attached to existing nodes with high degree.

    Args:
        N (int):Number of nodes

        M (int):Number of edges to attach from a new node to existing nodes

        seed (int) Seed for random number generator

    Returns:
        The NxN adjacency matrix of the network as a numpy array.

    '''

    A_nx = nx.barabasi_albert_graph(N, M, seed=seed)
    A = np.array(nx.adj_matrix(A_nx))

    if verbose:
        print('Barbasi-Albert Network Created: N = {N}, '
              'Mean Degree = {deg}'.format(N=N, deg=meanDegree(A)))

    return A
Пример #24
0
def main(argv):
    # if len(argv) < 2:
    #     sys.stderr.write("Usage: %s <input graph>\n" % (argv[0],))
    #     return 1
    # graph_fn = argv[1]
    graph_fn = "/Users/yaya/Desktop/CNdata1.txt"
    G = nx.Graph()  #let's create the graph first
    buildG(G, graph_fn, ',')
    
    if _DEBUG_:
        print ('G nodes:', G.nodes())
        print ('G no of nodes:', G.number_of_nodes())
    
    n = G.number_of_nodes()    #|V|
    A = nx.adj_matrix(G)    #adjacenct matrix

    m_ = 0.0    #the weighted version for number of edges
    for i in range(0,n):
        for j in range(0,n):
            m_ += A[i,j]
    m_ = m_/2.0
    if _DEBUG_:
        print ("m: %f" % m_)

    #calculate the weighted degree for each node
    Orig_deg = {}
    Orig_deg = UpdateDeg(A, G.nodes())

    #run Newman alg
    runGirvanNewman(G, Orig_deg, m_)
Пример #25
0
def modularity_matrix(G,edec=None):
    '''
    Computes the 'master' modularity matrix, without subgraph corrections.  The
    modularity matrix is defined as

                    Q(i,j) = A(i,j) - (k_i*k_j)/sum(k_i)

    where A is the adjacency matrix (weighted) of G and k_i are the (weighted)
    node degrees.

    INPUT
    -------

    G: networkx graph, required
        input graph for which modularity matrix is desired

    edec : string, optional
        edge decorator (name for weights) in weighted graphs

    OUTPUT:

    Q : numpy array
        Q will be of size len(G.nodes()) X len(G.nodes())
    '''
    # adjacency matrix piece (ordered according to G.nodes())
    Q = nx.adj_matrix(G,weight=edec)
    # degree-product portion (same order as adj_matrix!)
    nodes = G.nodes()
    ki = G.degree(weight=edec)
    kvec = np.zeros((len(nodes),1))
    for i in xrange(0,len(nodes)):
        kvec[i] = ki[nodes[i]]
    return Q - np.dot(kvec,kvec.T)/sum(ki.values())
Пример #26
0
    def _generate_dependency_list(self):
        """ Generates a dependency list for a list of graphs. Adds the
        following attributes to the pipeline:

        New attributes:
        ---------------

        procs: list (N) of underlying interface elements to be
        processed
        proc_done: a boolean vector (N) signifying whether a process
        has been executed
        proc_pending: a boolean vector (N) signifying whether a
        process is currently running.
        Note: A process is finished only when both proc_done==True and
        proc_pending==False
        depidx: a boolean matrix (NxN) storing the dependency
        structure accross processes. Process dependencies are derived
        from each column.
        """
        if not self._execgraph:
            raise Exception('Execution graph has not been generated')
        self.procs = self._execgraph.nodes()
        self.depidx = nx.adj_matrix(self._execgraph).__array__()
        self.proc_done    = np.zeros(len(self.procs), dtype=bool)
        self.proc_pending = np.zeros(len(self.procs), dtype=bool)
Пример #27
0
    def orient_undirected_graph(self, data, graph, **kwargs):
        """Run PC on an undirected graph.

        Args:
            data (pandas.DataFrame): DataFrame containing the data
            graph (networkx.Graph): Skeleton of the graph to orient

        Returns:
            networkx.DiGraph: Solution given by PC on the given skeleton.
        """
        # Building setup w/ arguments.
        self.arguments['{CITEST}'] = self.dir_CI_test[self.CI_test]
        self.arguments['{METHOD_INDEP}'] = self.dir_method_indep[self.CI_test]
        self.arguments['{DIRECTED}'] = 'TRUE'
        self.arguments['{ALPHA}'] = str(self.alpha)
        self.arguments['{NJOBS}'] = str(self.njobs)
        self.arguments['{VERBOSE}'] = str(self.verbose).upper()

        fe = DataFrame(nx.adj_matrix(graph, weight=None).todense())
        fg = DataFrame(1 - fe.values)

        results = self._run_pc(data,
                               fixedEdges=fe,
                               fixedGaps=fg,
                               verbose=self.verbose)

        return nx.relabel_nodes(nx.DiGraph(results),
                                {idx: i
                                 for idx, i in enumerate(data.columns)})
Пример #28
0
def generate_crescent_edges_graphs(n_graphs, n_nodes, p_edges, min_p_edges=None):
    """Creates a set of n graphs with a fixed n nodes and choice of
    possible edges with crescent probability p.

    """

    adj_matrix_dict = defaultdict()

    if min_p_edges:
        min_p_edges = math.log1p(n_nodes) / n_nodes

    filename = 'edges_' + str(n_graphs) + 'G_' + str(n_nodes) + 'N'
    filename += '_from_' + str(min_p_edges) + '_to_' + str(p_edges) + 'P'

    j = (p_edges - min_p_edges) / n_graphs  # p increment

    p_edges = min_p_edges

    for i in range(0, n_graphs):
    
        G = nx.erdos_renyi_graph(n_nodes, p_edges)
    
        if nx.is_connected(G):
            adj_matrix_dict[p_edges] = nx.adj_matrix(G)

        p_edges += j

    write_to_file(adj_matrix_dict, filename + '.dat', BENCHMARKS_FILE_DIR)

    return filename
Пример #29
0
def test_compute_ncut(epsilon=1e-8):
    weights_small = [np.array([[12, -1.3],
                               [0, 7.4]]),
                     np.array([[-8.9, 0.15],
                               [0.23, -5.7]])]
    adj_mat_small = weights_to_graph(weights_small).toarray()
    clustering_small = [0, 1, 0, 1, 0, 1]

    assert isclose(compute_ncut(adj_mat_small, clustering_small, epsilon),
                   0.09895, abs_tol=1e-3)


    weights_big = [np.array([[5.4, 0.03, 0, 0],
                             [0, 0, -0.4, 5.5],
                             [-12, 0, 4.8, -0.07],
                             [0, 6.3, 0.001, 7]]),
                   np.array([[-13.3, -0.4, 6, 0.1],
                             [0.7, -8, 0, -5.8],
                             [7.4, 0.3, -15, 0],
                             [0, 0, 0, -12.3]])]
    adj_mat_big = weights_to_graph(weights_big).toarray()
    clustering_big = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]

    assert isclose(compute_ncut(adj_mat_big, clustering_big, epsilon),
                   0.0372, abs_tol=1e-3)


    # test on a random graph, and use networkx implementation
    G = nx.generators.random_graphs.fast_gnp_random_graph(20, 0.8)
    adj_mat = nx.adj_matrix(G)

    assert isclose(nx.algorithms.cuts.normalized_cut_size(G,
                                                          list(G.nodes)[::2],
                                                          list(G.nodes)[1::2]),
             compute_ncut(adj_mat, [0, 1] * 10, 0))
Пример #30
0
def MAWe(G):
    """Eigenvalue approximation of maximum average degree. This is conjectured
    to be an upper bound on MAW.
    """
    eigenvalues, eigenvectors = scipy.linalg.eig(nx.adj_matrix(G) )
    MAWe = float(max(eigenvalues))
    return MAWe
Пример #31
0
 def get_neg_adj(self):
     pos_G = nx.Graph()
     pos_G.add_nodes_from(self.G.g.nodes())
     pos_G.add_edges_from(self.G.poslist)
     adj_pos = nx.adj_matrix(pos_G)
     #print(adj_pos.todense())
     return(adj_pos)
Пример #32
0
    def adjacency_matrix(self, uname_order = None, rid_order = None):
        """
        Get adjacency matrix between Neurons.

        # Arguments
            uname_order (list):
                A list of the uname of neurons to order the rows and columns of the adjacency matrix.
                If None, use rid_order. If rid_order is None, will sort uname for order.
            rid_order (list):
                A list of the rids of neurons to order the rows and columns of the adjacency matrix.
                If None, use uname_order. if uname_order is None, will sort uname for order.
        # Returns
            M (numpy.ndarray):
                A graph representing the connectivity of the neurons.
            uname_oder (list):
                A list of unames by which the rows and columns of M are ordered.
        """
        if uname_order is None and rid_order is None:
            order = sorted([(self.nodes[n]['uname'], n) for n in self.nodes()])
            uname_order = [uname for uname, _ in order]
            rid_order = [rid for _, rid in order]
        elif uname_order is None:
            # rid_order
            uname_order = [self.nodes[n]['uname'] for n in rid_order]
        else:
            # uname_order
            order_dict = {self.nodes[n]['uname']: n for n in self.nodes()}
            rid_order = [order_dict[uname] for uname in uname_order]
        M = nx.adj_matrix(self, nodelist = rid_order).todense()
        return M, uname_order
Пример #33
0
def get_T(G):
    ''' Return diffusion operator of a graph.

    The diffusion operator is defined as T = I - L, where L is the normalized
    Laplacian.

    Parameters
    ----------
    G : NetworkX graph
    
    Returns
    -------
    T : NumPy array
    Ln : NumPy array
      Normalized Laplacian of G.

    Notes
    -----
    Computing the normalized laplacian by hand. It seems there are some
    inconsistencies using nx.normalized_laplacian when G has selfloops.
    '''
    A = nx.adj_matrix(G, nodelist=sorted(G.nodes()))
    D = np.array(np.sum(A,1)).flatten()

    Disqrt = np.array(1 / np.sqrt(D))
    Disqrt = np.diag(Disqrt)
    L = np.diag(D) - A
    Ln = np.dot(np.dot(Disqrt, L), Disqrt)
    T =  np.eye(len(G)) - Ln
    T = (T + T.T) / 2 # Iron out numerical wrinkles
    
    return T, L
def gn(g):
    init_n = g.number_of_edges()
    print("总共的边数(相当于两两比赛的总次数):", init_n)    # 统计所有的边数  总共613  也就是613次比赛

    if init_n == 0:
        return None

    # 简单的一个预处理
    m = nx.adj_matrix(g)   # 按第一列从小到大 整理我们的数据
    m = m.sum() / 2  # 总的比赛数

    init_deg = get_deg(g)
    # print(init_deg)   # {"队0": 打的次数, "队1": 打的次数, ...}

    i = 1
    while g.number_of_edges() > 420:
        removing_based_on_betweeness(g)
        # 迭代5词计算一下模块度
        if i % 5 == 0:
            print("迭代次数:{} 模块度的值:{} 边数:{}".format(i, get_modularity(g, init_deg, m), g.number_of_edges()))
        i += 1
    print("模块度最大: {}".format(get_modularity(g, init_deg, m)))

    # 社团中止时的图
    nx.draw_networkx(g)
    plt.show()

    return nx.connected_components(g)
Пример #35
0
    def test_from_numpy_matrix_type(self):
        A = np.matrix([[1]])
        G = nx.from_numpy_matrix(A)
        assert type(G[0][0]['weight']) == int

        A = np.matrix([[1]]).astype(np.float)
        G = nx.from_numpy_matrix(A)
        assert type(G[0][0]['weight']) == float

        A = np.matrix([[1]]).astype(np.str)
        G = nx.from_numpy_matrix(A)
        assert type(G[0][0]['weight']) == str

        A = np.matrix([[1]]).astype(np.bool)
        G = nx.from_numpy_matrix(A)
        assert type(G[0][0]['weight']) == bool

        A = np.matrix([[1]]).astype(np.complex)
        G = nx.from_numpy_matrix(A)
        assert type(G[0][0]['weight']) == complex

        A = np.matrix([[1]]).astype(np.object)
        pytest.raises(TypeError, nx.from_numpy_matrix, A)

        G = nx.cycle_graph(3)
        A = nx.adj_matrix(G).todense()
        H = nx.from_numpy_matrix(A)
        assert all(type(m) == int and type(n) == int for m, n in H.edges())
        H = nx.from_numpy_array(A)
        assert all(type(m) == int and type(n) == int for m, n in H.edges())
Пример #36
0
    def convToMatrix(self, gList, vecMode=True):
        """
        gList: list of normal graphs
        vecMode: if false, unvectorized vals will be returned
        return: (list of adjacency matrixes, list of node vectors)
        
        """

        graphPropList = []
        graphAdjList = []

        #vectorize nodes for each node
        for g in tqdm(gList):
            for num, i in enumerate(g.nodes):

                if vecMode == False:
                    newVector = self.getVector(g.nodes[i]["label"])
                else:
                    newVector = g.nodes[i]["label"]

                if num == 0:
                    nodePropList = newVector
                else:
                    nodePropList = np.vstack((nodePropList, newVector))

            graphPropList.append(np.array(nodePropList, dtype="float32"))

            #adjacency matrices
            graphAdjList.append(
                np.array(nx.adj_matrix(g).todense(), dtype="float32"))

        return graphPropList, graphAdjList
Пример #37
0
def helper_genie3(X, theta_true, tf_names=[], BEELINE=False):  #_string
    print('Running GENIE3 method', X.shape)
    theta_true = theta_true.real
    ex_matrix = pd.DataFrame(X)
    if args.USE_TF_NAMES == 'yes' and len(tf_names) != 0:
        tf_names = ['G' + str(n) for n in tf_names]
    else:
        tf_names = None

    gene_names = ['G' + str(c) for c in ex_matrix.columns]
    ex_matrix.columns = gene_names
    network = genie3(expression_data=ex_matrix,
                     gene_names=gene_names,
                     tf_names=tf_names)  #, verbose=True)
    pred_edges = np.array(network[['TF', 'target', 'importance']])
    G_pred = nx.Graph()
    #    G_pred.add_nodes_from(['G'+str(n) for n in range(args.D)])
    G_pred.add_nodes_from(['G' + str(n) for n in range(len(gene_names))])
    G_pred.add_weighted_edges_from(pred_edges)
    #    pred_theta = nx.adj_matrix(G_pred).todense() + np.eye(args.D)
    pred_theta = nx.adj_matrix(G_pred).todense() + np.eye(len(gene_names))
    recovery_metrics = report_metrics(np.array(theta_true),
                                      np.array(pred_theta))
    print(
        'GENIE3: FDR, TPR, FPR, SHD, nnz_true, nnz_pred, precision, recall, Fb, aupr, auc'
    )
    print('GENIE3: Recovery of true theta: ', *np.around(recovery_metrics, 3))

    res = list(recovery_metrics)
    if BEELINE:
        res = [list(recovery_metrics), pred_theta]
    return res
Пример #38
0
def gen_adj(snippets,
            pre='snippet',
            suf='',
            tt_ratio=0.8,
            mode=None,
            max_len=64,
            w_mode='w'):
    with open(pre + '_adj' + suf + '.txt', w_mode, newline='') as f:
        wr = csv.writer(f)
        G_u = G.to_undirected()
        idx = 0
        pivot = int(len(snippets) * tt_ratio)
        for ts in snippets:
            if mode == 'val':
                if idx < pivot:
                    idx += 1
                    continue
            else:
                if idx > pivot:
                    break
                else:
                    idx += 1
            if mode == 'fc':
                final = np.ones((max_len, max_len), dtype=int)
            else:
                adj = nx.adj_matrix(G_u.subgraph(ts)).todense()
                final = np.zeros((max_len, max_len), dtype=int)
                final[:adj.shape[0], :adj.shape[1]] = adj
                final += np.eye(max_len, dtype=int)

            for row in final.tolist():
                wr.writerow(row)
            wr.writerow([])
            wr.writerow([])
Пример #39
0
def get_BASBM(sizes, p, m=2):
    """A stochastic block model where each block is a Barabasi Albert graph.

    Args:
        `sizes` (list): a list of ints describing the size of each block. Each
            size must be larger than m+1.
        `p`: (float or array): if an array then p[i][j] is the probability of connecting
            a node from block i to block j. If a float then its the probability of connecting
            nodes from different blocks.
        `m`: (int): the barabasi albert hyper-parameter.

    Returns:
        A networkx graph.
    """
    if isinstance(p, float) or isinstance(p, int):
        p = p * np.ones((len(sizes), len(sizes)))
    num_blocks = len(sizes)
    blocks = []
    for i in range(num_blocks):
        block_row = []
        for j in range(num_blocks):
            if i == j:
                block_row.append(
                    nx.adj_matrix(nx.barabasi_albert_graph(sizes[i],
                                                           m)).todense())
            else:
                block_row.append(
                    np.random.binomial(1, p[i][j], (sizes[i], sizes[j])))
        blocks.append(block_row)
    adj_matrix = np.block(blocks)
    return nx.from_numpy_array(adj_matrix)
Пример #40
0
def edgefeat(g, norm=False, fil='ricci'):
    """
    wrapper for edge_probability and ricciCurvature computation
    :param g: graph
    :param fil:  edge_p/ricci/jaccard
    :param whether normalize edge values or not
    :return: gp, a dense numpy array of shape (n_node, n_node)
    """
    g = nx.convert_node_labels_to_integers(g)
    assert nx.is_connected(g)
    adj_m = nx.adj_matrix(g).todense()  # dense matrix
    gp = np.zeros((len(g), len(g)))
    try:
        if fil == 'edge_p':
            gp = np.array(smoother(adj_m, h=0.3))
            gp = np.multiply(adj_m, gp)
        elif fil == 'ricci':
            g = ricciCurvature(g, alpha=0.5, weight='weight')
            ricci_dict = nx.get_edge_attributes(g, 'ricciCurvature')
            for u, v in ricci_dict.keys():
                gp[u][v] = ricci_dict[(u, v)]
            gp += gp.T
        elif fil == 'jaccard':
            jac_list = nx.jaccard_coefficient(g, g.edges(
            ))  # important since jaccard can also be defined on non edge
            for u, v, jac in jac_list:
                gp[u][v] = jac
            gp += gp.T
    except AssertionError:
        print('Have not implemented fil %s. Treat as all zeros' % fil)
        gp = np.zeros((len(g), len(g)))
    assert (gp == gp.T).all()
    if norm: gp = gp / float(max(abs(gp)))
    return gp
def construct_train_graph(dataset):
    train_pos, train_neg, test_pos, test_neg = get_train_test(dataset)
    graph, train_neg_txt, test_pos_txt, test_neg_txt = [],[],[],[]
    for i in range(len(train_pos[0])):
        graph.append((train_pos[0][i],train_pos[1][i]))
        train_neg_txt.append((train_neg[0][i],train_neg[1][i]))
        
    with open(dataset[:-4]+'train_pos.txt','a') as file:
        file.write("{}\n".format(graph))
    file.close()
    with open(dataset[:-4]+'train_neg.txt','a') as file:
        file.write("{}\n".format(train_neg_txt))
    file.close()
    
    for i in range(len(test_pos[0])):
        test_pos_txt.append((test_pos[0][i],test_pos[1][i]))
        test_neg_txt.append((test_neg[0][i],test_neg[1][i]))
    
    with open(dataset[:-4]+'test_pos.txt','a') as file:
        file.write("{}\n".format(test_pos_txt))
    file.close()
    with open(dataset[:-4]+'test_neg.txt','a') as file:
        file.write("{}\n".format(test_neg_txt))
    file.close()
    
    G = nx.Graph(graph)
    adj_mat = nx.adj_matrix(G)
    filename = 'new_'+dataset
    scio.savemat(filename, {'network':adj_mat})
    return
Пример #42
0
def get_matrix_jc(G):
    ## get all pairwise jaccard indexes
    nodes = list(G.nodes())
    adj = nx.adj_matrix(G)
    shared_nei = adj * adj
    d = shared_nei.diagonal()

    ## matrix i,j sum degree i,j
    ## jc = overlap / (i) + (j) - overlap
    ## the steps bellow get the denominator
    a = np.tile(d,(d.shape[0],1))
    a = a.T
    b = np.tile(d,(d.shape[0],1))
    sumk = a + b
    t = shared_nei.todense()
    denom = sumk - shared_nei

    ## jaccard
    jc = t/denom

    df = pd.DataFrame(jc)
    df.columns = nodes
    df.index = nodes

    return(df)
Пример #43
0
	def modularity(self):
		"""
		Compute the modularity. 

		Returns:
			Numerical value of the modularity of the graph. 
		"""
		g = self.gr
		A = nx.adj_matrix(g)
		degDict = nx.degree_centrality(g)

		adjDict = {}
		n = A.shape[0]
		B = A.sum(axis=1)
		for i in range(n):
			adjDict[g.nodes()[i]] = B[i,0]

		m = len(g.edges())

		connComponents = nx.connected_components(g)

		mod = 0

		for c in connComponents:
			edgesWithinCommunity = 0
			randomEdges = 0
			for u in c:
				edgesWithinCommunity += adjDict[u]
				randomEdges += degDict[u]
			mod += (float(edgesWithinCommunity) - float(randomEdges * randomEdges)/float(2 * m))	
		mod = mod/float(2 * m)
			
		return mod	
Пример #44
0
def main(argv):
    if len(argv) < 2:
        sys.stderr.write("Usage: %s <input graph>\n" % (argv[0],))
        return 1
    graph_fn = argv[1]
    G = nx.Graph()  #let's create the graph first
    buildG(G, graph_fn, ',')

    print G.nodes()
    print G.number_of_nodes()
    
    n = G.number_of_nodes()    #|V|
    A = nx.adj_matrix(G)    #adjacenct matrix

    m_ = 0.0    #the weighted version for number of edges
    for i in range(0,n):
        for j in range(0,n):
            m_ += A[i,j]
    m_ = m_/2.0
    print "m: %f" % m_

    #calculate the weighted degree for each node
    Orig_deg = {}
    Orig_deg = UpdateDeg(A, G.nodes())

    #run Newman alg
    runGirvanNewman(G, Orig_deg, m_)
Пример #45
0
def generate_graph(n_vertex, p):
    G = nx.erdos_renyi_graph(n_vertex, p)
    matrix = nx.adj_matrix(G)
    matrix = matrix.toarray()
    vertex = len(matrix)
    edges = 0
    for line in matrix:
        for val in line:
            if val == 1:
                edges += 1
    edges = edges // 2
    with open("test_" + str(n_vertex) + "_" + str(int(p * 10)) + ".txt",
              "w") as f:
        i = 0
        j = 0
        extra_edges = set({})
        print(str(vertex) + " " + str(edges))
        f.write(str(vertex) + " " + str(edges))
        for line in matrix:
            j = 0
            for column in line:
                if column == 1:
                    pair = (i, j)
                    if not pair in extra_edges:
                        f.write("\n")
                        f.write(str(i) + " " + str(j))
                        extra_edges.add((i, j))
                        extra_edges.add((j, i))
                j += 1
            i += 1
Пример #46
0
def generate_crescent_edges_graphs(n_graphs,
                                   n_nodes,
                                   p_edges,
                                   min_p_edges=None):
    """Creates a set of n graphs with a fixed n nodes and choice of
    possible edges with crescent probability p.

    """

    adj_matrix_dict = defaultdict()

    if min_p_edges:
        min_p_edges = math.log1p(n_nodes) / n_nodes

    filename = 'edges_' + str(n_graphs) + 'G_' + str(n_nodes) + 'N'
    filename += '_from_' + str(min_p_edges) + '_to_' + str(p_edges) + 'P'

    j = (p_edges - min_p_edges) / n_graphs  # p increment

    p_edges = min_p_edges

    for i in range(0, n_graphs):

        G = nx.erdos_renyi_graph(n_nodes, p_edges)

        if nx.is_connected(G):
            adj_matrix_dict[p_edges] = nx.adj_matrix(G)

        p_edges += j

    write_to_file(adj_matrix_dict, filename + '.dat', BENCHMARKS_FILE_DIR)

    return filename
Пример #47
0
    def adjacency_matrix(self) -> np.ndarray:
        """Converts the estimated structure ``_complete_graph`` to a boolean adjacency matrix representation.

        :return: The adjacency matrix of the graph ``_complete_graph``
        :rtype: numpy.ndArray
        """
        return nx.adj_matrix(self._complete_graph, self._nodes).toarray().astype(bool)
Пример #48
0
def prune(net):
    import networkx as nx

    # removes the unconnected components
    G = create_nx_from_network(net)
    connected_component = G.subgraph(nx.connected_components(G)[0])
    return UndirectedNetwork(connected_component.number_of_nodes(), nx.adj_matrix(connected_component))
Пример #49
0
    def __init__(self,Gs,max_depth=10,max_node=1000,tree_num=8,normalize=True):
        tree_info = get_graphs_tree_data(Gs,max_depth,K=tree_num)
        self.adj_all = []
        self.feature_all = []
        self.tree_info = tree_info
        self.label_all = []
        self.max_node = max_node

        self.feature_dim = len(Gs[0].node[0]['feat'])
        for g in Gs:
            adj = nx.adj_matrix(g).todense()
            adj = np.array(adj)
            if normalize:
                sqrt_deg = np.diag(1.0 / np.sqrt(np.sum(adj, axis=0, dtype=float).squeeze()))
                adj = np.matmul(np.matmul(sqrt_deg, adj), sqrt_deg)

            f = np.zeros([self.max_node, self.feature_dim], dtype=np.float)
            for i, u in enumerate(g.nodes()):
                if i < self.max_node:
                    f[i, :] = np.array(g.node[u]['feat'])
                else:
                    break
            self.feature_all.append(f)
            self.adj_all.append(adj)
            self.label_all.append(g.graph['label'])
Пример #50
0
            def get_sample():
                g = nx.generators.random_graphs.powerlaw_cluster_graph(
                    V, 1, self.triprob)
                temp_input = nx.adj_matrix(g).toarray()
                # temp_input = expm(temp_input)
                # temp_input = nx.laplacian_matrix(g).toarray()
                # temp_input = np.linalg.pinv(temp_input)
                # temp_input = nx.normalized_laplacian_matrix(g).toarray()

                temp_label = list(nx.betweenness_centrality(g).values())

                # input node feature
                feat = np.ones(shape=(V, 1))
                feat[:, 0] = np.reshape(
                    np.sum(nx.adj_matrix(g).todense(), axis=1), (V, ))
                return temp_input, np.array(temp_label), feat
Пример #51
0
def eigenvector_centrality_numpy(G, weight='weight'):
    """Compute the eigenvector centrality for the graph G.

    Parameters
    ----------
    G : graph
      A networkx graph

    weight : None or string, optional
      If None, all edge weights are considered equal.
      Otherwise holds the name of the edge attribute used as weight.


    Returns
    -------
    nodes : dictionary
       Dictionary of nodes with eigenvector centrality as the value.

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> centrality = nx.eigenvector_centrality_numpy(G)
    >>> print(['%s %0.2f'%(node,centrality[node]) for node in centrality])
    ['0 0.37', '1 0.60', '2 0.60', '3 0.37']

    Notes
    ------
    This algorithm uses the NumPy eigenvalue solver.

    For directed graphs this is "right" eigevector centrality.  For
    "left" eigenvector centrality, first reverse the graph with
    G.reverse().

    See Also
    --------
    eigenvector_centrality
    pagerank
    hits
    """
    try:
        import numpy as np
    except ImportError:
        raise ImportError('Requires NumPy: http://scipy.org/')

    if type(G) == nx.MultiGraph or type(G) == nx.MultiDiGraph:
        raise nx.NetworkXException('Not defined for multigraphs.')

    if len(G) == 0:
        raise nx.NetworkXException('Empty graph.')

    A = nx.adj_matrix(G, nodelist=G.nodes(), weight='weight')
    eigenvalues,eigenvectors = np.linalg.eig(A)
    # eigenvalue indices in reverse sorted order
    ind = eigenvalues.argsort()[::-1]
    # eigenvector of largest eigenvalue at ind[0], normalized
    largest = np.array(eigenvectors[:,ind[0]]).flatten().real
    norm = np.sign(largest.sum())*np.linalg.norm(largest)
    centrality = dict(zip(G,map(float,largest/norm)))
    return centrality
Пример #52
0
def gdraw0(graphs, plotname = 'default_name', measure = 'cosine'):
    pos = nx.graphviz_layout(graphs['kg'])


    adjs = [ array(nx.adj_matrix(g)) for g in graphs.values() ]
    nrms = []
    for a in adjs:
            n = sqrt(sum(a**2))
            nrms.append(a / n)
    
    kgelt = graphs.keys().index('kg')
    if measure == 'cosine':
        sims = array([round(nfu.cosine_adj(a1,nrms[kgelt]),8) for a1 in nrms])
    else:
        raise Exception()

    kg = graphs['kg']
    srto = argsort(graphs.keys()) 
    #XVALs give ranks of each key index.
    xvals = argsort(srto)


    cols = map(lambda x: 
               ('flt' in x and x.count('thr') > 1) and 'orange' or
               ('flt' in x) and 'red' or
               ('thr' in x) and 'yellow' or
               ('fg' in x) and 'green' or 
               ('su' in x) and 'blue' or 
               'black', graphs.keys())

    yvals = sims

    f = plt.gcf()
    f = myplots.fignum(3, (.25 * len(sims),10))
    f.clear()
    ax = f.add_subplot(111)
    myplots.padded_limits(ax,xvals,yvals + [0.], margin = [.02,.02])
    ax.scatter(xvals,yvals,100, color = cols)
    ax.set_ylabel('red fly similarity ({0})'.format(measure))
    ax.set_xlabel('networks')
    ax.set_xticklabels([])
    ax.set_xticks([])
    mark_ys = [0, median(sims), mean(sims), sort(sims)[::-1][1],1]
    ax.hlines(mark_ys, *ax.get_xlim(), linestyle = ':',alpha = .2)
    

    f.savefig(cfg.dataPath('figs/meAWG/filter_{0}_meth_{1}_nolabels.pdf'.\
                               format(plotname,measure)))


    ax.set_xticks(range(len(srto)))
    ax.annotate('\n'.join(' '.join(z) for z in zip(graphs.keys(),cols)),
                [0,1],xycoords = 'axes fraction', va = 'top')
    
    ax.set_xticklabels([graphs.keys()[i] for i in srto], 
                       rotation = 45, size = 'xx-small',ha = 'right')

    f.savefig(cfg.dataPath('figs/meAWG/filter_{0}_meth_{1}_labels.pdf'.\
                               format(plotname,measure)))
Пример #53
0
def cn(graph):
    """
    Common neighbours similarity index.
    :param graph: a graph to apply this similarity index on.
    :return: similarity matrix.
    """
    adjacency_matrix = nx.adj_matrix(graph)
    return adjacency_matrix**2
Пример #54
0
def weighted_degree(G):
    """Return an array of degrees that takes weights into account.

    For unweighted graphs, this is the same as the normal degree() method
    (though we return an array instead of a list).
    """
    amat = nx.adj_matrix(G).A  # get a normal array out of it
    return abs(amat).sum(0)  # weights are sums across rows
def total_weight(G):
    adj = nx.adj_matrix(G)
    N = G.number_of_nodes()
    total = 0.0
    for i in xrange(0, N):
        for j in xrange(i, N):
            total += adj[i, j]
    return total
 def test_adjacency_matrix(self):
     "Conversion to adjacency matrix"
     assert_equal(nx.adj_matrix(self.G),self.A)
     assert_equal(nx.adj_matrix(self.MG),self.A)
     assert_equal(nx.adj_matrix(self.MG2),self.MG2A)
     assert_equal(nx.adj_matrix(self.G,nodelist=[0,1]),self.A[:2,:2])
     assert_equal(nx.adj_matrix(self.WG),self.WA)
     assert_equal(nx.adj_matrix(self.WG,weight=None),self.A)
     assert_equal(nx.adj_matrix(self.MG2,weight=None),self.MG2A)
     assert_equal(nx.adj_matrix(self.WG,weight='other'),0.6*self.WA)
Пример #57
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))
Пример #58
0
def SpectralEmbedding(G, k = 5):
    '''
    Takes a input Graph, and embeds it into k-dimensional
    euclidean space using a top-k-eigenvector embedding.
    '''

    # creates row normalized weight matrix
    M = normalize(nx.adj_matrix(G.copy()), norm='l1', axis=1)
    _, v = eigsh(M, k = k, which = 'LM')
    return v
Пример #59
0
def icml_mostafa(G,transpose=False):
    A = nx.adj_matrix(G).todense()
    if transpose==True:
        A = A.T;

    m , n = A.shape; 
    deg = nx.degree(G);

    done = 1;
    I = np.zeros_like(range(m));
    O = np.zeros_like(range(n))

    l = 1;

    T = [];

    while done == 1:
        #find the candidate pair
        u = -1;
        v = -1;
        min_d_uv = m+n+1;

        for i in range(m):
            if I[i] == 0:
                for j in range(n):
                    if (A[i,j] == 1) and (O[j] == 0) and ((deg[i] + deg[j]) < min_d_uv):
                        min_d_uv = deg[i] + deg[j];
                        u = i;
                        v = j;
                        I[u] = 1;
                        O[v] = 1;
                    
        if (u == -1) and (v == -1): 
            done = 0;
        else:
            # set the rows and columns to one if used
            for j in range(n):
                if A[u,j] == 1:
                    A[u,j] = 0;
                    deg[j] -= 1;
                    O[j] = 1;
                    I[j] = 1;
            
            for i in range(m):
                if A[i,v] == 1:
                    A[i,v] = 0;
                    deg[i] -= 1;
                    I[i] = 1;
                    O[i] = 1;
            I[u] = 1;
            O[v] = 1;
            
            T.append((u,v));

    return T;
def add_edge_wts(G):
    # For each node, it adds the weights of all the edges incident on the node
    node_wts = {}
    nodes = G.nodes()
    # Find the adjacency matrix
    adj = nx.adj_matrix(G)
    summation = adj.sum(axis=1)
    N = G.number_of_nodes()
    for x in xrange(0, N):
        node_wts[nodes[x]] = summation[x, 0]
    return node_wts