def test_attr_sparse_matrix_directed(): G = nx.DiGraph() G.add_edge(0, 1, thickness=1, weight=3) G.add_edge(0, 1, thickness=1, weight=3) G.add_edge(0, 2, thickness=2) G.add_edge(1, 2, thickness=3) M = nx.attr_sparse_matrix(G, rc_order=[0, 1, 2]) data = np.array([[0., 1., 1.], [0., 0., 1.], [0., 0., 0.]]) npt.assert_equal(M.todense(), np.array(data))
def get_aggregation_data(self, network, aggr='sum', normalized=False): assert aggr in ['sum', 'mean'], NotImplementedError aggregation_data = nx.attr_sparse_matrix( network, edge_attr=self.name, normalized=normalized, rc_order=network.nodes) if aggr == 'sum': aggregation_data = aggregation_data.sum(axis=0) elif aggr == 'mean': aggregation_data = aggregation_data.mean(axis=0) aggregation_data = np.asarray(aggregation_data)[0] return aggregation_data
def nx_to_adj(graphs): """ Converts a list of nx.Graphs to a rank 3 np.array of adjacency matrices of shape `(num_graphs, num_nodes, num_nodes)`. :param graphs: a nx.Graph, or list of nx.Graphs. :return: a rank 3 np.array of adjacency matrices. """ if isinstance(graphs, nx.Graph): graphs = [graphs] return np.array([nx.attr_sparse_matrix(g)[0].toarray() for g in graphs])
def test_attr_sparse_matrix(): pytest.importorskip('scipy') G = nx.Graph() G.add_edge(0, 1, thickness=1, weight=3) G.add_edge(0, 2, thickness=2) G.add_edge(1, 2, thickness=3) M = nx.attr_sparse_matrix(G) mtx = M[0] data = np.ones((3, 3), float) np.fill_diagonal(data, 0) npt.assert_equal(mtx.todense(), np.array(data)) assert M[1] == [0, 1, 2]
def test_attr_sparse_matrix_directed(): pytest.importorskip("scipy") G = nx.DiGraph() G.add_edge(0, 1, thickness=1, weight=3) G.add_edge(0, 1, thickness=1, weight=3) G.add_edge(0, 2, thickness=2) G.add_edge(1, 2, thickness=3) M = nx.attr_sparse_matrix(G, rc_order=[0, 1, 2]) # fmt: off data = np.array([[0., 1., 1.], [0., 0., 1.], [0., 0., 0.]]) # fmt: on np.testing.assert_equal(M.todense(), np.array(data))
def random_walk_kernel(g1, g2, parameter_lambda, node_attribute='label'): """ Compute the random walk kernel of two graphs as described by Neuhaus and Bunke in the chapter 5.9 of "Bridging the Gap Between Graph Edit Distance and Kernel Machines (2007)" """ p = nx.cartesian_product(g1, g2) M = nx.attr_sparse_matrix(p, node_attr=node_attribute) A = M[0] L = A.shape[0] k = 0 A_exp = A for n in range(L): k += (parameter_lambda**n) * long(A_exp.sum()) if n < L: A_exp = A_exp * A return k
def random_walk_kernel(g1, g2, parameter_lambda, node_attribute="label"): """ Compute the random walk kernel of two graphs as described by Neuhaus and Bunke in the chapter 5.9 of "Bridging the Gap Between Graph Edit Distance and Kernel Machines (2007)" """ p = nx.cartesian_product(g1, g2) M = nx.attr_sparse_matrix(p, node_attr=node_attribute) A = M[0] L = A.shape[0] k = 0 A_exp = A for n in xrange(L): k += (parameter_lambda ** n) * long(A_exp.sum()) if n < L: A_exp = A_exp * A return k
def nx_to_edge_features(graphs, keys, post_processing=None): """ Converts a list of nx.Graphs to a rank 4 np.array of edge features matrices of shape `(num_graphs, num_nodes, num_nodes, num_features)`. Optionally applies a post-processing function to each attribute in the nx graphs. :param graphs: a nx.Graph, or a list of nx.Graphs; :param keys: a list of keys with which to index edge attributes. :param post_processing: a list of functions with which to post process each attribute associated to a key. `None` can be passed as post-processing function to leave the attribute unchanged. :return: a rank 3 np.array of feature matrices """ if post_processing is not None: if len(post_processing) != len(keys): raise ValueError( 'post_processing must contain an element for each key') for i in range(len(post_processing)): if post_processing[i] is None: post_processing[i] = lambda x: x if isinstance(graphs, nx.Graph): graphs = [graphs] output = [] for g in graphs: edge_features = [] for key in keys: ef = nx.attr_sparse_matrix(g, edge_attr=key)[0].toarray() if ef.ndim == 2: ef = ef[..., None] # Make it three dimensional to concatenate edge_features.append(ef) if post_processing is not None: edge_features = [ op(_) for op, _ in zip(post_processing, edge_features) ] if len(edge_features) > 1: edge_features = np.concatenate(edge_features, axis=-1) else: edge_features = np.array(edge_features[0]) output.append(edge_features) return np.array(output)
def get_adjacency_data(self, network, normalized=False): adjacency_data = nx.attr_sparse_matrix( network, edge_attr=self.name, normalized=normalized, rc_order=network.nodes).T return adjacency_data
def calc_grc_M(self): M = nx.attr_sparse_matrix(self.graph, edge_attr='bw_free', normalized=True, rc_order=self.graph.nodes).T return M