Python networkx.from_scipy_sparse_matrix() Examples

The following are 30 code examples for showing how to use networkx.from_scipy_sparse_matrix(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module networkx , or try the search function .

Example 1
Project: OpenNE   Author: thunlp   File: 20newsgroup.py    License: MIT License 8 votes vote down vote up
def text_to_graph(text):
    import networkx as nx
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.neighbors import kneighbors_graph

    # use tfidf to transform texts into feature vectors
    vectorizer = TfidfVectorizer()
    vectors = vectorizer.fit_transform(text)

    # build the graph which is full-connected
    N = vectors.shape[0]
    mat = kneighbors_graph(vectors, N, metric='cosine', mode='distance', include_self=True)
    mat.data = 1 - mat.data  # to similarity

    g = nx.from_scipy_sparse_matrix(mat, create_using=nx.Graph())

    return g 
Example 2
Project: news-popularity-prediction   Author: MKLab-ITI   File: comment_tree.py    License: Apache License 2.0 6 votes vote down vote up
def calculate_max_depth_over_max_width(comment_tree):
    comment_tree_nx = nx.from_scipy_sparse_matrix(comment_tree, create_using=nx.Graph())

    if len(comment_tree_nx) == 0:
        max_depth_over_max_width = 0.0
    else:
        node_to_depth = nx.shortest_path_length(comment_tree_nx, 0)
        depth_to_nodecount = collections.defaultdict(int)

        for k, v in node_to_depth.items():
            depth_to_nodecount[v] += 1

        max_depth = max(node_to_depth.values())
        max_width = max(depth_to_nodecount.values())

        max_depth_over_max_width = max_depth/max_width

    return max_depth_over_max_width 
Example 3
Project: news-popularity-prediction   Author: MKLab-ITI   File: comment_tree.py    License: Apache License 2.0 6 votes vote down vote up
def calculate_comment_tree_hirsch(comment_tree):
    comment_tree_nx = nx.from_scipy_sparse_matrix(comment_tree, create_using=nx.Graph())

    if len(comment_tree_nx) == 0:
        comment_tree_hirsch = 0.0
    else:
        node_to_depth = nx.shortest_path_length(comment_tree_nx, 0)

        depth_to_nodecount = collections.defaultdict(int)

        for k, v in node_to_depth.items():
            depth_to_nodecount[v] += 1

        comment_tree_hirsch = max(node_to_depth.values())
        while True:
            if depth_to_nodecount[comment_tree_hirsch] >= comment_tree_hirsch:
                break
            else:
                comment_tree_hirsch -= 1

    return comment_tree_hirsch 
Example 4
Project: DeepRobust   Author: DSE-MSU   File: test_rl_s2v.py    License: MIT License 6 votes vote down vote up
def init_setup():
    data = Dataset(root='/tmp/', name=args.dataset, setting='gcn')

    data.features = normalize_feature(data.features)
    adj, features, labels = data.adj, data.features, data.labels

    StaticGraph.graph = nx.from_scipy_sparse_matrix(adj)
    dict_of_lists = nx.to_dict_of_lists(StaticGraph.graph)

    idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
    device = torch.device('cuda') if args.ctx == 'gpu' else 'cpu'

    # black box setting
    adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False, sparse=True, device=device)
    victim_model = load_victim_model(data, device=device, file_path=args.saved_model)
    setattr(victim_model, 'norm_tool',  GraphNormTool(normalize=True, gm='gcn', device=device))
    output = victim_model.predict(features, adj)
    loss_test = F.nll_loss(output[idx_test], labels[idx_test])
    acc_test = accuracy(output[idx_test], labels[idx_test])
    print("Test set results:",
          "loss= {:.4f}".format(loss_test.item()),
          "accuracy= {:.4f}".format(acc_test.item()))

    return features, labels, idx_val, idx_test, victim_model, dict_of_lists, adj 
Example 5
Project: nlg-yongzhuo   Author: yongzhuo   File: textrank_sklearn.py    License: MIT License 6 votes vote down vote up
def textrank_tfidf(sentences, topk=6):
    """
        使用tf-idf作为相似度, networkx.pagerank获取中心句子作为摘要
    :param sentences: str, docs of text
    :param topk:int
    :return:list
    """
    # 切句子
    sentences = list(cut_sentence(sentences))
    # tf-idf相似度
    matrix_norm = tdidf_sim(sentences)
    # 构建相似度矩阵
    tfidf_sim = nx.from_scipy_sparse_matrix(matrix_norm * matrix_norm.T)
    # nx.pagerank
    sens_scores = nx.pagerank(tfidf_sim)
    # 得分排序
    sen_rank = sorted(sens_scores.items(), key=lambda x: x[1], reverse=True)
    # 保留topk个, 防止越界
    topk = min(len(sentences), topk)
    # 返回原句子和得分
    return [(sr[1], sentences[sr[0]]) for sr in sen_rank][0:topk] 
Example 6
Project: nlg-yongzhuo   Author: yongzhuo   File: textrank.py    License: MIT License 6 votes vote down vote up
def textrank_tfidf(sentences, topk=6):
    """
        使用tf-idf作为相似度, networkx.pagerank获取中心句子作为摘要
    :param sentences: str, docs of text
    :param topk:int
    :return:list
    """
    # 切句子
    sentences = list(cut_sentence(sentences))
    # tf-idf相似度
    matrix_norm = tdidf_sim(sentences)
    # 构建相似度矩阵
    tfidf_sim = nx.from_scipy_sparse_matrix(matrix_norm * matrix_norm.T)
    # nx.pagerank
    sens_scores = nx.pagerank(tfidf_sim)
    # 得分排序
    sen_rank = sorted(sens_scores.items(), key=lambda x: x[1], reverse=True)
    # 保留topk个, 防止越界
    topk = min(len(sentences), topk)
    # 返回原句子和得分
    return [(sr[1], sentences[sr[0]]) for sr in sen_rank][0:topk] 
Example 7
Project: text-analytics-with-python   Author: dipanjanS   File: document_summarization.py    License: Apache License 2.0 6 votes vote down vote up
def textrank_text_summarizer(documents, num_sentences=2,
                             feature_type='frequency'):
    
    vec, dt_matrix = build_feature_matrix(norm_sentences, 
                                      feature_type='tfidf')
    similarity_matrix = (dt_matrix * dt_matrix.T)
        
    similarity_graph = networkx.from_scipy_sparse_matrix(similarity_matrix)
    scores = networkx.pagerank(similarity_graph)   
    
    ranked_sentences = sorted(((score, index) 
                                for index, score 
                                in scores.items()), 
                              reverse=True)

    top_sentence_indices = [ranked_sentences[index][1] 
                            for index in range(num_sentences)]
    top_sentence_indices.sort()
    
    for index in top_sentence_indices:
        print sentences[index] 
Example 8
Project: pygsp   Author: epfl-lts2   File: test_graphs.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_differential_operator(self, n_vertices=98):
        r"""The Laplacian must always be the divergence of the gradient,
        whether the Laplacian is combinatorial or normalized, and whether the
        graph is directed or weighted."""
        def test_incidence_nx(graph):
            r"""Test that the incidence matrix corresponds to NetworkX."""
            incidence_pg = np.sign(graph.D.toarray())
            G = nx.OrderedDiGraph if graph.is_directed() else nx.OrderedGraph
            graph_nx = nx.from_scipy_sparse_matrix(graph.W, create_using=G)
            incidence_nx = nx.incidence_matrix(graph_nx, oriented=True)
            np.testing.assert_equal(incidence_pg, incidence_nx.toarray())
        for graph in [graphs.Graph(np.zeros((n_vertices, n_vertices))),
                      graphs.Graph(np.identity(n_vertices)),
                      graphs.Graph([[0, 0.8], [0.8, 0]]),
                      graphs.Graph([[1.3, 0], [0.4, 0.5]]),
                      graphs.ErdosRenyi(n_vertices, directed=False, seed=42),
                      graphs.ErdosRenyi(n_vertices, directed=True, seed=42)]:
            for lap_type in ['combinatorial', 'normalized']:
                graph.compute_laplacian(lap_type)
                graph.compute_differential_operator()
                L = graph.D.dot(graph.D.T)
                np.testing.assert_allclose(L.toarray(), graph.L.toarray())
                test_incidence_nx(graph) 
Example 9
Project: EDeN   Author: fabriziocosta   File: __init__.py    License: MIT License 5 votes vote down vote up
def draw_adjacency_graph(adjacency_matrix,
                         node_color=None,
                         size=10,
                         layout='graphviz',
                         prog='neato',
                         node_size=80,
                         colormap='autumn'):
    """draw_adjacency_graph."""
    graph = nx.from_scipy_sparse_matrix(adjacency_matrix)

    plt.figure(figsize=(size, size))
    plt.grid(False)
    plt.axis('off')

    if layout == 'graphviz':
        pos = nx.graphviz_layout(graph, prog=prog)
    else:
        pos = nx.spring_layout(graph)

    if len(node_color) == 0:
        node_color = 'gray'
    nx.draw_networkx_nodes(graph, pos,
                           node_color=node_color,
                           alpha=0.6,
                           node_size=node_size,
                           cmap=plt.get_cmap(colormap))
    nx.draw_networkx_edges(graph, pos, alpha=0.5)
    plt.show()


# draw a whole set of graphs:: 
Example 10
Project: news-popularity-prediction   Author: MKLab-ITI   File: comment_tree.py    License: Apache License 2.0 5 votes vote down vote up
def calculate_max_depth(comment_tree):
    comment_tree_nx = nx.from_scipy_sparse_matrix(comment_tree, create_using=nx.Graph())

    if len(comment_tree_nx) == 0:
        max_depth = 0.0
    else:
        node_to_depth = nx.shortest_path_length(comment_tree_nx, 0)
        max_depth = max(node_to_depth.values())

    return max_depth 
Example 11
Project: news-popularity-prediction   Author: MKLab-ITI   File: comment_tree.py    License: Apache License 2.0 5 votes vote down vote up
def calculate_avg_depth(comment_tree):
    comment_tree_nx = nx.from_scipy_sparse_matrix(comment_tree, create_using=nx.Graph())

    if len(comment_tree_nx) == 0:
        avg_depth = 0.0
    else:
        node_to_depth = nx.shortest_path_length(comment_tree_nx, 0)
        avg_depth = statistics.mean(node_to_depth.values())

    return avg_depth 
Example 12
Project: news-popularity-prediction   Author: MKLab-ITI   File: comment_tree.py    License: Apache License 2.0 5 votes vote down vote up
def calculate_max_width(comment_tree):
    comment_tree_nx = nx.from_scipy_sparse_matrix(comment_tree, create_using=nx.Graph())

    if len(comment_tree_nx) == 0:
        max_width = 1.0
    else:
        node_to_depth = nx.shortest_path_length(comment_tree_nx, 0)
        depth_to_nodecount = collections.defaultdict(int)

        for k, v in node_to_depth.items():
            depth_to_nodecount[v] += 1

        max_width = max(depth_to_nodecount.values())

    return max_width 
Example 13
Project: news-popularity-prediction   Author: MKLab-ITI   File: comment_tree.py    License: Apache License 2.0 5 votes vote down vote up
def calculate_avg_width(comment_tree):
    comment_tree_nx = nx.from_scipy_sparse_matrix(comment_tree, create_using=nx.Graph())

    if len(comment_tree_nx) == 0:
        avg_width = 1.0
    else:
        node_to_depth = nx.shortest_path_length(comment_tree_nx, 0)
        depth_to_nodecount = collections.defaultdict(int)

        for k, v in node_to_depth.items():
            depth_to_nodecount[v] += 1

        avg_width = statistics.mean(depth_to_nodecount.values())

    return avg_width 
Example 14
Project: skan   Author: jni   File: draw.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def overlay_skeleton_networkx(csr_graph, coordinates, *, axis=None,
                              image=None, cmap=None, **kwargs):
    """Draw the skeleton as a NetworkX graph, optionally overlaid on an image.

    Due to the size of NetworkX drawing elements, this is only recommended
    for very small skeletons.

    Parameters
    ----------
    csr_graph : SciPy Sparse matrix
        The skeleton graph in SciPy CSR format.
    coordinates : array, shape (N_points, 2)
        The coordinates of each point in the skeleton. ``coordinates.shape[0]``
        should be equal to ``csr_graph.shape[0]``.

    Other Parameters
    ----------------
    axis : Matplotlib Axes object, optional
        The Axes on which to plot the data. If None, a new figure and axes will
        be created.
    image : array, shape (M, N[, 3])
        An image on which to overlay the skeleton. ``image.shape`` should be
        greater than ``np.max(coordinates, axis=0)``.
    **kwargs : keyword arguments
        Arguments passed on to `nx.draw_networkx`. Particularly useful ones
        include ``node_size=`` and ``font_size=``.
    """
    if axis is None:
        _, axis = plt.subplots()
    if image is not None:
        cmap = cmap or 'gray'
        axis.imshow(image, cmap=cmap)
    gnx = nx.from_scipy_sparse_matrix(csr_graph)
    # Note: we invert the positions because Matplotlib uses x/y for
    # scatterplot, but the coordinates are row/column NumPy indexing
    positions = dict(zip(range(coordinates.shape[0]), coordinates[:, ::-1]))
    _clean_positions_dict(positions, gnx)  # remove nodes not in Graph
    nx.draw_networkx(gnx, pos=positions, ax=axis, **kwargs)
    return axis 
Example 15
Project: nodevectors   Author: VHRanger   File: graph_eval.py    License: MIT License 5 votes vote down vote up
def make_blogcatalog(edgelist="../data/blogcatalog.mat",
                    dedupe=True):
    """
    Graph with cluster labels from blogcatalog
    
    Dedupe: Whether to deduplicate results (else some nodes have multilabels)
    """
    mat = scipy.io.loadmat(edgelist)
    nodes = mat['network'].tocsr()
    groups = mat['group']
    G = nx.from_scipy_sparse_matrix(nodes)
    labels = (
        pd.DataFrame(groups.todense())
        .idxmax(axis=1)
        .reset_index(drop=False)
    )
    labels.columns = ['node', 'label']
    labels.node = labels.node.astype(int)
    if dedupe:
        labels = labels.loc[~labels.node.duplicated()
                        ].reset_index(drop=True)
        labels.label = labels.label.astype(int) - 1
        return G, labels
    else:
        df = pd.DataFrame(groups.todense())
        labels_list = df.apply(lambda row: list((row.loc[row > 0]).index), axis=1)
        return G, pd.DataFrame({'node': list(G), 'mlabels': pd.Series(labels_list)}) 
Example 16
Project: DeepRobust   Author: DSE-MSU   File: test_nipa.py    License: MIT License 5 votes vote down vote up
def init_setup():
    data = Dataset(root='/tmp/', name=args.dataset, setting='nettack')
    injecting_nodes(data)

    adj, features, labels = data.adj, data.features, data.labels

    StaticGraph.graph = nx.from_scipy_sparse_matrix(adj)
    dict_of_lists = nx.to_dict_of_lists(StaticGraph.graph)

    idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
    device = torch.device('cuda') if args.ctx == 'gpu' else 'cpu'

    # gray box setting
    adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False, sparse=True, device=device)
    # Setup victim model
    victim_model = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1,
                    nhid=16, dropout=0.5, weight_decay=5e-4, device=device)

    victim_model = victim_model.to(device)
    victim_model.fit(features, adj, labels, idx_train, idx_val)
    setattr(victim_model, 'norm_tool',  GraphNormTool(normalize=True, gm='gcn', device=device))

    output = victim_model.predict(features, adj)
    loss_test = F.nll_loss(output[idx_test], labels[idx_test])
    acc_test = accuracy(output[idx_test], labels[idx_test])
    print("Test set results:",
          "loss= {:.4f}".format(loss_test.item()),
          "accuracy= {:.4f}".format(acc_test.item()))

    return features, labels, idx_train, idx_val, idx_test, victim_model, dict_of_lists, adj 
Example 17
Project: GPF   Author: xchadesi   File: until.py    License: MIT License 5 votes vote down vote up
def mat_to_nxG(mat):
    g = nx.from_scipy_sparse_matrix(mat)
    return g 
Example 18
Project: PyTLDR   Author: jaijuneja   File: textrank.py    License: GNU General Public License v3.0 5 votes vote down vote up
def summarize(self, text, length=5, weighting='frequency', norm=None):
        """
        Implements the TextRank summarization algorithm, which follows closely to the PageRank algorithm for ranking
        web pages.

        :param text: a string of text to be summarized, path to a text file, or URL starting with http
        :param length: the length of the output summary; either a number of sentences (e.g. 5) or a percentage
        of the original document (e.g. 0.5)
        :param weighting: 'frequency', 'binary' or 'tfidf' weighting of sentence terms ('frequency' by default)
        :param norm: if 'l1' or 'l2', normalizes words by the length of their associated sentence to "down-weight"
        the voting power of long sentences (None by default)
        :return: list of sentences for the summary
        """

        text = self._parse_input(text)

        sentences, unprocessed_sentences = self._tokenizer.tokenize_sentences(text)

        length = self._parse_summary_length(length, len(sentences))
        if length == len(sentences):
            return unprocessed_sentences

        # Compute the word frequency matrix. If norm is set to 'l1' or 'l2' then words are normalized
        # by the length of their associated sentences (such that each vector of sentence terms sums to 1).
        word_matrix = self._compute_matrix(sentences, weighting=weighting, norm=norm)

        # Build the similarity graph by calculating the number of overlapping words between all
        # combinations of sentences.
        similarity_matrix = (word_matrix * word_matrix.T)

        similarity_graph = networkx.from_scipy_sparse_matrix(similarity_matrix)
        scores = networkx.pagerank(similarity_graph)

        ranked_sentences = sorted(
            ((score, ndx) for ndx, score in scores.items()), reverse=True
        )

        top_sentences = [ranked_sentences[i][1] for i in range(length)]
        top_sentences.sort()

        return [unprocessed_sentences[i] for i in top_sentences] 
Example 19
Project: NPHard   Author: intel-isl   File: reduce_lib.py    License: MIT License 5 votes vote down vote up
def reduce_graph(self, adj):
        # g = nx.from_scipy_sparse_matrix(adj)
        n_nodes, n_edges, e_froms, e_tos = self.__CtypeAdj(adj)
        reduced_node = (ctypes.c_int * (n_nodes))()
        new_n_nodes = ctypes.c_int()
        new_n_edges = ctypes.c_int()
        reduced_xadj = (ctypes.c_int * (n_nodes+1))()
        reduced_adjncy = (ctypes.c_int * (2*n_edges))()
        mapping = (ctypes.c_int * (n_nodes))()
        reverse_mapping = (ctypes.c_int * (n_nodes))()
        crt_is_size = self.lib.Reduce(n_nodes, n_edges, e_froms, e_tos, reduced_node,
                                      ctypes.byref(new_n_nodes), ctypes.byref(new_n_edges),
                                      reduced_xadj, reduced_adjncy, mapping, reverse_mapping)
        # crt_is_size = self.lib.Reduce(n_nodes, n_edges, e_froms, e_tos, reduced_node)
        new_n_nodes = new_n_nodes.value
        new_n_edges = new_n_edges.value
        reduced_node = np.asarray(reduced_node[:])
        reduced_xadj = np.asarray(reduced_xadj[:])
        reduced_xadj = reduced_xadj[:new_n_nodes+1]
        reduced_adjncy = np.asarray(reduced_adjncy[:])
        reduced_adjncy = reduced_adjncy[:new_n_edges]
        mapping = np.asarray(mapping[:])
        reverse_mapping = np.asarray(reverse_mapping[:])
        reverse_mapping = reverse_mapping[:new_n_nodes]
        reduced_adj = sp.csr_matrix((np.ones(new_n_edges), reduced_adjncy, reduced_xadj), shape=[new_n_nodes, new_n_nodes])
        return reduced_node, reduced_adj, mapping, reverse_mapping, crt_is_size
        # return reduced_node[:], crt_is_size 
Example 20
Project: chainer-chemistry   Author: chainer   File: reddit.py    License: MIT License 5 votes vote down vote up
def reddit_to_networkx(dirpath):
    print("Loading graph data")
    coo_adj = scipy.sparse.load_npz(os.path.join(dirpath, edge_file_name))
    G = nx.from_scipy_sparse_matrix(coo_adj)

    print("Loading node feature and label")
    # node feature, edge label
    reddit_data = numpy.load(os.path.join(dirpath, feat_file_name))
    G.graph['x'] = reddit_data['feature'].astype(numpy.float32)
    G.graph['y'] = reddit_data['label'].astype(numpy.int32)

    G.graph['label_num'] = 41
    # G = nx.convert_node_labels_to_integers(G)
    print("Finish loading graph: {}".format(dirpath))
    return G 
Example 21
Project: reveal-graph-embedding   Author: MKLab-ITI   File: competing_methods.py    License: Apache License 2.0 5 votes vote down vote up
def louvain(adjacency_matrix):
    """
    Performs community embedding using the LOUVAIN method.

    Introduced in: Blondel, V. D., Guillaume, J. L., Lambiotte, R., & Lefebvre, E. (2008).
                   Fast unfolding of communities in large networks.
                   Journal of Statistical Mechanics: Theory and Experiment, 2008(10), P10008.

    Inputs:  - A in R^(nxn): Adjacency matrix of an undirected network represented as a SciPy Sparse COOrdinate matrix.

    Outputs: - X in R^(nxC_n): The latent space embedding represented as a SciPy Sparse COOrdinate matrix.
    """
    # Convert to networkx undirected graph.
    adjacency_matrix = nx.from_scipy_sparse_matrix(adjacency_matrix, create_using=nx.Graph())

    # Call LOUVAIN algorithm to calculate a hierarchy of communities.
    tree = community.generate_dendogram(adjacency_matrix, part_init=None)

    # Embed communities
    row = list()
    col = list()
    append_row = row.append
    append_col = col.append

    community_counter = 0
    for i in range(len(tree)):
        partition = community.partition_at_level(tree, i)
        for n, c in partition.items():
            append_row(n)
            append_col(community_counter + c)

        community_counter += max(partition.values()) + 1

    row = np.array(row)
    col = np.array(col)
    data = np.ones(row.size, dtype=np.float64)

    louvain_features = sparse.coo_matrix((data, (row, col)), shape=(len(partition.keys()), community_counter),
                                         dtype=np.float64)

    return louvain_features 
Example 22
Project: reveal-graph-embedding   Author: MKLab-ITI   File: implicit.py    License: Apache License 2.0 5 votes vote down vote up
def get_stationary_distribution_directed(adjacency_matrix, rho):

    graph_nx = nx.from_scipy_sparse_matrix(adjacency_matrix, create_using=nx.DiGraph())

    stationary_distribution = pagerank_scipy(graph_nx,
                                             alpha=1-rho,
                                             personalization=None,
                                             max_iter=200,
                                             tol=1.0e-7,
                                             weight="weight",
                                             dangling=None)

    stationary_distribution = np.array([stationary_distribution[k] for k in sorted(stationary_distribution.keys())])

    return stationary_distribution 
Example 23
Project: gcnn-survey-paper   Author: google   File: link_prediction_utils.py    License: Apache License 2.0 5 votes vote down vote up
def adamic_adar(self):
    """Computes adamic adar scores."""
    graph = nx.from_scipy_sparse_matrix(self.adj_matrix)
    scores = nx.adamic_adar_index(graph)
    return scores 
Example 24
Project: gcnn-survey-paper   Author: google   File: link_prediction_utils.py    License: Apache License 2.0 5 votes vote down vote up
def jaccard_coeff(self):
    """Computes Jaccard coefficients."""
    graph = nx.from_scipy_sparse_matrix(self.adj_matrix)
    coeffs = nx.jaccard_coefficient(graph)
    return coeffs 
Example 25
Project: qgisSpaceSyntaxToolkit   Author: SpaceGroupUCL   File: test_convert_scipy.py    License: GNU General Public License v3.0 5 votes vote down vote up
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) 
Example 26
Project: qgisSpaceSyntaxToolkit   Author: SpaceGroupUCL   File: test_convert_scipy.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_shape(self):
        "Conversion from non-square sparse array."
        A = sp.sparse.lil_matrix([[1,2,3],[4,5,6]])
        assert_raises(nx.NetworkXError, nx.from_scipy_sparse_matrix, A) 
Example 27
Project: qgisSpaceSyntaxToolkit   Author: SpaceGroupUCL   File: test_convert_scipy.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_from_scipy_sparse_matrix_parallel_edges(self):
        """Tests that the :func:`networkx.from_scipy_sparse_matrix` function
        interprets integer weights as the number of parallel edges when
        creating a multigraph.

        """
        A = sparse.csr_matrix([[1, 1], [1, 2]])
        # First, with a simple graph, each integer entry in the adjacency
        # matrix is interpreted as the weight of a single edge in the graph.
        expected = nx.DiGraph()
        edges = [(0, 0), (0, 1), (1, 0)]
        expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
        expected.add_edge(1, 1, weight=2)
        actual = nx.from_scipy_sparse_matrix(A, parallel_edges=True,
                                             create_using=nx.DiGraph())
        assert_graphs_equal(actual, expected)
        actual = nx.from_scipy_sparse_matrix(A, parallel_edges=False,
                                             create_using=nx.DiGraph())
        assert_graphs_equal(actual, expected)
        # Now each integer entry in the adjacency matrix is interpreted as the
        # number of parallel edges in the graph if the appropriate keyword
        # argument is specified.
        edges = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 1)]
        expected = nx.MultiDiGraph()
        expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
        actual = nx.from_scipy_sparse_matrix(A, parallel_edges=True,
                                             create_using=nx.MultiDiGraph())
        assert_graphs_equal(actual, expected)
        expected = nx.MultiDiGraph()
        expected.add_edges_from(set(edges), weight=1)
        # The sole self-loop (edge 0) on vertex 1 should have weight 2.
        expected[1][1][0]['weight'] = 2
        actual = nx.from_scipy_sparse_matrix(A, parallel_edges=False,
                                             create_using=nx.MultiDiGraph())
        assert_graphs_equal(actual, expected) 
Example 28
Project: qgisSpaceSyntaxToolkit   Author: SpaceGroupUCL   File: test_convert_scipy.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_symmetric(self):
        """Tests that a symmetric matrix has edges added only once to an
        undirected multigraph when using
        :func:`networkx.from_scipy_sparse_matrix`.

        """
        A = sparse.csr_matrix([[0, 1], [1, 0]])
        G = nx.from_scipy_sparse_matrix(A, create_using=nx.MultiGraph())
        expected = nx.MultiGraph()
        expected.add_edge(0, 1, weight=1)
        assert_graphs_equal(G, expected) 
Example 29
Project: Carnets   Author: holzschu   File: test_convert_scipy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def identity_conversion(self, G, A, create_using):
        GG = nx.from_scipy_sparse_matrix(A, create_using=create_using)
        self.assert_isomorphic(G, GG)

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

        GI = nx.empty_graph(0, create_using).__class__(A)
        self.assert_isomorphic(G, GI)

        ACSR = A.tocsr()
        GI = nx.empty_graph(0, create_using).__class__(ACSR)
        self.assert_isomorphic(G, GI)

        ACOO = A.tocoo()
        GI = nx.empty_graph(0, create_using).__class__(ACOO)
        self.assert_isomorphic(G, GI)

        ACSC = A.tocsc()
        GI = nx.empty_graph(0, create_using).__class__(ACSC)
        self.assert_isomorphic(G, GI)

        AD = A.todense()
        GI = nx.empty_graph(0, create_using).__class__(AD)
        self.assert_isomorphic(G, GI)

        AA = A.toarray()
        GI = nx.empty_graph(0, create_using).__class__(AA)
        self.assert_isomorphic(G, GI) 
Example 30
Project: Carnets   Author: holzschu   File: test_convert_scipy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_shape(self):
        "Conversion from non-square sparse array."
        A = sp.sparse.lil_matrix([[1, 2, 3], [4, 5, 6]])
        assert_raises(nx.NetworkXError, nx.from_scipy_sparse_matrix, A)