.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/nx_to_grakel.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_nx_to_grakel.py: ========================================================= Example of transforming NetworkX graphs to GraKeL graphs. ========================================================= .. GENERATED FROM PYTHON SOURCE LINES 6-66 .. code-block:: Python from __future__ import print_function print(__doc__) import networkx as nx import numpy as np from grakelx.utils import graph_from_networkx # Creates a list of two simple graphs G1 = nx.Graph() G1.add_nodes_from([0, 1, 2]) G1.add_edges_from([(0, 1), (1, 2)]) G2 = nx.Graph() G2.add_nodes_from([0, 1, 2]) G2.add_edges_from([(0, 1), (0, 2), (1, 2)]) G_nx = [G1, G2] # Transforms list of NetworkX graphs into a list of GraKeL graphs G = graph_from_networkx(G_nx) print("1 - Simple graphs transformed\n") # Creates a list of two node-labeled graphs G1 = nx.Graph() G1.add_nodes_from([0, 1, 2]) G1.add_edges_from([(0, 1), (1, 2)]) nx.set_node_attributes(G1, {0: "a", 1: "b", 2: "a"}, "label") G2 = nx.Graph() G2.add_nodes_from([0, 1, 2]) G2.add_edges_from([(0, 1), (0, 2), (1, 2)]) nx.set_node_attributes(G2, {0: "a", 1: "b", 2: "c"}, "label") G_nx = [G1, G2] # Transforms list of NetworkX graphs into a list of GraKeL graphs G = graph_from_networkx(G_nx, node_labels_tag="label") print("2 - Node-labeled graphs transformed\n") # Creates a list of two node-attributed graphs G1 = nx.Graph() G1.add_nodes_from([0, 1, 2]) G1.add_edges_from([(0, 1), (1, 2)]) nx.set_node_attributes(G1, {0: np.array([1.1, 0.8]), 1: np.array([0.2, -0.3]), 2: np.array([0.9, 1.0])}, "attributes") G2 = nx.Graph() G2.add_nodes_from([0, 1, 2]) G2.add_edges_from([(0, 1), (0, 2), (1, 2)]) nx.set_node_attributes(G2, {0: np.array([1.8, 0.5]), 1: np.array([-0.1, 0.2]), 2: np.array([2.3, 1.2])}, "attributes") G_nx = [G1, G2] # Transforms list of NetworkX graphs into a list of GraKeL graphs G = graph_from_networkx(G_nx, node_labels_tag="attributes") print("3 - Node-attributed graphs transformed") .. _sphx_glr_download_auto_examples_nx_to_grakel.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: nx_to_grakel.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: nx_to_grakel.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: nx_to_grakel.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_