Last active
August 14, 2024 09:12
-
-
Save eriknw/e1e9c9ceea3aa09855c8d1bc69d5d8bf to your computer and use it in GitHub Desktop.
Fallback to NetworkX examples for https://github.com/networkx/networkx/pull/7585
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "edcc97a6-fab5-4f73-8c6a-fba3c6e1786f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import contextlib\n", | |
"import logging\n", | |
"import sys\n", | |
"import warnings\n", | |
"\n", | |
"import networkx as nx\n", | |
"import graphblas_algorithms as ga\n", | |
"import nx_pandas_graph as nxpdg\n", | |
"import nx_pandas_graph.classes\n", | |
"\n", | |
"# Sometimes it's nice to enable this to see caching log messages\n", | |
"nx.config.cache_converted_graphs = False\n", | |
"\n", | |
"nxl = logging.getLogger(\"networkx\")\n", | |
"nxl.addHandler(logging.StreamHandler())\n", | |
"nxl.setLevel(logging.DEBUG)\n", | |
"\n", | |
"warnings.filterwarnings('ignore', 'Using cached graph', UserWarning, \"networkx.utils.backends\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "18e15012-bef7-4a93-8efb-83df1057fae8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"@contextlib.contextmanager\n", | |
"def raises(exc):\n", | |
" try:\n", | |
" yield\n", | |
" except exc as exc2:\n", | |
" print(f\"{type(exc2).__name__}:\", *exc2.args, file=sys.stderr)\n", | |
" else:\n", | |
" raise RuntimeError(f\"Did not raise {exc}!\")\n", | |
"\n", | |
"G = nx.complete_graph(3)\n", | |
"Ggb = ga.classes.Graph.from_networkx(G)\n", | |
"Gpd = nxpdg.classes.Graph(G)\n", | |
"MG = nx.complete_graph(3, create_using=nx.MultiGraph)\n", | |
"MGgb = ga.classes.MultiGraph.from_networkx(MG)\n", | |
"MGpd = nxpdg.classes.MultiGraph(MG)\n", | |
"Gbad = type(\"Gbad\", (), {\"__networkx_backend__\": \"bad\"})() # An object for a backend that is not installed?!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "a51647d4-6fa8-4066-a379-deb2fcb2008e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `pagerank'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.3333333333333333}" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs backend graph\n", | |
"# Config: run with networkx via `backend=`\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.pagerank(Ggb, backend=\"networkx\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "e5b7ff1d-1bd0-4ffa-bea6-73769c018219", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"`compose' received graphs from multiple backends: {'graphblas', 'pandas_graph'}\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `compose'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.graph.Graph at 0x7f3f7e971820>" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: graphs from multiple backends\n", | |
"# Config: auto-networkx\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\"]\n", | |
"nx.algorithms.binary.compose(Gpd, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "671cbb21-2972-4340-98a3-fb715ff2e0fa", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"`compose' received graphs from multiple backends: {'graphblas', 'pandas_graph'}\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `compose'\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Graph with 3 nodes and 3 edges\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: graphs from multiple backends\n", | |
"# Config: auto-networkx then auto-backend\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\", \"graphblas\"]\n", | |
"print(nx.algorithms.binary.compose(Gpd, Ggb))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "69398b1b-0102-4ec1-8736-fd0bba300c7b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"TypeError: compose() graphs should all be from the same backend, found {'graphblas', 'pandas_graph'}. Automatic conversions between backends may become possible in future releases. Currently, it is only possible to automatically convert from backend graphs to networkx graphs by specifying 'networkx' in `nx.config.backend_priority` or via `backend='networkx'` keyword argument.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: multiple backends\n", | |
"# Config: no auto\n", | |
"# Result: TypeError; suggest how to use backend_priority\n", | |
"\n", | |
"nx.config.backend_priority.compose = []\n", | |
"with raises(TypeError):\n", | |
" nx.algorithms.binary.compose(Gpd, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "bbe28a7f-511a-4c6c-b592-10ca4e356956", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"TypeError: pagerank() is unable to convert graph from backend 'pandas_graph' to the specified backend 'graphblas'. Automatic conversions between backends may become possible in future releases.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph\n", | |
"# Config: run with different backend via `backend=`\n", | |
"# Result: TypeError, because backend-to-backend conversions is not yet supported\n", | |
"\n", | |
"with raises(TypeError):\n", | |
" nx.pagerank(Gpd, backend=\"graphblas\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "39beb7e6-0c07-42eb-8d2a-ea358656564a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"ImportError: 'bad' backend is not installed, but a graph from this backend was used in call to `pagerank. Is 'bad' installed correctly in your environment?\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: unknown backend graph\n", | |
"# Config: N/A\n", | |
"# Result: ImportError; nudge user to check their environment\n", | |
"\n", | |
"with raises(ImportError):\n", | |
" nx.pagerank(Gbad)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "7fd73c79-ae83-4b22-8659-88328d33465c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"`compose' received graphs from networkx and 'graphblas' backend. NetworkX is configured to convert and run with 'networkx'. To automatically convert and run with the backend (if it implements `compose'), add 'graphblas' before 'networkx' in `nx.config.backend_priority`'.\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `compose'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.graph.Graph at 0x7f3f7ef51be0>" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-networkx\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\"]\n", | |
"nx.algorithms.binary.compose(G, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "f9798ffd-5dcb-4aaf-b445-87a47734b321", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"TypeError: Unable to convert inputs and run compose. compose() has networkx and graphblas graphs, but NetworkX is not configured to automatically convert graphs from networkx to graphblas. To enable automatic conversions, set e.g.:\n", | |
" >>> nx.config.backend_priority.generators = [\"graphblas\"]\n", | |
"or:\n", | |
" >>> nx.config.backend_priority.compose = [\"graphblas\"]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: no auto\n", | |
"# Result: TypeError; suggest how to use backend_priority\n", | |
"\n", | |
"nx.config.backend_priority.compose = []\n", | |
"with raises(TypeError):\n", | |
" nx.algorithms.binary.compose(G, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "30a86f94-a965-40d7-8a4c-179946a0e936", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.Graph object at 0x7f3f7e9a0950>, H=<graphblas_algorithms.classes.graph.Graph object at 0x7f3fd81ba8a0>)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<graphblas_algorithms.classes.graph.Graph at 0x7f3f7f0089e0>" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-backend\n", | |
"# Result: convert and run with backend\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"graphblas\"]\n", | |
"nx.algorithms.binary.compose(G, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "e7edbfd5-0333-4db8-b58c-5112e4c5464a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.Graph object at 0x7f3f7f54afc0>, H=<graphblas_algorithms.classes.graph.Graph object at 0x7f3fd81ba8a0>)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<graphblas_algorithms.classes.graph.Graph at 0x7f3f7e971cd0>" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# (Same as above, but configured to fall back to networkx after backends)\n", | |
"\n", | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-backend then auto-networkx\n", | |
"# Result: convert and run with backend\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"graphblas\", \"networkx\"]\n", | |
"nx.algorithms.binary.compose(G, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "67444a08-8408-47bf-a835-600380166819", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"`compose' received graphs from networkx and 'graphblas' backend. NetworkX is configured to convert and run with 'networkx'. To automatically convert and run with the backend (if it implements `compose'), add 'graphblas' before 'networkx' in `nx.config.backend_priority`'.\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `compose'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.graph.Graph at 0x7f3f7e9a1850>" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-networkx then auto-backend\n", | |
"# Result: convert and run with networkx!\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\", \"graphblas\"]\n", | |
"nx.algorithms.binary.compose(G, Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "9b82600b-e22b-441d-aa7f-e5df5895313e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `pagerank' with arguments: (G=<graphblas_algorithms.classes.graph.Graph object at 0x7f3fd81ba8a0>, alpha=0.85, personalization=None, max_iter=100, tol=1e-06, nstart=None, weight='weight', dangling=None)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<graphblas_algorithms.classes.nodemap.NodeMap at 0x7f3f7f528080>" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph\n", | |
"# Config: N/A\n", | |
"# Result: run with backend\n", | |
"\n", | |
"nx.pagerank(Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "38835d84-d553-44eb-b6e6-829cb6e175bf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7ef5d2e0>, H=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7ef5d3d0>)\n", | |
"NotImplementedError: Backend 'graphblas' raised NotImplementedError when calling `compose'. Unable to fall back to networkx, because `backend='graphblas'` keyword argument was given, and 'networkx' is not in `nx.config.backend_priority`.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: run with backend via `backend=`; no auto\n", | |
"# Action: backend raises NotImplementedError\n", | |
"# Result: NotImplementedError; suggest how to use backend_priority\n", | |
"\n", | |
"nx.config.backend_priority.compose = []\n", | |
"with raises(NotImplementedError):\n", | |
" nx.algorithms.binary.compose(MG, MGgb, backend=\"graphblas\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "4e4a7827-3698-4d0d-82d5-ba9c4c8893d6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7e972c60>, H=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7ef5d3d0>)\n", | |
"NotImplementedError: Backend 'graphblas' raised NotImplementedError when calling `compose'. Unable to fall back to networkx, because `backend='graphblas'` keyword argument was given.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: run with backend via `backend=`; auto-networkx\n", | |
"# Action: backend raises NotImplementedError\n", | |
"# Result: NotImplementedError\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\"]\n", | |
"with raises(NotImplementedError):\n", | |
" nx.algorithms.binary.compose(MG, MGgb, backend=\"graphblas\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"id": "8787321b-10f8-451d-9f0f-cf1151eef6a6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7e9a3830>, H=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7ef5d3d0>)\n", | |
"NotImplementedError: Backend 'graphblas' raised NotImplementedError when calling `compose', and NetworkX is not configured to automatically convert to and run with networkx. To enable automatic conversions from backend graphs to networkx graphs, add 'networkx' to `nx.config.backend_priority.generators` or `nx.config.backend_priority.compose` list of backend names.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-backend\n", | |
"# Action: backend raises NotImplementedError\n", | |
"# Result: NotImplementedError; suggest how to use backend_priority\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"graphblas\"]\n", | |
"with raises(NotImplementedError):\n", | |
" nx.algorithms.binary.compose(MG, MGgb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"id": "c706e7db-6360-46ca-9565-4463ef525575", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7f9f4f20>, H=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7ef5d3d0>)\n", | |
"Backend 'graphblas' raised NotImplementedError when calling `compose'.\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `compose'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.multigraph.MultiGraph at 0x7f3f7e9c48c0>" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-backend then auto-networkx\n", | |
"# Action: backend raises NotImplementedError\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"graphblas\", \"networkx\"]\n", | |
"nx.algorithms.binary.compose(MG, MGgb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"id": "ff9fe803-bb73-4473-b468-97e3ce0b5f33", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"`compose' received graphs from networkx and 'graphblas' backend. NetworkX is configured to convert and run with 'networkx'. To automatically convert and run with the backend (if it implements `compose'), add 'graphblas' before 'networkx' in `nx.config.backend_priority`'.\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `compose'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.multigraph.MultiGraph at 0x7f3f7e9a3980>" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# (similar to above, but different backend priorities)\n", | |
"\n", | |
"# Inputs: backend graph and networkx graph\n", | |
"# Config: auto-networkx then auto-backend\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\", \"graphblas\"]\n", | |
"nx.algorithms.binary.compose(MG, MGgb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "ed6955ff-a313-4599-bc81-a20ac75a8ff9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"NotImplementedError: `betweenness_centrality' is not implemented by 'graphblas' backend.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph\n", | |
"# Config: run with backend via `backend=`\n", | |
"# Context: can_run=False\n", | |
"# Result: NotImplementedError\n", | |
"\n", | |
"with raises(NotImplementedError):\n", | |
" nx.betweenness_centrality(Ggb, backend=\"graphblas\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "3fc6bc62-1e83-4259-939f-fb0902da9d77", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Backend 'graphblas' does not implement `betweenness_centrality'\n", | |
"NotImplementedError: `betweenness_centrality' is not implemented by 'graphblas' backend, and NetworkX is not configured to automatically convert to and run with networkx. To enable automatic conversions from backend graphs to networkx graphs, add 'networkx' to `nx.config.backend_priority.algos` or `nx.config.backend_priority.betweenness_centrality` list of backend names.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph\n", | |
"# Config: no auto\n", | |
"# Context: can_run=False\n", | |
"# Result: NotImplementedError\n", | |
"\n", | |
"nx.config.backend_priority.algos = []\n", | |
"with raises(NotImplementedError):\n", | |
" nx.betweenness_centrality(Ggb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "0b7bf874-70b9-4cc9-8003-2faccd83147c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Backend 'graphblas' does not implement `contracted_nodes'\n", | |
"`contracted_nodes' will mutate an input graph. This prevents automatic conversion to, and use of, backends listed in `nx.config.backend_priority`. You may specify a backend to use by passing the `backend=` keyword argument, but this may change behavior by not mutating inputs.\n", | |
"RuntimeError: Unable to convert input backend graphs to run with 'networkx', because `contracted_nodes' will mutate an input. You may specify a backend to use by passing e.g. `backend='networkx'` keyword, but this may change behavior by not mutating inputs.\n" | |
] | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph\n", | |
"# Config: auto-networkx\n", | |
"# Context: can_run=False, call will mutate graph\n", | |
"# Result: RuntimeError\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\"]\n", | |
"with raises(RuntimeError):\n", | |
" nx.contracted_nodes(Ggb, 0, 1, copy=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "9b2121f6-1908-451e-92ee-1a01e26681de", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Backend 'graphblas' does not implement `contracted_nodes'\n", | |
"Converting input graphs to networkx graphs to run with the default networkx implementation of `contracted_nodes'\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.graph.Graph at 0x7f3f7fee2930>" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: backend graph\n", | |
"# Config: auto-networkx\n", | |
"# Context: can_run=False\n", | |
"# Result: convert and run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"networkx\"]\n", | |
"nx.contracted_nodes(Ggb, 0, 1, copy=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"id": "24c772b0-9a93-4c6d-a20a-0a4a38dc9970", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{0: 0.3333333333333333, 1: 0.3333333333333333, 2: 0.3333333333333333}" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: networkx graph\n", | |
"# Config: run with networkx via `backend=`\n", | |
"# Result: run with networkx\n", | |
"\n", | |
"nx.pagerank(G, backend=\"networkx\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "981fce9d-e917-406d-bdaf-27d9d6b64458", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `pagerank' with arguments: (G=<graphblas_algorithms.classes.graph.Graph object at 0x7f3f7e9c50a0>, alpha=0.85, personalization=None, max_iter=100, tol=1e-06, nstart=None, weight='weight', dangling=None)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<graphblas_algorithms.classes.nodemap.NodeMap at 0x7f3f7efec7a0>" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: networkx graph\n", | |
"# Config: run with backend via `backend=`\n", | |
"# Result: run with backend\n", | |
"\n", | |
"nx.pagerank(G, backend=\"graphblas\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"id": "f9b39559-5a97-4db0-82bb-a2d117b6a69a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Backend 'graphblas' does not implement `betweenness_centrality'\n", | |
"Trying next backend...\n", | |
"Using default 'networkx' for call to `betweenness_centrality' with arguments: (G=<networkx.classes.graph.Graph object at 0x7f3f93f6ea20>, k=None, normalized=True, weight=None, endpoints=False, seed=<random.Random object at 0x55cabb852f90>)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{0: 0.0, 1: 0.0, 2: 0.0}" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: networkx graph\n", | |
"# Config: auto-backend then auto-networkx\n", | |
"# Context: can_run=False\n", | |
"# Result: run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.algos = [\"graphblas\", \"networkx\"]\n", | |
"nx.betweenness_centrality(G)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"id": "be573fb9-97a3-4b66-b544-e39fed148e14", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{0: 0.0, 1: 0.0, 2: 0.0}" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: networkx graph\n", | |
"# Config: auto-networkx then auto-backend\n", | |
"# Result: run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.algos = [\"networkx\", \"graphblas\"]\n", | |
"nx.betweenness_centrality(G)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "7aea3dc2-180d-4fc6-bede-51fda372118d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `pagerank' with arguments: (G=<graphblas_algorithms.classes.graph.Graph object at 0x7f3f7e9c5f40>, alpha=0.85, personalization=None, max_iter=100, tol=1e-06, nstart=None, weight='weight', dangling=None)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<graphblas_algorithms.classes.nodemap.NodeMap at 0x7f3f7f528140>" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: networkx graph\n", | |
"# Config: auto-backend then auto-networkx\n", | |
"# Result: run with backend\n", | |
"\n", | |
"nx.config.backend_priority.algos = [\"graphblas\", \"networkx\"]\n", | |
"nx.pagerank(G)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "751a312e-5aff-43c7-b4fc-26f83ea9b962", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using backend 'graphblas' for call to `compose' with arguments: (G=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3ff814d280>, H=<graphblas_algorithms.classes.graph.MultiGraph object at 0x7f3f7e9c5250>)\n", | |
"Backend 'graphblas' raised NotImplementedError when calling `compose'. Trying next backend...\n", | |
"Using default 'networkx' for call to `compose' with arguments: (G=<networkx.classes.multigraph.MultiGraph object at 0x7f3f7f00a000>, H=<networkx.classes.multigraph.MultiGraph object at 0x7f3f7f00a000>)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<networkx.classes.multigraph.MultiGraph at 0x7f3f7e9c6750>" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Inputs: networkx graph\n", | |
"# Config: auto-backend\n", | |
"# Action: backend raises NotImplementedError\n", | |
"# Result: run with networkx\n", | |
"\n", | |
"nx.config.backend_priority.compose = [\"graphblas\"]\n", | |
"nx.algorithms.binary.compose(MG, MG)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.12.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment