From cdcd520dd1648f879c64afefa3cdbf2c9892b427 Mon Sep 17 00:00:00 2001 From: Erik Welch Date: Tue, 1 Oct 2024 10:14:33 -0700 Subject: [PATCH 1/2] `nx-cugraph`: add `NX_CUGRAPH=True` env var to enable full zero-code change This is for convenience and sets or updates NETWORKX environment variables. --- python/nx-cugraph/_nx_cugraph/__init__.py | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/python/nx-cugraph/_nx_cugraph/__init__.py b/python/nx-cugraph/_nx_cugraph/__init__.py index 428d266dd2e..e5409461a43 100644 --- a/python/nx-cugraph/_nx_cugraph/__init__.py +++ b/python/nx-cugraph/_nx_cugraph/__init__.py @@ -301,6 +301,45 @@ def get_info(): .lower() == "true", } + + # Enable zero-code change usage with a simple environment variable + # by setting or updating other NETWORKX environment variables. + if os.environ.get("NX_CUGRAPH", "").strip().lower() == "true": + from itertools import chain + + def update_env_var(varname): + """Add "cugraph" to a list of backend names environment variable.""" + if varname not in os.environ: + os.environ[varname] = "cugraph" + return + string = os.environ[varname] + vals = [ + stripped for x in string.strip().split(",") if (stripped := x.strip()) + ] + if "cugraph" not in vals: + # Should we append or prepend? Let's be first! + os.environ[varname] = ",".join(chain(["cugraph"], vals)) + + # Automatically convert NetworkX Graphs to nx-cugraph for algorithms + if (varname := "NETWORKX_BACKEND_PRIORITY_ALGOS") in os.environ: + # "*_ALGOS" is given priority in NetworkX >=3.4 + update_env_var(varname) + # But update this too to "just work" if users mix env vars and nx versions + os.environ["NETWORKX_BACKEND_PRIORITY"] = os.environ[varname] + else: + update_env_var("NETWORKX_BACKEND_PRIORITY") + # And for older NetworkX versions + update_env_var("NETWORKX_AUTOMATIC_BACKENDS") # For NetworkX 3.2 + update_env_var("NETWORKX_GRAPH_CONVERT") # For NetworkX 3.0 and 3.1 + # Automatically create nx-cugraph Graph from graph generators + update_env_var("NETWORKX_BACKEND_PRIORITY_GENERATORS") + # Run default NetworkX implementation (in >=3.4) if not implemented by nx-cugraph + if (varname := "NETWORKX_FALLBACK_TO_NX") not in os.environ: + os.environ[varname] = "true" + # Cache graph conversions (default is False in NetworkX 3.2 + if (varname := "NETWORKX_CACHE_CONVERTED_GRAPHS") not in os.environ: + os.environ[varname] = "true" + return d From b313144087cee4eb30a9be7c996d6204f30433c2 Mon Sep 17 00:00:00 2001 From: Erik Welch Date: Tue, 1 Oct 2024 12:24:59 -0700 Subject: [PATCH 2/2] Rename `NX_CUGRAPH` to `NX_CUGRAPH_AUTOCONFIG` --- python/nx-cugraph/_nx_cugraph/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/nx-cugraph/_nx_cugraph/__init__.py b/python/nx-cugraph/_nx_cugraph/__init__.py index e5409461a43..b69dedeb065 100644 --- a/python/nx-cugraph/_nx_cugraph/__init__.py +++ b/python/nx-cugraph/_nx_cugraph/__init__.py @@ -304,7 +304,7 @@ def get_info(): # Enable zero-code change usage with a simple environment variable # by setting or updating other NETWORKX environment variables. - if os.environ.get("NX_CUGRAPH", "").strip().lower() == "true": + if os.environ.get("NX_CUGRAPH_AUTOCONFIG", "").strip().lower() == "true": from itertools import chain def update_env_var(varname):