def base_experiment_compare(graph, P, W):
    print('#' * 10)
    for i in range(10):
        t = ComputeCoverTime(graph)
        print('Number of Options', i)
        print('CoverTime     ', t)
        lb = nx.algebraic_connectivity(nx.to_networkx_graph(graph))
        print('lambda        ', lb)
        print()

        avg_dist = average_shortest_distance(graph, W)

        graph_alg, options_alg = AverageShortestOptions(graph, P, 1)
        lb_alg = nx.algebraic_connectivity(nx.to_networkx_graph(graph_alg))
        avg_dist_alg = average_shortest_distance(graph_alg, W)

        graph_brute, options_brute = BruteOptions(graph, P, 1)
        lb_brute = nx.algebraic_connectivity(nx.to_networkx_graph(graph_brute))
        avg_dist_brute = average_shortest_distance(graph_brute, W)

        print('avg_shortest_dist: ', "alg:", avg_dist_alg, "brute:",
              avg_dist_brute)
        #print('lambda: ',"alg:", lb_alg,"brute:",lb_brute)
        print(options_alg, options_brute)

        graph = graph_alg
def compare_to_brute_exp(graph, P, W, num_options, show_graphs=False):
    avg_dist = average_shortest_distance(graph, W)
    ASPDM_list = [avg_dist]
    Brute_list = [avg_dist]

    graph_alg_orig = graph.copy()
    graph_brute = graph.copy()

    for i in range(num_options):
        print(i, "/", num_options, "\t\t", end="\r")
        graph_alg, options_alg = AverageShortestOptions(graph_alg_orig, P, i)
        avg_dist_alg = average_shortest_distance(graph_alg, W)

        graph_brute, options_brute = BruteOptions(graph_brute, P, 1)
        avg_dist_brute = average_shortest_distance(graph_brute, W)

        if show_graphs:
            fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
            nx.draw(nx.to_networkx_graph(graph), ax=ax1)
            nx.draw(nx.to_networkx_graph(graph_alg), ax=ax2)
            nx.draw(nx.to_networkx_graph(graph_brute), ax=ax3)
            plt.show()

        if ASPDM_list[-1] == avg_dist_alg and Brute_list[-1] == avg_dist_brute:
            ASPDM_list.append(avg_dist_alg)
            Brute_list.append(avg_dist_brute)
            break
        ASPDM_list.append(avg_dist_alg)
        Brute_list.append(avg_dist_brute)

    return ASPDM_list, Brute_list, graph_alg, graph_brute
Exemplo n.º 3
0
 def plot_tree(self, weights=None, title=None):
     import networkx as nx
     import matplotlib as mpl
     import matplotlib.pyplot as plt
     plt.figure()
     if weights is None:
         G = nx.to_networkx_graph(self.ssm)
         nx.draw_networkx(G)
     else:
         nnodes = self.ssm.shape[0]
         w_a = np.zeros(nnodes)
         for idx in weights['RECIDX'].unique():
             ct = weights[weights.RECIDX == idx].shape[0]
             try:
                 w_a[self.idx_d[idx]] = float(ct) / float(nnodes)
             except Exception as e:
                 import pdb
                 pdb.Pdb().set_trace()
         G = nx.to_networkx_graph(self.ssm)
         nx.draw_networkx(G,
                          node_color=w_a,
                          cmap=plt.get_cmap('plasma'),
                          vmin=0.0,
                          vmax=1.0)
     if title is not None:
         plt.title(title)
     plt.show()
Exemplo n.º 4
0
def load_data(prefix='./example_data/',
              file_name='YelpChi.mat',
              relations=['net_rur'],
              normalize=True,
              load_walks=False,
              train_size=0.8):
    adjs, feats, truelabels, train_idx, test_idx = load_mat_full(
        prefix, file_name, relations, train_size)
    gs = [nx.to_networkx_graph(adj) for adj in adjs]
    id_map = {int(i): i for i in range(len(truelabels))}
    class_map = {int(i): truelabels[i] for i in range(len(truelabels))}
    walks = []
    adj_main = np.sum(
        adjs
    )  # change the index to specify which adj matrix to use for aggregation
    G = nx.to_networkx_graph(adj_main)
    gs = [graph_process(g, feats, truelabels, test_idx) for g in gs]
    G = graph_process(G, feats, truelabels, test_idx)
    if normalize and not feats is None:
        from sklearn.preprocessing import StandardScaler
        train_ids = np.array([id_map[n] for n in G.nodes()])
        train_feats = feats[train_ids]
        scaler = StandardScaler()
        scaler.fit(train_feats)
        feats = scaler.transform(feats)
    if load_walks:
        with open(prefix + "-walks.txt") as fp:
            for line in fp:
                walks.append(map(conversion, line.split()))
    return G, feats, id_map, walks, class_map, gs
Exemplo n.º 5
0
def test_graph_india():
    A1 = np.loadtxt("adj_allVillageRelationships_vilno_1.csv", delimiter=",")
    A2 = np.loadtxt("adj_allVillageRelationships_vilno_2.csv", delimiter=",")
    G1 = nx.to_networkx_graph(A1)
    G2 = nx.to_networkx_graph(A2)
    basic_net_stats(G1)
    basic_net_stats(G2)
    plot_degree_distribution(G1)
    plot_degree_distribution(G2)
    plt.show()
Exemplo n.º 6
0
def betweenness_central(net_mat, normalized=True):
    net_mat = np.matrix(net_mat)
    graph = nx.to_networkx_graph(net_mat)
    bc = nx.betweenness_centrality(
        graph, normalized=normalized)  # dictionary where key = node
    bc = np.array([bc[i] for i in range(len(bc))])
    return bc
 def identity_conversion(self, G, A, create_using):
     GG = nx.from_numpy_matrix(A, create_using=create_using)
     self.assert_equal(G, GG)
     GW = nx.to_networkx_graph(A, create_using=create_using)
     self.assert_equal(G, GW)
     GI = create_using.__class__(A)
     self.assert_equal(G, GI)
Exemplo n.º 8
0
    def test_add4_random(self):

        cell_complex = models.CellComplex2D(0, 10, 10)

        np.random.seed(3)

        planes = simulator.random_planes(num_planes=4, max_distance=9, c=0)
        for p in planes:
            cell_complex.insert_partition(p)

        H = cell_complex.cell_graph()
        G = nx.to_networkx_graph({
            0: [1, 2, 4],
            1: [0, 6],
            2: [0, 3, 6],
            3: [2, 4, 7, 8],
            4: [0, 3, 5],
            5: [4, 8],
            6: [1, 2, 7],
            7: [6, 3],
            8: [3, 5]
        })
        self.assertTrue(nx.is_isomorphic(G, H))

        cell_complex.draw(scene_graph=H)
Exemplo n.º 9
0
def convert(A):

    # Create networkx graph from numpy adjacency matrix.

    g = nx.to_networkx_graph(A)

    return g
Exemplo n.º 10
0
 def test_non_multigraph_input(self, dod, mgi, edges):
     G = self.Graph(dod, multigraph_input=mgi)
     assert list(G.edges(keys=True, data=True)) == edges
     G = nx.to_networkx_graph(dod,
                              create_using=self.Graph,
                              multigraph_input=mgi)
     assert list(G.edges(keys=True, data=True)) == edges
def draw_graph(G_arr, row, col, pos, fname='Results/figures'):
    '''draw a numpy of graphs [num_graph,num_nodes,num_nodes], with coordinates'''
    plt.switch_backend('agg')
    for j in range(0, G_arr.shape[0], row + col):
        plt.figure()
        right = min(j + row + col, G_arr.shape[0])
        for i, id in enumerate(range(j, right)):
            plt.subplot(row, col, i + 1)
            plt.subplots_adjust(left=0,
                                bottom=0,
                                right=1,
                                top=1,
                                wspace=0,
                                hspace=0)
            plt.axis("off")

            G = nx.to_networkx_graph(data=G_arr[id])

            nx.draw_networkx_nodes(G,
                                   pos[id],
                                   node_size=1.5,
                                   node_color='#336699',
                                   alpha=1,
                                   linewidths=0.2,
                                   font_size=1.5)
            nx.draw_networkx_edges(G, pos[id], alpha=0.6, width=0.2)

        # plt.axis('off')
        # plt.title('Complete Graph of Odd-degree Nodes')
        # plt.show()
        plt.tight_layout()
        plt.savefig(fname + str(j) + '.png', dpi=800)
        plt.close()
Exemplo n.º 12
0
def draw(unet, partition=None, ax=None, cmap="Spectral"):

    if ax is None:
        ax = plt.gca()

    netx = nx.to_networkx_graph(unet.edge_dict)

    if partition is not None:

        col = np.linspace(0, 1, num=unet.number_of_communities)

        node_color = np.array([
            col[i] for node in range(unet.number_of_nodes)
            for i, comm in enumerate(partition) if node in comm
        ])
    else:

        node_color = "#1f78b4"

    nx.draw(netx,
            ax=ax,
            width=0.1,
            node_size=20,
            node_color=node_color,
            cmap=cmap)
Exemplo n.º 13
0
 def identity_conversion(self, G, A, create_using):
     GG = nx.from_numpy_matrix(A, create_using=create_using)
     self.assert_equal(G, GG)
     GW = nx.to_networkx_graph(A, create_using=create_using)
     self.assert_equal(G, GW)
     GI = create_using.__class__(A)
     self.assert_equal(G, GI)
Exemplo n.º 14
0
    def identity_conversion(self, G, A, create_using):
        GG = nx.from_scipy_sparse_matrix(A, create_using=create_using)
        self.assert_equal(G, GG)

        GW = nx.to_networkx_graph(A, create_using=create_using)
        self.assert_equal(G, GW)

        GI = create_using.__class__(A)
        self.assert_equal(G, GI)

        ACSR = A.tocsr()
        GI = create_using.__class__(ACSR)
        self.assert_equal(G, GI)

        ACOO = A.tocoo()
        GI = create_using.__class__(ACOO)
        self.assert_equal(G, GI)

        ACSC = A.tocsc()
        GI = create_using.__class__(ACSC)
        self.assert_equal(G, GI)

        AD = A.todense()
        GI = create_using.__class__(AD)
        self.assert_equal(G, GI)

        AA = A.toarray()
        GI = create_using.__class__(AA)
        self.assert_equal(G, GI)
Exemplo n.º 15
0
def t_delta_partition(t_delta_matrix,sm,verbose=False):
    import community;
    g=nx.to_networkx_graph(t_delta_matrix+t_delta_matrix.T - np.diag(t_delta_matrix.diagonal()) ,create_using=nx.Graph()); 
    if verbose==True:
        plt.figure, plt.pcolor(np.array(nx.to_numpy_matrix(g))), plt.colorbar();
        plt.show()
    return community.best_partition(g);
Exemplo n.º 16
0
def get_test_edges(adj):
    adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
    adj.eliminate_zeros()
    edges_all = sparse_to_tuple(adj)[0].tolist()

    edge_count = len(edges_all) / 2.0
    num_test = int(np.floor(edge_count / 10.))
    num_val = int(np.floor(edge_count / 20.))

    G = nx.to_networkx_graph(adj)
    test_edges = pick_edges(G, num_test)
    test_edges_false = pick_false_edges(G, num_test)

    G.remove_edges_from(test_edges)
    val_edges = pick_edges(G, num_val)
    val_edges_false = pick_false_edges(G, num_val)

    G.remove_edges_from(val_edges)
    adj_train = nx.to_scipy_sparse_matrix(G)
    train_edges = sparse_to_tuple(adj_train)[0].tolist()

    def ismember(a, b):
        seta = set([tuple(x) for x in a])
        setb = set([tuple(x) for x in b])
        return len(seta & setb) > 0

    assert not ismember(test_edges_false, edges_all)
    assert not ismember(val_edges_false, val_edges + train_edges)
    assert not ismember(val_edges, train_edges)
    assert not ismember(test_edges, train_edges)
    assert not ismember(val_edges, test_edges)
    assert ismember(val_edges, val_edges)

    return adj_train, train_edges, val_edges, val_edges_false, test_edges, test_edges_false
Exemplo n.º 17
0
def degree_dist(g):
    if isinstance(g,np.ndarray):
        g=nx.to_networkx_graph(g)    # if matrix is passed, convert to networkx
    d=dict(g.degree()).values()
    vals=list(set(d))
    counts=[d.count(i) for i in vals]
    return list(zip(vals, counts))
Exemplo n.º 18
0
 def createMST(self):
     '''
     Using the networkX library create a MST from the adjacency Matrix, this 
     was unused after initial investigation
     '''
     graph = nx.to_networkx_graph(self.adjacencyMatrix) # initialise graph
     self.mst = nx.minimum_spanning_tree(graph)
Exemplo n.º 19
0
    def identity_conversion(self, G, A, create_using):
        GG = nx.from_scipy_sparse_matrix(A, create_using=create_using)
        self.assert_equal(G, GG)

        GW = nx.to_networkx_graph(A, create_using=create_using)
        self.assert_equal(G, GW)

        GI = create_using.__class__(A)
        self.assert_equal(G, GI)

        ACSR = A.tocsr()
        GI = create_using.__class__(ACSR)
        self.assert_equal(G, GI)

        ACOO = A.tocoo()
        GI = create_using.__class__(ACOO)
        self.assert_equal(G, GI)

        ACSC = A.tocsc()
        GI = create_using.__class__(ACSC)
        self.assert_equal(G, GI)

        AD = A.todense()
        GI = create_using.__class__(AD)
        self.assert_equal(G, GI)

        AA = A.toarray()
        GI = create_using.__class__(AA)
        self.assert_equal(G, GI)
Exemplo n.º 20
0
    def _randomize(net, density=None):
        n_nodes = len(net.nodes())
        density = density or 1.0 / n_nodes
        max_attempts = 50

        for attempt in xrange(max_attempts):
            # create an random adjacency matrix with given density
            adjmat = N.random.rand(n_nodes, n_nodes)
            adjmat[adjmat >= (1.0 - density)] = 1
            adjmat[adjmat < 1] = 0

            # add required edges
            for src, dest in required_edges:
                adjmat[src][dest] = 1

            # remove prohibited edges
            for src, dest in prohibited_edges:
                adjmat[src][dest] = 0

            # remove self-loop edges (those along the diagonal)
            adjmat = N.invert(N.identity(n_nodes).astype(bool)) * adjmat

            # set the adjaceny matrix and check for acyclicity
            net = nx.to_networkx_graph(adjmat, create_using=Network())

            if net.is_acyclic():
                return net

        # got here without finding a single acyclic network.
        # so try with a less dense network
        return _randomize(density / 2)
def average_shortest_distance(A, W):
    D_dict = nx.all_pairs_shortest_path_length(nx.to_networkx_graph(A))
    sum = 0
    for source in D_dict:
        for target in range(len(A)):
            sum += source[1][target] * W[source[0], target]
    return sum / np.sum(W)
Exemplo n.º 22
0
def get_qoe(source, target, network, algorithm):
    G = nx.to_networkx_graph(
        network, create_using=None, multigraph_input=False)
    return {
        "dijkstra": nx.dijkstra_path_length(G, source, target),
        "bellman_ford": nx.bellman_ford_path_length(G, source, target)
    }[algorithm]
Exemplo n.º 23
0
    def _randomize(net, density=None):
        n_nodes = len(net.nodes())
        density = density or 1.0/n_nodes
        max_attempts = 50

        for attempt in xrange(max_attempts):
            # create an random adjacency matrix with given density
            adjmat = N.random.rand(n_nodes, n_nodes)
            adjmat[adjmat >= (1.0-density)] = 1
            adjmat[adjmat < 1] = 0
            
            # add required edges
            for src,dest in required_edges:
                adjmat[src][dest] = 1

            # remove prohibited edges
            for src,dest in prohibited_edges:
                adjmat[src][dest] = 0

            # remove self-loop edges (those along the diagonal)
            adjmat = N.invert(N.identity(n_nodes).astype(bool))*adjmat
            
            # set the adjaceny matrix and check for acyclicity
            net = nx.to_networkx_graph(adjmat, create_using=Network())

            if net.is_acyclic():
                return net

        # got here without finding a single acyclic network.
        # so try with a less dense network
        return _randomize(density/2)
Exemplo n.º 24
0
def degree_dist(g):
    if isinstance(g, np.ndarray):
        g = nx.to_networkx_graph(g)  # if matrix is passed, convert to networkx
    d = g.degree().values()
    vals = list(set(d))
    counts = [d.count(i) for i in vals]
    return zip(vals, counts)
Exemplo n.º 25
0
    def test_exceptions(self):
        # _prep_create_using
        G = {"a": "a"}
        H = nx.to_networkx_graph(G)
        assert_graphs_equal(H, nx.Graph([('a', 'a')]))
        assert_raises(TypeError, to_networkx_graph, G, create_using=0.0)

        # NX graph
        class G(object):
            adj = None

        assert_raises(nx.NetworkXError, to_networkx_graph, G)

        # pygraphviz  agraph
        class G(object):
            is_strict = None

        assert_raises(nx.NetworkXError, to_networkx_graph, G)

        # Dict of [dicts, lists]
        G = {"a": 0}
        assert_raises(TypeError, to_networkx_graph, G)

        # list or generator of edges
        class G(object):
            next = None

        assert_raises(nx.NetworkXError, to_networkx_graph, G)

        # no match
        assert_raises(nx.NetworkXError, to_networkx_graph, "a")
Exemplo n.º 26
0
    def identity_conversion(self, G, A, create_using):
        GG = nx.from_scipy_sparse_matrix(A, create_using=create_using)
        assert nx.is_isomorphic(G, GG)

        GW = nx.to_networkx_graph(A, create_using=create_using)
        assert nx.is_isomorphic(G, GW)

        GI = nx.empty_graph(0, create_using).__class__(A)
        assert nx.is_isomorphic(G, GI)

        ACSR = A.tocsr()
        GI = nx.empty_graph(0, create_using).__class__(ACSR)
        assert nx.is_isomorphic(G, GI)

        ACOO = A.tocoo()
        GI = nx.empty_graph(0, create_using).__class__(ACOO)
        assert nx.is_isomorphic(G, GI)

        ACSC = A.tocsc()
        GI = nx.empty_graph(0, create_using).__class__(ACSC)
        assert nx.is_isomorphic(G, GI)

        AD = A.todense()
        GI = nx.empty_graph(0, create_using).__class__(AD)
        assert nx.is_isomorphic(G, GI)

        AA = A.toarray()
        GI = nx.empty_graph(0, create_using).__class__(AA)
        assert nx.is_isomorphic(G, GI)
Exemplo n.º 27
0
    def test_exceptions(self):
        # _prep_create_using
        G = {"a": "a"}
        H = nx.to_networkx_graph(G)
        assert_graphs_equal(H, nx.Graph([('a', 'a')]))
        assert_raises(TypeError, to_networkx_graph, G, create_using=0.0)

        # NX graph
        class G(object):
            adj = None

        assert_raises(nx.NetworkXError, to_networkx_graph, G)

        # pygraphviz  agraph
        class G(object):
            is_strict = None

        assert_raises(nx.NetworkXError, to_networkx_graph, G)

        # Dict of [dicts, lists]
        G = {"a": 0}
        assert_raises(TypeError, to_networkx_graph, G)

        # list or generator of edges
        class G(object):
            next = None

        assert_raises(nx.NetworkXError, to_networkx_graph, G)

        # no match
        assert_raises(nx.NetworkXError, to_networkx_graph, "a")
Exemplo n.º 28
0
def to_normalize(J, netx=False):
    if netx:
        J = nx.to_numpy_array(J)
    max_J = np.max(J)
    min_J = np.min(J)

    if max_J >= 0 and max_J <= 1 and min_J >= 0 and min_J <= 1:
        if netx:
            return nx.to_networkx_graph(J)
        else:
            return J
    else:
        if netx:
            return nx.to_networkx_graph(J / max_J)
        else:
            return J / max_J
Exemplo n.º 29
0
def get_qoe(source, target, network, algorithm):
    G = nx.to_networkx_graph(network,
                             create_using=None,
                             multigraph_input=False)
    try:
        return nx.bellman_ford_path_length(G, source, target)
    except:
        return -1
def compare_to_brute_multiple_exp(N,
                                  num_options,
                                  num_trials,
                                  show_graphs=False):

    P = np.array(
        [np.random.permutation(np.arange(N))[:2] for i in range(N * N)])
    # P = []
    # [[P.append((i,j)) for i in range(N)] for j in range(N)]
    # P = np.array(P)
    W = get_weight_matrix(N, P)

    A = []
    B = []

    for i in range(num_trials):
        print("trial ", (i + 1), " of ", num_trials)
        #Gnx = nx.barabasi_albert_graph(n=N,m=1)
        Gnx = nx.fast_gnp_random_graph(n=N, p=.2)

        graph = nx.to_numpy_matrix(Gnx).astype(dtype='int')

        ASPDM_list, Brute_list, graph_alg, graph_brute = compare_to_brute_exp(
            graph, P, W, num_options)
        A.append(ASPDM_list)
        B.append(Brute_list)
        if show_graphs:
            fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
            nx.draw(nx.to_networkx_graph(graph), ax=ax1)
            nx.draw(nx.to_networkx_graph(graph_alg), ax=ax2)
            nx.draw(nx.to_networkx_graph(graph_brute), ax=ax3)
            plt.show()

    # A = np.array(A)
    # B = np.array(B)
    # X = np.arange(A.shape[1])
    # plt.fill_between(X,np.max(A,axis=0),np.min(A,axis=0),alpha=.5)
    # plt.fill_between(X,np.max(B,axis=0),np.min(B,axis=0),alpha=.5)
    fig = plt.figure()
    [plt.plot(data, color='orange') for data in A]
    [plt.plot(data, color='blue') for data in B]
    plt.xlabel('number of options')
    plt.ylabel('average shortest distance')
    plt.title('ASPDM-orange  Brute-Blue')
    fig.show()
    plt.show()
Exemplo n.º 31
0
def geoGraph(n, d, epsilon):
    """ Create a geometric graph: n points in d-dimensional space,
    nodes are connected if closer than epsilon"""
    points = np.random.random((n,d))
    pl2 = np.array([np.linalg.norm(points, axis=1)])**2
    eucDist = (pl2.T @ np.ones((1,n))) + (np.ones((n,1)) @ pl2) - (2 * points @ points.T)
    A = ((eucDist + np.eye(n)) < epsilon).astype(int)
    return nx.to_networkx_graph(A)
Exemplo n.º 32
0
def draw_sentence_graph(m, sentences=None):
    g = nx.to_networkx_graph(m)
    weights = [g[u][v]['weight'] * 3 for u, v in g.edges()]
    if sentences is not None:
        labels = {i: s for i, s in zip(g.nodes(), sentences)}
    else:
        labels = {i: i + 1 for i in g.nodes()}
    return nx.draw(g, labels=labels, with_labels=True, width=weights)
Exemplo n.º 33
0
 def identity_conversion(self, G, A, create_using):
     assert(A.sum() > 0)
     GG = nx.from_numpy_array(A, create_using=create_using)
     self.assert_equal(G, GG)
     GW = nx.to_networkx_graph(A, create_using=create_using)
     self.assert_equal(G, GW)
     GI = nx.empty_graph(0, create_using).__class__(A)
     self.assert_equal(G, GI)
Exemplo n.º 34
0
 def identity_conversion(self, G, A, create_using):
     assert (A.sum() > 0)
     GG = nx.from_numpy_array(A, create_using=create_using)
     self.assert_equal(G, GG)
     GW = nx.to_networkx_graph(A, create_using=create_using)
     self.assert_equal(G, GW)
     GI = nx.empty_graph(0, create_using).__class__(A)
     self.assert_equal(G, GI)
Exemplo n.º 35
0
def get_default_graph(get_edges):
    num_nodes = 100
    x = [random.random() for i in range(num_nodes)]
    y = [random.random() for i in range(num_nodes)]

    x = np.array(x)
    y = np.array(y)

    # Make a graph with num_nodes nodes and zero edges
    # Plot the nodes using x,y as the node positions

    graph = nx.Graph()
    for i in range(num_nodes):
        node_name = str(i)
        graph.add_node(node_name)

    # Now add some edges - use Delaunay tesselation
    # to produce a planar graph. Delaunay tesselation covers the
    # convex hull of a set of points with triangular simplices (in 2D)

    points = np.column_stack((x, y))
    dl = Delaunay(points)
    tri = dl.simplices

    if get_edges:
        edges = np.zeros((2, 6 * len(tri)), dtype=int)
        data = np.ones(6 * len(points))
        j = 0
        for i in range(len(tri)):
            edges[0][j] = tri[i][0]
            edges[1][j] = tri[i][1]
            j += 1
            edges[0][j] = tri[i][1]
            edges[1][j] = tri[i][0]
            j += 1
            edges[0][j] = tri[i][0]
            edges[1][j] = tri[i][2]
            j += 1
            edges[0][j] = tri[i][2]
            edges[1][j] = tri[i][0]
            j += 1
            edges[0][j] = tri[i][1]
            edges[1][j] = tri[i][2]
            j += 1
            edges[0][j] = tri[i][2]
            edges[1][j] = tri[i][1]
            j += 1

        data = np.ones(6 * len(tri))
        adjacency_matrix = sp.sparse.csc_matrix(
            (data, (edges[0, :], edges[1, :])))

        for i in range(adjacency_matrix.nnz):
            adjacency_matrix.data[i] = 1.0

        graph = nx.to_networkx_graph(adjacency_matrix)

    return graph
 def generate_microstate_hb_network(self, conformer_ids=None, labels=None):
     """
     Parameters
     ----------
     conformer_ids : TYPE
         Description
     """
     if conformer_ids is None:
         G = nx.to_networkx_graph(self.hb_matrix)
         return G
     else:
         G = nx.to_networkx_graph(self.hb_matrix[conformer_ids, :][:, conformer_ids])
         if labels is None:
             nx.relabel_nodes(G, dict(list(zip(G.nodes(), conformer_ids))), copy=False)
         else:
             assert len(labels) == len(conformer_ids), "Please make sure number of nodes and labels are equal."
             nx.relabel_nodes(G, dict(list(zip(G.nodes(), labels))), copy=False)
         return G
Exemplo n.º 37
0
def geoGraph(n, d, epsilon):
    """ Create a geometric graph: n points in d-dimensional space,
    nodes are connected if closer than epsilon"""
    points = np.random.random((n, d))
    pl2 = np.array([np.linalg.norm(points, axis=1)])**2
    eucDist = (pl2.T @ np.ones((1, n))) + (np.ones(
        (n, 1)) @ pl2) - (2 * points @ points.T)
    A = ((eucDist + np.eye(n)) < epsilon).astype(int)
    return nx.to_networkx_graph(A)
def display_graphic_graph(adjacency_list: Dict[str, List[str]],
                          sequence: List[int]):
    for vertex in adjacency_list:
        print(vertex, " -> ", adjacency_list[vertex])

    G = nx.to_networkx_graph(adjacency_list)
    nx.draw(G, with_labels=True, font_weight='bold')
    plt.show()
    plt.title(f'{sequence}')
Exemplo n.º 39
0
def StickyGraph(n, deg):
    """input: n, degree sequence."""
    assert(n == len(deg))
    deg = np.array(deg) / np.sqrt(np.sum(deg))
    A = np.zeros((n,n),dtype=int)
    for i,j in itertools.combinations(range(n),2):
        if (i != j) and (np.random.random() < deg[i]*deg[j]):
            A[i,j] = 1
            A[j,i] = 1
    return nx.to_networkx_graph(A)
Exemplo n.º 40
0
def geoGraphP(n, d, p):
    """ Create a geometric graph: n points in d-dimensional space,
    fraction p node pairs are connected"""
    points = np.random.random((n,d))
    pl2 = np.array([np.linalg.norm(points, axis=1)])**2
    eucDist = (pl2.T @ np.ones((1,n))) + (np.ones((n,1)) @ pl2) - (2 * points @ points.T)
    dists = np.sort(np.ravel(eucDist))
    epsilon = dists[n + np.floor((n**2-n) * p).astype(int)]
    A = ((eucDist + dists[-1] * np.eye(n)) < epsilon).astype(int)
    return nx.to_networkx_graph(A)
Exemplo n.º 41
0
def hits_algo(adj_matrix,hub_score):  
    # INPUT: Initial hub_score, authorities score and adjacency matrix.
    # OUTPUT: Converged
    print "Running HITS algorithm..."
    graph = nx.to_networkx_graph(adj_matrix)
    # print graph
    nstart = dict([(i, hub_score[i]) for i in xrange(len(hub_score))])
    # print nstart
    # return nx.hits(graph)
    return nx.hits(graph,nstart=nstart)
Exemplo n.º 42
0
def convertIDToGraph(id,motifSize):
	binary = bin(id);
	adj = np.zeros(motifSize*motifSize)
	for x in xrange(motifSize*motifSize):
		if binary[-x] == 'b':
			break
		adj[-x] = int(binary[-x])
	adj.shape = (motifSize,motifSize)
	graph = nx.to_networkx_graph(adj,create_using=nx.DiGraph())
	nx.draw_circular(graph)
	#plt.savefig("result/id-"+str(id)+"size-"+str(motifSize))
	plt.show()
Exemplo n.º 43
0
    def __init__(self, term_term_matrix, threshold=10):
        self.graph = {}

        for term_a in term_term_matrix:
            for term_b in term_term_matrix:
                if term_term_matrix[term_a][term_b] >= threshold:
                    tmp = self.graph.get(term_a, [])
                    tmp.append(term_b)
                    self.graph[term_a] = tmp

            if term_a not in self.graph:
                self.graph[term_a] = []

        self.nx_graph = nx.to_networkx_graph(self.graph)
Exemplo n.º 44
0
def convert_matrix_to_digraph(matrix):
    """
    Converts a python's matrix into a networkx's digraph.

    Args:
        matrix : MATRIX[[INT, INT, ...], [INT, INT, ...], ...]
            This matrix is the representation of the genes

    Returns:
        nx.DiGraph()
            The converted matrix into a digraph
    """
    genes = np.asmatrix(matrix)
    di_graph = nx.to_networkx_graph(genes, create_using=nx.DiGraph())
    return di_graph
Exemplo n.º 45
0
def test_graph_generator():
    A1 = np.loadtxt("adj_allVillageRelationships_vilno_1.csv", delimiter=",")
    A2 = np.loadtxt("adj_allVillageRelationships_vilno_2.csv", delimiter=",")
    G1 = nx.to_networkx_graph(A1)
    G2 = nx.to_networkx_graph(A2)
    gen1 = nx.connected_component_subgraphs(G1)
    G1_LCC = max(gen1, key=len)
    print(len(G1_LCC))
    print(G1.number_of_nodes())
    print(G1_LCC.number_of_nodes())
    print(G1_LCC.number_of_nodes()/G1.number_of_nodes())
    # g1 = gen1.__next__()
    # print(g1)
    # basic_net_stats(G1)
    # basic_net_stats(g1)
    gen2 = nx.connected_component_subgraphs(G2)
    G2_LCC = max(gen2, key=len)
    print(len(G2_LCC))
    print(G2.number_of_nodes())
    print(G2_LCC.number_of_nodes())
    print(G2_LCC.number_of_nodes()/G2.number_of_nodes())
    plt.figure()
    nx.draw(G2_LCC, with_labels=False)
    plt.show()
Exemplo n.º 46
0
def drawG(g,Xs=[],labels={},save=False,display=True):
    if type(g) == np.ndarray:
        g=nx.to_networkx_graph(g)
    nx.relabel_nodes(g, labels, copy=False)
    #pos=nx.spring_layout(g, scale=5.0)
    pos = nx.graphviz_layout(g, prog="fdp")
    nx.draw_networkx(g,pos,node_size=1000)
#    for node in range(numnodes):                    # if the above doesn't work
#        plt.annotate(str(node), xy=pos[node])       # here's a workaround
    if Xs != []:
        plt.title(Xs)
    plt.axis('off')
    if save==True:
        plt.savefig('temp.png')                      # need to parameterize
    if display==True:
        plt.show()
Exemplo n.º 47
0
def convertIDToGraph(id,motifSize,save=False):
	"""Plot graph with id and motifSize"""
	binary = bin(id);
	adj = np.zeros(motifSize*motifSize)
	for x in xrange(motifSize*motifSize):
		x+=1
		if binary[-x] == 'b':
			break
		adj[-x] = int(binary[-x])
	adj.shape = (motifSize,motifSize)
	graph = nx.to_networkx_graph(adj,create_using=nx.DiGraph())
	nx.draw_circular(graph)
	if save:
		plt.savefig("result/id-"+str(id)+"size-"+str(motifSize))
	else:
		plt.show()
	plt.clf()
Exemplo n.º 48
0
 def test_from_edgelist(self):
     # Pandas DataFrame
     g = nx.cycle_graph(10)
     G = nx.Graph()
     G.add_nodes_from(g)
     G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
     edgelist = nx.to_edgelist(G)
     source = [s for s, t, d in edgelist]
     target = [t for s, t, d in edgelist]
     weight = [d['weight'] for s, t, d in edgelist]
     edges = pd.DataFrame({'source': source,
                           'target': target,
                           'weight': weight})
     GG = nx.from_pandas_edgelist(edges, edge_attr='weight')
     assert_nodes_equal(G.nodes(), GG.nodes())
     assert_edges_equal(G.edges(), GG.edges())
     GW = nx.to_networkx_graph(edges, create_using=nx.Graph())
     assert_nodes_equal(G.nodes(), GW.nodes())
     assert_edges_equal(G.edges(), GW.edges())
Exemplo n.º 49
0
def convertIDToGraph(mid, motifSize, save=False):
  """Draw graph with id and motifSize"""
  binary = bin(mid);
  adj = np.zeros(motifSize*motifSize)
  l = 0
  for x in xrange(1,motifSize*motifSize+1):
	if binary[-x+l] == 'b':
	  break
	if (x-1) % (motifSize+1) == 0:
	  l += 1
	else:
	  adj[-x] = int(binary[-x+l])
  adj.shape = (motifSize,motifSize)
  graph = nx.to_networkx_graph(adj,create_using=nx.DiGraph())
  nx.draw_circular(graph)
  if save:
	plt.savefig("result/id-"+str(id)+"size-"+str(motifSize))
  else:
	plt.show()
  plt.clf()
Exemplo n.º 50
0
	def __str__(self,ID=''):
		# Matrix convertion to NetworkX graph:
		gx = networkx.to_networkx_graph(data=self.g)
		# Parameters
		deg = self.get_degrees()
		size_deg = [(10+(90*(i-min(deg)))/float(max(deg))) for i in deg] # size_deg = [(100*i/float(max(deg))) for i in deg]
		numlabels = False
		edgecolor = 'grey'
		nodecmap = pyplot.cm.rainbow
		# # Draw the graph with Matplotlib
		# networkx.draw(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		# pyplot.savefig(ID+"NetworkX_plot1.png")
		# pyplot.clf()
		# # Draw the graph with Matplotlib
		# networkx.draw_networkx(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		# pyplot.savefig(ID+"NetworkX_plot2-networkx.png")
		# pyplot.clf()
		# Draw the graph with a circular layout.
		# networkx.draw_circular(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		# pyplot.savefig(ID+"NetworkX_plot3-circular.png")
		# pyplot.clf()
		# # Draw the graph with a random layout.
		# networkx.draw_random(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		# pyplot.savefig(ID+"NetworkX_plot4-random.png")
		# pyplot.clf()
		# Draw the graph with a spectral layout.
		# networkx.draw_spectral(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		# pyplot.savefig(ID+"NetworkX_plot5-spectral.png")
		# pyplot.clf()
		# Draw the graph with a spring layout.
		networkx.draw_spring(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		pyplot.savefig(ID+"NetworkX_plot6-spring.png")
		pyplot.clf()
		# # Draw the graph with a shell layout.
		# networkx.draw_shell(gx, with_labels=numlabels, node_size=size_deg, linewidths=0, width=0.5, alpha=1, cmap=nodecmap, node_color=deg, edge_color=edgecolor)
		# pyplot.savefig(ID+"NetworkX_plot7-shell.png")
		# pyplot.clf()
		# pyplot.draw()
		# pyplot.show()
		return "\nNetwork display saved.\n"
Exemplo n.º 51
0
def textrank(document):
    """ Performs the whole process of sentence comparison by similarity
    and scores sentence according to their importance within the document.

    We first vectorize documents with a bag-of-words (Count Vectorizer), then
    normalize the sentence vectors with tf-idf. Then, we construct graph adjacency
    matrix where edge weights are similarity values using one of our similarity 
    functions. With that graph where vertices are sentences, we use networkx' 
    built-in PageRank algorithm to score the sentences and return a list
    of tuples (sentence, score, document) ordered by score.
    """ 
    if isinstance(document, basestring):      # if the document is not tokenized
        sentences = sent_tokenize_func(document)
    else:                                     # if the sentences are already tokenized
        sentences = document

    # vectorizer
    vectorizer = CountVectorizer()

    # vectorize individual sentences counting up the word occurences
    matrix = vectorizer.fit_transform(sentences)

    # normalize counts
    normalized = normalize_func(matrix)

    # creates a similarity graph matrix
    similarity_graph = similarity_func(normalized)

    # converts matrix to a networkx' graph struct
    nx_graph = nx.to_networkx_graph(similarity_graph)

    # use PageRank to score vertices in the graph
    scores = nx.pagerank(nx_graph)

    # return vertices ordered by score
    return sorted((TextRank(score=scores[i], sentence=sentence, document=i)
                  for i, sentence in enumerate(sentences)),
                  reverse=True)
Exemplo n.º 52
0
    def test_change_times(self):

        ns = tn.NetworkSequence()
        # complete graph on 4 vertices - could have used the networkx generator too
        t1 = np.array([[1,3,1,1],[3,1,1,1],[1,1,1,1],[1,1,1,1]])
        g = nx.to_networkx_graph(t1)

        ns.add_network(0, g)
        ns.add_network(10, g)
        ns.add_network(5, g)
        ns.add_network(25, g)
        ns.add_network(3, g)

        times = ns.get_list_of_change_times()


        ns.get_graph_matrix_for_time(8)
        ns.get_graph_matrix_for_time(12)
        ns.get_graph_matrix_for_time(0)
        ns.get_graph_matrix_for_time(50)
        ns.get_graph_matrix_for_time(1)


        self.assertTrue(True)
Exemplo n.º 53
0
log.append(starttime)
while converge < max_converge:
    graphs, bestval=rw.graphSearch(graphs,numkeep,Xs,numnodes,maxlen,jeff,expected_irts)
    log.append(bestval)
    if bestval == oldbestval:
        converge += 1
    else:
        bestgraphs.append(graphs[0])
        if len(bestgraphs) > 5:
            bestgraphs.pop(0)
        converge = 0
        oldbestval = bestval
    graphs=rw.genFromSeeds(graphs,numperseed,edgestotweak)

gs=[nx.to_networkx_graph(i) for i in bestgraphs]

# record endtime
endtime=str(datetime.now())
log.append(endtime)

for i, j in enumerate(gs):
    nx.relabel_nodes(j, items, copy=False)
    nx.write_dot(j,subj+"_"+str(i)+".dot")

# write iterations and start/end time to log
with open(subj+'_log.txt','w') as f:
    for item in log:
        print>>f, item

with open(subj+'_lists.csv','w') as f:
Exemplo n.º 54
0
for i in xrange(len(data)):
    j = reddits.index(data[i][0])
    url_dict[j] = str(data[i][1])
    size_dict[j] = int(data[i][2])
    if data[i][3] == 1:
       NSFW_dict[j] = 'NSFW'
    else:
       NSFW_dict[j] = 'SFW'
    title_dict[j] = str(data[i][5])



sql.close()
sql.status


labels = dict()
for i in xrange(len(reddits)):
    labels[i] = reddits[i]

G = nx.to_networkx_graph(A)
nx.set_node_attributes(G,'Subreddit Name',labels)

nx.set_node_attributes(G,'size',size_dict)
nx.set_node_attributes(G,'url',url_dict)
nx.set_node_attributes(G,'NSFW',NSFW_dict)
nx.set_node_attributes(G,'header title',headertitle_dict)
nx.set_node_attributes(G,'title',title_dict)
#nx.set_node_attributes(G,'Descriptions',desc_dict)
nx.write_gexf(G, "test.gexf")
Exemplo n.º 55
0
import networkx as net
import process_tweets

retweets=process_tweets.g

len(retweets)

retweets.remove_edges_from(retweets.selfloop_edges())
undir=net.to_networkx_graph(retweets)
core=net.k_core(undir)

len(core)

net.draw(core)

    
    for i in range(0,iterations):
        sampleoutput += currentchar
        currentNodeTransitions = [x for x in itertools.izip(alphabets,P[alphabets.index(currentchar),:])]
        currentchar = weighted_choice(currentNodeTransitions)
    return sampleoutput
#################################################
#filesList = ['Sherlock.txt','HuckleberryFinn.txt','Contos.txt','license.txt','Charnet2.py']
filesList = ['abc.txt']
#onlyfiles = [ f for f in listdir('./Data') if isfile(join(mypath,f)) ]

for f in filesList:
    chars = createCharsfromFile(f)
    alphabets = createAlphabetsfromChars(chars)
    transMat = createTransitionMatrix(chars,alphabets)
    P = (transMat.T/np.sum(transMat.T,axis=0)).T  
#    figure(f)
#    plt.imshow(Pthresh, cmap = cm.Greys_r, interpolation = None)
    sampleoutput = randomwalk(P,alphabets,1000,alphabets[0])
    
    fo = open(f + 'out.txt','w')
    fo.write(sampleoutput)
    fo.close()  

#a=[i+'->'+j for i,j in itertools.product(alphabets,alphabets)]
G=nx.to_networkx_graph(P,create_using=nx.DiGraph())
nx.write_graphml(G,"charnet2.graphml")
nx.write_gml(G,"charnet2.gml")


Exemplo n.º 57
0
def network_properties(command, root_path):
    subj_props = command['data_parameters']
    command = command['network_parameters']

    # U-INVITE won't work with perseverations
    if command['network_method'] == "U-INVITE":
        removePerseverations=True
    else:
        removePerseverations=False
    
    if subj_props['factor_type'] == "subject":
        ids = str(subj_props['subject'])
        group = False
    elif subj_props['factor_type'] == "group":
        ids = str(subj_props['group'])            # without str() causes unicode issues for "all" :(
        group = True

    filedata = snafu.readX(ids, subj_props['fullpath'], category=subj_props['category'], spellfile=label_to_filepath(subj_props['spellfile'], root_path, "spellfiles"), removePerseverations=removePerseverations, group=group)
    filedata.nonhierarchical()
    Xs = filedata.Xs
    items = filedata.items
    irts = filedata.irts
    numnodes = filedata.numnodes
    
    toydata=snafu.DataModel({
            'numx': len(Xs),
            'trim': 1,
            'jump': float(command['jump_probability']),
            'jumptype': command['jump_type'],
            'priming': float(command['priming_probability']),
            'startX': command['first_item']})
    fitinfo=snafu.Fitinfo({
            'prior_method': "zeroinflatedbetabinomial",
            'prior_a': 1,
            'prior_b': 2,
            'zibb_p': 0.5,
            'startGraph': command['starting_graph'],
            'goni_size': int(command['goni_windowsize']),
            'goni_threshold': int(command['goni_threshold']),
            'followtype': "avg", 
            'prune_limit': 100,
            'triangle_limit': 100,
            'other_limit': 100})
   
    if command['prior']=="None":
        prior=None
    elif command['prior']=="USF":
        usf_file_path = "/snet/USF_animal_subset.snet"
        filename = root_path + usf_file_path
        
        usf_graph, usf_items = snafu.read_graph(filename)
        usf_numnodes = len(usf_items)
        priordict = snafu.genGraphPrior([usf_graph], [usf_items], fitinfo=fitinfo)
        prior = (priordict, usf_items)
        
    if command['network_method']=="RW":
        bestgraph = snafu.noHidden(Xs, numnodes)
    elif command['network_method']=="Goni":
        bestgraph = snafu.goni(Xs, numnodes, td=toydata, valid=0, fitinfo=fitinfo)
    elif command['network_method']=="Chan":
        bestgraph = snafu.chan(Xs, numnodes)
    elif command['network_method']=="Kenett":
        bestgraph = snafu.kenett(Xs, numnodes)
    elif command['network_method']=="FirstEdge":
        bestgraph = snafu.firstEdge(Xs, numnodes)
    elif command['network_method']=="U-INVITE":
        bestgraph, ll = snafu.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, debug=False, prior=prior)
    
    nxg = nx.to_networkx_graph(bestgraph)
    nxg_json = jsonGraph(nxg, items)
    
    return graph_properties(nxg,nxg_json)
Exemplo n.º 58
0
def drawDot(g, filename, labels={}):
    if type(g) == np.ndarray:
        g=nx.to_networkx_graph(g)
    if labels != {}:
        nx.relabel_nodes(g, labels, copy=False)
    nx.drawing.write_dot(g, filename)
Exemplo n.º 59
0
    
    
    for i in xrange(n_clu):
        fp = open(filepath+"\\a"+str(i)+".dat","w")
        mat = [[] for row in range(len(a_clusters[i]))]
        row = 0
        for j in a_clusters[i]:
            for k in a_clusters[i]:
                mat[row].append(int(AAM[j][k]))
                fp.write(str(int(AAM[j][k])))
                fp.write(" ")
            fp.write("\n")
            row = row + 1
        fp.close()
        m = np.array(mat)
        G = nx.to_networkx_graph(m, None, False)
        a_cl_coeff[i] = nx.average_clustering(G, None, None, True)
        print("cluster file "+str(i)+" for network A written")
        
    
    b_clusters = [[] for row in range(n_clu)]
    j = 0
    #nodes in each cluster a
    for i in b_labels:
        j = j + 1
        b_clusters[i].append(j-1) 
    print("identified node in each cluster of network B")
        
    #write clusters b